LiveInterval.h revision de551f91d8816632a76a065084caab9fab6aacff
1//===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "llvm/Support/Allocator.h" 26#include <iosfwd> 27#include <cassert> 28#include <climits> 29 30namespace llvm { 31 class MachineInstr; 32 class TargetRegisterInfo; 33 struct LiveInterval; 34 35 /// VNInfo - If the value number definition is undefined (e.g. phi 36 /// merge point), it contains ~0u,x. If the value number is not in use, it 37 /// contains ~1u,x to indicate that the value # is not used. 38 /// def - Instruction # of the definition. 39 /// - or reg # of the definition if it's a stack slot liveinterval. 40 /// copy - Copy iff val# is defined by a copy; zero otherwise. 41 /// hasPHIKill - One or more of the kills are PHI nodes. 42 /// redefByEC - Re-defined by early clobber somewhere during the live range. 43 /// kills - Instruction # of the kills. 44 struct VNInfo { 45 unsigned id; 46 unsigned def; 47 MachineInstr *copy; 48 bool hasPHIKill : 1; 49 bool redefByEC : 1; 50 SmallVector<unsigned, 4> kills; 51 VNInfo() 52 : id(~1U), def(~1U), copy(0), hasPHIKill(false), redefByEC(false) {} 53 VNInfo(unsigned i, unsigned d, MachineInstr *c) 54 : id(i), def(d), copy(c), hasPHIKill(false), redefByEC(false) {} 55 }; 56 57 /// LiveRange structure - This represents a simple register range in the 58 /// program, with an inclusive start point and an exclusive end point. 59 /// These ranges are rendered as [start,end). 60 struct LiveRange { 61 unsigned start; // Start point of the interval (inclusive) 62 unsigned end; // End point of the interval (exclusive) 63 VNInfo *valno; // identifier for the value contained in this interval. 64 65 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) { 66 assert(S < E && "Cannot create empty or backwards range"); 67 } 68 69 /// contains - Return true if the index is covered by this range. 70 /// 71 bool contains(unsigned I) const { 72 return start <= I && I < end; 73 } 74 75 bool operator<(const LiveRange &LR) const { 76 return start < LR.start || (start == LR.start && end < LR.end); 77 } 78 bool operator==(const LiveRange &LR) const { 79 return start == LR.start && end == LR.end; 80 } 81 82 void dump() const; 83 void print(std::ostream &os) const; 84 void print(std::ostream *os) const { if (os) print(*os); } 85 86 private: 87 LiveRange(); // DO NOT IMPLEMENT 88 }; 89 90 std::ostream& operator<<(std::ostream& os, const LiveRange &LR); 91 92 93 inline bool operator<(unsigned V, const LiveRange &LR) { 94 return V < LR.start; 95 } 96 97 inline bool operator<(const LiveRange &LR, unsigned V) { 98 return LR.start < V; 99 } 100 101 /// LiveInterval - This class represents some number of live ranges for a 102 /// register or value. This class also contains a bit of register allocator 103 /// state. 104 struct LiveInterval { 105 typedef SmallVector<LiveRange,4> Ranges; 106 typedef SmallVector<VNInfo*,4> VNInfoList; 107 108 unsigned reg; // the register or stack slot of this interval 109 // if the top bits is set, it represents a stack slot. 110 float weight; // weight of this interval 111 unsigned short preference; // preferred register for this interval 112 Ranges ranges; // the ranges in which this register is live 113 VNInfoList valnos; // value#'s 114 115 public: 116 LiveInterval(unsigned Reg, float Weight, bool IsSS = false) 117 : reg(Reg), weight(Weight), preference(0) { 118 if (IsSS) 119 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1)); 120 } 121 122 typedef Ranges::iterator iterator; 123 iterator begin() { return ranges.begin(); } 124 iterator end() { return ranges.end(); } 125 126 typedef Ranges::const_iterator const_iterator; 127 const_iterator begin() const { return ranges.begin(); } 128 const_iterator end() const { return ranges.end(); } 129 130 typedef VNInfoList::iterator vni_iterator; 131 vni_iterator vni_begin() { return valnos.begin(); } 132 vni_iterator vni_end() { return valnos.end(); } 133 134 typedef VNInfoList::const_iterator const_vni_iterator; 135 const_vni_iterator vni_begin() const { return valnos.begin(); } 136 const_vni_iterator vni_end() const { return valnos.end(); } 137 138 /// advanceTo - Advance the specified iterator to point to the LiveRange 139 /// containing the specified position, or end() if the position is past the 140 /// end of the interval. If no LiveRange contains this position, but the 141 /// position is in a hole, this method returns an iterator pointing the the 142 /// LiveRange immediately after the hole. 143 iterator advanceTo(iterator I, unsigned Pos) { 144 if (Pos >= endNumber()) 145 return end(); 146 while (I->end <= Pos) ++I; 147 return I; 148 } 149 150 void clear() { 151 while (!valnos.empty()) { 152 VNInfo *VNI = valnos.back(); 153 valnos.pop_back(); 154 VNI->~VNInfo(); 155 } 156 157 ranges.clear(); 158 } 159 160 /// isStackSlot - Return true if this is a stack slot interval. 161 /// 162 bool isStackSlot() const { 163 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1)); 164 } 165 166 /// getStackSlotIndex - Return stack slot index if this is a stack slot 167 /// interval. 168 int getStackSlotIndex() const { 169 assert(isStackSlot() && "Interval is not a stack slot interval!"); 170 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1)); 171 } 172 173 bool hasAtLeastOneValue() const { return !valnos.empty(); } 174 175 bool containsOneValue() const { return valnos.size() == 1; } 176 177 unsigned getNumValNums() const { return (unsigned)valnos.size(); } 178 179 /// getValNumInfo - Returns pointer to the specified val#. 180 /// 181 inline VNInfo *getValNumInfo(unsigned ValNo) { 182 return valnos[ValNo]; 183 } 184 inline const VNInfo *getValNumInfo(unsigned ValNo) const { 185 return valnos[ValNo]; 186 } 187 188 /// copyValNumInfo - Copy the value number info for one value number to 189 /// another. 190 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) { 191 DstValNo->def = SrcValNo->def; 192 DstValNo->copy = SrcValNo->copy; 193 DstValNo->hasPHIKill = SrcValNo->hasPHIKill; 194 DstValNo->redefByEC = SrcValNo->redefByEC; 195 DstValNo->kills = SrcValNo->kills; 196 } 197 198 /// getNextValue - Create a new value number and return it. MIIdx specifies 199 /// the instruction that defines the value number. 200 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI, 201 BumpPtrAllocator &VNInfoAllocator) { 202#ifdef __GNUC__ 203 unsigned Alignment = (unsigned)__alignof__(VNInfo); 204#else 205 // FIXME: ugly. 206 unsigned Alignment = 8; 207#endif 208 VNInfo *VNI = 209 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo), 210 Alignment)); 211 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI); 212 valnos.push_back(VNI); 213 return VNI; 214 } 215 216 /// getUnknownValNo - Find a value# for unknown values, if there isn't one 217 /// create a new one. 218 VNInfo *getUnknownValNo(BumpPtrAllocator &VNInfoAllocator); 219 220 /// addKill - Add a kill instruction index to the specified value 221 /// number. 222 static void addKill(VNInfo *VNI, unsigned KillIdx) { 223 SmallVector<unsigned, 4> &kills = VNI->kills; 224 if (kills.empty()) { 225 kills.push_back(KillIdx); 226 } else { 227 SmallVector<unsigned, 4>::iterator 228 I = std::lower_bound(kills.begin(), kills.end(), KillIdx); 229 kills.insert(I, KillIdx); 230 } 231 } 232 233 /// addKills - Add a number of kills into the VNInfo kill vector. If this 234 /// interval is live at a kill point, then the kill is not added. 235 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) { 236 for (unsigned i = 0, e = static_cast<unsigned>(kills.size()); 237 i != e; ++i) { 238 unsigned KillIdx = kills[i]; 239 if (!liveBeforeAndAt(KillIdx)) { 240 SmallVector<unsigned, 4>::iterator 241 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx); 242 VNI->kills.insert(I, KillIdx); 243 } 244 } 245 } 246 247 /// removeKill - Remove the specified kill from the list of kills of 248 /// the specified val#. 249 static bool removeKill(VNInfo *VNI, unsigned KillIdx) { 250 SmallVector<unsigned, 4> &kills = VNI->kills; 251 SmallVector<unsigned, 4>::iterator 252 I = std::lower_bound(kills.begin(), kills.end(), KillIdx); 253 if (I != kills.end() && *I == KillIdx) { 254 kills.erase(I); 255 return true; 256 } 257 return false; 258 } 259 260 /// removeKills - Remove all the kills in specified range 261 /// [Start, End] of the specified val#. 262 static void removeKills(VNInfo *VNI, unsigned Start, unsigned End) { 263 SmallVector<unsigned, 4> &kills = VNI->kills; 264 SmallVector<unsigned, 4>::iterator 265 I = std::lower_bound(kills.begin(), kills.end(), Start); 266 SmallVector<unsigned, 4>::iterator 267 E = std::upper_bound(kills.begin(), kills.end(), End); 268 kills.erase(I, E); 269 } 270 271 /// isKill - Return true if the specified index is a kill of the 272 /// specified val#. 273 static bool isKill(const VNInfo *VNI, unsigned KillIdx) { 274 const SmallVector<unsigned, 4> &kills = VNI->kills; 275 SmallVector<unsigned, 4>::const_iterator 276 I = std::lower_bound(kills.begin(), kills.end(), KillIdx); 277 return I != kills.end() && *I == KillIdx; 278 } 279 280 /// isOnlyLROfValNo - Return true if the specified live range is the only 281 /// one defined by the its val#. 282 bool isOnlyLROfValNo( const LiveRange *LR) { 283 for (const_iterator I = begin(), E = end(); I != E; ++I) { 284 const LiveRange *Tmp = I; 285 if (Tmp != LR && Tmp->valno == LR->valno) 286 return false; 287 } 288 return true; 289 } 290 291 /// MergeValueNumberInto - This method is called when two value nubmers 292 /// are found to be equivalent. This eliminates V1, replacing all 293 /// LiveRanges with the V1 value number with the V2 value number. This can 294 /// cause merging of V1/V2 values numbers and compaction of the value space. 295 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2); 296 297 /// MergeInClobberRanges - For any live ranges that are not defined in the 298 /// current interval, but are defined in the Clobbers interval, mark them 299 /// used with an unknown definition value. Caller must pass in reference to 300 /// VNInfoAllocator since it will create a new val#. 301 void MergeInClobberRanges(const LiveInterval &Clobbers, 302 BumpPtrAllocator &VNInfoAllocator); 303 304 /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a 305 /// single LiveRange only. 306 void MergeInClobberRange(unsigned Start, unsigned End, 307 BumpPtrAllocator &VNInfoAllocator); 308 309 /// MergeValueInAsValue - Merge all of the live ranges of a specific val# 310 /// in RHS into this live interval as the specified value number. 311 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the 312 /// current interval, it will replace the value numbers of the overlaped 313 /// live ranges with the specified value number. 314 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo); 315 316 /// MergeValueInAsValue - Merge all of the live ranges of a specific val# 317 /// in RHS into this live interval as the specified value number. 318 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the 319 /// current interval, but only if the overlapping LiveRanges have the 320 /// specified value number. 321 void MergeValueInAsValue(const LiveInterval &RHS, 322 const VNInfo *RHSValNo, VNInfo *LHSValNo); 323 324 /// Copy - Copy the specified live interval. This copies all the fields 325 /// except for the register of the interval. 326 void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator); 327 328 bool empty() const { return ranges.empty(); } 329 330 /// beginNumber - Return the lowest numbered slot covered by interval. 331 unsigned beginNumber() const { 332 if (empty()) 333 return 0; 334 return ranges.front().start; 335 } 336 337 /// endNumber - return the maximum point of the interval of the whole, 338 /// exclusive. 339 unsigned endNumber() const { 340 if (empty()) 341 return 0; 342 return ranges.back().end; 343 } 344 345 bool expiredAt(unsigned index) const { 346 return index >= endNumber(); 347 } 348 349 bool liveAt(unsigned index) const; 350 351 // liveBeforeAndAt - Check if the interval is live at the index and the 352 // index just before it. If index is liveAt, check if it starts a new live 353 // range.If it does, then check if the previous live range ends at index-1. 354 bool liveBeforeAndAt(unsigned index) const; 355 356 /// getLiveRangeContaining - Return the live range that contains the 357 /// specified index, or null if there is none. 358 const LiveRange *getLiveRangeContaining(unsigned Idx) const { 359 const_iterator I = FindLiveRangeContaining(Idx); 360 return I == end() ? 0 : &*I; 361 } 362 363 /// FindLiveRangeContaining - Return an iterator to the live range that 364 /// contains the specified index, or end() if there is none. 365 const_iterator FindLiveRangeContaining(unsigned Idx) const; 366 367 /// FindLiveRangeContaining - Return an iterator to the live range that 368 /// contains the specified index, or end() if there is none. 369 iterator FindLiveRangeContaining(unsigned Idx); 370 371 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified 372 /// index (register interval) or defined by the specified register (stack 373 /// inteval). 374 VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const; 375 376 /// overlaps - Return true if the intersection of the two live intervals is 377 /// not empty. 378 bool overlaps(const LiveInterval& other) const { 379 return overlapsFrom(other, other.begin()); 380 } 381 382 /// overlapsFrom - Return true if the intersection of the two live intervals 383 /// is not empty. The specified iterator is a hint that we can begin 384 /// scanning the Other interval starting at I. 385 bool overlapsFrom(const LiveInterval& other, const_iterator I) const; 386 387 /// addRange - Add the specified LiveRange to this interval, merging 388 /// intervals as appropriate. This returns an iterator to the inserted live 389 /// range (which may have grown since it was inserted. 390 void addRange(LiveRange LR) { 391 addRangeFrom(LR, ranges.begin()); 392 } 393 394 /// join - Join two live intervals (this, and other) together. This applies 395 /// mappings to the value numbers in the LHS/RHS intervals as specified. If 396 /// the intervals are not joinable, this aborts. 397 void join(LiveInterval &Other, const int *ValNoAssignments, 398 const int *RHSValNoAssignments, 399 SmallVector<VNInfo*, 16> &NewVNInfo); 400 401 /// isInOneLiveRange - Return true if the range specified is entirely in the 402 /// a single LiveRange of the live interval. 403 bool isInOneLiveRange(unsigned Start, unsigned End); 404 405 /// removeRange - Remove the specified range from this interval. Note that 406 /// the range must be a single LiveRange in its entirety. 407 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false); 408 409 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) { 410 removeRange(LR.start, LR.end, RemoveDeadValNo); 411 } 412 413 /// removeValNo - Remove all the ranges defined by the specified value#. 414 /// Also remove the value# from value# list. 415 void removeValNo(VNInfo *ValNo); 416 417 /// getSize - Returns the sum of sizes of all the LiveRange's. 418 /// 419 unsigned getSize() const; 420 421 bool operator<(const LiveInterval& other) const { 422 return beginNumber() < other.beginNumber(); 423 } 424 425 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const; 426 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const { 427 if (OS) print(*OS, TRI); 428 } 429 void dump() const; 430 431 private: 432 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From); 433 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd); 434 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr); 435 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT 436 }; 437 438 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) { 439 LI.print(OS); 440 return OS; 441 } 442} 443 444#endif 445