1 //===- TailDuplicator.cpp - Duplicate blocks into predecessors' tails -----===//
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 utility class duplicates basic blocks ending in unconditional branches
10 // into the tails of their predecessors.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/TailDuplicator.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/ProfileSummaryInfo.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
25 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/MachineSizeOpts.h"
32 #include "llvm/CodeGen/MachineSSAUpdater.h"
33 #include "llvm/CodeGen/TargetInstrInfo.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/IR/DebugLoc.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include <algorithm>
44 #include <cassert>
45 #include <iterator>
46 #include <utility>
47 
48 using namespace llvm;
49 
50 #define DEBUG_TYPE "tailduplication"
51 
52 STATISTIC(NumTails, "Number of tails duplicated");
53 STATISTIC(NumTailDups, "Number of tail duplicated blocks");
54 STATISTIC(NumTailDupAdded,
55           "Number of instructions added due to tail duplication");
56 STATISTIC(NumTailDupRemoved,
57           "Number of instructions removed due to tail duplication");
58 STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
59 STATISTIC(NumAddedPHIs, "Number of phis added");
60 
61 // Heuristic for tail duplication.
62 static cl::opt<unsigned> TailDuplicateSize(
63     "tail-dup-size",
64     cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2),
65     cl::Hidden);
66 
67 static cl::opt<unsigned> TailDupIndirectBranchSize(
68     "tail-dup-indirect-size",
69     cl::desc("Maximum instructions to consider tail duplicating blocks that "
70              "end with indirect branches."), cl::init(20),
71     cl::Hidden);
72 
73 static cl::opt<bool>
74     TailDupVerify("tail-dup-verify",
75                   cl::desc("Verify sanity of PHI instructions during taildup"),
76                   cl::init(false), cl::Hidden);
77 
78 static cl::opt<unsigned> TailDupLimit("tail-dup-limit", cl::init(~0U),
79                                       cl::Hidden);
80 
81 void TailDuplicator::initMF(MachineFunction &MFin, bool PreRegAlloc,
82                             const MachineBranchProbabilityInfo *MBPIin,
83                             MBFIWrapper *MBFIin,
84                             ProfileSummaryInfo *PSIin,
85                             bool LayoutModeIn, unsigned TailDupSizeIn) {
86   MF = &MFin;
87   TII = MF->getSubtarget().getInstrInfo();
88   TRI = MF->getSubtarget().getRegisterInfo();
89   MRI = &MF->getRegInfo();
90   MMI = &MF->getMMI();
91   MBPI = MBPIin;
92   MBFI = MBFIin;
93   PSI = PSIin;
94   TailDupSize = TailDupSizeIn;
95 
96   assert(MBPI != nullptr && "Machine Branch Probability Info required");
97 
98   LayoutMode = LayoutModeIn;
99   this->PreRegAlloc = PreRegAlloc;
100 }
101 
102 static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) {
103   for (MachineBasicBlock &MBB : llvm::drop_begin(MF)) {
104     SmallSetVector<MachineBasicBlock *, 8> Preds(MBB.pred_begin(),
105                                                  MBB.pred_end());
106     MachineBasicBlock::iterator MI = MBB.begin();
107     while (MI != MBB.end()) {
108       if (!MI->isPHI())
109         break;
110       for (MachineBasicBlock *PredBB : Preds) {
111         bool Found = false;
112         for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
113           MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
114           if (PHIBB == PredBB) {
115             Found = true;
116             break;
117           }
118         }
119         if (!Found) {
120           dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
121                  << *MI;
122           dbgs() << "  missing input from predecessor "
123                  << printMBBReference(*PredBB) << '\n';
124           llvm_unreachable(nullptr);
125         }
126       }
127 
128       for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
129         MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
130         if (CheckExtra && !Preds.count(PHIBB)) {
131           dbgs() << "Warning: malformed PHI in " << printMBBReference(MBB)
132                  << ": " << *MI;
133           dbgs() << "  extra input from predecessor "
134                  << printMBBReference(*PHIBB) << '\n';
135           llvm_unreachable(nullptr);
136         }
137         if (PHIBB->getNumber() < 0) {
138           dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
139                  << *MI;
140           dbgs() << "  non-existing " << printMBBReference(*PHIBB) << '\n';
141           llvm_unreachable(nullptr);
142         }
143       }
144       ++MI;
145     }
146   }
147 }
148 
149 /// Tail duplicate the block and cleanup.
150 /// \p IsSimple - return value of isSimpleBB
151 /// \p MBB - block to be duplicated
152 /// \p ForcedLayoutPred - If non-null, treat this block as the layout
153 ///     predecessor, instead of using the ordering in MF
154 /// \p DuplicatedPreds - if non-null, \p DuplicatedPreds will contain a list of
155 ///     all Preds that received a copy of \p MBB.
156 /// \p RemovalCallback - if non-null, called just before MBB is deleted.
157 bool TailDuplicator::tailDuplicateAndUpdate(
158     bool IsSimple, MachineBasicBlock *MBB,
159     MachineBasicBlock *ForcedLayoutPred,
160     SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds,
161     function_ref<void(MachineBasicBlock *)> *RemovalCallback,
162     SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) {
163   // Save the successors list.
164   SmallSetVector<MachineBasicBlock *, 8> Succs(MBB->succ_begin(),
165                                                MBB->succ_end());
166 
167   SmallVector<MachineBasicBlock *, 8> TDBBs;
168   SmallVector<MachineInstr *, 16> Copies;
169   if (!tailDuplicate(IsSimple, MBB, ForcedLayoutPred,
170                      TDBBs, Copies, CandidatePtr))
171     return false;
172 
173   ++NumTails;
174 
175   SmallVector<MachineInstr *, 8> NewPHIs;
176   MachineSSAUpdater SSAUpdate(*MF, &NewPHIs);
177 
178   // TailBB's immediate successors are now successors of those predecessors
179   // which duplicated TailBB. Add the predecessors as sources to the PHI
180   // instructions.
181   bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken();
182   if (PreRegAlloc)
183     updateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
184 
185   // If it is dead, remove it.
186   if (isDead) {
187     NumTailDupRemoved += MBB->size();
188     removeDeadBlock(MBB, RemovalCallback);
189     ++NumDeadBlocks;
190   }
191 
192   // Update SSA form.
193   if (!SSAUpdateVRs.empty()) {
194     for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) {
195       unsigned VReg = SSAUpdateVRs[i];
196       SSAUpdate.Initialize(VReg);
197 
198       // If the original definition is still around, add it as an available
199       // value.
200       MachineInstr *DefMI = MRI->getVRegDef(VReg);
201       MachineBasicBlock *DefBB = nullptr;
202       if (DefMI) {
203         DefBB = DefMI->getParent();
204         SSAUpdate.AddAvailableValue(DefBB, VReg);
205       }
206 
207       // Add the new vregs as available values.
208       DenseMap<Register, AvailableValsTy>::iterator LI =
209           SSAUpdateVals.find(VReg);
210       for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
211         MachineBasicBlock *SrcBB = LI->second[j].first;
212         Register SrcReg = LI->second[j].second;
213         SSAUpdate.AddAvailableValue(SrcBB, SrcReg);
214       }
215 
216       // Rewrite uses that are outside of the original def's block.
217       MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg);
218       // Only remove instructions after loop, as DBG_VALUE_LISTs with multiple
219       // uses of VReg may invalidate the use iterator when erased.
220       SmallPtrSet<MachineInstr *, 4> InstrsToRemove;
221       while (UI != MRI->use_end()) {
222         MachineOperand &UseMO = *UI;
223         MachineInstr *UseMI = UseMO.getParent();
224         ++UI;
225         if (UseMI->isDebugValue()) {
226           // SSAUpdate can replace the use with an undef. That creates
227           // a debug instruction that is a kill.
228           // FIXME: Should it SSAUpdate job to delete debug instructions
229           // instead of replacing the use with undef?
230           InstrsToRemove.insert(UseMI);
231           continue;
232         }
233         if (UseMI->getParent() == DefBB && !UseMI->isPHI())
234           continue;
235         SSAUpdate.RewriteUse(UseMO);
236       }
237       for (auto *MI : InstrsToRemove)
238         MI->eraseFromParent();
239     }
240 
241     SSAUpdateVRs.clear();
242     SSAUpdateVals.clear();
243   }
244 
245   // Eliminate some of the copies inserted by tail duplication to maintain
246   // SSA form.
247   for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
248     MachineInstr *Copy = Copies[i];
249     if (!Copy->isCopy())
250       continue;
251     Register Dst = Copy->getOperand(0).getReg();
252     Register Src = Copy->getOperand(1).getReg();
253     if (MRI->hasOneNonDBGUse(Src) &&
254         MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) {
255       // Copy is the only use. Do trivial copy propagation here.
256       MRI->replaceRegWith(Dst, Src);
257       Copy->eraseFromParent();
258     }
259   }
260 
261   if (NewPHIs.size())
262     NumAddedPHIs += NewPHIs.size();
263 
264   if (DuplicatedPreds)
265     *DuplicatedPreds = std::move(TDBBs);
266 
267   return true;
268 }
269 
270 /// Look for small blocks that are unconditionally branched to and do not fall
271 /// through. Tail-duplicate their instructions into their predecessors to
272 /// eliminate (dynamic) branches.
273 bool TailDuplicator::tailDuplicateBlocks() {
274   bool MadeChange = false;
275 
276   if (PreRegAlloc && TailDupVerify) {
277     LLVM_DEBUG(dbgs() << "\n*** Before tail-duplicating\n");
278     VerifyPHIs(*MF, true);
279   }
280 
281   for (MachineBasicBlock &MBB :
282        llvm::make_early_inc_range(llvm::drop_begin(*MF))) {
283     if (NumTails == TailDupLimit)
284       break;
285 
286     bool IsSimple = isSimpleBB(&MBB);
287 
288     if (!shouldTailDuplicate(IsSimple, MBB))
289       continue;
290 
291     MadeChange |= tailDuplicateAndUpdate(IsSimple, &MBB, nullptr);
292   }
293 
294   if (PreRegAlloc && TailDupVerify)
295     VerifyPHIs(*MF, false);
296 
297   return MadeChange;
298 }
299 
300 static bool isDefLiveOut(Register Reg, MachineBasicBlock *BB,
301                          const MachineRegisterInfo *MRI) {
302   for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
303     if (UseMI.isDebugValue())
304       continue;
305     if (UseMI.getParent() != BB)
306       return true;
307   }
308   return false;
309 }
310 
311 static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) {
312   for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
313     if (MI->getOperand(i + 1).getMBB() == SrcBB)
314       return i;
315   return 0;
316 }
317 
318 // Remember which registers are used by phis in this block. This is
319 // used to determine which registers are liveout while modifying the
320 // block (which is why we need to copy the information).
321 static void getRegsUsedByPHIs(const MachineBasicBlock &BB,
322                               DenseSet<Register> *UsedByPhi) {
323   for (const auto &MI : BB) {
324     if (!MI.isPHI())
325       break;
326     for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
327       Register SrcReg = MI.getOperand(i).getReg();
328       UsedByPhi->insert(SrcReg);
329     }
330   }
331 }
332 
333 /// Add a definition and source virtual registers pair for SSA update.
334 void TailDuplicator::addSSAUpdateEntry(Register OrigReg, Register NewReg,
335                                        MachineBasicBlock *BB) {
336   DenseMap<Register, AvailableValsTy>::iterator LI =
337       SSAUpdateVals.find(OrigReg);
338   if (LI != SSAUpdateVals.end())
339     LI->second.push_back(std::make_pair(BB, NewReg));
340   else {
341     AvailableValsTy Vals;
342     Vals.push_back(std::make_pair(BB, NewReg));
343     SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
344     SSAUpdateVRs.push_back(OrigReg);
345   }
346 }
347 
348 /// Process PHI node in TailBB by turning it into a copy in PredBB. Remember the
349 /// source register that's contributed by PredBB and update SSA update map.
350 void TailDuplicator::processPHI(
351     MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
352     DenseMap<Register, RegSubRegPair> &LocalVRMap,
353     SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies,
354     const DenseSet<Register> &RegsUsedByPhi, bool Remove) {
355   Register DefReg = MI->getOperand(0).getReg();
356   unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
357   assert(SrcOpIdx && "Unable to find matching PHI source?");
358   Register SrcReg = MI->getOperand(SrcOpIdx).getReg();
359   unsigned SrcSubReg = MI->getOperand(SrcOpIdx).getSubReg();
360   const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
361   LocalVRMap.insert(std::make_pair(DefReg, RegSubRegPair(SrcReg, SrcSubReg)));
362 
363   // Insert a copy from source to the end of the block. The def register is the
364   // available value liveout of the block.
365   Register NewDef = MRI->createVirtualRegister(RC);
366   Copies.push_back(std::make_pair(NewDef, RegSubRegPair(SrcReg, SrcSubReg)));
367   if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg))
368     addSSAUpdateEntry(DefReg, NewDef, PredBB);
369 
370   if (!Remove)
371     return;
372 
373   // Remove PredBB from the PHI node.
374   MI->RemoveOperand(SrcOpIdx + 1);
375   MI->RemoveOperand(SrcOpIdx);
376   if (MI->getNumOperands() == 1)
377     MI->eraseFromParent();
378 }
379 
380 /// Duplicate a TailBB instruction to PredBB and update
381 /// the source operands due to earlier PHI translation.
382 void TailDuplicator::duplicateInstruction(
383     MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
384     DenseMap<Register, RegSubRegPair> &LocalVRMap,
385     const DenseSet<Register> &UsedByPhi) {
386   // Allow duplication of CFI instructions.
387   if (MI->isCFIInstruction()) {
388     BuildMI(*PredBB, PredBB->end(), PredBB->findDebugLoc(PredBB->begin()),
389       TII->get(TargetOpcode::CFI_INSTRUCTION)).addCFIIndex(
390       MI->getOperand(0).getCFIIndex());
391     return;
392   }
393   MachineInstr &NewMI = TII->duplicate(*PredBB, PredBB->end(), *MI);
394   if (PreRegAlloc) {
395     for (unsigned i = 0, e = NewMI.getNumOperands(); i != e; ++i) {
396       MachineOperand &MO = NewMI.getOperand(i);
397       if (!MO.isReg())
398         continue;
399       Register Reg = MO.getReg();
400       if (!Register::isVirtualRegister(Reg))
401         continue;
402       if (MO.isDef()) {
403         const TargetRegisterClass *RC = MRI->getRegClass(Reg);
404         Register NewReg = MRI->createVirtualRegister(RC);
405         MO.setReg(NewReg);
406         LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0)));
407         if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg))
408           addSSAUpdateEntry(Reg, NewReg, PredBB);
409       } else {
410         auto VI = LocalVRMap.find(Reg);
411         if (VI != LocalVRMap.end()) {
412           // Need to make sure that the register class of the mapped register
413           // will satisfy the constraints of the class of the register being
414           // replaced.
415           auto *OrigRC = MRI->getRegClass(Reg);
416           auto *MappedRC = MRI->getRegClass(VI->second.Reg);
417           const TargetRegisterClass *ConstrRC;
418           if (VI->second.SubReg != 0) {
419             ConstrRC = TRI->getMatchingSuperRegClass(MappedRC, OrigRC,
420                                                      VI->second.SubReg);
421             if (ConstrRC) {
422               // The actual constraining (as in "find appropriate new class")
423               // is done by getMatchingSuperRegClass, so now we only need to
424               // change the class of the mapped register.
425               MRI->setRegClass(VI->second.Reg, ConstrRC);
426             }
427           } else {
428             // For mapped registers that do not have sub-registers, simply
429             // restrict their class to match the original one.
430             ConstrRC = MRI->constrainRegClass(VI->second.Reg, OrigRC);
431           }
432 
433           if (ConstrRC) {
434             // If the class constraining succeeded, we can simply replace
435             // the old register with the mapped one.
436             MO.setReg(VI->second.Reg);
437             // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a
438             // sub-register, we need to compose the sub-register indices.
439             MO.setSubReg(TRI->composeSubRegIndices(MO.getSubReg(),
440                                                    VI->second.SubReg));
441           } else {
442             // The direct replacement is not possible, due to failing register
443             // class constraints. An explicit COPY is necessary. Create one
444             // that can be reused
445             auto *NewRC = MI->getRegClassConstraint(i, TII, TRI);
446             if (NewRC == nullptr)
447               NewRC = OrigRC;
448             Register NewReg = MRI->createVirtualRegister(NewRC);
449             BuildMI(*PredBB, NewMI, NewMI.getDebugLoc(),
450                     TII->get(TargetOpcode::COPY), NewReg)
451                 .addReg(VI->second.Reg, 0, VI->second.SubReg);
452             LocalVRMap.erase(VI);
453             LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0)));
454             MO.setReg(NewReg);
455             // The composed VI.Reg:VI.SubReg is replaced with NewReg, which
456             // is equivalent to the whole register Reg. Hence, Reg:subreg
457             // is same as NewReg:subreg, so keep the sub-register index
458             // unchanged.
459           }
460           // Clear any kill flags from this operand.  The new register could
461           // have uses after this one, so kills are not valid here.
462           MO.setIsKill(false);
463         }
464       }
465     }
466   }
467 }
468 
469 /// After FromBB is tail duplicated into its predecessor blocks, the successors
470 /// have gained new predecessors. Update the PHI instructions in them
471 /// accordingly.
472 void TailDuplicator::updateSuccessorsPHIs(
473     MachineBasicBlock *FromBB, bool isDead,
474     SmallVectorImpl<MachineBasicBlock *> &TDBBs,
475     SmallSetVector<MachineBasicBlock *, 8> &Succs) {
476   for (MachineBasicBlock *SuccBB : Succs) {
477     for (MachineInstr &MI : *SuccBB) {
478       if (!MI.isPHI())
479         break;
480       MachineInstrBuilder MIB(*FromBB->getParent(), MI);
481       unsigned Idx = 0;
482       for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
483         MachineOperand &MO = MI.getOperand(i + 1);
484         if (MO.getMBB() == FromBB) {
485           Idx = i;
486           break;
487         }
488       }
489 
490       assert(Idx != 0);
491       MachineOperand &MO0 = MI.getOperand(Idx);
492       Register Reg = MO0.getReg();
493       if (isDead) {
494         // Folded into the previous BB.
495         // There could be duplicate phi source entries. FIXME: Should sdisel
496         // or earlier pass fixed this?
497         for (unsigned i = MI.getNumOperands() - 2; i != Idx; i -= 2) {
498           MachineOperand &MO = MI.getOperand(i + 1);
499           if (MO.getMBB() == FromBB) {
500             MI.RemoveOperand(i + 1);
501             MI.RemoveOperand(i);
502           }
503         }
504       } else
505         Idx = 0;
506 
507       // If Idx is set, the operands at Idx and Idx+1 must be removed.
508       // We reuse the location to avoid expensive RemoveOperand calls.
509 
510       DenseMap<Register, AvailableValsTy>::iterator LI =
511           SSAUpdateVals.find(Reg);
512       if (LI != SSAUpdateVals.end()) {
513         // This register is defined in the tail block.
514         for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
515           MachineBasicBlock *SrcBB = LI->second[j].first;
516           // If we didn't duplicate a bb into a particular predecessor, we
517           // might still have added an entry to SSAUpdateVals to correcly
518           // recompute SSA. If that case, avoid adding a dummy extra argument
519           // this PHI.
520           if (!SrcBB->isSuccessor(SuccBB))
521             continue;
522 
523           Register SrcReg = LI->second[j].second;
524           if (Idx != 0) {
525             MI.getOperand(Idx).setReg(SrcReg);
526             MI.getOperand(Idx + 1).setMBB(SrcBB);
527             Idx = 0;
528           } else {
529             MIB.addReg(SrcReg).addMBB(SrcBB);
530           }
531         }
532       } else {
533         // Live in tail block, must also be live in predecessors.
534         for (unsigned j = 0, ee = TDBBs.size(); j != ee; ++j) {
535           MachineBasicBlock *SrcBB = TDBBs[j];
536           if (Idx != 0) {
537             MI.getOperand(Idx).setReg(Reg);
538             MI.getOperand(Idx + 1).setMBB(SrcBB);
539             Idx = 0;
540           } else {
541             MIB.addReg(Reg).addMBB(SrcBB);
542           }
543         }
544       }
545       if (Idx != 0) {
546         MI.RemoveOperand(Idx + 1);
547         MI.RemoveOperand(Idx);
548       }
549     }
550   }
551 }
552 
553 /// Determine if it is profitable to duplicate this block.
554 bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
555                                          MachineBasicBlock &TailBB) {
556   // When doing tail-duplication during layout, the block ordering is in flux,
557   // so canFallThrough returns a result based on incorrect information and
558   // should just be ignored.
559   if (!LayoutMode && TailBB.canFallThrough())
560     return false;
561 
562   // Don't try to tail-duplicate single-block loops.
563   if (TailBB.isSuccessor(&TailBB))
564     return false;
565 
566   // Set the limit on the cost to duplicate. When optimizing for size,
567   // duplicate only one, because one branch instruction can be eliminated to
568   // compensate for the duplication.
569   unsigned MaxDuplicateCount;
570   bool OptForSize = MF->getFunction().hasOptSize() ||
571                     llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI);
572   if (TailDupSize == 0)
573     MaxDuplicateCount = TailDuplicateSize;
574   else
575     MaxDuplicateCount = TailDupSize;
576   if (OptForSize)
577     MaxDuplicateCount = 1;
578 
579   // If the block to be duplicated ends in an unanalyzable fallthrough, don't
580   // duplicate it.
581   // A similar check is necessary in MachineBlockPlacement to make sure pairs of
582   // blocks with unanalyzable fallthrough get layed out contiguously.
583   MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
584   SmallVector<MachineOperand, 4> PredCond;
585   if (TII->analyzeBranch(TailBB, PredTBB, PredFBB, PredCond) &&
586       TailBB.canFallThrough())
587     return false;
588 
589   // If the target has hardware branch prediction that can handle indirect
590   // branches, duplicating them can often make them predictable when there
591   // are common paths through the code.  The limit needs to be high enough
592   // to allow undoing the effects of tail merging and other optimizations
593   // that rearrange the predecessors of the indirect branch.
594 
595   bool HasIndirectbr = false;
596   if (!TailBB.empty())
597     HasIndirectbr = TailBB.back().isIndirectBranch();
598 
599   if (HasIndirectbr && PreRegAlloc)
600     MaxDuplicateCount = TailDupIndirectBranchSize;
601 
602   // Check the instructions in the block to determine whether tail-duplication
603   // is invalid or unlikely to be profitable.
604   unsigned InstrCount = 0;
605   for (MachineInstr &MI : TailBB) {
606     // Non-duplicable things shouldn't be tail-duplicated.
607     // CFI instructions are marked as non-duplicable, because Darwin compact
608     // unwind info emission can't handle multiple prologue setups. In case of
609     // DWARF, allow them be duplicated, so that their existence doesn't prevent
610     // tail duplication of some basic blocks, that would be duplicated otherwise.
611     if (MI.isNotDuplicable() &&
612         (TailBB.getParent()->getTarget().getTargetTriple().isOSDarwin() ||
613         !MI.isCFIInstruction()))
614       return false;
615 
616     // Convergent instructions can be duplicated only if doing so doesn't add
617     // new control dependencies, which is what we're going to do here.
618     if (MI.isConvergent())
619       return false;
620 
621     // Do not duplicate 'return' instructions if this is a pre-regalloc run.
622     // A return may expand into a lot more instructions (e.g. reload of callee
623     // saved registers) after PEI.
624     if (PreRegAlloc && MI.isReturn())
625       return false;
626 
627     // Avoid duplicating calls before register allocation. Calls presents a
628     // barrier to register allocation so duplicating them may end up increasing
629     // spills.
630     if (PreRegAlloc && MI.isCall())
631       return false;
632 
633     // TailDuplicator::appendCopies will erroneously place COPYs after
634     // INLINEASM_BR instructions after 4b0aa5724fea, which demonstrates the same
635     // bug that was fixed in f7a53d82c090.
636     // FIXME: Use findPHICopyInsertPoint() to find the correct insertion point
637     //        for the COPY when replacing PHIs.
638     if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
639       return false;
640 
641     if (MI.isBundle())
642       InstrCount += MI.getBundleSize();
643     else if (!MI.isPHI() && !MI.isMetaInstruction())
644       InstrCount += 1;
645 
646     if (InstrCount > MaxDuplicateCount)
647       return false;
648   }
649 
650   // Check if any of the successors of TailBB has a PHI node in which the
651   // value corresponding to TailBB uses a subregister.
652   // If a phi node uses a register paired with a subregister, the actual
653   // "value type" of the phi may differ from the type of the register without
654   // any subregisters. Due to a bug, tail duplication may add a new operand
655   // without a necessary subregister, producing an invalid code. This is
656   // demonstrated by test/CodeGen/Hexagon/tail-dup-subreg-abort.ll.
657   // Disable tail duplication for this case for now, until the problem is
658   // fixed.
659   for (auto SB : TailBB.successors()) {
660     for (auto &I : *SB) {
661       if (!I.isPHI())
662         break;
663       unsigned Idx = getPHISrcRegOpIdx(&I, &TailBB);
664       assert(Idx != 0);
665       MachineOperand &PU = I.getOperand(Idx);
666       if (PU.getSubReg() != 0)
667         return false;
668     }
669   }
670 
671   if (HasIndirectbr && PreRegAlloc)
672     return true;
673 
674   if (IsSimple)
675     return true;
676 
677   if (!PreRegAlloc)
678     return true;
679 
680   return canCompletelyDuplicateBB(TailBB);
681 }
682 
683 /// True if this BB has only one unconditional jump.
684 bool TailDuplicator::isSimpleBB(MachineBasicBlock *TailBB) {
685   if (TailBB->succ_size() != 1)
686     return false;
687   if (TailBB->pred_empty())
688     return false;
689   MachineBasicBlock::iterator I = TailBB->getFirstNonDebugInstr(true);
690   if (I == TailBB->end())
691     return true;
692   return I->isUnconditionalBranch();
693 }
694 
695 static bool bothUsedInPHI(const MachineBasicBlock &A,
696                           const SmallPtrSet<MachineBasicBlock *, 8> &SuccsB) {
697   for (MachineBasicBlock *BB : A.successors())
698     if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI())
699       return true;
700 
701   return false;
702 }
703 
704 bool TailDuplicator::canCompletelyDuplicateBB(MachineBasicBlock &BB) {
705   for (MachineBasicBlock *PredBB : BB.predecessors()) {
706     if (PredBB->succ_size() > 1)
707       return false;
708 
709     MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
710     SmallVector<MachineOperand, 4> PredCond;
711     if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
712       return false;
713 
714     if (!PredCond.empty())
715       return false;
716   }
717   return true;
718 }
719 
720 bool TailDuplicator::duplicateSimpleBB(
721     MachineBasicBlock *TailBB, SmallVectorImpl<MachineBasicBlock *> &TDBBs,
722     const DenseSet<Register> &UsedByPhi,
723     SmallVectorImpl<MachineInstr *> &Copies) {
724   SmallPtrSet<MachineBasicBlock *, 8> Succs(TailBB->succ_begin(),
725                                             TailBB->succ_end());
726   SmallVector<MachineBasicBlock *, 8> Preds(TailBB->predecessors());
727   bool Changed = false;
728   for (MachineBasicBlock *PredBB : Preds) {
729     if (PredBB->hasEHPadSuccessor() || PredBB->mayHaveInlineAsmBr())
730       continue;
731 
732     if (bothUsedInPHI(*PredBB, Succs))
733       continue;
734 
735     MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
736     SmallVector<MachineOperand, 4> PredCond;
737     if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
738       continue;
739 
740     Changed = true;
741     LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
742                       << "From simple Succ: " << *TailBB);
743 
744     MachineBasicBlock *NewTarget = *TailBB->succ_begin();
745     MachineBasicBlock *NextBB = PredBB->getNextNode();
746 
747     // Make PredFBB explicit.
748     if (PredCond.empty())
749       PredFBB = PredTBB;
750 
751     // Make fall through explicit.
752     if (!PredTBB)
753       PredTBB = NextBB;
754     if (!PredFBB)
755       PredFBB = NextBB;
756 
757     // Redirect
758     if (PredFBB == TailBB)
759       PredFBB = NewTarget;
760     if (PredTBB == TailBB)
761       PredTBB = NewTarget;
762 
763     // Make the branch unconditional if possible
764     if (PredTBB == PredFBB) {
765       PredCond.clear();
766       PredFBB = nullptr;
767     }
768 
769     // Avoid adding fall through branches.
770     if (PredFBB == NextBB)
771       PredFBB = nullptr;
772     if (PredTBB == NextBB && PredFBB == nullptr)
773       PredTBB = nullptr;
774 
775     auto DL = PredBB->findBranchDebugLoc();
776     TII->removeBranch(*PredBB);
777 
778     if (!PredBB->isSuccessor(NewTarget))
779       PredBB->replaceSuccessor(TailBB, NewTarget);
780     else {
781       PredBB->removeSuccessor(TailBB, true);
782       assert(PredBB->succ_size() <= 1);
783     }
784 
785     if (PredTBB)
786       TII->insertBranch(*PredBB, PredTBB, PredFBB, PredCond, DL);
787 
788     TDBBs.push_back(PredBB);
789   }
790   return Changed;
791 }
792 
793 bool TailDuplicator::canTailDuplicate(MachineBasicBlock *TailBB,
794                                       MachineBasicBlock *PredBB) {
795   // EH edges are ignored by analyzeBranch.
796   if (PredBB->succ_size() > 1)
797     return false;
798 
799   MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
800   SmallVector<MachineOperand, 4> PredCond;
801   if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
802     return false;
803   if (!PredCond.empty())
804     return false;
805   return true;
806 }
807 
808 /// If it is profitable, duplicate TailBB's contents in each
809 /// of its predecessors.
810 /// \p IsSimple result of isSimpleBB
811 /// \p TailBB   Block to be duplicated.
812 /// \p ForcedLayoutPred  When non-null, use this block as the layout predecessor
813 ///                      instead of the previous block in MF's order.
814 /// \p TDBBs             A vector to keep track of all blocks tail-duplicated
815 ///                      into.
816 /// \p Copies            A vector of copy instructions inserted. Used later to
817 ///                      walk all the inserted copies and remove redundant ones.
818 bool TailDuplicator::tailDuplicate(bool IsSimple, MachineBasicBlock *TailBB,
819                           MachineBasicBlock *ForcedLayoutPred,
820                           SmallVectorImpl<MachineBasicBlock *> &TDBBs,
821                           SmallVectorImpl<MachineInstr *> &Copies,
822                           SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) {
823   LLVM_DEBUG(dbgs() << "\n*** Tail-duplicating " << printMBBReference(*TailBB)
824                     << '\n');
825 
826   bool ShouldUpdateTerminators = TailBB->canFallThrough();
827 
828   DenseSet<Register> UsedByPhi;
829   getRegsUsedByPHIs(*TailBB, &UsedByPhi);
830 
831   if (IsSimple)
832     return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi, Copies);
833 
834   // Iterate through all the unique predecessors and tail-duplicate this
835   // block into them, if possible. Copying the list ahead of time also
836   // avoids trouble with the predecessor list reallocating.
837   bool Changed = false;
838   SmallSetVector<MachineBasicBlock *, 8> Preds;
839   if (CandidatePtr)
840     Preds.insert(CandidatePtr->begin(), CandidatePtr->end());
841   else
842     Preds.insert(TailBB->pred_begin(), TailBB->pred_end());
843 
844   for (MachineBasicBlock *PredBB : Preds) {
845     assert(TailBB != PredBB &&
846            "Single-block loop should have been rejected earlier!");
847 
848     if (!canTailDuplicate(TailBB, PredBB))
849       continue;
850 
851     // Don't duplicate into a fall-through predecessor (at least for now).
852     // If profile is available, findDuplicateCandidates can choose better
853     // fall-through predecessor.
854     if (!(MF->getFunction().hasProfileData() && LayoutMode)) {
855       bool IsLayoutSuccessor = false;
856       if (ForcedLayoutPred)
857         IsLayoutSuccessor = (ForcedLayoutPred == PredBB);
858       else if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
859         IsLayoutSuccessor = true;
860       if (IsLayoutSuccessor)
861         continue;
862     }
863 
864     LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
865                       << "From Succ: " << *TailBB);
866 
867     TDBBs.push_back(PredBB);
868 
869     // Remove PredBB's unconditional branch.
870     TII->removeBranch(*PredBB);
871 
872     // Clone the contents of TailBB into PredBB.
873     DenseMap<Register, RegSubRegPair> LocalVRMap;
874     SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
875     for (MachineBasicBlock::iterator I = TailBB->begin(), E = TailBB->end();
876          I != E; /* empty */) {
877       MachineInstr *MI = &*I;
878       ++I;
879       if (MI->isPHI()) {
880         // Replace the uses of the def of the PHI with the register coming
881         // from PredBB.
882         processPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true);
883       } else {
884         // Replace def of virtual registers with new registers, and update
885         // uses with PHI source register or the new registers.
886         duplicateInstruction(MI, TailBB, PredBB, LocalVRMap, UsedByPhi);
887       }
888     }
889     appendCopies(PredBB, CopyInfos, Copies);
890 
891     NumTailDupAdded += TailBB->size() - 1; // subtract one for removed branch
892 
893     // Update the CFG.
894     PredBB->removeSuccessor(PredBB->succ_begin());
895     assert(PredBB->succ_empty() &&
896            "TailDuplicate called on block with multiple successors!");
897     for (MachineBasicBlock *Succ : TailBB->successors())
898       PredBB->addSuccessor(Succ, MBPI->getEdgeProbability(TailBB, Succ));
899 
900     // Update branches in pred to jump to tail's layout successor if needed.
901     if (ShouldUpdateTerminators)
902       PredBB->updateTerminator(TailBB->getNextNode());
903 
904     Changed = true;
905     ++NumTailDups;
906   }
907 
908   // If TailBB was duplicated into all its predecessors except for the prior
909   // block, which falls through unconditionally, move the contents of this
910   // block into the prior block.
911   MachineBasicBlock *PrevBB = ForcedLayoutPred;
912   if (!PrevBB)
913     PrevBB = &*std::prev(TailBB->getIterator());
914   MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
915   SmallVector<MachineOperand, 4> PriorCond;
916   // This has to check PrevBB->succ_size() because EH edges are ignored by
917   // analyzeBranch.
918   if (PrevBB->succ_size() == 1 &&
919       // Layout preds are not always CFG preds. Check.
920       *PrevBB->succ_begin() == TailBB &&
921       !TII->analyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond) &&
922       PriorCond.empty() &&
923       (!PriorTBB || PriorTBB == TailBB) &&
924       TailBB->pred_size() == 1 &&
925       !TailBB->hasAddressTaken()) {
926     LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
927                       << "From MBB: " << *TailBB);
928     // There may be a branch to the layout successor. This is unlikely but it
929     // happens. The correct thing to do is to remove the branch before
930     // duplicating the instructions in all cases.
931     TII->removeBranch(*PrevBB);
932     if (PreRegAlloc) {
933       DenseMap<Register, RegSubRegPair> LocalVRMap;
934       SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
935       MachineBasicBlock::iterator I = TailBB->begin();
936       // Process PHI instructions first.
937       while (I != TailBB->end() && I->isPHI()) {
938         // Replace the uses of the def of the PHI with the register coming
939         // from PredBB.
940         MachineInstr *MI = &*I++;
941         processPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi, true);
942       }
943 
944       // Now copy the non-PHI instructions.
945       while (I != TailBB->end()) {
946         // Replace def of virtual registers with new registers, and update
947         // uses with PHI source register or the new registers.
948         MachineInstr *MI = &*I++;
949         assert(!MI->isBundle() && "Not expecting bundles before regalloc!");
950         duplicateInstruction(MI, TailBB, PrevBB, LocalVRMap, UsedByPhi);
951         MI->eraseFromParent();
952       }
953       appendCopies(PrevBB, CopyInfos, Copies);
954     } else {
955       TII->removeBranch(*PrevBB);
956       // No PHIs to worry about, just splice the instructions over.
957       PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
958     }
959     PrevBB->removeSuccessor(PrevBB->succ_begin());
960     assert(PrevBB->succ_empty());
961     PrevBB->transferSuccessors(TailBB);
962 
963     // Update branches in PrevBB based on Tail's layout successor.
964     if (ShouldUpdateTerminators)
965       PrevBB->updateTerminator(TailBB->getNextNode());
966 
967     TDBBs.push_back(PrevBB);
968     Changed = true;
969   }
970 
971   // If this is after register allocation, there are no phis to fix.
972   if (!PreRegAlloc)
973     return Changed;
974 
975   // If we made no changes so far, we are safe.
976   if (!Changed)
977     return Changed;
978 
979   // Handle the nasty case in that we duplicated a block that is part of a loop
980   // into some but not all of its predecessors. For example:
981   //    1 -> 2 <-> 3                 |
982   //          \                      |
983   //           \---> rest            |
984   // if we duplicate 2 into 1 but not into 3, we end up with
985   // 12 -> 3 <-> 2 -> rest           |
986   //   \             /               |
987   //    \----->-----/                |
988   // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced
989   // with a phi in 3 (which now dominates 2).
990   // What we do here is introduce a copy in 3 of the register defined by the
991   // phi, just like when we are duplicating 2 into 3, but we don't copy any
992   // real instructions or remove the 3 -> 2 edge from the phi in 2.
993   for (MachineBasicBlock *PredBB : Preds) {
994     if (is_contained(TDBBs, PredBB))
995       continue;
996 
997     // EH edges
998     if (PredBB->succ_size() != 1)
999       continue;
1000 
1001     DenseMap<Register, RegSubRegPair> LocalVRMap;
1002     SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
1003     MachineBasicBlock::iterator I = TailBB->begin();
1004     // Process PHI instructions first.
1005     while (I != TailBB->end() && I->isPHI()) {
1006       // Replace the uses of the def of the PHI with the register coming
1007       // from PredBB.
1008       MachineInstr *MI = &*I++;
1009       processPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false);
1010     }
1011     appendCopies(PredBB, CopyInfos, Copies);
1012   }
1013 
1014   return Changed;
1015 }
1016 
1017 /// At the end of the block \p MBB generate COPY instructions between registers
1018 /// described by \p CopyInfos. Append resulting instructions to \p Copies.
1019 void TailDuplicator::appendCopies(MachineBasicBlock *MBB,
1020       SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos,
1021       SmallVectorImpl<MachineInstr*> &Copies) {
1022   MachineBasicBlock::iterator Loc = MBB->getFirstTerminator();
1023   const MCInstrDesc &CopyD = TII->get(TargetOpcode::COPY);
1024   for (auto &CI : CopyInfos) {
1025     auto C = BuildMI(*MBB, Loc, DebugLoc(), CopyD, CI.first)
1026                 .addReg(CI.second.Reg, 0, CI.second.SubReg);
1027     Copies.push_back(C);
1028   }
1029 }
1030 
1031 /// Remove the specified dead machine basic block from the function, updating
1032 /// the CFG.
1033 void TailDuplicator::removeDeadBlock(
1034     MachineBasicBlock *MBB,
1035     function_ref<void(MachineBasicBlock *)> *RemovalCallback) {
1036   assert(MBB->pred_empty() && "MBB must be dead!");
1037   LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
1038 
1039   MachineFunction *MF = MBB->getParent();
1040   // Update the call site info.
1041   for (const MachineInstr &MI : *MBB)
1042     if (MI.shouldUpdateCallSiteInfo())
1043       MF->eraseCallSiteInfo(&MI);
1044 
1045   if (RemovalCallback)
1046     (*RemovalCallback)(MBB);
1047 
1048   // Remove all successors.
1049   while (!MBB->succ_empty())
1050     MBB->removeSuccessor(MBB->succ_end() - 1);
1051 
1052   // Remove the block.
1053   MBB->eraseFromParent();
1054 }
1055