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.
42 /// Care must be taken in interpreting the def index of the value. The
43 /// following rules apply:
45 /// If the isDefAccurate() method returns false then def does not contain the
46 /// index of the defining MachineInstr, or even (necessarily) to a
47 /// MachineInstr at all. In general such a def index is not meaningful
48 /// and should not be used. The exception is that, for values originally
49 /// defined by PHI instructions, after PHI elimination def will contain the
50 /// index of the MBB in which the PHI originally existed. This can be used
51 /// to insert code (spills or copies) which deals with the value, which will
52 /// be live in to the block.
60 IS_DEF_ACCURATE = 1 << 4
71 typedef SmallVector<SlotIndex, 4> KillSet;
73 /// The ID number of this value.
76 /// The index of the defining instruction (if isDefAccurate() returns true).
82 VNInfo(LiveIntervals &li_)
83 : defflags(IS_UNUSED), id(~1U) { cr.copy = 0; }
86 /// VNInfo constructor.
87 /// d is presumed to point to the actual defining instr. If it doesn't
88 /// setIsDefAccurate(false) should be called after construction.
89 VNInfo(unsigned i, SlotIndex d, MachineInstr *c)
90 : flags(IS_DEF_ACCURATE), id(i), def(d) { cr.copy = c; }
92 /// VNInfo construtor, copies values from orig, except for the value number.
93 VNInfo(unsigned i, const VNInfo &orig)
94 : flags(orig.flags), cr(orig.cr), id(i), def(orig.def), kills(orig.kills)
97 /// Copy from the parameter into this VNInfo.
98 void copyFrom(VNInfo &src) {
105 /// Used for copying value number info.
106 unsigned getFlags() const { return flags; }
107 void setFlags(unsigned flags) { this->flags = flags; }
109 /// For a register interval, if this VN was definied by a copy instr
110 /// getCopy() returns a pointer to it, otherwise returns 0.
111 /// For a stack interval the behaviour of this method is undefined.
112 MachineInstr* getCopy() const { return cr.copy; }
113 /// For a register interval, set the copy member.
114 /// This method should not be called on stack intervals as it may lead to
115 /// undefined behavior.
116 void setCopy(MachineInstr *c) { cr.copy = c; }
118 /// For a stack interval, returns the reg which this stack interval was
120 /// For a register interval the behaviour of this method is undefined.
121 unsigned getReg() const { return cr.reg; }
122 /// For a stack interval, set the defining register.
123 /// This method should not be called on register intervals as it may lead
124 /// to undefined behaviour.
125 void setReg(unsigned reg) { cr.reg = reg; }
127 /// Returns true if one or more kills are PHI nodes.
128 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
129 /// Set the PHI kill flag on this value.
130 void setHasPHIKill(bool hasKill) {
132 flags |= HAS_PHI_KILL;
134 flags &= ~HAS_PHI_KILL;
137 /// Returns true if this value is re-defined by an early clobber somewhere
138 /// during the live range.
139 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
140 /// Set the "redef by early clobber" flag on this value.
141 void setHasRedefByEC(bool hasRedef) {
143 flags |= REDEF_BY_EC;
145 flags &= ~REDEF_BY_EC;
148 /// Returns true if this value is defined by a PHI instruction (or was,
149 /// PHI instrucions may have been eliminated).
150 bool isPHIDef() const { return flags & IS_PHI_DEF; }
151 /// Set the "phi def" flag on this value.
152 void setIsPHIDef(bool phiDef) {
156 flags &= ~IS_PHI_DEF;
159 /// Returns true if this value is unused.
160 bool isUnused() const { return flags & IS_UNUSED; }
161 /// Set the "is unused" flag on this value.
162 void setIsUnused(bool unused) {
169 /// Returns true if the def is accurate.
170 bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
171 /// Set the "is def accurate" flag on this value.
172 void setIsDefAccurate(bool defAccurate) {
174 flags |= IS_DEF_ACCURATE;
176 flags &= ~IS_DEF_ACCURATE;
179 /// Returns true if the given index is a kill of this value.
180 bool isKill(SlotIndex k) const {
181 KillSet::const_iterator
182 i = std::lower_bound(kills.begin(), kills.end(), k);
183 return (i != kills.end() && *i == k);
186 /// addKill - Add a kill instruction index to the specified value
188 void addKill(SlotIndex k) {
193 i = std::lower_bound(kills.begin(), kills.end(), k);
198 /// Remove the specified kill index from this value's kills list.
199 /// Returns true if the value was present, otherwise returns false.
200 bool removeKill(SlotIndex k) {
201 KillSet::iterator i = std::lower_bound(kills.begin(), kills.end(), k);
202 if (i != kills.end() && *i == k) {
209 /// Remove all kills in the range [s, e).
210 void removeKills(SlotIndex s, SlotIndex e) {
212 si = std::lower_bound(kills.begin(), kills.end(), s),
213 se = std::upper_bound(kills.begin(), kills.end(), e);
220 /// LiveRange structure - This represents a simple register range in the
221 /// program, with an inclusive start point and an exclusive end point.
222 /// These ranges are rendered as [start,end).
224 SlotIndex start; // Start point of the interval (inclusive)
225 SlotIndex end; // End point of the interval (exclusive)
226 VNInfo *valno; // identifier for the value contained in this interval.
228 LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
229 : start(S), end(E), valno(V) {
231 assert(S < E && "Cannot create empty or backwards range");
234 /// contains - Return true if the index is covered by this range.
236 bool contains(SlotIndex I) const {
237 return start <= I && I < end;
240 /// containsRange - Return true if the given range, [S, E), is covered by
242 bool containsRange(SlotIndex S, SlotIndex E) const {
243 assert((S < E) && "Backwards interval?");
244 return (start <= S && S < end) && (start < E && E <= end);
247 bool operator<(const LiveRange &LR) const {
248 return start < LR.start || (start == LR.start && end < LR.end);
250 bool operator==(const LiveRange &LR) const {
251 return start == LR.start && end == LR.end;
255 void print(raw_ostream &os) const;
258 LiveRange(); // DO NOT IMPLEMENT
261 raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
264 inline bool operator<(SlotIndex V, const LiveRange &LR) {
268 inline bool operator<(const LiveRange &LR, SlotIndex V) {
272 /// LiveInterval - This class represents some number of live ranges for a
273 /// register or value. This class also contains a bit of register allocator
278 typedef SmallVector<LiveRange,4> Ranges;
279 typedef SmallVector<VNInfo*,4> VNInfoList;
281 unsigned reg; // the register or stack slot of this interval
282 // if the top bits is set, it represents a stack slot.
283 float weight; // weight of this interval
284 Ranges ranges; // the ranges in which this register is live
285 VNInfoList valnos; // value#'s
298 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
299 : reg(Reg), weight(Weight) {
301 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
304 typedef Ranges::iterator iterator;
305 iterator begin() { return ranges.begin(); }
306 iterator end() { return ranges.end(); }
308 typedef Ranges::const_iterator const_iterator;
309 const_iterator begin() const { return ranges.begin(); }
310 const_iterator end() const { return ranges.end(); }
312 typedef VNInfoList::iterator vni_iterator;
313 vni_iterator vni_begin() { return valnos.begin(); }
314 vni_iterator vni_end() { return valnos.end(); }
316 typedef VNInfoList::const_iterator const_vni_iterator;
317 const_vni_iterator vni_begin() const { return valnos.begin(); }
318 const_vni_iterator vni_end() const { return valnos.end(); }
320 /// advanceTo - Advance the specified iterator to point to the LiveRange
321 /// containing the specified position, or end() if the position is past the
322 /// end of the interval. If no LiveRange contains this position, but the
323 /// position is in a hole, this method returns an iterator pointing to the
324 /// LiveRange immediately after the hole.
325 iterator advanceTo(iterator I, SlotIndex Pos) {
326 if (Pos >= endIndex())
328 while (I->end <= Pos) ++I;
337 /// isStackSlot - Return true if this is a stack slot interval.
339 bool isStackSlot() const {
340 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
343 /// getStackSlotIndex - Return stack slot index if this is a stack slot
345 int getStackSlotIndex() const {
346 assert(isStackSlot() && "Interval is not a stack slot interval!");
347 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
350 bool hasAtLeastOneValue() const { return !valnos.empty(); }
352 bool containsOneValue() const { return valnos.size() == 1; }
354 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
356 /// getValNumInfo - Returns pointer to the specified val#.
358 inline VNInfo *getValNumInfo(unsigned ValNo) {
359 return valnos[ValNo];
361 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
362 return valnos[ValNo];
365 /// getNextValue - Create a new value number and return it. MIIdx specifies
366 /// the instruction that defines the value number.
367 VNInfo *getNextValue(SlotIndex def, MachineInstr *CopyMI,
368 bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator){
370 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
372 new (VNI) VNInfo((unsigned)valnos.size(), def, CopyMI);
373 VNI->setIsDefAccurate(isDefAccurate);
374 valnos.push_back(VNI);
378 /// Create a copy of the given value. The new value will be identical except
379 /// for the Value number.
380 VNInfo *createValueCopy(const VNInfo *orig,
381 BumpPtrAllocator &VNInfoAllocator) {
383 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
386 new (VNI) VNInfo((unsigned)valnos.size(), *orig);
387 valnos.push_back(VNI);
391 /// addKills - Add a number of kills into the VNInfo kill vector. If this
392 /// interval is live at a kill point, then the kill is not added.
393 void addKills(VNInfo *VNI, const VNInfo::KillSet &kills) {
394 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
396 if (!liveBeforeAndAt(kills[i])) {
397 VNI->addKill(kills[i]);
402 /// isOnlyLROfValNo - Return true if the specified live range is the only
403 /// one defined by the its val#.
404 bool isOnlyLROfValNo(const LiveRange *LR) {
405 for (const_iterator I = begin(), E = end(); I != E; ++I) {
406 const LiveRange *Tmp = I;
407 if (Tmp != LR && Tmp->valno == LR->valno)
413 /// MergeValueNumberInto - This method is called when two value nubmers
414 /// are found to be equivalent. This eliminates V1, replacing all
415 /// LiveRanges with the V1 value number with the V2 value number. This can
416 /// cause merging of V1/V2 values numbers and compaction of the value space.
417 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
419 /// MergeInClobberRanges - For any live ranges that are not defined in the
420 /// current interval, but are defined in the Clobbers interval, mark them
421 /// used with an unknown definition value. Caller must pass in reference to
422 /// VNInfoAllocator since it will create a new val#.
423 void MergeInClobberRanges(LiveIntervals &li_,
424 const LiveInterval &Clobbers,
425 BumpPtrAllocator &VNInfoAllocator);
427 /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a
428 /// single LiveRange only.
429 void MergeInClobberRange(LiveIntervals &li_,
432 BumpPtrAllocator &VNInfoAllocator);
434 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
435 /// in RHS into this live interval as the specified value number.
436 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
437 /// current interval, it will replace the value numbers of the overlaped
438 /// live ranges with the specified value number.
439 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
441 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
442 /// in RHS into this live interval as the specified value number.
443 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
444 /// current interval, but only if the overlapping LiveRanges have the
445 /// specified value number.
446 void MergeValueInAsValue(const LiveInterval &RHS,
447 const VNInfo *RHSValNo, VNInfo *LHSValNo);
449 /// Copy - Copy the specified live interval. This copies all the fields
450 /// except for the register of the interval.
451 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
452 BumpPtrAllocator &VNInfoAllocator);
454 bool empty() const { return ranges.empty(); }
456 /// beginIndex - Return the lowest numbered slot covered by interval.
457 SlotIndex beginIndex() const {
458 assert(!empty() && "Call to beginIndex() on empty interval.");
459 return ranges.front().start;
462 /// endNumber - return the maximum point of the interval of the whole,
464 SlotIndex endIndex() const {
465 assert(!empty() && "Call to endIndex() on empty interval.");
466 return ranges.back().end;
469 bool expiredAt(SlotIndex index) const {
470 return index >= endIndex();
473 bool liveAt(SlotIndex index) const;
475 // liveBeforeAndAt - Check if the interval is live at the index and the
476 // index just before it. If index is liveAt, check if it starts a new live
477 // range.If it does, then check if the previous live range ends at index-1.
478 bool liveBeforeAndAt(SlotIndex index) const;
480 /// getLiveRangeContaining - Return the live range that contains the
481 /// specified index, or null if there is none.
482 const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
483 const_iterator I = FindLiveRangeContaining(Idx);
484 return I == end() ? 0 : &*I;
487 /// getLiveRangeContaining - Return the live range that contains the
488 /// specified index, or null if there is none.
489 LiveRange *getLiveRangeContaining(SlotIndex Idx) {
490 iterator I = FindLiveRangeContaining(Idx);
491 return I == end() ? 0 : &*I;
494 /// FindLiveRangeContaining - Return an iterator to the live range that
495 /// contains the specified index, or end() if there is none.
496 const_iterator FindLiveRangeContaining(SlotIndex Idx) const;
498 /// FindLiveRangeContaining - Return an iterator to the live range that
499 /// contains the specified index, or end() if there is none.
500 iterator FindLiveRangeContaining(SlotIndex Idx);
502 /// findDefinedVNInfo - Find the by the specified
503 /// index (register interval) or defined
504 VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
506 /// findDefinedVNInfo - Find the VNInfo that's defined by the specified
507 /// register (stack inteval only).
508 VNInfo *findDefinedVNInfoForStackInt(unsigned Reg) const;
511 /// overlaps - Return true if the intersection of the two live intervals is
513 bool overlaps(const LiveInterval& other) const {
514 return overlapsFrom(other, other.begin());
517 /// overlaps - Return true if the live interval overlaps a range specified
519 bool overlaps(SlotIndex Start, SlotIndex End) const;
521 /// overlapsFrom - Return true if the intersection of the two live intervals
522 /// is not empty. The specified iterator is a hint that we can begin
523 /// scanning the Other interval starting at I.
524 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
526 /// addRange - Add the specified LiveRange to this interval, merging
527 /// intervals as appropriate. This returns an iterator to the inserted live
528 /// range (which may have grown since it was inserted.
529 void addRange(LiveRange LR) {
530 addRangeFrom(LR, ranges.begin());
533 /// join - Join two live intervals (this, and other) together. This applies
534 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
535 /// the intervals are not joinable, this aborts.
536 void join(LiveInterval &Other,
537 const int *ValNoAssignments,
538 const int *RHSValNoAssignments,
539 SmallVector<VNInfo*, 16> &NewVNInfo,
540 MachineRegisterInfo *MRI);
542 /// isInOneLiveRange - Return true if the range specified is entirely in the
543 /// a single LiveRange of the live interval.
544 bool isInOneLiveRange(SlotIndex Start, SlotIndex End);
546 /// removeRange - Remove the specified range from this interval. Note that
547 /// the range must be a single LiveRange in its entirety.
548 void removeRange(SlotIndex Start, SlotIndex End,
549 bool RemoveDeadValNo = false);
551 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
552 removeRange(LR.start, LR.end, RemoveDeadValNo);
555 /// removeValNo - Remove all the ranges defined by the specified value#.
556 /// Also remove the value# from value# list.
557 void removeValNo(VNInfo *ValNo);
559 /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
561 void scaleNumbering(unsigned factor);
563 /// getSize - Returns the sum of sizes of all the LiveRange's.
565 unsigned getSize() const;
567 /// isSpillable - Can this interval be spilled?
568 bool isSpillable() const {
569 return weight != HUGE_VALF;
572 /// markNotSpillable - Mark interval as not spillable
573 void markNotSpillable() {
577 /// ComputeJoinedWeight - Set the weight of a live interval after
578 /// Other has been merged into it.
579 void ComputeJoinedWeight(const LiveInterval &Other);
581 bool operator<(const LiveInterval& other) const {
582 const SlotIndex &thisIndex = beginIndex();
583 const SlotIndex &otherIndex = other.beginIndex();
584 return (thisIndex < otherIndex ||
585 (thisIndex == otherIndex && reg < other.reg));
588 void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
593 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
594 void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
595 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
597 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
601 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {