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