1 //===-- RegisterPressure.cpp - Dynamic Register Pressure ------------------===// 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 RegisterPressure class which can be used to track 11 // MachineInstr level register pressure. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "RegisterClassInfo.h" 16 #include "RegisterPressure.h" 17 #include "llvm/CodeGen/LiveInterval.h" 18 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 19 #include "llvm/CodeGen/MachineRegisterInfo.h" 20 #include "llvm/Target/TargetMachine.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 using namespace llvm; 25 26 /// Increase register pressure for each set impacted by this register class. 27 static void increaseSetPressure(std::vector<unsigned> &CurrSetPressure, 28 std::vector<unsigned> &MaxSetPressure, 29 const TargetRegisterClass *RC, 30 const TargetRegisterInfo *TRI) { 31 unsigned Weight = TRI->getRegClassWeight(RC).RegWeight; 32 for (const int *PSet = TRI->getRegClassPressureSets(RC); 33 *PSet != -1; ++PSet) { 34 CurrSetPressure[*PSet] += Weight; 35 if (&CurrSetPressure != &MaxSetPressure 36 && CurrSetPressure[*PSet] > MaxSetPressure[*PSet]) { 37 MaxSetPressure[*PSet] = CurrSetPressure[*PSet]; 38 } 39 } 40 } 41 42 /// Decrease register pressure for each set impacted by this register class. 43 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure, 44 const TargetRegisterClass *RC, 45 const TargetRegisterInfo *TRI) { 46 unsigned Weight = TRI->getRegClassWeight(RC).RegWeight; 47 for (const int *PSet = TRI->getRegClassPressureSets(RC); 48 *PSet != -1; ++PSet) { 49 assert(CurrSetPressure[*PSet] >= Weight && "register pressure underflow"); 50 CurrSetPressure[*PSet] -= Weight; 51 } 52 } 53 54 /// Directly increase pressure only within this RegisterPressure result. 55 void RegisterPressure::increase(const TargetRegisterClass *RC, 56 const TargetRegisterInfo *TRI) { 57 increaseSetPressure(MaxSetPressure, MaxSetPressure, RC, TRI); 58 } 59 60 /// Directly decrease pressure only within this RegisterPressure result. 61 void RegisterPressure::decrease(const TargetRegisterClass *RC, 62 const TargetRegisterInfo *TRI) { 63 decreaseSetPressure(MaxSetPressure, RC, TRI); 64 } 65 66 void RegisterPressure::dump(const TargetRegisterInfo *TRI) { 67 dbgs() << "Live In: "; 68 for (unsigned i = 0, e = LiveInRegs.size(); i < e; ++i) 69 dbgs() << PrintReg(LiveInRegs[i], TRI) << " "; 70 dbgs() << '\n'; 71 dbgs() << "Live Out: "; 72 for (unsigned i = 0, e = LiveOutRegs.size(); i < e; ++i) 73 dbgs() << PrintReg(LiveOutRegs[i], TRI) << " "; 74 dbgs() << '\n'; 75 for (unsigned i = 0, e = MaxSetPressure.size(); i < e; ++i) { 76 if (MaxSetPressure[i] != 0) 77 dbgs() << TRI->getRegPressureSetName(i) << "=" << MaxSetPressure[i] 78 << '\n'; 79 } 80 } 81 82 /// Increase the current pressure as impacted by these physical registers and 83 /// bump the high water mark if needed. 84 void RegPressureTracker::increasePhysRegPressure(ArrayRef<unsigned> Regs) { 85 for (unsigned I = 0, E = Regs.size(); I != E; ++I) 86 increaseSetPressure(CurrSetPressure, P.MaxSetPressure, 87 TRI->getMinimalPhysRegClass(Regs[I]), TRI); 88 } 89 90 /// Simply decrease the current pressure as impacted by these physcial 91 /// registers. 92 void RegPressureTracker::decreasePhysRegPressure(ArrayRef<unsigned> Regs) { 93 for (unsigned I = 0, E = Regs.size(); I != E; ++I) 94 decreaseSetPressure(CurrSetPressure, TRI->getMinimalPhysRegClass(Regs[I]), 95 TRI); 96 } 97 98 /// Increase the current pressure as impacted by these virtual registers and 99 /// bump the high water mark if needed. 100 void RegPressureTracker::increaseVirtRegPressure(ArrayRef<unsigned> Regs) { 101 for (unsigned I = 0, E = Regs.size(); I != E; ++I) 102 increaseSetPressure(CurrSetPressure, P.MaxSetPressure, 103 MRI->getRegClass(Regs[I]), TRI); 104 } 105 106 /// Simply decrease the current pressure as impacted by these virtual registers. 107 void RegPressureTracker::decreaseVirtRegPressure(ArrayRef<unsigned> Regs) { 108 for (unsigned I = 0, E = Regs.size(); I != E; ++I) 109 decreaseSetPressure(CurrSetPressure, MRI->getRegClass(Regs[I]), TRI); 110 } 111 112 /// Clear the result so it can be used for another round of pressure tracking. 113 void IntervalPressure::reset() { 114 TopIdx = BottomIdx = SlotIndex(); 115 MaxSetPressure.clear(); 116 LiveInRegs.clear(); 117 LiveOutRegs.clear(); 118 } 119 120 /// Clear the result so it can be used for another round of pressure tracking. 121 void RegionPressure::reset() { 122 TopPos = BottomPos = MachineBasicBlock::const_iterator(); 123 MaxSetPressure.clear(); 124 LiveInRegs.clear(); 125 LiveOutRegs.clear(); 126 } 127 128 /// If the current top is not less than or equal to the next index, open it. 129 /// We happen to need the SlotIndex for the next top for pressure update. 130 void IntervalPressure::openTop(SlotIndex NextTop) { 131 if (TopIdx <= NextTop) 132 return; 133 TopIdx = SlotIndex(); 134 LiveInRegs.clear(); 135 } 136 137 /// If the current top is the previous instruction (before receding), open it. 138 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) { 139 if (TopPos != PrevTop) 140 return; 141 TopPos = MachineBasicBlock::const_iterator(); 142 LiveInRegs.clear(); 143 } 144 145 /// If the current bottom is not greater than the previous index, open it. 146 void IntervalPressure::openBottom(SlotIndex PrevBottom) { 147 if (BottomIdx > PrevBottom) 148 return; 149 BottomIdx = SlotIndex(); 150 LiveInRegs.clear(); 151 } 152 153 /// If the current bottom is the previous instr (before advancing), open it. 154 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) { 155 if (BottomPos != PrevBottom) 156 return; 157 BottomPos = MachineBasicBlock::const_iterator(); 158 LiveInRegs.clear(); 159 } 160 161 /// Setup the RegPressureTracker. 162 /// 163 /// TODO: Add support for pressure without LiveIntervals. 164 void RegPressureTracker::init(const MachineFunction *mf, 165 const RegisterClassInfo *rci, 166 const LiveIntervals *lis, 167 const MachineBasicBlock *mbb, 168 MachineBasicBlock::const_iterator pos) 169 { 170 MF = mf; 171 TRI = MF->getTarget().getRegisterInfo(); 172 RCI = rci; 173 MRI = &MF->getRegInfo(); 174 MBB = mbb; 175 176 if (RequireIntervals) { 177 assert(lis && "IntervalPressure requires LiveIntervals"); 178 LIS = lis; 179 } 180 181 CurrPos = pos; 182 while (CurrPos != MBB->end() && CurrPos->isDebugValue()) 183 ++CurrPos; 184 185 CurrSetPressure.assign(TRI->getNumRegPressureSets(), 0); 186 187 if (RequireIntervals) 188 static_cast<IntervalPressure&>(P).reset(); 189 else 190 static_cast<RegionPressure&>(P).reset(); 191 P.MaxSetPressure = CurrSetPressure; 192 193 LivePhysRegs.clear(); 194 LivePhysRegs.setUniverse(TRI->getNumRegs()); 195 LiveVirtRegs.clear(); 196 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs()); 197 } 198 199 /// Does this pressure result have a valid top position and live ins. 200 bool RegPressureTracker::isTopClosed() const { 201 if (RequireIntervals) 202 return static_cast<IntervalPressure&>(P).TopIdx.isValid(); 203 return (static_cast<RegionPressure&>(P).TopPos == 204 MachineBasicBlock::const_iterator()); 205 } 206 207 /// Does this pressure result have a valid bottom position and live outs. 208 bool RegPressureTracker::isBottomClosed() const { 209 if (RequireIntervals) 210 return static_cast<IntervalPressure&>(P).BottomIdx.isValid(); 211 return (static_cast<RegionPressure&>(P).BottomPos == 212 MachineBasicBlock::const_iterator()); 213 } 214 215 /// Set the boundary for the top of the region and summarize live ins. 216 void RegPressureTracker::closeTop() { 217 if (RequireIntervals) 218 static_cast<IntervalPressure&>(P).TopIdx = 219 LIS->getInstructionIndex(CurrPos).getRegSlot(); 220 else 221 static_cast<RegionPressure&>(P).TopPos = CurrPos; 222 223 assert(P.LiveInRegs.empty() && "inconsistent max pressure result"); 224 P.LiveInRegs.reserve(LivePhysRegs.size() + LiveVirtRegs.size()); 225 P.LiveInRegs.append(LivePhysRegs.begin(), LivePhysRegs.end()); 226 for (SparseSet<unsigned>::const_iterator I = 227 LiveVirtRegs.begin(), E = LiveVirtRegs.end(); I != E; ++I) 228 P.LiveInRegs.push_back(*I); 229 std::sort(P.LiveInRegs.begin(), P.LiveInRegs.end()); 230 P.LiveInRegs.erase(std::unique(P.LiveInRegs.begin(), P.LiveInRegs.end()), 231 P.LiveInRegs.end()); 232 } 233 234 /// Set the boundary for the bottom of the region and summarize live outs. 235 void RegPressureTracker::closeBottom() { 236 if (RequireIntervals) 237 if (CurrPos == MBB->end()) 238 static_cast<IntervalPressure&>(P).BottomIdx = LIS->getMBBEndIdx(MBB); 239 else 240 static_cast<IntervalPressure&>(P).BottomIdx = 241 LIS->getInstructionIndex(CurrPos).getRegSlot(); 242 else 243 static_cast<RegionPressure&>(P).BottomPos = CurrPos; 244 245 assert(P.LiveOutRegs.empty() && "inconsistent max pressure result"); 246 P.LiveOutRegs.reserve(LivePhysRegs.size() + LiveVirtRegs.size()); 247 P.LiveOutRegs.append(LivePhysRegs.begin(), LivePhysRegs.end()); 248 for (SparseSet<unsigned>::const_iterator I = 249 LiveVirtRegs.begin(), E = LiveVirtRegs.end(); I != E; ++I) 250 P.LiveOutRegs.push_back(*I); 251 std::sort(P.LiveOutRegs.begin(), P.LiveOutRegs.end()); 252 P.LiveOutRegs.erase(std::unique(P.LiveOutRegs.begin(), P.LiveOutRegs.end()), 253 P.LiveOutRegs.end()); 254 } 255 256 /// Finalize the region boundaries and record live ins and live outs. 257 void RegPressureTracker::closeRegion() { 258 if (!isTopClosed() && !isBottomClosed()) { 259 assert(LivePhysRegs.empty() && LiveVirtRegs.empty() && 260 "no region boundary"); 261 return; 262 } 263 if (!isBottomClosed()) 264 closeBottom(); 265 else if (!isTopClosed()) 266 closeTop(); 267 // If both top and bottom are closed, do nothing. 268 } 269 270 /// Return true if Reg aliases a register in Regs SparseSet. 271 static bool hasRegAlias(unsigned Reg, SparseSet<unsigned> &Regs, 272 const TargetRegisterInfo *TRI) { 273 assert(!TargetRegisterInfo::isVirtualRegister(Reg) && "only for physregs"); 274 for (const uint16_t *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) { 275 if (Regs.count(*Alias)) 276 return true; 277 } 278 return false; 279 } 280 281 /// Return true if Reg aliases a register in unsorted Regs SmallVector. 282 /// This is only valid for physical registers. 283 static SmallVectorImpl<unsigned>::iterator 284 findRegAlias(unsigned Reg, SmallVectorImpl<unsigned> &Regs, 285 const TargetRegisterInfo *TRI) { 286 for (const uint16_t *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) { 287 SmallVectorImpl<unsigned>::iterator I = 288 std::find(Regs.begin(), Regs.end(), *Alias); 289 if (I != Regs.end()) 290 return I; 291 } 292 return Regs.end(); 293 } 294 295 /// Return true if Reg can be inserted into Regs SmallVector. For virtual 296 /// register, do a linear search. For physical registers check for aliases. 297 static SmallVectorImpl<unsigned>::iterator 298 findReg(unsigned Reg, bool isVReg, SmallVectorImpl<unsigned> &Regs, 299 const TargetRegisterInfo *TRI) { 300 if(isVReg) 301 return std::find(Regs.begin(), Regs.end(), Reg); 302 return findRegAlias(Reg, Regs, TRI); 303 } 304 305 /// Collect this instruction's unique uses and defs into SmallVectors for 306 /// processing defs and uses in order. 307 template<bool isVReg> 308 struct RegisterOperands { 309 SmallVector<unsigned, 8> Uses; 310 SmallVector<unsigned, 8> Defs; 311 SmallVector<unsigned, 8> DeadDefs; 312 313 /// Push this operand's register onto the correct vector. 314 void collect(const MachineOperand &MO, const TargetRegisterInfo *TRI) { 315 if (MO.readsReg()) { 316 if (findReg(MO.getReg(), isVReg, Uses, TRI) == Uses.end()) 317 Uses.push_back(MO.getReg()); 318 } 319 if (MO.isDef()) { 320 if (MO.isDead()) { 321 if (findReg(MO.getReg(), isVReg, DeadDefs, TRI) == DeadDefs.end()) 322 DeadDefs.push_back(MO.getReg()); 323 } 324 else { 325 if (findReg(MO.getReg(), isVReg, Defs, TRI) == Defs.end()) 326 Defs.push_back(MO.getReg()); 327 } 328 } 329 } 330 }; 331 typedef RegisterOperands<false> PhysRegOperands; 332 typedef RegisterOperands<true> VirtRegOperands; 333 334 /// Collect physical and virtual register operands. 335 static void collectOperands(const MachineInstr *MI, 336 PhysRegOperands &PhysRegOpers, 337 VirtRegOperands &VirtRegOpers, 338 const TargetRegisterInfo *TRI, 339 const RegisterClassInfo *RCI) { 340 for(ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI) { 341 const MachineOperand &MO = *OperI; 342 if (!MO.isReg() || !MO.getReg()) 343 continue; 344 345 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) 346 VirtRegOpers.collect(MO, TRI); 347 else if (RCI->isAllocatable(MO.getReg())) 348 PhysRegOpers.collect(MO, TRI); 349 } 350 // Remove redundant physreg dead defs. 351 for (unsigned i = PhysRegOpers.DeadDefs.size(); i > 0; --i) { 352 unsigned Reg = PhysRegOpers.DeadDefs[i-1]; 353 if (findRegAlias(Reg, PhysRegOpers.Defs, TRI) != PhysRegOpers.Defs.end()) 354 PhysRegOpers.DeadDefs.erase(&PhysRegOpers.DeadDefs[i-1]); 355 } 356 } 357 358 /// Force liveness of registers. 359 void RegPressureTracker::addLiveRegs(ArrayRef<unsigned> Regs) { 360 for (unsigned i = 0, e = Regs.size(); i != e; ++i) { 361 if (TargetRegisterInfo::isVirtualRegister(Regs[i])) { 362 if (LiveVirtRegs.insert(Regs[i]).second) 363 increaseVirtRegPressure(Regs[i]); 364 } 365 else { 366 if (!hasRegAlias(Regs[i], LivePhysRegs, TRI)) { 367 LivePhysRegs.insert(Regs[i]); 368 increasePhysRegPressure(Regs[i]); 369 } 370 } 371 } 372 } 373 374 /// Add PhysReg to the live in set and increase max pressure. 375 void RegPressureTracker::discoverPhysLiveIn(unsigned Reg) { 376 assert(!LivePhysRegs.count(Reg) && "avoid bumping max pressure twice"); 377 if (findRegAlias(Reg, P.LiveInRegs, TRI) != P.LiveInRegs.end()) 378 return; 379 380 // At live in discovery, unconditionally increase the high water mark. 381 P.LiveInRegs.push_back(Reg); 382 P.increase(TRI->getMinimalPhysRegClass(Reg), TRI); 383 } 384 385 /// Add PhysReg to the live out set and increase max pressure. 386 void RegPressureTracker::discoverPhysLiveOut(unsigned Reg) { 387 assert(!LivePhysRegs.count(Reg) && "avoid bumping max pressure twice"); 388 if (findRegAlias(Reg, P.LiveOutRegs, TRI) != P.LiveOutRegs.end()) 389 return; 390 391 // At live out discovery, unconditionally increase the high water mark. 392 P.LiveOutRegs.push_back(Reg); 393 P.increase(TRI->getMinimalPhysRegClass(Reg), TRI); 394 } 395 396 /// Add VirtReg to the live in set and increase max pressure. 397 void RegPressureTracker::discoverVirtLiveIn(unsigned Reg) { 398 assert(!LiveVirtRegs.count(Reg) && "avoid bumping max pressure twice"); 399 if (std::find(P.LiveInRegs.begin(), P.LiveInRegs.end(), Reg) != 400 P.LiveInRegs.end()) 401 return; 402 403 // At live in discovery, unconditionally increase the high water mark. 404 P.LiveInRegs.push_back(Reg); 405 P.increase(MRI->getRegClass(Reg), TRI); 406 } 407 408 /// Add VirtReg to the live out set and increase max pressure. 409 void RegPressureTracker::discoverVirtLiveOut(unsigned Reg) { 410 assert(!LiveVirtRegs.count(Reg) && "avoid bumping max pressure twice"); 411 if (std::find(P.LiveOutRegs.begin(), P.LiveOutRegs.end(), Reg) != 412 P.LiveOutRegs.end()) 413 return; 414 415 // At live out discovery, unconditionally increase the high water mark. 416 P.LiveOutRegs.push_back(Reg); 417 P.increase(MRI->getRegClass(Reg), TRI); 418 } 419 420 /// Recede across the previous instruction. 421 bool RegPressureTracker::recede() { 422 // Check for the top of the analyzable region. 423 if (CurrPos == MBB->begin()) { 424 closeRegion(); 425 return false; 426 } 427 if (!isBottomClosed()) 428 closeBottom(); 429 430 // Open the top of the region using block iterators. 431 if (!RequireIntervals && isTopClosed()) 432 static_cast<RegionPressure&>(P).openTop(CurrPos); 433 434 // Find the previous instruction. 435 do 436 --CurrPos; 437 while (CurrPos != MBB->begin() && CurrPos->isDebugValue()); 438 439 if (CurrPos->isDebugValue()) { 440 closeRegion(); 441 return false; 442 } 443 SlotIndex SlotIdx; 444 if (RequireIntervals) 445 SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot(); 446 447 // Open the top of the region using slot indexes. 448 if (RequireIntervals && isTopClosed()) 449 static_cast<IntervalPressure&>(P).openTop(SlotIdx); 450 451 PhysRegOperands PhysRegOpers; 452 VirtRegOperands VirtRegOpers; 453 collectOperands(CurrPos, PhysRegOpers, VirtRegOpers, TRI, RCI); 454 455 // Boost pressure for all dead defs together. 456 increasePhysRegPressure(PhysRegOpers.DeadDefs); 457 increaseVirtRegPressure(VirtRegOpers.DeadDefs); 458 decreasePhysRegPressure(PhysRegOpers.DeadDefs); 459 decreaseVirtRegPressure(VirtRegOpers.DeadDefs); 460 461 // Kill liveness at live defs. 462 // TODO: consider earlyclobbers? 463 for (unsigned i = 0, e = PhysRegOpers.Defs.size(); i < e; ++i) { 464 unsigned Reg = PhysRegOpers.Defs[i]; 465 if (LivePhysRegs.erase(Reg)) 466 decreasePhysRegPressure(Reg); 467 else 468 discoverPhysLiveOut(Reg); 469 } 470 for (unsigned i = 0, e = VirtRegOpers.Defs.size(); i < e; ++i) { 471 unsigned Reg = VirtRegOpers.Defs[i]; 472 if (LiveVirtRegs.erase(Reg)) 473 decreaseVirtRegPressure(Reg); 474 else 475 discoverVirtLiveOut(Reg); 476 } 477 478 // Generate liveness for uses. 479 for (unsigned i = 0, e = PhysRegOpers.Uses.size(); i < e; ++i) { 480 unsigned Reg = PhysRegOpers.Uses[i]; 481 if (!hasRegAlias(Reg, LivePhysRegs, TRI)) { 482 increasePhysRegPressure(Reg); 483 LivePhysRegs.insert(Reg); 484 } 485 } 486 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) { 487 unsigned Reg = VirtRegOpers.Uses[i]; 488 if (!LiveVirtRegs.count(Reg)) { 489 // Adjust liveouts if LiveIntervals are available. 490 if (RequireIntervals) { 491 const LiveInterval *LI = &LIS->getInterval(Reg); 492 if (!LI->killedAt(SlotIdx)) 493 discoverVirtLiveOut(Reg); 494 } 495 increaseVirtRegPressure(Reg); 496 LiveVirtRegs.insert(Reg); 497 } 498 } 499 return true; 500 } 501 502 /// Advance across the current instruction. 503 bool RegPressureTracker::advance() { 504 // Check for the bottom of the analyzable region. 505 if (CurrPos == MBB->end()) { 506 closeRegion(); 507 return false; 508 } 509 if (!isTopClosed()) 510 closeTop(); 511 512 SlotIndex SlotIdx; 513 if (RequireIntervals) 514 SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot(); 515 516 // Open the bottom of the region using slot indexes. 517 if (isBottomClosed()) { 518 if (RequireIntervals) 519 static_cast<IntervalPressure&>(P).openBottom(SlotIdx); 520 else 521 static_cast<RegionPressure&>(P).openBottom(CurrPos); 522 } 523 524 PhysRegOperands PhysRegOpers; 525 VirtRegOperands VirtRegOpers; 526 collectOperands(CurrPos, PhysRegOpers, VirtRegOpers, TRI, RCI); 527 528 // Kill liveness at last uses. 529 for (unsigned i = 0, e = PhysRegOpers.Uses.size(); i < e; ++i) { 530 unsigned Reg = PhysRegOpers.Uses[i]; 531 if (!hasRegAlias(Reg, LivePhysRegs, TRI)) 532 discoverPhysLiveIn(Reg); 533 else { 534 // Allocatable physregs are always single-use before regalloc. 535 decreasePhysRegPressure(Reg); 536 LivePhysRegs.erase(Reg); 537 } 538 } 539 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) { 540 unsigned Reg = VirtRegOpers.Uses[i]; 541 if (RequireIntervals) { 542 const LiveInterval *LI = &LIS->getInterval(Reg); 543 if (LI->killedAt(SlotIdx)) { 544 if (LiveVirtRegs.erase(Reg)) 545 decreaseVirtRegPressure(Reg); 546 else 547 discoverVirtLiveIn(Reg); 548 } 549 } 550 else if (!LiveVirtRegs.count(Reg)) { 551 discoverVirtLiveIn(Reg); 552 increaseVirtRegPressure(Reg); 553 } 554 } 555 556 // Generate liveness for defs. 557 for (unsigned i = 0, e = PhysRegOpers.Defs.size(); i < e; ++i) { 558 unsigned Reg = PhysRegOpers.Defs[i]; 559 if (!hasRegAlias(Reg, LivePhysRegs, TRI)) { 560 increasePhysRegPressure(Reg); 561 LivePhysRegs.insert(Reg); 562 } 563 } 564 for (unsigned i = 0, e = VirtRegOpers.Defs.size(); i < e; ++i) { 565 unsigned Reg = VirtRegOpers.Defs[i]; 566 if (LiveVirtRegs.insert(Reg).second) 567 increaseVirtRegPressure(Reg); 568 } 569 570 // Boost pressure for all dead defs together. 571 increasePhysRegPressure(PhysRegOpers.DeadDefs); 572 increaseVirtRegPressure(VirtRegOpers.DeadDefs); 573 decreasePhysRegPressure(PhysRegOpers.DeadDefs); 574 decreaseVirtRegPressure(VirtRegOpers.DeadDefs); 575 576 // Find the next instruction. 577 do 578 ++CurrPos; 579 while (CurrPos != MBB->end() && CurrPos->isDebugValue()); 580 return true; 581 } 582 583 /// Find the max change in excess pressure across all sets. 584 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec, 585 ArrayRef<unsigned> NewPressureVec, 586 RegPressureDelta &Delta, 587 const TargetRegisterInfo *TRI) { 588 int ExcessUnits = 0; 589 unsigned PSetID = ~0U; 590 for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) { 591 unsigned POld = OldPressureVec[i]; 592 unsigned PNew = NewPressureVec[i]; 593 int PDiff = (int)PNew - (int)POld; 594 if (!PDiff) // No change in this set in the common case. 595 continue; 596 // Only consider change beyond the limit. 597 unsigned Limit = TRI->getRegPressureSetLimit(i); 598 if (Limit > POld) { 599 if (Limit > PNew) 600 PDiff = 0; // Under the limit 601 else 602 PDiff = PNew - Limit; // Just exceeded limit. 603 } 604 else if (Limit > PNew) 605 PDiff = Limit - POld; // Just obeyed limit. 606 607 if (std::abs(PDiff) > std::abs(ExcessUnits)) { 608 ExcessUnits = PDiff; 609 PSetID = i; 610 } 611 } 612 Delta.Excess.PSetID = PSetID; 613 Delta.Excess.UnitIncrease = ExcessUnits; 614 } 615 616 /// Find the max change in max pressure that either surpasses a critical PSet 617 /// limit or exceeds the current MaxPressureLimit. 618 /// 619 /// FIXME: comparing each element of the old and new MaxPressure vectors here is 620 /// silly. It's done now to demonstrate the concept but will go away with a 621 /// RegPressureTracker API change to work with pressure differences. 622 static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec, 623 ArrayRef<unsigned> NewMaxPressureVec, 624 ArrayRef<PressureElement> CriticalPSets, 625 ArrayRef<unsigned> MaxPressureLimit, 626 RegPressureDelta &Delta) { 627 Delta.CriticalMax = PressureElement(); 628 Delta.CurrentMax = PressureElement(); 629 630 unsigned CritIdx = 0, CritEnd = CriticalPSets.size(); 631 for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) { 632 unsigned POld = OldMaxPressureVec[i]; 633 unsigned PNew = NewMaxPressureVec[i]; 634 if (PNew == POld) // No change in this set in the common case. 635 continue; 636 637 while (CritIdx != CritEnd && CriticalPSets[CritIdx].PSetID < i) 638 ++CritIdx; 639 640 if (CritIdx != CritEnd && CriticalPSets[CritIdx].PSetID == i) { 641 int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].UnitIncrease; 642 if (PDiff > Delta.CriticalMax.UnitIncrease) { 643 Delta.CriticalMax.PSetID = i; 644 Delta.CriticalMax.UnitIncrease = PDiff; 645 } 646 } 647 648 // Find the greatest increase above MaxPressureLimit. 649 // (Ignores negative MDiff). 650 int MDiff = (int)PNew - (int)MaxPressureLimit[i]; 651 if (MDiff > Delta.CurrentMax.UnitIncrease) { 652 Delta.CurrentMax.PSetID = i; 653 Delta.CurrentMax.UnitIncrease = PNew; 654 } 655 } 656 } 657 658 /// Consider the pressure increase caused by traversing this instruction 659 /// bottom-up. Find the pressure set with the most change beyond its pressure 660 /// limit based on the tracker's current pressure, and return the change in 661 /// number of register units of that pressure set introduced by this 662 /// instruction. 663 /// 664 /// This assumes that the current LiveOut set is sufficient. 665 /// 666 /// FIXME: This is expensive for an on-the-fly query. We need to cache the 667 /// result per-SUnit with enough information to adjust for the current 668 /// scheduling position. But this works as a proof of concept. 669 void RegPressureTracker:: 670 getMaxUpwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta, 671 ArrayRef<PressureElement> CriticalPSets, 672 ArrayRef<unsigned> MaxPressureLimit) { 673 // Account for register pressure similar to RegPressureTracker::recede(). 674 PhysRegOperands PhysRegOpers; 675 VirtRegOperands VirtRegOpers; 676 collectOperands(MI, PhysRegOpers, VirtRegOpers, TRI, RCI); 677 678 // Snapshot Pressure. 679 // FIXME: The snapshot heap space should persist. But I'm planning to 680 // summarize the pressure effect so we don't need to snapshot at all. 681 std::vector<unsigned> SavedPressure = CurrSetPressure; 682 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure; 683 684 // Boost max pressure for all dead defs together. 685 // Since CurrSetPressure and MaxSetPressure 686 increasePhysRegPressure(PhysRegOpers.DeadDefs); 687 increaseVirtRegPressure(VirtRegOpers.DeadDefs); 688 decreasePhysRegPressure(PhysRegOpers.DeadDefs); 689 decreaseVirtRegPressure(VirtRegOpers.DeadDefs); 690 691 // Kill liveness at live defs. 692 decreasePhysRegPressure(PhysRegOpers.Defs); 693 decreaseVirtRegPressure(VirtRegOpers.Defs); 694 695 // Generate liveness for uses. 696 for (unsigned i = 0, e = PhysRegOpers.Uses.size(); i < e; ++i) { 697 unsigned Reg = PhysRegOpers.Uses[i]; 698 if (!hasRegAlias(Reg, LivePhysRegs, TRI) 699 && (findRegAlias(Reg, PhysRegOpers.Defs, TRI) 700 == PhysRegOpers.Defs.end())) { 701 increasePhysRegPressure(Reg); 702 } 703 } 704 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) { 705 unsigned Reg = VirtRegOpers.Uses[i]; 706 if (!LiveVirtRegs.count(Reg) 707 && (std::find(VirtRegOpers.Defs.begin(), VirtRegOpers.Defs.end(), Reg) 708 != VirtRegOpers.Defs.end())) { 709 increaseVirtRegPressure(Reg); 710 } 711 } 712 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, TRI); 713 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets, 714 MaxPressureLimit, Delta); 715 assert(Delta.CriticalMax.UnitIncrease >= 0 && 716 Delta.CurrentMax.UnitIncrease >= 0 && "cannot decrease max pressure"); 717 718 // Restore the tracker's state. 719 P.MaxSetPressure.swap(SavedMaxPressure); 720 CurrSetPressure.swap(SavedPressure); 721 } 722 723 /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx). 724 static bool findUseBetween(unsigned Reg, 725 SlotIndex PriorUseIdx, SlotIndex NextUseIdx, 726 const MachineRegisterInfo *MRI, 727 const LiveIntervals *LIS) { 728 for (MachineRegisterInfo::use_nodbg_iterator 729 UI = MRI->use_nodbg_begin(Reg), UE = MRI->use_nodbg_end(); 730 UI != UE; UI.skipInstruction()) { 731 const MachineInstr* MI = &*UI; 732 SlotIndex InstSlot = LIS->getInstructionIndex(MI).getRegSlot(); 733 if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx) 734 return true; 735 } 736 return false; 737 } 738 739 /// Consider the pressure increase caused by traversing this instruction 740 /// top-down. Find the register class with the most change in its pressure limit 741 /// based on the tracker's current pressure, and return the number of excess 742 /// register units of that pressure set introduced by this instruction. 743 /// 744 /// This assumes that the current LiveIn set is sufficient. 745 void RegPressureTracker:: 746 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta, 747 ArrayRef<PressureElement> CriticalPSets, 748 ArrayRef<unsigned> MaxPressureLimit) { 749 // Account for register pressure similar to RegPressureTracker::recede(). 750 PhysRegOperands PhysRegOpers; 751 VirtRegOperands VirtRegOpers; 752 collectOperands(MI, PhysRegOpers, VirtRegOpers, TRI, RCI); 753 754 // Snapshot Pressure. 755 std::vector<unsigned> SavedPressure = CurrSetPressure; 756 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure; 757 758 // Kill liveness at last uses. Assume allocatable physregs are single-use 759 // rather than checking LiveIntervals. 760 decreasePhysRegPressure(PhysRegOpers.Uses); 761 if (RequireIntervals) { 762 SlotIndex SlotIdx = LIS->getInstructionIndex(MI).getRegSlot(); 763 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) { 764 unsigned Reg = VirtRegOpers.Uses[i]; 765 const LiveInterval *LI = &LIS->getInterval(Reg); 766 // FIXME: allow the caller to pass in the list of vreg uses that remain to 767 // be bottom-scheduled to avoid searching uses at each query. 768 SlotIndex CurrIdx = LIS->getInstructionIndex(CurrPos).getRegSlot(); 769 if (LI->killedAt(SlotIdx) 770 && !findUseBetween(Reg, CurrIdx, SlotIdx, MRI, LIS)) { 771 decreaseVirtRegPressure(Reg); 772 } 773 } 774 } 775 776 // Generate liveness for defs. 777 increasePhysRegPressure(PhysRegOpers.Defs); 778 increaseVirtRegPressure(VirtRegOpers.Defs); 779 780 // Boost pressure for all dead defs together. 781 increasePhysRegPressure(PhysRegOpers.DeadDefs); 782 increaseVirtRegPressure(VirtRegOpers.DeadDefs); 783 decreasePhysRegPressure(PhysRegOpers.DeadDefs); 784 decreaseVirtRegPressure(VirtRegOpers.DeadDefs); 785 786 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, TRI); 787 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets, 788 MaxPressureLimit, Delta); 789 assert(Delta.CriticalMax.UnitIncrease >= 0 && 790 Delta.CurrentMax.UnitIncrease >= 0 && "cannot decrease max pressure"); 791 792 // Restore the tracker's state. 793 P.MaxSetPressure.swap(SavedMaxPressure); 794 CurrSetPressure.swap(SavedPressure); 795 } 796