blob: ba58fbd987a66824590bcc55677af6f8680a7f17 [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"
Devang Patel40a92a12008-03-05 21:50:24 +000023#include "llvm/ADT/SmallVector.h"
Devang Pateld91d3842008-03-10 18:11:41 +000024#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025using namespace llvm;
26
27char UnifyFunctionExitNodes::ID = 0;
28static RegisterPass<UnifyFunctionExitNodes>
29X("mergereturn", "Unify function exit nodes");
30
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031Pass *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 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 std::vector<BasicBlock*> ReturningBlocks;
54 std::vector<BasicBlock*> UnwindingBlocks;
55 std::vector<BasicBlock*> UnreachableBlocks;
56 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
57 if (isa<ReturnInst>(I->getTerminator()))
58 ReturningBlocks.push_back(I);
59 else if (isa<UnwindInst>(I->getTerminator()))
60 UnwindingBlocks.push_back(I);
61 else if (isa<UnreachableInst>(I->getTerminator()))
62 UnreachableBlocks.push_back(I);
63
64 // Handle unwinding blocks first.
65 if (UnwindingBlocks.empty()) {
66 UnwindBlock = 0;
67 } else if (UnwindingBlocks.size() == 1) {
68 UnwindBlock = UnwindingBlocks.front();
69 } else {
Gabor Greifd6da1d02008-04-06 20:25:17 +000070 UnwindBlock = BasicBlock::Create("UnifiedUnwindBlock", &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 new UnwindInst(UnwindBlock);
72
73 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
74 E = UnwindingBlocks.end(); I != E; ++I) {
75 BasicBlock *BB = *I;
76 BB->getInstList().pop_back(); // Remove the unwind insn
Gabor Greifd6da1d02008-04-06 20:25:17 +000077 BranchInst::Create(UnwindBlock, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 }
79 }
80
81 // Then unreachable blocks.
82 if (UnreachableBlocks.empty()) {
83 UnreachableBlock = 0;
84 } else if (UnreachableBlocks.size() == 1) {
85 UnreachableBlock = UnreachableBlocks.front();
86 } else {
Gabor Greifd6da1d02008-04-06 20:25:17 +000087 UnreachableBlock = BasicBlock::Create("UnifiedUnreachableBlock", &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 new UnreachableInst(UnreachableBlock);
89
90 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
91 E = UnreachableBlocks.end(); I != E; ++I) {
92 BasicBlock *BB = *I;
93 BB->getInstList().pop_back(); // Remove the unreachable inst.
Gabor Greifd6da1d02008-04-06 20:25:17 +000094 BranchInst::Create(UnreachableBlock, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 }
96 }
97
98 // Now handle return blocks.
99 if (ReturningBlocks.empty()) {
100 ReturnBlock = 0;
101 return false; // No blocks return
102 } else if (ReturningBlocks.size() == 1) {
103 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
104 return false;
105 }
106
107 // Otherwise, we need to insert a new basic block into the function, add a PHI
Devang Patel40a92a12008-03-05 21:50:24 +0000108 // nodes (if the function returns values), and convert all of the return
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // instructions into unconditional branches.
110 //
Gabor Greifd6da1d02008-04-06 20:25:17 +0000111 BasicBlock *NewRetBlock = BasicBlock::Create("UnifiedReturnBlock", &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
Dan Gohman29474e92008-07-23 00:34:11 +0000113 PHINode *PN = 0;
114 if (F.getReturnType() == Type::VoidTy) {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000115 ReturnInst::Create(NULL, NewRetBlock);
Dan Gohman29474e92008-07-23 00:34:11 +0000116 } else {
Devang Patel40a92a12008-03-05 21:50:24 +0000117 // If the function doesn't return void... add a PHI node to the block...
Dan Gohman29474e92008-07-23 00:34:11 +0000118 PN = PHINode::Create(F.getReturnType(), "UnifiedRetVal");
Devang Patel40a92a12008-03-05 21:50:24 +0000119 NewRetBlock->getInstList().push_back(PN);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000120 ReturnInst::Create(PN, NewRetBlock);
Devang Patel40a92a12008-03-05 21:50:24 +0000121 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123 // Loop over all of the blocks, replacing the return instruction with an
124 // unconditional branch.
125 //
126 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
127 E = ReturningBlocks.end(); I != E; ++I) {
128 BasicBlock *BB = *I;
129
130 // Add an incoming element to the PHI node for every return instruction that
131 // is merging into this new block...
Dan Gohman29474e92008-07-23 00:34:11 +0000132 if (PN)
133 PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134
135 BB->getInstList().pop_back(); // Remove the return insn
Gabor Greifd6da1d02008-04-06 20:25:17 +0000136 BranchInst::Create(NewRetBlock, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 }
138 ReturnBlock = NewRetBlock;
139 return true;
140}