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