blob: 9f129a85be6dd29b1c0316e36025b45a2aa1b274 [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"
Devang Patelc5eb3802008-03-05 21:50:24 +000023#include "llvm/ADT/SmallVector.h"
Devang Patel7498f902008-03-10 18:11:41 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner108e4ab2003-11-21 16:52:05 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Devang Patel19974732007-05-03 01:11:54 +000027char UnifyFunctionExitNodes::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000028static RegisterPass<UnifyFunctionExitNodes>
Chris Lattnerf6293092002-07-23 18:06:35 +000029X("mergereturn", "Unify function exit nodes");
Chris Lattner93193f82002-01-31 00:42:27 +000030
Chris Lattner191b0ba2006-06-02 18:40:06 +000031int UnifyFunctionExitNodes::stub;
32
Chris Lattner108e4ab2003-11-21 16:52:05 +000033Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf46057be2003-09-10 20:34:51 +000034 return new UnifyFunctionExitNodes();
35}
36
Chris Lattner6c0e0492003-03-31 17:30:25 +000037void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
38 // We preserve the non-critical-edgeness property
39 AU.addPreservedID(BreakCriticalEdgesID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000040 // This is a cluster of orthogonal Transforms
41 AU.addPreservedID(PromoteMemoryToRegisterID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000042 AU.addPreservedID(LowerSwitchID);
Chris Lattner6c0e0492003-03-31 17:30:25 +000043}
44
Chris Lattner63a0b2a2001-07-06 16:58:36 +000045// 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//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000049// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000050//
Chris Lattner18961502002-06-25 16:12:52 +000051bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000052 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000053 // return.
54 //
Chris Lattnerde579f12003-05-22 22:00:07 +000055 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf46057be2003-09-10 20:34:51 +000056 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000057 std::vector<BasicBlock*> UnreachableBlocks;
Chris Lattner18961502002-06-25 16:12:52 +000058 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
59 if (isa<ReturnInst>(I->getTerminator()))
60 ReturningBlocks.push_back(I);
Chris Lattnerf46057be2003-09-10 20:34:51 +000061 else if (isa<UnwindInst>(I->getTerminator()))
62 UnwindingBlocks.push_back(I);
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000063 else if (isa<UnreachableInst>(I->getTerminator()))
64 UnreachableBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000065
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000066 // Handle unwinding blocks first.
Chris Lattnerf46057be2003-09-10 20:34:51 +000067 if (UnwindingBlocks.empty()) {
68 UnwindBlock = 0;
69 } else if (UnwindingBlocks.size() == 1) {
70 UnwindBlock = UnwindingBlocks.front();
71 } else {
72 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattnerf8485c62003-11-20 18:25:24 +000073 new UnwindInst(UnwindBlock);
Chris Lattnerf46057be2003-09-10 20:34:51 +000074
Misha Brukmanfd939082005-04-21 23:48:37 +000075 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
Chris Lattnerf46057be2003-09-10 20:34:51 +000076 E = UnwindingBlocks.end(); I != E; ++I) {
77 BasicBlock *BB = *I;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000078 BB->getInstList().pop_back(); // Remove the unwind insn
Alkis Evlogimenos99c58f42004-05-26 21:38:14 +000079 new BranchInst(UnwindBlock, BB);
Chris Lattnerf46057be2003-09-10 20:34:51 +000080 }
81 }
82
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000083 // Then unreachable blocks.
84 if (UnreachableBlocks.empty()) {
85 UnreachableBlock = 0;
86 } else if (UnreachableBlocks.size() == 1) {
87 UnreachableBlock = UnreachableBlocks.front();
88 } else {
89 UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
90 new UnreachableInst(UnreachableBlock);
91
Misha Brukmanfd939082005-04-21 23:48:37 +000092 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000093 E = UnreachableBlocks.end(); I != E; ++I) {
94 BasicBlock *BB = *I;
95 BB->getInstList().pop_back(); // Remove the unreachable inst.
96 new BranchInst(UnreachableBlock, BB);
97 }
98 }
99
100 // Now handle return blocks.
Chris Lattner417cf7e2002-02-01 04:53:48 +0000101 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +0000102 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +0000103 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +0000104 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +0000105 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +0000106 return false;
107 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000108
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000109 // Otherwise, we need to insert a new basic block into the function, add a PHI
Devang Patelc5eb3802008-03-05 21:50:24 +0000110 // nodes (if the function returns values), and convert all of the return
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000111 // instructions into unconditional branches.
112 //
Chris Lattnerf46057be2003-09-10 20:34:51 +0000113 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000114
Devang Patelc5eb3802008-03-05 21:50:24 +0000115 SmallVector<Value *, 4> Phis;
116 unsigned NumRetVals = ReturningBlocks[0]->getTerminator()->getNumOperands();
117 if (NumRetVals == 0)
118 new ReturnInst(NULL, NewRetBlock);
119 else if (const StructType *STy = dyn_cast<StructType>(F.getReturnType())) {
Devang Patel7498f902008-03-10 18:11:41 +0000120 Instruction *InsertPt = NewRetBlock->getFirstNonPHI();
Devang Patelc5eb3802008-03-05 21:50:24 +0000121 for (unsigned i = 0; i < NumRetVals; ++i) {
Devang Patel7498f902008-03-10 18:11:41 +0000122 PHINode *PN = new PHINode(STy->getElementType(i), "UnifiedRetVal."
123 + utostr(i), InsertPt);
Devang Patelc5eb3802008-03-05 21:50:24 +0000124 Phis.push_back(PN);
125 }
126 new ReturnInst(&Phis[0], NumRetVals);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000127 }
Devang Patelc5eb3802008-03-05 21:50:24 +0000128 else {
129 // If the function doesn't return void... add a PHI node to the block...
130 PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
131 NewRetBlock->getInstList().push_back(PN);
132 Phis.push_back(PN);
133 new ReturnInst(PN, NewRetBlock);
134 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000135
136 // Loop over all of the blocks, replacing the return instruction with an
137 // unconditional branch.
138 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000139 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
Chris Lattnerde579f12003-05-22 22:00:07 +0000140 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +0000141 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 Patelc5eb3802008-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 }
Chris Lattner6c0e0492003-03-31 17:30:25 +0000150
151 BB->getInstList().pop_back(); // Remove the return insn
Chris Lattner108e4ab2003-11-21 16:52:05 +0000152 new BranchInst(NewRetBlock, BB);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000153 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000154 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000155 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000156}