blob: 31670b1464e41f45da64e22d40b859ce45d5db32 [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"
David Blaikie31b98d22018-06-04 21:23:21 +000014#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000015#include "llvm/IR/CFG.h"
Tom Stellardaa664d92013-08-06 02:43:45 +000016#include "llvm/Pass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/Transforms/Scalar.h"
Tom Stellardaa664d92013-08-06 02:43:45 +000018using namespace llvm;
19
Chandler Carruth964daaa2014-04-22 02:55:47 +000020#define DEBUG_TYPE "flattencfg"
21
Tom Stellardaa664d92013-08-06 02:43:45 +000022namespace {
23struct FlattenCFGPass : public FunctionPass {
24 static char ID; // Pass identification, replacement for typeid
25public:
26 FlattenCFGPass() : FunctionPass(ID) {
27 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
28 }
Craig Topper3e4c6972014-03-05 09:10:37 +000029 bool runOnFunction(Function &F) override;
Tom Stellardaa664d92013-08-06 02:43:45 +000030
Craig Topper3e4c6972014-03-05 09:10:37 +000031 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth7b560d42015-09-09 17:55:00 +000032 AU.addRequired<AAResultsWrapperPass>();
Tom Stellardaa664d92013-08-06 02:43:45 +000033 }
34
35private:
36 AliasAnalysis *AA;
37};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000038}
Tom Stellardaa664d92013-08-06 02:43:45 +000039
40char FlattenCFGPass::ID = 0;
41INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
42 false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000043INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Tom Stellardaa664d92013-08-06 02:43:45 +000044INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
45 false)
46
47// Public interface to the FlattenCFG pass
48FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
49
50/// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
51/// iterating until no more changes are made.
52static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
53 bool Changed = false;
54 bool LocalChange = true;
55 while (LocalChange) {
56 LocalChange = false;
57
58 // Loop over all of the basic blocks and remove them if they are unneeded...
59 //
60 for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
Duncan P. N. Exon Smith3a9c9e32015-10-13 18:26:00 +000061 if (FlattenCFG(&*BBIt++, AA)) {
Tom Stellardaa664d92013-08-06 02:43:45 +000062 LocalChange = true;
63 }
64 }
65 Changed |= LocalChange;
66 }
67 return Changed;
68}
69
70bool FlattenCFGPass::runOnFunction(Function &F) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000071 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Tom Stellardaa664d92013-08-06 02:43:45 +000072 bool EverChanged = false;
73 // iterativelyFlattenCFG can make some blocks dead.
74 while (iterativelyFlattenCFG(F, AA)) {
75 removeUnreachableBlocks(F);
76 EverChanged = true;
77 }
78 return EverChanged;
79}