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"
32 class MachineRegisterInfo;
33 class TargetRegisterInfo;
36 /// VNInfo - Value Number Information.
37 /// This class holds information about a machine level values, including
38 /// definition and use points.
40 /// Care must be taken in interpreting the def index of the value. The
41 /// following rules apply:
43 /// If the isDefAccurate() method returns false then def does not contain the
44 /// index of the defining MachineInstr, or even (necessarily) to a
45 /// MachineInstr at all. In general such a def index is not meaningful
46 /// and should not be used. The exception is that, for values originally
47 /// defined by PHI instructions, after PHI elimination def will contain the
48 /// index of the MBB in which the PHI originally existed. This can be used
49 /// to insert code (spills or copies) which deals with the value, which will
50 /// be live in to the block.
59 IS_DEF_ACCURATE = 1 << 4
65 /// Holds information about individual kills.
68 unsigned killIdx : 31;
70 KillInfo(bool isPHIKill, unsigned killIdx)
71 : isPHIKill(isPHIKill), killIdx(killIdx) {
73 assert(killIdx != 0 && "Zero kill indices are no longer permitted.");
78 typedef SmallVector<KillInfo, 4> KillSet;
80 /// The ID number of this value.
83 /// The index of the defining instruction (if isDefAccurate() returns true).
89 : flags(IS_UNUSED), id(~1U), def(0), copy(0) {}
91 /// VNInfo constructor.
92 /// d is presumed to point to the actual defining instr. If it doesn't
93 /// setIsDefAccurate(false) should be called after construction.
94 VNInfo(unsigned i, unsigned d, MachineInstr *c)
95 : flags(IS_DEF_ACCURATE), id(i), def(d), copy(c) {}
97 /// VNInfo construtor, copies values from orig, except for the value number.
98 VNInfo(unsigned i, const VNInfo &orig)
99 : flags(orig.flags), id(i), def(orig.def), copy(orig.copy),
102 /// Used for copying value number info.
103 unsigned getFlags() const { return flags; }
104 void setFlags(unsigned flags) { this->flags = flags; }
106 /// Returns true if one or more kills are PHI nodes.
107 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
108 void setHasPHIKill(bool hasKill) {
110 flags |= HAS_PHI_KILL;
112 flags &= ~HAS_PHI_KILL;
115 /// Returns true if this value is re-defined by an early clobber somewhere
116 /// during the live range.
117 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
118 void setHasRedefByEC(bool hasRedef) {
120 flags |= REDEF_BY_EC;
122 flags &= ~REDEF_BY_EC;
125 /// Returns true if this value is defined by a PHI instruction (or was,
126 /// PHI instrucions may have been eliminated).
127 bool isPHIDef() const { return flags & IS_PHI_DEF; }
128 void setIsPHIDef(bool phiDef) {
132 flags &= ~IS_PHI_DEF;
135 /// Returns true if this value is unused.
136 bool isUnused() const { return flags & IS_UNUSED; }
137 void setIsUnused(bool unused) {
144 /// Returns true if the def is accurate.
145 bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
146 void setIsDefAccurate(bool defAccurate) {
148 flags |= IS_DEF_ACCURATE;
150 flags &= ~IS_DEF_ACCURATE;
155 inline bool operator<(const VNInfo::KillInfo &k1, const VNInfo::KillInfo &k2) {
156 return k1.killIdx < k2.killIdx;
159 inline bool operator<(const VNInfo::KillInfo &k, unsigned idx) {
160 return k.killIdx < idx;
163 inline bool operator<(unsigned idx, const VNInfo::KillInfo &k) {
164 return idx < k.killIdx;
167 /// LiveRange structure - This represents a simple register range in the
168 /// program, with an inclusive start point and an exclusive end point.
169 /// These ranges are rendered as [start,end).
171 unsigned start; // Start point of the interval (inclusive)
172 unsigned end; // End point of the interval (exclusive)
173 VNInfo *valno; // identifier for the value contained in this interval.
175 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
176 assert(S < E && "Cannot create empty or backwards range");
179 /// contains - Return true if the index is covered by this range.
181 bool contains(unsigned I) const {
182 return start <= I && I < end;
185 bool operator<(const LiveRange &LR) const {
186 return start < LR.start || (start == LR.start && end < LR.end);
188 bool operator==(const LiveRange &LR) const {
189 return start == LR.start && end == LR.end;
193 void print(std::ostream &os) const;
194 void print(std::ostream *os) const { if (os) print(*os); }
197 LiveRange(); // DO NOT IMPLEMENT
200 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
203 inline bool operator<(unsigned V, const LiveRange &LR) {
207 inline bool operator<(const LiveRange &LR, unsigned V) {
211 /// LiveInterval - This class represents some number of live ranges for a
212 /// register or value. This class also contains a bit of register allocator
217 inline unsigned getVNInfoAlignment(void) {
219 return (unsigned)__alignof__(VNInfo);
229 typedef SmallVector<LiveRange,4> Ranges;
230 typedef SmallVector<VNInfo*,4> VNInfoList;
232 unsigned reg; // the register or stack slot of this interval
233 // if the top bits is set, it represents a stack slot.
234 float weight; // weight of this interval
235 Ranges ranges; // the ranges in which this register is live
236 VNInfoList valnos; // value#'s
247 static unsigned scale(unsigned slot, unsigned factor) {
248 unsigned index = slot / NUM,
250 assert(index <= ~0U / (factor * NUM) &&
251 "Rescaled interval would overflow");
252 return index * NUM * factor + offset;
257 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
258 : reg(Reg), weight(Weight) {
260 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
263 typedef Ranges::iterator iterator;
264 iterator begin() { return ranges.begin(); }
265 iterator end() { return ranges.end(); }
267 typedef Ranges::const_iterator const_iterator;
268 const_iterator begin() const { return ranges.begin(); }
269 const_iterator end() const { return ranges.end(); }
271 typedef VNInfoList::iterator vni_iterator;
272 vni_iterator vni_begin() { return valnos.begin(); }
273 vni_iterator vni_end() { return valnos.end(); }
275 typedef VNInfoList::const_iterator const_vni_iterator;
276 const_vni_iterator vni_begin() const { return valnos.begin(); }
277 const_vni_iterator vni_end() const { return valnos.end(); }
279 /// advanceTo - Advance the specified iterator to point to the LiveRange
280 /// containing the specified position, or end() if the position is past the
281 /// end of the interval. If no LiveRange contains this position, but the
282 /// position is in a hole, this method returns an iterator pointing the the
283 /// LiveRange immediately after the hole.
284 iterator advanceTo(iterator I, unsigned Pos) {
285 if (Pos >= endNumber())
287 while (I->end <= Pos) ++I;
292 while (!valnos.empty()) {
293 VNInfo *VNI = valnos.back();
301 /// isStackSlot - Return true if this is a stack slot interval.
303 bool isStackSlot() const {
304 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
307 /// getStackSlotIndex - Return stack slot index if this is a stack slot
309 int getStackSlotIndex() const {
310 assert(isStackSlot() && "Interval is not a stack slot interval!");
311 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
314 bool hasAtLeastOneValue() const { return !valnos.empty(); }
316 bool containsOneValue() const { return valnos.size() == 1; }
318 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
320 /// getValNumInfo - Returns pointer to the specified val#.
322 inline VNInfo *getValNumInfo(unsigned ValNo) {
323 return valnos[ValNo];
325 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
326 return valnos[ValNo];
329 /// copyValNumInfo - Copy the value number info for one value number to
331 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
332 DstValNo->def = SrcValNo->def;
333 DstValNo->copy = SrcValNo->copy;
334 DstValNo->setFlags(SrcValNo->getFlags());
335 DstValNo->kills = SrcValNo->kills;
338 /// getNextValue - Create a new value number and return it. MIIdx specifies
339 /// the instruction that defines the value number.
340 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
341 bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator) {
343 assert(MIIdx != ~0u && MIIdx != ~1u &&
344 "PHI def / unused flags should now be passed explicitly.");
346 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
347 getVNInfoAlignment()));
348 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
349 VNI->setIsDefAccurate(isDefAccurate);
350 valnos.push_back(VNI);
354 /// Create a copy of the given value. The new value will be identical except
355 /// for the Value number.
356 VNInfo *createValueCopy(const VNInfo *orig, BumpPtrAllocator &VNInfoAllocator) {
359 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
360 getVNInfoAlignment()));
362 new (VNI) VNInfo((unsigned)valnos.size(), *orig);
363 valnos.push_back(VNI);
367 /// addKill - Add a kill instruction index to the specified value
369 static void addKill(VNInfo *VNI, unsigned KillIdx, bool phiKill) {
370 VNInfo::KillSet &kills = VNI->kills;
371 VNInfo::KillInfo newKill(phiKill, KillIdx);
373 kills.push_back(newKill);
375 VNInfo::KillSet::iterator
376 I = std::lower_bound(kills.begin(), kills.end(), newKill);
377 kills.insert(I, newKill);
381 /// addKills - Add a number of kills into the VNInfo kill vector. If this
382 /// interval is live at a kill point, then the kill is not added.
383 void addKills(VNInfo *VNI, const VNInfo::KillSet &kills) {
384 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
386 const VNInfo::KillInfo &Kill = kills[i];
387 if (!liveBeforeAndAt(Kill.killIdx)) {
388 VNInfo::KillSet::iterator
389 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), Kill);
390 VNI->kills.insert(I, Kill);
395 /// removeKill - Remove the specified kill from the list of kills of
396 /// the specified val#.
397 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
398 VNInfo::KillSet &kills = VNI->kills;
399 VNInfo::KillSet::iterator
400 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
401 if (I != kills.end() && I->killIdx == KillIdx) {
408 /// removeKills - Remove all the kills in specified range
409 /// [Start, End] of the specified val#.
410 static void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
411 VNInfo::KillSet &kills = VNI->kills;
413 VNInfo::KillSet::iterator
414 I = std::lower_bound(kills.begin(), kills.end(), Start);
415 VNInfo::KillSet::iterator
416 E = std::upper_bound(kills.begin(), kills.end(), End);
420 /// isKill - Return true if the specified index is a kill of the
422 static bool isKill(const VNInfo *VNI, unsigned KillIdx) {
423 const VNInfo::KillSet &kills = VNI->kills;
424 VNInfo::KillSet::const_iterator
425 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
426 return I != kills.end() && I->killIdx == KillIdx;
429 /// isOnlyLROfValNo - Return true if the specified live range is the only
430 /// one defined by the its val#.
431 bool isOnlyLROfValNo(const LiveRange *LR) {
432 for (const_iterator I = begin(), E = end(); I != E; ++I) {
433 const LiveRange *Tmp = I;
434 if (Tmp != LR && Tmp->valno == LR->valno)
440 /// MergeValueNumberInto - This method is called when two value nubmers
441 /// are found to be equivalent. This eliminates V1, replacing all
442 /// LiveRanges with the V1 value number with the V2 value number. This can
443 /// cause merging of V1/V2 values numbers and compaction of the value space.
444 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
446 /// MergeInClobberRanges - For any live ranges that are not defined in the
447 /// current interval, but are defined in the Clobbers interval, mark them
448 /// used with an unknown definition value. Caller must pass in reference to
449 /// VNInfoAllocator since it will create a new val#.
450 void MergeInClobberRanges(const LiveInterval &Clobbers,
451 BumpPtrAllocator &VNInfoAllocator);
453 /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a
454 /// single LiveRange only.
455 void MergeInClobberRange(unsigned Start, unsigned End,
456 BumpPtrAllocator &VNInfoAllocator);
458 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
459 /// in RHS into this live interval as the specified value number.
460 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
461 /// current interval, it will replace the value numbers of the overlaped
462 /// live ranges with the specified value number.
463 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
465 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
466 /// in RHS into this live interval as the specified value number.
467 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
468 /// current interval, but only if the overlapping LiveRanges have the
469 /// specified value number.
470 void MergeValueInAsValue(const LiveInterval &RHS,
471 const VNInfo *RHSValNo, VNInfo *LHSValNo);
473 /// Copy - Copy the specified live interval. This copies all the fields
474 /// except for the register of the interval.
475 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
476 BumpPtrAllocator &VNInfoAllocator);
478 bool empty() const { return ranges.empty(); }
480 /// beginNumber - Return the lowest numbered slot covered by interval.
481 unsigned beginNumber() const {
484 return ranges.front().start;
487 /// endNumber - return the maximum point of the interval of the whole,
489 unsigned endNumber() const {
492 return ranges.back().end;
495 bool expiredAt(unsigned index) const {
496 return index >= endNumber();
499 bool liveAt(unsigned index) const;
501 // liveBeforeAndAt - Check if the interval is live at the index and the
502 // index just before it. If index is liveAt, check if it starts a new live
503 // range.If it does, then check if the previous live range ends at index-1.
504 bool liveBeforeAndAt(unsigned index) const;
506 /// getLiveRangeContaining - Return the live range that contains the
507 /// specified index, or null if there is none.
508 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
509 const_iterator I = FindLiveRangeContaining(Idx);
510 return I == end() ? 0 : &*I;
513 /// getLiveRangeContaining - Return the live range that contains the
514 /// specified index, or null if there is none.
515 LiveRange *getLiveRangeContaining(unsigned Idx) {
516 iterator I = FindLiveRangeContaining(Idx);
517 return I == end() ? 0 : &*I;
520 /// FindLiveRangeContaining - Return an iterator to the live range that
521 /// contains the specified index, or end() if there is none.
522 const_iterator FindLiveRangeContaining(unsigned Idx) const;
524 /// FindLiveRangeContaining - Return an iterator to the live range that
525 /// contains the specified index, or end() if there is none.
526 iterator FindLiveRangeContaining(unsigned Idx);
528 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
529 /// index (register interval) or defined by the specified register (stack
531 VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
533 /// overlaps - Return true if the intersection of the two live intervals is
535 bool overlaps(const LiveInterval& other) const {
536 return overlapsFrom(other, other.begin());
539 /// overlaps - Return true if the live interval overlaps a range specified
541 bool overlaps(unsigned Start, unsigned End) const;
543 /// overlapsFrom - Return true if the intersection of the two live intervals
544 /// is not empty. The specified iterator is a hint that we can begin
545 /// scanning the Other interval starting at I.
546 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
548 /// addRange - Add the specified LiveRange to this interval, merging
549 /// intervals as appropriate. This returns an iterator to the inserted live
550 /// range (which may have grown since it was inserted.
551 void addRange(LiveRange LR) {
552 addRangeFrom(LR, ranges.begin());
555 /// join - Join two live intervals (this, and other) together. This applies
556 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
557 /// the intervals are not joinable, this aborts.
558 void join(LiveInterval &Other, const int *ValNoAssignments,
559 const int *RHSValNoAssignments,
560 SmallVector<VNInfo*, 16> &NewVNInfo,
561 MachineRegisterInfo *MRI);
563 /// isInOneLiveRange - Return true if the range specified is entirely in the
564 /// a single LiveRange of the live interval.
565 bool isInOneLiveRange(unsigned Start, unsigned End);
567 /// removeRange - Remove the specified range from this interval. Note that
568 /// the range must be a single LiveRange in its entirety.
569 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
571 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
572 removeRange(LR.start, LR.end, RemoveDeadValNo);
575 /// removeValNo - Remove all the ranges defined by the specified value#.
576 /// Also remove the value# from value# list.
577 void removeValNo(VNInfo *ValNo);
579 /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
581 void scaleNumbering(unsigned factor);
583 /// getSize - Returns the sum of sizes of all the LiveRange's.
585 unsigned getSize() const;
587 bool operator<(const LiveInterval& other) const {
588 return beginNumber() < other.beginNumber();
591 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
592 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
593 if (OS) print(*OS, TRI);
599 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
600 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
601 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
602 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
606 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {