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 "RegisterClassInfo.h"
16 #include "RegisterPressure.h"
17 #include "llvm/CodeGen/LiveInterval.h"
18 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 /// Increase register pressure for each set impacted by this register class.
27 static void increaseSetPressure(std::vector<unsigned> &CurrSetPressure,
28 std::vector<unsigned> &MaxSetPressure,
29 const TargetRegisterClass *RC,
30 const TargetRegisterInfo *TRI) {
31 unsigned Weight = TRI->getRegClassWeight(RC).RegWeight;
32 for (const int *PSet = TRI->getRegClassPressureSets(RC);
33 *PSet != -1; ++PSet) {
34 CurrSetPressure[*PSet] += Weight;
35 if (&CurrSetPressure != &MaxSetPressure
36 && CurrSetPressure[*PSet] > MaxSetPressure[*PSet]) {
37 MaxSetPressure[*PSet] = CurrSetPressure[*PSet];
42 /// Decrease register pressure for each set impacted by this register class.
43 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure,
44 const TargetRegisterClass *RC,
45 const TargetRegisterInfo *TRI) {
46 unsigned Weight = TRI->getRegClassWeight(RC).RegWeight;
47 for (const int *PSet = TRI->getRegClassPressureSets(RC);
48 *PSet != -1; ++PSet) {
49 assert(CurrSetPressure[*PSet] >= Weight && "register pressure underflow");
50 CurrSetPressure[*PSet] -= Weight;
54 /// Directly increase pressure only within this RegisterPressure result.
55 void RegisterPressure::increase(const TargetRegisterClass *RC,
56 const TargetRegisterInfo *TRI) {
57 increaseSetPressure(MaxSetPressure, MaxSetPressure, RC, TRI);
60 /// Directly decrease pressure only within this RegisterPressure result.
61 void RegisterPressure::decrease(const TargetRegisterClass *RC,
62 const TargetRegisterInfo *TRI) {
63 decreaseSetPressure(MaxSetPressure, RC, TRI);
66 void RegisterPressure::dump(const TargetRegisterInfo *TRI) {
67 dbgs() << "Live In: ";
68 for (unsigned i = 0, e = LiveInRegs.size(); i < e; ++i)
69 dbgs() << PrintReg(LiveInRegs[i], TRI) << " ";
71 dbgs() << "Live Out: ";
72 for (unsigned i = 0, e = LiveOutRegs.size(); i < e; ++i)
73 dbgs() << PrintReg(LiveOutRegs[i], TRI) << " ";
75 for (unsigned i = 0, e = MaxSetPressure.size(); i < e; ++i) {
76 if (MaxSetPressure[i] != 0)
77 dbgs() << TRI->getRegPressureSetName(i) << "=" << MaxSetPressure[i]
82 /// Increase the current pressure as impacted by these physical registers and
83 /// bump the high water mark if needed.
84 void RegPressureTracker::increasePhysRegPressure(ArrayRef<unsigned> Regs) {
85 for (unsigned I = 0, E = Regs.size(); I != E; ++I)
86 increaseSetPressure(CurrSetPressure, P.MaxSetPressure,
87 TRI->getMinimalPhysRegClass(Regs[I]), TRI);
90 /// Simply decrease the current pressure as impacted by these physcial
92 void RegPressureTracker::decreasePhysRegPressure(ArrayRef<unsigned> Regs) {
93 for (unsigned I = 0, E = Regs.size(); I != E; ++I)
94 decreaseSetPressure(CurrSetPressure, TRI->getMinimalPhysRegClass(Regs[I]),
98 /// Increase the current pressure as impacted by these virtual registers and
99 /// bump the high water mark if needed.
100 void RegPressureTracker::increaseVirtRegPressure(ArrayRef<unsigned> Regs) {
101 for (unsigned I = 0, E = Regs.size(); I != E; ++I)
102 increaseSetPressure(CurrSetPressure, P.MaxSetPressure,
103 MRI->getRegClass(Regs[I]), TRI);
106 /// Simply decrease the current pressure as impacted by these virtual registers.
107 void RegPressureTracker::decreaseVirtRegPressure(ArrayRef<unsigned> Regs) {
108 for (unsigned I = 0, E = Regs.size(); I != E; ++I)
109 decreaseSetPressure(CurrSetPressure, MRI->getRegClass(Regs[I]), TRI);
112 /// Clear the result so it can be used for another round of pressure tracking.
113 void IntervalPressure::reset() {
114 TopIdx = BottomIdx = SlotIndex();
115 MaxSetPressure.clear();
120 /// Clear the result so it can be used for another round of pressure tracking.
121 void RegionPressure::reset() {
122 TopPos = BottomPos = MachineBasicBlock::const_iterator();
123 MaxSetPressure.clear();
128 /// If the current top is not less than or equal to the next index, open it.
129 /// We happen to need the SlotIndex for the next top for pressure update.
130 void IntervalPressure::openTop(SlotIndex NextTop) {
131 if (TopIdx <= NextTop)
133 TopIdx = SlotIndex();
137 /// If the current top is the previous instruction (before receding), open it.
138 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) {
139 if (TopPos != PrevTop)
141 TopPos = MachineBasicBlock::const_iterator();
145 /// If the current bottom is not greater than the previous index, open it.
146 void IntervalPressure::openBottom(SlotIndex PrevBottom) {
147 if (BottomIdx > PrevBottom)
149 BottomIdx = SlotIndex();
153 /// If the current bottom is the previous instr (before advancing), open it.
154 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) {
155 if (BottomPos != PrevBottom)
157 BottomPos = MachineBasicBlock::const_iterator();
161 /// Setup the RegPressureTracker.
163 /// TODO: Add support for pressure without LiveIntervals.
164 void RegPressureTracker::init(const MachineFunction *mf,
165 const RegisterClassInfo *rci,
166 const LiveIntervals *lis,
167 const MachineBasicBlock *mbb,
168 MachineBasicBlock::const_iterator pos)
171 TRI = MF->getTarget().getRegisterInfo();
173 MRI = &MF->getRegInfo();
176 if (RequireIntervals) {
177 assert(lis && "IntervalPressure requires LiveIntervals");
182 while (CurrPos != MBB->end() && CurrPos->isDebugValue())
185 CurrSetPressure.assign(TRI->getNumRegPressureSets(), 0);
187 if (RequireIntervals)
188 static_cast<IntervalPressure&>(P).reset();
190 static_cast<RegionPressure&>(P).reset();
191 P.MaxSetPressure = CurrSetPressure;
193 LivePhysRegs.clear();
194 LivePhysRegs.setUniverse(TRI->getNumRegs());
195 LiveVirtRegs.clear();
196 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
199 /// Does this pressure result have a valid top position and live ins.
200 bool RegPressureTracker::isTopClosed() const {
201 if (RequireIntervals)
202 return static_cast<IntervalPressure&>(P).TopIdx.isValid();
203 return (static_cast<RegionPressure&>(P).TopPos ==
204 MachineBasicBlock::const_iterator());
207 /// Does this pressure result have a valid bottom position and live outs.
208 bool RegPressureTracker::isBottomClosed() const {
209 if (RequireIntervals)
210 return static_cast<IntervalPressure&>(P).BottomIdx.isValid();
211 return (static_cast<RegionPressure&>(P).BottomPos ==
212 MachineBasicBlock::const_iterator());
215 /// Set the boundary for the top of the region and summarize live ins.
216 void RegPressureTracker::closeTop() {
217 if (RequireIntervals)
218 static_cast<IntervalPressure&>(P).TopIdx =
219 LIS->getInstructionIndex(CurrPos).getRegSlot();
221 static_cast<RegionPressure&>(P).TopPos = CurrPos;
223 assert(P.LiveInRegs.empty() && "inconsistent max pressure result");
224 P.LiveInRegs.reserve(LivePhysRegs.size() + LiveVirtRegs.size());
225 P.LiveInRegs.append(LivePhysRegs.begin(), LivePhysRegs.end());
226 for (SparseSet<unsigned>::const_iterator I =
227 LiveVirtRegs.begin(), E = LiveVirtRegs.end(); I != E; ++I)
228 P.LiveInRegs.push_back(*I);
229 std::sort(P.LiveInRegs.begin(), P.LiveInRegs.end());
230 P.LiveInRegs.erase(std::unique(P.LiveInRegs.begin(), P.LiveInRegs.end()),
234 /// Set the boundary for the bottom of the region and summarize live outs.
235 void RegPressureTracker::closeBottom() {
236 if (RequireIntervals)
237 if (CurrPos == MBB->end())
238 static_cast<IntervalPressure&>(P).BottomIdx = LIS->getMBBEndIdx(MBB);
240 static_cast<IntervalPressure&>(P).BottomIdx =
241 LIS->getInstructionIndex(CurrPos).getRegSlot();
243 static_cast<RegionPressure&>(P).BottomPos = CurrPos;
245 assert(P.LiveOutRegs.empty() && "inconsistent max pressure result");
246 P.LiveOutRegs.reserve(LivePhysRegs.size() + LiveVirtRegs.size());
247 P.LiveOutRegs.append(LivePhysRegs.begin(), LivePhysRegs.end());
248 for (SparseSet<unsigned>::const_iterator I =
249 LiveVirtRegs.begin(), E = LiveVirtRegs.end(); I != E; ++I)
250 P.LiveOutRegs.push_back(*I);
251 std::sort(P.LiveOutRegs.begin(), P.LiveOutRegs.end());
252 P.LiveOutRegs.erase(std::unique(P.LiveOutRegs.begin(), P.LiveOutRegs.end()),
253 P.LiveOutRegs.end());
256 /// Finalize the region boundaries and record live ins and live outs.
257 void RegPressureTracker::closeRegion() {
258 if (!isTopClosed() && !isBottomClosed()) {
259 assert(LivePhysRegs.empty() && LiveVirtRegs.empty() &&
260 "no region boundary");
263 if (!isBottomClosed())
265 else if (!isTopClosed())
267 // If both top and bottom are closed, do nothing.
270 /// Return true if Reg aliases a register in Regs SparseSet.
271 static bool hasRegAlias(unsigned Reg, SparseSet<unsigned> &Regs,
272 const TargetRegisterInfo *TRI) {
273 assert(!TargetRegisterInfo::isVirtualRegister(Reg) && "only for physregs");
274 for (const uint16_t *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) {
275 if (Regs.count(*Alias))
281 /// Return true if Reg aliases a register in unsorted Regs SmallVector.
282 /// This is only valid for physical registers.
283 static SmallVectorImpl<unsigned>::iterator
284 findRegAlias(unsigned Reg, SmallVectorImpl<unsigned> &Regs,
285 const TargetRegisterInfo *TRI) {
286 for (const uint16_t *Alias = TRI->getOverlaps(Reg); *Alias; ++Alias) {
287 SmallVectorImpl<unsigned>::iterator I =
288 std::find(Regs.begin(), Regs.end(), *Alias);
295 /// Return true if Reg can be inserted into Regs SmallVector. For virtual
296 /// register, do a linear search. For physical registers check for aliases.
297 static SmallVectorImpl<unsigned>::iterator
298 findReg(unsigned Reg, bool isVReg, SmallVectorImpl<unsigned> &Regs,
299 const TargetRegisterInfo *TRI) {
301 return std::find(Regs.begin(), Regs.end(), Reg);
302 return findRegAlias(Reg, Regs, TRI);
305 /// Collect this instruction's unique uses and defs into SmallVectors for
306 /// processing defs and uses in order.
307 template<bool isVReg>
308 struct RegisterOperands {
309 SmallVector<unsigned, 8> Uses;
310 SmallVector<unsigned, 8> Defs;
311 SmallVector<unsigned, 8> DeadDefs;
313 /// Push this operand's register onto the correct vector.
314 void collect(const MachineOperand &MO, const TargetRegisterInfo *TRI) {
316 if (findReg(MO.getReg(), isVReg, Uses, TRI) == Uses.end())
317 Uses.push_back(MO.getReg());
321 if (findReg(MO.getReg(), isVReg, DeadDefs, TRI) == DeadDefs.end())
322 DeadDefs.push_back(MO.getReg());
325 if (findReg(MO.getReg(), isVReg, Defs, TRI) == Defs.end())
326 Defs.push_back(MO.getReg());
331 typedef RegisterOperands<false> PhysRegOperands;
332 typedef RegisterOperands<true> VirtRegOperands;
334 /// Collect physical and virtual register operands.
335 static void collectOperands(const MachineInstr *MI,
336 PhysRegOperands &PhysRegOpers,
337 VirtRegOperands &VirtRegOpers,
338 const TargetRegisterInfo *TRI,
339 const RegisterClassInfo *RCI) {
340 for(ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI) {
341 const MachineOperand &MO = *OperI;
342 if (!MO.isReg() || !MO.getReg())
345 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
346 VirtRegOpers.collect(MO, TRI);
347 else if (RCI->isAllocatable(MO.getReg()))
348 PhysRegOpers.collect(MO, TRI);
350 // Remove redundant physreg dead defs.
351 for (unsigned i = PhysRegOpers.DeadDefs.size(); i > 0; --i) {
352 unsigned Reg = PhysRegOpers.DeadDefs[i-1];
353 if (findRegAlias(Reg, PhysRegOpers.Defs, TRI) != PhysRegOpers.Defs.end())
354 PhysRegOpers.DeadDefs.erase(&PhysRegOpers.DeadDefs[i-1]);
358 /// Force liveness of registers.
359 void RegPressureTracker::addLiveRegs(ArrayRef<unsigned> Regs) {
360 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
361 if (TargetRegisterInfo::isVirtualRegister(Regs[i])) {
362 if (LiveVirtRegs.insert(Regs[i]).second)
363 increaseVirtRegPressure(Regs[i]);
366 if (!hasRegAlias(Regs[i], LivePhysRegs, TRI)) {
367 LivePhysRegs.insert(Regs[i]);
368 increasePhysRegPressure(Regs[i]);
374 /// Add PhysReg to the live in set and increase max pressure.
375 void RegPressureTracker::discoverPhysLiveIn(unsigned Reg) {
376 assert(!LivePhysRegs.count(Reg) && "avoid bumping max pressure twice");
377 if (findRegAlias(Reg, P.LiveInRegs, TRI) != P.LiveInRegs.end())
380 // At live in discovery, unconditionally increase the high water mark.
381 P.LiveInRegs.push_back(Reg);
382 P.increase(TRI->getMinimalPhysRegClass(Reg), TRI);
385 /// Add PhysReg to the live out set and increase max pressure.
386 void RegPressureTracker::discoverPhysLiveOut(unsigned Reg) {
387 assert(!LivePhysRegs.count(Reg) && "avoid bumping max pressure twice");
388 if (findRegAlias(Reg, P.LiveOutRegs, TRI) != P.LiveOutRegs.end())
391 // At live out discovery, unconditionally increase the high water mark.
392 P.LiveOutRegs.push_back(Reg);
393 P.increase(TRI->getMinimalPhysRegClass(Reg), TRI);
396 /// Add VirtReg to the live in set and increase max pressure.
397 void RegPressureTracker::discoverVirtLiveIn(unsigned Reg) {
398 assert(!LiveVirtRegs.count(Reg) && "avoid bumping max pressure twice");
399 if (std::find(P.LiveInRegs.begin(), P.LiveInRegs.end(), Reg) !=
403 // At live in discovery, unconditionally increase the high water mark.
404 P.LiveInRegs.push_back(Reg);
405 P.increase(MRI->getRegClass(Reg), TRI);
408 /// Add VirtReg to the live out set and increase max pressure.
409 void RegPressureTracker::discoverVirtLiveOut(unsigned Reg) {
410 assert(!LiveVirtRegs.count(Reg) && "avoid bumping max pressure twice");
411 if (std::find(P.LiveOutRegs.begin(), P.LiveOutRegs.end(), Reg) !=
415 // At live out discovery, unconditionally increase the high water mark.
416 P.LiveOutRegs.push_back(Reg);
417 P.increase(MRI->getRegClass(Reg), TRI);
420 /// Recede across the previous instruction.
421 bool RegPressureTracker::recede() {
422 // Check for the top of the analyzable region.
423 if (CurrPos == MBB->begin()) {
427 if (!isBottomClosed())
430 // Open the top of the region using block iterators.
431 if (!RequireIntervals && isTopClosed())
432 static_cast<RegionPressure&>(P).openTop(CurrPos);
434 // Find the previous instruction.
437 while (CurrPos != MBB->begin() && CurrPos->isDebugValue());
439 if (CurrPos->isDebugValue()) {
444 if (RequireIntervals)
445 SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
447 // Open the top of the region using slot indexes.
448 if (RequireIntervals && isTopClosed())
449 static_cast<IntervalPressure&>(P).openTop(SlotIdx);
451 PhysRegOperands PhysRegOpers;
452 VirtRegOperands VirtRegOpers;
453 collectOperands(CurrPos, PhysRegOpers, VirtRegOpers, TRI, RCI);
455 // Boost pressure for all dead defs together.
456 increasePhysRegPressure(PhysRegOpers.DeadDefs);
457 increaseVirtRegPressure(VirtRegOpers.DeadDefs);
458 decreasePhysRegPressure(PhysRegOpers.DeadDefs);
459 decreaseVirtRegPressure(VirtRegOpers.DeadDefs);
461 // Kill liveness at live defs.
462 // TODO: consider earlyclobbers?
463 for (unsigned i = 0, e = PhysRegOpers.Defs.size(); i < e; ++i) {
464 unsigned Reg = PhysRegOpers.Defs[i];
465 if (LivePhysRegs.erase(Reg))
466 decreasePhysRegPressure(Reg);
468 discoverPhysLiveOut(Reg);
470 for (unsigned i = 0, e = VirtRegOpers.Defs.size(); i < e; ++i) {
471 unsigned Reg = VirtRegOpers.Defs[i];
472 if (LiveVirtRegs.erase(Reg))
473 decreaseVirtRegPressure(Reg);
475 discoverVirtLiveOut(Reg);
478 // Generate liveness for uses.
479 for (unsigned i = 0, e = PhysRegOpers.Uses.size(); i < e; ++i) {
480 unsigned Reg = PhysRegOpers.Uses[i];
481 if (!hasRegAlias(Reg, LivePhysRegs, TRI)) {
482 increasePhysRegPressure(Reg);
483 LivePhysRegs.insert(Reg);
486 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) {
487 unsigned Reg = VirtRegOpers.Uses[i];
488 if (!LiveVirtRegs.count(Reg)) {
489 // Adjust liveouts if LiveIntervals are available.
490 if (RequireIntervals) {
491 const LiveInterval *LI = &LIS->getInterval(Reg);
492 if (!LI->killedAt(SlotIdx))
493 discoverVirtLiveOut(Reg);
495 increaseVirtRegPressure(Reg);
496 LiveVirtRegs.insert(Reg);
502 /// Advance across the current instruction.
503 bool RegPressureTracker::advance() {
504 // Check for the bottom of the analyzable region.
505 if (CurrPos == MBB->end()) {
513 if (RequireIntervals)
514 SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
516 // Open the bottom of the region using slot indexes.
517 if (isBottomClosed()) {
518 if (RequireIntervals)
519 static_cast<IntervalPressure&>(P).openBottom(SlotIdx);
521 static_cast<RegionPressure&>(P).openBottom(CurrPos);
524 PhysRegOperands PhysRegOpers;
525 VirtRegOperands VirtRegOpers;
526 collectOperands(CurrPos, PhysRegOpers, VirtRegOpers, TRI, RCI);
528 // Kill liveness at last uses.
529 for (unsigned i = 0, e = PhysRegOpers.Uses.size(); i < e; ++i) {
530 unsigned Reg = PhysRegOpers.Uses[i];
531 if (!hasRegAlias(Reg, LivePhysRegs, TRI))
532 discoverPhysLiveIn(Reg);
534 // Allocatable physregs are always single-use before regalloc.
535 decreasePhysRegPressure(Reg);
536 LivePhysRegs.erase(Reg);
539 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) {
540 unsigned Reg = VirtRegOpers.Uses[i];
541 if (RequireIntervals) {
542 const LiveInterval *LI = &LIS->getInterval(Reg);
543 if (LI->killedAt(SlotIdx)) {
544 if (LiveVirtRegs.erase(Reg))
545 decreaseVirtRegPressure(Reg);
547 discoverVirtLiveIn(Reg);
550 else if (!LiveVirtRegs.count(Reg)) {
551 discoverVirtLiveIn(Reg);
552 increaseVirtRegPressure(Reg);
556 // Generate liveness for defs.
557 for (unsigned i = 0, e = PhysRegOpers.Defs.size(); i < e; ++i) {
558 unsigned Reg = PhysRegOpers.Defs[i];
559 if (!hasRegAlias(Reg, LivePhysRegs, TRI)) {
560 increasePhysRegPressure(Reg);
561 LivePhysRegs.insert(Reg);
564 for (unsigned i = 0, e = VirtRegOpers.Defs.size(); i < e; ++i) {
565 unsigned Reg = VirtRegOpers.Defs[i];
566 if (LiveVirtRegs.insert(Reg).second)
567 increaseVirtRegPressure(Reg);
570 // Boost pressure for all dead defs together.
571 increasePhysRegPressure(PhysRegOpers.DeadDefs);
572 increaseVirtRegPressure(VirtRegOpers.DeadDefs);
573 decreasePhysRegPressure(PhysRegOpers.DeadDefs);
574 decreaseVirtRegPressure(VirtRegOpers.DeadDefs);
576 // Find the next instruction.
579 while (CurrPos != MBB->end() && CurrPos->isDebugValue());
583 /// Find the max change in excess pressure across all sets.
584 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec,
585 ArrayRef<unsigned> NewPressureVec,
586 RegPressureDelta &Delta,
587 const TargetRegisterInfo *TRI) {
589 unsigned PSetID = ~0U;
590 for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) {
591 unsigned POld = OldPressureVec[i];
592 unsigned PNew = NewPressureVec[i];
593 int PDiff = (int)PNew - (int)POld;
594 if (!PDiff) // No change in this set in the common case.
596 // Only consider change beyond the limit.
597 unsigned Limit = TRI->getRegPressureSetLimit(i);
600 PDiff = 0; // Under the limit
602 PDiff = PNew - Limit; // Just exceeded limit.
604 else if (Limit > PNew)
605 PDiff = Limit - POld; // Just obeyed limit.
607 if (std::abs(PDiff) > std::abs(ExcessUnits)) {
612 Delta.Excess.PSetID = PSetID;
613 Delta.Excess.UnitIncrease = ExcessUnits;
616 /// Find the max change in max pressure that either surpasses a critical PSet
617 /// limit or exceeds the current MaxPressureLimit.
619 /// FIXME: comparing each element of the old and new MaxPressure vectors here is
620 /// silly. It's done now to demonstrate the concept but will go away with a
621 /// RegPressureTracker API change to work with pressure differences.
622 static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec,
623 ArrayRef<unsigned> NewMaxPressureVec,
624 ArrayRef<PressureElement> CriticalPSets,
625 ArrayRef<unsigned> MaxPressureLimit,
626 RegPressureDelta &Delta) {
627 Delta.CriticalMax = PressureElement();
628 Delta.CurrentMax = PressureElement();
630 unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
631 for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) {
632 unsigned POld = OldMaxPressureVec[i];
633 unsigned PNew = NewMaxPressureVec[i];
634 if (PNew == POld) // No change in this set in the common case.
637 while (CritIdx != CritEnd && CriticalPSets[CritIdx].PSetID < i)
640 if (CritIdx != CritEnd && CriticalPSets[CritIdx].PSetID == i) {
641 int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].UnitIncrease;
642 if (PDiff > Delta.CriticalMax.UnitIncrease) {
643 Delta.CriticalMax.PSetID = i;
644 Delta.CriticalMax.UnitIncrease = PDiff;
648 // Find the greatest increase above MaxPressureLimit.
649 // (Ignores negative MDiff).
650 int MDiff = (int)PNew - (int)MaxPressureLimit[i];
651 if (MDiff > Delta.CurrentMax.UnitIncrease) {
652 Delta.CurrentMax.PSetID = i;
653 Delta.CurrentMax.UnitIncrease = PNew;
658 /// Consider the pressure increase caused by traversing this instruction
659 /// bottom-up. Find the pressure set with the most change beyond its pressure
660 /// limit based on the tracker's current pressure, and return the change in
661 /// number of register units of that pressure set introduced by this
664 /// This assumes that the current LiveOut set is sufficient.
666 /// FIXME: This is expensive for an on-the-fly query. We need to cache the
667 /// result per-SUnit with enough information to adjust for the current
668 /// scheduling position. But this works as a proof of concept.
669 void RegPressureTracker::
670 getMaxUpwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
671 ArrayRef<PressureElement> CriticalPSets,
672 ArrayRef<unsigned> MaxPressureLimit) {
673 // Account for register pressure similar to RegPressureTracker::recede().
674 PhysRegOperands PhysRegOpers;
675 VirtRegOperands VirtRegOpers;
676 collectOperands(MI, PhysRegOpers, VirtRegOpers, TRI, RCI);
678 // Snapshot Pressure.
679 // FIXME: The snapshot heap space should persist. But I'm planning to
680 // summarize the pressure effect so we don't need to snapshot at all.
681 std::vector<unsigned> SavedPressure = CurrSetPressure;
682 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
684 // Boost max pressure for all dead defs together.
685 // Since CurrSetPressure and MaxSetPressure
686 increasePhysRegPressure(PhysRegOpers.DeadDefs);
687 increaseVirtRegPressure(VirtRegOpers.DeadDefs);
688 decreasePhysRegPressure(PhysRegOpers.DeadDefs);
689 decreaseVirtRegPressure(VirtRegOpers.DeadDefs);
691 // Kill liveness at live defs.
692 decreasePhysRegPressure(PhysRegOpers.Defs);
693 decreaseVirtRegPressure(VirtRegOpers.Defs);
695 // Generate liveness for uses.
696 for (unsigned i = 0, e = PhysRegOpers.Uses.size(); i < e; ++i) {
697 unsigned Reg = PhysRegOpers.Uses[i];
698 if (!hasRegAlias(Reg, LivePhysRegs, TRI)
699 && (findRegAlias(Reg, PhysRegOpers.Defs, TRI)
700 == PhysRegOpers.Defs.end())) {
701 increasePhysRegPressure(Reg);
704 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) {
705 unsigned Reg = VirtRegOpers.Uses[i];
706 if (!LiveVirtRegs.count(Reg)
707 && (std::find(VirtRegOpers.Defs.begin(), VirtRegOpers.Defs.end(), Reg)
708 != VirtRegOpers.Defs.end())) {
709 increaseVirtRegPressure(Reg);
712 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, TRI);
713 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
714 MaxPressureLimit, Delta);
715 assert(Delta.CriticalMax.UnitIncrease >= 0 &&
716 Delta.CurrentMax.UnitIncrease >= 0 && "cannot decrease max pressure");
718 // Restore the tracker's state.
719 P.MaxSetPressure.swap(SavedMaxPressure);
720 CurrSetPressure.swap(SavedPressure);
723 /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx).
724 static bool findUseBetween(unsigned Reg,
725 SlotIndex PriorUseIdx, SlotIndex NextUseIdx,
726 const MachineRegisterInfo *MRI,
727 const LiveIntervals *LIS) {
728 for (MachineRegisterInfo::use_nodbg_iterator
729 UI = MRI->use_nodbg_begin(Reg), UE = MRI->use_nodbg_end();
730 UI != UE; UI.skipInstruction()) {
731 const MachineInstr* MI = &*UI;
732 SlotIndex InstSlot = LIS->getInstructionIndex(MI).getRegSlot();
733 if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx)
739 /// Consider the pressure increase caused by traversing this instruction
740 /// top-down. Find the register class with the most change in its pressure limit
741 /// based on the tracker's current pressure, and return the number of excess
742 /// register units of that pressure set introduced by this instruction.
744 /// This assumes that the current LiveIn set is sufficient.
745 void RegPressureTracker::
746 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
747 ArrayRef<PressureElement> CriticalPSets,
748 ArrayRef<unsigned> MaxPressureLimit) {
749 // Account for register pressure similar to RegPressureTracker::recede().
750 PhysRegOperands PhysRegOpers;
751 VirtRegOperands VirtRegOpers;
752 collectOperands(MI, PhysRegOpers, VirtRegOpers, TRI, RCI);
754 // Snapshot Pressure.
755 std::vector<unsigned> SavedPressure = CurrSetPressure;
756 std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
758 // Kill liveness at last uses. Assume allocatable physregs are single-use
759 // rather than checking LiveIntervals.
760 decreasePhysRegPressure(PhysRegOpers.Uses);
761 if (RequireIntervals) {
762 SlotIndex SlotIdx = LIS->getInstructionIndex(MI).getRegSlot();
763 for (unsigned i = 0, e = VirtRegOpers.Uses.size(); i < e; ++i) {
764 unsigned Reg = VirtRegOpers.Uses[i];
765 const LiveInterval *LI = &LIS->getInterval(Reg);
766 // FIXME: allow the caller to pass in the list of vreg uses that remain to
767 // be bottom-scheduled to avoid searching uses at each query.
768 SlotIndex CurrIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
769 if (LI->killedAt(SlotIdx)
770 && !findUseBetween(Reg, CurrIdx, SlotIdx, MRI, LIS)) {
771 decreaseVirtRegPressure(Reg);
776 // Generate liveness for defs.
777 increasePhysRegPressure(PhysRegOpers.Defs);
778 increaseVirtRegPressure(VirtRegOpers.Defs);
780 // Boost pressure for all dead defs together.
781 increasePhysRegPressure(PhysRegOpers.DeadDefs);
782 increaseVirtRegPressure(VirtRegOpers.DeadDefs);
783 decreasePhysRegPressure(PhysRegOpers.DeadDefs);
784 decreaseVirtRegPressure(VirtRegOpers.DeadDefs);
786 computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, TRI);
787 computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
788 MaxPressureLimit, Delta);
789 assert(Delta.CriticalMax.UnitIncrease >= 0 &&
790 Delta.CurrentMax.UnitIncrease >= 0 && "cannot decrease max pressure");
792 // Restore the tracker's state.
793 P.MaxSetPressure.swap(SavedMaxPressure);
794 CurrSetPressure.swap(SavedPressure);