blob: 5d151ed70a0aae1ba0073be54c7f353b96ad5b83 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner63a0b2a2001-07-06 16:58:36 +00009//
Chris Lattnerb444a1f2002-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 Lattner63a0b2a2001-07-06 16:58:36 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerfc514f42002-05-07 19:18:48 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner6c0e0492003-03-31 17:30:25 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000019#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000020#include "llvm/Function.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021#include "llvm/Instructions.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000022#include "llvm/Type.h"
Chris Lattner108e4ab2003-11-21 16:52:05 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Devang Patel19974732007-05-03 01:11:54 +000025char UnifyFunctionExitNodes::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000026static RegisterPass<UnifyFunctionExitNodes>
Chris Lattnerf6293092002-07-23 18:06:35 +000027X("mergereturn", "Unify function exit nodes");
Chris Lattner93193f82002-01-31 00:42:27 +000028
Chris Lattner191b0ba2006-06-02 18:40:06 +000029int UnifyFunctionExitNodes::stub;
30
Chris Lattner108e4ab2003-11-21 16:52:05 +000031Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf46057be2003-09-10 20:34:51 +000032 return new UnifyFunctionExitNodes();
33}
34
Chris Lattner6c0e0492003-03-31 17:30:25 +000035void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
36 // We preserve the non-critical-edgeness property
37 AU.addPreservedID(BreakCriticalEdgesID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000038 // This is a cluster of orthogonal Transforms
39 AU.addPreservedID(PromoteMemoryToRegisterID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000040 AU.addPreservedID(LowerSwitchID);
Chris Lattner6c0e0492003-03-31 17:30:25 +000041}
42
Chris Lattner63a0b2a2001-07-06 16:58:36 +000043// 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//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000047// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000048//
Chris Lattner18961502002-06-25 16:12:52 +000049bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000050 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000051 // return.
52 //
Devang Patel4d519452008-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;
Chris Lattnerde579f12003-05-22 22:00:07 +000058 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf46057be2003-09-10 20:34:51 +000059 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000060 std::vector<BasicBlock*> UnreachableBlocks;
Chris Lattner18961502002-06-25 16:12:52 +000061 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
62 if (isa<ReturnInst>(I->getTerminator()))
63 ReturningBlocks.push_back(I);
Chris Lattnerf46057be2003-09-10 20:34:51 +000064 else if (isa<UnwindInst>(I->getTerminator()))
65 UnwindingBlocks.push_back(I);
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000066 else if (isa<UnreachableInst>(I->getTerminator()))
67 UnreachableBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000068
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000069 // Handle unwinding blocks first.
Chris Lattnerf46057be2003-09-10 20:34:51 +000070 if (UnwindingBlocks.empty()) {
71 UnwindBlock = 0;
72 } else if (UnwindingBlocks.size() == 1) {
73 UnwindBlock = UnwindingBlocks.front();
74 } else {
75 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattnerf8485c62003-11-20 18:25:24 +000076 new UnwindInst(UnwindBlock);
Chris Lattnerf46057be2003-09-10 20:34:51 +000077
Misha Brukmanfd939082005-04-21 23:48:37 +000078 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
Chris Lattnerf46057be2003-09-10 20:34:51 +000079 E = UnwindingBlocks.end(); I != E; ++I) {
80 BasicBlock *BB = *I;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000081 BB->getInstList().pop_back(); // Remove the unwind insn
Alkis Evlogimenos99c58f42004-05-26 21:38:14 +000082 new BranchInst(UnwindBlock, BB);
Chris Lattnerf46057be2003-09-10 20:34:51 +000083 }
84 }
85
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000086 // 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
Misha Brukmanfd939082005-04-21 23:48:37 +000095 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000096 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.
Chris Lattner417cf7e2002-02-01 04:53:48 +0000104 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +0000105 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +0000106 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +0000107 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +0000108 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +0000109 return false;
110 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000111
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000112 // Otherwise, we need to insert a new basic block into the function, add a PHI
Misha Brukmanfd939082005-04-21 23:48:37 +0000113 // node (if the function returns a value), and convert all of the return
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000114 // instructions into unconditional branches.
115 //
Chris Lattnerf46057be2003-09-10 20:34:51 +0000116 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000117
Chris Lattner6c0e0492003-03-31 17:30:25 +0000118 PHINode *PN = 0;
Chris Lattner18961502002-06-25 16:12:52 +0000119 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000120 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner6c0e0492003-03-31 17:30:25 +0000121 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner8606d992002-09-10 23:31:28 +0000122 NewRetBlock->getInstList().push_back(PN);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000123 }
Chris Lattner108e4ab2003-11-21 16:52:05 +0000124 new ReturnInst(PN, NewRetBlock);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000125
126 // Loop over all of the blocks, replacing the return instruction with an
127 // unconditional branch.
128 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000129 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
Chris Lattnerde579f12003-05-22 22:00:07 +0000130 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +0000131 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
Chris Lattner108e4ab2003-11-21 16:52:05 +0000138 new BranchInst(NewRetBlock, BB);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000139 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000140 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000141 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000142}