blob: 117b19fb8a42253e162fb114adb91fd1191b1bfc [file] [log] [blame]
Tom Stellardaa664d92013-08-06 02:43:45 +00001//===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements flattening of CFG.
11//
12//===----------------------------------------------------------------------===//
13
Tom Stellardaa664d92013-08-06 02:43:45 +000014#include "llvm/Analysis/AliasAnalysis.h"
David Blaikie31b98d22018-06-04 21:23:21 +000015#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000016#include "llvm/IR/CFG.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"
Tom Stellardaa664d92013-08-06 02:43:45 +000019using namespace llvm;
20
Chandler Carruth964daaa2014-04-22 02:55:47 +000021#define DEBUG_TYPE "flattencfg"
22
Tom Stellardaa664d92013-08-06 02:43:45 +000023namespace {
24struct FlattenCFGPass : public FunctionPass {
25 static char ID; // Pass identification, replacement for typeid
26public:
27 FlattenCFGPass() : FunctionPass(ID) {
28 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
29 }
Craig Topper3e4c6972014-03-05 09:10:37 +000030 bool runOnFunction(Function &F) override;
Tom Stellardaa664d92013-08-06 02:43:45 +000031
Craig Topper3e4c6972014-03-05 09:10:37 +000032 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth7b560d42015-09-09 17:55:00 +000033 AU.addRequired<AAResultsWrapperPass>();
Tom Stellardaa664d92013-08-06 02:43:45 +000034 }
35
36private:
37 AliasAnalysis *AA;
38};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000039}
Tom Stellardaa664d92013-08-06 02:43:45 +000040
41char FlattenCFGPass::ID = 0;
42INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
43 false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000044INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Tom Stellardaa664d92013-08-06 02:43:45 +000045INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
46 false)
47
48// Public interface to the FlattenCFG pass
49FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
50
51/// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
52/// iterating until no more changes are made.
53static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
54 bool Changed = false;
55 bool LocalChange = true;
56 while (LocalChange) {
57 LocalChange = false;
58
59 // Loop over all of the basic blocks and remove them if they are unneeded...
60 //
61 for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
Duncan P. N. Exon Smith3a9c9e32015-10-13 18:26:00 +000062 if (FlattenCFG(&*BBIt++, AA)) {
Tom Stellardaa664d92013-08-06 02:43:45 +000063 LocalChange = true;
64 }
65 }
66 Changed |= LocalChange;
67 }
68 return Changed;
69}
70
71bool FlattenCFGPass::runOnFunction(Function &F) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000072 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Tom Stellardaa664d92013-08-06 02:43:45 +000073 bool EverChanged = false;
74 // iterativelyFlattenCFG can make some blocks dead.
75 while (iterativelyFlattenCFG(F, AA)) {
76 removeUnreachableBlocks(F);
77 EverChanged = true;
78 }
79 return EverChanged;
80}