InstSimplifyPass.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===- InstSimplifyPass.cpp -----------------------------------------------===//
  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. #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
  9. #include "llvm/ADT/DepthFirstIterator.h"
  10. #include "llvm/ADT/SmallPtrSet.h"
  11. #include "llvm/ADT/Statistic.h"
  12. #include "llvm/Analysis/AssumptionCache.h"
  13. #include "llvm/Analysis/InstructionSimplify.h"
  14. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  15. #include "llvm/Analysis/TargetLibraryInfo.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/Dominators.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Type.h"
  20. #include "llvm/InitializePasses.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Transforms/Scalar.h"
  23. #include "llvm/Transforms/Utils.h"
  24. #include "llvm/Transforms/Utils/Local.h"
  25. using namespace llvm;
  26. #define DEBUG_TYPE "instsimplify"
  27. STATISTIC(NumSimplified, "Number of redundant instructions removed");
  28. static bool runImpl(Function &F, const SimplifyQuery &SQ,
  29. OptimizationRemarkEmitter *ORE) {
  30. SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
  31. bool Changed = false;
  32. do {
  33. for (BasicBlock &BB : F) {
  34. // Unreachable code can take on strange forms that we are not prepared to
  35. // handle. For example, an instruction may have itself as an operand.
  36. if (!SQ.DT->isReachableFromEntry(&BB))
  37. continue;
  38. SmallVector<WeakTrackingVH, 8> DeadInstsInBB;
  39. for (Instruction &I : BB) {
  40. // The first time through the loop, ToSimplify is empty and we try to
  41. // simplify all instructions. On later iterations, ToSimplify is not
  42. // empty and we only bother simplifying instructions that are in it.
  43. if (!ToSimplify->empty() && !ToSimplify->count(&I))
  44. continue;
  45. // Don't waste time simplifying dead/unused instructions.
  46. if (isInstructionTriviallyDead(&I)) {
  47. DeadInstsInBB.push_back(&I);
  48. Changed = true;
  49. } else if (!I.use_empty()) {
  50. if (Value *V = SimplifyInstruction(&I, SQ, ORE)) {
  51. // Mark all uses for resimplification next time round the loop.
  52. for (User *U : I.users())
  53. Next->insert(cast<Instruction>(U));
  54. I.replaceAllUsesWith(V);
  55. ++NumSimplified;
  56. Changed = true;
  57. // A call can get simplified, but it may not be trivially dead.
  58. if (isInstructionTriviallyDead(&I))
  59. DeadInstsInBB.push_back(&I);
  60. }
  61. }
  62. }
  63. RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI);
  64. }
  65. // Place the list of instructions to simplify on the next loop iteration
  66. // into ToSimplify.
  67. std::swap(ToSimplify, Next);
  68. Next->clear();
  69. } while (!ToSimplify->empty());
  70. return Changed;
  71. }
  72. namespace {
  73. struct InstSimplifyLegacyPass : public FunctionPass {
  74. static char ID; // Pass identification, replacement for typeid
  75. InstSimplifyLegacyPass() : FunctionPass(ID) {
  76. initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
  77. }
  78. void getAnalysisUsage(AnalysisUsage &AU) const override {
  79. AU.setPreservesCFG();
  80. AU.addRequired<DominatorTreeWrapperPass>();
  81. AU.addRequired<AssumptionCacheTracker>();
  82. AU.addRequired<TargetLibraryInfoWrapperPass>();
  83. AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
  84. }
  85. /// Remove instructions that simplify.
  86. bool runOnFunction(Function &F) override {
  87. if (skipFunction(F))
  88. return false;
  89. const DominatorTree *DT =
  90. &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  91. const TargetLibraryInfo *TLI =
  92. &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  93. AssumptionCache *AC =
  94. &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  95. OptimizationRemarkEmitter *ORE =
  96. &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
  97. const DataLayout &DL = F.getParent()->getDataLayout();
  98. const SimplifyQuery SQ(DL, TLI, DT, AC);
  99. return runImpl(F, SQ, ORE);
  100. }
  101. };
  102. } // namespace
  103. char InstSimplifyLegacyPass::ID = 0;
  104. INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
  105. "Remove redundant instructions", false, false)
  106. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  107. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  108. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  109. INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
  110. INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
  111. "Remove redundant instructions", false, false)
  112. // Public interface to the simplify instructions pass.
  113. FunctionPass *llvm::createInstSimplifyLegacyPass() {
  114. return new InstSimplifyLegacyPass();
  115. }
  116. PreservedAnalyses InstSimplifyPass::run(Function &F,
  117. FunctionAnalysisManager &AM) {
  118. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  119. auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  120. auto &AC = AM.getResult<AssumptionAnalysis>(F);
  121. auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
  122. const DataLayout &DL = F.getParent()->getDataLayout();
  123. const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
  124. bool Changed = runImpl(F, SQ, &ORE);
  125. if (!Changed)
  126. return PreservedAnalyses::all();
  127. PreservedAnalyses PA;
  128. PA.preserveSet<CFGAnalyses>();
  129. return PA;
  130. }