1 //===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements flattening of CFG.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Scalar.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/IR/CFG.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Transforms/Utils/Local.h"
21 #define DEBUG_TYPE "flattencfg"
24 struct FlattenCFGPass : public FunctionPass {
25 static char ID; // Pass identification, replacement for typeid
27 FlattenCFGPass() : FunctionPass(ID) {
28 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
30 bool runOnFunction(Function &F) override;
32 void getAnalysisUsage(AnalysisUsage &AU) const override {
33 AU.addRequired<AliasAnalysis>();
41 char FlattenCFGPass::ID = 0;
42 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
44 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
45 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
48 // Public interface to the FlattenCFG pass
49 FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
51 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
52 /// iterating until no more changes are made.
53 static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
55 bool LocalChange = true;
59 // Loop over all of the basic blocks and remove them if they are unneeded...
61 for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
62 if (FlattenCFG(BBIt++, AA)) {
66 Changed |= LocalChange;
71 bool FlattenCFGPass::runOnFunction(Function &F) {
72 AA = &getAnalysis<AliasAnalysis>();
73 bool EverChanged = false;
74 // iterativelyFlattenCFG can make some blocks dead.
75 while (iterativelyFlattenCFG(F, AA)) {
76 removeUnreachableBlocks(F);