InlineSpiller.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. //===- InlineSpiller.cpp - Insert spills and restores inline --------------===//
  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. // The inline spiller modifies the machine function directly instead of
  10. // inserting spills and restores in VirtRegMap.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "SplitKit.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/MapVector.h"
  17. #include "llvm/ADT/None.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SetVector.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/Analysis/AliasAnalysis.h"
  24. #include "llvm/CodeGen/LiveInterval.h"
  25. #include "llvm/CodeGen/LiveIntervalCalc.h"
  26. #include "llvm/CodeGen/LiveIntervals.h"
  27. #include "llvm/CodeGen/LiveRangeEdit.h"
  28. #include "llvm/CodeGen/LiveStacks.h"
  29. #include "llvm/CodeGen/MachineBasicBlock.h"
  30. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  31. #include "llvm/CodeGen/MachineDominators.h"
  32. #include "llvm/CodeGen/MachineFunction.h"
  33. #include "llvm/CodeGen/MachineFunctionPass.h"
  34. #include "llvm/CodeGen/MachineInstr.h"
  35. #include "llvm/CodeGen/MachineInstrBuilder.h"
  36. #include "llvm/CodeGen/MachineInstrBundle.h"
  37. #include "llvm/CodeGen/MachineLoopInfo.h"
  38. #include "llvm/CodeGen/MachineOperand.h"
  39. #include "llvm/CodeGen/MachineRegisterInfo.h"
  40. #include "llvm/CodeGen/SlotIndexes.h"
  41. #include "llvm/CodeGen/Spiller.h"
  42. #include "llvm/CodeGen/StackMaps.h"
  43. #include "llvm/CodeGen/TargetInstrInfo.h"
  44. #include "llvm/CodeGen/TargetOpcodes.h"
  45. #include "llvm/CodeGen/TargetRegisterInfo.h"
  46. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  47. #include "llvm/CodeGen/VirtRegMap.h"
  48. #include "llvm/Config/llvm-config.h"
  49. #include "llvm/Support/BlockFrequency.h"
  50. #include "llvm/Support/BranchProbability.h"
  51. #include "llvm/Support/CommandLine.h"
  52. #include "llvm/Support/Compiler.h"
  53. #include "llvm/Support/Debug.h"
  54. #include "llvm/Support/ErrorHandling.h"
  55. #include "llvm/Support/raw_ostream.h"
  56. #include <cassert>
  57. #include <iterator>
  58. #include <tuple>
  59. #include <utility>
  60. #include <vector>
  61. using namespace llvm;
  62. #define DEBUG_TYPE "regalloc"
  63. STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
  64. STATISTIC(NumSnippets, "Number of spilled snippets");
  65. STATISTIC(NumSpills, "Number of spills inserted");
  66. STATISTIC(NumSpillsRemoved, "Number of spills removed");
  67. STATISTIC(NumReloads, "Number of reloads inserted");
  68. STATISTIC(NumReloadsRemoved, "Number of reloads removed");
  69. STATISTIC(NumFolded, "Number of folded stack accesses");
  70. STATISTIC(NumFoldedLoads, "Number of folded loads");
  71. STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
  72. static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
  73. cl::desc("Disable inline spill hoisting"));
  74. static cl::opt<bool>
  75. RestrictStatepointRemat("restrict-statepoint-remat",
  76. cl::init(false), cl::Hidden,
  77. cl::desc("Restrict remat for statepoint operands"));
  78. namespace {
  79. class HoistSpillHelper : private LiveRangeEdit::Delegate {
  80. MachineFunction &MF;
  81. LiveIntervals &LIS;
  82. LiveStacks &LSS;
  83. AliasAnalysis *AA;
  84. MachineDominatorTree &MDT;
  85. MachineLoopInfo &Loops;
  86. VirtRegMap &VRM;
  87. MachineRegisterInfo &MRI;
  88. const TargetInstrInfo &TII;
  89. const TargetRegisterInfo &TRI;
  90. const MachineBlockFrequencyInfo &MBFI;
  91. InsertPointAnalysis IPA;
  92. // Map from StackSlot to the LiveInterval of the original register.
  93. // Note the LiveInterval of the original register may have been deleted
  94. // after it is spilled. We keep a copy here to track the range where
  95. // spills can be moved.
  96. DenseMap<int, std::unique_ptr<LiveInterval>> StackSlotToOrigLI;
  97. // Map from pair of (StackSlot and Original VNI) to a set of spills which
  98. // have the same stackslot and have equal values defined by Original VNI.
  99. // These spills are mergeable and are hoist candiates.
  100. using MergeableSpillsMap =
  101. MapVector<std::pair<int, VNInfo *>, SmallPtrSet<MachineInstr *, 16>>;
  102. MergeableSpillsMap MergeableSpills;
  103. /// This is the map from original register to a set containing all its
  104. /// siblings. To hoist a spill to another BB, we need to find out a live
  105. /// sibling there and use it as the source of the new spill.
  106. DenseMap<Register, SmallSetVector<Register, 16>> Virt2SiblingsMap;
  107. bool isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
  108. MachineBasicBlock &BB, Register &LiveReg);
  109. void rmRedundantSpills(
  110. SmallPtrSet<MachineInstr *, 16> &Spills,
  111. SmallVectorImpl<MachineInstr *> &SpillsToRm,
  112. DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill);
  113. void getVisitOrders(
  114. MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills,
  115. SmallVectorImpl<MachineDomTreeNode *> &Orders,
  116. SmallVectorImpl<MachineInstr *> &SpillsToRm,
  117. DenseMap<MachineDomTreeNode *, unsigned> &SpillsToKeep,
  118. DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill);
  119. void runHoistSpills(LiveInterval &OrigLI, VNInfo &OrigVNI,
  120. SmallPtrSet<MachineInstr *, 16> &Spills,
  121. SmallVectorImpl<MachineInstr *> &SpillsToRm,
  122. DenseMap<MachineBasicBlock *, unsigned> &SpillsToIns);
  123. public:
  124. HoistSpillHelper(MachineFunctionPass &pass, MachineFunction &mf,
  125. VirtRegMap &vrm)
  126. : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
  127. LSS(pass.getAnalysis<LiveStacks>()),
  128. AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
  129. MDT(pass.getAnalysis<MachineDominatorTree>()),
  130. Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
  131. MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()),
  132. TRI(*mf.getSubtarget().getRegisterInfo()),
  133. MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()),
  134. IPA(LIS, mf.getNumBlockIDs()) {}
  135. void addToMergeableSpills(MachineInstr &Spill, int StackSlot,
  136. unsigned Original);
  137. bool rmFromMergeableSpills(MachineInstr &Spill, int StackSlot);
  138. void hoistAllSpills();
  139. void LRE_DidCloneVirtReg(Register, Register) override;
  140. };
  141. class InlineSpiller : public Spiller {
  142. MachineFunction &MF;
  143. LiveIntervals &LIS;
  144. LiveStacks &LSS;
  145. AliasAnalysis *AA;
  146. MachineDominatorTree &MDT;
  147. MachineLoopInfo &Loops;
  148. VirtRegMap &VRM;
  149. MachineRegisterInfo &MRI;
  150. const TargetInstrInfo &TII;
  151. const TargetRegisterInfo &TRI;
  152. const MachineBlockFrequencyInfo &MBFI;
  153. // Variables that are valid during spill(), but used by multiple methods.
  154. LiveRangeEdit *Edit;
  155. LiveInterval *StackInt;
  156. int StackSlot;
  157. Register Original;
  158. // All registers to spill to StackSlot, including the main register.
  159. SmallVector<Register, 8> RegsToSpill;
  160. // All COPY instructions to/from snippets.
  161. // They are ignored since both operands refer to the same stack slot.
  162. SmallPtrSet<MachineInstr*, 8> SnippetCopies;
  163. // Values that failed to remat at some point.
  164. SmallPtrSet<VNInfo*, 8> UsedValues;
  165. // Dead defs generated during spilling.
  166. SmallVector<MachineInstr*, 8> DeadDefs;
  167. // Object records spills information and does the hoisting.
  168. HoistSpillHelper HSpiller;
  169. // Live range weight calculator.
  170. VirtRegAuxInfo &VRAI;
  171. ~InlineSpiller() override = default;
  172. public:
  173. InlineSpiller(MachineFunctionPass &Pass, MachineFunction &MF, VirtRegMap &VRM,
  174. VirtRegAuxInfo &VRAI)
  175. : MF(MF), LIS(Pass.getAnalysis<LiveIntervals>()),
  176. LSS(Pass.getAnalysis<LiveStacks>()),
  177. AA(&Pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
  178. MDT(Pass.getAnalysis<MachineDominatorTree>()),
  179. Loops(Pass.getAnalysis<MachineLoopInfo>()), VRM(VRM),
  180. MRI(MF.getRegInfo()), TII(*MF.getSubtarget().getInstrInfo()),
  181. TRI(*MF.getSubtarget().getRegisterInfo()),
  182. MBFI(Pass.getAnalysis<MachineBlockFrequencyInfo>()),
  183. HSpiller(Pass, MF, VRM), VRAI(VRAI) {}
  184. void spill(LiveRangeEdit &) override;
  185. void postOptimization() override;
  186. private:
  187. bool isSnippet(const LiveInterval &SnipLI);
  188. void collectRegsToSpill();
  189. bool isRegToSpill(Register Reg) { return is_contained(RegsToSpill, Reg); }
  190. bool isSibling(Register Reg);
  191. bool hoistSpillInsideBB(LiveInterval &SpillLI, MachineInstr &CopyMI);
  192. void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
  193. void markValueUsed(LiveInterval*, VNInfo*);
  194. bool canGuaranteeAssignmentAfterRemat(Register VReg, MachineInstr &MI);
  195. bool reMaterializeFor(LiveInterval &, MachineInstr &MI);
  196. void reMaterializeAll();
  197. bool coalesceStackAccess(MachineInstr *MI, Register Reg);
  198. bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>>,
  199. MachineInstr *LoadMI = nullptr);
  200. void insertReload(Register VReg, SlotIndex, MachineBasicBlock::iterator MI);
  201. void insertSpill(Register VReg, bool isKill, MachineBasicBlock::iterator MI);
  202. void spillAroundUses(Register Reg);
  203. void spillAll();
  204. };
  205. } // end anonymous namespace
  206. Spiller::~Spiller() = default;
  207. void Spiller::anchor() {}
  208. Spiller *llvm::createInlineSpiller(MachineFunctionPass &Pass,
  209. MachineFunction &MF, VirtRegMap &VRM,
  210. VirtRegAuxInfo &VRAI) {
  211. return new InlineSpiller(Pass, MF, VRM, VRAI);
  212. }
  213. //===----------------------------------------------------------------------===//
  214. // Snippets
  215. //===----------------------------------------------------------------------===//
  216. // When spilling a virtual register, we also spill any snippets it is connected
  217. // to. The snippets are small live ranges that only have a single real use,
  218. // leftovers from live range splitting. Spilling them enables memory operand
  219. // folding or tightens the live range around the single use.
  220. //
  221. // This minimizes register pressure and maximizes the store-to-load distance for
  222. // spill slots which can be important in tight loops.
  223. /// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
  224. /// otherwise return 0.
  225. static Register isFullCopyOf(const MachineInstr &MI, Register Reg) {
  226. if (!MI.isFullCopy())
  227. return Register();
  228. if (MI.getOperand(0).getReg() == Reg)
  229. return MI.getOperand(1).getReg();
  230. if (MI.getOperand(1).getReg() == Reg)
  231. return MI.getOperand(0).getReg();
  232. return Register();
  233. }
  234. static void getVDefInterval(const MachineInstr &MI, LiveIntervals &LIS) {
  235. for (const MachineOperand &MO : MI.operands())
  236. if (MO.isReg() && MO.isDef() && Register::isVirtualRegister(MO.getReg()))
  237. LIS.getInterval(MO.getReg());
  238. }
  239. /// isSnippet - Identify if a live interval is a snippet that should be spilled.
  240. /// It is assumed that SnipLI is a virtual register with the same original as
  241. /// Edit->getReg().
  242. bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
  243. Register Reg = Edit->getReg();
  244. // A snippet is a tiny live range with only a single instruction using it
  245. // besides copies to/from Reg or spills/fills. We accept:
  246. //
  247. // %snip = COPY %Reg / FILL fi#
  248. // %snip = USE %snip
  249. // %Reg = COPY %snip / SPILL %snip, fi#
  250. //
  251. if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
  252. return false;
  253. MachineInstr *UseMI = nullptr;
  254. // Check that all uses satisfy our criteria.
  255. for (MachineRegisterInfo::reg_instr_nodbg_iterator
  256. RI = MRI.reg_instr_nodbg_begin(SnipLI.reg()),
  257. E = MRI.reg_instr_nodbg_end();
  258. RI != E;) {
  259. MachineInstr &MI = *RI++;
  260. // Allow copies to/from Reg.
  261. if (isFullCopyOf(MI, Reg))
  262. continue;
  263. // Allow stack slot loads.
  264. int FI;
  265. if (SnipLI.reg() == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
  266. continue;
  267. // Allow stack slot stores.
  268. if (SnipLI.reg() == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
  269. continue;
  270. // Allow a single additional instruction.
  271. if (UseMI && &MI != UseMI)
  272. return false;
  273. UseMI = &MI;
  274. }
  275. return true;
  276. }
  277. /// collectRegsToSpill - Collect live range snippets that only have a single
  278. /// real use.
  279. void InlineSpiller::collectRegsToSpill() {
  280. Register Reg = Edit->getReg();
  281. // Main register always spills.
  282. RegsToSpill.assign(1, Reg);
  283. SnippetCopies.clear();
  284. // Snippets all have the same original, so there can't be any for an original
  285. // register.
  286. if (Original == Reg)
  287. return;
  288. for (MachineInstr &MI :
  289. llvm::make_early_inc_range(MRI.reg_instructions(Reg))) {
  290. Register SnipReg = isFullCopyOf(MI, Reg);
  291. if (!isSibling(SnipReg))
  292. continue;
  293. LiveInterval &SnipLI = LIS.getInterval(SnipReg);
  294. if (!isSnippet(SnipLI))
  295. continue;
  296. SnippetCopies.insert(&MI);
  297. if (isRegToSpill(SnipReg))
  298. continue;
  299. RegsToSpill.push_back(SnipReg);
  300. LLVM_DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
  301. ++NumSnippets;
  302. }
  303. }
  304. bool InlineSpiller::isSibling(Register Reg) {
  305. return Reg.isVirtual() && VRM.getOriginal(Reg) == Original;
  306. }
  307. /// It is beneficial to spill to earlier place in the same BB in case
  308. /// as follows:
  309. /// There is an alternative def earlier in the same MBB.
  310. /// Hoist the spill as far as possible in SpillMBB. This can ease
  311. /// register pressure:
  312. ///
  313. /// x = def
  314. /// y = use x
  315. /// s = copy x
  316. ///
  317. /// Hoisting the spill of s to immediately after the def removes the
  318. /// interference between x and y:
  319. ///
  320. /// x = def
  321. /// spill x
  322. /// y = use killed x
  323. ///
  324. /// This hoist only helps when the copy kills its source.
  325. ///
  326. bool InlineSpiller::hoistSpillInsideBB(LiveInterval &SpillLI,
  327. MachineInstr &CopyMI) {
  328. SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
  329. #ifndef NDEBUG
  330. VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
  331. assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
  332. #endif
  333. Register SrcReg = CopyMI.getOperand(1).getReg();
  334. LiveInterval &SrcLI = LIS.getInterval(SrcReg);
  335. VNInfo *SrcVNI = SrcLI.getVNInfoAt(Idx);
  336. LiveQueryResult SrcQ = SrcLI.Query(Idx);
  337. MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(SrcVNI->def);
  338. if (DefMBB != CopyMI.getParent() || !SrcQ.isKill())
  339. return false;
  340. // Conservatively extend the stack slot range to the range of the original
  341. // value. We may be able to do better with stack slot coloring by being more
  342. // careful here.
  343. assert(StackInt && "No stack slot assigned yet.");
  344. LiveInterval &OrigLI = LIS.getInterval(Original);
  345. VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
  346. StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
  347. LLVM_DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
  348. << *StackInt << '\n');
  349. // We are going to spill SrcVNI immediately after its def, so clear out
  350. // any later spills of the same value.
  351. eliminateRedundantSpills(SrcLI, SrcVNI);
  352. MachineBasicBlock *MBB = LIS.getMBBFromIndex(SrcVNI->def);
  353. MachineBasicBlock::iterator MII;
  354. if (SrcVNI->isPHIDef())
  355. MII = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
  356. else {
  357. MachineInstr *DefMI = LIS.getInstructionFromIndex(SrcVNI->def);
  358. assert(DefMI && "Defining instruction disappeared");
  359. MII = DefMI;
  360. ++MII;
  361. }
  362. MachineInstrSpan MIS(MII, MBB);
  363. // Insert spill without kill flag immediately after def.
  364. TII.storeRegToStackSlot(*MBB, MII, SrcReg, false, StackSlot,
  365. MRI.getRegClass(SrcReg), &TRI);
  366. LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MII);
  367. for (const MachineInstr &MI : make_range(MIS.begin(), MII))
  368. getVDefInterval(MI, LIS);
  369. --MII; // Point to store instruction.
  370. LLVM_DEBUG(dbgs() << "\thoisted: " << SrcVNI->def << '\t' << *MII);
  371. // If there is only 1 store instruction is required for spill, add it
  372. // to mergeable list. In X86 AMX, 2 intructions are required to store.
  373. // We disable the merge for this case.
  374. if (MIS.begin() == MII)
  375. HSpiller.addToMergeableSpills(*MII, StackSlot, Original);
  376. ++NumSpills;
  377. return true;
  378. }
  379. /// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
  380. /// redundant spills of this value in SLI.reg and sibling copies.
  381. void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
  382. assert(VNI && "Missing value");
  383. SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
  384. WorkList.push_back(std::make_pair(&SLI, VNI));
  385. assert(StackInt && "No stack slot assigned yet.");
  386. do {
  387. LiveInterval *LI;
  388. std::tie(LI, VNI) = WorkList.pop_back_val();
  389. Register Reg = LI->reg();
  390. LLVM_DEBUG(dbgs() << "Checking redundant spills for " << VNI->id << '@'
  391. << VNI->def << " in " << *LI << '\n');
  392. // Regs to spill are taken care of.
  393. if (isRegToSpill(Reg))
  394. continue;
  395. // Add all of VNI's live range to StackInt.
  396. StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
  397. LLVM_DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
  398. // Find all spills and copies of VNI.
  399. for (MachineInstr &MI :
  400. llvm::make_early_inc_range(MRI.use_nodbg_instructions(Reg))) {
  401. if (!MI.isCopy() && !MI.mayStore())
  402. continue;
  403. SlotIndex Idx = LIS.getInstructionIndex(MI);
  404. if (LI->getVNInfoAt(Idx) != VNI)
  405. continue;
  406. // Follow sibling copies down the dominator tree.
  407. if (Register DstReg = isFullCopyOf(MI, Reg)) {
  408. if (isSibling(DstReg)) {
  409. LiveInterval &DstLI = LIS.getInterval(DstReg);
  410. VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
  411. assert(DstVNI && "Missing defined value");
  412. assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
  413. WorkList.push_back(std::make_pair(&DstLI, DstVNI));
  414. }
  415. continue;
  416. }
  417. // Erase spills.
  418. int FI;
  419. if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
  420. LLVM_DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << MI);
  421. // eliminateDeadDefs won't normally remove stores, so switch opcode.
  422. MI.setDesc(TII.get(TargetOpcode::KILL));
  423. DeadDefs.push_back(&MI);
  424. ++NumSpillsRemoved;
  425. if (HSpiller.rmFromMergeableSpills(MI, StackSlot))
  426. --NumSpills;
  427. }
  428. }
  429. } while (!WorkList.empty());
  430. }
  431. //===----------------------------------------------------------------------===//
  432. // Rematerialization
  433. //===----------------------------------------------------------------------===//
  434. /// markValueUsed - Remember that VNI failed to rematerialize, so its defining
  435. /// instruction cannot be eliminated. See through snippet copies
  436. void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
  437. SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
  438. WorkList.push_back(std::make_pair(LI, VNI));
  439. do {
  440. std::tie(LI, VNI) = WorkList.pop_back_val();
  441. if (!UsedValues.insert(VNI).second)
  442. continue;
  443. if (VNI->isPHIDef()) {
  444. MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
  445. for (MachineBasicBlock *P : MBB->predecessors()) {
  446. VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(P));
  447. if (PVNI)
  448. WorkList.push_back(std::make_pair(LI, PVNI));
  449. }
  450. continue;
  451. }
  452. // Follow snippet copies.
  453. MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
  454. if (!SnippetCopies.count(MI))
  455. continue;
  456. LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
  457. assert(isRegToSpill(SnipLI.reg()) && "Unexpected register in copy");
  458. VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
  459. assert(SnipVNI && "Snippet undefined before copy");
  460. WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
  461. } while (!WorkList.empty());
  462. }
  463. bool InlineSpiller::canGuaranteeAssignmentAfterRemat(Register VReg,
  464. MachineInstr &MI) {
  465. if (!RestrictStatepointRemat)
  466. return true;
  467. // Here's a quick explanation of the problem we're trying to handle here:
  468. // * There are some pseudo instructions with more vreg uses than there are
  469. // physical registers on the machine.
  470. // * This is normally handled by spilling the vreg, and folding the reload
  471. // into the user instruction. (Thus decreasing the number of used vregs
  472. // until the remainder can be assigned to physregs.)
  473. // * However, since we may try to spill vregs in any order, we can end up
  474. // trying to spill each operand to the instruction, and then rematting it
  475. // instead. When that happens, the new live intervals (for the remats) are
  476. // expected to be trivially assignable (i.e. RS_Done). However, since we
  477. // may have more remats than physregs, we're guaranteed to fail to assign
  478. // one.
  479. // At the moment, we only handle this for STATEPOINTs since they're the only
  480. // pseudo op where we've seen this. If we start seeing other instructions
  481. // with the same problem, we need to revisit this.
  482. if (MI.getOpcode() != TargetOpcode::STATEPOINT)
  483. return true;
  484. // For STATEPOINTs we allow re-materialization for fixed arguments only hoping
  485. // that number of physical registers is enough to cover all fixed arguments.
  486. // If it is not true we need to revisit it.
  487. for (unsigned Idx = StatepointOpers(&MI).getVarIdx(),
  488. EndIdx = MI.getNumOperands();
  489. Idx < EndIdx; ++Idx) {
  490. MachineOperand &MO = MI.getOperand(Idx);
  491. if (MO.isReg() && MO.getReg() == VReg)
  492. return false;
  493. }
  494. return true;
  495. }
  496. /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
  497. bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) {
  498. // Analyze instruction
  499. SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops;
  500. VirtRegInfo RI = AnalyzeVirtRegInBundle(MI, VirtReg.reg(), &Ops);
  501. if (!RI.Reads)
  502. return false;
  503. SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
  504. VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
  505. if (!ParentVNI) {
  506. LLVM_DEBUG(dbgs() << "\tadding <undef> flags: ");
  507. for (MachineOperand &MO : MI.operands())
  508. if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg())
  509. MO.setIsUndef();
  510. LLVM_DEBUG(dbgs() << UseIdx << '\t' << MI);
  511. return true;
  512. }
  513. if (SnippetCopies.count(&MI))
  514. return false;
  515. LiveInterval &OrigLI = LIS.getInterval(Original);
  516. VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
  517. LiveRangeEdit::Remat RM(ParentVNI);
  518. RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
  519. if (!Edit->canRematerializeAt(RM, OrigVNI, UseIdx, false)) {
  520. markValueUsed(&VirtReg, ParentVNI);
  521. LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
  522. return false;
  523. }
  524. // If the instruction also writes VirtReg.reg, it had better not require the
  525. // same register for uses and defs.
  526. if (RI.Tied) {
  527. markValueUsed(&VirtReg, ParentVNI);
  528. LLVM_DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << MI);
  529. return false;
  530. }
  531. // Before rematerializing into a register for a single instruction, try to
  532. // fold a load into the instruction. That avoids allocating a new register.
  533. if (RM.OrigMI->canFoldAsLoad() &&
  534. foldMemoryOperand(Ops, RM.OrigMI)) {
  535. Edit->markRematerialized(RM.ParentVNI);
  536. ++NumFoldedLoads;
  537. return true;
  538. }
  539. // If we can't guarantee that we'll be able to actually assign the new vreg,
  540. // we can't remat.
  541. if (!canGuaranteeAssignmentAfterRemat(VirtReg.reg(), MI)) {
  542. markValueUsed(&VirtReg, ParentVNI);
  543. LLVM_DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
  544. return false;
  545. }
  546. // Allocate a new register for the remat.
  547. Register NewVReg = Edit->createFrom(Original);
  548. // Finally we can rematerialize OrigMI before MI.
  549. SlotIndex DefIdx =
  550. Edit->rematerializeAt(*MI.getParent(), MI, NewVReg, RM, TRI);
  551. // We take the DebugLoc from MI, since OrigMI may be attributed to a
  552. // different source location.
  553. auto *NewMI = LIS.getInstructionFromIndex(DefIdx);
  554. NewMI->setDebugLoc(MI.getDebugLoc());
  555. (void)DefIdx;
  556. LLVM_DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
  557. << *LIS.getInstructionFromIndex(DefIdx));
  558. // Replace operands
  559. for (const auto &OpPair : Ops) {
  560. MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
  561. if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg()) {
  562. MO.setReg(NewVReg);
  563. MO.setIsKill();
  564. }
  565. }
  566. LLVM_DEBUG(dbgs() << "\t " << UseIdx << '\t' << MI << '\n');
  567. ++NumRemats;
  568. return true;
  569. }
  570. /// reMaterializeAll - Try to rematerialize as many uses as possible,
  571. /// and trim the live ranges after.
  572. void InlineSpiller::reMaterializeAll() {
  573. if (!Edit->anyRematerializable(AA))
  574. return;
  575. UsedValues.clear();
  576. // Try to remat before all uses of snippets.
  577. bool anyRemat = false;
  578. for (Register Reg : RegsToSpill) {
  579. LiveInterval &LI = LIS.getInterval(Reg);
  580. for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
  581. // Debug values are not allowed to affect codegen.
  582. if (MI.isDebugValue())
  583. continue;
  584. assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
  585. "instruction that isn't a DBG_VALUE");
  586. anyRemat |= reMaterializeFor(LI, MI);
  587. }
  588. }
  589. if (!anyRemat)
  590. return;
  591. // Remove any values that were completely rematted.
  592. for (Register Reg : RegsToSpill) {
  593. LiveInterval &LI = LIS.getInterval(Reg);
  594. for (VNInfo *VNI : llvm::make_range(LI.vni_begin(), LI.vni_end())) {
  595. if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
  596. continue;
  597. MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
  598. MI->addRegisterDead(Reg, &TRI);
  599. if (!MI->allDefsAreDead())
  600. continue;
  601. LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
  602. DeadDefs.push_back(MI);
  603. }
  604. }
  605. // Eliminate dead code after remat. Note that some snippet copies may be
  606. // deleted here.
  607. if (DeadDefs.empty())
  608. return;
  609. LLVM_DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
  610. Edit->eliminateDeadDefs(DeadDefs, RegsToSpill, AA);
  611. // LiveRangeEdit::eliminateDeadDef is used to remove dead define instructions
  612. // after rematerialization. To remove a VNI for a vreg from its LiveInterval,
  613. // LiveIntervals::removeVRegDefAt is used. However, after non-PHI VNIs are all
  614. // removed, PHI VNI are still left in the LiveInterval.
  615. // So to get rid of unused reg, we need to check whether it has non-dbg
  616. // reference instead of whether it has non-empty interval.
  617. unsigned ResultPos = 0;
  618. for (Register Reg : RegsToSpill) {
  619. if (MRI.reg_nodbg_empty(Reg)) {
  620. Edit->eraseVirtReg(Reg);
  621. continue;
  622. }
  623. assert(LIS.hasInterval(Reg) &&
  624. (!LIS.getInterval(Reg).empty() || !MRI.reg_nodbg_empty(Reg)) &&
  625. "Empty and not used live-range?!");
  626. RegsToSpill[ResultPos++] = Reg;
  627. }
  628. RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
  629. LLVM_DEBUG(dbgs() << RegsToSpill.size()
  630. << " registers to spill after remat.\n");
  631. }
  632. //===----------------------------------------------------------------------===//
  633. // Spilling
  634. //===----------------------------------------------------------------------===//
  635. /// If MI is a load or store of StackSlot, it can be removed.
  636. bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, Register Reg) {
  637. int FI = 0;
  638. Register InstrReg = TII.isLoadFromStackSlot(*MI, FI);
  639. bool IsLoad = InstrReg;
  640. if (!IsLoad)
  641. InstrReg = TII.isStoreToStackSlot(*MI, FI);
  642. // We have a stack access. Is it the right register and slot?
  643. if (InstrReg != Reg || FI != StackSlot)
  644. return false;
  645. if (!IsLoad)
  646. HSpiller.rmFromMergeableSpills(*MI, StackSlot);
  647. LLVM_DEBUG(dbgs() << "Coalescing stack access: " << *MI);
  648. LIS.RemoveMachineInstrFromMaps(*MI);
  649. MI->eraseFromParent();
  650. if (IsLoad) {
  651. ++NumReloadsRemoved;
  652. --NumReloads;
  653. } else {
  654. ++NumSpillsRemoved;
  655. --NumSpills;
  656. }
  657. return true;
  658. }
  659. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  660. LLVM_DUMP_METHOD
  661. // Dump the range of instructions from B to E with their slot indexes.
  662. static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,
  663. MachineBasicBlock::iterator E,
  664. LiveIntervals const &LIS,
  665. const char *const header,
  666. Register VReg = Register()) {
  667. char NextLine = '\n';
  668. char SlotIndent = '\t';
  669. if (std::next(B) == E) {
  670. NextLine = ' ';
  671. SlotIndent = ' ';
  672. }
  673. dbgs() << '\t' << header << ": " << NextLine;
  674. for (MachineBasicBlock::iterator I = B; I != E; ++I) {
  675. SlotIndex Idx = LIS.getInstructionIndex(*I).getRegSlot();
  676. // If a register was passed in and this instruction has it as a
  677. // destination that is marked as an early clobber, print the
  678. // early-clobber slot index.
  679. if (VReg) {
  680. MachineOperand *MO = I->findRegisterDefOperand(VReg);
  681. if (MO && MO->isEarlyClobber())
  682. Idx = Idx.getRegSlot(true);
  683. }
  684. dbgs() << SlotIndent << Idx << '\t' << *I;
  685. }
  686. }
  687. #endif
  688. /// foldMemoryOperand - Try folding stack slot references in Ops into their
  689. /// instructions.
  690. ///
  691. /// @param Ops Operand indices from AnalyzeVirtRegInBundle().
  692. /// @param LoadMI Load instruction to use instead of stack slot when non-null.
  693. /// @return True on success.
  694. bool InlineSpiller::
  695. foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
  696. MachineInstr *LoadMI) {
  697. if (Ops.empty())
  698. return false;
  699. // Don't attempt folding in bundles.
  700. MachineInstr *MI = Ops.front().first;
  701. if (Ops.back().first != MI || MI->isBundled())
  702. return false;
  703. bool WasCopy = MI->isCopy();
  704. Register ImpReg;
  705. // TII::foldMemoryOperand will do what we need here for statepoint
  706. // (fold load into use and remove corresponding def). We will replace
  707. // uses of removed def with loads (spillAroundUses).
  708. // For that to work we need to untie def and use to pass it through
  709. // foldMemoryOperand and signal foldPatchpoint that it is allowed to
  710. // fold them.
  711. bool UntieRegs = MI->getOpcode() == TargetOpcode::STATEPOINT;
  712. // Spill subregs if the target allows it.
  713. // We always want to spill subregs for stackmap/patchpoint pseudos.
  714. bool SpillSubRegs = TII.isSubregFoldable() ||
  715. MI->getOpcode() == TargetOpcode::STATEPOINT ||
  716. MI->getOpcode() == TargetOpcode::PATCHPOINT ||
  717. MI->getOpcode() == TargetOpcode::STACKMAP;
  718. // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
  719. // operands.
  720. SmallVector<unsigned, 8> FoldOps;
  721. for (const auto &OpPair : Ops) {
  722. unsigned Idx = OpPair.second;
  723. assert(MI == OpPair.first && "Instruction conflict during operand folding");
  724. MachineOperand &MO = MI->getOperand(Idx);
  725. if (MO.isImplicit()) {
  726. ImpReg = MO.getReg();
  727. continue;
  728. }
  729. if (!SpillSubRegs && MO.getSubReg())
  730. return false;
  731. // We cannot fold a load instruction into a def.
  732. if (LoadMI && MO.isDef())
  733. return false;
  734. // Tied use operands should not be passed to foldMemoryOperand.
  735. if (UntieRegs || !MI->isRegTiedToDefOperand(Idx))
  736. FoldOps.push_back(Idx);
  737. }
  738. // If we only have implicit uses, we won't be able to fold that.
  739. // Moreover, TargetInstrInfo::foldMemoryOperand will assert if we try!
  740. if (FoldOps.empty())
  741. return false;
  742. MachineInstrSpan MIS(MI, MI->getParent());
  743. SmallVector<std::pair<unsigned, unsigned> > TiedOps;
  744. if (UntieRegs)
  745. for (unsigned Idx : FoldOps) {
  746. MachineOperand &MO = MI->getOperand(Idx);
  747. if (!MO.isTied())
  748. continue;
  749. unsigned Tied = MI->findTiedOperandIdx(Idx);
  750. if (MO.isUse())
  751. TiedOps.emplace_back(Tied, Idx);
  752. else {
  753. assert(MO.isDef() && "Tied to not use and def?");
  754. TiedOps.emplace_back(Idx, Tied);
  755. }
  756. MI->untieRegOperand(Idx);
  757. }
  758. MachineInstr *FoldMI =
  759. LoadMI ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, &LIS)
  760. : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, &LIS, &VRM);
  761. if (!FoldMI) {
  762. // Re-tie operands.
  763. for (auto Tied : TiedOps)
  764. MI->tieOperands(Tied.first, Tied.second);
  765. return false;
  766. }
  767. // Remove LIS for any dead defs in the original MI not in FoldMI.
  768. for (MIBundleOperands MO(*MI); MO.isValid(); ++MO) {
  769. if (!MO->isReg())
  770. continue;
  771. Register Reg = MO->getReg();
  772. if (!Reg || Register::isVirtualRegister(Reg) || MRI.isReserved(Reg)) {
  773. continue;
  774. }
  775. // Skip non-Defs, including undef uses and internal reads.
  776. if (MO->isUse())
  777. continue;
  778. PhysRegInfo RI = AnalyzePhysRegInBundle(*FoldMI, Reg, &TRI);
  779. if (RI.FullyDefined)
  780. continue;
  781. // FoldMI does not define this physreg. Remove the LI segment.
  782. assert(MO->isDead() && "Cannot fold physreg def");
  783. SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
  784. LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
  785. }
  786. int FI;
  787. if (TII.isStoreToStackSlot(*MI, FI) &&
  788. HSpiller.rmFromMergeableSpills(*MI, FI))
  789. --NumSpills;
  790. LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
  791. // Update the call site info.
  792. if (MI->isCandidateForCallSiteEntry())
  793. MI->getMF()->moveCallSiteInfo(MI, FoldMI);
  794. // If we've folded a store into an instruction labelled with debug-info,
  795. // record a substitution from the old operand to the memory operand. Handle
  796. // the simple common case where operand 0 is the one being folded, plus when
  797. // the destination operand is also a tied def. More values could be
  798. // substituted / preserved with more analysis.
  799. if (MI->peekDebugInstrNum() && Ops[0].second == 0) {
  800. // Helper lambda.
  801. auto MakeSubstitution = [this,FoldMI,MI,&Ops]() {
  802. // Substitute old operand zero to the new instructions memory operand.
  803. unsigned OldOperandNum = Ops[0].second;
  804. unsigned NewNum = FoldMI->getDebugInstrNum();
  805. unsigned OldNum = MI->getDebugInstrNum();
  806. MF.makeDebugValueSubstitution({OldNum, OldOperandNum},
  807. {NewNum, MachineFunction::DebugOperandMemNumber});
  808. };
  809. const MachineOperand &Op0 = MI->getOperand(Ops[0].second);
  810. if (Ops.size() == 1 && Op0.isDef()) {
  811. MakeSubstitution();
  812. } else if (Ops.size() == 2 && Op0.isDef() && MI->getOperand(1).isTied() &&
  813. Op0.getReg() == MI->getOperand(1).getReg()) {
  814. MakeSubstitution();
  815. }
  816. } else if (MI->peekDebugInstrNum()) {
  817. // This is a debug-labelled instruction, but the operand being folded isn't
  818. // at operand zero. Most likely this means it's a load being folded in.
  819. // Substitute any register defs from operand zero up to the one being
  820. // folded -- past that point, we don't know what the new operand indexes
  821. // will be.
  822. MF.substituteDebugValuesForInst(*MI, *FoldMI, Ops[0].second);
  823. }
  824. MI->eraseFromParent();
  825. // Insert any new instructions other than FoldMI into the LIS maps.
  826. assert(!MIS.empty() && "Unexpected empty span of instructions!");
  827. for (MachineInstr &MI : MIS)
  828. if (&MI != FoldMI)
  829. LIS.InsertMachineInstrInMaps(MI);
  830. // TII.foldMemoryOperand may have left some implicit operands on the
  831. // instruction. Strip them.
  832. if (ImpReg)
  833. for (unsigned i = FoldMI->getNumOperands(); i; --i) {
  834. MachineOperand &MO = FoldMI->getOperand(i - 1);
  835. if (!MO.isReg() || !MO.isImplicit())
  836. break;
  837. if (MO.getReg() == ImpReg)
  838. FoldMI->RemoveOperand(i - 1);
  839. }
  840. LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
  841. "folded"));
  842. if (!WasCopy)
  843. ++NumFolded;
  844. else if (Ops.front().second == 0) {
  845. ++NumSpills;
  846. // If there is only 1 store instruction is required for spill, add it
  847. // to mergeable list. In X86 AMX, 2 intructions are required to store.
  848. // We disable the merge for this case.
  849. if (std::distance(MIS.begin(), MIS.end()) <= 1)
  850. HSpiller.addToMergeableSpills(*FoldMI, StackSlot, Original);
  851. } else
  852. ++NumReloads;
  853. return true;
  854. }
  855. void InlineSpiller::insertReload(Register NewVReg,
  856. SlotIndex Idx,
  857. MachineBasicBlock::iterator MI) {
  858. MachineBasicBlock &MBB = *MI->getParent();
  859. MachineInstrSpan MIS(MI, &MBB);
  860. TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
  861. MRI.getRegClass(NewVReg), &TRI);
  862. LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
  863. LLVM_DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
  864. NewVReg));
  865. ++NumReloads;
  866. }
  867. /// Check if \p Def fully defines a VReg with an undefined value.
  868. /// If that's the case, that means the value of VReg is actually
  869. /// not relevant.
  870. static bool isRealSpill(const MachineInstr &Def) {
  871. if (!Def.isImplicitDef())
  872. return true;
  873. assert(Def.getNumOperands() == 1 &&
  874. "Implicit def with more than one definition");
  875. // We can say that the VReg defined by Def is undef, only if it is
  876. // fully defined by Def. Otherwise, some of the lanes may not be
  877. // undef and the value of the VReg matters.
  878. return Def.getOperand(0).getSubReg();
  879. }
  880. /// insertSpill - Insert a spill of NewVReg after MI.
  881. void InlineSpiller::insertSpill(Register NewVReg, bool isKill,
  882. MachineBasicBlock::iterator MI) {
  883. // Spill are not terminators, so inserting spills after terminators will
  884. // violate invariants in MachineVerifier.
  885. assert(!MI->isTerminator() && "Inserting a spill after a terminator");
  886. MachineBasicBlock &MBB = *MI->getParent();
  887. MachineInstrSpan MIS(MI, &MBB);
  888. MachineBasicBlock::iterator SpillBefore = std::next(MI);
  889. bool IsRealSpill = isRealSpill(*MI);
  890. if (IsRealSpill)
  891. TII.storeRegToStackSlot(MBB, SpillBefore, NewVReg, isKill, StackSlot,
  892. MRI.getRegClass(NewVReg), &TRI);
  893. else
  894. // Don't spill undef value.
  895. // Anything works for undef, in particular keeping the memory
  896. // uninitialized is a viable option and it saves code size and
  897. // run time.
  898. BuildMI(MBB, SpillBefore, MI->getDebugLoc(), TII.get(TargetOpcode::KILL))
  899. .addReg(NewVReg, getKillRegState(isKill));
  900. MachineBasicBlock::iterator Spill = std::next(MI);
  901. LIS.InsertMachineInstrRangeInMaps(Spill, MIS.end());
  902. for (const MachineInstr &MI : make_range(Spill, MIS.end()))
  903. getVDefInterval(MI, LIS);
  904. LLVM_DEBUG(
  905. dumpMachineInstrRangeWithSlotIndex(Spill, MIS.end(), LIS, "spill"));
  906. ++NumSpills;
  907. // If there is only 1 store instruction is required for spill, add it
  908. // to mergeable list. In X86 AMX, 2 intructions are required to store.
  909. // We disable the merge for this case.
  910. if (IsRealSpill && std::distance(Spill, MIS.end()) <= 1)
  911. HSpiller.addToMergeableSpills(*Spill, StackSlot, Original);
  912. }
  913. /// spillAroundUses - insert spill code around each use of Reg.
  914. void InlineSpiller::spillAroundUses(Register Reg) {
  915. LLVM_DEBUG(dbgs() << "spillAroundUses " << printReg(Reg) << '\n');
  916. LiveInterval &OldLI = LIS.getInterval(Reg);
  917. // Iterate over instructions using Reg.
  918. for (MachineInstr &MI : llvm::make_early_inc_range(MRI.reg_bundles(Reg))) {
  919. // Debug values are not allowed to affect codegen.
  920. if (MI.isDebugValue()) {
  921. // Modify DBG_VALUE now that the value is in a spill slot.
  922. MachineBasicBlock *MBB = MI.getParent();
  923. LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:\t" << MI);
  924. buildDbgValueForSpill(*MBB, &MI, MI, StackSlot, Reg);
  925. MBB->erase(MI);
  926. continue;
  927. }
  928. assert(!MI.isDebugInstr() && "Did not expect to find a use in debug "
  929. "instruction that isn't a DBG_VALUE");
  930. // Ignore copies to/from snippets. We'll delete them.
  931. if (SnippetCopies.count(&MI))
  932. continue;
  933. // Stack slot accesses may coalesce away.
  934. if (coalesceStackAccess(&MI, Reg))
  935. continue;
  936. // Analyze instruction.
  937. SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
  938. VirtRegInfo RI = AnalyzeVirtRegInBundle(MI, Reg, &Ops);
  939. // Find the slot index where this instruction reads and writes OldLI.
  940. // This is usually the def slot, except for tied early clobbers.
  941. SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
  942. if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
  943. if (SlotIndex::isSameInstr(Idx, VNI->def))
  944. Idx = VNI->def;
  945. // Check for a sibling copy.
  946. Register SibReg = isFullCopyOf(MI, Reg);
  947. if (SibReg && isSibling(SibReg)) {
  948. // This may actually be a copy between snippets.
  949. if (isRegToSpill(SibReg)) {
  950. LLVM_DEBUG(dbgs() << "Found new snippet copy: " << MI);
  951. SnippetCopies.insert(&MI);
  952. continue;
  953. }
  954. if (RI.Writes) {
  955. if (hoistSpillInsideBB(OldLI, MI)) {
  956. // This COPY is now dead, the value is already in the stack slot.
  957. MI.getOperand(0).setIsDead();
  958. DeadDefs.push_back(&MI);
  959. continue;
  960. }
  961. } else {
  962. // This is a reload for a sib-reg copy. Drop spills downstream.
  963. LiveInterval &SibLI = LIS.getInterval(SibReg);
  964. eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
  965. // The COPY will fold to a reload below.
  966. }
  967. }
  968. // Attempt to fold memory ops.
  969. if (foldMemoryOperand(Ops))
  970. continue;
  971. // Create a new virtual register for spill/fill.
  972. // FIXME: Infer regclass from instruction alone.
  973. Register NewVReg = Edit->createFrom(Reg);
  974. if (RI.Reads)
  975. insertReload(NewVReg, Idx, &MI);
  976. // Rewrite instruction operands.
  977. bool hasLiveDef = false;
  978. for (const auto &OpPair : Ops) {
  979. MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
  980. MO.setReg(NewVReg);
  981. if (MO.isUse()) {
  982. if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
  983. MO.setIsKill();
  984. } else {
  985. if (!MO.isDead())
  986. hasLiveDef = true;
  987. }
  988. }
  989. LLVM_DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << MI << '\n');
  990. // FIXME: Use a second vreg if instruction has no tied ops.
  991. if (RI.Writes)
  992. if (hasLiveDef)
  993. insertSpill(NewVReg, true, &MI);
  994. }
  995. }
  996. /// spillAll - Spill all registers remaining after rematerialization.
  997. void InlineSpiller::spillAll() {
  998. // Update LiveStacks now that we are committed to spilling.
  999. if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
  1000. StackSlot = VRM.assignVirt2StackSlot(Original);
  1001. StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
  1002. StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
  1003. } else
  1004. StackInt = &LSS.getInterval(StackSlot);
  1005. if (Original != Edit->getReg())
  1006. VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
  1007. assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
  1008. for (Register Reg : RegsToSpill)
  1009. StackInt->MergeSegmentsInAsValue(LIS.getInterval(Reg),
  1010. StackInt->getValNumInfo(0));
  1011. LLVM_DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
  1012. // Spill around uses of all RegsToSpill.
  1013. for (Register Reg : RegsToSpill)
  1014. spillAroundUses(Reg);
  1015. // Hoisted spills may cause dead code.
  1016. if (!DeadDefs.empty()) {
  1017. LLVM_DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
  1018. Edit->eliminateDeadDefs(DeadDefs, RegsToSpill, AA);
  1019. }
  1020. // Finally delete the SnippetCopies.
  1021. for (Register Reg : RegsToSpill) {
  1022. for (MachineInstr &MI :
  1023. llvm::make_early_inc_range(MRI.reg_instructions(Reg))) {
  1024. assert(SnippetCopies.count(&MI) && "Remaining use wasn't a snippet copy");
  1025. // FIXME: Do this with a LiveRangeEdit callback.
  1026. LIS.RemoveMachineInstrFromMaps(MI);
  1027. MI.eraseFromParent();
  1028. }
  1029. }
  1030. // Delete all spilled registers.
  1031. for (Register Reg : RegsToSpill)
  1032. Edit->eraseVirtReg(Reg);
  1033. }
  1034. void InlineSpiller::spill(LiveRangeEdit &edit) {
  1035. ++NumSpilledRanges;
  1036. Edit = &edit;
  1037. assert(!Register::isStackSlot(edit.getReg()) &&
  1038. "Trying to spill a stack slot.");
  1039. // Share a stack slot among all descendants of Original.
  1040. Original = VRM.getOriginal(edit.getReg());
  1041. StackSlot = VRM.getStackSlot(Original);
  1042. StackInt = nullptr;
  1043. LLVM_DEBUG(dbgs() << "Inline spilling "
  1044. << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
  1045. << ':' << edit.getParent() << "\nFrom original "
  1046. << printReg(Original) << '\n');
  1047. assert(edit.getParent().isSpillable() &&
  1048. "Attempting to spill already spilled value.");
  1049. assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
  1050. collectRegsToSpill();
  1051. reMaterializeAll();
  1052. // Remat may handle everything.
  1053. if (!RegsToSpill.empty())
  1054. spillAll();
  1055. Edit->calculateRegClassAndHint(MF, VRAI);
  1056. }
  1057. /// Optimizations after all the reg selections and spills are done.
  1058. void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); }
  1059. /// When a spill is inserted, add the spill to MergeableSpills map.
  1060. void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot,
  1061. unsigned Original) {
  1062. BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
  1063. LiveInterval &OrigLI = LIS.getInterval(Original);
  1064. // save a copy of LiveInterval in StackSlotToOrigLI because the original
  1065. // LiveInterval may be cleared after all its references are spilled.
  1066. if (StackSlotToOrigLI.find(StackSlot) == StackSlotToOrigLI.end()) {
  1067. auto LI = std::make_unique<LiveInterval>(OrigLI.reg(), OrigLI.weight());
  1068. LI->assign(OrigLI, Allocator);
  1069. StackSlotToOrigLI[StackSlot] = std::move(LI);
  1070. }
  1071. SlotIndex Idx = LIS.getInstructionIndex(Spill);
  1072. VNInfo *OrigVNI = StackSlotToOrigLI[StackSlot]->getVNInfoAt(Idx.getRegSlot());
  1073. std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
  1074. MergeableSpills[MIdx].insert(&Spill);
  1075. }
  1076. /// When a spill is removed, remove the spill from MergeableSpills map.
  1077. /// Return true if the spill is removed successfully.
  1078. bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill,
  1079. int StackSlot) {
  1080. auto It = StackSlotToOrigLI.find(StackSlot);
  1081. if (It == StackSlotToOrigLI.end())
  1082. return false;
  1083. SlotIndex Idx = LIS.getInstructionIndex(Spill);
  1084. VNInfo *OrigVNI = It->second->getVNInfoAt(Idx.getRegSlot());
  1085. std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
  1086. return MergeableSpills[MIdx].erase(&Spill);
  1087. }
  1088. /// Check BB to see if it is a possible target BB to place a hoisted spill,
  1089. /// i.e., there should be a living sibling of OrigReg at the insert point.
  1090. bool HoistSpillHelper::isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI,
  1091. MachineBasicBlock &BB, Register &LiveReg) {
  1092. SlotIndex Idx = IPA.getLastInsertPoint(OrigLI, BB);
  1093. // The original def could be after the last insert point in the root block,
  1094. // we can't hoist to here.
  1095. if (Idx < OrigVNI.def) {
  1096. // TODO: We could be better here. If LI is not alive in landing pad
  1097. // we could hoist spill after LIP.
  1098. LLVM_DEBUG(dbgs() << "can't spill in root block - def after LIP\n");
  1099. return false;
  1100. }
  1101. Register OrigReg = OrigLI.reg();
  1102. SmallSetVector<Register, 16> &Siblings = Virt2SiblingsMap[OrigReg];
  1103. assert(OrigLI.getVNInfoAt(Idx) == &OrigVNI && "Unexpected VNI");
  1104. for (const Register &SibReg : Siblings) {
  1105. LiveInterval &LI = LIS.getInterval(SibReg);
  1106. VNInfo *VNI = LI.getVNInfoAt(Idx);
  1107. if (VNI) {
  1108. LiveReg = SibReg;
  1109. return true;
  1110. }
  1111. }
  1112. return false;
  1113. }
  1114. /// Remove redundant spills in the same BB. Save those redundant spills in
  1115. /// SpillsToRm, and save the spill to keep and its BB in SpillBBToSpill map.
  1116. void HoistSpillHelper::rmRedundantSpills(
  1117. SmallPtrSet<MachineInstr *, 16> &Spills,
  1118. SmallVectorImpl<MachineInstr *> &SpillsToRm,
  1119. DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill) {
  1120. // For each spill saw, check SpillBBToSpill[] and see if its BB already has
  1121. // another spill inside. If a BB contains more than one spill, only keep the
  1122. // earlier spill with smaller SlotIndex.
  1123. for (const auto CurrentSpill : Spills) {
  1124. MachineBasicBlock *Block = CurrentSpill->getParent();
  1125. MachineDomTreeNode *Node = MDT.getBase().getNode(Block);
  1126. MachineInstr *PrevSpill = SpillBBToSpill[Node];
  1127. if (PrevSpill) {
  1128. SlotIndex PIdx = LIS.getInstructionIndex(*PrevSpill);
  1129. SlotIndex CIdx = LIS.getInstructionIndex(*CurrentSpill);
  1130. MachineInstr *SpillToRm = (CIdx > PIdx) ? CurrentSpill : PrevSpill;
  1131. MachineInstr *SpillToKeep = (CIdx > PIdx) ? PrevSpill : CurrentSpill;
  1132. SpillsToRm.push_back(SpillToRm);
  1133. SpillBBToSpill[MDT.getBase().getNode(Block)] = SpillToKeep;
  1134. } else {
  1135. SpillBBToSpill[MDT.getBase().getNode(Block)] = CurrentSpill;
  1136. }
  1137. }
  1138. for (const auto SpillToRm : SpillsToRm)
  1139. Spills.erase(SpillToRm);
  1140. }
  1141. /// Starting from \p Root find a top-down traversal order of the dominator
  1142. /// tree to visit all basic blocks containing the elements of \p Spills.
  1143. /// Redundant spills will be found and put into \p SpillsToRm at the same
  1144. /// time. \p SpillBBToSpill will be populated as part of the process and
  1145. /// maps a basic block to the first store occurring in the basic block.
  1146. /// \post SpillsToRm.union(Spills\@post) == Spills\@pre
  1147. void HoistSpillHelper::getVisitOrders(
  1148. MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills,
  1149. SmallVectorImpl<MachineDomTreeNode *> &Orders,
  1150. SmallVectorImpl<MachineInstr *> &SpillsToRm,
  1151. DenseMap<MachineDomTreeNode *, unsigned> &SpillsToKeep,
  1152. DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill) {
  1153. // The set contains all the possible BB nodes to which we may hoist
  1154. // original spills.
  1155. SmallPtrSet<MachineDomTreeNode *, 8> WorkSet;
  1156. // Save the BB nodes on the path from the first BB node containing
  1157. // non-redundant spill to the Root node.
  1158. SmallPtrSet<MachineDomTreeNode *, 8> NodesOnPath;
  1159. // All the spills to be hoisted must originate from a single def instruction
  1160. // to the OrigReg. It means the def instruction should dominate all the spills
  1161. // to be hoisted. We choose the BB where the def instruction is located as
  1162. // the Root.
  1163. MachineDomTreeNode *RootIDomNode = MDT[Root]->getIDom();
  1164. // For every node on the dominator tree with spill, walk up on the dominator
  1165. // tree towards the Root node until it is reached. If there is other node
  1166. // containing spill in the middle of the path, the previous spill saw will
  1167. // be redundant and the node containing it will be removed. All the nodes on
  1168. // the path starting from the first node with non-redundant spill to the Root
  1169. // node will be added to the WorkSet, which will contain all the possible
  1170. // locations where spills may be hoisted to after the loop below is done.
  1171. for (const auto Spill : Spills) {
  1172. MachineBasicBlock *Block = Spill->getParent();
  1173. MachineDomTreeNode *Node = MDT[Block];
  1174. MachineInstr *SpillToRm = nullptr;
  1175. while (Node != RootIDomNode) {
  1176. // If Node dominates Block, and it already contains a spill, the spill in
  1177. // Block will be redundant.
  1178. if (Node != MDT[Block] && SpillBBToSpill[Node]) {
  1179. SpillToRm = SpillBBToSpill[MDT[Block]];
  1180. break;
  1181. /// If we see the Node already in WorkSet, the path from the Node to
  1182. /// the Root node must already be traversed by another spill.
  1183. /// Then no need to repeat.
  1184. } else if (WorkSet.count(Node)) {
  1185. break;
  1186. } else {
  1187. NodesOnPath.insert(Node);
  1188. }
  1189. Node = Node->getIDom();
  1190. }
  1191. if (SpillToRm) {
  1192. SpillsToRm.push_back(SpillToRm);
  1193. } else {
  1194. // Add a BB containing the original spills to SpillsToKeep -- i.e.,
  1195. // set the initial status before hoisting start. The value of BBs
  1196. // containing original spills is set to 0, in order to descriminate
  1197. // with BBs containing hoisted spills which will be inserted to
  1198. // SpillsToKeep later during hoisting.
  1199. SpillsToKeep[MDT[Block]] = 0;
  1200. WorkSet.insert(NodesOnPath.begin(), NodesOnPath.end());
  1201. }
  1202. NodesOnPath.clear();
  1203. }
  1204. // Sort the nodes in WorkSet in top-down order and save the nodes
  1205. // in Orders. Orders will be used for hoisting in runHoistSpills.
  1206. unsigned idx = 0;
  1207. Orders.push_back(MDT.getBase().getNode(Root));
  1208. do {
  1209. MachineDomTreeNode *Node = Orders[idx++];
  1210. for (MachineDomTreeNode *Child : Node->children()) {
  1211. if (WorkSet.count(Child))
  1212. Orders.push_back(Child);
  1213. }
  1214. } while (idx != Orders.size());
  1215. assert(Orders.size() == WorkSet.size() &&
  1216. "Orders have different size with WorkSet");
  1217. #ifndef NDEBUG
  1218. LLVM_DEBUG(dbgs() << "Orders size is " << Orders.size() << "\n");
  1219. SmallVector<MachineDomTreeNode *, 32>::reverse_iterator RIt = Orders.rbegin();
  1220. for (; RIt != Orders.rend(); RIt++)
  1221. LLVM_DEBUG(dbgs() << "BB" << (*RIt)->getBlock()->getNumber() << ",");
  1222. LLVM_DEBUG(dbgs() << "\n");
  1223. #endif
  1224. }
  1225. /// Try to hoist spills according to BB hotness. The spills to removed will
  1226. /// be saved in \p SpillsToRm. The spills to be inserted will be saved in
  1227. /// \p SpillsToIns.
  1228. void HoistSpillHelper::runHoistSpills(
  1229. LiveInterval &OrigLI, VNInfo &OrigVNI,
  1230. SmallPtrSet<MachineInstr *, 16> &Spills,
  1231. SmallVectorImpl<MachineInstr *> &SpillsToRm,
  1232. DenseMap<MachineBasicBlock *, unsigned> &SpillsToIns) {
  1233. // Visit order of dominator tree nodes.
  1234. SmallVector<MachineDomTreeNode *, 32> Orders;
  1235. // SpillsToKeep contains all the nodes where spills are to be inserted
  1236. // during hoisting. If the spill to be inserted is an original spill
  1237. // (not a hoisted one), the value of the map entry is 0. If the spill
  1238. // is a hoisted spill, the value of the map entry is the VReg to be used
  1239. // as the source of the spill.
  1240. DenseMap<MachineDomTreeNode *, unsigned> SpillsToKeep;
  1241. // Map from BB to the first spill inside of it.
  1242. DenseMap<MachineDomTreeNode *, MachineInstr *> SpillBBToSpill;
  1243. rmRedundantSpills(Spills, SpillsToRm, SpillBBToSpill);
  1244. MachineBasicBlock *Root = LIS.getMBBFromIndex(OrigVNI.def);
  1245. getVisitOrders(Root, Spills, Orders, SpillsToRm, SpillsToKeep,
  1246. SpillBBToSpill);
  1247. // SpillsInSubTreeMap keeps the map from a dom tree node to a pair of
  1248. // nodes set and the cost of all the spills inside those nodes.
  1249. // The nodes set are the locations where spills are to be inserted
  1250. // in the subtree of current node.
  1251. using NodesCostPair =
  1252. std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency>;
  1253. DenseMap<MachineDomTreeNode *, NodesCostPair> SpillsInSubTreeMap;
  1254. // Iterate Orders set in reverse order, which will be a bottom-up order
  1255. // in the dominator tree. Once we visit a dom tree node, we know its
  1256. // children have already been visited and the spill locations in the
  1257. // subtrees of all the children have been determined.
  1258. SmallVector<MachineDomTreeNode *, 32>::reverse_iterator RIt = Orders.rbegin();
  1259. for (; RIt != Orders.rend(); RIt++) {
  1260. MachineBasicBlock *Block = (*RIt)->getBlock();
  1261. // If Block contains an original spill, simply continue.
  1262. if (SpillsToKeep.find(*RIt) != SpillsToKeep.end() && !SpillsToKeep[*RIt]) {
  1263. SpillsInSubTreeMap[*RIt].first.insert(*RIt);
  1264. // SpillsInSubTreeMap[*RIt].second contains the cost of spill.
  1265. SpillsInSubTreeMap[*RIt].second = MBFI.getBlockFreq(Block);
  1266. continue;
  1267. }
  1268. // Collect spills in subtree of current node (*RIt) to
  1269. // SpillsInSubTreeMap[*RIt].first.
  1270. for (MachineDomTreeNode *Child : (*RIt)->children()) {
  1271. if (SpillsInSubTreeMap.find(Child) == SpillsInSubTreeMap.end())
  1272. continue;
  1273. // The stmt "SpillsInSubTree = SpillsInSubTreeMap[*RIt].first" below
  1274. // should be placed before getting the begin and end iterators of
  1275. // SpillsInSubTreeMap[Child].first, or else the iterators may be
  1276. // invalidated when SpillsInSubTreeMap[*RIt] is seen the first time
  1277. // and the map grows and then the original buckets in the map are moved.
  1278. SmallPtrSet<MachineDomTreeNode *, 16> &SpillsInSubTree =
  1279. SpillsInSubTreeMap[*RIt].first;
  1280. BlockFrequency &SubTreeCost = SpillsInSubTreeMap[*RIt].second;
  1281. SubTreeCost += SpillsInSubTreeMap[Child].second;
  1282. auto BI = SpillsInSubTreeMap[Child].first.begin();
  1283. auto EI = SpillsInSubTreeMap[Child].first.end();
  1284. SpillsInSubTree.insert(BI, EI);
  1285. SpillsInSubTreeMap.erase(Child);
  1286. }
  1287. SmallPtrSet<MachineDomTreeNode *, 16> &SpillsInSubTree =
  1288. SpillsInSubTreeMap[*RIt].first;
  1289. BlockFrequency &SubTreeCost = SpillsInSubTreeMap[*RIt].second;
  1290. // No spills in subtree, simply continue.
  1291. if (SpillsInSubTree.empty())
  1292. continue;
  1293. // Check whether Block is a possible candidate to insert spill.
  1294. Register LiveReg;
  1295. if (!isSpillCandBB(OrigLI, OrigVNI, *Block, LiveReg))
  1296. continue;
  1297. // If there are multiple spills that could be merged, bias a little
  1298. // to hoist the spill.
  1299. BranchProbability MarginProb = (SpillsInSubTree.size() > 1)
  1300. ? BranchProbability(9, 10)
  1301. : BranchProbability(1, 1);
  1302. if (SubTreeCost > MBFI.getBlockFreq(Block) * MarginProb) {
  1303. // Hoist: Move spills to current Block.
  1304. for (const auto SpillBB : SpillsInSubTree) {
  1305. // When SpillBB is a BB contains original spill, insert the spill
  1306. // to SpillsToRm.
  1307. if (SpillsToKeep.find(SpillBB) != SpillsToKeep.end() &&
  1308. !SpillsToKeep[SpillBB]) {
  1309. MachineInstr *SpillToRm = SpillBBToSpill[SpillBB];
  1310. SpillsToRm.push_back(SpillToRm);
  1311. }
  1312. // SpillBB will not contain spill anymore, remove it from SpillsToKeep.
  1313. SpillsToKeep.erase(SpillBB);
  1314. }
  1315. // Current Block is the BB containing the new hoisted spill. Add it to
  1316. // SpillsToKeep. LiveReg is the source of the new spill.
  1317. SpillsToKeep[*RIt] = LiveReg;
  1318. LLVM_DEBUG({
  1319. dbgs() << "spills in BB: ";
  1320. for (const auto Rspill : SpillsInSubTree)
  1321. dbgs() << Rspill->getBlock()->getNumber() << " ";
  1322. dbgs() << "were promoted to BB" << (*RIt)->getBlock()->getNumber()
  1323. << "\n";
  1324. });
  1325. SpillsInSubTree.clear();
  1326. SpillsInSubTree.insert(*RIt);
  1327. SubTreeCost = MBFI.getBlockFreq(Block);
  1328. }
  1329. }
  1330. // For spills in SpillsToKeep with LiveReg set (i.e., not original spill),
  1331. // save them to SpillsToIns.
  1332. for (const auto &Ent : SpillsToKeep) {
  1333. if (Ent.second)
  1334. SpillsToIns[Ent.first->getBlock()] = Ent.second;
  1335. }
  1336. }
  1337. /// For spills with equal values, remove redundant spills and hoist those left
  1338. /// to less hot spots.
  1339. ///
  1340. /// Spills with equal values will be collected into the same set in
  1341. /// MergeableSpills when spill is inserted. These equal spills are originated
  1342. /// from the same defining instruction and are dominated by the instruction.
  1343. /// Before hoisting all the equal spills, redundant spills inside in the same
  1344. /// BB are first marked to be deleted. Then starting from the spills left, walk
  1345. /// up on the dominator tree towards the Root node where the define instruction
  1346. /// is located, mark the dominated spills to be deleted along the way and
  1347. /// collect the BB nodes on the path from non-dominated spills to the define
  1348. /// instruction into a WorkSet. The nodes in WorkSet are the candidate places
  1349. /// where we are considering to hoist the spills. We iterate the WorkSet in
  1350. /// bottom-up order, and for each node, we will decide whether to hoist spills
  1351. /// inside its subtree to that node. In this way, we can get benefit locally
  1352. /// even if hoisting all the equal spills to one cold place is impossible.
  1353. void HoistSpillHelper::hoistAllSpills() {
  1354. SmallVector<Register, 4> NewVRegs;
  1355. LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this);
  1356. for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
  1357. Register Reg = Register::index2VirtReg(i);
  1358. Register Original = VRM.getPreSplitReg(Reg);
  1359. if (!MRI.def_empty(Reg))
  1360. Virt2SiblingsMap[Original].insert(Reg);
  1361. }
  1362. // Each entry in MergeableSpills contains a spill set with equal values.
  1363. for (auto &Ent : MergeableSpills) {
  1364. int Slot = Ent.first.first;
  1365. LiveInterval &OrigLI = *StackSlotToOrigLI[Slot];
  1366. VNInfo *OrigVNI = Ent.first.second;
  1367. SmallPtrSet<MachineInstr *, 16> &EqValSpills = Ent.second;
  1368. if (Ent.second.empty())
  1369. continue;
  1370. LLVM_DEBUG({
  1371. dbgs() << "\nFor Slot" << Slot << " and VN" << OrigVNI->id << ":\n"
  1372. << "Equal spills in BB: ";
  1373. for (const auto spill : EqValSpills)
  1374. dbgs() << spill->getParent()->getNumber() << " ";
  1375. dbgs() << "\n";
  1376. });
  1377. // SpillsToRm is the spill set to be removed from EqValSpills.
  1378. SmallVector<MachineInstr *, 16> SpillsToRm;
  1379. // SpillsToIns is the spill set to be newly inserted after hoisting.
  1380. DenseMap<MachineBasicBlock *, unsigned> SpillsToIns;
  1381. runHoistSpills(OrigLI, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns);
  1382. LLVM_DEBUG({
  1383. dbgs() << "Finally inserted spills in BB: ";
  1384. for (const auto &Ispill : SpillsToIns)
  1385. dbgs() << Ispill.first->getNumber() << " ";
  1386. dbgs() << "\nFinally removed spills in BB: ";
  1387. for (const auto Rspill : SpillsToRm)
  1388. dbgs() << Rspill->getParent()->getNumber() << " ";
  1389. dbgs() << "\n";
  1390. });
  1391. // Stack live range update.
  1392. LiveInterval &StackIntvl = LSS.getInterval(Slot);
  1393. if (!SpillsToIns.empty() || !SpillsToRm.empty())
  1394. StackIntvl.MergeValueInAsValue(OrigLI, OrigVNI,
  1395. StackIntvl.getValNumInfo(0));
  1396. // Insert hoisted spills.
  1397. for (auto const &Insert : SpillsToIns) {
  1398. MachineBasicBlock *BB = Insert.first;
  1399. Register LiveReg = Insert.second;
  1400. MachineBasicBlock::iterator MII = IPA.getLastInsertPointIter(OrigLI, *BB);
  1401. MachineInstrSpan MIS(MII, BB);
  1402. TII.storeRegToStackSlot(*BB, MII, LiveReg, false, Slot,
  1403. MRI.getRegClass(LiveReg), &TRI);
  1404. LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MII);
  1405. for (const MachineInstr &MI : make_range(MIS.begin(), MII))
  1406. getVDefInterval(MI, LIS);
  1407. ++NumSpills;
  1408. }
  1409. // Remove redundant spills or change them to dead instructions.
  1410. NumSpills -= SpillsToRm.size();
  1411. for (auto const RMEnt : SpillsToRm) {
  1412. RMEnt->setDesc(TII.get(TargetOpcode::KILL));
  1413. for (unsigned i = RMEnt->getNumOperands(); i; --i) {
  1414. MachineOperand &MO = RMEnt->getOperand(i - 1);
  1415. if (MO.isReg() && MO.isImplicit() && MO.isDef() && !MO.isDead())
  1416. RMEnt->RemoveOperand(i - 1);
  1417. }
  1418. }
  1419. Edit.eliminateDeadDefs(SpillsToRm, None, AA);
  1420. }
  1421. }
  1422. /// For VirtReg clone, the \p New register should have the same physreg or
  1423. /// stackslot as the \p old register.
  1424. void HoistSpillHelper::LRE_DidCloneVirtReg(Register New, Register Old) {
  1425. if (VRM.hasPhys(Old))
  1426. VRM.assignVirt2Phys(New, VRM.getPhys(Old));
  1427. else if (VRM.getStackSlot(Old) != VirtRegMap::NO_STACK_SLOT)
  1428. VRM.assignVirt2StackSlot(New, VRM.getStackSlot(Old));
  1429. else
  1430. llvm_unreachable("VReg should be assigned either physreg or stackslot");
  1431. if (VRM.hasShape(Old))
  1432. VRM.assignVirt2Shape(New, VRM.getShape(Old));
  1433. }