xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/TailDuplicator.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- TailDuplicator.cpp - Duplicate blocks into predecessors' tails -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This utility class duplicates basic blocks ending in unconditional branches
100b57cec5SDimitry Andric // into the tails of their predecessors.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/CodeGen/TailDuplicator.h"
150b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
160b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
170b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
200b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
210b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineSSAUpdater.h"
3081ad6265SDimitry Andric #include "llvm/CodeGen/MachineSizeOpts.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
340b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
350b57cec5SDimitry Andric #include "llvm/IR/Function.h"
360b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
370b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
380b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
400b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
410b57cec5SDimitry Andric #include <algorithm>
420b57cec5SDimitry Andric #include <cassert>
430b57cec5SDimitry Andric #include <iterator>
440b57cec5SDimitry Andric #include <utility>
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric using namespace llvm;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric #define DEBUG_TYPE "tailduplication"
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric STATISTIC(NumTails, "Number of tails duplicated");
510b57cec5SDimitry Andric STATISTIC(NumTailDups, "Number of tail duplicated blocks");
520b57cec5SDimitry Andric STATISTIC(NumTailDupAdded,
530b57cec5SDimitry Andric           "Number of instructions added due to tail duplication");
540b57cec5SDimitry Andric STATISTIC(NumTailDupRemoved,
550b57cec5SDimitry Andric           "Number of instructions removed due to tail duplication");
560b57cec5SDimitry Andric STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
570b57cec5SDimitry Andric STATISTIC(NumAddedPHIs, "Number of phis added");
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric // Heuristic for tail duplication.
600b57cec5SDimitry Andric static cl::opt<unsigned> TailDuplicateSize(
610b57cec5SDimitry Andric     "tail-dup-size",
620b57cec5SDimitry Andric     cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2),
630b57cec5SDimitry Andric     cl::Hidden);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric static cl::opt<unsigned> TailDupIndirectBranchSize(
660b57cec5SDimitry Andric     "tail-dup-indirect-size",
670b57cec5SDimitry Andric     cl::desc("Maximum instructions to consider tail duplicating blocks that "
680b57cec5SDimitry Andric              "end with indirect branches."), cl::init(20),
690b57cec5SDimitry Andric     cl::Hidden);
700b57cec5SDimitry Andric 
71*0fca6ea1SDimitry Andric static cl::opt<unsigned>
72*0fca6ea1SDimitry Andric     TailDupPredSize("tail-dup-pred-size",
73*0fca6ea1SDimitry Andric                     cl::desc("Maximum predecessors (maximum successors at the "
74*0fca6ea1SDimitry Andric                              "same time) to consider tail duplicating blocks."),
75*0fca6ea1SDimitry Andric                     cl::init(16), cl::Hidden);
76*0fca6ea1SDimitry Andric 
77*0fca6ea1SDimitry Andric static cl::opt<unsigned>
78*0fca6ea1SDimitry Andric     TailDupSuccSize("tail-dup-succ-size",
79*0fca6ea1SDimitry Andric                     cl::desc("Maximum successors (maximum predecessors at the "
80*0fca6ea1SDimitry Andric                              "same time) to consider tail duplicating blocks."),
81*0fca6ea1SDimitry Andric                     cl::init(16), cl::Hidden);
82*0fca6ea1SDimitry Andric 
830b57cec5SDimitry Andric static cl::opt<bool>
840b57cec5SDimitry Andric     TailDupVerify("tail-dup-verify",
850b57cec5SDimitry Andric                   cl::desc("Verify sanity of PHI instructions during taildup"),
860b57cec5SDimitry Andric                   cl::init(false), cl::Hidden);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric static cl::opt<unsigned> TailDupLimit("tail-dup-limit", cl::init(~0U),
890b57cec5SDimitry Andric                                       cl::Hidden);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric void TailDuplicator::initMF(MachineFunction &MFin, bool PreRegAlloc,
920b57cec5SDimitry Andric                             const MachineBranchProbabilityInfo *MBPIin,
935ffd83dbSDimitry Andric                             MBFIWrapper *MBFIin,
94480093f4SDimitry Andric                             ProfileSummaryInfo *PSIin,
950b57cec5SDimitry Andric                             bool LayoutModeIn, unsigned TailDupSizeIn) {
960b57cec5SDimitry Andric   MF = &MFin;
970b57cec5SDimitry Andric   TII = MF->getSubtarget().getInstrInfo();
980b57cec5SDimitry Andric   TRI = MF->getSubtarget().getRegisterInfo();
990b57cec5SDimitry Andric   MRI = &MF->getRegInfo();
1000b57cec5SDimitry Andric   MBPI = MBPIin;
101480093f4SDimitry Andric   MBFI = MBFIin;
102480093f4SDimitry Andric   PSI = PSIin;
1030b57cec5SDimitry Andric   TailDupSize = TailDupSizeIn;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   assert(MBPI != nullptr && "Machine Branch Probability Info required");
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   LayoutMode = LayoutModeIn;
1080b57cec5SDimitry Andric   this->PreRegAlloc = PreRegAlloc;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) {
112349cc55cSDimitry Andric   for (MachineBasicBlock &MBB : llvm::drop_begin(MF)) {
113349cc55cSDimitry Andric     SmallSetVector<MachineBasicBlock *, 8> Preds(MBB.pred_begin(),
114349cc55cSDimitry Andric                                                  MBB.pred_end());
115349cc55cSDimitry Andric     MachineBasicBlock::iterator MI = MBB.begin();
116349cc55cSDimitry Andric     while (MI != MBB.end()) {
1170b57cec5SDimitry Andric       if (!MI->isPHI())
1180b57cec5SDimitry Andric         break;
1190b57cec5SDimitry Andric       for (MachineBasicBlock *PredBB : Preds) {
1200b57cec5SDimitry Andric         bool Found = false;
1210b57cec5SDimitry Andric         for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
1220b57cec5SDimitry Andric           MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
1230b57cec5SDimitry Andric           if (PHIBB == PredBB) {
1240b57cec5SDimitry Andric             Found = true;
1250b57cec5SDimitry Andric             break;
1260b57cec5SDimitry Andric           }
1270b57cec5SDimitry Andric         }
1280b57cec5SDimitry Andric         if (!Found) {
129349cc55cSDimitry Andric           dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
1300b57cec5SDimitry Andric                  << *MI;
1310b57cec5SDimitry Andric           dbgs() << "  missing input from predecessor "
1320b57cec5SDimitry Andric                  << printMBBReference(*PredBB) << '\n';
1330b57cec5SDimitry Andric           llvm_unreachable(nullptr);
1340b57cec5SDimitry Andric         }
1350b57cec5SDimitry Andric       }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric       for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
1380b57cec5SDimitry Andric         MachineBasicBlock *PHIBB = MI->getOperand(i + 1).getMBB();
1390b57cec5SDimitry Andric         if (CheckExtra && !Preds.count(PHIBB)) {
140349cc55cSDimitry Andric           dbgs() << "Warning: malformed PHI in " << printMBBReference(MBB)
1410b57cec5SDimitry Andric                  << ": " << *MI;
1420b57cec5SDimitry Andric           dbgs() << "  extra input from predecessor "
1430b57cec5SDimitry Andric                  << printMBBReference(*PHIBB) << '\n';
1440b57cec5SDimitry Andric           llvm_unreachable(nullptr);
1450b57cec5SDimitry Andric         }
1460b57cec5SDimitry Andric         if (PHIBB->getNumber() < 0) {
147349cc55cSDimitry Andric           dbgs() << "Malformed PHI in " << printMBBReference(MBB) << ": "
1480b57cec5SDimitry Andric                  << *MI;
1490b57cec5SDimitry Andric           dbgs() << "  non-existing " << printMBBReference(*PHIBB) << '\n';
1500b57cec5SDimitry Andric           llvm_unreachable(nullptr);
1510b57cec5SDimitry Andric         }
1520b57cec5SDimitry Andric       }
1530b57cec5SDimitry Andric       ++MI;
1540b57cec5SDimitry Andric     }
1550b57cec5SDimitry Andric   }
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric /// Tail duplicate the block and cleanup.
1590b57cec5SDimitry Andric /// \p IsSimple - return value of isSimpleBB
1600b57cec5SDimitry Andric /// \p MBB - block to be duplicated
1610b57cec5SDimitry Andric /// \p ForcedLayoutPred - If non-null, treat this block as the layout
1620b57cec5SDimitry Andric ///     predecessor, instead of using the ordering in MF
1630b57cec5SDimitry Andric /// \p DuplicatedPreds - if non-null, \p DuplicatedPreds will contain a list of
1640b57cec5SDimitry Andric ///     all Preds that received a copy of \p MBB.
1650b57cec5SDimitry Andric /// \p RemovalCallback - if non-null, called just before MBB is deleted.
1660b57cec5SDimitry Andric bool TailDuplicator::tailDuplicateAndUpdate(
1670b57cec5SDimitry Andric     bool IsSimple, MachineBasicBlock *MBB,
1680b57cec5SDimitry Andric     MachineBasicBlock *ForcedLayoutPred,
1690b57cec5SDimitry Andric     SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds,
1705ffd83dbSDimitry Andric     function_ref<void(MachineBasicBlock *)> *RemovalCallback,
1715ffd83dbSDimitry Andric     SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) {
1720b57cec5SDimitry Andric   // Save the successors list.
1730b57cec5SDimitry Andric   SmallSetVector<MachineBasicBlock *, 8> Succs(MBB->succ_begin(),
1740b57cec5SDimitry Andric                                                MBB->succ_end());
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   SmallVector<MachineBasicBlock *, 8> TDBBs;
1770b57cec5SDimitry Andric   SmallVector<MachineInstr *, 16> Copies;
1785ffd83dbSDimitry Andric   if (!tailDuplicate(IsSimple, MBB, ForcedLayoutPred,
1795ffd83dbSDimitry Andric                      TDBBs, Copies, CandidatePtr))
1800b57cec5SDimitry Andric     return false;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   ++NumTails;
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   SmallVector<MachineInstr *, 8> NewPHIs;
1850b57cec5SDimitry Andric   MachineSSAUpdater SSAUpdate(*MF, &NewPHIs);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   // TailBB's immediate successors are now successors of those predecessors
1880b57cec5SDimitry Andric   // which duplicated TailBB. Add the predecessors as sources to the PHI
1890b57cec5SDimitry Andric   // instructions.
1900b57cec5SDimitry Andric   bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken();
1910b57cec5SDimitry Andric   if (PreRegAlloc)
1920b57cec5SDimitry Andric     updateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   // If it is dead, remove it.
1950b57cec5SDimitry Andric   if (isDead) {
1960b57cec5SDimitry Andric     NumTailDupRemoved += MBB->size();
1970b57cec5SDimitry Andric     removeDeadBlock(MBB, RemovalCallback);
1980b57cec5SDimitry Andric     ++NumDeadBlocks;
1990b57cec5SDimitry Andric   }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   // Update SSA form.
2020b57cec5SDimitry Andric   if (!SSAUpdateVRs.empty()) {
203*0fca6ea1SDimitry Andric     for (unsigned VReg : SSAUpdateVRs) {
2040b57cec5SDimitry Andric       SSAUpdate.Initialize(VReg);
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric       // If the original definition is still around, add it as an available
2070b57cec5SDimitry Andric       // value.
2080b57cec5SDimitry Andric       MachineInstr *DefMI = MRI->getVRegDef(VReg);
2090b57cec5SDimitry Andric       MachineBasicBlock *DefBB = nullptr;
2100b57cec5SDimitry Andric       if (DefMI) {
2110b57cec5SDimitry Andric         DefBB = DefMI->getParent();
2120b57cec5SDimitry Andric         SSAUpdate.AddAvailableValue(DefBB, VReg);
2130b57cec5SDimitry Andric       }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric       // Add the new vregs as available values.
2165ffd83dbSDimitry Andric       DenseMap<Register, AvailableValsTy>::iterator LI =
2170b57cec5SDimitry Andric           SSAUpdateVals.find(VReg);
2180eae32dcSDimitry Andric       for (std::pair<MachineBasicBlock *, Register> &J : LI->second) {
2190eae32dcSDimitry Andric         MachineBasicBlock *SrcBB = J.first;
2200eae32dcSDimitry Andric         Register SrcReg = J.second;
2210b57cec5SDimitry Andric         SSAUpdate.AddAvailableValue(SrcBB, SrcReg);
2220b57cec5SDimitry Andric       }
2230b57cec5SDimitry Andric 
2240eae32dcSDimitry Andric       SmallVector<MachineOperand *> DebugUses;
2250b57cec5SDimitry Andric       // Rewrite uses that are outside of the original def's block.
2260eae32dcSDimitry Andric       for (MachineOperand &UseMO :
2270eae32dcSDimitry Andric            llvm::make_early_inc_range(MRI->use_operands(VReg))) {
2280b57cec5SDimitry Andric         MachineInstr *UseMI = UseMO.getParent();
2290eae32dcSDimitry Andric         // Rewrite debug uses last so that they can take advantage of any
2300eae32dcSDimitry Andric         // register mappings introduced by other users in its BB, since we
2310eae32dcSDimitry Andric         // cannot create new register definitions specifically for the debug
2320eae32dcSDimitry Andric         // instruction (as debug instructions should not affect CodeGen).
2330b57cec5SDimitry Andric         if (UseMI->isDebugValue()) {
2340eae32dcSDimitry Andric           DebugUses.push_back(&UseMO);
2350b57cec5SDimitry Andric           continue;
2360b57cec5SDimitry Andric         }
2370b57cec5SDimitry Andric         if (UseMI->getParent() == DefBB && !UseMI->isPHI())
2380b57cec5SDimitry Andric           continue;
2390b57cec5SDimitry Andric         SSAUpdate.RewriteUse(UseMO);
2400b57cec5SDimitry Andric       }
2410eae32dcSDimitry Andric       for (auto *UseMO : DebugUses) {
2420eae32dcSDimitry Andric         MachineInstr *UseMI = UseMO->getParent();
2430eae32dcSDimitry Andric         UseMO->setReg(
2440eae32dcSDimitry Andric             SSAUpdate.GetValueInMiddleOfBlock(UseMI->getParent(), true));
2450eae32dcSDimitry Andric       }
2460b57cec5SDimitry Andric     }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric     SSAUpdateVRs.clear();
2490b57cec5SDimitry Andric     SSAUpdateVals.clear();
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   // Eliminate some of the copies inserted by tail duplication to maintain
2530b57cec5SDimitry Andric   // SSA form.
254*0fca6ea1SDimitry Andric   for (MachineInstr *Copy : Copies) {
2550b57cec5SDimitry Andric     if (!Copy->isCopy())
2560b57cec5SDimitry Andric       continue;
2578bcb0991SDimitry Andric     Register Dst = Copy->getOperand(0).getReg();
2588bcb0991SDimitry Andric     Register Src = Copy->getOperand(1).getReg();
2590b57cec5SDimitry Andric     if (MRI->hasOneNonDBGUse(Src) &&
2600b57cec5SDimitry Andric         MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) {
2610b57cec5SDimitry Andric       // Copy is the only use. Do trivial copy propagation here.
2620b57cec5SDimitry Andric       MRI->replaceRegWith(Dst, Src);
2630b57cec5SDimitry Andric       Copy->eraseFromParent();
2640b57cec5SDimitry Andric     }
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   if (NewPHIs.size())
2680b57cec5SDimitry Andric     NumAddedPHIs += NewPHIs.size();
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   if (DuplicatedPreds)
2710b57cec5SDimitry Andric     *DuplicatedPreds = std::move(TDBBs);
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   return true;
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric /// Look for small blocks that are unconditionally branched to and do not fall
2770b57cec5SDimitry Andric /// through. Tail-duplicate their instructions into their predecessors to
2780b57cec5SDimitry Andric /// eliminate (dynamic) branches.
2790b57cec5SDimitry Andric bool TailDuplicator::tailDuplicateBlocks() {
2800b57cec5SDimitry Andric   bool MadeChange = false;
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   if (PreRegAlloc && TailDupVerify) {
2830b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\n*** Before tail-duplicating\n");
2840b57cec5SDimitry Andric     VerifyPHIs(*MF, true);
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
287349cc55cSDimitry Andric   for (MachineBasicBlock &MBB :
288349cc55cSDimitry Andric        llvm::make_early_inc_range(llvm::drop_begin(*MF))) {
2890b57cec5SDimitry Andric     if (NumTails == TailDupLimit)
2900b57cec5SDimitry Andric       break;
2910b57cec5SDimitry Andric 
292349cc55cSDimitry Andric     bool IsSimple = isSimpleBB(&MBB);
2930b57cec5SDimitry Andric 
294349cc55cSDimitry Andric     if (!shouldTailDuplicate(IsSimple, MBB))
2950b57cec5SDimitry Andric       continue;
2960b57cec5SDimitry Andric 
297349cc55cSDimitry Andric     MadeChange |= tailDuplicateAndUpdate(IsSimple, &MBB, nullptr);
2980b57cec5SDimitry Andric   }
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric   if (PreRegAlloc && TailDupVerify)
3010b57cec5SDimitry Andric     VerifyPHIs(*MF, false);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   return MadeChange;
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
3065ffd83dbSDimitry Andric static bool isDefLiveOut(Register Reg, MachineBasicBlock *BB,
3070b57cec5SDimitry Andric                          const MachineRegisterInfo *MRI) {
3080b57cec5SDimitry Andric   for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
3090b57cec5SDimitry Andric     if (UseMI.isDebugValue())
3100b57cec5SDimitry Andric       continue;
3110b57cec5SDimitry Andric     if (UseMI.getParent() != BB)
3120b57cec5SDimitry Andric       return true;
3130b57cec5SDimitry Andric   }
3140b57cec5SDimitry Andric   return false;
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) {
3180b57cec5SDimitry Andric   for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
3190b57cec5SDimitry Andric     if (MI->getOperand(i + 1).getMBB() == SrcBB)
3200b57cec5SDimitry Andric       return i;
3210b57cec5SDimitry Andric   return 0;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric // Remember which registers are used by phis in this block. This is
3250b57cec5SDimitry Andric // used to determine which registers are liveout while modifying the
3260b57cec5SDimitry Andric // block (which is why we need to copy the information).
3270b57cec5SDimitry Andric static void getRegsUsedByPHIs(const MachineBasicBlock &BB,
3285ffd83dbSDimitry Andric                               DenseSet<Register> *UsedByPhi) {
3290b57cec5SDimitry Andric   for (const auto &MI : BB) {
3300b57cec5SDimitry Andric     if (!MI.isPHI())
3310b57cec5SDimitry Andric       break;
3320b57cec5SDimitry Andric     for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
3338bcb0991SDimitry Andric       Register SrcReg = MI.getOperand(i).getReg();
3340b57cec5SDimitry Andric       UsedByPhi->insert(SrcReg);
3350b57cec5SDimitry Andric     }
3360b57cec5SDimitry Andric   }
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric /// Add a definition and source virtual registers pair for SSA update.
3405ffd83dbSDimitry Andric void TailDuplicator::addSSAUpdateEntry(Register OrigReg, Register NewReg,
3410b57cec5SDimitry Andric                                        MachineBasicBlock *BB) {
3425ffd83dbSDimitry Andric   DenseMap<Register, AvailableValsTy>::iterator LI =
3430b57cec5SDimitry Andric       SSAUpdateVals.find(OrigReg);
3440b57cec5SDimitry Andric   if (LI != SSAUpdateVals.end())
3450b57cec5SDimitry Andric     LI->second.push_back(std::make_pair(BB, NewReg));
3460b57cec5SDimitry Andric   else {
3470b57cec5SDimitry Andric     AvailableValsTy Vals;
3480b57cec5SDimitry Andric     Vals.push_back(std::make_pair(BB, NewReg));
3490b57cec5SDimitry Andric     SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
3500b57cec5SDimitry Andric     SSAUpdateVRs.push_back(OrigReg);
3510b57cec5SDimitry Andric   }
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric /// Process PHI node in TailBB by turning it into a copy in PredBB. Remember the
3550b57cec5SDimitry Andric /// source register that's contributed by PredBB and update SSA update map.
3560b57cec5SDimitry Andric void TailDuplicator::processPHI(
3570b57cec5SDimitry Andric     MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
3585ffd83dbSDimitry Andric     DenseMap<Register, RegSubRegPair> &LocalVRMap,
3595ffd83dbSDimitry Andric     SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies,
3605ffd83dbSDimitry Andric     const DenseSet<Register> &RegsUsedByPhi, bool Remove) {
3618bcb0991SDimitry Andric   Register DefReg = MI->getOperand(0).getReg();
3620b57cec5SDimitry Andric   unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
3630b57cec5SDimitry Andric   assert(SrcOpIdx && "Unable to find matching PHI source?");
3648bcb0991SDimitry Andric   Register SrcReg = MI->getOperand(SrcOpIdx).getReg();
3650b57cec5SDimitry Andric   unsigned SrcSubReg = MI->getOperand(SrcOpIdx).getSubReg();
3660b57cec5SDimitry Andric   const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
3670b57cec5SDimitry Andric   LocalVRMap.insert(std::make_pair(DefReg, RegSubRegPair(SrcReg, SrcSubReg)));
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   // Insert a copy from source to the end of the block. The def register is the
3700b57cec5SDimitry Andric   // available value liveout of the block.
3718bcb0991SDimitry Andric   Register NewDef = MRI->createVirtualRegister(RC);
3720b57cec5SDimitry Andric   Copies.push_back(std::make_pair(NewDef, RegSubRegPair(SrcReg, SrcSubReg)));
3730b57cec5SDimitry Andric   if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg))
3740b57cec5SDimitry Andric     addSSAUpdateEntry(DefReg, NewDef, PredBB);
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   if (!Remove)
3770b57cec5SDimitry Andric     return;
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric   // Remove PredBB from the PHI node.
38081ad6265SDimitry Andric   MI->removeOperand(SrcOpIdx + 1);
38181ad6265SDimitry Andric   MI->removeOperand(SrcOpIdx);
382bdd1243dSDimitry Andric   if (MI->getNumOperands() == 1 && !TailBB->hasAddressTaken())
3830b57cec5SDimitry Andric     MI->eraseFromParent();
384bdd1243dSDimitry Andric   else if (MI->getNumOperands() == 1)
385bdd1243dSDimitry Andric     MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric /// Duplicate a TailBB instruction to PredBB and update
3890b57cec5SDimitry Andric /// the source operands due to earlier PHI translation.
3900b57cec5SDimitry Andric void TailDuplicator::duplicateInstruction(
3910b57cec5SDimitry Andric     MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
3925ffd83dbSDimitry Andric     DenseMap<Register, RegSubRegPair> &LocalVRMap,
3935ffd83dbSDimitry Andric     const DenseSet<Register> &UsedByPhi) {
3940b57cec5SDimitry Andric   // Allow duplication of CFI instructions.
3950b57cec5SDimitry Andric   if (MI->isCFIInstruction()) {
3960b57cec5SDimitry Andric     BuildMI(*PredBB, PredBB->end(), PredBB->findDebugLoc(PredBB->begin()),
39781ad6265SDimitry Andric             TII->get(TargetOpcode::CFI_INSTRUCTION))
39881ad6265SDimitry Andric         .addCFIIndex(MI->getOperand(0).getCFIIndex())
39981ad6265SDimitry Andric         .setMIFlags(MI->getFlags());
4000b57cec5SDimitry Andric     return;
4010b57cec5SDimitry Andric   }
4020b57cec5SDimitry Andric   MachineInstr &NewMI = TII->duplicate(*PredBB, PredBB->end(), *MI);
4030b57cec5SDimitry Andric   if (PreRegAlloc) {
4040b57cec5SDimitry Andric     for (unsigned i = 0, e = NewMI.getNumOperands(); i != e; ++i) {
4050b57cec5SDimitry Andric       MachineOperand &MO = NewMI.getOperand(i);
4060b57cec5SDimitry Andric       if (!MO.isReg())
4070b57cec5SDimitry Andric         continue;
4088bcb0991SDimitry Andric       Register Reg = MO.getReg();
409bdd1243dSDimitry Andric       if (!Reg.isVirtual())
4100b57cec5SDimitry Andric         continue;
4110b57cec5SDimitry Andric       if (MO.isDef()) {
4120b57cec5SDimitry Andric         const TargetRegisterClass *RC = MRI->getRegClass(Reg);
4138bcb0991SDimitry Andric         Register NewReg = MRI->createVirtualRegister(RC);
4140b57cec5SDimitry Andric         MO.setReg(NewReg);
4150b57cec5SDimitry Andric         LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0)));
4160b57cec5SDimitry Andric         if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg))
4170b57cec5SDimitry Andric           addSSAUpdateEntry(Reg, NewReg, PredBB);
4180b57cec5SDimitry Andric       } else {
4190b57cec5SDimitry Andric         auto VI = LocalVRMap.find(Reg);
4200b57cec5SDimitry Andric         if (VI != LocalVRMap.end()) {
4210b57cec5SDimitry Andric           // Need to make sure that the register class of the mapped register
4220b57cec5SDimitry Andric           // will satisfy the constraints of the class of the register being
4230b57cec5SDimitry Andric           // replaced.
4240b57cec5SDimitry Andric           auto *OrigRC = MRI->getRegClass(Reg);
4250b57cec5SDimitry Andric           auto *MappedRC = MRI->getRegClass(VI->second.Reg);
4260b57cec5SDimitry Andric           const TargetRegisterClass *ConstrRC;
4270b57cec5SDimitry Andric           if (VI->second.SubReg != 0) {
4280b57cec5SDimitry Andric             ConstrRC = TRI->getMatchingSuperRegClass(MappedRC, OrigRC,
4290b57cec5SDimitry Andric                                                      VI->second.SubReg);
4300b57cec5SDimitry Andric             if (ConstrRC) {
4310b57cec5SDimitry Andric               // The actual constraining (as in "find appropriate new class")
4320b57cec5SDimitry Andric               // is done by getMatchingSuperRegClass, so now we only need to
4330b57cec5SDimitry Andric               // change the class of the mapped register.
4340b57cec5SDimitry Andric               MRI->setRegClass(VI->second.Reg, ConstrRC);
4350b57cec5SDimitry Andric             }
4360b57cec5SDimitry Andric           } else {
4370b57cec5SDimitry Andric             // For mapped registers that do not have sub-registers, simply
4380b57cec5SDimitry Andric             // restrict their class to match the original one.
43906c3fb27SDimitry Andric 
44006c3fb27SDimitry Andric             // We don't want debug instructions affecting the resulting code so
44106c3fb27SDimitry Andric             // if we're cloning a debug instruction then just use MappedRC
44206c3fb27SDimitry Andric             // rather than constraining the register class further.
44306c3fb27SDimitry Andric             ConstrRC = NewMI.isDebugInstr()
44406c3fb27SDimitry Andric                            ? MappedRC
44506c3fb27SDimitry Andric                            : MRI->constrainRegClass(VI->second.Reg, OrigRC);
4460b57cec5SDimitry Andric           }
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric           if (ConstrRC) {
4490b57cec5SDimitry Andric             // If the class constraining succeeded, we can simply replace
4500b57cec5SDimitry Andric             // the old register with the mapped one.
4510b57cec5SDimitry Andric             MO.setReg(VI->second.Reg);
4520b57cec5SDimitry Andric             // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a
4530b57cec5SDimitry Andric             // sub-register, we need to compose the sub-register indices.
45406c3fb27SDimitry Andric             MO.setSubReg(
45506c3fb27SDimitry Andric                 TRI->composeSubRegIndices(VI->second.SubReg, MO.getSubReg()));
4560b57cec5SDimitry Andric           } else {
4570b57cec5SDimitry Andric             // The direct replacement is not possible, due to failing register
4580b57cec5SDimitry Andric             // class constraints. An explicit COPY is necessary. Create one
45906c3fb27SDimitry Andric             // that can be reused.
46006c3fb27SDimitry Andric             Register NewReg = MRI->createVirtualRegister(OrigRC);
4610b57cec5SDimitry Andric             BuildMI(*PredBB, NewMI, NewMI.getDebugLoc(),
4620b57cec5SDimitry Andric                     TII->get(TargetOpcode::COPY), NewReg)
4630b57cec5SDimitry Andric                 .addReg(VI->second.Reg, 0, VI->second.SubReg);
4640b57cec5SDimitry Andric             LocalVRMap.erase(VI);
4650b57cec5SDimitry Andric             LocalVRMap.insert(std::make_pair(Reg, RegSubRegPair(NewReg, 0)));
4660b57cec5SDimitry Andric             MO.setReg(NewReg);
4670b57cec5SDimitry Andric             // The composed VI.Reg:VI.SubReg is replaced with NewReg, which
4680b57cec5SDimitry Andric             // is equivalent to the whole register Reg. Hence, Reg:subreg
4690b57cec5SDimitry Andric             // is same as NewReg:subreg, so keep the sub-register index
4700b57cec5SDimitry Andric             // unchanged.
4710b57cec5SDimitry Andric           }
4720b57cec5SDimitry Andric           // Clear any kill flags from this operand.  The new register could
4730b57cec5SDimitry Andric           // have uses after this one, so kills are not valid here.
4740b57cec5SDimitry Andric           MO.setIsKill(false);
4750b57cec5SDimitry Andric         }
4760b57cec5SDimitry Andric       }
4770b57cec5SDimitry Andric     }
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric /// After FromBB is tail duplicated into its predecessor blocks, the successors
4820b57cec5SDimitry Andric /// have gained new predecessors. Update the PHI instructions in them
4830b57cec5SDimitry Andric /// accordingly.
4840b57cec5SDimitry Andric void TailDuplicator::updateSuccessorsPHIs(
4850b57cec5SDimitry Andric     MachineBasicBlock *FromBB, bool isDead,
4860b57cec5SDimitry Andric     SmallVectorImpl<MachineBasicBlock *> &TDBBs,
4870b57cec5SDimitry Andric     SmallSetVector<MachineBasicBlock *, 8> &Succs) {
4880b57cec5SDimitry Andric   for (MachineBasicBlock *SuccBB : Succs) {
4890b57cec5SDimitry Andric     for (MachineInstr &MI : *SuccBB) {
4900b57cec5SDimitry Andric       if (!MI.isPHI())
4910b57cec5SDimitry Andric         break;
4920b57cec5SDimitry Andric       MachineInstrBuilder MIB(*FromBB->getParent(), MI);
4930b57cec5SDimitry Andric       unsigned Idx = 0;
4940b57cec5SDimitry Andric       for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
4950b57cec5SDimitry Andric         MachineOperand &MO = MI.getOperand(i + 1);
4960b57cec5SDimitry Andric         if (MO.getMBB() == FromBB) {
4970b57cec5SDimitry Andric           Idx = i;
4980b57cec5SDimitry Andric           break;
4990b57cec5SDimitry Andric         }
5000b57cec5SDimitry Andric       }
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric       assert(Idx != 0);
5030b57cec5SDimitry Andric       MachineOperand &MO0 = MI.getOperand(Idx);
5048bcb0991SDimitry Andric       Register Reg = MO0.getReg();
5050b57cec5SDimitry Andric       if (isDead) {
5060b57cec5SDimitry Andric         // Folded into the previous BB.
5070b57cec5SDimitry Andric         // There could be duplicate phi source entries. FIXME: Should sdisel
5080b57cec5SDimitry Andric         // or earlier pass fixed this?
5090b57cec5SDimitry Andric         for (unsigned i = MI.getNumOperands() - 2; i != Idx; i -= 2) {
5100b57cec5SDimitry Andric           MachineOperand &MO = MI.getOperand(i + 1);
5110b57cec5SDimitry Andric           if (MO.getMBB() == FromBB) {
51281ad6265SDimitry Andric             MI.removeOperand(i + 1);
51381ad6265SDimitry Andric             MI.removeOperand(i);
5140b57cec5SDimitry Andric           }
5150b57cec5SDimitry Andric         }
5160b57cec5SDimitry Andric       } else
5170b57cec5SDimitry Andric         Idx = 0;
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric       // If Idx is set, the operands at Idx and Idx+1 must be removed.
52081ad6265SDimitry Andric       // We reuse the location to avoid expensive removeOperand calls.
5210b57cec5SDimitry Andric 
5225ffd83dbSDimitry Andric       DenseMap<Register, AvailableValsTy>::iterator LI =
5230b57cec5SDimitry Andric           SSAUpdateVals.find(Reg);
5240b57cec5SDimitry Andric       if (LI != SSAUpdateVals.end()) {
5250b57cec5SDimitry Andric         // This register is defined in the tail block.
5260eae32dcSDimitry Andric         for (const std::pair<MachineBasicBlock *, Register> &J : LI->second) {
5270eae32dcSDimitry Andric           MachineBasicBlock *SrcBB = J.first;
5280b57cec5SDimitry Andric           // If we didn't duplicate a bb into a particular predecessor, we
5290b57cec5SDimitry Andric           // might still have added an entry to SSAUpdateVals to correcly
5300b57cec5SDimitry Andric           // recompute SSA. If that case, avoid adding a dummy extra argument
5310b57cec5SDimitry Andric           // this PHI.
5320b57cec5SDimitry Andric           if (!SrcBB->isSuccessor(SuccBB))
5330b57cec5SDimitry Andric             continue;
5340b57cec5SDimitry Andric 
5350eae32dcSDimitry Andric           Register SrcReg = J.second;
5360b57cec5SDimitry Andric           if (Idx != 0) {
5370b57cec5SDimitry Andric             MI.getOperand(Idx).setReg(SrcReg);
5380b57cec5SDimitry Andric             MI.getOperand(Idx + 1).setMBB(SrcBB);
5390b57cec5SDimitry Andric             Idx = 0;
5400b57cec5SDimitry Andric           } else {
5410b57cec5SDimitry Andric             MIB.addReg(SrcReg).addMBB(SrcBB);
5420b57cec5SDimitry Andric           }
5430b57cec5SDimitry Andric         }
5440b57cec5SDimitry Andric       } else {
5450b57cec5SDimitry Andric         // Live in tail block, must also be live in predecessors.
5460eae32dcSDimitry Andric         for (MachineBasicBlock *SrcBB : TDBBs) {
5470b57cec5SDimitry Andric           if (Idx != 0) {
5480b57cec5SDimitry Andric             MI.getOperand(Idx).setReg(Reg);
5490b57cec5SDimitry Andric             MI.getOperand(Idx + 1).setMBB(SrcBB);
5500b57cec5SDimitry Andric             Idx = 0;
5510b57cec5SDimitry Andric           } else {
5520b57cec5SDimitry Andric             MIB.addReg(Reg).addMBB(SrcBB);
5530b57cec5SDimitry Andric           }
5540b57cec5SDimitry Andric         }
5550b57cec5SDimitry Andric       }
5560b57cec5SDimitry Andric       if (Idx != 0) {
55781ad6265SDimitry Andric         MI.removeOperand(Idx + 1);
55881ad6265SDimitry Andric         MI.removeOperand(Idx);
5590b57cec5SDimitry Andric       }
5600b57cec5SDimitry Andric     }
5610b57cec5SDimitry Andric   }
5620b57cec5SDimitry Andric }
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric /// Determine if it is profitable to duplicate this block.
5650b57cec5SDimitry Andric bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
5660b57cec5SDimitry Andric                                          MachineBasicBlock &TailBB) {
5670b57cec5SDimitry Andric   // When doing tail-duplication during layout, the block ordering is in flux,
5680b57cec5SDimitry Andric   // so canFallThrough returns a result based on incorrect information and
5690b57cec5SDimitry Andric   // should just be ignored.
5700b57cec5SDimitry Andric   if (!LayoutMode && TailBB.canFallThrough())
5710b57cec5SDimitry Andric     return false;
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   // Don't try to tail-duplicate single-block loops.
5740b57cec5SDimitry Andric   if (TailBB.isSuccessor(&TailBB))
5750b57cec5SDimitry Andric     return false;
5760b57cec5SDimitry Andric 
577*0fca6ea1SDimitry Andric   // Duplicating a BB which has both multiple predecessors and successors will
578*0fca6ea1SDimitry Andric   // result in a complex CFG and also may cause huge amount of PHI nodes. If we
579*0fca6ea1SDimitry Andric   // want to remove this limitation, we have to address
580*0fca6ea1SDimitry Andric   // https://github.com/llvm/llvm-project/issues/78578.
581*0fca6ea1SDimitry Andric   if (TailBB.pred_size() > TailDupPredSize &&
582*0fca6ea1SDimitry Andric       TailBB.succ_size() > TailDupSuccSize)
583*0fca6ea1SDimitry Andric     return false;
584*0fca6ea1SDimitry Andric 
5850b57cec5SDimitry Andric   // Set the limit on the cost to duplicate. When optimizing for size,
5860b57cec5SDimitry Andric   // duplicate only one, because one branch instruction can be eliminated to
5870b57cec5SDimitry Andric   // compensate for the duplication.
5880b57cec5SDimitry Andric   unsigned MaxDuplicateCount;
589480093f4SDimitry Andric   bool OptForSize = MF->getFunction().hasOptSize() ||
590480093f4SDimitry Andric                     llvm::shouldOptimizeForSize(&TailBB, PSI, MBFI);
591480093f4SDimitry Andric   if (TailDupSize == 0)
5920b57cec5SDimitry Andric     MaxDuplicateCount = TailDuplicateSize;
5930b57cec5SDimitry Andric   else
5940b57cec5SDimitry Andric     MaxDuplicateCount = TailDupSize;
595480093f4SDimitry Andric   if (OptForSize)
596480093f4SDimitry Andric     MaxDuplicateCount = 1;
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric   // If the block to be duplicated ends in an unanalyzable fallthrough, don't
5990b57cec5SDimitry Andric   // duplicate it.
6000b57cec5SDimitry Andric   // A similar check is necessary in MachineBlockPlacement to make sure pairs of
6010b57cec5SDimitry Andric   // blocks with unanalyzable fallthrough get layed out contiguously.
6020b57cec5SDimitry Andric   MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
6030b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> PredCond;
6040b57cec5SDimitry Andric   if (TII->analyzeBranch(TailBB, PredTBB, PredFBB, PredCond) &&
6050b57cec5SDimitry Andric       TailBB.canFallThrough())
6060b57cec5SDimitry Andric     return false;
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   // If the target has hardware branch prediction that can handle indirect
6090b57cec5SDimitry Andric   // branches, duplicating them can often make them predictable when there
6100b57cec5SDimitry Andric   // are common paths through the code.  The limit needs to be high enough
6110b57cec5SDimitry Andric   // to allow undoing the effects of tail merging and other optimizations
6120b57cec5SDimitry Andric   // that rearrange the predecessors of the indirect branch.
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric   bool HasIndirectbr = false;
6150b57cec5SDimitry Andric   if (!TailBB.empty())
6160b57cec5SDimitry Andric     HasIndirectbr = TailBB.back().isIndirectBranch();
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   if (HasIndirectbr && PreRegAlloc)
6190b57cec5SDimitry Andric     MaxDuplicateCount = TailDupIndirectBranchSize;
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   // Check the instructions in the block to determine whether tail-duplication
6220b57cec5SDimitry Andric   // is invalid or unlikely to be profitable.
6230b57cec5SDimitry Andric   unsigned InstrCount = 0;
6240b57cec5SDimitry Andric   for (MachineInstr &MI : TailBB) {
6250b57cec5SDimitry Andric     // Non-duplicable things shouldn't be tail-duplicated.
6260b57cec5SDimitry Andric     // CFI instructions are marked as non-duplicable, because Darwin compact
6270b57cec5SDimitry Andric     // unwind info emission can't handle multiple prologue setups. In case of
6280b57cec5SDimitry Andric     // DWARF, allow them be duplicated, so that their existence doesn't prevent
6290b57cec5SDimitry Andric     // tail duplication of some basic blocks, that would be duplicated otherwise.
6300b57cec5SDimitry Andric     if (MI.isNotDuplicable() &&
6310b57cec5SDimitry Andric         (TailBB.getParent()->getTarget().getTargetTriple().isOSDarwin() ||
6320b57cec5SDimitry Andric         !MI.isCFIInstruction()))
6330b57cec5SDimitry Andric       return false;
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric     // Convergent instructions can be duplicated only if doing so doesn't add
6360b57cec5SDimitry Andric     // new control dependencies, which is what we're going to do here.
6370b57cec5SDimitry Andric     if (MI.isConvergent())
6380b57cec5SDimitry Andric       return false;
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric     // Do not duplicate 'return' instructions if this is a pre-regalloc run.
6410b57cec5SDimitry Andric     // A return may expand into a lot more instructions (e.g. reload of callee
6420b57cec5SDimitry Andric     // saved registers) after PEI.
6430b57cec5SDimitry Andric     if (PreRegAlloc && MI.isReturn())
6440b57cec5SDimitry Andric       return false;
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric     // Avoid duplicating calls before register allocation. Calls presents a
6470b57cec5SDimitry Andric     // barrier to register allocation so duplicating them may end up increasing
6480b57cec5SDimitry Andric     // spills.
6490b57cec5SDimitry Andric     if (PreRegAlloc && MI.isCall())
6500b57cec5SDimitry Andric       return false;
6510b57cec5SDimitry Andric 
652f91b0c1cSDimitry Andric     // TailDuplicator::appendCopies will erroneously place COPYs after
653f91b0c1cSDimitry Andric     // INLINEASM_BR instructions after 4b0aa5724fea, which demonstrates the same
654f91b0c1cSDimitry Andric     // bug that was fixed in f7a53d82c090.
655f91b0c1cSDimitry Andric     // FIXME: Use findPHICopyInsertPoint() to find the correct insertion point
656f91b0c1cSDimitry Andric     //        for the COPY when replacing PHIs.
657f91b0c1cSDimitry Andric     if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
658f91b0c1cSDimitry Andric       return false;
659f91b0c1cSDimitry Andric 
6605ffd83dbSDimitry Andric     if (MI.isBundle())
6615ffd83dbSDimitry Andric       InstrCount += MI.getBundleSize();
6625ffd83dbSDimitry Andric     else if (!MI.isPHI() && !MI.isMetaInstruction())
6630b57cec5SDimitry Andric       InstrCount += 1;
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric     if (InstrCount > MaxDuplicateCount)
6660b57cec5SDimitry Andric       return false;
6670b57cec5SDimitry Andric   }
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   // Check if any of the successors of TailBB has a PHI node in which the
6700b57cec5SDimitry Andric   // value corresponding to TailBB uses a subregister.
6710b57cec5SDimitry Andric   // If a phi node uses a register paired with a subregister, the actual
6720b57cec5SDimitry Andric   // "value type" of the phi may differ from the type of the register without
6730b57cec5SDimitry Andric   // any subregisters. Due to a bug, tail duplication may add a new operand
6740b57cec5SDimitry Andric   // without a necessary subregister, producing an invalid code. This is
6750b57cec5SDimitry Andric   // demonstrated by test/CodeGen/Hexagon/tail-dup-subreg-abort.ll.
6760b57cec5SDimitry Andric   // Disable tail duplication for this case for now, until the problem is
6770b57cec5SDimitry Andric   // fixed.
678fcaf7f86SDimitry Andric   for (auto *SB : TailBB.successors()) {
6790b57cec5SDimitry Andric     for (auto &I : *SB) {
6800b57cec5SDimitry Andric       if (!I.isPHI())
6810b57cec5SDimitry Andric         break;
6820b57cec5SDimitry Andric       unsigned Idx = getPHISrcRegOpIdx(&I, &TailBB);
6830b57cec5SDimitry Andric       assert(Idx != 0);
6840b57cec5SDimitry Andric       MachineOperand &PU = I.getOperand(Idx);
6850b57cec5SDimitry Andric       if (PU.getSubReg() != 0)
6860b57cec5SDimitry Andric         return false;
6870b57cec5SDimitry Andric     }
6880b57cec5SDimitry Andric   }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   if (HasIndirectbr && PreRegAlloc)
6910b57cec5SDimitry Andric     return true;
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   if (IsSimple)
6940b57cec5SDimitry Andric     return true;
6950b57cec5SDimitry Andric 
6960b57cec5SDimitry Andric   if (!PreRegAlloc)
6970b57cec5SDimitry Andric     return true;
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric   return canCompletelyDuplicateBB(TailBB);
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric /// True if this BB has only one unconditional jump.
7030b57cec5SDimitry Andric bool TailDuplicator::isSimpleBB(MachineBasicBlock *TailBB) {
7040b57cec5SDimitry Andric   if (TailBB->succ_size() != 1)
7050b57cec5SDimitry Andric     return false;
7060b57cec5SDimitry Andric   if (TailBB->pred_empty())
7070b57cec5SDimitry Andric     return false;
708fe6060f1SDimitry Andric   MachineBasicBlock::iterator I = TailBB->getFirstNonDebugInstr(true);
7090b57cec5SDimitry Andric   if (I == TailBB->end())
7100b57cec5SDimitry Andric     return true;
7110b57cec5SDimitry Andric   return I->isUnconditionalBranch();
7120b57cec5SDimitry Andric }
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric static bool bothUsedInPHI(const MachineBasicBlock &A,
7150b57cec5SDimitry Andric                           const SmallPtrSet<MachineBasicBlock *, 8> &SuccsB) {
7160b57cec5SDimitry Andric   for (MachineBasicBlock *BB : A.successors())
7170b57cec5SDimitry Andric     if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI())
7180b57cec5SDimitry Andric       return true;
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   return false;
7210b57cec5SDimitry Andric }
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric bool TailDuplicator::canCompletelyDuplicateBB(MachineBasicBlock &BB) {
7240b57cec5SDimitry Andric   for (MachineBasicBlock *PredBB : BB.predecessors()) {
7250b57cec5SDimitry Andric     if (PredBB->succ_size() > 1)
7260b57cec5SDimitry Andric       return false;
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric     MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
7290b57cec5SDimitry Andric     SmallVector<MachineOperand, 4> PredCond;
7300b57cec5SDimitry Andric     if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
7310b57cec5SDimitry Andric       return false;
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric     if (!PredCond.empty())
7340b57cec5SDimitry Andric       return false;
7350b57cec5SDimitry Andric   }
7360b57cec5SDimitry Andric   return true;
7370b57cec5SDimitry Andric }
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric bool TailDuplicator::duplicateSimpleBB(
7400b57cec5SDimitry Andric     MachineBasicBlock *TailBB, SmallVectorImpl<MachineBasicBlock *> &TDBBs,
741bdd1243dSDimitry Andric     const DenseSet<Register> &UsedByPhi) {
7420b57cec5SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 8> Succs(TailBB->succ_begin(),
7430b57cec5SDimitry Andric                                             TailBB->succ_end());
744e8d8bef9SDimitry Andric   SmallVector<MachineBasicBlock *, 8> Preds(TailBB->predecessors());
7450b57cec5SDimitry Andric   bool Changed = false;
7460b57cec5SDimitry Andric   for (MachineBasicBlock *PredBB : Preds) {
7475ffd83dbSDimitry Andric     if (PredBB->hasEHPadSuccessor() || PredBB->mayHaveInlineAsmBr())
7480b57cec5SDimitry Andric       continue;
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric     if (bothUsedInPHI(*PredBB, Succs))
7510b57cec5SDimitry Andric       continue;
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric     MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
7540b57cec5SDimitry Andric     SmallVector<MachineOperand, 4> PredCond;
7550b57cec5SDimitry Andric     if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
7560b57cec5SDimitry Andric       continue;
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric     Changed = true;
7590b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
7600b57cec5SDimitry Andric                       << "From simple Succ: " << *TailBB);
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric     MachineBasicBlock *NewTarget = *TailBB->succ_begin();
7630b57cec5SDimitry Andric     MachineBasicBlock *NextBB = PredBB->getNextNode();
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric     // Make PredFBB explicit.
7660b57cec5SDimitry Andric     if (PredCond.empty())
7670b57cec5SDimitry Andric       PredFBB = PredTBB;
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric     // Make fall through explicit.
7700b57cec5SDimitry Andric     if (!PredTBB)
7710b57cec5SDimitry Andric       PredTBB = NextBB;
7720b57cec5SDimitry Andric     if (!PredFBB)
7730b57cec5SDimitry Andric       PredFBB = NextBB;
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric     // Redirect
7760b57cec5SDimitry Andric     if (PredFBB == TailBB)
7770b57cec5SDimitry Andric       PredFBB = NewTarget;
7780b57cec5SDimitry Andric     if (PredTBB == TailBB)
7790b57cec5SDimitry Andric       PredTBB = NewTarget;
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric     // Make the branch unconditional if possible
7820b57cec5SDimitry Andric     if (PredTBB == PredFBB) {
7830b57cec5SDimitry Andric       PredCond.clear();
7840b57cec5SDimitry Andric       PredFBB = nullptr;
7850b57cec5SDimitry Andric     }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric     // Avoid adding fall through branches.
7880b57cec5SDimitry Andric     if (PredFBB == NextBB)
7890b57cec5SDimitry Andric       PredFBB = nullptr;
7900b57cec5SDimitry Andric     if (PredTBB == NextBB && PredFBB == nullptr)
7910b57cec5SDimitry Andric       PredTBB = nullptr;
7920b57cec5SDimitry Andric 
7930b57cec5SDimitry Andric     auto DL = PredBB->findBranchDebugLoc();
7940b57cec5SDimitry Andric     TII->removeBranch(*PredBB);
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric     if (!PredBB->isSuccessor(NewTarget))
7970b57cec5SDimitry Andric       PredBB->replaceSuccessor(TailBB, NewTarget);
7980b57cec5SDimitry Andric     else {
7990b57cec5SDimitry Andric       PredBB->removeSuccessor(TailBB, true);
8000b57cec5SDimitry Andric       assert(PredBB->succ_size() <= 1);
8010b57cec5SDimitry Andric     }
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric     if (PredTBB)
8040b57cec5SDimitry Andric       TII->insertBranch(*PredBB, PredTBB, PredFBB, PredCond, DL);
8050b57cec5SDimitry Andric 
8060b57cec5SDimitry Andric     TDBBs.push_back(PredBB);
8070b57cec5SDimitry Andric   }
8080b57cec5SDimitry Andric   return Changed;
8090b57cec5SDimitry Andric }
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric bool TailDuplicator::canTailDuplicate(MachineBasicBlock *TailBB,
8120b57cec5SDimitry Andric                                       MachineBasicBlock *PredBB) {
8130b57cec5SDimitry Andric   // EH edges are ignored by analyzeBranch.
8140b57cec5SDimitry Andric   if (PredBB->succ_size() > 1)
8150b57cec5SDimitry Andric     return false;
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric   MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
8180b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> PredCond;
8190b57cec5SDimitry Andric   if (TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond))
8200b57cec5SDimitry Andric     return false;
8210b57cec5SDimitry Andric   if (!PredCond.empty())
8220b57cec5SDimitry Andric     return false;
823bdd1243dSDimitry Andric   // FIXME: This is overly conservative; it may be ok to relax this in the
824bdd1243dSDimitry Andric   // future under more specific conditions. If TailBB is an INLINEASM_BR
825bdd1243dSDimitry Andric   // indirect target, we need to see if the edge from PredBB to TailBB is from
826bdd1243dSDimitry Andric   // an INLINEASM_BR in PredBB, and then also if that edge was from the
827bdd1243dSDimitry Andric   // indirect target list, fallthrough/default target, or potentially both. If
828bdd1243dSDimitry Andric   // it's both, TailDuplicator::tailDuplicate will remove the edge, corrupting
829bdd1243dSDimitry Andric   // the successor list in PredBB and predecessor list in TailBB.
830bdd1243dSDimitry Andric   if (TailBB->isInlineAsmBrIndirectTarget())
831bdd1243dSDimitry Andric     return false;
8320b57cec5SDimitry Andric   return true;
8330b57cec5SDimitry Andric }
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric /// If it is profitable, duplicate TailBB's contents in each
8360b57cec5SDimitry Andric /// of its predecessors.
8370b57cec5SDimitry Andric /// \p IsSimple result of isSimpleBB
8380b57cec5SDimitry Andric /// \p TailBB   Block to be duplicated.
8390b57cec5SDimitry Andric /// \p ForcedLayoutPred  When non-null, use this block as the layout predecessor
8400b57cec5SDimitry Andric ///                      instead of the previous block in MF's order.
8410b57cec5SDimitry Andric /// \p TDBBs             A vector to keep track of all blocks tail-duplicated
8420b57cec5SDimitry Andric ///                      into.
8430b57cec5SDimitry Andric /// \p Copies            A vector of copy instructions inserted. Used later to
8440b57cec5SDimitry Andric ///                      walk all the inserted copies and remove redundant ones.
8450b57cec5SDimitry Andric bool TailDuplicator::tailDuplicate(bool IsSimple, MachineBasicBlock *TailBB,
8460b57cec5SDimitry Andric                           MachineBasicBlock *ForcedLayoutPred,
8470b57cec5SDimitry Andric                           SmallVectorImpl<MachineBasicBlock *> &TDBBs,
8485ffd83dbSDimitry Andric                           SmallVectorImpl<MachineInstr *> &Copies,
8495ffd83dbSDimitry Andric                           SmallVectorImpl<MachineBasicBlock *> *CandidatePtr) {
8500b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\n*** Tail-duplicating " << printMBBReference(*TailBB)
8510b57cec5SDimitry Andric                     << '\n');
8520b57cec5SDimitry Andric 
8535ffd83dbSDimitry Andric   bool ShouldUpdateTerminators = TailBB->canFallThrough();
8545ffd83dbSDimitry Andric 
8555ffd83dbSDimitry Andric   DenseSet<Register> UsedByPhi;
8560b57cec5SDimitry Andric   getRegsUsedByPHIs(*TailBB, &UsedByPhi);
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric   if (IsSimple)
859bdd1243dSDimitry Andric     return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi);
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric   // Iterate through all the unique predecessors and tail-duplicate this
8620b57cec5SDimitry Andric   // block into them, if possible. Copying the list ahead of time also
8630b57cec5SDimitry Andric   // avoids trouble with the predecessor list reallocating.
8640b57cec5SDimitry Andric   bool Changed = false;
8655ffd83dbSDimitry Andric   SmallSetVector<MachineBasicBlock *, 8> Preds;
8665ffd83dbSDimitry Andric   if (CandidatePtr)
8675ffd83dbSDimitry Andric     Preds.insert(CandidatePtr->begin(), CandidatePtr->end());
8685ffd83dbSDimitry Andric   else
8695ffd83dbSDimitry Andric     Preds.insert(TailBB->pred_begin(), TailBB->pred_end());
8705ffd83dbSDimitry Andric 
8710b57cec5SDimitry Andric   for (MachineBasicBlock *PredBB : Preds) {
8720b57cec5SDimitry Andric     assert(TailBB != PredBB &&
8730b57cec5SDimitry Andric            "Single-block loop should have been rejected earlier!");
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric     if (!canTailDuplicate(TailBB, PredBB))
8760b57cec5SDimitry Andric       continue;
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric     // Don't duplicate into a fall-through predecessor (at least for now).
8795ffd83dbSDimitry Andric     // If profile is available, findDuplicateCandidates can choose better
8805ffd83dbSDimitry Andric     // fall-through predecessor.
8815ffd83dbSDimitry Andric     if (!(MF->getFunction().hasProfileData() && LayoutMode)) {
8820b57cec5SDimitry Andric       bool IsLayoutSuccessor = false;
8830b57cec5SDimitry Andric       if (ForcedLayoutPred)
8840b57cec5SDimitry Andric         IsLayoutSuccessor = (ForcedLayoutPred == PredBB);
8850b57cec5SDimitry Andric       else if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
8860b57cec5SDimitry Andric         IsLayoutSuccessor = true;
8870b57cec5SDimitry Andric       if (IsLayoutSuccessor)
8880b57cec5SDimitry Andric         continue;
8895ffd83dbSDimitry Andric     }
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
8920b57cec5SDimitry Andric                       << "From Succ: " << *TailBB);
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric     TDBBs.push_back(PredBB);
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric     // Remove PredBB's unconditional branch.
8970b57cec5SDimitry Andric     TII->removeBranch(*PredBB);
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric     // Clone the contents of TailBB into PredBB.
9005ffd83dbSDimitry Andric     DenseMap<Register, RegSubRegPair> LocalVRMap;
9015ffd83dbSDimitry Andric     SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
902349cc55cSDimitry Andric     for (MachineInstr &MI : llvm::make_early_inc_range(*TailBB)) {
903349cc55cSDimitry Andric       if (MI.isPHI()) {
9040b57cec5SDimitry Andric         // Replace the uses of the def of the PHI with the register coming
9050b57cec5SDimitry Andric         // from PredBB.
906349cc55cSDimitry Andric         processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true);
9070b57cec5SDimitry Andric       } else {
9080b57cec5SDimitry Andric         // Replace def of virtual registers with new registers, and update
9090b57cec5SDimitry Andric         // uses with PHI source register or the new registers.
910349cc55cSDimitry Andric         duplicateInstruction(&MI, TailBB, PredBB, LocalVRMap, UsedByPhi);
9110b57cec5SDimitry Andric       }
9120b57cec5SDimitry Andric     }
9130b57cec5SDimitry Andric     appendCopies(PredBB, CopyInfos, Copies);
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric     NumTailDupAdded += TailBB->size() - 1; // subtract one for removed branch
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric     // Update the CFG.
9180b57cec5SDimitry Andric     PredBB->removeSuccessor(PredBB->succ_begin());
9190b57cec5SDimitry Andric     assert(PredBB->succ_empty() &&
9200b57cec5SDimitry Andric            "TailDuplicate called on block with multiple successors!");
9210b57cec5SDimitry Andric     for (MachineBasicBlock *Succ : TailBB->successors())
9220b57cec5SDimitry Andric       PredBB->addSuccessor(Succ, MBPI->getEdgeProbability(TailBB, Succ));
9230b57cec5SDimitry Andric 
9245ffd83dbSDimitry Andric     // Update branches in pred to jump to tail's layout successor if needed.
9255ffd83dbSDimitry Andric     if (ShouldUpdateTerminators)
9265ffd83dbSDimitry Andric       PredBB->updateTerminator(TailBB->getNextNode());
9275ffd83dbSDimitry Andric 
9280b57cec5SDimitry Andric     Changed = true;
9290b57cec5SDimitry Andric     ++NumTailDups;
9300b57cec5SDimitry Andric   }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   // If TailBB was duplicated into all its predecessors except for the prior
9330b57cec5SDimitry Andric   // block, which falls through unconditionally, move the contents of this
9340b57cec5SDimitry Andric   // block into the prior block.
9350b57cec5SDimitry Andric   MachineBasicBlock *PrevBB = ForcedLayoutPred;
9360b57cec5SDimitry Andric   if (!PrevBB)
9370b57cec5SDimitry Andric     PrevBB = &*std::prev(TailBB->getIterator());
9380b57cec5SDimitry Andric   MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
9390b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> PriorCond;
9400b57cec5SDimitry Andric   // This has to check PrevBB->succ_size() because EH edges are ignored by
9410b57cec5SDimitry Andric   // analyzeBranch.
9420b57cec5SDimitry Andric   if (PrevBB->succ_size() == 1 &&
9430b57cec5SDimitry Andric       // Layout preds are not always CFG preds. Check.
9440b57cec5SDimitry Andric       *PrevBB->succ_begin() == TailBB &&
9450b57cec5SDimitry Andric       !TII->analyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond) &&
9460b57cec5SDimitry Andric       PriorCond.empty() &&
9470b57cec5SDimitry Andric       (!PriorTBB || PriorTBB == TailBB) &&
9480b57cec5SDimitry Andric       TailBB->pred_size() == 1 &&
9490b57cec5SDimitry Andric       !TailBB->hasAddressTaken()) {
9500b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
9510b57cec5SDimitry Andric                       << "From MBB: " << *TailBB);
9520b57cec5SDimitry Andric     // There may be a branch to the layout successor. This is unlikely but it
9530b57cec5SDimitry Andric     // happens. The correct thing to do is to remove the branch before
9540b57cec5SDimitry Andric     // duplicating the instructions in all cases.
955349cc55cSDimitry Andric     bool RemovedBranches = TII->removeBranch(*PrevBB) != 0;
956349cc55cSDimitry Andric 
957349cc55cSDimitry Andric     // If there are still tail instructions, abort the merge
958349cc55cSDimitry Andric     if (PrevBB->getFirstTerminator() == PrevBB->end()) {
9590b57cec5SDimitry Andric       if (PreRegAlloc) {
9605ffd83dbSDimitry Andric         DenseMap<Register, RegSubRegPair> LocalVRMap;
9615ffd83dbSDimitry Andric         SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
9620b57cec5SDimitry Andric         MachineBasicBlock::iterator I = TailBB->begin();
9630b57cec5SDimitry Andric         // Process PHI instructions first.
9640b57cec5SDimitry Andric         while (I != TailBB->end() && I->isPHI()) {
9650b57cec5SDimitry Andric           // Replace the uses of the def of the PHI with the register coming
9660b57cec5SDimitry Andric           // from PredBB.
9670b57cec5SDimitry Andric           MachineInstr *MI = &*I++;
968349cc55cSDimitry Andric           processPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi,
969349cc55cSDimitry Andric                      true);
9700b57cec5SDimitry Andric         }
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric         // Now copy the non-PHI instructions.
9730b57cec5SDimitry Andric         while (I != TailBB->end()) {
9740b57cec5SDimitry Andric           // Replace def of virtual registers with new registers, and update
9750b57cec5SDimitry Andric           // uses with PHI source register or the new registers.
9760b57cec5SDimitry Andric           MachineInstr *MI = &*I++;
9770b57cec5SDimitry Andric           assert(!MI->isBundle() && "Not expecting bundles before regalloc!");
9780b57cec5SDimitry Andric           duplicateInstruction(MI, TailBB, PrevBB, LocalVRMap, UsedByPhi);
9790b57cec5SDimitry Andric           MI->eraseFromParent();
9800b57cec5SDimitry Andric         }
9810b57cec5SDimitry Andric         appendCopies(PrevBB, CopyInfos, Copies);
9820b57cec5SDimitry Andric       } else {
9830b57cec5SDimitry Andric         TII->removeBranch(*PrevBB);
9840b57cec5SDimitry Andric         // No PHIs to worry about, just splice the instructions over.
9850b57cec5SDimitry Andric         PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
9860b57cec5SDimitry Andric       }
9870b57cec5SDimitry Andric       PrevBB->removeSuccessor(PrevBB->succ_begin());
9880b57cec5SDimitry Andric       assert(PrevBB->succ_empty());
9890b57cec5SDimitry Andric       PrevBB->transferSuccessors(TailBB);
9905ffd83dbSDimitry Andric 
9915ffd83dbSDimitry Andric       // Update branches in PrevBB based on Tail's layout successor.
9925ffd83dbSDimitry Andric       if (ShouldUpdateTerminators)
9935ffd83dbSDimitry Andric         PrevBB->updateTerminator(TailBB->getNextNode());
9945ffd83dbSDimitry Andric 
9950b57cec5SDimitry Andric       TDBBs.push_back(PrevBB);
9960b57cec5SDimitry Andric       Changed = true;
997349cc55cSDimitry Andric     } else {
998349cc55cSDimitry Andric       LLVM_DEBUG(dbgs() << "Abort merging blocks, the predecessor still "
999349cc55cSDimitry Andric                            "contains terminator instructions");
1000349cc55cSDimitry Andric       // Return early if no changes were made
1001349cc55cSDimitry Andric       if (!Changed)
1002349cc55cSDimitry Andric         return RemovedBranches;
1003349cc55cSDimitry Andric     }
1004349cc55cSDimitry Andric     Changed |= RemovedBranches;
10050b57cec5SDimitry Andric   }
10060b57cec5SDimitry Andric 
10070b57cec5SDimitry Andric   // If this is after register allocation, there are no phis to fix.
10080b57cec5SDimitry Andric   if (!PreRegAlloc)
10090b57cec5SDimitry Andric     return Changed;
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric   // If we made no changes so far, we are safe.
10120b57cec5SDimitry Andric   if (!Changed)
10130b57cec5SDimitry Andric     return Changed;
10140b57cec5SDimitry Andric 
10150b57cec5SDimitry Andric   // Handle the nasty case in that we duplicated a block that is part of a loop
10160b57cec5SDimitry Andric   // into some but not all of its predecessors. For example:
10170b57cec5SDimitry Andric   //    1 -> 2 <-> 3                 |
10180b57cec5SDimitry Andric   //          \                      |
10190b57cec5SDimitry Andric   //           \---> rest            |
10200b57cec5SDimitry Andric   // if we duplicate 2 into 1 but not into 3, we end up with
10210b57cec5SDimitry Andric   // 12 -> 3 <-> 2 -> rest           |
10220b57cec5SDimitry Andric   //   \             /               |
10230b57cec5SDimitry Andric   //    \----->-----/                |
10240b57cec5SDimitry Andric   // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced
10250b57cec5SDimitry Andric   // with a phi in 3 (which now dominates 2).
10260b57cec5SDimitry Andric   // What we do here is introduce a copy in 3 of the register defined by the
10270b57cec5SDimitry Andric   // phi, just like when we are duplicating 2 into 3, but we don't copy any
10280b57cec5SDimitry Andric   // real instructions or remove the 3 -> 2 edge from the phi in 2.
10290b57cec5SDimitry Andric   for (MachineBasicBlock *PredBB : Preds) {
10300b57cec5SDimitry Andric     if (is_contained(TDBBs, PredBB))
10310b57cec5SDimitry Andric       continue;
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric     // EH edges
10340b57cec5SDimitry Andric     if (PredBB->succ_size() != 1)
10350b57cec5SDimitry Andric       continue;
10360b57cec5SDimitry Andric 
10375ffd83dbSDimitry Andric     DenseMap<Register, RegSubRegPair> LocalVRMap;
10385ffd83dbSDimitry Andric     SmallVector<std::pair<Register, RegSubRegPair>, 4> CopyInfos;
10390b57cec5SDimitry Andric     // Process PHI instructions first.
104006c3fb27SDimitry Andric     for (MachineInstr &MI : make_early_inc_range(TailBB->phis())) {
10410b57cec5SDimitry Andric       // Replace the uses of the def of the PHI with the register coming
10420b57cec5SDimitry Andric       // from PredBB.
104306c3fb27SDimitry Andric       processPHI(&MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false);
10440b57cec5SDimitry Andric     }
10450b57cec5SDimitry Andric     appendCopies(PredBB, CopyInfos, Copies);
10460b57cec5SDimitry Andric   }
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric   return Changed;
10490b57cec5SDimitry Andric }
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric /// At the end of the block \p MBB generate COPY instructions between registers
10520b57cec5SDimitry Andric /// described by \p CopyInfos. Append resulting instructions to \p Copies.
10530b57cec5SDimitry Andric void TailDuplicator::appendCopies(MachineBasicBlock *MBB,
10545ffd83dbSDimitry Andric       SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos,
10550b57cec5SDimitry Andric       SmallVectorImpl<MachineInstr*> &Copies) {
10560b57cec5SDimitry Andric   MachineBasicBlock::iterator Loc = MBB->getFirstTerminator();
10570b57cec5SDimitry Andric   const MCInstrDesc &CopyD = TII->get(TargetOpcode::COPY);
10580b57cec5SDimitry Andric   for (auto &CI : CopyInfos) {
10590b57cec5SDimitry Andric     auto C = BuildMI(*MBB, Loc, DebugLoc(), CopyD, CI.first)
10600b57cec5SDimitry Andric                 .addReg(CI.second.Reg, 0, CI.second.SubReg);
10610b57cec5SDimitry Andric     Copies.push_back(C);
10620b57cec5SDimitry Andric   }
10630b57cec5SDimitry Andric }
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric /// Remove the specified dead machine basic block from the function, updating
10660b57cec5SDimitry Andric /// the CFG.
10670b57cec5SDimitry Andric void TailDuplicator::removeDeadBlock(
10680b57cec5SDimitry Andric     MachineBasicBlock *MBB,
10690b57cec5SDimitry Andric     function_ref<void(MachineBasicBlock *)> *RemovalCallback) {
10700b57cec5SDimitry Andric   assert(MBB->pred_empty() && "MBB must be dead!");
10710b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
10720b57cec5SDimitry Andric 
10735ffd83dbSDimitry Andric   MachineFunction *MF = MBB->getParent();
10745ffd83dbSDimitry Andric   // Update the call site info.
1075fe6060f1SDimitry Andric   for (const MachineInstr &MI : *MBB)
10765ffd83dbSDimitry Andric     if (MI.shouldUpdateCallSiteInfo())
10775ffd83dbSDimitry Andric       MF->eraseCallSiteInfo(&MI);
10785ffd83dbSDimitry Andric 
10790b57cec5SDimitry Andric   if (RemovalCallback)
10800b57cec5SDimitry Andric     (*RemovalCallback)(MBB);
10810b57cec5SDimitry Andric 
10820b57cec5SDimitry Andric   // Remove all successors.
10830b57cec5SDimitry Andric   while (!MBB->succ_empty())
10840b57cec5SDimitry Andric     MBB->removeSuccessor(MBB->succ_end() - 1);
10850b57cec5SDimitry Andric 
10860b57cec5SDimitry Andric   // Remove the block.
10870b57cec5SDimitry Andric   MBB->eraseFromParent();
10880b57cec5SDimitry Andric }
1089