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