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 /// isDefByCopy - Return true when this value was defined by a copy-like
94 /// instruction as determined by MachineInstr::isCopyLike.
95 bool isDefByCopy() const { return copy != 0; }
97 /// Returns true if one or more kills are PHI nodes.
98 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
99 /// Set the PHI kill flag on this value.
100 void setHasPHIKill(bool hasKill) {
102 flags |= HAS_PHI_KILL;
104 flags &= ~HAS_PHI_KILL;
107 /// Returns true if this value is re-defined by an early clobber somewhere
108 /// during the live range.
109 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
110 /// Set the "redef by early clobber" flag on this value.
111 void setHasRedefByEC(bool hasRedef) {
113 flags |= REDEF_BY_EC;
115 flags &= ~REDEF_BY_EC;
118 /// Returns true if this value is defined by a PHI instruction (or was,
119 /// PHI instrucions may have been eliminated).
120 bool isPHIDef() const { return flags & IS_PHI_DEF; }
121 /// Set the "phi def" flag on this value.
122 void setIsPHIDef(bool phiDef) {
126 flags &= ~IS_PHI_DEF;
129 /// Returns true if this value is unused.
130 bool isUnused() const { return flags & IS_UNUSED; }
131 /// Set the "is unused" flag on this value.
132 void setIsUnused(bool unused) {
140 /// LiveRange structure - This represents a simple register range in the
141 /// program, with an inclusive start point and an exclusive end point.
142 /// These ranges are rendered as [start,end).
144 SlotIndex start; // Start point of the interval (inclusive)
145 SlotIndex end; // End point of the interval (exclusive)
146 VNInfo *valno; // identifier for the value contained in this interval.
148 LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
149 : start(S), end(E), valno(V) {
151 assert(S < E && "Cannot create empty or backwards range");
154 /// contains - Return true if the index is covered by this range.
156 bool contains(SlotIndex I) const {
157 return start <= I && I < end;
160 /// containsRange - Return true if the given range, [S, E), is covered by
162 bool containsRange(SlotIndex S, SlotIndex E) const {
163 assert((S < E) && "Backwards interval?");
164 return (start <= S && S < end) && (start < E && E <= end);
167 bool operator<(const LiveRange &LR) const {
168 return start < LR.start || (start == LR.start && end < LR.end);
170 bool operator==(const LiveRange &LR) const {
171 return start == LR.start && end == LR.end;
175 void print(raw_ostream &os) const;
178 LiveRange(); // DO NOT IMPLEMENT
181 template <> struct isPodLike<LiveRange> { static const bool value = true; };
183 raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
186 inline bool operator<(SlotIndex V, const LiveRange &LR) {
190 inline bool operator<(const LiveRange &LR, SlotIndex V) {
194 /// LiveInterval - This class represents some number of live ranges for a
195 /// register or value. This class also contains a bit of register allocator
200 typedef SmallVector<LiveRange,4> Ranges;
201 typedef SmallVector<VNInfo*,4> VNInfoList;
203 unsigned reg; // the register or stack slot of this interval
204 // if the top bits is set, it represents a stack slot.
205 float weight; // weight of this interval
206 Ranges ranges; // the ranges in which this register is live
207 VNInfoList valnos; // value#'s
220 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
221 : reg(Reg), weight(Weight) {
223 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
226 typedef Ranges::iterator iterator;
227 iterator begin() { return ranges.begin(); }
228 iterator end() { return ranges.end(); }
230 typedef Ranges::const_iterator const_iterator;
231 const_iterator begin() const { return ranges.begin(); }
232 const_iterator end() const { return ranges.end(); }
234 typedef VNInfoList::iterator vni_iterator;
235 vni_iterator vni_begin() { return valnos.begin(); }
236 vni_iterator vni_end() { return valnos.end(); }
238 typedef VNInfoList::const_iterator const_vni_iterator;
239 const_vni_iterator vni_begin() const { return valnos.begin(); }
240 const_vni_iterator vni_end() const { return valnos.end(); }
242 /// advanceTo - Advance the specified iterator to point to the LiveRange
243 /// containing the specified position, or end() if the position is past the
244 /// end of the interval. If no LiveRange contains this position, but the
245 /// position is in a hole, this method returns an iterator pointing to the
246 /// LiveRange immediately after the hole.
247 iterator advanceTo(iterator I, SlotIndex Pos) {
248 if (Pos >= endIndex())
250 while (I->end <= Pos) ++I;
254 /// find - Return an iterator pointing to the first range that ends after
255 /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
256 /// when searching large intervals.
258 /// If Pos is contained in a LiveRange, that range is returned.
259 /// If Pos is in a hole, the following LiveRange is returned.
260 /// If Pos is beyond endIndex, end() is returned.
261 iterator find(SlotIndex Pos);
263 const_iterator find(SlotIndex Pos) const {
264 return const_cast<LiveInterval*>(this)->find(Pos);
272 /// isStackSlot - Return true if this is a stack slot interval.
274 bool isStackSlot() const {
275 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
278 /// getStackSlotIndex - Return stack slot index if this is a stack slot
280 int getStackSlotIndex() const {
281 assert(isStackSlot() && "Interval is not a stack slot interval!");
282 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
285 bool hasAtLeastOneValue() const { return !valnos.empty(); }
287 bool containsOneValue() const { return valnos.size() == 1; }
289 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
291 /// getValNumInfo - Returns pointer to the specified val#.
293 inline VNInfo *getValNumInfo(unsigned ValNo) {
294 return valnos[ValNo];
296 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
297 return valnos[ValNo];
300 /// getNextValue - Create a new value number and return it. MIIdx specifies
301 /// the instruction that defines the value number.
302 VNInfo *getNextValue(SlotIndex def, MachineInstr *CopyMI,
303 VNInfo::Allocator &VNInfoAllocator) {
305 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def, CopyMI);
306 valnos.push_back(VNI);
310 /// Create a copy of the given value. The new value will be identical except
311 /// for the Value number.
312 VNInfo *createValueCopy(const VNInfo *orig,
313 VNInfo::Allocator &VNInfoAllocator) {
315 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
316 valnos.push_back(VNI);
320 /// RenumberValues - Renumber all values in order of appearance and remove
322 /// Recalculate phi-kill flags in case any phi-def values were removed.
323 void RenumberValues(LiveIntervals &lis);
325 /// isOnlyLROfValNo - Return true if the specified live range is the only
326 /// one defined by the its val#.
327 bool isOnlyLROfValNo(const LiveRange *LR) {
328 for (const_iterator I = begin(), E = end(); I != E; ++I) {
329 const LiveRange *Tmp = I;
330 if (Tmp != LR && Tmp->valno == LR->valno)
336 /// MergeValueNumberInto - This method is called when two value nubmers
337 /// are found to be equivalent. This eliminates V1, replacing all
338 /// LiveRanges with the V1 value number with the V2 value number. This can
339 /// cause merging of V1/V2 values numbers and compaction of the value space.
340 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
342 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
343 /// in RHS into this live interval as the specified value number.
344 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
345 /// current interval, it will replace the value numbers of the overlaped
346 /// live ranges with the specified value number.
347 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
349 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
350 /// in RHS into this live interval as the specified value number.
351 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
352 /// current interval, but only if the overlapping LiveRanges have the
353 /// specified value number.
354 void MergeValueInAsValue(const LiveInterval &RHS,
355 const VNInfo *RHSValNo, VNInfo *LHSValNo);
357 /// Copy - Copy the specified live interval. This copies all the fields
358 /// except for the register of the interval.
359 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
360 VNInfo::Allocator &VNInfoAllocator);
362 bool empty() const { return ranges.empty(); }
364 /// beginIndex - Return the lowest numbered slot covered by interval.
365 SlotIndex beginIndex() const {
366 assert(!empty() && "Call to beginIndex() on empty interval.");
367 return ranges.front().start;
370 /// endNumber - return the maximum point of the interval of the whole,
372 SlotIndex endIndex() const {
373 assert(!empty() && "Call to endIndex() on empty interval.");
374 return ranges.back().end;
377 bool expiredAt(SlotIndex index) const {
378 return index >= endIndex();
381 bool liveAt(SlotIndex index) const {
382 const_iterator r = find(index);
383 return r != end() && r->start <= index;
386 /// killedAt - Return true if a live range ends at index. Note that the kill
387 /// point is not contained in the half-open live range. It is usually the
388 /// getDefIndex() slot following its last use.
389 bool killedAt(SlotIndex index) const {
390 const_iterator r = find(index.getUseIndex());
391 return r != end() && r->end == index;
394 /// killedInRange - Return true if the interval has kills in [Start,End).
395 /// Note that the kill point is considered the end of a live range, so it is
396 /// not contained in the live range. If a live range ends at End, it won't
397 /// be counted as a kill by this method.
398 bool killedInRange(SlotIndex Start, SlotIndex End) const;
400 /// getLiveRangeContaining - Return the live range that contains the
401 /// specified index, or null if there is none.
402 const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
403 const_iterator I = FindLiveRangeContaining(Idx);
404 return I == end() ? 0 : &*I;
407 /// getLiveRangeContaining - Return the live range that contains the
408 /// specified index, or null if there is none.
409 LiveRange *getLiveRangeContaining(SlotIndex Idx) {
410 iterator I = FindLiveRangeContaining(Idx);
411 return I == end() ? 0 : &*I;
414 /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
415 VNInfo *getVNInfoAt(SlotIndex Idx) const {
416 const_iterator I = FindLiveRangeContaining(Idx);
417 return I == end() ? 0 : I->valno;
420 /// FindLiveRangeContaining - Return an iterator to the live range that
421 /// contains the specified index, or end() if there is none.
422 iterator FindLiveRangeContaining(SlotIndex Idx) {
423 iterator I = find(Idx);
424 return I != end() && I->start <= Idx ? I : end();
427 const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
428 const_iterator I = find(Idx);
429 return I != end() && I->start <= Idx ? I : end();
432 /// findDefinedVNInfo - Find the by the specified
433 /// index (register interval) or defined
434 VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
437 /// overlaps - Return true if the intersection of the two live intervals is
439 bool overlaps(const LiveInterval& other) const {
442 return overlapsFrom(other, other.begin());
445 /// overlaps - Return true if the live interval overlaps a range specified
447 bool overlaps(SlotIndex Start, SlotIndex End) const;
449 /// overlapsFrom - Return true if the intersection of the two live intervals
450 /// is not empty. The specified iterator is a hint that we can begin
451 /// scanning the Other interval starting at I.
452 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
454 /// addRange - Add the specified LiveRange to this interval, merging
455 /// intervals as appropriate. This returns an iterator to the inserted live
456 /// range (which may have grown since it was inserted.
457 void addRange(LiveRange LR) {
458 addRangeFrom(LR, ranges.begin());
461 /// join - Join two live intervals (this, and other) together. This applies
462 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
463 /// the intervals are not joinable, this aborts.
464 void join(LiveInterval &Other,
465 const int *ValNoAssignments,
466 const int *RHSValNoAssignments,
467 SmallVector<VNInfo*, 16> &NewVNInfo,
468 MachineRegisterInfo *MRI);
470 /// isInOneLiveRange - Return true if the range specified is entirely in the
471 /// a single LiveRange of the live interval.
472 bool isInOneLiveRange(SlotIndex Start, SlotIndex End) const {
473 const_iterator r = find(Start);
474 return r != end() && r->containsRange(Start, End);
477 /// removeRange - Remove the specified range from this interval. Note that
478 /// the range must be a single LiveRange in its entirety.
479 void removeRange(SlotIndex Start, SlotIndex End,
480 bool RemoveDeadValNo = false);
482 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
483 removeRange(LR.start, LR.end, RemoveDeadValNo);
486 /// removeValNo - Remove all the ranges defined by the specified value#.
487 /// Also remove the value# from value# list.
488 void removeValNo(VNInfo *ValNo);
490 /// getSize - Returns the sum of sizes of all the LiveRange's.
492 unsigned getSize() const;
494 /// Returns true if the live interval is zero length, i.e. no live ranges
495 /// span instructions. It doesn't pay to spill such an interval.
496 bool isZeroLength() const {
497 for (const_iterator i = begin(), e = end(); i != e; ++i)
498 if (i->end.getPrevIndex() > i->start)
503 /// isSpillable - Can this interval be spilled?
504 bool isSpillable() const {
505 return weight != HUGE_VALF;
508 /// markNotSpillable - Mark interval as not spillable
509 void markNotSpillable() {
513 /// ComputeJoinedWeight - Set the weight of a live interval after
514 /// Other has been merged into it.
515 void ComputeJoinedWeight(const LiveInterval &Other);
517 bool operator<(const LiveInterval& other) const {
518 const SlotIndex &thisIndex = beginIndex();
519 const SlotIndex &otherIndex = other.beginIndex();
520 return (thisIndex < otherIndex ||
521 (thisIndex == otherIndex && reg < other.reg));
524 void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
529 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
530 void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
531 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
532 void markValNoForDeletion(VNInfo *V);
534 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
538 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {