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 "llvm/CodeGen/RegisterPressure.h" 16 #include "llvm/CodeGen/LiveInterval.h" 17 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 18 #include "llvm/CodeGen/MachineRegisterInfo.h" 19 #include "llvm/CodeGen/RegisterClassInfo.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace llvm; 24 25 /// Increase pressure for each pressure set provided by TargetRegisterInfo. 26 static void increaseSetPressure(std::vector<unsigned> &CurrSetPressure, 27 const MachineRegisterInfo &MRI, unsigned Reg, 28 LaneBitmask PrevMask, LaneBitmask NewMask) { 29 assert((PrevMask & ~NewMask) == 0 && "Must not remove bits"); 30 if (PrevMask != 0 || NewMask == 0) 31 return; 32 33 PSetIterator PSetI = MRI.getPressureSets(Reg); 34 unsigned Weight = PSetI.getWeight(); 35 for (; PSetI.isValid(); ++PSetI) 36 CurrSetPressure[*PSetI] += Weight; 37 } 38 39 /// Decrease pressure for each pressure set provided by TargetRegisterInfo. 40 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure, 41 const MachineRegisterInfo &MRI, unsigned Reg, 42 LaneBitmask PrevMask, LaneBitmask NewMask) { 43 assert((NewMask & !PrevMask) == 0 && "Must not add bits"); 44 if (NewMask != 0 || PrevMask == 0) 45 return; 46 47 PSetIterator PSetI = MRI.getPressureSets(Reg); 48 unsigned Weight = PSetI.getWeight(); 49 for (; PSetI.isValid(); ++PSetI) { 50 assert(CurrSetPressure[*PSetI] >= Weight && "register pressure underflow"); 51 CurrSetPressure[*PSetI] -= Weight; 52 } 53 } 54 55 LLVM_DUMP_METHOD 56 void llvm::dumpRegSetPressure(ArrayRef<unsigned> SetPressure, 57 const TargetRegisterInfo *TRI) { 58 bool Empty = true; 59 for (unsigned i = 0, e = SetPressure.size(); i < e; ++i) { 60 if (SetPressure[i] != 0) { 61 dbgs() << TRI->getRegPressureSetName(i) << "=" << SetPressure[i] << '\n'; 62 Empty = false; 63 } 64 } 65 if (Empty) 66 dbgs() << "\n"; 67 } 68 69 LLVM_DUMP_METHOD 70 void RegisterPressure::dump(const TargetRegisterInfo *TRI) const { 71 dbgs() << "Max Pressure: "; 72 dumpRegSetPressure(MaxSetPressure, TRI); 73 dbgs() << "Live In: "; 74 for (const RegisterMaskPair &P : LiveInRegs) { 75 dbgs() << PrintVRegOrUnit(P.RegUnit, TRI); 76 if (P.LaneMask != ~0u) 77 dbgs() << ':' << PrintLaneMask(P.LaneMask); 78 dbgs() << ' '; 79 } 80 dbgs() << '\n'; 81 dbgs() << "Live Out: "; 82 for (const RegisterMaskPair &P : LiveOutRegs) { 83 dbgs() << PrintVRegOrUnit(P.RegUnit, TRI); 84 if (P.LaneMask != ~0u) 85 dbgs() << ':' << PrintLaneMask(P.LaneMask); 86 dbgs() << ' '; 87 } 88 dbgs() << '\n'; 89 } 90 91 LLVM_DUMP_METHOD 92 void RegPressureTracker::dump() const { 93 if (!isTopClosed() || !isBottomClosed()) { 94 dbgs() << "Curr Pressure: "; 95 dumpRegSetPressure(CurrSetPressure, TRI); 96 } 97 P.dump(TRI); 98 } 99 100 void PressureDiff::dump(const TargetRegisterInfo &TRI) const { 101 const char *sep = ""; 102 for (const PressureChange &Change : *this) { 103 if (!Change.isValid()) 104 break; 105 dbgs() << sep << TRI.getRegPressureSetName(Change.getPSet()) 106 << " " << Change.getUnitInc(); 107 sep = " "; 108 } 109 dbgs() << '\n'; 110 } 111 112 void RegPressureTracker::increaseRegPressure(unsigned RegUnit, 113 LaneBitmask PreviousMask, 114 LaneBitmask NewMask) { 115 if (PreviousMask != 0 || NewMask == 0) 116 return; 117 118 PSetIterator PSetI = MRI->getPressureSets(RegUnit); 119 unsigned Weight = PSetI.getWeight(); 120 for (; PSetI.isValid(); ++PSetI) { 121 CurrSetPressure[*PSetI] += Weight; 122 P.MaxSetPressure[*PSetI] = 123 std::max(P.MaxSetPressure[*PSetI], CurrSetPressure[*PSetI]); 124 } 125 } 126 127 void RegPressureTracker::decreaseRegPressure(unsigned RegUnit, 128 LaneBitmask PreviousMask, 129 LaneBitmask NewMask) { 130 decreaseSetPressure(CurrSetPressure, *MRI, RegUnit, PreviousMask, NewMask); 131 } 132 133 /// Clear the result so it can be used for another round of pressure tracking. 134 void IntervalPressure::reset() { 135 TopIdx = BottomIdx = SlotIndex(); 136 MaxSetPressure.clear(); 137 LiveInRegs.clear(); 138 LiveOutRegs.clear(); 139 } 140 141 /// Clear the result so it can be used for another round of pressure tracking. 142 void RegionPressure::reset() { 143 TopPos = BottomPos = MachineBasicBlock::const_iterator(); 144 MaxSetPressure.clear(); 145 LiveInRegs.clear(); 146 LiveOutRegs.clear(); 147 } 148 149 /// If the current top is not less than or equal to the next index, open it. 150 /// We happen to need the SlotIndex for the next top for pressure update. 151 void IntervalPressure::openTop(SlotIndex NextTop) { 152 if (TopIdx <= NextTop) 153 return; 154 TopIdx = SlotIndex(); 155 LiveInRegs.clear(); 156 } 157 158 /// If the current top is the previous instruction (before receding), open it. 159 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) { 160 if (TopPos != PrevTop) 161 return; 162 TopPos = MachineBasicBlock::const_iterator(); 163 LiveInRegs.clear(); 164 } 165 166 /// If the current bottom is not greater than the previous index, open it. 167 void IntervalPressure::openBottom(SlotIndex PrevBottom) { 168 if (BottomIdx > PrevBottom) 169 return; 170 BottomIdx = SlotIndex(); 171 LiveInRegs.clear(); 172 } 173 174 /// If the current bottom is the previous instr (before advancing), open it. 175 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) { 176 if (BottomPos != PrevBottom) 177 return; 178 BottomPos = MachineBasicBlock::const_iterator(); 179 LiveInRegs.clear(); 180 } 181 182 void LiveRegSet::init(const MachineRegisterInfo &MRI) { 183 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 184 unsigned NumRegUnits = TRI.getNumRegs(); 185 unsigned NumVirtRegs = MRI.getNumVirtRegs(); 186 Regs.setUniverse(NumRegUnits + NumVirtRegs); 187 this->NumRegUnits = NumRegUnits; 188 } 189 190 void LiveRegSet::clear() { 191 Regs.clear(); 192 } 193 194 static const LiveRange *getLiveRange(const LiveIntervals &LIS, unsigned Reg) { 195 if (TargetRegisterInfo::isVirtualRegister(Reg)) 196 return &LIS.getInterval(Reg); 197 return LIS.getCachedRegUnit(Reg); 198 } 199 200 void RegPressureTracker::reset() { 201 MBB = nullptr; 202 LIS = nullptr; 203 204 CurrSetPressure.clear(); 205 LiveThruPressure.clear(); 206 P.MaxSetPressure.clear(); 207 208 if (RequireIntervals) 209 static_cast<IntervalPressure&>(P).reset(); 210 else 211 static_cast<RegionPressure&>(P).reset(); 212 213 LiveRegs.clear(); 214 UntiedDefs.clear(); 215 } 216 217 /// Setup the RegPressureTracker. 218 /// 219 /// TODO: Add support for pressure without LiveIntervals. 220 void RegPressureTracker::init(const MachineFunction *mf, 221 const RegisterClassInfo *rci, 222 const LiveIntervals *lis, 223 const MachineBasicBlock *mbb, 224 MachineBasicBlock::const_iterator pos, 225 bool TrackLaneMasks, bool TrackUntiedDefs) { 226 reset(); 227 228 MF = mf; 229 TRI = MF->getSubtarget().getRegisterInfo(); 230 RCI = rci; 231 MRI = &MF->getRegInfo(); 232 MBB = mbb; 233 this->TrackUntiedDefs = TrackUntiedDefs; 234 this->TrackLaneMasks = TrackLaneMasks; 235 236 if (RequireIntervals) { 237 assert(lis && "IntervalPressure requires LiveIntervals"); 238 LIS = lis; 239 } 240 241 CurrPos = pos; 242 CurrSetPressure.assign(TRI->getNumRegPressureSets(), 0); 243 244 P.MaxSetPressure = CurrSetPressure; 245 246 LiveRegs.init(*MRI); 247 if (TrackUntiedDefs) 248 UntiedDefs.setUniverse(MRI->getNumVirtRegs()); 249 } 250 251 /// Does this pressure result have a valid top position and live ins. 252 bool RegPressureTracker::isTopClosed() const { 253 if (RequireIntervals) 254 return static_cast<IntervalPressure&>(P).TopIdx.isValid(); 255 return (static_cast<RegionPressure&>(P).TopPos == 256 MachineBasicBlock::const_iterator()); 257 } 258 259 /// Does this pressure result have a valid bottom position and live outs. 260 bool RegPressureTracker::isBottomClosed() const { 261 if (RequireIntervals) 262 return static_cast<IntervalPressure&>(P).BottomIdx.isValid(); 263 return (static_cast<RegionPressure&>(P).BottomPos == 264 MachineBasicBlock::const_iterator()); 265 } 266 267 268 SlotIndex RegPressureTracker::getCurrSlot() const { 269 MachineBasicBlock::const_iterator IdxPos = CurrPos; 270 while (IdxPos != MBB->end() && IdxPos->isDebugValue()) 271 ++IdxPos; 272 if (IdxPos == MBB->end()) 273 return LIS->getMBBEndIdx(MBB); 274 return LIS->getInstructionIndex(*IdxPos).getRegSlot(); 275 } 276 277 /// Set the boundary for the top of the region and summarize live ins. 278 void RegPressureTracker::closeTop() { 279 if (RequireIntervals) 280 static_cast<IntervalPressure&>(P).TopIdx = getCurrSlot(); 281 else 282 static_cast<RegionPressure&>(P).TopPos = CurrPos; 283 284 assert(P.LiveInRegs.empty() && "inconsistent max pressure result"); 285 P.LiveInRegs.reserve(LiveRegs.size()); 286 LiveRegs.appendTo(P.LiveInRegs); 287 } 288 289 /// Set the boundary for the bottom of the region and summarize live outs. 290 void RegPressureTracker::closeBottom() { 291 if (RequireIntervals) 292 static_cast<IntervalPressure&>(P).BottomIdx = getCurrSlot(); 293 else 294 static_cast<RegionPressure&>(P).BottomPos = CurrPos; 295 296 assert(P.LiveOutRegs.empty() && "inconsistent max pressure result"); 297 P.LiveOutRegs.reserve(LiveRegs.size()); 298 LiveRegs.appendTo(P.LiveOutRegs); 299 } 300 301 /// Finalize the region boundaries and record live ins and live outs. 302 void RegPressureTracker::closeRegion() { 303 if (!isTopClosed() && !isBottomClosed()) { 304 assert(LiveRegs.size() == 0 && "no region boundary"); 305 return; 306 } 307 if (!isBottomClosed()) 308 closeBottom(); 309 else if (!isTopClosed()) 310 closeTop(); 311 // If both top and bottom are closed, do nothing. 312 } 313 314 /// The register tracker is unaware of global liveness so ignores normal 315 /// live-thru ranges. However, two-address or coalesced chains can also lead 316 /// to live ranges with no holes. Count these to inform heuristics that we 317 /// can never drop below this pressure. 318 void RegPressureTracker::initLiveThru(const RegPressureTracker &RPTracker) { 319 LiveThruPressure.assign(TRI->getNumRegPressureSets(), 0); 320 assert(isBottomClosed() && "need bottom-up tracking to intialize."); 321 for (const RegisterMaskPair &Pair : P.LiveOutRegs) { 322 unsigned RegUnit = Pair.RegUnit; 323 if (TargetRegisterInfo::isVirtualRegister(RegUnit) 324 && !RPTracker.hasUntiedDef(RegUnit)) 325 increaseSetPressure(LiveThruPressure, *MRI, RegUnit, 0, Pair.LaneMask); 326 } 327 } 328 329 static LaneBitmask getRegLanes(ArrayRef<RegisterMaskPair> RegUnits, 330 unsigned RegUnit) { 331 auto I = find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) { 332 return Other.RegUnit == RegUnit; 333 }); 334 if (I == RegUnits.end()) 335 return 0; 336 return I->LaneMask; 337 } 338 339 static void addRegLanes(SmallVectorImpl<RegisterMaskPair> &RegUnits, 340 RegisterMaskPair Pair) { 341 unsigned RegUnit = Pair.RegUnit; 342 assert(Pair.LaneMask != 0); 343 auto I = find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) { 344 return Other.RegUnit == RegUnit; 345 }); 346 if (I == RegUnits.end()) { 347 RegUnits.push_back(Pair); 348 } else { 349 I->LaneMask |= Pair.LaneMask; 350 } 351 } 352 353 static void setRegZero(SmallVectorImpl<RegisterMaskPair> &RegUnits, 354 unsigned RegUnit) { 355 auto I = find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) { 356 return Other.RegUnit == RegUnit; 357 }); 358 if (I == RegUnits.end()) { 359 RegUnits.push_back(RegisterMaskPair(RegUnit, 0)); 360 } else { 361 I->LaneMask = 0; 362 } 363 } 364 365 static void removeRegLanes(SmallVectorImpl<RegisterMaskPair> &RegUnits, 366 RegisterMaskPair Pair) { 367 unsigned RegUnit = Pair.RegUnit; 368 assert(Pair.LaneMask != 0); 369 auto I = find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) { 370 return Other.RegUnit == RegUnit; 371 }); 372 if (I != RegUnits.end()) { 373 I->LaneMask &= ~Pair.LaneMask; 374 if (I->LaneMask == 0) 375 RegUnits.erase(I); 376 } 377 } 378 379 static LaneBitmask getLanesWithProperty(const LiveIntervals &LIS, 380 const MachineRegisterInfo &MRI, bool TrackLaneMasks, unsigned RegUnit, 381 SlotIndex Pos, LaneBitmask SafeDefault, 382 bool(*Property)(const LiveRange &LR, SlotIndex Pos)) { 383 if (TargetRegisterInfo::isVirtualRegister(RegUnit)) { 384 const LiveInterval &LI = LIS.getInterval(RegUnit); 385 LaneBitmask Result = 0; 386 if (TrackLaneMasks && LI.hasSubRanges()) { 387 for (const LiveInterval::SubRange &SR : LI.subranges()) { 388 if (Property(SR, Pos)) 389 Result |= SR.LaneMask; 390 } 391 } else if (Property(LI, Pos)) { 392 Result = TrackLaneMasks ? MRI.getMaxLaneMaskForVReg(RegUnit) : ~0u; 393 } 394 395 return Result; 396 } else { 397 const LiveRange *LR = LIS.getCachedRegUnit(RegUnit); 398 // Be prepared for missing liveranges: We usually do not compute liveranges 399 // for physical registers on targets with many registers (GPUs). 400 if (LR == nullptr) 401 return SafeDefault; 402 return Property(*LR, Pos) ? ~0u : 0; 403 } 404 } 405 406 static LaneBitmask getLiveLanesAt(const LiveIntervals &LIS, 407 const MachineRegisterInfo &MRI, 408 bool TrackLaneMasks, unsigned RegUnit, 409 SlotIndex Pos) { 410 return getLanesWithProperty(LIS, MRI, TrackLaneMasks, RegUnit, Pos, ~0u, 411 [](const LiveRange &LR, SlotIndex Pos) { 412 return LR.liveAt(Pos); 413 }); 414 } 415 416 417 namespace { 418 419 /// Collect this instruction's unique uses and defs into SmallVectors for 420 /// processing defs and uses in order. 421 /// 422 /// FIXME: always ignore tied opers 423 class RegisterOperandsCollector { 424 RegisterOperands &RegOpers; 425 const TargetRegisterInfo &TRI; 426 const MachineRegisterInfo &MRI; 427 bool IgnoreDead; 428 429 RegisterOperandsCollector(RegisterOperands &RegOpers, 430 const TargetRegisterInfo &TRI, 431 const MachineRegisterInfo &MRI, bool IgnoreDead) 432 : RegOpers(RegOpers), TRI(TRI), MRI(MRI), IgnoreDead(IgnoreDead) {} 433 434 void collectInstr(const MachineInstr &MI) const { 435 for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI) 436 collectOperand(*OperI); 437 438 // Remove redundant physreg dead defs. 439 for (const RegisterMaskPair &P : RegOpers.Defs) 440 removeRegLanes(RegOpers.DeadDefs, P); 441 } 442 443 void collectInstrLanes(const MachineInstr &MI) const { 444 for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI) 445 collectOperandLanes(*OperI); 446 447 // Remove redundant physreg dead defs. 448 for (const RegisterMaskPair &P : RegOpers.Defs) 449 removeRegLanes(RegOpers.DeadDefs, P); 450 } 451 452 /// Push this operand's register onto the correct vectors. 453 void collectOperand(const MachineOperand &MO) const { 454 if (!MO.isReg() || !MO.getReg()) 455 return; 456 unsigned Reg = MO.getReg(); 457 if (MO.isUse()) { 458 if (!MO.isUndef() && !MO.isInternalRead()) 459 pushReg(Reg, RegOpers.Uses); 460 } else { 461 assert(MO.isDef()); 462 // Subregister definitions may imply a register read. 463 if (MO.readsReg()) 464 pushReg(Reg, RegOpers.Uses); 465 466 if (MO.isDead()) { 467 if (!IgnoreDead) 468 pushReg(Reg, RegOpers.DeadDefs); 469 } else 470 pushReg(Reg, RegOpers.Defs); 471 } 472 } 473 474 void pushReg(unsigned Reg, 475 SmallVectorImpl<RegisterMaskPair> &RegUnits) const { 476 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 477 addRegLanes(RegUnits, RegisterMaskPair(Reg, ~0u)); 478 } else if (MRI.isAllocatable(Reg)) { 479 for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) 480 addRegLanes(RegUnits, RegisterMaskPair(*Units, ~0u)); 481 } 482 } 483 484 void collectOperandLanes(const MachineOperand &MO) const { 485 if (!MO.isReg() || !MO.getReg()) 486 return; 487 unsigned Reg = MO.getReg(); 488 unsigned SubRegIdx = MO.getSubReg(); 489 if (MO.isUse()) { 490 if (!MO.isUndef() && !MO.isInternalRead()) 491 pushRegLanes(Reg, SubRegIdx, RegOpers.Uses); 492 } else { 493 assert(MO.isDef()); 494 // Treat read-undef subreg defs as definitions of the whole register. 495 if (MO.isUndef()) 496 SubRegIdx = 0; 497 498 if (MO.isDead()) { 499 if (!IgnoreDead) 500 pushRegLanes(Reg, SubRegIdx, RegOpers.DeadDefs); 501 } else 502 pushRegLanes(Reg, SubRegIdx, RegOpers.Defs); 503 } 504 } 505 506 void pushRegLanes(unsigned Reg, unsigned SubRegIdx, 507 SmallVectorImpl<RegisterMaskPair> &RegUnits) const { 508 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 509 LaneBitmask LaneMask = SubRegIdx != 0 510 ? TRI.getSubRegIndexLaneMask(SubRegIdx) 511 : MRI.getMaxLaneMaskForVReg(Reg); 512 addRegLanes(RegUnits, RegisterMaskPair(Reg, LaneMask)); 513 } else if (MRI.isAllocatable(Reg)) { 514 for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) 515 addRegLanes(RegUnits, RegisterMaskPair(*Units, ~0u)); 516 } 517 } 518 519 friend class llvm::RegisterOperands; 520 }; 521 522 } // namespace 523 524 void RegisterOperands::collect(const MachineInstr &MI, 525 const TargetRegisterInfo &TRI, 526 const MachineRegisterInfo &MRI, 527 bool TrackLaneMasks, bool IgnoreDead) { 528 RegisterOperandsCollector Collector(*this, TRI, MRI, IgnoreDead); 529 if (TrackLaneMasks) 530 Collector.collectInstrLanes(MI); 531 else 532 Collector.collectInstr(MI); 533 } 534 535 void RegisterOperands::detectDeadDefs(const MachineInstr &MI, 536 const LiveIntervals &LIS) { 537 SlotIndex SlotIdx = LIS.getInstructionIndex(MI); 538 for (auto RI = Defs.begin(); RI != Defs.end(); /*empty*/) { 539 unsigned Reg = RI->RegUnit; 540 const LiveRange *LR = getLiveRange(LIS, Reg); 541 if (LR != nullptr) { 542 LiveQueryResult LRQ = LR->Query(SlotIdx); 543 if (LRQ.isDeadDef()) { 544 // LiveIntervals knows this is a dead even though it's MachineOperand is 545 // not flagged as such. 546 DeadDefs.push_back(*RI); 547 RI = Defs.erase(RI); 548 continue; 549 } 550 } 551 ++RI; 552 } 553 } 554 555 void RegisterOperands::adjustLaneLiveness(const LiveIntervals &LIS, 556 const MachineRegisterInfo &MRI, 557 SlotIndex Pos, 558 MachineInstr *AddFlagsMI) { 559 for (auto I = Defs.begin(); I != Defs.end(); ) { 560 LaneBitmask LiveAfter = getLiveLanesAt(LIS, MRI, true, I->RegUnit, 561 Pos.getDeadSlot()); 562 // If the the def is all that is live after the instruction, then in case 563 // of a subregister def we need a read-undef flag. 564 unsigned RegUnit = I->RegUnit; 565 if (TargetRegisterInfo::isVirtualRegister(RegUnit) && 566 AddFlagsMI != nullptr && (LiveAfter & ~I->LaneMask) == 0) 567 AddFlagsMI->setRegisterDefReadUndef(RegUnit); 568 569 LaneBitmask ActualDef = I->LaneMask & LiveAfter; 570 if (ActualDef == 0) { 571 I = Defs.erase(I); 572 } else { 573 I->LaneMask = ActualDef; 574 ++I; 575 } 576 } 577 for (auto I = Uses.begin(); I != Uses.end(); ) { 578 LaneBitmask LiveBefore = getLiveLanesAt(LIS, MRI, true, I->RegUnit, 579 Pos.getBaseIndex()); 580 LaneBitmask LaneMask = I->LaneMask & LiveBefore; 581 if (LaneMask == 0) { 582 I = Uses.erase(I); 583 } else { 584 I->LaneMask = LaneMask; 585 ++I; 586 } 587 } 588 if (AddFlagsMI != nullptr) { 589 for (const RegisterMaskPair &P : DeadDefs) { 590 unsigned RegUnit = P.RegUnit; 591 if (!TargetRegisterInfo::isVirtualRegister(RegUnit)) 592 continue; 593 LaneBitmask LiveAfter = getLiveLanesAt(LIS, MRI, true, RegUnit, 594 Pos.getDeadSlot()); 595 if (LiveAfter == 0) 596 AddFlagsMI->setRegisterDefReadUndef(RegUnit); 597 } 598 } 599 } 600 601 /// Initialize an array of N PressureDiffs. 602 void PressureDiffs::init(unsigned N) { 603 Size = N; 604 if (N <= Max) { 605 memset(PDiffArray, 0, N * sizeof(PressureDiff)); 606 return; 607 } 608 Max = Size; 609 free(PDiffArray); 610 PDiffArray = reinterpret_cast<PressureDiff*>(calloc(N, sizeof(PressureDiff))); 611 } 612 613 void PressureDiffs::addInstruction(unsigned Idx, 614 const RegisterOperands &RegOpers, 615 const MachineRegisterInfo &MRI) { 616 PressureDiff &PDiff = (*this)[Idx]; 617 assert(!PDiff.begin()->isValid() && "stale PDiff"); 618 for (const RegisterMaskPair &P : RegOpers.Defs) 619 PDiff.addPressureChange(P.RegUnit, true, &MRI); 620 621 for (const RegisterMaskPair &P : RegOpers.Uses) 622 PDiff.addPressureChange(P.RegUnit, false, &MRI); 623 } 624 625 /// Add a change in pressure to the pressure diff of a given instruction. 626 void PressureDiff::addPressureChange(unsigned RegUnit, bool IsDec, 627 const MachineRegisterInfo *MRI) { 628 PSetIterator PSetI = MRI->getPressureSets(RegUnit); 629 int Weight = IsDec ? -PSetI.getWeight() : PSetI.getWeight(); 630 for (; PSetI.isValid(); ++PSetI) { 631 // Find an existing entry in the pressure diff for this PSet. 632 PressureDiff::iterator I = nonconst_begin(), E = nonconst_end(); 633 for (; I != E && I->isValid(); ++I) { 634 if (I->getPSet() >= *PSetI) 635 break; 636 } 637 // If all pressure sets are more constrained, skip the remaining PSets. 638 if (I == E) 639 break; 640 // Insert this PressureChange. 641 if (!I->isValid() || I->getPSet() != *PSetI) { 642 PressureChange PTmp = PressureChange(*PSetI); 643 for (PressureDiff::iterator J = I; J != E && PTmp.isValid(); ++J) 644 std::swap(*J, PTmp); 645 } 646 // Update the units for this pressure set. 647 unsigned NewUnitInc = I->getUnitInc() + Weight; 648 if (NewUnitInc != 0) { 649 I->setUnitInc(NewUnitInc); 650 } else { 651 // Remove entry 652 PressureDiff::iterator J; 653 for (J = std::next(I); J != E && J->isValid(); ++J, ++I) 654 *I = *J; 655 if (J != E) 656 *I = *J; 657 } 658 } 659 } 660 661 /// Force liveness of registers. 662 void RegPressureTracker::addLiveRegs(ArrayRef<RegisterMaskPair> Regs) { 663 for (const RegisterMaskPair &P : Regs) { 664 LaneBitmask PrevMask = LiveRegs.insert(P); 665 LaneBitmask NewMask = PrevMask | P.LaneMask; 666 increaseRegPressure(P.RegUnit, PrevMask, NewMask); 667 } 668 } 669 670 void RegPressureTracker::discoverLiveInOrOut(RegisterMaskPair Pair, 671 SmallVectorImpl<RegisterMaskPair> &LiveInOrOut) { 672 assert(Pair.LaneMask != 0); 673 674 unsigned RegUnit = Pair.RegUnit; 675 auto I = find_if(LiveInOrOut, [RegUnit](const RegisterMaskPair &Other) { 676 return Other.RegUnit == RegUnit; 677 }); 678 LaneBitmask PrevMask; 679 LaneBitmask NewMask; 680 if (I == LiveInOrOut.end()) { 681 PrevMask = 0; 682 NewMask = Pair.LaneMask; 683 LiveInOrOut.push_back(Pair); 684 } else { 685 PrevMask = I->LaneMask; 686 NewMask = PrevMask | Pair.LaneMask; 687 I->LaneMask = NewMask; 688 } 689 increaseSetPressure(P.MaxSetPressure, *MRI, RegUnit, PrevMask, NewMask); 690 } 691 692 void RegPressureTracker::discoverLiveIn(RegisterMaskPair Pair) { 693 discoverLiveInOrOut(Pair, P.LiveInRegs); 694 } 695 696 void RegPressureTracker::discoverLiveOut(RegisterMaskPair Pair) { 697 discoverLiveInOrOut(Pair, P.LiveOutRegs); 698 } 699 700 void RegPressureTracker::bumpDeadDefs(ArrayRef<RegisterMaskPair> DeadDefs) { 701 for (const RegisterMaskPair &P : DeadDefs) { 702 unsigned Reg = P.RegUnit; 703 LaneBitmask LiveMask = LiveRegs.contains(Reg); 704 LaneBitmask BumpedMask = LiveMask | P.LaneMask; 705 increaseRegPressure(Reg, LiveMask, BumpedMask); 706 } 707 for (const RegisterMaskPair &P : DeadDefs) { 708 unsigned Reg = P.RegUnit; 709 LaneBitmask LiveMask = LiveRegs.contains(Reg); 710 LaneBitmask BumpedMask = LiveMask | P.LaneMask; 711 decreaseRegPressure(Reg, BumpedMask, LiveMask); 712 } 713 } 714 715 /// Recede across the previous instruction. If LiveUses is provided, record any 716 /// RegUnits that are made live by the current instruction's uses. This includes 717 /// registers that are both defined and used by the instruction. If a pressure 718 /// difference pointer is provided record the changes is pressure caused by this 719 /// instruction independent of liveness. 720 void RegPressureTracker::recede(const RegisterOperands &RegOpers, 721 SmallVectorImpl<RegisterMaskPair> *LiveUses) { 722 assert(!CurrPos->isDebugValue()); 723 724 // Boost pressure for all dead defs together. 725 bumpDeadDefs(RegOpers.DeadDefs); 726 727 // Kill liveness at live defs. 728 // TODO: consider earlyclobbers? 729 for (const RegisterMaskPair &Def : RegOpers.Defs) { 730 unsigned Reg = Def.RegUnit; 731 732 LaneBitmask PreviousMask = LiveRegs.erase(Def); 733 LaneBitmask NewMask = PreviousMask & ~Def.LaneMask; 734 735 LaneBitmask LiveOut = Def.LaneMask & ~PreviousMask; 736 if (LiveOut != 0) { 737 discoverLiveOut(RegisterMaskPair(Reg, LiveOut)); 738 // Retroactively model effects on pressure of the live out lanes. 739 increaseSetPressure(CurrSetPressure, *MRI, Reg, 0, LiveOut); 740 PreviousMask = LiveOut; 741 } 742 743 if (NewMask == 0) { 744 // Add a 0 entry to LiveUses as a marker that the complete vreg has become 745 // dead. 746 if (TrackLaneMasks && LiveUses != nullptr) 747 setRegZero(*LiveUses, Reg); 748 } 749 750 decreaseRegPressure(Reg, PreviousMask, NewMask); 751 } 752 753 SlotIndex SlotIdx; 754 if (RequireIntervals) 755 SlotIdx = LIS->getInstructionIndex(*CurrPos).getRegSlot(); 756 757 // Generate liveness for uses. 758 for (const RegisterMaskPair &Use : RegOpers.Uses) { 759 unsigned Reg = Use.RegUnit; 760 assert(Use.LaneMask != 0); 761 LaneBitmask PreviousMask = LiveRegs.insert(Use); 762 LaneBitmask NewMask = PreviousMask | Use.LaneMask; 763 if (NewMask == PreviousMask) 764 continue; 765 766 // Did the register just become live? 767 if (PreviousMask == 0) { 768 if (LiveUses != nullptr) { 769 if (!TrackLaneMasks) { 770 addRegLanes(*LiveUses, RegisterMaskPair(Reg, NewMask)); 771 } else { 772 auto I = find_if(*LiveUses, [Reg](const RegisterMaskPair Other) { 773 return Other.RegUnit == Reg; 774 }); 775 bool IsRedef = I != LiveUses->end(); 776 if (IsRedef) { 777 // ignore re-defs here... 778 assert(I->LaneMask == 0); 779 removeRegLanes(*LiveUses, RegisterMaskPair(Reg, NewMask)); 780 } else { 781 addRegLanes(*LiveUses, RegisterMaskPair(Reg, NewMask)); 782 } 783 } 784 } 785 786 // Discover live outs if this may be the first occurance of this register. 787 if (RequireIntervals) { 788 LaneBitmask LiveOut = getLiveThroughAt(Reg, SlotIdx); 789 if (LiveOut != 0) 790 discoverLiveOut(RegisterMaskPair(Reg, LiveOut)); 791 } 792 } 793 794 increaseRegPressure(Reg, PreviousMask, NewMask); 795 } 796 if (TrackUntiedDefs) { 797 for (const RegisterMaskPair &Def : RegOpers.Defs) { 798 unsigned RegUnit = Def.RegUnit; 799 if (TargetRegisterInfo::isVirtualRegister(RegUnit) && 800 (LiveRegs.contains(RegUnit) & Def.LaneMask) == 0) 801 UntiedDefs.insert(RegUnit); 802 } 803 } 804 } 805 806 void RegPressureTracker::recedeSkipDebugValues() { 807 assert(CurrPos != MBB->begin()); 808 if (!isBottomClosed()) 809 closeBottom(); 810 811 // Open the top of the region using block iterators. 812 if (!RequireIntervals && isTopClosed()) 813 static_cast<RegionPressure&>(P).openTop(CurrPos); 814 815 // Find the previous instruction. 816 do 817 --CurrPos; 818 while (CurrPos != MBB->begin() && CurrPos->isDebugValue()); 819 820 SlotIndex SlotIdx; 821 if (RequireIntervals) 822 SlotIdx = LIS->getInstructionIndex(*CurrPos).getRegSlot(); 823 824 // Open the top of the region using slot indexes. 825 if (RequireIntervals && isTopClosed()) 826 static_cast<IntervalPressure&>(P).openTop(SlotIdx); 827 } 828 829 void RegPressureTracker::recede(SmallVectorImpl<RegisterMaskPair> *LiveUses) { 830 recedeSkipDebugValues(); 831 832 const MachineInstr &MI = *CurrPos; 833 RegisterOperands RegOpers; 834 RegOpers.collect(MI, *TRI, *MRI, TrackLaneMasks, false); 835 if (TrackLaneMasks) { 836 SlotIndex SlotIdx = LIS->getInstructionIndex(*CurrPos).getRegSlot(); 837 RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx); 838 } else if (RequireIntervals) { 839 RegOpers.detectDeadDefs(MI, *LIS); 840 } 841 842 recede(RegOpers, LiveUses); 843 } 844 845 /// Advance across the current instruction. 846 void RegPressureTracker::advance(const RegisterOperands &RegOpers) { 847 assert(!TrackUntiedDefs && "unsupported mode"); 848 assert(CurrPos != MBB->end()); 849 if (!isTopClosed()) 850 closeTop(); 851 852 SlotIndex SlotIdx; 853 if (RequireIntervals) 854 SlotIdx = getCurrSlot(); 855 856 // Open the bottom of the region using slot indexes. 857 if (isBottomClosed()) { 858 if (RequireIntervals) 859 static_cast<IntervalPressure&>(P).openBottom(SlotIdx); 860 else 861 static_cast<RegionPressure&>(P).openBottom(CurrPos); 862 } 863 864 for (const RegisterMaskPair &Use : RegOpers.Uses) { 865 unsigned Reg = Use.RegUnit; 866 LaneBitmask LiveMask = LiveRegs.contains(Reg); 867 LaneBitmask LiveIn = Use.LaneMask & ~LiveMask; 868 if (LiveIn != 0) { 869 discoverLiveIn(RegisterMaskPair(Reg, LiveIn)); 870 increaseRegPressure(Reg, LiveMask, LiveMask | LiveIn); 871 LiveRegs.insert(RegisterMaskPair(Reg, LiveIn)); 872 } 873 // Kill liveness at last uses. 874 if (RequireIntervals) { 875 LaneBitmask LastUseMask = getLastUsedLanes(Reg, SlotIdx); 876 if (LastUseMask != 0) { 877 LiveRegs.erase(RegisterMaskPair(Reg, LastUseMask)); 878 decreaseRegPressure(Reg, LiveMask, LiveMask & ~LastUseMask); 879 } 880 } 881 } 882 883 // Generate liveness for defs. 884 for (const RegisterMaskPair &Def : RegOpers.Defs) { 885 LaneBitmask PreviousMask = LiveRegs.insert(Def); 886 LaneBitmask NewMask = PreviousMask | Def.LaneMask; 887 increaseRegPressure(Def.RegUnit, PreviousMask, NewMask); 888 } 889 890 // Boost pressure for all dead defs together. 891 bumpDeadDefs(RegOpers.DeadDefs); 892 893 // Find the next instruction. 894 do 895 ++CurrPos; 896 while (CurrPos != MBB->end() && CurrPos->isDebugValue()); 897 } 898 899 void RegPressureTracker::advance() { 900 const MachineInstr &MI = *CurrPos; 901 RegisterOperands RegOpers; 902 RegOpers.collect(MI, *TRI, *MRI, TrackLaneMasks, false); 903 if (TrackLaneMasks) { 904 SlotIndex SlotIdx = getCurrSlot(); 905 RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx); 906 } 907 advance(RegOpers); 908 } 909 910 /// Find the max change in excess pressure across all sets. 911 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec, 912 ArrayRef<unsigned> NewPressureVec, 913 RegPressureDelta &Delta, 914 const RegisterClassInfo *RCI, 915 ArrayRef<unsigned> LiveThruPressureVec) { 916 Delta.Excess = PressureChange(); 917 for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) { 918 unsigned POld = OldPressureVec[i]; 919 unsigned PNew = NewPressureVec[i]; 920 int PDiff = (int)PNew - (int)POld; 921 if (!PDiff) // No change in this set in the common case. 922 continue; 923 // Only consider change beyond the limit. 924 unsigned Limit = RCI->getRegPressureSetLimit(i); 925 if (!LiveThruPressureVec.empty()) 926 Limit += LiveThruPressureVec[i]; 927 928 if (Limit > POld) { 929 if (Limit > PNew) 930 PDiff = 0; // Under the limit 931 else 932 PDiff = PNew - Limit; // Just exceeded limit. 933 } else if (Limit > PNew) 934 PDiff = Limit - POld; // Just obeyed limit. 935 936 if (PDiff) { 937 Delta.Excess = PressureChange(i); 938 Delta.Excess.setUnitInc(PDiff); 939 break; 940 } 941 } 942 } 943 944 /// Find the max change in max pressure that either surpasses a critical PSet 945 /// limit or exceeds the current MaxPressureLimit. 946 /// 947 /// FIXME: comparing each element of the old and new MaxPressure vectors here is 948 /// silly. It's done now to demonstrate the concept but will go away with a 949 /// RegPressureTracker API change to work with pressure differences. 950 static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec, 951 ArrayRef<unsigned> NewMaxPressureVec, 952 ArrayRef<PressureChange> CriticalPSets, 953 ArrayRef<unsigned> MaxPressureLimit, 954 RegPressureDelta &Delta) { 955 Delta.CriticalMax = PressureChange(); 956 Delta.CurrentMax = PressureChange(); 957 958 unsigned CritIdx = 0, CritEnd = CriticalPSets.size(); 959 for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) { 960 unsigned POld = OldMaxPressureVec[i]; 961 unsigned PNew = NewMaxPressureVec[i]; 962 if (PNew == POld) // No change in this set in the common case. 963 continue; 964 965 if (!Delta.CriticalMax.isValid()) { 966 while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < i) 967 ++CritIdx; 968 969 if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == i) { 970 int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].getUnitInc(); 971 if (PDiff > 0) { 972 Delta.CriticalMax = PressureChange(i); 973 Delta.CriticalMax.setUnitInc(PDiff); 974 } 975 } 976 } 977 // Find the first increase above MaxPressureLimit. 978 // (Ignores negative MDiff). 979 if (!Delta.CurrentMax.isValid() && PNew > MaxPressureLimit[i]) { 980 Delta.CurrentMax = PressureChange(i); 981 Delta.CurrentMax.setUnitInc(PNew - POld); 982 if (CritIdx == CritEnd || Delta.CriticalMax.isValid()) 983 break; 984 } 985 } 986 } 987 988 /// Record the upward impact of a single instruction on current register 989 /// pressure. Unlike the advance/recede pressure tracking interface, this does 990 /// not discover live in/outs. 991 /// 992 /// This is intended for speculative queries. It leaves pressure inconsistent 993 /// with the current position, so must be restored by the caller. 994 void RegPressureTracker::bumpUpwardPressure(const MachineInstr *MI) { 995 assert(!MI->isDebugValue() && "Expect a nondebug instruction."); 996 997 SlotIndex SlotIdx; 998 if (RequireIntervals) 999 SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot(); 1000 1001 // Account for register pressure similar to RegPressureTracker::recede(). 1002 RegisterOperands RegOpers; 1003 RegOpers.collect(*MI, *TRI, *MRI, TrackLaneMasks, /*IgnoreDead=*/true); 1004 assert(RegOpers.DeadDefs.size() == 0); 1005 if (TrackLaneMasks) 1006 RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx); 1007 else if (RequireIntervals) 1008 RegOpers.detectDeadDefs(*MI, *LIS); 1009 1010 // Boost max pressure for all dead defs together. 1011 // Since CurrSetPressure and MaxSetPressure 1012 bumpDeadDefs(RegOpers.DeadDefs); 1013 1014 // Kill liveness at live defs. 1015 for (const RegisterMaskPair &P : RegOpers.Defs) { 1016 unsigned Reg = P.RegUnit; 1017 LaneBitmask LiveLanes = LiveRegs.contains(Reg); 1018 LaneBitmask UseLanes = getRegLanes(RegOpers.Uses, Reg); 1019 LaneBitmask DefLanes = P.LaneMask; 1020 LaneBitmask LiveAfter = (LiveLanes & ~DefLanes) | UseLanes; 1021 decreaseRegPressure(Reg, LiveLanes, LiveAfter); 1022 } 1023 // Generate liveness for uses. 1024 for (const RegisterMaskPair &P : RegOpers.Uses) { 1025 unsigned Reg = P.RegUnit; 1026 LaneBitmask LiveLanes = LiveRegs.contains(Reg); 1027 LaneBitmask LiveAfter = LiveLanes | P.LaneMask; 1028 increaseRegPressure(Reg, LiveLanes, LiveAfter); 1029 } 1030 } 1031 1032 /// Consider the pressure increase caused by traversing this instruction 1033 /// bottom-up. Find the pressure set with the most change beyond its pressure 1034 /// limit based on the tracker's current pressure, and return the change in 1035 /// number of register units of that pressure set introduced by this 1036 /// instruction. 1037 /// 1038 /// This assumes that the current LiveOut set is sufficient. 1039 /// 1040 /// This is expensive for an on-the-fly query because it calls 1041 /// bumpUpwardPressure to recompute the pressure sets based on current 1042 /// liveness. This mainly exists to verify correctness, e.g. with 1043 /// -verify-misched. getUpwardPressureDelta is the fast version of this query 1044 /// that uses the per-SUnit cache of the PressureDiff. 1045 void RegPressureTracker:: 1046 getMaxUpwardPressureDelta(const MachineInstr *MI, PressureDiff *PDiff, 1047 RegPressureDelta &Delta, 1048 ArrayRef<PressureChange> CriticalPSets, 1049 ArrayRef<unsigned> MaxPressureLimit) { 1050 // Snapshot Pressure. 1051 // FIXME: The snapshot heap space should persist. But I'm planning to 1052 // summarize the pressure effect so we don't need to snapshot at all. 1053 std::vector<unsigned> SavedPressure = CurrSetPressure; 1054 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure; 1055 1056 bumpUpwardPressure(MI); 1057 1058 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI, 1059 LiveThruPressure); 1060 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets, 1061 MaxPressureLimit, Delta); 1062 assert(Delta.CriticalMax.getUnitInc() >= 0 && 1063 Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure"); 1064 1065 // Restore the tracker's state. 1066 P.MaxSetPressure.swap(SavedMaxPressure); 1067 CurrSetPressure.swap(SavedPressure); 1068 1069 #ifndef NDEBUG 1070 if (!PDiff) 1071 return; 1072 1073 // Check if the alternate algorithm yields the same result. 1074 RegPressureDelta Delta2; 1075 getUpwardPressureDelta(MI, *PDiff, Delta2, CriticalPSets, MaxPressureLimit); 1076 if (Delta != Delta2) { 1077 dbgs() << "PDiff: "; 1078 PDiff->dump(*TRI); 1079 dbgs() << "DELTA: " << *MI; 1080 if (Delta.Excess.isValid()) 1081 dbgs() << "Excess1 " << TRI->getRegPressureSetName(Delta.Excess.getPSet()) 1082 << " " << Delta.Excess.getUnitInc() << "\n"; 1083 if (Delta.CriticalMax.isValid()) 1084 dbgs() << "Critic1 " << TRI->getRegPressureSetName(Delta.CriticalMax.getPSet()) 1085 << " " << Delta.CriticalMax.getUnitInc() << "\n"; 1086 if (Delta.CurrentMax.isValid()) 1087 dbgs() << "CurrMx1 " << TRI->getRegPressureSetName(Delta.CurrentMax.getPSet()) 1088 << " " << Delta.CurrentMax.getUnitInc() << "\n"; 1089 if (Delta2.Excess.isValid()) 1090 dbgs() << "Excess2 " << TRI->getRegPressureSetName(Delta2.Excess.getPSet()) 1091 << " " << Delta2.Excess.getUnitInc() << "\n"; 1092 if (Delta2.CriticalMax.isValid()) 1093 dbgs() << "Critic2 " << TRI->getRegPressureSetName(Delta2.CriticalMax.getPSet()) 1094 << " " << Delta2.CriticalMax.getUnitInc() << "\n"; 1095 if (Delta2.CurrentMax.isValid()) 1096 dbgs() << "CurrMx2 " << TRI->getRegPressureSetName(Delta2.CurrentMax.getPSet()) 1097 << " " << Delta2.CurrentMax.getUnitInc() << "\n"; 1098 llvm_unreachable("RegP Delta Mismatch"); 1099 } 1100 #endif 1101 } 1102 1103 /// This is the fast version of querying register pressure that does not 1104 /// directly depend on current liveness. 1105 /// 1106 /// @param Delta captures information needed for heuristics. 1107 /// 1108 /// @param CriticalPSets Are the pressure sets that are known to exceed some 1109 /// limit within the region, not necessarily at the current position. 1110 /// 1111 /// @param MaxPressureLimit Is the max pressure within the region, not 1112 /// necessarily at the current position. 1113 void RegPressureTracker:: 1114 getUpwardPressureDelta(const MachineInstr *MI, /*const*/ PressureDiff &PDiff, 1115 RegPressureDelta &Delta, 1116 ArrayRef<PressureChange> CriticalPSets, 1117 ArrayRef<unsigned> MaxPressureLimit) const { 1118 unsigned CritIdx = 0, CritEnd = CriticalPSets.size(); 1119 for (PressureDiff::const_iterator 1120 PDiffI = PDiff.begin(), PDiffE = PDiff.end(); 1121 PDiffI != PDiffE && PDiffI->isValid(); ++PDiffI) { 1122 1123 unsigned PSetID = PDiffI->getPSet(); 1124 unsigned Limit = RCI->getRegPressureSetLimit(PSetID); 1125 if (!LiveThruPressure.empty()) 1126 Limit += LiveThruPressure[PSetID]; 1127 1128 unsigned POld = CurrSetPressure[PSetID]; 1129 unsigned MOld = P.MaxSetPressure[PSetID]; 1130 unsigned MNew = MOld; 1131 // Ignore DeadDefs here because they aren't captured by PressureChange. 1132 unsigned PNew = POld + PDiffI->getUnitInc(); 1133 assert((PDiffI->getUnitInc() >= 0) == (PNew >= POld) 1134 && "PSet overflow/underflow"); 1135 if (PNew > MOld) 1136 MNew = PNew; 1137 // Check if current pressure has exceeded the limit. 1138 if (!Delta.Excess.isValid()) { 1139 unsigned ExcessInc = 0; 1140 if (PNew > Limit) 1141 ExcessInc = POld > Limit ? PNew - POld : PNew - Limit; 1142 else if (POld > Limit) 1143 ExcessInc = Limit - POld; 1144 if (ExcessInc) { 1145 Delta.Excess = PressureChange(PSetID); 1146 Delta.Excess.setUnitInc(ExcessInc); 1147 } 1148 } 1149 // Check if max pressure has exceeded a critical pressure set max. 1150 if (MNew == MOld) 1151 continue; 1152 if (!Delta.CriticalMax.isValid()) { 1153 while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < PSetID) 1154 ++CritIdx; 1155 1156 if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == PSetID) { 1157 int CritInc = (int)MNew - (int)CriticalPSets[CritIdx].getUnitInc(); 1158 if (CritInc > 0 && CritInc <= INT16_MAX) { 1159 Delta.CriticalMax = PressureChange(PSetID); 1160 Delta.CriticalMax.setUnitInc(CritInc); 1161 } 1162 } 1163 } 1164 // Check if max pressure has exceeded the current max. 1165 if (!Delta.CurrentMax.isValid() && MNew > MaxPressureLimit[PSetID]) { 1166 Delta.CurrentMax = PressureChange(PSetID); 1167 Delta.CurrentMax.setUnitInc(MNew - MOld); 1168 } 1169 } 1170 } 1171 1172 /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx). 1173 /// The query starts with a lane bitmask which gets lanes/bits removed for every 1174 /// use we find. 1175 static LaneBitmask findUseBetween(unsigned Reg, LaneBitmask LastUseMask, 1176 SlotIndex PriorUseIdx, SlotIndex NextUseIdx, 1177 const MachineRegisterInfo &MRI, 1178 const LiveIntervals *LIS) { 1179 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 1180 for (const MachineOperand &MO : MRI.use_nodbg_operands(Reg)) { 1181 if (MO.isUndef()) 1182 continue; 1183 const MachineInstr *MI = MO.getParent(); 1184 SlotIndex InstSlot = LIS->getInstructionIndex(*MI).getRegSlot(); 1185 if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx) { 1186 unsigned SubRegIdx = MO.getSubReg(); 1187 LaneBitmask UseMask = TRI.getSubRegIndexLaneMask(SubRegIdx); 1188 LastUseMask &= ~UseMask; 1189 if (LastUseMask == 0) 1190 return 0; 1191 } 1192 } 1193 return LastUseMask; 1194 } 1195 1196 LaneBitmask RegPressureTracker::getLiveLanesAt(unsigned RegUnit, 1197 SlotIndex Pos) const { 1198 assert(RequireIntervals); 1199 return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, Pos, ~0u, 1200 [](const LiveRange &LR, SlotIndex Pos) { 1201 return LR.liveAt(Pos); 1202 }); 1203 } 1204 1205 LaneBitmask RegPressureTracker::getLastUsedLanes(unsigned RegUnit, 1206 SlotIndex Pos) const { 1207 assert(RequireIntervals); 1208 return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, 1209 Pos.getBaseIndex(), 0, 1210 [](const LiveRange &LR, SlotIndex Pos) { 1211 const LiveRange::Segment *S = LR.getSegmentContaining(Pos); 1212 return S != nullptr && S->end == Pos.getRegSlot(); 1213 }); 1214 } 1215 1216 LaneBitmask RegPressureTracker::getLiveThroughAt(unsigned RegUnit, 1217 SlotIndex Pos) const { 1218 assert(RequireIntervals); 1219 return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, Pos, 0u, 1220 [](const LiveRange &LR, SlotIndex Pos) { 1221 const LiveRange::Segment *S = LR.getSegmentContaining(Pos); 1222 return S != nullptr && S->start < Pos.getRegSlot(true) && 1223 S->end != Pos.getDeadSlot(); 1224 }); 1225 } 1226 1227 /// Record the downward impact of a single instruction on current register 1228 /// pressure. Unlike the advance/recede pressure tracking interface, this does 1229 /// not discover live in/outs. 1230 /// 1231 /// This is intended for speculative queries. It leaves pressure inconsistent 1232 /// with the current position, so must be restored by the caller. 1233 void RegPressureTracker::bumpDownwardPressure(const MachineInstr *MI) { 1234 assert(!MI->isDebugValue() && "Expect a nondebug instruction."); 1235 1236 SlotIndex SlotIdx; 1237 if (RequireIntervals) 1238 SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot(); 1239 1240 // Account for register pressure similar to RegPressureTracker::recede(). 1241 RegisterOperands RegOpers; 1242 RegOpers.collect(*MI, *TRI, *MRI, TrackLaneMasks, false); 1243 if (TrackLaneMasks) 1244 RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx); 1245 1246 if (RequireIntervals) { 1247 for (const RegisterMaskPair &Use : RegOpers.Uses) { 1248 unsigned Reg = Use.RegUnit; 1249 LaneBitmask LastUseMask = getLastUsedLanes(Reg, SlotIdx); 1250 if (LastUseMask == 0) 1251 continue; 1252 // The LastUseMask is queried from the liveness information of instruction 1253 // which may be further down the schedule. Some lanes may actually not be 1254 // last uses for the current position. 1255 // FIXME: allow the caller to pass in the list of vreg uses that remain 1256 // to be bottom-scheduled to avoid searching uses at each query. 1257 SlotIndex CurrIdx = getCurrSlot(); 1258 LastUseMask 1259 = findUseBetween(Reg, LastUseMask, CurrIdx, SlotIdx, *MRI, LIS); 1260 if (LastUseMask == 0) 1261 continue; 1262 1263 LaneBitmask LiveMask = LiveRegs.contains(Reg); 1264 LaneBitmask NewMask = LiveMask & ~LastUseMask; 1265 decreaseRegPressure(Reg, LiveMask, NewMask); 1266 } 1267 } 1268 1269 // Generate liveness for defs. 1270 for (const RegisterMaskPair &Def : RegOpers.Defs) { 1271 unsigned Reg = Def.RegUnit; 1272 LaneBitmask LiveMask = LiveRegs.contains(Reg); 1273 LaneBitmask NewMask = LiveMask | Def.LaneMask; 1274 increaseRegPressure(Reg, LiveMask, NewMask); 1275 } 1276 1277 // Boost pressure for all dead defs together. 1278 bumpDeadDefs(RegOpers.DeadDefs); 1279 } 1280 1281 /// Consider the pressure increase caused by traversing this instruction 1282 /// top-down. Find the register class with the most change in its pressure limit 1283 /// based on the tracker's current pressure, and return the number of excess 1284 /// register units of that pressure set introduced by this instruction. 1285 /// 1286 /// This assumes that the current LiveIn set is sufficient. 1287 /// 1288 /// This is expensive for an on-the-fly query because it calls 1289 /// bumpDownwardPressure to recompute the pressure sets based on current 1290 /// liveness. We don't yet have a fast version of downward pressure tracking 1291 /// analogous to getUpwardPressureDelta. 1292 void RegPressureTracker:: 1293 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta, 1294 ArrayRef<PressureChange> CriticalPSets, 1295 ArrayRef<unsigned> MaxPressureLimit) { 1296 // Snapshot Pressure. 1297 std::vector<unsigned> SavedPressure = CurrSetPressure; 1298 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure; 1299 1300 bumpDownwardPressure(MI); 1301 1302 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI, 1303 LiveThruPressure); 1304 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets, 1305 MaxPressureLimit, Delta); 1306 assert(Delta.CriticalMax.getUnitInc() >= 0 && 1307 Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure"); 1308 1309 // Restore the tracker's state. 1310 P.MaxSetPressure.swap(SavedMaxPressure); 1311 CurrSetPressure.swap(SavedPressure); 1312 } 1313 1314 /// Get the pressure of each PSet after traversing this instruction bottom-up. 1315 void RegPressureTracker:: 1316 getUpwardPressure(const MachineInstr *MI, 1317 std::vector<unsigned> &PressureResult, 1318 std::vector<unsigned> &MaxPressureResult) { 1319 // Snapshot pressure. 1320 PressureResult = CurrSetPressure; 1321 MaxPressureResult = P.MaxSetPressure; 1322 1323 bumpUpwardPressure(MI); 1324 1325 // Current pressure becomes the result. Restore current pressure. 1326 P.MaxSetPressure.swap(MaxPressureResult); 1327 CurrSetPressure.swap(PressureResult); 1328 } 1329 1330 /// Get the pressure of each PSet after traversing this instruction top-down. 1331 void RegPressureTracker:: 1332 getDownwardPressure(const MachineInstr *MI, 1333 std::vector<unsigned> &PressureResult, 1334 std::vector<unsigned> &MaxPressureResult) { 1335 // Snapshot pressure. 1336 PressureResult = CurrSetPressure; 1337 MaxPressureResult = P.MaxSetPressure; 1338 1339 bumpDownwardPressure(MI); 1340 1341 // Current pressure becomes the result. Restore current pressure. 1342 P.MaxSetPressure.swap(MaxPressureResult); 1343 CurrSetPressure.swap(PressureResult); 1344 } 1345