RegisterPressure.cpp 49 KB

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