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