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 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
44 typedef std::vector<MachineInstr*> Index2MiMap;
47 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
48 Reg2IntervalMap r2iMap_;
50 typedef DenseMap<unsigned> Reg2RegMap;
53 std::vector<bool> allocatableRegs_;
58 unsigned SrcReg, DstReg;
60 CopyRec getCopyRec(MachineInstr *MI, unsigned SrcReg, unsigned DstReg) {
77 static unsigned getBaseIndex(unsigned index) {
78 return index - (index % InstrSlots::NUM);
80 static unsigned getBoundaryIndex(unsigned index) {
81 return getBaseIndex(index + InstrSlots::NUM - 1);
83 static unsigned getLoadIndex(unsigned index) {
84 return getBaseIndex(index) + InstrSlots::LOAD;
86 static unsigned getUseIndex(unsigned index) {
87 return getBaseIndex(index) + InstrSlots::USE;
89 static unsigned getDefIndex(unsigned index) {
90 return getBaseIndex(index) + InstrSlots::DEF;
92 static unsigned getStoreIndex(unsigned index) {
93 return getBaseIndex(index) + InstrSlots::STORE;
96 typedef Reg2IntervalMap::iterator iterator;
97 typedef Reg2IntervalMap::const_iterator const_iterator;
98 const_iterator begin() const { return r2iMap_.begin(); }
99 const_iterator end() const { return r2iMap_.end(); }
100 iterator begin() { return r2iMap_.begin(); }
101 iterator end() { return r2iMap_.end(); }
102 unsigned getNumIntervals() const { return r2iMap_.size(); }
104 LiveInterval &getInterval(unsigned reg) {
105 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
106 assert(I != r2iMap_.end() && "Interval does not exist for register");
110 const LiveInterval &getInterval(unsigned reg) const {
111 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
112 assert(I != r2iMap_.end() && "Interval does not exist for register");
116 /// getInstructionIndex - returns the base index of instr
117 unsigned getInstructionIndex(MachineInstr* instr) const {
118 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
119 assert(it != mi2iMap_.end() && "Invalid instruction!");
123 /// getInstructionFromIndex - given an index in any slot of an
124 /// instruction return a pointer the instruction
125 MachineInstr* getInstructionFromIndex(unsigned index) const {
126 index /= InstrSlots::NUM; // convert index to vector index
127 assert(index < i2miMap_.size() &&
128 "index does not correspond to an instruction");
129 return i2miMap_[index];
132 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
136 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
137 virtual void releaseMemory();
139 /// runOnMachineFunction - pass entry point
140 virtual bool runOnMachineFunction(MachineFunction&);
142 /// print - Implement the dump method.
143 virtual void print(std::ostream &O, const Module* = 0) const;
146 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
148 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
149 // remove index -> MachineInstr and
150 // MachineInstr -> index mappings
151 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
152 if (mi2i != mi2iMap_.end()) {
153 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
154 mi2iMap_.erase(mi2i);
158 /// computeIntervals - compute live intervals
159 void computeIntervals();
161 /// joinIntervals - join compatible live intervals
162 void joinIntervals();
164 /// CopyCoallesceInMBB - Coallsece copies in the specified MBB, putting
165 /// copies that cannot yet be coallesced into the "TryAgain" list.
166 void CopyCoallesceInMBB(MachineBasicBlock *MBB,
167 std::vector<CopyRec> &TryAgain);
168 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
169 /// which are the src/dst of the copy instruction CopyMI. This returns true
170 /// if the copy was successfully coallesced away, or if it is never possible
171 /// to coallesce these this copy, due to register constraints. It returns
172 /// false if it is not currently possible to coallesce this interval, but
173 /// it may be possible if other things get coallesced.
174 bool JoinCopy(MachineInstr *CopyMI, unsigned SrcReg, unsigned DstReg);
176 /// JoinIntervals - Attempt to join these two intervals. On failure, this
177 /// returns false. Otherwise, if one of the intervals being joined is a
178 /// physreg, this method always canonicalizes DestInt to be it. The output
179 /// "SrcInt" will not have been modified, so we can use this information
180 /// below to update aliases.
181 bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS);
183 /// SimpleJoin - Attempt to join the specified interval into this one. The
184 /// caller of this method must guarantee that the RHS only contains a single
185 /// value number and that the RHS is not defined by a copy from this
186 /// interval. This returns false if the intervals are not joinable, or it
187 /// joins them and returns true.
188 bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
190 /// handleRegisterDef - update intervals for a register def
191 /// (calls handlePhysicalRegisterDef and
192 /// handleVirtualRegisterDef)
193 void handleRegisterDef(MachineBasicBlock *MBB,
194 MachineBasicBlock::iterator MI, unsigned MIIdx,
197 /// handleVirtualRegisterDef - update intervals for a virtual
199 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
200 MachineBasicBlock::iterator MI,
202 LiveInterval& interval);
204 /// handlePhysicalRegisterDef - update intervals for a physical register
206 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
207 MachineBasicBlock::iterator mi,
209 LiveInterval &interval,
212 /// Return true if the two specified registers belong to different
213 /// register classes. The registers may be either phys or virt regs.
214 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
217 bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
218 MachineInstr *CopyMI);
220 bool overlapsAliases(const LiveInterval *lhs,
221 const LiveInterval *rhs) const;
223 static LiveInterval createInterval(unsigned Reg);
225 LiveInterval &getOrCreateInterval(unsigned reg) {
226 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
227 if (I == r2iMap_.end())
228 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
232 /// rep - returns the representative of this register
233 unsigned rep(unsigned Reg) {
234 unsigned Rep = r2rMap_[Reg];
236 return r2rMap_[Reg] = rep(Rep);
240 void printRegName(unsigned reg) const;
243 } // End llvm namespace