SplitKit.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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. DestLI.createDeadDef(Def, Allocator);
  433. } else {
  434. CopyMI->bundleWithPred();
  435. }
  436. LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubIdx);
  437. DestLI.refineSubRanges(Allocator, LaneMask,
  438. [Def, &Allocator](LiveInterval::SubRange& SR) {
  439. SR.createDeadDef(Def, Allocator);
  440. });
  441. return Def;
  442. }
  443. SlotIndex SplitEditor::buildCopy(unsigned FromReg, unsigned ToReg,
  444. LaneBitmask LaneMask, MachineBasicBlock &MBB,
  445. MachineBasicBlock::iterator InsertBefore, bool Late, unsigned RegIdx) {
  446. const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
  447. if (LaneMask.all() || LaneMask == MRI.getMaxLaneMaskForVReg(FromReg)) {
  448. // The full vreg is copied.
  449. MachineInstr *CopyMI =
  450. BuildMI(MBB, InsertBefore, DebugLoc(), Desc, ToReg).addReg(FromReg);
  451. SlotIndexes &Indexes = *LIS.getSlotIndexes();
  452. return Indexes.insertMachineInstrInMaps(*CopyMI, Late).getRegSlot();
  453. }
  454. // Only a subset of lanes needs to be copied. The following is a simple
  455. // heuristic to construct a sequence of COPYs. We could add a target
  456. // specific callback if this turns out to be suboptimal.
  457. LiveInterval &DestLI = LIS.getInterval(Edit->get(RegIdx));
  458. // First pass: Try to find a perfectly matching subregister index. If none
  459. // exists find the one covering the most lanemask bits.
  460. SmallVector<unsigned, 8> PossibleIndexes;
  461. unsigned BestIdx = 0;
  462. unsigned BestCover = 0;
  463. const TargetRegisterClass *RC = MRI.getRegClass(FromReg);
  464. assert(RC == MRI.getRegClass(ToReg) && "Should have same reg class");
  465. for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) {
  466. // Is this index even compatible with the given class?
  467. if (TRI.getSubClassWithSubReg(RC, Idx) != RC)
  468. continue;
  469. LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
  470. // Early exit if we found a perfect match.
  471. if (SubRegMask == LaneMask) {
  472. BestIdx = Idx;
  473. break;
  474. }
  475. // The index must not cover any lanes outside \p LaneMask.
  476. if ((SubRegMask & ~LaneMask).any())
  477. continue;
  478. unsigned PopCount = countPopulation(SubRegMask.getAsInteger());
  479. PossibleIndexes.push_back(Idx);
  480. if (PopCount > BestCover) {
  481. BestCover = PopCount;
  482. BestIdx = Idx;
  483. }
  484. }
  485. // Abort if we cannot possibly implement the COPY with the given indexes.
  486. if (BestIdx == 0)
  487. report_fatal_error("Impossible to implement partial COPY");
  488. SlotIndex Def = buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore,
  489. BestIdx, DestLI, Late, SlotIndex());
  490. // Greedy heuristic: Keep iterating keeping the best covering subreg index
  491. // each time.
  492. LaneBitmask LanesLeft =
  493. LaneMask & ~(TRI.getSubRegIndexLaneMask(BestCover));
  494. while (LanesLeft.any()) {
  495. unsigned BestIdx = 0;
  496. int BestCover = INT_MIN;
  497. for (unsigned Idx : PossibleIndexes) {
  498. LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
  499. // Early exit if we found a perfect match.
  500. if (SubRegMask == LanesLeft) {
  501. BestIdx = Idx;
  502. break;
  503. }
  504. // Try to cover as much of the remaining lanes as possible but
  505. // as few of the already covered lanes as possible.
  506. int Cover = countPopulation((SubRegMask & LanesLeft).getAsInteger())
  507. - countPopulation((SubRegMask & ~LanesLeft).getAsInteger());
  508. if (Cover > BestCover) {
  509. BestCover = Cover;
  510. BestIdx = Idx;
  511. }
  512. }
  513. if (BestIdx == 0)
  514. report_fatal_error("Impossible to implement partial COPY");
  515. buildSingleSubRegCopy(FromReg, ToReg, MBB, InsertBefore, BestIdx,
  516. DestLI, Late, Def);
  517. LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx);
  518. }
  519. return Def;
  520. }
  521. VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
  522. VNInfo *ParentVNI,
  523. SlotIndex UseIdx,
  524. MachineBasicBlock &MBB,
  525. MachineBasicBlock::iterator I) {
  526. SlotIndex Def;
  527. LiveInterval *LI = &LIS.getInterval(Edit->get(RegIdx));
  528. // We may be trying to avoid interference that ends at a deleted instruction,
  529. // so always begin RegIdx 0 early and all others late.
  530. bool Late = RegIdx != 0;
  531. // Attempt cheap-as-a-copy rematerialization.
  532. unsigned Original = VRM.getOriginal(Edit->get(RegIdx));
  533. LiveInterval &OrigLI = LIS.getInterval(Original);
  534. VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
  535. unsigned Reg = LI->reg;
  536. bool DidRemat = false;
  537. if (OrigVNI) {
  538. LiveRangeEdit::Remat RM(ParentVNI);
  539. RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
  540. if (Edit->canRematerializeAt(RM, OrigVNI, UseIdx, true)) {
  541. Def = Edit->rematerializeAt(MBB, I, Reg, RM, TRI, Late);
  542. ++NumRemats;
  543. DidRemat = true;
  544. }
  545. }
  546. if (!DidRemat) {
  547. LaneBitmask LaneMask;
  548. if (LI->hasSubRanges()) {
  549. LaneMask = LaneBitmask::getNone();
  550. for (LiveInterval::SubRange &S : LI->subranges())
  551. LaneMask |= S.LaneMask;
  552. } else {
  553. LaneMask = LaneBitmask::getAll();
  554. }
  555. ++NumCopies;
  556. Def = buildCopy(Edit->getReg(), Reg, LaneMask, MBB, I, Late, RegIdx);
  557. }
  558. // Define the value in Reg.
  559. return defValue(RegIdx, ParentVNI, Def, false);
  560. }
  561. /// Create a new virtual register and live interval.
  562. unsigned SplitEditor::openIntv() {
  563. // Create the complement as index 0.
  564. if (Edit->empty())
  565. Edit->createEmptyInterval();
  566. // Create the open interval.
  567. OpenIdx = Edit->size();
  568. Edit->createEmptyInterval();
  569. return OpenIdx;
  570. }
  571. void SplitEditor::selectIntv(unsigned Idx) {
  572. assert(Idx != 0 && "Cannot select the complement interval");
  573. assert(Idx < Edit->size() && "Can only select previously opened interval");
  574. DEBUG(dbgs() << " selectIntv " << OpenIdx << " -> " << Idx << '\n');
  575. OpenIdx = Idx;
  576. }
  577. SlotIndex SplitEditor::enterIntvBefore(SlotIndex Idx) {
  578. assert(OpenIdx && "openIntv not called before enterIntvBefore");
  579. DEBUG(dbgs() << " enterIntvBefore " << Idx);
  580. Idx = Idx.getBaseIndex();
  581. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
  582. if (!ParentVNI) {
  583. DEBUG(dbgs() << ": not live\n");
  584. return Idx;
  585. }
  586. DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  587. MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
  588. assert(MI && "enterIntvBefore called with invalid index");
  589. VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(), MI);
  590. return VNI->def;
  591. }
  592. SlotIndex SplitEditor::enterIntvAfter(SlotIndex Idx) {
  593. assert(OpenIdx && "openIntv not called before enterIntvAfter");
  594. DEBUG(dbgs() << " enterIntvAfter " << Idx);
  595. Idx = Idx.getBoundaryIndex();
  596. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
  597. if (!ParentVNI) {
  598. DEBUG(dbgs() << ": not live\n");
  599. return Idx;
  600. }
  601. DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  602. MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
  603. assert(MI && "enterIntvAfter called with invalid index");
  604. VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Idx, *MI->getParent(),
  605. std::next(MachineBasicBlock::iterator(MI)));
  606. return VNI->def;
  607. }
  608. SlotIndex SplitEditor::enterIntvAtEnd(MachineBasicBlock &MBB) {
  609. assert(OpenIdx && "openIntv not called before enterIntvAtEnd");
  610. SlotIndex End = LIS.getMBBEndIdx(&MBB);
  611. SlotIndex Last = End.getPrevSlot();
  612. DEBUG(dbgs() << " enterIntvAtEnd BB#" << MBB.getNumber() << ", " << Last);
  613. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Last);
  614. if (!ParentVNI) {
  615. DEBUG(dbgs() << ": not live\n");
  616. return End;
  617. }
  618. DEBUG(dbgs() << ": valno " << ParentVNI->id);
  619. VNInfo *VNI = defFromParent(OpenIdx, ParentVNI, Last, MBB,
  620. SA.getLastSplitPointIter(&MBB));
  621. RegAssign.insert(VNI->def, End, OpenIdx);
  622. DEBUG(dump());
  623. return VNI->def;
  624. }
  625. /// useIntv - indicate that all instructions in MBB should use OpenLI.
  626. void SplitEditor::useIntv(const MachineBasicBlock &MBB) {
  627. useIntv(LIS.getMBBStartIdx(&MBB), LIS.getMBBEndIdx(&MBB));
  628. }
  629. void SplitEditor::useIntv(SlotIndex Start, SlotIndex End) {
  630. assert(OpenIdx && "openIntv not called before useIntv");
  631. DEBUG(dbgs() << " useIntv [" << Start << ';' << End << "):");
  632. RegAssign.insert(Start, End, OpenIdx);
  633. DEBUG(dump());
  634. }
  635. SlotIndex SplitEditor::leaveIntvAfter(SlotIndex Idx) {
  636. assert(OpenIdx && "openIntv not called before leaveIntvAfter");
  637. DEBUG(dbgs() << " leaveIntvAfter " << Idx);
  638. // The interval must be live beyond the instruction at Idx.
  639. SlotIndex Boundary = Idx.getBoundaryIndex();
  640. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Boundary);
  641. if (!ParentVNI) {
  642. DEBUG(dbgs() << ": not live\n");
  643. return Boundary.getNextSlot();
  644. }
  645. DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  646. MachineInstr *MI = LIS.getInstructionFromIndex(Boundary);
  647. assert(MI && "No instruction at index");
  648. // In spill mode, make live ranges as short as possible by inserting the copy
  649. // before MI. This is only possible if that instruction doesn't redefine the
  650. // value. The inserted COPY is not a kill, and we don't need to recompute
  651. // the source live range. The spiller also won't try to hoist this copy.
  652. if (SpillMode && !SlotIndex::isSameInstr(ParentVNI->def, Idx) &&
  653. MI->readsVirtualRegister(Edit->getReg())) {
  654. forceRecompute(0, ParentVNI);
  655. defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
  656. return Idx;
  657. }
  658. VNInfo *VNI = defFromParent(0, ParentVNI, Boundary, *MI->getParent(),
  659. std::next(MachineBasicBlock::iterator(MI)));
  660. return VNI->def;
  661. }
  662. SlotIndex SplitEditor::leaveIntvBefore(SlotIndex Idx) {
  663. assert(OpenIdx && "openIntv not called before leaveIntvBefore");
  664. DEBUG(dbgs() << " leaveIntvBefore " << Idx);
  665. // The interval must be live into the instruction at Idx.
  666. Idx = Idx.getBaseIndex();
  667. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Idx);
  668. if (!ParentVNI) {
  669. DEBUG(dbgs() << ": not live\n");
  670. return Idx.getNextSlot();
  671. }
  672. DEBUG(dbgs() << ": valno " << ParentVNI->id << '\n');
  673. MachineInstr *MI = LIS.getInstructionFromIndex(Idx);
  674. assert(MI && "No instruction at index");
  675. VNInfo *VNI = defFromParent(0, ParentVNI, Idx, *MI->getParent(), MI);
  676. return VNI->def;
  677. }
  678. SlotIndex SplitEditor::leaveIntvAtTop(MachineBasicBlock &MBB) {
  679. assert(OpenIdx && "openIntv not called before leaveIntvAtTop");
  680. SlotIndex Start = LIS.getMBBStartIdx(&MBB);
  681. DEBUG(dbgs() << " leaveIntvAtTop BB#" << MBB.getNumber() << ", " << Start);
  682. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
  683. if (!ParentVNI) {
  684. 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. DEBUG(dump());
  691. return VNI->def;
  692. }
  693. void SplitEditor::overlapIntv(SlotIndex Start, SlotIndex End) {
  694. assert(OpenIdx && "openIntv not called before overlapIntv");
  695. const VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(Start);
  696. assert(ParentVNI == Edit->getParent().getVNInfoBefore(End) &&
  697. "Parent changes value in extended range");
  698. assert(LIS.getMBBFromIndex(Start) == LIS.getMBBFromIndex(End) &&
  699. "Range cannot span basic blocks");
  700. // The complement interval will be extended as needed by LRCalc.extend().
  701. if (ParentVNI)
  702. forceRecompute(0, ParentVNI);
  703. DEBUG(dbgs() << " overlapIntv [" << Start << ';' << End << "):");
  704. RegAssign.insert(Start, End, OpenIdx);
  705. DEBUG(dump());
  706. }
  707. //===----------------------------------------------------------------------===//
  708. // Spill modes
  709. //===----------------------------------------------------------------------===//
  710. void SplitEditor::removeBackCopies(SmallVectorImpl<VNInfo*> &Copies) {
  711. LiveInterval *LI = &LIS.getInterval(Edit->get(0));
  712. DEBUG(dbgs() << "Removing " << Copies.size() << " back-copies.\n");
  713. RegAssignMap::iterator AssignI;
  714. AssignI.setMap(RegAssign);
  715. for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
  716. SlotIndex Def = Copies[i]->def;
  717. MachineInstr *MI = LIS.getInstructionFromIndex(Def);
  718. assert(MI && "No instruction for back-copy");
  719. MachineBasicBlock *MBB = MI->getParent();
  720. MachineBasicBlock::iterator MBBI(MI);
  721. bool AtBegin;
  722. do AtBegin = MBBI == MBB->begin();
  723. while (!AtBegin && (--MBBI)->isDebugValue());
  724. DEBUG(dbgs() << "Removing " << Def << '\t' << *MI);
  725. LIS.removeVRegDefAt(*LI, Def);
  726. LIS.RemoveMachineInstrFromMaps(*MI);
  727. MI->eraseFromParent();
  728. // Adjust RegAssign if a register assignment is killed at Def. We want to
  729. // avoid calculating the live range of the source register if possible.
  730. AssignI.find(Def.getPrevSlot());
  731. if (!AssignI.valid() || AssignI.start() >= Def)
  732. continue;
  733. // If MI doesn't kill the assigned register, just leave it.
  734. if (AssignI.stop() != Def)
  735. continue;
  736. unsigned RegIdx = AssignI.value();
  737. if (AtBegin || !MBBI->readsVirtualRegister(Edit->getReg())) {
  738. DEBUG(dbgs() << " cannot find simple kill of RegIdx " << RegIdx << '\n');
  739. forceRecompute(RegIdx, Edit->getParent().getVNInfoAt(Def));
  740. } else {
  741. SlotIndex Kill = LIS.getInstructionIndex(*MBBI).getRegSlot();
  742. DEBUG(dbgs() << " move kill to " << Kill << '\t' << *MBBI);
  743. AssignI.setStop(Kill);
  744. }
  745. }
  746. }
  747. MachineBasicBlock*
  748. SplitEditor::findShallowDominator(MachineBasicBlock *MBB,
  749. MachineBasicBlock *DefMBB) {
  750. if (MBB == DefMBB)
  751. return MBB;
  752. assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");
  753. const MachineLoopInfo &Loops = SA.Loops;
  754. const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);
  755. MachineDomTreeNode *DefDomNode = MDT[DefMBB];
  756. // Best candidate so far.
  757. MachineBasicBlock *BestMBB = MBB;
  758. unsigned BestDepth = UINT_MAX;
  759. for (;;) {
  760. const MachineLoop *Loop = Loops.getLoopFor(MBB);
  761. // MBB isn't in a loop, it doesn't get any better. All dominators have a
  762. // higher frequency by definition.
  763. if (!Loop) {
  764. DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
  765. << MBB->getNumber() << " at depth 0\n");
  766. return MBB;
  767. }
  768. // We'll never be able to exit the DefLoop.
  769. if (Loop == DefLoop) {
  770. DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
  771. << MBB->getNumber() << " in the same loop\n");
  772. return MBB;
  773. }
  774. // Least busy dominator seen so far.
  775. unsigned Depth = Loop->getLoopDepth();
  776. if (Depth < BestDepth) {
  777. BestMBB = MBB;
  778. BestDepth = Depth;
  779. DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"
  780. << MBB->getNumber() << " at depth " << Depth << '\n');
  781. }
  782. // Leave loop by going to the immediate dominator of the loop header.
  783. // This is a bigger stride than simply walking up the dominator tree.
  784. MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();
  785. // Too far up the dominator tree?
  786. if (!IDom || !MDT.dominates(DefDomNode, IDom))
  787. return BestMBB;
  788. MBB = IDom->getBlock();
  789. }
  790. }
  791. void SplitEditor::computeRedundantBackCopies(
  792. DenseSet<unsigned> &NotToHoistSet, SmallVectorImpl<VNInfo *> &BackCopies) {
  793. LiveInterval *LI = &LIS.getInterval(Edit->get(0));
  794. LiveInterval *Parent = &Edit->getParent();
  795. SmallVector<SmallPtrSet<VNInfo *, 8>, 8> EqualVNs(Parent->getNumValNums());
  796. SmallPtrSet<VNInfo *, 8> DominatedVNIs;
  797. // Aggregate VNIs having the same value as ParentVNI.
  798. for (VNInfo *VNI : LI->valnos) {
  799. if (VNI->isUnused())
  800. continue;
  801. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
  802. EqualVNs[ParentVNI->id].insert(VNI);
  803. }
  804. // For VNI aggregation of each ParentVNI, collect dominated, i.e.,
  805. // redundant VNIs to BackCopies.
  806. for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
  807. VNInfo *ParentVNI = Parent->getValNumInfo(i);
  808. if (!NotToHoistSet.count(ParentVNI->id))
  809. continue;
  810. SmallPtrSetIterator<VNInfo *> It1 = EqualVNs[ParentVNI->id].begin();
  811. SmallPtrSetIterator<VNInfo *> It2 = It1;
  812. for (; It1 != EqualVNs[ParentVNI->id].end(); ++It1) {
  813. It2 = It1;
  814. for (++It2; It2 != EqualVNs[ParentVNI->id].end(); ++It2) {
  815. if (DominatedVNIs.count(*It1) || DominatedVNIs.count(*It2))
  816. continue;
  817. MachineBasicBlock *MBB1 = LIS.getMBBFromIndex((*It1)->def);
  818. MachineBasicBlock *MBB2 = LIS.getMBBFromIndex((*It2)->def);
  819. if (MBB1 == MBB2) {
  820. DominatedVNIs.insert((*It1)->def < (*It2)->def ? (*It2) : (*It1));
  821. } else if (MDT.dominates(MBB1, MBB2)) {
  822. DominatedVNIs.insert(*It2);
  823. } else if (MDT.dominates(MBB2, MBB1)) {
  824. DominatedVNIs.insert(*It1);
  825. }
  826. }
  827. }
  828. if (!DominatedVNIs.empty()) {
  829. forceRecompute(0, ParentVNI);
  830. for (auto VNI : DominatedVNIs) {
  831. BackCopies.push_back(VNI);
  832. }
  833. DominatedVNIs.clear();
  834. }
  835. }
  836. }
  837. /// For SM_Size mode, find a common dominator for all the back-copies for
  838. /// the same ParentVNI and hoist the backcopies to the dominator BB.
  839. /// For SM_Speed mode, if the common dominator is hot and it is not beneficial
  840. /// to do the hoisting, simply remove the dominated backcopies for the same
  841. /// ParentVNI.
  842. void SplitEditor::hoistCopies() {
  843. // Get the complement interval, always RegIdx 0.
  844. LiveInterval *LI = &LIS.getInterval(Edit->get(0));
  845. LiveInterval *Parent = &Edit->getParent();
  846. // Track the nearest common dominator for all back-copies for each ParentVNI,
  847. // indexed by ParentVNI->id.
  848. typedef std::pair<MachineBasicBlock*, SlotIndex> DomPair;
  849. SmallVector<DomPair, 8> NearestDom(Parent->getNumValNums());
  850. // The total cost of all the back-copies for each ParentVNI.
  851. SmallVector<BlockFrequency, 8> Costs(Parent->getNumValNums());
  852. // The ParentVNI->id set for which hoisting back-copies are not beneficial
  853. // for Speed.
  854. DenseSet<unsigned> NotToHoistSet;
  855. // Find the nearest common dominator for parent values with multiple
  856. // back-copies. If a single back-copy dominates, put it in DomPair.second.
  857. for (VNInfo *VNI : LI->valnos) {
  858. if (VNI->isUnused())
  859. continue;
  860. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
  861. assert(ParentVNI && "Parent not live at complement def");
  862. // Don't hoist remats. The complement is probably going to disappear
  863. // completely anyway.
  864. if (Edit->didRematerialize(ParentVNI))
  865. continue;
  866. MachineBasicBlock *ValMBB = LIS.getMBBFromIndex(VNI->def);
  867. DomPair &Dom = NearestDom[ParentVNI->id];
  868. // Keep directly defined parent values. This is either a PHI or an
  869. // instruction in the complement range. All other copies of ParentVNI
  870. // should be eliminated.
  871. if (VNI->def == ParentVNI->def) {
  872. DEBUG(dbgs() << "Direct complement def at " << VNI->def << '\n');
  873. Dom = DomPair(ValMBB, VNI->def);
  874. continue;
  875. }
  876. // Skip the singly mapped values. There is nothing to gain from hoisting a
  877. // single back-copy.
  878. if (Values.lookup(std::make_pair(0, ParentVNI->id)).getPointer()) {
  879. DEBUG(dbgs() << "Single complement def at " << VNI->def << '\n');
  880. continue;
  881. }
  882. if (!Dom.first) {
  883. // First time we see ParentVNI. VNI dominates itself.
  884. Dom = DomPair(ValMBB, VNI->def);
  885. } else if (Dom.first == ValMBB) {
  886. // Two defs in the same block. Pick the earlier def.
  887. if (!Dom.second.isValid() || VNI->def < Dom.second)
  888. Dom.second = VNI->def;
  889. } else {
  890. // Different basic blocks. Check if one dominates.
  891. MachineBasicBlock *Near =
  892. MDT.findNearestCommonDominator(Dom.first, ValMBB);
  893. if (Near == ValMBB)
  894. // Def ValMBB dominates.
  895. Dom = DomPair(ValMBB, VNI->def);
  896. else if (Near != Dom.first)
  897. // None dominate. Hoist to common dominator, need new def.
  898. Dom = DomPair(Near, SlotIndex());
  899. Costs[ParentVNI->id] += MBFI.getBlockFreq(ValMBB);
  900. }
  901. DEBUG(dbgs() << "Multi-mapped complement " << VNI->id << '@' << VNI->def
  902. << " for parent " << ParentVNI->id << '@' << ParentVNI->def
  903. << " hoist to BB#" << Dom.first->getNumber() << ' '
  904. << Dom.second << '\n');
  905. }
  906. // Insert the hoisted copies.
  907. for (unsigned i = 0, e = Parent->getNumValNums(); i != e; ++i) {
  908. DomPair &Dom = NearestDom[i];
  909. if (!Dom.first || Dom.second.isValid())
  910. continue;
  911. // This value needs a hoisted copy inserted at the end of Dom.first.
  912. VNInfo *ParentVNI = Parent->getValNumInfo(i);
  913. MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);
  914. // Get a less loopy dominator than Dom.first.
  915. Dom.first = findShallowDominator(Dom.first, DefMBB);
  916. if (SpillMode == SM_Speed &&
  917. MBFI.getBlockFreq(Dom.first) > Costs[ParentVNI->id]) {
  918. NotToHoistSet.insert(ParentVNI->id);
  919. continue;
  920. }
  921. SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();
  922. Dom.second =
  923. defFromParent(0, ParentVNI, Last, *Dom.first,
  924. SA.getLastSplitPointIter(Dom.first))->def;
  925. }
  926. // Remove redundant back-copies that are now known to be dominated by another
  927. // def with the same value.
  928. SmallVector<VNInfo*, 8> BackCopies;
  929. for (VNInfo *VNI : LI->valnos) {
  930. if (VNI->isUnused())
  931. continue;
  932. VNInfo *ParentVNI = Edit->getParent().getVNInfoAt(VNI->def);
  933. const DomPair &Dom = NearestDom[ParentVNI->id];
  934. if (!Dom.first || Dom.second == VNI->def ||
  935. NotToHoistSet.count(ParentVNI->id))
  936. continue;
  937. BackCopies.push_back(VNI);
  938. forceRecompute(0, ParentVNI);
  939. }
  940. // If it is not beneficial to hoist all the BackCopies, simply remove
  941. // redundant BackCopies in speed mode.
  942. if (SpillMode == SM_Speed && !NotToHoistSet.empty())
  943. computeRedundantBackCopies(NotToHoistSet, BackCopies);
  944. removeBackCopies(BackCopies);
  945. }
  946. /// transferValues - Transfer all possible values to the new live ranges.
  947. /// Values that were rematerialized are left alone, they need LRCalc.extend().
  948. bool SplitEditor::transferValues() {
  949. bool Skipped = false;
  950. RegAssignMap::const_iterator AssignI = RegAssign.begin();
  951. for (const LiveRange::Segment &S : Edit->getParent()) {
  952. DEBUG(dbgs() << " blit " << S << ':');
  953. VNInfo *ParentVNI = S.valno;
  954. // RegAssign has holes where RegIdx 0 should be used.
  955. SlotIndex Start = S.start;
  956. AssignI.advanceTo(Start);
  957. do {
  958. unsigned RegIdx;
  959. SlotIndex End = S.end;
  960. if (!AssignI.valid()) {
  961. RegIdx = 0;
  962. } else if (AssignI.start() <= Start) {
  963. RegIdx = AssignI.value();
  964. if (AssignI.stop() < End) {
  965. End = AssignI.stop();
  966. ++AssignI;
  967. }
  968. } else {
  969. RegIdx = 0;
  970. End = std::min(End, AssignI.start());
  971. }
  972. // The interval [Start;End) is continuously mapped to RegIdx, ParentVNI.
  973. DEBUG(dbgs() << " [" << Start << ';' << End << ")=" << RegIdx
  974. << '(' << PrintReg(Edit->get(RegIdx)) << ')');
  975. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  976. // Check for a simply defined value that can be blitted directly.
  977. ValueForcePair VFP = Values.lookup(std::make_pair(RegIdx, ParentVNI->id));
  978. if (VNInfo *VNI = VFP.getPointer()) {
  979. DEBUG(dbgs() << ':' << VNI->id);
  980. LI.addSegment(LiveInterval::Segment(Start, End, VNI));
  981. Start = End;
  982. continue;
  983. }
  984. // Skip values with forced recomputation.
  985. if (VFP.getInt()) {
  986. DEBUG(dbgs() << "(recalc)");
  987. Skipped = true;
  988. Start = End;
  989. continue;
  990. }
  991. LiveRangeCalc &LRC = getLRCalc(RegIdx);
  992. // This value has multiple defs in RegIdx, but it wasn't rematerialized,
  993. // so the live range is accurate. Add live-in blocks in [Start;End) to the
  994. // LiveInBlocks.
  995. MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
  996. SlotIndex BlockStart, BlockEnd;
  997. std::tie(BlockStart, BlockEnd) = LIS.getSlotIndexes()->getMBBRange(&*MBB);
  998. // The first block may be live-in, or it may have its own def.
  999. if (Start != BlockStart) {
  1000. VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
  1001. assert(VNI && "Missing def for complex mapped value");
  1002. DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
  1003. // MBB has its own def. Is it also live-out?
  1004. if (BlockEnd <= End)
  1005. LRC.setLiveOutValue(&*MBB, VNI);
  1006. // Skip to the next block for live-in.
  1007. ++MBB;
  1008. BlockStart = BlockEnd;
  1009. }
  1010. // Handle the live-in blocks covered by [Start;End).
  1011. assert(Start <= BlockStart && "Expected live-in block");
  1012. while (BlockStart < End) {
  1013. DEBUG(dbgs() << ">BB#" << MBB->getNumber());
  1014. BlockEnd = LIS.getMBBEndIdx(&*MBB);
  1015. if (BlockStart == ParentVNI->def) {
  1016. // This block has the def of a parent PHI, so it isn't live-in.
  1017. assert(ParentVNI->isPHIDef() && "Non-phi defined at block start?");
  1018. VNInfo *VNI = LI.extendInBlock(BlockStart, std::min(BlockEnd, End));
  1019. assert(VNI && "Missing def for complex mapped parent PHI");
  1020. if (End >= BlockEnd)
  1021. LRC.setLiveOutValue(&*MBB, VNI); // Live-out as well.
  1022. } else {
  1023. // This block needs a live-in value. The last block covered may not
  1024. // be live-out.
  1025. if (End < BlockEnd)
  1026. LRC.addLiveInBlock(LI, MDT[&*MBB], End);
  1027. else {
  1028. // Live-through, and we don't know the value.
  1029. LRC.addLiveInBlock(LI, MDT[&*MBB]);
  1030. LRC.setLiveOutValue(&*MBB, nullptr);
  1031. }
  1032. }
  1033. BlockStart = BlockEnd;
  1034. ++MBB;
  1035. }
  1036. Start = End;
  1037. } while (Start != S.end);
  1038. DEBUG(dbgs() << '\n');
  1039. }
  1040. LRCalc[0].calculateValues();
  1041. if (SpillMode)
  1042. LRCalc[1].calculateValues();
  1043. return Skipped;
  1044. }
  1045. static bool removeDeadSegment(SlotIndex Def, LiveRange &LR) {
  1046. const LiveRange::Segment *Seg = LR.getSegmentContaining(Def);
  1047. if (Seg == nullptr)
  1048. return true;
  1049. if (Seg->end != Def.getDeadSlot())
  1050. return false;
  1051. // This is a dead PHI. Remove it.
  1052. LR.removeSegment(*Seg, true);
  1053. return true;
  1054. }
  1055. void SplitEditor::extendPHIRange(MachineBasicBlock &B, LiveRangeCalc &LRC,
  1056. LiveRange &LR, LaneBitmask LM,
  1057. ArrayRef<SlotIndex> Undefs) {
  1058. for (MachineBasicBlock *P : B.predecessors()) {
  1059. SlotIndex End = LIS.getMBBEndIdx(P);
  1060. SlotIndex LastUse = End.getPrevSlot();
  1061. // The predecessor may not have a live-out value. That is OK, like an
  1062. // undef PHI operand.
  1063. LiveInterval &PLI = Edit->getParent();
  1064. // Need the cast because the inputs to ?: would otherwise be deemed
  1065. // "incompatible": SubRange vs LiveInterval.
  1066. LiveRange &PSR = !LM.all() ? getSubRangeForMask(LM, PLI)
  1067. : static_cast<LiveRange&>(PLI);
  1068. if (PSR.liveAt(LastUse))
  1069. LRC.extend(LR, End, /*PhysReg=*/0, Undefs);
  1070. }
  1071. }
  1072. void SplitEditor::extendPHIKillRanges() {
  1073. // Extend live ranges to be live-out for successor PHI values.
  1074. // Visit each PHI def slot in the parent live interval. If the def is dead,
  1075. // remove it. Otherwise, extend the live interval to reach the end indexes
  1076. // of all predecessor blocks.
  1077. LiveInterval &ParentLI = Edit->getParent();
  1078. for (const VNInfo *V : ParentLI.valnos) {
  1079. if (V->isUnused() || !V->isPHIDef())
  1080. continue;
  1081. unsigned RegIdx = RegAssign.lookup(V->def);
  1082. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  1083. LiveRangeCalc &LRC = getLRCalc(RegIdx);
  1084. MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
  1085. if (!removeDeadSegment(V->def, LI))
  1086. extendPHIRange(B, LRC, LI, LaneBitmask::getAll(), /*Undefs=*/{});
  1087. }
  1088. SmallVector<SlotIndex, 4> Undefs;
  1089. LiveRangeCalc SubLRC;
  1090. for (LiveInterval::SubRange &PS : ParentLI.subranges()) {
  1091. for (const VNInfo *V : PS.valnos) {
  1092. if (V->isUnused() || !V->isPHIDef())
  1093. continue;
  1094. unsigned RegIdx = RegAssign.lookup(V->def);
  1095. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  1096. LiveInterval::SubRange &S = getSubRangeForMask(PS.LaneMask, LI);
  1097. if (removeDeadSegment(V->def, S))
  1098. continue;
  1099. MachineBasicBlock &B = *LIS.getMBBFromIndex(V->def);
  1100. SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
  1101. &LIS.getVNInfoAllocator());
  1102. Undefs.clear();
  1103. LI.computeSubRangeUndefs(Undefs, PS.LaneMask, MRI, *LIS.getSlotIndexes());
  1104. extendPHIRange(B, SubLRC, S, PS.LaneMask, Undefs);
  1105. }
  1106. }
  1107. }
  1108. /// rewriteAssigned - Rewrite all uses of Edit->getReg().
  1109. void SplitEditor::rewriteAssigned(bool ExtendRanges) {
  1110. struct ExtPoint {
  1111. ExtPoint(const MachineOperand &O, unsigned R, SlotIndex N)
  1112. : MO(O), RegIdx(R), Next(N) {}
  1113. MachineOperand MO;
  1114. unsigned RegIdx;
  1115. SlotIndex Next;
  1116. };
  1117. SmallVector<ExtPoint,4> ExtPoints;
  1118. for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg()),
  1119. RE = MRI.reg_end(); RI != RE;) {
  1120. MachineOperand &MO = *RI;
  1121. MachineInstr *MI = MO.getParent();
  1122. ++RI;
  1123. // LiveDebugVariables should have handled all DBG_VALUE instructions.
  1124. if (MI->isDebugValue()) {
  1125. DEBUG(dbgs() << "Zapping " << *MI);
  1126. MO.setReg(0);
  1127. continue;
  1128. }
  1129. // <undef> operands don't really read the register, so it doesn't matter
  1130. // which register we choose. When the use operand is tied to a def, we must
  1131. // use the same register as the def, so just do that always.
  1132. SlotIndex Idx = LIS.getInstructionIndex(*MI);
  1133. if (MO.isDef() || MO.isUndef())
  1134. Idx = Idx.getRegSlot(MO.isEarlyClobber());
  1135. // Rewrite to the mapped register at Idx.
  1136. unsigned RegIdx = RegAssign.lookup(Idx);
  1137. LiveInterval &LI = LIS.getInterval(Edit->get(RegIdx));
  1138. MO.setReg(LI.reg);
  1139. DEBUG(dbgs() << " rewr BB#" << MI->getParent()->getNumber() << '\t'
  1140. << Idx << ':' << RegIdx << '\t' << *MI);
  1141. // Extend liveness to Idx if the instruction reads reg.
  1142. if (!ExtendRanges || MO.isUndef())
  1143. continue;
  1144. // Skip instructions that don't read Reg.
  1145. if (MO.isDef()) {
  1146. if (!MO.getSubReg() && !MO.isEarlyClobber())
  1147. continue;
  1148. // We may want to extend a live range for a partial redef, or for a use
  1149. // tied to an early clobber.
  1150. Idx = Idx.getPrevSlot();
  1151. if (!Edit->getParent().liveAt(Idx))
  1152. continue;
  1153. } else
  1154. Idx = Idx.getRegSlot(true);
  1155. SlotIndex Next = Idx.getNextSlot();
  1156. if (LI.hasSubRanges()) {
  1157. // We have to delay extending subranges until we have seen all operands
  1158. // defining the register. This is because a <def,read-undef> operand
  1159. // will create an "undef" point, and we cannot extend any subranges
  1160. // until all of them have been accounted for.
  1161. if (MO.isUse())
  1162. ExtPoints.push_back(ExtPoint(MO, RegIdx, Next));
  1163. } else {
  1164. LiveRangeCalc &LRC = getLRCalc(RegIdx);
  1165. LRC.extend(LI, Next, 0, ArrayRef<SlotIndex>());
  1166. }
  1167. }
  1168. for (ExtPoint &EP : ExtPoints) {
  1169. LiveInterval &LI = LIS.getInterval(Edit->get(EP.RegIdx));
  1170. assert(LI.hasSubRanges());
  1171. LiveRangeCalc SubLRC;
  1172. unsigned Reg = EP.MO.getReg(), Sub = EP.MO.getSubReg();
  1173. LaneBitmask LM = Sub != 0 ? TRI.getSubRegIndexLaneMask(Sub)
  1174. : MRI.getMaxLaneMaskForVReg(Reg);
  1175. for (LiveInterval::SubRange &S : LI.subranges()) {
  1176. if ((S.LaneMask & LM).none())
  1177. continue;
  1178. // The problem here can be that the new register may have been created
  1179. // for a partially defined original register. For example:
  1180. // %vreg827:subreg_hireg<def,read-undef> = ...
  1181. // ...
  1182. // %vreg828<def> = COPY %vreg827
  1183. if (S.empty())
  1184. continue;
  1185. SubLRC.reset(&VRM.getMachineFunction(), LIS.getSlotIndexes(), &MDT,
  1186. &LIS.getVNInfoAllocator());
  1187. SmallVector<SlotIndex, 4> Undefs;
  1188. LI.computeSubRangeUndefs(Undefs, S.LaneMask, MRI, *LIS.getSlotIndexes());
  1189. SubLRC.extend(S, EP.Next, 0, Undefs);
  1190. }
  1191. }
  1192. for (unsigned R : *Edit) {
  1193. LiveInterval &LI = LIS.getInterval(R);
  1194. if (!LI.hasSubRanges())
  1195. continue;
  1196. LI.clear();
  1197. LI.removeEmptySubRanges();
  1198. LIS.constructMainRangeFromSubranges(LI);
  1199. }
  1200. }
  1201. void SplitEditor::deleteRematVictims() {
  1202. SmallVector<MachineInstr*, 8> Dead;
  1203. for (LiveRangeEdit::iterator I = Edit->begin(), E = Edit->end(); I != E; ++I){
  1204. LiveInterval *LI = &LIS.getInterval(*I);
  1205. for (const LiveRange::Segment &S : LI->segments) {
  1206. // Dead defs end at the dead slot.
  1207. if (S.end != S.valno->def.getDeadSlot())
  1208. continue;
  1209. if (S.valno->isPHIDef())
  1210. continue;
  1211. MachineInstr *MI = LIS.getInstructionFromIndex(S.valno->def);
  1212. assert(MI && "Missing instruction for dead def");
  1213. MI->addRegisterDead(LI->reg, &TRI);
  1214. if (!MI->allDefsAreDead())
  1215. continue;
  1216. DEBUG(dbgs() << "All defs dead: " << *MI);
  1217. Dead.push_back(MI);
  1218. }
  1219. }
  1220. if (Dead.empty())
  1221. return;
  1222. Edit->eliminateDeadDefs(Dead, None, &AA);
  1223. }
  1224. void SplitEditor::finish(SmallVectorImpl<unsigned> *LRMap) {
  1225. ++NumFinished;
  1226. // At this point, the live intervals in Edit contain VNInfos corresponding to
  1227. // the inserted copies.
  1228. // Add the original defs from the parent interval.
  1229. for (const VNInfo *ParentVNI : Edit->getParent().valnos) {
  1230. if (ParentVNI->isUnused())
  1231. continue;
  1232. unsigned RegIdx = RegAssign.lookup(ParentVNI->def);
  1233. defValue(RegIdx, ParentVNI, ParentVNI->def, true);
  1234. // Force rematted values to be recomputed everywhere.
  1235. // The new live ranges may be truncated.
  1236. if (Edit->didRematerialize(ParentVNI))
  1237. for (unsigned i = 0, e = Edit->size(); i != e; ++i)
  1238. forceRecompute(i, ParentVNI);
  1239. }
  1240. // Hoist back-copies to the complement interval when in spill mode.
  1241. switch (SpillMode) {
  1242. case SM_Partition:
  1243. // Leave all back-copies as is.
  1244. break;
  1245. case SM_Size:
  1246. case SM_Speed:
  1247. // hoistCopies will behave differently between size and speed.
  1248. hoistCopies();
  1249. }
  1250. // Transfer the simply mapped values, check if any are skipped.
  1251. bool Skipped = transferValues();
  1252. // Rewrite virtual registers, possibly extending ranges.
  1253. rewriteAssigned(Skipped);
  1254. if (Skipped)
  1255. extendPHIKillRanges();
  1256. else
  1257. ++NumSimple;
  1258. // Delete defs that were rematted everywhere.
  1259. if (Skipped)
  1260. deleteRematVictims();
  1261. // Get rid of unused values and set phi-kill flags.
  1262. for (unsigned Reg : *Edit) {
  1263. LiveInterval &LI = LIS.getInterval(Reg);
  1264. LI.removeEmptySubRanges();
  1265. LI.RenumberValues();
  1266. }
  1267. // Provide a reverse mapping from original indices to Edit ranges.
  1268. if (LRMap) {
  1269. LRMap->clear();
  1270. for (unsigned i = 0, e = Edit->size(); i != e; ++i)
  1271. LRMap->push_back(i);
  1272. }
  1273. // Now check if any registers were separated into multiple components.
  1274. ConnectedVNInfoEqClasses ConEQ(LIS);
  1275. for (unsigned i = 0, e = Edit->size(); i != e; ++i) {
  1276. // Don't use iterators, they are invalidated by create() below.
  1277. unsigned VReg = Edit->get(i);
  1278. LiveInterval &LI = LIS.getInterval(VReg);
  1279. SmallVector<LiveInterval*, 8> SplitLIs;
  1280. LIS.splitSeparateComponents(LI, SplitLIs);
  1281. unsigned Original = VRM.getOriginal(VReg);
  1282. for (LiveInterval *SplitLI : SplitLIs)
  1283. VRM.setIsSplitFromReg(SplitLI->reg, Original);
  1284. // The new intervals all map back to i.
  1285. if (LRMap)
  1286. LRMap->resize(Edit->size(), i);
  1287. }
  1288. // Calculate spill weight and allocation hints for new intervals.
  1289. Edit->calculateRegClassAndHint(VRM.getMachineFunction(), SA.Loops, MBFI);
  1290. assert(!LRMap || LRMap->size() == Edit->size());
  1291. }
  1292. //===----------------------------------------------------------------------===//
  1293. // Single Block Splitting
  1294. //===----------------------------------------------------------------------===//
  1295. bool SplitAnalysis::shouldSplitSingleBlock(const BlockInfo &BI,
  1296. bool SingleInstrs) const {
  1297. // Always split for multiple instructions.
  1298. if (!BI.isOneInstr())
  1299. return true;
  1300. // Don't split for single instructions unless explicitly requested.
  1301. if (!SingleInstrs)
  1302. return false;
  1303. // Splitting a live-through range always makes progress.
  1304. if (BI.LiveIn && BI.LiveOut)
  1305. return true;
  1306. // No point in isolating a copy. It has no register class constraints.
  1307. if (LIS.getInstructionFromIndex(BI.FirstInstr)->isCopyLike())
  1308. return false;
  1309. // Finally, don't isolate an end point that was created by earlier splits.
  1310. return isOriginalEndpoint(BI.FirstInstr);
  1311. }
  1312. void SplitEditor::splitSingleBlock(const SplitAnalysis::BlockInfo &BI) {
  1313. openIntv();
  1314. SlotIndex LastSplitPoint = SA.getLastSplitPoint(BI.MBB->getNumber());
  1315. SlotIndex SegStart = enterIntvBefore(std::min(BI.FirstInstr,
  1316. LastSplitPoint));
  1317. if (!BI.LiveOut || BI.LastInstr < LastSplitPoint) {
  1318. useIntv(SegStart, leaveIntvAfter(BI.LastInstr));
  1319. } else {
  1320. // The last use is after the last valid split point.
  1321. SlotIndex SegStop = leaveIntvBefore(LastSplitPoint);
  1322. useIntv(SegStart, SegStop);
  1323. overlapIntv(SegStop, BI.LastInstr);
  1324. }
  1325. }
  1326. //===----------------------------------------------------------------------===//
  1327. // Global Live Range Splitting Support
  1328. //===----------------------------------------------------------------------===//
  1329. // These methods support a method of global live range splitting that uses a
  1330. // global algorithm to decide intervals for CFG edges. They will insert split
  1331. // points and color intervals in basic blocks while avoiding interference.
  1332. //
  1333. // Note that splitSingleBlock is also useful for blocks where both CFG edges
  1334. // are on the stack.
  1335. void SplitEditor::splitLiveThroughBlock(unsigned MBBNum,
  1336. unsigned IntvIn, SlotIndex LeaveBefore,
  1337. unsigned IntvOut, SlotIndex EnterAfter){
  1338. SlotIndex Start, Stop;
  1339. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(MBBNum);
  1340. DEBUG(dbgs() << "BB#" << MBBNum << " [" << Start << ';' << Stop
  1341. << ") intf " << LeaveBefore << '-' << EnterAfter
  1342. << ", live-through " << IntvIn << " -> " << IntvOut);
  1343. assert((IntvIn || IntvOut) && "Use splitSingleBlock for isolated blocks");
  1344. assert((!LeaveBefore || LeaveBefore < Stop) && "Interference after block");
  1345. assert((!IntvIn || !LeaveBefore || LeaveBefore > Start) && "Impossible intf");
  1346. assert((!EnterAfter || EnterAfter >= Start) && "Interference before block");
  1347. MachineBasicBlock *MBB = VRM.getMachineFunction().getBlockNumbered(MBBNum);
  1348. if (!IntvOut) {
  1349. DEBUG(dbgs() << ", spill on entry.\n");
  1350. //
  1351. // <<<<<<<<< Possible LeaveBefore interference.
  1352. // |-----------| Live through.
  1353. // -____________ Spill on entry.
  1354. //
  1355. selectIntv(IntvIn);
  1356. SlotIndex Idx = leaveIntvAtTop(*MBB);
  1357. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1358. (void)Idx;
  1359. return;
  1360. }
  1361. if (!IntvIn) {
  1362. DEBUG(dbgs() << ", reload on exit.\n");
  1363. //
  1364. // >>>>>>> Possible EnterAfter interference.
  1365. // |-----------| Live through.
  1366. // ___________-- Reload on exit.
  1367. //
  1368. selectIntv(IntvOut);
  1369. SlotIndex Idx = enterIntvAtEnd(*MBB);
  1370. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1371. (void)Idx;
  1372. return;
  1373. }
  1374. if (IntvIn == IntvOut && !LeaveBefore && !EnterAfter) {
  1375. DEBUG(dbgs() << ", straight through.\n");
  1376. //
  1377. // |-----------| Live through.
  1378. // ------------- Straight through, same intv, no interference.
  1379. //
  1380. selectIntv(IntvOut);
  1381. useIntv(Start, Stop);
  1382. return;
  1383. }
  1384. // We cannot legally insert splits after LSP.
  1385. SlotIndex LSP = SA.getLastSplitPoint(MBBNum);
  1386. assert((!IntvOut || !EnterAfter || EnterAfter < LSP) && "Impossible intf");
  1387. if (IntvIn != IntvOut && (!LeaveBefore || !EnterAfter ||
  1388. LeaveBefore.getBaseIndex() > EnterAfter.getBoundaryIndex())) {
  1389. DEBUG(dbgs() << ", switch avoiding interference.\n");
  1390. //
  1391. // >>>> <<<< Non-overlapping EnterAfter/LeaveBefore interference.
  1392. // |-----------| Live through.
  1393. // ------======= Switch intervals between interference.
  1394. //
  1395. selectIntv(IntvOut);
  1396. SlotIndex Idx;
  1397. if (LeaveBefore && LeaveBefore < LSP) {
  1398. Idx = enterIntvBefore(LeaveBefore);
  1399. useIntv(Idx, Stop);
  1400. } else {
  1401. Idx = enterIntvAtEnd(*MBB);
  1402. }
  1403. selectIntv(IntvIn);
  1404. useIntv(Start, Idx);
  1405. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1406. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1407. return;
  1408. }
  1409. DEBUG(dbgs() << ", create local intv for interference.\n");
  1410. //
  1411. // >>><><><><<<< Overlapping EnterAfter/LeaveBefore interference.
  1412. // |-----------| Live through.
  1413. // ==---------== Switch intervals before/after interference.
  1414. //
  1415. assert(LeaveBefore <= EnterAfter && "Missed case");
  1416. selectIntv(IntvOut);
  1417. SlotIndex Idx = enterIntvAfter(EnterAfter);
  1418. useIntv(Idx, Stop);
  1419. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1420. selectIntv(IntvIn);
  1421. Idx = leaveIntvBefore(LeaveBefore);
  1422. useIntv(Start, Idx);
  1423. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1424. }
  1425. void SplitEditor::splitRegInBlock(const SplitAnalysis::BlockInfo &BI,
  1426. unsigned IntvIn, SlotIndex LeaveBefore) {
  1427. SlotIndex Start, Stop;
  1428. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
  1429. DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
  1430. << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
  1431. << ", reg-in " << IntvIn << ", leave before " << LeaveBefore
  1432. << (BI.LiveOut ? ", stack-out" : ", killed in block"));
  1433. assert(IntvIn && "Must have register in");
  1434. assert(BI.LiveIn && "Must be live-in");
  1435. assert((!LeaveBefore || LeaveBefore > Start) && "Bad interference");
  1436. if (!BI.LiveOut && (!LeaveBefore || LeaveBefore >= BI.LastInstr)) {
  1437. DEBUG(dbgs() << " before interference.\n");
  1438. //
  1439. // <<< Interference after kill.
  1440. // |---o---x | Killed in block.
  1441. // ========= Use IntvIn everywhere.
  1442. //
  1443. selectIntv(IntvIn);
  1444. useIntv(Start, BI.LastInstr);
  1445. return;
  1446. }
  1447. SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
  1448. if (!LeaveBefore || LeaveBefore > BI.LastInstr.getBoundaryIndex()) {
  1449. //
  1450. // <<< Possible interference after last use.
  1451. // |---o---o---| Live-out on stack.
  1452. // =========____ Leave IntvIn after last use.
  1453. //
  1454. // < Interference after last use.
  1455. // |---o---o--o| Live-out on stack, late last use.
  1456. // ============ Copy to stack after LSP, overlap IntvIn.
  1457. // \_____ Stack interval is live-out.
  1458. //
  1459. if (BI.LastInstr < LSP) {
  1460. DEBUG(dbgs() << ", spill after last use before interference.\n");
  1461. selectIntv(IntvIn);
  1462. SlotIndex Idx = leaveIntvAfter(BI.LastInstr);
  1463. useIntv(Start, Idx);
  1464. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1465. } else {
  1466. DEBUG(dbgs() << ", spill before last split point.\n");
  1467. selectIntv(IntvIn);
  1468. SlotIndex Idx = leaveIntvBefore(LSP);
  1469. overlapIntv(Idx, BI.LastInstr);
  1470. useIntv(Start, Idx);
  1471. assert((!LeaveBefore || Idx <= LeaveBefore) && "Interference");
  1472. }
  1473. return;
  1474. }
  1475. // The interference is overlapping somewhere we wanted to use IntvIn. That
  1476. // means we need to create a local interval that can be allocated a
  1477. // different register.
  1478. unsigned LocalIntv = openIntv();
  1479. (void)LocalIntv;
  1480. DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
  1481. if (!BI.LiveOut || BI.LastInstr < LSP) {
  1482. //
  1483. // <<<<<<< Interference overlapping uses.
  1484. // |---o---o---| Live-out on stack.
  1485. // =====----____ Leave IntvIn before interference, then spill.
  1486. //
  1487. SlotIndex To = leaveIntvAfter(BI.LastInstr);
  1488. SlotIndex From = enterIntvBefore(LeaveBefore);
  1489. useIntv(From, To);
  1490. selectIntv(IntvIn);
  1491. useIntv(Start, From);
  1492. assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
  1493. return;
  1494. }
  1495. // <<<<<<< Interference overlapping uses.
  1496. // |---o---o--o| Live-out on stack, late last use.
  1497. // =====------- Copy to stack before LSP, overlap LocalIntv.
  1498. // \_____ Stack interval is live-out.
  1499. //
  1500. SlotIndex To = leaveIntvBefore(LSP);
  1501. overlapIntv(To, BI.LastInstr);
  1502. SlotIndex From = enterIntvBefore(std::min(To, LeaveBefore));
  1503. useIntv(From, To);
  1504. selectIntv(IntvIn);
  1505. useIntv(Start, From);
  1506. assert((!LeaveBefore || From <= LeaveBefore) && "Interference");
  1507. }
  1508. void SplitEditor::splitRegOutBlock(const SplitAnalysis::BlockInfo &BI,
  1509. unsigned IntvOut, SlotIndex EnterAfter) {
  1510. SlotIndex Start, Stop;
  1511. std::tie(Start, Stop) = LIS.getSlotIndexes()->getMBBRange(BI.MBB);
  1512. DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " [" << Start << ';' << Stop
  1513. << "), uses " << BI.FirstInstr << '-' << BI.LastInstr
  1514. << ", reg-out " << IntvOut << ", enter after " << EnterAfter
  1515. << (BI.LiveIn ? ", stack-in" : ", defined in block"));
  1516. SlotIndex LSP = SA.getLastSplitPoint(BI.MBB->getNumber());
  1517. assert(IntvOut && "Must have register out");
  1518. assert(BI.LiveOut && "Must be live-out");
  1519. assert((!EnterAfter || EnterAfter < LSP) && "Bad interference");
  1520. if (!BI.LiveIn && (!EnterAfter || EnterAfter <= BI.FirstInstr)) {
  1521. DEBUG(dbgs() << " after interference.\n");
  1522. //
  1523. // >>>> Interference before def.
  1524. // | o---o---| Defined in block.
  1525. // ========= Use IntvOut everywhere.
  1526. //
  1527. selectIntv(IntvOut);
  1528. useIntv(BI.FirstInstr, Stop);
  1529. return;
  1530. }
  1531. if (!EnterAfter || EnterAfter < BI.FirstInstr.getBaseIndex()) {
  1532. DEBUG(dbgs() << ", reload after interference.\n");
  1533. //
  1534. // >>>> Interference before def.
  1535. // |---o---o---| Live-through, stack-in.
  1536. // ____========= Enter IntvOut before first use.
  1537. //
  1538. selectIntv(IntvOut);
  1539. SlotIndex Idx = enterIntvBefore(std::min(LSP, BI.FirstInstr));
  1540. useIntv(Idx, Stop);
  1541. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1542. return;
  1543. }
  1544. // The interference is overlapping somewhere we wanted to use IntvOut. That
  1545. // means we need to create a local interval that can be allocated a
  1546. // different register.
  1547. DEBUG(dbgs() << ", interference overlaps uses.\n");
  1548. //
  1549. // >>>>>>> Interference overlapping uses.
  1550. // |---o---o---| Live-through, stack-in.
  1551. // ____---====== Create local interval for interference range.
  1552. //
  1553. selectIntv(IntvOut);
  1554. SlotIndex Idx = enterIntvAfter(EnterAfter);
  1555. useIntv(Idx, Stop);
  1556. assert((!EnterAfter || Idx >= EnterAfter) && "Interference");
  1557. openIntv();
  1558. SlotIndex From = enterIntvBefore(std::min(Idx, BI.FirstInstr));
  1559. useIntv(From, Idx);
  1560. }