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/IntEqClasses.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 /// Merge flags from another VNInfo
85 void mergeFlags(const VNInfo *VNI) {
86 flags = (flags | VNI->flags) & ~IS_UNUSED;
89 /// For a register interval, if this VN was definied by a copy instr
90 /// getCopy() returns a pointer to it, otherwise returns 0.
91 /// For a stack interval the behaviour of this method is undefined.
92 MachineInstr* getCopy() const { return copy; }
93 /// For a register interval, set the copy member.
94 /// This method should not be called on stack intervals as it may lead to
95 /// undefined behavior.
96 void setCopy(MachineInstr *c) { copy = c; }
98 /// isDefByCopy - Return true when this value was defined by a copy-like
99 /// instruction as determined by MachineInstr::isCopyLike.
100 bool isDefByCopy() const { return copy != 0; }
102 /// Returns true if one or more kills are PHI nodes.
103 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
104 /// Set the PHI kill flag on this value.
105 void setHasPHIKill(bool hasKill) {
107 flags |= HAS_PHI_KILL;
109 flags &= ~HAS_PHI_KILL;
112 /// Returns true if this value is re-defined by an early clobber somewhere
113 /// during the live range.
114 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
115 /// Set the "redef by early clobber" flag on this value.
116 void setHasRedefByEC(bool hasRedef) {
118 flags |= REDEF_BY_EC;
120 flags &= ~REDEF_BY_EC;
123 /// Returns true if this value is defined by a PHI instruction (or was,
124 /// PHI instrucions may have been eliminated).
125 bool isPHIDef() const { return flags & IS_PHI_DEF; }
126 /// Set the "phi def" flag on this value.
127 void setIsPHIDef(bool phiDef) {
131 flags &= ~IS_PHI_DEF;
134 /// Returns true if this value is unused.
135 bool isUnused() const { return flags & IS_UNUSED; }
136 /// Set the "is unused" flag on this value.
137 void setIsUnused(bool unused) {
145 /// LiveRange structure - This represents a simple register range in the
146 /// program, with an inclusive start point and an exclusive end point.
147 /// These ranges are rendered as [start,end).
149 SlotIndex start; // Start point of the interval (inclusive)
150 SlotIndex end; // End point of the interval (exclusive)
151 VNInfo *valno; // identifier for the value contained in this interval.
153 LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
154 : start(S), end(E), valno(V) {
156 assert(S < E && "Cannot create empty or backwards range");
159 /// contains - Return true if the index is covered by this range.
161 bool contains(SlotIndex I) const {
162 return start <= I && I < end;
165 /// containsRange - Return true if the given range, [S, E), is covered by
167 bool containsRange(SlotIndex S, SlotIndex E) const {
168 assert((S < E) && "Backwards interval?");
169 return (start <= S && S < end) && (start < E && E <= end);
172 bool operator<(const LiveRange &LR) const {
173 return start < LR.start || (start == LR.start && end < LR.end);
175 bool operator==(const LiveRange &LR) const {
176 return start == LR.start && end == LR.end;
180 void print(raw_ostream &os) const;
183 LiveRange(); // DO NOT IMPLEMENT
186 template <> struct isPodLike<LiveRange> { static const bool value = true; };
188 raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
191 inline bool operator<(SlotIndex V, const LiveRange &LR) {
195 inline bool operator<(const LiveRange &LR, SlotIndex V) {
199 /// LiveInterval - This class represents some number of live ranges for a
200 /// register or value. This class also contains a bit of register allocator
205 typedef SmallVector<LiveRange,4> Ranges;
206 typedef SmallVector<VNInfo*,4> VNInfoList;
208 const unsigned reg; // the register or stack slot of this interval.
209 float weight; // weight of this interval
210 Ranges ranges; // the ranges in which this register is live
211 VNInfoList valnos; // value#'s
224 LiveInterval(unsigned Reg, float Weight)
225 : reg(Reg), weight(Weight) {}
227 typedef Ranges::iterator iterator;
228 iterator begin() { return ranges.begin(); }
229 iterator end() { return ranges.end(); }
231 typedef Ranges::const_iterator const_iterator;
232 const_iterator begin() const { return ranges.begin(); }
233 const_iterator end() const { return ranges.end(); }
235 typedef VNInfoList::iterator vni_iterator;
236 vni_iterator vni_begin() { return valnos.begin(); }
237 vni_iterator vni_end() { return valnos.end(); }
239 typedef VNInfoList::const_iterator const_vni_iterator;
240 const_vni_iterator vni_begin() const { return valnos.begin(); }
241 const_vni_iterator vni_end() const { return valnos.end(); }
243 /// advanceTo - Advance the specified iterator to point to the LiveRange
244 /// containing the specified position, or end() if the position is past the
245 /// end of the interval. If no LiveRange contains this position, but the
246 /// position is in a hole, this method returns an iterator pointing to the
247 /// LiveRange immediately after the hole.
248 iterator advanceTo(iterator I, SlotIndex Pos) {
250 if (Pos >= endIndex())
252 while (I->end <= Pos) ++I;
256 /// find - Return an iterator pointing to the first range that ends after
257 /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
258 /// when searching large intervals.
260 /// If Pos is contained in a LiveRange, that range is returned.
261 /// If Pos is in a hole, the following LiveRange is returned.
262 /// If Pos is beyond endIndex, end() is returned.
263 iterator find(SlotIndex Pos);
265 const_iterator find(SlotIndex Pos) const {
266 return const_cast<LiveInterval*>(this)->find(Pos);
274 bool hasAtLeastOneValue() const { return !valnos.empty(); }
276 bool containsOneValue() const { return valnos.size() == 1; }
278 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
280 /// getValNumInfo - Returns pointer to the specified val#.
282 inline VNInfo *getValNumInfo(unsigned ValNo) {
283 return valnos[ValNo];
285 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
286 return valnos[ValNo];
289 /// containsValue - Returns true if VNI belongs to this interval.
290 bool containsValue(const VNInfo *VNI) const {
291 return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id);
294 /// getNextValue - Create a new value number and return it. MIIdx specifies
295 /// the instruction that defines the value number.
296 VNInfo *getNextValue(SlotIndex def, MachineInstr *CopyMI,
297 VNInfo::Allocator &VNInfoAllocator) {
299 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def, CopyMI);
300 valnos.push_back(VNI);
304 /// Create a copy of the given value. The new value will be identical except
305 /// for the Value number.
306 VNInfo *createValueCopy(const VNInfo *orig,
307 VNInfo::Allocator &VNInfoAllocator) {
309 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
310 valnos.push_back(VNI);
314 /// RenumberValues - Renumber all values in order of appearance and remove
316 void RenumberValues(LiveIntervals &lis);
318 /// isOnlyLROfValNo - Return true if the specified live range is the only
319 /// one defined by the its val#.
320 bool isOnlyLROfValNo(const LiveRange *LR) {
321 for (const_iterator I = begin(), E = end(); I != E; ++I) {
322 const LiveRange *Tmp = I;
323 if (Tmp != LR && Tmp->valno == LR->valno)
329 /// MergeValueNumberInto - This method is called when two value nubmers
330 /// are found to be equivalent. This eliminates V1, replacing all
331 /// LiveRanges with the V1 value number with the V2 value number. This can
332 /// cause merging of V1/V2 values numbers and compaction of the value space.
333 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
335 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
336 /// in RHS into this live interval as the specified value number.
337 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
338 /// current interval, it will replace the value numbers of the overlaped
339 /// live ranges with the specified value number.
340 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
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, but only if the overlapping LiveRanges have the
346 /// specified value number.
347 void MergeValueInAsValue(const LiveInterval &RHS,
348 const VNInfo *RHSValNo, VNInfo *LHSValNo);
350 /// Copy - Copy the specified live interval. This copies all the fields
351 /// except for the register of the interval.
352 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
353 VNInfo::Allocator &VNInfoAllocator);
355 bool empty() const { return ranges.empty(); }
357 /// beginIndex - Return the lowest numbered slot covered by interval.
358 SlotIndex beginIndex() const {
359 assert(!empty() && "Call to beginIndex() on empty interval.");
360 return ranges.front().start;
363 /// endNumber - return the maximum point of the interval of the whole,
365 SlotIndex endIndex() const {
366 assert(!empty() && "Call to endIndex() on empty interval.");
367 return ranges.back().end;
370 bool expiredAt(SlotIndex index) const {
371 return index >= endIndex();
374 bool liveAt(SlotIndex index) const {
375 const_iterator r = find(index);
376 return r != end() && r->start <= index;
379 /// killedAt - Return true if a live range ends at index. Note that the kill
380 /// point is not contained in the half-open live range. It is usually the
381 /// getDefIndex() slot following its last use.
382 bool killedAt(SlotIndex index) const {
383 const_iterator r = find(index.getUseIndex());
384 return r != end() && r->end == index;
387 /// killedInRange - Return true if the interval has kills in [Start,End).
388 /// Note that the kill point is considered the end of a live range, so it is
389 /// not contained in the live range. If a live range ends at End, it won't
390 /// be counted as a kill by this method.
391 bool killedInRange(SlotIndex Start, SlotIndex End) const;
393 /// getLiveRangeContaining - Return the live range that contains the
394 /// specified index, or null if there is none.
395 const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
396 const_iterator I = FindLiveRangeContaining(Idx);
397 return I == end() ? 0 : &*I;
400 /// getLiveRangeContaining - Return the live range that contains the
401 /// specified index, or null if there is none.
402 LiveRange *getLiveRangeContaining(SlotIndex Idx) {
403 iterator I = FindLiveRangeContaining(Idx);
404 return I == end() ? 0 : &*I;
407 /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
408 VNInfo *getVNInfoAt(SlotIndex Idx) const {
409 const_iterator I = FindLiveRangeContaining(Idx);
410 return I == end() ? 0 : I->valno;
413 /// getVNInfoBefore - Return the VNInfo that is live up to but not
414 /// necessarilly including Idx, or NULL. Use this to find the reaching def
415 /// used by an instruction at this SlotIndex position.
416 VNInfo *getVNInfoBefore(SlotIndex Idx) const {
417 const_iterator I = FindLiveRangeContaining(Idx.getPrevIndex());
418 return I == end() ? 0 : I->valno;
421 /// FindLiveRangeContaining - Return an iterator to the live range that
422 /// contains the specified index, or end() if there is none.
423 iterator FindLiveRangeContaining(SlotIndex Idx) {
424 iterator I = find(Idx);
425 return I != end() && I->start <= Idx ? I : end();
428 const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
429 const_iterator I = find(Idx);
430 return I != end() && I->start <= Idx ? I : end();
433 /// findDefinedVNInfo - Find the by the specified
434 /// index (register interval) or defined
435 VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
438 /// overlaps - Return true if the intersection of the two live intervals is
440 bool overlaps(const LiveInterval& other) const {
443 return overlapsFrom(other, other.begin());
446 /// overlaps - Return true if the live interval overlaps a range specified
448 bool overlaps(SlotIndex Start, SlotIndex End) const;
450 /// overlapsFrom - Return true if the intersection of the two live intervals
451 /// is not empty. The specified iterator is a hint that we can begin
452 /// scanning the Other interval starting at I.
453 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
455 /// addRange - Add the specified LiveRange to this interval, merging
456 /// intervals as appropriate. This returns an iterator to the inserted live
457 /// range (which may have grown since it was inserted.
458 void addRange(LiveRange LR) {
459 addRangeFrom(LR, ranges.begin());
462 /// extendInBlock - If this interval is live before Kill in the basic block
463 /// that starts at StartIdx, extend it to be live up to Kill, and return
464 /// the value. If there is no live range before Kill, return NULL.
465 VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill);
467 /// join - Join two live intervals (this, and other) together. This applies
468 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
469 /// the intervals are not joinable, this aborts.
470 void join(LiveInterval &Other,
471 const int *ValNoAssignments,
472 const int *RHSValNoAssignments,
473 SmallVector<VNInfo*, 16> &NewVNInfo,
474 MachineRegisterInfo *MRI);
476 /// isInOneLiveRange - Return true if the range specified is entirely in the
477 /// a single LiveRange of the live interval.
478 bool isInOneLiveRange(SlotIndex Start, SlotIndex End) const {
479 const_iterator r = find(Start);
480 return r != end() && r->containsRange(Start, End);
483 /// removeRange - Remove the specified range from this interval. Note that
484 /// the range must be a single LiveRange in its entirety.
485 void removeRange(SlotIndex Start, SlotIndex End,
486 bool RemoveDeadValNo = false);
488 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
489 removeRange(LR.start, LR.end, RemoveDeadValNo);
492 /// removeValNo - Remove all the ranges defined by the specified value#.
493 /// Also remove the value# from value# list.
494 void removeValNo(VNInfo *ValNo);
496 /// getSize - Returns the sum of sizes of all the LiveRange's.
498 unsigned getSize() const;
500 /// Returns true if the live interval is zero length, i.e. no live ranges
501 /// span instructions. It doesn't pay to spill such an interval.
502 bool isZeroLength(SlotIndexes *Indexes) const {
503 for (const_iterator i = begin(), e = end(); i != e; ++i)
504 if (Indexes->getNextNonNullIndex(i->start).getBaseIndex() <
505 i->end.getBaseIndex())
510 /// isSpillable - Can this interval be spilled?
511 bool isSpillable() const {
512 return weight != HUGE_VALF;
515 /// markNotSpillable - Mark interval as not spillable
516 void markNotSpillable() {
520 /// ComputeJoinedWeight - Set the weight of a live interval after
521 /// Other has been merged into it.
522 void ComputeJoinedWeight(const LiveInterval &Other);
524 bool operator<(const LiveInterval& other) const {
525 const SlotIndex &thisIndex = beginIndex();
526 const SlotIndex &otherIndex = other.beginIndex();
527 return (thisIndex < otherIndex ||
528 (thisIndex == otherIndex && reg < other.reg));
531 void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
536 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
537 void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
538 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
539 void markValNoForDeletion(VNInfo *V);
541 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
545 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
550 /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
551 /// LiveInterval into equivalence clases of connected components. A
552 /// LiveInterval that has multiple connected components can be broken into
553 /// multiple LiveIntervals.
555 /// Given a LiveInterval that may have multiple connected components, run:
557 /// unsigned numComps = ConEQ.Classify(LI);
558 /// if (numComps > 1) {
559 /// // allocate numComps-1 new LiveIntervals into LIS[1..]
560 /// ConEQ.Distribute(LIS);
563 class ConnectedVNInfoEqClasses {
565 IntEqClasses EqClass;
567 // Note that values a and b are connected.
568 void Connect(unsigned a, unsigned b);
573 explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
575 /// Classify - Classify the values in LI into connected components.
576 /// Return the number of connected components.
577 unsigned Classify(const LiveInterval *LI);
579 /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
580 /// the equivalence class assigned the VNI.
581 unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
583 /// Distribute - Distribute values in LIV[0] into a separate LiveInterval
584 /// for each connected component. LIV must have a LiveInterval for each
585 /// connected component. The LiveIntervals in Liv[1..] must be empty.
586 /// Instructions using LIV[0] are rewritten.
587 void Distribute(LiveInterval *LIV[], MachineRegisterInfo &MRI);