llvm.org GIT mirror llvm / e2c6d13
Teach SimplifyCFG how to simplify indirectbr instructions. - Eliminate redundant successors. - Convert an indirectbr with one successor into a direct branch. Also, generalize SimplifyCFG to be able to be run on a function entry block. It knows quite a few simplifications which are applicable to the entry block, and it only needs a few checks to avoid trouble with the entry block. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111060 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
6 changed file(s) with 97 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
117117 /// of the CFG. It returns true if a modification was made, possibly deleting
118118 /// the basic block that was pointed to.
119119 ///
120 /// WARNING: The entry node of a method may not be simplified.
121 ///
122120 bool SimplifyCFG(BasicBlock *BB, const TargetData *TD = 0);
123121
124122 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
284284 while (LocalChange) {
285285 LocalChange = false;
286286
287 // Loop over all of the basic blocks (except the first one) and remove them
288 // if they are unneeded...
287 // Loop over all of the basic blocks and remove them if they are unneeded...
289288 //
290 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
289 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
291290 if (SimplifyCFG(BBIt++, TD)) {
292291 LocalChange = true;
293292 ++NumSimpl;
489489 /// rewriting all the predecessors to branch to the successor block and return
490490 /// true. If we can't transform, return false.
491491 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
492 assert(BB != &BB->getParent()->getEntryBlock() &&
493 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
494
492495 // We can't eliminate infinite loops.
493496 BasicBlock *Succ = cast(BB->getTerminator())->getSuccessor(0);
494497 if (BB == Succ) return false;
17231723
17241724 assert(BB && BB->getParent() && "Block not embedded in function!");
17251725 assert(BB->getTerminator() && "Degenerate basic block encountered!");
1726 assert(&BB->getParent()->getEntryBlock() != BB &&
1727 "Can't Simplify entry block!");
1728
1729 // Remove basic blocks that have no predecessors... or that just have themself
1730 // as a predecessor. These are unreachable.
1731 if (pred_begin(BB) == pred_end(BB) || BB->getSinglePredecessor() == BB) {
1726
1727 // Remove basic blocks that have no predecessors (except the entry block)...
1728 // or that just have themself as a predecessor. These are unreachable.
1729 if ((pred_begin(BB) == pred_end(BB) &&
1730 &BB->getParent()->getEntryBlock() != BB) ||
1731 BB->getSinglePredecessor() == BB) {
17321732 DEBUG(dbgs() << "Removing BB: \n" << *BB);
17331733 DeleteDeadBlock(BB);
17341734 return true;
18791879 while (isa(BBI))
18801880 ++BBI;
18811881 if (BBI->isTerminator()) // Terminator is the only non-phi instruction!
1882 if (TryToSimplifyUncondBranchFromEmptyBlock(BB))
1883 return true;
1882 if (BB != &BB->getParent()->getEntryBlock())
1883 if (TryToSimplifyUncondBranchFromEmptyBlock(BB))
1884 return true;
18841885
18851886 } else { // Conditional branch
18861887 if (isValueEqualityComparison(BI)) {
20482049 }
20492050
20502051 // If this block is now dead, remove it.
2051 if (pred_begin(BB) == pred_end(BB)) {
2052 if (pred_begin(BB) == pred_end(BB) &&
2053 BB != &BB->getParent()->getEntryBlock()) {
20522054 // We know there are no successors, so just nuke the block.
20532055 M->getBasicBlockList().erase(BB);
20542056 return true;
20552057 }
2058 }
2059 } else if (IndirectBrInst *IBI = dyn_cast(BB->getTerminator())) {
2060 // Eliminate redundant destinations.
2061 SmallPtrSet Succs;
2062 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
2063 BasicBlock *Dest = IBI->getDestination(i);
2064 if (!Succs.insert(Dest)) {
2065 Dest->removePredecessor(BB);
2066 IBI->removeDestination(i);
2067 --i; --e;
2068 Changed = true;
2069 }
2070 }
2071
2072 if (IBI->getNumDestinations() == 0) {
2073 // If the indirectbr has no successors, change it to unreachable.
2074 new UnreachableInst(IBI->getContext(), IBI);
2075 IBI->eraseFromParent();
2076 Changed = true;
2077 } else if (IBI->getNumDestinations() == 1) {
2078 // If the indirectbr has one successor, change it to a direct branch.
2079 BranchInst::Create(IBI->getDestination(0), IBI);
2080 IBI->eraseFromParent();
2081 Changed = true;
20562082 }
20572083 }
20582084
20672093 // is a conditional branch, see if we can hoist any code from this block up
20682094 // into our predecessor.
20692095 pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
2070 BasicBlock *OnlyPred = *PI++;
2071 for (; PI != PE; ++PI) // Search all predecessors, see if they are all same
2072 if (*PI != OnlyPred) {
2096 BasicBlock *OnlyPred = 0;
2097 for (; PI != PE; ++PI) { // Search all predecessors, see if they are all same
2098 if (!OnlyPred)
2099 OnlyPred = *PI;
2100 else if (*PI != OnlyPred) {
20732101 OnlyPred = 0; // There are multiple different predecessors...
20742102 break;
20752103 }
2104 }
20762105
20772106 if (OnlyPred)
20782107 if (BranchInst *BI = dyn_cast(OnlyPred->getTerminator()))
21712200 /// eliminates unreachable basic blocks, and does other "peephole" optimization
21722201 /// of the CFG. It returns true if a modification was made.
21732202 ///
2174 /// WARNING: The entry node of a function may not be simplified.
2175 ///
21762203 bool llvm::SimplifyCFG(BasicBlock *BB, const TargetData *TD) {
21772204 return SimplifyCFGOpt(TD).run(BB);
21782205 }
5353 return: ; preds = %entry
5454 ret void
5555 ; CHECK: @test5
56 ; CHECK-NEXT: bb:
5756 ; CHECK-NEXT: ret void
5857 }
0 ; RUN: opt -S -simplifycfg < %s | FileCheck %s
1
2 ; SimplifyCFG should eliminate redundant indirectbr edges.
3
4 ; CHECK: indbrtest0
5 ; CHECK: indirectbr i8* %t, [label %BB0, label %BB1, label %BB2]
6 ; CHECK: %x = phi i32 [ 0, %BB0 ], [ 1, %entry ]
7
8 declare void @foo()
9 declare void @A()
10 declare void @B(i32)
11 declare void @C()
12
13 define void @indbrtest0(i8** %P, i8** %Q) {
14 entry:
15 store i8* blockaddress(@indbrtest0, %BB0), i8** %P
16 store i8* blockaddress(@indbrtest0, %BB1), i8** %P
17 store i8* blockaddress(@indbrtest0, %BB2), i8** %P
18 call void @foo()
19 %t = load i8** %Q
20 indirectbr i8* %t, [label %BB0, label %BB1, label %BB2, label %BB0, label %BB1, label %BB2]
21 BB0:
22 call void @A()
23 br label %BB1
24 BB1:
25 %x = phi i32 [ 0, %BB0 ], [ 1, %entry ], [ 1, %entry ]
26 call void @B(i32 %x)
27 ret void
28 BB2:
29 call void @C()
30 ret void
31 }
32
33 ; SimplifyCFG should convert the indirectbr into a directbr. It would be even
34 ; better if it removed the branch altogether, but simplifycfdg currently misses
35 ; that because the predecessor is the entry block.
36
37 ; CHECK: indbrtest1
38 ; CHECK: br label %BB0
39
40 define void @indbrtest1(i8** %P, i8** %Q) {
41 entry:
42 store i8* blockaddress(@indbrtest1, %BB0), i8** %P
43 call void @foo()
44 %t = load i8** %Q
45 indirectbr i8* %t, [label %BB0, label %BB0]
46 BB0:
47 call void @A()
48 ret void
49 }
50