Revert 51775.
[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 is distributed under the University of Illinois Open Source
6 // 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/Allocator.h"
26 #include <iosfwd>
27 #include <cassert>
28
29 namespace llvm {
30   class MachineInstr;
31   class TargetRegisterInfo;
32   struct LiveInterval;
33
34   /// VNInfo - If the value number definition is undefined (e.g. phi
35   /// merge point), it contains ~0u,x. If the value number is not in use, it
36   /// contains ~1u,x to indicate that the value # is not used. 
37   ///   def   - Instruction # of the definition.
38   ///   copy  - Copy iff val# is defined by a copy; zero otherwise.
39   ///   hasPHIKill - One or more of the kills are PHI nodes.
40   ///   kills - Instruction # of the kills.
41   struct VNInfo {
42     unsigned id;
43     unsigned def;
44     MachineInstr *copy;
45     bool hasPHIKill;
46     SmallVector<unsigned, 4> kills;
47     VNInfo() : id(~1U), def(~1U), copy(0), hasPHIKill(false) {}
48     VNInfo(unsigned i, unsigned d, MachineInstr *c)
49       : id(i), def(d), copy(c), hasPHIKill(false) {}
50   };
51
52   /// LiveRange structure - This represents a simple register range in the
53   /// program, with an inclusive start point and an exclusive end point.
54   /// These ranges are rendered as [start,end).
55   struct LiveRange {
56     unsigned start;  // Start point of the interval (inclusive)
57     unsigned end;    // End point of the interval (exclusive)
58     VNInfo *valno;   // identifier for the value contained in this interval.
59
60     LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
61       assert(S < E && "Cannot create empty or backwards range");
62     }
63
64     /// contains - Return true if the index is covered by this range.
65     ///
66     bool contains(unsigned I) const {
67       return start <= I && I < end;
68     }
69
70     bool operator<(const LiveRange &LR) const {
71       return start < LR.start || (start == LR.start && end < LR.end);
72     }
73     bool operator==(const LiveRange &LR) const {
74       return start == LR.start && end == LR.end;
75     }
76
77     void dump() const;
78     void print(std::ostream &os) const;
79     void print(std::ostream *os) const { if (os) print(*os); }
80
81   private:
82     LiveRange(); // DO NOT IMPLEMENT
83   };
84
85   std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
86
87
88   inline bool operator<(unsigned V, const LiveRange &LR) {
89     return V < LR.start;
90   }
91
92   inline bool operator<(const LiveRange &LR, unsigned V) {
93     return LR.start < V;
94   }
95
96   /// LiveInterval - This class represents some number of live ranges for a
97   /// register or value.  This class also contains a bit of register allocator
98   /// state.
99   struct LiveInterval {
100     typedef SmallVector<LiveRange,4> Ranges;
101     typedef SmallVector<VNInfo*,4> VNInfoList;
102
103     unsigned reg;        // the register of this interval
104     unsigned preference; // preferred register to allocate for this interval
105     float weight;        // weight of this interval
106     Ranges ranges;       // the ranges in which this register is live
107     VNInfoList valnos;   // value#'s
108
109   public:
110     LiveInterval(unsigned Reg, float Weight)
111       : reg(Reg), preference(0), weight(Weight) {
112     }
113
114     typedef Ranges::iterator iterator;
115     iterator begin() { return ranges.begin(); }
116     iterator end()   { return ranges.end(); }
117
118     typedef Ranges::const_iterator const_iterator;
119     const_iterator begin() const { return ranges.begin(); }
120     const_iterator end() const  { return ranges.end(); }
121
122     typedef VNInfoList::iterator vni_iterator;
123     vni_iterator vni_begin() { return valnos.begin(); }
124     vni_iterator vni_end() { return valnos.end(); }
125
126     typedef VNInfoList::const_iterator const_vni_iterator;
127     const_vni_iterator vni_begin() const { return valnos.begin(); }
128     const_vni_iterator vni_end() const { return valnos.end(); }
129
130     /// advanceTo - Advance the specified iterator to point to the LiveRange
131     /// containing the specified position, or end() if the position is past the
132     /// end of the interval.  If no LiveRange contains this position, but the
133     /// position is in a hole, this method returns an iterator pointing the the
134     /// LiveRange immediately after the hole.
135     iterator advanceTo(iterator I, unsigned Pos) {
136       if (Pos >= endNumber())
137         return end();
138       while (I->end <= Pos) ++I;
139       return I;
140     }
141
142     bool containsOneValue() const { return valnos.size() == 1; }
143
144     unsigned getNumValNums() const { return (unsigned)valnos.size(); }
145     
146     /// getValNumInfo - Returns pointer to the specified val#.
147     ///
148     inline VNInfo *getValNumInfo(unsigned ValNo) {
149       return valnos[ValNo];
150     }
151     inline const VNInfo *getValNumInfo(unsigned ValNo) const {
152       return valnos[ValNo];
153     }
154     
155     /// copyValNumInfo - Copy the value number info for one value number to
156     /// another.
157     void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
158       DstValNo->def = SrcValNo->def;
159       DstValNo->copy = SrcValNo->copy;
160       DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
161       DstValNo->kills = SrcValNo->kills;
162     }
163
164     /// getNextValue - Create a new value number and return it.  MIIdx specifies
165     /// the instruction that defines the value number.
166     VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
167                          BumpPtrAllocator &VNInfoAllocator) {
168 #ifdef __GNUC__
169       unsigned Alignment = (unsigned)__alignof__(VNInfo);
170 #else
171       // FIXME: ugly.
172       unsigned Alignment = 8;
173 #endif
174       VNInfo *VNI =
175         static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
176                                                       Alignment));
177       new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
178       valnos.push_back(VNI);
179       return VNI;
180     }
181
182     /// addKillForValNum - Add a kill instruction index to the specified value
183     /// number.
184     static void addKill(VNInfo *VNI, unsigned KillIdx) {
185       SmallVector<unsigned, 4> &kills = VNI->kills;
186       if (kills.empty()) {
187         kills.push_back(KillIdx);
188       } else {
189         SmallVector<unsigned, 4>::iterator
190           I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
191         kills.insert(I, KillIdx);
192       }
193     }
194
195     /// addKills - Add a number of kills into the VNInfo kill vector. If this
196     /// interval is live at a kill point, then the kill is not added.
197     void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
198       for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
199            i != e; ++i) {
200         unsigned KillIdx = kills[i];
201         if (!liveBeforeAndAt(KillIdx)) {
202           SmallVector<unsigned, 4>::iterator
203             I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
204           VNI->kills.insert(I, KillIdx);
205         }
206       }
207     }
208
209     /// removeKill - Remove the specified kill from the list of kills of
210     /// the specified val#.
211     static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
212       SmallVector<unsigned, 4> &kills = VNI->kills;
213       SmallVector<unsigned, 4>::iterator
214         I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
215       if (I != kills.end() && *I == KillIdx) {
216         kills.erase(I);
217         return true;
218       }
219       return false;
220     }
221
222     /// removeKills - Remove all the kills in specified range
223     /// [Start, End] of the specified val#.
224     void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
225       SmallVector<unsigned, 4> &kills = VNI->kills;
226       SmallVector<unsigned, 4>::iterator
227         I = std::lower_bound(kills.begin(), kills.end(), Start);
228       SmallVector<unsigned, 4>::iterator
229         E = std::upper_bound(kills.begin(), kills.end(), End);
230       kills.erase(I, E);
231     }
232
233     /// isKill - Return true if the specified index is a kill of the
234     /// specified val#.
235     bool isKill(const VNInfo *VNI, unsigned KillIdx) const {
236       const SmallVector<unsigned, 4> &kills = VNI->kills;
237       SmallVector<unsigned, 4>::const_iterator
238         I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
239       return I != kills.end() && *I == KillIdx;
240     }
241     
242     /// MergeValueNumberInto - This method is called when two value nubmers
243     /// are found to be equivalent.  This eliminates V1, replacing all
244     /// LiveRanges with the V1 value number with the V2 value number.  This can
245     /// cause merging of V1/V2 values numbers and compaction of the value space.
246     void MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
247
248     /// MergeInClobberRanges - For any live ranges that are not defined in the
249     /// current interval, but are defined in the Clobbers interval, mark them
250     /// used with an unknown definition value. Caller must pass in reference to
251     /// VNInfoAllocator since it will create a new val#.
252     void MergeInClobberRanges(const LiveInterval &Clobbers,
253                               BumpPtrAllocator &VNInfoAllocator);
254
255     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
256     /// in RHS into this live interval as the specified value number.
257     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
258     /// current interval, it will replace the value numbers of the overlaped
259     /// live ranges with the specified value number.
260     void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
261
262     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
263     /// in RHS into this live interval as the specified value number.
264     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
265     /// current interval, but only if the overlapping LiveRanges have the
266     /// specified value number.
267     void MergeValueInAsValue(const LiveInterval &RHS,
268                              const VNInfo *RHSValNo, VNInfo *LHSValNo);
269
270     /// Copy - Copy the specified live interval. This copies all the fields
271     /// except for the register of the interval.
272     void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator);
273     
274     bool empty() const { return ranges.empty(); }
275
276     /// beginNumber - Return the lowest numbered slot covered by interval.
277     unsigned beginNumber() const {
278       if (empty())
279         return 0;
280       return ranges.front().start;
281     }
282
283     /// endNumber - return the maximum point of the interval of the whole,
284     /// exclusive.
285     unsigned endNumber() const {
286       if (empty())
287         return 0;
288       return ranges.back().end;
289     }
290
291     bool expiredAt(unsigned index) const {
292       return index >= endNumber();
293     }
294
295     bool liveAt(unsigned index) const;
296
297     // liveBeforeAndAt - Check if the interval is live at the index and the
298     // index just before it. If index is liveAt, check if it starts a new live
299     // range.If it does, then check if the previous live range ends at index-1.
300     bool liveBeforeAndAt(unsigned index) const;
301
302     /// getLiveRangeContaining - Return the live range that contains the
303     /// specified index, or null if there is none.
304     const LiveRange *getLiveRangeContaining(unsigned Idx) const {
305       const_iterator I = FindLiveRangeContaining(Idx);
306       return I == end() ? 0 : &*I;
307     }
308
309     /// FindLiveRangeContaining - Return an iterator to the live range that
310     /// contains the specified index, or end() if there is none.
311     const_iterator FindLiveRangeContaining(unsigned Idx) const;
312
313     /// FindLiveRangeContaining - Return an iterator to the live range that
314     /// contains the specified index, or end() if there is none.
315     iterator FindLiveRangeContaining(unsigned Idx);
316     
317     /// overlaps - Return true if the intersection of the two live intervals is
318     /// not empty.
319     bool overlaps(const LiveInterval& other) const {
320       return overlapsFrom(other, other.begin());
321     }
322
323     /// overlapsFrom - Return true if the intersection of the two live intervals
324     /// is not empty.  The specified iterator is a hint that we can begin
325     /// scanning the Other interval starting at I.
326     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
327
328     /// addRange - Add the specified LiveRange to this interval, merging
329     /// intervals as appropriate.  This returns an iterator to the inserted live
330     /// range (which may have grown since it was inserted.
331     void addRange(LiveRange LR) {
332       addRangeFrom(LR, ranges.begin());
333     }
334
335     /// join - Join two live intervals (this, and other) together.  This applies
336     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
337     /// the intervals are not joinable, this aborts.
338     void join(LiveInterval &Other, const int *ValNoAssignments,
339               const int *RHSValNoAssignments,
340               SmallVector<VNInfo*, 16> &NewVNInfo);
341
342     /// removeRange - Remove the specified range from this interval.  Note that
343     /// the range must already be in this interval in its entirety.
344     void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
345
346     void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
347       removeRange(LR.start, LR.end, RemoveDeadValNo);
348     }
349
350     /// removeValNo - Remove all the ranges defined by the specified value#.
351     /// Also remove the value# from value# list.
352     void removeValNo(VNInfo *ValNo);
353
354     /// getSize - Returns the sum of sizes of all the LiveRange's.
355     ///
356     unsigned getSize() const;
357
358     bool operator<(const LiveInterval& other) const {
359       return beginNumber() < other.beginNumber();
360     }
361
362     void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
363     void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
364       if (OS) print(*OS, TRI);
365     }
366     void dump() const;
367
368   private:
369     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
370     void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
371     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
372     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
373   };
374
375   inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
376     LI.print(OS);
377     return OS;
378   }
379 }
380
381 #endif