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 #define DEBUG_TYPE "flattencfg"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Transforms/Utils/Local.h"
23 struct FlattenCFGPass : public FunctionPass {
24 static char ID; // Pass identification, replacement for typeid
26 FlattenCFGPass() : FunctionPass(ID) {
27 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
29 bool runOnFunction(Function &F);
31 void getAnalysisUsage(AnalysisUsage &AU) const {
32 AU.addRequired<AliasAnalysis>();
40 char FlattenCFGPass::ID = 0;
41 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
43 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
44 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
47 // Public interface to the FlattenCFG pass
48 FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
50 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
51 /// iterating until no more changes are made.
52 static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
54 bool LocalChange = true;
58 // Loop over all of the basic blocks and remove them if they are unneeded...
60 for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
61 if (FlattenCFG(BBIt++, AA)) {
65 Changed |= LocalChange;
70 bool FlattenCFGPass::runOnFunction(Function &F) {
71 AA = &getAnalysis<AliasAnalysis>();
72 bool EverChanged = false;
73 // iterativelyFlattenCFG can make some blocks dead.
74 while (iterativelyFlattenCFG(F, AA)) {
75 removeUnreachableBlocks(F);