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,
28 std::vector<unsigned> &MaxSetPressure,
29 const int *PSet, unsigned Weight) {
30 for (; *PSet != -1; ++PSet) {
31 CurrSetPressure[*PSet] += Weight;
32 if (&CurrSetPressure != &MaxSetPressure
33 && CurrSetPressure[*PSet] > MaxSetPressure[*PSet]) {
34 MaxSetPressure[*PSet] = CurrSetPressure[*PSet];
39 /// Decrease pressure for each pressure set provided by TargetRegisterInfo.
40 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure,
41 const int *PSet, unsigned Weight) {
42 for (; *PSet != -1; ++PSet) {
43 assert(CurrSetPressure[*PSet] >= Weight && "register pressure underflow");
44 CurrSetPressure[*PSet] -= Weight;
48 /// Directly increase pressure only within this RegisterPressure result.
49 void RegisterPressure::increase(unsigned Reg, const TargetRegisterInfo *TRI,
50 const MachineRegisterInfo *MRI) {
51 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
52 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
53 increaseSetPressure(MaxSetPressure, MaxSetPressure,
54 TRI->getRegClassPressureSets(RC),
55 TRI->getRegClassWeight(RC).RegWeight);
58 increaseSetPressure(MaxSetPressure, MaxSetPressure,
59 TRI->getRegUnitPressureSets(Reg),
60 TRI->getRegUnitWeight(Reg));
64 /// Directly decrease pressure only within this RegisterPressure result.
65 void RegisterPressure::decrease(unsigned Reg, const TargetRegisterInfo *TRI,
66 const MachineRegisterInfo *MRI) {
67 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
68 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
69 decreaseSetPressure(MaxSetPressure, TRI->getRegClassPressureSets(RC),
70 TRI->getRegClassWeight(RC).RegWeight);
73 decreaseSetPressure(MaxSetPressure, TRI->getRegUnitPressureSets(Reg),
74 TRI->getRegUnitWeight(Reg));
78 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
79 static void dumpSetPressure(const std::vector<unsigned> &SetPressure,
80 const TargetRegisterInfo *TRI) {
81 for (unsigned i = 0, e = SetPressure.size(); i < e; ++i) {
82 if (SetPressure[i] != 0)
83 dbgs() << TRI->getRegPressureSetName(i) << "=" << SetPressure[i] << '\n';
87 void RegisterPressure::dump(const TargetRegisterInfo *TRI) const {
88 dbgs() << "Max Pressure: ";
89 dumpSetPressure(MaxSetPressure, TRI);
90 dbgs() << "Live In: ";
91 for (unsigned i = 0, e = LiveInRegs.size(); i < e; ++i)
92 dbgs() << PrintReg(LiveInRegs[i], TRI) << " ";
94 dbgs() << "Live Out: ";
95 for (unsigned i = 0, e = LiveOutRegs.size(); i < e; ++i)
96 dbgs() << PrintReg(LiveOutRegs[i], TRI) << " ";
100 void RegPressureTracker::dump() const {
101 dbgs() << "Curr Pressure: ";
102 dumpSetPressure(CurrSetPressure, TRI);
107 /// Increase the current pressure as impacted by these registers and bump
108 /// the high water mark if needed.
109 void RegPressureTracker::increaseRegPressure(ArrayRef<unsigned> Regs) {
110 for (unsigned I = 0, E = Regs.size(); I != E; ++I) {
111 if (TargetRegisterInfo::isVirtualRegister(Regs[I])) {
112 const TargetRegisterClass *RC = MRI->getRegClass(Regs[I]);
113 increaseSetPressure(CurrSetPressure, P.MaxSetPressure,
114 TRI->getRegClassPressureSets(RC),
115 TRI->getRegClassWeight(RC).RegWeight);
118 increaseSetPressure(CurrSetPressure, P.MaxSetPressure,
119 TRI->getRegUnitPressureSets(Regs[I]),
120 TRI->getRegUnitWeight(Regs[I]));
125 /// Simply decrease the current pressure as impacted by these registers.
126 void RegPressureTracker::decreaseRegPressure(ArrayRef<unsigned> Regs) {
127 for (unsigned I = 0, E = Regs.size(); I != E; ++I) {
128 if (TargetRegisterInfo::isVirtualRegister(Regs[I])) {
129 const TargetRegisterClass *RC = MRI->getRegClass(Regs[I]);
130 decreaseSetPressure(CurrSetPressure,
131 TRI->getRegClassPressureSets(RC),
132 TRI->getRegClassWeight(RC).RegWeight);
135 decreaseSetPressure(CurrSetPressure, TRI->getRegUnitPressureSets(Regs[I]),
136 TRI->getRegUnitWeight(Regs[I]));
141 /// Clear the result so it can be used for another round of pressure tracking.
142 void IntervalPressure::reset() {
143 TopIdx = BottomIdx = SlotIndex();
144 MaxSetPressure.clear();
149 /// Clear the result so it can be used for another round of pressure tracking.
150 void RegionPressure::reset() {
151 TopPos = BottomPos = MachineBasicBlock::const_iterator();
152 MaxSetPressure.clear();
157 /// If the current top is not less than or equal to the next index, open it.
158 /// We happen to need the SlotIndex for the next top for pressure update.
159 void IntervalPressure::openTop(SlotIndex NextTop) {
160 if (TopIdx <= NextTop)
162 TopIdx = SlotIndex();
166 /// If the current top is the previous instruction (before receding), open it.
167 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) {
168 if (TopPos != PrevTop)
170 TopPos = MachineBasicBlock::const_iterator();
174 /// If the current bottom is not greater than the previous index, open it.
175 void IntervalPressure::openBottom(SlotIndex PrevBottom) {
176 if (BottomIdx > PrevBottom)
178 BottomIdx = SlotIndex();
182 /// If the current bottom is the previous instr (before advancing), open it.
183 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) {
184 if (BottomPos != PrevBottom)
186 BottomPos = MachineBasicBlock::const_iterator();
190 const LiveInterval *RegPressureTracker::getInterval(unsigned Reg) const {
191 if (TargetRegisterInfo::isVirtualRegister(Reg))
192 return &LIS->getInterval(Reg);
193 return LIS->getCachedRegUnit(Reg);
196 /// Setup the RegPressureTracker.
198 /// TODO: Add support for pressure without LiveIntervals.
199 void RegPressureTracker::init(const MachineFunction *mf,
200 const RegisterClassInfo *rci,
201 const LiveIntervals *lis,
202 const MachineBasicBlock *mbb,
203 MachineBasicBlock::const_iterator pos)
206 TRI = MF->getTarget().getRegisterInfo();
208 MRI = &MF->getRegInfo();
211 if (RequireIntervals) {
212 assert(lis && "IntervalPressure requires LiveIntervals");
217 CurrSetPressure.assign(TRI->getNumRegPressureSets(), 0);
219 if (RequireIntervals)
220 static_cast<IntervalPressure&>(P).reset();
222 static_cast<RegionPressure&>(P).reset();
223 P.MaxSetPressure = CurrSetPressure;
225 LiveRegs.PhysRegs.clear();
226 LiveRegs.PhysRegs.setUniverse(TRI->getNumRegs());
227 LiveRegs.VirtRegs.clear();
228 LiveRegs.VirtRegs.setUniverse(MRI->getNumVirtRegs());
231 /// Does this pressure result have a valid top position and live ins.
232 bool RegPressureTracker::isTopClosed() const {
233 if (RequireIntervals)
234 return static_cast<IntervalPressure&>(P).TopIdx.isValid();
235 return (static_cast<RegionPressure&>(P).TopPos ==
236 MachineBasicBlock::const_iterator());
239 /// Does this pressure result have a valid bottom position and live outs.
240 bool RegPressureTracker::isBottomClosed() const {
241 if (RequireIntervals)
242 return static_cast<IntervalPressure&>(P).BottomIdx.isValid();
243 return (static_cast<RegionPressure&>(P).BottomPos ==
244 MachineBasicBlock::const_iterator());
248 SlotIndex RegPressureTracker::getCurrSlot() const {
249 MachineBasicBlock::const_iterator IdxPos = CurrPos;
250 while (IdxPos != MBB->end() && IdxPos->isDebugValue())
252 if (IdxPos == MBB->end())
253 return LIS->getMBBEndIdx(MBB);
254 return LIS->getInstructionIndex(IdxPos).getRegSlot();
257 /// Set the boundary for the top of the region and summarize live ins.
258 void RegPressureTracker::closeTop() {
259 if (RequireIntervals)
260 static_cast<IntervalPressure&>(P).TopIdx = getCurrSlot();
262 static_cast<RegionPressure&>(P).TopPos = CurrPos;
264 assert(P.LiveInRegs.empty() && "inconsistent max pressure result");
265 P.LiveInRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size());
266 P.LiveInRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end());
267 for (SparseSet<unsigned>::const_iterator I =
268 LiveRegs.VirtRegs.begin(), E = LiveRegs.VirtRegs.end(); I != E; ++I)
269 P.LiveInRegs.push_back(*I);
270 std::sort(P.LiveInRegs.begin(), P.LiveInRegs.end());
271 P.LiveInRegs.erase(std::unique(P.LiveInRegs.begin(), P.LiveInRegs.end()),
275 /// Set the boundary for the bottom of the region and summarize live outs.
276 void RegPressureTracker::closeBottom() {
277 if (RequireIntervals)
278 static_cast<IntervalPressure&>(P).BottomIdx = getCurrSlot();
280 static_cast<RegionPressure&>(P).BottomPos = CurrPos;
282 assert(P.LiveOutRegs.empty() && "inconsistent max pressure result");
283 P.LiveOutRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size());
284 P.LiveOutRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end());
285 for (SparseSet<unsigned>::const_iterator I =
286 LiveRegs.VirtRegs.begin(), E = LiveRegs.VirtRegs.end(); I != E; ++I)
287 P.LiveOutRegs.push_back(*I);
288 std::sort(P.LiveOutRegs.begin(), P.LiveOutRegs.end());
289 P.LiveOutRegs.erase(std::unique(P.LiveOutRegs.begin(), P.LiveOutRegs.end()),
290 P.LiveOutRegs.end());
293 /// Finalize the region boundaries and record live ins and live outs.
294 void RegPressureTracker::closeRegion() {
295 if (!isTopClosed() && !isBottomClosed()) {
296 assert(LiveRegs.PhysRegs.empty() && LiveRegs.VirtRegs.empty() &&
297 "no region boundary");
300 if (!isBottomClosed())
302 else if (!isTopClosed())
304 // If both top and bottom are closed, do nothing.
307 /// \brief Convenient wrapper for checking membership in RegisterOperands.
308 static bool containsReg(ArrayRef<unsigned> Regs, unsigned Reg) {
309 return std::find(Regs.begin(), Regs.end(), Reg) != Regs.end();
312 /// Collect this instruction's unique uses and defs into SmallVectors for
313 /// processing defs and uses in order.
314 class RegisterOperands {
315 const TargetRegisterInfo *TRI;
316 const MachineRegisterInfo *MRI;
319 SmallVector<unsigned, 8> Uses;
320 SmallVector<unsigned, 8> Defs;
321 SmallVector<unsigned, 8> DeadDefs;
323 RegisterOperands(const TargetRegisterInfo *tri,
324 const MachineRegisterInfo *mri): TRI(tri), MRI(mri) {}
326 /// Push this operand's register onto the correct vector.
327 void collect(const MachineOperand &MO) {
328 if (!MO.isReg() || !MO.getReg())
331 pushRegUnits(MO.getReg(), Uses);
334 pushRegUnits(MO.getReg(), DeadDefs);
336 pushRegUnits(MO.getReg(), Defs);
341 void pushRegUnits(unsigned Reg, SmallVectorImpl<unsigned> &Regs) {
342 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
343 if (containsReg(Regs, Reg))
347 else if (MRI->isAllocatable(Reg)) {
348 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
349 if (containsReg(Regs, *Units))
351 Regs.push_back(*Units);
357 /// Collect physical and virtual register operands.
358 static void collectOperands(const MachineInstr *MI,
359 RegisterOperands &RegOpers) {
360 for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI)
361 RegOpers.collect(*OperI);
363 // Remove redundant physreg dead defs.
364 SmallVectorImpl<unsigned>::iterator I =
365 std::remove_if(RegOpers.DeadDefs.begin(), RegOpers.DeadDefs.end(),
366 std::bind1st(std::ptr_fun(containsReg), RegOpers.Defs));
367 RegOpers.DeadDefs.erase(I, RegOpers.DeadDefs.end());
370 /// Force liveness of registers.
371 void RegPressureTracker::addLiveRegs(ArrayRef<unsigned> Regs) {
372 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
373 if (LiveRegs.insert(Regs[i]))
374 increaseRegPressure(Regs[i]);
378 /// Add Reg to the live in set and increase max pressure.
379 void RegPressureTracker::discoverLiveIn(unsigned Reg) {
380 assert(!LiveRegs.contains(Reg) && "avoid bumping max pressure twice");
381 if (containsReg(P.LiveInRegs, Reg))
384 // At live in discovery, unconditionally increase the high water mark.
385 P.LiveInRegs.push_back(Reg);
386 P.increase(Reg, TRI, MRI);
389 /// Add Reg to the live out set and increase max pressure.
390 void RegPressureTracker::discoverLiveOut(unsigned Reg) {
391 assert(!LiveRegs.contains(Reg) && "avoid bumping max pressure twice");
392 if (containsReg(P.LiveOutRegs, Reg))
395 // At live out discovery, unconditionally increase the high water mark.
396 P.LiveOutRegs.push_back(Reg);
397 P.increase(Reg, TRI, MRI);
400 /// Recede across the previous instruction.
401 bool RegPressureTracker::recede() {
402 // Check for the top of the analyzable region.
403 if (CurrPos == MBB->begin()) {
407 if (!isBottomClosed())
410 // Open the top of the region using block iterators.
411 if (!RequireIntervals && isTopClosed())
412 static_cast<RegionPressure&>(P).openTop(CurrPos);
414 // Find the previous instruction.
417 while (CurrPos != MBB->begin() && CurrPos->isDebugValue());
419 if (CurrPos->isDebugValue()) {
424 if (RequireIntervals)
425 SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
427 // Open the top of the region using slot indexes.
428 if (RequireIntervals && isTopClosed())
429 static_cast<IntervalPressure&>(P).openTop(SlotIdx);
431 RegisterOperands RegOpers(TRI, MRI);
432 collectOperands(CurrPos, RegOpers);
434 // Boost pressure for all dead defs together.
435 increaseRegPressure(RegOpers.DeadDefs);
436 decreaseRegPressure(RegOpers.DeadDefs);
438 // Kill liveness at live defs.
439 // TODO: consider earlyclobbers?
440 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
441 unsigned Reg = RegOpers.Defs[i];
442 if (LiveRegs.erase(Reg))
443 decreaseRegPressure(Reg);
445 discoverLiveOut(Reg);
448 // Generate liveness for uses.
449 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
450 unsigned Reg = RegOpers.Uses[i];
451 if (!LiveRegs.contains(Reg)) {
452 // Adjust liveouts if LiveIntervals are available.
453 if (RequireIntervals) {
454 const LiveInterval *LI = getInterval(Reg);
455 if (LI && !LI->killedAt(SlotIdx))
456 discoverLiveOut(Reg);
458 increaseRegPressure(Reg);
459 LiveRegs.insert(Reg);
465 /// Advance across the current instruction.
466 bool RegPressureTracker::advance() {
467 // Check for the bottom of the analyzable region.
468 if (CurrPos == MBB->end()) {
476 if (RequireIntervals)
477 SlotIdx = getCurrSlot();
479 // Open the bottom of the region using slot indexes.
480 if (isBottomClosed()) {
481 if (RequireIntervals)
482 static_cast<IntervalPressure&>(P).openBottom(SlotIdx);
484 static_cast<RegionPressure&>(P).openBottom(CurrPos);
487 RegisterOperands RegOpers(TRI, MRI);
488 collectOperands(CurrPos, RegOpers);
490 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
491 unsigned Reg = RegOpers.Uses[i];
492 // Discover live-ins.
493 bool isLive = LiveRegs.contains(Reg);
496 // Kill liveness at last uses.
497 bool lastUse = false;
498 if (RequireIntervals) {
499 const LiveInterval *LI = getInterval(Reg);
500 lastUse = LI && LI->killedAt(SlotIdx);
503 // Allocatable physregs are always single-use before register rewriting.
504 lastUse = !TargetRegisterInfo::isVirtualRegister(Reg);
506 if (lastUse && isLive) {
508 decreaseRegPressure(Reg);
510 else if (!lastUse && !isLive)
511 increaseRegPressure(Reg);
514 // Generate liveness for defs.
515 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
516 unsigned Reg = RegOpers.Defs[i];
517 if (LiveRegs.insert(Reg))
518 increaseRegPressure(Reg);
521 // Boost pressure for all dead defs together.
522 increaseRegPressure(RegOpers.DeadDefs);
523 decreaseRegPressure(RegOpers.DeadDefs);
525 // Find the next instruction.
528 while (CurrPos != MBB->end() && CurrPos->isDebugValue());
532 /// Find the max change in excess pressure across all sets.
533 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec,
534 ArrayRef<unsigned> NewPressureVec,
535 RegPressureDelta &Delta,
536 const RegisterClassInfo *RCI) {
538 unsigned PSetID = ~0U;
539 for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) {
540 unsigned POld = OldPressureVec[i];
541 unsigned PNew = NewPressureVec[i];
542 int PDiff = (int)PNew - (int)POld;
543 if (!PDiff) // No change in this set in the common case.
545 // Only consider change beyond the limit.
546 unsigned Limit = RCI->getRegPressureSetLimit(i);
549 PDiff = 0; // Under the limit
551 PDiff = PNew - Limit; // Just exceeded limit.
553 else if (Limit > PNew)
554 PDiff = Limit - POld; // Just obeyed limit.
556 if (std::abs(PDiff) > std::abs(ExcessUnits)) {
561 Delta.Excess.PSetID = PSetID;
562 Delta.Excess.UnitIncrease = ExcessUnits;
565 /// Find the max change in max pressure that either surpasses a critical PSet
566 /// limit or exceeds the current MaxPressureLimit.
568 /// FIXME: comparing each element of the old and new MaxPressure vectors here is
569 /// silly. It's done now to demonstrate the concept but will go away with a
570 /// RegPressureTracker API change to work with pressure differences.
571 static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec,
572 ArrayRef<unsigned> NewMaxPressureVec,
573 ArrayRef<PressureElement> CriticalPSets,
574 ArrayRef<unsigned> MaxPressureLimit,
575 RegPressureDelta &Delta) {
576 Delta.CriticalMax = PressureElement();
577 Delta.CurrentMax = PressureElement();
579 unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
580 for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) {
581 unsigned POld = OldMaxPressureVec[i];
582 unsigned PNew = NewMaxPressureVec[i];
583 if (PNew == POld) // No change in this set in the common case.
586 while (CritIdx != CritEnd && CriticalPSets[CritIdx].PSetID < i)
589 if (CritIdx != CritEnd && CriticalPSets[CritIdx].PSetID == i) {
590 int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].UnitIncrease;
591 if (PDiff > Delta.CriticalMax.UnitIncrease) {
592 Delta.CriticalMax.PSetID = i;
593 Delta.CriticalMax.UnitIncrease = PDiff;
597 // Find the greatest increase above MaxPressureLimit.
598 // (Ignores negative MDiff).
599 int MDiff = (int)PNew - (int)MaxPressureLimit[i];
600 if (MDiff > Delta.CurrentMax.UnitIncrease) {
601 Delta.CurrentMax.PSetID = i;
602 Delta.CurrentMax.UnitIncrease = MDiff;
607 /// Record the upward impact of a single instruction on current register
608 /// pressure. Unlike the advance/recede pressure tracking interface, this does
609 /// not discover live in/outs.
611 /// This is intended for speculative queries. It leaves pressure inconsistent
612 /// with the current position, so must be restored by the caller.
613 void RegPressureTracker::bumpUpwardPressure(const MachineInstr *MI) {
614 assert(!MI->isDebugValue() && "Expect a nondebug instruction.");
616 // Account for register pressure similar to RegPressureTracker::recede().
617 RegisterOperands RegOpers(TRI, MRI);
618 collectOperands(MI, RegOpers);
620 // Boost max pressure for all dead defs together.
621 // Since CurrSetPressure and MaxSetPressure
622 increaseRegPressure(RegOpers.DeadDefs);
623 decreaseRegPressure(RegOpers.DeadDefs);
625 // Kill liveness at live defs.
626 for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
627 unsigned Reg = RegOpers.Defs[i];
628 if (!containsReg(RegOpers.Uses, Reg))
629 decreaseRegPressure(Reg);
631 // Generate liveness for uses.
632 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
633 unsigned Reg = RegOpers.Uses[i];
634 if (!LiveRegs.contains(Reg))
635 increaseRegPressure(Reg);
639 /// Consider the pressure increase caused by traversing this instruction
640 /// bottom-up. Find the pressure set with the most change beyond its pressure
641 /// limit based on the tracker's current pressure, and return the change in
642 /// number of register units of that pressure set introduced by this
645 /// This assumes that the current LiveOut set is sufficient.
647 /// FIXME: This is expensive for an on-the-fly query. We need to cache the
648 /// result per-SUnit with enough information to adjust for the current
649 /// scheduling position. But this works as a proof of concept.
650 void RegPressureTracker::
651 getMaxUpwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
652 ArrayRef<PressureElement> CriticalPSets,
653 ArrayRef<unsigned> MaxPressureLimit) {
654 // Snapshot Pressure.
655 // FIXME: The snapshot heap space should persist. But I'm planning to
656 // summarize the pressure effect so we don't need to snapshot at all.
657 std::vector<unsigned> SavedPressure = CurrSetPressure;
658 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
660 bumpUpwardPressure(MI);
662 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI);
663 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
664 MaxPressureLimit, Delta);
665 assert(Delta.CriticalMax.UnitIncrease >= 0 &&
666 Delta.CurrentMax.UnitIncrease >= 0 && "cannot decrease max pressure");
668 // Restore the tracker's state.
669 P.MaxSetPressure.swap(SavedMaxPressure);
670 CurrSetPressure.swap(SavedPressure);
673 /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx).
674 static bool findUseBetween(unsigned Reg,
675 SlotIndex PriorUseIdx, SlotIndex NextUseIdx,
676 const MachineRegisterInfo *MRI,
677 const LiveIntervals *LIS) {
678 for (MachineRegisterInfo::use_nodbg_iterator
679 UI = MRI->use_nodbg_begin(Reg), UE = MRI->use_nodbg_end();
680 UI != UE; UI.skipInstruction()) {
681 const MachineInstr* MI = &*UI;
682 if (MI->isDebugValue())
684 SlotIndex InstSlot = LIS->getInstructionIndex(MI).getRegSlot();
685 if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx)
691 /// Record the downward impact of a single instruction on current register
692 /// pressure. Unlike the advance/recede pressure tracking interface, this does
693 /// not discover live in/outs.
695 /// This is intended for speculative queries. It leaves pressure inconsistent
696 /// with the current position, so must be restored by the caller.
697 void RegPressureTracker::bumpDownwardPressure(const MachineInstr *MI) {
698 assert(!MI->isDebugValue() && "Expect a nondebug instruction.");
700 // Account for register pressure similar to RegPressureTracker::recede().
701 RegisterOperands RegOpers(TRI, MRI);
702 collectOperands(MI, RegOpers);
704 // Kill liveness at last uses. Assume allocatable physregs are single-use
705 // rather than checking LiveIntervals.
707 if (RequireIntervals)
708 SlotIdx = LIS->getInstructionIndex(MI).getRegSlot();
710 for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
711 unsigned Reg = RegOpers.Uses[i];
712 if (RequireIntervals) {
713 // FIXME: allow the caller to pass in the list of vreg uses that remain
714 // to be bottom-scheduled to avoid searching uses at each query.
715 SlotIndex CurrIdx = getCurrSlot();
716 const LiveInterval *LI = getInterval(Reg);
717 if (LI && LI->killedAt(SlotIdx)
718 && !findUseBetween(Reg, CurrIdx, SlotIdx, MRI, LIS)) {
719 decreaseRegPressure(Reg);
722 else if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
723 // Allocatable physregs are always single-use before register rewriting.
724 decreaseRegPressure(Reg);
728 // Generate liveness for defs.
729 increaseRegPressure(RegOpers.Defs);
731 // Boost pressure for all dead defs together.
732 increaseRegPressure(RegOpers.DeadDefs);
733 decreaseRegPressure(RegOpers.DeadDefs);
736 /// Consider the pressure increase caused by traversing this instruction
737 /// top-down. Find the register class with the most change in its pressure limit
738 /// based on the tracker's current pressure, and return the number of excess
739 /// register units of that pressure set introduced by this instruction.
741 /// This assumes that the current LiveIn set is sufficient.
742 void RegPressureTracker::
743 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
744 ArrayRef<PressureElement> CriticalPSets,
745 ArrayRef<unsigned> MaxPressureLimit) {
746 // Snapshot Pressure.
747 std::vector<unsigned> SavedPressure = CurrSetPressure;
748 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
750 bumpDownwardPressure(MI);
752 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI);
753 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
754 MaxPressureLimit, Delta);
755 assert(Delta.CriticalMax.UnitIncrease >= 0 &&
756 Delta.CurrentMax.UnitIncrease >= 0 && "cannot decrease max pressure");
758 // Restore the tracker's state.
759 P.MaxSetPressure.swap(SavedMaxPressure);
760 CurrSetPressure.swap(SavedPressure);
763 /// Get the pressure of each PSet after traversing this instruction bottom-up.
764 void RegPressureTracker::
765 getUpwardPressure(const MachineInstr *MI,
766 std::vector<unsigned> &PressureResult,
767 std::vector<unsigned> &MaxPressureResult) {
768 // Snapshot pressure.
769 PressureResult = CurrSetPressure;
770 MaxPressureResult = P.MaxSetPressure;
772 bumpUpwardPressure(MI);
774 // Current pressure becomes the result. Restore current pressure.
775 P.MaxSetPressure.swap(MaxPressureResult);
776 CurrSetPressure.swap(PressureResult);
779 /// Get the pressure of each PSet after traversing this instruction top-down.
780 void RegPressureTracker::
781 getDownwardPressure(const MachineInstr *MI,
782 std::vector<unsigned> &PressureResult,
783 std::vector<unsigned> &MaxPressureResult) {
784 // Snapshot pressure.
785 PressureResult = CurrSetPressure;
786 MaxPressureResult = P.MaxSetPressure;
788 bumpDownwardPressure(MI);
790 // Current pressure becomes the result. Restore current pressure.
791 P.MaxSetPressure.swap(MaxPressureResult);
792 CurrSetPressure.swap(PressureResult);