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.
54 typedef BumpPtrAllocator Allocator;
56 /// The ID number of this value.
59 /// The index of the defining instruction.
62 /// VNInfo constructor.
63 VNInfo(unsigned i, SlotIndex d)
64 : flags(0), id(i), def(d)
67 /// VNInfo construtor, copies values from orig, except for the value number.
68 VNInfo(unsigned i, const VNInfo &orig)
69 : flags(orig.flags), id(i), def(orig.def)
72 /// Copy from the parameter into this VNInfo.
73 void copyFrom(VNInfo &src) {
78 /// Used for copying value number info.
79 unsigned getFlags() const { return flags; }
80 void setFlags(unsigned flags) { this->flags = flags; }
82 /// Merge flags from another VNInfo
83 void mergeFlags(const VNInfo *VNI) {
84 flags = (flags | VNI->flags) & ~IS_UNUSED;
87 /// Returns true if one or more kills are PHI nodes.
88 /// Obsolete, do not use!
89 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
90 /// Set the PHI kill flag on this value.
91 void setHasPHIKill(bool hasKill) {
93 flags |= HAS_PHI_KILL;
95 flags &= ~HAS_PHI_KILL;
98 /// Returns true if this value is re-defined by an early clobber somewhere
99 /// during the live range.
100 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
101 /// Set the "redef by early clobber" flag on this value.
102 void setHasRedefByEC(bool hasRedef) {
104 flags |= REDEF_BY_EC;
106 flags &= ~REDEF_BY_EC;
109 /// Returns true if this value is defined by a PHI instruction (or was,
110 /// PHI instrucions may have been eliminated).
111 bool isPHIDef() const { return flags & IS_PHI_DEF; }
112 /// Set the "phi def" flag on this value.
113 void setIsPHIDef(bool phiDef) {
117 flags &= ~IS_PHI_DEF;
120 /// Returns true if this value is unused.
121 bool isUnused() const { return flags & IS_UNUSED; }
122 /// Set the "is unused" flag on this value.
123 void setIsUnused(bool unused) {
131 /// LiveRange structure - This represents a simple register range in the
132 /// program, with an inclusive start point and an exclusive end point.
133 /// These ranges are rendered as [start,end).
135 SlotIndex start; // Start point of the interval (inclusive)
136 SlotIndex end; // End point of the interval (exclusive)
137 VNInfo *valno; // identifier for the value contained in this interval.
139 LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
140 : start(S), end(E), valno(V) {
142 assert(S < E && "Cannot create empty or backwards range");
145 /// contains - Return true if the index is covered by this range.
147 bool contains(SlotIndex I) const {
148 return start <= I && I < end;
151 /// containsRange - Return true if the given range, [S, E), is covered by
153 bool containsRange(SlotIndex S, SlotIndex E) const {
154 assert((S < E) && "Backwards interval?");
155 return (start <= S && S < end) && (start < E && E <= end);
158 bool operator<(const LiveRange &LR) const {
159 return start < LR.start || (start == LR.start && end < LR.end);
161 bool operator==(const LiveRange &LR) const {
162 return start == LR.start && end == LR.end;
166 void print(raw_ostream &os) const;
169 LiveRange(); // DO NOT IMPLEMENT
172 template <> struct isPodLike<LiveRange> { static const bool value = true; };
174 raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
177 inline bool operator<(SlotIndex V, const LiveRange &LR) {
181 inline bool operator<(const LiveRange &LR, SlotIndex V) {
185 /// LiveInterval - This class represents some number of live ranges for a
186 /// register or value. This class also contains a bit of register allocator
191 typedef SmallVector<LiveRange,4> Ranges;
192 typedef SmallVector<VNInfo*,4> VNInfoList;
194 const unsigned reg; // the register or stack slot of this interval.
195 float weight; // weight of this interval
196 Ranges ranges; // the ranges in which this register is live
197 VNInfoList valnos; // value#'s
210 LiveInterval(unsigned Reg, float Weight)
211 : reg(Reg), weight(Weight) {}
213 typedef Ranges::iterator iterator;
214 iterator begin() { return ranges.begin(); }
215 iterator end() { return ranges.end(); }
217 typedef Ranges::const_iterator const_iterator;
218 const_iterator begin() const { return ranges.begin(); }
219 const_iterator end() const { return ranges.end(); }
221 typedef VNInfoList::iterator vni_iterator;
222 vni_iterator vni_begin() { return valnos.begin(); }
223 vni_iterator vni_end() { return valnos.end(); }
225 typedef VNInfoList::const_iterator const_vni_iterator;
226 const_vni_iterator vni_begin() const { return valnos.begin(); }
227 const_vni_iterator vni_end() const { return valnos.end(); }
229 /// advanceTo - Advance the specified iterator to point to the LiveRange
230 /// containing the specified position, or end() if the position is past the
231 /// end of the interval. If no LiveRange contains this position, but the
232 /// position is in a hole, this method returns an iterator pointing to the
233 /// LiveRange immediately after the hole.
234 iterator advanceTo(iterator I, SlotIndex Pos) {
236 if (Pos >= endIndex())
238 while (I->end <= Pos) ++I;
242 /// find - Return an iterator pointing to the first range that ends after
243 /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
244 /// when searching large intervals.
246 /// If Pos is contained in a LiveRange, that range is returned.
247 /// If Pos is in a hole, the following LiveRange is returned.
248 /// If Pos is beyond endIndex, end() is returned.
249 iterator find(SlotIndex Pos);
251 const_iterator find(SlotIndex Pos) const {
252 return const_cast<LiveInterval*>(this)->find(Pos);
260 bool hasAtLeastOneValue() const { return !valnos.empty(); }
262 bool containsOneValue() const { return valnos.size() == 1; }
264 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
266 /// getValNumInfo - Returns pointer to the specified val#.
268 inline VNInfo *getValNumInfo(unsigned ValNo) {
269 return valnos[ValNo];
271 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
272 return valnos[ValNo];
275 /// containsValue - Returns true if VNI belongs to this interval.
276 bool containsValue(const VNInfo *VNI) const {
277 return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id);
280 /// getNextValue - Create a new value number and return it. MIIdx specifies
281 /// the instruction that defines the value number.
282 VNInfo *getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator) {
284 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def);
285 valnos.push_back(VNI);
289 /// Create a copy of the given value. The new value will be identical except
290 /// for the Value number.
291 VNInfo *createValueCopy(const VNInfo *orig,
292 VNInfo::Allocator &VNInfoAllocator) {
294 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
295 valnos.push_back(VNI);
299 /// RenumberValues - Renumber all values in order of appearance and remove
301 void RenumberValues(LiveIntervals &lis);
303 /// isOnlyLROfValNo - Return true if the specified live range is the only
304 /// one defined by the its val#.
305 bool isOnlyLROfValNo(const LiveRange *LR) {
306 for (const_iterator I = begin(), E = end(); I != E; ++I) {
307 const LiveRange *Tmp = I;
308 if (Tmp != LR && Tmp->valno == LR->valno)
314 /// MergeValueNumberInto - This method is called when two value nubmers
315 /// are found to be equivalent. This eliminates V1, replacing all
316 /// LiveRanges with the V1 value number with the V2 value number. This can
317 /// cause merging of V1/V2 values numbers and compaction of the value space.
318 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
320 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
321 /// in RHS into this live interval as the specified value number.
322 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
323 /// current interval, it will replace the value numbers of the overlaped
324 /// live ranges with the specified value number.
325 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
327 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
328 /// in RHS into this live interval as the specified value number.
329 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
330 /// current interval, but only if the overlapping LiveRanges have the
331 /// specified value number.
332 void MergeValueInAsValue(const LiveInterval &RHS,
333 const VNInfo *RHSValNo, VNInfo *LHSValNo);
335 /// Copy - Copy the specified live interval. This copies all the fields
336 /// except for the register of the interval.
337 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
338 VNInfo::Allocator &VNInfoAllocator);
340 bool empty() const { return ranges.empty(); }
342 /// beginIndex - Return the lowest numbered slot covered by interval.
343 SlotIndex beginIndex() const {
344 assert(!empty() && "Call to beginIndex() on empty interval.");
345 return ranges.front().start;
348 /// endNumber - return the maximum point of the interval of the whole,
350 SlotIndex endIndex() const {
351 assert(!empty() && "Call to endIndex() on empty interval.");
352 return ranges.back().end;
355 bool expiredAt(SlotIndex index) const {
356 return index >= endIndex();
359 bool liveAt(SlotIndex index) const {
360 const_iterator r = find(index);
361 return r != end() && r->start <= index;
364 /// killedAt - Return true if a live range ends at index. Note that the kill
365 /// point is not contained in the half-open live range. It is usually the
366 /// getDefIndex() slot following its last use.
367 bool killedAt(SlotIndex index) const {
368 const_iterator r = find(index.getRegSlot(true));
369 return r != end() && r->end == index;
372 /// killedInRange - Return true if the interval has kills in [Start,End).
373 /// Note that the kill point is considered the end of a live range, so it is
374 /// not contained in the live range. If a live range ends at End, it won't
375 /// be counted as a kill by this method.
376 bool killedInRange(SlotIndex Start, SlotIndex End) const;
378 /// getLiveRangeContaining - Return the live range that contains the
379 /// specified index, or null if there is none.
380 const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
381 const_iterator I = FindLiveRangeContaining(Idx);
382 return I == end() ? 0 : &*I;
385 /// getLiveRangeContaining - Return the live range that contains the
386 /// specified index, or null if there is none.
387 LiveRange *getLiveRangeContaining(SlotIndex Idx) {
388 iterator I = FindLiveRangeContaining(Idx);
389 return I == end() ? 0 : &*I;
392 const LiveRange *getLiveRangeBefore(SlotIndex Idx) const {
393 return getLiveRangeContaining(Idx.getPrevSlot());
396 LiveRange *getLiveRangeBefore(SlotIndex Idx) {
397 return getLiveRangeContaining(Idx.getPrevSlot());
400 /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
401 VNInfo *getVNInfoAt(SlotIndex Idx) const {
402 const_iterator I = FindLiveRangeContaining(Idx);
403 return I == end() ? 0 : I->valno;
406 /// getVNInfoBefore - Return the VNInfo that is live up to but not
407 /// necessarilly including Idx, or NULL. Use this to find the reaching def
408 /// used by an instruction at this SlotIndex position.
409 VNInfo *getVNInfoBefore(SlotIndex Idx) const {
410 const_iterator I = FindLiveRangeContaining(Idx.getPrevSlot());
411 return I == end() ? 0 : I->valno;
414 /// FindLiveRangeContaining - Return an iterator to the live range that
415 /// contains the specified index, or end() if there is none.
416 iterator FindLiveRangeContaining(SlotIndex Idx) {
417 iterator I = find(Idx);
418 return I != end() && I->start <= Idx ? I : end();
421 const_iterator FindLiveRangeContaining(SlotIndex Idx) const {
422 const_iterator I = find(Idx);
423 return I != end() && I->start <= Idx ? I : end();
426 /// findDefinedVNInfo - Find the by the specified
427 /// index (register interval) or defined
428 VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
431 /// overlaps - Return true if the intersection of the two live intervals is
433 bool overlaps(const LiveInterval& other) const {
436 return overlapsFrom(other, other.begin());
439 /// overlaps - Return true if the live interval overlaps a range specified
441 bool overlaps(SlotIndex Start, SlotIndex End) const;
443 /// overlapsFrom - Return true if the intersection of the two live intervals
444 /// is not empty. The specified iterator is a hint that we can begin
445 /// scanning the Other interval starting at I.
446 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
448 /// addRange - Add the specified LiveRange to this interval, merging
449 /// intervals as appropriate. This returns an iterator to the inserted live
450 /// range (which may have grown since it was inserted.
451 void addRange(LiveRange LR) {
452 addRangeFrom(LR, ranges.begin());
455 /// extendInBlock - If this interval is live before Kill in the basic block
456 /// that starts at StartIdx, extend it to be live up to Kill, and return
457 /// the value. If there is no live range before Kill, return NULL.
458 VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill);
460 /// join - Join two live intervals (this, and other) together. This applies
461 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
462 /// the intervals are not joinable, this aborts.
463 void join(LiveInterval &Other,
464 const int *ValNoAssignments,
465 const int *RHSValNoAssignments,
466 SmallVector<VNInfo*, 16> &NewVNInfo,
467 MachineRegisterInfo *MRI);
469 /// isInOneLiveRange - Return true if the range specified is entirely in the
470 /// a single LiveRange of the live interval.
471 bool isInOneLiveRange(SlotIndex Start, SlotIndex End) const {
472 const_iterator r = find(Start);
473 return r != end() && r->containsRange(Start, End);
476 /// removeRange - Remove the specified range from this interval. Note that
477 /// the range must be a single LiveRange in its entirety.
478 void removeRange(SlotIndex Start, SlotIndex End,
479 bool RemoveDeadValNo = false);
481 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
482 removeRange(LR.start, LR.end, RemoveDeadValNo);
485 /// removeValNo - Remove all the ranges defined by the specified value#.
486 /// Also remove the value# from value# list.
487 void removeValNo(VNInfo *ValNo);
489 /// getSize - Returns the sum of sizes of all the LiveRange's.
491 unsigned getSize() const;
493 /// Returns true if the live interval is zero length, i.e. no live ranges
494 /// span instructions. It doesn't pay to spill such an interval.
495 bool isZeroLength(SlotIndexes *Indexes) const {
496 for (const_iterator i = begin(), e = end(); i != e; ++i)
497 if (Indexes->getNextNonNullIndex(i->start).getBaseIndex() <
498 i->end.getBaseIndex())
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) {
543 /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
544 /// LiveInterval into equivalence clases of connected components. A
545 /// LiveInterval that has multiple connected components can be broken into
546 /// multiple LiveIntervals.
548 /// Given a LiveInterval that may have multiple connected components, run:
550 /// unsigned numComps = ConEQ.Classify(LI);
551 /// if (numComps > 1) {
552 /// // allocate numComps-1 new LiveIntervals into LIS[1..]
553 /// ConEQ.Distribute(LIS);
556 class ConnectedVNInfoEqClasses {
558 IntEqClasses EqClass;
560 // Note that values a and b are connected.
561 void Connect(unsigned a, unsigned b);
566 explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
568 /// Classify - Classify the values in LI into connected components.
569 /// Return the number of connected components.
570 unsigned Classify(const LiveInterval *LI);
572 /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
573 /// the equivalence class assigned the VNI.
574 unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
576 /// Distribute - Distribute values in LIV[0] into a separate LiveInterval
577 /// for each connected component. LIV must have a LiveInterval for each
578 /// connected component. The LiveIntervals in Liv[1..] must be empty.
579 /// Instructions using LIV[0] are rewritten.
580 void Distribute(LiveInterval *LIV[], MachineRegisterInfo &MRI);