LiveRangeCalc.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. //===- LiveRangeCalc.cpp - Calculate live ranges --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Implementation of the LiveRangeCalc class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "LiveRangeCalc.h"
  13. #include "llvm/ADT/BitVector.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SetVector.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/CodeGen/LiveInterval.h"
  18. #include "llvm/CodeGen/MachineBasicBlock.h"
  19. #include "llvm/CodeGen/MachineDominators.h"
  20. #include "llvm/CodeGen/MachineFunction.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineOperand.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/SlotIndexes.h"
  25. #include "llvm/CodeGen/TargetRegisterInfo.h"
  26. #include "llvm/MC/LaneBitmask.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <algorithm>
  30. #include <cassert>
  31. #include <iterator>
  32. #include <tuple>
  33. #include <utility>
  34. using namespace llvm;
  35. #define DEBUG_TYPE "regalloc"
  36. // Reserve an address that indicates a value that is known to be "undef".
  37. static VNInfo UndefVNI(0xbad, SlotIndex());
  38. void LiveRangeCalc::resetLiveOutMap() {
  39. unsigned NumBlocks = MF->getNumBlockIDs();
  40. Seen.clear();
  41. Seen.resize(NumBlocks);
  42. EntryInfos.clear();
  43. Map.resize(NumBlocks);
  44. }
  45. void LiveRangeCalc::reset(const MachineFunction *mf,
  46. SlotIndexes *SI,
  47. MachineDominatorTree *MDT,
  48. VNInfo::Allocator *VNIA) {
  49. MF = mf;
  50. MRI = &MF->getRegInfo();
  51. Indexes = SI;
  52. DomTree = MDT;
  53. Alloc = VNIA;
  54. resetLiveOutMap();
  55. LiveIn.clear();
  56. }
  57. static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
  58. LiveRange &LR, const MachineOperand &MO) {
  59. const MachineInstr &MI = *MO.getParent();
  60. SlotIndex DefIdx =
  61. Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
  62. // Create the def in LR. This may find an existing def.
  63. LR.createDeadDef(DefIdx, Alloc);
  64. }
  65. void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
  66. assert(MRI && Indexes && "call reset() first");
  67. // Step 1: Create minimal live segments for every definition of Reg.
  68. // Visit all def operands. If the same instruction has multiple defs of Reg,
  69. // createDeadDef() will deduplicate.
  70. const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
  71. unsigned Reg = LI.reg;
  72. for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
  73. if (!MO.isDef() && !MO.readsReg())
  74. continue;
  75. unsigned SubReg = MO.getSubReg();
  76. if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
  77. LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
  78. : MRI->getMaxLaneMaskForVReg(Reg);
  79. // If this is the first time we see a subregister def, initialize
  80. // subranges by creating a copy of the main range.
  81. if (!LI.hasSubRanges() && !LI.empty()) {
  82. LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
  83. LI.createSubRangeFrom(*Alloc, ClassMask, LI);
  84. }
  85. LI.refineSubRanges(*Alloc, SubMask,
  86. [&MO, this](LiveInterval::SubRange &SR) {
  87. if (MO.isDef())
  88. createDeadDef(*Indexes, *Alloc, SR, MO);
  89. },
  90. *Indexes, TRI);
  91. }
  92. // Create the def in the main liverange. We do not have to do this if
  93. // subranges are tracked as we recreate the main range later in this case.
  94. if (MO.isDef() && !LI.hasSubRanges())
  95. createDeadDef(*Indexes, *Alloc, LI, MO);
  96. }
  97. // We may have created empty live ranges for partially undefined uses, we
  98. // can't keep them because we won't find defs in them later.
  99. LI.removeEmptySubRanges();
  100. // Step 2: Extend live segments to all uses, constructing SSA form as
  101. // necessary.
  102. if (LI.hasSubRanges()) {
  103. for (LiveInterval::SubRange &S : LI.subranges()) {
  104. LiveRangeCalc SubLRC;
  105. SubLRC.reset(MF, Indexes, DomTree, Alloc);
  106. SubLRC.extendToUses(S, Reg, S.LaneMask, &LI);
  107. }
  108. LI.clear();
  109. constructMainRangeFromSubranges(LI);
  110. } else {
  111. resetLiveOutMap();
  112. extendToUses(LI, Reg, LaneBitmask::getAll());
  113. }
  114. }
  115. void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
  116. // First create dead defs at all defs found in subranges.
  117. LiveRange &MainRange = LI;
  118. assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
  119. "Expect empty main liverange");
  120. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  121. for (const VNInfo *VNI : SR.valnos) {
  122. if (!VNI->isUnused() && !VNI->isPHIDef())
  123. MainRange.createDeadDef(VNI->def, *Alloc);
  124. }
  125. }
  126. resetLiveOutMap();
  127. extendToUses(MainRange, LI.reg, LaneBitmask::getAll(), &LI);
  128. }
  129. void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
  130. assert(MRI && Indexes && "call reset() first");
  131. // Visit all def operands. If the same instruction has multiple defs of Reg,
  132. // LR.createDeadDef() will deduplicate.
  133. for (MachineOperand &MO : MRI->def_operands(Reg))
  134. createDeadDef(*Indexes, *Alloc, LR, MO);
  135. }
  136. void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask Mask,
  137. LiveInterval *LI) {
  138. SmallVector<SlotIndex, 4> Undefs;
  139. if (LI != nullptr)
  140. LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
  141. // Visit all operands that read Reg. This may include partial defs.
  142. bool IsSubRange = !Mask.all();
  143. const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
  144. for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
  145. // Clear all kill flags. They will be reinserted after register allocation
  146. // by LiveIntervals::addKillFlags().
  147. if (MO.isUse())
  148. MO.setIsKill(false);
  149. // MO::readsReg returns "true" for subregister defs. This is for keeping
  150. // liveness of the entire register (i.e. for the main range of the live
  151. // interval). For subranges, definitions of non-overlapping subregisters
  152. // do not count as uses.
  153. if (!MO.readsReg() || (IsSubRange && MO.isDef()))
  154. continue;
  155. unsigned SubReg = MO.getSubReg();
  156. if (SubReg != 0) {
  157. LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
  158. if (MO.isDef())
  159. SLM = ~SLM;
  160. // Ignore uses not reading the current (sub)range.
  161. if ((SLM & Mask).none())
  162. continue;
  163. }
  164. // Determine the actual place of the use.
  165. const MachineInstr *MI = MO.getParent();
  166. unsigned OpNo = (&MO - &MI->getOperand(0));
  167. SlotIndex UseIdx;
  168. if (MI->isPHI()) {
  169. assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
  170. // The actual place where a phi operand is used is the end of the pred
  171. // MBB. PHI operands are paired: (Reg, PredMBB).
  172. UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
  173. } else {
  174. // Check for early-clobber redefs.
  175. bool isEarlyClobber = false;
  176. unsigned DefIdx;
  177. if (MO.isDef())
  178. isEarlyClobber = MO.isEarlyClobber();
  179. else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
  180. // FIXME: This would be a lot easier if tied early-clobber uses also
  181. // had an early-clobber flag.
  182. isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
  183. }
  184. UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
  185. }
  186. // MI is reading Reg. We may have visited MI before if it happens to be
  187. // reading Reg multiple times. That is OK, extend() is idempotent.
  188. extend(LR, UseIdx, Reg, Undefs);
  189. }
  190. }
  191. void LiveRangeCalc::updateFromLiveIns() {
  192. LiveRangeUpdater Updater;
  193. for (const LiveInBlock &I : LiveIn) {
  194. if (!I.DomNode)
  195. continue;
  196. MachineBasicBlock *MBB = I.DomNode->getBlock();
  197. assert(I.Value && "No live-in value found");
  198. SlotIndex Start, End;
  199. std::tie(Start, End) = Indexes->getMBBRange(MBB);
  200. if (I.Kill.isValid())
  201. // Value is killed inside this block.
  202. End = I.Kill;
  203. else {
  204. // The value is live-through, update LiveOut as well.
  205. // Defer the Domtree lookup until it is needed.
  206. assert(Seen.test(MBB->getNumber()));
  207. Map[MBB] = LiveOutPair(I.Value, nullptr);
  208. }
  209. Updater.setDest(&I.LR);
  210. Updater.add(Start, End, I.Value);
  211. }
  212. LiveIn.clear();
  213. }
  214. void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
  215. ArrayRef<SlotIndex> Undefs) {
  216. assert(Use.isValid() && "Invalid SlotIndex");
  217. assert(Indexes && "Missing SlotIndexes");
  218. assert(DomTree && "Missing dominator tree");
  219. MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
  220. assert(UseMBB && "No MBB at Use");
  221. // Is there a def in the same MBB we can extend?
  222. auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
  223. if (EP.first != nullptr || EP.second)
  224. return;
  225. // Find the single reaching def, or determine if Use is jointly dominated by
  226. // multiple values, and we may need to create even more phi-defs to preserve
  227. // VNInfo SSA form. Perform a search for all predecessor blocks where we
  228. // know the dominating VNInfo.
  229. if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
  230. return;
  231. // When there were multiple different values, we may need new PHIs.
  232. calculateValues();
  233. }
  234. // This function is called by a client after using the low-level API to add
  235. // live-out and live-in blocks. The unique value optimization is not
  236. // available, SplitEditor::transferValues handles that case directly anyway.
  237. void LiveRangeCalc::calculateValues() {
  238. assert(Indexes && "Missing SlotIndexes");
  239. assert(DomTree && "Missing dominator tree");
  240. updateSSA();
  241. updateFromLiveIns();
  242. }
  243. bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
  244. MachineBasicBlock &MBB, BitVector &DefOnEntry,
  245. BitVector &UndefOnEntry) {
  246. unsigned BN = MBB.getNumber();
  247. if (DefOnEntry[BN])
  248. return true;
  249. if (UndefOnEntry[BN])
  250. return false;
  251. auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
  252. for (MachineBasicBlock *S : B.successors())
  253. DefOnEntry[S->getNumber()] = true;
  254. DefOnEntry[BN] = true;
  255. return true;
  256. };
  257. SetVector<unsigned> WorkList;
  258. // Checking if the entry of MBB is reached by some def: add all predecessors
  259. // that are potentially defined-on-exit to the work list.
  260. for (MachineBasicBlock *P : MBB.predecessors())
  261. WorkList.insert(P->getNumber());
  262. for (unsigned i = 0; i != WorkList.size(); ++i) {
  263. // Determine if the exit from the block is reached by some def.
  264. unsigned N = WorkList[i];
  265. MachineBasicBlock &B = *MF->getBlockNumbered(N);
  266. if (Seen[N]) {
  267. const LiveOutPair &LOB = Map[&B];
  268. if (LOB.first != nullptr && LOB.first != &UndefVNI)
  269. return MarkDefined(B);
  270. }
  271. SlotIndex Begin, End;
  272. std::tie(Begin, End) = Indexes->getMBBRange(&B);
  273. // Treat End as not belonging to B.
  274. // If LR has a segment S that starts at the next block, i.e. [End, ...),
  275. // std::upper_bound will return the segment following S. Instead,
  276. // S should be treated as the first segment that does not overlap B.
  277. LiveRange::iterator UB = std::upper_bound(LR.begin(), LR.end(),
  278. End.getPrevSlot());
  279. if (UB != LR.begin()) {
  280. LiveRange::Segment &Seg = *std::prev(UB);
  281. if (Seg.end > Begin) {
  282. // There is a segment that overlaps B. If the range is not explicitly
  283. // undefined between the end of the segment and the end of the block,
  284. // treat the block as defined on exit. If it is, go to the next block
  285. // on the work list.
  286. if (LR.isUndefIn(Undefs, Seg.end, End))
  287. continue;
  288. return MarkDefined(B);
  289. }
  290. }
  291. // No segment overlaps with this block. If this block is not defined on
  292. // entry, or it undefines the range, do not process its predecessors.
  293. if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
  294. UndefOnEntry[N] = true;
  295. continue;
  296. }
  297. if (DefOnEntry[N])
  298. return MarkDefined(B);
  299. // Still don't know: add all predecessors to the work list.
  300. for (MachineBasicBlock *P : B.predecessors())
  301. WorkList.insert(P->getNumber());
  302. }
  303. UndefOnEntry[BN] = true;
  304. return false;
  305. }
  306. bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
  307. SlotIndex Use, unsigned PhysReg,
  308. ArrayRef<SlotIndex> Undefs) {
  309. unsigned UseMBBNum = UseMBB.getNumber();
  310. // Block numbers where LR should be live-in.
  311. SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
  312. // Remember if we have seen more than one value.
  313. bool UniqueVNI = true;
  314. VNInfo *TheVNI = nullptr;
  315. bool FoundUndef = false;
  316. // Using Seen as a visited set, perform a BFS for all reaching defs.
  317. for (unsigned i = 0; i != WorkList.size(); ++i) {
  318. MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
  319. #ifndef NDEBUG
  320. if (MBB->pred_empty()) {
  321. MBB->getParent()->verify();
  322. errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo())
  323. << " does not have a corresponding definition on every path:\n";
  324. const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
  325. if (MI != nullptr)
  326. errs() << Use << " " << *MI;
  327. report_fatal_error("Use not jointly dominated by defs.");
  328. }
  329. if (Register::isPhysicalRegister(PhysReg) && !MBB->isLiveIn(PhysReg)) {
  330. MBB->getParent()->verify();
  331. const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
  332. errs() << "The register " << printReg(PhysReg, TRI)
  333. << " needs to be live in to " << printMBBReference(*MBB)
  334. << ", but is missing from the live-in list.\n";
  335. report_fatal_error("Invalid global physical register");
  336. }
  337. #endif
  338. FoundUndef |= MBB->pred_empty();
  339. for (MachineBasicBlock *Pred : MBB->predecessors()) {
  340. // Is this a known live-out block?
  341. if (Seen.test(Pred->getNumber())) {
  342. if (VNInfo *VNI = Map[Pred].first) {
  343. if (TheVNI && TheVNI != VNI)
  344. UniqueVNI = false;
  345. TheVNI = VNI;
  346. }
  347. continue;
  348. }
  349. SlotIndex Start, End;
  350. std::tie(Start, End) = Indexes->getMBBRange(Pred);
  351. // First time we see Pred. Try to determine the live-out value, but set
  352. // it as null if Pred is live-through with an unknown value.
  353. auto EP = LR.extendInBlock(Undefs, Start, End);
  354. VNInfo *VNI = EP.first;
  355. FoundUndef |= EP.second;
  356. setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
  357. if (VNI) {
  358. if (TheVNI && TheVNI != VNI)
  359. UniqueVNI = false;
  360. TheVNI = VNI;
  361. }
  362. if (VNI || EP.second)
  363. continue;
  364. // No, we need a live-in value for Pred as well
  365. if (Pred != &UseMBB)
  366. WorkList.push_back(Pred->getNumber());
  367. else
  368. // Loopback to UseMBB, so value is really live through.
  369. Use = SlotIndex();
  370. }
  371. }
  372. LiveIn.clear();
  373. FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
  374. if (!Undefs.empty() && FoundUndef)
  375. UniqueVNI = false;
  376. // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
  377. // neither require it. Skip the sorting overhead for small updates.
  378. if (WorkList.size() > 4)
  379. array_pod_sort(WorkList.begin(), WorkList.end());
  380. // If a unique reaching def was found, blit in the live ranges immediately.
  381. if (UniqueVNI) {
  382. assert(TheVNI != nullptr && TheVNI != &UndefVNI);
  383. LiveRangeUpdater Updater(&LR);
  384. for (unsigned BN : WorkList) {
  385. SlotIndex Start, End;
  386. std::tie(Start, End) = Indexes->getMBBRange(BN);
  387. // Trim the live range in UseMBB.
  388. if (BN == UseMBBNum && Use.isValid())
  389. End = Use;
  390. else
  391. Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
  392. Updater.add(Start, End, TheVNI);
  393. }
  394. return true;
  395. }
  396. // Prepare the defined/undefined bit vectors.
  397. EntryInfoMap::iterator Entry;
  398. bool DidInsert;
  399. std::tie(Entry, DidInsert) = EntryInfos.insert(
  400. std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
  401. if (DidInsert) {
  402. // Initialize newly inserted entries.
  403. unsigned N = MF->getNumBlockIDs();
  404. Entry->second.first.resize(N);
  405. Entry->second.second.resize(N);
  406. }
  407. BitVector &DefOnEntry = Entry->second.first;
  408. BitVector &UndefOnEntry = Entry->second.second;
  409. // Multiple values were found, so transfer the work list to the LiveIn array
  410. // where UpdateSSA will use it as a work list.
  411. LiveIn.reserve(WorkList.size());
  412. for (unsigned BN : WorkList) {
  413. MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
  414. if (!Undefs.empty() &&
  415. !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
  416. continue;
  417. addLiveInBlock(LR, DomTree->getNode(MBB));
  418. if (MBB == &UseMBB)
  419. LiveIn.back().Kill = Use;
  420. }
  421. return false;
  422. }
  423. // This is essentially the same iterative algorithm that SSAUpdater uses,
  424. // except we already have a dominator tree, so we don't have to recompute it.
  425. void LiveRangeCalc::updateSSA() {
  426. assert(Indexes && "Missing SlotIndexes");
  427. assert(DomTree && "Missing dominator tree");
  428. // Interate until convergence.
  429. bool Changed;
  430. do {
  431. Changed = false;
  432. // Propagate live-out values down the dominator tree, inserting phi-defs
  433. // when necessary.
  434. for (LiveInBlock &I : LiveIn) {
  435. MachineDomTreeNode *Node = I.DomNode;
  436. // Skip block if the live-in value has already been determined.
  437. if (!Node)
  438. continue;
  439. MachineBasicBlock *MBB = Node->getBlock();
  440. MachineDomTreeNode *IDom = Node->getIDom();
  441. LiveOutPair IDomValue;
  442. // We need a live-in value to a block with no immediate dominator?
  443. // This is probably an unreachable block that has survived somehow.
  444. bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
  445. // IDom dominates all of our predecessors, but it may not be their
  446. // immediate dominator. Check if any of them have live-out values that are
  447. // properly dominated by IDom. If so, we need a phi-def here.
  448. if (!needPHI) {
  449. IDomValue = Map[IDom->getBlock()];
  450. // Cache the DomTree node that defined the value.
  451. if (IDomValue.first && IDomValue.first != &UndefVNI &&
  452. !IDomValue.second) {
  453. Map[IDom->getBlock()].second = IDomValue.second =
  454. DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
  455. }
  456. for (MachineBasicBlock *Pred : MBB->predecessors()) {
  457. LiveOutPair &Value = Map[Pred];
  458. if (!Value.first || Value.first == IDomValue.first)
  459. continue;
  460. if (Value.first == &UndefVNI) {
  461. needPHI = true;
  462. break;
  463. }
  464. // Cache the DomTree node that defined the value.
  465. if (!Value.second)
  466. Value.second =
  467. DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
  468. // This predecessor is carrying something other than IDomValue.
  469. // It could be because IDomValue hasn't propagated yet, or it could be
  470. // because MBB is in the dominance frontier of that value.
  471. if (DomTree->dominates(IDom, Value.second)) {
  472. needPHI = true;
  473. break;
  474. }
  475. }
  476. }
  477. // The value may be live-through even if Kill is set, as can happen when
  478. // we are called from extendRange. In that case LiveOutSeen is true, and
  479. // LiveOut indicates a foreign or missing value.
  480. LiveOutPair &LOP = Map[MBB];
  481. // Create a phi-def if required.
  482. if (needPHI) {
  483. Changed = true;
  484. assert(Alloc && "Need VNInfo allocator to create PHI-defs");
  485. SlotIndex Start, End;
  486. std::tie(Start, End) = Indexes->getMBBRange(MBB);
  487. LiveRange &LR = I.LR;
  488. VNInfo *VNI = LR.getNextValue(Start, *Alloc);
  489. I.Value = VNI;
  490. // This block is done, we know the final value.
  491. I.DomNode = nullptr;
  492. // Add liveness since updateFromLiveIns now skips this node.
  493. if (I.Kill.isValid()) {
  494. if (VNI)
  495. LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
  496. } else {
  497. if (VNI)
  498. LR.addSegment(LiveInterval::Segment(Start, End, VNI));
  499. LOP = LiveOutPair(VNI, Node);
  500. }
  501. } else if (IDomValue.first && IDomValue.first != &UndefVNI) {
  502. // No phi-def here. Remember incoming value.
  503. I.Value = IDomValue.first;
  504. // If the IDomValue is killed in the block, don't propagate through.
  505. if (I.Kill.isValid())
  506. continue;
  507. // Propagate IDomValue if it isn't killed:
  508. // MBB is live-out and doesn't define its own value.
  509. if (LOP.first == IDomValue.first)
  510. continue;
  511. Changed = true;
  512. LOP = IDomValue;
  513. }
  514. }
  515. } while (Changed);
  516. }
  517. bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB,
  518. ArrayRef<SlotIndex> Defs,
  519. const SlotIndexes &Indexes) {
  520. const MachineFunction &MF = *MBB->getParent();
  521. BitVector DefBlocks(MF.getNumBlockIDs());
  522. for (SlotIndex I : Defs)
  523. DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber());
  524. SetVector<unsigned> PredQueue;
  525. PredQueue.insert(MBB->getNumber());
  526. for (unsigned i = 0; i != PredQueue.size(); ++i) {
  527. unsigned BN = PredQueue[i];
  528. if (DefBlocks[BN])
  529. return true;
  530. const MachineBasicBlock *B = MF.getBlockNumbered(BN);
  531. for (const MachineBasicBlock *P : B->predecessors())
  532. PredQueue.insert(P->getNumber());
  533. }
  534. return false;
  535. }