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