blob: 5c87a5b15968a410bc697126f13bc2f070257c9d [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"
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
Chris Lattner1e435162002-07-26 21:12:44 +000025static RegisterOpt<UnifyFunctionExitNodes>
Chris Lattnerf6293092002-07-23 18:06:35 +000026X("mergereturn", "Unify function exit nodes");
Chris Lattner93193f82002-01-31 00:42:27 +000027
Chris Lattner108e4ab2003-11-21 16:52:05 +000028Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf46057be2003-09-10 20:34:51 +000029 return new UnifyFunctionExitNodes();
30}
31
Chris Lattner6c0e0492003-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 Lattner63a0b2a2001-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 Lattner2fbfdcf2002-04-07 20:49:59 +000041// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000042//
Chris Lattner18961502002-06-25 16:12:52 +000043bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000044 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000045 // return.
46 //
Chris Lattnerde579f12003-05-22 22:00:07 +000047 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf46057be2003-09-10 20:34:51 +000048 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattner18961502002-06-25 16:12:52 +000049 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
50 if (isa<ReturnInst>(I->getTerminator()))
51 ReturningBlocks.push_back(I);
Chris Lattnerf46057be2003-09-10 20:34:51 +000052 else if (isa<UnwindInst>(I->getTerminator()))
53 UnwindingBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000054
Chris Lattnerf46057be2003-09-10 20:34:51 +000055 // Handle unwinding blocks first...
56 if (UnwindingBlocks.empty()) {
57 UnwindBlock = 0;
58 } else if (UnwindingBlocks.size() == 1) {
59 UnwindBlock = UnwindingBlocks.front();
60 } else {
61 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattnerf8485c62003-11-20 18:25:24 +000062 new UnwindInst(UnwindBlock);
Chris Lattnerf46057be2003-09-10 20:34:51 +000063
64 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
65 E = UnwindingBlocks.end(); I != E; ++I) {
66 BasicBlock *BB = *I;
67 BB->getInstList().pop_back(); // Remove the return insn
Alkis Evlogimenos99c58f42004-05-26 21:38:14 +000068 new BranchInst(UnwindBlock, BB);
Chris Lattnerf46057be2003-09-10 20:34:51 +000069 }
70 }
71
72 // Now handle return blocks...
Chris Lattner417cf7e2002-02-01 04:53:48 +000073 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000074 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +000075 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +000076 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000077 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +000078 return false;
79 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +000080
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081 // Otherwise, we need to insert a new basic block into the function, add a PHI
Chris Lattner63a0b2a2001-07-06 16:58:36 +000082 // node (if the function returns a value), and convert all of the return
83 // instructions into unconditional branches.
84 //
Chris Lattnerf46057be2003-09-10 20:34:51 +000085 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000086
Chris Lattner6c0e0492003-03-31 17:30:25 +000087 PHINode *PN = 0;
Chris Lattner18961502002-06-25 16:12:52 +000088 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000089 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner6c0e0492003-03-31 17:30:25 +000090 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner8606d992002-09-10 23:31:28 +000091 NewRetBlock->getInstList().push_back(PN);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000092 }
Chris Lattner108e4ab2003-11-21 16:52:05 +000093 new ReturnInst(PN, NewRetBlock);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000094
95 // Loop over all of the blocks, replacing the return instruction with an
96 // unconditional branch.
97 //
Chris Lattnerde579f12003-05-22 22:00:07 +000098 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
99 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +0000100 BasicBlock *BB = *I;
101
102 // Add an incoming element to the PHI node for every return instruction that
103 // is merging into this new block...
104 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
105
106 BB->getInstList().pop_back(); // Remove the return insn
Chris Lattner108e4ab2003-11-21 16:52:05 +0000107 new BranchInst(NewRetBlock, BB);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000108 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000109 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000110 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000111}