LLVM  16.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/PassManager.h"
15 #include "llvm/IR/ValueHandle.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Transforms/Scalar.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "flattencfg"
25 
26 namespace {
27 struct FlattenCFGLegacyPass : public FunctionPass {
28  static char ID; // Pass identification, replacement for typeid
29 public:
30  FlattenCFGLegacyPass() : 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 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
44 /// iterating until no more changes are made.
45 bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
46  bool Changed = false;
47  bool LocalChange = true;
48 
49  // Use block handles instead of iterating over function blocks directly
50  // to avoid using iterators invalidated by erasing blocks.
51  std::vector<WeakVH> Blocks;
52  Blocks.reserve(F.size());
53  for (auto &BB : F)
54  Blocks.push_back(&BB);
55 
56  while (LocalChange) {
57  LocalChange = false;
58 
59  // Loop over all of the basic blocks and try to flatten them.
60  for (WeakVH &BlockHandle : Blocks) {
61  // Skip blocks erased by FlattenCFG.
62  if (auto *BB = cast_or_null<BasicBlock>(BlockHandle))
63  if (FlattenCFG(BB, AA))
64  LocalChange = true;
65  }
66  Changed |= LocalChange;
67  }
68  return Changed;
69 }
70 } // namespace
71 
73 
74 INITIALIZE_PASS_BEGIN(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG",
75  false, false)
77 INITIALIZE_PASS_END(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG",
79 
80 // Public interface to the FlattenCFG pass
82  return new FlattenCFGLegacyPass();
83 }
84 
86  AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
87  bool EverChanged = false;
88  // iterativelyFlattenCFG can make some blocks dead.
89  while (iterativelyFlattenCFG(F, AA)) {
91  EverChanged = true;
92  }
93  return EverChanged;
94 }
95 
98  bool EverChanged = false;
99  AliasAnalysis *AA = &AM.getResult<AAManager>(F);
100  // iterativelyFlattenCFG can make some blocks dead.
101  while (iterativelyFlattenCFG(F, AA)) {
103  EverChanged = true;
104  }
105  return EverChanged ? PreservedAnalyses::none() : PreservedAnalyses::all();
106 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::AAManager
A manager for alias analyses.
Definition: AliasAnalysis.h:876
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
Scalar.h
llvm::Function
Definition: Function.h:60
Pass.h
llvm::createFlattenCFGPass
FunctionPass * createFlattenCFGPass()
Definition: FlattenCFGPass.cpp:81
Local.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
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:2581
llvm::initializeFlattenCFGLegacyPassPass
void initializeFlattenCFGLegacyPassPass(PassRegistry &)
F
#define F(x, y, z)
Definition: MD5.cpp:55
flattencfg
flattencfg
Definition: FlattenCFGPass.cpp:77
AliasAnalysis.h
Flatten
bool Flatten(LoopNest &LN, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, TargetTransformInfo *TTI, LPMUpdater *U, MemorySSAUpdater *MSSAU)
Definition: LoopFlatten.cpp:897
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
llvm::AAResults
Definition: AliasAnalysis.h:294
FlattenCFG.h
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
false
Definition: StackSlotColoring.cpp:141
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG", false, false) INITIALIZE_PASS_END(FlattenCFGLegacyPass
CFG
Flatten the CFG
Definition: FlattenCFGPass.cpp:77
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::FlattenCFGPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: FlattenCFGPass.cpp:96
llvm::FlattenCFG
bool FlattenCFG(BasicBlock *BB, AAResults *AA=nullptr)
This function is used to flatten a CFG.
Definition: FlattenCFG.cpp:548
ValueHandle.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:85
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
PassManager.h
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:924
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
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
InitializePasses.h