LLVM API Documentation
00001 //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- 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 implements the LiveInterval analysis pass. Given some numbering of 00011 // each the machine instructions (in this implemention depth-first order) an 00012 // interval [i, j) is said to be a live interval for register v if there is no 00013 // instruction with number j' > j such that v is live at j' and there is no 00014 // instruction with number i' < i such that v is live at i'. In this 00015 // implementation intervals can have holes, i.e. an interval might look like 00016 // [1,20), [50,65), [1000,1001). 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H 00021 #define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H 00022 00023 #include "llvm/ADT/IndexedMap.h" 00024 #include "llvm/ADT/SmallVector.h" 00025 #include "llvm/CodeGen/LiveInterval.h" 00026 #include "llvm/CodeGen/MachineBasicBlock.h" 00027 #include "llvm/CodeGen/MachineFunctionPass.h" 00028 #include "llvm/CodeGen/SlotIndexes.h" 00029 #include "llvm/Support/Allocator.h" 00030 #include "llvm/Target/TargetRegisterInfo.h" 00031 #include <cmath> 00032 #include <iterator> 00033 00034 namespace llvm { 00035 00036 class AliasAnalysis; 00037 class BitVector; 00038 class LiveRangeCalc; 00039 class LiveVariables; 00040 class MachineDominatorTree; 00041 class MachineLoopInfo; 00042 class TargetRegisterInfo; 00043 class MachineRegisterInfo; 00044 class TargetInstrInfo; 00045 class TargetRegisterClass; 00046 class VirtRegMap; 00047 00048 class LiveIntervals : public MachineFunctionPass { 00049 MachineFunction* MF; 00050 MachineRegisterInfo* MRI; 00051 const TargetMachine* TM; 00052 const TargetRegisterInfo* TRI; 00053 const TargetInstrInfo* TII; 00054 AliasAnalysis *AA; 00055 SlotIndexes* Indexes; 00056 MachineDominatorTree *DomTree; 00057 LiveRangeCalc *LRCalc; 00058 00059 /// Special pool allocator for VNInfo's (LiveInterval val#). 00060 /// 00061 VNInfo::Allocator VNInfoAllocator; 00062 00063 /// Live interval pointers for all the virtual registers. 00064 IndexedMap<LiveInterval*, VirtReg2IndexFunctor> VirtRegIntervals; 00065 00066 /// RegMaskSlots - Sorted list of instructions with register mask operands. 00067 /// Always use the 'r' slot, RegMasks are normal clobbers, not early 00068 /// clobbers. 00069 SmallVector<SlotIndex, 8> RegMaskSlots; 00070 00071 /// RegMaskBits - This vector is parallel to RegMaskSlots, it holds a 00072 /// pointer to the corresponding register mask. This pointer can be 00073 /// recomputed as: 00074 /// 00075 /// MI = Indexes->getInstructionFromIndex(RegMaskSlot[N]); 00076 /// unsigned OpNum = findRegMaskOperand(MI); 00077 /// RegMaskBits[N] = MI->getOperand(OpNum).getRegMask(); 00078 /// 00079 /// This is kept in a separate vector partly because some standard 00080 /// libraries don't support lower_bound() with mixed objects, partly to 00081 /// improve locality when searching in RegMaskSlots. 00082 /// Also see the comment in LiveInterval::find(). 00083 SmallVector<const uint32_t*, 8> RegMaskBits; 00084 00085 /// For each basic block number, keep (begin, size) pairs indexing into the 00086 /// RegMaskSlots and RegMaskBits arrays. 00087 /// Note that basic block numbers may not be layout contiguous, that's why 00088 /// we can't just keep track of the first register mask in each basic 00089 /// block. 00090 SmallVector<std::pair<unsigned, unsigned>, 8> RegMaskBlocks; 00091 00092 /// RegUnitIntervals - Keep a live interval for each register unit as a way 00093 /// of tracking fixed physreg interference. 00094 SmallVector<LiveInterval*, 0> RegUnitIntervals; 00095 00096 public: 00097 static char ID; // Pass identification, replacement for typeid 00098 LiveIntervals(); 00099 virtual ~LiveIntervals(); 00100 00101 // Calculate the spill weight to assign to a single instruction. 00102 static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth); 00103 00104 LiveInterval &getInterval(unsigned Reg) { 00105 LiveInterval *LI = VirtRegIntervals[Reg]; 00106 assert(LI && "Interval does not exist for virtual register"); 00107 return *LI; 00108 } 00109 00110 const LiveInterval &getInterval(unsigned Reg) const { 00111 return const_cast<LiveIntervals*>(this)->getInterval(Reg); 00112 } 00113 00114 bool hasInterval(unsigned Reg) const { 00115 return VirtRegIntervals.inBounds(Reg) && VirtRegIntervals[Reg]; 00116 } 00117 00118 // Interval creation. 00119 LiveInterval &getOrCreateInterval(unsigned Reg) { 00120 if (!hasInterval(Reg)) { 00121 VirtRegIntervals.grow(Reg); 00122 VirtRegIntervals[Reg] = createInterval(Reg); 00123 } 00124 return getInterval(Reg); 00125 } 00126 00127 // Interval removal. 00128 void removeInterval(unsigned Reg) { 00129 delete VirtRegIntervals[Reg]; 00130 VirtRegIntervals[Reg] = 0; 00131 } 00132 00133 /// addLiveRangeToEndOfBlock - Given a register and an instruction, 00134 /// adds a live range from that instruction to the end of its MBB. 00135 LiveRange addLiveRangeToEndOfBlock(unsigned reg, 00136 MachineInstr* startInst); 00137 00138 /// shrinkToUses - After removing some uses of a register, shrink its live 00139 /// range to just the remaining uses. This method does not compute reaching 00140 /// defs for new uses, and it doesn't remove dead defs. 00141 /// Dead PHIDef values are marked as unused. 00142 /// New dead machine instructions are added to the dead vector. 00143 /// Return true if the interval may have been separated into multiple 00144 /// connected components. 00145 bool shrinkToUses(LiveInterval *li, 00146 SmallVectorImpl<MachineInstr*> *dead = 0); 00147 00148 /// extendToIndices - Extend the live range of LI to reach all points in 00149 /// Indices. The points in the Indices array must be jointly dominated by 00150 /// existing defs in LI. PHI-defs are added as needed to maintain SSA form. 00151 /// 00152 /// If a SlotIndex in Indices is the end index of a basic block, LI will be 00153 /// extended to be live out of the basic block. 00154 /// 00155 /// See also LiveRangeCalc::extend(). 00156 void extendToIndices(LiveInterval *LI, ArrayRef<SlotIndex> Indices); 00157 00158 /// pruneValue - If an LI value is live at Kill, prune its live range by 00159 /// removing any liveness reachable from Kill. Add live range end points to 00160 /// EndPoints such that extendToIndices(LI, EndPoints) will reconstruct the 00161 /// value's live range. 00162 /// 00163 /// Calling pruneValue() and extendToIndices() can be used to reconstruct 00164 /// SSA form after adding defs to a virtual register. 00165 void pruneValue(LiveInterval *LI, SlotIndex Kill, 00166 SmallVectorImpl<SlotIndex> *EndPoints); 00167 00168 SlotIndexes *getSlotIndexes() const { 00169 return Indexes; 00170 } 00171 00172 AliasAnalysis *getAliasAnalysis() const { 00173 return AA; 00174 } 00175 00176 /// isNotInMIMap - returns true if the specified machine instr has been 00177 /// removed or was never entered in the map. 00178 bool isNotInMIMap(const MachineInstr* Instr) const { 00179 return !Indexes->hasIndex(Instr); 00180 } 00181 00182 /// Returns the base index of the given instruction. 00183 SlotIndex getInstructionIndex(const MachineInstr *instr) const { 00184 return Indexes->getInstructionIndex(instr); 00185 } 00186 00187 /// Returns the instruction associated with the given index. 00188 MachineInstr* getInstructionFromIndex(SlotIndex index) const { 00189 return Indexes->getInstructionFromIndex(index); 00190 } 00191 00192 /// Return the first index in the given basic block. 00193 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const { 00194 return Indexes->getMBBStartIdx(mbb); 00195 } 00196 00197 /// Return the last index in the given basic block. 00198 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const { 00199 return Indexes->getMBBEndIdx(mbb); 00200 } 00201 00202 bool isLiveInToMBB(const LiveInterval &li, 00203 const MachineBasicBlock *mbb) const { 00204 return li.liveAt(getMBBStartIdx(mbb)); 00205 } 00206 00207 bool isLiveOutOfMBB(const LiveInterval &li, 00208 const MachineBasicBlock *mbb) const { 00209 return li.liveAt(getMBBEndIdx(mbb).getPrevSlot()); 00210 } 00211 00212 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const { 00213 return Indexes->getMBBFromIndex(index); 00214 } 00215 00216 void insertMBBInMaps(MachineBasicBlock *MBB) { 00217 Indexes->insertMBBInMaps(MBB); 00218 assert(unsigned(MBB->getNumber()) == RegMaskBlocks.size() && 00219 "Blocks must be added in order."); 00220 RegMaskBlocks.push_back(std::make_pair(RegMaskSlots.size(), 0)); 00221 } 00222 00223 SlotIndex InsertMachineInstrInMaps(MachineInstr *MI) { 00224 return Indexes->insertMachineInstrInMaps(MI); 00225 } 00226 00227 void RemoveMachineInstrFromMaps(MachineInstr *MI) { 00228 Indexes->removeMachineInstrFromMaps(MI); 00229 } 00230 00231 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) { 00232 Indexes->replaceMachineInstrInMaps(MI, NewMI); 00233 } 00234 00235 bool findLiveInMBBs(SlotIndex Start, SlotIndex End, 00236 SmallVectorImpl<MachineBasicBlock*> &MBBs) const { 00237 return Indexes->findLiveInMBBs(Start, End, MBBs); 00238 } 00239 00240 VNInfo::Allocator& getVNInfoAllocator() { return VNInfoAllocator; } 00241 00242 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 00243 virtual void releaseMemory(); 00244 00245 /// runOnMachineFunction - pass entry point 00246 virtual bool runOnMachineFunction(MachineFunction&); 00247 00248 /// print - Implement the dump method. 00249 virtual void print(raw_ostream &O, const Module* = 0) const; 00250 00251 /// intervalIsInOneMBB - If LI is confined to a single basic block, return 00252 /// a pointer to that block. If LI is live in to or out of any block, 00253 /// return NULL. 00254 MachineBasicBlock *intervalIsInOneMBB(const LiveInterval &LI) const; 00255 00256 /// Returns true if VNI is killed by any PHI-def values in LI. 00257 /// This may conservatively return true to avoid expensive computations. 00258 bool hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const; 00259 00260 /// addKillFlags - Add kill flags to any instruction that kills a virtual 00261 /// register. 00262 void addKillFlags(const VirtRegMap*); 00263 00264 /// handleMove - call this method to notify LiveIntervals that 00265 /// instruction 'mi' has been moved within a basic block. This will update 00266 /// the live intervals for all operands of mi. Moves between basic blocks 00267 /// are not supported. 00268 /// 00269 /// \param UpdateFlags Update live intervals for nonallocatable physregs. 00270 void handleMove(MachineInstr* MI, bool UpdateFlags = false); 00271 00272 /// moveIntoBundle - Update intervals for operands of MI so that they 00273 /// begin/end on the SlotIndex for BundleStart. 00274 /// 00275 /// \param UpdateFlags Update live intervals for nonallocatable physregs. 00276 /// 00277 /// Requires MI and BundleStart to have SlotIndexes, and assumes 00278 /// existing liveness is accurate. BundleStart should be the first 00279 /// instruction in the Bundle. 00280 void handleMoveIntoBundle(MachineInstr* MI, MachineInstr* BundleStart, 00281 bool UpdateFlags = false); 00282 00283 /// repairIntervalsInRange - Update live intervals for instructions in a 00284 /// range of iterators. It is intended for use after target hooks that may 00285 /// insert or remove instructions, and is only efficient for a small number 00286 /// of instructions. 00287 /// 00288 /// OrigRegs is a vector of registers that were originally used by the 00289 /// instructions in the range between the two iterators. 00290 /// 00291 /// Currently, the only only changes that are supported are simple removal 00292 /// and addition of uses. 00293 void repairIntervalsInRange(MachineBasicBlock *MBB, 00294 MachineBasicBlock::iterator Begin, 00295 MachineBasicBlock::iterator End, 00296 ArrayRef<unsigned> OrigRegs); 00297 00298 // Register mask functions. 00299 // 00300 // Machine instructions may use a register mask operand to indicate that a 00301 // large number of registers are clobbered by the instruction. This is 00302 // typically used for calls. 00303 // 00304 // For compile time performance reasons, these clobbers are not recorded in 00305 // the live intervals for individual physical registers. Instead, 00306 // LiveIntervalAnalysis maintains a sorted list of instructions with 00307 // register mask operands. 00308 00309 /// getRegMaskSlots - Returns a sorted array of slot indices of all 00310 /// instructions with register mask operands. 00311 ArrayRef<SlotIndex> getRegMaskSlots() const { return RegMaskSlots; } 00312 00313 /// getRegMaskSlotsInBlock - Returns a sorted array of slot indices of all 00314 /// instructions with register mask operands in the basic block numbered 00315 /// MBBNum. 00316 ArrayRef<SlotIndex> getRegMaskSlotsInBlock(unsigned MBBNum) const { 00317 std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum]; 00318 return getRegMaskSlots().slice(P.first, P.second); 00319 } 00320 00321 /// getRegMaskBits() - Returns an array of register mask pointers 00322 /// corresponding to getRegMaskSlots(). 00323 ArrayRef<const uint32_t*> getRegMaskBits() const { return RegMaskBits; } 00324 00325 /// getRegMaskBitsInBlock - Returns an array of mask pointers corresponding 00326 /// to getRegMaskSlotsInBlock(MBBNum). 00327 ArrayRef<const uint32_t*> getRegMaskBitsInBlock(unsigned MBBNum) const { 00328 std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum]; 00329 return getRegMaskBits().slice(P.first, P.second); 00330 } 00331 00332 /// checkRegMaskInterference - Test if LI is live across any register mask 00333 /// instructions, and compute a bit mask of physical registers that are not 00334 /// clobbered by any of them. 00335 /// 00336 /// Returns false if LI doesn't cross any register mask instructions. In 00337 /// that case, the bit vector is not filled in. 00338 bool checkRegMaskInterference(LiveInterval &LI, 00339 BitVector &UsableRegs); 00340 00341 // Register unit functions. 00342 // 00343 // Fixed interference occurs when MachineInstrs use physregs directly 00344 // instead of virtual registers. This typically happens when passing 00345 // arguments to a function call, or when instructions require operands in 00346 // fixed registers. 00347 // 00348 // Each physreg has one or more register units, see MCRegisterInfo. We 00349 // track liveness per register unit to handle aliasing registers more 00350 // efficiently. 00351 00352 /// getRegUnit - Return the live range for Unit. 00353 /// It will be computed if it doesn't exist. 00354 LiveInterval &getRegUnit(unsigned Unit) { 00355 LiveInterval *LI = RegUnitIntervals[Unit]; 00356 if (!LI) { 00357 // Compute missing ranges on demand. 00358 RegUnitIntervals[Unit] = LI = new LiveInterval(Unit, HUGE_VALF); 00359 computeRegUnitInterval(LI); 00360 } 00361 return *LI; 00362 } 00363 00364 /// getCachedRegUnit - Return the live range for Unit if it has already 00365 /// been computed, or NULL if it hasn't been computed yet. 00366 LiveInterval *getCachedRegUnit(unsigned Unit) { 00367 return RegUnitIntervals[Unit]; 00368 } 00369 00370 const LiveInterval *getCachedRegUnit(unsigned Unit) const { 00371 return RegUnitIntervals[Unit]; 00372 } 00373 00374 private: 00375 /// Compute live intervals for all virtual registers. 00376 void computeVirtRegs(); 00377 00378 /// Compute RegMaskSlots and RegMaskBits. 00379 void computeRegMasks(); 00380 00381 static LiveInterval* createInterval(unsigned Reg); 00382 00383 void printInstrs(raw_ostream &O) const; 00384 void dumpInstrs() const; 00385 00386 void computeLiveInRegUnits(); 00387 void computeRegUnitInterval(LiveInterval*); 00388 void computeVirtRegInterval(LiveInterval*); 00389 00390 class HMEditor; 00391 }; 00392 } // End llvm namespace 00393 00394 #endif