Allow overlaps between virtreg and physreg live ranges.
[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/IntEqClasses.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 CoalescerPair;
33   class LiveIntervals;
34   class MachineInstr;
35   class MachineRegisterInfo;
36   class TargetRegisterInfo;
37   class raw_ostream;
38
39   /// VNInfo - Value Number Information.
40   /// This class holds information about a machine level values, including
41   /// definition and use points.
42   ///
43   class VNInfo {
44   public:
45     typedef BumpPtrAllocator Allocator;
46
47     /// The ID number of this value.
48     unsigned id;
49
50     /// The index of the defining instruction.
51     SlotIndex def;
52
53     /// VNInfo constructor.
54     VNInfo(unsigned i, SlotIndex d)
55       : id(i), def(d)
56     { }
57
58     /// VNInfo construtor, copies values from orig, except for the value number.
59     VNInfo(unsigned i, const VNInfo &orig)
60       : id(i), def(orig.def)
61     { }
62
63     /// Copy from the parameter into this VNInfo.
64     void copyFrom(VNInfo &src) {
65       def = src.def;
66     }
67
68     /// Returns true if this value is defined by a PHI instruction (or was,
69     /// PHI instrucions may have been eliminated).
70     /// PHI-defs begin at a block boundary, all other defs begin at register or
71     /// EC slots.
72     bool isPHIDef() const { return def.isBlock(); }
73
74     /// Returns true if this value is unused.
75     bool isUnused() const { return !def.isValid(); }
76
77     /// Mark this value as unused.
78     void markUnused() { def = SlotIndex(); }
79   };
80
81   /// LiveRange structure - This represents a simple register range in the
82   /// program, with an inclusive start point and an exclusive end point.
83   /// These ranges are rendered as [start,end).
84   struct LiveRange {
85     SlotIndex start;  // Start point of the interval (inclusive)
86     SlotIndex end;    // End point of the interval (exclusive)
87     VNInfo *valno;   // identifier for the value contained in this interval.
88
89     LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
90       : start(S), end(E), valno(V) {
91
92       assert(S < E && "Cannot create empty or backwards range");
93     }
94
95     /// contains - Return true if the index is covered by this range.
96     ///
97     bool contains(SlotIndex I) const {
98       return start <= I && I < end;
99     }
100
101     /// containsRange - Return true if the given range, [S, E), is covered by
102     /// this range.
103     bool containsRange(SlotIndex S, SlotIndex E) const {
104       assert((S < E) && "Backwards interval?");
105       return (start <= S && S < end) && (start < E && E <= end);
106     }
107
108     bool operator<(const LiveRange &LR) const {
109       return start < LR.start || (start == LR.start && end < LR.end);
110     }
111     bool operator==(const LiveRange &LR) const {
112       return start == LR.start && end == LR.end;
113     }
114
115     void dump() const;
116     void print(raw_ostream &os) const;
117
118   private:
119     LiveRange(); // DO NOT IMPLEMENT
120   };
121
122   template <> struct isPodLike<LiveRange> { static const bool value = true; };
123
124   raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
125
126
127   inline bool operator<(SlotIndex V, const LiveRange &LR) {
128     return V < LR.start;
129   }
130
131   inline bool operator<(const LiveRange &LR, SlotIndex V) {
132     return LR.start < V;
133   }
134
135   /// LiveInterval - This class represents some number of live ranges for a
136   /// register or value.  This class also contains a bit of register allocator
137   /// state.
138   class LiveInterval {
139   public:
140
141     typedef SmallVector<LiveRange,4> Ranges;
142     typedef SmallVector<VNInfo*,4> VNInfoList;
143
144     const unsigned reg;  // the register or stack slot of this interval.
145     float weight;        // weight of this interval
146     Ranges ranges;       // the ranges in which this register is live
147     VNInfoList valnos;   // value#'s
148
149     struct InstrSlots {
150       enum {
151         LOAD  = 0,
152         USE   = 1,
153         DEF   = 2,
154         STORE = 3,
155         NUM   = 4
156       };
157
158     };
159
160     LiveInterval(unsigned Reg, float Weight)
161       : reg(Reg), weight(Weight) {}
162
163     typedef Ranges::iterator iterator;
164     iterator begin() { return ranges.begin(); }
165     iterator end()   { return ranges.end(); }
166
167     typedef Ranges::const_iterator const_iterator;
168     const_iterator begin() const { return ranges.begin(); }
169     const_iterator end() const  { return ranges.end(); }
170
171     typedef VNInfoList::iterator vni_iterator;
172     vni_iterator vni_begin() { return valnos.begin(); }
173     vni_iterator vni_end() { return valnos.end(); }
174
175     typedef VNInfoList::const_iterator const_vni_iterator;
176     const_vni_iterator vni_begin() const { return valnos.begin(); }
177     const_vni_iterator vni_end() const { return valnos.end(); }
178
179     /// advanceTo - Advance the specified iterator to point to the LiveRange
180     /// containing the specified position, or end() if the position is past the
181     /// end of the interval.  If no LiveRange contains this position, but the
182     /// position is in a hole, this method returns an iterator pointing to the
183     /// LiveRange immediately after the hole.
184     iterator advanceTo(iterator I, SlotIndex Pos) {
185       assert(I != end());
186       if (Pos >= endIndex())
187         return end();
188       while (I->end <= Pos) ++I;
189       return I;
190     }
191
192     /// find - Return an iterator pointing to the first range that ends after
193     /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
194     /// when searching large intervals.
195     ///
196     /// If Pos is contained in a LiveRange, that range is returned.
197     /// If Pos is in a hole, the following LiveRange is returned.
198     /// If Pos is beyond endIndex, end() is returned.
199     iterator find(SlotIndex Pos);
200
201     const_iterator find(SlotIndex Pos) const {
202       return const_cast<LiveInterval*>(this)->find(Pos);
203     }
204
205     void clear() {
206       valnos.clear();
207       ranges.clear();
208     }
209
210     bool hasAtLeastOneValue() const { return !valnos.empty(); }
211
212     bool containsOneValue() const { return valnos.size() == 1; }
213
214     unsigned getNumValNums() const { return (unsigned)valnos.size(); }
215
216     /// getValNumInfo - Returns pointer to the specified val#.
217     ///
218     inline VNInfo *getValNumInfo(unsigned ValNo) {
219       return valnos[ValNo];
220     }
221     inline const VNInfo *getValNumInfo(unsigned ValNo) const {
222       return valnos[ValNo];
223     }
224
225     /// containsValue - Returns true if VNI belongs to this interval.
226     bool containsValue(const VNInfo *VNI) const {
227       return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id);
228     }
229
230     /// getNextValue - Create a new value number and return it.  MIIdx specifies
231     /// the instruction that defines the value number.
232     VNInfo *getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator) {
233       VNInfo *VNI =
234         new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def);
235       valnos.push_back(VNI);
236       return VNI;
237     }
238
239     /// createDeadDef - Make sure the interval has a value defined at Def.
240     /// If one already exists, return it. Otherwise allocate a new value and
241     /// add liveness for a dead def.
242     VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator);
243
244     /// Create a copy of the given value. The new value will be identical except
245     /// for the Value number.
246     VNInfo *createValueCopy(const VNInfo *orig,
247                             VNInfo::Allocator &VNInfoAllocator) {
248       VNInfo *VNI =
249         new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
250       valnos.push_back(VNI);
251       return VNI;
252     }
253
254     /// RenumberValues - Renumber all values in order of appearance and remove
255     /// unused values.
256     void RenumberValues(LiveIntervals &lis);
257
258     /// MergeValueNumberInto - This method is called when two value nubmers
259     /// are found to be equivalent.  This eliminates V1, replacing all
260     /// LiveRanges with the V1 value number with the V2 value number.  This can
261     /// cause merging of V1/V2 values numbers and compaction of the value space.
262     VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
263
264     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
265     /// in RHS into this live interval as the specified value number.
266     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
267     /// current interval, it will replace the value numbers of the overlaped
268     /// live ranges with the specified value number.
269     void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
270
271     /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
272     /// in RHS into this live interval as the specified value number.
273     /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
274     /// current interval, but only if the overlapping LiveRanges have the
275     /// specified value number.
276     void MergeValueInAsValue(const LiveInterval &RHS,
277                              const VNInfo *RHSValNo, VNInfo *LHSValNo);
278
279     /// Copy - Copy the specified live interval. This copies all the fields
280     /// except for the register of the interval.
281     void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
282               VNInfo::Allocator &VNInfoAllocator);
283
284     bool empty() const { return ranges.empty(); }
285
286     /// beginIndex - Return the lowest numbered slot covered by interval.
287     SlotIndex beginIndex() const {
288       assert(!empty() && "Call to beginIndex() on empty interval.");
289       return ranges.front().start;
290     }
291
292     /// endNumber - return the maximum point of the interval of the whole,
293     /// exclusive.
294     SlotIndex endIndex() const {
295       assert(!empty() && "Call to endIndex() on empty interval.");
296       return ranges.back().end;
297     }
298
299     bool expiredAt(SlotIndex index) const {
300       return index >= endIndex();
301     }
302
303     bool liveAt(SlotIndex index) const {
304       const_iterator r = find(index);
305       return r != end() && r->start <= index;
306     }
307
308     /// killedAt - Return true if a live range ends at index. Note that the kill
309     /// point is not contained in the half-open live range. It is usually the
310     /// getDefIndex() slot following its last use.
311     bool killedAt(SlotIndex index) const {
312       const_iterator r = find(index.getRegSlot(true));
313       return r != end() && r->end == index;
314     }
315
316     /// killedInRange - Return true if the interval has kills in [Start,End).
317     /// Note that the kill point is considered the end of a live range, so it is
318     /// not contained in the live range. If a live range ends at End, it won't
319     /// be counted as a kill by this method.
320     bool killedInRange(SlotIndex Start, SlotIndex End) const;
321
322     /// getLiveRangeContaining - Return the live range that contains the
323     /// specified index, or null if there is none.
324     const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
325       const_iterator I = FindLiveRangeContaining(Idx);
326       return I == end() ? 0 : &*I;
327     }
328
329     /// getLiveRangeContaining - Return the live range that contains the
330     /// specified index, or null if there is none.
331     LiveRange *getLiveRangeContaining(SlotIndex Idx) {
332       iterator I = FindLiveRangeContaining(Idx);
333       return I == end() ? 0 : &*I;
334     }
335
336     /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
337     VNInfo *getVNInfoAt(SlotIndex Idx) const {
338       const_iterator I = FindLiveRangeContaining(Idx);
339       return I == end() ? 0 : I->valno;
340     }
341
342     /// getVNInfoBefore - Return the VNInfo that is live up to but not
343     /// necessarilly including Idx, or NULL. Use this to find the reaching def
344     /// used by an instruction at this SlotIndex position.
345     VNInfo *getVNInfoBefore(SlotIndex Idx) const {
346       const_iterator I = FindLiveRangeContaining(Idx.getPrevSlot());
347       return I == end() ? 0 : I->valno;
348     }
349
350     /// FindLiveRangeContaining - Return an iterator to the live range that
351     /// contains the specified index, or end() if there is none.
352     iterator FindLiveRangeContaining(SlotIndex Idx) {
353       iterator I = find(Idx);
354       return I != end() && I->start <= Idx ? I : end();
355     }
356
357     const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
358       const_iterator I = find(Idx);
359       return I != end() && I->start <= Idx ? I : end();
360     }
361
362     /// overlaps - Return true if the intersection of the two live intervals is
363     /// not empty.
364     bool overlaps(const LiveInterval& other) const {
365       if (other.empty())
366         return false;
367       return overlapsFrom(other, other.begin());
368     }
369
370     /// overlaps - Return true if the two intervals have overlapping segments
371     /// that are not coalescable according to CP.
372     ///
373     /// Overlapping segments where one interval is defined by a coalescable
374     /// copy are allowed.
375     bool overlaps(const LiveInterval &Other, const CoalescerPair &CP,
376                   const SlotIndexes&) const;
377
378     /// overlaps - Return true if the live interval overlaps a range specified
379     /// by [Start, End).
380     bool overlaps(SlotIndex Start, SlotIndex End) const;
381
382     /// overlapsFrom - Return true if the intersection of the two live intervals
383     /// is not empty.  The specified iterator is a hint that we can begin
384     /// scanning the Other interval starting at I.
385     bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
386
387     /// addRange - Add the specified LiveRange to this interval, merging
388     /// intervals as appropriate.  This returns an iterator to the inserted live
389     /// range (which may have grown since it was inserted.
390     void addRange(LiveRange LR) {
391       addRangeFrom(LR, ranges.begin());
392     }
393
394     /// extendInBlock - If this interval is live before Kill in the basic block
395     /// that starts at StartIdx, extend it to be live up to Kill, and return
396     /// the value. If there is no live range before Kill, return NULL.
397     VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill);
398
399     /// join - Join two live intervals (this, and other) together.  This applies
400     /// mappings to the value numbers in the LHS/RHS intervals as specified.  If
401     /// the intervals are not joinable, this aborts.
402     void join(LiveInterval &Other,
403               const int *ValNoAssignments,
404               const int *RHSValNoAssignments,
405               SmallVector<VNInfo*, 16> &NewVNInfo,
406               MachineRegisterInfo *MRI);
407
408     /// isInOneLiveRange - Return true if the range specified is entirely in the
409     /// a single LiveRange of the live interval.
410     bool isInOneLiveRange(SlotIndex Start, SlotIndex End) const {
411       const_iterator r = find(Start);
412       return r != end() && r->containsRange(Start, End);
413     }
414
415     /// removeRange - Remove the specified range from this interval.  Note that
416     /// the range must be a single LiveRange in its entirety.
417     void removeRange(SlotIndex Start, SlotIndex End,
418                      bool RemoveDeadValNo = false);
419
420     void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
421       removeRange(LR.start, LR.end, RemoveDeadValNo);
422     }
423
424     /// removeValNo - Remove all the ranges defined by the specified value#.
425     /// Also remove the value# from value# list.
426     void removeValNo(VNInfo *ValNo);
427
428     /// getSize - Returns the sum of sizes of all the LiveRange's.
429     ///
430     unsigned getSize() const;
431
432     /// Returns true if the live interval is zero length, i.e. no live ranges
433     /// span instructions. It doesn't pay to spill such an interval.
434     bool isZeroLength(SlotIndexes *Indexes) const {
435       for (const_iterator i = begin(), e = end(); i != e; ++i)
436         if (Indexes->getNextNonNullIndex(i->start).getBaseIndex() <
437             i->end.getBaseIndex())
438           return false;
439       return true;
440     }
441
442     /// isSpillable - Can this interval be spilled?
443     bool isSpillable() const {
444       return weight != HUGE_VALF;
445     }
446
447     /// markNotSpillable - Mark interval as not spillable
448     void markNotSpillable() {
449       weight = HUGE_VALF;
450     }
451
452     bool operator<(const LiveInterval& other) const {
453       const SlotIndex &thisIndex = beginIndex();
454       const SlotIndex &otherIndex = other.beginIndex();
455       return (thisIndex < otherIndex ||
456               (thisIndex == otherIndex && reg < other.reg));
457     }
458
459     void print(raw_ostream &OS) const;
460     void dump() const;
461
462     /// \brief Walk the interval and assert if any invariants fail to hold.
463     ///
464     /// Note that this is a no-op when asserts are disabled.
465 #ifdef NDEBUG
466     void verify() const {}
467 #else
468     void verify() const;
469 #endif
470
471   private:
472
473     Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
474     void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
475     Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
476     void markValNoForDeletion(VNInfo *V);
477     void mergeIntervalRanges(const LiveInterval &RHS,
478                              VNInfo *LHSValNo = 0,
479                              const VNInfo *RHSValNo = 0);
480
481     LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
482
483   };
484
485   inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
486     LI.print(OS);
487     return OS;
488   }
489
490   /// LiveRangeQuery - Query information about a live range around a given
491   /// instruction. This class hides the implementation details of live ranges,
492   /// and it should be used as the primary interface for examining live ranges
493   /// around instructions.
494   ///
495   class LiveRangeQuery {
496     VNInfo *EarlyVal;
497     VNInfo *LateVal;
498     SlotIndex EndPoint;
499     bool Kill;
500
501   public:
502     /// Create a LiveRangeQuery for the given live range and instruction index.
503     /// The sub-instruction slot of Idx doesn't matter, only the instruction it
504     /// refers to is considered.
505     LiveRangeQuery(const LiveInterval &LI, SlotIndex Idx)
506       : EarlyVal(0), LateVal(0), Kill(false) {
507       // Find the segment that enters the instruction.
508       LiveInterval::const_iterator I = LI.find(Idx.getBaseIndex());
509       LiveInterval::const_iterator E = LI.end();
510       if (I == E)
511         return;
512       // Is this an instruction live-in segment?
513       if (SlotIndex::isEarlierInstr(I->start, Idx)) {
514         EarlyVal = I->valno;
515         EndPoint = I->end;
516         // Move to the potentially live-out segment.
517         if (SlotIndex::isSameInstr(Idx, I->end)) {
518           Kill = true;
519           if (++I == E)
520             return;
521         }
522       }
523       // I now points to the segment that may be live-through, or defined by
524       // this instr. Ignore segments starting after the current instr.
525       if (SlotIndex::isEarlierInstr(Idx, I->start))
526         return;
527       LateVal = I->valno;
528       EndPoint = I->end;
529     }
530
531     /// Return the value that is live-in to the instruction. This is the value
532     /// that will be read by the instruction's use operands. Return NULL if no
533     /// value is live-in.
534     VNInfo *valueIn() const {
535       return EarlyVal;
536     }
537
538     /// Return true if the live-in value is killed by this instruction. This
539     /// means that either the live range ends at the instruction, or it changes
540     /// value.
541     bool isKill() const {
542       return Kill;
543     }
544
545     /// Return true if this instruction has a dead def.
546     bool isDeadDef() const {
547       return EndPoint.isDead();
548     }
549
550     /// Return the value leaving the instruction, if any. This can be a
551     /// live-through value, or a live def. A dead def returns NULL.
552     VNInfo *valueOut() const {
553       return isDeadDef() ? 0 : LateVal;
554     }
555
556     /// Return the value defined by this instruction, if any. This includes
557     /// dead defs, it is the value created by the instruction's def operands.
558     VNInfo *valueDefined() const {
559       return EarlyVal == LateVal ? 0 : LateVal;
560     }
561
562     /// Return the end point of the last live range segment to interact with
563     /// the instruction, if any.
564     ///
565     /// The end point is an invalid SlotIndex only if the live range doesn't
566     /// intersect the instruction at all.
567     ///
568     /// The end point may be at or past the end of the instruction's basic
569     /// block. That means the value was live out of the block.
570     SlotIndex endPoint() const {
571       return EndPoint;
572     }
573   };
574
575   /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
576   /// LiveInterval into equivalence clases of connected components. A
577   /// LiveInterval that has multiple connected components can be broken into
578   /// multiple LiveIntervals.
579   ///
580   /// Given a LiveInterval that may have multiple connected components, run:
581   ///
582   ///   unsigned numComps = ConEQ.Classify(LI);
583   ///   if (numComps > 1) {
584   ///     // allocate numComps-1 new LiveIntervals into LIS[1..]
585   ///     ConEQ.Distribute(LIS);
586   /// }
587
588   class ConnectedVNInfoEqClasses {
589     LiveIntervals &LIS;
590     IntEqClasses EqClass;
591
592     // Note that values a and b are connected.
593     void Connect(unsigned a, unsigned b);
594
595     unsigned Renumber();
596
597   public:
598     explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
599
600     /// Classify - Classify the values in LI into connected components.
601     /// Return the number of connected components.
602     unsigned Classify(const LiveInterval *LI);
603
604     /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
605     /// the equivalence class assigned the VNI.
606     unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
607
608     /// Distribute - Distribute values in LIV[0] into a separate LiveInterval
609     /// for each connected component. LIV must have a LiveInterval for each
610     /// connected component. The LiveIntervals in Liv[1..] must be empty.
611     /// Instructions using LIV[0] are rewritten.
612     void Distribute(LiveInterval *LIV[], MachineRegisterInfo &MRI);
613
614   };
615
616 }
617 #endif