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