RegisterPressure.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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 (Register::isVirtualRegister(RegUnit)
  321. && !RPTracker.hasUntiedDef(RegUnit))
  322. increaseSetPressure(LiveThruPressure, *MRI, RegUnit,
  323. LaneBitmask::getNone(), Pair.LaneMask);
  324. }
  325. }
  326. static LaneBitmask getRegLanes(ArrayRef<RegisterMaskPair> RegUnits,
  327. Register RegUnit) {
  328. auto I = llvm::find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) {
  329. return Other.RegUnit == RegUnit;
  330. });
  331. if (I == RegUnits.end())
  332. return LaneBitmask::getNone();
  333. return I->LaneMask;
  334. }
  335. static void addRegLanes(SmallVectorImpl<RegisterMaskPair> &RegUnits,
  336. RegisterMaskPair Pair) {
  337. Register RegUnit = Pair.RegUnit;
  338. assert(Pair.LaneMask.any());
  339. auto I = llvm::find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) {
  340. return Other.RegUnit == RegUnit;
  341. });
  342. if (I == RegUnits.end()) {
  343. RegUnits.push_back(Pair);
  344. } else {
  345. I->LaneMask |= Pair.LaneMask;
  346. }
  347. }
  348. static void setRegZero(SmallVectorImpl<RegisterMaskPair> &RegUnits,
  349. Register RegUnit) {
  350. auto I = llvm::find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) {
  351. return Other.RegUnit == RegUnit;
  352. });
  353. if (I == RegUnits.end()) {
  354. RegUnits.push_back(RegisterMaskPair(RegUnit, LaneBitmask::getNone()));
  355. } else {
  356. I->LaneMask = LaneBitmask::getNone();
  357. }
  358. }
  359. static void removeRegLanes(SmallVectorImpl<RegisterMaskPair> &RegUnits,
  360. RegisterMaskPair Pair) {
  361. Register RegUnit = Pair.RegUnit;
  362. assert(Pair.LaneMask.any());
  363. auto I = llvm::find_if(RegUnits, [RegUnit](const RegisterMaskPair Other) {
  364. return Other.RegUnit == RegUnit;
  365. });
  366. if (I != RegUnits.end()) {
  367. I->LaneMask &= ~Pair.LaneMask;
  368. if (I->LaneMask.none())
  369. RegUnits.erase(I);
  370. }
  371. }
  372. static LaneBitmask
  373. getLanesWithProperty(const LiveIntervals &LIS, const MachineRegisterInfo &MRI,
  374. bool TrackLaneMasks, Register RegUnit, SlotIndex Pos,
  375. LaneBitmask SafeDefault,
  376. bool (*Property)(const LiveRange &LR, SlotIndex Pos)) {
  377. if (RegUnit.isVirtual()) {
  378. const LiveInterval &LI = LIS.getInterval(RegUnit);
  379. LaneBitmask Result;
  380. if (TrackLaneMasks && LI.hasSubRanges()) {
  381. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  382. if (Property(SR, Pos))
  383. Result |= SR.LaneMask;
  384. }
  385. } else if (Property(LI, Pos)) {
  386. Result = TrackLaneMasks ? MRI.getMaxLaneMaskForVReg(RegUnit)
  387. : LaneBitmask::getAll();
  388. }
  389. return Result;
  390. } else {
  391. const LiveRange *LR = LIS.getCachedRegUnit(RegUnit);
  392. // Be prepared for missing liveranges: We usually do not compute liveranges
  393. // for physical registers on targets with many registers (GPUs).
  394. if (LR == nullptr)
  395. return SafeDefault;
  396. return Property(*LR, Pos) ? LaneBitmask::getAll() : LaneBitmask::getNone();
  397. }
  398. }
  399. static LaneBitmask getLiveLanesAt(const LiveIntervals &LIS,
  400. const MachineRegisterInfo &MRI,
  401. bool TrackLaneMasks, Register RegUnit,
  402. SlotIndex Pos) {
  403. return getLanesWithProperty(LIS, MRI, TrackLaneMasks, RegUnit, Pos,
  404. LaneBitmask::getAll(),
  405. [](const LiveRange &LR, SlotIndex Pos) {
  406. return LR.liveAt(Pos);
  407. });
  408. }
  409. namespace {
  410. /// Collect this instruction's unique uses and defs into SmallVectors for
  411. /// processing defs and uses in order.
  412. ///
  413. /// FIXME: always ignore tied opers
  414. class RegisterOperandsCollector {
  415. friend class llvm::RegisterOperands;
  416. RegisterOperands &RegOpers;
  417. const TargetRegisterInfo &TRI;
  418. const MachineRegisterInfo &MRI;
  419. bool IgnoreDead;
  420. RegisterOperandsCollector(RegisterOperands &RegOpers,
  421. const TargetRegisterInfo &TRI,
  422. const MachineRegisterInfo &MRI, bool IgnoreDead)
  423. : RegOpers(RegOpers), TRI(TRI), MRI(MRI), IgnoreDead(IgnoreDead) {}
  424. void collectInstr(const MachineInstr &MI) const {
  425. for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI)
  426. collectOperand(*OperI);
  427. // Remove redundant physreg dead defs.
  428. for (const RegisterMaskPair &P : RegOpers.Defs)
  429. removeRegLanes(RegOpers.DeadDefs, P);
  430. }
  431. void collectInstrLanes(const MachineInstr &MI) const {
  432. for (ConstMIBundleOperands OperI(MI); OperI.isValid(); ++OperI)
  433. collectOperandLanes(*OperI);
  434. // Remove redundant physreg dead defs.
  435. for (const RegisterMaskPair &P : RegOpers.Defs)
  436. removeRegLanes(RegOpers.DeadDefs, P);
  437. }
  438. /// Push this operand's register onto the correct vectors.
  439. void collectOperand(const MachineOperand &MO) const {
  440. if (!MO.isReg() || !MO.getReg())
  441. return;
  442. Register Reg = MO.getReg();
  443. if (MO.isUse()) {
  444. if (!MO.isUndef() && !MO.isInternalRead())
  445. pushReg(Reg, RegOpers.Uses);
  446. } else {
  447. assert(MO.isDef());
  448. // Subregister definitions may imply a register read.
  449. if (MO.readsReg())
  450. pushReg(Reg, RegOpers.Uses);
  451. if (MO.isDead()) {
  452. if (!IgnoreDead)
  453. pushReg(Reg, RegOpers.DeadDefs);
  454. } else
  455. pushReg(Reg, RegOpers.Defs);
  456. }
  457. }
  458. void pushReg(Register Reg,
  459. SmallVectorImpl<RegisterMaskPair> &RegUnits) const {
  460. if (Reg.isVirtual()) {
  461. addRegLanes(RegUnits, RegisterMaskPair(Reg, LaneBitmask::getAll()));
  462. } else if (MRI.isAllocatable(Reg)) {
  463. for (MCRegUnitIterator Units(Reg.asMCReg(), &TRI); Units.isValid();
  464. ++Units)
  465. addRegLanes(RegUnits, RegisterMaskPair(*Units, LaneBitmask::getAll()));
  466. }
  467. }
  468. void collectOperandLanes(const MachineOperand &MO) const {
  469. if (!MO.isReg() || !MO.getReg())
  470. return;
  471. Register Reg = MO.getReg();
  472. unsigned SubRegIdx = MO.getSubReg();
  473. if (MO.isUse()) {
  474. if (!MO.isUndef() && !MO.isInternalRead())
  475. pushRegLanes(Reg, SubRegIdx, RegOpers.Uses);
  476. } else {
  477. assert(MO.isDef());
  478. // Treat read-undef subreg defs as definitions of the whole register.
  479. if (MO.isUndef())
  480. SubRegIdx = 0;
  481. if (MO.isDead()) {
  482. if (!IgnoreDead)
  483. pushRegLanes(Reg, SubRegIdx, RegOpers.DeadDefs);
  484. } else
  485. pushRegLanes(Reg, SubRegIdx, RegOpers.Defs);
  486. }
  487. }
  488. void pushRegLanes(Register Reg, unsigned SubRegIdx,
  489. SmallVectorImpl<RegisterMaskPair> &RegUnits) const {
  490. if (Reg.isVirtual()) {
  491. LaneBitmask LaneMask = SubRegIdx != 0
  492. ? TRI.getSubRegIndexLaneMask(SubRegIdx)
  493. : MRI.getMaxLaneMaskForVReg(Reg);
  494. addRegLanes(RegUnits, RegisterMaskPair(Reg, LaneMask));
  495. } else if (MRI.isAllocatable(Reg)) {
  496. for (MCRegUnitIterator Units(Reg.asMCReg(), &TRI); Units.isValid();
  497. ++Units)
  498. addRegLanes(RegUnits, RegisterMaskPair(*Units, LaneBitmask::getAll()));
  499. }
  500. }
  501. };
  502. } // end anonymous namespace
  503. void RegisterOperands::collect(const MachineInstr &MI,
  504. const TargetRegisterInfo &TRI,
  505. const MachineRegisterInfo &MRI,
  506. bool TrackLaneMasks, bool IgnoreDead) {
  507. RegisterOperandsCollector Collector(*this, TRI, MRI, IgnoreDead);
  508. if (TrackLaneMasks)
  509. Collector.collectInstrLanes(MI);
  510. else
  511. Collector.collectInstr(MI);
  512. }
  513. void RegisterOperands::detectDeadDefs(const MachineInstr &MI,
  514. const LiveIntervals &LIS) {
  515. SlotIndex SlotIdx = LIS.getInstructionIndex(MI);
  516. for (auto RI = Defs.begin(); RI != Defs.end(); /*empty*/) {
  517. Register Reg = RI->RegUnit;
  518. const LiveRange *LR = getLiveRange(LIS, Reg);
  519. if (LR != nullptr) {
  520. LiveQueryResult LRQ = LR->Query(SlotIdx);
  521. if (LRQ.isDeadDef()) {
  522. // LiveIntervals knows this is a dead even though it's MachineOperand is
  523. // not flagged as such.
  524. DeadDefs.push_back(*RI);
  525. RI = Defs.erase(RI);
  526. continue;
  527. }
  528. }
  529. ++RI;
  530. }
  531. }
  532. void RegisterOperands::adjustLaneLiveness(const LiveIntervals &LIS,
  533. const MachineRegisterInfo &MRI,
  534. SlotIndex Pos,
  535. MachineInstr *AddFlagsMI) {
  536. for (auto I = Defs.begin(); I != Defs.end(); ) {
  537. LaneBitmask LiveAfter = getLiveLanesAt(LIS, MRI, true, I->RegUnit,
  538. Pos.getDeadSlot());
  539. // If the def is all that is live after the instruction, then in case
  540. // of a subregister def we need a read-undef flag.
  541. Register RegUnit = I->RegUnit;
  542. if (Register::isVirtualRegister(RegUnit) &&
  543. AddFlagsMI != nullptr && (LiveAfter & ~I->LaneMask).none())
  544. AddFlagsMI->setRegisterDefReadUndef(RegUnit);
  545. LaneBitmask ActualDef = I->LaneMask & LiveAfter;
  546. if (ActualDef.none()) {
  547. I = Defs.erase(I);
  548. } else {
  549. I->LaneMask = ActualDef;
  550. ++I;
  551. }
  552. }
  553. for (auto I = Uses.begin(); I != Uses.end(); ) {
  554. LaneBitmask LiveBefore = getLiveLanesAt(LIS, MRI, true, I->RegUnit,
  555. Pos.getBaseIndex());
  556. LaneBitmask LaneMask = I->LaneMask & LiveBefore;
  557. if (LaneMask.none()) {
  558. I = Uses.erase(I);
  559. } else {
  560. I->LaneMask = LaneMask;
  561. ++I;
  562. }
  563. }
  564. if (AddFlagsMI != nullptr) {
  565. for (const RegisterMaskPair &P : DeadDefs) {
  566. Register RegUnit = P.RegUnit;
  567. if (!Register::isVirtualRegister(RegUnit))
  568. continue;
  569. LaneBitmask LiveAfter = getLiveLanesAt(LIS, MRI, true, RegUnit,
  570. Pos.getDeadSlot());
  571. if (LiveAfter.none())
  572. AddFlagsMI->setRegisterDefReadUndef(RegUnit);
  573. }
  574. }
  575. }
  576. /// Initialize an array of N PressureDiffs.
  577. void PressureDiffs::init(unsigned N) {
  578. Size = N;
  579. if (N <= Max) {
  580. memset(PDiffArray, 0, N * sizeof(PressureDiff));
  581. return;
  582. }
  583. Max = Size;
  584. free(PDiffArray);
  585. PDiffArray = static_cast<PressureDiff*>(safe_calloc(N, sizeof(PressureDiff)));
  586. }
  587. void PressureDiffs::addInstruction(unsigned Idx,
  588. const RegisterOperands &RegOpers,
  589. const MachineRegisterInfo &MRI) {
  590. PressureDiff &PDiff = (*this)[Idx];
  591. assert(!PDiff.begin()->isValid() && "stale PDiff");
  592. for (const RegisterMaskPair &P : RegOpers.Defs)
  593. PDiff.addPressureChange(P.RegUnit, true, &MRI);
  594. for (const RegisterMaskPair &P : RegOpers.Uses)
  595. PDiff.addPressureChange(P.RegUnit, false, &MRI);
  596. }
  597. /// Add a change in pressure to the pressure diff of a given instruction.
  598. void PressureDiff::addPressureChange(Register RegUnit, bool IsDec,
  599. const MachineRegisterInfo *MRI) {
  600. PSetIterator PSetI = MRI->getPressureSets(RegUnit);
  601. int Weight = IsDec ? -PSetI.getWeight() : PSetI.getWeight();
  602. for (; PSetI.isValid(); ++PSetI) {
  603. // Find an existing entry in the pressure diff for this PSet.
  604. PressureDiff::iterator I = nonconst_begin(), E = nonconst_end();
  605. for (; I != E && I->isValid(); ++I) {
  606. if (I->getPSet() >= *PSetI)
  607. break;
  608. }
  609. // If all pressure sets are more constrained, skip the remaining PSets.
  610. if (I == E)
  611. break;
  612. // Insert this PressureChange.
  613. if (!I->isValid() || I->getPSet() != *PSetI) {
  614. PressureChange PTmp = PressureChange(*PSetI);
  615. for (PressureDiff::iterator J = I; J != E && PTmp.isValid(); ++J)
  616. std::swap(*J, PTmp);
  617. }
  618. // Update the units for this pressure set.
  619. unsigned NewUnitInc = I->getUnitInc() + Weight;
  620. if (NewUnitInc != 0) {
  621. I->setUnitInc(NewUnitInc);
  622. } else {
  623. // Remove entry
  624. PressureDiff::iterator J;
  625. for (J = std::next(I); J != E && J->isValid(); ++J, ++I)
  626. *I = *J;
  627. *I = PressureChange();
  628. }
  629. }
  630. }
  631. /// Force liveness of registers.
  632. void RegPressureTracker::addLiveRegs(ArrayRef<RegisterMaskPair> Regs) {
  633. for (const RegisterMaskPair &P : Regs) {
  634. LaneBitmask PrevMask = LiveRegs.insert(P);
  635. LaneBitmask NewMask = PrevMask | P.LaneMask;
  636. increaseRegPressure(P.RegUnit, PrevMask, NewMask);
  637. }
  638. }
  639. void RegPressureTracker::discoverLiveInOrOut(RegisterMaskPair Pair,
  640. SmallVectorImpl<RegisterMaskPair> &LiveInOrOut) {
  641. assert(Pair.LaneMask.any());
  642. Register RegUnit = Pair.RegUnit;
  643. auto I = llvm::find_if(LiveInOrOut, [RegUnit](const RegisterMaskPair &Other) {
  644. return Other.RegUnit == RegUnit;
  645. });
  646. LaneBitmask PrevMask;
  647. LaneBitmask NewMask;
  648. if (I == LiveInOrOut.end()) {
  649. PrevMask = LaneBitmask::getNone();
  650. NewMask = Pair.LaneMask;
  651. LiveInOrOut.push_back(Pair);
  652. } else {
  653. PrevMask = I->LaneMask;
  654. NewMask = PrevMask | Pair.LaneMask;
  655. I->LaneMask = NewMask;
  656. }
  657. increaseSetPressure(P.MaxSetPressure, *MRI, RegUnit, PrevMask, NewMask);
  658. }
  659. void RegPressureTracker::discoverLiveIn(RegisterMaskPair Pair) {
  660. discoverLiveInOrOut(Pair, P.LiveInRegs);
  661. }
  662. void RegPressureTracker::discoverLiveOut(RegisterMaskPair Pair) {
  663. discoverLiveInOrOut(Pair, P.LiveOutRegs);
  664. }
  665. void RegPressureTracker::bumpDeadDefs(ArrayRef<RegisterMaskPair> DeadDefs) {
  666. for (const RegisterMaskPair &P : DeadDefs) {
  667. Register Reg = P.RegUnit;
  668. LaneBitmask LiveMask = LiveRegs.contains(Reg);
  669. LaneBitmask BumpedMask = LiveMask | P.LaneMask;
  670. increaseRegPressure(Reg, LiveMask, BumpedMask);
  671. }
  672. for (const RegisterMaskPair &P : DeadDefs) {
  673. Register Reg = P.RegUnit;
  674. LaneBitmask LiveMask = LiveRegs.contains(Reg);
  675. LaneBitmask BumpedMask = LiveMask | P.LaneMask;
  676. decreaseRegPressure(Reg, BumpedMask, LiveMask);
  677. }
  678. }
  679. /// Recede across the previous instruction. If LiveUses is provided, record any
  680. /// RegUnits that are made live by the current instruction's uses. This includes
  681. /// registers that are both defined and used by the instruction. If a pressure
  682. /// difference pointer is provided record the changes is pressure caused by this
  683. /// instruction independent of liveness.
  684. void RegPressureTracker::recede(const RegisterOperands &RegOpers,
  685. SmallVectorImpl<RegisterMaskPair> *LiveUses) {
  686. assert(!CurrPos->isDebugOrPseudoInstr());
  687. // Boost pressure for all dead defs together.
  688. bumpDeadDefs(RegOpers.DeadDefs);
  689. // Kill liveness at live defs.
  690. // TODO: consider earlyclobbers?
  691. for (const RegisterMaskPair &Def : RegOpers.Defs) {
  692. Register Reg = Def.RegUnit;
  693. LaneBitmask PreviousMask = LiveRegs.erase(Def);
  694. LaneBitmask NewMask = PreviousMask & ~Def.LaneMask;
  695. LaneBitmask LiveOut = Def.LaneMask & ~PreviousMask;
  696. if (LiveOut.any()) {
  697. discoverLiveOut(RegisterMaskPair(Reg, LiveOut));
  698. // Retroactively model effects on pressure of the live out lanes.
  699. increaseSetPressure(CurrSetPressure, *MRI, Reg, LaneBitmask::getNone(),
  700. LiveOut);
  701. PreviousMask = LiveOut;
  702. }
  703. if (NewMask.none()) {
  704. // Add a 0 entry to LiveUses as a marker that the complete vreg has become
  705. // dead.
  706. if (TrackLaneMasks && LiveUses != nullptr)
  707. setRegZero(*LiveUses, Reg);
  708. }
  709. decreaseRegPressure(Reg, PreviousMask, NewMask);
  710. }
  711. SlotIndex SlotIdx;
  712. if (RequireIntervals)
  713. SlotIdx = LIS->getInstructionIndex(*CurrPos).getRegSlot();
  714. // Generate liveness for uses.
  715. for (const RegisterMaskPair &Use : RegOpers.Uses) {
  716. Register Reg = Use.RegUnit;
  717. assert(Use.LaneMask.any());
  718. LaneBitmask PreviousMask = LiveRegs.insert(Use);
  719. LaneBitmask NewMask = PreviousMask | Use.LaneMask;
  720. if (NewMask == PreviousMask)
  721. continue;
  722. // Did the register just become live?
  723. if (PreviousMask.none()) {
  724. if (LiveUses != nullptr) {
  725. if (!TrackLaneMasks) {
  726. addRegLanes(*LiveUses, RegisterMaskPair(Reg, NewMask));
  727. } else {
  728. auto I =
  729. llvm::find_if(*LiveUses, [Reg](const RegisterMaskPair Other) {
  730. return Other.RegUnit == Reg;
  731. });
  732. bool IsRedef = I != LiveUses->end();
  733. if (IsRedef) {
  734. // ignore re-defs here...
  735. assert(I->LaneMask.none());
  736. removeRegLanes(*LiveUses, RegisterMaskPair(Reg, NewMask));
  737. } else {
  738. addRegLanes(*LiveUses, RegisterMaskPair(Reg, NewMask));
  739. }
  740. }
  741. }
  742. // Discover live outs if this may be the first occurance of this register.
  743. if (RequireIntervals) {
  744. LaneBitmask LiveOut = getLiveThroughAt(Reg, SlotIdx);
  745. if (LiveOut.any())
  746. discoverLiveOut(RegisterMaskPair(Reg, LiveOut));
  747. }
  748. }
  749. increaseRegPressure(Reg, PreviousMask, NewMask);
  750. }
  751. if (TrackUntiedDefs) {
  752. for (const RegisterMaskPair &Def : RegOpers.Defs) {
  753. Register RegUnit = Def.RegUnit;
  754. if (Register::isVirtualRegister(RegUnit) &&
  755. (LiveRegs.contains(RegUnit) & Def.LaneMask).none())
  756. UntiedDefs.insert(RegUnit);
  757. }
  758. }
  759. }
  760. void RegPressureTracker::recedeSkipDebugValues() {
  761. assert(CurrPos != MBB->begin());
  762. if (!isBottomClosed())
  763. closeBottom();
  764. // Open the top of the region using block iterators.
  765. if (!RequireIntervals && isTopClosed())
  766. static_cast<RegionPressure&>(P).openTop(CurrPos);
  767. // Find the previous instruction.
  768. CurrPos = prev_nodbg(CurrPos, MBB->begin());
  769. SlotIndex SlotIdx;
  770. if (RequireIntervals && !CurrPos->isDebugOrPseudoInstr())
  771. SlotIdx = LIS->getInstructionIndex(*CurrPos).getRegSlot();
  772. // Open the top of the region using slot indexes.
  773. if (RequireIntervals && isTopClosed())
  774. static_cast<IntervalPressure&>(P).openTop(SlotIdx);
  775. }
  776. void RegPressureTracker::recede(SmallVectorImpl<RegisterMaskPair> *LiveUses) {
  777. recedeSkipDebugValues();
  778. if (CurrPos->isDebugInstr() || CurrPos->isPseudoProbe()) {
  779. // It's possible to only have debug_value and pseudo probe instructions and
  780. // hit the start of the block.
  781. assert(CurrPos == MBB->begin());
  782. return;
  783. }
  784. const MachineInstr &MI = *CurrPos;
  785. RegisterOperands RegOpers;
  786. RegOpers.collect(MI, *TRI, *MRI, TrackLaneMasks, false);
  787. if (TrackLaneMasks) {
  788. SlotIndex SlotIdx = LIS->getInstructionIndex(*CurrPos).getRegSlot();
  789. RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx);
  790. } else if (RequireIntervals) {
  791. RegOpers.detectDeadDefs(MI, *LIS);
  792. }
  793. recede(RegOpers, LiveUses);
  794. }
  795. /// Advance across the current instruction.
  796. void RegPressureTracker::advance(const RegisterOperands &RegOpers) {
  797. assert(!TrackUntiedDefs && "unsupported mode");
  798. assert(CurrPos != MBB->end());
  799. if (!isTopClosed())
  800. closeTop();
  801. SlotIndex SlotIdx;
  802. if (RequireIntervals)
  803. SlotIdx = getCurrSlot();
  804. // Open the bottom of the region using slot indexes.
  805. if (isBottomClosed()) {
  806. if (RequireIntervals)
  807. static_cast<IntervalPressure&>(P).openBottom(SlotIdx);
  808. else
  809. static_cast<RegionPressure&>(P).openBottom(CurrPos);
  810. }
  811. for (const RegisterMaskPair &Use : RegOpers.Uses) {
  812. Register Reg = Use.RegUnit;
  813. LaneBitmask LiveMask = LiveRegs.contains(Reg);
  814. LaneBitmask LiveIn = Use.LaneMask & ~LiveMask;
  815. if (LiveIn.any()) {
  816. discoverLiveIn(RegisterMaskPair(Reg, LiveIn));
  817. increaseRegPressure(Reg, LiveMask, LiveMask | LiveIn);
  818. LiveRegs.insert(RegisterMaskPair(Reg, LiveIn));
  819. }
  820. // Kill liveness at last uses.
  821. if (RequireIntervals) {
  822. LaneBitmask LastUseMask = getLastUsedLanes(Reg, SlotIdx);
  823. if (LastUseMask.any()) {
  824. LiveRegs.erase(RegisterMaskPair(Reg, LastUseMask));
  825. decreaseRegPressure(Reg, LiveMask, LiveMask & ~LastUseMask);
  826. }
  827. }
  828. }
  829. // Generate liveness for defs.
  830. for (const RegisterMaskPair &Def : RegOpers.Defs) {
  831. LaneBitmask PreviousMask = LiveRegs.insert(Def);
  832. LaneBitmask NewMask = PreviousMask | Def.LaneMask;
  833. increaseRegPressure(Def.RegUnit, PreviousMask, NewMask);
  834. }
  835. // Boost pressure for all dead defs together.
  836. bumpDeadDefs(RegOpers.DeadDefs);
  837. // Find the next instruction.
  838. CurrPos = next_nodbg(CurrPos, MBB->end());
  839. }
  840. void RegPressureTracker::advance() {
  841. const MachineInstr &MI = *CurrPos;
  842. RegisterOperands RegOpers;
  843. RegOpers.collect(MI, *TRI, *MRI, TrackLaneMasks, false);
  844. if (TrackLaneMasks) {
  845. SlotIndex SlotIdx = getCurrSlot();
  846. RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx);
  847. }
  848. advance(RegOpers);
  849. }
  850. /// Find the max change in excess pressure across all sets.
  851. static void computeExcessPressureDelta(ArrayRef<unsigned> OldPressureVec,
  852. ArrayRef<unsigned> NewPressureVec,
  853. RegPressureDelta &Delta,
  854. const RegisterClassInfo *RCI,
  855. ArrayRef<unsigned> LiveThruPressureVec) {
  856. Delta.Excess = PressureChange();
  857. for (unsigned i = 0, e = OldPressureVec.size(); i < e; ++i) {
  858. unsigned POld = OldPressureVec[i];
  859. unsigned PNew = NewPressureVec[i];
  860. int PDiff = (int)PNew - (int)POld;
  861. if (!PDiff) // No change in this set in the common case.
  862. continue;
  863. // Only consider change beyond the limit.
  864. unsigned Limit = RCI->getRegPressureSetLimit(i);
  865. if (!LiveThruPressureVec.empty())
  866. Limit += LiveThruPressureVec[i];
  867. if (Limit > POld) {
  868. if (Limit > PNew)
  869. PDiff = 0; // Under the limit
  870. else
  871. PDiff = PNew - Limit; // Just exceeded limit.
  872. } else if (Limit > PNew)
  873. PDiff = Limit - POld; // Just obeyed limit.
  874. if (PDiff) {
  875. Delta.Excess = PressureChange(i);
  876. Delta.Excess.setUnitInc(PDiff);
  877. break;
  878. }
  879. }
  880. }
  881. /// Find the max change in max pressure that either surpasses a critical PSet
  882. /// limit or exceeds the current MaxPressureLimit.
  883. ///
  884. /// FIXME: comparing each element of the old and new MaxPressure vectors here is
  885. /// silly. It's done now to demonstrate the concept but will go away with a
  886. /// RegPressureTracker API change to work with pressure differences.
  887. static void computeMaxPressureDelta(ArrayRef<unsigned> OldMaxPressureVec,
  888. ArrayRef<unsigned> NewMaxPressureVec,
  889. ArrayRef<PressureChange> CriticalPSets,
  890. ArrayRef<unsigned> MaxPressureLimit,
  891. RegPressureDelta &Delta) {
  892. Delta.CriticalMax = PressureChange();
  893. Delta.CurrentMax = PressureChange();
  894. unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
  895. for (unsigned i = 0, e = OldMaxPressureVec.size(); i < e; ++i) {
  896. unsigned POld = OldMaxPressureVec[i];
  897. unsigned PNew = NewMaxPressureVec[i];
  898. if (PNew == POld) // No change in this set in the common case.
  899. continue;
  900. if (!Delta.CriticalMax.isValid()) {
  901. while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < i)
  902. ++CritIdx;
  903. if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == i) {
  904. int PDiff = (int)PNew - (int)CriticalPSets[CritIdx].getUnitInc();
  905. if (PDiff > 0) {
  906. Delta.CriticalMax = PressureChange(i);
  907. Delta.CriticalMax.setUnitInc(PDiff);
  908. }
  909. }
  910. }
  911. // Find the first increase above MaxPressureLimit.
  912. // (Ignores negative MDiff).
  913. if (!Delta.CurrentMax.isValid() && PNew > MaxPressureLimit[i]) {
  914. Delta.CurrentMax = PressureChange(i);
  915. Delta.CurrentMax.setUnitInc(PNew - POld);
  916. if (CritIdx == CritEnd || Delta.CriticalMax.isValid())
  917. break;
  918. }
  919. }
  920. }
  921. /// Record the upward impact of a single instruction on current register
  922. /// pressure. Unlike the advance/recede pressure tracking interface, this does
  923. /// not discover live in/outs.
  924. ///
  925. /// This is intended for speculative queries. It leaves pressure inconsistent
  926. /// with the current position, so must be restored by the caller.
  927. void RegPressureTracker::bumpUpwardPressure(const MachineInstr *MI) {
  928. assert(!MI->isDebugOrPseudoInstr() && "Expect a nondebug instruction.");
  929. SlotIndex SlotIdx;
  930. if (RequireIntervals)
  931. SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
  932. // Account for register pressure similar to RegPressureTracker::recede().
  933. RegisterOperands RegOpers;
  934. RegOpers.collect(*MI, *TRI, *MRI, TrackLaneMasks, /*IgnoreDead=*/true);
  935. assert(RegOpers.DeadDefs.size() == 0);
  936. if (TrackLaneMasks)
  937. RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx);
  938. else if (RequireIntervals)
  939. RegOpers.detectDeadDefs(*MI, *LIS);
  940. // Boost max pressure for all dead defs together.
  941. // Since CurrSetPressure and MaxSetPressure
  942. bumpDeadDefs(RegOpers.DeadDefs);
  943. // Kill liveness at live defs.
  944. for (const RegisterMaskPair &P : RegOpers.Defs) {
  945. Register Reg = P.RegUnit;
  946. LaneBitmask LiveLanes = LiveRegs.contains(Reg);
  947. LaneBitmask UseLanes = getRegLanes(RegOpers.Uses, Reg);
  948. LaneBitmask DefLanes = P.LaneMask;
  949. LaneBitmask LiveAfter = (LiveLanes & ~DefLanes) | UseLanes;
  950. decreaseRegPressure(Reg, LiveLanes, LiveAfter);
  951. }
  952. // Generate liveness for uses.
  953. for (const RegisterMaskPair &P : RegOpers.Uses) {
  954. Register Reg = P.RegUnit;
  955. LaneBitmask LiveLanes = LiveRegs.contains(Reg);
  956. LaneBitmask LiveAfter = LiveLanes | P.LaneMask;
  957. increaseRegPressure(Reg, LiveLanes, LiveAfter);
  958. }
  959. }
  960. /// Consider the pressure increase caused by traversing this instruction
  961. /// bottom-up. Find the pressure set with the most change beyond its pressure
  962. /// limit based on the tracker's current pressure, and return the change in
  963. /// number of register units of that pressure set introduced by this
  964. /// instruction.
  965. ///
  966. /// This assumes that the current LiveOut set is sufficient.
  967. ///
  968. /// This is expensive for an on-the-fly query because it calls
  969. /// bumpUpwardPressure to recompute the pressure sets based on current
  970. /// liveness. This mainly exists to verify correctness, e.g. with
  971. /// -verify-misched. getUpwardPressureDelta is the fast version of this query
  972. /// that uses the per-SUnit cache of the PressureDiff.
  973. void RegPressureTracker::
  974. getMaxUpwardPressureDelta(const MachineInstr *MI, PressureDiff *PDiff,
  975. RegPressureDelta &Delta,
  976. ArrayRef<PressureChange> CriticalPSets,
  977. ArrayRef<unsigned> MaxPressureLimit) {
  978. // Snapshot Pressure.
  979. // FIXME: The snapshot heap space should persist. But I'm planning to
  980. // summarize the pressure effect so we don't need to snapshot at all.
  981. std::vector<unsigned> SavedPressure = CurrSetPressure;
  982. std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
  983. bumpUpwardPressure(MI);
  984. computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI,
  985. LiveThruPressure);
  986. computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
  987. MaxPressureLimit, Delta);
  988. assert(Delta.CriticalMax.getUnitInc() >= 0 &&
  989. Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure");
  990. // Restore the tracker's state.
  991. P.MaxSetPressure.swap(SavedMaxPressure);
  992. CurrSetPressure.swap(SavedPressure);
  993. #ifndef NDEBUG
  994. if (!PDiff)
  995. return;
  996. // Check if the alternate algorithm yields the same result.
  997. RegPressureDelta Delta2;
  998. getUpwardPressureDelta(MI, *PDiff, Delta2, CriticalPSets, MaxPressureLimit);
  999. if (Delta != Delta2) {
  1000. dbgs() << "PDiff: ";
  1001. PDiff->dump(*TRI);
  1002. dbgs() << "DELTA: " << *MI;
  1003. if (Delta.Excess.isValid())
  1004. dbgs() << "Excess1 " << TRI->getRegPressureSetName(Delta.Excess.getPSet())
  1005. << " " << Delta.Excess.getUnitInc() << "\n";
  1006. if (Delta.CriticalMax.isValid())
  1007. dbgs() << "Critic1 " << TRI->getRegPressureSetName(Delta.CriticalMax.getPSet())
  1008. << " " << Delta.CriticalMax.getUnitInc() << "\n";
  1009. if (Delta.CurrentMax.isValid())
  1010. dbgs() << "CurrMx1 " << TRI->getRegPressureSetName(Delta.CurrentMax.getPSet())
  1011. << " " << Delta.CurrentMax.getUnitInc() << "\n";
  1012. if (Delta2.Excess.isValid())
  1013. dbgs() << "Excess2 " << TRI->getRegPressureSetName(Delta2.Excess.getPSet())
  1014. << " " << Delta2.Excess.getUnitInc() << "\n";
  1015. if (Delta2.CriticalMax.isValid())
  1016. dbgs() << "Critic2 " << TRI->getRegPressureSetName(Delta2.CriticalMax.getPSet())
  1017. << " " << Delta2.CriticalMax.getUnitInc() << "\n";
  1018. if (Delta2.CurrentMax.isValid())
  1019. dbgs() << "CurrMx2 " << TRI->getRegPressureSetName(Delta2.CurrentMax.getPSet())
  1020. << " " << Delta2.CurrentMax.getUnitInc() << "\n";
  1021. llvm_unreachable("RegP Delta Mismatch");
  1022. }
  1023. #endif
  1024. }
  1025. /// This is the fast version of querying register pressure that does not
  1026. /// directly depend on current liveness.
  1027. ///
  1028. /// @param Delta captures information needed for heuristics.
  1029. ///
  1030. /// @param CriticalPSets Are the pressure sets that are known to exceed some
  1031. /// limit within the region, not necessarily at the current position.
  1032. ///
  1033. /// @param MaxPressureLimit Is the max pressure within the region, not
  1034. /// necessarily at the current position.
  1035. void RegPressureTracker::
  1036. getUpwardPressureDelta(const MachineInstr *MI, /*const*/ PressureDiff &PDiff,
  1037. RegPressureDelta &Delta,
  1038. ArrayRef<PressureChange> CriticalPSets,
  1039. ArrayRef<unsigned> MaxPressureLimit) const {
  1040. unsigned CritIdx = 0, CritEnd = CriticalPSets.size();
  1041. for (PressureDiff::const_iterator
  1042. PDiffI = PDiff.begin(), PDiffE = PDiff.end();
  1043. PDiffI != PDiffE && PDiffI->isValid(); ++PDiffI) {
  1044. unsigned PSetID = PDiffI->getPSet();
  1045. unsigned Limit = RCI->getRegPressureSetLimit(PSetID);
  1046. if (!LiveThruPressure.empty())
  1047. Limit += LiveThruPressure[PSetID];
  1048. unsigned POld = CurrSetPressure[PSetID];
  1049. unsigned MOld = P.MaxSetPressure[PSetID];
  1050. unsigned MNew = MOld;
  1051. // Ignore DeadDefs here because they aren't captured by PressureChange.
  1052. unsigned PNew = POld + PDiffI->getUnitInc();
  1053. assert((PDiffI->getUnitInc() >= 0) == (PNew >= POld)
  1054. && "PSet overflow/underflow");
  1055. if (PNew > MOld)
  1056. MNew = PNew;
  1057. // Check if current pressure has exceeded the limit.
  1058. if (!Delta.Excess.isValid()) {
  1059. unsigned ExcessInc = 0;
  1060. if (PNew > Limit)
  1061. ExcessInc = POld > Limit ? PNew - POld : PNew - Limit;
  1062. else if (POld > Limit)
  1063. ExcessInc = Limit - POld;
  1064. if (ExcessInc) {
  1065. Delta.Excess = PressureChange(PSetID);
  1066. Delta.Excess.setUnitInc(ExcessInc);
  1067. }
  1068. }
  1069. // Check if max pressure has exceeded a critical pressure set max.
  1070. if (MNew == MOld)
  1071. continue;
  1072. if (!Delta.CriticalMax.isValid()) {
  1073. while (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() < PSetID)
  1074. ++CritIdx;
  1075. if (CritIdx != CritEnd && CriticalPSets[CritIdx].getPSet() == PSetID) {
  1076. int CritInc = (int)MNew - (int)CriticalPSets[CritIdx].getUnitInc();
  1077. if (CritInc > 0 && CritInc <= std::numeric_limits<int16_t>::max()) {
  1078. Delta.CriticalMax = PressureChange(PSetID);
  1079. Delta.CriticalMax.setUnitInc(CritInc);
  1080. }
  1081. }
  1082. }
  1083. // Check if max pressure has exceeded the current max.
  1084. if (!Delta.CurrentMax.isValid() && MNew > MaxPressureLimit[PSetID]) {
  1085. Delta.CurrentMax = PressureChange(PSetID);
  1086. Delta.CurrentMax.setUnitInc(MNew - MOld);
  1087. }
  1088. }
  1089. }
  1090. /// Helper to find a vreg use between two indices [PriorUseIdx, NextUseIdx).
  1091. /// The query starts with a lane bitmask which gets lanes/bits removed for every
  1092. /// use we find.
  1093. static LaneBitmask findUseBetween(unsigned Reg, LaneBitmask LastUseMask,
  1094. SlotIndex PriorUseIdx, SlotIndex NextUseIdx,
  1095. const MachineRegisterInfo &MRI,
  1096. const LiveIntervals *LIS) {
  1097. const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
  1098. for (const MachineOperand &MO : MRI.use_nodbg_operands(Reg)) {
  1099. if (MO.isUndef())
  1100. continue;
  1101. const MachineInstr *MI = MO.getParent();
  1102. SlotIndex InstSlot = LIS->getInstructionIndex(*MI).getRegSlot();
  1103. if (InstSlot >= PriorUseIdx && InstSlot < NextUseIdx) {
  1104. unsigned SubRegIdx = MO.getSubReg();
  1105. LaneBitmask UseMask = TRI.getSubRegIndexLaneMask(SubRegIdx);
  1106. LastUseMask &= ~UseMask;
  1107. if (LastUseMask.none())
  1108. return LaneBitmask::getNone();
  1109. }
  1110. }
  1111. return LastUseMask;
  1112. }
  1113. LaneBitmask RegPressureTracker::getLiveLanesAt(Register RegUnit,
  1114. SlotIndex Pos) const {
  1115. assert(RequireIntervals);
  1116. return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, Pos,
  1117. LaneBitmask::getAll(),
  1118. [](const LiveRange &LR, SlotIndex Pos) {
  1119. return LR.liveAt(Pos);
  1120. });
  1121. }
  1122. LaneBitmask RegPressureTracker::getLastUsedLanes(Register RegUnit,
  1123. SlotIndex Pos) const {
  1124. assert(RequireIntervals);
  1125. return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit,
  1126. Pos.getBaseIndex(), LaneBitmask::getNone(),
  1127. [](const LiveRange &LR, SlotIndex Pos) {
  1128. const LiveRange::Segment *S = LR.getSegmentContaining(Pos);
  1129. return S != nullptr && S->end == Pos.getRegSlot();
  1130. });
  1131. }
  1132. LaneBitmask RegPressureTracker::getLiveThroughAt(Register RegUnit,
  1133. SlotIndex Pos) const {
  1134. assert(RequireIntervals);
  1135. return getLanesWithProperty(*LIS, *MRI, TrackLaneMasks, RegUnit, Pos,
  1136. LaneBitmask::getNone(),
  1137. [](const LiveRange &LR, SlotIndex Pos) {
  1138. const LiveRange::Segment *S = LR.getSegmentContaining(Pos);
  1139. return S != nullptr && S->start < Pos.getRegSlot(true) &&
  1140. S->end != Pos.getDeadSlot();
  1141. });
  1142. }
  1143. /// Record the downward impact of a single instruction on current register
  1144. /// pressure. Unlike the advance/recede pressure tracking interface, this does
  1145. /// not discover live in/outs.
  1146. ///
  1147. /// This is intended for speculative queries. It leaves pressure inconsistent
  1148. /// with the current position, so must be restored by the caller.
  1149. void RegPressureTracker::bumpDownwardPressure(const MachineInstr *MI) {
  1150. assert(!MI->isDebugOrPseudoInstr() && "Expect a nondebug instruction.");
  1151. SlotIndex SlotIdx;
  1152. if (RequireIntervals)
  1153. SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
  1154. // Account for register pressure similar to RegPressureTracker::recede().
  1155. RegisterOperands RegOpers;
  1156. RegOpers.collect(*MI, *TRI, *MRI, TrackLaneMasks, false);
  1157. if (TrackLaneMasks)
  1158. RegOpers.adjustLaneLiveness(*LIS, *MRI, SlotIdx);
  1159. if (RequireIntervals) {
  1160. for (const RegisterMaskPair &Use : RegOpers.Uses) {
  1161. Register Reg = Use.RegUnit;
  1162. LaneBitmask LastUseMask = getLastUsedLanes(Reg, SlotIdx);
  1163. if (LastUseMask.none())
  1164. continue;
  1165. // The LastUseMask is queried from the liveness information of instruction
  1166. // which may be further down the schedule. Some lanes may actually not be
  1167. // last uses for the current position.
  1168. // FIXME: allow the caller to pass in the list of vreg uses that remain
  1169. // to be bottom-scheduled to avoid searching uses at each query.
  1170. SlotIndex CurrIdx = getCurrSlot();
  1171. LastUseMask
  1172. = findUseBetween(Reg, LastUseMask, CurrIdx, SlotIdx, *MRI, LIS);
  1173. if (LastUseMask.none())
  1174. continue;
  1175. LaneBitmask LiveMask = LiveRegs.contains(Reg);
  1176. LaneBitmask NewMask = LiveMask & ~LastUseMask;
  1177. decreaseRegPressure(Reg, LiveMask, NewMask);
  1178. }
  1179. }
  1180. // Generate liveness for defs.
  1181. for (const RegisterMaskPair &Def : RegOpers.Defs) {
  1182. Register Reg = Def.RegUnit;
  1183. LaneBitmask LiveMask = LiveRegs.contains(Reg);
  1184. LaneBitmask NewMask = LiveMask | Def.LaneMask;
  1185. increaseRegPressure(Reg, LiveMask, NewMask);
  1186. }
  1187. // Boost pressure for all dead defs together.
  1188. bumpDeadDefs(RegOpers.DeadDefs);
  1189. }
  1190. /// Consider the pressure increase caused by traversing this instruction
  1191. /// top-down. Find the register class with the most change in its pressure limit
  1192. /// based on the tracker's current pressure, and return the number of excess
  1193. /// register units of that pressure set introduced by this instruction.
  1194. ///
  1195. /// This assumes that the current LiveIn set is sufficient.
  1196. ///
  1197. /// This is expensive for an on-the-fly query because it calls
  1198. /// bumpDownwardPressure to recompute the pressure sets based on current
  1199. /// liveness. We don't yet have a fast version of downward pressure tracking
  1200. /// analogous to getUpwardPressureDelta.
  1201. void RegPressureTracker::
  1202. getMaxDownwardPressureDelta(const MachineInstr *MI, RegPressureDelta &Delta,
  1203. ArrayRef<PressureChange> CriticalPSets,
  1204. ArrayRef<unsigned> MaxPressureLimit) {
  1205. // Snapshot Pressure.
  1206. std::vector<unsigned> SavedPressure = CurrSetPressure;
  1207. std::vector<unsigned> SavedMaxPressure = P.MaxSetPressure;
  1208. bumpDownwardPressure(MI);
  1209. computeExcessPressureDelta(SavedPressure, CurrSetPressure, Delta, RCI,
  1210. LiveThruPressure);
  1211. computeMaxPressureDelta(SavedMaxPressure, P.MaxSetPressure, CriticalPSets,
  1212. MaxPressureLimit, Delta);
  1213. assert(Delta.CriticalMax.getUnitInc() >= 0 &&
  1214. Delta.CurrentMax.getUnitInc() >= 0 && "cannot decrease max pressure");
  1215. // Restore the tracker's state.
  1216. P.MaxSetPressure.swap(SavedMaxPressure);
  1217. CurrSetPressure.swap(SavedPressure);
  1218. }
  1219. /// Get the pressure of each PSet after traversing this instruction bottom-up.
  1220. void RegPressureTracker::
  1221. getUpwardPressure(const MachineInstr *MI,
  1222. std::vector<unsigned> &PressureResult,
  1223. std::vector<unsigned> &MaxPressureResult) {
  1224. // Snapshot pressure.
  1225. PressureResult = CurrSetPressure;
  1226. MaxPressureResult = P.MaxSetPressure;
  1227. bumpUpwardPressure(MI);
  1228. // Current pressure becomes the result. Restore current pressure.
  1229. P.MaxSetPressure.swap(MaxPressureResult);
  1230. CurrSetPressure.swap(PressureResult);
  1231. }
  1232. /// Get the pressure of each PSet after traversing this instruction top-down.
  1233. void RegPressureTracker::
  1234. getDownwardPressure(const MachineInstr *MI,
  1235. std::vector<unsigned> &PressureResult,
  1236. std::vector<unsigned> &MaxPressureResult) {
  1237. // Snapshot pressure.
  1238. PressureResult = CurrSetPressure;
  1239. MaxPressureResult = P.MaxSetPressure;
  1240. bumpDownwardPressure(MI);
  1241. // Current pressure becomes the result. Restore current pressure.
  1242. P.MaxSetPressure.swap(MaxPressureResult);
  1243. CurrSetPressure.swap(PressureResult);
  1244. }