InlineSpiller.cpp 62 KB

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