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