Add LiveIntervals::Interval::empty() member function.
[oota-llvm.git] / include / llvm / CodeGen / LiveIntervalAnalysis.h
1 //===-- llvm/CodeGen/LiveInterval.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
11 // numbering of each the machine instructions (in this implemention
12 // depth-first order) an interval [i, j) is said to be a live interval
13 // for register v if there is no instruction with number j' > j such
14 // that v is live at j' abd there is no instruction with number i' < i
15 // such that v is live at i'. In this implementation intervals can
16 // have holes, i.e. an interval might look like [1,20), [50,65),
17 // [1000,1001)
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_CODEGEN_LIVEINTERVALS_H
22 #define LLVM_CODEGEN_LIVEINTERVALS_H
23
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include <list>
26
27 namespace llvm {
28
29     class LiveVariables;
30     class MRegisterInfo;
31
32     class LiveIntervals : public MachineFunctionPass
33     {
34     public:
35         struct Interval {
36             typedef std::pair<unsigned, unsigned> Range;
37             typedef std::vector<Range> Ranges;
38             unsigned reg;   // the register of this interval
39             float weight;   // weight of this interval (number of uses
40                             // * 10^loopDepth)
41             Ranges ranges;  // the ranges in which this register is live
42
43             Interval(unsigned r);
44
45             bool empty() const { return ranges.empty(); }
46
47             unsigned start() const {
48                 assert(!empty() && "empty interval for register");
49                 return ranges.front().first;
50             }
51
52             unsigned end() const {
53                 assert(!empty() && "empty interval for register");
54                 return ranges.back().second;
55             }
56
57             bool expiredAt(unsigned index) const {
58                 return end() <= (index + 1);
59             }
60
61             bool liveAt(unsigned index) const;
62
63             bool overlaps(const Interval& other) const;
64
65             void addRange(unsigned start, unsigned end);
66
67             void join(const Interval& other);
68
69         private:
70             Ranges::iterator mergeRangesForward(Ranges::iterator it);
71
72             Ranges::iterator mergeRangesBackward(Ranges::iterator it);
73         };
74
75         struct StartPointComp {
76             bool operator()(const Interval& lhs, const Interval& rhs) {
77                 return lhs.ranges.front().first < rhs.ranges.front().first;
78             }
79         };
80
81         struct EndPointComp {
82             bool operator()(const Interval& lhs, const Interval& rhs) {
83                 return lhs.ranges.back().second < rhs.ranges.back().second;
84             }
85         };
86
87         typedef std::list<Interval> Intervals;
88         typedef std::map<unsigned, unsigned> Reg2RegMap;
89         typedef std::vector<MachineBasicBlock*> MachineBasicBlockPtrs;
90
91     private:
92         MachineFunction* mf_;
93         const TargetMachine* tm_;
94         const MRegisterInfo* mri_;
95         MachineBasicBlock* currentMbb_;
96         MachineBasicBlock::iterator currentInstr_;
97         LiveVariables* lv_;
98
99         typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
100         MbbIndex2MbbMap mbbi2mbbMap_;
101
102         typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
103         Mi2IndexMap mi2iMap_;
104
105         typedef std::vector<MachineInstr*> Index2MiMap;
106         Index2MiMap i2miMap_;
107
108         typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
109         Reg2IntervalMap r2iMap_;
110
111         Reg2RegMap r2rMap_;
112
113         Intervals intervals_;
114
115     public:
116         virtual void getAnalysisUsage(AnalysisUsage &AU) const;
117         virtual void releaseMemory();
118
119         /// runOnMachineFunction - pass entry point
120         virtual bool runOnMachineFunction(MachineFunction&);
121
122         unsigned getInstructionIndex(MachineInstr* instr) const;
123
124         MachineInstr* getInstructionFromIndex(unsigned index) const;
125
126         Intervals& getIntervals() { return intervals_; }
127
128         void updateSpilledInterval(Interval& i);
129
130     private:
131         /// computeIntervals - compute live intervals
132         void computeIntervals();
133
134         /// joinIntervals - join compatible live intervals
135         void joinIntervals();
136
137         /// handleRegisterDef - update intervals for a register def
138         /// (calls handlePhysicalRegisterDef and
139         /// handleVirtualRegisterDef)
140         void handleRegisterDef(MachineBasicBlock* mbb,
141                                MachineBasicBlock::iterator mi,
142                                unsigned reg);
143
144         /// handleVirtualRegisterDef - update intervals for a virtual
145         /// register def
146         void handleVirtualRegisterDef(MachineBasicBlock* mbb,
147                                       MachineBasicBlock::iterator mi,
148                                       unsigned reg);
149
150         /// handlePhysicalRegisterDef - update intervals for a
151         /// physical register def
152         void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
153                                        MachineBasicBlock::iterator mi,
154                                        unsigned reg);
155
156         bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
157
158         /// rep - returns the representative of this register
159         unsigned rep(unsigned reg);
160
161         void printRegName(unsigned reg) const;
162     };
163
164     inline bool operator==(const LiveIntervals::Interval& lhs,
165                            const LiveIntervals::Interval& rhs) {
166         return lhs.reg == rhs.reg;
167     }
168
169     std::ostream& operator<<(std::ostream& os,
170                              const LiveIntervals::Interval& li);
171
172 } // End llvm namespace
173
174 #endif