LLVM API Documentation
00001 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the LiveRange and LiveInterval classes. Given some 00011 // numbering of each the machine instructions an interval [i, j) is said to be a 00012 // live interval for register v if there is no instruction with number j' > j 00013 // such that v is live at j' and there is no instruction with number i' < i such 00014 // that v is live at i'. In this implementation intervals can have holes, 00015 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each 00016 // individual range is represented as an instance of LiveRange, and the whole 00017 // interval is represented as an instance of LiveInterval. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H 00022 #define LLVM_CODEGEN_LIVEINTERVAL_H 00023 00024 #include <iosfwd> 00025 #include <vector> 00026 #include <cassert> 00027 00028 namespace llvm { 00029 class MRegisterInfo; 00030 00031 /// LiveRange structure - This represents a simple register range in the 00032 /// program, with an inclusive start point and an exclusive end point. 00033 /// These ranges are rendered as [start,end). 00034 struct LiveRange { 00035 unsigned start; // Start point of the interval (inclusive) 00036 unsigned end; // End point of the interval (exclusive) 00037 unsigned ValId; // identifier for the value contained in this interval. 00038 00039 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) { 00040 assert(S < E && "Cannot create empty or backwards range"); 00041 } 00042 00043 /// contains - Return true if the index is covered by this range. 00044 /// 00045 bool contains(unsigned I) const { 00046 return start <= I && I < end; 00047 } 00048 00049 bool operator<(const LiveRange &LR) const { 00050 return start < LR.start || (start == LR.start && end < LR.end); 00051 } 00052 bool operator==(const LiveRange &LR) const { 00053 return start == LR.start && end == LR.end; 00054 } 00055 00056 void dump() const; 00057 00058 private: 00059 LiveRange(); // DO NOT IMPLEMENT 00060 }; 00061 std::ostream& operator<<(std::ostream& os, const LiveRange &LR); 00062 00063 inline bool operator<(unsigned V, const LiveRange &LR) { 00064 return V < LR.start; 00065 } 00066 00067 inline bool operator<(const LiveRange &LR, unsigned V) { 00068 return LR.start < V; 00069 } 00070 00071 /// LiveInterval - This class represents some number of live ranges for a 00072 /// register or value. This class also contains a bit of register allocator 00073 /// state. 00074 struct LiveInterval { 00075 typedef std::vector<LiveRange> Ranges; 00076 unsigned reg; // the register of this interval 00077 float weight; // weight of this interval 00078 Ranges ranges; // the ranges in which this register is live 00079 00080 LiveInterval(unsigned Reg, float Weight) 00081 : reg(Reg), weight(Weight), NumValues(0) { 00082 } 00083 00084 typedef Ranges::iterator iterator; 00085 iterator begin() { return ranges.begin(); } 00086 iterator end() { return ranges.end(); } 00087 00088 typedef Ranges::const_iterator const_iterator; 00089 const_iterator begin() const { return ranges.begin(); } 00090 const_iterator end() const { return ranges.end(); } 00091 00092 00093 /// advanceTo - Advance the specified iterator to point to the LiveRange 00094 /// containing the specified position, or end() if the position is past the 00095 /// end of the interval. If no LiveRange contains this position, but the 00096 /// position is in a hole, this method returns an iterator pointing the the 00097 /// LiveRange immediately after the hole. 00098 iterator advanceTo(iterator I, unsigned Pos) { 00099 if (Pos >= endNumber()) 00100 return end(); 00101 while (I->end <= Pos) ++I; 00102 return I; 00103 } 00104 00105 void swap(LiveInterval& other) { 00106 std::swap(reg, other.reg); 00107 std::swap(weight, other.weight); 00108 ranges.swap(other.ranges); 00109 std::swap(NumValues, other.NumValues); 00110 } 00111 00112 bool containsOneValue() const { return NumValues == 1; } 00113 00114 unsigned getNextValue() { 00115 return NumValues++; 00116 } 00117 00118 bool empty() const { return ranges.empty(); } 00119 00120 /// beginNumber - Return the lowest numbered slot covered by interval. 00121 unsigned beginNumber() const { 00122 assert(!empty() && "empty interval for register"); 00123 return ranges.front().start; 00124 } 00125 00126 /// endNumber - return the maximum point of the interval of the whole, 00127 /// exclusive. 00128 unsigned endNumber() const { 00129 assert(!empty() && "empty interval for register"); 00130 return ranges.back().end; 00131 } 00132 00133 bool expiredAt(unsigned index) const { 00134 return index >= endNumber(); 00135 } 00136 00137 bool liveAt(unsigned index) const; 00138 00139 /// getLiveRangeContaining - Return the live range that contains the 00140 /// specified index, or null if there is none. 00141 const LiveRange *getLiveRangeContaining(unsigned Idx) const; 00142 00143 00144 /// joinable - Two intervals are joinable if the either don't overlap at all 00145 /// or if the destination of the copy is a single assignment value, and it 00146 /// only overlaps with one value in the source interval. 00147 bool joinable(const LiveInterval& other, unsigned CopyIdx) const; 00148 00149 /// getOverlapingRanges - Given another live interval which is defined as a 00150 /// copy from this one, return a list of all of the live ranges where the 00151 /// two overlap and have different value numbers. 00152 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx, 00153 std::vector<LiveRange*> &Ranges); 00154 00155 /// overlaps - Return true if the intersection of the two live intervals is 00156 /// not empty. 00157 bool overlaps(const LiveInterval& other) const { 00158 return overlapsFrom(other, other.begin()); 00159 } 00160 00161 /// overlapsFrom - Return true if the intersection of the two live intervals 00162 /// is not empty. The specified iterator is a hint that we can begin 00163 /// scanning the Other interval starting at I. 00164 bool overlapsFrom(const LiveInterval& other, const_iterator I) const; 00165 00166 /// addRange - Add the specified LiveRange to this interval, merging 00167 /// intervals as appropriate. This returns an iterator to the inserted live 00168 /// range (which may have grown since it was inserted. 00169 void addRange(LiveRange LR) { 00170 addRangeFrom(LR, ranges.begin()); 00171 } 00172 00173 /// join - Join two live intervals (this, and other) together. This 00174 /// operation is the result of a copy instruction in the source program, 00175 /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'. This 00176 /// destroys 'other'. 00177 void join(LiveInterval& other, unsigned CopyIdx); 00178 00179 00180 /// removeRange - Remove the specified range from this interval. Note that 00181 /// the range must already be in this interval in its entirety. 00182 void removeRange(unsigned Start, unsigned End); 00183 00184 bool operator<(const LiveInterval& other) const { 00185 return beginNumber() < other.beginNumber(); 00186 } 00187 00188 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const; 00189 void dump() const; 00190 00191 private: 00192 unsigned NumValues; // the number of distinct values in this interval. 00193 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From); 00194 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd); 00195 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr); 00196 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT 00197 }; 00198 00199 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) { 00200 LI.print(OS); 00201 return OS; 00202 } 00203 } 00204 00205 #endif