SplitKit.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. //===- SplitKit.cpp - Toolkit for splitting live ranges -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the SplitAnalysis class as well as mutator functions for
  10. // live range splitting.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "SplitKit.h"
  14. #include "llvm/ADT/None.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/Analysis/AliasAnalysis.h"
  18. #include "llvm/CodeGen/LiveRangeEdit.h"
  19. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  20. #include "llvm/CodeGen/MachineDominators.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineInstrBuilder.h"
  23. #include "llvm/CodeGen/MachineLoopInfo.h"
  24. #include "llvm/CodeGen/MachineOperand.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/TargetInstrInfo.h"
  27. #include "llvm/CodeGen/TargetOpcodes.h"
  28. #include "llvm/CodeGen/TargetRegisterInfo.h"
  29. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  30. #include "llvm/CodeGen/VirtRegMap.h"
  31. #include "llvm/Config/llvm-config.h"
  32. #include "llvm/IR/DebugLoc.h"
  33. #include "llvm/Support/Allocator.h"
  34. #include "llvm/Support/BlockFrequency.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/ErrorHandling.h"
  37. #include "llvm/Support/raw_ostream.h"
  38. #include <algorithm>
  39. #include <cassert>
  40. #include <iterator>
  41. #include <limits>
  42. #include <tuple>
  43. using namespace llvm;
  44. #define DEBUG_TYPE "regalloc"
  45. STATISTIC(NumFinished, "Number of splits finished");
  46. STATISTIC(NumSimple, "Number of splits that were simple");
  47. STATISTIC(NumCopies, "Number of copies inserted for splitting");
  48. STATISTIC(NumRemats, "Number of rematerialized defs for splitting");
  49. //===----------------------------------------------------------------------===//
  50. // Last Insert Point Analysis
  51. //===----------------------------------------------------------------------===//
  52. InsertPointAnalysis::InsertPointAnalysis(const LiveIntervals &lis,
  53. unsigned BBNum)
  54. : LIS(lis), LastInsertPoint(BBNum) {}
  55. SlotIndex
  56. InsertPointAnalysis::computeLastInsertPoint(const LiveInterval &CurLI,
  57. const MachineBasicBlock &MBB) {
  58. unsigned Num = MBB.getNumber();
  59. std::pair<SlotIndex, SlotIndex> &LIP = LastInsertPoint[Num];
  60. SlotIndex MBBEnd = LIS.getMBBEndIdx(&MBB);
  61. SmallVector<const MachineBasicBlock *, 1> ExceptionalSuccessors;
  62. bool EHPadSuccessor = false;
  63. for (const MachineBasicBlock *SMBB : MBB.successors()) {
  64. if (SMBB->isEHPad()) {
  65. ExceptionalSuccessors.push_back(SMBB);
  66. EHPadSuccessor = true;
  67. } else if (SMBB->isInlineAsmBrIndirectTarget())
  68. ExceptionalSuccessors.push_back(SMBB);
  69. }
  70. // Compute insert points on the first call. The pair is independent of the
  71. // current live interval.
  72. if (!LIP.first.isValid()) {
  73. MachineBasicBlock::const_iterator FirstTerm = MBB.getFirstTerminator();
  74. if (FirstTerm == MBB.end())
  75. LIP.first = MBBEnd;
  76. else
  77. LIP.first = LIS.getInstructionIndex(*FirstTerm);
  78. // If there is a landing pad or inlineasm_br successor, also find the
  79. // instruction. If there is no such instruction, we don't need to do
  80. // anything special. We assume there cannot be multiple instructions that
  81. // are Calls with EHPad successors or INLINEASM_BR in a block. Further, we
  82. // assume that if there are any, they will be after any other call
  83. // instructions in the block.
  84. if (ExceptionalSuccessors.empty())
  85. return LIP.first;
  86. for (const MachineInstr &MI : llvm::reverse(MBB)) {
  87. if ((EHPadSuccessor && MI.isCall()) ||
  88. MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
  89. LIP.second = LIS.getInstructionIndex(MI);
  90. break;
  91. }
  92. }
  93. }
  94. // If CurLI is live into a landing pad successor, move the last insert point
  95. // back to the call that may throw.
  96. if (!LIP.second)
  97. return LIP.first;
  98. if (none_of(ExceptionalSuccessors, [&](const MachineBasicBlock *EHPad) {
  99. return LIS.isLiveInToMBB(CurLI, EHPad);
  100. }))
  101. return LIP.first;
  102. // Find the value leaving MBB.
  103. const VNInfo *VNI = CurLI.getVNInfoBefore(MBBEnd);
  104. if (!VNI)
  105. return LIP.first;
  106. // The def of statepoint instruction is a gc relocation and it should be alive
  107. // in landing pad. So we cannot split interval after statepoint instruction.
  108. if (SlotIndex::isSameInstr(VNI->def, LIP.second))
  109. if (auto *I = LIS.getInstructionFromIndex(LIP.second))
  110. if (I->getOpcode() == TargetOpcode::STATEPOINT)
  111. return LIP.second;
  112. // If the value leaving MBB was defined after the call in MBB, it can't
  113. // really be live-in to the landing pad. This can happen if the landing pad
  114. // has a PHI, and this register is undef on the exceptional edge.
  115. // <rdar://problem/10664933>
  116. if (!SlotIndex::isEarlierInstr(VNI->def, LIP.second) && VNI->def < MBBEnd)
  117. return LIP.first;
  118. // Value is properly live-in to the landing pad.
  119. // Only allow inserts before the call.
  120. return LIP.second;
  121. }
  122. MachineBasicBlock::iterator
  123. InsertPointAnalysis::getLastInsertPointIter(const LiveInterval &CurLI,
  124. MachineBasicBlock &MBB) {
  125. SlotIndex LIP = getLastInsertPoint(CurLI, MBB);
  126. if (LIP == LIS.getMBBEndIdx(&MBB))
  127. return MBB.end();
  128. return LIS.getInstructionFromIndex(LIP);
  129. }
  130. //===----------------------------------------------------------------------===//
  131. // Split Analysis
  132. //===----------------------------------------------------------------------===//
  133. SplitAnalysis::SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis,
  134. const MachineLoopInfo &mli)
  135. : MF(vrm.getMachineFunction()), VRM(vrm), LIS(lis), Loops(mli),
  136. TII(*MF.getSubtarget().getInstrInfo()), IPA(lis, MF.getNumBlockIDs()) {}
  137. void SplitAnalysis::clear() {
  138. UseSlots.clear();
  139. UseBlocks.clear();
  140. ThroughBlocks.clear();
  141. CurLI = nullptr;
  142. }
  143. /// analyzeUses - Count instructions, basic blocks, and loops using CurLI.
  144. void SplitAnalysis::analyzeUses() {
  145. assert(UseSlots.empty() && "Call clear first");
  146. // First get all the defs from the interval values. This provides the correct
  147. // slots for early clobbers.
  148. for (const VNInfo *VNI : CurLI->valnos)
  149. if (!VNI->isPHIDef() && !VNI->isUnused())
  150. UseSlots.push_back(VNI->def);
  151. // Get use slots form the use-def chain.
  152. const MachineRegisterInfo &MRI = MF.getRegInfo();
  153. for (MachineOperand &MO : MRI.use_nodbg_operands(CurLI->reg()))
  154. if (!MO.isUndef())
  155. UseSlots.push_back(LIS.getInstructionIndex(*MO.getParent()).getRegSlot());
  156. array_pod_sort(UseSlots.begin(), UseSlots.end());
  157. // Remove duplicates, keeping the smaller slot for each instruction.
  158. // That is what we want for early clobbers.
  159. UseSlots.erase(std::unique(UseSlots.begin(), UseSlots.end(),
  160. SlotIndex::isSameInstr),
  161. UseSlots.end());
  162. // Compute per-live block info.
  163. calcLiveBlockInfo();
  164. LLVM_DEBUG(dbgs() << "Analyze counted " << UseSlots.size() << " instrs in "
  165. << UseBlocks.size() << " blocks, through "
  166. << NumThroughBlocks << " blocks.\n");
  167. }
  168. /// calcLiveBlockInfo - Fill the LiveBlocks array with information about blocks
  169. /// where CurLI is live.
  170. void SplitAnalysis::calcLiveBlockInfo() {
  171. ThroughBlocks.resize(MF.getNumBlockIDs());
  172. NumThroughBlocks = NumGapBlocks = 0;
  173. if (CurLI->empty())
  174. return;
  175. LiveInterval::const_iterator LVI = CurLI->begin();
  176. LiveInterval::const_iterator LVE = CurLI->end();
  177. SmallVectorImpl<SlotIndex>::const_iterator UseI, UseE;
  178. UseI = UseSlots.begin();
  179. UseE = UseSlots.end();
  180. // Loop over basic blocks where CurLI is live.
  181. MachineFunction::iterator MFI =
  182. LIS.getMBBFromIndex(LVI->start)->getIterator();
  183. while (true) {
  184. BlockInfo BI;
  185. BI.MBB = &*MFI;
  186. SlotIndex Start, Stop;
  187. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
  188. // If the block contains no uses, the range must be live through. At one
  189. // point, RegisterCoalescer could create dangling ranges that ended
  190. // mid-block.
  191. if (UseI == UseE || *UseI >= Stop) {
  192. ++NumThroughBlocks;
  193. ThroughBlocks.set(BI.MBB->getNumber());
  194. // The range shouldn't end mid-block if there are no uses. This shouldn't
  195. // happen.
  196. assert(LVI->end >= Stop && "range ends mid block with no uses");
  197. } else {
  198. // This block has uses. Find the first and last uses in the block.
  199. BI.FirstInstr = *UseI;
  200. assert(BI.FirstInstr >= Start);
  201. do ++UseI;
  202. while (UseI != UseE && *UseI < Stop);
  203. BI.LastInstr = UseI[-1];
  204. assert(BI.LastInstr < Stop);
  205. // LVI is the first live segment overlapping MBB.
  206. BI.LiveIn = LVI->start <= Start;
  207. // When not live in, the first use should be a def.
  208. if (!BI.LiveIn) {
  209. assert(LVI->start == LVI->valno->def && "Dangling Segment start");
  210. assert(LVI->start == BI.FirstInstr && "First instr should be a def");
  211. BI.FirstDef = BI.FirstInstr;
  212. }
  213. // Look for gaps in the live range.
  214. BI.LiveOut = true;
  215. while (LVI->end < Stop) {
  216. SlotIndex LastStop = LVI->end;
  217. if (++LVI == LVE || LVI->start >= Stop) {
  218. BI.LiveOut = false;
  219. BI.LastInstr = LastStop;
  220. break;
  221. }
  222. if (LastStop < LVI->start) {
  223. // There is a gap in the live range. Create duplicate entries for the
  224. // live-in snippet and the live-out snippet.
  225. ++NumGapBlocks;
  226. // Push the Live-in part.
  227. BI.LiveOut = false;
  228. UseBlocks.push_back(BI);
  229. UseBlocks.back().LastInstr = LastStop;
  230. // Set up BI for the live-out part.
  231. BI.LiveIn = false;
  232. BI.LiveOut = true;
  233. BI.FirstInstr = BI.FirstDef = LVI->start;
  234. }
  235. // A Segment that starts in the middle of the block must be a def.
  236. assert(LVI->start == LVI->valno->def && "Dangling Segment start");
  237. if (!BI.FirstDef)
  238. BI.FirstDef = LVI->start;
  239. }
  240. UseBlocks.push_back(BI);
  241. // LVI is now at LVE or LVI->end >= Stop.
  242. if (LVI == LVE)
  243. break;
  244. }
  245. // Live segment ends exactly at Stop. Move to the next segment.
  246. if (LVI->end == Stop && ++LVI == LVE)
  247. break;
  248. // Pick the next basic block.
  249. if (LVI->start < Stop)
  250. ++MFI;
  251. else
  252. MFI = LIS.getMBBFromIndex(LVI->start)->getIterator();
  253. }
  254. assert(getNumLiveBlocks() == countLiveBlocks(CurLI) && "Bad block count");
  255. }
  256. unsigned SplitAnalysis::countLiveBlocks(const LiveInterval *cli) const {
  257. if (cli->empty())
  258. return 0;
  259. LiveInterval *li = const_cast<LiveInterval*>(cli);
  260. LiveInterval::iterator LVI = li->begin();
  261. LiveInterval::iterator LVE = li->end();
  262. unsigned Count = 0;
  263. // Loop over basic blocks where li is live.
  264. MachineFunction::const_iterator MFI =
  265. LIS.getMBBFromIndex(LVI->start)->getIterator();
  266. SlotIndex Stop = LIS.getMBBEndIdx(&*MFI);
  267. while (true) {
  268. ++Count;
  269. LVI = li->advanceTo(LVI, Stop);
  270. if (LVI == LVE)
  271. return Count;
  272. do {
  273. ++MFI;
  274. Stop = LIS.getMBBEndIdx(&*MFI);
  275. } while (Stop <= LVI->start);
  276. }
  277. }
  278. bool SplitAnalysis::isOriginalEndpoint(SlotIndex Idx) const {
  279. unsigned OrigReg = VRM.getOriginal(CurLI->reg());
  280. const LiveInterval &Orig = LIS.getInterval(OrigReg);
  281. assert(!Orig.empty() && "Splitting empty interval?");
  282. LiveInterval::const_iterator I = Orig.find(Idx);
  283. // Range containing Idx should begin at Idx.
  284. if (I != Orig.end() && I->start <= Idx)
  285. return I->start == Idx;
  286. // Range does not contain Idx, previous must end at Idx.
  287. return I != Orig.begin() && (--I)->end == Idx;
  288. }
  289. void SplitAnalysis::analyze(const LiveInterval *li) {
  290. clear();
  291. CurLI = li;
  292. analyzeUses();
  293. }
  294. //===----------------------------------------------------------------------===//
  295. // Split Editor
  296. //===----------------------------------------------------------------------===//
  297. /// Create a new SplitEditor for editing the LiveInterval analyzed by SA.
  298. SplitEditor::SplitEditor(SplitAnalysis &SA, AliasAnalysis &AA,
  299. LiveIntervals &LIS, VirtRegMap &VRM,
  300. MachineDominatorTree &MDT,
  301. MachineBlockFrequencyInfo &MBFI, VirtRegAuxInfo &VRAI)
  302. : SA(SA), AA(AA), LIS(LIS), VRM(VRM),
  303. MRI(VRM.getMachineFunction().getRegInfo()), MDT(MDT),
  304. TII(*VRM.getMachineFunction().getSubtarget().getInstrInfo()),
  305. TRI(*VRM.getMachineFunction().getSubtarget().getRegisterInfo()),
  306. MBFI(MBFI), VRAI(VRAI), RegAssign(Allocator) {}
  307. void SplitEditor::reset(LiveRangeEdit &LRE, ComplementSpillMode SM) {
  308. Edit = &LRE;
  309. SpillMode = SM;
  310. OpenIdx = 0;
  311. RegAssign.clear();
  312. Values.clear();
  313. // Reset the LiveIntervalCalc instances needed for this spill mode.
  314. LICalc[0].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
  315. &LIS.getVNInfoAllocator());
  316. if (SpillMode)
  317. LICalc[1].reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
  318. &LIS.getVNInfoAllocator());
  319. // We don't need an AliasAnalysis since we will only be performing
  320. // cheap-as-a-copy remats anyway.
  321. Edit->anyRematerializable(nullptr);
  322. }
  323. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  324. LLVM_DUMP_METHOD void SplitEditor::dump() const {
  325. if (RegAssign.empty()) {
  326. dbgs() << " empty\n";
  327. return;
  328. }
  329. for (RegAssignMap::const_iterator I = RegAssign.begin(); I.valid(); ++I)
  330. dbgs() << " [" << I.start() << ';' << I.stop() << "):" << I.value();
  331. dbgs() << '\n';
  332. }
  333. #endif
  334. LiveInterval::SubRange &SplitEditor::getSubRangeForMaskExact(LaneBitmask LM,
  335. LiveInterval &LI) {
  336. for (LiveInterval::SubRange &S : LI.subranges())
  337. if (S.LaneMask == LM)
  338. return S;
  339. llvm_unreachable("SubRange for this mask not found");
  340. }
  341. LiveInterval::SubRange &SplitEditor::getSubRangeForMask(LaneBitmask LM,
  342. LiveInterval &LI) {
  343. for (LiveInterval::SubRange &S : LI.subranges())
  344. if ((S.LaneMask & LM) == LM)
  345. return S;
  346. llvm_unreachable("SubRange for this mask not found");
  347. }
  348. void SplitEditor::addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original) {
  349. if (!LI.hasSubRanges()) {
  350. LI.createDeadDef(VNI);
  351. return;
  352. }
  353. SlotIndex Def = VNI->def;
  354. if (Original) {
  355. // If we are transferring a def from the original interval, make sure
  356. // to only update the subranges for which the original subranges had
  357. // a def at this location.
  358. for (LiveInterval::SubRange &S : LI.subranges()) {
  359. auto &PS = getSubRangeForMask(S.LaneMask, Edit->getParent());
  360. VNInfo *PV = PS.getVNInfoAt(Def);
  361. if (PV != nullptr && PV->def == Def)
  362. S.createDeadDef(Def, LIS.getVNInfoAllocator());
  363. }
  364. } else {
  365. // This is a new def: either from rematerialization, or from an inserted
  366. // copy. Since rematerialization can regenerate a definition of a sub-
  367. // register, we need to check which subranges need to be updated.
  368. const MachineInstr *DefMI = LIS.getInstructionFromIndex(Def);
  369. assert(DefMI != nullptr);
  370. LaneBitmask LM;
  371. for (const MachineOperand &DefOp : DefMI->defs()) {
  372. Register R = DefOp.getReg();
  373. if (R != LI.reg())
  374. continue;
  375. if (unsigned SR = DefOp.getSubReg())
  376. LM |= TRI.getSubRegIndexLaneMask(SR);
  377. else {
  378. LM = MRI.getMaxLaneMaskForVReg(R);
  379. break;
  380. }
  381. }
  382. for (LiveInterval::SubRange &S : LI.subranges())
  383. if ((S.LaneMask & LM).any())
  384. S.createDeadDef(Def, LIS.getVNInfoAllocator());
  385. }
  386. }
  387. VNInfo *SplitEditor::defValue(unsigned RegIdx,
  388. const VNInfo *ParentVNI,
  389. SlotIndex Idx,
  390. bool Original) {
  391. assert(ParentVNI && "Mapping NULL value");
  392. assert(Idx.isValid() && "Invalid SlotIndex");
  393. assert(Edit->getParent().getVNInfoAt(Idx) == ParentVNI && "Bad Parent VNI");
  394. LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
  395. // Create a new value.
  396. VNInfo *VNI = LI->getNextValue(Idx, LIS.getVNInfoAllocator());
  397. bool Force = LI->hasSubRanges();
  398. ValueForcePair FP(Force ? nullptr : VNI, Force);
  399. // Use insert for lookup, so we can add missing values with a second lookup.
  400. std::pair<ValueMap::iterator, bool> InsP =
  401. Values.insert(std::make_pair(std::make_pair(RegIdx, ParentVNI->id), FP));
  402. // This was the first time (RegIdx, ParentVNI) was mapped, and it is not
  403. // forced. Keep it as a simple def without any liveness.
  404. if (!Force && InsP.second)
  405. return VNI;
  406. // If the previous value was a simple mapping, add liveness for it now.
  407. if (VNInfo *OldVNI = InsP.first->second.getPointer()) {
  408. addDeadDef(*LI, OldVNI, Original);
  409. // No longer a simple mapping. Switch to a complex mapping. If the
  410. // interval has subranges, make it a forced mapping.
  411. InsP.first->second = ValueForcePair(nullptr, Force);
  412. }
  413. // This is a complex mapping, add liveness for VNI
  414. addDeadDef(*LI, VNI, Original);
  415. return VNI;
  416. }
  417. void SplitEditor::forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI) {
  418. ValueForcePair &VFP = Values[std::make_pair(RegIdx, ParentVNI.id)];
  419. VNInfo *VNI = VFP.getPointer();
  420. // ParentVNI was either unmapped or already complex mapped. Either way, just
  421. // set the force bit.
  422. if (!VNI) {
  423. VFP.setInt(true);
  424. return;
  425. }
  426. // This was previously a single mapping. Make sure the old def is represented
  427. // by a trivial live range.
  428. addDeadDef(LIS.getInterval(Edit->get(RegIdx)), VNI, false);
  429. // Mark as complex mapped, forced.
  430. VFP = ValueForcePair(nullptr, true);
  431. }
  432. SlotIndex SplitEditor::buildSingleSubRegCopy(Register FromReg, Register ToReg,
  433. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  434. unsigned SubIdx, LiveInterval &DestLI, bool Late, SlotIndex Def) {
  435. const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
  436. bool FirstCopy = !Def.isValid();
  437. MachineInstr *CopyMI = BuildMI(MBB, InsertBefore, DebugLoc(), Desc)
  438. .addReg(ToReg, RegState::Define | getUndefRegState(FirstCopy)
  439. | getInternalReadRegState(!FirstCopy), SubIdx)
  440. .addReg(FromReg, 0, SubIdx);
  441. SlotIndexes &Indexes = *LIS.getSlotIndexes();
  442. if (FirstCopy) {
  443. Def = Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
  444. } else {
  445. CopyMI->bundleWithPred();
  446. }
  447. return Def;
  448. }
  449. SlotIndex SplitEditor::buildCopy(Register FromReg, Register ToReg,
  450. LaneBitmask LaneMask, MachineBasicBlock &MBB,
  451. MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
  452. const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
  453. SlotIndexes &Indexes = *LIS.getSlotIndexes();
  454. if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
  455. // The full vreg is copied.
  456. MachineInstr *CopyMI =
  457. BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
  458. return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
  459. }
  460. // Only a subset of lanes needs to be copied. The following is a simple
  461. // heuristic to construct a sequence of COPYs. We could add a target
  462. // specific callback if this turns out to be suboptimal.
  463. LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
  464. // First pass: Try to find a perfectly matching subregister index. If none
  465. // exists find the one covering the most lanemask bits.
  466. const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
  467. assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
  468. SmallVector<unsigned, 8> SubIndexes;
  469. // Abort if we cannot possibly implement the COPY with the given indexes.
  470. if (!TRI.getCoveringSubRegIndexes(MRI, RC, LaneMask, SubIndexes))
  471. report_fatal_error("Impossible to implement partial COPY");
  472. SlotIndex Def;
  473. for (unsigned BestIdx : SubIndexes) {
  474. Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
  475. DestLI, Late, Def);
  476. }
  477. BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
  478. DestLI.refineSubRanges(
  479. Allocator, LaneMask,
  480. [Def, &Allocator](LiveInterval::SubRange &SR) {
  481. SR.createDeadDef(Def, Allocator);
  482. },
  483. Indexes, TRI);
  484. return Def;
  485. }
  486. VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
  487. VNInfo *ParentVNI,
  488. SlotIndex UseIdx,
  489. MachineBasicBlock &MBB,
  490. MachineBasicBlock::iterator I) {
  491. SlotIndex Def;
  492. LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
  493. // We may be trying to avoid interference that ends at a deleted instruction,
  494. // so always begin RegIdx 0 early and all others late.
  495. bool Late = RegIdx != 0;
  496. // Attempt cheap-as-a-copy rematerialization.
  497. unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
  498. LiveInterval &OrigLI = LIS.getInterval(Original);
  499. VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
  500. Register Reg = LI->reg();
  501. bool DidRemat = false;
  502. if (OrigVNI) {
  503. LiveRangeEdit::Remat RM(ParentVNI);
  504. RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
  505. if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
  506. Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
  507. ++NumRemats;
  508. DidRemat = true;
  509. }
  510. }
  511. if (!DidRemat) {
  512. LaneBitmask LaneMask;
  513. if (OrigLI.hasSubRanges()) {
  514. LaneMask = LaneBitmask::getNone();
  515. for (LiveInterval::SubRange &S : OrigLI.subranges()) {
  516. if (S.liveAt(UseIdx))
  517. LaneMask |= S.LaneMask;
  518. }
  519. } else {
  520. LaneMask = LaneBitmask::getAll();
  521. }
  522. if (LaneMask.none()) {
  523. const MCInstrDesc &Desc = TII.get(TargetOpcode::IMPLICIT_DEF);
  524. MachineInstr *ImplicitDef = BuildMI(MBB, I, DebugLoc(), Desc, Reg);
  525. SlotIndexes &Indexes = *LIS.getSlotIndexes();
  526. Def = Indexes.insertMachineInstrInMaps(*ImplicitDef, Late).getRegSlot();
  527. } else {
  528. ++NumCopies;
  529. Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
  530. }
  531. }
  532. // Define the value in Reg.
  533. return defValue(RegIdx, ParentVNI, Def, false);
  534. }
  535. /// Create a new virtual register and live interval.
  536. unsigned SplitEditor::openIntv() {
  537. // Create the complement as index 0.
  538. if (Edit->empty())
  539. Edit->createEmptyInterval();
  540. // Create the open interval.
  541. OpenIdx = Edit->size();
  542. Edit->createEmptyInterval();
  543. return OpenIdx;
  544. }
  545. void SplitEditor::selectIntv(unsigned Idx) {
  546. assert(Idx != 0 && "Cannot select the complement interval");
  547. assert(Idx < Edit->size() && "Can only select previously opened interval");
  548. LLVM_DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
  549. OpenIdx = Idx;
  550. }
  551. SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
  552. assert(OpenIdx && "openIntv not called before enterIntvBefore");
  553. LLVM_DEBUG(dbgs() << " enterIntvBefore " << Idx);
  554. Idx = Idx.getBaseIndex();
  555. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
  556. if (!ParentVNI) {
  557. LLVM_DEBUG(dbgs() << ": not live\n");
  558. return Idx;
  559. }
  560. LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  561. MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
  562. assert(MI && "enterIntvBefore called with invalid index");
  563. VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
  564. return VNI->def;
  565. }
  566. SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
  567. assert(OpenIdx && "openIntv not called before enterIntvAfter");
  568. LLVM_DEBUG(dbgs() << " enterIntvAfter " << Idx);
  569. Idx = Idx.getBoundaryIndex();
  570. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
  571. if (!ParentVNI) {
  572. LLVM_DEBUG(dbgs() << ": not live\n");
  573. return Idx;
  574. }
  575. LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  576. MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
  577. assert(MI && "enterIntvAfter called with invalid index");
  578. VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
  579. std::next(MachineBasicBlock::iterator(MI)));
  580. return VNI->def;
  581. }
  582. SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
  583. assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
  584. SlotIndex End = LIS.getMBBEndIdx(&MBB);
  585. SlotIndex Last = End.getPrevSlot();
  586. LLVM_DEBUG(dbgs() << " enterIntvAtEnd " << printMBBReference(MBB) << ", "
  587. << Last);
  588. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
  589. if (!ParentVNI) {
  590. LLVM_DEBUG(dbgs() << ": not live\n");
  591. return End;
  592. }
  593. SlotIndex LSP = SA.getLastSplitPoint(&MBB);
  594. if (LSP < Last) {
  595. // It could be that the use after LSP is a def, and thus the ParentVNI
  596. // just selected starts at that def. For this case to exist, the def
  597. // must be part of a tied def/use pair (as otherwise we'd have split
  598. // distinct live ranges into individual live intervals), and thus we
  599. // can insert the def into the VNI of the use and the tied def/use
  600. // pair can live in the resulting interval.
  601. Last = LSP;
  602. ParentVNI = Edit->getParent().getVNInfoAt(Last);
  603. if (!ParentVNI) {
  604. // undef use --> undef tied def
  605. LLVM_DEBUG(dbgs() << ": tied use not live\n");
  606. return End;
  607. }
  608. }
  609. LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id);
  610. VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
  611. SA.getLastSplitPointIter(&MBB));
  612. RegAssign.insert(VNI->def, End, OpenIdx);
  613. LLVM_DEBUG(dump());
  614. return VNI->def;
  615. }
  616. /// useIntv - indicate that all instructions in MBB should use OpenLI.
  617. void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
  618. useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
  619. }
  620. void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
  621. assert(OpenIdx && "openIntv not called before useIntv");
  622. LLVM_DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
  623. RegAssign.insert(Start, End, OpenIdx);
  624. LLVM_DEBUG(dump());
  625. }
  626. SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
  627. assert(OpenIdx && "openIntv not called before leaveIntvAfter");
  628. LLVM_DEBUG(dbgs() << " leaveIntvAfter " << Idx);
  629. // The interval must be live beyond the instruction at Idx.
  630. SlotIndex Boundary = Idx.getBoundaryIndex();
  631. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
  632. if (!ParentVNI) {
  633. LLVM_DEBUG(dbgs() << ": not live\n");
  634. return Boundary.getNextSlot();
  635. }
  636. LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  637. MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
  638. assert(MI && "No instruction at index");
  639. // In spill mode, make live ranges as short as possible by inserting the copy
  640. // before MI. This is only possible if that instruction doesn't redefine the
  641. // value. The inserted COPY is not a kill, and we don't need to recompute
  642. // the source live range. The spiller also won't try to hoist this copy.
  643. if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
  644. MI->readsVirtualRegister(Edit->getReg())) {
  645. forceRecompute(0, *ParentVNI);
  646. defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
  647. return Idx;
  648. }
  649. VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
  650. std::next(MachineBasicBlock::iterator(MI)));
  651. return VNI->def;
  652. }
  653. SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
  654. assert(OpenIdx && "openIntv not called before leaveIntvBefore");
  655. LLVM_DEBUG(dbgs() << " leaveIntvBefore " << Idx);
  656. // The interval must be live into the instruction at Idx.
  657. Idx = Idx.getBaseIndex();
  658. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
  659. if (!ParentVNI) {
  660. LLVM_DEBUG(dbgs() << ": not live\n");
  661. return Idx.getNextSlot();
  662. }
  663. LLVM_DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  664. MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
  665. assert(MI && "No instruction at index");
  666. VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
  667. return VNI->def;
  668. }
  669. SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
  670. assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
  671. SlotIndex Start = LIS.getMBBStartIdx(&MBB);
  672. LLVM_DEBUG(dbgs() << " leaveIntvAtTop " << printMBBReference(MBB) << ", "
  673. << Start);
  674. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
  675. if (!ParentVNI) {
  676. LLVM_DEBUG(dbgs() << ": not live\n");
  677. return Start;
  678. }
  679. VNInfo *VNI = defFromParent(0, ParentVNI, Start, MBB,
  680. MBB.SkipPHIsLabelsAndDebug(MBB.begin()));
  681. RegAssign.insert(Start, VNI->def, OpenIdx);
  682. LLVM_DEBUG(dump());
  683. return VNI->def;
  684. }
  685. static bool hasTiedUseOf(MachineInstr &MI, unsigned Reg) {
  686. return any_of(MI.defs(), [Reg](const MachineOperand &MO) {
  687. return MO.isReg() && MO.isTied() && MO.getReg() == Reg;
  688. });
  689. }
  690. void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
  691. assert(OpenIdx && "openIntv not called before overlapIntv");
  692. const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
  693. assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
  694. "Parent changes value in extended range");
  695. assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
  696. "Range cannot span basic blocks");
  697. // The complement interval will be extended as needed by LICalc.extend().
  698. if (ParentVNI)
  699. forceRecompute(0, *ParentVNI);
  700. // If the last use is tied to a def, we can't mark it as live for the
  701. // interval which includes only the use. That would cause the tied pair
  702. // to end up in two different intervals.
  703. if (auto *MI = LIS.getInstructionFromIndex(End))
  704. if (hasTiedUseOf(*MI, Edit->getReg())) {
  705. LLVM_DEBUG(dbgs() << "skip overlap due to tied def at end\n");
  706. return;
  707. }
  708. LLVM_DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
  709. RegAssign.insert(Start, End, OpenIdx);
  710. LLVM_DEBUG(dump());
  711. }
  712. //===----------------------------------------------------------------------===//
  713. // Spill modes
  714. //===----------------------------------------------------------------------===//
  715. void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
  716. LiveInterval *LI = &LIS.getInterval(Edit->get(0));
  717. LLVM_DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
  718. RegAssignMap::iterator AssignI;
  719. AssignI.setMap(RegAssign);
  720. for (const VNInfo *C : Copies) {
  721. SlotIndex Def = C->def;
  722. MachineInstr *MI = LIS.getInstructionFromIndex(Def);
  723. assert(MI && "No instruction for back-copy");
  724. MachineBasicBlock *MBB = MI->getParent();
  725. MachineBasicBlock::iterator MBBI(MI);
  726. bool AtBegin;
  727. do AtBegin = MBBI == MBB->begin();
  728. while (!AtBegin && (--MBBI)->isDebugOrPseudoInstr());
  729. LLVM_DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
  730. LIS.removeVRegDefAt(*LI, Def);
  731. LIS.RemoveMachineInstrFromMaps(*MI);
  732. MI->eraseFromParent();
  733. // Adjust RegAssign if a register assignment is killed at Def. We want to
  734. // avoid calculating the live range of the source register if possible.
  735. AssignI.find(Def.getPrevSlot());
  736. if (!AssignI.valid() || AssignI.start() >= Def)
  737. continue;
  738. // If MI doesn't kill the assigned register, just leave it.
  739. if (AssignI.stop() != Def)
  740. continue;
  741. unsigned RegIdx = AssignI.value();
  742. // We could hoist back-copy right after another back-copy. As a result
  743. // MMBI points to copy instruction which is actually dead now.
  744. // We cannot set its stop to MBBI which will be the same as start and
  745. // interval does not support that.
  746. SlotIndex Kill =
  747. AtBegin ? SlotIndex() : LIS.getInstructionIndex(*MBBI).getRegSlot();
  748. if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg()) ||
  749. Kill <= AssignI.start()) {
  750. LLVM_DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx
  751. << '\n');
  752. forceRecompute(RegIdx, *Edit->getParent().getVNInfoAt(Def));
  753. } else {
  754. LLVM_DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
  755. AssignI.setStop(Kill);
  756. }
  757. }
  758. }
  759. MachineBasicBlock*
  760. SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
  761. MachineBasicBlock *DefMBB) {
  762. if (MBB == DefMBB)
  763. return MBB;
  764. assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
  765. const MachineLoopInfo &Loops = SA.Loops;
  766. const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
  767. MachineDomTreeNode *DefDomNode = MDT[DefMBB];
  768. // Best candidate so far.
  769. MachineBasicBlock *BestMBB = MBB;
  770. unsigned BestDepth = std::numeric_limits<unsigned>::max();
  771. while (true) {
  772. const MachineLoop *Loop = Loops.getLoopFor(MBB);
  773. // MBB isn't in a loop, it doesn't get any better. All dominators have a
  774. // higher frequency by definition.
  775. if (!Loop) {
  776. LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
  777. << " dominates " << printMBBReference(*MBB)
  778. << " at depth 0\n");
  779. return MBB;
  780. }
  781. // We'll never be able to exit the DefLoop.
  782. if (Loop == DefLoop) {
  783. LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
  784. << " dominates " << printMBBReference(*MBB)
  785. << " in the same loop\n");
  786. return MBB;
  787. }
  788. // Least busy dominator seen so far.
  789. unsigned Depth = Loop->getLoopDepth();
  790. if (Depth < BestDepth) {
  791. BestMBB = MBB;
  792. BestDepth = Depth;
  793. LLVM_DEBUG(dbgs() << "Def in " << printMBBReference(*DefMBB)
  794. << " dominates " << printMBBReference(*MBB)
  795. << " at depth " << Depth << '\n');
  796. }
  797. // Leave loop by going to the immediate dominator of the loop header.
  798. // This is a bigger stride than simply walking up the dominator tree.
  799. MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
  800. // Too far up the dominator tree?
  801. if (!IDom || !MDT.dominates(DefDomNode, IDom))
  802. return BestMBB;
  803. MBB = IDom->getBlock();
  804. }
  805. }
  806. void SplitEditor::computeRedundantBackCopies(
  807. DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
  808. LiveInterval *LI = &LIS.getInterval(Edit->get(0));
  809. LiveInterval *Parent = &Edit->getParent();
  810. SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
  811. SmallPtrSet<VNInfo *, 8> DominatedVNIs;
  812. // Aggregate VNIs having the same value as ParentVNI.
  813. for (VNInfo *VNI : LI->valnos) {
  814. if (VNI->isUnused())
  815. continue;
  816. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
  817. EqualVNs[ParentVNI->id].insert(VNI);
  818. }
  819. // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
  820. // redundant VNIs to BackCopies.
  821. for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
  822. VNInfo *ParentVNI = Parent->getValNumInfo(i);
  823. if (!NotToHoistSet.count(ParentVNI->id))
  824. continue;
  825. SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
  826. SmallPtrSetIterator<VNInfo *> It2 = It1;
  827. for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
  828. It2 = It1;
  829. for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
  830. if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
  831. continue;
  832. MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
  833. MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
  834. if (MBB1 == MBB2) {
  835. DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
  836. } else if (MDT.dominates(MBB1, MBB2)) {
  837. DominatedVNIs.insert(*It2);
  838. } else if (MDT.dominates(MBB2, MBB1)) {
  839. DominatedVNIs.insert(*It1);
  840. }
  841. }
  842. }
  843. if (!DominatedVNIs.empty()) {
  844. forceRecompute(0, *ParentVNI);
  845. append_range(BackCopies, DominatedVNIs);
  846. DominatedVNIs.clear();
  847. }
  848. }
  849. }
  850. /// For SM_Size mode, find a common dominator for all the back-copies for
  851. /// the same ParentVNI and hoist the backcopies to the dominator BB.
  852. /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
  853. /// to do the hoisting, simply remove the dominated backcopies for the same
  854. /// ParentVNI.
  855. void SplitEditor::hoistCopies() {
  856. // Get the complement interval, always RegIdx 0.
  857. LiveInterval *LI = &LIS.getInterval(Edit->get(0));
  858. LiveInterval *Parent = &Edit->getParent();
  859. // Track the nearest common dominator for all back-copies for each ParentVNI,
  860. // indexed by ParentVNI->id.
  861. using DomPair = std::pair<MachineBasicBlock *, SlotIndex>;
  862. SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
  863. // The total cost of all the back-copies for each ParentVNI.
  864. SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
  865. // The ParentVNI->id set for which hoisting back-copies are not beneficial
  866. // for Speed.
  867. DenseSet<unsigned> NotToHoistSet;
  868. // Find the nearest common dominator for parent values with multiple
  869. // back-copies. If a single back-copy dominates, put it in DomPair.second.
  870. for (VNInfo *VNI : LI->valnos) {
  871. if (VNI->isUnused())
  872. continue;
  873. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
  874. assert(ParentVNI && "Parent not live at complement def");
  875. // Don't hoist remats. The complement is probably going to disappear
  876. // completely anyway.
  877. if (Edit->didRematerialize(ParentVNI))
  878. continue;
  879. MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
  880. DomPair &Dom = NearestDom[ParentVNI->id];
  881. // Keep directly defined parent values. This is either a PHI or an
  882. // instruction in the complement range. All other copies of ParentVNI
  883. // should be eliminated.
  884. if (VNI->def == ParentVNI->def) {
  885. LLVM_DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
  886. Dom = DomPair(ValMBB, VNI->def);
  887. continue;
  888. }
  889. // Skip the singly mapped values. There is nothing to gain from hoisting a
  890. // single back-copy.
  891. if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
  892. LLVM_DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
  893. continue;
  894. }
  895. if (!Dom.first) {
  896. // First time we see ParentVNI. VNI dominates itself.
  897. Dom = DomPair(ValMBB, VNI->def);
  898. } else if (Dom.first == ValMBB) {
  899. // Two defs in the same block. Pick the earlier def.
  900. if (!Dom.second.isValid() || VNI->def < Dom.second)
  901. Dom.second = VNI->def;
  902. } else {
  903. // Different basic blocks. Check if one dominates.
  904. MachineBasicBlock *Near =
  905. MDT.findNearestCommonDominator(Dom.first, ValMBB);
  906. if (Near == ValMBB)
  907. // Def ValMBB dominates.
  908. Dom = DomPair(ValMBB, VNI->def);
  909. else if (Near != Dom.first)
  910. // None dominate. Hoist to common dominator, need new def.
  911. Dom = DomPair(Near, SlotIndex());
  912. Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
  913. }
  914. LLVM_DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@'
  915. << VNI->def << " for parent " << ParentVNI->id << '@'
  916. << ParentVNI->def << " hoist to "
  917. << printMBBReference(*Dom.first) << ' ' << Dom.second
  918. << '\n');
  919. }
  920. // Insert the hoisted copies.
  921. for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
  922. DomPair &Dom = NearestDom[i];
  923. if (!Dom.first || Dom.second.isValid())
  924. continue;
  925. // This value needs a hoisted copy inserted at the end of Dom.first.
  926. VNInfo *ParentVNI = Parent->getValNumInfo(i);
  927. MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
  928. // Get a less loopy dominator than Dom.first.
  929. Dom.first = findShallowDominator(Dom.first, DefMBB);
  930. if (SpillMode == SM_Speed &&
  931. MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
  932. NotToHoistSet.insert(ParentVNI->id);
  933. continue;
  934. }
  935. SlotIndex LSP = SA.getLastSplitPoint(Dom.first);
  936. if (LSP <= ParentVNI->def) {
  937. NotToHoistSet.insert(ParentVNI->id);
  938. continue;
  939. }
  940. Dom.second = defFromParent(0, ParentVNI, LSP, *Dom.first,
  941. SA.getLastSplitPointIter(Dom.first))->def;
  942. }
  943. // Remove redundant back-copies that are now known to be dominated by another
  944. // def with the same value.
  945. SmallVector<VNInfo*, 8> BackCopies;
  946. for (VNInfo *VNI : LI->valnos) {
  947. if (VNI->isUnused())
  948. continue;
  949. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
  950. const DomPair &Dom = NearestDom[ParentVNI->id];
  951. if (!Dom.first || Dom.second == VNI->def ||
  952. NotToHoistSet.count(ParentVNI->id))
  953. continue;
  954. BackCopies.push_back(VNI);
  955. forceRecompute(0, *ParentVNI);
  956. }
  957. // If it is not beneficial to hoist all the BackCopies, simply remove
  958. // redundant BackCopies in speed mode.
  959. if (SpillMode == SM_Speed && !NotToHoistSet.empty())
  960. computeRedundantBackCopies(NotToHoistSet, BackCopies);
  961. removeBackCopies(BackCopies);
  962. }
  963. /// transferValues - Transfer all possible values to the new live ranges.
  964. /// Values that were rematerialized are left alone, they need LICalc.extend().
  965. bool SplitEditor::transferValues() {
  966. bool Skipped = false;
  967. RegAssignMap::const_iterator AssignI = RegAssign.begin();
  968. for (const LiveRange::Segment &S : Edit->getParent()) {
  969. LLVM_DEBUG(dbgs() << " blit " << S << ':');
  970. VNInfo *ParentVNI = S.valno;
  971. // RegAssign has holes where RegIdx 0 should be used.
  972. SlotIndex Start = S.start;
  973. AssignI.advanceTo(Start);
  974. do {
  975. unsigned RegIdx;
  976. SlotIndex End = S.end;
  977. if (!AssignI.valid()) {
  978. RegIdx = 0;
  979. } else if (AssignI.start() <= Start) {
  980. RegIdx = AssignI.value();
  981. if (AssignI.stop() < End) {
  982. End = AssignI.stop();
  983. ++AssignI;
  984. }
  985. } else {
  986. RegIdx = 0;
  987. End = std::min(End, AssignI.start());
  988. }
  989. // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
  990. LLVM_DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx << '('
  991. << printReg(Edit->get(RegIdx)) << ')');
  992. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  993. // Check for a simply defined value that can be blitted directly.
  994. ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
  995. if (VNInfo *VNI = VFP.getPointer()) {
  996. LLVM_DEBUG(dbgs() << ':' << VNI->id);
  997. LI.addSegment(LiveInterval::Segment(Start, End, VNI));
  998. Start = End;
  999. continue;
  1000. }
  1001. // Skip values with forced recomputation.
  1002. if (VFP.getInt()) {
  1003. LLVM_DEBUG(dbgs() << "(recalc)");
  1004. Skipped = true;
  1005. Start = End;
  1006. continue;
  1007. }
  1008. LiveIntervalCalc &LIC = getLICalc(RegIdx);
  1009. // This value has multiple defs in RegIdx, but it wasn't rematerialized,
  1010. // so the live range is accurate. Add live-in blocks in [Start;End) to the
  1011. // LiveInBlocks.
  1012. MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
  1013. SlotIndex BlockStart, BlockEnd;
  1014. std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
  1015. // The first block may be live-in, or it may have its own def.
  1016. if (Start != BlockStart) {
  1017. VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
  1018. assert(VNI && "Missing def for complex mapped value");
  1019. LLVM_DEBUG(dbgs() << ':' << VNI->id << "*" << printMBBReference(*MBB));
  1020. // MBB has its own def. Is it also live-out?
  1021. if (BlockEnd <= End)
  1022. LIC.setLiveOutValue(&*MBB, VNI);
  1023. // Skip to the next block for live-in.
  1024. ++MBB;
  1025. BlockStart = BlockEnd;
  1026. }
  1027. // Handle the live-in blocks covered by [Start;End).
  1028. assert(Start <= BlockStart && "Expected live-in block");
  1029. while (BlockStart < End) {
  1030. LLVM_DEBUG(dbgs() << ">" << printMBBReference(*MBB));
  1031. BlockEnd = LIS.getMBBEndIdx(&*MBB);
  1032. if (BlockStart == ParentVNI->def) {
  1033. // This block has the def of a parent PHI, so it isn't live-in.
  1034. assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
  1035. VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
  1036. assert(VNI && "Missing def for complex mapped parent PHI");
  1037. if (End >= BlockEnd)
  1038. LIC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
  1039. } else {
  1040. // This block needs a live-in value. The last block covered may not
  1041. // be live-out.
  1042. if (End < BlockEnd)
  1043. LIC.addLiveInBlock(LI, MDT[&*MBB], End);
  1044. else {
  1045. // Live-through, and we don't know the value.
  1046. LIC.addLiveInBlock(LI, MDT[&*MBB]);
  1047. LIC.setLiveOutValue(&*MBB, nullptr);
  1048. }
  1049. }
  1050. BlockStart = BlockEnd;
  1051. ++MBB;
  1052. }
  1053. Start = End;
  1054. } while (Start != S.end);
  1055. LLVM_DEBUG(dbgs() << '\n');
  1056. }
  1057. LICalc[0].calculateValues();
  1058. if (SpillMode)
  1059. LICalc[1].calculateValues();
  1060. return Skipped;
  1061. }
  1062. static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
  1063. const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
  1064. if (Seg == nullptr)
  1065. return true;
  1066. if (Seg->end != Def.getDeadSlot())
  1067. return false;
  1068. // This is a dead PHI. Remove it.
  1069. LR.removeSegment(*Seg, true);
  1070. return true;
  1071. }
  1072. void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC,
  1073. LiveRange &LR, LaneBitmask LM,
  1074. ArrayRef<SlotIndex> Undefs) {
  1075. for (MachineBasicBlock *P : B.predecessors()) {
  1076. SlotIndex End = LIS.getMBBEndIdx(P);
  1077. SlotIndex LastUse = End.getPrevSlot();
  1078. // The predecessor may not have a live-out value. That is OK, like an
  1079. // undef PHI operand.
  1080. LiveInterval &PLI = Edit->getParent();
  1081. // Need the cast because the inputs to ?: would otherwise be deemed
  1082. // "incompatible": SubRange vs LiveInterval.
  1083. LiveRange &PSR = !LM.all() ? getSubRangeForMaskExact(LM, PLI)
  1084. : static_cast<LiveRange &>(PLI);
  1085. if (PSR.liveAt(LastUse))
  1086. LIC.extend(LR, End, /*PhysReg=*/0, Undefs);
  1087. }
  1088. }
  1089. void SplitEditor::extendPHIKillRanges() {
  1090. // Extend live ranges to be live-out for successor PHI values.
  1091. // Visit each PHI def slot in the parent live interval. If the def is dead,
  1092. // remove it. Otherwise, extend the live interval to reach the end indexes
  1093. // of all predecessor blocks.
  1094. LiveInterval &ParentLI = Edit->getParent();
  1095. for (const VNInfo *V : ParentLI.valnos) {
  1096. if (V->isUnused() || !V->isPHIDef())
  1097. continue;
  1098. unsigned RegIdx = RegAssign.lookup(V->def);
  1099. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  1100. LiveIntervalCalc &LIC = getLICalc(RegIdx);
  1101. MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
  1102. if (!removeDeadSegment(V->def, LI))
  1103. extendPHIRange(B, LIC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
  1104. }
  1105. SmallVector<SlotIndex, 4> Undefs;
  1106. LiveIntervalCalc SubLIC;
  1107. for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
  1108. for (const VNInfo *V : PS.valnos) {
  1109. if (V->isUnused() || !V->isPHIDef())
  1110. continue;
  1111. unsigned RegIdx = RegAssign.lookup(V->def);
  1112. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  1113. LiveInterval::SubRange &S = getSubRangeForMaskExact(PS.LaneMask, LI);
  1114. if (removeDeadSegment(V->def, S))
  1115. continue;
  1116. MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
  1117. SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
  1118. &LIS.getVNInfoAllocator());
  1119. Undefs.clear();
  1120. LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
  1121. extendPHIRange(B, SubLIC, S, PS.LaneMask, Undefs);
  1122. }
  1123. }
  1124. }
  1125. /// rewriteAssigned - Rewrite all uses of Edit->getReg().
  1126. void SplitEditor::rewriteAssigned(bool ExtendRanges) {
  1127. struct ExtPoint {
  1128. ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
  1129. : MO(O), RegIdx(R), Next(N) {}
  1130. MachineOperand MO;
  1131. unsigned RegIdx;
  1132. SlotIndex Next;
  1133. };
  1134. SmallVector<ExtPoint,4> ExtPoints;
  1135. for (MachineOperand &MO :
  1136. llvm::make_early_inc_range(MRI.reg_operands(Edit->getReg()))) {
  1137. MachineInstr *MI = MO.getParent();
  1138. // LiveDebugVariables should have handled all DBG_VALUE instructions.
  1139. if (MI->isDebugValue()) {
  1140. LLVM_DEBUG(dbgs() << "Zapping " << *MI);
  1141. MO.setReg(0);
  1142. continue;
  1143. }
  1144. // <undef> operands don't really read the register, so it doesn't matter
  1145. // which register we choose. When the use operand is tied to a def, we must
  1146. // use the same register as the def, so just do that always.
  1147. SlotIndex Idx = LIS.getInstructionIndex(*MI);
  1148. if (MO.isDef() || MO.isUndef())
  1149. Idx = Idx.getRegSlot(MO.isEarlyClobber());
  1150. // Rewrite to the mapped register at Idx.
  1151. unsigned RegIdx = RegAssign.lookup(Idx);
  1152. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  1153. MO.setReg(LI.reg());
  1154. LLVM_DEBUG(dbgs() << " rewr " << printMBBReference(*MI->getParent())
  1155. << '\t' << Idx << ':' << RegIdx << '\t' << *MI);
  1156. // Extend liveness to Idx if the instruction reads reg.
  1157. if (!ExtendRanges || MO.isUndef())
  1158. continue;
  1159. // Skip instructions that don't read Reg.
  1160. if (MO.isDef()) {
  1161. if (!MO.getSubReg() && !MO.isEarlyClobber())
  1162. continue;
  1163. // We may want to extend a live range for a partial redef, or for a use
  1164. // tied to an early clobber.
  1165. Idx = Idx.getPrevSlot();
  1166. if (!Edit->getParent().liveAt(Idx))
  1167. continue;
  1168. } else
  1169. Idx = Idx.getRegSlot(true);
  1170. SlotIndex Next = Idx.getNextSlot();
  1171. if (LI.hasSubRanges()) {
  1172. // We have to delay extending subranges until we have seen all operands
  1173. // defining the register. This is because a <def,read-undef> operand
  1174. // will create an "undef" point, and we cannot extend any subranges
  1175. // until all of them have been accounted for.
  1176. if (MO.isUse())
  1177. ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
  1178. } else {
  1179. LiveIntervalCalc &LIC = getLICalc(RegIdx);
  1180. LIC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
  1181. }
  1182. }
  1183. for (ExtPoint &EP : ExtPoints) {
  1184. LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
  1185. assert(LI.hasSubRanges());
  1186. LiveIntervalCalc SubLIC;
  1187. Register Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
  1188. LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
  1189. : MRI.getMaxLaneMaskForVReg(Reg);
  1190. for (LiveInterval::SubRange &S : LI.subranges()) {
  1191. if ((S.LaneMask & LM).none())
  1192. continue;
  1193. // The problem here can be that the new register may have been created
  1194. // for a partially defined original register. For example:
  1195. // %0:subreg_hireg<def,read-undef> = ...
  1196. // ...
  1197. // %1 = COPY %0
  1198. if (S.empty())
  1199. continue;
  1200. SubLIC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
  1201. &LIS.getVNInfoAllocator());
  1202. SmallVector<SlotIndex, 4> Undefs;
  1203. LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
  1204. SubLIC.extend(S, EP.Next, 0, Undefs);
  1205. }
  1206. }
  1207. for (Register R : *Edit) {
  1208. LiveInterval &LI = LIS.getInterval(R);
  1209. if (!LI.hasSubRanges())
  1210. continue;
  1211. LI.clear();
  1212. LI.removeEmptySubRanges();
  1213. LIS.constructMainRangeFromSubranges(LI);
  1214. }
  1215. }
  1216. void SplitEditor::deleteRematVictims() {
  1217. SmallVector<MachineInstr*, 8> Dead;
  1218. for (const Register &R : *Edit) {
  1219. LiveInterval *LI = &LIS.getInterval(R);
  1220. for (const LiveRange::Segment &S : LI->segments) {
  1221. // Dead defs end at the dead slot.
  1222. if (S.end != S.valno->def.getDeadSlot())
  1223. continue;
  1224. if (S.valno->isPHIDef())
  1225. continue;
  1226. MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
  1227. assert(MI && "Missing instruction for dead def");
  1228. MI->addRegisterDead(LI->reg(), &TRI);
  1229. if (!MI->allDefsAreDead())
  1230. continue;
  1231. LLVM_DEBUG(dbgs() << "All defs dead: " << *MI);
  1232. Dead.push_back(MI);
  1233. }
  1234. }
  1235. if (Dead.empty())
  1236. return;
  1237. Edit->eliminateDeadDefs(Dead, None, &AA);
  1238. }
  1239. void SplitEditor::forceRecomputeVNI(const VNInfo &ParentVNI) {
  1240. // Fast-path for common case.
  1241. if (!ParentVNI.isPHIDef()) {
  1242. for (unsigned I = 0, E = Edit->size(); I != E; ++I)
  1243. forceRecompute(I, ParentVNI);
  1244. return;
  1245. }
  1246. // Trace value through phis.
  1247. SmallPtrSet<const VNInfo *, 8> Visited; ///< whether VNI was/is in worklist.
  1248. SmallVector<const VNInfo *, 4> WorkList;
  1249. Visited.insert(&ParentVNI);
  1250. WorkList.push_back(&ParentVNI);
  1251. const LiveInterval &ParentLI = Edit->getParent();
  1252. const SlotIndexes &Indexes = *LIS.getSlotIndexes();
  1253. do {
  1254. const VNInfo &VNI = *WorkList.back();
  1255. WorkList.pop_back();
  1256. for (unsigned I = 0, E = Edit->size(); I != E; ++I)
  1257. forceRecompute(I, VNI);
  1258. if (!VNI.isPHIDef())
  1259. continue;
  1260. MachineBasicBlock &MBB = *Indexes.getMBBFromIndex(VNI.def);
  1261. for (const MachineBasicBlock *Pred : MBB.predecessors()) {
  1262. SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
  1263. VNInfo *PredVNI = ParentLI.getVNInfoBefore(PredEnd);
  1264. assert(PredVNI && "Value available in PhiVNI predecessor");
  1265. if (Visited.insert(PredVNI).second)
  1266. WorkList.push_back(PredVNI);
  1267. }
  1268. } while(!WorkList.empty());
  1269. }
  1270. void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
  1271. ++NumFinished;
  1272. // At this point, the live intervals in Edit contain VNInfos corresponding to
  1273. // the inserted copies.
  1274. // Add the original defs from the parent interval.
  1275. for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
  1276. if (ParentVNI->isUnused())
  1277. continue;
  1278. unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
  1279. defValue(RegIdx, ParentVNI, ParentVNI->def, true);
  1280. // Force rematted values to be recomputed everywhere.
  1281. // The new live ranges may be truncated.
  1282. if (Edit->didRematerialize(ParentVNI))
  1283. forceRecomputeVNI(*ParentVNI);
  1284. }
  1285. // Hoist back-copies to the complement interval when in spill mode.
  1286. switch (SpillMode) {
  1287. case SM_Partition:
  1288. // Leave all back-copies as is.
  1289. break;
  1290. case SM_Size:
  1291. case SM_Speed:
  1292. // hoistCopies will behave differently between size and speed.
  1293. hoistCopies();
  1294. }
  1295. // Transfer the simply mapped values, check if any are skipped.
  1296. bool Skipped = transferValues();
  1297. // Rewrite virtual registers, possibly extending ranges.
  1298. rewriteAssigned(Skipped);
  1299. if (Skipped)
  1300. extendPHIKillRanges();
  1301. else
  1302. ++NumSimple;
  1303. // Delete defs that were rematted everywhere.
  1304. if (Skipped)
  1305. deleteRematVictims();
  1306. // Get rid of unused values and set phi-kill flags.
  1307. for (Register Reg : *Edit) {
  1308. LiveInterval &LI = LIS.getInterval(Reg);
  1309. LI.removeEmptySubRanges();
  1310. LI.RenumberValues();
  1311. }
  1312. // Provide a reverse mapping from original indices to Edit ranges.
  1313. if (LRMap) {
  1314. LRMap->clear();
  1315. for (unsigned i = 0, e = Edit->size(); i != e; ++i)
  1316. LRMap->push_back(i);
  1317. }
  1318. // Now check if any registers were separated into multiple components.
  1319. ConnectedVNInfoEqClasses ConEQ(LIS);
  1320. for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
  1321. // Don't use iterators, they are invalidated by create() below.
  1322. Register VReg = Edit->get(i);
  1323. LiveInterval &LI = LIS.getInterval(VReg);
  1324. SmallVector<LiveInterval*, 8> SplitLIs;
  1325. LIS.splitSeparateComponents(LI, SplitLIs);
  1326. Register Original = VRM.getOriginal(VReg);
  1327. for (LiveInterval *SplitLI : SplitLIs)
  1328. VRM.setIsSplitFromReg(SplitLI->reg(), Original);
  1329. // The new intervals all map back to i.
  1330. if (LRMap)
  1331. LRMap->resize(Edit->size(), i);
  1332. }
  1333. // Calculate spill weight and allocation hints for new intervals.
  1334. Edit->calculateRegClassAndHint(VRM.getMachineFunction(), VRAI);
  1335. assert(!LRMap || LRMap->size() == Edit->size());
  1336. }
  1337. //===----------------------------------------------------------------------===//
  1338. // Single Block Splitting
  1339. //===----------------------------------------------------------------------===//
  1340. bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
  1341. bool SingleInstrs) const {
  1342. // Always split for multiple instructions.
  1343. if (!BI.isOneInstr())
  1344. return true;
  1345. // Don't split for single instructions unless explicitly requested.
  1346. if (!SingleInstrs)
  1347. return false;
  1348. // Splitting a live-through range always makes progress.
  1349. if (BI.LiveIn && BI.LiveOut)
  1350. return true;
  1351. // No point in isolating a copy. It has no register class constraints.
  1352. if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
  1353. return false;
  1354. // Finally, don't isolate an end point that was created by earlier splits.
  1355. return isOriginalEndpoint(BI.FirstInstr);
  1356. }
  1357. void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
  1358. openIntv();
  1359. SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB);
  1360. SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
  1361. LastSplitPoint));
  1362. if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
  1363. useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
  1364. } else {
  1365. // The last use is after the last valid split point.
  1366. SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
  1367. useIntv(SegStart, SegStop);
  1368. overlapIntv(SegStop, BI.LastInstr);
  1369. }
  1370. }
  1371. //===----------------------------------------------------------------------===//
  1372. // Global Live Range Splitting Support
  1373. //===----------------------------------------------------------------------===//
  1374. // These methods support a method of global live range splitting that uses a
  1375. // global algorithm to decide intervals for CFG edges. They will insert split
  1376. // points and color intervals in basic blocks while avoiding interference.
  1377. //
  1378. // Note that splitSingleBlock is also useful for blocks where both CFG edges
  1379. // are on the stack.
  1380. void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
  1381. unsigned IntvIn, SlotIndex LeaveBefore,
  1382. unsigned IntvOut, SlotIndex EnterAfter){
  1383. SlotIndex Start, Stop;
  1384. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
  1385. LLVM_DEBUG(dbgs() << "%bb." << MBBNum << " [" << Start << ';' << Stop
  1386. << ") intf " << LeaveBefore << '-' << EnterAfter
  1387. << ", live-through " << IntvIn << " -> " << IntvOut);
  1388. assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
  1389. assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
  1390. assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
  1391. assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
  1392. MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
  1393. if (!IntvOut) {
  1394. LLVM_DEBUG(dbgs() << ", spill on entry.\n");
  1395. //
  1396. // <<<<<<<<< Possible LeaveBefore interference.
  1397. // |-----------| Live through.
  1398. // -____________ Spill on entry.
  1399. //
  1400. selectIntv(IntvIn);
  1401. SlotIndex Idx = leaveIntvAtTop(*MBB);
  1402. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1403. (void)Idx;
  1404. return;
  1405. }
  1406. if (!IntvIn) {
  1407. LLVM_DEBUG(dbgs() << ", reload on exit.\n");
  1408. //
  1409. // >>>>>>> Possible EnterAfter interference.
  1410. // |-----------| Live through.
  1411. // ___________-- Reload on exit.
  1412. //
  1413. selectIntv(IntvOut);
  1414. SlotIndex Idx = enterIntvAtEnd(*MBB);
  1415. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1416. (void)Idx;
  1417. return;
  1418. }
  1419. if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
  1420. LLVM_DEBUG(dbgs() << ", straight through.\n");
  1421. //
  1422. // |-----------| Live through.
  1423. // ------------- Straight through, same intv, no interference.
  1424. //
  1425. selectIntv(IntvOut);
  1426. useIntv(Start, Stop);
  1427. return;
  1428. }
  1429. // We cannot legally insert splits after LSP.
  1430. SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
  1431. assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
  1432. if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
  1433. LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
  1434. LLVM_DEBUG(dbgs() << ", switch avoiding interference.\n");
  1435. //
  1436. // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
  1437. // |-----------| Live through.
  1438. // ------======= Switch intervals between interference.
  1439. //
  1440. selectIntv(IntvOut);
  1441. SlotIndex Idx;
  1442. if (LeaveBefore && LeaveBefore < LSP) {
  1443. Idx = enterIntvBefore(LeaveBefore);
  1444. useIntv(Idx, Stop);
  1445. } else {
  1446. Idx = enterIntvAtEnd(*MBB);
  1447. }
  1448. selectIntv(IntvIn);
  1449. useIntv(Start, Idx);
  1450. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1451. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1452. return;
  1453. }
  1454. LLVM_DEBUG(dbgs() << ", create local intv for interference.\n");
  1455. //
  1456. // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
  1457. // |-----------| Live through.
  1458. // ==---------== Switch intervals before/after interference.
  1459. //
  1460. assert(LeaveBefore <= EnterAfter && "Missed case");
  1461. selectIntv(IntvOut);
  1462. SlotIndex Idx = enterIntvAfter(EnterAfter);
  1463. useIntv(Idx, Stop);
  1464. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1465. selectIntv(IntvIn);
  1466. Idx = leaveIntvBefore(LeaveBefore);
  1467. useIntv(Start, Idx);
  1468. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1469. }
  1470. void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
  1471. unsigned IntvIn, SlotIndex LeaveBefore) {
  1472. SlotIndex Start, Stop;
  1473. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
  1474. LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
  1475. << Stop << "), uses " << BI.FirstInstr << '-'
  1476. << BI.LastInstr << ", reg-in " << IntvIn
  1477. << ", leave before " << LeaveBefore
  1478. << (BI.LiveOut ? ", stack-out" : ", killed in block"));
  1479. assert(IntvIn && "Must have register in");
  1480. assert(BI.LiveIn && "Must be live-in");
  1481. assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
  1482. if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
  1483. LLVM_DEBUG(dbgs() << " before interference.\n");
  1484. //
  1485. // <<< Interference after kill.
  1486. // |---o---x | Killed in block.
  1487. // ========= Use IntvIn everywhere.
  1488. //
  1489. selectIntv(IntvIn);
  1490. useIntv(Start, BI.LastInstr);
  1491. return;
  1492. }
  1493. SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
  1494. if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
  1495. //
  1496. // <<< Possible interference after last use.
  1497. // |---o---o---| Live-out on stack.
  1498. // =========____ Leave IntvIn after last use.
  1499. //
  1500. // < Interference after last use.
  1501. // |---o---o--o| Live-out on stack, late last use.
  1502. // ============ Copy to stack after LSP, overlap IntvIn.
  1503. // \_____ Stack interval is live-out.
  1504. //
  1505. if (BI.LastInstr < LSP) {
  1506. LLVM_DEBUG(dbgs() << ", spill after last use before interference.\n");
  1507. selectIntv(IntvIn);
  1508. SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
  1509. useIntv(Start, Idx);
  1510. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1511. } else {
  1512. LLVM_DEBUG(dbgs() << ", spill before last split point.\n");
  1513. selectIntv(IntvIn);
  1514. SlotIndex Idx = leaveIntvBefore(LSP);
  1515. overlapIntv(Idx, BI.LastInstr);
  1516. useIntv(Start, Idx);
  1517. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1518. }
  1519. return;
  1520. }
  1521. // The interference is overlapping somewhere we wanted to use IntvIn. That
  1522. // means we need to create a local interval that can be allocated a
  1523. // different register.
  1524. unsigned LocalIntv = openIntv();
  1525. (void)LocalIntv;
  1526. LLVM_DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
  1527. if (!BI.LiveOut || BI.LastInstr < LSP) {
  1528. //
  1529. // <<<<<<< Interference overlapping uses.
  1530. // |---o---o---| Live-out on stack.
  1531. // =====----____ Leave IntvIn before interference, then spill.
  1532. //
  1533. SlotIndex To = leaveIntvAfter(BI.LastInstr);
  1534. SlotIndex From = enterIntvBefore(LeaveBefore);
  1535. useIntv(From, To);
  1536. selectIntv(IntvIn);
  1537. useIntv(Start, From);
  1538. assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
  1539. return;
  1540. }
  1541. // <<<<<<< Interference overlapping uses.
  1542. // |---o---o--o| Live-out on stack, late last use.
  1543. // =====------- Copy to stack before LSP, overlap LocalIntv.
  1544. // \_____ Stack interval is live-out.
  1545. //
  1546. SlotIndex To = leaveIntvBefore(LSP);
  1547. overlapIntv(To, BI.LastInstr);
  1548. SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
  1549. useIntv(From, To);
  1550. selectIntv(IntvIn);
  1551. useIntv(Start, From);
  1552. assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
  1553. }
  1554. void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
  1555. unsigned IntvOut, SlotIndex EnterAfter) {
  1556. SlotIndex Start, Stop;
  1557. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
  1558. LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " [" << Start << ';'
  1559. << Stop << "), uses " << BI.FirstInstr << '-'
  1560. << BI.LastInstr << ", reg-out " << IntvOut
  1561. << ", enter after " << EnterAfter
  1562. << (BI.LiveIn ? ", stack-in" : ", defined in block"));
  1563. SlotIndex LSP = SA.getLastSplitPoint(BI.MBB);
  1564. assert(IntvOut && "Must have register out");
  1565. assert(BI.LiveOut && "Must be live-out");
  1566. assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
  1567. if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
  1568. LLVM_DEBUG(dbgs() << " after interference.\n");
  1569. //
  1570. // >>>> Interference before def.
  1571. // | o---o---| Defined in block.
  1572. // ========= Use IntvOut everywhere.
  1573. //
  1574. selectIntv(IntvOut);
  1575. useIntv(BI.FirstInstr, Stop);
  1576. return;
  1577. }
  1578. if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
  1579. LLVM_DEBUG(dbgs() << ", reload after interference.\n");
  1580. //
  1581. // >>>> Interference before def.
  1582. // |---o---o---| Live-through, stack-in.
  1583. // ____========= Enter IntvOut before first use.
  1584. //
  1585. selectIntv(IntvOut);
  1586. SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
  1587. useIntv(Idx, Stop);
  1588. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1589. return;
  1590. }
  1591. // The interference is overlapping somewhere we wanted to use IntvOut. That
  1592. // means we need to create a local interval that can be allocated a
  1593. // different register.
  1594. LLVM_DEBUG(dbgs() << ", interference overlaps uses.\n");
  1595. //
  1596. // >>>>>>> Interference overlapping uses.
  1597. // |---o---o---| Live-through, stack-in.
  1598. // ____---====== Create local interval for interference range.
  1599. //
  1600. selectIntv(IntvOut);
  1601. SlotIndex Idx = enterIntvAfter(EnterAfter);
  1602. useIntv(Idx, Stop);
  1603. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1604. openIntv();
  1605. SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
  1606. useIntv(From, Idx);
  1607. }
  1608. void SplitAnalysis::BlockInfo::print(raw_ostream &OS) const {
  1609. OS << "{" << printMBBReference(*MBB) << ", "
  1610. << "uses " << FirstInstr << " to " << LastInstr << ", "
  1611. << "1st def " << FirstDef << ", "
  1612. << (LiveIn ? "live in" : "dead in") << ", "
  1613. << (LiveOut ? "live out" : "dead out") << "}";
  1614. }
  1615. void SplitAnalysis::BlockInfo::dump() const {
  1616. print(dbgs());
  1617. dbgs() << "\n";
  1618. }