SplitKit.h revision fc60d7729bb5b63b7d61e370e51bd05e9a18b8bc
1//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains the SplitAnalysis class as well as mutator functions for 11// live range splitting. 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/ADT/SmallPtrSet.h" 16#include "llvm/ADT/DenseMap.h" 17#include "llvm/CodeGen/SlotIndexes.h" 18 19namespace llvm { 20 21class LiveInterval; 22class LiveIntervals; 23class MachineInstr; 24class MachineLoop; 25class MachineLoopInfo; 26class MachineRegisterInfo; 27class TargetInstrInfo; 28class VirtRegMap; 29class VNInfo; 30 31/// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting 32/// opportunities. 33class SplitAnalysis { 34public: 35 const MachineFunction &mf_; 36 const LiveIntervals &lis_; 37 const MachineLoopInfo &loops_; 38 const TargetInstrInfo &tii_; 39 40 // Instructions using the the current register. 41 typedef SmallPtrSet<const MachineInstr*, 16> InstrPtrSet; 42 InstrPtrSet usingInstrs_; 43 44 // The number of instructions using curli in each basic block. 45 typedef DenseMap<const MachineBasicBlock*, unsigned> BlockCountMap; 46 BlockCountMap usingBlocks_; 47 48 // The number of basic block using curli in each loop. 49 typedef DenseMap<const MachineLoop*, unsigned> LoopCountMap; 50 LoopCountMap usingLoops_; 51 52private: 53 // Current live interval. 54 const LiveInterval *curli_; 55 56 // Sumarize statistics by counting instructions using curli_. 57 void analyzeUses(); 58 59 /// canAnalyzeBranch - Return true if MBB ends in a branch that can be 60 /// analyzed. 61 bool canAnalyzeBranch(const MachineBasicBlock *MBB); 62 63public: 64 SplitAnalysis(const MachineFunction &mf, const LiveIntervals &lis, 65 const MachineLoopInfo &mli); 66 67 /// analyze - set curli to the specified interval, and analyze how it may be 68 /// split. 69 void analyze(const LiveInterval *li); 70 71 /// removeUse - Update statistics by noting that mi no longer uses curli. 72 void removeUse(const MachineInstr *mi); 73 74 const LiveInterval *getCurLI() { return curli_; } 75 76 /// clear - clear all data structures so SplitAnalysis is ready to analyze a 77 /// new interval. 78 void clear(); 79 80 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet; 81 typedef SmallPtrSet<const MachineLoop*, 16> LoopPtrSet; 82 83 // Sets of basic blocks surrounding a machine loop. 84 struct LoopBlocks { 85 BlockPtrSet Loop; // Blocks in the loop. 86 BlockPtrSet Preds; // Loop predecessor blocks. 87 BlockPtrSet Exits; // Loop exit blocks. 88 89 void clear() { 90 Loop.clear(); 91 Preds.clear(); 92 Exits.clear(); 93 } 94 }; 95 96 // Calculate the block sets surrounding the loop. 97 void getLoopBlocks(const MachineLoop *Loop, LoopBlocks &Blocks); 98 99 /// LoopPeripheralUse - how is a variable used in and around a loop? 100 /// Peripheral blocks are the loop predecessors and exit blocks. 101 enum LoopPeripheralUse { 102 ContainedInLoop, // All uses are inside the loop. 103 SinglePeripheral, // At most one instruction per peripheral block. 104 MultiPeripheral, // Multiple instructions in some peripheral blocks. 105 OutsideLoop // Uses outside loop periphery. 106 }; 107 108 /// analyzeLoopPeripheralUse - Return an enum describing how curli_ is used in 109 /// and around the Loop. 110 LoopPeripheralUse analyzeLoopPeripheralUse(const LoopBlocks&); 111 112 /// getCriticalExits - It may be necessary to partially break critical edges 113 /// leaving the loop if an exit block has phi uses of curli. Collect the exit 114 /// blocks that need special treatment into CriticalExits. 115 void getCriticalExits(const LoopBlocks &Blocks, BlockPtrSet &CriticalExits); 116 117 /// canSplitCriticalExits - Return true if it is possible to insert new exit 118 /// blocks before the blocks in CriticalExits. 119 bool canSplitCriticalExits(const LoopBlocks &Blocks, 120 BlockPtrSet &CriticalExits); 121 122 /// getBestSplitLoop - Return the loop where curli may best be split to a 123 /// separate register, or NULL. 124 const MachineLoop *getBestSplitLoop(); 125 126 /// getMultiUseBlocks - Add basic blocks to Blocks that may benefit from 127 /// having curli split to a new live interval. Return true if Blocks can be 128 /// passed to SplitEditor::splitSingleBlocks. 129 bool getMultiUseBlocks(BlockPtrSet &Blocks); 130 131 /// getBlockForInsideSplit - If curli is contained inside a single basic block, 132 /// and it wou pay to subdivide the interval inside that block, return it. 133 /// Otherwise return NULL. The returned block can be passed to 134 /// SplitEditor::splitInsideBlock. 135 const MachineBasicBlock *getBlockForInsideSplit(); 136}; 137 138 139/// LiveIntervalMap - Map values from a large LiveInterval into a small 140/// interval that is a subset. Insert phi-def values as needed. This class is 141/// used by SplitEditor to create new smaller LiveIntervals. 142/// 143/// parentli_ is the larger interval, li_ is the subset interval. Every value 144/// in li_ corresponds to exactly one value in parentli_, and the live range 145/// of the value is contained within the live range of the parentli_ value. 146/// Values in parentli_ may map to any number of openli_ values, including 0. 147class LiveIntervalMap { 148 LiveIntervals &lis_; 149 150 // The parent interval is never changed. 151 const LiveInterval &parentli_; 152 153 // The child interval's values are fully contained inside parentli_ values. 154 LiveInterval *li_; 155 156 typedef DenseMap<const VNInfo*, VNInfo*> ValueMap; 157 158 // Map parentli_ values to simple values in li_ that are defined at the same 159 // SlotIndex, or NULL for parentli_ values that have complex li_ defs. 160 // Note there is a difference between values mapping to NULL (complex), and 161 // values not present (unknown/unmapped). 162 ValueMap valueMap_; 163 164public: 165 LiveIntervalMap(LiveIntervals &lis, 166 const LiveInterval &parentli) 167 : lis_(lis), parentli_(parentli), li_(0) {} 168 169 /// reset - clear all data structures and start a new live interval. 170 void reset(LiveInterval *); 171 172 /// getLI - return the current live interval. 173 LiveInterval *getLI() const { return li_; } 174 175 /// defValue - define a value in li_ from the parentli_ value VNI and Idx. 176 /// Idx does not have to be ParentVNI->def, but it must be contained within 177 /// ParentVNI's live range in parentli_. 178 /// Return the new li_ value. 179 VNInfo *defValue(const VNInfo *ParentVNI, SlotIndex Idx); 180 181 /// mapValue - map ParentVNI to the corresponding li_ value at Idx. It is 182 /// assumed that ParentVNI is live at Idx. 183 /// If ParentVNI has not been defined by defValue, it is assumed that 184 /// ParentVNI->def dominates Idx. 185 /// If ParentVNI has been defined by defValue one or more times, a value that 186 /// dominates Idx will be returned. This may require creating extra phi-def 187 /// values and adding live ranges to li_. 188 /// If simple is not NULL, *simple will indicate if ParentVNI is a simply 189 /// mapped value. 190 VNInfo *mapValue(const VNInfo *ParentVNI, SlotIndex Idx, bool *simple = 0); 191 192 // extendTo - Find the last li_ value defined in MBB at or before Idx. The 193 // parentli is assumed to be live at Idx. Extend the live range to include 194 // Idx. Return the found VNInfo, or NULL. 195 VNInfo *extendTo(MachineBasicBlock *MBB, SlotIndex Idx); 196 197 /// isMapped - Return true is ParentVNI is a known mapped value. It may be a 198 /// simple 1-1 mapping or a complex mapping to later defs. 199 bool isMapped(const VNInfo *ParentVNI) const { 200 return valueMap_.count(ParentVNI); 201 } 202 203 /// isComplexMapped - Return true if ParentVNI has received new definitions 204 /// with defValue. 205 bool isComplexMapped(const VNInfo *ParentVNI) const; 206 207 // addSimpleRange - Add a simple range from parentli_ to li_. 208 // ParentVNI must be live in the [Start;End) interval. 209 void addSimpleRange(SlotIndex Start, SlotIndex End, const VNInfo *ParentVNI); 210 211 /// addRange - Add live ranges to li_ where [Start;End) intersects parentli_. 212 /// All needed values whose def is not inside [Start;End) must be defined 213 /// beforehand so mapValue will work. 214 void addRange(SlotIndex Start, SlotIndex End); 215 216 /// defByCopyFrom - Insert a copy from Reg to li, assuming that Reg carries 217 /// ParentVNI. Add a minimal live range for the new value and return it. 218 VNInfo *defByCopyFrom(unsigned Reg, 219 const VNInfo *ParentVNI, 220 MachineBasicBlock &MBB, 221 MachineBasicBlock::iterator I); 222 223}; 224 225 226/// SplitEditor - Edit machine code and LiveIntervals for live range 227/// splitting. 228/// 229/// - Create a SplitEditor from a SplitAnalysis. 230/// - Start a new live interval with openIntv. 231/// - Mark the places where the new interval is entered using enterIntv* 232/// - Mark the ranges where the new interval is used with useIntv* 233/// - Mark the places where the interval is exited with exitIntv*. 234/// - Finish the current interval with closeIntv and repeat from 2. 235/// - Rewrite instructions with rewrite(). 236/// 237class SplitEditor { 238 SplitAnalysis &sa_; 239 LiveIntervals &lis_; 240 VirtRegMap &vrm_; 241 MachineRegisterInfo &mri_; 242 const TargetInstrInfo &tii_; 243 244 /// curli_ - The immutable interval we are currently splitting. 245 const LiveInterval *const curli_; 246 247 /// dupli_ - Created as a copy of curli_, ranges are carved out as new 248 /// intervals get added through openIntv / closeIntv. This is used to avoid 249 /// editing curli_. 250 LiveIntervalMap dupli_; 251 252 /// Currently open LiveInterval. 253 LiveIntervalMap openli_; 254 255 /// createInterval - Create a new virtual register and LiveInterval with same 256 /// register class and spill slot as curli. 257 LiveInterval *createInterval(); 258 259 /// All the new intervals created for this split are added to intervals_. 260 SmallVectorImpl<LiveInterval*> &intervals_; 261 262 /// The index into intervals_ of the first interval we added. There may be 263 /// others from before we got it. 264 unsigned firstInterval; 265 266 /// intervalsLiveAt - Return true if any member of intervals_ is live at Idx. 267 bool intervalsLiveAt(SlotIndex Idx) const; 268 269 /// Values in curli whose live range has been truncated when entering an open 270 /// li. 271 SmallPtrSet<const VNInfo*, 8> truncatedValues; 272 273 /// addTruncSimpleRange - Add the given simple range to dupli_ after 274 /// truncating any overlap with intervals_. 275 void addTruncSimpleRange(SlotIndex Start, SlotIndex End, VNInfo *VNI); 276 277public: 278 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 279 /// Newly created intervals will be appended to newIntervals. 280 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, 281 SmallVectorImpl<LiveInterval*> &newIntervals); 282 283 /// getAnalysis - Get the corresponding analysis. 284 SplitAnalysis &getAnalysis() { return sa_; } 285 286 /// Create a new virtual register and live interval. 287 void openIntv(); 288 289 /// enterIntvBefore - Enter openli before the instruction at Idx. If curli is 290 /// not live before Idx, a COPY is not inserted. 291 void enterIntvBefore(SlotIndex Idx); 292 293 /// enterIntvAtEnd - Enter openli at the end of MBB. 294 void enterIntvAtEnd(MachineBasicBlock &MBB); 295 296 /// useIntv - indicate that all instructions in MBB should use openli. 297 void useIntv(const MachineBasicBlock &MBB); 298 299 /// useIntv - indicate that all instructions in range should use openli. 300 void useIntv(SlotIndex Start, SlotIndex End); 301 302 /// leaveIntvAfter - Leave openli after the instruction at Idx. 303 void leaveIntvAfter(SlotIndex Idx); 304 305 /// leaveIntvAtTop - Leave the interval at the top of MBB. 306 /// Currently, only one value can leave the interval. 307 void leaveIntvAtTop(MachineBasicBlock &MBB); 308 309 /// closeIntv - Indicate that we are done editing the currently open 310 /// LiveInterval, and ranges can be trimmed. 311 void closeIntv(); 312 313 /// rewrite - after all the new live ranges have been created, rewrite 314 /// instructions using curli to use the new intervals. 315 /// Return true if curli has been completely replaced, false if curli is still 316 /// intact, and needs to be spilled or split further. 317 bool rewrite(); 318 319 // ===--- High level methods ---=== 320 321 /// splitAroundLoop - Split curli into a separate live interval inside 322 /// the loop. Return true if curli has been completely replaced, false if 323 /// curli is still intact, and needs to be spilled or split further. 324 bool splitAroundLoop(const MachineLoop*); 325 326 /// splitSingleBlocks - Split curli into a separate live interval inside each 327 /// basic block in Blocks. Return true if curli has been completely replaced, 328 /// false if curli is still intact, and needs to be spilled or split further. 329 bool splitSingleBlocks(const SplitAnalysis::BlockPtrSet &Blocks); 330 331 /// splitInsideBlock - Split curli into multiple intervals inside MBB. Return 332 /// true if curli has been completely replaced, false if curli is still 333 /// intact, and needs to be spilled or split further. 334 bool splitInsideBlock(const MachineBasicBlock *); 335}; 336 337} 338