blob: d3c7b328958b03040711f38bca3f7b112ea9d45f [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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);
Chris Lattner4fe87d62006-05-09 04:13:41 +000035 // This is a cluster of orthogonal Transforms
36 AU.addPreservedID(PromoteMemoryToRegisterID);
37 AU.addPreservedID(LowerSelectID);
38 AU.addPreservedID(LowerSwitchID);
Chris Lattner07f7e5d2003-03-31 17:30:25 +000039}
40
Chris Lattner29aae152001-07-06 16:58:36 +000041// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
42// BasicBlock, and converting all returns to unconditional branches to this
43// new basic block. The singular exit node is returned.
44//
Chris Lattner62b7fd12002-04-07 20:49:59 +000045// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner29aae152001-07-06 16:58:36 +000046//
Chris Lattnerfda72b12002-06-25 16:12:52 +000047bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000048 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner29aae152001-07-06 16:58:36 +000049 // return.
50 //
Chris Lattner8d0a71a2003-05-22 22:00:07 +000051 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf9413962003-09-10 20:34:51 +000052 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattner98e54142004-10-16 18:21:33 +000053 std::vector<BasicBlock*> UnreachableBlocks;
Chris Lattnerfda72b12002-06-25 16:12:52 +000054 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
55 if (isa<ReturnInst>(I->getTerminator()))
56 ReturningBlocks.push_back(I);
Chris Lattnerf9413962003-09-10 20:34:51 +000057 else if (isa<UnwindInst>(I->getTerminator()))
58 UnwindingBlocks.push_back(I);
Chris Lattner98e54142004-10-16 18:21:33 +000059 else if (isa<UnreachableInst>(I->getTerminator()))
60 UnreachableBlocks.push_back(I);
Chris Lattner29aae152001-07-06 16:58:36 +000061
Chris Lattner98e54142004-10-16 18:21:33 +000062 // Handle unwinding blocks first.
Chris Lattnerf9413962003-09-10 20:34:51 +000063 if (UnwindingBlocks.empty()) {
64 UnwindBlock = 0;
65 } else if (UnwindingBlocks.size() == 1) {
66 UnwindBlock = UnwindingBlocks.front();
67 } else {
68 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattner2af51722003-11-20 18:25:24 +000069 new UnwindInst(UnwindBlock);
Chris Lattnerf9413962003-09-10 20:34:51 +000070
Misha Brukmanb1c93172005-04-21 23:48:37 +000071 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
Chris Lattnerf9413962003-09-10 20:34:51 +000072 E = UnwindingBlocks.end(); I != E; ++I) {
73 BasicBlock *BB = *I;
Chris Lattner98e54142004-10-16 18:21:33 +000074 BB->getInstList().pop_back(); // Remove the unwind insn
Alkis Evlogimenos9e84b502004-05-26 21:38:14 +000075 new BranchInst(UnwindBlock, BB);
Chris Lattnerf9413962003-09-10 20:34:51 +000076 }
77 }
78
Chris Lattner98e54142004-10-16 18:21:33 +000079 // Then unreachable blocks.
80 if (UnreachableBlocks.empty()) {
81 UnreachableBlock = 0;
82 } else if (UnreachableBlocks.size() == 1) {
83 UnreachableBlock = UnreachableBlocks.front();
84 } else {
85 UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
86 new UnreachableInst(UnreachableBlock);
87
Misha Brukmanb1c93172005-04-21 23:48:37 +000088 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
Chris Lattner98e54142004-10-16 18:21:33 +000089 E = UnreachableBlocks.end(); I != E; ++I) {
90 BasicBlock *BB = *I;
91 BB->getInstList().pop_back(); // Remove the unreachable inst.
92 new BranchInst(UnreachableBlock, BB);
93 }
94 }
95
96 // Now handle return blocks.
Chris Lattner86595ae2002-02-01 04:53:48 +000097 if (ReturningBlocks.empty()) {
Chris Lattnerf9413962003-09-10 20:34:51 +000098 ReturnBlock = 0;
Chris Lattner64d13342002-05-07 18:51:25 +000099 return false; // No blocks return
Chris Lattnerf9f28962002-01-31 01:12:06 +0000100 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf9413962003-09-10 20:34:51 +0000101 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattnerf9f28962002-01-31 01:12:06 +0000102 return false;
103 }
Chris Lattner29aae152001-07-06 16:58:36 +0000104
Chris Lattner62b7fd12002-04-07 20:49:59 +0000105 // Otherwise, we need to insert a new basic block into the function, add a PHI
Misha Brukmanb1c93172005-04-21 23:48:37 +0000106 // node (if the function returns a value), and convert all of the return
Chris Lattner29aae152001-07-06 16:58:36 +0000107 // instructions into unconditional branches.
108 //
Chris Lattnerf9413962003-09-10 20:34:51 +0000109 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner29aae152001-07-06 16:58:36 +0000110
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000111 PHINode *PN = 0;
Chris Lattnerfda72b12002-06-25 16:12:52 +0000112 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000113 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000114 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner3d7720a2002-09-10 23:31:28 +0000115 NewRetBlock->getInstList().push_back(PN);
Chris Lattner29aae152001-07-06 16:58:36 +0000116 }
Chris Lattnera2960002003-11-21 16:52:05 +0000117 new ReturnInst(PN, NewRetBlock);
Chris Lattner29aae152001-07-06 16:58:36 +0000118
119 // Loop over all of the blocks, replacing the return instruction with an
120 // unconditional branch.
121 //
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
Chris Lattner8d0a71a2003-05-22 22:00:07 +0000123 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000124 BasicBlock *BB = *I;
125
126 // Add an incoming element to the PHI node for every return instruction that
127 // is merging into this new block...
128 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
129
130 BB->getInstList().pop_back(); // Remove the return insn
Chris Lattnera2960002003-11-21 16:52:05 +0000131 new BranchInst(NewRetBlock, BB);
Chris Lattner29aae152001-07-06 16:58:36 +0000132 }
Chris Lattnerf9413962003-09-10 20:34:51 +0000133 ReturnBlock = NewRetBlock;
Chris Lattnerf9f28962002-01-31 01:12:06 +0000134 return true;
Chris Lattner29aae152001-07-06 16:58:36 +0000135}