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