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