1 //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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' abd 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/ADT/DenseMap.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/LiveInterval.h"
31 class TargetInstrInfo;
34 class LiveIntervals : public MachineFunctionPass {
36 const TargetMachine* tm_;
37 const MRegisterInfo* mri_;
38 const TargetInstrInfo* tii_;
41 /// MBB2IdxMap - The index of the first instruction in the specified basic
43 std::vector<unsigned> MBB2IdxMap;
45 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
48 typedef std::vector<MachineInstr*> Index2MiMap;
51 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
52 Reg2IntervalMap r2iMap_;
54 typedef DenseMap<unsigned> Reg2RegMap;
57 std::vector<bool> allocatableRegs_;
62 unsigned SrcReg, DstReg;
64 CopyRec getCopyRec(MachineInstr *MI, unsigned SrcReg, unsigned DstReg) {
81 static unsigned getBaseIndex(unsigned index) {
82 return index - (index % InstrSlots::NUM);
84 static unsigned getBoundaryIndex(unsigned index) {
85 return getBaseIndex(index + InstrSlots::NUM - 1);
87 static unsigned getLoadIndex(unsigned index) {
88 return getBaseIndex(index) + InstrSlots::LOAD;
90 static unsigned getUseIndex(unsigned index) {
91 return getBaseIndex(index) + InstrSlots::USE;
93 static unsigned getDefIndex(unsigned index) {
94 return getBaseIndex(index) + InstrSlots::DEF;
96 static unsigned getStoreIndex(unsigned index) {
97 return getBaseIndex(index) + InstrSlots::STORE;
100 typedef Reg2IntervalMap::iterator iterator;
101 typedef Reg2IntervalMap::const_iterator const_iterator;
102 const_iterator begin() const { return r2iMap_.begin(); }
103 const_iterator end() const { return r2iMap_.end(); }
104 iterator begin() { return r2iMap_.begin(); }
105 iterator end() { return r2iMap_.end(); }
106 unsigned getNumIntervals() const { return r2iMap_.size(); }
108 LiveInterval &getInterval(unsigned reg) {
109 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
110 assert(I != r2iMap_.end() && "Interval does not exist for register");
114 const LiveInterval &getInterval(unsigned reg) const {
115 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
116 assert(I != r2iMap_.end() && "Interval does not exist for register");
120 /// getMBBStartIdx - Return the base index of the first instruction in the
121 /// specified MachineBasicBlock.
122 unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
123 return getMBBStartIdx(MBB->getNumber());
126 unsigned getMBBStartIdx(unsigned MBBNo) const {
127 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
128 return MBB2IdxMap[MBBNo];
131 /// getInstructionIndex - returns the base index of instr
132 unsigned getInstructionIndex(MachineInstr* instr) const {
133 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
134 assert(it != mi2iMap_.end() && "Invalid instruction!");
138 /// getInstructionFromIndex - given an index in any slot of an
139 /// instruction return a pointer the instruction
140 MachineInstr* getInstructionFromIndex(unsigned index) const {
141 index /= InstrSlots::NUM; // convert index to vector index
142 assert(index < i2miMap_.size() &&
143 "index does not correspond to an instruction");
144 return i2miMap_[index];
147 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
151 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
152 virtual void releaseMemory();
154 /// runOnMachineFunction - pass entry point
155 virtual bool runOnMachineFunction(MachineFunction&);
157 /// print - Implement the dump method.
158 virtual void print(std::ostream &O, const Module* = 0) const;
161 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
163 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
164 // remove index -> MachineInstr and
165 // MachineInstr -> index mappings
166 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
167 if (mi2i != mi2iMap_.end()) {
168 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
169 mi2iMap_.erase(mi2i);
173 /// computeIntervals - Compute live intervals.
174 void computeIntervals();
176 /// joinIntervals - join compatible live intervals
177 void joinIntervals();
179 /// CopyCoallesceInMBB - Coallsece copies in the specified MBB, putting
180 /// copies that cannot yet be coallesced into the "TryAgain" list.
181 void CopyCoallesceInMBB(MachineBasicBlock *MBB,
182 std::vector<CopyRec> &TryAgain);
183 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
184 /// which are the src/dst of the copy instruction CopyMI. This returns true
185 /// if the copy was successfully coallesced away, or if it is never possible
186 /// to coallesce these this copy, due to register constraints. It returns
187 /// false if it is not currently possible to coallesce this interval, but
188 /// it may be possible if other things get coallesced.
189 bool JoinCopy(MachineInstr *CopyMI, unsigned SrcReg, unsigned DstReg);
191 /// JoinIntervals - Attempt to join these two intervals. On failure, this
192 /// returns false. Otherwise, if one of the intervals being joined is a
193 /// physreg, this method always canonicalizes DestInt to be it. The output
194 /// "SrcInt" will not have been modified, so we can use this information
195 /// below to update aliases.
196 bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS);
198 /// SimpleJoin - Attempt to join the specified interval into this one. The
199 /// caller of this method must guarantee that the RHS only contains a single
200 /// value number and that the RHS is not defined by a copy from this
201 /// interval. This returns false if the intervals are not joinable, or it
202 /// joins them and returns true.
203 bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
205 /// handleRegisterDef - update intervals for a register def
206 /// (calls handlePhysicalRegisterDef and
207 /// handleVirtualRegisterDef)
208 void handleRegisterDef(MachineBasicBlock *MBB,
209 MachineBasicBlock::iterator MI, unsigned MIIdx,
212 /// handleVirtualRegisterDef - update intervals for a virtual
214 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
215 MachineBasicBlock::iterator MI,
217 LiveInterval& interval);
219 /// handlePhysicalRegisterDef - update intervals for a physical register
221 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
222 MachineBasicBlock::iterator mi,
224 LiveInterval &interval,
227 /// Return true if the two specified registers belong to different
228 /// register classes. The registers may be either phys or virt regs.
229 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
232 bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
233 MachineInstr *CopyMI);
235 bool overlapsAliases(const LiveInterval *lhs,
236 const LiveInterval *rhs) const;
238 static LiveInterval createInterval(unsigned Reg);
240 LiveInterval &getOrCreateInterval(unsigned reg) {
241 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
242 if (I == r2iMap_.end())
243 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
247 /// rep - returns the representative of this register
248 unsigned rep(unsigned Reg) {
249 unsigned Rep = r2rMap_[Reg];
251 return r2rMap_[Reg] = rep(Rep);
255 void printRegName(unsigned reg) const;
258 } // End llvm namespace