Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 1 | //===-- LoopPredication.cpp - Guard based loop predication 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 | // The LoopPredication pass tries to convert loop variant range checks to loop |
| 11 | // invariant by widening checks across loop iterations. For example, it will |
| 12 | // convert |
| 13 | // |
| 14 | // for (i = 0; i < n; i++) { |
| 15 | // guard(i < len); |
| 16 | // ... |
| 17 | // } |
| 18 | // |
| 19 | // to |
| 20 | // |
| 21 | // for (i = 0; i < n; i++) { |
| 22 | // guard(n - 1 < len); |
| 23 | // ... |
| 24 | // } |
| 25 | // |
| 26 | // After this transformation the condition of the guard is loop invariant, so |
| 27 | // loop-unswitch can later unswitch the loop by this condition which basically |
| 28 | // predicates the loop by the widened condition: |
| 29 | // |
| 30 | // if (n - 1 < len) |
| 31 | // for (i = 0; i < n; i++) { |
| 32 | // ... |
| 33 | // } |
| 34 | // else |
| 35 | // deoptimize |
| 36 | // |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | #include "llvm/Transforms/Scalar/LoopPredication.h" |
| 40 | #include "llvm/Pass.h" |
| 41 | #include "llvm/Analysis/LoopInfo.h" |
| 42 | #include "llvm/Analysis/LoopPass.h" |
| 43 | #include "llvm/Analysis/ScalarEvolution.h" |
| 44 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 45 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 46 | #include "llvm/IR/Function.h" |
| 47 | #include "llvm/IR/GlobalValue.h" |
| 48 | #include "llvm/IR/IntrinsicInst.h" |
| 49 | #include "llvm/IR/Module.h" |
| 50 | #include "llvm/IR/PatternMatch.h" |
| 51 | #include "llvm/Support/Debug.h" |
| 52 | #include "llvm/Transforms/Scalar.h" |
| 53 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 54 | |
| 55 | #define DEBUG_TYPE "loop-predication" |
| 56 | |
| 57 | using namespace llvm; |
| 58 | |
| 59 | namespace { |
| 60 | class LoopPredication { |
| 61 | ScalarEvolution *SE; |
| 62 | |
| 63 | Loop *L; |
| 64 | const DataLayout *DL; |
| 65 | BasicBlock *Preheader; |
| 66 | |
| 67 | Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander, |
| 68 | IRBuilder<> &Builder); |
| 69 | bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander); |
| 70 | |
| 71 | public: |
| 72 | LoopPredication(ScalarEvolution *SE) : SE(SE){}; |
| 73 | bool runOnLoop(Loop *L); |
| 74 | }; |
| 75 | |
| 76 | class LoopPredicationLegacyPass : public LoopPass { |
| 77 | public: |
| 78 | static char ID; |
| 79 | LoopPredicationLegacyPass() : LoopPass(ID) { |
| 80 | initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 81 | } |
| 82 | |
| 83 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 84 | getLoopAnalysisUsage(AU); |
| 85 | } |
| 86 | |
| 87 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 88 | if (skipLoop(L)) |
| 89 | return false; |
| 90 | auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 91 | LoopPredication LP(SE); |
| 92 | return LP.runOnLoop(L); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | char LoopPredicationLegacyPass::ID = 0; |
| 97 | } // end namespace llvm |
| 98 | |
| 99 | INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication", |
| 100 | "Loop predication", false, false) |
| 101 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 102 | INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication", |
| 103 | "Loop predication", false, false) |
| 104 | |
| 105 | Pass *llvm::createLoopPredicationPass() { |
| 106 | return new LoopPredicationLegacyPass(); |
| 107 | } |
| 108 | |
| 109 | PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM, |
| 110 | LoopStandardAnalysisResults &AR, |
| 111 | LPMUpdater &U) { |
| 112 | LoopPredication LP(&AR.SE); |
| 113 | if (!LP.runOnLoop(&L)) |
| 114 | return PreservedAnalyses::all(); |
| 115 | |
| 116 | return getLoopPassPreservedAnalyses(); |
| 117 | } |
| 118 | |
| 119 | /// If ICI can be widened to a loop invariant condition emits the loop |
| 120 | /// invariant condition in the loop preheader and return it, otherwise |
| 121 | /// returns None. |
| 122 | Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI, |
| 123 | SCEVExpander &Expander, |
| 124 | IRBuilder<> &Builder) { |
| 125 | DEBUG(dbgs() << "Analyzing ICmpInst condition:\n"); |
| 126 | DEBUG(ICI->dump()); |
| 127 | |
| 128 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 129 | Value *LHS = ICI->getOperand(0); |
| 130 | Value *RHS = ICI->getOperand(1); |
| 131 | const SCEV *LHSS = SE->getSCEV(LHS); |
| 132 | if (isa<SCEVCouldNotCompute>(LHSS)) |
| 133 | return None; |
| 134 | const SCEV *RHSS = SE->getSCEV(RHS); |
| 135 | if (isa<SCEVCouldNotCompute>(RHSS)) |
| 136 | return None; |
| 137 | |
| 138 | // Canonicalize RHS to be loop invariant bound, LHS - a loop computable index |
| 139 | if (SE->isLoopInvariant(LHSS, L)) { |
| 140 | std::swap(LHS, RHS); |
| 141 | std::swap(LHSS, RHSS); |
| 142 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 143 | } |
| 144 | if (!SE->isLoopInvariant(RHSS, L)) |
| 145 | return None; |
| 146 | |
| 147 | Value *Bound = RHS; |
| 148 | const SCEVAddRecExpr *IndexAR = dyn_cast<SCEVAddRecExpr>(LHSS); |
| 149 | if (!IndexAR || IndexAR->getLoop() != L) |
| 150 | return None; |
| 151 | |
| 152 | DEBUG(dbgs() << "IndexAR: "); |
| 153 | DEBUG(IndexAR->dump()); |
| 154 | |
| 155 | bool IsIncreasing = false; |
| 156 | if (!SE->isMonotonicPredicate(IndexAR, Pred, IsIncreasing)) |
| 157 | return None; |
| 158 | |
| 159 | // If the predicate is increasing the condition can change from false to true |
| 160 | // as the loop progresses, in this case take the value on the first iteration |
| 161 | // for the widened check. Otherwise the condition can change from true to |
| 162 | // false as the loop progresses, so take the value on the last iteration. |
| 163 | const SCEV *NewLHSS = IsIncreasing |
| 164 | ? IndexAR->getStart() |
| 165 | : SE->getSCEVAtScope(IndexAR, L->getParentLoop()); |
| 166 | if (NewLHSS == IndexAR) { |
Artur Pilipenko | 2cbaded | 2017-02-01 12:25:38 +0000 | [diff] [blame^] | 167 | DEBUG(dbgs() << "Can't compute NewLHSS!\n"); |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 168 | return None; |
| 169 | } |
| 170 | |
| 171 | DEBUG(dbgs() << "NewLHSS: "); |
| 172 | DEBUG(NewLHSS->dump()); |
| 173 | |
| 174 | if (!SE->isLoopInvariant(NewLHSS, L) || !isSafeToExpand(NewLHSS, *SE)) |
| 175 | return None; |
| 176 | |
| 177 | DEBUG(dbgs() << "NewLHSS is loop invariant and safe to expand. Expand!\n"); |
| 178 | |
| 179 | Value *NewLHS = Expander.expandCodeFor(NewLHSS, Bound->getType(), |
| 180 | Preheader->getTerminator()); |
| 181 | return Builder.CreateICmp(Pred, NewLHS, Bound); |
| 182 | } |
| 183 | |
| 184 | bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard, |
| 185 | SCEVExpander &Expander) { |
| 186 | DEBUG(dbgs() << "Processing guard:\n"); |
| 187 | DEBUG(Guard->dump()); |
| 188 | |
| 189 | IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator())); |
| 190 | |
| 191 | // The guard condition is expected to be in form of: |
| 192 | // cond1 && cond2 && cond3 ... |
| 193 | // Iterate over subconditions looking for for icmp conditions which can be |
| 194 | // widened across loop iterations. Widening these conditions remember the |
| 195 | // resulting list of subconditions in Checks vector. |
| 196 | SmallVector<Value *, 4> Worklist(1, Guard->getOperand(0)); |
| 197 | SmallPtrSet<Value *, 4> Visited; |
| 198 | |
| 199 | SmallVector<Value *, 4> Checks; |
| 200 | |
| 201 | unsigned NumWidened = 0; |
| 202 | do { |
| 203 | Value *Condition = Worklist.pop_back_val(); |
| 204 | if (!Visited.insert(Condition).second) |
| 205 | continue; |
| 206 | |
| 207 | Value *LHS, *RHS; |
| 208 | using namespace llvm::PatternMatch; |
| 209 | if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) { |
| 210 | Worklist.push_back(LHS); |
| 211 | Worklist.push_back(RHS); |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) { |
| 216 | if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, Builder)) { |
| 217 | Checks.push_back(NewRangeCheck.getValue()); |
| 218 | NumWidened++; |
| 219 | continue; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Save the condition as is if we can't widen it |
| 224 | Checks.push_back(Condition); |
| 225 | } while (Worklist.size() != 0); |
| 226 | |
| 227 | if (NumWidened == 0) |
| 228 | return false; |
| 229 | |
| 230 | // Emit the new guard condition |
| 231 | Builder.SetInsertPoint(Guard); |
| 232 | Value *LastCheck = nullptr; |
| 233 | for (auto *Check : Checks) |
| 234 | if (!LastCheck) |
| 235 | LastCheck = Check; |
| 236 | else |
| 237 | LastCheck = Builder.CreateAnd(LastCheck, Check); |
| 238 | Guard->setOperand(0, LastCheck); |
| 239 | |
| 240 | DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n"); |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | bool LoopPredication::runOnLoop(Loop *Loop) { |
| 245 | L = Loop; |
| 246 | |
| 247 | DEBUG(dbgs() << "Analyzing "); |
| 248 | DEBUG(L->dump()); |
| 249 | |
| 250 | Module *M = L->getHeader()->getModule(); |
| 251 | |
| 252 | // There is nothing to do if the module doesn't use guards |
| 253 | auto *GuardDecl = |
| 254 | M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard)); |
| 255 | if (!GuardDecl || GuardDecl->use_empty()) |
| 256 | return false; |
| 257 | |
| 258 | DL = &M->getDataLayout(); |
| 259 | |
| 260 | Preheader = L->getLoopPreheader(); |
| 261 | if (!Preheader) |
| 262 | return false; |
| 263 | |
| 264 | // Collect all the guards into a vector and process later, so as not |
| 265 | // to invalidate the instruction iterator. |
| 266 | SmallVector<IntrinsicInst *, 4> Guards; |
| 267 | for (const auto BB : L->blocks()) |
| 268 | for (auto &I : *BB) |
| 269 | if (auto *II = dyn_cast<IntrinsicInst>(&I)) |
| 270 | if (II->getIntrinsicID() == Intrinsic::experimental_guard) |
| 271 | Guards.push_back(II); |
| 272 | |
| 273 | SCEVExpander Expander(*SE, *DL, "loop-predication"); |
| 274 | |
| 275 | bool Changed = false; |
| 276 | for (auto *Guard : Guards) |
| 277 | Changed |= widenGuardConditions(Guard, Expander); |
| 278 | |
| 279 | return Changed; |
| 280 | } |