8181a2093d4426bbd45270f48c49f36a8741e3f7
[oota-llvm.git] / include / llvm / CodeGen / LiveInterval.h
1 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- 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 LiveRange and LiveInterval classes.  Given some
11 // numbering of each the machine instructions an interval [i, j) is said to be a
12 // live interval for register v if there is no instruction with number j' > j
13 // such that v is live at j' and there is no instruction with number i' < i such
14 // that v is live at i'. In this implementation intervals can have holes,
15 // i.e. an interval might look like [1,20), [50,65), [1000,1001).  Each
16 // individual range is represented as an instance of LiveRange, and the whole
17 // interval is represented as an instance of LiveInterval.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22 #define LLVM_CODEGEN_LIVEINTERVAL_H
23
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Streams.h"
26 #include <iosfwd>
27 #include <vector>
28 #include <cassert>
29 #include <ostream>
30
31 namespace llvm {
32   class MRegisterInfo;
33
34   /// LiveRange structure - This represents a simple register range in the
35   /// program, with an inclusive start point and an exclusive end point.
36   /// These ranges are rendered as [start,end).
37   struct LiveRange {
38     unsigned start;  // Start point of the interval (inclusive)
39     unsigned end;    // End point of the interval (exclusive)
40     unsigned ValId;  // identifier for the value contained in this interval.
41
42     LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
43       assert(S < E && "Cannot create empty or backwards range");
44     }
45
46     /// contains - Return true if the index is covered by this range.
47     ///
48     bool contains(unsigned I) const {
49       return start <= I && I < end;
50     }
51
52     bool operator<(const LiveRange &LR) const {
53       return start < LR.start || (start == LR.start && end < LR.end);
54     }
55     bool operator==(const LiveRange &LR) const {
56       return start == LR.start && end == LR.end;
57     }
58
59     void dump() const;
60
61   private:
62     LiveRange(); // DO NOT IMPLEMENT
63   };
64
65   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
66   inline OStream& operator<<(OStream& os, const LiveRange &LR) {
67     if (os.stream()) *os.stream() << LR;
68     return os;
69   }
70
71   inline bool operator<(unsigned V, const LiveRange &LR) {
72     return V < LR.start;
73   }
74
75   inline bool operator<(const LiveRange &LR, unsigned V) {
76     return LR.start < V;
77   }
78
79   /// LiveInterval - This class represents some number of live ranges for a
80   /// register or value.  This class also contains a bit of register allocator
81   /// state.
82   struct LiveInterval {
83     typedef SmallVector<LiveRange,4> Ranges;
84     unsigned reg;        // the register of this interval
85     float weight;        // weight of this interval
86     Ranges ranges;       // the ranges in which this register is live
87   private:
88     /// ValueNumberInfo - If this value number is not defined by a copy, this
89     /// holds ~0,x.  If the value number is not in use, it contains ~1,x to
90     /// indicate that the value # is not used.  If the val# is defined by a
91     /// copy, the first entry is the instruction # of the copy, and the second
92     /// is the register number copied from.
93     SmallVector<std::pair<unsigned,unsigned>, 4> ValueNumberInfo;
94   public:
95
96     LiveInterval(unsigned Reg, float Weight)
97       : reg(Reg), weight(Weight) {
98     }
99
100     typedef Ranges::iterator iterator;
101     iterator begin() { return ranges.begin(); }
102     iterator end()   { return ranges.end(); }
103
104     typedef Ranges::const_iterator const_iterator;
105     const_iterator begin() const { return ranges.begin(); }
106     const_iterator end() const  { return ranges.end(); }
107
108
109     /// advanceTo - Advance the specified iterator to point to the LiveRange
110     /// containing the specified position, or end() if the position is past the
111     /// end of the interval.  If no LiveRange contains this position, but the
112     /// position is in a hole, this method returns an iterator pointing the the
113     /// LiveRange immediately after the hole.
114     iterator advanceTo(iterator I, unsigned Pos) {
115       if (Pos >= endNumber())
116         return end();
117       while (I->end <= Pos) ++I;
118       return I;
119     }
120
121     void swap(LiveInterval& other) {
122       std::swap(reg, other.reg);
123       std::swap(weight, other.weight);
124       std::swap(ranges, other.ranges);
125       std::swap(ValueNumberInfo, other.ValueNumberInfo);
126     }
127
128     bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
129
130     unsigned getNumValNums() const { return ValueNumberInfo.size(); }
131     
132     /// getNextValue - Create a new value number and return it.  MIIdx specifies
133     /// the instruction that defines the value number.
134     unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
135       ValueNumberInfo.push_back(std::make_pair(MIIdx, SrcReg));
136       return ValueNumberInfo.size()-1;
137     }
138     
139     /// getInstForValNum - Return the machine instruction index that defines the
140     /// specified value number.
141     unsigned getInstForValNum(unsigned ValNo) const {
142       //assert(ValNo < ValueNumberInfo.size());
143       return ValueNumberInfo[ValNo].first;
144     }
145     
146     unsigned getSrcRegForValNum(unsigned ValNo) const {
147       //assert(ValNo < ValueNumberInfo.size());
148       if (ValueNumberInfo[ValNo].first < ~2U)
149         return ValueNumberInfo[ValNo].second;
150       return 0;
151     }
152     
153     std::pair<unsigned, unsigned> getValNumInfo(unsigned ValNo) const {
154       //assert(ValNo < ValueNumberInfo.size());
155       return ValueNumberInfo[ValNo];
156     }
157     
158     /// setValueNumberInfo - Change the value number info for the specified
159     /// value number.
160     void setValueNumberInfo(unsigned ValNo,
161                             const std::pair<unsigned, unsigned> &I){
162       ValueNumberInfo[ValNo] = I;
163     }
164     
165     /// MergeValueNumberInto - This method is called when two value nubmers
166     /// are found to be equivalent.  This eliminates V1, replacing all
167     /// LiveRanges with the V1 value number with the V2 value number.  This can
168     /// cause merging of V1/V2 values numbers and compaction of the value space.
169     void MergeValueNumberInto(unsigned V1, unsigned V2);
170
171     /// MergeInClobberRanges - For any live ranges that are not defined in the
172     /// current interval, but are defined in the Clobbers interval, mark them
173     /// used with an unknown definition value.
174     void MergeInClobberRanges(const LiveInterval &Clobbers);
175
176     
177     /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
178     /// interval as the specified value number.  The LiveRanges in RHS are
179     /// allowed to overlap with LiveRanges in the current interval, but only if
180     /// the overlapping LiveRanges have the specified value number.
181     void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
182     
183     bool empty() const { return ranges.empty(); }
184
185     /// beginNumber - Return the lowest numbered slot covered by interval.
186     unsigned beginNumber() const {
187       assert(!empty() && "empty interval for register");
188       return ranges.front().start;
189     }
190
191     /// endNumber - return the maximum point of the interval of the whole,
192     /// exclusive.
193     unsigned endNumber() const {
194       assert(!empty() && "empty interval for register");
195       return ranges.back().end;
196     }
197
198     bool expiredAt(unsigned index) const {
199       return index >= endNumber();
200     }
201
202     bool liveAt(unsigned index) const;
203
204     /// getLiveRangeContaining - Return the live range that contains the
205     /// specified index, or null if there is none.
206     const LiveRange *getLiveRangeContaining(unsigned Idx) const {
207       const_iterator I = FindLiveRangeContaining(Idx);
208       return I == end() ? 0 : &*I;
209     }
210
211     /// FindLiveRangeContaining - Return an iterator to the live range that
212     /// contains the specified index, or end() if there is none.
213     const_iterator FindLiveRangeContaining(unsigned Idx) const;
214
215     /// FindLiveRangeContaining - Return an iterator to the live range that
216     /// contains the specified index, or end() if there is none.
217     iterator FindLiveRangeContaining(unsigned Idx);
218     
219     /// getOverlapingRanges - Given another live interval which is defined as a
220     /// copy from this one, return a list of all of the live ranges where the
221     /// two overlap and have different value numbers.
222     void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
223                              std::vector<LiveRange*> &Ranges);
224
225     /// overlaps - Return true if the intersection of the two live intervals is
226     /// not empty.
227     bool overlaps(const LiveInterval& other) const {
228       return overlapsFrom(other, other.begin());
229     }
230
231     /// overlapsFrom - Return true if the intersection of the two live intervals
232     /// is not empty.  The specified iterator is a hint that we can begin
233     /// scanning the Other interval starting at I.
234     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
235
236     /// addRange - Add the specified LiveRange to this interval, merging
237     /// intervals as appropriate.  This returns an iterator to the inserted live
238     /// range (which may have grown since it was inserted.
239     void addRange(LiveRange LR) {
240       addRangeFrom(LR, ranges.begin());
241     }
242
243     /// join - Join two live intervals (this, and other) together.  This applies
244     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
245     /// the intervals are not joinable, this aborts.
246     void join(LiveInterval &Other, int *ValNoAssignments,
247               int *RHSValNoAssignments,
248               SmallVector<std::pair<unsigned,unsigned>,16> &NewValueNumberInfo);
249
250     /// removeRange - Remove the specified range from this interval.  Note that
251     /// the range must already be in this interval in its entirety.
252     void removeRange(unsigned Start, unsigned End);
253
254     void removeRange(LiveRange LR) {
255       removeRange(LR.start, LR.end);
256     }
257
258     bool operator<(const LiveInterval& other) const {
259       return beginNumber() < other.beginNumber();
260     }
261
262     void print(OStream OS, const MRegisterInfo *MRI = 0) const;
263     void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const {
264       print(OStream(OS), MRI);
265     }
266     void dump() const;
267
268   private:
269     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
270     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
271     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
272     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
273   };
274
275   inline OStream &operator<<(OStream &OS, const LiveInterval &LI) {
276     LI.print(OS);
277     return OS;
278   }
279
280   inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
281     LI.print(OS);
282     return OS;
283   }
284 }
285
286 #endif