1 //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is used to ensure that functions have at most one return
11 // instruction in them. Additionally, it keeps track of which node is the new
12 // exit node of the CFG. If there are no exit nodes in the CFG, the getExitNode
13 // method will return a null pointer.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Type.h"
25 static RegisterOpt
<UnifyFunctionExitNodes
>
26 X("mergereturn", "Unify function exit nodes");
28 int UnifyFunctionExitNodes::stub
;
30 Pass
*llvm::createUnifyFunctionExitNodesPass() {
31 return new UnifyFunctionExitNodes();
34 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage
&AU
) const{
35 // We preserve the non-critical-edgeness property
36 AU
.addPreservedID(BreakCriticalEdgesID
);
37 // This is a cluster of orthogonal Transforms
38 AU
.addPreservedID(PromoteMemoryToRegisterID
);
39 AU
.addPreservedID(LowerSelectID
);
40 AU
.addPreservedID(LowerSwitchID
);
43 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
44 // BasicBlock, and converting all returns to unconditional branches to this
45 // new basic block. The singular exit node is returned.
47 // If there are no return stmts in the Function, a null pointer is returned.
49 bool UnifyFunctionExitNodes::runOnFunction(Function
&F
) {
50 // Loop over all of the blocks in a function, tracking all of the blocks that
53 std::vector
<BasicBlock
*> ReturningBlocks
;
54 std::vector
<BasicBlock
*> UnwindingBlocks
;
55 std::vector
<BasicBlock
*> UnreachableBlocks
;
56 for(Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
)
57 if (isa
<ReturnInst
>(I
->getTerminator()))
58 ReturningBlocks
.push_back(I
);
59 else if (isa
<UnwindInst
>(I
->getTerminator()))
60 UnwindingBlocks
.push_back(I
);
61 else if (isa
<UnreachableInst
>(I
->getTerminator()))
62 UnreachableBlocks
.push_back(I
);
64 // Handle unwinding blocks first.
65 if (UnwindingBlocks
.empty()) {
67 } else if (UnwindingBlocks
.size() == 1) {
68 UnwindBlock
= UnwindingBlocks
.front();
70 UnwindBlock
= new BasicBlock("UnifiedUnwindBlock", &F
);
71 new UnwindInst(UnwindBlock
);
73 for (std::vector
<BasicBlock
*>::iterator I
= UnwindingBlocks
.begin(),
74 E
= UnwindingBlocks
.end(); I
!= E
; ++I
) {
76 BB
->getInstList().pop_back(); // Remove the unwind insn
77 new BranchInst(UnwindBlock
, BB
);
81 // Then unreachable blocks.
82 if (UnreachableBlocks
.empty()) {
84 } else if (UnreachableBlocks
.size() == 1) {
85 UnreachableBlock
= UnreachableBlocks
.front();
87 UnreachableBlock
= new BasicBlock("UnifiedUnreachableBlock", &F
);
88 new UnreachableInst(UnreachableBlock
);
90 for (std::vector
<BasicBlock
*>::iterator I
= UnreachableBlocks
.begin(),
91 E
= UnreachableBlocks
.end(); I
!= E
; ++I
) {
93 BB
->getInstList().pop_back(); // Remove the unreachable inst.
94 new BranchInst(UnreachableBlock
, BB
);
98 // Now handle return blocks.
99 if (ReturningBlocks
.empty()) {
101 return false; // No blocks return
102 } else if (ReturningBlocks
.size() == 1) {
103 ReturnBlock
= ReturningBlocks
.front(); // Already has a single return block
107 // Otherwise, we need to insert a new basic block into the function, add a PHI
108 // node (if the function returns a value), and convert all of the return
109 // instructions into unconditional branches.
111 BasicBlock
*NewRetBlock
= new BasicBlock("UnifiedReturnBlock", &F
);
114 if (F
.getReturnType() != Type::VoidTy
) {
115 // If the function doesn't return void... add a PHI node to the block...
116 PN
= new PHINode(F
.getReturnType(), "UnifiedRetVal");
117 NewRetBlock
->getInstList().push_back(PN
);
119 new ReturnInst(PN
, NewRetBlock
);
121 // Loop over all of the blocks, replacing the return instruction with an
122 // unconditional branch.
124 for (std::vector
<BasicBlock
*>::iterator I
= ReturningBlocks
.begin(),
125 E
= ReturningBlocks
.end(); I
!= E
; ++I
) {
128 // Add an incoming element to the PHI node for every return instruction that
129 // is merging into this new block...
130 if (PN
) PN
->addIncoming(BB
->getTerminator()->getOperand(0), BB
);
132 BB
->getInstList().pop_back(); // Remove the return insn
133 new BranchInst(NewRetBlock
, BB
);
135 ReturnBlock
= NewRetBlock
;