FlattenCFGPass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "llvm/Analysis/AliasAnalysis.h"
  13. #include "llvm/IR/CFG.h"
  14. #include "llvm/IR/InstrTypes.h"
  15. #include "llvm/IR/PassManager.h"
  16. #include "llvm/IR/ValueHandle.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/Transforms/Scalar.h"
  20. #include "llvm/Transforms/Scalar/FlattenCFG.h"
  21. #include "llvm/Transforms/Utils/Local.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "flattencfg"
  24. namespace {
  25. struct FlattenCFGLegacyPass : public FunctionPass {
  26. static char ID; // Pass identification, replacement for typeid
  27. public:
  28. FlattenCFGLegacyPass() : FunctionPass(ID) {
  29. initializeFlattenCFGLegacyPassPass(*PassRegistry::getPassRegistry());
  30. }
  31. bool runOnFunction(Function &F) override;
  32. void getAnalysisUsage(AnalysisUsage &AU) const override {
  33. AU.addRequired<AAResultsWrapperPass>();
  34. }
  35. private:
  36. AliasAnalysis *AA;
  37. };
  38. /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
  39. /// iterating until no more changes are made.
  40. bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
  41. bool Changed = false;
  42. bool LocalChange = true;
  43. // Use block handles instead of iterating over function blocks directly
  44. // to avoid using iterators invalidated by erasing blocks.
  45. std::vector<WeakVH> Blocks;
  46. Blocks.reserve(F.size());
  47. for (auto &BB : F)
  48. Blocks.push_back(&BB);
  49. while (LocalChange) {
  50. LocalChange = false;
  51. // Loop over all of the basic blocks and try to flatten them.
  52. for (WeakVH &BlockHandle : Blocks) {
  53. // Skip blocks erased by FlattenCFG.
  54. if (auto *BB = cast_or_null<BasicBlock>(BlockHandle))
  55. if (FlattenCFG(BB, AA))
  56. LocalChange = true;
  57. }
  58. Changed |= LocalChange;
  59. }
  60. return Changed;
  61. }
  62. } // namespace
  63. char FlattenCFGLegacyPass::ID = 0;
  64. INITIALIZE_PASS_BEGIN(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG",
  65. false, false)
  66. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  67. INITIALIZE_PASS_END(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG",
  68. false, false)
  69. // Public interface to the FlattenCFG pass
  70. FunctionPass *llvm::createFlattenCFGPass() {
  71. return new FlattenCFGLegacyPass();
  72. }
  73. bool FlattenCFGLegacyPass::runOnFunction(Function &F) {
  74. AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
  75. bool EverChanged = false;
  76. // iterativelyFlattenCFG can make some blocks dead.
  77. while (iterativelyFlattenCFG(F, AA)) {
  78. removeUnreachableBlocks(F);
  79. EverChanged = true;
  80. }
  81. return EverChanged;
  82. }
  83. PreservedAnalyses FlattenCFGPass::run(Function &F,
  84. FunctionAnalysisManager &AM) {
  85. bool EverChanged = false;
  86. AliasAnalysis *AA = &AM.getResult<AAManager>(F);
  87. // iterativelyFlattenCFG can make some blocks dead.
  88. while (iterativelyFlattenCFG(F, AA)) {
  89. removeUnreachableBlocks(F);
  90. EverChanged = true;
  91. }
  92. return EverChanged ? PreservedAnalyses::none() : PreservedAnalyses::all();
  93. }