1 //===-- TailDuplication.cpp - Duplicate blocks into predecessors' tails ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass duplicates basic blocks ending in unconditional branches into
11 // the tails of their predecessors.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "tailduplication"
16 #include "llvm/Function.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/MachineSSAUpdater.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/ADT/SmallSet.h"
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/ADT/Statistic.h"
31 STATISTIC(NumTailDups , "Number of tail duplicated blocks");
32 STATISTIC(NumInstrDups , "Additional instructions due to tail duplication");
33 STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
35 // Heuristic for tail duplication.
36 static cl::opt<unsigned>
37 TailDuplicateSize("tail-dup-size",
38 cl::desc("Maximum instructions to consider tail duplicating"),
39 cl::init(2), cl::Hidden);
41 typedef std::vector<unsigned> AvailableValsTy;
44 /// TailDuplicatePass - Perform tail duplication.
45 class TailDuplicatePass : public MachineFunctionPass {
47 const TargetInstrInfo *TII;
48 MachineModuleInfo *MMI;
49 MachineRegisterInfo *MRI;
51 // SSAUpdateVRs - A list of virtual registers for which to update SSA form.
52 SmallVector<unsigned, 16> SSAUpdateVRs;
54 // SSAUpdateVals - For each virtual register in SSAUpdateVals keep a list of
55 // source virtual registers.
56 DenseMap<unsigned, AvailableValsTy> SSAUpdateVals;
60 explicit TailDuplicatePass(bool PreRA) :
61 MachineFunctionPass(&ID), PreRegAlloc(PreRA) {}
63 virtual bool runOnMachineFunction(MachineFunction &MF);
64 virtual const char *getPassName() const { return "Tail Duplication"; }
67 void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg);
68 void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
69 MachineBasicBlock *PredBB,
70 DenseMap<unsigned, unsigned> &LocalVRMap);
71 void DuplicateInstruction(MachineInstr *MI,
72 MachineBasicBlock *TailBB,
73 MachineBasicBlock *PredBB,
75 DenseMap<unsigned, unsigned> &LocalVRMap);
76 void UpdateSuccessorsPHIs(MachineBasicBlock *FromBB,MachineBasicBlock *ToBB,
77 SmallSetVector<MachineBasicBlock*,8> &Succs);
78 bool TailDuplicateBlocks(MachineFunction &MF);
79 bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF);
80 void RemoveDeadBlock(MachineBasicBlock *MBB);
83 char TailDuplicatePass::ID = 0;
86 FunctionPass *llvm::createTailDuplicatePass(bool PreRegAlloc) {
87 return new TailDuplicatePass(PreRegAlloc);
90 bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) {
91 TII = MF.getTarget().getInstrInfo();
92 MRI = &MF.getRegInfo();
93 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
95 bool MadeChange = false;
96 bool MadeChangeThisIteration = true;
97 while (MadeChangeThisIteration) {
98 MadeChangeThisIteration = false;
99 MadeChangeThisIteration |= TailDuplicateBlocks(MF);
100 MadeChange |= MadeChangeThisIteration;
106 /// TailDuplicateBlocks - Look for small blocks that are unconditionally
107 /// branched to and do not fall through. Tail-duplicate their instructions
108 /// into their predecessors to eliminate (dynamic) branches.
109 bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) {
110 bool MadeChange = false;
112 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
113 MachineBasicBlock *MBB = I++;
115 // Only duplicate blocks that end with unconditional branches.
116 if (MBB->canFallThrough())
119 MadeChange |= TailDuplicate(MBB, MF);
121 // If it is dead, remove it. Don't do this if this pass is run before
122 // register allocation to avoid having to update PHI nodes.
123 if (!PreRegAlloc && MBB->pred_empty()) {
124 NumInstrDups -= MBB->size();
125 RemoveDeadBlock(MBB);
134 static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB,
135 const MachineRegisterInfo *MRI) {
136 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
137 UE = MRI->use_end(); UI != UE; ++UI) {
138 MachineInstr *UseMI = &*UI;
139 if (UseMI->getParent() != BB)
145 static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) {
146 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
147 if (MI->getOperand(i+1).getMBB() == SrcBB)
152 /// AddSSAUpdateEntry - Add a definition and source virtual registers pair for
154 void TailDuplicatePass::AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg) {
155 DenseMap<unsigned, AvailableValsTy>::iterator LI =
156 SSAUpdateVals.find(OrigReg);
157 if (LI != SSAUpdateVals.end())
158 LI->second.push_back(NewReg);
160 AvailableValsTy Vals;
161 Vals.push_back(NewReg);
162 SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
163 SSAUpdateVRs.push_back(OrigReg);
167 /// ProcessPHI - Process but do not duplicate a PHI node in TailBB. Remember the
168 /// source register that's contributed by PredBB and update SSA update map.
169 void TailDuplicatePass::ProcessPHI(MachineInstr *MI,
170 MachineBasicBlock *TailBB,
171 MachineBasicBlock *PredBB,
172 DenseMap<unsigned, unsigned> &LocalVRMap) {
173 unsigned DefReg = MI->getOperand(0).getReg();
174 unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
175 assert(SrcOpIdx && "Unable to find matching PHI source?");
176 unsigned SrcReg = MI->getOperand(SrcOpIdx).getReg();
177 LocalVRMap.insert(std::make_pair(DefReg, SrcReg));
178 if (isDefLiveOut(DefReg, TailBB, MRI))
179 AddSSAUpdateEntry(DefReg, SrcReg);
181 // Remove PredBB from the PHI node.
182 MI->RemoveOperand(SrcOpIdx+1);
183 MI->RemoveOperand(SrcOpIdx);
184 if (MI->getNumOperands() == 1)
185 MI->eraseFromParent();
188 /// DuplicateInstruction - Duplicate a TailBB instruction to PredBB and update
189 /// the source operands due to earlier PHI translation.
190 void TailDuplicatePass::DuplicateInstruction(MachineInstr *MI,
191 MachineBasicBlock *TailBB,
192 MachineBasicBlock *PredBB,
194 DenseMap<unsigned, unsigned> &LocalVRMap) {
195 MachineInstr *NewMI = MF.CloneMachineInstr(MI);
196 for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
197 MachineOperand &MO = NewMI->getOperand(i);
200 unsigned Reg = MO.getReg();
201 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg))
204 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
205 unsigned NewReg = MRI->createVirtualRegister(RC);
207 LocalVRMap.insert(std::make_pair(Reg, NewReg));
208 if (isDefLiveOut(Reg, TailBB, MRI))
209 AddSSAUpdateEntry(Reg, NewReg);
211 DenseMap<unsigned, unsigned>::iterator VI = LocalVRMap.find(Reg);
212 if (VI != LocalVRMap.end())
213 MO.setReg(VI->second);
216 PredBB->insert(PredBB->end(), NewMI);
219 /// UpdateSuccessorsPHIs - After FromBB is tail duplicated into its predecessor
220 /// blocks, the successors have gained new predecessors. Update the PHI
221 /// instructions in them accordingly.
222 void TailDuplicatePass::UpdateSuccessorsPHIs(MachineBasicBlock *FromBB,
223 MachineBasicBlock *ToBB,
224 SmallSetVector<MachineBasicBlock*,8> &Succs) {
225 for (SmallSetVector<MachineBasicBlock*, 8>::iterator SI = Succs.begin(),
226 SE = Succs.end(); SI != SE; ++SI) {
227 MachineBasicBlock *SuccBB = *SI;
228 for (MachineBasicBlock::iterator II = SuccBB->begin(), EE = SuccBB->end();
230 if (II->getOpcode() != TargetInstrInfo::PHI)
232 for (unsigned i = 1, e = II->getNumOperands(); i != e; i += 2) {
233 MachineOperand &MO1 = II->getOperand(i+1);
234 if (MO1.getMBB() != FromBB)
236 MachineOperand &MO0 = II->getOperand(i);
237 unsigned Reg = MO0.getReg();
239 // Folded into the previous BB.
240 II->RemoveOperand(i+1);
241 II->RemoveOperand(i);
243 DenseMap<unsigned, AvailableValsTy>::iterator LI =
244 SSAUpdateVals.find(Reg);
245 if (LI == SSAUpdateVals.end())
247 for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
248 unsigned NewReg = LI->second[j];
249 MachineInstr *DefMI = MRI->getVRegDef(NewReg);
250 II->addOperand(MachineOperand::CreateReg(NewReg, false));
251 II->addOperand(MachineOperand::CreateMBB(DefMI->getParent()));
259 /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each
260 /// of its predecessors.
261 bool TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB,
262 MachineFunction &MF) {
263 // Don't try to tail-duplicate single-block loops.
264 if (TailBB->isSuccessor(TailBB))
267 // Set the limit on the number of instructions to duplicate, with a default
268 // of one less than the tail-merge threshold. When optimizing for size,
269 // duplicate only one, because one branch instruction can be eliminated to
270 // compensate for the duplication.
271 unsigned MaxDuplicateCount;
272 if (!TailBB->empty() && TailBB->back().getDesc().isIndirectBranch())
273 // If the target has hardware branch prediction that can handle indirect
274 // branches, duplicating them can often make them predictable when there
275 // are common paths through the code. The limit needs to be high enough
276 // to allow undoing the effects of tail merging.
277 MaxDuplicateCount = 20;
278 else if (MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
279 MaxDuplicateCount = 1;
281 MaxDuplicateCount = TailDuplicateSize;
283 // Check the instructions in the block to determine whether tail-duplication
284 // is invalid or unlikely to be profitable.
285 unsigned InstrCount = 0;
286 bool HasCall = false;
287 for (MachineBasicBlock::iterator I = TailBB->begin();
288 I != TailBB->end(); ++I) {
289 // Non-duplicable things shouldn't be tail-duplicated.
290 if (I->getDesc().isNotDuplicable()) return false;
291 // Do not duplicate 'return' instructions if this is a pre-regalloc run.
292 // A return may expand into a lot more instructions (e.g. reload of callee
293 // saved registers) after PEI.
294 if (PreRegAlloc && I->getDesc().isReturn()) return false;
295 // Don't duplicate more than the threshold.
296 if (InstrCount == MaxDuplicateCount) return false;
297 // Remember if we saw a call.
298 if (I->getDesc().isCall()) HasCall = true;
299 if (I->getOpcode() != TargetInstrInfo::PHI)
302 // Heuristically, don't tail-duplicate calls if it would expand code size,
303 // as it's less likely to be worth the extra cost.
304 if (InstrCount > 1 && HasCall)
307 // Iterate through all the unique predecessors and tail-duplicate this
308 // block into them, if possible. Copying the list ahead of time also
309 // avoids trouble with the predecessor list reallocating.
310 bool Changed = false;
311 SmallSetVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(),
313 for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(),
314 PE = Preds.end(); PI != PE; ++PI) {
315 MachineBasicBlock *PredBB = *PI;
317 assert(TailBB != PredBB &&
318 "Single-block loop should have been rejected earlier!");
319 if (PredBB->succ_size() > 1) continue;
321 MachineBasicBlock *PredTBB, *PredFBB;
322 SmallVector<MachineOperand, 4> PredCond;
323 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
325 if (!PredCond.empty())
327 // EH edges are ignored by AnalyzeBranch.
328 if (PredBB->succ_size() != 1)
330 // Don't duplicate into a fall-through predecessor (at least for now).
331 if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
334 DEBUG(errs() << "\nTail-duplicating into PredBB: " << *PredBB
335 << "From Succ: " << *TailBB);
337 // Remove PredBB's unconditional branch.
338 TII->RemoveBranch(*PredBB);
340 // Clone the contents of TailBB into PredBB.
341 DenseMap<unsigned, unsigned> LocalVRMap;
342 MachineBasicBlock::iterator I = TailBB->begin();
343 while (I != TailBB->end()) {
344 MachineInstr *MI = &*I;
346 if (MI->getOpcode() == TargetInstrInfo::PHI) {
347 // Replace the uses of the def of the PHI with the register coming
349 ProcessPHI(MI, TailBB, PredBB, LocalVRMap);
351 // Replace def of virtual registers with new registers, and update
352 // uses with PHI source register or the new registers.
353 DuplicateInstruction(MI, TailBB, PredBB, MF, LocalVRMap);
356 NumInstrDups += TailBB->size() - 1; // subtract one for removed branch
359 PredBB->removeSuccessor(PredBB->succ_begin());
360 assert(PredBB->succ_empty() &&
361 "TailDuplicate called on block with multiple successors!");
362 for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(),
363 E = TailBB->succ_end(); I != E; ++I)
364 PredBB->addSuccessor(*I);
370 // Save the successors list.
371 SmallSetVector<MachineBasicBlock*, 8> Succs(TailBB->succ_begin(),
374 // If TailBB was duplicated into all its predecessors except for the prior
375 // block, which falls through unconditionally, move the contents of this
376 // block into the prior block.
377 MachineBasicBlock *PrevBB = prior(MachineFunction::iterator(TailBB));
378 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
379 SmallVector<MachineOperand, 4> PriorCond;
380 bool PriorUnAnalyzable =
381 TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true);
382 // This has to check PrevBB->succ_size() because EH edges are ignored by
384 // If TailBB starts with PHIs, then don't bother. Let the post regalloc
386 MachineBasicBlock *NewTailBB = 0;
387 if (!PriorUnAnalyzable && PriorCond.empty() && !PriorTBB &&
388 TailBB->pred_size() == 1 && PrevBB->succ_size() == 1 &&
389 !TailBB->hasAddressTaken()) {
390 DEBUG(errs() << "\nMerging into block: " << *PrevBB
391 << "From MBB: " << *TailBB);
393 DenseMap<unsigned, unsigned> LocalVRMap;
394 MachineBasicBlock::iterator I = TailBB->begin();
395 // Process PHI instructions first.
396 while (I != TailBB->end() && I->getOpcode() == TargetInstrInfo::PHI) {
397 // Replace the uses of the def of the PHI with the register coming
399 MachineInstr *MI = &*I++;
400 ProcessPHI(MI, TailBB, PrevBB, LocalVRMap);
402 MI->eraseFromParent();
405 // Now copy the non-PHI instructions.
406 while (I != TailBB->end()) {
407 // Replace def of virtual registers with new registers, and update
408 // uses with PHI source register or the new registers.
409 MachineInstr *MI = &*I++;
410 DuplicateInstruction(MI, TailBB, PrevBB, MF, LocalVRMap);
411 MI->eraseFromParent();
414 // No PHIs to worry about, just splice the instructions over.
415 PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
417 PrevBB->removeSuccessor(PrevBB->succ_begin());
418 assert(PrevBB->succ_empty());
419 PrevBB->transferSuccessors(TailBB);
427 // TailBB's immediate successors are now successors of those predecessors
428 // which duplicated TailBB. Add the predecessors as sources to the PHI
430 UpdateSuccessorsPHIs(TailBB, NewTailBB, Succs);
432 if (!SSAUpdateVRs.empty()) {
434 MachineSSAUpdater SSAUpdate(MF);
435 for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) {
436 unsigned VReg = SSAUpdateVRs[i];
437 SSAUpdate.Initialize(VReg);
439 // If the original definition is still around, add it as an available
441 MachineInstr *DefMI = MRI->getVRegDef(VReg);
442 MachineBasicBlock *DefBB = 0;
444 DefBB = DefMI->getParent();
445 SSAUpdate.AddAvailableValue(DefBB, VReg);
448 // Add the new vregs as available values.
449 DenseMap<unsigned, AvailableValsTy>::iterator LI =
450 SSAUpdateVals.find(VReg);
451 for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
452 unsigned NewReg = LI->second[j];
453 MachineInstr *DefMI = MRI->getVRegDef(NewReg);
454 SSAUpdate.AddAvailableValue(DefMI->getParent(), NewReg);
457 // Rewrite uses that are outside of the original def's block.
458 MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg);
459 while (UI != MRI->use_end()) {
460 MachineOperand &UseMO = UI.getOperand();
461 MachineInstr *UseMI = &*UI;
463 if (UseMI->getParent() != DefBB)
464 SSAUpdate.RewriteUse(UseMO);
468 SSAUpdateVRs.clear();
469 SSAUpdateVals.clear();
475 /// RemoveDeadBlock - Remove the specified dead machine basic block from the
476 /// function, updating the CFG.
477 void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) {
478 assert(MBB->pred_empty() && "MBB must be dead!");
479 DEBUG(errs() << "\nRemoving MBB: " << *MBB);
481 // Remove all successors.
482 while (!MBB->succ_empty())
483 MBB->removeSuccessor(MBB->succ_end()-1);
485 // If there are any labels in the basic block, unregister them from
486 // MachineModuleInfo.
487 if (MMI && !MBB->empty()) {
488 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
491 // The label ID # is always operand #0, an immediate.
492 MMI->InvalidateLabel(I->getOperand(0).getImm());
497 MBB->eraseFromParent();