Removed VNInfo::isDefAccurate(). Def "accuracy" can be checked by testing whether...
[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 "llvm/Support/AlignOf.h"
27 #include "llvm/CodeGen/SlotIndexes.h"
28 #include <cassert>
29 #include <climits>
30
31 namespace llvm {
32   class LiveIntervals;
33   class MachineInstr;
34   class MachineRegisterInfo;
35   class TargetRegisterInfo;
36   class raw_ostream;
37
38   /// VNInfo - Value Number Information.
39   /// This class holds information about a machine level values, including
40   /// definition and use points.
41   ///
42   class VNInfo {
43   private:
44     enum {
45       HAS_PHI_KILL    = 1,
46       REDEF_BY_EC     = 1 << 1,
47       IS_PHI_DEF      = 1 << 2,
48       IS_UNUSED       = 1 << 3
49     };
50
51     MachineInstr *copy;
52     unsigned char flags;
53
54   public:
55     typedef BumpPtrAllocator Allocator;
56
57     /// The ID number of this value.
58     unsigned id;
59
60     /// The index of the defining instruction (if isDefAccurate() returns true).
61     SlotIndex def;
62
63     /// VNInfo constructor.
64     VNInfo(unsigned i, SlotIndex d, MachineInstr *c)
65       : copy(c), flags(0), id(i), def(d)
66     { }
67
68     /// VNInfo construtor, copies values from orig, except for the value number.
69     VNInfo(unsigned i, const VNInfo &orig)
70       : copy(orig.copy), flags(orig.flags), id(i), def(orig.def)
71     { }
72
73     /// Copy from the parameter into this VNInfo.
74     void copyFrom(VNInfo &src) {
75       flags = src.flags;
76       copy = src.copy;
77       def = src.def;
78     }
79
80     /// Used for copying value number info.
81     unsigned getFlags() const { return flags; }
82     void setFlags(unsigned flags) { this->flags = flags; }
83
84     /// For a register interval, if this VN was definied by a copy instr
85     /// getCopy() returns a pointer to it, otherwise returns 0.
86     /// For a stack interval the behaviour of this method is undefined.
87     MachineInstr* getCopy() const { return copy; }
88     /// For a register interval, set the copy member.
89     /// This method should not be called on stack intervals as it may lead to
90     /// undefined behavior.
91     void setCopy(MachineInstr *c) { copy = c; }
92
93     /// Returns true if one or more kills are PHI nodes.
94     bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
95     /// Set the PHI kill flag on this value.
96     void setHasPHIKill(bool hasKill) {
97       if (hasKill)
98         flags |= HAS_PHI_KILL;
99       else
100         flags &= ~HAS_PHI_KILL;
101     }
102
103     /// Returns true if this value is re-defined by an early clobber somewhere
104     /// during the live range.
105     bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
106     /// Set the "redef by early clobber" flag on this value.
107     void setHasRedefByEC(bool hasRedef) {
108       if (hasRedef)
109         flags |= REDEF_BY_EC;
110       else
111         flags &= ~REDEF_BY_EC;
112     }
113
114     /// Returns true if this value is defined by a PHI instruction (or was,
115     /// PHI instrucions may have been eliminated).
116     bool isPHIDef() const { return flags & IS_PHI_DEF; }
117     /// Set the "phi def" flag on this value.
118     void setIsPHIDef(bool phiDef) {
119       if (phiDef)
120         flags |= IS_PHI_DEF;
121       else
122         flags &= ~IS_PHI_DEF;
123     }
124
125     /// Returns true if this value is unused.
126     bool isUnused() const { return flags & IS_UNUSED; }
127     /// Set the "is unused" flag on this value.
128     void setIsUnused(bool unused) {
129       if (unused)
130         flags |= IS_UNUSED;
131       else
132         flags &= ~IS_UNUSED;
133     }
134   };
135
136   /// LiveRange structure - This represents a simple register range in the
137   /// program, with an inclusive start point and an exclusive end point.
138   /// These ranges are rendered as [start,end).
139   struct LiveRange {
140     SlotIndex start;  // Start point of the interval (inclusive)
141     SlotIndex end;    // End point of the interval (exclusive)
142     VNInfo *valno;   // identifier for the value contained in this interval.
143
144     LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
145       : start(S), end(E), valno(V) {
146
147       assert(S < E && "Cannot create empty or backwards range");
148     }
149
150     /// contains - Return true if the index is covered by this range.
151     ///
152     bool contains(SlotIndex I) const {
153       return start <= I && I < end;
154     }
155
156     /// containsRange - Return true if the given range, [S, E), is covered by
157     /// this range.
158     bool containsRange(SlotIndex S, SlotIndex E) const {
159       assert((S < E) && "Backwards interval?");
160       return (start <= S && S < end) && (start < E && E <= end);
161     }
162
163     bool operator<(const LiveRange &LR) const {
164       return start < LR.start || (start == LR.start && end < LR.end);
165     }
166     bool operator==(const LiveRange &LR) const {
167       return start == LR.start && end == LR.end;
168     }
169
170     void dump() const;
171     void print(raw_ostream &os) const;
172
173   private:
174     LiveRange(); // DO NOT IMPLEMENT
175   };
176
177   template <> struct isPodLike<LiveRange> { static const bool value = true; };
178
179   raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
180
181
182   inline bool operator<(SlotIndex V, const LiveRange &LR) {
183     return V < LR.start;
184   }
185
186   inline bool operator<(const LiveRange &LR, SlotIndex V) {
187     return LR.start < V;
188   }
189
190   /// LiveInterval - This class represents some number of live ranges for a
191   /// register or value.  This class also contains a bit of register allocator
192   /// state.
193   class LiveInterval {
194   public:
195
196     typedef SmallVector<LiveRange,4> Ranges;
197     typedef SmallVector<VNInfo*,4> VNInfoList;
198
199     unsigned reg;        // the register or stack slot of this interval
200                          // if the top bits is set, it represents a stack slot.
201     float weight;        // weight of this interval
202     Ranges ranges;       // the ranges in which this register is live
203     VNInfoList valnos;   // value#'s
204
205     struct InstrSlots {
206       enum {
207         LOAD  = 0,
208         USE   = 1,
209         DEF   = 2,
210         STORE = 3,
211         NUM   = 4
212       };
213
214     };
215
216     LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
217       : reg(Reg), weight(Weight) {
218       if (IsSS)
219         reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
220     }
221
222     typedef Ranges::iterator iterator;
223     iterator begin() { return ranges.begin(); }
224     iterator end()   { return ranges.end(); }
225
226     typedef Ranges::const_iterator const_iterator;
227     const_iterator begin() const { return ranges.begin(); }
228     const_iterator end() const  { return ranges.end(); }
229
230     typedef VNInfoList::iterator vni_iterator;
231     vni_iterator vni_begin() { return valnos.begin(); }
232     vni_iterator vni_end() { return valnos.end(); }
233
234     typedef VNInfoList::const_iterator const_vni_iterator;
235     const_vni_iterator vni_begin() const { return valnos.begin(); }
236     const_vni_iterator vni_end() const { return valnos.end(); }
237
238     /// advanceTo - Advance the specified iterator to point to the LiveRange
239     /// containing the specified position, or end() if the position is past the
240     /// end of the interval.  If no LiveRange contains this position, but the
241     /// position is in a hole, this method returns an iterator pointing to the
242     /// LiveRange immediately after the hole.
243     iterator advanceTo(iterator I, SlotIndex Pos) {
244       if (Pos >= endIndex())
245         return end();
246       while (I->end <= Pos) ++I;
247       return I;
248     }
249
250     /// find - Return an iterator pointing to the first range that ends after
251     /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
252     /// when searching large intervals.
253     ///
254     /// If Pos is contained in a LiveRange, that range is returned.
255     /// If Pos is in a hole, the following LiveRange is returned.
256     /// If Pos is beyond endIndex, end() is returned.
257     iterator find(SlotIndex Pos);
258
259     const_iterator find(SlotIndex Pos) const {
260       return const_cast<LiveInterval*>(this)->find(Pos);
261     }
262
263     void clear() {
264       valnos.clear();
265       ranges.clear();
266     }
267
268     /// isStackSlot - Return true if this is a stack slot interval.
269     ///
270     bool isStackSlot() const {
271       return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
272     }
273
274     /// getStackSlotIndex - Return stack slot index if this is a stack slot
275     /// interval.
276     int getStackSlotIndex() const {
277       assert(isStackSlot() && "Interval is not a stack slot interval!");
278       return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
279     }
280
281     bool hasAtLeastOneValue() const { return !valnos.empty(); }
282
283     bool containsOneValue() const { return valnos.size() == 1; }
284
285     unsigned getNumValNums() const { return (unsigned)valnos.size(); }
286
287     /// getValNumInfo - Returns pointer to the specified val#.
288     ///
289     inline VNInfo *getValNumInfo(unsigned ValNo) {
290       return valnos[ValNo];
291     }
292     inline const VNInfo *getValNumInfo(unsigned ValNo) const {
293       return valnos[ValNo];
294     }
295
296     /// getNextValue - Create a new value number and return it.  MIIdx specifies
297     /// the instruction that defines the value number.
298     VNInfo *getNextValue(SlotIndex def, MachineInstr *CopyMI,
299                          VNInfo::Allocator &VNInfoAllocator) {
300       VNInfo *VNI =
301         new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def, CopyMI);
302       valnos.push_back(VNI);
303       return VNI;
304     }
305
306     /// Create a copy of the given value. The new value will be identical except
307     /// for the Value number.
308     VNInfo *createValueCopy(const VNInfo *orig,
309                             VNInfo::Allocator &VNInfoAllocator) {
310       VNInfo *VNI =
311         new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
312       valnos.push_back(VNI);
313       return VNI;
314     }
315
316     /// RenumberValues - Renumber all values in order of appearance and remove
317     /// unused values.
318     /// Recalculate phi-kill flags in case any phi-def values were removed.
319     void RenumberValues(LiveIntervals &lis);
320
321     /// isOnlyLROfValNo - Return true if the specified live range is the only
322     /// one defined by the its val#.
323     bool isOnlyLROfValNo(const LiveRange *LR) {
324       for (const_iterator I = begin(), E = end(); I != E; ++I) {
325         const LiveRange *Tmp = I;
326         if (Tmp != LR && Tmp->valno == LR->valno)
327           return false;
328       }
329       return true;
330     }
331
332     /// MergeValueNumberInto - This method is called when two value nubmers
333     /// are found to be equivalent.  This eliminates V1, replacing all
334     /// LiveRanges with the V1 value number with the V2 value number.  This can
335     /// cause merging of V1/V2 values numbers and compaction of the value space.
336     VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
337
338     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
339     /// in RHS into this live interval as the specified value number.
340     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
341     /// current interval, it will replace the value numbers of the overlaped
342     /// live ranges with the specified value number.
343     void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
344
345     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
346     /// in RHS into this live interval as the specified value number.
347     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
348     /// current interval, but only if the overlapping LiveRanges have the
349     /// specified value number.
350     void MergeValueInAsValue(const LiveInterval &RHS,
351                              const VNInfo *RHSValNo, VNInfo *LHSValNo);
352
353     /// Copy - Copy the specified live interval. This copies all the fields
354     /// except for the register of the interval.
355     void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
356               VNInfo::Allocator &VNInfoAllocator);
357
358     bool empty() const { return ranges.empty(); }
359
360     /// beginIndex - Return the lowest numbered slot covered by interval.
361     SlotIndex beginIndex() const {
362       assert(!empty() && "Call to beginIndex() on empty interval.");
363       return ranges.front().start;
364     }
365
366     /// endNumber - return the maximum point of the interval of the whole,
367     /// exclusive.
368     SlotIndex endIndex() const {
369       assert(!empty() && "Call to endIndex() on empty interval.");
370       return ranges.back().end;
371     }
372
373     bool expiredAt(SlotIndex index) const {
374       return index >= endIndex();
375     }
376
377     bool liveAt(SlotIndex index) const {
378       const_iterator r = find(index);
379       return r != end() && r->start <= index;
380     }
381
382     /// killedAt - Return true if a live range ends at index. Note that the kill
383     /// point is not contained in the half-open live range. It is usually the
384     /// getDefIndex() slot following its last use.
385     bool killedAt(SlotIndex index) const {
386       const_iterator r = find(index.getUseIndex());
387       return r != end() && r->end == index;
388     }
389
390     /// killedInRange - Return true if the interval has kills in [Start,End).
391     /// Note that the kill point is considered the end of a live range, so it is
392     /// not contained in the live range. If a live range ends at End, it won't
393     /// be counted as a kill by this method.
394     bool killedInRange(SlotIndex Start, SlotIndex End) const;
395
396     /// getLiveRangeContaining - Return the live range that contains the
397     /// specified index, or null if there is none.
398     const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
399       const_iterator I = FindLiveRangeContaining(Idx);
400       return I == end() ? 0 : &*I;
401     }
402
403     /// getLiveRangeContaining - Return the live range that contains the
404     /// specified index, or null if there is none.
405     LiveRange *getLiveRangeContaining(SlotIndex Idx) {
406       iterator I = FindLiveRangeContaining(Idx);
407       return I == end() ? 0 : &*I;
408     }
409
410     /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
411     VNInfo *getVNInfoAt(SlotIndex Idx) const {
412       const_iterator I = FindLiveRangeContaining(Idx);
413       return I == end() ? 0 : I->valno;
414     }
415
416     /// FindLiveRangeContaining - Return an iterator to the live range that
417     /// contains the specified index, or end() if there is none.
418     iterator FindLiveRangeContaining(SlotIndex Idx) {
419       iterator I = find(Idx);
420       return I != end() && I->start <= Idx ? I : end();
421     }
422
423     const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
424       const_iterator I = find(Idx);
425       return I != end() && I->start <= Idx ? I : end();
426     }
427
428     /// findDefinedVNInfo - Find the by the specified
429     /// index (register interval) or defined
430     VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
431
432
433     /// overlaps - Return true if the intersection of the two live intervals is
434     /// not empty.
435     bool overlaps(const LiveInterval& other) const {
436       if (other.empty())
437         return false;
438       return overlapsFrom(other, other.begin());
439     }
440
441     /// overlaps - Return true if the live interval overlaps a range specified
442     /// by [Start, End).
443     bool overlaps(SlotIndex Start, SlotIndex End) const;
444
445     /// overlapsFrom - Return true if the intersection of the two live intervals
446     /// is not empty.  The specified iterator is a hint that we can begin
447     /// scanning the Other interval starting at I.
448     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
449
450     /// addRange - Add the specified LiveRange to this interval, merging
451     /// intervals as appropriate.  This returns an iterator to the inserted live
452     /// range (which may have grown since it was inserted.
453     void addRange(LiveRange LR) {
454       addRangeFrom(LR, ranges.begin());
455     }
456
457     /// join - Join two live intervals (this, and other) together.  This applies
458     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
459     /// the intervals are not joinable, this aborts.
460     void join(LiveInterval &Other,
461               const int *ValNoAssignments,
462               const int *RHSValNoAssignments,
463               SmallVector<VNInfo*, 16> &NewVNInfo,
464               MachineRegisterInfo *MRI);
465
466     /// isInOneLiveRange - Return true if the range specified is entirely in the
467     /// a single LiveRange of the live interval.
468     bool isInOneLiveRange(SlotIndex Start, SlotIndex End) const {
469       const_iterator r = find(Start);
470       return r != end() && r->containsRange(Start, End);
471     }
472
473     /// removeRange - Remove the specified range from this interval.  Note that
474     /// the range must be a single LiveRange in its entirety.
475     void removeRange(SlotIndex Start, SlotIndex End,
476                      bool RemoveDeadValNo = false);
477
478     void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
479       removeRange(LR.start, LR.end, RemoveDeadValNo);
480     }
481
482     /// removeValNo - Remove all the ranges defined by the specified value#.
483     /// Also remove the value# from value# list.
484     void removeValNo(VNInfo *ValNo);
485
486     /// getSize - Returns the sum of sizes of all the LiveRange's.
487     ///
488     unsigned getSize() const;
489
490     /// Returns true if the live interval is zero length, i.e. no live ranges
491     /// span instructions. It doesn't pay to spill such an interval.
492     bool isZeroLength() const {
493       for (const_iterator i = begin(), e = end(); i != e; ++i)
494         if (i->end.getPrevIndex() > i->start)
495           return false;
496       return true;
497     }
498
499     /// isSpillable - Can this interval be spilled?
500     bool isSpillable() const {
501       return weight != HUGE_VALF;
502     }
503
504     /// markNotSpillable - Mark interval as not spillable
505     void markNotSpillable() {
506       weight = HUGE_VALF;
507     }
508
509     /// ComputeJoinedWeight - Set the weight of a live interval after
510     /// Other has been merged into it.
511     void ComputeJoinedWeight(const LiveInterval &Other);
512
513     bool operator<(const LiveInterval& other) const {
514       const SlotIndex &thisIndex = beginIndex();
515       const SlotIndex &otherIndex = other.beginIndex();
516       return (thisIndex < otherIndex ||
517               (thisIndex == otherIndex && reg < other.reg));
518     }
519
520     void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
521     void dump() const;
522
523   private:
524
525     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
526     void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
527     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
528     void markValNoForDeletion(VNInfo *V);
529
530     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
531
532   };
533
534   inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
535     LI.print(OS);
536     return OS;
537   }
538 }
539
540 #endif