blob: 72512430b36629c03e879c00f29940ff516eb91e [file] [log] [blame]
Tom Stellardaa664d92013-08-06 02:43:45 +00001//===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellardaa664d92013-08-06 02:43:45 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements flattening of CFG.
10//
11//===----------------------------------------------------------------------===//
12
Tom Stellardaa664d92013-08-06 02:43:45 +000013#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000014#include "llvm/IR/CFG.h"
Jakub Kuderskie6b21642019-09-19 19:39:42 +000015#include "llvm/IR/ValueHandle.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080016#include "llvm/InitializePasses.h"
Tom Stellardaa664d92013-08-06 02:43:45 +000017#include "llvm/Pass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/Transforms/Scalar.h"
Jakub Kuderskie6b21642019-09-19 19:39:42 +000019#include "llvm/Transforms/Utils/Local.h"
20
Tom Stellardaa664d92013-08-06 02:43:45 +000021using namespace llvm;
22
Chandler Carruth964daaa2014-04-22 02:55:47 +000023#define DEBUG_TYPE "flattencfg"
24
Tom Stellardaa664d92013-08-06 02:43:45 +000025namespace {
26struct FlattenCFGPass : public FunctionPass {
27 static char ID; // Pass identification, replacement for typeid
28public:
29 FlattenCFGPass() : FunctionPass(ID) {
30 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
31 }
Craig Topper3e4c6972014-03-05 09:10:37 +000032 bool runOnFunction(Function &F) override;
Tom Stellardaa664d92013-08-06 02:43:45 +000033
Craig Topper3e4c6972014-03-05 09:10:37 +000034 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth7b560d42015-09-09 17:55:00 +000035 AU.addRequired<AAResultsWrapperPass>();
Tom Stellardaa664d92013-08-06 02:43:45 +000036 }
37
38private:
39 AliasAnalysis *AA;
40};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000041}
Tom Stellardaa664d92013-08-06 02:43:45 +000042
43char FlattenCFGPass::ID = 0;
44INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
45 false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000046INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Tom Stellardaa664d92013-08-06 02:43:45 +000047INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
48 false)
49
50// Public interface to the FlattenCFG pass
51FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
52
53/// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
54/// iterating until no more changes are made.
55static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
56 bool Changed = false;
57 bool LocalChange = true;
Jakub Kuderskie6b21642019-09-19 19:39:42 +000058
59 // Use block handles instead of iterating over function blocks directly
60 // to avoid using iterators invalidated by erasing blocks.
61 std::vector<WeakVH> Blocks;
62 Blocks.reserve(F.size());
63 for (auto &BB : F)
64 Blocks.push_back(&BB);
65
Tom Stellardaa664d92013-08-06 02:43:45 +000066 while (LocalChange) {
67 LocalChange = false;
68
Jakub Kuderskie6b21642019-09-19 19:39:42 +000069 // Loop over all of the basic blocks and try to flatten them.
70 for (WeakVH &BlockHandle : Blocks) {
71 // Skip blocks erased by FlattenCFG.
72 if (auto *BB = cast_or_null<BasicBlock>(BlockHandle))
73 if (FlattenCFG(BB, AA))
74 LocalChange = true;
Tom Stellardaa664d92013-08-06 02:43:45 +000075 }
76 Changed |= LocalChange;
77 }
78 return Changed;
79}
80
81bool FlattenCFGPass::runOnFunction(Function &F) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000082 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Tom Stellardaa664d92013-08-06 02:43:45 +000083 bool EverChanged = false;
84 // iterativelyFlattenCFG can make some blocks dead.
85 while (iterativelyFlattenCFG(F, AA)) {
86 removeUnreachableBlocks(F);
87 EverChanged = true;
88 }
89 return EverChanged;
90}