1 //===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
3 // This pass is used to ensure that functions have at most one return
4 // instruction in them. Additionally, it keeps track of which node is the new
5 // exit node of the CFG. If there are no exit nodes in the CFG, the getExitNode
6 // method will return a null pointer.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
11 #include "llvm/BasicBlock.h"
12 #include "llvm/Function.h"
13 #include "llvm/iTerminators.h"
14 #include "llvm/iPHINode.h"
15 #include "llvm/Type.h"
18 AnalysisID UnifyFunctionExitNodes::ID(AnalysisID::create<UnifyFunctionExitNodes>());
20 static RegisterOpt<UnifyFunctionExitNodes>
21 X("mergereturn", "Unify function exit nodes");
23 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
24 // BasicBlock, and converting all returns to unconditional branches to this
25 // new basic block. The singular exit node is returned.
27 // If there are no return stmts in the Function, a null pointer is returned.
29 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
30 // Loop over all of the blocks in a function, tracking all of the blocks that
33 vector<BasicBlock*> ReturningBlocks;
34 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
35 if (isa<ReturnInst>(I->getTerminator()))
36 ReturningBlocks.push_back(I);
38 if (ReturningBlocks.empty()) {
40 return false; // No blocks return
41 } else if (ReturningBlocks.size() == 1) {
42 ExitNode = ReturningBlocks.front(); // Already has a single return block
46 // Otherwise, we need to insert a new basic block into the function, add a PHI
47 // node (if the function returns a value), and convert all of the return
48 // instructions into unconditional branches.
50 BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
52 if (F.getReturnType() != Type::VoidTy) {
53 // If the function doesn't return void... add a PHI node to the block...
54 PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
55 NewRetBlock->getInstList().push_back(PN);
57 // Add an incoming element to the PHI node for every return instruction that
58 // is merging into this new block...
59 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
60 E = ReturningBlocks.end(); I != E; ++I)
61 PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
63 // Add a return instruction to return the result of the PHI node...
64 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
66 // If it returns void, just add a return void instruction to the block
67 NewRetBlock->getInstList().push_back(new ReturnInst());
70 // Loop over all of the blocks, replacing the return instruction with an
71 // unconditional branch.
73 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
74 E = ReturningBlocks.end(); I != E; ++I) {
75 (*I)->getInstList().pop_back(); // Remove the return insn
76 (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
78 ExitNode = NewRetBlock;