LiveInterval.h revision ffd1326ff8dfc652a8026c3faebf55bbba7c32c7
1//===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22#define LLVM_CODEGEN_LIVEINTERVAL_H
23
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/Allocator.h"
26#include <iosfwd>
27#include <cassert>
28#include <climits>
29
30namespace llvm {
31  class MachineInstr;
32  class MachineRegisterInfo;
33  class TargetRegisterInfo;
34  struct LiveInterval;
35
36  /// VNInfo - Value Number Information.
37  /// This class holds information about a machine level values, including
38  /// definition and use points.
39  ///
40  /// Care must be taken in interpreting the def index of the value. The
41  /// following rules apply:
42  ///
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.
51
52  class VNInfo {
53  private:
54    enum {
55      HAS_PHI_KILL    = 1,
56      REDEF_BY_EC     = 1 << 1,
57      IS_PHI_DEF      = 1 << 2,
58      IS_UNUSED       = 1 << 3,
59      IS_DEF_ACCURATE = 1 << 4
60    };
61
62    unsigned char flags;
63
64  public:
65    /// Holds information about individual kills.
66    struct KillInfo {
67      bool isPHIKill : 1;
68      unsigned killIdx : 31;
69
70      KillInfo(bool isPHIKill, unsigned killIdx)
71        : isPHIKill(isPHIKill), killIdx(killIdx) {
72
73        assert(killIdx != 0 && "Zero kill indices are no longer permitted.");
74      }
75
76    };
77
78    typedef SmallVector<KillInfo, 4> KillSet;
79
80    /// The ID number of this value.
81    unsigned id;
82
83    /// The index of the defining instruction (if isDefAccurate() returns true).
84    unsigned def;
85    MachineInstr *copy;
86    KillSet kills;
87
88    VNInfo()
89      : flags(IS_UNUSED), id(~1U), def(0), copy(0) {}
90
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) {}
96
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),
100        kills(orig.kills) {}
101
102    /// Used for copying value number info.
103    unsigned getFlags() const { return flags; }
104    void setFlags(unsigned flags) { this->flags = flags; }
105
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) {
109      if (hasKill)
110        flags |= HAS_PHI_KILL;
111      else
112        flags &= ~HAS_PHI_KILL;
113    }
114
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) {
119      if (hasRedef)
120        flags |= REDEF_BY_EC;
121      else
122        flags &= ~REDEF_BY_EC;
123    }
124
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) {
129      if (phiDef)
130        flags |= IS_PHI_DEF;
131      else
132        flags &= ~IS_PHI_DEF;
133    }
134
135    /// Returns true if this value is unused.
136    bool isUnused() const { return flags & IS_UNUSED; }
137    void setIsUnused(bool unused) {
138      if (unused)
139        flags |= IS_UNUSED;
140      else
141        flags &= ~IS_UNUSED;
142    }
143
144    /// Returns true if the def is accurate.
145    bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
146    void setIsDefAccurate(bool defAccurate) {
147      if (defAccurate)
148        flags |= IS_DEF_ACCURATE;
149      else
150        flags &= ~IS_DEF_ACCURATE;
151    }
152
153  };
154
155  inline bool operator<(const VNInfo::KillInfo &k1, const VNInfo::KillInfo &k2) {
156    return k1.killIdx < k2.killIdx;
157  }
158
159  inline bool operator<(const VNInfo::KillInfo &k, unsigned idx) {
160    return k.killIdx < idx;
161  }
162
163  inline bool operator<(unsigned idx, const VNInfo::KillInfo &k) {
164    return idx < k.killIdx;
165  }
166
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).
170  struct LiveRange {
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.
174
175    LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
176      assert(S < E && "Cannot create empty or backwards range");
177    }
178
179    /// contains - Return true if the index is covered by this range.
180    ///
181    bool contains(unsigned I) const {
182      return start <= I && I < end;
183    }
184
185    bool operator<(const LiveRange &LR) const {
186      return start < LR.start || (start == LR.start && end < LR.end);
187    }
188    bool operator==(const LiveRange &LR) const {
189      return start == LR.start && end == LR.end;
190    }
191
192    void dump() const;
193    void print(std::ostream &os) const;
194    void print(std::ostream *os) const { if (os) print(*os); }
195
196  private:
197    LiveRange(); // DO NOT IMPLEMENT
198  };
199
200  std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
201
202
203  inline bool operator<(unsigned V, const LiveRange &LR) {
204    return V < LR.start;
205  }
206
207  inline bool operator<(const LiveRange &LR, unsigned V) {
208    return LR.start < V;
209  }
210
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
213  /// state.
214  struct LiveInterval {
215    typedef SmallVector<LiveRange,4> Ranges;
216    typedef SmallVector<VNInfo*,4> VNInfoList;
217
218    unsigned reg;        // the register or stack slot of this interval
219                         // if the top bits is set, it represents a stack slot.
220    float weight;        // weight of this interval
221    Ranges ranges;       // the ranges in which this register is live
222    VNInfoList valnos;   // value#'s
223
224  public:
225
226    struct InstrSlots {
227      enum {
228        LOAD  = 0,
229        USE   = 1,
230        DEF   = 2,
231        STORE = 3,
232        NUM   = 4
233      };
234
235      static unsigned scale(unsigned slot, unsigned factor) {
236        unsigned index = slot / NUM,
237                 offset = slot % NUM;
238        assert(index <= ~0U / (factor * NUM) &&
239               "Rescaled interval would overflow");
240        return index * NUM * factor + offset;
241      }
242
243    };
244
245    LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
246      : reg(Reg), weight(Weight) {
247      if (IsSS)
248        reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
249    }
250
251    typedef Ranges::iterator iterator;
252    iterator begin() { return ranges.begin(); }
253    iterator end()   { return ranges.end(); }
254
255    typedef Ranges::const_iterator const_iterator;
256    const_iterator begin() const { return ranges.begin(); }
257    const_iterator end() const  { return ranges.end(); }
258
259    typedef VNInfoList::iterator vni_iterator;
260    vni_iterator vni_begin() { return valnos.begin(); }
261    vni_iterator vni_end() { return valnos.end(); }
262
263    typedef VNInfoList::const_iterator const_vni_iterator;
264    const_vni_iterator vni_begin() const { return valnos.begin(); }
265    const_vni_iterator vni_end() const { return valnos.end(); }
266
267    /// advanceTo - Advance the specified iterator to point to the LiveRange
268    /// containing the specified position, or end() if the position is past the
269    /// end of the interval.  If no LiveRange contains this position, but the
270    /// position is in a hole, this method returns an iterator pointing the the
271    /// LiveRange immediately after the hole.
272    iterator advanceTo(iterator I, unsigned Pos) {
273      if (Pos >= endNumber())
274        return end();
275      while (I->end <= Pos) ++I;
276      return I;
277    }
278
279    void clear() {
280      while (!valnos.empty()) {
281        VNInfo *VNI = valnos.back();
282        valnos.pop_back();
283        VNI->~VNInfo();
284      }
285
286      ranges.clear();
287    }
288
289    /// isStackSlot - Return true if this is a stack slot interval.
290    ///
291    bool isStackSlot() const {
292      return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
293    }
294
295    /// getStackSlotIndex - Return stack slot index if this is a stack slot
296    /// interval.
297    int getStackSlotIndex() const {
298      assert(isStackSlot() && "Interval is not a stack slot interval!");
299      return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
300    }
301
302    bool hasAtLeastOneValue() const { return !valnos.empty(); }
303
304    bool containsOneValue() const { return valnos.size() == 1; }
305
306    unsigned getNumValNums() const { return (unsigned)valnos.size(); }
307
308    /// getValNumInfo - Returns pointer to the specified val#.
309    ///
310    inline VNInfo *getValNumInfo(unsigned ValNo) {
311      return valnos[ValNo];
312    }
313    inline const VNInfo *getValNumInfo(unsigned ValNo) const {
314      return valnos[ValNo];
315    }
316
317    /// copyValNumInfo - Copy the value number info for one value number to
318    /// another.
319    void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
320      DstValNo->def = SrcValNo->def;
321      DstValNo->copy = SrcValNo->copy;
322      DstValNo->setFlags(SrcValNo->getFlags());
323      DstValNo->kills = SrcValNo->kills;
324    }
325
326    /// getNextValue - Create a new value number and return it.  MIIdx specifies
327    /// the instruction that defines the value number.
328    VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
329                         bool isDefAccurate, BumpPtrAllocator &VNInfoAllocator) {
330
331      assert(MIIdx != ~0u && MIIdx != ~1u &&
332             "PHI def / unused flags should now be passed explicitly.");
333#ifdef __GNUC__
334      unsigned Alignment = (unsigned)__alignof__(VNInfo);
335#else
336      // FIXME: ugly.
337      unsigned Alignment = 8;
338#endif
339      VNInfo *VNI =
340        static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
341                                                      Alignment));
342      new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
343      VNI->setIsDefAccurate(isDefAccurate);
344      valnos.push_back(VNI);
345      return VNI;
346    }
347
348    /// Create a copy of the given value. The new value will be identical except
349    /// for the Value number.
350    VNInfo *createValueCopy(const VNInfo *orig, BumpPtrAllocator &VNInfoAllocator) {
351
352#ifdef __GNUC__
353      unsigned Alignment = (unsigned)__alignof__(VNInfo);
354#else
355      // FIXME: ugly.
356      unsigned Alignment = 8;
357#endif
358      VNInfo *VNI =
359        static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
360                                                      Alignment));
361
362      new (VNI) VNInfo((unsigned)valnos.size(), *orig);
363      valnos.push_back(VNI);
364      return VNI;
365    }
366
367    /// addKill - Add a kill instruction index to the specified value
368    /// number.
369    static void addKill(VNInfo *VNI, unsigned KillIdx, bool phiKill) {
370      VNInfo::KillSet &kills = VNI->kills;
371      VNInfo::KillInfo newKill(phiKill, KillIdx);
372      if (kills.empty()) {
373        kills.push_back(newKill);
374      } else {
375        VNInfo::KillSet::iterator
376          I = std::lower_bound(kills.begin(), kills.end(), newKill);
377        kills.insert(I, newKill);
378      }
379    }
380
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());
385           i != e; ++i) {
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);
391        }
392      }
393    }
394
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) {
402        kills.erase(I);
403        return true;
404      }
405      return false;
406    }
407
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;
412
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);
417      kills.erase(I, E);
418    }
419
420    /// isKill - Return true if the specified index is a kill of the
421    /// specified val#.
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;
427    }
428
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)
435          return false;
436      }
437      return true;
438    }
439
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);
445
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);
452
453    /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a
454    /// single LiveRange only.
455    void MergeInClobberRange(unsigned Start, unsigned End,
456                             BumpPtrAllocator &VNInfoAllocator);
457
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);
464
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);
472
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);
477
478    bool empty() const { return ranges.empty(); }
479
480    /// beginNumber - Return the lowest numbered slot covered by interval.
481    unsigned beginNumber() const {
482      if (empty())
483        return 0;
484      return ranges.front().start;
485    }
486
487    /// endNumber - return the maximum point of the interval of the whole,
488    /// exclusive.
489    unsigned endNumber() const {
490      if (empty())
491        return 0;
492      return ranges.back().end;
493    }
494
495    bool expiredAt(unsigned index) const {
496      return index >= endNumber();
497    }
498
499    bool liveAt(unsigned index) const;
500
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;
505
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;
511    }
512
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;
518    }
519
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;
523
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);
527
528    /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
529    /// index (register interval) or defined by the specified register (stack
530    /// inteval).
531    VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
532
533    /// overlaps - Return true if the intersection of the two live intervals is
534    /// not empty.
535    bool overlaps(const LiveInterval& other) const {
536      return overlapsFrom(other, other.begin());
537    }
538
539    /// overlaps - Return true if the live interval overlaps a range specified
540    /// by [Start, End).
541    bool overlaps(unsigned Start, unsigned End) const;
542
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;
547
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());
553    }
554
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);
562
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);
566
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);
570
571    void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
572      removeRange(LR.start, LR.end, RemoveDeadValNo);
573    }
574
575    /// removeValNo - Remove all the ranges defined by the specified value#.
576    /// Also remove the value# from value# list.
577    void removeValNo(VNInfo *ValNo);
578
579    /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
580    /// instructions.
581    void scaleNumbering(unsigned factor);
582
583    /// getSize - Returns the sum of sizes of all the LiveRange's.
584    ///
585    unsigned getSize() const;
586
587    bool operator<(const LiveInterval& other) const {
588      return beginNumber() < other.beginNumber();
589    }
590
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);
594    }
595    void dump() const;
596
597  private:
598    Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
599    void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
600    Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
601    LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
602  };
603
604  inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {
605    LI.print(OS);
606    return OS;
607  }
608}
609
610#endif
611