PHIElimination.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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/TargetOpcodes.h"
  34. #include "llvm/CodeGen/TargetRegisterInfo.h"
  35. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  36. #include "llvm/Pass.h"
  37. #include "llvm/Support/CommandLine.h"
  38. #include "llvm/Support/Debug.h"
  39. #include "llvm/Support/raw_ostream.h"
  40. #include <cassert>
  41. #include <iterator>
  42. #include <utility>
  43. using namespace llvm;
  44. #define DEBUG_TYPE "phi-node-elimination"
  45. static cl::opt<bool>
  46. DisableEdgeSplitting("disable-phi-elim-edge-splitting", cl::init(false),
  47. cl::Hidden, cl::desc("Disable critical edge splitting "
  48. "during PHI elimination"));
  49. static cl::opt<bool>
  50. SplitAllCriticalEdges("phi-elim-split-all-critical-edges", cl::init(false),
  51. cl::Hidden, cl::desc("Split all critical edges during "
  52. "PHI elimination"));
  53. static cl::opt<bool> NoPhiElimLiveOutEarlyExit(
  54. "no-phi-elim-live-out-early-exit", cl::init(false), cl::Hidden,
  55. cl::desc("Do not use an early exit if isLiveOutPastPHIs returns true."));
  56. namespace {
  57. class PHIElimination : public MachineFunctionPass {
  58. MachineRegisterInfo *MRI; // Machine register information
  59. LiveVariables *LV;
  60. LiveIntervals *LIS;
  61. public:
  62. static char ID; // Pass identification, replacement for typeid
  63. PHIElimination() : MachineFunctionPass(ID) {
  64. initializePHIEliminationPass(*PassRegistry::getPassRegistry());
  65. }
  66. bool runOnMachineFunction(MachineFunction &MF) override;
  67. void getAnalysisUsage(AnalysisUsage &AU) const override;
  68. private:
  69. /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
  70. /// in predecessor basic blocks.
  71. bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
  72. void LowerPHINode(MachineBasicBlock &MBB,
  73. MachineBasicBlock::iterator LastPHIIt);
  74. /// analyzePHINodes - Gather information about the PHI nodes in
  75. /// here. In particular, we want to map the number of uses of a virtual
  76. /// register which is used in a PHI node. We map that to the BB the
  77. /// vreg is coming from. This is used later to determine when the vreg
  78. /// is killed in the BB.
  79. void analyzePHINodes(const MachineFunction& MF);
  80. /// Split critical edges where necessary for good coalescer performance.
  81. bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
  82. MachineLoopInfo *MLI,
  83. std::vector<SparseBitVector<>> *LiveInSets);
  84. // These functions are temporary abstractions around LiveVariables and
  85. // LiveIntervals, so they can go away when LiveVariables does.
  86. bool isLiveIn(Register Reg, const MachineBasicBlock *MBB);
  87. bool isLiveOutPastPHIs(Register Reg, const MachineBasicBlock *MBB);
  88. using BBVRegPair = std::pair<unsigned, Register>;
  89. using VRegPHIUse = DenseMap<BBVRegPair, unsigned>;
  90. // Count the number of non-undef PHI uses of each register in each BB.
  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. // Split critical edges to help the coalescer.
  126. if (!DisableEdgeSplitting && (LV || LIS)) {
  127. // A set of live-in regs for each MBB which is used to update LV
  128. // efficiently also with large functions.
  129. std::vector<SparseBitVector<>> LiveInSets;
  130. if (LV) {
  131. LiveInSets.resize(MF.size());
  132. for (unsigned Index = 0, e = MRI->getNumVirtRegs(); Index != e; ++Index) {
  133. // Set the bit for this register for each MBB where it is
  134. // live-through or live-in (killed).
  135. Register VirtReg = Register::index2VirtReg(Index);
  136. MachineInstr *DefMI = MRI->getVRegDef(VirtReg);
  137. if (!DefMI)
  138. continue;
  139. LiveVariables::VarInfo &VI = LV->getVarInfo(VirtReg);
  140. SparseBitVector<>::iterator AliveBlockItr = VI.AliveBlocks.begin();
  141. SparseBitVector<>::iterator EndItr = VI.AliveBlocks.end();
  142. while (AliveBlockItr != EndItr) {
  143. unsigned BlockNum = *(AliveBlockItr++);
  144. LiveInSets[BlockNum].set(Index);
  145. }
  146. // The register is live into an MBB in which it is killed but not
  147. // defined. See comment for VarInfo in LiveVariables.h.
  148. MachineBasicBlock *DefMBB = DefMI->getParent();
  149. if (VI.Kills.size() > 1 ||
  150. (!VI.Kills.empty() && VI.Kills.front()->getParent() != DefMBB))
  151. for (auto *MI : VI.Kills)
  152. LiveInSets[MI->getParent()->getNumber()].set(Index);
  153. }
  154. }
  155. MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
  156. for (auto &MBB : MF)
  157. Changed |= SplitPHIEdges(MF, MBB, MLI, (LV ? &LiveInSets : nullptr));
  158. }
  159. // This pass takes the function out of SSA form.
  160. MRI->leaveSSA();
  161. // Populate VRegPHIUseCount
  162. analyzePHINodes(MF);
  163. // Eliminate PHI instructions by inserting copies into predecessor blocks.
  164. for (auto &MBB : MF)
  165. Changed |= EliminatePHINodes(MF, MBB);
  166. // Remove dead IMPLICIT_DEF instructions.
  167. for (MachineInstr *DefMI : ImpDefs) {
  168. Register DefReg = DefMI->getOperand(0).getReg();
  169. if (MRI->use_nodbg_empty(DefReg)) {
  170. if (LIS)
  171. LIS->RemoveMachineInstrFromMaps(*DefMI);
  172. DefMI->eraseFromParent();
  173. }
  174. }
  175. // Clean up the lowered PHI instructions.
  176. for (auto &I : LoweredPHIs) {
  177. if (LIS)
  178. LIS->RemoveMachineInstrFromMaps(*I.first);
  179. MF.deleteMachineInstr(I.first);
  180. }
  181. // TODO: we should use the incremental DomTree updater here.
  182. if (Changed)
  183. if (auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>())
  184. MDT->getBase().recalculate(MF);
  185. LoweredPHIs.clear();
  186. ImpDefs.clear();
  187. VRegPHIUseCount.clear();
  188. MF.getProperties().set(MachineFunctionProperties::Property::NoPHIs);
  189. return Changed;
  190. }
  191. /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
  192. /// predecessor basic blocks.
  193. bool PHIElimination::EliminatePHINodes(MachineFunction &MF,
  194. MachineBasicBlock &MBB) {
  195. if (MBB.empty() || !MBB.front().isPHI())
  196. return false; // Quick exit for basic blocks without PHIs.
  197. // Get an iterator to the last PHI node.
  198. MachineBasicBlock::iterator LastPHIIt =
  199. std::prev(MBB.SkipPHIsAndLabels(MBB.begin()));
  200. while (MBB.front().isPHI())
  201. LowerPHINode(MBB, LastPHIIt);
  202. return true;
  203. }
  204. /// Return true if all defs of VirtReg are implicit-defs.
  205. /// This includes registers with no defs.
  206. static bool isImplicitlyDefined(unsigned VirtReg,
  207. const MachineRegisterInfo &MRI) {
  208. for (MachineInstr &DI : MRI.def_instructions(VirtReg))
  209. if (!DI.isImplicitDef())
  210. return false;
  211. return true;
  212. }
  213. /// Return true if all sources of the phi node are implicit_def's, or undef's.
  214. static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
  215. const MachineRegisterInfo &MRI) {
  216. for (unsigned I = 1, E = MPhi.getNumOperands(); I != E; I += 2) {
  217. const MachineOperand &MO = MPhi.getOperand(I);
  218. if (!isImplicitlyDefined(MO.getReg(), MRI) && !MO.isUndef())
  219. return false;
  220. }
  221. return true;
  222. }
  223. /// LowerPHINode - Lower the PHI node at the top of the specified block.
  224. void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
  225. MachineBasicBlock::iterator LastPHIIt) {
  226. ++NumLowered;
  227. MachineBasicBlock::iterator AfterPHIsIt = std::next(LastPHIIt);
  228. // Unlink the PHI node from the basic block, but don't delete the PHI yet.
  229. MachineInstr *MPhi = MBB.remove(&*MBB.begin());
  230. unsigned NumSrcs = (MPhi->getNumOperands() - 1) / 2;
  231. Register DestReg = MPhi->getOperand(0).getReg();
  232. assert(MPhi->getOperand(0).getSubReg() == 0 && "Can't handle sub-reg PHIs");
  233. bool isDead = MPhi->getOperand(0).isDead();
  234. // Create a new register for the incoming PHI arguments.
  235. MachineFunction &MF = *MBB.getParent();
  236. unsigned IncomingReg = 0;
  237. bool reusedIncoming = false; // Is IncomingReg reused from an earlier PHI?
  238. // Insert a register to register copy at the top of the current block (but
  239. // after any remaining phi nodes) which copies the new incoming register
  240. // into the phi node destination.
  241. MachineInstr *PHICopy = nullptr;
  242. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  243. if (allPhiOperandsUndefined(*MPhi, *MRI))
  244. // If all sources of a PHI node are implicit_def or undef uses, just emit an
  245. // implicit_def instead of a copy.
  246. PHICopy = BuildMI(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
  247. TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
  248. else {
  249. // Can we reuse an earlier PHI node? This only happens for critical edges,
  250. // typically those created by tail duplication.
  251. unsigned &entry = LoweredPHIs[MPhi];
  252. if (entry) {
  253. // An identical PHI node was already lowered. Reuse the incoming register.
  254. IncomingReg = entry;
  255. reusedIncoming = true;
  256. ++NumReused;
  257. LLVM_DEBUG(dbgs() << "Reusing " << printReg(IncomingReg) << " for "
  258. << *MPhi);
  259. } else {
  260. const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(DestReg);
  261. entry = IncomingReg = MF.getRegInfo().createVirtualRegister(RC);
  262. }
  263. // Give the target possiblity to handle special cases fallthrough otherwise
  264. PHICopy = TII->createPHIDestinationCopy(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
  265. IncomingReg, DestReg);
  266. }
  267. if (MPhi->peekDebugInstrNum()) {
  268. // If referred to by debug-info, store where this PHI was.
  269. MachineFunction *MF = MBB.getParent();
  270. unsigned ID = MPhi->peekDebugInstrNum();
  271. auto P = MachineFunction::DebugPHIRegallocPos(&MBB, IncomingReg, 0);
  272. auto Res = MF->DebugPHIPositions.insert({ID, P});
  273. assert(Res.second);
  274. (void)Res;
  275. }
  276. // Update live variable information if there is any.
  277. if (LV) {
  278. if (IncomingReg) {
  279. LiveVariables::VarInfo &VI = LV->getVarInfo(IncomingReg);
  280. // Increment use count of the newly created virtual register.
  281. LV->setPHIJoin(IncomingReg);
  282. MachineInstr *OldKill = nullptr;
  283. bool IsPHICopyAfterOldKill = false;
  284. if (reusedIncoming && (OldKill = VI.findKill(&MBB))) {
  285. // Calculate whether the PHICopy is after the OldKill.
  286. // In general, the PHICopy is inserted as the first non-phi instruction
  287. // by default, so it's before the OldKill. But some Target hooks for
  288. // createPHIDestinationCopy() may modify the default insert position of
  289. // PHICopy.
  290. for (auto I = MBB.SkipPHIsAndLabels(MBB.begin()), E = MBB.end();
  291. I != E; ++I) {
  292. if (I == PHICopy)
  293. break;
  294. if (I == OldKill) {
  295. IsPHICopyAfterOldKill = true;
  296. break;
  297. }
  298. }
  299. }
  300. // When we are reusing the incoming register and it has been marked killed
  301. // by OldKill, if the PHICopy is after the OldKill, we should remove the
  302. // killed flag from OldKill.
  303. if (IsPHICopyAfterOldKill) {
  304. LLVM_DEBUG(dbgs() << "Remove old kill from " << *OldKill);
  305. LV->removeVirtualRegisterKilled(IncomingReg, *OldKill);
  306. LLVM_DEBUG(MBB.dump());
  307. }
  308. // Add information to LiveVariables to know that the first used incoming
  309. // value or the resued incoming value whose PHICopy is after the OldKIll
  310. // is killed. Note that because the value is defined in several places
  311. // (once each for each incoming block), the "def" block and instruction
  312. // fields for the VarInfo is not filled in.
  313. if (!OldKill || IsPHICopyAfterOldKill)
  314. LV->addVirtualRegisterKilled(IncomingReg, *PHICopy);
  315. }
  316. // Since we are going to be deleting the PHI node, if it is the last use of
  317. // any registers, or if the value itself is dead, we need to move this
  318. // information over to the new copy we just inserted.
  319. LV->removeVirtualRegistersKilled(*MPhi);
  320. // If the result is dead, update LV.
  321. if (isDead) {
  322. LV->addVirtualRegisterDead(DestReg, *PHICopy);
  323. LV->removeVirtualRegisterDead(DestReg, *MPhi);
  324. }
  325. }
  326. // Update LiveIntervals for the new copy or implicit def.
  327. if (LIS) {
  328. SlotIndex DestCopyIndex = LIS->InsertMachineInstrInMaps(*PHICopy);
  329. SlotIndex MBBStartIndex = LIS->getMBBStartIdx(&MBB);
  330. if (IncomingReg) {
  331. // Add the region from the beginning of MBB to the copy instruction to
  332. // IncomingReg's live interval.
  333. LiveInterval &IncomingLI = LIS->createEmptyInterval(IncomingReg);
  334. VNInfo *IncomingVNI = IncomingLI.getVNInfoAt(MBBStartIndex);
  335. if (!IncomingVNI)
  336. IncomingVNI = IncomingLI.getNextValue(MBBStartIndex,
  337. LIS->getVNInfoAllocator());
  338. IncomingLI.addSegment(LiveInterval::Segment(MBBStartIndex,
  339. DestCopyIndex.getRegSlot(),
  340. IncomingVNI));
  341. }
  342. LiveInterval &DestLI = LIS->getInterval(DestReg);
  343. assert(!DestLI.empty() && "PHIs should have nonempty LiveIntervals.");
  344. if (DestLI.endIndex().isDead()) {
  345. // A dead PHI's live range begins and ends at the start of the MBB, but
  346. // the lowered copy, which will still be dead, needs to begin and end at
  347. // the copy instruction.
  348. VNInfo *OrigDestVNI = DestLI.getVNInfoAt(MBBStartIndex);
  349. assert(OrigDestVNI && "PHI destination should be live at block entry.");
  350. DestLI.removeSegment(MBBStartIndex, MBBStartIndex.getDeadSlot());
  351. DestLI.createDeadDef(DestCopyIndex.getRegSlot(),
  352. LIS->getVNInfoAllocator());
  353. DestLI.removeValNo(OrigDestVNI);
  354. } else {
  355. // Otherwise, remove the region from the beginning of MBB to the copy
  356. // instruction from DestReg's live interval.
  357. DestLI.removeSegment(MBBStartIndex, DestCopyIndex.getRegSlot());
  358. VNInfo *DestVNI = DestLI.getVNInfoAt(DestCopyIndex.getRegSlot());
  359. assert(DestVNI && "PHI destination should be live at its definition.");
  360. DestVNI->def = DestCopyIndex.getRegSlot();
  361. }
  362. }
  363. // Adjust the VRegPHIUseCount map to account for the removal of this PHI node.
  364. for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2) {
  365. if (!MPhi->getOperand(i).isUndef()) {
  366. --VRegPHIUseCount[BBVRegPair(
  367. MPhi->getOperand(i + 1).getMBB()->getNumber(),
  368. MPhi->getOperand(i).getReg())];
  369. }
  370. }
  371. // Now loop over all of the incoming arguments, changing them to copy into the
  372. // IncomingReg register in the corresponding predecessor basic block.
  373. SmallPtrSet<MachineBasicBlock*, 8> MBBsInsertedInto;
  374. for (int i = NumSrcs - 1; i >= 0; --i) {
  375. Register SrcReg = MPhi->getOperand(i * 2 + 1).getReg();
  376. unsigned SrcSubReg = MPhi->getOperand(i*2+1).getSubReg();
  377. bool SrcUndef = MPhi->getOperand(i*2+1).isUndef() ||
  378. isImplicitlyDefined(SrcReg, *MRI);
  379. assert(SrcReg.isVirtual() &&
  380. "Machine PHI Operands must all be virtual registers!");
  381. // Get the MachineBasicBlock equivalent of the BasicBlock that is the source
  382. // path the PHI.
  383. MachineBasicBlock &opBlock = *MPhi->getOperand(i*2+2).getMBB();
  384. // Check to make sure we haven't already emitted the copy for this block.
  385. // This can happen because PHI nodes may have multiple entries for the same
  386. // basic block.
  387. if (!MBBsInsertedInto.insert(&opBlock).second)
  388. continue; // If the copy has already been emitted, we're done.
  389. MachineInstr *SrcRegDef = MRI->getVRegDef(SrcReg);
  390. if (SrcRegDef && TII->isUnspillableTerminator(SrcRegDef)) {
  391. assert(SrcRegDef->getOperand(0).isReg() &&
  392. SrcRegDef->getOperand(0).isDef() &&
  393. "Expected operand 0 to be a reg def!");
  394. // Now that the PHI's use has been removed (as the instruction was
  395. // removed) there should be no other uses of the SrcReg.
  396. assert(MRI->use_empty(SrcReg) &&
  397. "Expected a single use from UnspillableTerminator");
  398. SrcRegDef->getOperand(0).setReg(IncomingReg);
  399. // Update LiveVariables.
  400. if (LV) {
  401. LiveVariables::VarInfo &SrcVI = LV->getVarInfo(SrcReg);
  402. LiveVariables::VarInfo &IncomingVI = LV->getVarInfo(IncomingReg);
  403. IncomingVI.AliveBlocks = std::move(SrcVI.AliveBlocks);
  404. SrcVI.AliveBlocks.clear();
  405. }
  406. continue;
  407. }
  408. // Find a safe location to insert the copy, this may be the first terminator
  409. // in the block (or end()).
  410. MachineBasicBlock::iterator InsertPos =
  411. findPHICopyInsertPoint(&opBlock, &MBB, SrcReg);
  412. // Insert the copy.
  413. MachineInstr *NewSrcInstr = nullptr;
  414. if (!reusedIncoming && IncomingReg) {
  415. if (SrcUndef) {
  416. // The source register is undefined, so there is no need for a real
  417. // COPY, but we still need to ensure joint dominance by defs.
  418. // Insert an IMPLICIT_DEF instruction.
  419. NewSrcInstr = BuildMI(opBlock, InsertPos, MPhi->getDebugLoc(),
  420. TII->get(TargetOpcode::IMPLICIT_DEF),
  421. IncomingReg);
  422. // Clean up the old implicit-def, if there even was one.
  423. if (MachineInstr *DefMI = MRI->getVRegDef(SrcReg))
  424. if (DefMI->isImplicitDef())
  425. ImpDefs.insert(DefMI);
  426. } else {
  427. // Delete the debug location, since the copy is inserted into a
  428. // different basic block.
  429. NewSrcInstr = TII->createPHISourceCopy(opBlock, InsertPos, nullptr,
  430. SrcReg, SrcSubReg, IncomingReg);
  431. }
  432. }
  433. // We only need to update the LiveVariables kill of SrcReg if this was the
  434. // last PHI use of SrcReg to be lowered on this CFG edge and it is not live
  435. // out of the predecessor. We can also ignore undef sources.
  436. if (LV && !SrcUndef &&
  437. !VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)] &&
  438. !LV->isLiveOut(SrcReg, opBlock)) {
  439. // We want to be able to insert a kill of the register if this PHI (aka,
  440. // the copy we just inserted) is the last use of the source value. Live
  441. // variable analysis conservatively handles this by saying that the value
  442. // is live until the end of the block the PHI entry lives in. If the value
  443. // really is dead at the PHI copy, there will be no successor blocks which
  444. // have the value live-in.
  445. // Okay, if we now know that the value is not live out of the block, we
  446. // can add a kill marker in this block saying that it kills the incoming
  447. // value!
  448. // In our final twist, we have to decide which instruction kills the
  449. // register. In most cases this is the copy, however, terminator
  450. // instructions at the end of the block may also use the value. In this
  451. // case, we should mark the last such terminator as being the killing
  452. // block, not the copy.
  453. MachineBasicBlock::iterator KillInst = opBlock.end();
  454. for (MachineBasicBlock::iterator Term = InsertPos; Term != opBlock.end();
  455. ++Term) {
  456. if (Term->readsRegister(SrcReg))
  457. KillInst = Term;
  458. }
  459. if (KillInst == opBlock.end()) {
  460. // No terminator uses the register.
  461. if (reusedIncoming || !IncomingReg) {
  462. // We may have to rewind a bit if we didn't insert a copy this time.
  463. KillInst = InsertPos;
  464. while (KillInst != opBlock.begin()) {
  465. --KillInst;
  466. if (KillInst->isDebugInstr())
  467. continue;
  468. if (KillInst->readsRegister(SrcReg))
  469. break;
  470. }
  471. } else {
  472. // We just inserted this copy.
  473. KillInst = NewSrcInstr;
  474. }
  475. }
  476. assert(KillInst->readsRegister(SrcReg) && "Cannot find kill instruction");
  477. // Finally, mark it killed.
  478. LV->addVirtualRegisterKilled(SrcReg, *KillInst);
  479. // This vreg no longer lives all of the way through opBlock.
  480. unsigned opBlockNum = opBlock.getNumber();
  481. LV->getVarInfo(SrcReg).AliveBlocks.reset(opBlockNum);
  482. }
  483. if (LIS) {
  484. if (NewSrcInstr) {
  485. LIS->InsertMachineInstrInMaps(*NewSrcInstr);
  486. LIS->addSegmentToEndOfBlock(IncomingReg, *NewSrcInstr);
  487. }
  488. if (!SrcUndef &&
  489. !VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)]) {
  490. LiveInterval &SrcLI = LIS->getInterval(SrcReg);
  491. bool isLiveOut = false;
  492. for (MachineBasicBlock *Succ : opBlock.successors()) {
  493. SlotIndex startIdx = LIS->getMBBStartIdx(Succ);
  494. VNInfo *VNI = SrcLI.getVNInfoAt(startIdx);
  495. // Definitions by other PHIs are not truly live-in for our purposes.
  496. if (VNI && VNI->def != startIdx) {
  497. isLiveOut = true;
  498. break;
  499. }
  500. }
  501. if (!isLiveOut) {
  502. MachineBasicBlock::iterator KillInst = opBlock.end();
  503. for (MachineBasicBlock::iterator Term = InsertPos;
  504. Term != opBlock.end(); ++Term) {
  505. if (Term->readsRegister(SrcReg))
  506. KillInst = Term;
  507. }
  508. if (KillInst == opBlock.end()) {
  509. // No terminator uses the register.
  510. if (reusedIncoming || !IncomingReg) {
  511. // We may have to rewind a bit if we didn't just insert a copy.
  512. KillInst = InsertPos;
  513. while (KillInst != opBlock.begin()) {
  514. --KillInst;
  515. if (KillInst->isDebugInstr())
  516. continue;
  517. if (KillInst->readsRegister(SrcReg))
  518. break;
  519. }
  520. } else {
  521. // We just inserted this copy.
  522. KillInst = std::prev(InsertPos);
  523. }
  524. }
  525. assert(KillInst->readsRegister(SrcReg) &&
  526. "Cannot find kill instruction");
  527. SlotIndex LastUseIndex = LIS->getInstructionIndex(*KillInst);
  528. SrcLI.removeSegment(LastUseIndex.getRegSlot(),
  529. LIS->getMBBEndIdx(&opBlock));
  530. }
  531. }
  532. }
  533. }
  534. // Really delete the PHI instruction now, if it is not in the LoweredPHIs map.
  535. if (reusedIncoming || !IncomingReg) {
  536. if (LIS)
  537. LIS->RemoveMachineInstrFromMaps(*MPhi);
  538. MF.deleteMachineInstr(MPhi);
  539. }
  540. }
  541. /// analyzePHINodes - Gather information about the PHI nodes in here. In
  542. /// particular, we want to map the number of uses of a virtual register which is
  543. /// used in a PHI node. We map that to the BB the vreg is coming from. This is
  544. /// used later to determine when the vreg is killed in the BB.
  545. void PHIElimination::analyzePHINodes(const MachineFunction& MF) {
  546. for (const auto &MBB : MF) {
  547. for (const auto &BBI : MBB) {
  548. if (!BBI.isPHI())
  549. break;
  550. for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
  551. if (!BBI.getOperand(i).isUndef()) {
  552. ++VRegPHIUseCount[BBVRegPair(
  553. BBI.getOperand(i + 1).getMBB()->getNumber(),
  554. BBI.getOperand(i).getReg())];
  555. }
  556. }
  557. }
  558. }
  559. }
  560. bool PHIElimination::SplitPHIEdges(MachineFunction &MF,
  561. MachineBasicBlock &MBB,
  562. MachineLoopInfo *MLI,
  563. std::vector<SparseBitVector<>> *LiveInSets) {
  564. if (MBB.empty() || !MBB.front().isPHI() || MBB.isEHPad())
  565. return false; // Quick exit for basic blocks without PHIs.
  566. const MachineLoop *CurLoop = MLI ? MLI->getLoopFor(&MBB) : nullptr;
  567. bool IsLoopHeader = CurLoop && &MBB == CurLoop->getHeader();
  568. bool Changed = false;
  569. for (MachineBasicBlock::iterator BBI = MBB.begin(), BBE = MBB.end();
  570. BBI != BBE && BBI->isPHI(); ++BBI) {
  571. for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
  572. Register Reg = BBI->getOperand(i).getReg();
  573. MachineBasicBlock *PreMBB = BBI->getOperand(i+1).getMBB();
  574. // Is there a critical edge from PreMBB to MBB?
  575. if (PreMBB->succ_size() == 1)
  576. continue;
  577. // Avoid splitting backedges of loops. It would introduce small
  578. // out-of-line blocks into the loop which is very bad for code placement.
  579. if (PreMBB == &MBB && !SplitAllCriticalEdges)
  580. continue;
  581. const MachineLoop *PreLoop = MLI ? MLI->getLoopFor(PreMBB) : nullptr;
  582. if (IsLoopHeader && PreLoop == CurLoop && !SplitAllCriticalEdges)
  583. continue;
  584. // LV doesn't consider a phi use live-out, so isLiveOut only returns true
  585. // when the source register is live-out for some other reason than a phi
  586. // use. That means the copy we will insert in PreMBB won't be a kill, and
  587. // there is a risk it may not be coalesced away.
  588. //
  589. // If the copy would be a kill, there is no need to split the edge.
  590. bool ShouldSplit = isLiveOutPastPHIs(Reg, PreMBB);
  591. if (!ShouldSplit && !NoPhiElimLiveOutEarlyExit)
  592. continue;
  593. if (ShouldSplit) {
  594. LLVM_DEBUG(dbgs() << printReg(Reg) << " live-out before critical edge "
  595. << printMBBReference(*PreMBB) << " -> "
  596. << printMBBReference(MBB) << ": " << *BBI);
  597. }
  598. // If Reg is not live-in to MBB, it means it must be live-in to some
  599. // other PreMBB successor, and we can avoid the interference by splitting
  600. // the edge.
  601. //
  602. // If Reg *is* live-in to MBB, the interference is inevitable and a copy
  603. // is likely to be left after coalescing. If we are looking at a loop
  604. // exiting edge, split it so we won't insert code in the loop, otherwise
  605. // don't bother.
  606. ShouldSplit = ShouldSplit && !isLiveIn(Reg, &MBB);
  607. // Check for a loop exiting edge.
  608. if (!ShouldSplit && CurLoop != PreLoop) {
  609. LLVM_DEBUG({
  610. dbgs() << "Split wouldn't help, maybe avoid loop copies?\n";
  611. if (PreLoop)
  612. dbgs() << "PreLoop: " << *PreLoop;
  613. if (CurLoop)
  614. dbgs() << "CurLoop: " << *CurLoop;
  615. });
  616. // This edge could be entering a loop, exiting a loop, or it could be
  617. // both: Jumping directly form one loop to the header of a sibling
  618. // loop.
  619. // Split unless this edge is entering CurLoop from an outer loop.
  620. ShouldSplit = PreLoop && !PreLoop->contains(CurLoop);
  621. }
  622. if (!ShouldSplit && !SplitAllCriticalEdges)
  623. continue;
  624. if (!PreMBB->SplitCriticalEdge(&MBB, *this, LiveInSets)) {
  625. LLVM_DEBUG(dbgs() << "Failed to split critical edge.\n");
  626. continue;
  627. }
  628. Changed = true;
  629. ++NumCriticalEdgesSplit;
  630. }
  631. }
  632. return Changed;
  633. }
  634. bool PHIElimination::isLiveIn(Register Reg, const MachineBasicBlock *MBB) {
  635. assert((LV || LIS) &&
  636. "isLiveIn() requires either LiveVariables or LiveIntervals");
  637. if (LIS)
  638. return LIS->isLiveInToMBB(LIS->getInterval(Reg), MBB);
  639. else
  640. return LV->isLiveIn(Reg, *MBB);
  641. }
  642. bool PHIElimination::isLiveOutPastPHIs(Register Reg,
  643. const MachineBasicBlock *MBB) {
  644. assert((LV || LIS) &&
  645. "isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals");
  646. // LiveVariables considers uses in PHIs to be in the predecessor basic block,
  647. // so that a register used only in a PHI is not live out of the block. In
  648. // contrast, LiveIntervals considers uses in PHIs to be on the edge rather than
  649. // in the predecessor basic block, so that a register used only in a PHI is live
  650. // out of the block.
  651. if (LIS) {
  652. const LiveInterval &LI = LIS->getInterval(Reg);
  653. for (const MachineBasicBlock *SI : MBB->successors())
  654. if (LI.liveAt(LIS->getMBBStartIdx(SI)))
  655. return true;
  656. return false;
  657. } else {
  658. return LV->isLiveOut(Reg, *MBB);
  659. }
  660. }