InlineSpiller.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. //===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // The inline spiller modifies the machine function directly instead of
  11. // inserting spills and restores in VirtRegMap.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "Spiller.h"
  15. #include "llvm/ADT/SetVector.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/ADT/TinyPtrVector.h"
  18. #include "llvm/Analysis/AliasAnalysis.h"
  19. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  20. #include "llvm/CodeGen/LiveRangeEdit.h"
  21. #include "llvm/CodeGen/LiveStackAnalysis.h"
  22. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  23. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  24. #include "llvm/CodeGen/MachineDominators.h"
  25. #include "llvm/CodeGen/MachineFrameInfo.h"
  26. #include "llvm/CodeGen/MachineFunction.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineInstrBundle.h"
  29. #include "llvm/CodeGen/MachineLoopInfo.h"
  30. #include "llvm/CodeGen/MachineRegisterInfo.h"
  31. #include "llvm/CodeGen/VirtRegMap.h"
  32. #include "llvm/Support/CommandLine.h"
  33. #include "llvm/Support/Debug.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include "llvm/Target/TargetInstrInfo.h"
  36. using namespace llvm;
  37. #define DEBUG_TYPE "regalloc"
  38. STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
  39. STATISTIC(NumSnippets, "Number of spilled snippets");
  40. STATISTIC(NumSpills, "Number of spills inserted");
  41. STATISTIC(NumSpillsRemoved, "Number of spills removed");
  42. STATISTIC(NumReloads, "Number of reloads inserted");
  43. STATISTIC(NumReloadsRemoved, "Number of reloads removed");
  44. STATISTIC(NumFolded, "Number of folded stack accesses");
  45. STATISTIC(NumFoldedLoads, "Number of folded loads");
  46. STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
  47. STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads");
  48. STATISTIC(NumHoists, "Number of hoisted spills");
  49. static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
  50. cl::desc("Disable inline spill hoisting"));
  51. namespace {
  52. class InlineSpiller : public Spiller {
  53. MachineFunction &MF;
  54. LiveIntervals &LIS;
  55. LiveStacks &LSS;
  56. AliasAnalysis *AA;
  57. MachineDominatorTree &MDT;
  58. MachineLoopInfo &Loops;
  59. VirtRegMap &VRM;
  60. MachineFrameInfo &MFI;
  61. MachineRegisterInfo &MRI;
  62. const TargetInstrInfo &TII;
  63. const TargetRegisterInfo &TRI;
  64. const MachineBlockFrequencyInfo &MBFI;
  65. // Variables that are valid during spill(), but used by multiple methods.
  66. LiveRangeEdit *Edit;
  67. LiveInterval *StackInt;
  68. int StackSlot;
  69. unsigned Original;
  70. // All registers to spill to StackSlot, including the main register.
  71. SmallVector<unsigned, 8> RegsToSpill;
  72. // All COPY instructions to/from snippets.
  73. // They are ignored since both operands refer to the same stack slot.
  74. SmallPtrSet<MachineInstr*, 8> SnippetCopies;
  75. // Values that failed to remat at some point.
  76. SmallPtrSet<VNInfo*, 8> UsedValues;
  77. public:
  78. // Information about a value that was defined by a copy from a sibling
  79. // register.
  80. struct SibValueInfo {
  81. // True when all reaching defs were reloads: No spill is necessary.
  82. bool AllDefsAreReloads;
  83. // True when value is defined by an original PHI not from splitting.
  84. bool DefByOrigPHI;
  85. // True when the COPY defining this value killed its source.
  86. bool KillsSource;
  87. // The preferred register to spill.
  88. unsigned SpillReg;
  89. // The value of SpillReg that should be spilled.
  90. VNInfo *SpillVNI;
  91. // The block where SpillVNI should be spilled. Currently, this must be the
  92. // block containing SpillVNI->def.
  93. MachineBasicBlock *SpillMBB;
  94. // A defining instruction that is not a sibling copy or a reload, or NULL.
  95. // This can be used as a template for rematerialization.
  96. MachineInstr *DefMI;
  97. // List of values that depend on this one. These values are actually the
  98. // same, but live range splitting has placed them in different registers,
  99. // or SSA update needed to insert PHI-defs to preserve SSA form. This is
  100. // copies of the current value and phi-kills. Usually only phi-kills cause
  101. // more than one dependent value.
  102. TinyPtrVector<VNInfo*> Deps;
  103. SibValueInfo(unsigned Reg, VNInfo *VNI)
  104. : AllDefsAreReloads(true), DefByOrigPHI(false), KillsSource(false),
  105. SpillReg(Reg), SpillVNI(VNI), SpillMBB(nullptr), DefMI(nullptr) {}
  106. // Returns true when a def has been found.
  107. bool hasDef() const { return DefByOrigPHI || DefMI; }
  108. };
  109. private:
  110. // Values in RegsToSpill defined by sibling copies.
  111. typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap;
  112. SibValueMap SibValues;
  113. // Dead defs generated during spilling.
  114. SmallVector<MachineInstr*, 8> DeadDefs;
  115. ~InlineSpiller() override {}
  116. public:
  117. InlineSpiller(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
  118. : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
  119. LSS(pass.getAnalysis<LiveStacks>()),
  120. AA(&pass.getAnalysis<AliasAnalysis>()),
  121. MDT(pass.getAnalysis<MachineDominatorTree>()),
  122. Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
  123. MFI(*mf.getFrameInfo()), MRI(mf.getRegInfo()),
  124. TII(*mf.getSubtarget().getInstrInfo()),
  125. TRI(*mf.getSubtarget().getRegisterInfo()),
  126. MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()) {}
  127. void spill(LiveRangeEdit &) override;
  128. private:
  129. bool isSnippet(const LiveInterval &SnipLI);
  130. void collectRegsToSpill();
  131. bool isRegToSpill(unsigned Reg) {
  132. return std::find(RegsToSpill.begin(),
  133. RegsToSpill.end(), Reg) != RegsToSpill.end();
  134. }
  135. bool isSibling(unsigned Reg);
  136. MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
  137. void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = nullptr);
  138. void analyzeSiblingValues();
  139. bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
  140. void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
  141. void markValueUsed(LiveInterval*, VNInfo*);
  142. bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
  143. void reMaterializeAll();
  144. bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
  145. bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >,
  146. MachineInstr *LoadMI = nullptr);
  147. void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI);
  148. void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI);
  149. void spillAroundUses(unsigned Reg);
  150. void spillAll();
  151. };
  152. }
  153. namespace llvm {
  154. Spiller::~Spiller() { }
  155. void Spiller::anchor() { }
  156. Spiller *createInlineSpiller(MachineFunctionPass &pass,
  157. MachineFunction &mf,
  158. VirtRegMap &vrm) {
  159. return new InlineSpiller(pass, mf, vrm);
  160. }
  161. }
  162. //===----------------------------------------------------------------------===//
  163. // Snippets
  164. //===----------------------------------------------------------------------===//
  165. // When spilling a virtual register, we also spill any snippets it is connected
  166. // to. The snippets are small live ranges that only have a single real use,
  167. // leftovers from live range splitting. Spilling them enables memory operand
  168. // folding or tightens the live range around the single use.
  169. //
  170. // This minimizes register pressure and maximizes the store-to-load distance for
  171. // spill slots which can be important in tight loops.
  172. /// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
  173. /// otherwise return 0.
  174. static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
  175. if (!MI->isFullCopy())
  176. return 0;
  177. if (MI->getOperand(0).getReg() == Reg)
  178. return MI->getOperand(1).getReg();
  179. if (MI->getOperand(1).getReg() == Reg)
  180. return MI->getOperand(0).getReg();
  181. return 0;
  182. }
  183. /// isSnippet - Identify if a live interval is a snippet that should be spilled.
  184. /// It is assumed that SnipLI is a virtual register with the same original as
  185. /// Edit->getReg().
  186. bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
  187. unsigned Reg = Edit->getReg();
  188. // A snippet is a tiny live range with only a single instruction using it
  189. // besides copies to/from Reg or spills/fills. We accept:
  190. //
  191. // %snip = COPY %Reg / FILL fi#
  192. // %snip = USE %snip
  193. // %Reg = COPY %snip / SPILL %snip, fi#
  194. //
  195. if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
  196. return false;
  197. MachineInstr *UseMI = nullptr;
  198. // Check that all uses satisfy our criteria.
  199. for (MachineRegisterInfo::reg_instr_nodbg_iterator
  200. RI = MRI.reg_instr_nodbg_begin(SnipLI.reg),
  201. E = MRI.reg_instr_nodbg_end(); RI != E; ) {
  202. MachineInstr *MI = &*(RI++);
  203. // Allow copies to/from Reg.
  204. if (isFullCopyOf(MI, Reg))
  205. continue;
  206. // Allow stack slot loads.
  207. int FI;
  208. if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
  209. continue;
  210. // Allow stack slot stores.
  211. if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
  212. continue;
  213. // Allow a single additional instruction.
  214. if (UseMI && MI != UseMI)
  215. return false;
  216. UseMI = MI;
  217. }
  218. return true;
  219. }
  220. /// collectRegsToSpill - Collect live range snippets that only have a single
  221. /// real use.
  222. void InlineSpiller::collectRegsToSpill() {
  223. unsigned Reg = Edit->getReg();
  224. // Main register always spills.
  225. RegsToSpill.assign(1, Reg);
  226. SnippetCopies.clear();
  227. // Snippets all have the same original, so there can't be any for an original
  228. // register.
  229. if (Original == Reg)
  230. return;
  231. for (MachineRegisterInfo::reg_instr_iterator
  232. RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end(); RI != E; ) {
  233. MachineInstr *MI = &*(RI++);
  234. unsigned SnipReg = isFullCopyOf(MI, Reg);
  235. if (!isSibling(SnipReg))
  236. continue;
  237. LiveInterval &SnipLI = LIS.getInterval(SnipReg);
  238. if (!isSnippet(SnipLI))
  239. continue;
  240. SnippetCopies.insert(MI);
  241. if (isRegToSpill(SnipReg))
  242. continue;
  243. RegsToSpill.push_back(SnipReg);
  244. DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
  245. ++NumSnippets;
  246. }
  247. }
  248. //===----------------------------------------------------------------------===//
  249. // Sibling Values
  250. //===----------------------------------------------------------------------===//
  251. // After live range splitting, some values to be spilled may be defined by
  252. // copies from sibling registers. We trace the sibling copies back to the
  253. // original value if it still exists. We need it for rematerialization.
  254. //
  255. // Even when the value can't be rematerialized, we still want to determine if
  256. // the value has already been spilled, or we may want to hoist the spill from a
  257. // loop.
  258. bool InlineSpiller::isSibling(unsigned Reg) {
  259. return TargetRegisterInfo::isVirtualRegister(Reg) &&
  260. VRM.getOriginal(Reg) == Original;
  261. }
  262. #ifndef NDEBUG
  263. static raw_ostream &operator<<(raw_ostream &OS,
  264. const InlineSpiller::SibValueInfo &SVI) {
  265. OS << "spill " << PrintReg(SVI.SpillReg) << ':'
  266. << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def;
  267. if (SVI.SpillMBB)
  268. OS << " in BB#" << SVI.SpillMBB->getNumber();
  269. if (SVI.AllDefsAreReloads)
  270. OS << " all-reloads";
  271. if (SVI.DefByOrigPHI)
  272. OS << " orig-phi";
  273. if (SVI.KillsSource)
  274. OS << " kill";
  275. OS << " deps[";
  276. for (unsigned i = 0, e = SVI.Deps.size(); i != e; ++i)
  277. OS << ' ' << SVI.Deps[i]->id << '@' << SVI.Deps[i]->def;
  278. OS << " ]";
  279. if (SVI.DefMI)
  280. OS << " def: " << *SVI.DefMI;
  281. else
  282. OS << '\n';
  283. return OS;
  284. }
  285. #endif
  286. /// propagateSiblingValue - Propagate the value in SVI to dependents if it is
  287. /// known. Otherwise remember the dependency for later.
  288. ///
  289. /// @param SVIIter SibValues entry to propagate.
  290. /// @param VNI Dependent value, or NULL to propagate to all saved dependents.
  291. void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVIIter,
  292. VNInfo *VNI) {
  293. SibValueMap::value_type *SVI = &*SVIIter;
  294. // When VNI is non-NULL, add it to SVI's deps, and only propagate to that.
  295. TinyPtrVector<VNInfo*> FirstDeps;
  296. if (VNI) {
  297. FirstDeps.push_back(VNI);
  298. SVI->second.Deps.push_back(VNI);
  299. }
  300. // Has the value been completely determined yet? If not, defer propagation.
  301. if (!SVI->second.hasDef())
  302. return;
  303. // Work list of values to propagate.
  304. SmallSetVector<SibValueMap::value_type *, 8> WorkList;
  305. WorkList.insert(SVI);
  306. do {
  307. SVI = WorkList.pop_back_val();
  308. TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps;
  309. VNI = nullptr;
  310. SibValueInfo &SV = SVI->second;
  311. if (!SV.SpillMBB)
  312. SV.SpillMBB = LIS.getMBBFromIndex(SV.SpillVNI->def);
  313. DEBUG(dbgs() << " prop to " << Deps->size() << ": "
  314. << SVI->first->id << '@' << SVI->first->def << ":\t" << SV);
  315. assert(SV.hasDef() && "Propagating undefined value");
  316. // Should this value be propagated as a preferred spill candidate? We don't
  317. // propagate values of registers that are about to spill.
  318. bool PropSpill = !DisableHoisting && !isRegToSpill(SV.SpillReg);
  319. unsigned SpillDepth = ~0u;
  320. for (TinyPtrVector<VNInfo*>::iterator DepI = Deps->begin(),
  321. DepE = Deps->end(); DepI != DepE; ++DepI) {
  322. SibValueMap::iterator DepSVI = SibValues.find(*DepI);
  323. assert(DepSVI != SibValues.end() && "Dependent value not in SibValues");
  324. SibValueInfo &DepSV = DepSVI->second;
  325. if (!DepSV.SpillMBB)
  326. DepSV.SpillMBB = LIS.getMBBFromIndex(DepSV.SpillVNI->def);
  327. bool Changed = false;
  328. // Propagate defining instruction.
  329. if (!DepSV.hasDef()) {
  330. Changed = true;
  331. DepSV.DefMI = SV.DefMI;
  332. DepSV.DefByOrigPHI = SV.DefByOrigPHI;
  333. }
  334. // Propagate AllDefsAreReloads. For PHI values, this computes an AND of
  335. // all predecessors.
  336. if (!SV.AllDefsAreReloads && DepSV.AllDefsAreReloads) {
  337. Changed = true;
  338. DepSV.AllDefsAreReloads = false;
  339. }
  340. // Propagate best spill value.
  341. if (PropSpill && SV.SpillVNI != DepSV.SpillVNI) {
  342. if (SV.SpillMBB == DepSV.SpillMBB) {
  343. // DepSV is in the same block. Hoist when dominated.
  344. if (DepSV.KillsSource && SV.SpillVNI->def < DepSV.SpillVNI->def) {
  345. // This is an alternative def earlier in the same MBB.
  346. // Hoist the spill as far as possible in SpillMBB. This can ease
  347. // register pressure:
  348. //
  349. // x = def
  350. // y = use x
  351. // s = copy x
  352. //
  353. // Hoisting the spill of s to immediately after the def removes the
  354. // interference between x and y:
  355. //
  356. // x = def
  357. // spill x
  358. // y = use x<kill>
  359. //
  360. // This hoist only helps when the DepSV copy kills its source.
  361. Changed = true;
  362. DepSV.SpillReg = SV.SpillReg;
  363. DepSV.SpillVNI = SV.SpillVNI;
  364. DepSV.SpillMBB = SV.SpillMBB;
  365. }
  366. } else {
  367. // DepSV is in a different block.
  368. if (SpillDepth == ~0u)
  369. SpillDepth = Loops.getLoopDepth(SV.SpillMBB);
  370. // Also hoist spills to blocks with smaller loop depth, but make sure
  371. // that the new value dominates. Non-phi dependents are always
  372. // dominated, phis need checking.
  373. const BranchProbability MarginProb(4, 5); // 80%
  374. // Hoist a spill to outer loop if there are multiple dependents (it
  375. // can be beneficial if more than one dependents are hoisted) or
  376. // if DepSV (the hoisting source) is hotter than SV (the hoisting
  377. // destination) (we add a 80% margin to bias a little towards
  378. // loop depth).
  379. bool HoistCondition =
  380. (MBFI.getBlockFreq(DepSV.SpillMBB) >=
  381. (MBFI.getBlockFreq(SV.SpillMBB) * MarginProb)) ||
  382. Deps->size() > 1;
  383. if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) &&
  384. HoistCondition &&
  385. (!DepSVI->first->isPHIDef() ||
  386. MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) {
  387. Changed = true;
  388. DepSV.SpillReg = SV.SpillReg;
  389. DepSV.SpillVNI = SV.SpillVNI;
  390. DepSV.SpillMBB = SV.SpillMBB;
  391. }
  392. }
  393. }
  394. if (!Changed)
  395. continue;
  396. // Something changed in DepSVI. Propagate to dependents.
  397. WorkList.insert(&*DepSVI);
  398. DEBUG(dbgs() << " update " << DepSVI->first->id << '@'
  399. << DepSVI->first->def << " to:\t" << DepSV);
  400. }
  401. } while (!WorkList.empty());
  402. }
  403. /// traceSiblingValue - Trace a value that is about to be spilled back to the
  404. /// real defining instructions by looking through sibling copies. Always stay
  405. /// within the range of OrigVNI so the registers are known to carry the same
  406. /// value.
  407. ///
  408. /// Determine if the value is defined by all reloads, so spilling isn't
  409. /// necessary - the value is already in the stack slot.
  410. ///
  411. /// Return a defining instruction that may be a candidate for rematerialization.
  412. ///
  413. MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
  414. VNInfo *OrigVNI) {
  415. // Check if a cached value already exists.
  416. SibValueMap::iterator SVI;
  417. bool Inserted;
  418. std::tie(SVI, Inserted) =
  419. SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI)));
  420. if (!Inserted) {
  421. DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':'
  422. << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second);
  423. return SVI->second.DefMI;
  424. }
  425. DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
  426. << UseVNI->id << '@' << UseVNI->def << '\n');
  427. // List of (Reg, VNI) that have been inserted into SibValues, but need to be
  428. // processed.
  429. SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
  430. WorkList.push_back(std::make_pair(UseReg, UseVNI));
  431. LiveInterval &OrigLI = LIS.getInterval(Original);
  432. do {
  433. unsigned Reg;
  434. VNInfo *VNI;
  435. std::tie(Reg, VNI) = WorkList.pop_back_val();
  436. DEBUG(dbgs() << " " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def
  437. << ":\t");
  438. // First check if this value has already been computed.
  439. SVI = SibValues.find(VNI);
  440. assert(SVI != SibValues.end() && "Missing SibValues entry");
  441. // Trace through PHI-defs created by live range splitting.
  442. if (VNI->isPHIDef()) {
  443. // Stop at original PHIs. We don't know the value at the
  444. // predecessors. Look up the VNInfo for the current definition
  445. // in OrigLI, to properly determine whether or not this phi was
  446. // added by splitting.
  447. if (VNI->def == OrigLI.getVNInfoAt(VNI->def)->def) {
  448. DEBUG(dbgs() << "orig phi value\n");
  449. SVI->second.DefByOrigPHI = true;
  450. SVI->second.AllDefsAreReloads = false;
  451. propagateSiblingValue(SVI);
  452. continue;
  453. }
  454. // This is a PHI inserted by live range splitting. We could trace the
  455. // live-out value from predecessor blocks, but that search can be very
  456. // expensive if there are many predecessors and many more PHIs as
  457. // generated by tail-dup when it sees an indirectbr. Instead, look at
  458. // all the non-PHI defs that have the same value as OrigVNI. They must
  459. // jointly dominate VNI->def. This is not optimal since VNI may actually
  460. // be jointly dominated by a smaller subset of defs, so there is a change
  461. // we will miss a AllDefsAreReloads optimization.
  462. // Separate all values dominated by OrigVNI into PHIs and non-PHIs.
  463. SmallVector<VNInfo*, 8> PHIs, NonPHIs;
  464. LiveInterval &LI = LIS.getInterval(Reg);
  465. for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end();
  466. VI != VE; ++VI) {
  467. VNInfo *VNI2 = *VI;
  468. if (VNI2->isUnused())
  469. continue;
  470. if (!OrigLI.containsOneValue() &&
  471. OrigLI.getVNInfoAt(VNI2->def) != OrigVNI)
  472. continue;
  473. if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def)
  474. PHIs.push_back(VNI2);
  475. else
  476. NonPHIs.push_back(VNI2);
  477. }
  478. DEBUG(dbgs() << "split phi value, checking " << PHIs.size()
  479. << " phi-defs, and " << NonPHIs.size()
  480. << " non-phi/orig defs\n");
  481. // Create entries for all the PHIs. Don't add them to the worklist, we
  482. // are processing all of them in one go here.
  483. for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
  484. SibValues.insert(std::make_pair(PHIs[i], SibValueInfo(Reg, PHIs[i])));
  485. // Add every PHI as a dependent of all the non-PHIs.
  486. for (unsigned i = 0, e = NonPHIs.size(); i != e; ++i) {
  487. VNInfo *NonPHI = NonPHIs[i];
  488. // Known value? Try an insertion.
  489. std::tie(SVI, Inserted) =
  490. SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI)));
  491. // Add all the PHIs as dependents of NonPHI.
  492. SVI->second.Deps.insert(SVI->second.Deps.end(), PHIs.begin(),
  493. PHIs.end());
  494. // This is the first time we see NonPHI, add it to the worklist.
  495. if (Inserted)
  496. WorkList.push_back(std::make_pair(Reg, NonPHI));
  497. else
  498. // Propagate to all inserted PHIs, not just VNI.
  499. propagateSiblingValue(SVI);
  500. }
  501. // Next work list item.
  502. continue;
  503. }
  504. MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
  505. assert(MI && "Missing def");
  506. // Trace through sibling copies.
  507. if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
  508. if (isSibling(SrcReg)) {
  509. LiveInterval &SrcLI = LIS.getInterval(SrcReg);
  510. LiveQueryResult SrcQ = SrcLI.Query(VNI->def);
  511. assert(SrcQ.valueIn() && "Copy from non-existing value");
  512. // Check if this COPY kills its source.
  513. SVI->second.KillsSource = SrcQ.isKill();
  514. VNInfo *SrcVNI = SrcQ.valueIn();
  515. DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':'
  516. << SrcVNI->id << '@' << SrcVNI->def
  517. << " kill=" << unsigned(SVI->second.KillsSource) << '\n');
  518. // Known sibling source value? Try an insertion.
  519. std::tie(SVI, Inserted) = SibValues.insert(
  520. std::make_pair(SrcVNI, SibValueInfo(SrcReg, SrcVNI)));
  521. // This is the first time we see Src, add it to the worklist.
  522. if (Inserted)
  523. WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
  524. propagateSiblingValue(SVI, VNI);
  525. // Next work list item.
  526. continue;
  527. }
  528. }
  529. // Track reachable reloads.
  530. SVI->second.DefMI = MI;
  531. SVI->second.SpillMBB = MI->getParent();
  532. int FI;
  533. if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
  534. DEBUG(dbgs() << "reload\n");
  535. propagateSiblingValue(SVI);
  536. // Next work list item.
  537. continue;
  538. }
  539. // Potential remat candidate.
  540. DEBUG(dbgs() << "def " << *MI);
  541. SVI->second.AllDefsAreReloads = false;
  542. propagateSiblingValue(SVI);
  543. } while (!WorkList.empty());
  544. // Look up the value we were looking for. We already did this lookup at the
  545. // top of the function, but SibValues may have been invalidated.
  546. SVI = SibValues.find(UseVNI);
  547. assert(SVI != SibValues.end() && "Didn't compute requested info");
  548. DEBUG(dbgs() << " traced to:\t" << SVI->second);
  549. return SVI->second.DefMI;
  550. }
  551. /// analyzeSiblingValues - Trace values defined by sibling copies back to
  552. /// something that isn't a sibling copy.
  553. ///
  554. /// Keep track of values that may be rematerializable.
  555. void InlineSpiller::analyzeSiblingValues() {
  556. SibValues.clear();
  557. // No siblings at all?
  558. if (Edit->getReg() == Original)
  559. return;
  560. LiveInterval &OrigLI = LIS.getInterval(Original);
  561. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
  562. unsigned Reg = RegsToSpill[i];
  563. LiveInterval &LI = LIS.getInterval(Reg);
  564. for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
  565. VE = LI.vni_end(); VI != VE; ++VI) {
  566. VNInfo *VNI = *VI;
  567. if (VNI->isUnused())
  568. continue;
  569. MachineInstr *DefMI = nullptr;
  570. if (!VNI->isPHIDef()) {
  571. DefMI = LIS.getInstructionFromIndex(VNI->def);
  572. assert(DefMI && "No defining instruction");
  573. }
  574. // Check possible sibling copies.
  575. if (VNI->isPHIDef() || DefMI->isCopy()) {
  576. VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
  577. assert(OrigVNI && "Def outside original live range");
  578. if (OrigVNI->def != VNI->def)
  579. DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
  580. }
  581. if (DefMI && Edit->checkRematerializable(VNI, DefMI, AA)) {
  582. DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
  583. << VNI->def << " may remat from " << *DefMI);
  584. }
  585. }
  586. }
  587. }
  588. /// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
  589. /// a spill at a better location.
  590. bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
  591. SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
  592. VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
  593. assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
  594. SibValueMap::iterator I = SibValues.find(VNI);
  595. if (I == SibValues.end())
  596. return false;
  597. const SibValueInfo &SVI = I->second;
  598. // Let the normal folding code deal with the boring case.
  599. if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
  600. return false;
  601. // SpillReg may have been deleted by remat and DCE.
  602. if (!LIS.hasInterval(SVI.SpillReg)) {
  603. DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n');
  604. SibValues.erase(I);
  605. return false;
  606. }
  607. LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg);
  608. if (!SibLI.containsValue(SVI.SpillVNI)) {
  609. DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n');
  610. SibValues.erase(I);
  611. return false;
  612. }
  613. // Conservatively extend the stack slot range to the range of the original
  614. // value. We may be able to do better with stack slot coloring by being more
  615. // careful here.
  616. assert(StackInt && "No stack slot assigned yet.");
  617. LiveInterval &OrigLI = LIS.getInterval(Original);
  618. VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
  619. StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
  620. DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
  621. << *StackInt << '\n');
  622. // Already spilled everywhere.
  623. if (SVI.AllDefsAreReloads) {
  624. DEBUG(dbgs() << "\tno spill needed: " << SVI);
  625. ++NumOmitReloadSpill;
  626. return true;
  627. }
  628. // We are going to spill SVI.SpillVNI immediately after its def, so clear out
  629. // any later spills of the same value.
  630. eliminateRedundantSpills(SibLI, SVI.SpillVNI);
  631. MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
  632. MachineBasicBlock::iterator MII;
  633. if (SVI.SpillVNI->isPHIDef())
  634. MII = MBB->SkipPHIsAndLabels(MBB->begin());
  635. else {
  636. MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
  637. assert(DefMI && "Defining instruction disappeared");
  638. MII = DefMI;
  639. ++MII;
  640. }
  641. // Insert spill without kill flag immediately after def.
  642. TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
  643. MRI.getRegClass(SVI.SpillReg), &TRI);
  644. --MII; // Point to store instruction.
  645. LIS.InsertMachineInstrInMaps(MII);
  646. DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
  647. ++NumSpills;
  648. ++NumHoists;
  649. return true;
  650. }
  651. /// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
  652. /// redundant spills of this value in SLI.reg and sibling copies.
  653. void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
  654. assert(VNI && "Missing value");
  655. SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
  656. WorkList.push_back(std::make_pair(&SLI, VNI));
  657. assert(StackInt && "No stack slot assigned yet.");
  658. do {
  659. LiveInterval *LI;
  660. std::tie(LI, VNI) = WorkList.pop_back_val();
  661. unsigned Reg = LI->reg;
  662. DEBUG(dbgs() << "Checking redundant spills for "
  663. << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
  664. // Regs to spill are taken care of.
  665. if (isRegToSpill(Reg))
  666. continue;
  667. // Add all of VNI's live range to StackInt.
  668. StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
  669. DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
  670. // Find all spills and copies of VNI.
  671. for (MachineRegisterInfo::use_instr_nodbg_iterator
  672. UI = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
  673. UI != E; ) {
  674. MachineInstr *MI = &*(UI++);
  675. if (!MI->isCopy() && !MI->mayStore())
  676. continue;
  677. SlotIndex Idx = LIS.getInstructionIndex(MI);
  678. if (LI->getVNInfoAt(Idx) != VNI)
  679. continue;
  680. // Follow sibling copies down the dominator tree.
  681. if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
  682. if (isSibling(DstReg)) {
  683. LiveInterval &DstLI = LIS.getInterval(DstReg);
  684. VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
  685. assert(DstVNI && "Missing defined value");
  686. assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
  687. WorkList.push_back(std::make_pair(&DstLI, DstVNI));
  688. }
  689. continue;
  690. }
  691. // Erase spills.
  692. int FI;
  693. if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
  694. DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
  695. // eliminateDeadDefs won't normally remove stores, so switch opcode.
  696. MI->setDesc(TII.get(TargetOpcode::KILL));
  697. DeadDefs.push_back(MI);
  698. ++NumSpillsRemoved;
  699. --NumSpills;
  700. }
  701. }
  702. } while (!WorkList.empty());
  703. }
  704. //===----------------------------------------------------------------------===//
  705. // Rematerialization
  706. //===----------------------------------------------------------------------===//
  707. /// markValueUsed - Remember that VNI failed to rematerialize, so its defining
  708. /// instruction cannot be eliminated. See through snippet copies
  709. void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
  710. SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
  711. WorkList.push_back(std::make_pair(LI, VNI));
  712. do {
  713. std::tie(LI, VNI) = WorkList.pop_back_val();
  714. if (!UsedValues.insert(VNI).second)
  715. continue;
  716. if (VNI->isPHIDef()) {
  717. MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
  718. for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
  719. PE = MBB->pred_end(); PI != PE; ++PI) {
  720. VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI));
  721. if (PVNI)
  722. WorkList.push_back(std::make_pair(LI, PVNI));
  723. }
  724. continue;
  725. }
  726. // Follow snippet copies.
  727. MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
  728. if (!SnippetCopies.count(MI))
  729. continue;
  730. LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
  731. assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
  732. VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
  733. assert(SnipVNI && "Snippet undefined before copy");
  734. WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
  735. } while (!WorkList.empty());
  736. }
  737. /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
  738. bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
  739. MachineBasicBlock::iterator MI) {
  740. // Analyze instruction
  741. SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops;
  742. MIBundleOperands::VirtRegInfo RI =
  743. MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops);
  744. if (!RI.Reads)
  745. return false;
  746. SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
  747. VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
  748. if (!ParentVNI) {
  749. DEBUG(dbgs() << "\tadding <undef> flags: ");
  750. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  751. MachineOperand &MO = MI->getOperand(i);
  752. if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
  753. MO.setIsUndef();
  754. }
  755. DEBUG(dbgs() << UseIdx << '\t' << *MI);
  756. return true;
  757. }
  758. if (SnippetCopies.count(MI))
  759. return false;
  760. // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
  761. LiveRangeEdit::Remat RM(ParentVNI);
  762. SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
  763. if (SibI != SibValues.end())
  764. RM.OrigMI = SibI->second.DefMI;
  765. if (!Edit->canRematerializeAt(RM, UseIdx, false)) {
  766. markValueUsed(&VirtReg, ParentVNI);
  767. DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
  768. return false;
  769. }
  770. // If the instruction also writes VirtReg.reg, it had better not require the
  771. // same register for uses and defs.
  772. if (RI.Tied) {
  773. markValueUsed(&VirtReg, ParentVNI);
  774. DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
  775. return false;
  776. }
  777. // Before rematerializing into a register for a single instruction, try to
  778. // fold a load into the instruction. That avoids allocating a new register.
  779. if (RM.OrigMI->canFoldAsLoad() &&
  780. foldMemoryOperand(Ops, RM.OrigMI)) {
  781. Edit->markRematerialized(RM.ParentVNI);
  782. ++NumFoldedLoads;
  783. return true;
  784. }
  785. // Alocate a new register for the remat.
  786. unsigned NewVReg = Edit->createFrom(Original);
  787. // Finally we can rematerialize OrigMI before MI.
  788. SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewVReg, RM,
  789. TRI);
  790. (void)DefIdx;
  791. DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
  792. << *LIS.getInstructionFromIndex(DefIdx));
  793. // Replace operands
  794. for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
  795. MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second);
  796. if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
  797. MO.setReg(NewVReg);
  798. MO.setIsKill();
  799. }
  800. }
  801. DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI << '\n');
  802. ++NumRemats;
  803. return true;
  804. }
  805. /// reMaterializeAll - Try to rematerialize as many uses as possible,
  806. /// and trim the live ranges after.
  807. void InlineSpiller::reMaterializeAll() {
  808. // analyzeSiblingValues has already tested all relevant defining instructions.
  809. if (!Edit->anyRematerializable(AA))
  810. return;
  811. UsedValues.clear();
  812. // Try to remat before all uses of snippets.
  813. bool anyRemat = false;
  814. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
  815. unsigned Reg = RegsToSpill[i];
  816. LiveInterval &LI = LIS.getInterval(Reg);
  817. for (MachineRegisterInfo::reg_bundle_iterator
  818. RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
  819. RegI != E; ) {
  820. MachineInstr *MI = &*(RegI++);
  821. // Debug values are not allowed to affect codegen.
  822. if (MI->isDebugValue())
  823. continue;
  824. anyRemat |= reMaterializeFor(LI, MI);
  825. }
  826. }
  827. if (!anyRemat)
  828. return;
  829. // Remove any values that were completely rematted.
  830. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
  831. unsigned Reg = RegsToSpill[i];
  832. LiveInterval &LI = LIS.getInterval(Reg);
  833. for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
  834. I != E; ++I) {
  835. VNInfo *VNI = *I;
  836. if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
  837. continue;
  838. MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
  839. MI->addRegisterDead(Reg, &TRI);
  840. if (!MI->allDefsAreDead())
  841. continue;
  842. DEBUG(dbgs() << "All defs dead: " << *MI);
  843. DeadDefs.push_back(MI);
  844. }
  845. }
  846. // Eliminate dead code after remat. Note that some snippet copies may be
  847. // deleted here.
  848. if (DeadDefs.empty())
  849. return;
  850. DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
  851. Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
  852. // Get rid of deleted and empty intervals.
  853. unsigned ResultPos = 0;
  854. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
  855. unsigned Reg = RegsToSpill[i];
  856. if (!LIS.hasInterval(Reg))
  857. continue;
  858. LiveInterval &LI = LIS.getInterval(Reg);
  859. if (LI.empty()) {
  860. Edit->eraseVirtReg(Reg);
  861. continue;
  862. }
  863. RegsToSpill[ResultPos++] = Reg;
  864. }
  865. RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
  866. DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
  867. }
  868. //===----------------------------------------------------------------------===//
  869. // Spilling
  870. //===----------------------------------------------------------------------===//
  871. /// If MI is a load or store of StackSlot, it can be removed.
  872. bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
  873. int FI = 0;
  874. unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI);
  875. bool IsLoad = InstrReg;
  876. if (!IsLoad)
  877. InstrReg = TII.isStoreToStackSlot(MI, FI);
  878. // We have a stack access. Is it the right register and slot?
  879. if (InstrReg != Reg || FI != StackSlot)
  880. return false;
  881. DEBUG(dbgs() << "Coalescing stack access: " << *MI);
  882. LIS.RemoveMachineInstrFromMaps(MI);
  883. MI->eraseFromParent();
  884. if (IsLoad) {
  885. ++NumReloadsRemoved;
  886. --NumReloads;
  887. } else {
  888. ++NumSpillsRemoved;
  889. --NumSpills;
  890. }
  891. return true;
  892. }
  893. #if !defined(NDEBUG)
  894. // Dump the range of instructions from B to E with their slot indexes.
  895. static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,
  896. MachineBasicBlock::iterator E,
  897. LiveIntervals const &LIS,
  898. const char *const header,
  899. unsigned VReg =0) {
  900. char NextLine = '\n';
  901. char SlotIndent = '\t';
  902. if (std::next(B) == E) {
  903. NextLine = ' ';
  904. SlotIndent = ' ';
  905. }
  906. dbgs() << '\t' << header << ": " << NextLine;
  907. for (MachineBasicBlock::iterator I = B; I != E; ++I) {
  908. SlotIndex Idx = LIS.getInstructionIndex(I).getRegSlot();
  909. // If a register was passed in and this instruction has it as a
  910. // destination that is marked as an early clobber, print the
  911. // early-clobber slot index.
  912. if (VReg) {
  913. MachineOperand *MO = I->findRegisterDefOperand(VReg);
  914. if (MO && MO->isEarlyClobber())
  915. Idx = Idx.getRegSlot(true);
  916. }
  917. dbgs() << SlotIndent << Idx << '\t' << *I;
  918. }
  919. }
  920. #endif
  921. /// foldMemoryOperand - Try folding stack slot references in Ops into their
  922. /// instructions.
  923. ///
  924. /// @param Ops Operand indices from analyzeVirtReg().
  925. /// @param LoadMI Load instruction to use instead of stack slot when non-null.
  926. /// @return True on success.
  927. bool InlineSpiller::
  928. foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
  929. MachineInstr *LoadMI) {
  930. if (Ops.empty())
  931. return false;
  932. // Don't attempt folding in bundles.
  933. MachineInstr *MI = Ops.front().first;
  934. if (Ops.back().first != MI || MI->isBundled())
  935. return false;
  936. bool WasCopy = MI->isCopy();
  937. unsigned ImpReg = 0;
  938. bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::STATEPOINT ||
  939. MI->getOpcode() == TargetOpcode::PATCHPOINT ||
  940. MI->getOpcode() == TargetOpcode::STACKMAP);
  941. // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
  942. // operands.
  943. SmallVector<unsigned, 8> FoldOps;
  944. for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
  945. unsigned Idx = Ops[i].second;
  946. assert(MI == Ops[i].first && "Instruction conflict during operand folding");
  947. MachineOperand &MO = MI->getOperand(Idx);
  948. if (MO.isImplicit()) {
  949. ImpReg = MO.getReg();
  950. continue;
  951. }
  952. // FIXME: Teach targets to deal with subregs.
  953. if (!SpillSubRegs && MO.getSubReg())
  954. return false;
  955. // We cannot fold a load instruction into a def.
  956. if (LoadMI && MO.isDef())
  957. return false;
  958. // Tied use operands should not be passed to foldMemoryOperand.
  959. if (!MI->isRegTiedToDefOperand(Idx))
  960. FoldOps.push_back(Idx);
  961. }
  962. MachineInstrSpan MIS(MI);
  963. MachineInstr *FoldMI =
  964. LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
  965. : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
  966. if (!FoldMI)
  967. return false;
  968. // Remove LIS for any dead defs in the original MI not in FoldMI.
  969. for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
  970. if (!MO->isReg())
  971. continue;
  972. unsigned Reg = MO->getReg();
  973. if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
  974. MRI.isReserved(Reg)) {
  975. continue;
  976. }
  977. // Skip non-Defs, including undef uses and internal reads.
  978. if (MO->isUse())
  979. continue;
  980. MIBundleOperands::PhysRegInfo RI =
  981. MIBundleOperands(FoldMI).analyzePhysReg(Reg, &TRI);
  982. if (RI.Defines)
  983. continue;
  984. // FoldMI does not define this physreg. Remove the LI segment.
  985. assert(MO->isDead() && "Cannot fold physreg def");
  986. SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
  987. LIS.removePhysRegDefAt(Reg, Idx);
  988. }
  989. LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
  990. MI->eraseFromParent();
  991. // Insert any new instructions other than FoldMI into the LIS maps.
  992. assert(!MIS.empty() && "Unexpected empty span of instructions!");
  993. for (MachineBasicBlock::iterator MII = MIS.begin(), End = MIS.end();
  994. MII != End; ++MII)
  995. if (&*MII != FoldMI)
  996. LIS.InsertMachineInstrInMaps(&*MII);
  997. // TII.foldMemoryOperand may have left some implicit operands on the
  998. // instruction. Strip them.
  999. if (ImpReg)
  1000. for (unsigned i = FoldMI->getNumOperands(); i; --i) {
  1001. MachineOperand &MO = FoldMI->getOperand(i - 1);
  1002. if (!MO.isReg() || !MO.isImplicit())
  1003. break;
  1004. if (MO.getReg() == ImpReg)
  1005. FoldMI->RemoveOperand(i - 1);
  1006. }
  1007. DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
  1008. "folded"));
  1009. if (!WasCopy)
  1010. ++NumFolded;
  1011. else if (Ops.front().second == 0)
  1012. ++NumSpills;
  1013. else
  1014. ++NumReloads;
  1015. return true;
  1016. }
  1017. void InlineSpiller::insertReload(unsigned NewVReg,
  1018. SlotIndex Idx,
  1019. MachineBasicBlock::iterator MI) {
  1020. MachineBasicBlock &MBB = *MI->getParent();
  1021. MachineInstrSpan MIS(MI);
  1022. TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
  1023. MRI.getRegClass(NewVReg), &TRI);
  1024. LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
  1025. DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
  1026. NewVReg));
  1027. ++NumReloads;
  1028. }
  1029. /// insertSpill - Insert a spill of NewVReg after MI.
  1030. void InlineSpiller::insertSpill(unsigned NewVReg, bool isKill,
  1031. MachineBasicBlock::iterator MI) {
  1032. MachineBasicBlock &MBB = *MI->getParent();
  1033. MachineInstrSpan MIS(MI);
  1034. TII.storeRegToStackSlot(MBB, std::next(MI), NewVReg, isKill, StackSlot,
  1035. MRI.getRegClass(NewVReg), &TRI);
  1036. LIS.InsertMachineInstrRangeInMaps(std::next(MI), MIS.end());
  1037. DEBUG(dumpMachineInstrRangeWithSlotIndex(std::next(MI), MIS.end(), LIS,
  1038. "spill"));
  1039. ++NumSpills;
  1040. }
  1041. /// spillAroundUses - insert spill code around each use of Reg.
  1042. void InlineSpiller::spillAroundUses(unsigned Reg) {
  1043. DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
  1044. LiveInterval &OldLI = LIS.getInterval(Reg);
  1045. // Iterate over instructions using Reg.
  1046. for (MachineRegisterInfo::reg_bundle_iterator
  1047. RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
  1048. RegI != E; ) {
  1049. MachineInstr *MI = &*(RegI++);
  1050. // Debug values are not allowed to affect codegen.
  1051. if (MI->isDebugValue()) {
  1052. // Modify DBG_VALUE now that the value is in a spill slot.
  1053. bool IsIndirect = MI->isIndirectDebugValue();
  1054. uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
  1055. const MDNode *Var = MI->getDebugVariable();
  1056. const MDNode *Expr = MI->getDebugExpression();
  1057. DebugLoc DL = MI->getDebugLoc();
  1058. DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
  1059. MachineBasicBlock *MBB = MI->getParent();
  1060. assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
  1061. "Expected inlined-at fields to agree");
  1062. BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE))
  1063. .addFrameIndex(StackSlot)
  1064. .addImm(Offset)
  1065. .addMetadata(Var)
  1066. .addMetadata(Expr);
  1067. continue;
  1068. }
  1069. // Ignore copies to/from snippets. We'll delete them.
  1070. if (SnippetCopies.count(MI))
  1071. continue;
  1072. // Stack slot accesses may coalesce away.
  1073. if (coalesceStackAccess(MI, Reg))
  1074. continue;
  1075. // Analyze instruction.
  1076. SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
  1077. MIBundleOperands::VirtRegInfo RI =
  1078. MIBundleOperands(MI).analyzeVirtReg(Reg, &Ops);
  1079. // Find the slot index where this instruction reads and writes OldLI.
  1080. // This is usually the def slot, except for tied early clobbers.
  1081. SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
  1082. if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
  1083. if (SlotIndex::isSameInstr(Idx, VNI->def))
  1084. Idx = VNI->def;
  1085. // Check for a sibling copy.
  1086. unsigned SibReg = isFullCopyOf(MI, Reg);
  1087. if (SibReg && isSibling(SibReg)) {
  1088. // This may actually be a copy between snippets.
  1089. if (isRegToSpill(SibReg)) {
  1090. DEBUG(dbgs() << "Found new snippet copy: " << *MI);
  1091. SnippetCopies.insert(MI);
  1092. continue;
  1093. }
  1094. if (RI.Writes) {
  1095. // Hoist the spill of a sib-reg copy.
  1096. if (hoistSpill(OldLI, MI)) {
  1097. // This COPY is now dead, the value is already in the stack slot.
  1098. MI->getOperand(0).setIsDead();
  1099. DeadDefs.push_back(MI);
  1100. continue;
  1101. }
  1102. } else {
  1103. // This is a reload for a sib-reg copy. Drop spills downstream.
  1104. LiveInterval &SibLI = LIS.getInterval(SibReg);
  1105. eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
  1106. // The COPY will fold to a reload below.
  1107. }
  1108. }
  1109. // Attempt to fold memory ops.
  1110. if (foldMemoryOperand(Ops))
  1111. continue;
  1112. // Create a new virtual register for spill/fill.
  1113. // FIXME: Infer regclass from instruction alone.
  1114. unsigned NewVReg = Edit->createFrom(Reg);
  1115. if (RI.Reads)
  1116. insertReload(NewVReg, Idx, MI);
  1117. // Rewrite instruction operands.
  1118. bool hasLiveDef = false;
  1119. for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
  1120. MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second);
  1121. MO.setReg(NewVReg);
  1122. if (MO.isUse()) {
  1123. if (!Ops[i].first->isRegTiedToDefOperand(Ops[i].second))
  1124. MO.setIsKill();
  1125. } else {
  1126. if (!MO.isDead())
  1127. hasLiveDef = true;
  1128. }
  1129. }
  1130. DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI << '\n');
  1131. // FIXME: Use a second vreg if instruction has no tied ops.
  1132. if (RI.Writes)
  1133. if (hasLiveDef)
  1134. insertSpill(NewVReg, true, MI);
  1135. }
  1136. }
  1137. /// spillAll - Spill all registers remaining after rematerialization.
  1138. void InlineSpiller::spillAll() {
  1139. // Update LiveStacks now that we are committed to spilling.
  1140. if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
  1141. StackSlot = VRM.assignVirt2StackSlot(Original);
  1142. StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
  1143. StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
  1144. } else
  1145. StackInt = &LSS.getInterval(StackSlot);
  1146. if (Original != Edit->getReg())
  1147. VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
  1148. assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
  1149. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
  1150. StackInt->MergeSegmentsInAsValue(LIS.getInterval(RegsToSpill[i]),
  1151. StackInt->getValNumInfo(0));
  1152. DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
  1153. // Spill around uses of all RegsToSpill.
  1154. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
  1155. spillAroundUses(RegsToSpill[i]);
  1156. // Hoisted spills may cause dead code.
  1157. if (!DeadDefs.empty()) {
  1158. DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
  1159. Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
  1160. }
  1161. // Finally delete the SnippetCopies.
  1162. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
  1163. for (MachineRegisterInfo::reg_instr_iterator
  1164. RI = MRI.reg_instr_begin(RegsToSpill[i]), E = MRI.reg_instr_end();
  1165. RI != E; ) {
  1166. MachineInstr *MI = &*(RI++);
  1167. assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
  1168. // FIXME: Do this with a LiveRangeEdit callback.
  1169. LIS.RemoveMachineInstrFromMaps(MI);
  1170. MI->eraseFromParent();
  1171. }
  1172. }
  1173. // Delete all spilled registers.
  1174. for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
  1175. Edit->eraseVirtReg(RegsToSpill[i]);
  1176. }
  1177. void InlineSpiller::spill(LiveRangeEdit &edit) {
  1178. ++NumSpilledRanges;
  1179. Edit = &edit;
  1180. assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
  1181. && "Trying to spill a stack slot.");
  1182. // Share a stack slot among all descendants of Original.
  1183. Original = VRM.getOriginal(edit.getReg());
  1184. StackSlot = VRM.getStackSlot(Original);
  1185. StackInt = nullptr;
  1186. DEBUG(dbgs() << "Inline spilling "
  1187. << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
  1188. << ':' << edit.getParent()
  1189. << "\nFrom original " << PrintReg(Original) << '\n');
  1190. assert(edit.getParent().isSpillable() &&
  1191. "Attempting to spill already spilled value.");
  1192. assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
  1193. collectRegsToSpill();
  1194. analyzeSiblingValues();
  1195. reMaterializeAll();
  1196. // Remat may handle everything.
  1197. if (!RegsToSpill.empty())
  1198. spillAll();
  1199. Edit->calculateRegClassAndHint(MF, Loops, MBFI);
  1200. }