InstSimplifyPass.cpp 5.3 KB

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