Use newly added API in MRegisterInfo.
[oota-llvm.git] / lib / 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 "LiveInterval.h"
25
26 namespace llvm {
27
28   class LiveVariables;
29   class MRegisterInfo;
30   class VirtRegMap;
31
32   class LiveIntervals : public MachineFunctionPass {
33     MachineFunction* mf_;
34     const TargetMachine* tm_;
35     const MRegisterInfo* mri_;
36     LiveVariables* lv_;
37
38     typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
39     Mi2IndexMap mi2iMap_;
40
41     typedef std::vector<MachineInstr*> Index2MiMap;
42     Index2MiMap i2miMap_;
43
44     typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
45     Reg2IntervalMap r2iMap_;
46
47     typedef std::map<unsigned, unsigned> Reg2RegMap;
48     Reg2RegMap r2rMap_;
49
50     std::vector<bool> allocatableRegs_;
51
52   public:
53     struct InstrSlots
54     {
55       enum {
56         LOAD  = 0,
57         USE   = 1,
58         DEF   = 2,
59         STORE = 3,
60         NUM   = 4,
61       };
62     };
63
64     static unsigned getBaseIndex(unsigned index) {
65       return index - (index % InstrSlots::NUM);
66     }
67     static unsigned getBoundaryIndex(unsigned index) {
68       return getBaseIndex(index + InstrSlots::NUM - 1);
69     }
70     static unsigned getLoadIndex(unsigned index) {
71       return getBaseIndex(index) + InstrSlots::LOAD;
72     }
73     static unsigned getUseIndex(unsigned index) {
74       return getBaseIndex(index) + InstrSlots::USE;
75     }
76     static unsigned getDefIndex(unsigned index) {
77       return getBaseIndex(index) + InstrSlots::DEF;
78     }
79     static unsigned getStoreIndex(unsigned index) {
80       return getBaseIndex(index) + InstrSlots::STORE;
81     }
82
83     // FIXME: this should really be a const_iterator
84     typedef Reg2IntervalMap::iterator iterator;
85     iterator begin() { return r2iMap_.begin(); }
86     iterator end() { return r2iMap_.end(); }
87     unsigned getNumIntervals() const { return r2iMap_.size(); }
88
89     LiveInterval &getInterval(unsigned reg) {
90       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
91       assert(I != r2iMap_.end() && "Interval does not exist for register");
92       return I->second;
93     }
94
95     const LiveInterval &getInterval(unsigned reg) const {
96       Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
97       assert(I != r2iMap_.end() && "Interval does not exist for register");
98       return I->second;
99     }
100
101     /// getInstructionIndex - returns the base index of instr
102     unsigned getInstructionIndex(MachineInstr* instr) const {
103       Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
104       assert(it != mi2iMap_.end() && "Invalid instruction!");
105       return it->second;
106     }
107
108     /// getInstructionFromIndex - given an index in any slot of an
109     /// instruction return a pointer the instruction
110     MachineInstr* getInstructionFromIndex(unsigned index) const {
111       index /= InstrSlots::NUM; // convert index to vector index
112       assert(index < i2miMap_.size() &&
113              "index does not correspond to an instruction");
114       return i2miMap_[index];
115     }
116
117     std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
118                                                      VirtRegMap& vrm,
119                                                      int slot);
120
121     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
122     virtual void releaseMemory();
123
124     /// runOnMachineFunction - pass entry point
125     virtual bool runOnMachineFunction(MachineFunction&);
126
127   private:
128     /// computeIntervals - compute live intervals
129     void computeIntervals();
130
131     /// joinIntervals - join compatible live intervals
132     void joinIntervals();
133
134     /// joinIntervalsInMachineBB - Join intervals based on move
135     /// instructions in the specified basic block.
136     void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
137
138     /// handleRegisterDef - update intervals for a register def
139     /// (calls handlePhysicalRegisterDef and
140     /// handleVirtualRegisterDef)
141     void handleRegisterDef(MachineBasicBlock* mbb,
142                            MachineBasicBlock::iterator mi,
143                            unsigned reg);
144
145     /// handleVirtualRegisterDef - update intervals for a virtual
146     /// register def
147     void handleVirtualRegisterDef(MachineBasicBlock* mbb,
148                                   MachineBasicBlock::iterator mi,
149                                   LiveInterval& interval);
150
151     /// handlePhysicalRegisterDef - update intervals for a
152     /// physical register def
153     void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
154                                    MachineBasicBlock::iterator mi,
155                                    LiveInterval& interval);
156
157     /// Return true if the two specified registers belong to different
158     /// register classes.  The registers may be either phys or virt regs.
159     bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
160
161     bool overlapsAliases(const LiveInterval *lhs,
162                          const LiveInterval *rhs) const;
163
164     static LiveInterval createInterval(unsigned Reg);
165
166     LiveInterval &getOrCreateInterval(unsigned reg) {
167       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
168       if (I == r2iMap_.end())
169         I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
170       return I->second;
171     }
172
173     /// rep - returns the representative of this register
174     unsigned rep(unsigned reg) {
175       Reg2RegMap::iterator it = r2rMap_.find(reg);
176       if (it != r2rMap_.end())
177         return it->second = rep(it->second);
178       return reg;
179     }
180
181     void printRegName(unsigned reg) const;
182   };
183
184 } // End llvm namespace
185
186 #endif