blob: 76b565c0e22a902ce1ba53625e6b1d4e16e3f962 [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
31int UnifyFunctionExitNodes::stub;
32
33Pass *llvm::createUnifyFunctionExitNodesPass() {
34 return new UnifyFunctionExitNodes();
35}
36
37void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
38 // We preserve the non-critical-edgeness property
39 AU.addPreservedID(BreakCriticalEdgesID);
40 // This is a cluster of orthogonal Transforms
41 AU.addPreservedID(PromoteMemoryToRegisterID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 AU.addPreservedID(LowerSwitchID);
43}
44
45// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
46// BasicBlock, and converting all returns to unconditional branches to this
47// new basic block. The singular exit node is returned.
48//
49// If there are no return stmts in the Function, a null pointer is returned.
50//
51bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
52 // Loop over all of the blocks in a function, tracking all of the blocks that
53 // return.
54 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 std::vector<BasicBlock*> ReturningBlocks;
56 std::vector<BasicBlock*> UnwindingBlocks;
57 std::vector<BasicBlock*> UnreachableBlocks;
58 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
59 if (isa<ReturnInst>(I->getTerminator()))
60 ReturningBlocks.push_back(I);
61 else if (isa<UnwindInst>(I->getTerminator()))
62 UnwindingBlocks.push_back(I);
63 else if (isa<UnreachableInst>(I->getTerminator()))
64 UnreachableBlocks.push_back(I);
65
66 // Handle unwinding blocks first.
67 if (UnwindingBlocks.empty()) {
68 UnwindBlock = 0;
69 } else if (UnwindingBlocks.size() == 1) {
70 UnwindBlock = UnwindingBlocks.front();
71 } else {
Gabor Greifd6da1d02008-04-06 20:25:17 +000072 UnwindBlock = BasicBlock::Create("UnifiedUnwindBlock", &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 new UnwindInst(UnwindBlock);
74
75 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
76 E = UnwindingBlocks.end(); I != E; ++I) {
77 BasicBlock *BB = *I;
78 BB->getInstList().pop_back(); // Remove the unwind insn
Gabor Greifd6da1d02008-04-06 20:25:17 +000079 BranchInst::Create(UnwindBlock, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 }
81 }
82
83 // Then unreachable blocks.
84 if (UnreachableBlocks.empty()) {
85 UnreachableBlock = 0;
86 } else if (UnreachableBlocks.size() == 1) {
87 UnreachableBlock = UnreachableBlocks.front();
88 } else {
Gabor Greifd6da1d02008-04-06 20:25:17 +000089 UnreachableBlock = BasicBlock::Create("UnifiedUnreachableBlock", &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 new UnreachableInst(UnreachableBlock);
91
92 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
93 E = UnreachableBlocks.end(); I != E; ++I) {
94 BasicBlock *BB = *I;
95 BB->getInstList().pop_back(); // Remove the unreachable inst.
Gabor Greifd6da1d02008-04-06 20:25:17 +000096 BranchInst::Create(UnreachableBlock, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 }
98 }
99
100 // Now handle return blocks.
101 if (ReturningBlocks.empty()) {
102 ReturnBlock = 0;
103 return false; // No blocks return
104 } else if (ReturningBlocks.size() == 1) {
105 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
106 return false;
107 }
108
109 // Otherwise, we need to insert a new basic block into the function, add a PHI
Devang Patel40a92a12008-03-05 21:50:24 +0000110 // nodes (if the function returns values), and convert all of the return
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 // instructions into unconditional branches.
112 //
Gabor Greifd6da1d02008-04-06 20:25:17 +0000113 BasicBlock *NewRetBlock = BasicBlock::Create("UnifiedReturnBlock", &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
Devang Patel40a92a12008-03-05 21:50:24 +0000115 SmallVector<Value *, 4> Phis;
116 unsigned NumRetVals = ReturningBlocks[0]->getTerminator()->getNumOperands();
117 if (NumRetVals == 0)
Gabor Greifd6da1d02008-04-06 20:25:17 +0000118 ReturnInst::Create(NULL, NewRetBlock);
Devang Patel40a92a12008-03-05 21:50:24 +0000119 else if (const StructType *STy = dyn_cast<StructType>(F.getReturnType())) {
Devang Pateld91d3842008-03-10 18:11:41 +0000120 Instruction *InsertPt = NewRetBlock->getFirstNonPHI();
Devang Patel40a92a12008-03-05 21:50:24 +0000121 for (unsigned i = 0; i < NumRetVals; ++i) {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000122 PHINode *PN = PHINode::Create(STy->getElementType(i), "UnifiedRetVal."
123 + utostr(i), InsertPt);
Devang Patel40a92a12008-03-05 21:50:24 +0000124 Phis.push_back(PN);
125 }
Gabor Greifd6da1d02008-04-06 20:25:17 +0000126 ReturnInst::Create(&Phis[0], NumRetVals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 }
Devang Patel40a92a12008-03-05 21:50:24 +0000128 else {
129 // If the function doesn't return void... add a PHI node to the block...
Gabor Greifd6da1d02008-04-06 20:25:17 +0000130 PHINode *PN = PHINode::Create(F.getReturnType(), "UnifiedRetVal");
Devang Patel40a92a12008-03-05 21:50:24 +0000131 NewRetBlock->getInstList().push_back(PN);
132 Phis.push_back(PN);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000133 ReturnInst::Create(PN, NewRetBlock);
Devang Patel40a92a12008-03-05 21:50:24 +0000134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
136 // Loop over all of the blocks, replacing the return instruction with an
137 // unconditional branch.
138 //
139 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
140 E = ReturningBlocks.end(); I != E; ++I) {
141 BasicBlock *BB = *I;
142
143 // Add an incoming element to the PHI node for every return instruction that
144 // is merging into this new block...
Devang Patel40a92a12008-03-05 21:50:24 +0000145 if (!Phis.empty()) {
146 for (unsigned i = 0; i < NumRetVals; ++i)
147 cast<PHINode>(Phis[i])->addIncoming(BB->getTerminator()->getOperand(i),
148 BB);
149 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150
151 BB->getInstList().pop_back(); // Remove the return insn
Gabor Greifd6da1d02008-04-06 20:25:17 +0000152 BranchInst::Create(NewRetBlock, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 }
154 ReturnBlock = NewRetBlock;
155 return true;
156}