blob: 0c1eda7c0cc82922b7f14e4dadd26b59c9bd61d2 [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//
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 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
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 Lattnerec7c1ab2004-10-16 18:21:33 +000049 std::vector<BasicBlock*> UnreachableBlocks;
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 Lattnerec7c1ab2004-10-16 18:21:33 +000055 else if (isa<UnreachableInst>(I->getTerminator()))
56 UnreachableBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000057
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000058 // Handle unwinding blocks first.
Chris Lattnerf46057be2003-09-10 20:34:51 +000059 if (UnwindingBlocks.empty()) {
60 UnwindBlock = 0;
61 } else if (UnwindingBlocks.size() == 1) {
62 UnwindBlock = UnwindingBlocks.front();
63 } else {
64 UnwindBlock = new BasicBlock("UnifiedUnwindBlock", &F);
Chris Lattnerf8485c62003-11-20 18:25:24 +000065 new UnwindInst(UnwindBlock);
Chris Lattnerf46057be2003-09-10 20:34:51 +000066
Misha Brukmanfd939082005-04-21 23:48:37 +000067 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
Chris Lattnerf46057be2003-09-10 20:34:51 +000068 E = UnwindingBlocks.end(); I != E; ++I) {
69 BasicBlock *BB = *I;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000070 BB->getInstList().pop_back(); // Remove the unwind insn
Alkis Evlogimenos99c58f42004-05-26 21:38:14 +000071 new BranchInst(UnwindBlock, BB);
Chris Lattnerf46057be2003-09-10 20:34:51 +000072 }
73 }
74
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000075 // Then unreachable blocks.
76 if (UnreachableBlocks.empty()) {
77 UnreachableBlock = 0;
78 } else if (UnreachableBlocks.size() == 1) {
79 UnreachableBlock = UnreachableBlocks.front();
80 } else {
81 UnreachableBlock = new BasicBlock("UnifiedUnreachableBlock", &F);
82 new UnreachableInst(UnreachableBlock);
83
Misha Brukmanfd939082005-04-21 23:48:37 +000084 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000085 E = UnreachableBlocks.end(); I != E; ++I) {
86 BasicBlock *BB = *I;
87 BB->getInstList().pop_back(); // Remove the unreachable inst.
88 new BranchInst(UnreachableBlock, BB);
89 }
90 }
91
92 // Now handle return blocks.
Chris Lattner417cf7e2002-02-01 04:53:48 +000093 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000094 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +000095 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +000096 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +000097 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +000098 return false;
99 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000100
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000101 // Otherwise, we need to insert a new basic block into the function, add a PHI
Misha Brukmanfd939082005-04-21 23:48:37 +0000102 // node (if the function returns a value), and convert all of the return
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000103 // instructions into unconditional branches.
104 //
Chris Lattnerf46057be2003-09-10 20:34:51 +0000105 BasicBlock *NewRetBlock = new BasicBlock("UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000106
Chris Lattner6c0e0492003-03-31 17:30:25 +0000107 PHINode *PN = 0;
Chris Lattner18961502002-06-25 16:12:52 +0000108 if (F.getReturnType() != Type::VoidTy) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000109 // If the function doesn't return void... add a PHI node to the block...
Chris Lattner6c0e0492003-03-31 17:30:25 +0000110 PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
Chris Lattner8606d992002-09-10 23:31:28 +0000111 NewRetBlock->getInstList().push_back(PN);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000112 }
Chris Lattner108e4ab2003-11-21 16:52:05 +0000113 new ReturnInst(PN, NewRetBlock);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000114
115 // Loop over all of the blocks, replacing the return instruction with an
116 // unconditional branch.
117 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000118 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
Chris Lattnerde579f12003-05-22 22:00:07 +0000119 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +0000120 BasicBlock *BB = *I;
121
122 // Add an incoming element to the PHI node for every return instruction that
123 // is merging into this new block...
124 if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
125
126 BB->getInstList().pop_back(); // Remove the return insn
Chris Lattner108e4ab2003-11-21 16:52:05 +0000127 new BranchInst(NewRetBlock, BB);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000128 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000129 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000130 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000131}