f01608c176ccd2b8537a1905b88ebe21bd1bc2aa
[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     {
58       enum {
59         LOAD  = 0,
60         USE   = 1,
61         DEF   = 2,
62         STORE = 3,
63         NUM   = 4
64       };
65     };
66
67     static unsigned getBaseIndex(unsigned index) {
68       return index - (index % InstrSlots::NUM);
69     }
70     static unsigned getBoundaryIndex(unsigned index) {
71       return getBaseIndex(index + InstrSlots::NUM - 1);
72     }
73     static unsigned getLoadIndex(unsigned index) {
74       return getBaseIndex(index) + InstrSlots::LOAD;
75     }
76     static unsigned getUseIndex(unsigned index) {
77       return getBaseIndex(index) + InstrSlots::USE;
78     }
79     static unsigned getDefIndex(unsigned index) {
80       return getBaseIndex(index) + InstrSlots::DEF;
81     }
82     static unsigned getStoreIndex(unsigned index) {
83       return getBaseIndex(index) + InstrSlots::STORE;
84     }
85
86     typedef Reg2IntervalMap::iterator iterator;
87     typedef Reg2IntervalMap::const_iterator const_iterator;
88     const_iterator begin() const { return r2iMap_.begin(); }
89     const_iterator end() const { return r2iMap_.end(); }
90     iterator begin() { return r2iMap_.begin(); }
91     iterator end() { return r2iMap_.end(); }
92     unsigned getNumIntervals() const { return r2iMap_.size(); }
93
94     LiveInterval &getInterval(unsigned reg) {
95       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
96       assert(I != r2iMap_.end() && "Interval does not exist for register");
97       return I->second;
98     }
99
100     const LiveInterval &getInterval(unsigned reg) const {
101       Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
102       assert(I != r2iMap_.end() && "Interval does not exist for register");
103       return I->second;
104     }
105
106     /// getInstructionIndex - returns the base index of instr
107     unsigned getInstructionIndex(MachineInstr* instr) const {
108       Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
109       assert(it != mi2iMap_.end() && "Invalid instruction!");
110       return it->second;
111     }
112
113     /// getInstructionFromIndex - given an index in any slot of an
114     /// instruction return a pointer the instruction
115     MachineInstr* getInstructionFromIndex(unsigned index) const {
116       index /= InstrSlots::NUM; // convert index to vector index
117       assert(index < i2miMap_.size() &&
118              "index does not correspond to an instruction");
119       return i2miMap_[index];
120     }
121
122     std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
123                                                      VirtRegMap& vrm,
124                                                      int slot);
125
126     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
127     virtual void releaseMemory();
128
129     /// runOnMachineFunction - pass entry point
130     virtual bool runOnMachineFunction(MachineFunction&);
131
132     /// print - Implement the dump method.
133     virtual void print(std::ostream &O, const Module* = 0) const;
134
135   private:
136     /// computeIntervals - compute live intervals
137     void computeIntervals();
138
139     /// joinIntervals - join compatible live intervals
140     void joinIntervals();
141
142     /// joinIntervalsInMachineBB - Join intervals based on move
143     /// instructions in the specified basic block.
144     void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
145
146     /// handleRegisterDef - update intervals for a register def
147     /// (calls handlePhysicalRegisterDef and
148     /// handleVirtualRegisterDef)
149     void handleRegisterDef(MachineBasicBlock* mbb,
150                            MachineBasicBlock::iterator mi,
151                            unsigned reg);
152
153     /// handleVirtualRegisterDef - update intervals for a virtual
154     /// register def
155     void handleVirtualRegisterDef(MachineBasicBlock* mbb,
156                                   MachineBasicBlock::iterator mi,
157                                   LiveInterval& interval);
158
159     /// handlePhysicalRegisterDef - update intervals for a physical register
160     /// def.  If the defining instruction is a move instruction, SrcReg will be
161     /// the input register, and DestReg will be the result.  Note that Interval
162     /// may not match DestReg (it might be an alias instead).
163     ///
164     void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
165                                    MachineBasicBlock::iterator mi,
166                                    LiveInterval& interval,
167                                    unsigned SrcReg, unsigned DestReg,
168                                    bool isLiveIn = false);
169
170     /// Return true if the two specified registers belong to the same or
171     /// compatible register classes.  The registers may be either phys or
172     /// virt regs.
173     bool compatibleRegisterClasses(unsigned RegA, unsigned RegB,
174                                    bool &Swap) const;
175
176     bool AdjustIfAllOverlappingRangesAreCopiesFrom(LiveInterval &IntA,
177                                                    LiveInterval &IntB,
178                                                    unsigned CopyIdx);
179
180     bool overlapsAliases(const LiveInterval *lhs,
181                          const LiveInterval *rhs) const;
182
183     static LiveInterval createInterval(unsigned Reg);
184
185     LiveInterval &getOrCreateInterval(unsigned reg) {
186       Reg2IntervalMap::iterator I = r2iMap_.find(reg);
187       if (I == r2iMap_.end())
188         I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
189       return I->second;
190     }
191
192     /// rep - returns the representative of this register
193     unsigned rep(unsigned Reg) {
194       unsigned Rep = r2rMap_[Reg];
195       if (Rep)
196         return r2rMap_[Reg] = rep(Rep);
197       return Reg;
198     }
199
200     void printRegName(unsigned reg) const;
201   };
202
203 } // End llvm namespace
204
205 #endif