LiveInterval.h revision bae74d9192f04d8185c7b4580565d56cc4ef53f2
1//===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 <iosfwd>
25#include <vector>
26#include <cassert>
27
28namespace llvm {
29  /// LiveRange structure - This represents a simple register range in the
30  /// program, with an inclusive start point and an exclusive end point.
31  /// These ranges are rendered as [start,end).
32  struct LiveRange {
33    unsigned start;  // Start point of the interval (inclusive)
34    unsigned end;    // End point of the interval (exclusive)
35    unsigned ValId;  // identifier for the value contained in this interval.
36
37    LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
38      assert(S < E && "Cannot create empty or backwards range");
39    }
40
41    /// contains - Return true if the index is covered by this range.
42    ///
43    bool contains(unsigned I) const {
44      return start <= I && I < end;
45    }
46
47    bool operator<(const LiveRange &LR) const {
48      return start < LR.start || (start == LR.start && end < LR.end);
49    }
50    bool operator==(const LiveRange &LR) const {
51      return start == LR.start && end == LR.end;
52    }
53
54    void dump() const;
55
56  private:
57    LiveRange(); // DO NOT IMPLEMENT
58  };
59  std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
60
61  inline bool operator<(unsigned V, const LiveRange &LR) {
62    return V < LR.start;
63  }
64
65
66  /// LiveInterval - This class represents some number of live ranges for a
67  /// register or value.  This class also contains a bit of register allocator
68  /// state.
69  struct LiveInterval {
70    typedef std::vector<LiveRange> Ranges;
71    unsigned reg;        // the register of this interval
72    float weight;        // weight of this interval
73    Ranges ranges;       // the ranges in which this register is live
74
75    LiveInterval(unsigned Reg, float Weight)
76      : reg(Reg), weight(Weight), NumValues(0) {
77    }
78
79    typedef Ranges::iterator iterator;
80    iterator begin() { return ranges.begin(); }
81    iterator end()   { return ranges.end(); }
82
83    typedef Ranges::const_iterator const_iterator;
84    const_iterator begin() const { return ranges.begin(); }
85    const_iterator end() const  { return ranges.end(); }
86
87
88    /// advanceTo - Advance the specified iterator to point to the LiveRange
89    /// containing the specified position, or end() if the position is past the
90    /// end of the interval.  If no LiveRange contains this position, but the
91    /// position is in a hole, this method returns an iterator pointing the the
92    /// LiveRange immediately after the hold.
93    iterator advanceTo(iterator I, unsigned Pos) {
94      if (Pos >= endNumber())
95        return end();
96      while (I->end <= Pos) ++I;
97      return I;
98    }
99
100    void swap(LiveInterval& other) {
101      std::swap(reg, other.reg);
102      std::swap(weight, other.weight);
103      ranges.swap(other.ranges);
104      std::swap(NumValues, other.NumValues);
105    }
106
107    bool containsOneValue() const { return NumValues == 1; }
108
109    unsigned getNextValue() {
110      return NumValues++;
111    }
112
113    bool empty() const { return ranges.empty(); }
114
115    /// beginNumber - Return the lowest numbered slot covered by interval.
116    unsigned beginNumber() const {
117      assert(!empty() && "empty interval for register");
118      return ranges.front().start;
119    }
120
121    /// endNumber - return the maximum point of the interval of the whole,
122    /// exclusive.
123    unsigned endNumber() const {
124      assert(!empty() && "empty interval for register");
125      return ranges.back().end;
126    }
127
128    bool expiredAt(unsigned index) const {
129      return index >= endNumber();
130    }
131
132    bool liveAt(unsigned index) const;
133
134    /// getLiveRangeContaining - Return the live range that contains the
135    /// specified index, or null if there is none.
136    const LiveRange *getLiveRangeContaining(unsigned Idx) const;
137
138
139    /// joinable - Two intervals are joinable if the either don't overlap at all
140    /// or if the destination of the copy is a single assignment value, and it
141    /// only overlaps with one value in the source interval.
142    bool joinable(const LiveInterval& other, unsigned CopyIdx) const;
143
144
145    /// overlaps - Return true if the intersection of the two live intervals is
146    /// not empty.
147    bool overlaps(const LiveInterval& other) const {
148      return overlapsFrom(other, other.begin());
149    }
150
151    /// overlapsFrom - Return true if the intersection of the two live intervals
152    /// is not empty.  The specified iterator is a hint that we can begin
153    /// scanning the Other interval starting at I.
154    bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
155
156    /// addRange - Add the specified LiveRange to this interval, merging
157    /// intervals as appropriate.  This returns an iterator to the inserted live
158    /// range (which may have grown since it was inserted.
159    void addRange(LiveRange LR) {
160      addRangeFrom(LR, ranges.begin());
161    }
162
163    /// join - Join two live intervals (this, and other) together.  This
164    /// operation is the result of a copy instruction in the source program,
165    /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'.  This
166    /// destroys 'other'.
167    void join(LiveInterval& other, unsigned CopyIdx);
168
169
170    /// removeRange - Remove the specified range from this interval.  Note that
171    /// the range must already be in this interval in its entirety.
172    void removeRange(unsigned Start, unsigned End);
173
174    bool operator<(const LiveInterval& other) const {
175      return beginNumber() < other.beginNumber();
176    }
177
178    void dump() const;
179
180  private:
181    unsigned NumValues;  // the number of distinct values in this interval.
182    Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
183    void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
184    Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
185    LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
186  };
187
188  std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
189}
190
191#endif
192