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