xref: /llvm-project/llvm/lib/CodeGen/RegisterPressure.cpp (revision 9e6494c0fb29dfb5d4d2b7bf3ed7af261efee034)
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 
14 #include "llvm/CodeGen/RegisterPressure.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/CodeGen/LiveInterval.h"
19 #include "llvm/CodeGen/LiveIntervals.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineInstrBundle.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/RegisterClassInfo.h"
27 #include "llvm/CodeGen/SlotIndexes.h"
28 #include "llvm/CodeGen/TargetRegisterInfo.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/MC/LaneBitmask.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.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 
46 using namespace llvm;
47 
48 /// Increase pressure for each pressure set provided by TargetRegisterInfo.
49 static 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.
63 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure,
64                                 const MachineRegisterInfo &MRI, Register Reg,
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)
79 LLVM_DUMP_METHOD
80 void llvm::dumpRegSetPressure(ArrayRef<unsigned> SetPressure,
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 
93 LLVM_DUMP_METHOD
94 void RegisterPressure::dump(const TargetRegisterInfo *TRI) const {
95   dbgs() << "Max Pressure: ";
96   dumpRegSetPressure(MaxSetPressure, TRI);
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 
115 LLVM_DUMP_METHOD
116 void RegPressureTracker::dump() const {
117   if (!isTopClosed() || !isBottomClosed()) {
118     dbgs() << "Curr Pressure: ";
119     dumpRegSetPressure(CurrSetPressure, TRI);
120   }
121   P.dump(TRI);
122 }
123 
124 LLVM_DUMP_METHOD
125 void PressureDiff::dump(const TargetRegisterInfo &TRI) const {
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 
137 LLVM_DUMP_METHOD
138 void PressureChange::dump() const {
139   dbgs() << "[" << getPSetOrMax() << ", " << getUnitInc() << "]\n";
140 }
141 
142 void RegPressureDelta::dump() const {
143   dbgs() << "[Excess=";
144   Excess.dump();
145   dbgs() << ", CriticalMax=";
146   CriticalMax.dump();
147   dbgs() << ", CurrentMax=";
148   CurrentMax.dump();
149   dbgs() << "]\n";
150 }
151 
152 #endif
153 
154 void RegPressureTracker::increaseRegPressure(Register RegUnit,
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 
169 void RegPressureTracker::decreaseRegPressure(Register RegUnit,
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.
176 void IntervalPressure::reset() {
177   TopIdx = BottomIdx = SlotIndex();
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.
184 void RegionPressure::reset() {
185   TopPos = BottomPos = MachineBasicBlock::const_iterator();
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.
193 void IntervalPressure::openTop(SlotIndex NextTop) {
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.
201 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) {
202   if (TopPos != PrevTop)
203     return;
204   TopPos = MachineBasicBlock::const_iterator();
205   LiveInRegs.clear();
206 }
207 
208 /// If the current bottom is not greater than the previous index, open it.
209 void IntervalPressure::openBottom(SlotIndex PrevBottom) {
210   if (BottomIdx > PrevBottom)
211     return;
212   BottomIdx = SlotIndex();
213   LiveInRegs.clear();
214 }
215 
216 /// If the current bottom is the previous instr (before advancing), open it.
217 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) {
218   if (BottomPos != PrevBottom)
219     return;
220   BottomPos = MachineBasicBlock::const_iterator();
221   LiveInRegs.clear();
222 }
223 
224 void LiveRegSet::init(const MachineRegisterInfo &MRI) {
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 
232 void LiveRegSet::clear() {
233   Regs.clear();
234 }
235 
236 static const LiveRange *getLiveRange(const LiveIntervals &LIS, unsigned Reg) {
237   if (Register::isVirtualRegister(Reg))
238     return &LIS.getInterval(Reg);
239   return LIS.getCachedRegUnit(Reg);
240 }
241 
242 void RegPressureTracker::reset() {
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.
262 void RegPressureTracker::init(const MachineFunction *mf,
263                               const RegisterClassInfo *rci,
264                               const LiveIntervals *lis,
265                               const MachineBasicBlock *mbb,
266                               MachineBasicBlock::const_iterator pos,
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.
294 bool RegPressureTracker::isTopClosed() const {
295   if (RequireIntervals)
296     return static_cast<IntervalPressure&>(P).TopIdx.isValid();
297   return (static_cast<RegionPressure&>(P).TopPos ==
298           MachineBasicBlock::const_iterator());
299 }
300 
301 /// Does this pressure result have a valid bottom position and live outs.
302 bool RegPressureTracker::isBottomClosed() const {
303   if (RequireIntervals)
304     return static_cast<IntervalPressure&>(P).BottomIdx.isValid();
305   return (static_cast<RegionPressure&>(P).BottomPos ==
306           MachineBasicBlock::const_iterator());
307 }
308 
309 SlotIndex RegPressureTracker::getCurrSlot() const {
310   MachineBasicBlock::const_iterator IdxPos =
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.
318 void RegPressureTracker::closeTop() {
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.
330 void RegPressureTracker::closeBottom() {
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.
342 void RegPressureTracker::closeRegion() {
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.
358 void RegPressureTracker::initLiveThru(const RegPressureTracker &RPTracker) {
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,
365                           LaneBitmask::getNone(), Pair.LaneMask);
366   }
367 }
368 
369 static LaneBitmask getRegLanes(ArrayRef<VRegMaskOrUnit> RegUnits,
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 
379 static void addRegLanes(SmallVectorImpl<VRegMaskOrUnit> &RegUnits,
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 
393 static void setRegZero(SmallVectorImpl<VRegMaskOrUnit> &RegUnits,
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 
405 static void removeRegLanes(SmallVectorImpl<VRegMaskOrUnit> &RegUnits,
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 
419 static LaneBitmask
420 getLanesWithProperty(const LiveIntervals &LIS, const MachineRegisterInfo &MRI,
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)
434                               : LaneBitmask::getAll();
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 
448 static LaneBitmask getLiveLanesAt(const LiveIntervals &LIS,
449                                   const MachineRegisterInfo &MRI,
450                                   bool TrackLaneMasks, Register RegUnit,
451                                   SlotIndex Pos) {
452   return getLanesWithProperty(LIS, MRI, TrackLaneMasks, RegUnit, Pos,
453                               LaneBitmask::getAll(),
454                               [](const LiveRange &LR, SlotIndex Pos) {
455                                 return LR.liveAt(Pos);
456                               });
457 }
458 
459 namespace {
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
465 class RegisterOperandsCollector {
466   friend class llvm::RegisterOperands;
467 
468   RegisterOperands &RegOpers;
469   const TargetRegisterInfo &TRI;
470   const MachineRegisterInfo &MRI;
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()) {
520       addRegLanes(RegUnits, VRegMaskOrUnit(Reg, LaneBitmask::getAll()));
521     } else if (MRI.isAllocatable(Reg)) {
522       for (MCRegUnit Unit : TRI.regunits(Reg.asMCReg()))
523         addRegLanes(RegUnits, VRegMaskOrUnit(Unit, LaneBitmask::getAll()));
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()))
558         addRegLanes(RegUnits, VRegMaskOrUnit(Unit, LaneBitmask::getAll()));
559     }
560   }
561 };
562 
563 } // end anonymous namespace
564 
565 void RegisterOperands::collect(const MachineInstr &MI,
566                                const TargetRegisterInfo &TRI,
567                                const MachineRegisterInfo &MRI,
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 
576 void RegisterOperands::detectDeadDefs(const MachineInstr &MI,
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 
596 void RegisterOperands::adjustLaneLiveness(const LiveIntervals &LIS,
597                                           const MachineRegisterInfo &MRI,
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.
637 void 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 
648 void PressureDiffs::addInstruction(unsigned Idx,
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.
661 void PressureDiff::addPressureChange(Register RegUnit, bool IsDec,
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
687       PressureDiff::iterator J;
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.
696 void RegPressureTracker::addLiveRegs(ArrayRef<VRegMaskOrUnit> Regs) {
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 
704 void RegPressureTracker::discoverLiveInOrOut(
705     VRegMaskOrUnit Pair, SmallVectorImpl<VRegMaskOrUnit> &LiveInOrOut) {
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 
726 void RegPressureTracker::discoverLiveIn(VRegMaskOrUnit Pair) {
727   discoverLiveInOrOut(Pair, P.LiveInRegs);
728 }
729 
730 void RegPressureTracker::discoverLiveOut(VRegMaskOrUnit Pair) {
731   discoverLiveInOrOut(Pair, P.LiveOutRegs);
732 }
733 
734 void RegPressureTracker::bumpDeadDefs(ArrayRef<VRegMaskOrUnit> DeadDefs) {
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.
754 void RegPressureTracker::recede(const RegisterOperands &RegOpers,
755                                 SmallVectorImpl<VRegMaskOrUnit> *LiveUses) {
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()) {
771       discoverLiveOut(VRegMaskOrUnit(Reg, LiveOut));
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())
825           discoverLiveOut(VRegMaskOrUnit(Reg, LiveOut));
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 
841 void RegPressureTracker::recedeSkipDebugValues() {
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 
862 void RegPressureTracker::recede(SmallVectorImpl<VRegMaskOrUnit> *LiveUses) {
863   recedeSkipDebugValues();
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.
885 void RegPressureTracker::advance(const RegisterOperands &RegOpers) {
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()) {
908       discoverLiveIn(VRegMaskOrUnit(Reg, LiveIn));
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 
936 void RegPressureTracker::advance() {
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.
948 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec,
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.
987 static 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.
1031 void RegPressureTracker::bumpUpwardPressure(const MachineInstr *MI) {
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.
1085 void RegPressureTracker::
1086 getMaxUpwardPressureDelta(const MachineInstr *MI, PressureDiff *PDiff,
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 
1096   bumpUpwardPressure(MI);
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.
1153 void RegPressureTracker::
1154 getUpwardPressureDelta(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();
1159   for (PressureDiff::const_iterator
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.
1215 static 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 
1236 LaneBitmask RegPressureTracker::getLiveLanesAt(Register RegUnit,
1237                                                SlotIndex Pos) const {
1238   assert(RequireIntervals);
1239   return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, Pos,
1240                               LaneBitmask::getAll(),
1241       [](const LiveRange &LR, SlotIndex Pos) {
1242         return LR.liveAt(Pos);
1243       });
1244 }
1245 
1246 LaneBitmask RegPressureTracker::getLastUsedLanes(Register RegUnit,
1247                                                  SlotIndex Pos) const {
1248   assert(RequireIntervals);
1249   return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit,
1250                               Pos.getBaseIndex(), LaneBitmask::getNone(),
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 
1257 LaneBitmask RegPressureTracker::getLiveThroughAt(Register RegUnit,
1258                                                  SlotIndex Pos) const {
1259   assert(RequireIntervals);
1260   return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, Pos,
1261                               LaneBitmask::getNone(),
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.
1275 void RegPressureTracker::bumpDownwardPressure(const MachineInstr *MI) {
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.
1334 void RegPressureTracker::
1335 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
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 
1342   bumpDownwardPressure(MI);
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.
1357 void RegPressureTracker::
1358 getUpwardPressure(const MachineInstr *MI,
1359                   std::vector<unsigned> &PressureResult,
1360                   std::vector<unsigned> &MaxPressureResult) {
1361   // Snapshot pressure.
1362   PressureResult = CurrSetPressure;
1363   MaxPressureResult = P.MaxSetPressure;
1364 
1365   bumpUpwardPressure(MI);
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.
1373 void RegPressureTracker::
1374 getDownwardPressure(const MachineInstr *MI,
1375                     std::vector<unsigned> &PressureResult,
1376                     std::vector<unsigned> &MaxPressureResult) {
1377   // Snapshot pressure.
1378   PressureResult = CurrSetPressure;
1379   MaxPressureResult = P.MaxSetPressure;
1380 
1381   bumpDownwardPressure(MI);
1382 
1383   // Current pressure becomes the result. Restore current pressure.
1384   P.MaxSetPressure.swap(MaxPressureResult);
1385   CurrSetPressure.swap(PressureResult);
1386 }
1387