FlattenCFGPass.cpp 3.3 KB

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