LLVM 20.0.0git
UnifyFunctionExitNodes.cpp
Go to the documentation of this file.
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
15#include "llvm/IR/BasicBlock.h"
16#include "llvm/IR/Function.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>(I.getTerminator()))
28 UnreachableBlocks.push_back(&I);
29
30 if (UnreachableBlocks.size() <= 1)
31 return false;
32
33 BasicBlock *UnreachableBlock =
34 BasicBlock::Create(F.getContext(), "UnifiedUnreachableBlock", &F);
35 new UnreachableInst(F.getContext(), UnreachableBlock);
36
37 for (BasicBlock *BB : UnreachableBlocks) {
38 BB->back().eraseFromParent(); // Remove the unreachable inst.
39 BranchInst::Create(UnreachableBlock, 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>(I.getTerminator()))
50 ReturningBlocks.push_back(&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(F.getContext(),
59 "UnifiedReturnBlock", &F);
60
61 PHINode *PN = nullptr;
62 if (F.getReturnType()->isVoidTy()) {
63 ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
64 } else {
65 // If the function doesn't return void... add a PHI node to the block...
66 PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
67 "UnifiedRetVal");
68 PN->insertInto(NewRetBlock, NewRetBlock->end());
69 ReturnInst::Create(F.getContext(), PN, 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(BB->getTerminator()->getOperand(0), BB);
79
80 BB->back().eraseFromParent(); // Remove the return insn
81 BranchInst::Create(NewRetBlock, BB);
82 }
83
84 return true;
85}
86} // namespace
87
90 bool Changed = false;
91 Changed |= unifyUnreachableBlocks(F);
92 Changed |= unifyReturnBlocks(F);
93 return Changed ? PreservedAnalyses() : PreservedAnalyses::all();
94}
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:212
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This function has undefined behavior.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18