LLVM  10.0.0svn
FlattenCFGPass.cpp
Go to the documentation of this file.
1 //===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements flattening of CFG.
10 //
11 //===----------------------------------------------------------------------===//
12 
15 #include "llvm/IR/CFG.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Transforms/Scalar.h"
18 using namespace llvm;
19 
20 #define DEBUG_TYPE "flattencfg"
21 
22 namespace {
23 struct FlattenCFGPass : public FunctionPass {
24  static char ID; // Pass identification, replacement for typeid
25 public:
26  FlattenCFGPass() : FunctionPass(ID) {
28  }
29  bool runOnFunction(Function &F) override;
30 
31  void getAnalysisUsage(AnalysisUsage &AU) const override {
33  }
34 
35 private:
36  AliasAnalysis *AA;
37 };
38 }
39 
40 char FlattenCFGPass::ID = 0;
41 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
42  false)
44 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
45  false)
46 
47 // Public interface to the FlattenCFG pass
48 FunctionPass *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.
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();) {
61  if (FlattenCFG(&*BBIt++, AA)) {
62  LocalChange = true;
63  }
64  }
65  Changed |= LocalChange;
66  }
67  return Changed;
68 }
69 
71  AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
72  bool EverChanged = false;
73  // iterativelyFlattenCFG can make some blocks dead.
74  while (iterativelyFlattenCFG(F, AA)) {
76  EverChanged = true;
77  }
78  return EverChanged;
79 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator end()
Definition: Function.h:682
bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI=nullptr, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function&#39;s entry.
Definition: Local.cpp:2215
INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false, false) INITIALIZE_PASS_END(FlattenCFGPass
F(f)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
iterator begin()
Definition: Function.h:680
bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA=nullptr)
This function is used to flatten a CFG.
Definition: FlattenCFG.cpp:489
flattencfg
static bool runOnFunction(Function &F, bool PostInlining)
FunctionPass * createFlattenCFGPass()
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA)
iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function, iterating until no more ch...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Iterator for intrusive lists based on ilist_node.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Flatten the CFG
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
void initializeFlattenCFGPassPass(PassRegistry &)