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 { |
| 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) |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 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) { |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 38 | assert(S < E && "Cannot create empty or backwards range"); |
| 39 | } |
| 40 | |
Chris Lattner | e6ad392 | 2004-07-23 18:39:12 +0000 | [diff] [blame] | 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 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 53 | |
| 54 | void dump() const; |
| 55 | |
Chris Lattner | e6ad392 | 2004-07-23 18:39:12 +0000 | [diff] [blame] | 56 | private: |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 57 | LiveRange(); // DO NOT IMPLEMENT |
| 58 | }; |
| 59 | std::ostream& operator<<(std::ostream& os, const LiveRange &LR); |
| 60 | |
Chris Lattner | ebd7e6c | 2004-07-23 18:13:24 +0000 | [diff] [blame] | 61 | inline bool operator<(unsigned V, const LiveRange &LR) { |
| 62 | return V < LR.start; |
| 63 | } |
| 64 | |
| 65 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 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 |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 74 | |
| 75 | LiveInterval(unsigned Reg, float Weight) |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 76 | : reg(Reg), weight(Weight), NumValues(0) { |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 79 | |
| 80 | typedef Ranges::iterator iterator; |
| 81 | iterator begin() { return ranges.begin(); } |
| 82 | iterator end() { return ranges.end(); } |
| 83 | |
Alkis Evlogimenos | a1613db | 2004-07-24 11:44:15 +0000 | [diff] [blame] | 84 | void swap(LiveInterval& other) { |
| 85 | std::swap(reg, other.reg); |
| 86 | std::swap(weight, other.weight); |
| 87 | ranges.swap(other.ranges); |
| 88 | std::swap(NumValues, other.NumValues); |
| 89 | } |
| 90 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 91 | bool containsOneValue() const { return NumValues == 1; } |
| 92 | |
| 93 | unsigned getNextValue() { |
| 94 | return NumValues++; |
| 95 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 96 | |
| 97 | bool empty() const { return ranges.empty(); } |
| 98 | |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 99 | /// beginNumber - Return the lowest numbered slot covered by interval. |
| 100 | unsigned beginNumber() const { |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 101 | assert(!empty() && "empty interval for register"); |
| 102 | return ranges.front().start; |
| 103 | } |
| 104 | |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 105 | /// endNumber - return the maximum point of the interval of the whole, |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 106 | /// exclusive. |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 107 | unsigned endNumber() const { |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 108 | assert(!empty() && "empty interval for register"); |
| 109 | return ranges.back().end; |
| 110 | } |
| 111 | |
| 112 | bool expiredAt(unsigned index) const { |
Chris Lattner | 8d8d513 | 2004-11-18 01:34:44 +0000 | [diff] [blame^] | 113 | return index >= endNumber(); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool liveAt(unsigned index) const; |
| 117 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 118 | /// getLiveRangeContaining - Return the live range that contains the |
| 119 | /// specified index, or null if there is none. |
Chris Lattner | d3a205e | 2004-07-25 06:23:01 +0000 | [diff] [blame] | 120 | const LiveRange *getLiveRangeContaining(unsigned Idx) const; |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | /// joinable - Two intervals are joinable if the either don't overlap at all |
| 124 | /// or if the destination of the copy is a single assignment value, and it |
| 125 | /// only overlaps with one value in the source interval. |
| 126 | bool joinable(const LiveInterval& other, unsigned CopyIdx) const; |
| 127 | |
| 128 | |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 129 | bool overlaps(const LiveInterval& other) const; |
| 130 | |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 131 | /// addRange - Add the specified LiveRange to this interval, merging |
| 132 | /// intervals as appropriate. This returns an iterator to the inserted live |
| 133 | /// range (which may have grown since it was inserted. |
| 134 | void addRange(LiveRange LR) { |
| 135 | addRangeFrom(LR, ranges.begin()); |
| 136 | } |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 137 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 138 | /// join - Join two live intervals (this, and other) together. This |
| 139 | /// operation is the result of a copy instruction in the source program, |
| 140 | /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'. This |
| 141 | /// destroys 'other'. |
| 142 | void join(LiveInterval& other, unsigned CopyIdx); |
| 143 | |
| 144 | |
| 145 | /// removeRange - Remove the specified range from this interval. Note that |
| 146 | /// the range must already be in this interval in its entirety. |
| 147 | void removeRange(unsigned Start, unsigned End); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 148 | |
| 149 | bool operator<(const LiveInterval& other) const { |
Chris Lattner | 23b71c1 | 2004-11-18 01:29:39 +0000 | [diff] [blame] | 150 | return beginNumber() < other.beginNumber(); |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 153 | void dump() const; |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 154 | |
| 155 | private: |
Chris Lattner | abf295f | 2004-07-24 02:52:23 +0000 | [diff] [blame] | 156 | unsigned NumValues; // the number of distinct values in this interval. |
Chris Lattner | b26c215 | 2004-07-23 19:38:44 +0000 | [diff] [blame] | 157 | Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From); |
| 158 | void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd); |
| 159 | Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr); |
Alkis Evlogimenos | 3f86193 | 2004-07-24 18:55:15 +0000 | [diff] [blame] | 160 | LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT |
Chris Lattner | fb449b9 | 2004-07-23 17:49:16 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | std::ostream& operator<<(std::ostream& os, const LiveInterval& li); |
| 164 | } |
| 165 | |
| 166 | #endif |