SimplifyInstructions.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is a utility pass used for testing the InstructionSimplify analysis.
  11. // The analysis is applied to every instruction, and if it simplifies then the
  12. // instruction is replaced by the simplification. If you are looking for a pass
  13. // that performs serious instruction folding, use the instcombine pass instead.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Scalar.h"
  17. #include "llvm/ADT/DepthFirstIterator.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/AssumptionCache.h"
  21. #include "llvm/Analysis/InstructionSimplify.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/Dominators.h"
  24. #include "llvm/IR/Function.h"
  25. #include "llvm/IR/Type.h"
  26. #include "llvm/Pass.h"
  27. #include "llvm/Analysis/TargetLibraryInfo.h"
  28. #include "llvm/Transforms/Utils/Local.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "instsimplify"
  31. STATISTIC(NumSimplified, "Number of redundant instructions removed");
  32. namespace {
  33. struct InstSimplifier : public FunctionPass {
  34. static char ID; // Pass identification, replacement for typeid
  35. InstSimplifier() : FunctionPass(ID) {
  36. initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
  37. }
  38. void getAnalysisUsage(AnalysisUsage &AU) const override {
  39. AU.setPreservesCFG();
  40. AU.addRequired<AssumptionCacheTracker>();
  41. AU.addRequired<TargetLibraryInfoWrapperPass>();
  42. }
  43. /// runOnFunction - Remove instructions that simplify.
  44. bool runOnFunction(Function &F) override {
  45. const DominatorTreeWrapperPass *DTWP =
  46. getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  47. const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
  48. const DataLayout &DL = F.getParent()->getDataLayout();
  49. const TargetLibraryInfo *TLI =
  50. &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  51. AssumptionCache *AC =
  52. &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  53. SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
  54. bool Changed = false;
  55. do {
  56. for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
  57. // Here be subtlety: the iterator must be incremented before the loop
  58. // body (not sure why), so a range-for loop won't work here.
  59. for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
  60. Instruction *I = BI++;
  61. // The first time through the loop ToSimplify is empty and we try to
  62. // simplify all instructions. On later iterations ToSimplify is not
  63. // empty and we only bother simplifying instructions that are in it.
  64. if (!ToSimplify->empty() && !ToSimplify->count(I))
  65. continue;
  66. // Don't waste time simplifying unused instructions.
  67. if (!I->use_empty())
  68. if (Value *V = SimplifyInstruction(I, &DL, TLI, DT, AC)) {
  69. // Mark all uses for resimplification next time round the loop.
  70. for (User *U : I->users())
  71. Next->insert(cast<Instruction>(U));
  72. I->replaceAllUsesWith(V);
  73. ++NumSimplified;
  74. Changed = true;
  75. }
  76. bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
  77. if (res) {
  78. // RecursivelyDeleteTriviallyDeadInstruction can remove
  79. // more than one instruction, so simply incrementing the
  80. // iterator does not work. When instructions get deleted
  81. // re-iterate instead.
  82. BI = BB->begin(); BE = BB->end();
  83. Changed |= res;
  84. }
  85. }
  86. // Place the list of instructions to simplify on the next loop iteration
  87. // into ToSimplify.
  88. std::swap(ToSimplify, Next);
  89. Next->clear();
  90. } while (!ToSimplify->empty());
  91. return Changed;
  92. }
  93. };
  94. }
  95. char InstSimplifier::ID = 0;
  96. INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
  97. "Remove redundant instructions", false, false)
  98. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  99. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  100. INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
  101. "Remove redundant instructions", false, false)
  102. char &llvm::InstructionSimplifierID = InstSimplifier::ID;
  103. // Public interface to the simplify instructions pass.
  104. FunctionPass *llvm::createInstructionSimplifierPass() {
  105. return new InstSimplifier();
  106. }