PHIElimination.cpp 26 KB

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