xref: /llvm-project/llvm/lib/CodeGen/PHIElimination.cpp (revision 1562b70eaf6e0b95910fa684dfc53bd5ca6252e7)
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 
15 #include "llvm/CodeGen/PHIElimination.h"
16 #include "PHIEliminationUtils.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/CodeGen/LiveIntervals.h"
23 #include "llvm/CodeGen/LiveVariables.h"
24 #include "llvm/CodeGen/MachineBasicBlock.h"
25 #include "llvm/CodeGen/MachineDomTreeUpdater.h"
26 #include "llvm/CodeGen/MachineDominators.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineLoopInfo.h"
32 #include "llvm/CodeGen/MachineOperand.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/SlotIndexes.h"
35 #include "llvm/CodeGen/TargetInstrInfo.h"
36 #include "llvm/CodeGen/TargetOpcodes.h"
37 #include "llvm/CodeGen/TargetRegisterInfo.h"
38 #include "llvm/CodeGen/TargetSubtargetInfo.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <cassert>
44 #include <iterator>
45 #include <utility>
46 
47 using namespace llvm;
48 
49 #define DEBUG_TYPE "phi-node-elimination"
50 
51 static cl::opt<bool>
52     DisableEdgeSplitting("disable-phi-elim-edge-splitting", cl::init(false),
53                          cl::Hidden,
54                          cl::desc("Disable critical edge splitting "
55                                   "during PHI elimination"));
56 
57 static cl::opt<bool>
58     SplitAllCriticalEdges("phi-elim-split-all-critical-edges", cl::init(false),
59                           cl::Hidden,
60                           cl::desc("Split all critical edges during "
61                                    "PHI elimination"));
62 
63 static cl::opt<bool> NoPhiElimLiveOutEarlyExit(
64     "no-phi-elim-live-out-early-exit", cl::init(false), cl::Hidden,
65     cl::desc("Do not use an early exit if isLiveOutPastPHIs returns true."));
66 
67 namespace {
68 
69 class PHIEliminationImpl {
70   MachineRegisterInfo *MRI = nullptr; // Machine register information
71   LiveVariables *LV = nullptr;
72   LiveIntervals *LIS = nullptr;
73   MachineLoopInfo *MLI = nullptr;
74   MachineDominatorTree *MDT = nullptr;
75 
76   /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
77   /// in predecessor basic blocks.
78   bool EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB);
79 
80   void LowerPHINode(MachineBasicBlock &MBB,
81                     MachineBasicBlock::iterator LastPHIIt,
82                     bool AllEdgesCritical);
83 
84   /// analyzePHINodes - Gather information about the PHI nodes in
85   /// here. In particular, we want to map the number of uses of a virtual
86   /// register which is used in a PHI node. We map that to the BB the
87   /// vreg is coming from. This is used later to determine when the vreg
88   /// is killed in the BB.
89   void analyzePHINodes(const MachineFunction &MF);
90 
91   /// Split critical edges where necessary for good coalescer performance.
92   bool SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
93                      MachineLoopInfo *MLI,
94                      std::vector<SparseBitVector<>> *LiveInSets,
95                      MachineDomTreeUpdater &MDTU);
96 
97   // These functions are temporary abstractions around LiveVariables and
98   // LiveIntervals, so they can go away when LiveVariables does.
99   bool isLiveIn(Register Reg, const MachineBasicBlock *MBB);
100   bool isLiveOutPastPHIs(Register Reg, const MachineBasicBlock *MBB);
101 
102   using BBVRegPair = std::pair<unsigned, Register>;
103   using VRegPHIUse = DenseMap<BBVRegPair, unsigned>;
104 
105   // Count the number of non-undef PHI uses of each register in each BB.
106   VRegPHIUse VRegPHIUseCount;
107 
108   // Defs of PHI sources which are implicit_def.
109   SmallPtrSet<MachineInstr *, 4> ImpDefs;
110 
111   // Map reusable lowered PHI node -> incoming join register.
112   using LoweredPHIMap =
113       DenseMap<MachineInstr *, unsigned, MachineInstrExpressionTrait>;
114   LoweredPHIMap LoweredPHIs;
115 
116   MachineFunctionPass *P = nullptr;
117   MachineFunctionAnalysisManager *MFAM = nullptr;
118 
119 public:
120   PHIEliminationImpl(MachineFunctionPass *P) : P(P) {
121     auto *LVWrapper = P->getAnalysisIfAvailable<LiveVariablesWrapperPass>();
122     auto *LISWrapper = P->getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
123     auto *MLIWrapper = P->getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
124     auto *MDTWrapper =
125         P->getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
126     LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
127     LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
128     MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
129     MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
130   }
131 
132   PHIEliminationImpl(MachineFunction &MF, MachineFunctionAnalysisManager &AM)
133       : LV(AM.getCachedResult<LiveVariablesAnalysis>(MF)),
134         LIS(AM.getCachedResult<LiveIntervalsAnalysis>(MF)),
135         MLI(AM.getCachedResult<MachineLoopAnalysis>(MF)),
136         MDT(AM.getCachedResult<MachineDominatorTreeAnalysis>(MF)), MFAM(&AM) {}
137 
138   bool run(MachineFunction &MF);
139 };
140 
141 class PHIElimination : public MachineFunctionPass {
142 public:
143   static char ID; // Pass identification, replacement for typeid
144 
145   PHIElimination() : MachineFunctionPass(ID) {
146     initializePHIEliminationPass(*PassRegistry::getPassRegistry());
147   }
148 
149   bool runOnMachineFunction(MachineFunction &MF) override {
150     PHIEliminationImpl Impl(this);
151     return Impl.run(MF);
152   }
153 
154   MachineFunctionProperties getSetProperties() const override {
155     return MachineFunctionProperties().set(
156         MachineFunctionProperties::Property::NoPHIs);
157   }
158 
159   void getAnalysisUsage(AnalysisUsage &AU) const override;
160 };
161 
162 } // end anonymous namespace
163 
164 PreservedAnalyses
165 PHIEliminationPass::run(MachineFunction &MF,
166                         MachineFunctionAnalysisManager &MFAM) {
167   PHIEliminationImpl Impl(MF, MFAM);
168   bool Changed = Impl.run(MF);
169   if (!Changed)
170     return PreservedAnalyses::all();
171   auto PA = getMachineFunctionPassPreservedAnalyses();
172   PA.preserve<LiveIntervalsAnalysis>();
173   PA.preserve<LiveVariablesAnalysis>();
174   PA.preserve<SlotIndexesAnalysis>();
175   PA.preserve<MachineDominatorTreeAnalysis>();
176   PA.preserve<MachineLoopAnalysis>();
177   return PA;
178 }
179 
180 STATISTIC(NumLowered, "Number of phis lowered");
181 STATISTIC(NumCriticalEdgesSplit, "Number of critical edges split");
182 STATISTIC(NumReused, "Number of reused lowered phis");
183 
184 char PHIElimination::ID = 0;
185 
186 char &llvm::PHIEliminationID = PHIElimination::ID;
187 
188 INITIALIZE_PASS_BEGIN(PHIElimination, DEBUG_TYPE,
189                       "Eliminate PHI nodes for register allocation", false,
190                       false)
191 INITIALIZE_PASS_DEPENDENCY(LiveVariablesWrapperPass)
192 INITIALIZE_PASS_END(PHIElimination, DEBUG_TYPE,
193                     "Eliminate PHI nodes for register allocation", false, false)
194 
195 void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
196   AU.addUsedIfAvailable<LiveVariablesWrapperPass>();
197   AU.addPreserved<LiveVariablesWrapperPass>();
198   AU.addPreserved<SlotIndexesWrapperPass>();
199   AU.addPreserved<LiveIntervalsWrapperPass>();
200   AU.addPreserved<MachineDominatorTreeWrapperPass>();
201   AU.addPreserved<MachineLoopInfoWrapperPass>();
202   MachineFunctionPass::getAnalysisUsage(AU);
203 }
204 
205 bool PHIEliminationImpl::run(MachineFunction &MF) {
206   MRI = &MF.getRegInfo();
207 
208   MachineDominatorTree *MDT = nullptr;
209   if (P) {
210     auto *MDTWrapper =
211         P->getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
212     MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
213   } else {
214     MDT = MFAM->getCachedResult<MachineDominatorTreeAnalysis>(MF);
215   }
216   MachineDomTreeUpdater MDTU(MDT, MachineDomTreeUpdater::UpdateStrategy::Lazy);
217 
218   bool Changed = false;
219 
220   // Split critical edges to help the coalescer.
221   if (!DisableEdgeSplitting && (LV || LIS)) {
222     // A set of live-in regs for each MBB which is used to update LV
223     // efficiently also with large functions.
224     std::vector<SparseBitVector<>> LiveInSets;
225     if (LV) {
226       LiveInSets.resize(MF.size());
227       for (unsigned Index = 0, e = MRI->getNumVirtRegs(); Index != e; ++Index) {
228         // Set the bit for this register for each MBB where it is
229         // live-through or live-in (killed).
230         Register VirtReg = Register::index2VirtReg(Index);
231         MachineInstr *DefMI = MRI->getVRegDef(VirtReg);
232         if (!DefMI)
233           continue;
234         LiveVariables::VarInfo &VI = LV->getVarInfo(VirtReg);
235         SparseBitVector<>::iterator AliveBlockItr = VI.AliveBlocks.begin();
236         SparseBitVector<>::iterator EndItr = VI.AliveBlocks.end();
237         while (AliveBlockItr != EndItr) {
238           unsigned BlockNum = *(AliveBlockItr++);
239           LiveInSets[BlockNum].set(Index);
240         }
241         // The register is live into an MBB in which it is killed but not
242         // defined. See comment for VarInfo in LiveVariables.h.
243         MachineBasicBlock *DefMBB = DefMI->getParent();
244         if (VI.Kills.size() > 1 ||
245             (!VI.Kills.empty() && VI.Kills.front()->getParent() != DefMBB))
246           for (auto *MI : VI.Kills)
247             LiveInSets[MI->getParent()->getNumber()].set(Index);
248       }
249     }
250 
251     for (auto &MBB : MF)
252       Changed |=
253           SplitPHIEdges(MF, MBB, MLI, (LV ? &LiveInSets : nullptr), MDTU);
254   }
255 
256   // This pass takes the function out of SSA form.
257   MRI->leaveSSA();
258 
259   // Populate VRegPHIUseCount
260   if (LV || LIS)
261     analyzePHINodes(MF);
262 
263   // Eliminate PHI instructions by inserting copies into predecessor blocks.
264   for (auto &MBB : MF)
265     Changed |= EliminatePHINodes(MF, MBB);
266 
267   // Remove dead IMPLICIT_DEF instructions.
268   for (MachineInstr *DefMI : ImpDefs) {
269     Register DefReg = DefMI->getOperand(0).getReg();
270     if (MRI->use_nodbg_empty(DefReg)) {
271       if (LIS)
272         LIS->RemoveMachineInstrFromMaps(*DefMI);
273       DefMI->eraseFromParent();
274     }
275   }
276 
277   // Clean up the lowered PHI instructions.
278   for (auto &I : LoweredPHIs) {
279     if (LIS)
280       LIS->RemoveMachineInstrFromMaps(*I.first);
281     MF.deleteMachineInstr(I.first);
282   }
283 
284   LoweredPHIs.clear();
285   ImpDefs.clear();
286   VRegPHIUseCount.clear();
287 
288   MF.getProperties().set(MachineFunctionProperties::Property::NoPHIs);
289 
290   return Changed;
291 }
292 
293 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
294 /// predecessor basic blocks.
295 bool PHIEliminationImpl::EliminatePHINodes(MachineFunction &MF,
296                                            MachineBasicBlock &MBB) {
297   if (MBB.empty() || !MBB.front().isPHI())
298     return false; // Quick exit for basic blocks without PHIs.
299 
300   // Get an iterator to the last PHI node.
301   MachineBasicBlock::iterator LastPHIIt =
302       std::prev(MBB.SkipPHIsAndLabels(MBB.begin()));
303 
304   // If all incoming edges are critical, we try to deduplicate identical PHIs so
305   // that we generate fewer copies. If at any edge is non-critical, we either
306   // have less than two predecessors (=> no PHIs) or a predecessor has only us
307   // as a successor (=> identical PHI node can't occur in different block).
308   bool AllEdgesCritical = MBB.pred_size() >= 2;
309   for (MachineBasicBlock *Pred : MBB.predecessors()) {
310     if (Pred->succ_size() < 2) {
311       AllEdgesCritical = false;
312       break;
313     }
314   }
315 
316   while (MBB.front().isPHI())
317     LowerPHINode(MBB, LastPHIIt, AllEdgesCritical);
318 
319   return true;
320 }
321 
322 /// Return true if all defs of VirtReg are implicit-defs.
323 /// This includes registers with no defs.
324 static bool isImplicitlyDefined(unsigned VirtReg,
325                                 const MachineRegisterInfo &MRI) {
326   for (MachineInstr &DI : MRI.def_instructions(VirtReg))
327     if (!DI.isImplicitDef())
328       return false;
329   return true;
330 }
331 
332 /// Return true if all sources of the phi node are implicit_def's, or undef's.
333 static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
334                                     const MachineRegisterInfo &MRI) {
335   for (unsigned I = 1, E = MPhi.getNumOperands(); I != E; I += 2) {
336     const MachineOperand &MO = MPhi.getOperand(I);
337     if (!isImplicitlyDefined(MO.getReg(), MRI) && !MO.isUndef())
338       return false;
339   }
340   return true;
341 }
342 /// LowerPHINode - Lower the PHI node at the top of the specified block.
343 void PHIEliminationImpl::LowerPHINode(MachineBasicBlock &MBB,
344                                       MachineBasicBlock::iterator LastPHIIt,
345                                       bool AllEdgesCritical) {
346   ++NumLowered;
347 
348   MachineBasicBlock::iterator AfterPHIsIt = std::next(LastPHIIt);
349 
350   // Unlink the PHI node from the basic block, but don't delete the PHI yet.
351   MachineInstr *MPhi = MBB.remove(&*MBB.begin());
352 
353   unsigned NumSrcs = (MPhi->getNumOperands() - 1) / 2;
354   Register DestReg = MPhi->getOperand(0).getReg();
355   assert(MPhi->getOperand(0).getSubReg() == 0 && "Can't handle sub-reg PHIs");
356   bool isDead = MPhi->getOperand(0).isDead();
357 
358   // Create a new register for the incoming PHI arguments.
359   MachineFunction &MF = *MBB.getParent();
360   unsigned IncomingReg = 0;
361   bool EliminateNow = true;    // delay elimination of nodes in LoweredPHIs
362   bool reusedIncoming = false; // Is IncomingReg reused from an earlier PHI?
363 
364   // Insert a register to register copy at the top of the current block (but
365   // after any remaining phi nodes) which copies the new incoming register
366   // into the phi node destination.
367   MachineInstr *PHICopy = nullptr;
368   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
369   if (allPhiOperandsUndefined(*MPhi, *MRI))
370     // If all sources of a PHI node are implicit_def or undef uses, just emit an
371     // implicit_def instead of a copy.
372     PHICopy = BuildMI(MBB, AfterPHIsIt, MPhi->getDebugLoc(),
373                       TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
374   else {
375     // Can we reuse an earlier PHI node? This only happens for critical edges,
376     // typically those created by tail duplication. Typically, an identical PHI
377     // node can't occur, so avoid hashing/storing such PHIs, which is somewhat
378     // expensive.
379     unsigned *Entry = nullptr;
380     if (AllEdgesCritical)
381       Entry = &LoweredPHIs[MPhi];
382     if (Entry && *Entry) {
383       // An identical PHI node was already lowered. Reuse the incoming register.
384       IncomingReg = *Entry;
385       reusedIncoming = true;
386       ++NumReused;
387       LLVM_DEBUG(dbgs() << "Reusing " << printReg(IncomingReg) << " for "
388                         << *MPhi);
389     } else {
390       const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(DestReg);
391       IncomingReg = MF.getRegInfo().createVirtualRegister(RC);
392       if (Entry) {
393         EliminateNow = false;
394         *Entry = IncomingReg;
395       }
396     }
397 
398     // Give the target possiblity to handle special cases fallthrough otherwise
399     PHICopy = TII->createPHIDestinationCopy(
400         MBB, AfterPHIsIt, MPhi->getDebugLoc(), IncomingReg, DestReg);
401   }
402 
403   if (MPhi->peekDebugInstrNum()) {
404     // If referred to by debug-info, store where this PHI was.
405     MachineFunction *MF = MBB.getParent();
406     unsigned ID = MPhi->peekDebugInstrNum();
407     auto P = MachineFunction::DebugPHIRegallocPos(&MBB, IncomingReg, 0);
408     auto Res = MF->DebugPHIPositions.insert({ID, P});
409     assert(Res.second);
410     (void)Res;
411   }
412 
413   // Update live variable information if there is any.
414   if (LV) {
415     if (IncomingReg) {
416       LiveVariables::VarInfo &VI = LV->getVarInfo(IncomingReg);
417 
418       MachineInstr *OldKill = nullptr;
419       bool IsPHICopyAfterOldKill = false;
420 
421       if (reusedIncoming && (OldKill = VI.findKill(&MBB))) {
422         // Calculate whether the PHICopy is after the OldKill.
423         // In general, the PHICopy is inserted as the first non-phi instruction
424         // by default, so it's before the OldKill. But some Target hooks for
425         // createPHIDestinationCopy() may modify the default insert position of
426         // PHICopy.
427         for (auto I = MBB.SkipPHIsAndLabels(MBB.begin()), E = MBB.end(); I != E;
428              ++I) {
429           if (I == PHICopy)
430             break;
431 
432           if (I == OldKill) {
433             IsPHICopyAfterOldKill = true;
434             break;
435           }
436         }
437       }
438 
439       // When we are reusing the incoming register and it has been marked killed
440       // by OldKill, if the PHICopy is after the OldKill, we should remove the
441       // killed flag from OldKill.
442       if (IsPHICopyAfterOldKill) {
443         LLVM_DEBUG(dbgs() << "Remove old kill from " << *OldKill);
444         LV->removeVirtualRegisterKilled(IncomingReg, *OldKill);
445         LLVM_DEBUG(MBB.dump());
446       }
447 
448       // Add information to LiveVariables to know that the first used incoming
449       // value or the resued incoming value whose PHICopy is after the OldKIll
450       // is killed. Note that because the value is defined in several places
451       // (once each for each incoming block), the "def" block and instruction
452       // fields for the VarInfo is not filled in.
453       if (!OldKill || IsPHICopyAfterOldKill)
454         LV->addVirtualRegisterKilled(IncomingReg, *PHICopy);
455     }
456 
457     // Since we are going to be deleting the PHI node, if it is the last use of
458     // any registers, or if the value itself is dead, we need to move this
459     // information over to the new copy we just inserted.
460     LV->removeVirtualRegistersKilled(*MPhi);
461 
462     // If the result is dead, update LV.
463     if (isDead) {
464       LV->addVirtualRegisterDead(DestReg, *PHICopy);
465       LV->removeVirtualRegisterDead(DestReg, *MPhi);
466     }
467   }
468 
469   // Update LiveIntervals for the new copy or implicit def.
470   if (LIS) {
471     SlotIndex DestCopyIndex = LIS->InsertMachineInstrInMaps(*PHICopy);
472 
473     SlotIndex MBBStartIndex = LIS->getMBBStartIdx(&MBB);
474     if (IncomingReg) {
475       // Add the region from the beginning of MBB to the copy instruction to
476       // IncomingReg's live interval.
477       LiveInterval &IncomingLI = LIS->getOrCreateEmptyInterval(IncomingReg);
478       VNInfo *IncomingVNI = IncomingLI.getVNInfoAt(MBBStartIndex);
479       if (!IncomingVNI)
480         IncomingVNI =
481             IncomingLI.getNextValue(MBBStartIndex, LIS->getVNInfoAllocator());
482       IncomingLI.addSegment(LiveInterval::Segment(
483           MBBStartIndex, DestCopyIndex.getRegSlot(), IncomingVNI));
484     }
485 
486     LiveInterval &DestLI = LIS->getInterval(DestReg);
487     assert(!DestLI.empty() && "PHIs should have non-empty LiveIntervals.");
488 
489     SlotIndex NewStart = DestCopyIndex.getRegSlot();
490 
491     SmallVector<LiveRange *> ToUpdate({&DestLI});
492     for (auto &SR : DestLI.subranges())
493       ToUpdate.push_back(&SR);
494 
495     for (auto LR : ToUpdate) {
496       auto DestSegment = LR->find(MBBStartIndex);
497       assert(DestSegment != LR->end() &&
498              "PHI destination must be live in block");
499 
500       if (LR->endIndex().isDead()) {
501         // A dead PHI's live range begins and ends at the start of the MBB, but
502         // the lowered copy, which will still be dead, needs to begin and end at
503         // the copy instruction.
504         VNInfo *OrigDestVNI = LR->getVNInfoAt(DestSegment->start);
505         assert(OrigDestVNI && "PHI destination should be live at block entry.");
506         LR->removeSegment(DestSegment->start, DestSegment->start.getDeadSlot());
507         LR->createDeadDef(NewStart, LIS->getVNInfoAllocator());
508         LR->removeValNo(OrigDestVNI);
509         continue;
510       }
511 
512       // Destination copies are not inserted in the same order as the PHI nodes
513       // they replace. Hence the start of the live range may need to be adjusted
514       // to match the actual slot index of the copy.
515       if (DestSegment->start > NewStart) {
516         VNInfo *VNI = LR->getVNInfoAt(DestSegment->start);
517         assert(VNI && "value should be defined for known segment");
518         LR->addSegment(
519             LiveInterval::Segment(NewStart, DestSegment->start, VNI));
520       } else if (DestSegment->start < NewStart) {
521         assert(DestSegment->start >= MBBStartIndex);
522         assert(DestSegment->end >= DestCopyIndex.getRegSlot());
523         LR->removeSegment(DestSegment->start, NewStart);
524       }
525       VNInfo *DestVNI = LR->getVNInfoAt(NewStart);
526       assert(DestVNI && "PHI destination should be live at its definition.");
527       DestVNI->def = NewStart;
528     }
529   }
530 
531   // Adjust the VRegPHIUseCount map to account for the removal of this PHI node.
532   if (LV || LIS) {
533     for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2) {
534       if (!MPhi->getOperand(i).isUndef()) {
535         --VRegPHIUseCount[BBVRegPair(
536             MPhi->getOperand(i + 1).getMBB()->getNumber(),
537             MPhi->getOperand(i).getReg())];
538       }
539     }
540   }
541 
542   // Now loop over all of the incoming arguments, changing them to copy into the
543   // IncomingReg register in the corresponding predecessor basic block.
544   SmallPtrSet<MachineBasicBlock *, 8> MBBsInsertedInto;
545   for (int i = NumSrcs - 1; i >= 0; --i) {
546     Register SrcReg = MPhi->getOperand(i * 2 + 1).getReg();
547     unsigned SrcSubReg = MPhi->getOperand(i * 2 + 1).getSubReg();
548     bool SrcUndef = MPhi->getOperand(i * 2 + 1).isUndef() ||
549                     isImplicitlyDefined(SrcReg, *MRI);
550     assert(SrcReg.isVirtual() &&
551            "Machine PHI Operands must all be virtual registers!");
552 
553     // Get the MachineBasicBlock equivalent of the BasicBlock that is the source
554     // path the PHI.
555     MachineBasicBlock &opBlock = *MPhi->getOperand(i * 2 + 2).getMBB();
556 
557     // Check to make sure we haven't already emitted the copy for this block.
558     // This can happen because PHI nodes may have multiple entries for the same
559     // basic block.
560     if (!MBBsInsertedInto.insert(&opBlock).second)
561       continue; // If the copy has already been emitted, we're done.
562 
563     MachineInstr *SrcRegDef = MRI->getVRegDef(SrcReg);
564     if (SrcRegDef && TII->isUnspillableTerminator(SrcRegDef)) {
565       assert(SrcRegDef->getOperand(0).isReg() &&
566              SrcRegDef->getOperand(0).isDef() &&
567              "Expected operand 0 to be a reg def!");
568       // Now that the PHI's use has been removed (as the instruction was
569       // removed) there should be no other uses of the SrcReg.
570       assert(MRI->use_empty(SrcReg) &&
571              "Expected a single use from UnspillableTerminator");
572       SrcRegDef->getOperand(0).setReg(IncomingReg);
573 
574       // Update LiveVariables.
575       if (LV) {
576         LiveVariables::VarInfo &SrcVI = LV->getVarInfo(SrcReg);
577         LiveVariables::VarInfo &IncomingVI = LV->getVarInfo(IncomingReg);
578         IncomingVI.AliveBlocks = std::move(SrcVI.AliveBlocks);
579         SrcVI.AliveBlocks.clear();
580       }
581 
582       continue;
583     }
584 
585     // Find a safe location to insert the copy, this may be the first terminator
586     // in the block (or end()).
587     MachineBasicBlock::iterator InsertPos =
588         findPHICopyInsertPoint(&opBlock, &MBB, SrcReg);
589 
590     // Insert the copy.
591     MachineInstr *NewSrcInstr = nullptr;
592     if (!reusedIncoming && IncomingReg) {
593       if (SrcUndef) {
594         // The source register is undefined, so there is no need for a real
595         // COPY, but we still need to ensure joint dominance by defs.
596         // Insert an IMPLICIT_DEF instruction.
597         NewSrcInstr =
598             BuildMI(opBlock, InsertPos, MPhi->getDebugLoc(),
599                     TII->get(TargetOpcode::IMPLICIT_DEF), IncomingReg);
600 
601         // Clean up the old implicit-def, if there even was one.
602         if (MachineInstr *DefMI = MRI->getVRegDef(SrcReg))
603           if (DefMI->isImplicitDef())
604             ImpDefs.insert(DefMI);
605       } else {
606         // Delete the debug location, since the copy is inserted into a
607         // different basic block.
608         NewSrcInstr = TII->createPHISourceCopy(opBlock, InsertPos, nullptr,
609                                                SrcReg, SrcSubReg, IncomingReg);
610       }
611     }
612 
613     // We only need to update the LiveVariables kill of SrcReg if this was the
614     // last PHI use of SrcReg to be lowered on this CFG edge and it is not live
615     // out of the predecessor. We can also ignore undef sources.
616     if (LV && !SrcUndef &&
617         !VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)] &&
618         !LV->isLiveOut(SrcReg, opBlock)) {
619       // We want to be able to insert a kill of the register if this PHI (aka,
620       // the copy we just inserted) is the last use of the source value. Live
621       // variable analysis conservatively handles this by saying that the value
622       // is live until the end of the block the PHI entry lives in. If the value
623       // really is dead at the PHI copy, there will be no successor blocks which
624       // have the value live-in.
625 
626       // Okay, if we now know that the value is not live out of the block, we
627       // can add a kill marker in this block saying that it kills the incoming
628       // value!
629 
630       // In our final twist, we have to decide which instruction kills the
631       // register.  In most cases this is the copy, however, terminator
632       // instructions at the end of the block may also use the value. In this
633       // case, we should mark the last such terminator as being the killing
634       // block, not the copy.
635       MachineBasicBlock::iterator KillInst = opBlock.end();
636       for (MachineBasicBlock::iterator Term = InsertPos; Term != opBlock.end();
637            ++Term) {
638         if (Term->readsRegister(SrcReg, /*TRI=*/nullptr))
639           KillInst = Term;
640       }
641 
642       if (KillInst == opBlock.end()) {
643         // No terminator uses the register.
644 
645         if (reusedIncoming || !IncomingReg) {
646           // We may have to rewind a bit if we didn't insert a copy this time.
647           KillInst = InsertPos;
648           while (KillInst != opBlock.begin()) {
649             --KillInst;
650             if (KillInst->isDebugInstr())
651               continue;
652             if (KillInst->readsRegister(SrcReg, /*TRI=*/nullptr))
653               break;
654           }
655         } else {
656           // We just inserted this copy.
657           KillInst = NewSrcInstr;
658         }
659       }
660       assert(KillInst->readsRegister(SrcReg, /*TRI=*/nullptr) &&
661              "Cannot find kill instruction");
662 
663       // Finally, mark it killed.
664       LV->addVirtualRegisterKilled(SrcReg, *KillInst);
665 
666       // This vreg no longer lives all of the way through opBlock.
667       unsigned opBlockNum = opBlock.getNumber();
668       LV->getVarInfo(SrcReg).AliveBlocks.reset(opBlockNum);
669     }
670 
671     if (LIS) {
672       if (NewSrcInstr) {
673         LIS->InsertMachineInstrInMaps(*NewSrcInstr);
674         LIS->addSegmentToEndOfBlock(IncomingReg, *NewSrcInstr);
675       }
676 
677       if (!SrcUndef &&
678           !VRegPHIUseCount[BBVRegPair(opBlock.getNumber(), SrcReg)]) {
679         LiveInterval &SrcLI = LIS->getInterval(SrcReg);
680 
681         bool isLiveOut = false;
682         for (MachineBasicBlock *Succ : opBlock.successors()) {
683           SlotIndex startIdx = LIS->getMBBStartIdx(Succ);
684           VNInfo *VNI = SrcLI.getVNInfoAt(startIdx);
685 
686           // Definitions by other PHIs are not truly live-in for our purposes.
687           if (VNI && VNI->def != startIdx) {
688             isLiveOut = true;
689             break;
690           }
691         }
692 
693         if (!isLiveOut) {
694           MachineBasicBlock::iterator KillInst = opBlock.end();
695           for (MachineBasicBlock::iterator Term = InsertPos;
696                Term != opBlock.end(); ++Term) {
697             if (Term->readsRegister(SrcReg, /*TRI=*/nullptr))
698               KillInst = Term;
699           }
700 
701           if (KillInst == opBlock.end()) {
702             // No terminator uses the register.
703 
704             if (reusedIncoming || !IncomingReg) {
705               // We may have to rewind a bit if we didn't just insert a copy.
706               KillInst = InsertPos;
707               while (KillInst != opBlock.begin()) {
708                 --KillInst;
709                 if (KillInst->isDebugInstr())
710                   continue;
711                 if (KillInst->readsRegister(SrcReg, /*TRI=*/nullptr))
712                   break;
713               }
714             } else {
715               // We just inserted this copy.
716               KillInst = std::prev(InsertPos);
717             }
718           }
719           assert(KillInst->readsRegister(SrcReg, /*TRI=*/nullptr) &&
720                  "Cannot find kill instruction");
721 
722           SlotIndex LastUseIndex = LIS->getInstructionIndex(*KillInst);
723           SrcLI.removeSegment(LastUseIndex.getRegSlot(),
724                               LIS->getMBBEndIdx(&opBlock));
725           for (auto &SR : SrcLI.subranges()) {
726             SR.removeSegment(LastUseIndex.getRegSlot(),
727                              LIS->getMBBEndIdx(&opBlock));
728           }
729         }
730       }
731     }
732   }
733 
734   // Really delete the PHI instruction now, if it is not in the LoweredPHIs map.
735   if (EliminateNow) {
736     if (LIS)
737       LIS->RemoveMachineInstrFromMaps(*MPhi);
738     MF.deleteMachineInstr(MPhi);
739   }
740 }
741 
742 /// analyzePHINodes - Gather information about the PHI nodes in here. In
743 /// particular, we want to map the number of uses of a virtual register which is
744 /// used in a PHI node. We map that to the BB the vreg is coming from. This is
745 /// used later to determine when the vreg is killed in the BB.
746 void PHIEliminationImpl::analyzePHINodes(const MachineFunction &MF) {
747   for (const auto &MBB : MF) {
748     for (const auto &BBI : MBB) {
749       if (!BBI.isPHI())
750         break;
751       for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
752         if (!BBI.getOperand(i).isUndef()) {
753           ++VRegPHIUseCount[BBVRegPair(
754               BBI.getOperand(i + 1).getMBB()->getNumber(),
755               BBI.getOperand(i).getReg())];
756         }
757       }
758     }
759   }
760 }
761 
762 bool PHIEliminationImpl::SplitPHIEdges(
763     MachineFunction &MF, MachineBasicBlock &MBB, MachineLoopInfo *MLI,
764     std::vector<SparseBitVector<>> *LiveInSets, MachineDomTreeUpdater &MDTU) {
765   if (MBB.empty() || !MBB.front().isPHI() || MBB.isEHPad())
766     return false; // Quick exit for basic blocks without PHIs.
767 
768   const MachineLoop *CurLoop = MLI ? MLI->getLoopFor(&MBB) : nullptr;
769   bool IsLoopHeader = CurLoop && &MBB == CurLoop->getHeader();
770 
771   bool Changed = false;
772   for (MachineBasicBlock::iterator BBI = MBB.begin(), BBE = MBB.end();
773        BBI != BBE && BBI->isPHI(); ++BBI) {
774     for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
775       Register Reg = BBI->getOperand(i).getReg();
776       MachineBasicBlock *PreMBB = BBI->getOperand(i + 1).getMBB();
777       // Is there a critical edge from PreMBB to MBB?
778       if (PreMBB->succ_size() == 1)
779         continue;
780 
781       // Avoid splitting backedges of loops. It would introduce small
782       // out-of-line blocks into the loop which is very bad for code placement.
783       if (PreMBB == &MBB && !SplitAllCriticalEdges)
784         continue;
785       const MachineLoop *PreLoop = MLI ? MLI->getLoopFor(PreMBB) : nullptr;
786       if (IsLoopHeader && PreLoop == CurLoop && !SplitAllCriticalEdges)
787         continue;
788 
789       // LV doesn't consider a phi use live-out, so isLiveOut only returns true
790       // when the source register is live-out for some other reason than a phi
791       // use. That means the copy we will insert in PreMBB won't be a kill, and
792       // there is a risk it may not be coalesced away.
793       //
794       // If the copy would be a kill, there is no need to split the edge.
795       bool ShouldSplit = isLiveOutPastPHIs(Reg, PreMBB);
796       if (!ShouldSplit && !NoPhiElimLiveOutEarlyExit)
797         continue;
798       if (ShouldSplit) {
799         LLVM_DEBUG(dbgs() << printReg(Reg) << " live-out before critical edge "
800                           << printMBBReference(*PreMBB) << " -> "
801                           << printMBBReference(MBB) << ": " << *BBI);
802       }
803 
804       // If Reg is not live-in to MBB, it means it must be live-in to some
805       // other PreMBB successor, and we can avoid the interference by splitting
806       // the edge.
807       //
808       // If Reg *is* live-in to MBB, the interference is inevitable and a copy
809       // is likely to be left after coalescing. If we are looking at a loop
810       // exiting edge, split it so we won't insert code in the loop, otherwise
811       // don't bother.
812       ShouldSplit = ShouldSplit && !isLiveIn(Reg, &MBB);
813 
814       // Check for a loop exiting edge.
815       if (!ShouldSplit && CurLoop != PreLoop) {
816         LLVM_DEBUG({
817           dbgs() << "Split wouldn't help, maybe avoid loop copies?\n";
818           if (PreLoop)
819             dbgs() << "PreLoop: " << *PreLoop;
820           if (CurLoop)
821             dbgs() << "CurLoop: " << *CurLoop;
822         });
823         // This edge could be entering a loop, exiting a loop, or it could be
824         // both: Jumping directly form one loop to the header of a sibling
825         // loop.
826         // Split unless this edge is entering CurLoop from an outer loop.
827         ShouldSplit = PreLoop && !PreLoop->contains(CurLoop);
828       }
829       if (!ShouldSplit && !SplitAllCriticalEdges)
830         continue;
831       if (!(P ? PreMBB->SplitCriticalEdge(&MBB, *P, LiveInSets, &MDTU)
832               : PreMBB->SplitCriticalEdge(&MBB, *MFAM, LiveInSets, &MDTU))) {
833         LLVM_DEBUG(dbgs() << "Failed to split critical edge.\n");
834         continue;
835       }
836       Changed = true;
837       ++NumCriticalEdgesSplit;
838     }
839   }
840   return Changed;
841 }
842 
843 bool PHIEliminationImpl::isLiveIn(Register Reg, const MachineBasicBlock *MBB) {
844   assert((LV || LIS) &&
845          "isLiveIn() requires either LiveVariables or LiveIntervals");
846   if (LIS)
847     return LIS->isLiveInToMBB(LIS->getInterval(Reg), MBB);
848   else
849     return LV->isLiveIn(Reg, *MBB);
850 }
851 
852 bool PHIEliminationImpl::isLiveOutPastPHIs(Register Reg,
853                                            const MachineBasicBlock *MBB) {
854   assert((LV || LIS) &&
855          "isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals");
856   // LiveVariables considers uses in PHIs to be in the predecessor basic block,
857   // so that a register used only in a PHI is not live out of the block. In
858   // contrast, LiveIntervals considers uses in PHIs to be on the edge rather
859   // than in the predecessor basic block, so that a register used only in a PHI
860   // is live out of the block.
861   if (LIS) {
862     const LiveInterval &LI = LIS->getInterval(Reg);
863     for (const MachineBasicBlock *SI : MBB->successors())
864       if (LI.liveAt(LIS->getMBBStartIdx(SI)))
865         return true;
866     return false;
867   } else {
868     return LV->isLiveOut(Reg, *MBB);
869   }
870 }
871