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