SplitKit.cpp 63 KB

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