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 char UnifyFunctionExitNodes::ID = 0;
26 static RegisterPass<UnifyFunctionExitNodes>
27 X("mergereturn", "Unify function exit nodes");
29 int UnifyFunctionExitNodes::stub;
31 Pass *llvm::createUnifyFunctionExitNodesPass() {
32 return new UnifyFunctionExitNodes();
35 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
36 // We preserve the non-critical-edgeness property
37 AU.addPreservedID(BreakCriticalEdgesID);
38 // This is a cluster of orthogonal Transforms
39 AU.addPreservedID(PromoteMemoryToRegisterID);
40 AU.addPreservedID(LowerSelectID);
41 AU.addPreservedID(LowerSwitchID);
44 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
45 // BasicBlock, and converting all returns to unconditional branches to this
46 // new basic block. The singular exit node is returned.
48 // If there are no return stmts in the Function, a null pointer is returned.
50 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
51 // Loop over all of the blocks in a function, tracking all of the blocks that
54 std::vector<BasicBlock*> ReturningBlocks;
55 std::vector<BasicBlock*> UnwindingBlocks;
56 std::vector<BasicBlock*> UnreachableBlocks;
57 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
58 if (isa<ReturnInst>(I->getTerminator()))
59 ReturningBlocks.push_back(I);
60 else if (isa<UnwindInst>(I->getTerminator()))
61 UnwindingBlocks.push_back(I);
62 else if (isa<UnreachableInst>(I->getTerminator()))
63 UnreachableBlocks.push_back(I);
65 // Handle unwinding blocks first.
66 if (UnwindingBlocks.empty()) {
68 } else if (UnwindingBlocks.size() == 1) {
69 UnwindBlock = UnwindingBlocks.front();
71 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
72 new UnwindInst(UnwindBlock);
74 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
75 E = UnwindingBlocks.end(); I != E; ++I) {
77 BB->getInstList().pop_back(); // Remove the unwind insn
78 new BranchInst(UnwindBlock, BB);
82 // Then unreachable blocks.
83 if (UnreachableBlocks.empty()) {
85 } else if (UnreachableBlocks.size() == 1) {
86 UnreachableBlock = UnreachableBlocks.front();
88 UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
89 new UnreachableInst(UnreachableBlock);
91 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
92 E = UnreachableBlocks.end(); I != E; ++I) {
94 BB->getInstList().pop_back(); // Remove the unreachable inst.
95 new BranchInst(UnreachableBlock, BB);
99 // Now handle return blocks.
100 if (ReturningBlocks.empty()) {
102 return false; // No blocks return
103 } else if (ReturningBlocks.size() == 1) {
104 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
108 // Otherwise, we need to insert a new basic block into the function, add a PHI
109 // node (if the function returns a value), and convert all of the return
110 // instructions into unconditional branches.
112 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
115 if (F.getReturnType() != Type::VoidTy) {
116 // If the function doesn't return void... add a PHI node to the block...
117 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
118 NewRetBlock->getInstList().push_back(PN);
120 new ReturnInst(PN, NewRetBlock);
122 // Loop over all of the blocks, replacing the return instruction with an
123 // unconditional branch.
125 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
126 E = ReturningBlocks.end(); I != E; ++I) {
129 // Add an incoming element to the PHI node for every return instruction that
130 // is merging into this new block...
131 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
133 BB->getInstList().pop_back(); // Remove the return insn
134 new BranchInst(NewRetBlock, BB);
136 ReturnBlock = NewRetBlock;