1 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22 #define LLVM_CODEGEN_LIVEINTERVAL_H
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/AlignOf.h"
27 #include "llvm/CodeGen/SlotIndexes.h"
34 class MachineRegisterInfo;
35 class TargetRegisterInfo;
38 /// VNInfo - Value Number Information.
39 /// This class holds information about a machine level values, including
40 /// definition and use points.
55 typedef BumpPtrAllocator Allocator;
57 /// The ID number of this value.
60 /// The index of the defining instruction (if isDefAccurate() returns true).
63 /// VNInfo constructor.
64 VNInfo(unsigned i, SlotIndex d, MachineInstr *c)
65 : copy(c), flags(0), id(i), def(d)
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)
73 /// Copy from the parameter into this VNInfo.
74 void copyFrom(VNInfo &src) {
80 /// Used for copying value number info.
81 unsigned getFlags() const { return flags; }
82 void setFlags(unsigned flags) { this->flags = flags; }
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; }
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) {
98 flags |= HAS_PHI_KILL;
100 flags &= ~HAS_PHI_KILL;
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) {
109 flags |= REDEF_BY_EC;
111 flags &= ~REDEF_BY_EC;
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) {
122 flags &= ~IS_PHI_DEF;
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) {
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).
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.
144 LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
145 : start(S), end(E), valno(V) {
147 assert(S < E && "Cannot create empty or backwards range");
150 /// contains - Return true if the index is covered by this range.
152 bool contains(SlotIndex I) const {
153 return start <= I && I < end;
156 /// containsRange - Return true if the given range, [S, E), is covered by
158 bool containsRange(SlotIndex S, SlotIndex E) const {
159 assert((S < E) && "Backwards interval?");
160 return (start <= S && S < end) && (start < E && E <= end);
163 bool operator<(const LiveRange &LR) const {
164 return start < LR.start || (start == LR.start && end < LR.end);
166 bool operator==(const LiveRange &LR) const {
167 return start == LR.start && end == LR.end;
171 void print(raw_ostream &os) const;
174 LiveRange(); // DO NOT IMPLEMENT
177 template <> struct isPodLike<LiveRange> { static const bool value = true; };
179 raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
182 inline bool operator<(SlotIndex V, const LiveRange &LR) {
186 inline bool operator<(const LiveRange &LR, SlotIndex V) {
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
196 typedef SmallVector<LiveRange,4> Ranges;
197 typedef SmallVector<VNInfo*,4> VNInfoList;
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
216 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
217 : reg(Reg), weight(Weight) {
219 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
222 typedef Ranges::iterator iterator;
223 iterator begin() { return ranges.begin(); }
224 iterator end() { return ranges.end(); }
226 typedef Ranges::const_iterator const_iterator;
227 const_iterator begin() const { return ranges.begin(); }
228 const_iterator end() const { return ranges.end(); }
230 typedef VNInfoList::iterator vni_iterator;
231 vni_iterator vni_begin() { return valnos.begin(); }
232 vni_iterator vni_end() { return valnos.end(); }
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(); }
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())
246 while (I->end <= Pos) ++I;
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.
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);
259 const_iterator find(SlotIndex Pos) const {
260 return const_cast<LiveInterval*>(this)->find(Pos);
268 /// isStackSlot - Return true if this is a stack slot interval.
270 bool isStackSlot() const {
271 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
274 /// getStackSlotIndex - Return stack slot index if this is a stack slot
276 int getStackSlotIndex() const {
277 assert(isStackSlot() && "Interval is not a stack slot interval!");
278 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
281 bool hasAtLeastOneValue() const { return !valnos.empty(); }
283 bool containsOneValue() const { return valnos.size() == 1; }
285 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
287 /// getValNumInfo - Returns pointer to the specified val#.
289 inline VNInfo *getValNumInfo(unsigned ValNo) {
290 return valnos[ValNo];
292 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
293 return valnos[ValNo];
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) {
301 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def, CopyMI);
302 valnos.push_back(VNI);
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) {
311 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
312 valnos.push_back(VNI);
316 /// RenumberValues - Renumber all values in order of appearance and remove
318 /// Recalculate phi-kill flags in case any phi-def values were removed.
319 void RenumberValues(LiveIntervals &lis);
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)
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);
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);
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);
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);
358 bool empty() const { return ranges.empty(); }
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;
366 /// endNumber - return the maximum point of the interval of the whole,
368 SlotIndex endIndex() const {
369 assert(!empty() && "Call to endIndex() on empty interval.");
370 return ranges.back().end;
373 bool expiredAt(SlotIndex index) const {
374 return index >= endIndex();
377 bool liveAt(SlotIndex index) const {
378 const_iterator r = find(index);
379 return r != end() && r->start <= index;
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;
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;
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;
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;
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;
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();
423 const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
424 const_iterator I = find(Idx);
425 return I != end() && I->start <= Idx ? I : end();
428 /// findDefinedVNInfo - Find the by the specified
429 /// index (register interval) or defined
430 VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
433 /// overlaps - Return true if the intersection of the two live intervals is
435 bool overlaps(const LiveInterval& other) const {
438 return overlapsFrom(other, other.begin());
441 /// overlaps - Return true if the live interval overlaps a range specified
443 bool overlaps(SlotIndex Start, SlotIndex End) const;
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;
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());
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);
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);
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);
478 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
479 removeRange(LR.start, LR.end, RemoveDeadValNo);
482 /// removeValNo - Remove all the ranges defined by the specified value#.
483 /// Also remove the value# from value# list.
484 void removeValNo(VNInfo *ValNo);
486 /// getSize - Returns the sum of sizes of all the LiveRange's.
488 unsigned getSize() const;
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)
499 /// isSpillable - Can this interval be spilled?
500 bool isSpillable() const {
501 return weight != HUGE_VALF;
504 /// markNotSpillable - Mark interval as not spillable
505 void markNotSpillable() {
509 /// ComputeJoinedWeight - Set the weight of a live interval after
510 /// Other has been merged into it.
511 void ComputeJoinedWeight(const LiveInterval &Other);
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));
520 void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
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);
530 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
534 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {