LLVM: lib/Transforms/Utils/UnifyFunctionExitNodes.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
19using namespace llvm;
20
21namespace {
22
23bool unifyUnreachableBlocks(Function &F) {
24 std::vector<BasicBlock *> UnreachableBlocks;
25
28 UnreachableBlocks.push_back(&I);
29
30 if (UnreachableBlocks.size() <= 1)
31 return false;
32
36
37 for (BasicBlock *BB : UnreachableBlocks) {
38 BB->back().eraseFromParent();
40 }
41
42 return true;
43}
44
45bool unifyReturnBlocks(Function &F) {
46 std::vector<BasicBlock *> ReturningBlocks;
47
50 ReturningBlocks.push_back(&I);
51
52 if (ReturningBlocks.size() <= 1)
53 return false;
54
55
56
57
59 "UnifiedReturnBlock", &F);
60
62 if (F.getReturnType()->isVoidTy()) {
64 } else {
65
66 PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
67 "UnifiedRetVal");
70 }
71
72
73
74 for (BasicBlock *BB : ReturningBlocks) {
75
76
77 if (PN)
78 PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
79
80 BB->back().eraseFromParent();
82 }
83
84 return true;
85}
86}
87
91 Changed |= unifyUnreachableBlocks(F);
92 Changed |= unifyReturnBlocks(F);
94}
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
LLVM_ABI 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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition UnifyFunctionExitNodes.cpp:88
This function has undefined behavior.
This is an optimization pass for GlobalISel generic memory operations.
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.