blob: 5d151ed70a0aae1ba0073be54c7f353b96ad5b83 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15//===----------------------------------------------------------------------===//
16
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"
23using namespace llvm;
24
25char UnifyFunctionExitNodes::ID = 0;
26static RegisterPass<UnifyFunctionExitNodes>
27X("mergereturn", "Unify function exit nodes");
28
29int UnifyFunctionExitNodes::stub;
30
31Pass *llvm::createUnifyFunctionExitNodesPass() {
32 return new UnifyFunctionExitNodes();
33}
34
35void 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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 AU.addPreservedID(LowerSwitchID);
41}
42
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.
46//
47// If there are no return stmts in the Function, a null pointer is returned.
48//
49bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
50 // Loop over all of the blocks in a function, tracking all of the blocks that
51 // return.
52 //
Devang Patel4bba6c22008-03-05 00:36:59 +000053
54 // PHINode can not handle aggregates returned by multiple value ret
55 // instructions. TODO: Handle each return value independently.
56 if (isa<StructType>(F.getReturnType()))
57 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 std::vector<BasicBlock*> ReturningBlocks;
59 std::vector<BasicBlock*> UnwindingBlocks;
60 std::vector<BasicBlock*> UnreachableBlocks;
61 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
62 if (isa<ReturnInst>(I->getTerminator()))
63 ReturningBlocks.push_back(I);
64 else if (isa<UnwindInst>(I->getTerminator()))
65 UnwindingBlocks.push_back(I);
66 else if (isa<UnreachableInst>(I->getTerminator()))
67 UnreachableBlocks.push_back(I);
68
69 // Handle unwinding blocks first.
70 if (UnwindingBlocks.empty()) {
71 UnwindBlock = 0;
72 } else if (UnwindingBlocks.size() == 1) {
73 UnwindBlock = UnwindingBlocks.front();
74 } else {
75 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
76 new UnwindInst(UnwindBlock);
77
78 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
79 E = UnwindingBlocks.end(); I != E; ++I) {
80 BasicBlock *BB = *I;
81 BB->getInstList().pop_back(); // Remove the unwind insn
82 new BranchInst(UnwindBlock, BB);
83 }
84 }
85
86 // Then unreachable blocks.
87 if (UnreachableBlocks.empty()) {
88 UnreachableBlock = 0;
89 } else if (UnreachableBlocks.size() == 1) {
90 UnreachableBlock = UnreachableBlocks.front();
91 } else {
92 UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
93 new UnreachableInst(UnreachableBlock);
94
95 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
96 E = UnreachableBlocks.end(); I != E; ++I) {
97 BasicBlock *BB = *I;
98 BB->getInstList().pop_back(); // Remove the unreachable inst.
99 new BranchInst(UnreachableBlock, BB);
100 }
101 }
102
103 // Now handle return blocks.
104 if (ReturningBlocks.empty()) {
105 ReturnBlock = 0;
106 return false; // No blocks return
107 } else if (ReturningBlocks.size() == 1) {
108 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
109 return false;
110 }
111
112 // Otherwise, we need to insert a new basic block into the function, add a PHI
113 // node (if the function returns a value), and convert all of the return
114 // instructions into unconditional branches.
115 //
116 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
117
118 PHINode *PN = 0;
119 if (F.getReturnType() != Type::VoidTy) {
120 // If the function doesn't return void... add a PHI node to the block...
121 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
122 NewRetBlock->getInstList().push_back(PN);
123 }
124 new ReturnInst(PN, NewRetBlock);
125
126 // Loop over all of the blocks, replacing the return instruction with an
127 // unconditional branch.
128 //
129 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
130 E = ReturningBlocks.end(); I != E; ++I) {
131 BasicBlock *BB = *I;
132
133 // Add an incoming element to the PHI node for every return instruction that
134 // is merging into this new block...
135 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
136
137 BB->getInstList().pop_back(); // Remove the return insn
138 new BranchInst(NewRetBlock, BB);
139 }
140 ReturnBlock = NewRetBlock;
141 return true;
142}