PHIElimination.cpp 30 KB

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