LoopInstSimplify.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //===- LoopInstSimplify.cpp - Loop Instruction Simplification 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 pass performs lightweight instruction simplification on loop bodies.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/Analysis/AssumptionCache.h"
  18. #include "llvm/Analysis/InstructionSimplify.h"
  19. #include "llvm/Analysis/LoopInfo.h"
  20. #include "llvm/Analysis/LoopIterator.h"
  21. #include "llvm/Analysis/LoopPass.h"
  22. #include "llvm/Analysis/MemorySSA.h"
  23. #include "llvm/Analysis/MemorySSAUpdater.h"
  24. #include "llvm/Analysis/TargetLibraryInfo.h"
  25. #include "llvm/IR/BasicBlock.h"
  26. #include "llvm/IR/Dominators.h"
  27. #include "llvm/IR/Instruction.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/Module.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include "llvm/InitializePasses.h"
  32. #include "llvm/Pass.h"
  33. #include "llvm/Support/Casting.h"
  34. #include "llvm/Transforms/Scalar.h"
  35. #include "llvm/Transforms/Utils/Local.h"
  36. #include "llvm/Transforms/Utils/LoopUtils.h"
  37. #include <optional>
  38. #include <utility>
  39. using namespace llvm;
  40. #define DEBUG_TYPE "loop-instsimplify"
  41. STATISTIC(NumSimplified, "Number of redundant instructions simplified");
  42. static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI,
  43. AssumptionCache &AC, const TargetLibraryInfo &TLI,
  44. MemorySSAUpdater *MSSAU) {
  45. const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
  46. SimplifyQuery SQ(DL, &TLI, &DT, &AC);
  47. // On the first pass over the loop body we try to simplify every instruction.
  48. // On subsequent passes, we can restrict this to only simplifying instructions
  49. // where the inputs have been updated. We end up needing two sets: one
  50. // containing the instructions we are simplifying in *this* pass, and one for
  51. // the instructions we will want to simplify in the *next* pass. We use
  52. // pointers so we can swap between two stably allocated sets.
  53. SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
  54. // Track the PHI nodes that have already been visited during each iteration so
  55. // that we can identify when it is necessary to iterate.
  56. SmallPtrSet<PHINode *, 4> VisitedPHIs;
  57. // While simplifying we may discover dead code or cause code to become dead.
  58. // Keep track of all such instructions and we will delete them at the end.
  59. SmallVector<WeakTrackingVH, 8> DeadInsts;
  60. // First we want to create an RPO traversal of the loop body. By processing in
  61. // RPO we can ensure that definitions are processed prior to uses (for non PHI
  62. // uses) in all cases. This ensures we maximize the simplifications in each
  63. // iteration over the loop and minimizes the possible causes for continuing to
  64. // iterate.
  65. LoopBlocksRPO RPOT(&L);
  66. RPOT.perform(&LI);
  67. MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr;
  68. bool Changed = false;
  69. for (;;) {
  70. if (MSSAU && VerifyMemorySSA)
  71. MSSA->verifyMemorySSA();
  72. for (BasicBlock *BB : RPOT) {
  73. for (Instruction &I : *BB) {
  74. if (auto *PI = dyn_cast<PHINode>(&I))
  75. VisitedPHIs.insert(PI);
  76. if (I.use_empty()) {
  77. if (isInstructionTriviallyDead(&I, &TLI))
  78. DeadInsts.push_back(&I);
  79. continue;
  80. }
  81. // We special case the first iteration which we can detect due to the
  82. // empty `ToSimplify` set.
  83. bool IsFirstIteration = ToSimplify->empty();
  84. if (!IsFirstIteration && !ToSimplify->count(&I))
  85. continue;
  86. Value *V = simplifyInstruction(&I, SQ.getWithInstruction(&I));
  87. if (!V || !LI.replacementPreservesLCSSAForm(&I, V))
  88. continue;
  89. for (Use &U : llvm::make_early_inc_range(I.uses())) {
  90. auto *UserI = cast<Instruction>(U.getUser());
  91. U.set(V);
  92. // Do not bother dealing with unreachable code.
  93. if (!DT.isReachableFromEntry(UserI->getParent()))
  94. continue;
  95. // If the instruction is used by a PHI node we have already processed
  96. // we'll need to iterate on the loop body to converge, so add it to
  97. // the next set.
  98. if (auto *UserPI = dyn_cast<PHINode>(UserI))
  99. if (VisitedPHIs.count(UserPI)) {
  100. Next->insert(UserPI);
  101. continue;
  102. }
  103. // If we are only simplifying targeted instructions and the user is an
  104. // instruction in the loop body, add it to our set of targeted
  105. // instructions. Because we process defs before uses (outside of PHIs)
  106. // we won't have visited it yet.
  107. //
  108. // We also skip any uses outside of the loop being simplified. Those
  109. // should always be PHI nodes due to LCSSA form, and we don't want to
  110. // try to simplify those away.
  111. assert((L.contains(UserI) || isa<PHINode>(UserI)) &&
  112. "Uses outside the loop should be PHI nodes due to LCSSA!");
  113. if (!IsFirstIteration && L.contains(UserI))
  114. ToSimplify->insert(UserI);
  115. }
  116. if (MSSAU)
  117. if (Instruction *SimpleI = dyn_cast_or_null<Instruction>(V))
  118. if (MemoryAccess *MA = MSSA->getMemoryAccess(&I))
  119. if (MemoryAccess *ReplacementMA = MSSA->getMemoryAccess(SimpleI))
  120. MA->replaceAllUsesWith(ReplacementMA);
  121. assert(I.use_empty() && "Should always have replaced all uses!");
  122. if (isInstructionTriviallyDead(&I, &TLI))
  123. DeadInsts.push_back(&I);
  124. ++NumSimplified;
  125. Changed = true;
  126. }
  127. }
  128. // Delete any dead instructions found thus far now that we've finished an
  129. // iteration over all instructions in all the loop blocks.
  130. if (!DeadInsts.empty()) {
  131. Changed = true;
  132. RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI, MSSAU);
  133. }
  134. if (MSSAU && VerifyMemorySSA)
  135. MSSA->verifyMemorySSA();
  136. // If we never found a PHI that needs to be simplified in the next
  137. // iteration, we're done.
  138. if (Next->empty())
  139. break;
  140. // Otherwise, put the next set in place for the next iteration and reset it
  141. // and the visited PHIs for that iteration.
  142. std::swap(Next, ToSimplify);
  143. Next->clear();
  144. VisitedPHIs.clear();
  145. DeadInsts.clear();
  146. }
  147. return Changed;
  148. }
  149. namespace {
  150. class LoopInstSimplifyLegacyPass : public LoopPass {
  151. public:
  152. static char ID; // Pass ID, replacement for typeid
  153. LoopInstSimplifyLegacyPass() : LoopPass(ID) {
  154. initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
  155. }
  156. bool runOnLoop(Loop *L, LPPassManager &LPM) override {
  157. if (skipLoop(L))
  158. return false;
  159. DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  160. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  161. AssumptionCache &AC =
  162. getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
  163. *L->getHeader()->getParent());
  164. const TargetLibraryInfo &TLI =
  165. getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
  166. *L->getHeader()->getParent());
  167. MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
  168. MemorySSAUpdater MSSAU(MSSA);
  169. return simplifyLoopInst(*L, DT, LI, AC, TLI, &MSSAU);
  170. }
  171. void getAnalysisUsage(AnalysisUsage &AU) const override {
  172. AU.addRequired<AssumptionCacheTracker>();
  173. AU.addRequired<DominatorTreeWrapperPass>();
  174. AU.addRequired<TargetLibraryInfoWrapperPass>();
  175. AU.setPreservesCFG();
  176. AU.addRequired<MemorySSAWrapperPass>();
  177. AU.addPreserved<MemorySSAWrapperPass>();
  178. getLoopAnalysisUsage(AU);
  179. }
  180. };
  181. } // end anonymous namespace
  182. PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
  183. LoopStandardAnalysisResults &AR,
  184. LPMUpdater &) {
  185. std::optional<MemorySSAUpdater> MSSAU;
  186. if (AR.MSSA) {
  187. MSSAU = MemorySSAUpdater(AR.MSSA);
  188. if (VerifyMemorySSA)
  189. AR.MSSA->verifyMemorySSA();
  190. }
  191. if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI,
  192. MSSAU ? &*MSSAU : nullptr))
  193. return PreservedAnalyses::all();
  194. auto PA = getLoopPassPreservedAnalyses();
  195. PA.preserveSet<CFGAnalyses>();
  196. if (AR.MSSA)
  197. PA.preserve<MemorySSAAnalysis>();
  198. return PA;
  199. }
  200. char LoopInstSimplifyLegacyPass::ID = 0;
  201. INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
  202. "Simplify instructions in loops", false, false)
  203. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  204. INITIALIZE_PASS_DEPENDENCY(LoopPass)
  205. INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
  206. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  207. INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
  208. "Simplify instructions in loops", false, false)
  209. Pass *llvm::createLoopInstSimplifyPass() {
  210. return new LoopInstSimplifyLegacyPass();
  211. }