blob: 977616378d04bf80874350f2c3923775fb5a5fc5 [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner29aae152001-07-06 16:58:36 +00009//
Chris Lattner64d13342002-05-07 18:51:25 +000010// 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.
Chris Lattner29aae152001-07-06 16:58:36 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner15435fd2002-05-07 19:18:48 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner07f7e5d2003-03-31 17:30:25 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner29aae152001-07-06 16:58:36 +000019#include "llvm/BasicBlock.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000020#include "llvm/Function.h"
Alkis Evlogimenosfd7a2d42004-07-29 12:17:34 +000021#include "llvm/Instructions.h"
Chris Lattner29aae152001-07-06 16:58:36 +000022#include "llvm/Type.h"
Chris Lattnera2960002003-11-21 16:52:05 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattnera2c09852002-07-26 21:12:44 +000025static RegisterOpt<UnifyFunctionExitNodes>
Chris Lattnerb28b6802002-07-23 18:06:35 +000026X("mergereturn", "Unify function exit nodes");
Chris Lattnerccf571a2002-01-31 00:42:27 +000027
Chris Lattnera2960002003-11-21 16:52:05 +000028Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf9413962003-09-10 20:34:51 +000029 return new UnifyFunctionExitNodes();
30}
31
Chris Lattner07f7e5d2003-03-31 17:30:25 +000032void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
33 // We preserve the non-critical-edgeness property
34 AU.addPreservedID(BreakCriticalEdgesID);
35}
36
Chris Lattner29aae152001-07-06 16:58:36 +000037// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
38// BasicBlock, and converting all returns to unconditional branches to this
39// new basic block. The singular exit node is returned.
40//
Chris Lattner62b7fd12002-04-07 20:49:59 +000041// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner29aae152001-07-06 16:58:36 +000042//
Chris Lattnerfda72b12002-06-25 16:12:52 +000043bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000044 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner29aae152001-07-06 16:58:36 +000045 // return.
46 //
Chris Lattner8d0a71a2003-05-22 22:00:07 +000047 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf9413962003-09-10 20:34:51 +000048 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattner98e54142004-10-16 18:21:33 +000049 std::vector<BasicBlock*> UnreachableBlocks;
Chris Lattnerfda72b12002-06-25 16:12:52 +000050 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
51 if (isa<ReturnInst>(I->getTerminator()))
52 ReturningBlocks.push_back(I);
Chris Lattnerf9413962003-09-10 20:34:51 +000053 else if (isa<UnwindInst>(I->getTerminator()))
54 UnwindingBlocks.push_back(I);
Chris Lattner98e54142004-10-16 18:21:33 +000055 else if (isa<UnreachableInst>(I->getTerminator()))
56 UnreachableBlocks.push_back(I);
Chris Lattner29aae152001-07-06 16:58:36 +000057
Chris Lattner98e54142004-10-16 18:21:33 +000058 // Handle unwinding blocks first.
Chris Lattnerf9413962003-09-10 20:34:51 +000059 if (UnwindingBlocks.empty()) {
60 UnwindBlock = 0;
61 } else if (UnwindingBlocks.size() == 1) {
62 UnwindBlock = UnwindingBlocks.front();
63 } else {
64 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattner2af51722003-11-20 18:25:24 +000065 new UnwindInst(UnwindBlock);
Chris Lattnerf9413962003-09-10 20:34:51 +000066
67 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
68 E = UnwindingBlocks.end(); I != E; ++I) {
69 BasicBlock *BB = *I;
Chris Lattner98e54142004-10-16 18:21:33 +000070 BB->getInstList().pop_back(); // Remove the unwind insn
Alkis Evlogimenos9e84b502004-05-26 21:38:14 +000071 new BranchInst(UnwindBlock, BB);
Chris Lattnerf9413962003-09-10 20:34:51 +000072 }
73 }
74
Chris Lattner98e54142004-10-16 18:21:33 +000075 // Then unreachable blocks.
76 if (UnreachableBlocks.empty()) {
77 UnreachableBlock = 0;
78 } else if (UnreachableBlocks.size() == 1) {
79 UnreachableBlock = UnreachableBlocks.front();
80 } else {
81 UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
82 new UnreachableInst(UnreachableBlock);
83
84 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
85 E = UnreachableBlocks.end(); I != E; ++I) {
86 BasicBlock *BB = *I;
87 BB->getInstList().pop_back(); // Remove the unreachable inst.
88 new BranchInst(UnreachableBlock, BB);
89 }
90 }
91
92 // Now handle return blocks.
Chris Lattner86595ae2002-02-01 04:53:48 +000093 if (ReturningBlocks.empty()) {
Chris Lattnerf9413962003-09-10 20:34:51 +000094 ReturnBlock = 0;
Chris Lattner64d13342002-05-07 18:51:25 +000095 return false; // No blocks return
Chris Lattnerf9f28962002-01-31 01:12:06 +000096 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf9413962003-09-10 20:34:51 +000097 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattnerf9f28962002-01-31 01:12:06 +000098 return false;
99 }
Chris Lattner29aae152001-07-06 16:58:36 +0000100
Chris Lattner62b7fd12002-04-07 20:49:59 +0000101 // Otherwise, we need to insert a new basic block into the function, add a PHI
Chris Lattner29aae152001-07-06 16:58:36 +0000102 // node (if the function returns a value), and convert all of the return
103 // instructions into unconditional branches.
104 //
Chris Lattnerf9413962003-09-10 20:34:51 +0000105 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner29aae152001-07-06 16:58:36 +0000106
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000107 PHINode *PN = 0;
Chris Lattnerfda72b12002-06-25 16:12:52 +0000108 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000109 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000110 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner3d7720a2002-09-10 23:31:28 +0000111 NewRetBlock->getInstList().push_back(PN);
Chris Lattner29aae152001-07-06 16:58:36 +0000112 }
Chris Lattnera2960002003-11-21 16:52:05 +0000113 new ReturnInst(PN, NewRetBlock);
Chris Lattner29aae152001-07-06 16:58:36 +0000114
115 // Loop over all of the blocks, replacing the return instruction with an
116 // unconditional branch.
117 //
Chris Lattner8d0a71a2003-05-22 22:00:07 +0000118 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
119 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000120 BasicBlock *BB = *I;
121
122 // Add an incoming element to the PHI node for every return instruction that
123 // is merging into this new block...
124 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
125
126 BB->getInstList().pop_back(); // Remove the return insn
Chris Lattnera2960002003-11-21 16:52:05 +0000127 new BranchInst(NewRetBlock, BB);
Chris Lattner29aae152001-07-06 16:58:36 +0000128 }
Chris Lattnerf9413962003-09-10 20:34:51 +0000129 ReturnBlock = NewRetBlock;
Chris Lattnerf9f28962002-01-31 01:12:06 +0000130 return true;
Chris Lattner29aae152001-07-06 16:58:36 +0000131}