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