LiveInterval.h revision f7da2c7b0c6293c268881628fc351bed7763f1f4
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 "llvm/ADT/SmallVector.h" 25#include <iosfwd> 26#include <vector> 27#include <cassert> 28 29namespace llvm { 30 class MRegisterInfo; 31 32 /// LiveRange structure - This represents a simple register range in the 33 /// program, with an inclusive start point and an exclusive end point. 34 /// These ranges are rendered as [start,end). 35 struct LiveRange { 36 unsigned start; // Start point of the interval (inclusive) 37 unsigned end; // End point of the interval (exclusive) 38 unsigned ValId; // identifier for the value contained in this interval. 39 40 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) { 41 assert(S < E && "Cannot create empty or backwards range"); 42 } 43 44 /// contains - Return true if the index is covered by this range. 45 /// 46 bool contains(unsigned I) const { 47 return start <= I && I < end; 48 } 49 50 bool operator<(const LiveRange &LR) const { 51 return start < LR.start || (start == LR.start && end < LR.end); 52 } 53 bool operator==(const LiveRange &LR) const { 54 return start == LR.start && end == LR.end; 55 } 56 57 void dump() const; 58 59 private: 60 LiveRange(); // DO NOT IMPLEMENT 61 }; 62 std::ostream& operator<<(std::ostream& os, const LiveRange &LR); 63 64 inline bool operator<(unsigned V, const LiveRange &LR) { 65 return V < LR.start; 66 } 67 68 inline bool operator<(const LiveRange &LR, unsigned V) { 69 return LR.start < V; 70 } 71 72 /// LiveInterval - This class represents some number of live ranges for a 73 /// register or value. This class also contains a bit of register allocator 74 /// state. 75 struct LiveInterval { 76 typedef SmallVector<LiveRange,4> Ranges; 77 unsigned reg; // the register of this interval 78 float weight; // weight of this interval 79 Ranges ranges; // the ranges in which this register is live 80 private: 81 unsigned NumValues; // the number of distinct values in this interval. 82 83 /// InstDefiningValue - This tracks the def index of the instruction that 84 /// defines a particular value number in the interval. This may be ~0, 85 /// which is treated as unknown, or ~1, which is a deleted value number. 86 SmallVector<unsigned, 4> InstDefiningValue; 87 public: 88 89 LiveInterval(unsigned Reg, float Weight) 90 : reg(Reg), weight(Weight), NumValues(0) { 91 } 92 93 typedef Ranges::iterator iterator; 94 iterator begin() { return ranges.begin(); } 95 iterator end() { return ranges.end(); } 96 97 typedef Ranges::const_iterator const_iterator; 98 const_iterator begin() const { return ranges.begin(); } 99 const_iterator end() const { return ranges.end(); } 100 101 102 /// advanceTo - Advance the specified iterator to point to the LiveRange 103 /// containing the specified position, or end() if the position is past the 104 /// end of the interval. If no LiveRange contains this position, but the 105 /// position is in a hole, this method returns an iterator pointing the the 106 /// LiveRange immediately after the hole. 107 iterator advanceTo(iterator I, unsigned Pos) { 108 if (Pos >= endNumber()) 109 return end(); 110 while (I->end <= Pos) ++I; 111 return I; 112 } 113 114 void swap(LiveInterval& other) { 115 std::swap(reg, other.reg); 116 std::swap(weight, other.weight); 117 std::swap(ranges, other.ranges); 118 std::swap(NumValues, other.NumValues); 119 std::swap(InstDefiningValue, other.InstDefiningValue); 120 } 121 122 bool containsOneValue() const { return NumValues == 1; } 123 124 unsigned getNumValNums() const { return NumValues; } 125 126 /// getNextValue - Create a new value number and return it. MIIdx specifies 127 /// the instruction that defines the value number. 128 unsigned getNextValue(unsigned MIIdx) { 129 InstDefiningValue.push_back(MIIdx); 130 return NumValues++; 131 } 132 133 /// getInstForValNum - Return the machine instruction index that defines the 134 /// specified value number. 135 unsigned getInstForValNum(unsigned ValNo) const { 136 return InstDefiningValue[ValNo]; 137 } 138 139 /// setInstDefiningValNum - Change the instruction defining the specified 140 /// value number to the specified instruction. 141 void setInstDefiningValNum(unsigned ValNo, unsigned MIIdx) { 142 InstDefiningValue[ValNo] = MIIdx; 143 } 144 145 /// MergeValueNumberInto - This method is called when two value nubmers 146 /// are found to be equivalent. This eliminates V1, replacing all 147 /// LiveRanges with the V1 value number with the V2 value number. This can 148 /// cause merging of V1/V2 values numbers and compaction of the value space. 149 void MergeValueNumberInto(unsigned V1, unsigned V2); 150 151 152 bool empty() const { return ranges.empty(); } 153 154 /// beginNumber - Return the lowest numbered slot covered by interval. 155 unsigned beginNumber() const { 156 assert(!empty() && "empty interval for register"); 157 return ranges.front().start; 158 } 159 160 /// endNumber - return the maximum point of the interval of the whole, 161 /// exclusive. 162 unsigned endNumber() const { 163 assert(!empty() && "empty interval for register"); 164 return ranges.back().end; 165 } 166 167 bool expiredAt(unsigned index) const { 168 return index >= endNumber(); 169 } 170 171 bool liveAt(unsigned index) const; 172 173 /// getLiveRangeContaining - Return the live range that contains the 174 /// specified index, or null if there is none. 175 const LiveRange *getLiveRangeContaining(unsigned Idx) const { 176 const_iterator I = FindLiveRangeContaining(Idx); 177 return I == end() ? 0 : &*I; 178 } 179 180 /// FindLiveRangeContaining - Return an iterator to the live range that 181 /// contains the specified index, or end() if there is none. 182 const_iterator FindLiveRangeContaining(unsigned Idx) const; 183 184 /// FindLiveRangeContaining - Return an iterator to the live range that 185 /// contains the specified index, or end() if there is none. 186 iterator FindLiveRangeContaining(unsigned Idx); 187 188 /// joinable - Two intervals are joinable if the either don't overlap at all 189 /// or if the destination of the copy is a single assignment value, and it 190 /// only overlaps with one value in the source interval. 191 bool joinable(const LiveInterval& other, unsigned CopyIdx) const; 192 193 /// getOverlapingRanges - Given another live interval which is defined as a 194 /// copy from this one, return a list of all of the live ranges where the 195 /// two overlap and have different value numbers. 196 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx, 197 std::vector<LiveRange*> &Ranges); 198 199 /// overlaps - Return true if the intersection of the two live intervals is 200 /// not empty. 201 bool overlaps(const LiveInterval& other) const { 202 return overlapsFrom(other, other.begin()); 203 } 204 205 /// overlapsFrom - Return true if the intersection of the two live intervals 206 /// is not empty. The specified iterator is a hint that we can begin 207 /// scanning the Other interval starting at I. 208 bool overlapsFrom(const LiveInterval& other, const_iterator I) const; 209 210 /// addRange - Add the specified LiveRange to this interval, merging 211 /// intervals as appropriate. This returns an iterator to the inserted live 212 /// range (which may have grown since it was inserted. 213 void addRange(LiveRange LR) { 214 addRangeFrom(LR, ranges.begin()); 215 } 216 217 /// join - Join two live intervals (this, and other) together. This 218 /// operation is the result of a copy instruction in the source program, 219 /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'. This 220 /// destroys 'other'. 221 void join(LiveInterval& other, unsigned CopyIdx); 222 223 224 /// removeRange - Remove the specified range from this interval. Note that 225 /// the range must already be in this interval in its entirety. 226 void removeRange(unsigned Start, unsigned End); 227 228 bool operator<(const LiveInterval& other) const { 229 return beginNumber() < other.beginNumber(); 230 } 231 232 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const; 233 void dump() const; 234 235 private: 236 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From); 237 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd); 238 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr); 239 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT 240 }; 241 242 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) { 243 LI.print(OS); 244 return OS; 245 } 246} 247 248#endif 249