llvm.org GIT mirror llvm / 15acadd
Split tail duplication into a separate pass. This is needed to avoid running tail duplication when doing branch folding for if-conversion, and we also want to be able to run tail duplication earlier to fix some reg alloc problems. Move the CanFallThrough function from BranchFolding to MachineBasicBlock so that it can be shared by TailDuplication. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@89904 91177308-0d34-0410-b5e6-96231b3b80d8 Bob Wilson 10 years ago
8 changed file(s) with 320 addition(s) and 239 deletion(s). Raw diff Collapse all Expand all
270270 /// ends with an unconditional branch to some other block.
271271 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
272272
273 /// canFallThrough - Return true if the block can implicitly transfer
274 /// control to the block after it by falling off the end of it. This should
275 /// return false if it can reach the block after it, but it uses an explicit
276 /// branch to do so (e.g., a table jump). True is a conservative answer.
277 bool canFallThrough();
278
273279 /// getFirstTerminator - returns an iterator to the first terminator
274280 /// instruction of this basic block. If a terminator does not exist,
275281 /// it returns end()
128128 /// branches.
129129 FunctionPass *createBranchFoldingPass(bool DefaultEnableTailMerge);
130130
131 /// TailDuplication Pass - Duplicate blocks with unconditional branches
132 /// into tails of their predecessors.
133 FunctionPass *createTailDuplicationPass();
134
131135 /// IfConverter Pass - This pass performs machine code if conversion.
132136 FunctionPass *createIfConverterPass();
133137
4040 STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
4141 STATISTIC(NumBranchOpts, "Number of branches optimized");
4242 STATISTIC(NumTailMerge , "Number of block tails merged");
43 STATISTIC(NumTailDups , "Number of tail duplicated blocks");
44 STATISTIC(NumInstrDups , "Additional instructions due to tail duplication");
4543
4644 static cl::opt FlagEnableTailMerge("enable-tail-merge",
4745 cl::init(cl::BOU_UNSET), cl::Hidden);
201199 MadeChangeThisIteration = false;
202200 MadeChangeThisIteration |= TailMergeBlocks(MF);
203201 MadeChangeThisIteration |= OptimizeBranches(MF);
204 MadeChange |= MadeChangeThisIteration;
205 }
206
207 // Do tail duplication after tail merging is done. Otherwise it is
208 // tough to avoid situations where tail duplication and tail merging undo
209 // each other's transformations ad infinitum.
210 MadeChangeThisIteration = true;
211 while (MadeChangeThisIteration) {
212 MadeChangeThisIteration = false;
213 MadeChangeThisIteration |= TailDuplicateBlocks(MF);
214202 MadeChange |= MadeChangeThisIteration;
215203 }
216204
917905 }
918906
919907
920 /// CanFallThrough - Return true if the specified block (with the specified
921 /// branch condition) can implicitly transfer control to the block after it by
922 /// falling off the end of it. This should return false if it can reach the
923 /// block after it, but it uses an explicit branch to do so (e.g. a table jump).
924 ///
925 /// True is a conservative answer.
926 ///
927 bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB,
928 bool BranchUnAnalyzable,
929 MachineBasicBlock *TBB,
930 MachineBasicBlock *FBB,
931 const SmallVectorImpl &Cond) {
932 MachineFunction::iterator Fallthrough = CurBB;
933 ++Fallthrough;
934 // If FallthroughBlock is off the end of the function, it can't fall through.
935 if (Fallthrough == CurBB->getParent()->end())
936 return false;
937
938 // If FallthroughBlock isn't a successor of CurBB, no fallthrough is possible.
939 if (!CurBB->isSuccessor(Fallthrough))
940 return false;
941
942 // If we couldn't analyze the branch, examine the last instruction.
943 // If the block doesn't end in a known control barrier, assume fallthrough
944 // is possible. The isPredicable check is needed because this code can be
945 // called during IfConversion, where an instruction which is normally a
946 // Barrier is predicated and thus no longer an actual control barrier. This
947 // is over-conservative though, because if an instruction isn't actually
948 // predicated we could still treat it like a barrier.
949 if (BranchUnAnalyzable)
950 return CurBB->empty() || !CurBB->back().getDesc().isBarrier() ||
951 CurBB->back().getDesc().isPredicable();
952
953 // If there is no branch, control always falls through.
954 if (TBB == 0) return true;
955
956 // If there is some explicit branch to the fallthrough block, it can obviously
957 // reach, even though the branch should get folded to fall through implicitly.
958 if (MachineFunction::iterator(TBB) == Fallthrough ||
959 MachineFunction::iterator(FBB) == Fallthrough)
960 return true;
961
962 // If it's an unconditional branch to some block not the fall through, it
963 // doesn't fall through.
964 if (Cond.empty()) return false;
965
966 // Otherwise, if it is conditional and has no explicit false block, it falls
967 // through.
968 return FBB == 0;
969 }
970
971 /// CanFallThrough - Return true if the specified can implicitly transfer
972 /// control to the block after it by falling off the end of it. This should
973 /// return false if it can reach the block after it, but it uses an explicit
974 /// branch to do so (e.g. a table jump).
975 ///
976 /// True is a conservative answer.
977 ///
978 bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) {
979 MachineBasicBlock *TBB = 0, *FBB = 0;
980 SmallVector Cond;
981 bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond, true);
982 return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond);
983 }
984
985908 /// IsBetterFallthrough - Return true if it would be clearly better to
986909 /// fall-through to MBB1 than to fall through into MBB2. This has to return
987910 /// a strict ordering, returning true for both (MBB1,MBB2) and (MBB2,MBB1) will
1002925 MachineInstr *MBB1I = --MBB1->end();
1003926 MachineInstr *MBB2I = --MBB2->end();
1004927 return MBB2I->getDesc().isCall() && !MBB1I->getDesc().isCall();
1005 }
1006
1007 /// TailDuplicateBlocks - Look for small blocks that are unconditionally
1008 /// branched to and do not fall through. Tail-duplicate their instructions
1009 /// into their predecessors to eliminate (dynamic) branches.
1010 bool BranchFolder::TailDuplicateBlocks(MachineFunction &MF) {
1011 bool MadeChange = false;
1012
1013 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
1014 MachineBasicBlock *MBB = I++;
1015
1016 // Only duplicate blocks that end with unconditional branches.
1017 if (CanFallThrough(MBB))
1018 continue;
1019
1020 MadeChange |= TailDuplicate(MBB, MF);
1021
1022 // If it is dead, remove it.
1023 if (MBB->pred_empty()) {
1024 NumInstrDups -= MBB->size();
1025 RemoveDeadBlock(MBB);
1026 MadeChange = true;
1027 ++NumDeadBlocks;
1028 }
1029 }
1030 return MadeChange;
1031 }
1032
1033 /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each
1034 /// of its predecessors.
1035 bool BranchFolder::TailDuplicate(MachineBasicBlock *TailBB,
1036 MachineFunction &MF) {
1037 // Don't try to tail-duplicate single-block loops.
1038 if (TailBB->isSuccessor(TailBB))
1039 return false;
1040
1041 // Set the limit on the number of instructions to duplicate, with a default
1042 // of one less than the tail-merge threshold. When optimizing for size,
1043 // duplicate only one, because one branch instruction can be eliminated to
1044 // compensate for the duplication.
1045 unsigned MaxDuplicateCount;
1046 if (MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
1047 MaxDuplicateCount = 1;
1048 else if (TII->isProfitableToDuplicateIndirectBranch() &&
1049 !TailBB->empty() && TailBB->back().getDesc().isIndirectBranch())
1050 // If the target has hardware branch prediction that can handle indirect
1051 // branches, duplicating them can often make them predictable when there
1052 // are common paths through the code. The limit needs to be high enough
1053 // to allow undoing the effects of tail merging.
1054 MaxDuplicateCount = 20;
1055 else
1056 MaxDuplicateCount = TailMergeSize - 1;
1057
1058 // Check the instructions in the block to determine whether tail-duplication
1059 // is invalid or unlikely to be profitable.
1060 unsigned i = 0;
1061 bool HasCall = false;
1062 for (MachineBasicBlock::iterator I = TailBB->begin();
1063 I != TailBB->end(); ++I, ++i) {
1064 // Non-duplicable things shouldn't be tail-duplicated.
1065 if (I->getDesc().isNotDuplicable()) return false;
1066 // Don't duplicate more than the threshold.
1067 if (i == MaxDuplicateCount) return false;
1068 // Remember if we saw a call.
1069 if (I->getDesc().isCall()) HasCall = true;
1070 }
1071 // Heuristically, don't tail-duplicate calls if it would expand code size,
1072 // as it's less likely to be worth the extra cost.
1073 if (i > 1 && HasCall)
1074 return false;
1075
1076 // Iterate through all the unique predecessors and tail-duplicate this
1077 // block into them, if possible. Copying the list ahead of time also
1078 // avoids trouble with the predecessor list reallocating.
1079 bool Changed = false;
1080 SmallSetVector Preds(TailBB->pred_begin(),
1081 TailBB->pred_end());
1082 for (SmallSetVector::iterator PI = Preds.begin(),
1083 PE = Preds.end(); PI != PE; ++PI) {
1084 MachineBasicBlock *PredBB = *PI;
1085
1086 assert(TailBB != PredBB &&
1087 "Single-block loop should have been rejected earlier!");
1088 if (PredBB->succ_size() > 1) continue;
1089
1090 MachineBasicBlock *PredTBB, *PredFBB;
1091 SmallVector PredCond;
1092 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
1093 continue;
1094 if (!PredCond.empty())
1095 continue;
1096 // EH edges are ignored by AnalyzeBranch.
1097 if (PredBB->succ_size() != 1)
1098 continue;
1099 // Don't duplicate into a fall-through predecessor (at least for now).
1100 if (PredBB->isLayoutSuccessor(TailBB) && CanFallThrough(PredBB))
1101 continue;
1102
1103 DEBUG(errs() << "\nTail-duplicating into PredBB: " << *PredBB
1104 << "From Succ: " << *TailBB);
1105
1106 // Remove PredBB's unconditional branch.
1107 TII->RemoveBranch(*PredBB);
1108 // Clone the contents of TailBB into PredBB.
1109 for (MachineBasicBlock::iterator I = TailBB->begin(), E = TailBB->end();
1110 I != E; ++I) {
1111 MachineInstr *NewMI = MF.CloneMachineInstr(I);
1112 PredBB->insert(PredBB->end(), NewMI);
1113 }
1114 NumInstrDups += TailBB->size() - 1; // subtract one for removed branch
1115
1116 // Update the CFG.
1117 PredBB->removeSuccessor(PredBB->succ_begin());
1118 assert(PredBB->succ_empty() &&
1119 "TailDuplicate called on block with multiple successors!");
1120 for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(),
1121 E = TailBB->succ_end(); I != E; ++I)
1122 PredBB->addSuccessor(*I);
1123
1124 Changed = true;
1125 ++NumTailDups;
1126 }
1127
1128 // If TailBB was duplicated into all its predecessors except for the prior
1129 // block, which falls through unconditionally, move the contents of this
1130 // block into the prior block.
1131 MachineBasicBlock &PrevBB = *prior(MachineFunction::iterator(TailBB));
1132 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
1133 SmallVector PriorCond;
1134 bool PriorUnAnalyzable =
1135 TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true);
1136 // This has to check PrevBB->succ_size() because EH edges are ignored by
1137 // AnalyzeBranch.
1138 if (!PriorUnAnalyzable && PriorCond.empty() && !PriorTBB &&
1139 TailBB->pred_size() == 1 && PrevBB.succ_size() == 1 &&
1140 !TailBB->hasAddressTaken()) {
1141 DEBUG(errs() << "\nMerging into block: " << PrevBB
1142 << "From MBB: " << *TailBB);
1143 PrevBB.splice(PrevBB.end(), TailBB, TailBB->begin(), TailBB->end());
1144 PrevBB.removeSuccessor(PrevBB.succ_begin());;
1145 assert(PrevBB.succ_empty());
1146 PrevBB.transferSuccessors(TailBB);
1147 Changed = true;
1148 }
1149
1150 return Changed;
1151928 }
1152929
1153930 /// OptimizeBlock - Analyze and optimize control flow related to the specified
12741051 // the assert condition out of the loop body.
12751052 if (MBB->succ_empty() && !PriorCond.empty() && PriorFBB == 0 &&
12761053 MachineFunction::iterator(PriorTBB) == FallThrough &&
1277 !CanFallThrough(MBB)) {
1054 !MBB->canFallThrough()) {
12781055 bool DoTransform = true;
12791056
12801057 // We have to be careful that the succs of PredBB aren't both no-successor
12981075 // In this case, we could actually be moving the return block *into* a
12991076 // loop!
13001077 if (DoTransform && !MBB->succ_empty() &&
1301 (!CanFallThrough(PriorTBB) || PriorTBB->empty()))
1078 (!PriorTBB->canFallThrough() || PriorTBB->empty()))
13021079 DoTransform = false;
13031080
13041081
14301207 // If the prior block doesn't fall through into this block, and if this
14311208 // block doesn't fall through into some other block, see if we can find a
14321209 // place to move this block where a fall-through will happen.
1433 if (!CanFallThrough(&PrevBB, PriorUnAnalyzable,
1434 PriorTBB, PriorFBB, PriorCond)) {
1210 if (!PrevBB.canFallThrough()) {
14351211
14361212 // Now we know that there was no fall-through into this block, check to
14371213 // see if it has a fall-through into its successor.
1438 bool CurFallsThru = CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB,
1439 CurCond);
1214 bool CurFallsThru = MBB->canFallThrough();
14401215
14411216 if (!MBB->isLandingPad()) {
14421217 // Check all the predecessors of this block. If one of them has no fall
14481223 MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough;
14491224 MachineBasicBlock *PredTBB, *PredFBB;
14501225 SmallVector PredCond;
1451 if (PredBB != MBB && !CanFallThrough(PredBB) &&
1226 if (PredBB != MBB && !PredBB->canFallThrough() &&
14521227 !TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)
14531228 && (!CurFallsThru || !CurTBB || !CurFBB)
14541229 && (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) {
14871262 // and if the successor isn't an EH destination, we can arrange for the
14881263 // fallthrough to happen.
14891264 if (SuccBB != MBB && &*SuccPrev != MBB &&
1490 !CanFallThrough(SuccPrev) && !CurUnAnalyzable &&
1265 !SuccPrev->canFallThrough() && !CurUnAnalyzable &&
14911266 !SuccBB->isLandingPad()) {
14921267 MBB->moveBefore(SuccBB);
14931268 MadeChange = true;
104104 unsigned CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
105105 unsigned maxCommonTailLength);
106106
107 bool TailDuplicateBlocks(MachineFunction &MF);
108 bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF);
109
110107 bool OptimizeBranches(MachineFunction &MF);
111108 bool OptimizeBlock(MachineBasicBlock *MBB);
112109 void RemoveDeadBlock(MachineBasicBlock *MBB);
113110 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
114
115 bool CanFallThrough(MachineBasicBlock *CurBB);
116 bool CanFallThrough(MachineBasicBlock *CurBB, bool BranchUnAnalyzable,
117 MachineBasicBlock *TBB, MachineBasicBlock *FBB,
118 const SmallVectorImpl &Cond);
119111 };
120112 }
121113
6262 StackProtector.cpp
6363 StackSlotColoring.cpp
6464 StrongPHIElimination.cpp
65 TailDuplication.cpp
6566 TargetInstrInfoImpl.cpp
6667 TwoAddressInstructionPass.cpp
6768 UnreachableBlockElim.cpp
3434 cl::desc("Disable Post Regalloc"));
3535 static cl::opt DisableBranchFold("disable-branch-fold", cl::Hidden,
3636 cl::desc("Disable branch folding"));
37 static cl::opt DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
38 cl::desc("Disable tail duplication"));
3739 static cl::opt DisableCodePlace("disable-code-place", cl::Hidden,
3840 cl::desc("Disable code placement"));
3941 static cl::opt DisableSSC("disable-ssc", cl::Hidden,
343345 printAndVerify(PM, "After BranchFolding");
344346 }
345347
348 // Tail duplication.
349 if (OptLevel != CodeGenOpt::None && !DisableTailDuplicate) {
350 PM.add(createTailDuplicationPass());
351 printAndVerify(PM, "After TailDuplication");
352 }
353
346354 PM.add(createGCMachineCodeAnalysisPass());
347355
348356 if (PrintGCInfo)
361361 return next(I) == MachineFunction::const_iterator(MBB);
362362 }
363363
364 bool MachineBasicBlock::canFallThrough() {
365 MachineBasicBlock *TBB = 0, *FBB = 0;
366 SmallVector Cond;
367 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
368 bool BranchUnAnalyzable = TII->AnalyzeBranch(*this, TBB, FBB, Cond, true);
369
370 MachineFunction::iterator Fallthrough = this;
371 ++Fallthrough;
372 // If FallthroughBlock is off the end of the function, it can't fall through.
373 if (Fallthrough == getParent()->end())
374 return false;
375
376 // If FallthroughBlock isn't a successor, no fallthrough is possible.
377 if (!isSuccessor(Fallthrough))
378 return false;
379
380 // If we couldn't analyze the branch, examine the last instruction.
381 // If the block doesn't end in a known control barrier, assume fallthrough
382 // is possible. The isPredicable check is needed because this code can be
383 // called during IfConversion, where an instruction which is normally a
384 // Barrier is predicated and thus no longer an actual control barrier. This
385 // is over-conservative though, because if an instruction isn't actually
386 // predicated we could still treat it like a barrier.
387 if (BranchUnAnalyzable)
388 return empty() || !back().getDesc().isBarrier() ||
389 back().getDesc().isPredicable();
390
391 // If there is no branch, control always falls through.
392 if (TBB == 0) return true;
393
394 // If there is some explicit branch to the fallthrough block, it can obviously
395 // reach, even though the branch should get folded to fall through implicitly.
396 if (MachineFunction::iterator(TBB) == Fallthrough ||
397 MachineFunction::iterator(FBB) == Fallthrough)
398 return true;
399
400 // If it's an unconditional branch to some block not the fall through, it
401 // doesn't fall through.
402 if (Cond.empty()) return false;
403
404 // Otherwise, if it is conditional and has no explicit false block, it falls
405 // through.
406 return FBB == 0;
407 }
408
364409 /// removeFromParent - This method unlinks 'this' from the containing function,
365410 /// and returns it, but does not delete it.
366411 MachineBasicBlock *MachineBasicBlock::removeFromParent() {
0 //===-- TailDuplication.cpp - Duplicate blocks into predecessors' tails ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass duplicates basic blocks ending in unconditional branches into
10 // the tails of their predecessors.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "tailduplication"
15 #include "llvm/Function.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/Statistic.h"
26 using namespace llvm;
27
28 STATISTIC(NumTailDups , "Number of tail duplicated blocks");
29 STATISTIC(NumInstrDups , "Additional instructions due to tail duplication");
30 STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
31
32 // Heuristic for tail duplication.
33 static cl::opt
34 TailDuplicateSize("tail-dup-size",
35 cl::desc("Maximum instructions to consider tail duplicating"),
36 cl::init(2), cl::Hidden);
37
38 namespace {
39 /// TailDuplicationPass - Perform tail duplication.
40 class TailDuplicationPass : public MachineFunctionPass {
41 const TargetInstrInfo *TII;
42 MachineModuleInfo *MMI;
43
44 public:
45 static char ID;
46 explicit TailDuplicationPass() : MachineFunctionPass(&ID) {}
47
48 virtual bool runOnMachineFunction(MachineFunction &MF);
49 virtual const char *getPassName() const { return "Tail Duplication"; }
50
51 private:
52 bool TailDuplicateBlocks(MachineFunction &MF);
53 bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF);
54 void RemoveDeadBlock(MachineBasicBlock *MBB);
55 };
56
57 char TailDuplicationPass::ID = 0;
58 }
59
60 FunctionPass *llvm::createTailDuplicationPass() {
61 return new TailDuplicationPass();
62 }
63
64 bool TailDuplicationPass::runOnMachineFunction(MachineFunction &MF) {
65 TII = MF.getTarget().getInstrInfo();
66 MMI = getAnalysisIfAvailable();
67
68 bool MadeChange = false;
69 bool MadeChangeThisIteration = true;
70 while (MadeChangeThisIteration) {
71 MadeChangeThisIteration = false;
72 MadeChangeThisIteration |= TailDuplicateBlocks(MF);
73 MadeChange |= MadeChangeThisIteration;
74 }
75
76 return MadeChange;
77 }
78
79 /// TailDuplicateBlocks - Look for small blocks that are unconditionally
80 /// branched to and do not fall through. Tail-duplicate their instructions
81 /// into their predecessors to eliminate (dynamic) branches.
82 bool TailDuplicationPass::TailDuplicateBlocks(MachineFunction &MF) {
83 bool MadeChange = false;
84
85 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
86 MachineBasicBlock *MBB = I++;
87
88 // Only duplicate blocks that end with unconditional branches.
89 if (MBB->canFallThrough())
90 continue;
91
92 MadeChange |= TailDuplicate(MBB, MF);
93
94 // If it is dead, remove it.
95 if (MBB->pred_empty()) {
96 NumInstrDups -= MBB->size();
97 RemoveDeadBlock(MBB);
98 MadeChange = true;
99 ++NumDeadBlocks;
100 }
101 }
102 return MadeChange;
103 }
104
105 /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each
106 /// of its predecessors.
107 bool TailDuplicationPass::TailDuplicate(MachineBasicBlock *TailBB,
108 MachineFunction &MF) {
109 // Don't try to tail-duplicate single-block loops.
110 if (TailBB->isSuccessor(TailBB))
111 return false;
112
113 // Set the limit on the number of instructions to duplicate, with a default
114 // of one less than the tail-merge threshold. When optimizing for size,
115 // duplicate only one, because one branch instruction can be eliminated to
116 // compensate for the duplication.
117 unsigned MaxDuplicateCount;
118 if (MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
119 MaxDuplicateCount = 1;
120 else if (TII->isProfitableToDuplicateIndirectBranch() &&
121 !TailBB->empty() && TailBB->back().getDesc().isIndirectBranch())
122 // If the target has hardware branch prediction that can handle indirect
123 // branches, duplicating them can often make them predictable when there
124 // are common paths through the code. The limit needs to be high enough
125 // to allow undoing the effects of tail merging.
126 MaxDuplicateCount = 20;
127 else
128 MaxDuplicateCount = TailDuplicateSize;
129
130 // Check the instructions in the block to determine whether tail-duplication
131 // is invalid or unlikely to be profitable.
132 unsigned i = 0;
133 bool HasCall = false;
134 for (MachineBasicBlock::iterator I = TailBB->begin();
135 I != TailBB->end(); ++I, ++i) {
136 // Non-duplicable things shouldn't be tail-duplicated.
137 if (I->getDesc().isNotDuplicable()) return false;
138 // Don't duplicate more than the threshold.
139 if (i == MaxDuplicateCount) return false;
140 // Remember if we saw a call.
141 if (I->getDesc().isCall()) HasCall = true;
142 }
143 // Heuristically, don't tail-duplicate calls if it would expand code size,
144 // as it's less likely to be worth the extra cost.
145 if (i > 1 && HasCall)
146 return false;
147
148 // Iterate through all the unique predecessors and tail-duplicate this
149 // block into them, if possible. Copying the list ahead of time also
150 // avoids trouble with the predecessor list reallocating.
151 bool Changed = false;
152 SmallSetVector Preds(TailBB->pred_begin(),
153 TailBB->pred_end());
154 for (SmallSetVector::iterator PI = Preds.begin(),
155 PE = Preds.end(); PI != PE; ++PI) {
156 MachineBasicBlock *PredBB = *PI;
157
158 assert(TailBB != PredBB &&
159 "Single-block loop should have been rejected earlier!");
160 if (PredBB->succ_size() > 1) continue;
161
162 MachineBasicBlock *PredTBB, *PredFBB;
163 SmallVector PredCond;
164 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
165 continue;
166 if (!PredCond.empty())
167 continue;
168 // EH edges are ignored by AnalyzeBranch.
169 if (PredBB->succ_size() != 1)
170 continue;
171 // Don't duplicate into a fall-through predecessor (at least for now).
172 if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
173 continue;
174
175 DEBUG(errs() << "\nTail-duplicating into PredBB: " << *PredBB
176 << "From Succ: " << *TailBB);
177
178 // Remove PredBB's unconditional branch.
179 TII->RemoveBranch(*PredBB);
180 // Clone the contents of TailBB into PredBB.
181 for (MachineBasicBlock::iterator I = TailBB->begin(), E = TailBB->end();
182 I != E; ++I) {
183 MachineInstr *NewMI = MF.CloneMachineInstr(I);
184 PredBB->insert(PredBB->end(), NewMI);
185 }
186 NumInstrDups += TailBB->size() - 1; // subtract one for removed branch
187
188 // Update the CFG.
189 PredBB->removeSuccessor(PredBB->succ_begin());
190 assert(PredBB->succ_empty() &&
191 "TailDuplicate called on block with multiple successors!");
192 for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(),
193 E = TailBB->succ_end(); I != E; ++I)
194 PredBB->addSuccessor(*I);
195
196 Changed = true;
197 ++NumTailDups;
198 }
199
200 // If TailBB was duplicated into all its predecessors except for the prior
201 // block, which falls through unconditionally, move the contents of this
202 // block into the prior block.
203 MachineBasicBlock &PrevBB = *prior(MachineFunction::iterator(TailBB));
204 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
205 SmallVector PriorCond;
206 bool PriorUnAnalyzable =
207 TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true);
208 // This has to check PrevBB->succ_size() because EH edges are ignored by
209 // AnalyzeBranch.
210 if (!PriorUnAnalyzable && PriorCond.empty() && !PriorTBB &&
211 TailBB->pred_size() == 1 && PrevBB.succ_size() == 1 &&
212 !TailBB->hasAddressTaken()) {
213 DEBUG(errs() << "\nMerging into block: " << PrevBB
214 << "From MBB: " << *TailBB);
215 PrevBB.splice(PrevBB.end(), TailBB, TailBB->begin(), TailBB->end());
216 PrevBB.removeSuccessor(PrevBB.succ_begin());;
217 assert(PrevBB.succ_empty());
218 PrevBB.transferSuccessors(TailBB);
219 Changed = true;
220 }
221
222 return Changed;
223 }
224
225 /// RemoveDeadBlock - Remove the specified dead machine basic block from the
226 /// function, updating the CFG.
227 void TailDuplicationPass::RemoveDeadBlock(MachineBasicBlock *MBB) {
228 assert(MBB->pred_empty() && "MBB must be dead!");
229 DEBUG(errs() << "\nRemoving MBB: " << *MBB);
230
231 // Remove all successors.
232 while (!MBB->succ_empty())
233 MBB->removeSuccessor(MBB->succ_end()-1);
234
235 // If there are any labels in the basic block, unregister them from
236 // MachineModuleInfo.
237 if (MMI && !MBB->empty()) {
238 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
239 I != E; ++I) {
240 if (I->isLabel())
241 // The label ID # is always operand #0, an immediate.
242 MMI->InvalidateLabel(I->getOperand(0).getImm());
243 }
244 }
245
246 // Remove the block.
247 MBB->eraseFromParent();
248 }
249