blob: 9385f825523cd4ba5a712ab6455e4946442b375c [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner29aae152001-07-06 16:58:36 +00009//
Chris Lattner64d13342002-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 Lattner29aae152001-07-06 16:58:36 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner15435fd2002-05-07 19:18:48 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnera2960002003-11-21 16:52:05 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Devang Patel8c78a0b2007-05-03 01:11:54 +000026char UnifyFunctionExitNodes::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000027INITIALIZE_PASS(UnifyFunctionExitNodes, "mergereturn",
Owen Andersondf7a4f22010-10-07 22:25:06 +000028 "Unify function exit nodes", false, false)
Chris Lattnerccf571a2002-01-31 00:42:27 +000029
Chris Lattnera2960002003-11-21 16:52:05 +000030Pass *llvm::createUnifyFunctionExitNodesPass() {
Chris Lattnerf9413962003-09-10 20:34:51 +000031 return new UnifyFunctionExitNodes();
32}
33
Chris Lattner07f7e5d2003-03-31 17:30:25 +000034void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
35 // We preserve the non-critical-edgeness property
36 AU.addPreservedID(BreakCriticalEdgesID);
Chris Lattner4fe87d62006-05-09 04:13:41 +000037 // This is a cluster of orthogonal Transforms
Chris Lattner4fe87d62006-05-09 04:13:41 +000038 AU.addPreservedID(LowerSwitchID);
Chris Lattner07f7e5d2003-03-31 17:30:25 +000039}
40
Chris Lattner29aae152001-07-06 16:58:36 +000041// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
42// BasicBlock, and converting all returns to unconditional branches to this
43// new basic block. The singular exit node is returned.
44//
Chris Lattner62b7fd12002-04-07 20:49:59 +000045// If there are no return stmts in the Function, a null pointer is returned.
Chris Lattner29aae152001-07-06 16:58:36 +000046//
Chris Lattnerfda72b12002-06-25 16:12:52 +000047bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000048 // Loop over all of the blocks in a function, tracking all of the blocks that
Chris Lattner29aae152001-07-06 16:58:36 +000049 // return.
50 //
Chris Lattner8d0a71a2003-05-22 22:00:07 +000051 std::vector<BasicBlock*> ReturningBlocks;
Chris Lattner98e54142004-10-16 18:21:33 +000052 std::vector<BasicBlock*> UnreachableBlocks;
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000053 for (BasicBlock &I : F)
54 if (isa<ReturnInst>(I.getTerminator()))
55 ReturningBlocks.push_back(&I);
56 else if (isa<UnreachableInst>(I.getTerminator()))
57 UnreachableBlocks.push_back(&I);
Chris Lattner29aae152001-07-06 16:58:36 +000058
Chris Lattner98e54142004-10-16 18:21:33 +000059 // Then unreachable blocks.
60 if (UnreachableBlocks.empty()) {
Craig Topperf40110f2014-04-25 05:29:35 +000061 UnreachableBlock = nullptr;
Chris Lattner98e54142004-10-16 18:21:33 +000062 } else if (UnreachableBlocks.size() == 1) {
63 UnreachableBlock = UnreachableBlocks.front();
64 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +000065 UnreachableBlock = BasicBlock::Create(F.getContext(),
66 "UnifiedUnreachableBlock", &F);
67 new UnreachableInst(F.getContext(), UnreachableBlock);
Chris Lattner98e54142004-10-16 18:21:33 +000068
Benjamin Kramer135f7352016-06-26 12:28:59 +000069 for (BasicBlock *BB : UnreachableBlocks) {
Chris Lattner98e54142004-10-16 18:21:33 +000070 BB->getInstList().pop_back(); // Remove the unreachable inst.
Gabor Greife9ecc682008-04-06 20:25:17 +000071 BranchInst::Create(UnreachableBlock, BB);
Chris Lattner98e54142004-10-16 18:21:33 +000072 }
73 }
74
75 // Now handle return blocks.
Chris Lattner86595ae2002-02-01 04:53:48 +000076 if (ReturningBlocks.empty()) {
Craig Topperf40110f2014-04-25 05:29:35 +000077 ReturnBlock = nullptr;
Chris Lattner64d13342002-05-07 18:51:25 +000078 return false; // No blocks return
Chris Lattnerf9f28962002-01-31 01:12:06 +000079 } else if (ReturningBlocks.size() == 1) {
Chris Lattnerf9413962003-09-10 20:34:51 +000080 ReturnBlock = ReturningBlocks.front(); // Already has a single return block
Chris Lattnerf9f28962002-01-31 01:12:06 +000081 return false;
82 }
Chris Lattner29aae152001-07-06 16:58:36 +000083
Chris Lattner62b7fd12002-04-07 20:49:59 +000084 // Otherwise, we need to insert a new basic block into the function, add a PHI
Devang Patel3b1c95f2008-03-05 21:50:24 +000085 // nodes (if the function returns values), and convert all of the return
Chris Lattner29aae152001-07-06 16:58:36 +000086 // instructions into unconditional branches.
87 //
Owen Anderson55f1c092009-08-13 21:58:54 +000088 BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(),
89 "UnifiedReturnBlock", &F);
Chris Lattner29aae152001-07-06 16:58:36 +000090
Craig Topperf40110f2014-04-25 05:29:35 +000091 PHINode *PN = nullptr;
Benjamin Kramerccce8ba2010-01-05 13:12:22 +000092 if (F.getReturnType()->isVoidTy()) {
Craig Topperf40110f2014-04-25 05:29:35 +000093 ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
Dan Gohmanfa1211f2008-07-23 00:34:11 +000094 } else {
Devang Patel3b1c95f2008-03-05 21:50:24 +000095 // If the function doesn't return void... add a PHI node to the block...
Jay Foad52131342011-03-30 11:28:46 +000096 PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
97 "UnifiedRetVal");
Devang Patel3b1c95f2008-03-05 21:50:24 +000098 NewRetBlock->getInstList().push_back(PN);
Owen Anderson55f1c092009-08-13 21:58:54 +000099 ReturnInst::Create(F.getContext(), PN, NewRetBlock);
Devang Patel3b1c95f2008-03-05 21:50:24 +0000100 }
Chris Lattner29aae152001-07-06 16:58:36 +0000101
102 // Loop over all of the blocks, replacing the return instruction with an
103 // unconditional branch.
104 //
Benjamin Kramer135f7352016-06-26 12:28:59 +0000105 for (BasicBlock *BB : ReturningBlocks) {
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000106 // Add an incoming element to the PHI node for every return instruction that
107 // is merging into this new block...
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000108 if (PN)
109 PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
Chris Lattner07f7e5d2003-03-31 17:30:25 +0000110
111 BB->getInstList().pop_back(); // Remove the return insn
Gabor Greife9ecc682008-04-06 20:25:17 +0000112 BranchInst::Create(NewRetBlock, BB);
Chris Lattner29aae152001-07-06 16:58:36 +0000113 }
Chris Lattnerf9413962003-09-10 20:34:51 +0000114 ReturnBlock = NewRetBlock;
Chris Lattnerf9f28962002-01-31 01:12:06 +0000115 return true;
Chris Lattner29aae152001-07-06 16:58:36 +0000116}