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 
14 #include "llvm/IR/CFG.h"
15 #include "llvm/IR/ValueHandle.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Transforms/Scalar.h"
19 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "flattencfg"
23 
24 namespace {
25 struct FlattenCFGPass : public FunctionPass {
26  static char ID; // Pass identification, replacement for typeid
27 public:
28  FlattenCFGPass() : FunctionPass(ID) {
30  }
31  bool runOnFunction(Function &F) override;
32 
33  void getAnalysisUsage(AnalysisUsage &AU) const override {
35  }
36 
37 private:
38  AliasAnalysis *AA;
39 };
40 }
41 
42 char FlattenCFGPass::ID = 0;
43 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
44  false)
46 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
47  false)
48 
49 // Public interface to the FlattenCFG pass
50 FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
51 
52 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
53 /// iterating until no more changes are made.
55  bool Changed = false;
56  bool LocalChange = true;
57 
58  // Use block handles instead of iterating over function blocks directly
59  // to avoid using iterators invalidated by erasing blocks.
60  std::vector<WeakVH> Blocks;
61  Blocks.reserve(F.size());
62  for (auto &BB : F)
63  Blocks.push_back(&BB);
64 
65  while (LocalChange) {
66  LocalChange = false;
67 
68  // Loop over all of the basic blocks and try to flatten them.
69  for (WeakVH &BlockHandle : Blocks) {
70  // Skip blocks erased by FlattenCFG.
71  if (auto *BB = cast_or_null<BasicBlock>(BlockHandle))
72  if (FlattenCFG(BB, AA))
73  LocalChange = true;
74  }
75  Changed |= LocalChange;
76  }
77  return Changed;
78 }
79 
81  AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
82  bool EverChanged = false;
83  // iterativelyFlattenCFG can make some blocks dead.
84  while (iterativelyFlattenCFG(F, AA)) {
86  EverChanged = true;
87  }
88  return EverChanged;
89 }
size_t size() const
Definition: Function.h:685
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
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
A nullable Value handle that is nullable.
Definition: ValueHandle.h:140
bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA=nullptr)
This function is used to flatten a CFG.
Definition: FlattenCFG.cpp:499
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
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Flatten the CFG
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function&#39;s entry.
Definition: Local.cpp:2214
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
void initializeFlattenCFGPassPass(PassRegistry &)