PHIElimination.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. //===- PhiElimination.cpp - Eliminate PHI nodes by inserting copies -------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass eliminates machine instruction PHI nodes by inserting copy
  10. // instructions. This destroys SSA information, but is the desired input for
  11. // some register allocators.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "PHIEliminationUtils.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallPtrSet.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. #include "llvm/CodeGen/LiveInterval.h"
  20. #include "llvm/CodeGen/LiveIntervals.h"
  21. #include "llvm/CodeGen/LiveVariables.h"
  22. #include "llvm/CodeGen/MachineBasicBlock.h"
  23. #include "llvm/CodeGen/MachineDominators.h"
  24. #include "llvm/CodeGen/MachineFunction.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineLoopInfo.h"
  29. #include "llvm/CodeGen/MachineOperand.h"
  30. #include "llvm/CodeGen/MachineRegisterInfo.h"
  31. #include "llvm/CodeGen/SlotIndexes.h"
  32. #include "llvm/CodeGen/TargetInstrInfo.h"
  33. #include "llvm/CodeGen/TargetLowering.h"
  34. #include "llvm/CodeGen/TargetOpcodes.h"
  35. #include "llvm/CodeGen/TargetPassConfig.h"
  36. #include "llvm/CodeGen/TargetRegisterInfo.h"
  37. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  38. #include "llvm/Pass.h"
  39. #include "llvm/Support/CommandLine.h"
  40. #include "llvm/Support/Debug.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include <cassert>
  43. #include <iterator>
  44. #include <utility>
  45. using namespace llvm;
  46. #define DEBUG_TYPE "phi-node-elimination"
  47. static cl::opt<bool>
  48. DisableEdgeSplitting("disable-phi-elim-edge-splitting", cl::init(false),
  49. cl::Hidden, cl::desc("Disable critical edge splitting "
  50. "during PHI elimination"));
  51. static cl::opt<bool>
  52. SplitAllCriticalEdges("phi-elim-split-all-critical-edges", cl::init(false),
  53. cl::Hidden, cl::desc("Split all critical edges during "
  54. "PHI elimination"));
  55. static cl::opt<bool> NoPhiElimLiveOutEarlyExit(
  56. "no-phi-elim-live-out-early-exit", cl::init(false), cl::Hidden,
  57. cl::desc("Do not use an early exit if isLiveOutPastPHIs returns true."));
  58. namespace {
  59. class PHIElimination : public MachineFunctionPass {
  60. MachineRegisterInfo *MRI; // Machine register information
  61. LiveVariables *LV;
  62. LiveIntervals *LIS;
  63. public:
  64. static char ID; // Pass identification, replacement for typeid
  65. PHIElimination() : MachineFunctionPass(ID) {
  66. initializePHIEliminationPass(*PassRegistry::getPassRegistry());
  67. }
  68. bool runOnMachineFunction(MachineFunction &MF) override;
  69. void getAnalysisUsage(AnalysisUsage &AU) const override;
  70. private:
  71. /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
  72. /// in predecessor basic blocks.
  73. bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
  74. void LowerPHINode(MachineBasicBlock &MBB,
  75. MachineBasicBlock::iterator LastPHIIt);
  76. /// analyzePHINodes - Gather information about the PHI nodes in
  77. /// here. In particular, we want to map the number of uses of a virtual
  78. /// register which is used in a PHI node. We map that to the BB the
  79. /// vreg is coming from. This is used later to determine when the vreg
  80. /// is killed in the BB.
  81. void analyzePHINodes(const MachineFunction& MF);
  82. /// Split critical edges where necessary for good coalescer performance.
  83. bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
  84. MachineLoopInfo *MLI);
  85. // These functions are temporary abstractions around LiveVariables and
  86. // LiveIntervals, so they can go away when LiveVariables does.
  87. bool isLiveIn(unsigned Reg, const MachineBasicBlock *MBB);
  88. bool isLiveOutPastPHIs(unsigned Reg, const MachineBasicBlock *MBB);
  89. using BBVRegPair = std::pair<unsigned, unsigned>;
  90. using VRegPHIUse = DenseMap<BBVRegPair, unsigned>;
  91. VRegPHIUse VRegPHIUseCount;
  92. // Defs of PHI sources which are implicit_def.
  93. SmallPtrSet<MachineInstr*, 4> ImpDefs;
  94. // Map reusable lowered PHI node -> incoming join register.
  95. using LoweredPHIMap =
  96. DenseMap<MachineInstr*, unsigned, MachineInstrExpressionTrait>;
  97. LoweredPHIMap LoweredPHIs;
  98. };
  99. } // end anonymous namespace
  100. STATISTIC(NumLowered, "Number of phis lowered");
  101. STATISTIC(NumCriticalEdgesSplit, "Number of critical edges split");
  102. STATISTIC(NumReused, "Number of reused lowered phis");
  103. char PHIElimination::ID = 0;
  104. char& llvm::PHIEliminationID = PHIElimination::ID;
  105. INITIALIZE_PASS_BEGIN(PHIElimination, DEBUG_TYPE,
  106. "Eliminate PHI nodes for register allocation",
  107. false, false)
  108. INITIALIZE_PASS_DEPENDENCY(LiveVariables)
  109. INITIALIZE_PASS_END(PHIElimination, DEBUG_TYPE,
  110. "Eliminate PHI nodes for register allocation", false, false)
  111. void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
  112. AU.addUsedIfAvailable<LiveVariables>();
  113. AU.addPreserved<LiveVariables>();
  114. AU.addPreserved<SlotIndexes>();
  115. AU.addPreserved<LiveIntervals>();
  116. AU.addPreserved<MachineDominatorTree>();
  117. AU.addPreserved<MachineLoopInfo>();
  118. MachineFunctionPass::getAnalysisUsage(AU);
  119. }
  120. bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
  121. MRI = &MF.getRegInfo();
  122. LV = getAnalysisIfAvailable<LiveVariables>();
  123. LIS = getAnalysisIfAvailable<LiveIntervals>();
  124. bool Changed = false;
  125. // This pass takes the function out of SSA form.
  126. MRI->leaveSSA();
  127. // Split critical edges to help the coalescer.
  128. if (!DisableEdgeSplitting && (LV || LIS)) {
  129. MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
  130. for (auto &MBB : MF)
  131. Changed |= SplitPHIEdges(MF, MBB, MLI);
  132. }
  133. // Populate VRegPHIUseCount
  134. analyzePHINodes(MF);
  135. // Eliminate PHI instructions by inserting copies into predecessor blocks.
  136. for (auto &MBB : MF)
  137. Changed |= EliminatePHINodes(MF, MBB);
  138. // Remove dead IMPLICIT_DEF instructions.
  139. for (MachineInstr *DefMI : ImpDefs) {
  140. Register DefReg = DefMI->getOperand(0).getReg();
  141. if (MRI->use_nodbg_empty(DefReg)) {
  142. if (LIS)
  143. LIS->RemoveMachineInstrFromMaps(*DefMI);
  144. DefMI->eraseFromParent();
  145. }
  146. }
  147. // Clean up the lowered PHI instructions.
  148. for (auto &I : LoweredPHIs) {
  149. if (LIS)
  150. LIS->RemoveMachineInstrFromMaps(*I.first);
  151. MF.DeleteMachineInstr(I.first);
  152. }
  153. LoweredPHIs.clear();
  154. ImpDefs.clear();
  155. VRegPHIUseCount.clear();
  156. MF.getProperties().set(MachineFunctionProperties::Property::NoPHIs);
  157. return Changed;
  158. }
  159. /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
  160. /// predecessor basic blocks.
  161. bool PHIElimination::EliminatePHINodes(MachineFunction &MF,
  162. MachineBasicBlock &MBB) {
  163. if (MBB.empty() || !MBB.front().isPHI())
  164. return false; // Quick exit for basic blocks without PHIs.
  165. // Get an iterator to the last PHI node.
  166. MachineBasicBlock::iterator LastPHIIt =
  167. std::prev(MBB.SkipPHIsAndLabels(MBB.begin()));
  168. while (MBB.front().isPHI())
  169. LowerPHINode(MBB, LastPHIIt);
  170. return true;
  171. }
  172. /// Return true if all defs of VirtReg are implicit-defs.
  173. /// This includes registers with no defs.
  174. static bool isImplicitlyDefined(unsigned VirtReg,
  175. const MachineRegisterInfo &MRI) {
  176. for (MachineInstr &DI : MRI.def_instructions(VirtReg))
  177. if (!DI.isImplicitDef())
  178. return false;
  179. return true;
  180. }
  181. /// Return true if all sources of the phi node are implicit_def's, or undef's.
  182. static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
  183. const MachineRegisterInfo &MRI) {
  184. for (unsigned I = 1, E = MPhi.getNumOperands(); I != E; I += 2) {
  185. const MachineOperand &MO = MPhi.getOperand(I);
  186. if (!isImplicitlyDefined(MO.getReg(), MRI) && !MO.isUndef())
  187. return false;
  188. }
  189. return true;
  190. }
  191. /// LowerPHINode - Lower the PHI node at the top of the specified block.
  192. void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
  193. MachineBasicBlock::iterator LastPHIIt) {
  194. ++NumLowered;
  195. MachineBasicBlock::iterator AfterPHIsIt = std::next(LastPHIIt);
  196. // Unlink the PHI node from the basic block, but don't delete the PHI yet.
  197. MachineInstr *MPhi = MBB.remove(&*MBB.begin());
  198. unsigned NumSrcs = (MPhi->getNumOperands() - 1) / 2;
  199. Register DestReg = MPhi->getOperand(0).getReg();
  200. assert(MPhi->getOperand(0).getSubReg() == 0 && "Can't handle sub-reg PHIs");
  201. bool isDead = MPhi->getOperand(0).isDead();
  202. // Create a new register for the incoming PHI arguments.
  203. MachineFunction &MF = *MBB.getParent();
  204. unsigned IncomingReg = 0;
  205. bool reusedIncoming = false; // Is IncomingReg reused from an earlier PHI?
  206. // Insert a register to register copy at the top of the current block (but
  207. // after any remaining phi nodes) which copies the new incoming register
  208. // into the phi node destination.
  209. MachineInstr *PHICopy = nullptr;
  210. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  211. if (allPhiOperandsUndefined(*MPhi, *MRI))
  212. // If all sources of a PHI node are implicit_def or undef uses, just emit an
  213. // implicit_def instead of a copy.
  214. PHICopy = BuildMI(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
  215. TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
  216. else {
  217. // Can we reuse an earlier PHI node? This only happens for critical edges,
  218. // typically those created by tail duplication.
  219. unsigned &entry = LoweredPHIs[MPhi];
  220. if (entry) {
  221. // An identical PHI node was already lowered. Reuse the incoming register.
  222. IncomingReg = entry;
  223. reusedIncoming = true;
  224. ++NumReused;
  225. LLVM_DEBUG(dbgs() << "Reusing " << printReg(IncomingReg) << " for "
  226. << *MPhi);
  227. } else {
  228. const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(DestReg);
  229. entry = IncomingReg = MF.getRegInfo().createVirtualRegister(RC);
  230. }
  231. // Give the target possiblity to handle special cases fallthrough otherwise
  232. PHICopy = TII->createPHIDestinationCopy(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
  233. IncomingReg, DestReg);
  234. }
  235. // Update live variable information if there is any.
  236. if (LV) {
  237. if (IncomingReg) {
  238. LiveVariables::VarInfo &VI = LV->getVarInfo(IncomingReg);
  239. // Increment use count of the newly created virtual register.
  240. LV->setPHIJoin(IncomingReg);
  241. // When we are reusing the incoming register, it may already have been
  242. // killed in this block. The old kill will also have been inserted at
  243. // AfterPHIsIt, so it appears before the current PHICopy.
  244. if (reusedIncoming)
  245. if (MachineInstr *OldKill = VI.findKill(&MBB)) {
  246. LLVM_DEBUG(dbgs() << "Remove old kill from " << *OldKill);
  247. LV->removeVirtualRegisterKilled(IncomingReg, *OldKill);
  248. LLVM_DEBUG(MBB.dump());
  249. }
  250. // Add information to LiveVariables to know that the incoming value is
  251. // killed. Note that because the value is defined in several places (once
  252. // each for each incoming block), the "def" block and instruction fields
  253. // for the VarInfo is not filled in.
  254. LV->addVirtualRegisterKilled(IncomingReg, *PHICopy);
  255. }
  256. // Since we are going to be deleting the PHI node, if it is the last use of
  257. // any registers, or if the value itself is dead, we need to move this
  258. // information over to the new copy we just inserted.
  259. LV->removeVirtualRegistersKilled(*MPhi);
  260. // If the result is dead, update LV.
  261. if (isDead) {
  262. LV->addVirtualRegisterDead(DestReg, *PHICopy);
  263. LV->removeVirtualRegisterDead(DestReg, *MPhi);
  264. }
  265. }
  266. // Update LiveIntervals for the new copy or implicit def.
  267. if (LIS) {
  268. SlotIndex DestCopyIndex = LIS->InsertMachineInstrInMaps(*PHICopy);
  269. SlotIndex MBBStartIndex = LIS->getMBBStartIdx(&MBB);
  270. if (IncomingReg) {
  271. // Add the region from the beginning of MBB to the copy instruction to
  272. // IncomingReg's live interval.
  273. LiveInterval &IncomingLI = LIS->createEmptyInterval(IncomingReg);
  274. VNInfo *IncomingVNI = IncomingLI.getVNInfoAt(MBBStartIndex);
  275. if (!IncomingVNI)
  276. IncomingVNI = IncomingLI.getNextValue(MBBStartIndex,
  277. LIS->getVNInfoAllocator());
  278. IncomingLI.addSegment(LiveInterval::Segment(MBBStartIndex,
  279. DestCopyIndex.getRegSlot(),
  280. IncomingVNI));
  281. }
  282. LiveInterval &DestLI = LIS->getInterval(DestReg);
  283. assert(DestLI.begin() != DestLI.end() &&
  284. "PHIs should have nonempty LiveIntervals.");
  285. if (DestLI.endIndex().isDead()) {
  286. // A dead PHI's live range begins and ends at the start of the MBB, but
  287. // the lowered copy, which will still be dead, needs to begin and end at
  288. // the copy instruction.
  289. VNInfo *OrigDestVNI = DestLI.getVNInfoAt(MBBStartIndex);
  290. assert(OrigDestVNI && "PHI destination should be live at block entry.");
  291. DestLI.removeSegment(MBBStartIndex, MBBStartIndex.getDeadSlot());
  292. DestLI.createDeadDef(DestCopyIndex.getRegSlot(),
  293. LIS->getVNInfoAllocator());
  294. DestLI.removeValNo(OrigDestVNI);
  295. } else {
  296. // Otherwise, remove the region from the beginning of MBB to the copy
  297. // instruction from DestReg's live interval.
  298. DestLI.removeSegment(MBBStartIndex, DestCopyIndex.getRegSlot());
  299. VNInfo *DestVNI = DestLI.getVNInfoAt(DestCopyIndex.getRegSlot());
  300. assert(DestVNI && "PHI destination should be live at its definition.");
  301. DestVNI->def = DestCopyIndex.getRegSlot();
  302. }
  303. }
  304. // Adjust the VRegPHIUseCount map to account for the removal of this PHI node.
  305. for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2)
  306. --VRegPHIUseCount[BBVRegPair(MPhi->getOperand(i+1).getMBB()->getNumber(),
  307. MPhi->getOperand(i).getReg())];
  308. // Now loop over all of the incoming arguments, changing them to copy into the
  309. // IncomingReg register in the corresponding predecessor basic block.
  310. SmallPtrSet<MachineBasicBlock*, 8> MBBsInsertedInto;
  311. for (int i = NumSrcs - 1; i >= 0; --i) {
  312. Register SrcReg = MPhi->getOperand(i * 2 + 1).getReg();
  313. unsigned SrcSubReg = MPhi->getOperand(i*2+1).getSubReg();
  314. bool SrcUndef = MPhi->getOperand(i*2+1).isUndef() ||
  315. isImplicitlyDefined(SrcReg, *MRI);
  316. assert(Register::isVirtualRegister(SrcReg) &&
  317. "Machine PHI Operands must all be virtual registers!");
  318. // Get the MachineBasicBlock equivalent of the BasicBlock that is the source
  319. // path the PHI.
  320. MachineBasicBlock &opBlock = *MPhi->getOperand(i*2+2).getMBB();
  321. // Check to make sure we haven't already emitted the copy for this block.
  322. // This can happen because PHI nodes may have multiple entries for the same
  323. // basic block.
  324. if (!MBBsInsertedInto.insert(&opBlock).second)
  325. continue; // If the copy has already been emitted, we're done.
  326. // Find a safe location to insert the copy, this may be the first terminator
  327. // in the block (or end()).
  328. MachineBasicBlock::iterator InsertPos =
  329. findPHICopyInsertPoint(&opBlock, &MBB, SrcReg);
  330. // Insert the copy.
  331. MachineInstr *NewSrcInstr = nullptr;
  332. if (!reusedIncoming && IncomingReg) {
  333. if (SrcUndef) {
  334. // The source register is undefined, so there is no need for a real
  335. // COPY, but we still need to ensure joint dominance by defs.
  336. // Insert an IMPLICIT_DEF instruction.
  337. NewSrcInstr = BuildMI(opBlock, InsertPos, MPhi->getDebugLoc(),
  338. TII->get(TargetOpcode::IMPLICIT_DEF),
  339. IncomingReg);
  340. // Clean up the old implicit-def, if there even was one.
  341. if (MachineInstr *DefMI = MRI->getVRegDef(SrcReg))
  342. if (DefMI->isImplicitDef())
  343. ImpDefs.insert(DefMI);
  344. } else {
  345. NewSrcInstr =
  346. TII->createPHISourceCopy(opBlock, InsertPos, MPhi->getDebugLoc(),
  347. SrcReg, SrcSubReg, IncomingReg);
  348. }
  349. }
  350. // We only need to update the LiveVariables kill of SrcReg if this was the
  351. // last PHI use of SrcReg to be lowered on this CFG edge and it is not live
  352. // out of the predecessor. We can also ignore undef sources.
  353. if (LV && !SrcUndef &&
  354. !VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)] &&
  355. !LV->isLiveOut(SrcReg, opBlock)) {
  356. // We want to be able to insert a kill of the register if this PHI (aka,
  357. // the copy we just inserted) is the last use of the source value. Live
  358. // variable analysis conservatively handles this by saying that the value
  359. // is live until the end of the block the PHI entry lives in. If the value
  360. // really is dead at the PHI copy, there will be no successor blocks which
  361. // have the value live-in.
  362. // Okay, if we now know that the value is not live out of the block, we
  363. // can add a kill marker in this block saying that it kills the incoming
  364. // value!
  365. // In our final twist, we have to decide which instruction kills the
  366. // register. In most cases this is the copy, however, terminator
  367. // instructions at the end of the block may also use the value. In this
  368. // case, we should mark the last such terminator as being the killing
  369. // block, not the copy.
  370. MachineBasicBlock::iterator KillInst = opBlock.end();
  371. MachineBasicBlock::iterator FirstTerm = opBlock.getFirstTerminator();
  372. for (MachineBasicBlock::iterator Term = FirstTerm;
  373. Term != opBlock.end(); ++Term) {
  374. if (Term->readsRegister(SrcReg))
  375. KillInst = Term;
  376. }
  377. if (KillInst == opBlock.end()) {
  378. // No terminator uses the register.
  379. if (reusedIncoming || !IncomingReg) {
  380. // We may have to rewind a bit if we didn't insert a copy this time.
  381. KillInst = FirstTerm;
  382. while (KillInst != opBlock.begin()) {
  383. --KillInst;
  384. if (KillInst->isDebugInstr())
  385. continue;
  386. if (KillInst->readsRegister(SrcReg))
  387. break;
  388. }
  389. } else {
  390. // We just inserted this copy.
  391. KillInst = NewSrcInstr;
  392. }
  393. }
  394. assert(KillInst->readsRegister(SrcReg) && "Cannot find kill instruction");
  395. // Finally, mark it killed.
  396. LV->addVirtualRegisterKilled(SrcReg, *KillInst);
  397. // This vreg no longer lives all of the way through opBlock.
  398. unsigned opBlockNum = opBlock.getNumber();
  399. LV->getVarInfo(SrcReg).AliveBlocks.reset(opBlockNum);
  400. }
  401. if (LIS) {
  402. if (NewSrcInstr) {
  403. LIS->InsertMachineInstrInMaps(*NewSrcInstr);
  404. LIS->addSegmentToEndOfBlock(IncomingReg, *NewSrcInstr);
  405. }
  406. if (!SrcUndef &&
  407. !VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)]) {
  408. LiveInterval &SrcLI = LIS->getInterval(SrcReg);
  409. bool isLiveOut = false;
  410. for (MachineBasicBlock::succ_iterator SI = opBlock.succ_begin(),
  411. SE = opBlock.succ_end(); SI != SE; ++SI) {
  412. SlotIndex startIdx = LIS->getMBBStartIdx(*SI);
  413. VNInfo *VNI = SrcLI.getVNInfoAt(startIdx);
  414. // Definitions by other PHIs are not truly live-in for our purposes.
  415. if (VNI && VNI->def != startIdx) {
  416. isLiveOut = true;
  417. break;
  418. }
  419. }
  420. if (!isLiveOut) {
  421. MachineBasicBlock::iterator KillInst = opBlock.end();
  422. MachineBasicBlock::iterator FirstTerm = opBlock.getFirstTerminator();
  423. for (MachineBasicBlock::iterator Term = FirstTerm;
  424. Term != opBlock.end(); ++Term) {
  425. if (Term->readsRegister(SrcReg))
  426. KillInst = Term;
  427. }
  428. if (KillInst == opBlock.end()) {
  429. // No terminator uses the register.
  430. if (reusedIncoming || !IncomingReg) {
  431. // We may have to rewind a bit if we didn't just insert a copy.
  432. KillInst = FirstTerm;
  433. while (KillInst != opBlock.begin()) {
  434. --KillInst;
  435. if (KillInst->isDebugInstr())
  436. continue;
  437. if (KillInst->readsRegister(SrcReg))
  438. break;
  439. }
  440. } else {
  441. // We just inserted this copy.
  442. KillInst = std::prev(InsertPos);
  443. }
  444. }
  445. assert(KillInst->readsRegister(SrcReg) &&
  446. "Cannot find kill instruction");
  447. SlotIndex LastUseIndex = LIS->getInstructionIndex(*KillInst);
  448. SrcLI.removeSegment(LastUseIndex.getRegSlot(),
  449. LIS->getMBBEndIdx(&opBlock));
  450. }
  451. }
  452. }
  453. }
  454. // Really delete the PHI instruction now, if it is not in the LoweredPHIs map.
  455. if (reusedIncoming || !IncomingReg) {
  456. if (LIS)
  457. LIS->RemoveMachineInstrFromMaps(*MPhi);
  458. MF.DeleteMachineInstr(MPhi);
  459. }
  460. }
  461. /// analyzePHINodes - Gather information about the PHI nodes in here. In
  462. /// particular, we want to map the number of uses of a virtual register which is
  463. /// used in a PHI node. We map that to the BB the vreg is coming from. This is
  464. /// used later to determine when the vreg is killed in the BB.
  465. void PHIElimination::analyzePHINodes(const MachineFunction& MF) {
  466. for (const auto &MBB : MF)
  467. for (const auto &BBI : MBB) {
  468. if (!BBI.isPHI())
  469. break;
  470. for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2)
  471. ++VRegPHIUseCount[BBVRegPair(BBI.getOperand(i+1).getMBB()->getNumber(),
  472. BBI.getOperand(i).getReg())];
  473. }
  474. }
  475. bool PHIElimination::SplitPHIEdges(MachineFunction &MF,
  476. MachineBasicBlock &MBB,
  477. MachineLoopInfo *MLI) {
  478. if (MBB.empty() || !MBB.front().isPHI() || MBB.isEHPad())
  479. return false; // Quick exit for basic blocks without PHIs.
  480. const MachineLoop *CurLoop = MLI ? MLI->getLoopFor(&MBB) : nullptr;
  481. bool IsLoopHeader = CurLoop && &MBB == CurLoop->getHeader();
  482. bool Changed = false;
  483. for (MachineBasicBlock::iterator BBI = MBB.begin(), BBE = MBB.end();
  484. BBI != BBE && BBI->isPHI(); ++BBI) {
  485. for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
  486. Register Reg = BBI->getOperand(i).getReg();
  487. MachineBasicBlock *PreMBB = BBI->getOperand(i+1).getMBB();
  488. // Is there a critical edge from PreMBB to MBB?
  489. if (PreMBB->succ_size() == 1)
  490. continue;
  491. // Avoid splitting backedges of loops. It would introduce small
  492. // out-of-line blocks into the loop which is very bad for code placement.
  493. if (PreMBB == &MBB && !SplitAllCriticalEdges)
  494. continue;
  495. const MachineLoop *PreLoop = MLI ? MLI->getLoopFor(PreMBB) : nullptr;
  496. if (IsLoopHeader && PreLoop == CurLoop && !SplitAllCriticalEdges)
  497. continue;
  498. // LV doesn't consider a phi use live-out, so isLiveOut only returns true
  499. // when the source register is live-out for some other reason than a phi
  500. // use. That means the copy we will insert in PreMBB won't be a kill, and
  501. // there is a risk it may not be coalesced away.
  502. //
  503. // If the copy would be a kill, there is no need to split the edge.
  504. bool ShouldSplit = isLiveOutPastPHIs(Reg, PreMBB);
  505. if (!ShouldSplit && !NoPhiElimLiveOutEarlyExit)
  506. continue;
  507. if (ShouldSplit) {
  508. LLVM_DEBUG(dbgs() << printReg(Reg) << " live-out before critical edge "
  509. << printMBBReference(*PreMBB) << " -> "
  510. << printMBBReference(MBB) << ": " << *BBI);
  511. }
  512. // If Reg is not live-in to MBB, it means it must be live-in to some
  513. // other PreMBB successor, and we can avoid the interference by splitting
  514. // the edge.
  515. //
  516. // If Reg *is* live-in to MBB, the interference is inevitable and a copy
  517. // is likely to be left after coalescing. If we are looking at a loop
  518. // exiting edge, split it so we won't insert code in the loop, otherwise
  519. // don't bother.
  520. ShouldSplit = ShouldSplit && !isLiveIn(Reg, &MBB);
  521. // Check for a loop exiting edge.
  522. if (!ShouldSplit && CurLoop != PreLoop) {
  523. LLVM_DEBUG({
  524. dbgs() << "Split wouldn't help, maybe avoid loop copies?\n";
  525. if (PreLoop)
  526. dbgs() << "PreLoop: " << *PreLoop;
  527. if (CurLoop)
  528. dbgs() << "CurLoop: " << *CurLoop;
  529. });
  530. // This edge could be entering a loop, exiting a loop, or it could be
  531. // both: Jumping directly form one loop to the header of a sibling
  532. // loop.
  533. // Split unless this edge is entering CurLoop from an outer loop.
  534. ShouldSplit = PreLoop && !PreLoop->contains(CurLoop);
  535. }
  536. if (!ShouldSplit && !SplitAllCriticalEdges)
  537. continue;
  538. if (!PreMBB->SplitCriticalEdge(&MBB, *this)) {
  539. LLVM_DEBUG(dbgs() << "Failed to split critical edge.\n");
  540. continue;
  541. }
  542. Changed = true;
  543. ++NumCriticalEdgesSplit;
  544. }
  545. }
  546. return Changed;
  547. }
  548. bool PHIElimination::isLiveIn(unsigned Reg, const MachineBasicBlock *MBB) {
  549. assert((LV || LIS) &&
  550. "isLiveIn() requires either LiveVariables or LiveIntervals");
  551. if (LIS)
  552. return LIS->isLiveInToMBB(LIS->getInterval(Reg), MBB);
  553. else
  554. return LV->isLiveIn(Reg, *MBB);
  555. }
  556. bool PHIElimination::isLiveOutPastPHIs(unsigned Reg,
  557. const MachineBasicBlock *MBB) {
  558. assert((LV || LIS) &&
  559. "isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals");
  560. // LiveVariables considers uses in PHIs to be in the predecessor basic block,
  561. // so that a register used only in a PHI is not live out of the block. In
  562. // contrast, LiveIntervals considers uses in PHIs to be on the edge rather than
  563. // in the predecessor basic block, so that a register used only in a PHI is live
  564. // out of the block.
  565. if (LIS) {
  566. const LiveInterval &LI = LIS->getInterval(Reg);
  567. for (const MachineBasicBlock *SI : MBB->successors())
  568. if (LI.liveAt(LIS->getMBBStartIdx(SI)))
  569. return true;
  570. return false;
  571. } else {
  572. return LV->isLiveOut(Reg, *MBB);
  573. }
  574. }