InlineSpiller.cpp 47 KB

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