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