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"
33 class MachineRegisterInfo;
34 class TargetRegisterInfo;
37 /// VNInfo - Value Number Information.
38 /// This class holds information about a machine level values, including
39 /// definition and use points.
41 /// Care must be taken in interpreting the def index of the value. The
42 /// following rules apply:
44 /// If the isDefAccurate() method returns false then def does not contain the
45 /// index of the defining MachineInstr, or even (necessarily) to a
46 /// MachineInstr at all. In general such a def index is not meaningful
47 /// and should not be used. The exception is that, for values originally
48 /// defined by PHI instructions, after PHI elimination def will contain the
49 /// index of the MBB in which the PHI originally existed. This can be used
50 /// to insert code (spills or copies) which deals with the value, which will
51 /// be live in to the block.
60 IS_DEF_ACCURATE = 1 << 4
66 /// Holds information about individual kills.
69 unsigned killIdx : 31;
71 KillInfo(bool isPHIKill, unsigned killIdx)
72 : isPHIKill(isPHIKill), killIdx(killIdx) {
74 assert(killIdx != 0 && "Zero kill indices are no longer permitted.");
79 typedef SmallVector<KillInfo, 4> KillSet;
81 /// The ID number of this value.
84 /// The index of the defining instruction (if isDefAccurate() returns true).
90 : flags(IS_UNUSED), id(~1U), def(0), copy(0) {}
92 /// VNInfo constructor.
93 /// d is presumed to point to the actual defining instr. If it doesn't
94 /// setIsDefAccurate(false) should be called after construction.
95 VNInfo(unsigned i, unsigned d, MachineInstr *c)
96 : flags(IS_DEF_ACCURATE), id(i), def(d), copy(c) {}
98 /// VNInfo construtor, copies values from orig, except for the value number.
99 VNInfo(unsigned i, const VNInfo &orig)
100 : flags(orig.flags), id(i), def(orig.def), copy(orig.copy),
103 /// Used for copying value number info.
104 unsigned getFlags() const { return flags; }
105 void setFlags(unsigned flags) { this->flags = flags; }
107 /// Returns true if one or more kills are PHI nodes.
108 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
109 void setHasPHIKill(bool hasKill) {
111 flags |= HAS_PHI_KILL;
113 flags &= ~HAS_PHI_KILL;
116 /// Returns true if this value is re-defined by an early clobber somewhere
117 /// during the live range.
118 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
119 void setHasRedefByEC(bool hasRedef) {
121 flags |= REDEF_BY_EC;
123 flags &= ~REDEF_BY_EC;
126 /// Returns true if this value is defined by a PHI instruction (or was,
127 /// PHI instrucions may have been eliminated).
128 bool isPHIDef() const { return flags & IS_PHI_DEF; }
129 void setIsPHIDef(bool phiDef) {
133 flags &= ~IS_PHI_DEF;
136 /// Returns true if this value is unused.
137 bool isUnused() const { return flags & IS_UNUSED; }
138 void setIsUnused(bool unused) {
145 /// Returns true if the def is accurate.
146 bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
147 void setIsDefAccurate(bool defAccurate) {
149 flags |= IS_DEF_ACCURATE;
151 flags &= ~IS_DEF_ACCURATE;
156 inline bool operator<(const VNInfo::KillInfo &k1, const VNInfo::KillInfo &k2) {
157 return k1.killIdx < k2.killIdx;
160 inline bool operator<(const VNInfo::KillInfo &k, unsigned idx) {
161 return k.killIdx < idx;
164 inline bool operator<(unsigned idx, const VNInfo::KillInfo &k) {
165 return idx < k.killIdx;
168 /// LiveRange structure - This represents a simple register range in the
169 /// program, with an inclusive start point and an exclusive end point.
170 /// These ranges are rendered as [start,end).
172 unsigned start; // Start point of the interval (inclusive)
173 unsigned end; // End point of the interval (exclusive)
174 VNInfo *valno; // identifier for the value contained in this interval.
176 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
177 assert(S < E && "Cannot create empty or backwards range");
180 /// contains - Return true if the index is covered by this range.
182 bool contains(unsigned I) const {
183 return start <= I && I < end;
186 bool operator<(const LiveRange &LR) const {
187 return start < LR.start || (start == LR.start && end < LR.end);
189 bool operator==(const LiveRange &LR) const {
190 return start == LR.start && end == LR.end;
194 void print(std::ostream &os) const;
195 void print(std::ostream *os) const { if (os) print(*os); }
198 LiveRange(); // DO NOT IMPLEMENT
201 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
204 inline bool operator<(unsigned V, const LiveRange &LR) {
208 inline bool operator<(const LiveRange &LR, unsigned V) {
212 /// LiveInterval - This class represents some number of live ranges for a
213 /// register or value. This class also contains a bit of register allocator
218 typedef SmallVector<LiveRange,4> Ranges;
219 typedef SmallVector<VNInfo*,4> VNInfoList;
221 unsigned reg; // the register or stack slot of this interval
222 // if the top bits is set, it represents a stack slot.
223 float weight; // weight of this interval
224 Ranges ranges; // the ranges in which this register is live
225 VNInfoList valnos; // value#'s
236 static unsigned scale(unsigned slot, unsigned factor) {
237 unsigned index = slot / NUM,
239 assert(index <= ~0U / (factor * NUM) &&
240 "Rescaled interval would overflow");
241 return index * NUM * factor + offset;
246 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
247 : reg(Reg), weight(Weight) {
249 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
252 typedef Ranges::iterator iterator;
253 iterator begin() { return ranges.begin(); }
254 iterator end() { return ranges.end(); }
256 typedef Ranges::const_iterator const_iterator;
257 const_iterator begin() const { return ranges.begin(); }
258 const_iterator end() const { return ranges.end(); }
260 typedef VNInfoList::iterator vni_iterator;
261 vni_iterator vni_begin() { return valnos.begin(); }
262 vni_iterator vni_end() { return valnos.end(); }
264 typedef VNInfoList::const_iterator const_vni_iterator;
265 const_vni_iterator vni_begin() const { return valnos.begin(); }
266 const_vni_iterator vni_end() const { return valnos.end(); }
268 /// advanceTo - Advance the specified iterator to point to the LiveRange
269 /// containing the specified position, or end() if the position is past the
270 /// end of the interval. If no LiveRange contains this position, but the
271 /// position is in a hole, this method returns an iterator pointing the the
272 /// LiveRange immediately after the hole.
273 iterator advanceTo(iterator I, unsigned Pos) {
274 if (Pos >= endNumber())
276 while (I->end <= Pos) ++I;
281 while (!valnos.empty()) {
282 VNInfo *VNI = valnos.back();
290 /// isStackSlot - Return true if this is a stack slot interval.
292 bool isStackSlot() const {
293 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
296 /// getStackSlotIndex - Return stack slot index if this is a stack slot
298 int getStackSlotIndex() const {
299 assert(isStackSlot() && "Interval is not a stack slot interval!");
300 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
303 bool hasAtLeastOneValue() const { return !valnos.empty(); }
305 bool containsOneValue() const { return valnos.size() == 1; }
307 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
309 /// getValNumInfo - Returns pointer to the specified val#.
311 inline VNInfo *getValNumInfo(unsigned ValNo) {
312 return valnos[ValNo];
314 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
315 return valnos[ValNo];
318 /// copyValNumInfo - Copy the value number info for one value number to
320 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
321 DstValNo->def = SrcValNo->def;
322 DstValNo->copy = SrcValNo->copy;
323 DstValNo->setFlags(SrcValNo->getFlags());
324 DstValNo->kills = SrcValNo->kills;
327 /// getNextValue - Create a new value number and return it. MIIdx specifies
328 /// the instruction that defines the value number.
329 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
330 bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator) {
332 assert(MIIdx != ~0u && MIIdx != ~1u &&
333 "PHI def / unused flags should now be passed explicitly.");
335 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
337 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
338 VNI->setIsDefAccurate(isDefAccurate);
339 valnos.push_back(VNI);
343 /// Create a copy of the given value. The new value will be identical except
344 /// for the Value number.
345 VNInfo *createValueCopy(const VNInfo *orig, BumpPtrAllocator &VNInfoAllocator) {
348 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
351 new (VNI) VNInfo((unsigned)valnos.size(), *orig);
352 valnos.push_back(VNI);
356 /// addKill - Add a kill instruction index to the specified value
358 static void addKill(VNInfo *VNI, unsigned KillIdx, bool phiKill) {
359 VNInfo::KillSet &kills = VNI->kills;
360 VNInfo::KillInfo newKill(phiKill, KillIdx);
362 kills.push_back(newKill);
364 VNInfo::KillSet::iterator
365 I = std::lower_bound(kills.begin(), kills.end(), newKill);
366 kills.insert(I, newKill);
370 /// addKills - Add a number of kills into the VNInfo kill vector. If this
371 /// interval is live at a kill point, then the kill is not added.
372 void addKills(VNInfo *VNI, const VNInfo::KillSet &kills) {
373 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
375 const VNInfo::KillInfo &Kill = kills[i];
376 if (!liveBeforeAndAt(Kill.killIdx)) {
377 VNInfo::KillSet::iterator
378 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), Kill);
379 VNI->kills.insert(I, Kill);
384 /// removeKill - Remove the specified kill from the list of kills of
385 /// the specified val#.
386 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
387 VNInfo::KillSet &kills = VNI->kills;
388 VNInfo::KillSet::iterator
389 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
390 if (I != kills.end() && I->killIdx == KillIdx) {
397 /// removeKills - Remove all the kills in specified range
398 /// [Start, End] of the specified val#.
399 static void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
400 VNInfo::KillSet &kills = VNI->kills;
402 VNInfo::KillSet::iterator
403 I = std::lower_bound(kills.begin(), kills.end(), Start);
404 VNInfo::KillSet::iterator
405 E = std::upper_bound(kills.begin(), kills.end(), End);
409 /// isKill - Return true if the specified index is a kill of the
411 static bool isKill(const VNInfo *VNI, unsigned KillIdx) {
412 const VNInfo::KillSet &kills = VNI->kills;
413 VNInfo::KillSet::const_iterator
414 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
415 return I != kills.end() && I->killIdx == KillIdx;
418 /// isOnlyLROfValNo - Return true if the specified live range is the only
419 /// one defined by the its val#.
420 bool isOnlyLROfValNo(const LiveRange *LR) {
421 for (const_iterator I = begin(), E = end(); I != E; ++I) {
422 const LiveRange *Tmp = I;
423 if (Tmp != LR && Tmp->valno == LR->valno)
429 /// MergeValueNumberInto - This method is called when two value nubmers
430 /// are found to be equivalent. This eliminates V1, replacing all
431 /// LiveRanges with the V1 value number with the V2 value number. This can
432 /// cause merging of V1/V2 values numbers and compaction of the value space.
433 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
435 /// MergeInClobberRanges - For any live ranges that are not defined in the
436 /// current interval, but are defined in the Clobbers interval, mark them
437 /// used with an unknown definition value. Caller must pass in reference to
438 /// VNInfoAllocator since it will create a new val#.
439 void MergeInClobberRanges(const LiveInterval &Clobbers,
440 BumpPtrAllocator &VNInfoAllocator);
442 /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a
443 /// single LiveRange only.
444 void MergeInClobberRange(unsigned Start, unsigned End,
445 BumpPtrAllocator &VNInfoAllocator);
447 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
448 /// in RHS into this live interval as the specified value number.
449 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
450 /// current interval, it will replace the value numbers of the overlaped
451 /// live ranges with the specified value number.
452 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
454 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
455 /// in RHS into this live interval as the specified value number.
456 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
457 /// current interval, but only if the overlapping LiveRanges have the
458 /// specified value number.
459 void MergeValueInAsValue(const LiveInterval &RHS,
460 const VNInfo *RHSValNo, VNInfo *LHSValNo);
462 /// Copy - Copy the specified live interval. This copies all the fields
463 /// except for the register of the interval.
464 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
465 BumpPtrAllocator &VNInfoAllocator);
467 bool empty() const { return ranges.empty(); }
469 /// beginNumber - Return the lowest numbered slot covered by interval.
470 unsigned beginNumber() const {
473 return ranges.front().start;
476 /// endNumber - return the maximum point of the interval of the whole,
478 unsigned endNumber() const {
481 return ranges.back().end;
484 bool expiredAt(unsigned index) const {
485 return index >= endNumber();
488 bool liveAt(unsigned index) const;
490 // liveBeforeAndAt - Check if the interval is live at the index and the
491 // index just before it. If index is liveAt, check if it starts a new live
492 // range.If it does, then check if the previous live range ends at index-1.
493 bool liveBeforeAndAt(unsigned index) const;
495 /// getLiveRangeContaining - Return the live range that contains the
496 /// specified index, or null if there is none.
497 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
498 const_iterator I = FindLiveRangeContaining(Idx);
499 return I == end() ? 0 : &*I;
502 /// getLiveRangeContaining - Return the live range that contains the
503 /// specified index, or null if there is none.
504 LiveRange *getLiveRangeContaining(unsigned Idx) {
505 iterator I = FindLiveRangeContaining(Idx);
506 return I == end() ? 0 : &*I;
509 /// FindLiveRangeContaining - Return an iterator to the live range that
510 /// contains the specified index, or end() if there is none.
511 const_iterator FindLiveRangeContaining(unsigned Idx) const;
513 /// FindLiveRangeContaining - Return an iterator to the live range that
514 /// contains the specified index, or end() if there is none.
515 iterator FindLiveRangeContaining(unsigned Idx);
517 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
518 /// index (register interval) or defined by the specified register (stack
520 VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
522 /// overlaps - Return true if the intersection of the two live intervals is
524 bool overlaps(const LiveInterval& other) const {
525 return overlapsFrom(other, other.begin());
528 /// overlaps - Return true if the live interval overlaps a range specified
530 bool overlaps(unsigned Start, unsigned End) const;
532 /// overlapsFrom - Return true if the intersection of the two live intervals
533 /// is not empty. The specified iterator is a hint that we can begin
534 /// scanning the Other interval starting at I.
535 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
537 /// addRange - Add the specified LiveRange to this interval, merging
538 /// intervals as appropriate. This returns an iterator to the inserted live
539 /// range (which may have grown since it was inserted.
540 void addRange(LiveRange LR) {
541 addRangeFrom(LR, ranges.begin());
544 /// join - Join two live intervals (this, and other) together. This applies
545 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
546 /// the intervals are not joinable, this aborts.
547 void join(LiveInterval &Other, const int *ValNoAssignments,
548 const int *RHSValNoAssignments,
549 SmallVector<VNInfo*, 16> &NewVNInfo,
550 MachineRegisterInfo *MRI);
552 /// isInOneLiveRange - Return true if the range specified is entirely in the
553 /// a single LiveRange of the live interval.
554 bool isInOneLiveRange(unsigned Start, unsigned End);
556 /// removeRange - Remove the specified range from this interval. Note that
557 /// the range must be a single LiveRange in its entirety.
558 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
560 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
561 removeRange(LR.start, LR.end, RemoveDeadValNo);
564 /// removeValNo - Remove all the ranges defined by the specified value#.
565 /// Also remove the value# from value# list.
566 void removeValNo(VNInfo *ValNo);
568 /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
570 void scaleNumbering(unsigned factor);
572 /// getSize - Returns the sum of sizes of all the LiveRange's.
574 unsigned getSize() const;
576 bool operator<(const LiveInterval& other) const {
577 return beginNumber() < other.beginNumber();
580 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
581 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
582 if (OS) print(*OS, TRI);
588 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
589 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
590 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
591 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
595 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {