There are times when the coalescer would not coalesce away a copy but the copy
[oota-llvm.git] / include / llvm / CodeGen / LiveIntervalAnalysis.h
1 //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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).
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
21 #define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
22
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/LiveInterval.h"
25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Support/Allocator.h"
30
31 namespace llvm {
32
33   class LiveVariables;
34   class MRegisterInfo;
35   class TargetInstrInfo;
36   class TargetRegisterClass;
37   class VirtRegMap;
38   typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair;
39
40   class LiveIntervals : public MachineFunctionPass {
41     MachineFunction* mf_;
42     const TargetMachine* tm_;
43     const MRegisterInfo* mri_;
44     const TargetInstrInfo* tii_;
45     LiveVariables* lv_;
46
47     /// Special pool allocator for VNInfo's (LiveInterval val#).
48     ///
49     BumpPtrAllocator VNInfoAllocator;
50
51     /// MBB2IdxMap - The indexes of the first and last instructions in the
52     /// specified basic block.
53     std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
54
55     /// Idx2MBBMap - Sorted list of pairs of index of first instruction
56     /// and MBB id.
57     std::vector<IdxMBBPair> Idx2MBBMap;
58
59     typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
60     Mi2IndexMap mi2iMap_;
61
62     typedef std::vector<MachineInstr*> Index2MiMap;
63     Index2MiMap i2miMap_;
64
65     typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
66     Reg2IntervalMap r2iMap_;
67
68     BitVector allocatableRegs_;
69
70     std::vector<MachineInstr*> ClonedMIs;
71
72   public:
73     static char ID; // Pass identification, replacement for typeid
74     LiveIntervals() : MachineFunctionPass((intptr_t)&ID) {}
75
76     struct InstrSlots {
77       enum {
78         LOAD  = 0,
79         USE   = 1,
80         DEF   = 2,
81         STORE = 3,
82         NUM   = 4
83       };
84     };
85
86     static unsigned getBaseIndex(unsigned index) {
87       return index - (index % InstrSlots::NUM);
88     }
89     static unsigned getBoundaryIndex(unsigned index) {
90       return getBaseIndex(index + InstrSlots::NUM - 1);
91     }
92     static unsigned getLoadIndex(unsigned index) {
93       return getBaseIndex(index) + InstrSlots::LOAD;
94     }
95     static unsigned getUseIndex(unsigned index) {
96       return getBaseIndex(index) + InstrSlots::USE;
97     }
98     static unsigned getDefIndex(unsigned index) {
99       return getBaseIndex(index) + InstrSlots::DEF;
100     }
101     static unsigned getStoreIndex(unsigned index) {
102       return getBaseIndex(index) + InstrSlots::STORE;
103     }
104
105     typedef Reg2IntervalMap::iterator iterator;
106     typedef Reg2IntervalMap::const_iterator const_iterator;
107     const_iterator begin() const { return r2iMap_.begin(); }
108     const_iterator end() const { return r2iMap_.end(); }
109     iterator begin() { return r2iMap_.begin(); }
110     iterator end() { return r2iMap_.end(); }
111     unsigned getNumIntervals() const { return r2iMap_.size(); }
112
113     LiveInterval &getInterval(unsigned reg) {
114       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
115       assert(I != r2iMap_.end() && "Interval does not exist for register");
116       return I->second;
117     }
118
119     const LiveInterval &getInterval(unsigned reg) const {
120       Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
121       assert(I != r2iMap_.end() && "Interval does not exist for register");
122       return I->second;
123     }
124
125     bool hasInterval(unsigned reg) const {
126       return r2iMap_.count(reg);
127     }
128
129     /// getMBBStartIdx - Return the base index of the first instruction in the
130     /// specified MachineBasicBlock.
131     unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
132       return getMBBStartIdx(MBB->getNumber());
133     }
134     unsigned getMBBStartIdx(unsigned MBBNo) const {
135       assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
136       return MBB2IdxMap[MBBNo].first;
137     }
138
139     /// getMBBEndIdx - Return the store index of the last instruction in the
140     /// specified MachineBasicBlock.
141     unsigned getMBBEndIdx(MachineBasicBlock *MBB) const {
142       return getMBBEndIdx(MBB->getNumber());
143     }
144     unsigned getMBBEndIdx(unsigned MBBNo) const {
145       assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
146       return MBB2IdxMap[MBBNo].second;
147     }
148
149     /// getInstructionIndex - returns the base index of instr
150     unsigned getInstructionIndex(MachineInstr* instr) const {
151       Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
152       assert(it != mi2iMap_.end() && "Invalid instruction!");
153       return it->second;
154     }
155
156     /// getInstructionFromIndex - given an index in any slot of an
157     /// instruction return a pointer the instruction
158     MachineInstr* getInstructionFromIndex(unsigned index) const {
159       index /= InstrSlots::NUM; // convert index to vector index
160       assert(index < i2miMap_.size() &&
161              "index does not correspond to an instruction");
162       return i2miMap_[index];
163     }
164
165     /// conflictsWithPhysRegDef - Returns true if the specified register
166     /// is defined during the duration of the specified interval.
167     bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm,
168                                  unsigned reg);
169
170     /// findLiveInMBBs - Given a live range, if the value of the range
171     /// is live in any MBB returns true as well as the list of basic blocks
172     /// where the value is live in.
173     bool findLiveInMBBs(const LiveRange &LR,
174                         SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
175
176     // Interval creation
177
178     LiveInterval &getOrCreateInterval(unsigned reg) {
179       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
180       if (I == r2iMap_.end())
181         I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
182       return I->second;
183     }
184
185     std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
186                                                  VirtRegMap& vrm, unsigned reg);
187
188     // Interval removal
189
190     void removeInterval(unsigned Reg) {
191       r2iMap_.erase(Reg);
192     }
193
194     /// isRemoved - returns true if the specified machine instr has been
195     /// removed.
196     bool isRemoved(MachineInstr* instr) const {
197       return !mi2iMap_.count(instr);
198     }
199
200     /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
201     /// deleted.
202     void RemoveMachineInstrFromMaps(MachineInstr *MI) {
203       // remove index -> MachineInstr and
204       // MachineInstr -> index mappings
205       Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
206       if (mi2i != mi2iMap_.end()) {
207         i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
208         mi2iMap_.erase(mi2i);
209       }
210     }
211
212     BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
213
214     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
215     virtual void releaseMemory();
216
217     /// runOnMachineFunction - pass entry point
218     virtual bool runOnMachineFunction(MachineFunction&);
219
220     /// print - Implement the dump method.
221     virtual void print(std::ostream &O, const Module* = 0) const;
222     void print(std::ostream *O, const Module* M = 0) const {
223       if (O) print(*O, M);
224     }
225
226   private:      
227     /// computeIntervals - Compute live intervals.
228     void computeIntervals();
229     
230     /// handleRegisterDef - update intervals for a register def
231     /// (calls handlePhysicalRegisterDef and
232     /// handleVirtualRegisterDef)
233     void handleRegisterDef(MachineBasicBlock *MBB,
234                            MachineBasicBlock::iterator MI, unsigned MIIdx,
235                            unsigned reg);
236
237     /// handleVirtualRegisterDef - update intervals for a virtual
238     /// register def
239     void handleVirtualRegisterDef(MachineBasicBlock *MBB,
240                                   MachineBasicBlock::iterator MI,
241                                   unsigned MIIdx,
242                                   LiveInterval& interval);
243
244     /// handlePhysicalRegisterDef - update intervals for a physical register
245     /// def.
246     void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
247                                    MachineBasicBlock::iterator mi,
248                                    unsigned MIIdx,
249                                    LiveInterval &interval,
250                                    unsigned SrcReg);
251
252     /// handleLiveInRegister - Create interval for a livein register.
253     void handleLiveInRegister(MachineBasicBlock* mbb,
254                               unsigned MIIdx,
255                               LiveInterval &interval, bool isAlias = false);
256
257     /// isReMaterializable - Returns true if the definition MI of the specified
258     /// val# of the specified interval is re-materializable.
259     bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
260                             MachineInstr *MI);
261
262     /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
263     /// slot / to reg or any rematerialized load into ith operand of specified
264     /// MI. If it is successul, MI is updated with the newly created MI and
265     /// returns true.
266     bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
267                               MachineInstr *DefMI, unsigned index, unsigned i,
268                               bool isSS, int slot, unsigned reg);
269
270     static LiveInterval createInterval(unsigned Reg);
271
272     void printRegName(unsigned reg) const;
273   };
274
275 } // End llvm namespace
276
277 #endif