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