blob: 89710edaf56deb003cfa2b15aced602ac0a2ba3c [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
John Criswellb576c942003-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 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"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000021#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000022#include "llvm/iPHINode.h"
Chris Lattner63a0b2a2001-07-06 16:58:36 +000023#include "llvm/Type.h"
Chris Lattner108e4ab2003-11-21 16:52:05 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner1e435162002-07-26 21:12:44 +000026static RegisterOpt<UnifyFunctionExitNodes>
Chris Lattnerf6293092002-07-23 18:06:35 +000027X("mergereturn", "Unify function exit nodes");
Chris Lattner93193f82002-01-31 00:42:27 +000028
Chris Lattner108e4ab2003-11-21 16:52:05 +000029Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf46057be2003-09-10 20:34:51 +000030 return new UnifyFunctionExitNodes();
31}
32
Chris Lattner6c0e0492003-03-31 17:30:25 +000033void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
34 // We preserve the non-critical-edgeness property
35 AU.addPreservedID(BreakCriticalEdgesID);
36}
37
Chris Lattner63a0b2a2001-07-06 16:58:36 +000038// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
39// BasicBlock, and converting all returns to unconditional branches to this
40// new basic block. The singular exit node is returned.
41//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000042// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000043//
Chris Lattner18961502002-06-25 16:12:52 +000044bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000045 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000046 // return.
47 //
Chris Lattnerde579f12003-05-22 22:00:07 +000048 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf46057be2003-09-10 20:34:51 +000049 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattner18961502002-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 Lattnerf46057be2003-09-10 20:34:51 +000053 else if (isa<UnwindInst>(I->getTerminator()))
54 UnwindingBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000055
Chris Lattnerf46057be2003-09-10 20:34:51 +000056 // Handle unwinding blocks first...
57 if (UnwindingBlocks.empty()) {
58 UnwindBlock = 0;
59 } else if (UnwindingBlocks.size() == 1) {
60 UnwindBlock = UnwindingBlocks.front();
61 } else {
62 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattnerf8485c62003-11-20 18:25:24 +000063 new UnwindInst(UnwindBlock);
Chris Lattnerf46057be2003-09-10 20:34:51 +000064
65 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
66 E = UnwindingBlocks.end(); I != E; ++I) {
67 BasicBlock *BB = *I;
68 BB->getInstList().pop_back(); // Remove the return insn
Alkis Evlogimenos99c58f42004-05-26 21:38:14 +000069 new BranchInst(UnwindBlock, BB);
Chris Lattnerf46057be2003-09-10 20:34:51 +000070 }
71 }
72
73 // Now handle return blocks...
Chris Lattner417cf7e2002-02-01 04:53:48 +000074 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000075 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +000076 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +000077 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000078 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +000079 return false;
80 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +000081
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000082 // Otherwise, we need to insert a new basic block into the function, add a PHI
Chris Lattner63a0b2a2001-07-06 16:58:36 +000083 // node (if the function returns a value), and convert all of the return
84 // instructions into unconditional branches.
85 //
Chris Lattnerf46057be2003-09-10 20:34:51 +000086 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000087
Chris Lattner6c0e0492003-03-31 17:30:25 +000088 PHINode *PN = 0;
Chris Lattner18961502002-06-25 16:12:52 +000089 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner6c0e0492003-03-31 17:30:25 +000091 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner8606d992002-09-10 23:31:28 +000092 NewRetBlock->getInstList().push_back(PN);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000093 }
Chris Lattner108e4ab2003-11-21 16:52:05 +000094 new ReturnInst(PN, NewRetBlock);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000095
96 // Loop over all of the blocks, replacing the return instruction with an
97 // unconditional branch.
98 //
Chris Lattnerde579f12003-05-22 22:00:07 +000099 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
100 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +0000101 BasicBlock *BB = *I;
102
103 // Add an incoming element to the PHI node for every return instruction that
104 // is merging into this new block...
105 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
106
107 BB->getInstList().pop_back(); // Remove the return insn
Chris Lattner108e4ab2003-11-21 16:52:05 +0000108 new BranchInst(NewRetBlock, BB);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000109 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000110 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000111 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000112}