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 static RegisterOpt<UnifyFunctionExitNodes>
19 X("mergereturn", "Unify function exit nodes");
20 AnalysisID UnifyFunctionExitNodes::ID = X;
22 // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
23 // BasicBlock, and converting all returns to unconditional branches to this
24 // new basic block. The singular exit node is returned.
26 // If there are no return stmts in the Function, a null pointer is returned.
28 bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
29 // Loop over all of the blocks in a function, tracking all of the blocks that
32 vector<BasicBlock*> ReturningBlocks;
33 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
34 if (isa<ReturnInst>(I->getTerminator()))
35 ReturningBlocks.push_back(I);
37 if (ReturningBlocks.empty()) {
39 return false; // No blocks return
40 } else if (ReturningBlocks.size() == 1) {
41 ExitNode = ReturningBlocks.front(); // Already has a single return block
45 // Otherwise, we need to insert a new basic block into the function, add a PHI
46 // node (if the function returns a value), and convert all of the return
47 // instructions into unconditional branches.
49 BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
51 if (F.getReturnType() != Type::VoidTy) {
52 // If the function doesn't return void... add a PHI node to the block...
53 PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
54 NewRetBlock->getInstList().push_back(PN);
56 // Add an incoming element to the PHI node for every return instruction that
57 // is merging into this new block...
58 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
59 E = ReturningBlocks.end(); I != E; ++I)
60 PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
62 // Add a return instruction to return the result of the PHI node...
63 NewRetBlock->getInstList().push_back(new ReturnInst(PN));
65 // If it returns void, just add a return void instruction to the block
66 NewRetBlock->getInstList().push_back(new ReturnInst());
69 // Loop over all of the blocks, replacing the return instruction with an
70 // unconditional branch.
72 for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
73 E = ReturningBlocks.end(); I != E; ++I) {
74 (*I)->getInstList().pop_back(); // Remove the return insn
75 (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
77 ExitNode = NewRetBlock;