1//===- UnifyFunctionExitNodes.cpp - Make all functions have a single exit -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass is used to ensure that functions have at most one return and one
10// unreachable instruction in them.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
15#include "llvm/IR/BasicBlock.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/Type.h"
19using namespace llvm;
20
21namespace {
22
23bool unifyUnreachableBlocks(Function &F) {
24 std::vector<BasicBlock *> UnreachableBlocks;
25
26 for (BasicBlock &I : F)
27 if (isa<UnreachableInst>(Val: I.getTerminator()))
28 UnreachableBlocks.push_back(x: &I);
29
30 if (UnreachableBlocks.size() <= 1)
31 return false;
32
33 BasicBlock *UnreachableBlock =
34 BasicBlock::Create(Context&: F.getContext(), Name: "UnifiedUnreachableBlock", Parent: &F);
35 new UnreachableInst(F.getContext(), UnreachableBlock);
36
37 for (BasicBlock *BB : UnreachableBlocks) {
38 BB->back().eraseFromParent(); // Remove the unreachable inst.
39 BranchInst::Create(IfTrue: UnreachableBlock, InsertBefore: BB);
40 }
41
42 return true;
43}
44
45bool unifyReturnBlocks(Function &F) {
46 std::vector<BasicBlock *> ReturningBlocks;
47
48 for (BasicBlock &I : F)
49 if (isa<ReturnInst>(Val: I.getTerminator()))
50 ReturningBlocks.push_back(x: &I);
51
52 if (ReturningBlocks.size() <= 1)
53 return false;
54
55 // Insert a new basic block into the function, add PHI nodes (if the function
56 // returns values), and convert all of the return instructions into
57 // unconditional branches.
58 BasicBlock *NewRetBlock = BasicBlock::Create(Context&: F.getContext(),
59 Name: "UnifiedReturnBlock", Parent: &F);
60
61 PHINode *PN = nullptr;
62 if (F.getReturnType()->isVoidTy()) {
63 ReturnInst::Create(C&: F.getContext(), retVal: nullptr, InsertBefore: NewRetBlock);
64 } else {
65 // If the function doesn't return void... add a PHI node to the block...
66 PN = PHINode::Create(Ty: F.getReturnType(), NumReservedValues: ReturningBlocks.size(),
67 NameStr: "UnifiedRetVal");
68 PN->insertInto(ParentBB: NewRetBlock, It: NewRetBlock->end());
69 ReturnInst::Create(C&: F.getContext(), retVal: PN, InsertBefore: NewRetBlock);
70 }
71
72 // Loop over all of the blocks, replacing the return instruction with an
73 // unconditional branch.
74 for (BasicBlock *BB : ReturningBlocks) {
75 // Add an incoming element to the PHI node for every return instruction that
76 // is merging into this new block...
77 if (PN)
78 PN->addIncoming(V: BB->getTerminator()->getOperand(i: 0), BB);
79
80 BB->back().eraseFromParent(); // Remove the return insn
81 BranchInst::Create(IfTrue: NewRetBlock, InsertBefore: BB);
82 }
83
84 return true;
85}
86} // namespace
87
88PreservedAnalyses UnifyFunctionExitNodesPass::run(Function &F,
89 FunctionAnalysisManager &AM) {
90 bool Changed = false;
91 Changed |= unifyUnreachableBlocks(F);
92 Changed |= unifyReturnBlocks(F);
93 return Changed ? PreservedAnalyses() : PreservedAnalyses::all();
94}
95