1 //===-- RegisterPressure.cpp - Dynamic Register Pressure ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the RegisterPressure class which can be used to track
11 // MachineInstr level register pressure.
13 //===----------------------------------------------------------------------===//
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 #include "llvm/Target/TargetMachine.h"
26 /// Increase pressure for each pressure set provided by TargetRegisterInfo.
27 static void increaseSetPressure(std::vector<unsigned> &CurrSetPressure,
29 unsigned Weight = PSetI.getWeight();
30 for (; PSetI.isValid(); ++PSetI)
31 CurrSetPressure[*PSetI] += Weight;
34 /// Decrease pressure for each pressure set provided by TargetRegisterInfo.
35 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure,
37 unsigned Weight = PSetI.getWeight();
38 for (; PSetI.isValid(); ++PSetI) {
39 assert(CurrSetPressure[*PSetI] >= Weight && "register pressure underflow");
40 CurrSetPressure[*PSetI] -= Weight;
44 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
45 void llvm::dumpRegSetPressure(ArrayRef<unsigned> SetPressure,
46 const TargetRegisterInfo *TRI) {
48 for (unsigned i = 0, e = SetPressure.size(); i < e; ++i) {
49 if (SetPressure[i] != 0) {
50 dbgs() << TRI->getRegPressureSetName(i) << "=" << SetPressure[i] << '\n';
58 void RegisterPressure::dump(const TargetRegisterInfo *TRI) const {
59 dbgs() << "Max Pressure: ";
60 dumpRegSetPressure(MaxSetPressure, TRI);
61 dbgs() << "Live In: ";
62 for (unsigned i = 0, e = LiveInRegs.size(); i < e; ++i)
63 dbgs() << PrintReg(LiveInRegs[i], TRI) << " ";
65 dbgs() << "Live Out: ";
66 for (unsigned i = 0, e = LiveOutRegs.size(); i < e; ++i)
67 dbgs() << PrintReg(LiveOutRegs[i], TRI) << " ";
71 void RegPressureTracker::dump() const {
72 if (!isTopClosed() || !isBottomClosed()) {
73 dbgs() << "Curr Pressure: ";
74 dumpRegSetPressure(CurrSetPressure, TRI);
80 /// Increase the current pressure as impacted by these registers and bump
81 /// the high water mark if needed.
82 void RegPressureTracker::increaseRegPressure(ArrayRef<unsigned> RegUnits) {
83 for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
84 PSetIterator PSetI = MRI->getPressureSets(RegUnits[i]);
85 unsigned Weight = PSetI.getWeight();
86 for (; PSetI.isValid(); ++PSetI) {
87 CurrSetPressure[*PSetI] += Weight;
88 if (CurrSetPressure[*PSetI] > P.MaxSetPressure[*PSetI]) {
89 P.MaxSetPressure[*PSetI] = CurrSetPressure[*PSetI];
95 /// Simply decrease the current pressure as impacted by these registers.
96 void RegPressureTracker::decreaseRegPressure(ArrayRef<unsigned> RegUnits) {
97 for (unsigned I = 0, E = RegUnits.size(); I != E; ++I)
98 decreaseSetPressure(CurrSetPressure, MRI->getPressureSets(RegUnits[I]));
101 /// Clear the result so it can be used for another round of pressure tracking.
102 void IntervalPressure::reset() {
103 TopIdx = BottomIdx = SlotIndex();
104 MaxSetPressure.clear();
109 /// Clear the result so it can be used for another round of pressure tracking.
110 void RegionPressure::reset() {
111 TopPos = BottomPos = MachineBasicBlock::const_iterator();
112 MaxSetPressure.clear();
117 /// If the current top is not less than or equal to the next index, open it.
118 /// We happen to need the SlotIndex for the next top for pressure update.
119 void IntervalPressure::openTop(SlotIndex NextTop) {
120 if (TopIdx <= NextTop)
122 TopIdx = SlotIndex();
126 /// If the current top is the previous instruction (before receding), open it.
127 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) {
128 if (TopPos != PrevTop)
130 TopPos = MachineBasicBlock::const_iterator();
134 /// If the current bottom is not greater than the previous index, open it.
135 void IntervalPressure::openBottom(SlotIndex PrevBottom) {
136 if (BottomIdx > PrevBottom)
138 BottomIdx = SlotIndex();
142 /// If the current bottom is the previous instr (before advancing), open it.
143 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) {
144 if (BottomPos != PrevBottom)
146 BottomPos = MachineBasicBlock::const_iterator();
150 const LiveInterval *RegPressureTracker::getInterval(unsigned Reg) const {
151 if (TargetRegisterInfo::isVirtualRegister(Reg))
152 return &LIS->getInterval(Reg);
153 return LIS->getCachedRegUnit(Reg);
156 /// Setup the RegPressureTracker.
158 /// TODO: Add support for pressure without LiveIntervals.
159 void RegPressureTracker::init(const MachineFunction *mf,
160 const RegisterClassInfo *rci,
161 const LiveIntervals *lis,
162 const MachineBasicBlock *mbb,
163 MachineBasicBlock::const_iterator pos,
164 bool ShouldTrackUntiedDefs)
167 TRI = MF->getTarget().getRegisterInfo();
169 MRI = &MF->getRegInfo();
171 TrackUntiedDefs = ShouldTrackUntiedDefs;
173 if (RequireIntervals) {
174 assert(lis && "IntervalPressure requires LiveIntervals");
179 CurrSetPressure.assign(TRI->getNumRegPressureSets(), 0);
180 LiveThruPressure.clear();
182 if (RequireIntervals)
183 static_cast<IntervalPressure&>(P).reset();
185 static_cast<RegionPressure&>(P).reset();
186 P.MaxSetPressure = CurrSetPressure;
188 LiveRegs.PhysRegs.clear();
189 LiveRegs.PhysRegs.setUniverse(TRI->getNumRegs());
190 LiveRegs.VirtRegs.clear();
191 LiveRegs.VirtRegs.setUniverse(MRI->getNumVirtRegs());
194 UntiedDefs.setUniverse(MRI->getNumVirtRegs());
197 /// Does this pressure result have a valid top position and live ins.
198 bool RegPressureTracker::isTopClosed() const {
199 if (RequireIntervals)
200 return static_cast<IntervalPressure&>(P).TopIdx.isValid();
201 return (static_cast<RegionPressure&>(P).TopPos ==
202 MachineBasicBlock::const_iterator());
205 /// Does this pressure result have a valid bottom position and live outs.
206 bool RegPressureTracker::isBottomClosed() const {
207 if (RequireIntervals)
208 return static_cast<IntervalPressure&>(P).BottomIdx.isValid();
209 return (static_cast<RegionPressure&>(P).BottomPos ==
210 MachineBasicBlock::const_iterator());
214 SlotIndex RegPressureTracker::getCurrSlot() const {
215 MachineBasicBlock::const_iterator IdxPos = CurrPos;
216 while (IdxPos != MBB->end() && IdxPos->isDebugValue())
218 if (IdxPos == MBB->end())
219 return LIS->getMBBEndIdx(MBB);
220 return LIS->getInstructionIndex(IdxPos).getRegSlot();
223 /// Set the boundary for the top of the region and summarize live ins.
224 void RegPressureTracker::closeTop() {
225 if (RequireIntervals)
226 static_cast<IntervalPressure&>(P).TopIdx = getCurrSlot();
228 static_cast<RegionPressure&>(P).TopPos = CurrPos;
230 assert(P.LiveInRegs.empty() && "inconsistent max pressure result");
231 P.LiveInRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size());
232 P.LiveInRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end());
233 for (SparseSet<unsigned>::const_iterator I =
234 LiveRegs.VirtRegs.begin(), E = LiveRegs.VirtRegs.end(); I != E; ++I)
235 P.LiveInRegs.push_back(*I);
236 std::sort(P.LiveInRegs.begin(), P.LiveInRegs.end());
237 P.LiveInRegs.erase(std::unique(P.LiveInRegs.begin(), P.LiveInRegs.end()),
241 /// Set the boundary for the bottom of the region and summarize live outs.
242 void RegPressureTracker::closeBottom() {
243 if (RequireIntervals)
244 static_cast<IntervalPressure&>(P).BottomIdx = getCurrSlot();
246 static_cast<RegionPressure&>(P).BottomPos = CurrPos;
248 assert(P.LiveOutRegs.empty() && "inconsistent max pressure result");
249 P.LiveOutRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size());
250 P.LiveOutRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end());
251 for (SparseSet<unsigned>::const_iterator I =
252 LiveRegs.VirtRegs.begin(), E = LiveRegs.VirtRegs.end(); I != E; ++I)
253 P.LiveOutRegs.push_back(*I);
254 std::sort(P.LiveOutRegs.begin(), P.LiveOutRegs.end());
255 P.LiveOutRegs.erase(std::unique(P.LiveOutRegs.begin(), P.LiveOutRegs.end()),
256 P.LiveOutRegs.end());
259 /// Finalize the region boundaries and record live ins and live outs.
260 void RegPressureTracker::closeRegion() {
261 if (!isTopClosed() && !isBottomClosed()) {
262 assert(LiveRegs.PhysRegs.empty() && LiveRegs.VirtRegs.empty() &&
263 "no region boundary");
266 if (!isBottomClosed())
268 else if (!isTopClosed())
270 // If both top and bottom are closed, do nothing.
273 /// The register tracker is unaware of global liveness so ignores normal
274 /// live-thru ranges. However, two-address or coalesced chains can also lead
275 /// to live ranges with no holes. Count these to inform heuristics that we
276 /// can never drop below this pressure.
277 void RegPressureTracker::initLiveThru(const RegPressureTracker &RPTracker) {
278 LiveThruPressure.assign(TRI->getNumRegPressureSets(), 0);
279 assert(isBottomClosed() && "need bottom-up tracking to intialize.");
280 for (unsigned i = 0, e = P.LiveOutRegs.size(); i < e; ++i) {
281 unsigned Reg = P.LiveOutRegs[i];
282 if (TargetRegisterInfo::isVirtualRegister(Reg)
283 && !RPTracker.hasUntiedDef(Reg)) {
284 increaseSetPressure(LiveThruPressure, MRI->getPressureSets(Reg));
289 /// \brief Convenient wrapper for checking membership in RegisterOperands.
290 /// (std::count() doesn't have an early exit).
291 static bool containsReg(ArrayRef<unsigned> RegUnits, unsigned RegUnit) {
292 return std::find(RegUnits.begin(), RegUnits.end(), RegUnit) != RegUnits.end();
295 /// Collect this instruction's unique uses and defs into SmallVectors for
296 /// processing defs and uses in order.
298 /// FIXME: always ignore tied opers
299 class RegisterOperands {
300 const TargetRegisterInfo *TRI;
301 const MachineRegisterInfo *MRI;
305 SmallVector<unsigned, 8> Uses;
306 SmallVector<unsigned, 8> Defs;
307 SmallVector<unsigned, 8> DeadDefs;
309 RegisterOperands(const TargetRegisterInfo *tri,
310 const MachineRegisterInfo *mri, bool ID = false):
311 TRI(tri), MRI(mri), IgnoreDead(ID) {}
313 /// Push this operand's register onto the correct vector.
314 void collect(const MachineOperand &MO) {
315 if (!MO.isReg() || !MO.getReg())
318 pushRegUnits(MO.getReg(), Uses);
322 pushRegUnits(MO.getReg(), DeadDefs);
325 pushRegUnits(MO.getReg(), Defs);
330 void pushRegUnits(unsigned Reg, SmallVectorImpl<unsigned> &RegUnits) {
331 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
332 if (containsReg(RegUnits, Reg))
334 RegUnits.push_back(Reg);
336 else if (MRI->isAllocatable(Reg)) {
337 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
338 if (containsReg(RegUnits, *Units))
340 RegUnits.push_back(*Units);
346 /// Collect physical and virtual register operands.
347 static void collectOperands(const MachineInstr *MI,
348 RegisterOperands &RegOpers) {
349 for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI)
350 RegOpers.collect(*OperI);
352 // Remove redundant physreg dead defs.
353 SmallVectorImpl<unsigned>::iterator I =
354 std::remove_if(RegOpers.DeadDefs.begin(), RegOpers.DeadDefs.end(),
355 std::bind1st(std::ptr_fun(containsReg), RegOpers.Defs));
356 RegOpers.DeadDefs.erase(I, RegOpers.DeadDefs.end());
359 /// Initialize an array of N PressureDiffs.
360 void PressureDiffs::init(unsigned N) {
363 memset(PDiffArray, 0, N * sizeof(PressureDiff));
368 PDiffArray = reinterpret_cast<PressureDiff*>(calloc(N, sizeof(PressureDiff)));
371 /// Add a change in pressure to the pressure diff of a given instruction.
372 void PressureDiff::addPressureChange(unsigned RegUnit, bool IsDec,
373 const MachineRegisterInfo *MRI) {
374 PSetIterator PSetI = MRI->getPressureSets(RegUnit);
375 int Weight = IsDec ? -PSetI.getWeight() : PSetI.getWeight();
376 for (; PSetI.isValid(); ++PSetI) {
377 // Find an existing entry in the pressure diff for this PSet.
378 PressureDiff::iterator I = begin(), E = end();
379 for (; I != E && I->isValid(); ++I) {
380 if (I->getPSet() >= *PSetI)
383 // If all pressure sets are more constrained, skip the remaining PSets.
386 // Insert this PressureChange.
387 if (!I->isValid() || I->getPSet() != *PSetI) {
388 PressureChange PTmp = PressureChange(*PSetI);
389 for (PressureDiff::iterator J = I; J != E && PTmp.isValid(); ++J)
392 // Update the units for this pressure set.
393 I->setUnitInc(I->getUnitInc() + Weight);
397 /// Record the pressure difference induced by the given operand list.
398 static void collectPDiff(PressureDiff &PDiff, RegisterOperands &RegOpers,
399 const MachineRegisterInfo *MRI) {
400 assert(!PDiff.begin()->isValid() && "stale PDiff");
402 for (unsigned i = 0, e = RegOpers.Defs.size(); i != e; ++i)
403 PDiff.addPressureChange(RegOpers.Defs[i], true, MRI);
405 for (unsigned i = 0, e = RegOpers.Uses.size(); i != e; ++i)
406 PDiff.addPressureChange(RegOpers.Uses[i], false, MRI);
409 /// Force liveness of registers.
410 void RegPressureTracker::addLiveRegs(ArrayRef<unsigned> Regs) {
411 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
412 if (LiveRegs.insert(Regs[i]))
413 increaseRegPressure(Regs[i]);
417 /// Add Reg to the live in set and increase max pressure.
418 void RegPressureTracker::discoverLiveIn(unsigned Reg) {
419 assert(!LiveRegs.contains(Reg) && "avoid bumping max pressure twice");
420 if (containsReg(P.LiveInRegs, Reg))
423 // At live in discovery, unconditionally increase the high water mark.
424 P.LiveInRegs.push_back(Reg);
425 increaseSetPressure(P.MaxSetPressure, MRI->getPressureSets(Reg));
428 /// Add Reg to the live out set and increase max pressure.
429 void RegPressureTracker::discoverLiveOut(unsigned Reg) {
430 assert(!LiveRegs.contains(Reg) && "avoid bumping max pressure twice");
431 if (containsReg(P.LiveOutRegs, Reg))
434 // At live out discovery, unconditionally increase the high water mark.
435 P.LiveOutRegs.push_back(Reg);
436 increaseSetPressure(P.MaxSetPressure, MRI->getPressureSets(Reg));
439 /// Recede across the previous instruction. If LiveUses is provided, record any
440 /// RegUnits that are made live by the current instruction's uses. This includes
441 /// registers that are both defined and used by the instruction. If a pressure
442 /// difference pointer is provided record the changes is pressure caused by this
443 /// instruction independent of liveness.
444 bool RegPressureTracker::recede(SmallVectorImpl<unsigned> *LiveUses,
445 PressureDiff *PDiff) {
446 // Check for the top of the analyzable region.
447 if (CurrPos == MBB->begin()) {
451 if (!isBottomClosed())
454 // Open the top of the region using block iterators.
455 if (!RequireIntervals && isTopClosed())
456 static_cast<RegionPressure&>(P).openTop(CurrPos);
458 // Find the previous instruction.
461 while (CurrPos != MBB->begin() && CurrPos->isDebugValue());
463 if (CurrPos->isDebugValue()) {
468 if (RequireIntervals)
469 SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
471 // Open the top of the region using slot indexes.
472 if (RequireIntervals && isTopClosed())
473 static_cast<IntervalPressure&>(P).openTop(SlotIdx);
475 RegisterOperands RegOpers(TRI, MRI);
476 collectOperands(CurrPos, RegOpers);
479 collectPDiff(*PDiff, RegOpers, MRI);
481 // Boost pressure for all dead defs together.
482 increaseRegPressure(RegOpers.DeadDefs);
483 decreaseRegPressure(RegOpers.DeadDefs);
485 // Kill liveness at live defs.
486 // TODO: consider earlyclobbers?
487 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
488 unsigned Reg = RegOpers.Defs[i];
489 if (LiveRegs.erase(Reg))
490 decreaseRegPressure(Reg);
492 discoverLiveOut(Reg);
495 // Generate liveness for uses.
496 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
497 unsigned Reg = RegOpers.Uses[i];
498 if (!LiveRegs.contains(Reg)) {
499 // Adjust liveouts if LiveIntervals are available.
500 if (RequireIntervals) {
501 const LiveInterval *LI = getInterval(Reg);
502 // Check if this LR is killed and not redefined here.
504 LiveRangeQuery LRQ(*LI, SlotIdx);
505 if (!LRQ.isKill() && !LRQ.valueDefined())
506 discoverLiveOut(Reg);
509 increaseRegPressure(Reg);
510 LiveRegs.insert(Reg);
511 if (LiveUses && !containsReg(*LiveUses, Reg))
512 LiveUses->push_back(Reg);
515 if (TrackUntiedDefs) {
516 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
517 unsigned Reg = RegOpers.Defs[i];
518 if (TargetRegisterInfo::isVirtualRegister(Reg) && !LiveRegs.contains(Reg))
519 UntiedDefs.insert(Reg);
525 /// Advance across the current instruction.
526 bool RegPressureTracker::advance() {
527 assert(!TrackUntiedDefs && "unsupported mode");
529 // Check for the bottom of the analyzable region.
530 if (CurrPos == MBB->end()) {
538 if (RequireIntervals)
539 SlotIdx = getCurrSlot();
541 // Open the bottom of the region using slot indexes.
542 if (isBottomClosed()) {
543 if (RequireIntervals)
544 static_cast<IntervalPressure&>(P).openBottom(SlotIdx);
546 static_cast<RegionPressure&>(P).openBottom(CurrPos);
549 RegisterOperands RegOpers(TRI, MRI);
550 collectOperands(CurrPos, RegOpers);
552 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
553 unsigned Reg = RegOpers.Uses[i];
554 // Discover live-ins.
555 bool isLive = LiveRegs.contains(Reg);
558 // Kill liveness at last uses.
559 bool lastUse = false;
560 if (RequireIntervals) {
561 const LiveInterval *LI = getInterval(Reg);
562 lastUse = LI && LiveRangeQuery(*LI, SlotIdx).isKill();
565 // Allocatable physregs are always single-use before register rewriting.
566 lastUse = !TargetRegisterInfo::isVirtualRegister(Reg);
568 if (lastUse && isLive) {
570 decreaseRegPressure(Reg);
572 else if (!lastUse && !isLive)
573 increaseRegPressure(Reg);
576 // Generate liveness for defs.
577 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
578 unsigned Reg = RegOpers.Defs[i];
579 if (LiveRegs.insert(Reg))
580 increaseRegPressure(Reg);
583 // Boost pressure for all dead defs together.
584 increaseRegPressure(RegOpers.DeadDefs);
585 decreaseRegPressure(RegOpers.DeadDefs);
587 // Find the next instruction.
590 while (CurrPos != MBB->end() && CurrPos->isDebugValue());
594 /// Find the max change in excess pressure across all sets.
595 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec,
596 ArrayRef<unsigned> NewPressureVec,
597 RegPressureDelta &Delta,
598 const RegisterClassInfo *RCI,
599 ArrayRef<unsigned> LiveThruPressureVec) {
600 Delta.Excess = PressureChange();
601 for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) {
602 unsigned POld = OldPressureVec[i];
603 unsigned PNew = NewPressureVec[i];
604 int PDiff = (int)PNew - (int)POld;
605 if (!PDiff) // No change in this set in the common case.
607 // Only consider change beyond the limit.
608 unsigned Limit = RCI->getRegPressureSetLimit(i);
609 if (!LiveThruPressureVec.empty())
610 Limit += LiveThruPressureVec[i];
614 PDiff = 0; // Under the limit
616 PDiff = PNew - Limit; // Just exceeded limit.
618 else if (Limit > PNew)
619 PDiff = Limit - POld; // Just obeyed limit.
622 Delta.Excess = PressureChange(i);
623 Delta.Excess.setUnitInc(PDiff);
629 /// Find the max change in max pressure that either surpasses a critical PSet
630 /// limit or exceeds the current MaxPressureLimit.
632 /// FIXME: comparing each element of the old and new MaxPressure vectors here is
633 /// silly. It's done now to demonstrate the concept but will go away with a
634 /// RegPressureTracker API change to work with pressure differences.
635 static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec,
636 ArrayRef<unsigned> NewMaxPressureVec,
637 ArrayRef<PressureChange> CriticalPSets,
638 ArrayRef<unsigned> MaxPressureLimit,
639 RegPressureDelta &Delta) {
640 Delta.CriticalMax = PressureChange();
641 Delta.CurrentMax = PressureChange();
643 unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
644 for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) {
645 unsigned POld = OldMaxPressureVec[i];
646 unsigned PNew = NewMaxPressureVec[i];
647 if (PNew == POld) // No change in this set in the common case.
650 if (!Delta.CriticalMax.isValid()) {
651 while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < i)
654 if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == i) {
655 int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].getUnitInc();
657 Delta.CriticalMax = PressureChange(i);
658 Delta.CriticalMax.setUnitInc(PDiff);
662 // Find the first increase above MaxPressureLimit.
663 // (Ignores negative MDiff).
664 if (!Delta.CurrentMax.isValid() && PNew > MaxPressureLimit[i]) {
665 Delta.CurrentMax = PressureChange(i);
666 Delta.CurrentMax.setUnitInc(PNew - POld);
667 if (CritIdx == CritEnd || Delta.CriticalMax.isValid())
673 /// Record the upward impact of a single instruction on current register
674 /// pressure. Unlike the advance/recede pressure tracking interface, this does
675 /// not discover live in/outs.
677 /// This is intended for speculative queries. It leaves pressure inconsistent
678 /// with the current position, so must be restored by the caller.
679 void RegPressureTracker::bumpUpwardPressure(const MachineInstr *MI) {
680 assert(!MI->isDebugValue() && "Expect a nondebug instruction.");
682 // Account for register pressure similar to RegPressureTracker::recede().
683 RegisterOperands RegOpers(TRI, MRI, /*IgnoreDead=*/true);
684 collectOperands(MI, RegOpers);
686 // Boost max pressure for all dead defs together.
687 // Since CurrSetPressure and MaxSetPressure
688 increaseRegPressure(RegOpers.DeadDefs);
689 decreaseRegPressure(RegOpers.DeadDefs);
691 // Kill liveness at live defs.
692 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
693 unsigned Reg = RegOpers.Defs[i];
694 if (!containsReg(RegOpers.Uses, Reg))
695 decreaseRegPressure(Reg);
697 // Generate liveness for uses.
698 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
699 unsigned Reg = RegOpers.Uses[i];
700 if (!LiveRegs.contains(Reg))
701 increaseRegPressure(Reg);
705 /// Consider the pressure increase caused by traversing this instruction
706 /// bottom-up. Find the pressure set with the most change beyond its pressure
707 /// limit based on the tracker's current pressure, and return the change in
708 /// number of register units of that pressure set introduced by this
711 /// This assumes that the current LiveOut set is sufficient.
713 /// FIXME: This is expensive for an on-the-fly query. We need to cache the
714 /// result per-SUnit with enough information to adjust for the current
715 /// scheduling position. But this works as a proof of concept.
716 void RegPressureTracker::
717 getMaxUpwardPressureDelta(const MachineInstr *MI, PressureDiff *PDiff,
718 RegPressureDelta &Delta,
719 ArrayRef<PressureChange> CriticalPSets,
720 ArrayRef<unsigned> MaxPressureLimit) {
721 // Snapshot Pressure.
722 // FIXME: The snapshot heap space should persist. But I'm planning to
723 // summarize the pressure effect so we don't need to snapshot at all.
724 std::vector<unsigned> SavedPressure = CurrSetPressure;
725 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
727 bumpUpwardPressure(MI);
729 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI,
731 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
732 MaxPressureLimit, Delta);
733 assert(Delta.CriticalMax.getUnitInc() >= 0 &&
734 Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure");
736 // Restore the tracker's state.
737 P.MaxSetPressure.swap(SavedMaxPressure);
738 CurrSetPressure.swap(SavedPressure);
744 // Check if the alternate algorithm yields the same result.
745 RegPressureDelta Delta2;
746 getUpwardPressureDelta(MI, *PDiff, Delta2, CriticalPSets, MaxPressureLimit);
747 if (Delta != Delta2) {
748 dbgs() << "DELTA: " << *MI;
749 if (Delta.Excess.isValid())
750 dbgs() << "Excess1 " << TRI->getRegPressureSetName(Delta.Excess.getPSet())
751 << " " << Delta.Excess.getUnitInc() << "\n";
752 if (Delta.CriticalMax.isValid())
753 dbgs() << "Critic1 " << TRI->getRegPressureSetName(Delta.CriticalMax.getPSet())
754 << " " << Delta.CriticalMax.getUnitInc() << "\n";
755 if (Delta.CurrentMax.isValid())
756 dbgs() << "CurrMx1 " << TRI->getRegPressureSetName(Delta.CurrentMax.getPSet())
757 << " " << Delta.CurrentMax.getUnitInc() << "\n";
758 if (Delta2.Excess.isValid())
759 dbgs() << "Excess2 " << TRI->getRegPressureSetName(Delta2.Excess.getPSet())
760 << " " << Delta2.Excess.getUnitInc() << "\n";
761 if (Delta2.CriticalMax.isValid())
762 dbgs() << "Critic2 " << TRI->getRegPressureSetName(Delta2.CriticalMax.getPSet())
763 << " " << Delta2.CriticalMax.getUnitInc() << "\n";
764 if (Delta2.CurrentMax.isValid())
765 dbgs() << "CurrMx2 " << TRI->getRegPressureSetName(Delta2.CurrentMax.getPSet())
766 << " " << Delta2.CurrentMax.getUnitInc() << "\n";
767 llvm_unreachable("RegP Delta Mismatch");
772 /// This is a prototype of the fast version of querying register pressure that
773 /// does not directly depend on current liveness. It's still slow because we
774 /// recompute pressure change on-the-fly. This implementation only exists to
775 /// prove correctness.
777 /// @param Delta captures information needed for heuristics.
779 /// @param CriticalPSets Are the pressure sets that are known to exceed some
780 /// limit within the region, not necessarily at the current position.
782 /// @param MaxPressureLimit Is the max pressure within the region, not
783 /// necessarily at the current position.
784 void RegPressureTracker::
785 getUpwardPressureDelta(const MachineInstr *MI, /*const*/ PressureDiff &PDiff,
786 RegPressureDelta &Delta,
787 ArrayRef<PressureChange> CriticalPSets,
788 ArrayRef<unsigned> MaxPressureLimit) const {
789 unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
790 for (PressureDiff::const_iterator
791 PDiffI = PDiff.begin(), PDiffE = PDiff.end();
792 PDiffI != PDiffE && PDiffI->isValid(); ++PDiffI) {
794 unsigned PSetID = PDiffI->getPSet();
795 unsigned Limit = RCI->getRegPressureSetLimit(PSetID);
796 if (!LiveThruPressure.empty())
797 Limit += LiveThruPressure[PSetID];
799 unsigned POld = CurrSetPressure[PSetID];
800 unsigned MOld = P.MaxSetPressure[PSetID];
801 unsigned MNew = MOld;
802 // Ignore DeadDefs here because they aren't captured by PressureChange.
803 unsigned PNew = POld + PDiffI->getUnitInc();
804 assert((PDiffI->getUnitInc() >= 0) == (PNew >= POld) && "PSet overflow");
807 // Check if current pressure has exceeded the limit.
808 if (!Delta.Excess.isValid()) {
809 unsigned ExcessInc = 0;
811 ExcessInc = POld > Limit ? PNew - POld : PNew - Limit;
812 else if (POld > Limit)
813 ExcessInc = Limit - POld;
815 Delta.Excess = PressureChange(PSetID);
816 Delta.Excess.setUnitInc(ExcessInc);
819 // Check if max pressure has exceeded a critical pressure set max.
822 if (!Delta.CriticalMax.isValid()) {
823 while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < PSetID)
826 if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == PSetID) {
827 int CritInc = (int)MNew - (int)CriticalPSets[CritIdx].getUnitInc();
828 if (CritInc > 0 && CritInc <= INT16_MAX) {
829 Delta.CriticalMax = PressureChange(PSetID);
830 Delta.CriticalMax.setUnitInc(CritInc);
834 // Check if max pressure has exceeded the current max.
835 if (!Delta.CurrentMax.isValid() && MNew > MaxPressureLimit[PSetID]) {
836 Delta.CurrentMax = PressureChange(PSetID);
837 Delta.CurrentMax.setUnitInc(MNew - MOld);
842 /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx).
843 static bool findUseBetween(unsigned Reg,
844 SlotIndex PriorUseIdx, SlotIndex NextUseIdx,
845 const MachineRegisterInfo *MRI,
846 const LiveIntervals *LIS) {
847 for (MachineRegisterInfo::use_nodbg_iterator
848 UI = MRI->use_nodbg_begin(Reg), UE = MRI->use_nodbg_end();
849 UI != UE; UI.skipInstruction()) {
850 const MachineInstr* MI = &*UI;
851 if (MI->isDebugValue())
853 SlotIndex InstSlot = LIS->getInstructionIndex(MI).getRegSlot();
854 if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx)
860 /// Record the downward impact of a single instruction on current register
861 /// pressure. Unlike the advance/recede pressure tracking interface, this does
862 /// not discover live in/outs.
864 /// This is intended for speculative queries. It leaves pressure inconsistent
865 /// with the current position, so must be restored by the caller.
866 void RegPressureTracker::bumpDownwardPressure(const MachineInstr *MI) {
867 assert(!MI->isDebugValue() && "Expect a nondebug instruction.");
869 // Account for register pressure similar to RegPressureTracker::recede().
870 RegisterOperands RegOpers(TRI, MRI);
871 collectOperands(MI, RegOpers);
873 // Kill liveness at last uses. Assume allocatable physregs are single-use
874 // rather than checking LiveIntervals.
876 if (RequireIntervals)
877 SlotIdx = LIS->getInstructionIndex(MI).getRegSlot();
879 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
880 unsigned Reg = RegOpers.Uses[i];
881 if (RequireIntervals) {
882 // FIXME: allow the caller to pass in the list of vreg uses that remain
883 // to be bottom-scheduled to avoid searching uses at each query.
884 SlotIndex CurrIdx = getCurrSlot();
885 const LiveInterval *LI = getInterval(Reg);
887 LiveRangeQuery LRQ(*LI, SlotIdx);
888 if (LRQ.isKill() && !findUseBetween(Reg, CurrIdx, SlotIdx, MRI, LIS))
889 decreaseRegPressure(Reg);
892 else if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
893 // Allocatable physregs are always single-use before register rewriting.
894 decreaseRegPressure(Reg);
898 // Generate liveness for defs.
899 increaseRegPressure(RegOpers.Defs);
901 // Boost pressure for all dead defs together.
902 increaseRegPressure(RegOpers.DeadDefs);
903 decreaseRegPressure(RegOpers.DeadDefs);
906 /// Consider the pressure increase caused by traversing this instruction
907 /// top-down. Find the register class with the most change in its pressure limit
908 /// based on the tracker's current pressure, and return the number of excess
909 /// register units of that pressure set introduced by this instruction.
911 /// This assumes that the current LiveIn set is sufficient.
912 void RegPressureTracker::
913 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
914 ArrayRef<PressureChange> CriticalPSets,
915 ArrayRef<unsigned> MaxPressureLimit) {
916 // Snapshot Pressure.
917 std::vector<unsigned> SavedPressure = CurrSetPressure;
918 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
920 bumpDownwardPressure(MI);
922 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI,
924 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
925 MaxPressureLimit, Delta);
926 assert(Delta.CriticalMax.getUnitInc() >= 0 &&
927 Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure");
929 // Restore the tracker's state.
930 P.MaxSetPressure.swap(SavedMaxPressure);
931 CurrSetPressure.swap(SavedPressure);
934 /// Get the pressure of each PSet after traversing this instruction bottom-up.
935 void RegPressureTracker::
936 getUpwardPressure(const MachineInstr *MI,
937 std::vector<unsigned> &PressureResult,
938 std::vector<unsigned> &MaxPressureResult) {
939 // Snapshot pressure.
940 PressureResult = CurrSetPressure;
941 MaxPressureResult = P.MaxSetPressure;
943 bumpUpwardPressure(MI);
945 // Current pressure becomes the result. Restore current pressure.
946 P.MaxSetPressure.swap(MaxPressureResult);
947 CurrSetPressure.swap(PressureResult);
950 /// Get the pressure of each PSet after traversing this instruction top-down.
951 void RegPressureTracker::
952 getDownwardPressure(const MachineInstr *MI,
953 std::vector<unsigned> &PressureResult,
954 std::vector<unsigned> &MaxPressureResult) {
955 // Snapshot pressure.
956 PressureResult = CurrSetPressure;
957 MaxPressureResult = P.MaxSetPressure;
959 bumpDownwardPressure(MI);
961 // Current pressure becomes the result. Restore current pressure.
962 P.MaxSetPressure.swap(MaxPressureResult);
963 CurrSetPressure.swap(PressureResult);