LLVM API Documentation
00001 //===-------- SplitKit.h - Toolkit for splitting live ranges ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the SplitAnalysis class as well as mutator functions for 00011 // live range splitting. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_LIB_CODEGEN_SPLITKIT_H 00016 #define LLVM_LIB_CODEGEN_SPLITKIT_H 00017 00018 #include "LiveRangeCalc.h" 00019 #include "llvm/ADT/ArrayRef.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 #include "llvm/ADT/IntervalMap.h" 00022 #include "llvm/ADT/SmallPtrSet.h" 00023 00024 namespace llvm { 00025 00026 class ConnectedVNInfoEqClasses; 00027 class LiveInterval; 00028 class LiveIntervals; 00029 class LiveRangeEdit; 00030 class MachineBlockFrequencyInfo; 00031 class MachineInstr; 00032 class MachineLoopInfo; 00033 class MachineRegisterInfo; 00034 class TargetInstrInfo; 00035 class TargetRegisterInfo; 00036 class VirtRegMap; 00037 class VNInfo; 00038 class raw_ostream; 00039 00040 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting 00041 /// opportunities. 00042 class SplitAnalysis { 00043 public: 00044 const MachineFunction &MF; 00045 const VirtRegMap &VRM; 00046 const LiveIntervals &LIS; 00047 const MachineLoopInfo &Loops; 00048 const TargetInstrInfo &TII; 00049 00050 /// Additional information about basic blocks where the current variable is 00051 /// live. Such a block will look like one of these templates: 00052 /// 00053 /// 1. | o---x | Internal to block. Variable is only live in this block. 00054 /// 2. |---x | Live-in, kill. 00055 /// 3. | o---| Def, live-out. 00056 /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks. 00057 /// 5. |---o---o---| Live-through with uses or defs. 00058 /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks. 00059 /// 00060 /// Two BlockInfo entries are created for template 4. One for the live-in 00061 /// segment, and one for the live-out segment. These entries look as if the 00062 /// block were split in the middle where the live range isn't live. 00063 /// 00064 /// Live-through blocks without any uses don't get BlockInfo entries. They 00065 /// are simply listed in ThroughBlocks instead. 00066 /// 00067 struct BlockInfo { 00068 MachineBasicBlock *MBB; 00069 SlotIndex FirstInstr; ///< First instr accessing current reg. 00070 SlotIndex LastInstr; ///< Last instr accessing current reg. 00071 SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex(). 00072 bool LiveIn; ///< Current reg is live in. 00073 bool LiveOut; ///< Current reg is live out. 00074 00075 /// isOneInstr - Returns true when this BlockInfo describes a single 00076 /// instruction. 00077 bool isOneInstr() const { 00078 return SlotIndex::isSameInstr(FirstInstr, LastInstr); 00079 } 00080 }; 00081 00082 private: 00083 // Current live interval. 00084 const LiveInterval *CurLI; 00085 00086 // Sorted slot indexes of using instructions. 00087 SmallVector<SlotIndex, 8> UseSlots; 00088 00089 /// LastSplitPoint - Last legal split point in each basic block in the current 00090 /// function. The first entry is the first terminator, the second entry is the 00091 /// last valid split point for a variable that is live in to a landing pad 00092 /// successor. 00093 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastSplitPoint; 00094 00095 /// UseBlocks - Blocks where CurLI has uses. 00096 SmallVector<BlockInfo, 8> UseBlocks; 00097 00098 /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where 00099 /// the live range has a gap. 00100 unsigned NumGapBlocks; 00101 00102 /// ThroughBlocks - Block numbers where CurLI is live through without uses. 00103 BitVector ThroughBlocks; 00104 00105 /// NumThroughBlocks - Number of live-through blocks. 00106 unsigned NumThroughBlocks; 00107 00108 /// DidRepairRange - analyze was forced to shrinkToUses(). 00109 bool DidRepairRange; 00110 00111 SlotIndex computeLastSplitPoint(unsigned Num); 00112 00113 // Sumarize statistics by counting instructions using CurLI. 00114 void analyzeUses(); 00115 00116 /// calcLiveBlockInfo - Compute per-block information about CurLI. 00117 bool calcLiveBlockInfo(); 00118 00119 public: 00120 SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, 00121 const MachineLoopInfo &mli); 00122 00123 /// analyze - set CurLI to the specified interval, and analyze how it may be 00124 /// split. 00125 void analyze(const LiveInterval *li); 00126 00127 /// didRepairRange() - Returns true if CurLI was invalid and has been repaired 00128 /// by analyze(). This really shouldn't happen, but sometimes the coalescer 00129 /// can create live ranges that end in mid-air. 00130 bool didRepairRange() const { return DidRepairRange; } 00131 00132 /// clear - clear all data structures so SplitAnalysis is ready to analyze a 00133 /// new interval. 00134 void clear(); 00135 00136 /// getParent - Return the last analyzed interval. 00137 const LiveInterval &getParent() const { return *CurLI; } 00138 00139 /// getLastSplitPoint - Return the base index of the last valid split point 00140 /// in the basic block numbered Num. 00141 SlotIndex getLastSplitPoint(unsigned Num) { 00142 // Inline the common simple case. 00143 if (LastSplitPoint[Num].first.isValid() && 00144 !LastSplitPoint[Num].second.isValid()) 00145 return LastSplitPoint[Num].first; 00146 return computeLastSplitPoint(Num); 00147 } 00148 00149 /// getLastSplitPointIter - Returns the last split point as an iterator. 00150 MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock*); 00151 00152 /// isOriginalEndpoint - Return true if the original live range was killed or 00153 /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def, 00154 /// and 'use' for an early-clobber def. 00155 /// This can be used to recognize code inserted by earlier live range 00156 /// splitting. 00157 bool isOriginalEndpoint(SlotIndex Idx) const; 00158 00159 /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI. 00160 /// This include both use and def operands, at most one entry per instruction. 00161 ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; } 00162 00163 /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks 00164 /// where CurLI has uses. 00165 ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; } 00166 00167 /// getNumThroughBlocks - Return the number of through blocks. 00168 unsigned getNumThroughBlocks() const { return NumThroughBlocks; } 00169 00170 /// isThroughBlock - Return true if CurLI is live through MBB without uses. 00171 bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); } 00172 00173 /// getThroughBlocks - Return the set of through blocks. 00174 const BitVector &getThroughBlocks() const { return ThroughBlocks; } 00175 00176 /// getNumLiveBlocks - Return the number of blocks where CurLI is live. 00177 unsigned getNumLiveBlocks() const { 00178 return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks(); 00179 } 00180 00181 /// countLiveBlocks - Return the number of blocks where li is live. This is 00182 /// guaranteed to return the same number as getNumLiveBlocks() after calling 00183 /// analyze(li). 00184 unsigned countLiveBlocks(const LiveInterval *li) const; 00185 00186 typedef SmallPtrSet<const MachineBasicBlock*, 16> BlockPtrSet; 00187 00188 /// shouldSplitSingleBlock - Returns true if it would help to create a local 00189 /// live range for the instructions in BI. There is normally no benefit to 00190 /// creating a live range for a single instruction, but it does enable 00191 /// register class inflation if the instruction has a restricted register 00192 /// class. 00193 /// 00194 /// @param BI The block to be isolated. 00195 /// @param SingleInstrs True when single instructions should be isolated. 00196 bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const; 00197 }; 00198 00199 00200 /// SplitEditor - Edit machine code and LiveIntervals for live range 00201 /// splitting. 00202 /// 00203 /// - Create a SplitEditor from a SplitAnalysis. 00204 /// - Start a new live interval with openIntv. 00205 /// - Mark the places where the new interval is entered using enterIntv* 00206 /// - Mark the ranges where the new interval is used with useIntv* 00207 /// - Mark the places where the interval is exited with exitIntv*. 00208 /// - Finish the current interval with closeIntv and repeat from 2. 00209 /// - Rewrite instructions with finish(). 00210 /// 00211 class SplitEditor { 00212 SplitAnalysis &SA; 00213 LiveIntervals &LIS; 00214 VirtRegMap &VRM; 00215 MachineRegisterInfo &MRI; 00216 MachineDominatorTree &MDT; 00217 const TargetInstrInfo &TII; 00218 const TargetRegisterInfo &TRI; 00219 const MachineBlockFrequencyInfo &MBFI; 00220 00221 public: 00222 00223 /// ComplementSpillMode - Select how the complement live range should be 00224 /// created. SplitEditor automatically creates interval 0 to contain 00225 /// anything that isn't added to another interval. This complement interval 00226 /// can get quite complicated, and it can sometimes be an advantage to allow 00227 /// it to overlap the other intervals. If it is going to spill anyway, no 00228 /// registers are wasted by keeping a value in two places at the same time. 00229 enum ComplementSpillMode { 00230 /// SM_Partition(Default) - Try to create the complement interval so it 00231 /// doesn't overlap any other intervals, and the original interval is 00232 /// partitioned. This may require a large number of back copies and extra 00233 /// PHI-defs. Only segments marked with overlapIntv will be overlapping. 00234 SM_Partition, 00235 00236 /// SM_Size - Overlap intervals to minimize the number of inserted COPY 00237 /// instructions. Copies to the complement interval are hoisted to their 00238 /// common dominator, so only one COPY is required per value in the 00239 /// complement interval. This also means that no extra PHI-defs need to be 00240 /// inserted in the complement interval. 00241 SM_Size, 00242 00243 /// SM_Speed - Overlap intervals to minimize the expected execution 00244 /// frequency of the inserted copies. This is very similar to SM_Size, but 00245 /// the complement interval may get some extra PHI-defs. 00246 SM_Speed 00247 }; 00248 00249 private: 00250 00251 /// Edit - The current parent register and new intervals created. 00252 LiveRangeEdit *Edit; 00253 00254 /// Index into Edit of the currently open interval. 00255 /// The index 0 is used for the complement, so the first interval started by 00256 /// openIntv will be 1. 00257 unsigned OpenIdx; 00258 00259 /// The current spill mode, selected by reset(). 00260 ComplementSpillMode SpillMode; 00261 00262 typedef IntervalMap<SlotIndex, unsigned> RegAssignMap; 00263 00264 /// Allocator for the interval map. This will eventually be shared with 00265 /// SlotIndexes and LiveIntervals. 00266 RegAssignMap::Allocator Allocator; 00267 00268 /// RegAssign - Map of the assigned register indexes. 00269 /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at 00270 /// Idx. 00271 RegAssignMap RegAssign; 00272 00273 typedef PointerIntPair<VNInfo*, 1> ValueForcePair; 00274 typedef DenseMap<std::pair<unsigned, unsigned>, ValueForcePair> ValueMap; 00275 00276 /// Values - keep track of the mapping from parent values to values in the new 00277 /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains: 00278 /// 00279 /// 1. No entry - the value is not mapped to Edit.get(RegIdx). 00280 /// 2. (Null, false) - the value is mapped to multiple values in 00281 /// Edit.get(RegIdx). Each value is represented by a minimal live range at 00282 /// its def. The full live range can be inferred exactly from the range 00283 /// of RegIdx in RegAssign. 00284 /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and 00285 /// the live range must be recomputed using LiveRangeCalc::extend(). 00286 /// 4. (VNI, false) The value is mapped to a single new value. 00287 /// The new value has no live ranges anywhere. 00288 ValueMap Values; 00289 00290 /// LRCalc - Cache for computing live ranges and SSA update. Each instance 00291 /// can only handle non-overlapping live ranges, so use a separate 00292 /// LiveRangeCalc instance for the complement interval when in spill mode. 00293 LiveRangeCalc LRCalc[2]; 00294 00295 /// getLRCalc - Return the LRCalc to use for RegIdx. In spill mode, the 00296 /// complement interval can overlap the other intervals, so it gets its own 00297 /// LRCalc instance. When not in spill mode, all intervals can share one. 00298 LiveRangeCalc &getLRCalc(unsigned RegIdx) { 00299 return LRCalc[SpillMode != SM_Partition && RegIdx != 0]; 00300 } 00301 00302 /// defValue - define a value in RegIdx from ParentVNI at Idx. 00303 /// Idx does not have to be ParentVNI->def, but it must be contained within 00304 /// ParentVNI's live range in ParentLI. The new value is added to the value 00305 /// map. 00306 /// Return the new LI value. 00307 VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx); 00308 00309 /// forceRecompute - Force the live range of ParentVNI in RegIdx to be 00310 /// recomputed by LiveRangeCalc::extend regardless of the number of defs. 00311 /// This is used for values whose live range doesn't match RegAssign exactly. 00312 /// They could have rematerialized, or back-copies may have been moved. 00313 void forceRecompute(unsigned RegIdx, const VNInfo *ParentVNI); 00314 00315 /// defFromParent - Define Reg from ParentVNI at UseIdx using either 00316 /// rematerialization or a COPY from parent. Return the new value. 00317 VNInfo *defFromParent(unsigned RegIdx, 00318 VNInfo *ParentVNI, 00319 SlotIndex UseIdx, 00320 MachineBasicBlock &MBB, 00321 MachineBasicBlock::iterator I); 00322 00323 /// removeBackCopies - Remove the copy instructions that defines the values 00324 /// in the vector in the complement interval. 00325 void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies); 00326 00327 /// getShallowDominator - Returns the least busy dominator of MBB that is 00328 /// also dominated by DefMBB. Busy is measured by loop depth. 00329 MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB, 00330 MachineBasicBlock *DefMBB); 00331 00332 /// hoistCopiesForSize - Hoist back-copies to the complement interval in a 00333 /// way that minimizes code size. This implements the SM_Size spill mode. 00334 void hoistCopiesForSize(); 00335 00336 /// transferValues - Transfer values to the new ranges. 00337 /// Return true if any ranges were skipped. 00338 bool transferValues(); 00339 00340 /// extendPHIKillRanges - Extend the ranges of all values killed by original 00341 /// parent PHIDefs. 00342 void extendPHIKillRanges(); 00343 00344 /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers. 00345 void rewriteAssigned(bool ExtendRanges); 00346 00347 /// deleteRematVictims - Delete defs that are dead after rematerializing. 00348 void deleteRematVictims(); 00349 00350 public: 00351 /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 00352 /// Newly created intervals will be appended to newIntervals. 00353 SplitEditor(SplitAnalysis &SA, LiveIntervals&, VirtRegMap&, 00354 MachineDominatorTree&, MachineBlockFrequencyInfo &); 00355 00356 /// reset - Prepare for a new split. 00357 void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition); 00358 00359 /// Create a new virtual register and live interval. 00360 /// Return the interval index, starting from 1. Interval index 0 is the 00361 /// implicit complement interval. 00362 unsigned openIntv(); 00363 00364 /// currentIntv - Return the current interval index. 00365 unsigned currentIntv() const { return OpenIdx; } 00366 00367 /// selectIntv - Select a previously opened interval index. 00368 void selectIntv(unsigned Idx); 00369 00370 /// enterIntvBefore - Enter the open interval before the instruction at Idx. 00371 /// If the parent interval is not live before Idx, a COPY is not inserted. 00372 /// Return the beginning of the new live range. 00373 SlotIndex enterIntvBefore(SlotIndex Idx); 00374 00375 /// enterIntvAfter - Enter the open interval after the instruction at Idx. 00376 /// Return the beginning of the new live range. 00377 SlotIndex enterIntvAfter(SlotIndex Idx); 00378 00379 /// enterIntvAtEnd - Enter the open interval at the end of MBB. 00380 /// Use the open interval from the inserted copy to the MBB end. 00381 /// Return the beginning of the new live range. 00382 SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB); 00383 00384 /// useIntv - indicate that all instructions in MBB should use OpenLI. 00385 void useIntv(const MachineBasicBlock &MBB); 00386 00387 /// useIntv - indicate that all instructions in range should use OpenLI. 00388 void useIntv(SlotIndex Start, SlotIndex End); 00389 00390 /// leaveIntvAfter - Leave the open interval after the instruction at Idx. 00391 /// Return the end of the live range. 00392 SlotIndex leaveIntvAfter(SlotIndex Idx); 00393 00394 /// leaveIntvBefore - Leave the open interval before the instruction at Idx. 00395 /// Return the end of the live range. 00396 SlotIndex leaveIntvBefore(SlotIndex Idx); 00397 00398 /// leaveIntvAtTop - Leave the interval at the top of MBB. 00399 /// Add liveness from the MBB top to the copy. 00400 /// Return the end of the live range. 00401 SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB); 00402 00403 /// overlapIntv - Indicate that all instructions in range should use the open 00404 /// interval, but also let the complement interval be live. 00405 /// 00406 /// This doubles the register pressure, but is sometimes required to deal with 00407 /// register uses after the last valid split point. 00408 /// 00409 /// The Start index should be a return value from a leaveIntv* call, and End 00410 /// should be in the same basic block. The parent interval must have the same 00411 /// value across the range. 00412 /// 00413 void overlapIntv(SlotIndex Start, SlotIndex End); 00414 00415 /// finish - after all the new live ranges have been created, compute the 00416 /// remaining live range, and rewrite instructions to use the new registers. 00417 /// @param LRMap When not null, this vector will map each live range in Edit 00418 /// back to the indices returned by openIntv. 00419 /// There may be extra indices created by dead code elimination. 00420 void finish(SmallVectorImpl<unsigned> *LRMap = nullptr); 00421 00422 /// dump - print the current interval maping to dbgs(). 00423 void dump() const; 00424 00425 // ===--- High level methods ---=== 00426 00427 /// splitSingleBlock - Split CurLI into a separate live interval around the 00428 /// uses in a single block. This is intended to be used as part of a larger 00429 /// split, and doesn't call finish(). 00430 void splitSingleBlock(const SplitAnalysis::BlockInfo &BI); 00431 00432 /// splitLiveThroughBlock - Split CurLI in the given block such that it 00433 /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in 00434 /// the block, but they will be ignored when placing split points. 00435 /// 00436 /// @param MBBNum Block number. 00437 /// @param IntvIn Interval index entering the block. 00438 /// @param LeaveBefore When set, leave IntvIn before this point. 00439 /// @param IntvOut Interval index leaving the block. 00440 /// @param EnterAfter When set, enter IntvOut after this point. 00441 void splitLiveThroughBlock(unsigned MBBNum, 00442 unsigned IntvIn, SlotIndex LeaveBefore, 00443 unsigned IntvOut, SlotIndex EnterAfter); 00444 00445 /// splitRegInBlock - Split CurLI in the given block such that it enters the 00446 /// block in IntvIn and leaves it on the stack (or not at all). Split points 00447 /// are placed in a way that avoids putting uses in the stack interval. This 00448 /// may require creating a local interval when there is interference. 00449 /// 00450 /// @param BI Block descriptor. 00451 /// @param IntvIn Interval index entering the block. Not 0. 00452 /// @param LeaveBefore When set, leave IntvIn before this point. 00453 void splitRegInBlock(const SplitAnalysis::BlockInfo &BI, 00454 unsigned IntvIn, SlotIndex LeaveBefore); 00455 00456 /// splitRegOutBlock - Split CurLI in the given block such that it enters the 00457 /// block on the stack (or isn't live-in at all) and leaves it in IntvOut. 00458 /// Split points are placed to avoid interference and such that the uses are 00459 /// not in the stack interval. This may require creating a local interval 00460 /// when there is interference. 00461 /// 00462 /// @param BI Block descriptor. 00463 /// @param IntvOut Interval index leaving the block. 00464 /// @param EnterAfter When set, enter IntvOut after this point. 00465 void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, 00466 unsigned IntvOut, SlotIndex EnterAfter); 00467 }; 00468 00469 } 00470 00471 #endif