blob: 3d64d532d9d8de6847f83e8fc1e7fa41426c574f [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 Patel7498f902008-03-10 18:11:41 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner108e4ab2003-11-21 16:52:05 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Devang Patel19974732007-05-03 01:11:54 +000026char UnifyFunctionExitNodes::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000027INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn",
28 "Unify function exit nodes", false, false);
Chris Lattner93193f82002-01-31 00:42:27 +000029
Chris Lattner108e4ab2003-11-21 16:52:05 +000030Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf46057be2003-09-10 20:34:51 +000031 return new UnifyFunctionExitNodes();
32}
33
Chris Lattner6c0e0492003-03-31 17:30:25 +000034void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
35 // We preserve the non-critical-edgeness property
36 AU.addPreservedID(BreakCriticalEdgesID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000037 // This is a cluster of orthogonal Transforms
38 AU.addPreservedID(PromoteMemoryToRegisterID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000039 AU.addPreservedID(LowerSwitchID);
Chris Lattner6c0e0492003-03-31 17:30:25 +000040}
41
Chris Lattner63a0b2a2001-07-06 16:58:36 +000042// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
43// BasicBlock, and converting all returns to unconditional branches to this
44// new basic block. The singular exit node is returned.
45//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000046// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner63a0b2a2001-07-06 16:58:36 +000047//
Chris Lattner18961502002-06-25 16:12:52 +000048bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000049 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner63a0b2a2001-07-06 16:58:36 +000050 // return.
51 //
Chris Lattnerde579f12003-05-22 22:00:07 +000052 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattnerf46057be2003-09-10 20:34:51 +000053 std::vector<BasicBlock*> UnwindingBlocks;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000054 std::vector<BasicBlock*> UnreachableBlocks;
Chris Lattner18961502002-06-25 16:12:52 +000055 for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
56 if (isa<ReturnInst>(I->getTerminator()))
57 ReturningBlocks.push_back(I);
Chris Lattnerf46057be2003-09-10 20:34:51 +000058 else if (isa<UnwindInst>(I->getTerminator()))
59 UnwindingBlocks.push_back(I);
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000060 else if (isa<UnreachableInst>(I->getTerminator()))
61 UnreachableBlocks.push_back(I);
Chris Lattner63a0b2a2001-07-06 16:58:36 +000062
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000063 // Handle unwinding blocks first.
Chris Lattnerf46057be2003-09-10 20:34:51 +000064 if (UnwindingBlocks.empty()) {
65 UnwindBlock = 0;
66 } else if (UnwindingBlocks.size() == 1) {
67 UnwindBlock = UnwindingBlocks.front();
68 } else {
Owen Anderson1d0be152009-08-13 21:58:54 +000069 UnwindBlock = BasicBlock::Create(F.getContext(), "UnifiedUnwindBlock", &F);
70 new UnwindInst(F.getContext(), UnwindBlock);
Chris Lattnerf46057be2003-09-10 20:34:51 +000071
Misha Brukmanfd939082005-04-21 23:48:37 +000072 for (std::vector<BasicBlock*>::iterator I = UnwindingBlocks.begin(),
Chris Lattnerf46057be2003-09-10 20:34:51 +000073 E = UnwindingBlocks.end(); I != E; ++I) {
74 BasicBlock *BB = *I;
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000075 BB->getInstList().pop_back(); // Remove the unwind insn
Gabor Greif051a9502008-04-06 20:25:17 +000076 BranchInst::Create(UnwindBlock, BB);
Chris Lattnerf46057be2003-09-10 20:34:51 +000077 }
78 }
79
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000080 // Then unreachable blocks.
81 if (UnreachableBlocks.empty()) {
82 UnreachableBlock = 0;
83 } else if (UnreachableBlocks.size() == 1) {
84 UnreachableBlock = UnreachableBlocks.front();
85 } else {
Owen Anderson1d0be152009-08-13 21:58:54 +000086 UnreachableBlock = BasicBlock::Create(F.getContext(),
87 "UnifiedUnreachableBlock", &F);
88 new UnreachableInst(F.getContext(), UnreachableBlock);
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000089
Misha Brukmanfd939082005-04-21 23:48:37 +000090 for (std::vector<BasicBlock*>::iterator I = UnreachableBlocks.begin(),
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000091 E = UnreachableBlocks.end(); I != E; ++I) {
92 BasicBlock *BB = *I;
93 BB->getInstList().pop_back(); // Remove the unreachable inst.
Gabor Greif051a9502008-04-06 20:25:17 +000094 BranchInst::Create(UnreachableBlock, BB);
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000095 }
96 }
97
98 // Now handle return blocks.
Chris Lattner417cf7e2002-02-01 04:53:48 +000099 if (ReturningBlocks.empty()) {
Chris Lattnerf46057be2003-09-10 20:34:51 +0000100 ReturnBlock = 0;
Chris Lattnerb444a1f2002-05-07 18:51:25 +0000101 return false; // No blocks return
Chris Lattner21801532002-01-31 01:12:06 +0000102 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf46057be2003-09-10 20:34:51 +0000103 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattner21801532002-01-31 01:12:06 +0000104 return false;
105 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000106
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000107 // Otherwise, we need to insert a new basic block into the function, add a PHI
Devang Patelc5eb3802008-03-05 21:50:24 +0000108 // nodes (if the function returns values), and convert all of the return
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000109 // instructions into unconditional branches.
110 //
Owen Anderson1d0be152009-08-13 21:58:54 +0000111 BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
112 "UnifiedReturnBlock", &F);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000113
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000114 PHINode *PN = 0;
Benjamin Kramerf0127052010-01-05 13:12:22 +0000115 if (F.getReturnType()->isVoidTy()) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000116 ReturnInst::Create(F.getContext(), NULL, NewRetBlock);
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000117 } else {
Devang Patelc5eb3802008-03-05 21:50:24 +0000118 // If the function doesn't return void... add a PHI node to the block...
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000119 PN = PHINode::Create(F.getReturnType(), "UnifiedRetVal");
Devang Patelc5eb3802008-03-05 21:50:24 +0000120 NewRetBlock->getInstList().push_back(PN);
Owen Anderson1d0be152009-08-13 21:58:54 +0000121 ReturnInst::Create(F.getContext(), PN, NewRetBlock);
Devang Patelc5eb3802008-03-05 21:50:24 +0000122 }
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000123
124 // Loop over all of the blocks, replacing the return instruction with an
125 // unconditional branch.
126 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000127 for (std::vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
Chris Lattnerde579f12003-05-22 22:00:07 +0000128 E = ReturningBlocks.end(); I != E; ++I) {
Chris Lattner6c0e0492003-03-31 17:30:25 +0000129 BasicBlock *BB = *I;
130
131 // Add an incoming element to the PHI node for every return instruction that
132 // is merging into this new block...
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000133 if (PN)
134 PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
Chris Lattner6c0e0492003-03-31 17:30:25 +0000135
136 BB->getInstList().pop_back(); // Remove the return insn
Gabor Greif051a9502008-04-06 20:25:17 +0000137 BranchInst::Create(NewRetBlock, BB);
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000138 }
Chris Lattnerf46057be2003-09-10 20:34:51 +0000139 ReturnBlock = NewRetBlock;
Chris Lattner21801532002-01-31 01:12:06 +0000140 return true;
Chris Lattner63a0b2a2001-07-06 16:58:36 +0000141}