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