1 //===- llvm/Analysis/IVDescriptors.cpp - IndVar Descriptors -----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file "describes" induction and recurrence variables.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Analysis/IVDescriptors.h"
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/Analysis/BasicAliasAnalysis.h"
16 #include "llvm/Analysis/DemandedBits.h"
17 #include "llvm/Analysis/DomTreeUpdater.h"
18 #include "llvm/Analysis/GlobalsModRef.h"
19 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/Analysis/MustExecute.h"
23 #include "llvm/Analysis/ScalarEvolution.h"
24 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
25 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/Dominators.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/PatternMatch.h"
32 #include "llvm/IR/ValueHandle.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/KnownBits.h"
36 
37 using namespace llvm;
38 using namespace llvm::PatternMatch;
39 
40 #define DEBUG_TYPE "iv-descriptors"
41 
42 bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
43                                         SmallPtrSetImpl<Instruction *> &Set) {
44   for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use)
45     if (!Set.count(dyn_cast<Instruction>(*Use)))
46       return false;
47   return true;
48 }
49 
50 bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind) {
51   switch (Kind) {
52   default:
53     break;
54   case RecurKind::Add:
55   case RecurKind::Mul:
56   case RecurKind::Or:
57   case RecurKind::And:
58   case RecurKind::Xor:
59   case RecurKind::SMax:
60   case RecurKind::SMin:
61   case RecurKind::UMax:
62   case RecurKind::UMin:
63     return true;
64   }
65   return false;
66 }
67 
68 bool RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurKind Kind) {
69   return (Kind != RecurKind::None) && !isIntegerRecurrenceKind(Kind);
70 }
71 
72 bool RecurrenceDescriptor::isArithmeticRecurrenceKind(RecurKind Kind) {
73   switch (Kind) {
74   default:
75     break;
76   case RecurKind::Add:
77   case RecurKind::Mul:
78   case RecurKind::FAdd:
79   case RecurKind::FMul:
80     return true;
81   }
82   return false;
83 }
84 
85 /// Determines if Phi may have been type-promoted. If Phi has a single user
86 /// that ANDs the Phi with a type mask, return the user. RT is updated to
87 /// account for the narrower bit width represented by the mask, and the AND
88 /// instruction is added to CI.
89 static Instruction *lookThroughAnd(PHINode *Phi, Type *&RT,
90                                    SmallPtrSetImpl<Instruction *> &Visited,
91                                    SmallPtrSetImpl<Instruction *> &CI) {
92   if (!Phi->hasOneUse())
93     return Phi;
94 
95   const APInt *M = nullptr;
96   Instruction *I, *J = cast<Instruction>(Phi->use_begin()->getUser());
97 
98   // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT
99   // with a new integer type of the corresponding bit width.
100   if (match(J, m_c_And(m_Instruction(I), m_APInt(M)))) {
101     int32_t Bits = (*M + 1).exactLogBase2();
102     if (Bits > 0) {
103       RT = IntegerType::get(Phi->getContext(), Bits);
104       Visited.insert(Phi);
105       CI.insert(J);
106       return J;
107     }
108   }
109   return Phi;
110 }
111 
112 /// Compute the minimal bit width needed to represent a reduction whose exit
113 /// instruction is given by Exit.
114 static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
115                                                      DemandedBits *DB,
116                                                      AssumptionCache *AC,
117                                                      DominatorTree *DT) {
118   bool IsSigned = false;
119   const DataLayout &DL = Exit->getModule()->getDataLayout();
120   uint64_t MaxBitWidth = DL.getTypeSizeInBits(Exit->getType());
121 
122   if (DB) {
123     // Use the demanded bits analysis to determine the bits that are live out
124     // of the exit instruction, rounding up to the nearest power of two. If the
125     // use of demanded bits results in a smaller bit width, we know the value
126     // must be positive (i.e., IsSigned = false), because if this were not the
127     // case, the sign bit would have been demanded.
128     auto Mask = DB->getDemandedBits(Exit);
129     MaxBitWidth = Mask.getBitWidth() - Mask.countLeadingZeros();
130   }
131 
132   if (MaxBitWidth == DL.getTypeSizeInBits(Exit->getType()) && AC && DT) {
133     // If demanded bits wasn't able to limit the bit width, we can try to use
134     // value tracking instead. This can be the case, for example, if the value
135     // may be negative.
136     auto NumSignBits = ComputeNumSignBits(Exit, DL, 0, AC, nullptr, DT);
137     auto NumTypeBits = DL.getTypeSizeInBits(Exit->getType());
138     MaxBitWidth = NumTypeBits - NumSignBits;
139     KnownBits Bits = computeKnownBits(Exit, DL);
140     if (!Bits.isNonNegative()) {
141       // If the value is not known to be non-negative, we set IsSigned to true,
142       // meaning that we will use sext instructions instead of zext
143       // instructions to restore the original type.
144       IsSigned = true;
145       if (!Bits.isNegative())
146         // If the value is not known to be negative, we don't known what the
147         // upper bit is, and therefore, we don't know what kind of extend we
148         // will need. In this case, just increase the bit width by one bit and
149         // use sext.
150         ++MaxBitWidth;
151     }
152   }
153   if (!isPowerOf2_64(MaxBitWidth))
154     MaxBitWidth = NextPowerOf2(MaxBitWidth);
155 
156   return std::make_pair(Type::getIntNTy(Exit->getContext(), MaxBitWidth),
157                         IsSigned);
158 }
159 
160 /// Collect cast instructions that can be ignored in the vectorizer's cost
161 /// model, given a reduction exit value and the minimal type in which the
162 /// reduction can be represented.
163 static void collectCastsToIgnore(Loop *TheLoop, Instruction *Exit,
164                                  Type *RecurrenceType,
165                                  SmallPtrSetImpl<Instruction *> &Casts) {
166 
167   SmallVector<Instruction *, 8> Worklist;
168   SmallPtrSet<Instruction *, 8> Visited;
169   Worklist.push_back(Exit);
170 
171   while (!Worklist.empty()) {
172     Instruction *Val = Worklist.pop_back_val();
173     Visited.insert(Val);
174     if (auto *Cast = dyn_cast<CastInst>(Val))
175       if (Cast->getSrcTy() == RecurrenceType) {
176         // If the source type of a cast instruction is equal to the recurrence
177         // type, it will be eliminated, and should be ignored in the vectorizer
178         // cost model.
179         Casts.insert(Cast);
180         continue;
181       }
182 
183     // Add all operands to the work list if they are loop-varying values that
184     // we haven't yet visited.
185     for (Value *O : cast<User>(Val)->operands())
186       if (auto *I = dyn_cast<Instruction>(O))
187         if (TheLoop->contains(I) && !Visited.count(I))
188           Worklist.push_back(I);
189   }
190 }
191 
192 bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurKind Kind,
193                                            Loop *TheLoop, bool HasFunNoNaNAttr,
194                                            RecurrenceDescriptor &RedDes,
195                                            DemandedBits *DB,
196                                            AssumptionCache *AC,
197                                            DominatorTree *DT) {
198   if (Phi->getNumIncomingValues() != 2)
199     return false;
200 
201   // Reduction variables are only found in the loop header block.
202   if (Phi->getParent() != TheLoop->getHeader())
203     return false;
204 
205   // Obtain the reduction start value from the value that comes from the loop
206   // preheader.
207   Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
208 
209   // ExitInstruction is the single value which is used outside the loop.
210   // We only allow for a single reduction value to be used outside the loop.
211   // This includes users of the reduction, variables (which form a cycle
212   // which ends in the phi node).
213   Instruction *ExitInstruction = nullptr;
214   // Indicates that we found a reduction operation in our scan.
215   bool FoundReduxOp = false;
216 
217   // We start with the PHI node and scan for all of the users of this
218   // instruction. All users must be instructions that can be used as reduction
219   // variables (such as ADD). We must have a single out-of-block user. The cycle
220   // must include the original PHI.
221   bool FoundStartPHI = false;
222 
223   // To recognize min/max patterns formed by a icmp select sequence, we store
224   // the number of instruction we saw from the recognized min/max pattern,
225   //  to make sure we only see exactly the two instructions.
226   unsigned NumCmpSelectPatternInst = 0;
227   InstDesc ReduxDesc(false, nullptr);
228 
229   // Data used for determining if the recurrence has been type-promoted.
230   Type *RecurrenceType = Phi->getType();
231   SmallPtrSet<Instruction *, 4> CastInsts;
232   Instruction *Start = Phi;
233   bool IsSigned = false;
234 
235   SmallPtrSet<Instruction *, 8> VisitedInsts;
236   SmallVector<Instruction *, 8> Worklist;
237 
238   // Return early if the recurrence kind does not match the type of Phi. If the
239   // recurrence kind is arithmetic, we attempt to look through AND operations
240   // resulting from the type promotion performed by InstCombine.  Vector
241   // operations are not limited to the legal integer widths, so we may be able
242   // to evaluate the reduction in the narrower width.
243   if (RecurrenceType->isFloatingPointTy()) {
244     if (!isFloatingPointRecurrenceKind(Kind))
245       return false;
246   } else {
247     if (!isIntegerRecurrenceKind(Kind))
248       return false;
249     if (isArithmeticRecurrenceKind(Kind))
250       Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts);
251   }
252 
253   Worklist.push_back(Start);
254   VisitedInsts.insert(Start);
255 
256   // Start with all flags set because we will intersect this with the reduction
257   // flags from all the reduction operations.
258   FastMathFlags FMF = FastMathFlags::getFast();
259 
260   // A value in the reduction can be used:
261   //  - By the reduction:
262   //      - Reduction operation:
263   //        - One use of reduction value (safe).
264   //        - Multiple use of reduction value (not safe).
265   //      - PHI:
266   //        - All uses of the PHI must be the reduction (safe).
267   //        - Otherwise, not safe.
268   //  - By instructions outside of the loop (safe).
269   //      * One value may have several outside users, but all outside
270   //        uses must be of the same value.
271   //  - By an instruction that is not part of the reduction (not safe).
272   //    This is either:
273   //      * An instruction type other than PHI or the reduction operation.
274   //      * A PHI in the header other than the initial PHI.
275   while (!Worklist.empty()) {
276     Instruction *Cur = Worklist.back();
277     Worklist.pop_back();
278 
279     // No Users.
280     // If the instruction has no users then this is a broken chain and can't be
281     // a reduction variable.
282     if (Cur->use_empty())
283       return false;
284 
285     bool IsAPhi = isa<PHINode>(Cur);
286 
287     // A header PHI use other than the original PHI.
288     if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
289       return false;
290 
291     // Reductions of instructions such as Div, and Sub is only possible if the
292     // LHS is the reduction variable.
293     if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
294         !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
295         !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
296       return false;
297 
298     // Any reduction instruction must be of one of the allowed kinds. We ignore
299     // the starting value (the Phi or an AND instruction if the Phi has been
300     // type-promoted).
301     if (Cur != Start) {
302       ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
303       if (!ReduxDesc.isRecurrence())
304         return false;
305       // FIXME: FMF is allowed on phi, but propagation is not handled correctly.
306       if (isa<FPMathOperator>(ReduxDesc.getPatternInst()) && !IsAPhi)
307         FMF &= ReduxDesc.getPatternInst()->getFastMathFlags();
308       // Update this reduction kind if we matched a new instruction.
309       // TODO: Can we eliminate the need for a 2nd InstDesc by keeping 'Kind'
310       //       state accurate while processing the worklist?
311       if (ReduxDesc.getRecKind() != RecurKind::None)
312         Kind = ReduxDesc.getRecKind();
313     }
314 
315     bool IsASelect = isa<SelectInst>(Cur);
316 
317     // A conditional reduction operation must only have 2 or less uses in
318     // VisitedInsts.
319     if (IsASelect && (Kind == RecurKind::FAdd || Kind == RecurKind::FMul) &&
320         hasMultipleUsesOf(Cur, VisitedInsts, 2))
321       return false;
322 
323     // A reduction operation must only have one use of the reduction value.
324     if (!IsAPhi && !IsASelect && !isMinMaxRecurrenceKind(Kind) &&
325         hasMultipleUsesOf(Cur, VisitedInsts, 1))
326       return false;
327 
328     // All inputs to a PHI node must be a reduction value.
329     if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
330       return false;
331 
332     if (isIntMinMaxRecurrenceKind(Kind) &&
333         (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
334       ++NumCmpSelectPatternInst;
335     if (isFPMinMaxRecurrenceKind(Kind) &&
336         (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
337       ++NumCmpSelectPatternInst;
338 
339     // Check  whether we found a reduction operator.
340     FoundReduxOp |= !IsAPhi && Cur != Start;
341 
342     // Process users of current instruction. Push non-PHI nodes after PHI nodes
343     // onto the stack. This way we are going to have seen all inputs to PHI
344     // nodes once we get to them.
345     SmallVector<Instruction *, 8> NonPHIs;
346     SmallVector<Instruction *, 8> PHIs;
347     for (User *U : Cur->users()) {
348       Instruction *UI = cast<Instruction>(U);
349 
350       // Check if we found the exit user.
351       BasicBlock *Parent = UI->getParent();
352       if (!TheLoop->contains(Parent)) {
353         // If we already know this instruction is used externally, move on to
354         // the next user.
355         if (ExitInstruction == Cur)
356           continue;
357 
358         // Exit if you find multiple values used outside or if the header phi
359         // node is being used. In this case the user uses the value of the
360         // previous iteration, in which case we would loose "VF-1" iterations of
361         // the reduction operation if we vectorize.
362         if (ExitInstruction != nullptr || Cur == Phi)
363           return false;
364 
365         // The instruction used by an outside user must be the last instruction
366         // before we feed back to the reduction phi. Otherwise, we loose VF-1
367         // operations on the value.
368         if (!is_contained(Phi->operands(), Cur))
369           return false;
370 
371         ExitInstruction = Cur;
372         continue;
373       }
374 
375       // Process instructions only once (termination). Each reduction cycle
376       // value must only be used once, except by phi nodes and min/max
377       // reductions which are represented as a cmp followed by a select.
378       InstDesc IgnoredVal(false, nullptr);
379       if (VisitedInsts.insert(UI).second) {
380         if (isa<PHINode>(UI))
381           PHIs.push_back(UI);
382         else
383           NonPHIs.push_back(UI);
384       } else if (!isa<PHINode>(UI) &&
385                  ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
386                    !isa<SelectInst>(UI)) ||
387                   (!isConditionalRdxPattern(Kind, UI).isRecurrence() &&
388                    !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence())))
389         return false;
390 
391       // Remember that we completed the cycle.
392       if (UI == Phi)
393         FoundStartPHI = true;
394     }
395     Worklist.append(PHIs.begin(), PHIs.end());
396     Worklist.append(NonPHIs.begin(), NonPHIs.end());
397   }
398 
399   // This means we have seen one but not the other instruction of the
400   // pattern or more than just a select and cmp.
401   if (isMinMaxRecurrenceKind(Kind) && NumCmpSelectPatternInst != 2)
402     return false;
403 
404   if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
405     return false;
406 
407   if (Start != Phi) {
408     // If the starting value is not the same as the phi node, we speculatively
409     // looked through an 'and' instruction when evaluating a potential
410     // arithmetic reduction to determine if it may have been type-promoted.
411     //
412     // We now compute the minimal bit width that is required to represent the
413     // reduction. If this is the same width that was indicated by the 'and', we
414     // can represent the reduction in the smaller type. The 'and' instruction
415     // will be eliminated since it will essentially be a cast instruction that
416     // can be ignore in the cost model. If we compute a different type than we
417     // did when evaluating the 'and', the 'and' will not be eliminated, and we
418     // will end up with different kinds of operations in the recurrence
419     // expression (e.g., IntegerAND, IntegerADD). We give up if this is
420     // the case.
421     //
422     // The vectorizer relies on InstCombine to perform the actual
423     // type-shrinking. It does this by inserting instructions to truncate the
424     // exit value of the reduction to the width indicated by RecurrenceType and
425     // then extend this value back to the original width. If IsSigned is false,
426     // a 'zext' instruction will be generated; otherwise, a 'sext' will be
427     // used.
428     //
429     // TODO: We should not rely on InstCombine to rewrite the reduction in the
430     //       smaller type. We should just generate a correctly typed expression
431     //       to begin with.
432     Type *ComputedType;
433     std::tie(ComputedType, IsSigned) =
434         computeRecurrenceType(ExitInstruction, DB, AC, DT);
435     if (ComputedType != RecurrenceType)
436       return false;
437 
438     // The recurrence expression will be represented in a narrower type. If
439     // there are any cast instructions that will be unnecessary, collect them
440     // in CastInsts. Note that the 'and' instruction was already included in
441     // this list.
442     //
443     // TODO: A better way to represent this may be to tag in some way all the
444     //       instructions that are a part of the reduction. The vectorizer cost
445     //       model could then apply the recurrence type to these instructions,
446     //       without needing a white list of instructions to ignore.
447     //       This may also be useful for the inloop reductions, if it can be
448     //       kept simple enough.
449     collectCastsToIgnore(TheLoop, ExitInstruction, RecurrenceType, CastInsts);
450   }
451 
452   // We found a reduction var if we have reached the original phi node and we
453   // only have a single instruction with out-of-loop users.
454 
455   // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
456   // is saved as part of the RecurrenceDescriptor.
457 
458   // Save the description of this reduction variable.
459   RecurrenceDescriptor RD(RdxStart, ExitInstruction, Kind, FMF,
460                           ReduxDesc.getUnsafeAlgebraInst(), RecurrenceType,
461                           IsSigned, CastInsts);
462   RedDes = RD;
463 
464   return true;
465 }
466 
467 RecurrenceDescriptor::InstDesc
468 RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I,
469                                                const InstDesc &Prev) {
470   assert((isa<CmpInst>(I) || isa<SelectInst>(I)) &&
471          "Expected a cmp or select instruction");
472 
473   // We must handle the select(cmp()) as a single instruction. Advance to the
474   // select.
475   CmpInst::Predicate Pred;
476   if (match(I, m_OneUse(m_Cmp(Pred, m_Value(), m_Value())))) {
477     if (auto *Select = dyn_cast<SelectInst>(*I->user_begin()))
478       return InstDesc(Select, Prev.getRecKind());
479   }
480 
481   // Only match select with single use cmp condition.
482   if (!match(I, m_Select(m_OneUse(m_Cmp(Pred, m_Value(), m_Value())), m_Value(),
483                          m_Value())))
484     return InstDesc(false, I);
485 
486   // Look for a min/max pattern.
487   if (match(I, m_UMin(m_Value(), m_Value())))
488     return InstDesc(I, RecurKind::UMin);
489   if (match(I, m_UMax(m_Value(), m_Value())))
490     return InstDesc(I, RecurKind::UMax);
491   if (match(I, m_SMax(m_Value(), m_Value())))
492     return InstDesc(I, RecurKind::SMax);
493   if (match(I, m_SMin(m_Value(), m_Value())))
494     return InstDesc(I, RecurKind::SMin);
495   if (match(I, m_OrdFMin(m_Value(), m_Value())))
496     return InstDesc(I, RecurKind::FMin);
497   if (match(I, m_OrdFMax(m_Value(), m_Value())))
498     return InstDesc(I, RecurKind::FMax);
499   if (match(I, m_UnordFMin(m_Value(), m_Value())))
500     return InstDesc(I, RecurKind::FMin);
501   if (match(I, m_UnordFMax(m_Value(), m_Value())))
502     return InstDesc(I, RecurKind::FMax);
503 
504   return InstDesc(false, I);
505 }
506 
507 /// Returns true if the select instruction has users in the compare-and-add
508 /// reduction pattern below. The select instruction argument is the last one
509 /// in the sequence.
510 ///
511 /// %sum.1 = phi ...
512 /// ...
513 /// %cmp = fcmp pred %0, %CFP
514 /// %add = fadd %0, %sum.1
515 /// %sum.2 = select %cmp, %add, %sum.1
516 RecurrenceDescriptor::InstDesc
517 RecurrenceDescriptor::isConditionalRdxPattern(RecurKind Kind, Instruction *I) {
518   SelectInst *SI = dyn_cast<SelectInst>(I);
519   if (!SI)
520     return InstDesc(false, I);
521 
522   CmpInst *CI = dyn_cast<CmpInst>(SI->getCondition());
523   // Only handle single use cases for now.
524   if (!CI || !CI->hasOneUse())
525     return InstDesc(false, I);
526 
527   Value *TrueVal = SI->getTrueValue();
528   Value *FalseVal = SI->getFalseValue();
529   // Handle only when either of operands of select instruction is a PHI
530   // node for now.
531   if ((isa<PHINode>(*TrueVal) && isa<PHINode>(*FalseVal)) ||
532       (!isa<PHINode>(*TrueVal) && !isa<PHINode>(*FalseVal)))
533     return InstDesc(false, I);
534 
535   Instruction *I1 =
536       isa<PHINode>(*TrueVal) ? dyn_cast<Instruction>(FalseVal)
537                              : dyn_cast<Instruction>(TrueVal);
538   if (!I1 || !I1->isBinaryOp())
539     return InstDesc(false, I);
540 
541   Value *Op1, *Op2;
542   if ((m_FAdd(m_Value(Op1), m_Value(Op2)).match(I1)  ||
543        m_FSub(m_Value(Op1), m_Value(Op2)).match(I1)) &&
544       I1->isFast())
545     return InstDesc(Kind == RecurKind::FAdd, SI);
546 
547   if (m_FMul(m_Value(Op1), m_Value(Op2)).match(I1) && (I1->isFast()))
548     return InstDesc(Kind == RecurKind::FMul, SI);
549 
550   return InstDesc(false, I);
551 }
552 
553 RecurrenceDescriptor::InstDesc
554 RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurKind Kind,
555                                         InstDesc &Prev, bool HasFunNoNaNAttr) {
556   Instruction *UAI = Prev.getUnsafeAlgebraInst();
557   if (!UAI && isa<FPMathOperator>(I) && !I->hasAllowReassoc())
558     UAI = I; // Found an unsafe (unvectorizable) algebra instruction.
559 
560   switch (I->getOpcode()) {
561   default:
562     return InstDesc(false, I);
563   case Instruction::PHI:
564     return InstDesc(I, Prev.getRecKind(), Prev.getUnsafeAlgebraInst());
565   case Instruction::Sub:
566   case Instruction::Add:
567     return InstDesc(Kind == RecurKind::Add, I);
568   case Instruction::Mul:
569     return InstDesc(Kind == RecurKind::Mul, I);
570   case Instruction::And:
571     return InstDesc(Kind == RecurKind::And, I);
572   case Instruction::Or:
573     return InstDesc(Kind == RecurKind::Or, I);
574   case Instruction::Xor:
575     return InstDesc(Kind == RecurKind::Xor, I);
576   case Instruction::FDiv:
577   case Instruction::FMul:
578     return InstDesc(Kind == RecurKind::FMul, I, UAI);
579   case Instruction::FSub:
580   case Instruction::FAdd:
581     return InstDesc(Kind == RecurKind::FAdd, I, UAI);
582   case Instruction::Select:
583     if (Kind == RecurKind::FAdd || Kind == RecurKind::FMul)
584       return isConditionalRdxPattern(Kind, I);
585     LLVM_FALLTHROUGH;
586   case Instruction::FCmp:
587   case Instruction::ICmp:
588     if (!isIntMinMaxRecurrenceKind(Kind) &&
589         (!HasFunNoNaNAttr || !isFPMinMaxRecurrenceKind(Kind)))
590       return InstDesc(false, I);
591     return isMinMaxSelectCmpPattern(I, Prev);
592   }
593 }
594 
595 bool RecurrenceDescriptor::hasMultipleUsesOf(
596     Instruction *I, SmallPtrSetImpl<Instruction *> &Insts,
597     unsigned MaxNumUses) {
598   unsigned NumUses = 0;
599   for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
600        ++Use) {
601     if (Insts.count(dyn_cast<Instruction>(*Use)))
602       ++NumUses;
603     if (NumUses > MaxNumUses)
604       return true;
605   }
606 
607   return false;
608 }
609 bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
610                                           RecurrenceDescriptor &RedDes,
611                                           DemandedBits *DB, AssumptionCache *AC,
612                                           DominatorTree *DT) {
613 
614   BasicBlock *Header = TheLoop->getHeader();
615   Function &F = *Header->getParent();
616   bool HasFunNoNaNAttr =
617       F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
618 
619   if (AddReductionVar(Phi, RecurKind::Add, TheLoop, HasFunNoNaNAttr, RedDes, DB,
620                       AC, DT)) {
621     LLVM_DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
622     return true;
623   }
624   if (AddReductionVar(Phi, RecurKind::Mul, TheLoop, HasFunNoNaNAttr, RedDes, DB,
625                       AC, DT)) {
626     LLVM_DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
627     return true;
628   }
629   if (AddReductionVar(Phi, RecurKind::Or, TheLoop, HasFunNoNaNAttr, RedDes, DB,
630                       AC, DT)) {
631     LLVM_DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
632     return true;
633   }
634   if (AddReductionVar(Phi, RecurKind::And, TheLoop, HasFunNoNaNAttr, RedDes, DB,
635                       AC, DT)) {
636     LLVM_DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
637     return true;
638   }
639   if (AddReductionVar(Phi, RecurKind::Xor, TheLoop, HasFunNoNaNAttr, RedDes, DB,
640                       AC, DT)) {
641     LLVM_DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
642     return true;
643   }
644   if (AddReductionVar(Phi, RecurKind::SMax, TheLoop, HasFunNoNaNAttr, RedDes,
645                       DB, AC, DT)) {
646     LLVM_DEBUG(dbgs() << "Found a SMAX reduction PHI." << *Phi << "\n");
647     return true;
648   }
649   if (AddReductionVar(Phi, RecurKind::SMin, TheLoop, HasFunNoNaNAttr, RedDes,
650                       DB, AC, DT)) {
651     LLVM_DEBUG(dbgs() << "Found a SMIN reduction PHI." << *Phi << "\n");
652     return true;
653   }
654   if (AddReductionVar(Phi, RecurKind::UMax, TheLoop, HasFunNoNaNAttr, RedDes,
655                       DB, AC, DT)) {
656     LLVM_DEBUG(dbgs() << "Found a UMAX reduction PHI." << *Phi << "\n");
657     return true;
658   }
659   if (AddReductionVar(Phi, RecurKind::UMin, TheLoop, HasFunNoNaNAttr, RedDes,
660                       DB, AC, DT)) {
661     LLVM_DEBUG(dbgs() << "Found a UMIN reduction PHI." << *Phi << "\n");
662     return true;
663   }
664   if (AddReductionVar(Phi, RecurKind::FMul, TheLoop, HasFunNoNaNAttr, RedDes,
665                       DB, AC, DT)) {
666     LLVM_DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
667     return true;
668   }
669   if (AddReductionVar(Phi, RecurKind::FAdd, TheLoop, HasFunNoNaNAttr, RedDes,
670                       DB, AC, DT)) {
671     LLVM_DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
672     return true;
673   }
674   if (AddReductionVar(Phi, RecurKind::FMax, TheLoop, HasFunNoNaNAttr, RedDes,
675                       DB, AC, DT)) {
676     LLVM_DEBUG(dbgs() << "Found a float MAX reduction PHI." << *Phi << "\n");
677     return true;
678   }
679   if (AddReductionVar(Phi, RecurKind::FMin, TheLoop, HasFunNoNaNAttr, RedDes,
680                       DB, AC, DT)) {
681     LLVM_DEBUG(dbgs() << "Found a float MIN reduction PHI." << *Phi << "\n");
682     return true;
683   }
684   // Not a reduction of known type.
685   return false;
686 }
687 
688 bool RecurrenceDescriptor::isFirstOrderRecurrence(
689     PHINode *Phi, Loop *TheLoop,
690     DenseMap<Instruction *, Instruction *> &SinkAfter, DominatorTree *DT) {
691 
692   // Ensure the phi node is in the loop header and has two incoming values.
693   if (Phi->getParent() != TheLoop->getHeader() ||
694       Phi->getNumIncomingValues() != 2)
695     return false;
696 
697   // Ensure the loop has a preheader and a single latch block. The loop
698   // vectorizer will need the latch to set up the next iteration of the loop.
699   auto *Preheader = TheLoop->getLoopPreheader();
700   auto *Latch = TheLoop->getLoopLatch();
701   if (!Preheader || !Latch)
702     return false;
703 
704   // Ensure the phi node's incoming blocks are the loop preheader and latch.
705   if (Phi->getBasicBlockIndex(Preheader) < 0 ||
706       Phi->getBasicBlockIndex(Latch) < 0)
707     return false;
708 
709   // Get the previous value. The previous value comes from the latch edge while
710   // the initial value comes form the preheader edge.
711   auto *Previous = dyn_cast<Instruction>(Phi->getIncomingValueForBlock(Latch));
712   if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous) ||
713       SinkAfter.count(Previous)) // Cannot rely on dominance due to motion.
714     return false;
715 
716   // Ensure every user of the phi node is dominated by the previous value.
717   // The dominance requirement ensures the loop vectorizer will not need to
718   // vectorize the initial value prior to the first iteration of the loop.
719   // TODO: Consider extending this sinking to handle memory instructions and
720   // phis with multiple users.
721 
722   // Returns true, if all users of I are dominated by DominatedBy.
723   auto allUsesDominatedBy = [DT](Instruction *I, Instruction *DominatedBy) {
724     return all_of(I->uses(), [DT, DominatedBy](Use &U) {
725       return DT->dominates(DominatedBy, U);
726     });
727   };
728 
729   if (Phi->hasOneUse()) {
730     Instruction *I = Phi->user_back();
731 
732     // If the user of the PHI is also the incoming value, we potentially have a
733     // reduction and which cannot be handled by sinking.
734     if (Previous == I)
735       return false;
736 
737     // We cannot sink terminator instructions.
738     if (I->getParent()->getTerminator() == I)
739       return false;
740 
741     // Do not try to sink an instruction multiple times (if multiple operands
742     // are first order recurrences).
743     // TODO: We can support this case, by sinking the instruction after the
744     // 'deepest' previous instruction.
745     if (SinkAfter.find(I) != SinkAfter.end())
746       return false;
747 
748     if (DT->dominates(Previous, I)) // We already are good w/o sinking.
749       return true;
750 
751     // We can sink any instruction without side effects, as long as all users
752     // are dominated by the instruction we are sinking after.
753     if (I->getParent() == Phi->getParent() && !I->mayHaveSideEffects() &&
754         allUsesDominatedBy(I, Previous)) {
755       SinkAfter[I] = Previous;
756       return true;
757     }
758   }
759 
760   return allUsesDominatedBy(Phi, Previous);
761 }
762 
763 /// This function returns the identity element (or neutral element) for
764 /// the operation K.
765 Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurKind K, Type *Tp) {
766   switch (K) {
767   case RecurKind::Xor:
768   case RecurKind::Add:
769   case RecurKind::Or:
770     // Adding, Xoring, Oring zero to a number does not change it.
771     return ConstantInt::get(Tp, 0);
772   case RecurKind::Mul:
773     // Multiplying a number by 1 does not change it.
774     return ConstantInt::get(Tp, 1);
775   case RecurKind::And:
776     // AND-ing a number with an all-1 value does not change it.
777     return ConstantInt::get(Tp, -1, true);
778   case RecurKind::FMul:
779     // Multiplying a number by 1 does not change it.
780     return ConstantFP::get(Tp, 1.0L);
781   case RecurKind::FAdd:
782     // Adding zero to a number does not change it.
783     return ConstantFP::get(Tp, 0.0L);
784   case RecurKind::UMin:
785     return ConstantInt::get(Tp, -1);
786   case RecurKind::UMax:
787     return ConstantInt::get(Tp, 0);
788   case RecurKind::SMin:
789     return ConstantInt::get(Tp,
790                             APInt::getSignedMaxValue(Tp->getIntegerBitWidth()));
791   case RecurKind::SMax:
792     return ConstantInt::get(Tp,
793                             APInt::getSignedMinValue(Tp->getIntegerBitWidth()));
794   case RecurKind::FMin:
795     return ConstantFP::getInfinity(Tp, true);
796   case RecurKind::FMax:
797     return ConstantFP::getInfinity(Tp, false);
798   default:
799     llvm_unreachable("Unknown recurrence kind");
800   }
801 }
802 
803 unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
804   switch (Kind) {
805   case RecurKind::Add:
806     return Instruction::Add;
807   case RecurKind::Mul:
808     return Instruction::Mul;
809   case RecurKind::Or:
810     return Instruction::Or;
811   case RecurKind::And:
812     return Instruction::And;
813   case RecurKind::Xor:
814     return Instruction::Xor;
815   case RecurKind::FMul:
816     return Instruction::FMul;
817   case RecurKind::FAdd:
818     return Instruction::FAdd;
819   case RecurKind::SMax:
820   case RecurKind::SMin:
821   case RecurKind::UMax:
822   case RecurKind::UMin:
823     return Instruction::ICmp;
824   case RecurKind::FMax:
825   case RecurKind::FMin:
826     return Instruction::FCmp;
827   default:
828     llvm_unreachable("Unknown recurrence operation");
829   }
830 }
831 
832 SmallVector<Instruction *, 4>
833 RecurrenceDescriptor::getReductionOpChain(PHINode *Phi, Loop *L) const {
834   SmallVector<Instruction *, 4> ReductionOperations;
835   unsigned RedOp = getOpcode(Kind);
836 
837   // Search down from the Phi to the LoopExitInstr, looking for instructions
838   // with a single user of the correct type for the reduction.
839 
840   // Note that we check that the type of the operand is correct for each item in
841   // the chain, including the last (the loop exit value). This can come up from
842   // sub, which would otherwise be treated as an add reduction. MinMax also need
843   // to check for a pair of icmp/select, for which we use getNextInstruction and
844   // isCorrectOpcode functions to step the right number of instruction, and
845   // check the icmp/select pair.
846   // FIXME: We also do not attempt to look through Phi/Select's yet, which might
847   // be part of the reduction chain, or attempt to looks through And's to find a
848   // smaller bitwidth. Subs are also currently not allowed (which are usually
849   // treated as part of a add reduction) as they are expected to generally be
850   // more expensive than out-of-loop reductions, and need to be costed more
851   // carefully.
852   unsigned ExpectedUses = 1;
853   if (RedOp == Instruction::ICmp || RedOp == Instruction::FCmp)
854     ExpectedUses = 2;
855 
856   auto getNextInstruction = [&](Instruction *Cur) {
857     if (RedOp == Instruction::ICmp || RedOp == Instruction::FCmp) {
858       // We are expecting a icmp/select pair, which we go to the next select
859       // instruction if we can. We already know that Cur has 2 uses.
860       if (isa<SelectInst>(*Cur->user_begin()))
861         return cast<Instruction>(*Cur->user_begin());
862       else
863         return cast<Instruction>(*std::next(Cur->user_begin()));
864     }
865     return cast<Instruction>(*Cur->user_begin());
866   };
867   auto isCorrectOpcode = [&](Instruction *Cur) {
868     if (RedOp == Instruction::ICmp || RedOp == Instruction::FCmp) {
869       Value *LHS, *RHS;
870       return SelectPatternResult::isMinOrMax(
871           matchSelectPattern(Cur, LHS, RHS).Flavor);
872     }
873     return Cur->getOpcode() == RedOp;
874   };
875 
876   // The loop exit instruction we check first (as a quick test) but add last. We
877   // check the opcode is correct (and dont allow them to be Subs) and that they
878   // have expected to have the expected number of uses. They will have one use
879   // from the phi and one from a LCSSA value, no matter the type.
880   if (!isCorrectOpcode(LoopExitInstr) || !LoopExitInstr->hasNUses(2))
881     return {};
882 
883   // Check that the Phi has one (or two for min/max) uses.
884   if (!Phi->hasNUses(ExpectedUses))
885     return {};
886   Instruction *Cur = getNextInstruction(Phi);
887 
888   // Each other instruction in the chain should have the expected number of uses
889   // and be the correct opcode.
890   while (Cur != LoopExitInstr) {
891     if (!isCorrectOpcode(Cur) || !Cur->hasNUses(ExpectedUses))
892       return {};
893 
894     ReductionOperations.push_back(Cur);
895     Cur = getNextInstruction(Cur);
896   }
897 
898   ReductionOperations.push_back(Cur);
899   return ReductionOperations;
900 }
901 
902 InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K,
903                                          const SCEV *Step, BinaryOperator *BOp,
904                                          SmallVectorImpl<Instruction *> *Casts)
905     : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) {
906   assert(IK != IK_NoInduction && "Not an induction");
907 
908   // Start value type should match the induction kind and the value
909   // itself should not be null.
910   assert(StartValue && "StartValue is null");
911   assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) &&
912          "StartValue is not a pointer for pointer induction");
913   assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) &&
914          "StartValue is not an integer for integer induction");
915 
916   // Check the Step Value. It should be non-zero integer value.
917   assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) &&
918          "Step value is zero");
919 
920   assert((IK != IK_PtrInduction || getConstIntStepValue()) &&
921          "Step value should be constant for pointer induction");
922   assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) &&
923          "StepValue is not an integer");
924 
925   assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) &&
926          "StepValue is not FP for FpInduction");
927   assert((IK != IK_FpInduction ||
928           (InductionBinOp &&
929            (InductionBinOp->getOpcode() == Instruction::FAdd ||
930             InductionBinOp->getOpcode() == Instruction::FSub))) &&
931          "Binary opcode should be specified for FP induction");
932 
933   if (Casts) {
934     for (auto &Inst : *Casts) {
935       RedundantCasts.push_back(Inst);
936     }
937   }
938 }
939 
940 ConstantInt *InductionDescriptor::getConstIntStepValue() const {
941   if (isa<SCEVConstant>(Step))
942     return dyn_cast<ConstantInt>(cast<SCEVConstant>(Step)->getValue());
943   return nullptr;
944 }
945 
946 bool InductionDescriptor::isFPInductionPHI(PHINode *Phi, const Loop *TheLoop,
947                                            ScalarEvolution *SE,
948                                            InductionDescriptor &D) {
949 
950   // Here we only handle FP induction variables.
951   assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type");
952 
953   if (TheLoop->getHeader() != Phi->getParent())
954     return false;
955 
956   // The loop may have multiple entrances or multiple exits; we can analyze
957   // this phi if it has a unique entry value and a unique backedge value.
958   if (Phi->getNumIncomingValues() != 2)
959     return false;
960   Value *BEValue = nullptr, *StartValue = nullptr;
961   if (TheLoop->contains(Phi->getIncomingBlock(0))) {
962     BEValue = Phi->getIncomingValue(0);
963     StartValue = Phi->getIncomingValue(1);
964   } else {
965     assert(TheLoop->contains(Phi->getIncomingBlock(1)) &&
966            "Unexpected Phi node in the loop");
967     BEValue = Phi->getIncomingValue(1);
968     StartValue = Phi->getIncomingValue(0);
969   }
970 
971   BinaryOperator *BOp = dyn_cast<BinaryOperator>(BEValue);
972   if (!BOp)
973     return false;
974 
975   Value *Addend = nullptr;
976   if (BOp->getOpcode() == Instruction::FAdd) {
977     if (BOp->getOperand(0) == Phi)
978       Addend = BOp->getOperand(1);
979     else if (BOp->getOperand(1) == Phi)
980       Addend = BOp->getOperand(0);
981   } else if (BOp->getOpcode() == Instruction::FSub)
982     if (BOp->getOperand(0) == Phi)
983       Addend = BOp->getOperand(1);
984 
985   if (!Addend)
986     return false;
987 
988   // The addend should be loop invariant
989   if (auto *I = dyn_cast<Instruction>(Addend))
990     if (TheLoop->contains(I))
991       return false;
992 
993   // FP Step has unknown SCEV
994   const SCEV *Step = SE->getUnknown(Addend);
995   D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp);
996   return true;
997 }
998 
999 /// This function is called when we suspect that the update-chain of a phi node
1000 /// (whose symbolic SCEV expression sin \p PhiScev) contains redundant casts,
1001 /// that can be ignored. (This can happen when the PSCEV rewriter adds a runtime
1002 /// predicate P under which the SCEV expression for the phi can be the
1003 /// AddRecurrence \p AR; See createAddRecFromPHIWithCast). We want to find the
1004 /// cast instructions that are involved in the update-chain of this induction.
1005 /// A caller that adds the required runtime predicate can be free to drop these
1006 /// cast instructions, and compute the phi using \p AR (instead of some scev
1007 /// expression with casts).
1008 ///
1009 /// For example, without a predicate the scev expression can take the following
1010 /// form:
1011 ///      (Ext ix (Trunc iy ( Start + i*Step ) to ix) to iy)
1012 ///
1013 /// It corresponds to the following IR sequence:
1014 /// %for.body:
1015 ///   %x = phi i64 [ 0, %ph ], [ %add, %for.body ]
1016 ///   %casted_phi = "ExtTrunc i64 %x"
1017 ///   %add = add i64 %casted_phi, %step
1018 ///
1019 /// where %x is given in \p PN,
1020 /// PSE.getSCEV(%x) is equal to PSE.getSCEV(%casted_phi) under a predicate,
1021 /// and the IR sequence that "ExtTrunc i64 %x" represents can take one of
1022 /// several forms, for example, such as:
1023 ///   ExtTrunc1:    %casted_phi = and  %x, 2^n-1
1024 /// or:
1025 ///   ExtTrunc2:    %t = shl %x, m
1026 ///                 %casted_phi = ashr %t, m
1027 ///
1028 /// If we are able to find such sequence, we return the instructions
1029 /// we found, namely %casted_phi and the instructions on its use-def chain up
1030 /// to the phi (not including the phi).
1031 static bool getCastsForInductionPHI(PredicatedScalarEvolution &PSE,
1032                                     const SCEVUnknown *PhiScev,
1033                                     const SCEVAddRecExpr *AR,
1034                                     SmallVectorImpl<Instruction *> &CastInsts) {
1035 
1036   assert(CastInsts.empty() && "CastInsts is expected to be empty.");
1037   auto *PN = cast<PHINode>(PhiScev->getValue());
1038   assert(PSE.getSCEV(PN) == AR && "Unexpected phi node SCEV expression");
1039   const Loop *L = AR->getLoop();
1040 
1041   // Find any cast instructions that participate in the def-use chain of
1042   // PhiScev in the loop.
1043   // FORNOW/TODO: We currently expect the def-use chain to include only
1044   // two-operand instructions, where one of the operands is an invariant.
1045   // createAddRecFromPHIWithCasts() currently does not support anything more
1046   // involved than that, so we keep the search simple. This can be
1047   // extended/generalized as needed.
1048 
1049   auto getDef = [&](const Value *Val) -> Value * {
1050     const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Val);
1051     if (!BinOp)
1052       return nullptr;
1053     Value *Op0 = BinOp->getOperand(0);
1054     Value *Op1 = BinOp->getOperand(1);
1055     Value *Def = nullptr;
1056     if (L->isLoopInvariant(Op0))
1057       Def = Op1;
1058     else if (L->isLoopInvariant(Op1))
1059       Def = Op0;
1060     return Def;
1061   };
1062 
1063   // Look for the instruction that defines the induction via the
1064   // loop backedge.
1065   BasicBlock *Latch = L->getLoopLatch();
1066   if (!Latch)
1067     return false;
1068   Value *Val = PN->getIncomingValueForBlock(Latch);
1069   if (!Val)
1070     return false;
1071 
1072   // Follow the def-use chain until the induction phi is reached.
1073   // If on the way we encounter a Value that has the same SCEV Expr as the
1074   // phi node, we can consider the instructions we visit from that point
1075   // as part of the cast-sequence that can be ignored.
1076   bool InCastSequence = false;
1077   auto *Inst = dyn_cast<Instruction>(Val);
1078   while (Val != PN) {
1079     // If we encountered a phi node other than PN, or if we left the loop,
1080     // we bail out.
1081     if (!Inst || !L->contains(Inst)) {
1082       return false;
1083     }
1084     auto *AddRec = dyn_cast<SCEVAddRecExpr>(PSE.getSCEV(Val));
1085     if (AddRec && PSE.areAddRecsEqualWithPreds(AddRec, AR))
1086       InCastSequence = true;
1087     if (InCastSequence) {
1088       // Only the last instruction in the cast sequence is expected to have
1089       // uses outside the induction def-use chain.
1090       if (!CastInsts.empty())
1091         if (!Inst->hasOneUse())
1092           return false;
1093       CastInsts.push_back(Inst);
1094     }
1095     Val = getDef(Val);
1096     if (!Val)
1097       return false;
1098     Inst = dyn_cast<Instruction>(Val);
1099   }
1100 
1101   return InCastSequence;
1102 }
1103 
1104 bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop,
1105                                          PredicatedScalarEvolution &PSE,
1106                                          InductionDescriptor &D, bool Assume) {
1107   Type *PhiTy = Phi->getType();
1108 
1109   // Handle integer and pointer inductions variables.
1110   // Now we handle also FP induction but not trying to make a
1111   // recurrent expression from the PHI node in-place.
1112 
1113   if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() && !PhiTy->isFloatTy() &&
1114       !PhiTy->isDoubleTy() && !PhiTy->isHalfTy())
1115     return false;
1116 
1117   if (PhiTy->isFloatingPointTy())
1118     return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D);
1119 
1120   const SCEV *PhiScev = PSE.getSCEV(Phi);
1121   const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
1122 
1123   // We need this expression to be an AddRecExpr.
1124   if (Assume && !AR)
1125     AR = PSE.getAsAddRec(Phi);
1126 
1127   if (!AR) {
1128     LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
1129     return false;
1130   }
1131 
1132   // Record any Cast instructions that participate in the induction update
1133   const auto *SymbolicPhi = dyn_cast<SCEVUnknown>(PhiScev);
1134   // If we started from an UnknownSCEV, and managed to build an addRecurrence
1135   // only after enabling Assume with PSCEV, this means we may have encountered
1136   // cast instructions that required adding a runtime check in order to
1137   // guarantee the correctness of the AddRecurrence respresentation of the
1138   // induction.
1139   if (PhiScev != AR && SymbolicPhi) {
1140     SmallVector<Instruction *, 2> Casts;
1141     if (getCastsForInductionPHI(PSE, SymbolicPhi, AR, Casts))
1142       return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR, &Casts);
1143   }
1144 
1145   return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR);
1146 }
1147 
1148 bool InductionDescriptor::isInductionPHI(
1149     PHINode *Phi, const Loop *TheLoop, ScalarEvolution *SE,
1150     InductionDescriptor &D, const SCEV *Expr,
1151     SmallVectorImpl<Instruction *> *CastsToIgnore) {
1152   Type *PhiTy = Phi->getType();
1153   // We only handle integer and pointer inductions variables.
1154   if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
1155     return false;
1156 
1157   // Check that the PHI is consecutive.
1158   const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
1159   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
1160 
1161   if (!AR) {
1162     LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
1163     return false;
1164   }
1165 
1166   if (AR->getLoop() != TheLoop) {
1167     // FIXME: We should treat this as a uniform. Unfortunately, we
1168     // don't currently know how to handled uniform PHIs.
1169     LLVM_DEBUG(
1170         dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n");
1171     return false;
1172   }
1173 
1174   Value *StartValue =
1175       Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader());
1176 
1177   BasicBlock *Latch = AR->getLoop()->getLoopLatch();
1178   if (!Latch)
1179     return false;
1180   BinaryOperator *BOp =
1181       dyn_cast<BinaryOperator>(Phi->getIncomingValueForBlock(Latch));
1182 
1183   const SCEV *Step = AR->getStepRecurrence(*SE);
1184   // Calculate the pointer stride and check if it is consecutive.
1185   // The stride may be a constant or a loop invariant integer value.
1186   const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step);
1187   if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop))
1188     return false;
1189 
1190   if (PhiTy->isIntegerTy()) {
1191     D = InductionDescriptor(StartValue, IK_IntInduction, Step, BOp,
1192                             CastsToIgnore);
1193     return true;
1194   }
1195 
1196   assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
1197   // Pointer induction should be a constant.
1198   if (!ConstStep)
1199     return false;
1200 
1201   ConstantInt *CV = ConstStep->getValue();
1202   Type *PointerElementType = PhiTy->getPointerElementType();
1203   // The pointer stride cannot be determined if the pointer element type is not
1204   // sized.
1205   if (!PointerElementType->isSized())
1206     return false;
1207 
1208   const DataLayout &DL = Phi->getModule()->getDataLayout();
1209   int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
1210   if (!Size)
1211     return false;
1212 
1213   int64_t CVSize = CV->getSExtValue();
1214   if (CVSize % Size)
1215     return false;
1216   auto *StepValue =
1217       SE->getConstant(CV->getType(), CVSize / Size, true /* signed */);
1218   D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue, BOp);
1219   return true;
1220 }
1221