SplitKit.cpp 68 KB

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