LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TailDuplication.cpp
Go to the documentation of this file.
1 //===-- TailDuplication.cpp - Duplicate blocks into predecessors' tails ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass duplicates basic blocks ending in unconditional branches into
11 // the tails of their predecessors.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "tailduplication"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/Statistic.h"
28 #include "llvm/IR/Function.h"
30 #include "llvm/Support/Debug.h"
35 using namespace llvm;
36 
37 STATISTIC(NumTails , "Number of tails duplicated");
38 STATISTIC(NumTailDups , "Number of tail duplicated blocks");
39 STATISTIC(NumInstrDups , "Additional instructions due to tail duplication");
40 STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
41 STATISTIC(NumAddedPHIs , "Number of phis added");
42 
43 // Heuristic for tail duplication.
44 static cl::opt<unsigned>
45 TailDuplicateSize("tail-dup-size",
46  cl::desc("Maximum instructions to consider tail duplicating"),
47  cl::init(2), cl::Hidden);
48 
49 static cl::opt<bool>
50 TailDupVerify("tail-dup-verify",
51  cl::desc("Verify sanity of PHI instructions during taildup"),
52  cl::init(false), cl::Hidden);
53 
54 static cl::opt<unsigned>
55 TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden);
56 
57 typedef std::vector<std::pair<MachineBasicBlock*,unsigned> > AvailableValsTy;
58 
59 namespace {
60  /// TailDuplicatePass - Perform tail duplication.
61  class TailDuplicatePass : public MachineFunctionPass {
62  const TargetInstrInfo *TII;
63  const TargetRegisterInfo *TRI;
64  MachineModuleInfo *MMI;
67  bool PreRegAlloc;
68 
69  // SSAUpdateVRs - A list of virtual registers for which to update SSA form.
70  SmallVector<unsigned, 16> SSAUpdateVRs;
71 
72  // SSAUpdateVals - For each virtual register in SSAUpdateVals keep a list of
73  // source virtual registers.
75 
76  public:
77  static char ID;
78  explicit TailDuplicatePass() :
79  MachineFunctionPass(ID), PreRegAlloc(false) {}
80 
81  virtual bool runOnMachineFunction(MachineFunction &MF);
82 
83  private:
84  void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
85  MachineBasicBlock *BB);
86  void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
87  MachineBasicBlock *PredBB,
88  DenseMap<unsigned, unsigned> &LocalVRMap,
89  SmallVectorImpl<std::pair<unsigned,unsigned> > &Copies,
90  const DenseSet<unsigned> &UsedByPhi,
91  bool Remove);
92  void DuplicateInstruction(MachineInstr *MI,
93  MachineBasicBlock *TailBB,
94  MachineBasicBlock *PredBB,
95  MachineFunction &MF,
96  DenseMap<unsigned, unsigned> &LocalVRMap,
97  const DenseSet<unsigned> &UsedByPhi);
98  void UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
101  bool TailDuplicateBlocks(MachineFunction &MF);
102  bool shouldTailDuplicate(const MachineFunction &MF,
103  bool IsSimple, MachineBasicBlock &TailBB);
104  bool isSimpleBB(MachineBasicBlock *TailBB);
105  bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
106  bool duplicateSimpleBB(MachineBasicBlock *TailBB,
108  const DenseSet<unsigned> &RegsUsedByPhi,
110  bool TailDuplicate(MachineBasicBlock *TailBB,
111  bool IsSimple,
112  MachineFunction &MF,
115  bool TailDuplicateAndUpdate(MachineBasicBlock *MBB,
116  bool IsSimple,
117  MachineFunction &MF);
118 
119  void RemoveDeadBlock(MachineBasicBlock *MBB);
120  };
121 
122  char TailDuplicatePass::ID = 0;
123 }
124 
126 
127 INITIALIZE_PASS(TailDuplicatePass, "tailduplication", "Tail Duplication",
128  false, false)
129 
130 bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) {
131  TII = MF.getTarget().getInstrInfo();
132  TRI = MF.getTarget().getRegisterInfo();
133  MRI = &MF.getRegInfo();
134  MMI = getAnalysisIfAvailable<MachineModuleInfo>();
135  PreRegAlloc = MRI->isSSA();
136  RS.reset();
137  if (MRI->tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF))
138  RS.reset(new RegScavenger());
139 
140  bool MadeChange = false;
141  while (TailDuplicateBlocks(MF))
142  MadeChange = true;
143 
144  return MadeChange;
145 }
146 
147 static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) {
148  for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) {
149  MachineBasicBlock *MBB = I;
151  MBB->pred_end());
153  while (MI != MBB->end()) {
154  if (!MI->isPHI())
155  break;
157  PE = Preds.end(); PI != PE; ++PI) {
158  MachineBasicBlock *PredBB = *PI;
159  bool Found = false;
160  for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
161  MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB();
162  if (PHIBB == PredBB) {
163  Found = true;
164  break;
165  }
166  }
167  if (!Found) {
168  dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI;
169  dbgs() << " missing input from predecessor BB#"
170  << PredBB->getNumber() << '\n';
171  llvm_unreachable(0);
172  }
173  }
174 
175  for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
176  MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB();
177  if (CheckExtra && !Preds.count(PHIBB)) {
178  dbgs() << "Warning: malformed PHI in BB#" << MBB->getNumber()
179  << ": " << *MI;
180  dbgs() << " extra input from predecessor BB#"
181  << PHIBB->getNumber() << '\n';
182  llvm_unreachable(0);
183  }
184  if (PHIBB->getNumber() < 0) {
185  dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI;
186  dbgs() << " non-existing BB#" << PHIBB->getNumber() << '\n';
187  llvm_unreachable(0);
188  }
189  }
190  ++MI;
191  }
192  }
193 }
194 
195 /// TailDuplicateAndUpdate - Tail duplicate the block and cleanup.
196 bool
197 TailDuplicatePass::TailDuplicateAndUpdate(MachineBasicBlock *MBB,
198  bool IsSimple,
199  MachineFunction &MF) {
200  // Save the successors list.
202  MBB->succ_end());
203 
206  if (!TailDuplicate(MBB, IsSimple, MF, TDBBs, Copies))
207  return false;
208 
209  ++NumTails;
210 
212  MachineSSAUpdater SSAUpdate(MF, &NewPHIs);
213 
214  // TailBB's immediate successors are now successors of those predecessors
215  // which duplicated TailBB. Add the predecessors as sources to the PHI
216  // instructions.
217  bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken();
218  if (PreRegAlloc)
219  UpdateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
220 
221  // If it is dead, remove it.
222  if (isDead) {
223  NumInstrDups -= MBB->size();
224  RemoveDeadBlock(MBB);
225  ++NumDeadBlocks;
226  }
227 
228  // Update SSA form.
229  if (!SSAUpdateVRs.empty()) {
230  for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) {
231  unsigned VReg = SSAUpdateVRs[i];
232  SSAUpdate.Initialize(VReg);
233 
234  // If the original definition is still around, add it as an available
235  // value.
236  MachineInstr *DefMI = MRI->getVRegDef(VReg);
237  MachineBasicBlock *DefBB = 0;
238  if (DefMI) {
239  DefBB = DefMI->getParent();
240  SSAUpdate.AddAvailableValue(DefBB, VReg);
241  }
242 
243  // Add the new vregs as available values.
245  SSAUpdateVals.find(VReg);
246  for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
247  MachineBasicBlock *SrcBB = LI->second[j].first;
248  unsigned SrcReg = LI->second[j].second;
249  SSAUpdate.AddAvailableValue(SrcBB, SrcReg);
250  }
251 
252  // Rewrite uses that are outside of the original def's block.
253  MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg);
254  while (UI != MRI->use_end()) {
255  MachineOperand &UseMO = UI.getOperand();
256  MachineInstr *UseMI = &*UI;
257  ++UI;
258  if (UseMI->isDebugValue()) {
259  // SSAUpdate can replace the use with an undef. That creates
260  // a debug instruction that is a kill.
261  // FIXME: Should it SSAUpdate job to delete debug instructions
262  // instead of replacing the use with undef?
263  UseMI->eraseFromParent();
264  continue;
265  }
266  if (UseMI->getParent() == DefBB && !UseMI->isPHI())
267  continue;
268  SSAUpdate.RewriteUse(UseMO);
269  }
270  }
271 
272  SSAUpdateVRs.clear();
273  SSAUpdateVals.clear();
274  }
275 
276  // Eliminate some of the copies inserted by tail duplication to maintain
277  // SSA form.
278  for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
279  MachineInstr *Copy = Copies[i];
280  if (!Copy->isCopy())
281  continue;
282  unsigned Dst = Copy->getOperand(0).getReg();
283  unsigned Src = Copy->getOperand(1).getReg();
284  if (MRI->hasOneNonDBGUse(Src) &&
285  MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) {
286  // Copy is the only use. Do trivial copy propagation here.
287  MRI->replaceRegWith(Dst, Src);
288  Copy->eraseFromParent();
289  }
290  }
291 
292  if (NewPHIs.size())
293  NumAddedPHIs += NewPHIs.size();
294 
295  return true;
296 }
297 
298 /// TailDuplicateBlocks - Look for small blocks that are unconditionally
299 /// branched to and do not fall through. Tail-duplicate their instructions
300 /// into their predecessors to eliminate (dynamic) branches.
301 bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) {
302  bool MadeChange = false;
303 
304  if (PreRegAlloc && TailDupVerify) {
305  DEBUG(dbgs() << "\n*** Before tail-duplicating\n");
306  VerifyPHIs(MF, true);
307  }
308 
309  for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
310  MachineBasicBlock *MBB = I++;
311 
312  if (NumTails == TailDupLimit)
313  break;
314 
315  bool IsSimple = isSimpleBB(MBB);
316 
317  if (!shouldTailDuplicate(MF, IsSimple, *MBB))
318  continue;
319 
320  MadeChange |= TailDuplicateAndUpdate(MBB, IsSimple, MF);
321  }
322 
323  if (PreRegAlloc && TailDupVerify)
324  VerifyPHIs(MF, false);
325 
326  return MadeChange;
327 }
328 
329 static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB,
330  const MachineRegisterInfo *MRI) {
331  for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
332  UE = MRI->use_end(); UI != UE; ++UI) {
333  MachineInstr *UseMI = &*UI;
334  if (UseMI->isDebugValue())
335  continue;
336  if (UseMI->getParent() != BB)
337  return true;
338  }
339  return false;
340 }
341 
343  for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
344  if (MI->getOperand(i+1).getMBB() == SrcBB)
345  return i;
346  return 0;
347 }
348 
349 
350 // Remember which registers are used by phis in this block. This is
351 // used to determine which registers are liveout while modifying the
352 // block (which is why we need to copy the information).
353 static void getRegsUsedByPHIs(const MachineBasicBlock &BB,
354  DenseSet<unsigned> *UsedByPhi) {
355  for(MachineBasicBlock::const_iterator I = BB.begin(), E = BB.end();
356  I != E; ++I) {
357  const MachineInstr &MI = *I;
358  if (!MI.isPHI())
359  break;
360  for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
361  unsigned SrcReg = MI.getOperand(i).getReg();
362  UsedByPhi->insert(SrcReg);
363  }
364  }
365 }
366 
367 /// AddSSAUpdateEntry - Add a definition and source virtual registers pair for
368 /// SSA update.
369 void TailDuplicatePass::AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
370  MachineBasicBlock *BB) {
371  DenseMap<unsigned, AvailableValsTy>::iterator LI= SSAUpdateVals.find(OrigReg);
372  if (LI != SSAUpdateVals.end())
373  LI->second.push_back(std::make_pair(BB, NewReg));
374  else {
375  AvailableValsTy Vals;
376  Vals.push_back(std::make_pair(BB, NewReg));
377  SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
378  SSAUpdateVRs.push_back(OrigReg);
379  }
380 }
381 
382 /// ProcessPHI - Process PHI node in TailBB by turning it into a copy in PredBB.
383 /// Remember the source register that's contributed by PredBB and update SSA
384 /// update map.
385 void TailDuplicatePass::ProcessPHI(
387  DenseMap<unsigned, unsigned> &LocalVRMap,
388  SmallVectorImpl<std::pair<unsigned, unsigned> > &Copies,
389  const DenseSet<unsigned> &RegsUsedByPhi, bool Remove) {
390  unsigned DefReg = MI->getOperand(0).getReg();
391  unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
392  assert(SrcOpIdx && "Unable to find matching PHI source?");
393  unsigned SrcReg = MI->getOperand(SrcOpIdx).getReg();
394  const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
395  LocalVRMap.insert(std::make_pair(DefReg, SrcReg));
396 
397  // Insert a copy from source to the end of the block. The def register is the
398  // available value liveout of the block.
399  unsigned NewDef = MRI->createVirtualRegister(RC);
400  Copies.push_back(std::make_pair(NewDef, SrcReg));
401  if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg))
402  AddSSAUpdateEntry(DefReg, NewDef, PredBB);
403 
404  if (!Remove)
405  return;
406 
407  // Remove PredBB from the PHI node.
408  MI->RemoveOperand(SrcOpIdx+1);
409  MI->RemoveOperand(SrcOpIdx);
410  if (MI->getNumOperands() == 1)
411  MI->eraseFromParent();
412 }
413 
414 /// DuplicateInstruction - Duplicate a TailBB instruction to PredBB and update
415 /// the source operands due to earlier PHI translation.
416 void TailDuplicatePass::DuplicateInstruction(MachineInstr *MI,
417  MachineBasicBlock *TailBB,
418  MachineBasicBlock *PredBB,
419  MachineFunction &MF,
420  DenseMap<unsigned, unsigned> &LocalVRMap,
421  const DenseSet<unsigned> &UsedByPhi) {
422  MachineInstr *NewMI = TII->duplicate(MI, MF);
423  for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
424  MachineOperand &MO = NewMI->getOperand(i);
425  if (!MO.isReg())
426  continue;
427  unsigned Reg = MO.getReg();
429  continue;
430  if (MO.isDef()) {
431  const TargetRegisterClass *RC = MRI->getRegClass(Reg);
432  unsigned NewReg = MRI->createVirtualRegister(RC);
433  MO.setReg(NewReg);
434  LocalVRMap.insert(std::make_pair(Reg, NewReg));
435  if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg))
436  AddSSAUpdateEntry(Reg, NewReg, PredBB);
437  } else {
438  DenseMap<unsigned, unsigned>::iterator VI = LocalVRMap.find(Reg);
439  if (VI != LocalVRMap.end()) {
440  MO.setReg(VI->second);
441  MRI->constrainRegClass(VI->second, MRI->getRegClass(Reg));
442  }
443  }
444  }
445  PredBB->insert(PredBB->instr_end(), NewMI);
446 }
447 
448 /// UpdateSuccessorsPHIs - After FromBB is tail duplicated into its predecessor
449 /// blocks, the successors have gained new predecessors. Update the PHI
450 /// instructions in them accordingly.
451 void
452 TailDuplicatePass::UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
456  SE = Succs.end(); SI != SE; ++SI) {
457  MachineBasicBlock *SuccBB = *SI;
458  for (MachineBasicBlock::iterator II = SuccBB->begin(), EE = SuccBB->end();
459  II != EE; ++II) {
460  if (!II->isPHI())
461  break;
462  MachineInstrBuilder MIB(*FromBB->getParent(), II);
463  unsigned Idx = 0;
464  for (unsigned i = 1, e = II->getNumOperands(); i != e; i += 2) {
465  MachineOperand &MO = II->getOperand(i+1);
466  if (MO.getMBB() == FromBB) {
467  Idx = i;
468  break;
469  }
470  }
471 
472  assert(Idx != 0);
473  MachineOperand &MO0 = II->getOperand(Idx);
474  unsigned Reg = MO0.getReg();
475  if (isDead) {
476  // Folded into the previous BB.
477  // There could be duplicate phi source entries. FIXME: Should sdisel
478  // or earlier pass fixed this?
479  for (unsigned i = II->getNumOperands()-2; i != Idx; i -= 2) {
480  MachineOperand &MO = II->getOperand(i+1);
481  if (MO.getMBB() == FromBB) {
482  II->RemoveOperand(i+1);
483  II->RemoveOperand(i);
484  }
485  }
486  } else
487  Idx = 0;
488 
489  // If Idx is set, the operands at Idx and Idx+1 must be removed.
490  // We reuse the location to avoid expensive RemoveOperand calls.
491 
493  if (LI != SSAUpdateVals.end()) {
494  // This register is defined in the tail block.
495  for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
496  MachineBasicBlock *SrcBB = LI->second[j].first;
497  // If we didn't duplicate a bb into a particular predecessor, we
498  // might still have added an entry to SSAUpdateVals to correcly
499  // recompute SSA. If that case, avoid adding a dummy extra argument
500  // this PHI.
501  if (!SrcBB->isSuccessor(SuccBB))
502  continue;
503 
504  unsigned SrcReg = LI->second[j].second;
505  if (Idx != 0) {
506  II->getOperand(Idx).setReg(SrcReg);
507  II->getOperand(Idx+1).setMBB(SrcBB);
508  Idx = 0;
509  } else {
510  MIB.addReg(SrcReg).addMBB(SrcBB);
511  }
512  }
513  } else {
514  // Live in tail block, must also be live in predecessors.
515  for (unsigned j = 0, ee = TDBBs.size(); j != ee; ++j) {
516  MachineBasicBlock *SrcBB = TDBBs[j];
517  if (Idx != 0) {
518  II->getOperand(Idx).setReg(Reg);
519  II->getOperand(Idx+1).setMBB(SrcBB);
520  Idx = 0;
521  } else {
522  MIB.addReg(Reg).addMBB(SrcBB);
523  }
524  }
525  }
526  if (Idx != 0) {
527  II->RemoveOperand(Idx+1);
528  II->RemoveOperand(Idx);
529  }
530  }
531  }
532 }
533 
534 /// shouldTailDuplicate - Determine if it is profitable to duplicate this block.
535 bool
536 TailDuplicatePass::shouldTailDuplicate(const MachineFunction &MF,
537  bool IsSimple,
538  MachineBasicBlock &TailBB) {
539  // Only duplicate blocks that end with unconditional branches.
540  if (TailBB.canFallThrough())
541  return false;
542 
543  // Don't try to tail-duplicate single-block loops.
544  if (TailBB.isSuccessor(&TailBB))
545  return false;
546 
547  // Set the limit on the cost to duplicate. When optimizing for size,
548  // duplicate only one, because one branch instruction can be eliminated to
549  // compensate for the duplication.
550  unsigned MaxDuplicateCount;
551  if (TailDuplicateSize.getNumOccurrences() == 0 &&
552  MF.getFunction()->getAttributes().
553  hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize))
554  MaxDuplicateCount = 1;
555  else
556  MaxDuplicateCount = TailDuplicateSize;
557 
558  // If the target has hardware branch prediction that can handle indirect
559  // branches, duplicating them can often make them predictable when there
560  // are common paths through the code. The limit needs to be high enough
561  // to allow undoing the effects of tail merging and other optimizations
562  // that rearrange the predecessors of the indirect branch.
563 
564  bool HasIndirectbr = false;
565  if (!TailBB.empty())
566  HasIndirectbr = TailBB.back().isIndirectBranch();
567 
568  if (HasIndirectbr && PreRegAlloc)
569  MaxDuplicateCount = 20;
570 
571  // Check the instructions in the block to determine whether tail-duplication
572  // is invalid or unlikely to be profitable.
573  unsigned InstrCount = 0;
574  for (MachineBasicBlock::iterator I = TailBB.begin(); I != TailBB.end(); ++I) {
575  // Non-duplicable things shouldn't be tail-duplicated.
576  if (I->isNotDuplicable())
577  return false;
578 
579  // Do not duplicate 'return' instructions if this is a pre-regalloc run.
580  // A return may expand into a lot more instructions (e.g. reload of callee
581  // saved registers) after PEI.
582  if (PreRegAlloc && I->isReturn())
583  return false;
584 
585  // Avoid duplicating calls before register allocation. Calls presents a
586  // barrier to register allocation so duplicating them may end up increasing
587  // spills.
588  if (PreRegAlloc && I->isCall())
589  return false;
590 
591  if (!I->isPHI() && !I->isDebugValue())
592  InstrCount += 1;
593 
594  if (InstrCount > MaxDuplicateCount)
595  return false;
596  }
597 
598  if (HasIndirectbr && PreRegAlloc)
599  return true;
600 
601  if (IsSimple)
602  return true;
603 
604  if (!PreRegAlloc)
605  return true;
606 
607  return canCompletelyDuplicateBB(TailBB);
608 }
609 
610 /// isSimpleBB - True if this BB has only one unconditional jump.
611 bool
612 TailDuplicatePass::isSimpleBB(MachineBasicBlock *TailBB) {
613  if (TailBB->succ_size() != 1)
614  return false;
615  if (TailBB->pred_empty())
616  return false;
617  MachineBasicBlock::iterator I = TailBB->begin();
618  MachineBasicBlock::iterator E = TailBB->end();
619  while (I != E && I->isDebugValue())
620  ++I;
621  if (I == E)
622  return true;
623  return I->isUnconditionalBranch();
624 }
625 
626 static bool
630  SE = A.succ_end(); SI != SE; ++SI) {
631  MachineBasicBlock *BB = *SI;
632  if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI())
633  return true;
634  }
635 
636  return false;
637 }
638 
639 bool
640 TailDuplicatePass::canCompletelyDuplicateBB(MachineBasicBlock &BB) {
642  PE = BB.pred_end(); PI != PE; ++PI) {
643  MachineBasicBlock *PredBB = *PI;
644 
645  if (PredBB->succ_size() > 1)
646  return false;
647 
648  MachineBasicBlock *PredTBB = NULL, *PredFBB = NULL;
650  if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
651  return false;
652 
653  if (!PredCond.empty())
654  return false;
655  }
656  return true;
657 }
658 
659 bool
660 TailDuplicatePass::duplicateSimpleBB(MachineBasicBlock *TailBB,
662  const DenseSet<unsigned> &UsedByPhi,
665  TailBB->succ_end());
667  TailBB->pred_end());
668  bool Changed = false;
670  PE = Preds.end(); PI != PE; ++PI) {
671  MachineBasicBlock *PredBB = *PI;
672 
673  if (PredBB->getLandingPadSuccessor())
674  continue;
675 
676  if (bothUsedInPHI(*PredBB, Succs))
677  continue;
678 
679  MachineBasicBlock *PredTBB = NULL, *PredFBB = NULL;
681  if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
682  continue;
683 
684  Changed = true;
685  DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
686  << "From simple Succ: " << *TailBB);
687 
688  MachineBasicBlock *NewTarget = *TailBB->succ_begin();
690 
691  // Make PredFBB explicit.
692  if (PredCond.empty())
693  PredFBB = PredTBB;
694 
695  // Make fall through explicit.
696  if (!PredTBB)
697  PredTBB = NextBB;
698  if (!PredFBB)
699  PredFBB = NextBB;
700 
701  // Redirect
702  if (PredFBB == TailBB)
703  PredFBB = NewTarget;
704  if (PredTBB == TailBB)
705  PredTBB = NewTarget;
706 
707  // Make the branch unconditional if possible
708  if (PredTBB == PredFBB) {
709  PredCond.clear();
710  PredFBB = NULL;
711  }
712 
713  // Avoid adding fall through branches.
714  if (PredFBB == NextBB)
715  PredFBB = NULL;
716  if (PredTBB == NextBB && PredFBB == NULL)
717  PredTBB = NULL;
718 
719  TII->RemoveBranch(*PredBB);
720 
721  if (PredTBB)
722  TII->InsertBranch(*PredBB, PredTBB, PredFBB, PredCond, DebugLoc());
723 
724  PredBB->removeSuccessor(TailBB);
725  unsigned NumSuccessors = PredBB->succ_size();
726  assert(NumSuccessors <= 1);
727  if (NumSuccessors == 0 || *PredBB->succ_begin() != NewTarget)
728  PredBB->addSuccessor(NewTarget);
729 
730  TDBBs.push_back(PredBB);
731  }
732  return Changed;
733 }
734 
735 /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each
736 /// of its predecessors.
737 bool
738 TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB,
739  bool IsSimple,
740  MachineFunction &MF,
743  DEBUG(dbgs() << "\n*** Tail-duplicating BB#" << TailBB->getNumber() << '\n');
744 
745  DenseSet<unsigned> UsedByPhi;
746  getRegsUsedByPHIs(*TailBB, &UsedByPhi);
747 
748  if (IsSimple)
749  return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi, Copies);
750 
751  // Iterate through all the unique predecessors and tail-duplicate this
752  // block into them, if possible. Copying the list ahead of time also
753  // avoids trouble with the predecessor list reallocating.
754  bool Changed = false;
756  TailBB->pred_end());
758  PE = Preds.end(); PI != PE; ++PI) {
759  MachineBasicBlock *PredBB = *PI;
760 
761  assert(TailBB != PredBB &&
762  "Single-block loop should have been rejected earlier!");
763  // EH edges are ignored by AnalyzeBranch.
764  if (PredBB->succ_size() > 1)
765  continue;
766 
767  MachineBasicBlock *PredTBB, *PredFBB;
769  if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
770  continue;
771  if (!PredCond.empty())
772  continue;
773  // Don't duplicate into a fall-through predecessor (at least for now).
774  if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
775  continue;
776 
777  DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
778  << "From Succ: " << *TailBB);
779 
780  TDBBs.push_back(PredBB);
781 
782  // Remove PredBB's unconditional branch.
783  TII->RemoveBranch(*PredBB);
784 
785  if (RS && !TailBB->livein_empty()) {
786  // Update PredBB livein.
787  RS->enterBasicBlock(PredBB);
788  if (!PredBB->empty())
789  RS->forward(prior(PredBB->end()));
790  BitVector RegsLiveAtExit(TRI->getNumRegs());
791  RS->getRegsUsed(RegsLiveAtExit, false);
793  E = TailBB->livein_end(); I != E; ++I) {
794  if (!RegsLiveAtExit[*I])
795  // If a register is previously livein to the tail but it's not live
796  // at the end of predecessor BB, then it should be added to its
797  // livein list.
798  PredBB->addLiveIn(*I);
799  }
800  }
801 
802  // Clone the contents of TailBB into PredBB.
803  DenseMap<unsigned, unsigned> LocalVRMap;
805  // Use instr_iterator here to properly handle bundles, e.g.
806  // ARM Thumb2 IT block.
808  while (I != TailBB->instr_end()) {
809  MachineInstr *MI = &*I;
810  ++I;
811  if (MI->isPHI()) {
812  // Replace the uses of the def of the PHI with the register coming
813  // from PredBB.
814  ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true);
815  } else {
816  // Replace def of virtual registers with new registers, and update
817  // uses with PHI source register or the new registers.
818  DuplicateInstruction(MI, TailBB, PredBB, MF, LocalVRMap, UsedByPhi);
819  }
820  }
822  for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) {
823  Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(),
824  TII->get(TargetOpcode::COPY),
825  CopyInfos[i].first).addReg(CopyInfos[i].second));
826  }
827 
828  // Simplify
829  TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true);
830 
831  NumInstrDups += TailBB->size() - 1; // subtract one for removed branch
832 
833  // Update the CFG.
834  PredBB->removeSuccessor(PredBB->succ_begin());
835  assert(PredBB->succ_empty() &&
836  "TailDuplicate called on block with multiple successors!");
837  for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(),
838  E = TailBB->succ_end(); I != E; ++I)
839  PredBB->addSuccessor(*I);
840 
841  Changed = true;
842  ++NumTailDups;
843  }
844 
845  // If TailBB was duplicated into all its predecessors except for the prior
846  // block, which falls through unconditionally, move the contents of this
847  // block into the prior block.
849  MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
851  // This has to check PrevBB->succ_size() because EH edges are ignored by
852  // AnalyzeBranch.
853  if (PrevBB->succ_size() == 1 &&
854  !TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true) &&
855  PriorCond.empty() && !PriorTBB && TailBB->pred_size() == 1 &&
856  !TailBB->hasAddressTaken()) {
857  DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
858  << "From MBB: " << *TailBB);
859  if (PreRegAlloc) {
860  DenseMap<unsigned, unsigned> LocalVRMap;
862  MachineBasicBlock::iterator I = TailBB->begin();
863  // Process PHI instructions first.
864  while (I != TailBB->end() && I->isPHI()) {
865  // Replace the uses of the def of the PHI with the register coming
866  // from PredBB.
867  MachineInstr *MI = &*I++;
868  ProcessPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi, true);
869  if (MI->getParent())
870  MI->eraseFromParent();
871  }
872 
873  // Now copy the non-PHI instructions.
874  while (I != TailBB->end()) {
875  // Replace def of virtual registers with new registers, and update
876  // uses with PHI source register or the new registers.
877  MachineInstr *MI = &*I++;
878  assert(!MI->isBundle() && "Not expecting bundles before regalloc!");
879  DuplicateInstruction(MI, TailBB, PrevBB, MF, LocalVRMap, UsedByPhi);
880  MI->eraseFromParent();
881  }
883  for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) {
884  Copies.push_back(BuildMI(*PrevBB, Loc, DebugLoc(),
885  TII->get(TargetOpcode::COPY),
886  CopyInfos[i].first)
887  .addReg(CopyInfos[i].second));
888  }
889  } else {
890  // No PHIs to worry about, just splice the instructions over.
891  PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
892  }
893  PrevBB->removeSuccessor(PrevBB->succ_begin());
894  assert(PrevBB->succ_empty());
895  PrevBB->transferSuccessors(TailBB);
896  TDBBs.push_back(PrevBB);
897  Changed = true;
898  }
899 
900  // If this is after register allocation, there are no phis to fix.
901  if (!PreRegAlloc)
902  return Changed;
903 
904  // If we made no changes so far, we are safe.
905  if (!Changed)
906  return Changed;
907 
908 
909  // Handle the nasty case in that we duplicated a block that is part of a loop
910  // into some but not all of its predecessors. For example:
911  // 1 -> 2 <-> 3 |
912  // \ |
913  // \---> rest |
914  // if we duplicate 2 into 1 but not into 3, we end up with
915  // 12 -> 3 <-> 2 -> rest |
916  // \ / |
917  // \----->-----/ |
918  // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced
919  // with a phi in 3 (which now dominates 2).
920  // What we do here is introduce a copy in 3 of the register defined by the
921  // phi, just like when we are duplicating 2 into 3, but we don't copy any
922  // real instructions or remove the 3 -> 2 edge from the phi in 2.
924  PE = Preds.end(); PI != PE; ++PI) {
925  MachineBasicBlock *PredBB = *PI;
926  if (std::find(TDBBs.begin(), TDBBs.end(), PredBB) != TDBBs.end())
927  continue;
928 
929  // EH edges
930  if (PredBB->succ_size() != 1)
931  continue;
932 
933  DenseMap<unsigned, unsigned> LocalVRMap;
935  MachineBasicBlock::iterator I = TailBB->begin();
936  // Process PHI instructions first.
937  while (I != TailBB->end() && I->isPHI()) {
938  // Replace the uses of the def of the PHI with the register coming
939  // from PredBB.
940  MachineInstr *MI = &*I++;
941  ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false);
942  }
944  for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) {
945  Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(),
946  TII->get(TargetOpcode::COPY),
947  CopyInfos[i].first).addReg(CopyInfos[i].second));
948  }
949  }
950 
951  return Changed;
952 }
953 
954 /// RemoveDeadBlock - Remove the specified dead machine basic block from the
955 /// function, updating the CFG.
956 void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) {
957  assert(MBB->pred_empty() && "MBB must be dead!");
958  DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
959 
960  // Remove all successors.
961  while (!MBB->succ_empty())
962  MBB->removeSuccessor(MBB->succ_end()-1);
963 
964  // Remove the block.
965  MBB->eraseFromParent();
966 }
unsigned succ_size() const
const MachineFunction * getParent() const
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const
static cl::opt< bool > TailDupVerify("tail-dup-verify", cl::desc("Verify sanity of PHI instructions during taildup"), cl::init(false), cl::Hidden)
instr_iterator instr_begin()
instr_iterator instr_end()
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const
MachineBasicBlock * getMBB() const
std::vector< unsigned >::const_iterator livein_iterator
static bool isVirtualRegister(unsigned Reg)
void addLiveIn(unsigned Reg)
const Function * getFunction() const
static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB)
Instructions::iterator instr_iterator
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:79
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:411
livein_iterator livein_begin() const
static use_iterator use_end()
const HexagonInstrInfo * TII
bool isPHI() const
Definition: MachineInstr.h:648
#define llvm_unreachable(msg)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
std::vector< MachineBasicBlock * >::iterator succ_iterator
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
unsigned getNumOperands() const
Definition: MachineInstr.h:265
void RemoveOperand(unsigned i)
void transferSuccessors(MachineBasicBlock *fromMBB)
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
Definition: SmallPtrSet.h:264
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
STATISTIC(NumTails,"Number of tails duplicated")
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:69
std::vector< MachineBasicBlock * >::iterator pred_iterator
static bool bothUsedInPHI(const MachineBasicBlock &A, SmallPtrSet< MachineBasicBlock *, 8 > SuccsB)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:119
bool isDebugValue() const
Definition: MachineInstr.h:639
bool isBundle() const
Definition: MachineInstr.h:666
bundle_iterator< MachineInstr, instr_iterator > iterator
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:314
static void getRegsUsedByPHIs(const MachineBasicBlock &BB, DenseSet< unsigned > *UsedByPhi)
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value. See class MCOperandInfo.
livein_iterator livein_end() const
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
static cl::opt< unsigned > TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden)
bool isIndirectBranch(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:380
bool isCopy() const
Definition: MachineInstr.h:669
ItTy next(ItTy it, Dist n)
Definition: STLExtras.h:154
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, const SmallVectorImpl< MachineOperand > &Cond, DebugLoc DL) const
const MachineBasicBlock * getLandingPadSuccessor() const
static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, const MachineRegisterInfo *MRI)
static cl::opt< unsigned > TailDuplicateSize("tail-dup-size", cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2), cl::Hidden)
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
void removeSuccessor(MachineBasicBlock *succ)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:153
bool count(const ValueT &V) const
Definition: DenseSet.h:45
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:218
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:117
std::vector< std::pair< MachineBasicBlock *, unsigned > > AvailableValsTy
char & TailDuplicateID
bool isSuccessor(const MachineBasicBlock *MBB) const
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:170
bundle_iterator< const MachineInstr, const_instr_iterator > const_iterator
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
use_iterator use_begin(unsigned RegNo) const
void setReg(unsigned Reg)
#define I(x, y, z)
Definition: MD5.cpp:54
INITIALIZE_PASS(TailDuplicatePass,"tailduplication","Tail Duplication", false, false) bool TailDuplicatePass
static void VerifyPHIs(MachineFunction &MF, bool CheckExtra)
instr_iterator insert(instr_iterator I, MachineInstr *M)
unsigned getReg() const
getReg - Returns the register number.
virtual const HexagonRegisterInfo & getRegisterInfo() const
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
BasicBlockListType::iterator iterator
ItTy prior(ItTy it, Dist n)
Definition: STLExtras.h:167
#define DEBUG(X)
Definition: Debug.h:97
const MCRegisterInfo & MRI
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
void addSuccessor(MachineBasicBlock *succ, uint32_t weight=0)
unsigned pred_size() const