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/iTerminators.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/Type.h"
25 static RegisterOpt<UnifyFunctionExitNodes>
26 X("mergereturn", "Unify function exit nodes");
28 Pass *createUnifyFunctionExitNodesPass() {
29 return new UnifyFunctionExitNodes();
32 void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
33 // We preserve the non-critical-edgeness property
34 AU.addPreservedID(BreakCriticalEdgesID);
37 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
38 // BasicBlock, and converting all returns to unconditional branches to this
39 // new basic block. The singular exit node is returned.
41 // If there are no return stmts in the Function, a null pointer is returned.
43 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
44 // Loop over all of the blocks in a function, tracking all of the blocks that
47 std::vector<BasicBlock*> ReturningBlocks;
48 std::vector<BasicBlock*> UnwindingBlocks;
49 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
50 if (isa<ReturnInst>(I->getTerminator()))
51 ReturningBlocks.push_back(I);
52 else if (isa<UnwindInst>(I->getTerminator()))
53 UnwindingBlocks.push_back(I);
55 // Handle unwinding blocks first...
56 if (UnwindingBlocks.empty()) {
58 } else if (UnwindingBlocks.size() == 1) {
59 UnwindBlock = UnwindingBlocks.front();
61 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
62 UnwindBlock->getInstList().push_back(new UnwindInst());
64 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
65 E = UnwindingBlocks.end(); I != E; ++I) {
67 BB->getInstList().pop_back(); // Remove the return insn
68 BB->getInstList().push_back(new BranchInst(UnwindBlock));
72 // Now handle return blocks...
73 if (ReturningBlocks.empty()) {
75 return false; // No blocks return
76 } else if (ReturningBlocks.size() == 1) {
77 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
81 // Otherwise, we need to insert a new basic block into the function, add a PHI
82 // node (if the function returns a value), and convert all of the return
83 // instructions into unconditional branches.
85 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
88 if (F.getReturnType() != Type::VoidTy) {
89 // If the function doesn't return void... add a PHI node to the block...
90 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
91 NewRetBlock->getInstList().push_back(PN);
92 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
94 // If it returns void, just add a return void instruction to the block
95 NewRetBlock->getInstList().push_back(new ReturnInst());
98 // Loop over all of the blocks, replacing the return instruction with an
99 // unconditional branch.
101 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
102 E = ReturningBlocks.end(); I != E; ++I) {
105 // Add an incoming element to the PHI node for every return instruction that
106 // is merging into this new block...
107 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
109 BB->getInstList().pop_back(); // Remove the return insn
110 BB->getInstList().push_back(new BranchInst(NewRetBlock));
112 ReturnBlock = NewRetBlock;