Fix some comment typos.
[oota-llvm.git] / lib / CodeGen / RegisterPressure.cpp
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                                 PSetIterator PSetI) {
28   unsigned Weight = PSetI.getWeight();
29   for (; PSetI.isValid(); ++PSetI)
30     CurrSetPressure[*PSetI] += Weight;
31 }
32
33 /// Decrease pressure for each pressure set provided by TargetRegisterInfo.
34 static void decreaseSetPressure(std::vector<unsigned> &CurrSetPressure,
35                                 PSetIterator PSetI) {
36   unsigned Weight = PSetI.getWeight();
37   for (; PSetI.isValid(); ++PSetI) {
38     assert(CurrSetPressure[*PSetI] >= Weight && "register pressure underflow");
39     CurrSetPressure[*PSetI] -= Weight;
40   }
41 }
42
43 LLVM_DUMP_METHOD
44 void llvm::dumpRegSetPressure(ArrayRef<unsigned> SetPressure,
45                               const TargetRegisterInfo *TRI) {
46   bool Empty = true;
47   for (unsigned i = 0, e = SetPressure.size(); i < e; ++i) {
48     if (SetPressure[i] != 0) {
49       dbgs() << TRI->getRegPressureSetName(i) << "=" << SetPressure[i] << '\n';
50       Empty = false;
51     }
52   }
53   if (Empty)
54     dbgs() << "\n";
55 }
56
57 LLVM_DUMP_METHOD
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() << PrintVRegOrUnit(LiveInRegs[i], TRI) << " ";
64   dbgs() << '\n';
65   dbgs() << "Live Out: ";
66   for (unsigned i = 0, e = LiveOutRegs.size(); i < e; ++i)
67     dbgs() << PrintVRegOrUnit(LiveOutRegs[i], TRI) << " ";
68   dbgs() << '\n';
69 }
70
71 LLVM_DUMP_METHOD
72 void RegPressureTracker::dump() const {
73   if (!isTopClosed() || !isBottomClosed()) {
74     dbgs() << "Curr Pressure: ";
75     dumpRegSetPressure(CurrSetPressure, TRI);
76   }
77   P.dump(TRI);
78 }
79
80 void PressureDiff::dump(const TargetRegisterInfo &TRI) const {
81   for (const PressureChange &Change : *this) {
82     if (!Change.isValid() || Change.getUnitInc() == 0)
83       continue;
84     dbgs() << "    " << TRI.getRegPressureSetName(Change.getPSet())
85            << " " << Change.getUnitInc();
86   }
87   dbgs() << '\n';
88 }
89
90 /// Increase the current pressure as impacted by these registers and bump
91 /// the high water mark if needed.
92 void RegPressureTracker::increaseRegPressure(ArrayRef<unsigned> RegUnits) {
93   for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
94     PSetIterator PSetI = MRI->getPressureSets(RegUnits[i]);
95     unsigned Weight = PSetI.getWeight();
96     for (; PSetI.isValid(); ++PSetI) {
97       CurrSetPressure[*PSetI] += Weight;
98       if (CurrSetPressure[*PSetI] > P.MaxSetPressure[*PSetI]) {
99         P.MaxSetPressure[*PSetI] = CurrSetPressure[*PSetI];
100       }
101     }
102   }
103 }
104
105 /// Simply decrease the current pressure as impacted by these registers.
106 void RegPressureTracker::decreaseRegPressure(ArrayRef<unsigned> RegUnits) {
107   for (unsigned I = 0, E = RegUnits.size(); I != E; ++I)
108     decreaseSetPressure(CurrSetPressure, MRI->getPressureSets(RegUnits[I]));
109 }
110
111 /// Clear the result so it can be used for another round of pressure tracking.
112 void IntervalPressure::reset() {
113   TopIdx = BottomIdx = SlotIndex();
114   MaxSetPressure.clear();
115   LiveInRegs.clear();
116   LiveOutRegs.clear();
117 }
118
119 /// Clear the result so it can be used for another round of pressure tracking.
120 void RegionPressure::reset() {
121   TopPos = BottomPos = MachineBasicBlock::const_iterator();
122   MaxSetPressure.clear();
123   LiveInRegs.clear();
124   LiveOutRegs.clear();
125 }
126
127 /// If the current top is not less than or equal to the next index, open it.
128 /// We happen to need the SlotIndex for the next top for pressure update.
129 void IntervalPressure::openTop(SlotIndex NextTop) {
130   if (TopIdx <= NextTop)
131     return;
132   TopIdx = SlotIndex();
133   LiveInRegs.clear();
134 }
135
136 /// If the current top is the previous instruction (before receding), open it.
137 void RegionPressure::openTop(MachineBasicBlock::const_iterator PrevTop) {
138   if (TopPos != PrevTop)
139     return;
140   TopPos = MachineBasicBlock::const_iterator();
141   LiveInRegs.clear();
142 }
143
144 /// If the current bottom is not greater than the previous index, open it.
145 void IntervalPressure::openBottom(SlotIndex PrevBottom) {
146   if (BottomIdx > PrevBottom)
147     return;
148   BottomIdx = SlotIndex();
149   LiveInRegs.clear();
150 }
151
152 /// If the current bottom is the previous instr (before advancing), open it.
153 void RegionPressure::openBottom(MachineBasicBlock::const_iterator PrevBottom) {
154   if (BottomPos != PrevBottom)
155     return;
156   BottomPos = MachineBasicBlock::const_iterator();
157   LiveInRegs.clear();
158 }
159
160 const LiveRange *RegPressureTracker::getLiveRange(unsigned Reg) const {
161   if (TargetRegisterInfo::isVirtualRegister(Reg))
162     return &LIS->getInterval(Reg);
163   return LIS->getCachedRegUnit(Reg);
164 }
165
166 void RegPressureTracker::reset() {
167   MBB = nullptr;
168   LIS = nullptr;
169
170   CurrSetPressure.clear();
171   LiveThruPressure.clear();
172   P.MaxSetPressure.clear();
173
174   if (RequireIntervals)
175     static_cast<IntervalPressure&>(P).reset();
176   else
177     static_cast<RegionPressure&>(P).reset();
178
179   LiveRegs.PhysRegs.clear();
180   LiveRegs.VirtRegs.clear();
181   UntiedDefs.clear();
182 }
183
184 /// Setup the RegPressureTracker.
185 ///
186 /// TODO: Add support for pressure without LiveIntervals.
187 void RegPressureTracker::init(const MachineFunction *mf,
188                               const RegisterClassInfo *rci,
189                               const LiveIntervals *lis,
190                               const MachineBasicBlock *mbb,
191                               MachineBasicBlock::const_iterator pos,
192                               bool ShouldTrackUntiedDefs)
193 {
194   reset();
195
196   MF = mf;
197   TRI = MF->getSubtarget().getRegisterInfo();
198   RCI = rci;
199   MRI = &MF->getRegInfo();
200   MBB = mbb;
201   TrackUntiedDefs = ShouldTrackUntiedDefs;
202
203   if (RequireIntervals) {
204     assert(lis && "IntervalPressure requires LiveIntervals");
205     LIS = lis;
206   }
207
208   CurrPos = pos;
209   CurrSetPressure.assign(TRI->getNumRegPressureSets(), 0);
210
211   P.MaxSetPressure = CurrSetPressure;
212
213   LiveRegs.PhysRegs.setUniverse(TRI->getNumRegs());
214   LiveRegs.VirtRegs.setUniverse(MRI->getNumVirtRegs());
215   if (TrackUntiedDefs)
216     UntiedDefs.setUniverse(MRI->getNumVirtRegs());
217 }
218
219 /// Does this pressure result have a valid top position and live ins.
220 bool RegPressureTracker::isTopClosed() const {
221   if (RequireIntervals)
222     return static_cast<IntervalPressure&>(P).TopIdx.isValid();
223   return (static_cast<RegionPressure&>(P).TopPos ==
224           MachineBasicBlock::const_iterator());
225 }
226
227 /// Does this pressure result have a valid bottom position and live outs.
228 bool RegPressureTracker::isBottomClosed() const {
229   if (RequireIntervals)
230     return static_cast<IntervalPressure&>(P).BottomIdx.isValid();
231   return (static_cast<RegionPressure&>(P).BottomPos ==
232           MachineBasicBlock::const_iterator());
233 }
234
235
236 SlotIndex RegPressureTracker::getCurrSlot() const {
237   MachineBasicBlock::const_iterator IdxPos = CurrPos;
238   while (IdxPos != MBB->end() && IdxPos->isDebugValue())
239     ++IdxPos;
240   if (IdxPos == MBB->end())
241     return LIS->getMBBEndIdx(MBB);
242   return LIS->getInstructionIndex(IdxPos).getRegSlot();
243 }
244
245 /// Set the boundary for the top of the region and summarize live ins.
246 void RegPressureTracker::closeTop() {
247   if (RequireIntervals)
248     static_cast<IntervalPressure&>(P).TopIdx = getCurrSlot();
249   else
250     static_cast<RegionPressure&>(P).TopPos = CurrPos;
251
252   assert(P.LiveInRegs.empty() && "inconsistent max pressure result");
253   P.LiveInRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size());
254   P.LiveInRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end());
255   for (SparseSet<unsigned>::const_iterator I =
256          LiveRegs.VirtRegs.begin(), E = LiveRegs.VirtRegs.end(); I != E; ++I)
257     P.LiveInRegs.push_back(*I);
258   std::sort(P.LiveInRegs.begin(), P.LiveInRegs.end());
259   P.LiveInRegs.erase(std::unique(P.LiveInRegs.begin(), P.LiveInRegs.end()),
260                      P.LiveInRegs.end());
261 }
262
263 /// Set the boundary for the bottom of the region and summarize live outs.
264 void RegPressureTracker::closeBottom() {
265   if (RequireIntervals)
266     static_cast<IntervalPressure&>(P).BottomIdx = getCurrSlot();
267   else
268     static_cast<RegionPressure&>(P).BottomPos = CurrPos;
269
270   assert(P.LiveOutRegs.empty() && "inconsistent max pressure result");
271   P.LiveOutRegs.reserve(LiveRegs.PhysRegs.size() + LiveRegs.VirtRegs.size());
272   P.LiveOutRegs.append(LiveRegs.PhysRegs.begin(), LiveRegs.PhysRegs.end());
273   for (SparseSet<unsigned>::const_iterator I =
274          LiveRegs.VirtRegs.begin(), E = LiveRegs.VirtRegs.end(); I != E; ++I)
275     P.LiveOutRegs.push_back(*I);
276   std::sort(P.LiveOutRegs.begin(), P.LiveOutRegs.end());
277   P.LiveOutRegs.erase(std::unique(P.LiveOutRegs.begin(), P.LiveOutRegs.end()),
278                       P.LiveOutRegs.end());
279 }
280
281 /// Finalize the region boundaries and record live ins and live outs.
282 void RegPressureTracker::closeRegion() {
283   if (!isTopClosed() && !isBottomClosed()) {
284     assert(LiveRegs.PhysRegs.empty() && LiveRegs.VirtRegs.empty() &&
285            "no region boundary");
286     return;
287   }
288   if (!isBottomClosed())
289     closeBottom();
290   else if (!isTopClosed())
291     closeTop();
292   // If both top and bottom are closed, do nothing.
293 }
294
295 /// The register tracker is unaware of global liveness so ignores normal
296 /// live-thru ranges. However, two-address or coalesced chains can also lead
297 /// to live ranges with no holes. Count these to inform heuristics that we
298 /// can never drop below this pressure.
299 void RegPressureTracker::initLiveThru(const RegPressureTracker &RPTracker) {
300   LiveThruPressure.assign(TRI->getNumRegPressureSets(), 0);
301   assert(isBottomClosed() && "need bottom-up tracking to intialize.");
302   for (unsigned i = 0, e = P.LiveOutRegs.size(); i < e; ++i) {
303     unsigned Reg = P.LiveOutRegs[i];
304     if (TargetRegisterInfo::isVirtualRegister(Reg)
305         && !RPTracker.hasUntiedDef(Reg)) {
306       increaseSetPressure(LiveThruPressure, MRI->getPressureSets(Reg));
307     }
308   }
309 }
310
311 /// \brief Convenient wrapper for checking membership in RegisterOperands.
312 /// (std::count() doesn't have an early exit).
313 static bool containsReg(ArrayRef<unsigned> RegUnits, unsigned RegUnit) {
314   return std::find(RegUnits.begin(), RegUnits.end(), RegUnit) != RegUnits.end();
315 }
316
317 namespace {
318 /// Collect this instruction's unique uses and defs into SmallVectors for
319 /// processing defs and uses in order.
320 ///
321 /// FIXME: always ignore tied opers
322 class RegisterOperands {
323   const TargetRegisterInfo *TRI;
324   const MachineRegisterInfo *MRI;
325   bool IgnoreDead;
326
327 public:
328   SmallVector<unsigned, 8> Uses;
329   SmallVector<unsigned, 8> Defs;
330   SmallVector<unsigned, 8> DeadDefs;
331
332   RegisterOperands(const TargetRegisterInfo *tri,
333                    const MachineRegisterInfo *mri, bool ID = false):
334     TRI(tri), MRI(mri), IgnoreDead(ID) {}
335
336   /// Push this operand's register onto the correct vector.
337   void collect(const MachineOperand &MO) {
338     if (!MO.isReg() || !MO.getReg())
339       return;
340     if (MO.readsReg())
341       pushRegUnits(MO.getReg(), Uses);
342     if (MO.isDef()) {
343       if (MO.isDead()) {
344         if (!IgnoreDead)
345           pushRegUnits(MO.getReg(), DeadDefs);
346       }
347       else
348         pushRegUnits(MO.getReg(), Defs);
349     }
350   }
351
352 protected:
353   void pushRegUnits(unsigned Reg, SmallVectorImpl<unsigned> &RegUnits) {
354     if (TargetRegisterInfo::isVirtualRegister(Reg)) {
355       if (containsReg(RegUnits, Reg))
356         return;
357       RegUnits.push_back(Reg);
358     }
359     else if (MRI->isAllocatable(Reg)) {
360       for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
361         if (containsReg(RegUnits, *Units))
362           continue;
363         RegUnits.push_back(*Units);
364       }
365     }
366   }
367 };
368 } // namespace
369
370 /// Collect physical and virtual register operands.
371 static void collectOperands(const MachineInstr *MI,
372                             RegisterOperands &RegOpers) {
373   for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI)
374     RegOpers.collect(*OperI);
375
376   // Remove redundant physreg dead defs.
377   SmallVectorImpl<unsigned>::iterator I =
378     std::remove_if(RegOpers.DeadDefs.begin(), RegOpers.DeadDefs.end(),
379                    std::bind1st(std::ptr_fun(containsReg), RegOpers.Defs));
380   RegOpers.DeadDefs.erase(I, RegOpers.DeadDefs.end());
381 }
382
383 /// Initialize an array of N PressureDiffs.
384 void PressureDiffs::init(unsigned N) {
385   Size = N;
386   if (N <= Max) {
387     memset(PDiffArray, 0, N * sizeof(PressureDiff));
388     return;
389   }
390   Max = Size;
391   free(PDiffArray);
392   PDiffArray = reinterpret_cast<PressureDiff*>(calloc(N, sizeof(PressureDiff)));
393 }
394
395 /// Add a change in pressure to the pressure diff of a given instruction.
396 void PressureDiff::addPressureChange(unsigned RegUnit, bool IsDec,
397                                      const MachineRegisterInfo *MRI) {
398   PSetIterator PSetI = MRI->getPressureSets(RegUnit);
399   int Weight = IsDec ? -PSetI.getWeight() : PSetI.getWeight();
400   for (; PSetI.isValid(); ++PSetI) {
401     // Find an existing entry in the pressure diff for this PSet.
402     PressureDiff::iterator I = begin(), E = end();
403     for (; I != E && I->isValid(); ++I) {
404       if (I->getPSet() >= *PSetI)
405         break;
406     }
407     // If all pressure sets are more constrained, skip the remaining PSets.
408     if (I == E)
409       break;
410     // Insert this PressureChange.
411     if (!I->isValid() || I->getPSet() != *PSetI) {
412       PressureChange PTmp = PressureChange(*PSetI);
413       for (PressureDiff::iterator J = I; J != E && PTmp.isValid(); ++J)
414         std::swap(*J,PTmp);
415     }
416     // Update the units for this pressure set.
417     I->setUnitInc(I->getUnitInc() + Weight);
418   }
419 }
420
421 /// Record the pressure difference induced by the given operand list.
422 static void collectPDiff(PressureDiff &PDiff, RegisterOperands &RegOpers,
423                          const MachineRegisterInfo *MRI) {
424   assert(!PDiff.begin()->isValid() && "stale PDiff");
425
426   for (unsigned i = 0, e = RegOpers.Defs.size(); i != e; ++i)
427     PDiff.addPressureChange(RegOpers.Defs[i], true, MRI);
428
429   for (unsigned i = 0, e = RegOpers.Uses.size(); i != e; ++i)
430     PDiff.addPressureChange(RegOpers.Uses[i], false, MRI);
431 }
432
433 /// Force liveness of registers.
434 void RegPressureTracker::addLiveRegs(ArrayRef<unsigned> Regs) {
435   for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
436     if (LiveRegs.insert(Regs[i]))
437       increaseRegPressure(Regs[i]);
438   }
439 }
440
441 /// Add Reg to the live in set and increase max pressure.
442 void RegPressureTracker::discoverLiveIn(unsigned Reg) {
443   assert(!LiveRegs.contains(Reg) && "avoid bumping max pressure twice");
444   if (containsReg(P.LiveInRegs, Reg))
445     return;
446
447   // At live in discovery, unconditionally increase the high water mark.
448   P.LiveInRegs.push_back(Reg);
449   increaseSetPressure(P.MaxSetPressure, MRI->getPressureSets(Reg));
450 }
451
452 /// Add Reg to the live out set and increase max pressure.
453 void RegPressureTracker::discoverLiveOut(unsigned Reg) {
454   assert(!LiveRegs.contains(Reg) && "avoid bumping max pressure twice");
455   if (containsReg(P.LiveOutRegs, Reg))
456     return;
457
458   // At live out discovery, unconditionally increase the high water mark.
459   P.LiveOutRegs.push_back(Reg);
460   increaseSetPressure(P.MaxSetPressure, MRI->getPressureSets(Reg));
461 }
462
463 /// Recede across the previous instruction. If LiveUses is provided, record any
464 /// RegUnits that are made live by the current instruction's uses. This includes
465 /// registers that are both defined and used by the instruction.  If a pressure
466 /// difference pointer is provided record the changes is pressure caused by this
467 /// instruction independent of liveness.
468 bool RegPressureTracker::recede(SmallVectorImpl<unsigned> *LiveUses,
469                                 PressureDiff *PDiff) {
470   // Check for the top of the analyzable region.
471   if (CurrPos == MBB->begin()) {
472     closeRegion();
473     return false;
474   }
475   if (!isBottomClosed())
476     closeBottom();
477
478   // Open the top of the region using block iterators.
479   if (!RequireIntervals && isTopClosed())
480     static_cast<RegionPressure&>(P).openTop(CurrPos);
481
482   // Find the previous instruction.
483   do
484     --CurrPos;
485   while (CurrPos != MBB->begin() && CurrPos->isDebugValue());
486
487   if (CurrPos->isDebugValue()) {
488     closeRegion();
489     return false;
490   }
491   SlotIndex SlotIdx;
492   if (RequireIntervals)
493     SlotIdx = LIS->getInstructionIndex(CurrPos).getRegSlot();
494
495   // Open the top of the region using slot indexes.
496   if (RequireIntervals && isTopClosed())
497     static_cast<IntervalPressure&>(P).openTop(SlotIdx);
498
499   RegisterOperands RegOpers(TRI, MRI);
500   collectOperands(CurrPos, RegOpers);
501
502   if (PDiff)
503     collectPDiff(*PDiff, RegOpers, MRI);
504
505   // Boost pressure for all dead defs together.
506   increaseRegPressure(RegOpers.DeadDefs);
507   decreaseRegPressure(RegOpers.DeadDefs);
508
509   // Kill liveness at live defs.
510   // TODO: consider earlyclobbers?
511   for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
512     unsigned Reg = RegOpers.Defs[i];
513     bool DeadDef = false;
514     if (RequireIntervals) {
515       const LiveRange *LR = getLiveRange(Reg);
516       if (LR) {
517         LiveQueryResult LRQ = LR->Query(SlotIdx);
518         DeadDef = LRQ.isDeadDef();
519       }
520     }
521     if (DeadDef) {
522       // LiveIntervals knows this is a dead even though it's MachineOperand is
523       // not flagged as such. Since this register will not be recorded as
524       // live-out, increase its PDiff value to avoid underflowing pressure.
525       if (PDiff)
526         PDiff->addPressureChange(Reg, false, MRI);
527     } else {
528       if (LiveRegs.erase(Reg))
529         decreaseRegPressure(Reg);
530       else
531         discoverLiveOut(Reg);
532     }
533   }
534
535   // Generate liveness for uses.
536   for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
537     unsigned Reg = RegOpers.Uses[i];
538     if (!LiveRegs.contains(Reg)) {
539       // Adjust liveouts if LiveIntervals are available.
540       if (RequireIntervals) {
541         const LiveRange *LR = getLiveRange(Reg);
542         if (LR) {
543           LiveQueryResult LRQ = LR->Query(SlotIdx);
544           if (!LRQ.isKill() && !LRQ.valueDefined())
545             discoverLiveOut(Reg);
546         }
547       }
548       increaseRegPressure(Reg);
549       LiveRegs.insert(Reg);
550       if (LiveUses && !containsReg(*LiveUses, Reg))
551         LiveUses->push_back(Reg);
552     }
553   }
554   if (TrackUntiedDefs) {
555     for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
556       unsigned Reg = RegOpers.Defs[i];
557       if (TargetRegisterInfo::isVirtualRegister(Reg) && !LiveRegs.contains(Reg))
558         UntiedDefs.insert(Reg);
559     }
560   }
561   return true;
562 }
563
564 /// Advance across the current instruction.
565 bool RegPressureTracker::advance() {
566   assert(!TrackUntiedDefs && "unsupported mode");
567
568   // Check for the bottom of the analyzable region.
569   if (CurrPos == MBB->end()) {
570     closeRegion();
571     return false;
572   }
573   if (!isTopClosed())
574     closeTop();
575
576   SlotIndex SlotIdx;
577   if (RequireIntervals)
578     SlotIdx = getCurrSlot();
579
580   // Open the bottom of the region using slot indexes.
581   if (isBottomClosed()) {
582     if (RequireIntervals)
583       static_cast<IntervalPressure&>(P).openBottom(SlotIdx);
584     else
585       static_cast<RegionPressure&>(P).openBottom(CurrPos);
586   }
587
588   RegisterOperands RegOpers(TRI, MRI);
589   collectOperands(CurrPos, RegOpers);
590
591   for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
592     unsigned Reg = RegOpers.Uses[i];
593     // Discover live-ins.
594     bool isLive = LiveRegs.contains(Reg);
595     if (!isLive)
596       discoverLiveIn(Reg);
597     // Kill liveness at last uses.
598     bool lastUse = false;
599     if (RequireIntervals) {
600       const LiveRange *LR = getLiveRange(Reg);
601       lastUse = LR && LR->Query(SlotIdx).isKill();
602     }
603     else {
604       // Allocatable physregs are always single-use before register rewriting.
605       lastUse = !TargetRegisterInfo::isVirtualRegister(Reg);
606     }
607     if (lastUse && isLive) {
608       LiveRegs.erase(Reg);
609       decreaseRegPressure(Reg);
610     }
611     else if (!lastUse && !isLive)
612       increaseRegPressure(Reg);
613   }
614
615   // Generate liveness for defs.
616   for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
617     unsigned Reg = RegOpers.Defs[i];
618     if (LiveRegs.insert(Reg))
619       increaseRegPressure(Reg);
620   }
621
622   // Boost pressure for all dead defs together.
623   increaseRegPressure(RegOpers.DeadDefs);
624   decreaseRegPressure(RegOpers.DeadDefs);
625
626   // Find the next instruction.
627   do
628     ++CurrPos;
629   while (CurrPos != MBB->end() && CurrPos->isDebugValue());
630   return true;
631 }
632
633 /// Find the max change in excess pressure across all sets.
634 static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec,
635                                        ArrayRef<unsigned> NewPressureVec,
636                                        RegPressureDelta &Delta,
637                                        const RegisterClassInfo *RCI,
638                                        ArrayRef<unsigned> LiveThruPressureVec) {
639   Delta.Excess = PressureChange();
640   for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) {
641     unsigned POld = OldPressureVec[i];
642     unsigned PNew = NewPressureVec[i];
643     int PDiff = (int)PNew - (int)POld;
644     if (!PDiff) // No change in this set in the common case.
645       continue;
646     // Only consider change beyond the limit.
647     unsigned Limit = RCI->getRegPressureSetLimit(i);
648     if (!LiveThruPressureVec.empty())
649       Limit += LiveThruPressureVec[i];
650
651     if (Limit > POld) {
652       if (Limit > PNew)
653         PDiff = 0;            // Under the limit
654       else
655         PDiff = PNew - Limit; // Just exceeded limit.
656     }
657     else if (Limit > PNew)
658       PDiff = Limit - POld;   // Just obeyed limit.
659
660     if (PDiff) {
661       Delta.Excess = PressureChange(i);
662       Delta.Excess.setUnitInc(PDiff);
663       break;
664     }
665   }
666 }
667
668 /// Find the max change in max pressure that either surpasses a critical PSet
669 /// limit or exceeds the current MaxPressureLimit.
670 ///
671 /// FIXME: comparing each element of the old and new MaxPressure vectors here is
672 /// silly. It's done now to demonstrate the concept but will go away with a
673 /// RegPressureTracker API change to work with pressure differences.
674 static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec,
675                                     ArrayRef<unsigned> NewMaxPressureVec,
676                                     ArrayRef<PressureChange> CriticalPSets,
677                                     ArrayRef<unsigned> MaxPressureLimit,
678                                     RegPressureDelta &Delta) {
679   Delta.CriticalMax = PressureChange();
680   Delta.CurrentMax = PressureChange();
681
682   unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
683   for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) {
684     unsigned POld = OldMaxPressureVec[i];
685     unsigned PNew = NewMaxPressureVec[i];
686     if (PNew == POld) // No change in this set in the common case.
687       continue;
688
689     if (!Delta.CriticalMax.isValid()) {
690       while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < i)
691         ++CritIdx;
692
693       if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == i) {
694         int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].getUnitInc();
695         if (PDiff > 0) {
696           Delta.CriticalMax = PressureChange(i);
697           Delta.CriticalMax.setUnitInc(PDiff);
698         }
699       }
700     }
701     // Find the first increase above MaxPressureLimit.
702     // (Ignores negative MDiff).
703     if (!Delta.CurrentMax.isValid() && PNew > MaxPressureLimit[i]) {
704       Delta.CurrentMax = PressureChange(i);
705       Delta.CurrentMax.setUnitInc(PNew - POld);
706       if (CritIdx == CritEnd || Delta.CriticalMax.isValid())
707         break;
708     }
709   }
710 }
711
712 /// Record the upward impact of a single instruction on current register
713 /// pressure. Unlike the advance/recede pressure tracking interface, this does
714 /// not discover live in/outs.
715 ///
716 /// This is intended for speculative queries. It leaves pressure inconsistent
717 /// with the current position, so must be restored by the caller.
718 void RegPressureTracker::bumpUpwardPressure(const MachineInstr *MI) {
719   assert(!MI->isDebugValue() && "Expect a nondebug instruction.");
720
721   // Account for register pressure similar to RegPressureTracker::recede().
722   RegisterOperands RegOpers(TRI, MRI, /*IgnoreDead=*/true);
723   collectOperands(MI, RegOpers);
724
725   // Boost max pressure for all dead defs together.
726   // Since CurrSetPressure and MaxSetPressure
727   increaseRegPressure(RegOpers.DeadDefs);
728   decreaseRegPressure(RegOpers.DeadDefs);
729
730   // Kill liveness at live defs.
731   for (unsigned i = 0, e = RegOpers.Defs.size(); i < e; ++i) {
732     unsigned Reg = RegOpers.Defs[i];
733     bool DeadDef = false;
734     if (RequireIntervals) {
735       const LiveRange *LR = getLiveRange(Reg);
736       if (LR) {
737         SlotIndex SlotIdx = LIS->getInstructionIndex(MI);
738         LiveQueryResult LRQ = LR->Query(SlotIdx);
739         DeadDef = LRQ.isDeadDef();
740       }
741     }
742     if (!DeadDef) {
743       if (!containsReg(RegOpers.Uses, Reg))
744         decreaseRegPressure(Reg);
745     }
746   }
747   // Generate liveness for uses.
748   for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
749     unsigned Reg = RegOpers.Uses[i];
750     if (!LiveRegs.contains(Reg))
751       increaseRegPressure(Reg);
752   }
753 }
754
755 /// Consider the pressure increase caused by traversing this instruction
756 /// bottom-up. Find the pressure set with the most change beyond its pressure
757 /// limit based on the tracker's current pressure, and return the change in
758 /// number of register units of that pressure set introduced by this
759 /// instruction.
760 ///
761 /// This assumes that the current LiveOut set is sufficient.
762 ///
763 /// This is expensive for an on-the-fly query because it calls
764 /// bumpUpwardPressure to recompute the pressure sets based on current
765 /// liveness. This mainly exists to verify correctness, e.g. with
766 /// -verify-misched. getUpwardPressureDelta is the fast version of this query
767 /// that uses the per-SUnit cache of the PressureDiff.
768 void RegPressureTracker::
769 getMaxUpwardPressureDelta(const MachineInstr *MI, PressureDiff *PDiff,
770                           RegPressureDelta &Delta,
771                           ArrayRef<PressureChange> CriticalPSets,
772                           ArrayRef<unsigned> MaxPressureLimit) {
773   // Snapshot Pressure.
774   // FIXME: The snapshot heap space should persist. But I'm planning to
775   // summarize the pressure effect so we don't need to snapshot at all.
776   std::vector<unsigned> SavedPressure = CurrSetPressure;
777   std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
778
779   bumpUpwardPressure(MI);
780
781   computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI,
782                              LiveThruPressure);
783   computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
784                           MaxPressureLimit, Delta);
785   assert(Delta.CriticalMax.getUnitInc() >= 0 &&
786          Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure");
787
788   // Restore the tracker's state.
789   P.MaxSetPressure.swap(SavedMaxPressure);
790   CurrSetPressure.swap(SavedPressure);
791
792 #ifndef NDEBUG
793   if (!PDiff)
794     return;
795
796   // Check if the alternate algorithm yields the same result.
797   RegPressureDelta Delta2;
798   getUpwardPressureDelta(MI, *PDiff, Delta2, CriticalPSets, MaxPressureLimit);
799   if (Delta != Delta2) {
800     dbgs() << "PDiff: ";
801     PDiff->dump(*TRI);
802     dbgs() << "DELTA: " << *MI;
803     if (Delta.Excess.isValid())
804       dbgs() << "Excess1 " << TRI->getRegPressureSetName(Delta.Excess.getPSet())
805              << " " << Delta.Excess.getUnitInc() << "\n";
806     if (Delta.CriticalMax.isValid())
807       dbgs() << "Critic1 " << TRI->getRegPressureSetName(Delta.CriticalMax.getPSet())
808              << " " << Delta.CriticalMax.getUnitInc() << "\n";
809     if (Delta.CurrentMax.isValid())
810       dbgs() << "CurrMx1 " << TRI->getRegPressureSetName(Delta.CurrentMax.getPSet())
811              << " " << Delta.CurrentMax.getUnitInc() << "\n";
812     if (Delta2.Excess.isValid())
813       dbgs() << "Excess2 " << TRI->getRegPressureSetName(Delta2.Excess.getPSet())
814              << " " << Delta2.Excess.getUnitInc() << "\n";
815     if (Delta2.CriticalMax.isValid())
816       dbgs() << "Critic2 " << TRI->getRegPressureSetName(Delta2.CriticalMax.getPSet())
817              << " " << Delta2.CriticalMax.getUnitInc() << "\n";
818     if (Delta2.CurrentMax.isValid())
819       dbgs() << "CurrMx2 " << TRI->getRegPressureSetName(Delta2.CurrentMax.getPSet())
820              << " " << Delta2.CurrentMax.getUnitInc() << "\n";
821     llvm_unreachable("RegP Delta Mismatch");
822   }
823 #endif
824 }
825
826 /// This is the fast version of querying register pressure that does not
827 /// directly depend on current liveness.
828 ///
829 /// @param Delta captures information needed for heuristics.
830 ///
831 /// @param CriticalPSets Are the pressure sets that are known to exceed some
832 /// limit within the region, not necessarily at the current position.
833 ///
834 /// @param MaxPressureLimit Is the max pressure within the region, not
835 /// necessarily at the current position.
836 void RegPressureTracker::
837 getUpwardPressureDelta(const MachineInstr *MI, /*const*/ PressureDiff &PDiff,
838                        RegPressureDelta &Delta,
839                        ArrayRef<PressureChange> CriticalPSets,
840                        ArrayRef<unsigned> MaxPressureLimit) const {
841   unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
842   for (PressureDiff::const_iterator
843          PDiffI = PDiff.begin(), PDiffE = PDiff.end();
844        PDiffI != PDiffE && PDiffI->isValid(); ++PDiffI) {
845
846     unsigned PSetID = PDiffI->getPSet();
847     unsigned Limit = RCI->getRegPressureSetLimit(PSetID);
848     if (!LiveThruPressure.empty())
849       Limit += LiveThruPressure[PSetID];
850
851     unsigned POld = CurrSetPressure[PSetID];
852     unsigned MOld = P.MaxSetPressure[PSetID];
853     unsigned MNew = MOld;
854     // Ignore DeadDefs here because they aren't captured by PressureChange.
855     unsigned PNew = POld + PDiffI->getUnitInc();
856     assert((PDiffI->getUnitInc() >= 0) == (PNew >= POld) && "PSet overflow");
857     if (PNew > MOld)
858       MNew = PNew;
859     // Check if current pressure has exceeded the limit.
860     if (!Delta.Excess.isValid()) {
861       unsigned ExcessInc = 0;
862       if (PNew > Limit)
863         ExcessInc = POld > Limit ? PNew - POld : PNew - Limit;
864       else if (POld > Limit)
865         ExcessInc = Limit - POld;
866       if (ExcessInc) {
867         Delta.Excess = PressureChange(PSetID);
868         Delta.Excess.setUnitInc(ExcessInc);
869       }
870     }
871     // Check if max pressure has exceeded a critical pressure set max.
872     if (MNew == MOld)
873       continue;
874     if (!Delta.CriticalMax.isValid()) {
875       while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < PSetID)
876         ++CritIdx;
877
878       if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == PSetID) {
879         int CritInc = (int)MNew - (int)CriticalPSets[CritIdx].getUnitInc();
880         if (CritInc > 0 && CritInc <= INT16_MAX) {
881           Delta.CriticalMax = PressureChange(PSetID);
882           Delta.CriticalMax.setUnitInc(CritInc);
883         }
884       }
885     }
886     // Check if max pressure has exceeded the current max.
887     if (!Delta.CurrentMax.isValid() && MNew > MaxPressureLimit[PSetID]) {
888       Delta.CurrentMax = PressureChange(PSetID);
889       Delta.CurrentMax.setUnitInc(MNew - MOld);
890     }
891   }
892 }
893
894 /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx).
895 static bool findUseBetween(unsigned Reg,
896                            SlotIndex PriorUseIdx, SlotIndex NextUseIdx,
897                            const MachineRegisterInfo *MRI,
898                            const LiveIntervals *LIS) {
899   for (MachineRegisterInfo::use_instr_nodbg_iterator
900        UI = MRI->use_instr_nodbg_begin(Reg),
901        UE = MRI->use_instr_nodbg_end(); UI != UE; ++UI) {
902       const MachineInstr* MI = &*UI;
903       if (MI->isDebugValue())
904         continue;
905       SlotIndex InstSlot = LIS->getInstructionIndex(MI).getRegSlot();
906       if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx)
907         return true;
908   }
909   return false;
910 }
911
912 /// Record the downward impact of a single instruction on current register
913 /// pressure. Unlike the advance/recede pressure tracking interface, this does
914 /// not discover live in/outs.
915 ///
916 /// This is intended for speculative queries. It leaves pressure inconsistent
917 /// with the current position, so must be restored by the caller.
918 void RegPressureTracker::bumpDownwardPressure(const MachineInstr *MI) {
919   assert(!MI->isDebugValue() && "Expect a nondebug instruction.");
920
921   // Account for register pressure similar to RegPressureTracker::recede().
922   RegisterOperands RegOpers(TRI, MRI);
923   collectOperands(MI, RegOpers);
924
925   // Kill liveness at last uses. Assume allocatable physregs are single-use
926   // rather than checking LiveIntervals.
927   SlotIndex SlotIdx;
928   if (RequireIntervals)
929     SlotIdx = LIS->getInstructionIndex(MI).getRegSlot();
930
931   for (unsigned i = 0, e = RegOpers.Uses.size(); i < e; ++i) {
932     unsigned Reg = RegOpers.Uses[i];
933     if (RequireIntervals) {
934       // FIXME: allow the caller to pass in the list of vreg uses that remain
935       // to be bottom-scheduled to avoid searching uses at each query.
936       SlotIndex CurrIdx = getCurrSlot();
937       const LiveRange *LR = getLiveRange(Reg);
938       if (LR) {
939         LiveQueryResult LRQ = LR->Query(SlotIdx);
940         if (LRQ.isKill() && !findUseBetween(Reg, CurrIdx, SlotIdx, MRI, LIS)) {
941           decreaseRegPressure(Reg);
942         }
943       }
944     }
945     else if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
946       // Allocatable physregs are always single-use before register rewriting.
947       decreaseRegPressure(Reg);
948     }
949   }
950
951   // Generate liveness for defs.
952   increaseRegPressure(RegOpers.Defs);
953
954   // Boost pressure for all dead defs together.
955   increaseRegPressure(RegOpers.DeadDefs);
956   decreaseRegPressure(RegOpers.DeadDefs);
957 }
958
959 /// Consider the pressure increase caused by traversing this instruction
960 /// top-down. Find the register class with the most change in its pressure limit
961 /// based on the tracker's current pressure, and return the number of excess
962 /// register units of that pressure set introduced by this instruction.
963 ///
964 /// This assumes that the current LiveIn set is sufficient.
965 ///
966 /// This is expensive for an on-the-fly query because it calls
967 /// bumpDownwardPressure to recompute the pressure sets based on current
968 /// liveness. We don't yet have a fast version of downward pressure tracking
969 /// analogous to getUpwardPressureDelta.
970 void RegPressureTracker::
971 getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
972                             ArrayRef<PressureChange> CriticalPSets,
973                             ArrayRef<unsigned> MaxPressureLimit) {
974   // Snapshot Pressure.
975   std::vector<unsigned> SavedPressure = CurrSetPressure;
976   std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
977
978   bumpDownwardPressure(MI);
979
980   computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI,
981                              LiveThruPressure);
982   computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
983                           MaxPressureLimit, Delta);
984   assert(Delta.CriticalMax.getUnitInc() >= 0 &&
985          Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure");
986
987   // Restore the tracker's state.
988   P.MaxSetPressure.swap(SavedMaxPressure);
989   CurrSetPressure.swap(SavedPressure);
990 }
991
992 /// Get the pressure of each PSet after traversing this instruction bottom-up.
993 void RegPressureTracker::
994 getUpwardPressure(const MachineInstr *MI,
995                   std::vector<unsigned> &PressureResult,
996                   std::vector<unsigned> &MaxPressureResult) {
997   // Snapshot pressure.
998   PressureResult = CurrSetPressure;
999   MaxPressureResult = P.MaxSetPressure;
1000
1001   bumpUpwardPressure(MI);
1002
1003   // Current pressure becomes the result. Restore current pressure.
1004   P.MaxSetPressure.swap(MaxPressureResult);
1005   CurrSetPressure.swap(PressureResult);
1006 }
1007
1008 /// Get the pressure of each PSet after traversing this instruction top-down.
1009 void RegPressureTracker::
1010 getDownwardPressure(const MachineInstr *MI,
1011                     std::vector<unsigned> &PressureResult,
1012                     std::vector<unsigned> &MaxPressureResult) {
1013   // Snapshot pressure.
1014   PressureResult = CurrSetPressure;
1015   MaxPressureResult = P.MaxSetPressure;
1016
1017   bumpDownwardPressure(MI);
1018
1019   // Current pressure becomes the result. Restore current pressure.
1020   P.MaxSetPressure.swap(MaxPressureResult);
1021   CurrSetPressure.swap(PressureResult);
1022 }