1 //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LiveInterval analysis pass. Given some numbering of
11 // each the machine instructions (in this implemention depth-first order) an
12 // interval [i, j) is said to be a live interval for register v if there is no
13 // instruction with number j' > j such that v is live at j' and there is no
14 // instruction with number i' < i such that v is live at i'. In this
15 // implementation intervals can have holes, i.e. an interval might look like
16 // [1,20), [50,65), [1000,1001).
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
21 #define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/LiveInterval.h"
26 #include "llvm/CodeGen/SlotIndexes.h"
27 #include "llvm/ADT/BitVector.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Support/Allocator.h"
39 class MachineLoopInfo;
40 class TargetRegisterInfo;
41 class MachineRegisterInfo;
42 class TargetInstrInfo;
43 class TargetRegisterClass;
46 class LiveIntervals : public MachineFunctionPass {
48 MachineRegisterInfo* MRI;
49 const TargetMachine* TM;
50 const TargetRegisterInfo* TRI;
51 const TargetInstrInfo* TII;
56 /// Special pool allocator for VNInfo's (LiveInterval val#).
58 VNInfo::Allocator VNInfoAllocator;
60 typedef DenseMap<unsigned, LiveInterval*> Reg2IntervalMap;
61 Reg2IntervalMap R2IMap;
63 /// AllocatableRegs - A bit vector of allocatable registers.
64 BitVector AllocatableRegs;
66 /// ReservedRegs - A bit vector of reserved registers.
67 BitVector ReservedRegs;
69 /// RegMaskSlots - Sorted list of instructions with register mask operands.
70 /// Always use the 'r' slot, RegMasks are normal clobbers, not early
72 SmallVector<SlotIndex, 8> RegMaskSlots;
74 /// RegMaskBits - This vector is parallel to RegMaskSlots, it holds a
75 /// pointer to the corresponding register mask. This pointer can be
78 /// MI = Indexes->getInstructionFromIndex(RegMaskSlot[N]);
79 /// unsigned OpNum = findRegMaskOperand(MI);
80 /// RegMaskBits[N] = MI->getOperand(OpNum).getRegMask();
82 /// This is kept in a separate vector partly because some standard
83 /// libraries don't support lower_bound() with mixed objects, partly to
84 /// improve locality when searching in RegMaskSlots.
85 /// Also see the comment in LiveInterval::find().
86 SmallVector<const uint32_t*, 8> RegMaskBits;
88 /// For each basic block number, keep (begin, size) pairs indexing into the
89 /// RegMaskSlots and RegMaskBits arrays.
90 /// Note that basic block numbers may not be layout contiguous, that's why
91 /// we can't just keep track of the first register mask in each basic
93 SmallVector<std::pair<unsigned, unsigned>, 8> RegMaskBlocks;
96 static char ID; // Pass identification, replacement for typeid
97 LiveIntervals() : MachineFunctionPass(ID) {
98 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
101 // Calculate the spill weight to assign to a single instruction.
102 static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth);
104 typedef Reg2IntervalMap::iterator iterator;
105 typedef Reg2IntervalMap::const_iterator const_iterator;
106 const_iterator begin() const { return R2IMap.begin(); }
107 const_iterator end() const { return R2IMap.end(); }
108 iterator begin() { return R2IMap.begin(); }
109 iterator end() { return R2IMap.end(); }
110 unsigned getNumIntervals() const { return (unsigned)R2IMap.size(); }
112 LiveInterval &getInterval(unsigned reg) {
113 Reg2IntervalMap::iterator I = R2IMap.find(reg);
114 assert(I != R2IMap.end() && "Interval does not exist for register");
118 const LiveInterval &getInterval(unsigned reg) const {
119 Reg2IntervalMap::const_iterator I = R2IMap.find(reg);
120 assert(I != R2IMap.end() && "Interval does not exist for register");
124 bool hasInterval(unsigned reg) const {
125 return R2IMap.count(reg);
128 /// isAllocatable - is the physical register reg allocatable in the current
130 bool isAllocatable(unsigned reg) const {
131 return AllocatableRegs.test(reg);
134 /// isReserved - is the physical register reg reserved in the current
136 bool isReserved(unsigned reg) const {
137 return ReservedRegs.test(reg);
141 LiveInterval &getOrCreateInterval(unsigned reg) {
142 Reg2IntervalMap::iterator I = R2IMap.find(reg);
143 if (I == R2IMap.end())
144 I = R2IMap.insert(std::make_pair(reg, createInterval(reg))).first;
148 /// addLiveRangeToEndOfBlock - Given a register and an instruction,
149 /// adds a live range from that instruction to the end of its MBB.
150 LiveRange addLiveRangeToEndOfBlock(unsigned reg,
151 MachineInstr* startInst);
153 /// shrinkToUses - After removing some uses of a register, shrink its live
154 /// range to just the remaining uses. This method does not compute reaching
155 /// defs for new uses, and it doesn't remove dead defs.
156 /// Dead PHIDef values are marked as unused.
157 /// New dead machine instructions are added to the dead vector.
158 /// Return true if the interval may have been separated into multiple
159 /// connected components.
160 bool shrinkToUses(LiveInterval *li,
161 SmallVectorImpl<MachineInstr*> *dead = 0);
165 void removeInterval(unsigned Reg) {
166 DenseMap<unsigned, LiveInterval*>::iterator I = R2IMap.find(Reg);
171 SlotIndexes *getSlotIndexes() const {
175 AliasAnalysis *getAliasAnalysis() const {
179 /// isNotInMIMap - returns true if the specified machine instr has been
180 /// removed or was never entered in the map.
181 bool isNotInMIMap(const MachineInstr* Instr) const {
182 return !Indexes->hasIndex(Instr);
185 /// Returns the base index of the given instruction.
186 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
187 return Indexes->getInstructionIndex(instr);
190 /// Returns the instruction associated with the given index.
191 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
192 return Indexes->getInstructionFromIndex(index);
195 /// Return the first index in the given basic block.
196 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
197 return Indexes->getMBBStartIdx(mbb);
200 /// Return the last index in the given basic block.
201 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
202 return Indexes->getMBBEndIdx(mbb);
205 bool isLiveInToMBB(const LiveInterval &li,
206 const MachineBasicBlock *mbb) const {
207 return li.liveAt(getMBBStartIdx(mbb));
210 bool isLiveOutOfMBB(const LiveInterval &li,
211 const MachineBasicBlock *mbb) const {
212 return li.liveAt(getMBBEndIdx(mbb).getPrevSlot());
215 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
216 return Indexes->getMBBFromIndex(index);
219 SlotIndex InsertMachineInstrInMaps(MachineInstr *MI) {
220 return Indexes->insertMachineInstrInMaps(MI);
223 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
224 Indexes->removeMachineInstrFromMaps(MI);
227 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
228 Indexes->replaceMachineInstrInMaps(MI, NewMI);
231 bool findLiveInMBBs(SlotIndex Start, SlotIndex End,
232 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
233 return Indexes->findLiveInMBBs(Start, End, MBBs);
236 VNInfo::Allocator& getVNInfoAllocator() { return VNInfoAllocator; }
238 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
239 virtual void releaseMemory();
241 /// runOnMachineFunction - pass entry point
242 virtual bool runOnMachineFunction(MachineFunction&);
244 /// print - Implement the dump method.
245 virtual void print(raw_ostream &O, const Module* = 0) const;
247 /// isReMaterializable - Returns true if every definition of MI of every
248 /// val# of the specified interval is re-materializable. Also returns true
249 /// by reference if all of the defs are load instructions.
250 bool isReMaterializable(const LiveInterval &li,
251 const SmallVectorImpl<LiveInterval*> *SpillIs,
254 /// intervalIsInOneMBB - If LI is confined to a single basic block, return
255 /// a pointer to that block. If LI is live in to or out of any block,
257 MachineBasicBlock *intervalIsInOneMBB(const LiveInterval &LI) const;
259 /// addKillFlags - Add kill flags to any instruction that kills a virtual
263 /// handleMove - call this method to notify LiveIntervals that
264 /// instruction 'mi' has been moved within a basic block. This will update
265 /// the live intervals for all operands of mi. Moves between basic blocks
266 /// are not supported.
267 void handleMove(MachineInstr* MI);
269 /// moveIntoBundle - Update intervals for operands of MI so that they
270 /// begin/end on the SlotIndex for BundleStart.
272 /// Requires MI and BundleStart to have SlotIndexes, and assumes
273 /// existing liveness is accurate. BundleStart should be the first
274 /// instruction in the Bundle.
275 void handleMoveIntoBundle(MachineInstr* MI, MachineInstr* BundleStart);
277 // Register mask functions.
279 // Machine instructions may use a register mask operand to indicate that a
280 // large number of registers are clobbered by the instruction. This is
281 // typically used for calls.
283 // For compile time performance reasons, these clobbers are not recorded in
284 // the live intervals for individual physical registers. Instead,
285 // LiveIntervalAnalysis maintains a sorted list of instructions with
286 // register mask operands.
288 /// getRegMaskSlots - Returns a sorted array of slot indices of all
289 /// instructions with register mask operands.
290 ArrayRef<SlotIndex> getRegMaskSlots() const { return RegMaskSlots; }
292 /// getRegMaskSlotsInBlock - Returns a sorted array of slot indices of all
293 /// instructions with register mask operands in the basic block numbered
295 ArrayRef<SlotIndex> getRegMaskSlotsInBlock(unsigned MBBNum) const {
296 std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum];
297 return getRegMaskSlots().slice(P.first, P.second);
300 /// getRegMaskBits() - Returns an array of register mask pointers
301 /// corresponding to getRegMaskSlots().
302 ArrayRef<const uint32_t*> getRegMaskBits() const { return RegMaskBits; }
304 /// getRegMaskBitsInBlock - Returns an array of mask pointers corresponding
305 /// to getRegMaskSlotsInBlock(MBBNum).
306 ArrayRef<const uint32_t*> getRegMaskBitsInBlock(unsigned MBBNum) const {
307 std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum];
308 return getRegMaskBits().slice(P.first, P.second);
311 /// checkRegMaskInterference - Test if LI is live across any register mask
312 /// instructions, and compute a bit mask of physical registers that are not
313 /// clobbered by any of them.
315 /// Returns false if LI doesn't cross any register mask instructions. In
316 /// that case, the bit vector is not filled in.
317 bool checkRegMaskInterference(LiveInterval &LI,
318 BitVector &UsableRegs);
321 /// computeIntervals - Compute live intervals.
322 void computeIntervals();
324 /// handleRegisterDef - update intervals for a register def
325 /// (calls handlePhysicalRegisterDef and
326 /// handleVirtualRegisterDef)
327 void handleRegisterDef(MachineBasicBlock *MBB,
328 MachineBasicBlock::iterator MI,
330 MachineOperand& MO, unsigned MOIdx);
332 /// isPartialRedef - Return true if the specified def at the specific index
333 /// is partially re-defining the specified live interval. A common case of
334 /// this is a definition of the sub-register.
335 bool isPartialRedef(SlotIndex MIIdx, MachineOperand &MO,
336 LiveInterval &interval);
338 /// handleVirtualRegisterDef - update intervals for a virtual
340 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
341 MachineBasicBlock::iterator MI,
342 SlotIndex MIIdx, MachineOperand& MO,
344 LiveInterval& interval);
346 /// handlePhysicalRegisterDef - update intervals for a physical register
348 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
349 MachineBasicBlock::iterator mi,
350 SlotIndex MIIdx, MachineOperand& MO,
351 LiveInterval &interval);
353 /// handleLiveInRegister - Create interval for a livein register.
354 void handleLiveInRegister(MachineBasicBlock* mbb,
356 LiveInterval &interval);
358 static LiveInterval* createInterval(unsigned Reg);
360 void printInstrs(raw_ostream &O) const;
361 void dumpInstrs() const;
365 } // End llvm namespace