xref: /llvm-project/llvm/lib/Transforms/Scalar/LoopPredication.cpp (revision c488dfabac2702a45bac689318fa40d7426c73b0)
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   /// Represents an induction variable check:
62   ///   icmp Pred, <induction variable>, <loop invariant limit>
63   struct LoopICmp {
64     ICmpInst::Predicate Pred;
65     const SCEVAddRecExpr *IV;
66     const SCEV *Limit;
67     LoopICmp(ICmpInst::Predicate Pred, const SCEVAddRecExpr *IV,
68              const SCEV *Limit)
69         : Pred(Pred), IV(IV), Limit(Limit) {}
70     LoopICmp() {}
71   };
72 
73   ScalarEvolution *SE;
74 
75   Loop *L;
76   const DataLayout *DL;
77   BasicBlock *Preheader;
78 
79   Optional<LoopICmp> parseLoopICmp(ICmpInst *ICI);
80 
81   Value *expandCheck(SCEVExpander &Expander, IRBuilder<> &Builder,
82                      ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
83                      Instruction *InsertAt);
84 
85   Optional<Value *> widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander,
86                                         IRBuilder<> &Builder);
87   bool widenGuardConditions(IntrinsicInst *II, SCEVExpander &Expander);
88 
89 public:
90   LoopPredication(ScalarEvolution *SE) : SE(SE){};
91   bool runOnLoop(Loop *L);
92 };
93 
94 class LoopPredicationLegacyPass : public LoopPass {
95 public:
96   static char ID;
97   LoopPredicationLegacyPass() : LoopPass(ID) {
98     initializeLoopPredicationLegacyPassPass(*PassRegistry::getPassRegistry());
99   }
100 
101   void getAnalysisUsage(AnalysisUsage &AU) const override {
102     getLoopAnalysisUsage(AU);
103   }
104 
105   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
106     if (skipLoop(L))
107       return false;
108     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
109     LoopPredication LP(SE);
110     return LP.runOnLoop(L);
111   }
112 };
113 
114 char LoopPredicationLegacyPass::ID = 0;
115 } // end namespace llvm
116 
117 INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass, "loop-predication",
118                       "Loop predication", false, false)
119 INITIALIZE_PASS_DEPENDENCY(LoopPass)
120 INITIALIZE_PASS_END(LoopPredicationLegacyPass, "loop-predication",
121                     "Loop predication", false, false)
122 
123 Pass *llvm::createLoopPredicationPass() {
124   return new LoopPredicationLegacyPass();
125 }
126 
127 PreservedAnalyses LoopPredicationPass::run(Loop &L, LoopAnalysisManager &AM,
128                                            LoopStandardAnalysisResults &AR,
129                                            LPMUpdater &U) {
130   LoopPredication LP(&AR.SE);
131   if (!LP.runOnLoop(&L))
132     return PreservedAnalyses::all();
133 
134   return getLoopPassPreservedAnalyses();
135 }
136 
137 Optional<LoopPredication::LoopICmp>
138 LoopPredication::parseLoopICmp(ICmpInst *ICI) {
139   ICmpInst::Predicate Pred = ICI->getPredicate();
140 
141   Value *LHS = ICI->getOperand(0);
142   Value *RHS = ICI->getOperand(1);
143   const SCEV *LHSS = SE->getSCEV(LHS);
144   if (isa<SCEVCouldNotCompute>(LHSS))
145     return None;
146   const SCEV *RHSS = SE->getSCEV(RHS);
147   if (isa<SCEVCouldNotCompute>(RHSS))
148     return None;
149 
150   // Canonicalize RHS to be loop invariant bound, LHS - a loop computable IV
151   if (SE->isLoopInvariant(LHSS, L)) {
152     std::swap(LHS, RHS);
153     std::swap(LHSS, RHSS);
154     Pred = ICmpInst::getSwappedPredicate(Pred);
155   }
156 
157   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHSS);
158   if (!AR || AR->getLoop() != L)
159     return None;
160 
161   return LoopICmp(Pred, AR, RHSS);
162 }
163 
164 Value *LoopPredication::expandCheck(SCEVExpander &Expander,
165                                     IRBuilder<> &Builder,
166                                     ICmpInst::Predicate Pred, const SCEV *LHS,
167                                     const SCEV *RHS, Instruction *InsertAt) {
168   Type *Ty = LHS->getType();
169   assert(Ty == RHS->getType() && "expandCheck operands have different types?");
170   Value *LHSV = Expander.expandCodeFor(LHS, Ty, InsertAt);
171   Value *RHSV = Expander.expandCodeFor(RHS, Ty, InsertAt);
172   return Builder.CreateICmp(Pred, LHSV, RHSV);
173 }
174 
175 /// If ICI can be widened to a loop invariant condition emits the loop
176 /// invariant condition in the loop preheader and return it, otherwise
177 /// returns None.
178 Optional<Value *> LoopPredication::widenICmpRangeCheck(ICmpInst *ICI,
179                                                        SCEVExpander &Expander,
180                                                        IRBuilder<> &Builder) {
181   DEBUG(dbgs() << "Analyzing ICmpInst condition:\n");
182   DEBUG(ICI->dump());
183 
184   auto RangeCheck = parseLoopICmp(ICI);
185   if (!RangeCheck)
186     return None;
187 
188   ICmpInst::Predicate Pred = RangeCheck->Pred;
189   const SCEVAddRecExpr *IndexAR = RangeCheck->IV;
190   const SCEV *RHSS = RangeCheck->Limit;
191 
192   auto CanExpand = [this](const SCEV *S) {
193     return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE);
194   };
195   if (!CanExpand(RHSS))
196     return None;
197 
198   DEBUG(dbgs() << "IndexAR: ");
199   DEBUG(IndexAR->dump());
200 
201   bool IsIncreasing = false;
202   if (!SE->isMonotonicPredicate(IndexAR, Pred, IsIncreasing))
203     return None;
204 
205   // If the predicate is increasing the condition can change from false to true
206   // as the loop progresses, in this case take the value on the first iteration
207   // for the widened check. Otherwise the condition can change from true to
208   // false as the loop progresses, so take the value on the last iteration.
209   const SCEV *NewLHSS = IsIncreasing
210                             ? IndexAR->getStart()
211                             : SE->getSCEVAtScope(IndexAR, L->getParentLoop());
212   if (NewLHSS == IndexAR) {
213     DEBUG(dbgs() << "Can't compute NewLHSS!\n");
214     return None;
215   }
216 
217   DEBUG(dbgs() << "NewLHSS: ");
218   DEBUG(NewLHSS->dump());
219 
220   if (!CanExpand(NewLHSS))
221     return None;
222 
223   DEBUG(dbgs() << "NewLHSS is loop invariant and safe to expand. Expand!\n");
224 
225   Instruction *InsertAt = Preheader->getTerminator();
226   return expandCheck(Expander, Builder, Pred, NewLHSS, RHSS, InsertAt);
227 }
228 
229 bool LoopPredication::widenGuardConditions(IntrinsicInst *Guard,
230                                            SCEVExpander &Expander) {
231   DEBUG(dbgs() << "Processing guard:\n");
232   DEBUG(Guard->dump());
233 
234   IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator()));
235 
236   // The guard condition is expected to be in form of:
237   //   cond1 && cond2 && cond3 ...
238   // Iterate over subconditions looking for for icmp conditions which can be
239   // widened across loop iterations. Widening these conditions remember the
240   // resulting list of subconditions in Checks vector.
241   SmallVector<Value *, 4> Worklist(1, Guard->getOperand(0));
242   SmallPtrSet<Value *, 4> Visited;
243 
244   SmallVector<Value *, 4> Checks;
245 
246   unsigned NumWidened = 0;
247   do {
248     Value *Condition = Worklist.pop_back_val();
249     if (!Visited.insert(Condition).second)
250       continue;
251 
252     Value *LHS, *RHS;
253     using namespace llvm::PatternMatch;
254     if (match(Condition, m_And(m_Value(LHS), m_Value(RHS)))) {
255       Worklist.push_back(LHS);
256       Worklist.push_back(RHS);
257       continue;
258     }
259 
260     if (ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) {
261       if (auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, Builder)) {
262         Checks.push_back(NewRangeCheck.getValue());
263         NumWidened++;
264         continue;
265       }
266     }
267 
268     // Save the condition as is if we can't widen it
269     Checks.push_back(Condition);
270   } while (Worklist.size() != 0);
271 
272   if (NumWidened == 0)
273     return false;
274 
275   // Emit the new guard condition
276   Builder.SetInsertPoint(Guard);
277   Value *LastCheck = nullptr;
278   for (auto *Check : Checks)
279     if (!LastCheck)
280       LastCheck = Check;
281     else
282       LastCheck = Builder.CreateAnd(LastCheck, Check);
283   Guard->setOperand(0, LastCheck);
284 
285   DEBUG(dbgs() << "Widened checks = " << NumWidened << "\n");
286   return true;
287 }
288 
289 bool LoopPredication::runOnLoop(Loop *Loop) {
290   L = Loop;
291 
292   DEBUG(dbgs() << "Analyzing ");
293   DEBUG(L->dump());
294 
295   Module *M = L->getHeader()->getModule();
296 
297   // There is nothing to do if the module doesn't use guards
298   auto *GuardDecl =
299       M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard));
300   if (!GuardDecl || GuardDecl->use_empty())
301     return false;
302 
303   DL = &M->getDataLayout();
304 
305   Preheader = L->getLoopPreheader();
306   if (!Preheader)
307     return false;
308 
309   // Collect all the guards into a vector and process later, so as not
310   // to invalidate the instruction iterator.
311   SmallVector<IntrinsicInst *, 4> Guards;
312   for (const auto BB : L->blocks())
313     for (auto &I : *BB)
314       if (auto *II = dyn_cast<IntrinsicInst>(&I))
315         if (II->getIntrinsicID() == Intrinsic::experimental_guard)
316           Guards.push_back(II);
317 
318   if (Guards.empty())
319     return false;
320 
321   SCEVExpander Expander(*SE, *DL, "loop-predication");
322 
323   bool Changed = false;
324   for (auto *Guard : Guards)
325     Changed |= widenGuardConditions(Guard, Expander);
326 
327   return Changed;
328 }
329