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