1 //===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
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 file defines common loop utility functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Utils/LoopUtils.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Analysis/BasicAliasAnalysis.h"
17 #include "llvm/Analysis/GlobalsModRef.h"
18 #include "llvm/Analysis/GlobalsModRef.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/Analysis/ScalarEvolution.h"
22 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
23 #include "llvm/Analysis/ScalarEvolutionExpander.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/PatternMatch.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/Debug.h"
32 
33 using namespace llvm;
34 using namespace llvm::PatternMatch;
35 
36 #define DEBUG_TYPE "loop-utils"
37 
38 bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
39                                         SmallPtrSetImpl<Instruction *> &Set) {
40   for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use)
41     if (!Set.count(dyn_cast<Instruction>(*Use)))
42       return false;
43   return true;
44 }
45 
46 bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurrenceKind Kind) {
47   switch (Kind) {
48   default:
49     break;
50   case RK_IntegerAdd:
51   case RK_IntegerMult:
52   case RK_IntegerOr:
53   case RK_IntegerAnd:
54   case RK_IntegerXor:
55   case RK_IntegerMinMax:
56     return true;
57   }
58   return false;
59 }
60 
61 bool RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurrenceKind Kind) {
62   return (Kind != RK_NoRecurrence) && !isIntegerRecurrenceKind(Kind);
63 }
64 
65 bool RecurrenceDescriptor::isArithmeticRecurrenceKind(RecurrenceKind Kind) {
66   switch (Kind) {
67   default:
68     break;
69   case RK_IntegerAdd:
70   case RK_IntegerMult:
71   case RK_FloatAdd:
72   case RK_FloatMult:
73     return true;
74   }
75   return false;
76 }
77 
78 Instruction *
79 RecurrenceDescriptor::lookThroughAnd(PHINode *Phi, Type *&RT,
80                                      SmallPtrSetImpl<Instruction *> &Visited,
81                                      SmallPtrSetImpl<Instruction *> &CI) {
82   if (!Phi->hasOneUse())
83     return Phi;
84 
85   const APInt *M = nullptr;
86   Instruction *I, *J = cast<Instruction>(Phi->use_begin()->getUser());
87 
88   // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT
89   // with a new integer type of the corresponding bit width.
90   if (match(J, m_CombineOr(m_And(m_Instruction(I), m_APInt(M)),
91                            m_And(m_APInt(M), m_Instruction(I))))) {
92     int32_t Bits = (*M + 1).exactLogBase2();
93     if (Bits > 0) {
94       RT = IntegerType::get(Phi->getContext(), Bits);
95       Visited.insert(Phi);
96       CI.insert(J);
97       return J;
98     }
99   }
100   return Phi;
101 }
102 
103 bool RecurrenceDescriptor::getSourceExtensionKind(
104     Instruction *Start, Instruction *Exit, Type *RT, bool &IsSigned,
105     SmallPtrSetImpl<Instruction *> &Visited,
106     SmallPtrSetImpl<Instruction *> &CI) {
107 
108   SmallVector<Instruction *, 8> Worklist;
109   bool FoundOneOperand = false;
110   unsigned DstSize = RT->getPrimitiveSizeInBits();
111   Worklist.push_back(Exit);
112 
113   // Traverse the instructions in the reduction expression, beginning with the
114   // exit value.
115   while (!Worklist.empty()) {
116     Instruction *I = Worklist.pop_back_val();
117     for (Use &U : I->operands()) {
118 
119       // Terminate the traversal if the operand is not an instruction, or we
120       // reach the starting value.
121       Instruction *J = dyn_cast<Instruction>(U.get());
122       if (!J || J == Start)
123         continue;
124 
125       // Otherwise, investigate the operation if it is also in the expression.
126       if (Visited.count(J)) {
127         Worklist.push_back(J);
128         continue;
129       }
130 
131       // If the operand is not in Visited, it is not a reduction operation, but
132       // it does feed into one. Make sure it is either a single-use sign- or
133       // zero-extend instruction.
134       CastInst *Cast = dyn_cast<CastInst>(J);
135       bool IsSExtInst = isa<SExtInst>(J);
136       if (!Cast || !Cast->hasOneUse() || !(isa<ZExtInst>(J) || IsSExtInst))
137         return false;
138 
139       // Ensure the source type of the extend is no larger than the reduction
140       // type. It is not necessary for the types to be identical.
141       unsigned SrcSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
142       if (SrcSize > DstSize)
143         return false;
144 
145       // Furthermore, ensure that all such extends are of the same kind.
146       if (FoundOneOperand) {
147         if (IsSigned != IsSExtInst)
148           return false;
149       } else {
150         FoundOneOperand = true;
151         IsSigned = IsSExtInst;
152       }
153 
154       // Lastly, if the source type of the extend matches the reduction type,
155       // add the extend to CI so that we can avoid accounting for it in the
156       // cost model.
157       if (SrcSize == DstSize)
158         CI.insert(Cast);
159     }
160   }
161   return true;
162 }
163 
164 bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind,
165                                            Loop *TheLoop, bool HasFunNoNaNAttr,
166                                            RecurrenceDescriptor &RedDes) {
167   if (Phi->getNumIncomingValues() != 2)
168     return false;
169 
170   // Reduction variables are only found in the loop header block.
171   if (Phi->getParent() != TheLoop->getHeader())
172     return false;
173 
174   // Obtain the reduction start value from the value that comes from the loop
175   // preheader.
176   Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
177 
178   // ExitInstruction is the single value which is used outside the loop.
179   // We only allow for a single reduction value to be used outside the loop.
180   // This includes users of the reduction, variables (which form a cycle
181   // which ends in the phi node).
182   Instruction *ExitInstruction = nullptr;
183   // Indicates that we found a reduction operation in our scan.
184   bool FoundReduxOp = false;
185 
186   // We start with the PHI node and scan for all of the users of this
187   // instruction. All users must be instructions that can be used as reduction
188   // variables (such as ADD). We must have a single out-of-block user. The cycle
189   // must include the original PHI.
190   bool FoundStartPHI = false;
191 
192   // To recognize min/max patterns formed by a icmp select sequence, we store
193   // the number of instruction we saw from the recognized min/max pattern,
194   //  to make sure we only see exactly the two instructions.
195   unsigned NumCmpSelectPatternInst = 0;
196   InstDesc ReduxDesc(false, nullptr);
197 
198   // Data used for determining if the recurrence has been type-promoted.
199   Type *RecurrenceType = Phi->getType();
200   SmallPtrSet<Instruction *, 4> CastInsts;
201   Instruction *Start = Phi;
202   bool IsSigned = false;
203 
204   SmallPtrSet<Instruction *, 8> VisitedInsts;
205   SmallVector<Instruction *, 8> Worklist;
206 
207   // Return early if the recurrence kind does not match the type of Phi. If the
208   // recurrence kind is arithmetic, we attempt to look through AND operations
209   // resulting from the type promotion performed by InstCombine.  Vector
210   // operations are not limited to the legal integer widths, so we may be able
211   // to evaluate the reduction in the narrower width.
212   if (RecurrenceType->isFloatingPointTy()) {
213     if (!isFloatingPointRecurrenceKind(Kind))
214       return false;
215   } else {
216     if (!isIntegerRecurrenceKind(Kind))
217       return false;
218     if (isArithmeticRecurrenceKind(Kind))
219       Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts);
220   }
221 
222   Worklist.push_back(Start);
223   VisitedInsts.insert(Start);
224 
225   // A value in the reduction can be used:
226   //  - By the reduction:
227   //      - Reduction operation:
228   //        - One use of reduction value (safe).
229   //        - Multiple use of reduction value (not safe).
230   //      - PHI:
231   //        - All uses of the PHI must be the reduction (safe).
232   //        - Otherwise, not safe.
233   //  - By one instruction outside of the loop (safe).
234   //  - By further instructions outside of the loop (not safe).
235   //  - By an instruction that is not part of the reduction (not safe).
236   //    This is either:
237   //      * An instruction type other than PHI or the reduction operation.
238   //      * A PHI in the header other than the initial PHI.
239   while (!Worklist.empty()) {
240     Instruction *Cur = Worklist.back();
241     Worklist.pop_back();
242 
243     // No Users.
244     // If the instruction has no users then this is a broken chain and can't be
245     // a reduction variable.
246     if (Cur->use_empty())
247       return false;
248 
249     bool IsAPhi = isa<PHINode>(Cur);
250 
251     // A header PHI use other than the original PHI.
252     if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
253       return false;
254 
255     // Reductions of instructions such as Div, and Sub is only possible if the
256     // LHS is the reduction variable.
257     if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
258         !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
259         !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
260       return false;
261 
262     // Any reduction instruction must be of one of the allowed kinds. We ignore
263     // the starting value (the Phi or an AND instruction if the Phi has been
264     // type-promoted).
265     if (Cur != Start) {
266       ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
267       if (!ReduxDesc.isRecurrence())
268         return false;
269     }
270 
271     // A reduction operation must only have one use of the reduction value.
272     if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax &&
273         hasMultipleUsesOf(Cur, VisitedInsts))
274       return false;
275 
276     // All inputs to a PHI node must be a reduction value.
277     if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
278       return false;
279 
280     if (Kind == RK_IntegerMinMax &&
281         (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
282       ++NumCmpSelectPatternInst;
283     if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
284       ++NumCmpSelectPatternInst;
285 
286     // Check  whether we found a reduction operator.
287     FoundReduxOp |= !IsAPhi && Cur != Start;
288 
289     // Process users of current instruction. Push non-PHI nodes after PHI nodes
290     // onto the stack. This way we are going to have seen all inputs to PHI
291     // nodes once we get to them.
292     SmallVector<Instruction *, 8> NonPHIs;
293     SmallVector<Instruction *, 8> PHIs;
294     for (User *U : Cur->users()) {
295       Instruction *UI = cast<Instruction>(U);
296 
297       // Check if we found the exit user.
298       BasicBlock *Parent = UI->getParent();
299       if (!TheLoop->contains(Parent)) {
300         // Exit if you find multiple outside users or if the header phi node is
301         // being used. In this case the user uses the value of the previous
302         // iteration, in which case we would loose "VF-1" iterations of the
303         // reduction operation if we vectorize.
304         if (ExitInstruction != nullptr || Cur == Phi)
305           return false;
306 
307         // The instruction used by an outside user must be the last instruction
308         // before we feed back to the reduction phi. Otherwise, we loose VF-1
309         // operations on the value.
310         if (!is_contained(Phi->operands(), Cur))
311           return false;
312 
313         ExitInstruction = Cur;
314         continue;
315       }
316 
317       // Process instructions only once (termination). Each reduction cycle
318       // value must only be used once, except by phi nodes and min/max
319       // reductions which are represented as a cmp followed by a select.
320       InstDesc IgnoredVal(false, nullptr);
321       if (VisitedInsts.insert(UI).second) {
322         if (isa<PHINode>(UI))
323           PHIs.push_back(UI);
324         else
325           NonPHIs.push_back(UI);
326       } else if (!isa<PHINode>(UI) &&
327                  ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
328                    !isa<SelectInst>(UI)) ||
329                   !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
330         return false;
331 
332       // Remember that we completed the cycle.
333       if (UI == Phi)
334         FoundStartPHI = true;
335     }
336     Worklist.append(PHIs.begin(), PHIs.end());
337     Worklist.append(NonPHIs.begin(), NonPHIs.end());
338   }
339 
340   // This means we have seen one but not the other instruction of the
341   // pattern or more than just a select and cmp.
342   if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) &&
343       NumCmpSelectPatternInst != 2)
344     return false;
345 
346   if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
347     return false;
348 
349   // If we think Phi may have been type-promoted, we also need to ensure that
350   // all source operands of the reduction are either SExtInsts or ZEstInsts. If
351   // so, we will be able to evaluate the reduction in the narrower bit width.
352   if (Start != Phi)
353     if (!getSourceExtensionKind(Start, ExitInstruction, RecurrenceType,
354                                 IsSigned, VisitedInsts, CastInsts))
355       return false;
356 
357   // We found a reduction var if we have reached the original phi node and we
358   // only have a single instruction with out-of-loop users.
359 
360   // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
361   // is saved as part of the RecurrenceDescriptor.
362 
363   // Save the description of this reduction variable.
364   RecurrenceDescriptor RD(
365       RdxStart, ExitInstruction, Kind, ReduxDesc.getMinMaxKind(),
366       ReduxDesc.getUnsafeAlgebraInst(), RecurrenceType, IsSigned, CastInsts);
367   RedDes = RD;
368 
369   return true;
370 }
371 
372 /// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction
373 /// pattern corresponding to a min(X, Y) or max(X, Y).
374 RecurrenceDescriptor::InstDesc
375 RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) {
376 
377   assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
378          "Expect a select instruction");
379   Instruction *Cmp = nullptr;
380   SelectInst *Select = nullptr;
381 
382   // We must handle the select(cmp()) as a single instruction. Advance to the
383   // select.
384   if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
385     if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
386       return InstDesc(false, I);
387     return InstDesc(Select, Prev.getMinMaxKind());
388   }
389 
390   // Only handle single use cases for now.
391   if (!(Select = dyn_cast<SelectInst>(I)))
392     return InstDesc(false, I);
393   if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
394       !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
395     return InstDesc(false, I);
396   if (!Cmp->hasOneUse())
397     return InstDesc(false, I);
398 
399   Value *CmpLeft;
400   Value *CmpRight;
401 
402   // Look for a min/max pattern.
403   if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
404     return InstDesc(Select, MRK_UIntMin);
405   else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
406     return InstDesc(Select, MRK_UIntMax);
407   else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
408     return InstDesc(Select, MRK_SIntMax);
409   else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
410     return InstDesc(Select, MRK_SIntMin);
411   else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
412     return InstDesc(Select, MRK_FloatMin);
413   else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
414     return InstDesc(Select, MRK_FloatMax);
415   else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
416     return InstDesc(Select, MRK_FloatMin);
417   else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
418     return InstDesc(Select, MRK_FloatMax);
419 
420   return InstDesc(false, I);
421 }
422 
423 RecurrenceDescriptor::InstDesc
424 RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
425                                         InstDesc &Prev, bool HasFunNoNaNAttr) {
426   bool FP = I->getType()->isFloatingPointTy();
427   Instruction *UAI = Prev.getUnsafeAlgebraInst();
428   if (!UAI && FP && !I->hasUnsafeAlgebra())
429     UAI = I; // Found an unsafe (unvectorizable) algebra instruction.
430 
431   switch (I->getOpcode()) {
432   default:
433     return InstDesc(false, I);
434   case Instruction::PHI:
435     return InstDesc(I, Prev.getMinMaxKind(), Prev.getUnsafeAlgebraInst());
436   case Instruction::Sub:
437   case Instruction::Add:
438     return InstDesc(Kind == RK_IntegerAdd, I);
439   case Instruction::Mul:
440     return InstDesc(Kind == RK_IntegerMult, I);
441   case Instruction::And:
442     return InstDesc(Kind == RK_IntegerAnd, I);
443   case Instruction::Or:
444     return InstDesc(Kind == RK_IntegerOr, I);
445   case Instruction::Xor:
446     return InstDesc(Kind == RK_IntegerXor, I);
447   case Instruction::FMul:
448     return InstDesc(Kind == RK_FloatMult, I, UAI);
449   case Instruction::FSub:
450   case Instruction::FAdd:
451     return InstDesc(Kind == RK_FloatAdd, I, UAI);
452   case Instruction::FCmp:
453   case Instruction::ICmp:
454   case Instruction::Select:
455     if (Kind != RK_IntegerMinMax &&
456         (!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
457       return InstDesc(false, I);
458     return isMinMaxSelectCmpPattern(I, Prev);
459   }
460 }
461 
462 bool RecurrenceDescriptor::hasMultipleUsesOf(
463     Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) {
464   unsigned NumUses = 0;
465   for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
466        ++Use) {
467     if (Insts.count(dyn_cast<Instruction>(*Use)))
468       ++NumUses;
469     if (NumUses > 1)
470       return true;
471   }
472 
473   return false;
474 }
475 bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
476                                           RecurrenceDescriptor &RedDes) {
477 
478   BasicBlock *Header = TheLoop->getHeader();
479   Function &F = *Header->getParent();
480   bool HasFunNoNaNAttr =
481       F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
482 
483   if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
484     DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
485     return true;
486   }
487   if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
488     DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
489     return true;
490   }
491   if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) {
492     DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
493     return true;
494   }
495   if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) {
496     DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
497     return true;
498   }
499   if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) {
500     DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
501     return true;
502   }
503   if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr,
504                       RedDes)) {
505     DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
506     return true;
507   }
508   if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
509     DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
510     return true;
511   }
512   if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
513     DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
514     return true;
515   }
516   if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) {
517     DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n");
518     return true;
519   }
520   // Not a reduction of known type.
521   return false;
522 }
523 
524 bool RecurrenceDescriptor::isFirstOrderRecurrence(PHINode *Phi, Loop *TheLoop,
525                                                   DominatorTree *DT) {
526 
527   // Ensure the phi node is in the loop header and has two incoming values.
528   if (Phi->getParent() != TheLoop->getHeader() ||
529       Phi->getNumIncomingValues() != 2)
530     return false;
531 
532   // Ensure the loop has a preheader and a single latch block. The loop
533   // vectorizer will need the latch to set up the next iteration of the loop.
534   auto *Preheader = TheLoop->getLoopPreheader();
535   auto *Latch = TheLoop->getLoopLatch();
536   if (!Preheader || !Latch)
537     return false;
538 
539   // Ensure the phi node's incoming blocks are the loop preheader and latch.
540   if (Phi->getBasicBlockIndex(Preheader) < 0 ||
541       Phi->getBasicBlockIndex(Latch) < 0)
542     return false;
543 
544   // Get the previous value. The previous value comes from the latch edge while
545   // the initial value comes form the preheader edge.
546   auto *Previous = dyn_cast<Instruction>(Phi->getIncomingValueForBlock(Latch));
547   if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous))
548     return false;
549 
550   // Ensure every user of the phi node is dominated by the previous value. The
551   // dominance requirement ensures the loop vectorizer will not need to
552   // vectorize the initial value prior to the first iteration of the loop.
553   for (User *U : Phi->users())
554     if (auto *I = dyn_cast<Instruction>(U))
555       if (!DT->dominates(Previous, I))
556         return false;
557 
558   return true;
559 }
560 
561 /// This function returns the identity element (or neutral element) for
562 /// the operation K.
563 Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
564                                                       Type *Tp) {
565   switch (K) {
566   case RK_IntegerXor:
567   case RK_IntegerAdd:
568   case RK_IntegerOr:
569     // Adding, Xoring, Oring zero to a number does not change it.
570     return ConstantInt::get(Tp, 0);
571   case RK_IntegerMult:
572     // Multiplying a number by 1 does not change it.
573     return ConstantInt::get(Tp, 1);
574   case RK_IntegerAnd:
575     // AND-ing a number with an all-1 value does not change it.
576     return ConstantInt::get(Tp, -1, true);
577   case RK_FloatMult:
578     // Multiplying a number by 1 does not change it.
579     return ConstantFP::get(Tp, 1.0L);
580   case RK_FloatAdd:
581     // Adding zero to a number does not change it.
582     return ConstantFP::get(Tp, 0.0L);
583   default:
584     llvm_unreachable("Unknown recurrence kind");
585   }
586 }
587 
588 /// This function translates the recurrence kind to an LLVM binary operator.
589 unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
590   switch (Kind) {
591   case RK_IntegerAdd:
592     return Instruction::Add;
593   case RK_IntegerMult:
594     return Instruction::Mul;
595   case RK_IntegerOr:
596     return Instruction::Or;
597   case RK_IntegerAnd:
598     return Instruction::And;
599   case RK_IntegerXor:
600     return Instruction::Xor;
601   case RK_FloatMult:
602     return Instruction::FMul;
603   case RK_FloatAdd:
604     return Instruction::FAdd;
605   case RK_IntegerMinMax:
606     return Instruction::ICmp;
607   case RK_FloatMinMax:
608     return Instruction::FCmp;
609   default:
610     llvm_unreachable("Unknown recurrence operation");
611   }
612 }
613 
614 Value *RecurrenceDescriptor::createMinMaxOp(IRBuilder<> &Builder,
615                                             MinMaxRecurrenceKind RK,
616                                             Value *Left, Value *Right) {
617   CmpInst::Predicate P = CmpInst::ICMP_NE;
618   switch (RK) {
619   default:
620     llvm_unreachable("Unknown min/max recurrence kind");
621   case MRK_UIntMin:
622     P = CmpInst::ICMP_ULT;
623     break;
624   case MRK_UIntMax:
625     P = CmpInst::ICMP_UGT;
626     break;
627   case MRK_SIntMin:
628     P = CmpInst::ICMP_SLT;
629     break;
630   case MRK_SIntMax:
631     P = CmpInst::ICMP_SGT;
632     break;
633   case MRK_FloatMin:
634     P = CmpInst::FCMP_OLT;
635     break;
636   case MRK_FloatMax:
637     P = CmpInst::FCMP_OGT;
638     break;
639   }
640 
641   // We only match FP sequences with unsafe algebra, so we can unconditionally
642   // set it on any generated instructions.
643   IRBuilder<>::FastMathFlagGuard FMFG(Builder);
644   FastMathFlags FMF;
645   FMF.setUnsafeAlgebra();
646   Builder.setFastMathFlags(FMF);
647 
648   Value *Cmp;
649   if (RK == MRK_FloatMin || RK == MRK_FloatMax)
650     Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
651   else
652     Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
653 
654   Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
655   return Select;
656 }
657 
658 InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K,
659                                          const SCEV *Step, BinaryOperator *BOp)
660   : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) {
661   assert(IK != IK_NoInduction && "Not an induction");
662 
663   // Start value type should match the induction kind and the value
664   // itself should not be null.
665   assert(StartValue && "StartValue is null");
666   assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) &&
667          "StartValue is not a pointer for pointer induction");
668   assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) &&
669          "StartValue is not an integer for integer induction");
670 
671   // Check the Step Value. It should be non-zero integer value.
672   assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) &&
673          "Step value is zero");
674 
675   assert((IK != IK_PtrInduction || getConstIntStepValue()) &&
676          "Step value should be constant for pointer induction");
677   assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) &&
678          "StepValue is not an integer");
679 
680   assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) &&
681          "StepValue is not FP for FpInduction");
682   assert((IK != IK_FpInduction || (InductionBinOp &&
683           (InductionBinOp->getOpcode() == Instruction::FAdd ||
684            InductionBinOp->getOpcode() == Instruction::FSub))) &&
685          "Binary opcode should be specified for FP induction");
686 }
687 
688 int InductionDescriptor::getConsecutiveDirection() const {
689   ConstantInt *ConstStep = getConstIntStepValue();
690   if (ConstStep && (ConstStep->isOne() || ConstStep->isMinusOne()))
691     return ConstStep->getSExtValue();
692   return 0;
693 }
694 
695 ConstantInt *InductionDescriptor::getConstIntStepValue() const {
696   if (isa<SCEVConstant>(Step))
697     return dyn_cast<ConstantInt>(cast<SCEVConstant>(Step)->getValue());
698   return nullptr;
699 }
700 
701 Value *InductionDescriptor::transform(IRBuilder<> &B, Value *Index,
702                                       ScalarEvolution *SE,
703                                       const DataLayout& DL) const {
704 
705   SCEVExpander Exp(*SE, DL, "induction");
706   assert(Index->getType() == Step->getType() &&
707          "Index type does not match StepValue type");
708   switch (IK) {
709   case IK_IntInduction: {
710     assert(Index->getType() == StartValue->getType() &&
711            "Index type does not match StartValue type");
712 
713     // FIXME: Theoretically, we can call getAddExpr() of ScalarEvolution
714     // and calculate (Start + Index * Step) for all cases, without
715     // special handling for "isOne" and "isMinusOne".
716     // But in the real life the result code getting worse. We mix SCEV
717     // expressions and ADD/SUB operations and receive redundant
718     // intermediate values being calculated in different ways and
719     // Instcombine is unable to reduce them all.
720 
721     if (getConstIntStepValue() &&
722         getConstIntStepValue()->isMinusOne())
723       return B.CreateSub(StartValue, Index);
724     if (getConstIntStepValue() &&
725         getConstIntStepValue()->isOne())
726       return B.CreateAdd(StartValue, Index);
727     const SCEV *S = SE->getAddExpr(SE->getSCEV(StartValue),
728                                    SE->getMulExpr(Step, SE->getSCEV(Index)));
729     return Exp.expandCodeFor(S, StartValue->getType(), &*B.GetInsertPoint());
730   }
731   case IK_PtrInduction: {
732     assert(isa<SCEVConstant>(Step) &&
733            "Expected constant step for pointer induction");
734     const SCEV *S = SE->getMulExpr(SE->getSCEV(Index), Step);
735     Index = Exp.expandCodeFor(S, Index->getType(), &*B.GetInsertPoint());
736     return B.CreateGEP(nullptr, StartValue, Index);
737   }
738   case IK_FpInduction: {
739     assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value");
740     assert(InductionBinOp &&
741            (InductionBinOp->getOpcode() == Instruction::FAdd ||
742             InductionBinOp->getOpcode() == Instruction::FSub) &&
743            "Original bin op should be defined for FP induction");
744 
745     Value *StepValue = cast<SCEVUnknown>(Step)->getValue();
746 
747     // Floating point operations had to be 'fast' to enable the induction.
748     FastMathFlags Flags;
749     Flags.setUnsafeAlgebra();
750 
751     Value *MulExp = B.CreateFMul(StepValue, Index);
752     if (isa<Instruction>(MulExp))
753       // We have to check, the MulExp may be a constant.
754       cast<Instruction>(MulExp)->setFastMathFlags(Flags);
755 
756     Value *BOp = B.CreateBinOp(InductionBinOp->getOpcode() , StartValue,
757                                MulExp, "induction");
758     if (isa<Instruction>(BOp))
759       cast<Instruction>(BOp)->setFastMathFlags(Flags);
760 
761     return BOp;
762   }
763   case IK_NoInduction:
764     return nullptr;
765   }
766   llvm_unreachable("invalid enum");
767 }
768 
769 bool InductionDescriptor::isFPInductionPHI(PHINode *Phi, const Loop *TheLoop,
770                                            ScalarEvolution *SE,
771                                            InductionDescriptor &D) {
772 
773   // Here we only handle FP induction variables.
774   assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type");
775 
776   if (TheLoop->getHeader() != Phi->getParent())
777     return false;
778 
779   // The loop may have multiple entrances or multiple exits; we can analyze
780   // this phi if it has a unique entry value and a unique backedge value.
781   if (Phi->getNumIncomingValues() != 2)
782     return false;
783   Value *BEValue = nullptr, *StartValue = nullptr;
784   if (TheLoop->contains(Phi->getIncomingBlock(0))) {
785     BEValue = Phi->getIncomingValue(0);
786     StartValue = Phi->getIncomingValue(1);
787   } else {
788     assert(TheLoop->contains(Phi->getIncomingBlock(1)) &&
789            "Unexpected Phi node in the loop");
790     BEValue = Phi->getIncomingValue(1);
791     StartValue = Phi->getIncomingValue(0);
792   }
793 
794   BinaryOperator *BOp = dyn_cast<BinaryOperator>(BEValue);
795   if (!BOp)
796     return false;
797 
798   Value *Addend = nullptr;
799   if (BOp->getOpcode() == Instruction::FAdd) {
800     if (BOp->getOperand(0) == Phi)
801       Addend = BOp->getOperand(1);
802     else if (BOp->getOperand(1) == Phi)
803       Addend = BOp->getOperand(0);
804   } else if (BOp->getOpcode() == Instruction::FSub)
805     if (BOp->getOperand(0) == Phi)
806       Addend = BOp->getOperand(1);
807 
808   if (!Addend)
809     return false;
810 
811   // The addend should be loop invariant
812   if (auto *I = dyn_cast<Instruction>(Addend))
813     if (TheLoop->contains(I))
814       return false;
815 
816   // FP Step has unknown SCEV
817   const SCEV *Step = SE->getUnknown(Addend);
818   D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp);
819   return true;
820 }
821 
822 bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop,
823                                          PredicatedScalarEvolution &PSE,
824                                          InductionDescriptor &D,
825                                          bool Assume) {
826   Type *PhiTy = Phi->getType();
827 
828   // Handle integer and pointer inductions variables.
829   // Now we handle also FP induction but not trying to make a
830   // recurrent expression from the PHI node in-place.
831 
832   if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() &&
833       !PhiTy->isFloatTy() && !PhiTy->isDoubleTy() && !PhiTy->isHalfTy())
834     return false;
835 
836   if (PhiTy->isFloatingPointTy())
837     return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D);
838 
839   const SCEV *PhiScev = PSE.getSCEV(Phi);
840   const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
841 
842   // We need this expression to be an AddRecExpr.
843   if (Assume && !AR)
844     AR = PSE.getAsAddRec(Phi);
845 
846   if (!AR) {
847     DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
848     return false;
849   }
850 
851   return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR);
852 }
853 
854 bool InductionDescriptor::isInductionPHI(PHINode *Phi, const Loop *TheLoop,
855                                          ScalarEvolution *SE,
856                                          InductionDescriptor &D,
857                                          const SCEV *Expr) {
858   Type *PhiTy = Phi->getType();
859   // We only handle integer and pointer inductions variables.
860   if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
861     return false;
862 
863   // Check that the PHI is consecutive.
864   const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
865   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
866 
867   if (!AR) {
868     DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
869     return false;
870   }
871 
872   assert(TheLoop->getHeader() == Phi->getParent() &&
873          "PHI is an AddRec for a different loop?!");
874   Value *StartValue =
875     Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader());
876   const SCEV *Step = AR->getStepRecurrence(*SE);
877   // Calculate the pointer stride and check if it is consecutive.
878   // The stride may be a constant or a loop invariant integer value.
879   const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step);
880   if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop))
881     return false;
882 
883   if (PhiTy->isIntegerTy()) {
884     D = InductionDescriptor(StartValue, IK_IntInduction, Step);
885     return true;
886   }
887 
888   assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
889   // Pointer induction should be a constant.
890   if (!ConstStep)
891     return false;
892 
893   ConstantInt *CV = ConstStep->getValue();
894   Type *PointerElementType = PhiTy->getPointerElementType();
895   // The pointer stride cannot be determined if the pointer element type is not
896   // sized.
897   if (!PointerElementType->isSized())
898     return false;
899 
900   const DataLayout &DL = Phi->getModule()->getDataLayout();
901   int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
902   if (!Size)
903     return false;
904 
905   int64_t CVSize = CV->getSExtValue();
906   if (CVSize % Size)
907     return false;
908   auto *StepValue = SE->getConstant(CV->getType(), CVSize / Size,
909                                     true /* signed */);
910   D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue);
911   return true;
912 }
913 
914 /// \brief Returns the instructions that use values defined in the loop.
915 SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) {
916   SmallVector<Instruction *, 8> UsedOutside;
917 
918   for (auto *Block : L->getBlocks())
919     // FIXME: I believe that this could use copy_if if the Inst reference could
920     // be adapted into a pointer.
921     for (auto &Inst : *Block) {
922       auto Users = Inst.users();
923       if (any_of(Users, [&](User *U) {
924             auto *Use = cast<Instruction>(U);
925             return !L->contains(Use->getParent());
926           }))
927         UsedOutside.push_back(&Inst);
928     }
929 
930   return UsedOutside;
931 }
932 
933 void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) {
934   // By definition, all loop passes need the LoopInfo analysis and the
935   // Dominator tree it depends on. Because they all participate in the loop
936   // pass manager, they must also preserve these.
937   AU.addRequired<DominatorTreeWrapperPass>();
938   AU.addPreserved<DominatorTreeWrapperPass>();
939   AU.addRequired<LoopInfoWrapperPass>();
940   AU.addPreserved<LoopInfoWrapperPass>();
941 
942   // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
943   // here because users shouldn't directly get them from this header.
944   extern char &LoopSimplifyID;
945   extern char &LCSSAID;
946   AU.addRequiredID(LoopSimplifyID);
947   AU.addPreservedID(LoopSimplifyID);
948   AU.addRequiredID(LCSSAID);
949   AU.addPreservedID(LCSSAID);
950   // This is used in the LPPassManager to perform LCSSA verification on passes
951   // which preserve lcssa form
952   AU.addRequired<LCSSAVerificationPass>();
953   AU.addPreserved<LCSSAVerificationPass>();
954 
955   // Loop passes are designed to run inside of a loop pass manager which means
956   // that any function analyses they require must be required by the first loop
957   // pass in the manager (so that it is computed before the loop pass manager
958   // runs) and preserved by all loop pasess in the manager. To make this
959   // reasonably robust, the set needed for most loop passes is maintained here.
960   // If your loop pass requires an analysis not listed here, you will need to
961   // carefully audit the loop pass manager nesting structure that results.
962   AU.addRequired<AAResultsWrapperPass>();
963   AU.addPreserved<AAResultsWrapperPass>();
964   AU.addPreserved<BasicAAWrapperPass>();
965   AU.addPreserved<GlobalsAAWrapperPass>();
966   AU.addPreserved<SCEVAAWrapperPass>();
967   AU.addRequired<ScalarEvolutionWrapperPass>();
968   AU.addPreserved<ScalarEvolutionWrapperPass>();
969 }
970 
971 /// Manually defined generic "LoopPass" dependency initialization. This is used
972 /// to initialize the exact set of passes from above in \c
973 /// getLoopAnalysisUsage. It can be used within a loop pass's initialization
974 /// with:
975 ///
976 ///   INITIALIZE_PASS_DEPENDENCY(LoopPass)
977 ///
978 /// As-if "LoopPass" were a pass.
979 void llvm::initializeLoopPassPass(PassRegistry &Registry) {
980   INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
981   INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
982   INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
983   INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
984   INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
985   INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
986   INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
987   INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
988   INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
989 }
990 
991 /// \brief Find string metadata for loop
992 ///
993 /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an
994 /// operand or null otherwise.  If the string metadata is not found return
995 /// Optional's not-a-value.
996 Optional<const MDOperand *> llvm::findStringMetadataForLoop(Loop *TheLoop,
997                                                             StringRef Name) {
998   MDNode *LoopID = TheLoop->getLoopID();
999   // Return none if LoopID is false.
1000   if (!LoopID)
1001     return None;
1002 
1003   // First operand should refer to the loop id itself.
1004   assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
1005   assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
1006 
1007   // Iterate over LoopID operands and look for MDString Metadata
1008   for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
1009     MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
1010     if (!MD)
1011       continue;
1012     MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1013     if (!S)
1014       continue;
1015     // Return true if MDString holds expected MetaData.
1016     if (Name.equals(S->getString()))
1017       switch (MD->getNumOperands()) {
1018       case 1:
1019         return nullptr;
1020       case 2:
1021         return &MD->getOperand(1);
1022       default:
1023         llvm_unreachable("loop metadata has 0 or 1 operand");
1024       }
1025   }
1026   return None;
1027 }
1028 
1029 /// Returns true if the instruction in a loop is guaranteed to execute at least
1030 /// once.
1031 bool llvm::isGuaranteedToExecute(const Instruction &Inst,
1032                                  const DominatorTree *DT, const Loop *CurLoop,
1033                                  const LoopSafetyInfo *SafetyInfo) {
1034   // We have to check to make sure that the instruction dominates all
1035   // of the exit blocks.  If it doesn't, then there is a path out of the loop
1036   // which does not execute this instruction, so we can't hoist it.
1037 
1038   // If the instruction is in the header block for the loop (which is very
1039   // common), it is always guaranteed to dominate the exit blocks.  Since this
1040   // is a common case, and can save some work, check it now.
1041   if (Inst.getParent() == CurLoop->getHeader())
1042     // If there's a throw in the header block, we can't guarantee we'll reach
1043     // Inst.
1044     return !SafetyInfo->HeaderMayThrow;
1045 
1046   // Somewhere in this loop there is an instruction which may throw and make us
1047   // exit the loop.
1048   if (SafetyInfo->MayThrow)
1049     return false;
1050 
1051   // Get the exit blocks for the current loop.
1052   SmallVector<BasicBlock *, 8> ExitBlocks;
1053   CurLoop->getExitBlocks(ExitBlocks);
1054 
1055   // Verify that the block dominates each of the exit blocks of the loop.
1056   for (BasicBlock *ExitBlock : ExitBlocks)
1057     if (!DT->dominates(Inst.getParent(), ExitBlock))
1058       return false;
1059 
1060   // As a degenerate case, if the loop is statically infinite then we haven't
1061   // proven anything since there are no exit blocks.
1062   if (ExitBlocks.empty())
1063     return false;
1064 
1065   // FIXME: In general, we have to prove that the loop isn't an infinite loop.
1066   // See http::llvm.org/PR24078 .  (The "ExitBlocks.empty()" check above is
1067   // just a special case of this.)
1068   return true;
1069 }
1070 
1071 Optional<unsigned> llvm::getLoopEstimatedTripCount(Loop *L) {
1072   // Only support loops with a unique exiting block, and a latch.
1073   if (!L->getExitingBlock())
1074     return None;
1075 
1076   // Get the branch weights for the the loop's backedge.
1077   BranchInst *LatchBR =
1078       dyn_cast<BranchInst>(L->getLoopLatch()->getTerminator());
1079   if (!LatchBR || LatchBR->getNumSuccessors() != 2)
1080     return None;
1081 
1082   assert((LatchBR->getSuccessor(0) == L->getHeader() ||
1083           LatchBR->getSuccessor(1) == L->getHeader()) &&
1084          "At least one edge out of the latch must go to the header");
1085 
1086   // To estimate the number of times the loop body was executed, we want to
1087   // know the number of times the backedge was taken, vs. the number of times
1088   // we exited the loop.
1089   uint64_t TrueVal, FalseVal;
1090   if (!LatchBR->extractProfMetadata(TrueVal, FalseVal))
1091     return None;
1092 
1093   if (!TrueVal || !FalseVal)
1094     return 0;
1095 
1096   // Divide the count of the backedge by the count of the edge exiting the loop,
1097   // rounding to nearest.
1098   if (LatchBR->getSuccessor(0) == L->getHeader())
1099     return (TrueVal + (FalseVal / 2)) / FalseVal;
1100   else
1101     return (FalseVal + (TrueVal / 2)) / TrueVal;
1102 }
1103