4420e059ea95e442421d7ae2f25ccf9e94f9e3bc
[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/ADT/DenseMap.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/LiveInterval.h"
26
27 namespace llvm {
28
29   class LiveVariables;
30   class MRegisterInfo;
31   class TargetInstrInfo;
32   class VirtRegMap;
33
34   class LiveIntervals : public MachineFunctionPass {
35     MachineFunction* mf_;
36     const TargetMachine* tm_;
37     const MRegisterInfo* mri_;
38     const TargetInstrInfo* tii_;
39     LiveVariables* lv_;
40
41     typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
42     Mi2IndexMap mi2iMap_;
43
44     typedef std::vector<MachineInstr*> Index2MiMap;
45     Index2MiMap i2miMap_;
46
47     typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
48     Reg2IntervalMap r2iMap_;
49
50     typedef DenseMap<unsigned> Reg2RegMap;
51     Reg2RegMap r2rMap_;
52
53     std::vector<bool> allocatableRegs_;
54
55   public:
56     struct InstrSlots {
57       enum {
58         LOAD  = 0,
59         USE   = 1,
60         DEF   = 2,
61         STORE = 3,
62         NUM   = 4
63       };
64     };
65
66     static unsigned getBaseIndex(unsigned index) {
67       return index - (index % InstrSlots::NUM);
68     }
69     static unsigned getBoundaryIndex(unsigned index) {
70       return getBaseIndex(index + InstrSlots::NUM - 1);
71     }
72     static unsigned getLoadIndex(unsigned index) {
73       return getBaseIndex(index) + InstrSlots::LOAD;
74     }
75     static unsigned getUseIndex(unsigned index) {
76       return getBaseIndex(index) + InstrSlots::USE;
77     }
78     static unsigned getDefIndex(unsigned index) {
79       return getBaseIndex(index) + InstrSlots::DEF;
80     }
81     static unsigned getStoreIndex(unsigned index) {
82       return getBaseIndex(index) + InstrSlots::STORE;
83     }
84
85     typedef Reg2IntervalMap::iterator iterator;
86     typedef Reg2IntervalMap::const_iterator const_iterator;
87     const_iterator begin() const { return r2iMap_.begin(); }
88     const_iterator end() const { return r2iMap_.end(); }
89     iterator begin() { return r2iMap_.begin(); }
90     iterator end() { return r2iMap_.end(); }
91     unsigned getNumIntervals() const { return r2iMap_.size(); }
92
93     LiveInterval &getInterval(unsigned reg) {
94       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
95       assert(I != r2iMap_.end() && "Interval does not exist for register");
96       return I->second;
97     }
98
99     const LiveInterval &getInterval(unsigned reg) const {
100       Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
101       assert(I != r2iMap_.end() && "Interval does not exist for register");
102       return I->second;
103     }
104
105     /// getInstructionIndex - returns the base index of instr
106     unsigned getInstructionIndex(MachineInstr* instr) const {
107       Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
108       assert(it != mi2iMap_.end() && "Invalid instruction!");
109       return it->second;
110     }
111
112     /// getInstructionFromIndex - given an index in any slot of an
113     /// instruction return a pointer the instruction
114     MachineInstr* getInstructionFromIndex(unsigned index) const {
115       index /= InstrSlots::NUM; // convert index to vector index
116       assert(index < i2miMap_.size() &&
117              "index does not correspond to an instruction");
118       return i2miMap_[index];
119     }
120
121     std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
122                                                      VirtRegMap& vrm,
123                                                      int slot);
124
125     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
126     virtual void releaseMemory();
127
128     /// runOnMachineFunction - pass entry point
129     virtual bool runOnMachineFunction(MachineFunction&);
130
131     /// print - Implement the dump method.
132     virtual void print(std::ostream &O, const Module* = 0) const;
133
134   private:
135     /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
136     /// deleted.
137     void RemoveMachineInstrFromMaps(MachineInstr *MI) {
138       // remove index -> MachineInstr and
139       // MachineInstr -> index mappings
140       Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
141       if (mi2i != mi2iMap_.end()) {
142         i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
143         mi2iMap_.erase(mi2i);
144       }
145     }
146       
147     /// computeIntervals - compute live intervals
148     void computeIntervals();
149
150     /// joinIntervals - join compatible live intervals
151     void joinIntervals();
152
153     /// CopyCoallesceInMBB - Coallsece copies in the specified MBB.
154     void CopyCoallesceInMBB(MachineBasicBlock *MBB);
155
156     /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
157     /// which are the src/dst of the copy instruction CopyMI.  This returns true
158     /// if the copy was successfully coallesced away, or if it is never possible
159     /// to coallesce these this copy, due to register constraints.  It returns
160     /// false if it is not currently possible to coallesce this interval, but
161     /// it may be possible if other things get coallesced.
162     bool JoinCopy(MachineInstr *CopyMI, unsigned SrcReg, unsigned DstReg);
163     
164     /// JoinIntervals - Attempt to join these two intervals.  On failure, this
165     /// returns false.  Otherwise, if one of the intervals being joined is a
166     /// physreg, this method always canonicalizes DestInt to be it.  The output
167     /// "SrcInt" will not have been modified, so we can use this information
168     /// below to update aliases.
169     bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS);
170     
171     /// handleRegisterDef - update intervals for a register def
172     /// (calls handlePhysicalRegisterDef and
173     /// handleVirtualRegisterDef)
174     void handleRegisterDef(MachineBasicBlock* mbb,
175                            MachineBasicBlock::iterator mi,
176                            unsigned reg);
177
178     /// handleVirtualRegisterDef - update intervals for a virtual
179     /// register def
180     void handleVirtualRegisterDef(MachineBasicBlock* mbb,
181                                   MachineBasicBlock::iterator mi,
182                                   LiveInterval& interval);
183
184     /// handlePhysicalRegisterDef - update intervals for a physical register
185     /// def.
186     void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
187                                    MachineBasicBlock::iterator mi,
188                                    LiveInterval &interval,
189                                    unsigned SrcReg);
190
191     /// Return true if the two specified registers belong to different
192     /// register classes.  The registers may be either phys or virt regs.
193     bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
194
195
196     bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
197                               MachineInstr *CopyMI);
198
199     bool overlapsAliases(const LiveInterval *lhs,
200                          const LiveInterval *rhs) const;
201
202     static LiveInterval createInterval(unsigned Reg);
203
204     LiveInterval &getOrCreateInterval(unsigned reg) {
205       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
206       if (I == r2iMap_.end())
207         I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
208       return I->second;
209     }
210
211     /// rep - returns the representative of this register
212     unsigned rep(unsigned Reg) {
213       unsigned Rep = r2rMap_[Reg];
214       if (Rep)
215         return r2rMap_[Reg] = rep(Rep);
216       return Reg;
217     }
218
219     void printRegName(unsigned reg) const;
220   };
221
222 } // End llvm namespace
223
224 #endif