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