LLVM  13.0.0git
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/InstrTypes.h"
16 #include "llvm/IR/ValueHandle.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Transforms/Scalar.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "flattencfg"
25 
26 namespace {
27 struct FlattenCFGPass : public FunctionPass {
28  static char ID; // Pass identification, replacement for typeid
29 public:
30  FlattenCFGPass() : FunctionPass(ID) {
32  }
33  bool runOnFunction(Function &F) override;
34 
35  void getAnalysisUsage(AnalysisUsage &AU) const override {
37  }
38 
39 private:
40  AliasAnalysis *AA;
41 };
42 }
43 
44 char FlattenCFGPass::ID = 0;
45 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
46  false)
48 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
49  false)
50 
51 // Public interface to the FlattenCFG pass
52 FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
53 
54 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
55 /// iterating until no more changes are made.
57  bool Changed = false;
58  bool LocalChange = true;
59 
60  // Use block handles instead of iterating over function blocks directly
61  // to avoid using iterators invalidated by erasing blocks.
62  std::vector<WeakVH> Blocks;
63  Blocks.reserve(F.size());
64  for (auto &BB : F)
65  Blocks.push_back(&BB);
66 
67  while (LocalChange) {
68  LocalChange = false;
69 
70  // Loop over all of the basic blocks and try to flatten them.
71  for (WeakVH &BlockHandle : Blocks) {
72  // Skip blocks erased by FlattenCFG.
73  if (auto *BB = cast_or_null<BasicBlock>(BlockHandle))
74  if (FlattenCFG(BB, AA))
75  LocalChange = true;
76  }
77  Changed |= LocalChange;
78  }
79  return Changed;
80 }
81 
83  AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
84  bool EverChanged = false;
85  // iterativelyFlattenCFG can make some blocks dead.
86  while (iterativelyFlattenCFG(F, AA)) {
88  EverChanged = true;
89  }
90  return EverChanged;
91 }
iterativelyFlattenCFG
static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA)
iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function, iterating until no more ch...
Definition: FlattenCFGPass.cpp:56
llvm
Definition: AllocatorList.h:23
Scalar.h
llvm::Function
Definition: Function.h:61
Pass.h
llvm::createFlattenCFGPass
FunctionPass * createFlattenCFGPass()
Definition: FlattenCFGPass.cpp:52
Local.h
llvm::WeakVH
A nullable Value handle that is nullable.
Definition: ValueHandle.h:144
llvm::removeUnreachableBlocks
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:2401
F
#define F(x, y, z)
Definition: MD5.cpp:56
flattencfg
flattencfg
Definition: FlattenCFGPass.cpp:48
AliasAnalysis.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::AAResults
Definition: AliasAnalysis.h:456
InstrTypes.h
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
false
Definition: StackSlotColoring.cpp:142
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
CFG.h
CFG
Flatten the CFG
Definition: FlattenCFGPass.cpp:48
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::initializeFlattenCFGPassPass
void initializeFlattenCFGPassPass(PassRegistry &)
llvm::FlattenCFG
bool FlattenCFG(BasicBlock *BB, AAResults *AA=nullptr)
This function is used to flatten a CFG.
Definition: FlattenCFG.cpp:543
ValueHandle.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false, false) INITIALIZE_PASS_END(FlattenCFGPass
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:1269
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
Flatten
bool Flatten(DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, TargetTransformInfo *TTI)
Definition: LoopFlatten.cpp:661
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38