2
0

LoopInstSimplify.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
  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 pass performs lightweight instruction simplification on loop bodies.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Scalar.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/Analysis/AssumptionCache.h"
  17. #include "llvm/Analysis/InstructionSimplify.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. #include "llvm/Analysis/LoopPass.h"
  20. #include "llvm/Analysis/ScalarEvolution.h"
  21. #include "llvm/IR/DataLayout.h"
  22. #include "llvm/IR/Dominators.h"
  23. #include "llvm/IR/Instructions.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Analysis/TargetLibraryInfo.h"
  26. #include "llvm/Transforms/Utils/Local.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "loop-instsimplify"
  29. STATISTIC(NumSimplified, "Number of redundant instructions simplified");
  30. namespace {
  31. class LoopInstSimplify : public LoopPass {
  32. public:
  33. static char ID; // Pass ID, replacement for typeid
  34. LoopInstSimplify() : LoopPass(ID) {
  35. initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
  36. }
  37. bool runOnLoop(Loop*, LPPassManager&) override;
  38. void getAnalysisUsage(AnalysisUsage &AU) const override {
  39. AU.setPreservesCFG();
  40. AU.addRequired<AssumptionCacheTracker>();
  41. AU.addRequired<LoopInfoWrapperPass>();
  42. AU.addRequiredID(LoopSimplifyID);
  43. AU.addPreservedID(LoopSimplifyID);
  44. AU.addPreservedID(LCSSAID);
  45. AU.addPreserved<ScalarEvolution>();
  46. AU.addRequired<TargetLibraryInfoWrapperPass>();
  47. }
  48. };
  49. }
  50. char LoopInstSimplify::ID = 0;
  51. INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
  52. "Simplify instructions in loops", false, false)
  53. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  54. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  55. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  56. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  57. INITIALIZE_PASS_DEPENDENCY(LCSSA)
  58. INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
  59. "Simplify instructions in loops", false, false)
  60. Pass *llvm::createLoopInstSimplifyPass() {
  61. return new LoopInstSimplify();
  62. }
  63. bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
  64. if (skipOptnoneFunction(L))
  65. return false;
  66. DominatorTreeWrapperPass *DTWP =
  67. getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  68. DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
  69. LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  70. const TargetLibraryInfo *TLI =
  71. &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  72. auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
  73. *L->getHeader()->getParent());
  74. SmallVector<BasicBlock*, 8> ExitBlocks;
  75. L->getUniqueExitBlocks(ExitBlocks);
  76. array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
  77. SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
  78. // The bit we are stealing from the pointer represents whether this basic
  79. // block is the header of a subloop, in which case we only process its phis.
  80. typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
  81. SmallVector<WorklistItem, 16> VisitStack;
  82. SmallPtrSet<BasicBlock*, 32> Visited;
  83. bool Changed = false;
  84. bool LocalChanged;
  85. do {
  86. LocalChanged = false;
  87. VisitStack.clear();
  88. Visited.clear();
  89. VisitStack.push_back(WorklistItem(L->getHeader(), false));
  90. while (!VisitStack.empty()) {
  91. WorklistItem Item = VisitStack.pop_back_val();
  92. BasicBlock *BB = Item.getPointer();
  93. bool IsSubloopHeader = Item.getInt();
  94. const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
  95. // Simplify instructions in the current basic block.
  96. for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
  97. Instruction *I = BI++;
  98. // The first time through the loop ToSimplify is empty and we try to
  99. // simplify all instructions. On later iterations ToSimplify is not
  100. // empty and we only bother simplifying instructions that are in it.
  101. if (!ToSimplify->empty() && !ToSimplify->count(I))
  102. continue;
  103. // Don't bother simplifying unused instructions.
  104. if (!I->use_empty()) {
  105. Value *V = SimplifyInstruction(I, DL, TLI, DT, &AC);
  106. if (V && LI->replacementPreservesLCSSAForm(I, V)) {
  107. // Mark all uses for resimplification next time round the loop.
  108. for (User *U : I->users())
  109. Next->insert(cast<Instruction>(U));
  110. I->replaceAllUsesWith(V);
  111. LocalChanged = true;
  112. ++NumSimplified;
  113. }
  114. }
  115. bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
  116. if (res) {
  117. // RecursivelyDeleteTriviallyDeadInstruction can remove
  118. // more than one instruction, so simply incrementing the
  119. // iterator does not work. When instructions get deleted
  120. // re-iterate instead.
  121. BI = BB->begin(); BE = BB->end();
  122. LocalChanged |= res;
  123. }
  124. if (IsSubloopHeader && !isa<PHINode>(I))
  125. break;
  126. }
  127. // Add all successors to the worklist, except for loop exit blocks and the
  128. // bodies of subloops. We visit the headers of loops so that we can process
  129. // their phis, but we contract the rest of the subloop body and only follow
  130. // edges leading back to the original loop.
  131. for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
  132. ++SI) {
  133. BasicBlock *SuccBB = *SI;
  134. if (!Visited.insert(SuccBB).second)
  135. continue;
  136. const Loop *SuccLoop = LI->getLoopFor(SuccBB);
  137. if (SuccLoop && SuccLoop->getHeader() == SuccBB
  138. && L->contains(SuccLoop)) {
  139. VisitStack.push_back(WorklistItem(SuccBB, true));
  140. SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
  141. SuccLoop->getExitBlocks(SubLoopExitBlocks);
  142. for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
  143. BasicBlock *ExitBB = SubLoopExitBlocks[i];
  144. if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
  145. VisitStack.push_back(WorklistItem(ExitBB, false));
  146. }
  147. continue;
  148. }
  149. bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
  150. ExitBlocks.end(), SuccBB);
  151. if (IsExitBlock)
  152. continue;
  153. VisitStack.push_back(WorklistItem(SuccBB, false));
  154. }
  155. }
  156. // Place the list of instructions to simplify on the next loop iteration
  157. // into ToSimplify.
  158. std::swap(ToSimplify, Next);
  159. Next->clear();
  160. Changed |= LocalChanged;
  161. } while (LocalChanged);
  162. return Changed;
  163. }