LLVM 24.0.0git
IVDescriptors.h
Go to the documentation of this file.
1//===- llvm/Analysis/IVDescriptors.h - 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#ifndef LLVM_ANALYSIS_IVDESCRIPTORS_H
14#define LLVM_ANALYSIS_IVDESCRIPTORS_H
15
19#include "llvm/IR/ValueHandle.h"
21
22namespace llvm {
23
24class AssumptionCache;
25class DemandedBits;
26class DominatorTree;
27class Loop;
29class ScalarEvolution;
30class SCEV;
31class SCEVPredicate;
32class StoreInst;
33enum class SCEVFlags;
34template <typename SCEVPtrT> struct SCEVUseT;
36
37/// These are the kinds of recurrences that we support.
38enum class RecurKind {
39 // clang-format off
40 None, ///< Not a recurrence.
41 Add, ///< Sum of integers.
42 Sub, ///< Subtraction of integers
43 AddChainWithSubs, ///< A chain of adds and subs
44 Mul, ///< Product of integers.
45 Or, ///< Bitwise or logical OR of integers.
46 And, ///< Bitwise or logical AND of integers.
47 Xor, ///< Bitwise or logical XOR of integers.
48 SMin, ///< Signed integer min implemented in terms of select(cmp()).
49 SMax, ///< Signed integer max implemented in terms of select(cmp()).
50 UMin, ///< Unsigned integer min implemented in terms of select(cmp()).
51 UMax, ///< Unsigned integer max implemented in terms of select(cmp()).
52 FAdd, ///< Sum of floats.
53 FAddChainWithSubs, ///< A chain of fadds and fsubs.
54 FSub, ///< Subtraction of floats.
55 FMul, ///< Product of floats.
56 FMin, ///< FP min as select(cmp()) with nnan/nsz, or llvm.minnum.
57 FMax, ///< FP max as select(cmp()) with nnan/nsz, or llvm.maxnum.
58 FMinNum, ///< FP min with llvm.minnum semantics including NaNs.
59 FMaxNum, ///< FP max with llvm.maxnum semantics including NaNs.
60 FMinSelectNoFMF, ///< FMin as select(cmp()) lacking nnan or nsz, on IEEE types
61 ///< with IEEE denormals.
62 FMaxSelectNoFMF, ///< FMax as select(cmp()) lacking nnan or nsz, on IEEE types
63 ///< with IEEE denormals.
64 FMinimum, ///< FP min with llvm.minimum semantics
65 FMaximum, ///< FP max with llvm.maximum semantics
66 FMinimumNum, ///< FP min with llvm.minimumnum semantics
67 FMaximumNum, ///< FP max with llvm.maximumnum semantics
68 FMulAdd, ///< Sum of float products with llvm.fmuladd(a * b + sum).
69 AnyOf, ///< AnyOf reduction with select(cond,x,y) where one of (x,y) is
70 ///< loop invariant, and both x and y are integer type.
71 FindIV, ///< FindIV reduction with select(icmp(),x,y) where one of (x,y) is
72 ///< a loop induction variable (increasing or decreasing), and both
73 ///< x and y are integer type. The signedness and direction are
74 ///< stored separately.
75 FindLast, ///< FindLast reduction with select(cmp(),x,y) where x and y
76 ///< are an integer type, one is the current recurrence value,
77 ///< and the other is an arbitrary value.
78 // clang-format on
79 // TODO: Any_of and FindLast reduction need not be restricted to integer type
80 // only.
81};
82
83/// The RecurrenceDescriptor is used to identify recurrences variables in a
84/// loop. Reduction is a special case of recurrence that has uses of the
85/// recurrence variable outside the loop. The method isReductionPHI identifies
86/// reductions that are basic recurrences.
87///
88/// Basic recurrences are defined as the summation, product, OR, AND, XOR, min,
89/// or max of a set of terms. For example: for(i=0; i<n; i++) { total +=
90/// array[i]; } is a summation of array elements. Basic recurrences are a
91/// special case of chains of recurrences (CR). See ScalarEvolution for CR
92/// references.
93
94/// This struct holds information about recurrence variables.
96public:
98
100 RecurKind K, FastMathFlags FMF, Instruction *ExactFP,
101 Type *RT, bool Signed, bool Ordered,
103 Type *MinWidthCastToRecurTy,
104 bool PhiHasUsesOutsideReductionChain = false)
105 : IntermediateStore(Store), StartValue(Start), LoopExitInstr(Exit),
106 Kind(K), FMF(FMF), ExactFPMathInst(ExactFP), RecurrenceType(RT),
107 IsSigned(Signed), IsOrdered(Ordered),
108 PhiHasUsesOutsideReductionChain(PhiHasUsesOutsideReductionChain),
109 MinWidthCastToRecurrenceType(MinWidthCastToRecurTy) {
110 CastInsts.insert_range(CI);
111 assert(
112 (!PhiHasUsesOutsideReductionChain || isMinMaxRecurrenceKind(K)) &&
113 "Only min/max recurrences are allowed to have multiple uses currently");
114 }
115
116 /// Simpler constructor for min/max recurrences that don't track cast
117 /// instructions.
119 RecurKind K, FastMathFlags FMF, Instruction *ExactFP,
120 Type *RT, bool IsMultiUse = false)
121 : IntermediateStore(Store), StartValue(Start), LoopExitInstr(Exit),
122 Kind(K), FMF(FMF), ExactFPMathInst(ExactFP), RecurrenceType(RT),
123 PhiHasUsesOutsideReductionChain(IsMultiUse) {}
124
125 /// This POD struct holds information about a potential recurrence operation.
126 class InstDesc {
127 public:
128 InstDesc(bool IsRecur, Instruction *I, Instruction *ExactFP = nullptr)
129 : IsRecurrence(IsRecur), PatternLastInst(I),
130 RecKind(RecurKind::None), ExactFPMathInst(ExactFP) {}
131
133 : IsRecurrence(true), PatternLastInst(I), RecKind(K),
134 ExactFPMathInst(ExactFP) {}
135
136 bool isRecurrence() const { return IsRecurrence; }
137
138 bool needsExactFPMath() const { return ExactFPMathInst != nullptr; }
139
140 Instruction *getExactFPMathInst() const { return ExactFPMathInst; }
141
142 RecurKind getRecKind() const { return RecKind; }
143
144 Instruction *getPatternInst() const { return PatternLastInst; }
145
146 private:
147 // Is this instruction a recurrence candidate.
148 bool IsRecurrence;
149 // The last instruction in a min/max pattern (select of the select(icmp())
150 // pattern), or the current recurrence instruction otherwise.
151 Instruction *PatternLastInst;
152 // If this is a min/max pattern.
153 RecurKind RecKind;
154 // Recurrence does not allow floating-point reassociation.
155 Instruction *ExactFPMathInst;
156 };
157
158 /// Returns a struct describing if the instruction 'I' can be a recurrence
159 /// variable of type 'Kind' for a Loop \p L and reduction PHI \p Phi.
160 /// If the recurrence is a min/max pattern of select(icmp()) this function
161 /// advances the instruction pointer 'I' from the compare instruction to the
162 /// select instruction and stores this pointer in 'PatternLastInst' member of
163 /// the returned struct.
164 LLVM_ABI static InstDesc isRecurrenceInstr(Loop *L, PHINode *Phi,
165 Instruction *I, RecurKind Kind,
166 InstDesc &Prev,
167 ScalarEvolution *SE);
168
169 /// Returns true if instruction I has multiple uses in Insts
172 unsigned MaxNumUses);
173
174 /// Returns true if all uses of the instruction I is within the Set.
175 LLVM_ABI static bool areAllUsesIn(Instruction *I,
177
178 /// Returns a struct describing whether the instruction is a
179 /// Select(Cond, X, Y)
180 /// where one of (X, Y) is a loop invariant integer and the other is a PHI
181 /// value. \p Prev specifies the description of an already processed select
182 /// instruction, so its corresponding cmp can be matched to it.
183 LLVM_ABI static InstDesc isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
184 Instruction *I, InstDesc &Prev);
185
186 /// Returns a struct describing whether the instruction is either a
187 /// Select(ICmp(A, B), X, Y), or
188 /// Select(FCmp(A, B), X, Y)
189 /// where one of (X, Y) is an increasing (FindLastIV) or decreasing
190 /// (FindFirstIV) loop induction variable, or an arbitrary integer value
191 /// (FindLast), and the other is a PHI value.
192 LLVM_ABI static InstDesc isFindPattern(Loop *TheLoop, PHINode *OrigPhi,
194
195 /// Returns a struct describing if the instruction is a
196 /// Select(FCmp(X, Y), (Z = X op PHINode), PHINode) instruction pattern.
198
199 /// Returns the opcode corresponding to the RecurrenceKind.
200 LLVM_ABI static unsigned getOpcode(RecurKind Kind);
201
202 /// Returns true if Phi is a reduction of type Kind and adds it to the
203 /// RecurrenceDescriptor. If either \p DB is non-null or \p AC and \p DT are
204 /// non-null, the minimal bit width needed to compute the reduction will be
205 /// computed.
206 LLVM_ABI static bool
207 AddReductionVar(PHINode *Phi, RecurKind Kind, Loop *TheLoop,
208 RecurrenceDescriptor &RedDes, DemandedBits *DB = nullptr,
209 AssumptionCache *AC = nullptr, DominatorTree *DT = nullptr,
210 ScalarEvolution *SE = nullptr);
211
212 /// Returns true if Phi is a reduction in TheLoop. The RecurrenceDescriptor
213 /// is returned in RedDes. If either \p DB is non-null or \p AC and \p DT are
214 /// non-null, the minimal bit width needed to compute the reduction will be
215 /// computed. If \p SE is non-null, store instructions to loop invariant
216 /// addresses are processed.
217 LLVM_ABI static bool
218 isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes,
219 DemandedBits *DB = nullptr, AssumptionCache *AC = nullptr,
220 DominatorTree *DT = nullptr, ScalarEvolution *SE = nullptr);
221
222 /// Returns true if Phi is a fixed-order recurrence. A fixed-order recurrence
223 /// is a non-reduction recurrence relation in which the value of the
224 /// recurrence in the current loop iteration equals a value defined in a
225 /// previous iteration (e.g. if the value is defined in the previous
226 /// iteration, we refer to it as first-order recurrence, if it is defined in
227 /// the iteration before the previous, we refer to it as second-order
228 /// recurrence and so on). Note that this function optimistically assumes that
229 /// uses of the recurrence can be re-ordered if necessary and users need to
230 /// check and perform the re-ordering.
231 LLVM_ABI static bool isFixedOrderRecurrence(PHINode *Phi, Loop *TheLoop,
232 DominatorTree *DT);
233
234 RecurKind getRecurrenceKind() const { return Kind; }
235
236 unsigned getOpcode() const { return getOpcode(getRecurrenceKind()); }
237
238 FastMathFlags getFastMathFlags() const { return FMF; }
239
240 TrackingVH<Value> getRecurrenceStartValue() const { return StartValue; }
241
242 Instruction *getLoopExitInstr() const { return LoopExitInstr; }
243
244 /// Returns true if the recurrence has floating-point math that requires
245 /// precise (ordered) operations.
246 bool hasExactFPMath() const { return ExactFPMathInst != nullptr; }
247
248 /// Returns 1st non-reassociative FP instruction in the PHI node's use-chain.
249 Instruction *getExactFPMathInst() const { return ExactFPMathInst; }
250
251 /// Returns true if the recurrence kind is an integer kind.
253
254 /// Returns true if the recurrence kind is a floating point kind.
256
257 /// Returns true if the recurrence kind is for a sub operation.
258 LLVM_ABI static bool isSubRecurrenceKind(RecurKind Kind);
259
260 /// Returns true if the recurrence kind is an integer min/max kind.
262 return Kind == RecurKind::UMin || Kind == RecurKind::UMax ||
263 Kind == RecurKind::SMin || Kind == RecurKind::SMax;
264 }
265
266 /// Returns true if the recurrence kind is a floating-point minnum/maxnum
267 /// kind.
269 return Kind == RecurKind::FMinNum || Kind == RecurKind::FMaxNum;
270 }
271
272 /// Returns true if the recurrence kind is a select-based floating-point
273 /// min/max kind lacking nnan or nsz.
278
279 /// Returns true if the recurrence kind is a floating-point min/max kind.
281 return Kind == RecurKind::FMin || Kind == RecurKind::FMax ||
282 Kind == RecurKind::FMinimum || Kind == RecurKind::FMaximum ||
286 }
287
288 /// Returns true if the recurrence kind is any min/max kind.
291 }
292
293 /// Returns true if the recurrence kind is of the form
294 /// select(cond,x,y) where one of (x,y) is loop invariant.
296 return Kind == RecurKind::AnyOf;
297 }
298
299 /// Returns true if the recurrence kind is of the form
300 /// select(cmp(),x,y) where one of (x,y) is a loop induction variable.
302 return Kind == RecurKind::FindIV;
303 }
304
305 /// Returns true if the recurrence kind is of the form
306 /// select(cmp(),x,y) where one of (x,y) is an arbitrary value and the
307 /// other is a recurrence.
309 return Kind == RecurKind::FindLast;
310 }
311
312 static bool isFindRecurrenceKind(RecurKind Kind) {
314 }
315
316 /// Returns the type of the recurrence. This type can be narrower than the
317 /// actual type of the Phi if the recurrence has been type-promoted.
318 Type *getRecurrenceType() const { return RecurrenceType; }
319
320 /// Returns a reference to the instructions used for type-promoting the
321 /// recurrence.
322 const SmallPtrSet<Instruction *, 8> &getCastInsts() const { return CastInsts; }
323
324 /// Returns the scalar source type of the narrowest cast to the recurrence
325 /// type, or null if there is no such cast.
327 return MinWidthCastToRecurrenceType;
328 }
329
330 /// Returns true if all source operands of the recurrence are SExtInsts.
331 bool isSigned() const { return IsSigned; }
332
333 /// Expose an ordered FP reduction to the instance users.
334 bool isOrdered() const { return IsOrdered; }
335
336 /// Returns true if the reduction PHI has any uses outside the reduction
337 /// chain. This is relevant for min/max reductions that are part of a FindIV
338 /// pattern.
340 return PhiHasUsesOutsideReductionChain;
341 }
342
343 /// Attempts to find a chain of operations from Phi to LoopExitInst that can
344 /// be treated as a set of reductions instructions for in-loop reductions.
346 Loop *L) const;
347
348 /// Returns true if the instruction is a call to the llvm.fmuladd intrinsic.
350 return isa<IntrinsicInst>(I) &&
351 cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fmuladd;
352 }
353
354 /// Reductions may store temporary or final result to an invariant address.
355 /// If there is such a store in the loop then, after successfull run of
356 /// AddReductionVar method, this field will be assigned the last met store.
358
359private:
360 // The starting value of the recurrence.
361 // It does not have to be zero!
362 TrackingVH<Value> StartValue;
363 // The instruction who's value is used outside the loop.
364 Instruction *LoopExitInstr = nullptr;
365 // The kind of the recurrence.
367 // The fast-math flags on the recurrent instructions. We propagate these
368 // fast-math flags into the vectorized FP instructions we generate.
369 FastMathFlags FMF;
370 // First instance of non-reassociative floating-point in the PHI's use-chain.
371 Instruction *ExactFPMathInst = nullptr;
372 // The type of the recurrence.
373 Type *RecurrenceType = nullptr;
374 // True if all source operands of the recurrence are SExtInsts.
375 bool IsSigned = false;
376 // True if this recurrence can be treated as an in-order reduction.
377 // Currently only a non-reassociative FAdd can be considered in-order,
378 // if it is also the only FAdd in the PHI's use chain.
379 bool IsOrdered = false;
380 // True if the reduction PHI has in-loop users outside the reduction chain.
381 // This is relevant for min/max reductions that are part of a FindIV pattern.
382 bool PhiHasUsesOutsideReductionChain = false;
383 // Instructions used for type-promoting the recurrence.
385 // The scalar source type of the narrowest cast to the recurrence type.
386 Type *MinWidthCastToRecurrenceType = nullptr;
387};
388
389/// A struct for saving information about induction variables.
391public:
392 /// This enum represents the kinds of inductions that we support.
394 IK_NoInduction, ///< Not an induction variable.
395 IK_IntInduction, ///< Integer induction variable. Step = C.
396 IK_PtrInduction, ///< Pointer induction var. Step = C.
397 IK_FpInduction ///< Floating point induction variable.
398 };
399
400public:
401 /// Default constructor - creates an invalid induction.
403
404 /// Returns the canonical integer induction for type \p Ty with start = 0
405 /// and step = 1.
408
409 Value *getStartValue() const { return StartValue; }
410 InductionKind getKind() const { return IK; }
411 const SCEV *getStep() const { return Step; }
412 BinaryOperator *getInductionBinOp() const { return InductionBinOp; }
413 LLVM_ABI std::optional<APInt> getConstStepValue() const;
414
415 /// Returns true if \p Phi is an induction in the loop \p L. If \p Phi is an
416 /// induction, the induction descriptor \p D will contain the data describing
417 /// this induction. Since Induction Phis can only be present inside loop
418 /// headers, the function will assert if it is passed a Phi whose parent is
419 /// not the loop header. If by some other means the caller has a better SCEV
420 /// expression for \p Phi than the one returned by the ScalarEvolution
421 /// analysis, it can be passed through \p Expr. If the def-use chain
422 /// associated with the phi includes casts (that we know we can ignore
423 /// under proper runtime checks), they are passed through \p CastsToIgnore.
424 /// SCEV predicates checking potential overflow for \p Phi to be an induction,
425 /// if any, are passed via \p NoWrapPreds and recorded.
426 LLVM_ABI static bool
427 isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE,
429 ArrayRef<const SCEVPredicate *> NoWrapPreds = {},
430 const SCEV *Expr = nullptr,
431 SmallVectorImpl<Instruction *> *CastsToIgnore = nullptr);
432
433 /// Returns true if \p Phi is a floating point induction in the loop \p L.
434 /// If \p Phi is an induction, the induction descriptor \p D will contain
435 /// the data describing this induction.
436 LLVM_ABI static bool isFPInductionPHI(PHINode *Phi, const Loop *L,
437 ScalarEvolution *SE,
439
440 /// Returns true if \p Phi is a loop \p L induction, in the context associated
441 /// with the run-time predicate of PSE. If \p Assume is true, this can add
442 /// further SCEV predicates to \p PSE in order to prove that \p Phi is an
443 /// induction.
444 /// If \p Phi is an induction, \p D will contain the data describing this
445 /// induction.
446 LLVM_ABI static bool isInductionPHI(PHINode *Phi, const Loop *L,
447 PredicatedScalarEvolution &PSE,
449 bool Assume = false);
450
451 /// Returns floating-point induction operator that does not allow
452 /// reassociation (transforming the induction requires an override of normal
453 /// floating-point rules).
455 if (IK == IK_FpInduction && InductionBinOp &&
456 !InductionBinOp->hasAllowReassoc())
457 return InductionBinOp;
458 return nullptr;
459 }
460
461 /// Returns binary opcode of the induction operator.
463 return InductionBinOp ? InductionBinOp->getOpcode()
464 : Instruction::BinaryOpsEnd;
465 }
466
467 /// Returns an ArrayRef to the type cast instructions in the induction
468 /// update chain, that are redundant when guarded with a runtime
469 /// SCEV overflow check.
470 ArrayRef<Instruction *> getCastInsts() const { return RedundantCasts; }
471
472 /// Returns the SCEV predicates associated with this induction.
474 return NoWrapPredicates;
475 }
476
477private:
478 /// Private constructor - used by \c isInductionPHI and
479 /// \c getCanonicalIntInduction.
480 InductionDescriptor(Value *Start, InductionKind K, const SCEV *Step,
481 BinaryOperator *InductionBinOp = nullptr,
482 SmallVectorImpl<Instruction *> *Casts = nullptr,
483 ArrayRef<const SCEVPredicate *> NoWrapPreds = {});
484
485 /// Start value.
486 TrackingVH<Value> StartValue;
487 /// Induction kind.
488 InductionKind IK = IK_NoInduction;
489 /// Step value.
490 const SCEV *Step = nullptr;
491 // Instruction that advances induction variable.
492 BinaryOperator *InductionBinOp = nullptr;
493 // Instructions used for type-casts of the induction variable,
494 // that are redundant when guarded with a runtime SCEV overflow check.
495 SmallVector<Instruction *, 2> RedundantCasts;
496 // SCEV predicates checking overflow needed for this induction.
498};
499
500/// Describes a conditional induction variable: an induction variable that is
501/// updated only on loop iterations for which a certain predicate is satisfied.
502/// Its step must be loop-invariant.
504public:
506
507 /// Returns true if \p PN is a conditional induction variable in the loop
508 /// \p L. If it is, \p Desc will contain the data describing the PHI.
509 LLVM_ABI static bool
512 ScalarEvolution &SE);
513
514 /// Returns the header PHI described by this descriptor.
515 PHINode *getHeaderPHI() const { return HeaderPHI; }
516
517 /// Returns the backedge PHI that selects between StepInst and the HeaderPHI.
518 PHINode *getBackedgePHI() const { return BackedgePHI; }
519
520 /// Returns the instruction that updates the conditional induction PHI.
521 Instruction *getStepInst() const { return StepInst; }
522
523 /// Returns a SCEV expression for the initial value of the conditional
524 /// induction PHI.
525 const SCEV *getStartSCEV() const { return StartSCEV; }
526
527 /// Returns a SCEV expression for the step of the conditional induction PHI.
528 /// This is the value it increments by when the predicate is satisfied.
529 const SCEV *getStepSCEV() const { return StepSCEV; }
530
531 /// Returns the SCEV no-wrap flags that apply to StepInst.
532 SCEVFlags getSCEVNoWrapFlags() const { return NoWrapFlags; }
533
534 /// Returns an over-approximation of the conditional induction as a SCEVAddRec
535 /// assuming the condition is always true. The loop \p L must be the loop
536 /// containing the conditional induction.
538 const Loop &L) const;
539
540private:
541 ConditionalInductionDescriptor(PHINode *HeaderPHI, PHINode *BackedgePHI,
542 Instruction *StepInst, const SCEV *StartSCEV,
543 const SCEV *StepSCEV, SCEVFlags NoWrapFlags)
544 : HeaderPHI(HeaderPHI), BackedgePHI(BackedgePHI), StepInst(StepInst),
545 StartSCEV(StartSCEV), StepSCEV(StepSCEV), NoWrapFlags(NoWrapFlags) {}
546
547 /// The header PHI (this is the PHI described by the descriptor).
548 PHINode *HeaderPHI = nullptr;
549
550 /// The backedge PHI that selects between StepInst and the HeaderPHI.
551 PHINode *BackedgePHI = nullptr;
552
553 /// The instruction that updates the conditional induction PHI.
554 Instruction *StepInst = nullptr;
555
556 /// SCEV expression representing the start value for the conditional
557 /// induction PHI.
558 const SCEV *StartSCEV = nullptr;
559
560 /// SCEV expression representing the step value for the conditional induction
561 /// PHI.
562 const SCEV *StepSCEV = nullptr;
563
564 /// The SCEV no-wrap flags that apply to StepInst.
565 SCEVFlags NoWrapFlags{};
566};
567
568} // end namespace llvm
569
570#endif // LLVM_ANALYSIS_IVDESCRIPTORS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ABI
Definition Compiler.h:211
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
SCEVFlags getSCEVNoWrapFlags() const
Returns the SCEV no-wrap flags that apply to StepInst.
LLVM_ABI SCEVUse getUnconditionalAddRec(ScalarEvolution &SE, const Loop &L) const
Returns an over-approximation of the conditional induction as a SCEVAddRec assuming the condition is ...
PHINode * getHeaderPHI() const
Returns the header PHI described by this descriptor.
static LLVM_ABI bool isConditionalInductionPHI(PHINode *PN, const Loop *L, ConditionalInductionDescriptor &Desc, ScalarEvolution &SE)
Returns true if PN is a conditional induction variable in the loop L.
Instruction * getStepInst() const
Returns the instruction that updates the conditional induction PHI.
const SCEV * getStartSCEV() const
Returns a SCEV expression for the initial value of the conditional induction PHI.
const SCEV * getStepSCEV() const
Returns a SCEV expression for the step of the conditional induction PHI.
PHINode * getBackedgePHI() const
Returns the backedge PHI that selects between StepInst and the HeaderPHI.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
A struct for saving information about induction variables.
BinaryOperator * getInductionBinOp() const
InductionKind getKind() const
static LLVM_ABI InductionDescriptor getCanonicalIntInduction(Type *Ty, ScalarEvolution &SE)
Returns the canonical integer induction for type Ty with start = 0 and step = 1.
LLVM_ABI std::optional< APInt > getConstStepValue() const
Instruction * getExactFPMathInst() const
Returns floating-point induction operator that does not allow reassociation (transforming the inducti...
const SCEV * getStep() const
ArrayRef< const SCEVPredicate * > getNoWrapPredicates() const
Returns the SCEV predicates associated with this induction.
static LLVM_ABI bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, ArrayRef< const SCEVPredicate * > NoWrapPreds={}, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction * > *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
ArrayRef< Instruction * > getCastInsts() const
Returns an ArrayRef to the type cast instructions in the induction update chain, that are redundant w...
InductionKind
This enum represents the kinds of inductions that we support.
@ IK_NoInduction
Not an induction variable.
@ IK_FpInduction
Floating point induction variable.
@ IK_PtrInduction
Pointer induction var. Step = C.
@ IK_IntInduction
Integer induction variable. Step = C.
static LLVM_ABI bool isFPInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D)
Returns true if Phi is a floating point induction in the loop L.
Instruction::BinaryOps getInductionOpcode() const
Returns binary opcode of the induction operator.
Value * getStartValue() const
InductionDescriptor()=default
Default constructor - creates an invalid induction.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
InstDesc(bool IsRecur, Instruction *I, Instruction *ExactFP=nullptr)
InstDesc(Instruction *I, RecurKind K, Instruction *ExactFP=nullptr)
Instruction * getExactFPMathInst() const
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
static bool isFPMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating-point min/max kind.
static bool isFPMinMaxSelectNoFMFRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a select-based floating-point min/max kind lacking nnan or nsz...
Instruction * getExactFPMathInst() const
Returns 1st non-reassociative FP instruction in the PHI node's use-chain.
static bool isFMulAddIntrinsic(Instruction *I)
Returns true if the instruction is a call to the llvm.fmuladd intrinsic.
Type * getMinWidthCastToRecurrenceType() const
Returns the scalar source type of the narrowest cast to the recurrence type, or null if there is no s...
FastMathFlags getFastMathFlags() const
RecurrenceDescriptor(Value *Start, Instruction *Exit, StoreInst *Store, RecurKind K, FastMathFlags FMF, Instruction *ExactFP, Type *RT, bool Signed, bool Ordered, SmallPtrSetImpl< Instruction * > &CI, Type *MinWidthCastToRecurTy, bool PhiHasUsesOutsideReductionChain=false)
static bool isFPMinMaxNumRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating-point minnum/maxnum kind.
static LLVM_ABI bool isFixedOrderRecurrence(PHINode *Phi, Loop *TheLoop, DominatorTree *DT)
Returns true if Phi is a fixed-order recurrence.
bool hasExactFPMath() const
Returns true if the recurrence has floating-point math that requires precise (ordered) operations.
Instruction * getLoopExitInstr() const
static LLVM_ABI InstDesc isConditionalRdxPattern(Instruction *I)
Returns a struct describing if the instruction is a Select(FCmp(X, Y), (Z = X op PHINode),...
static LLVM_ABI bool hasMultipleUsesOf(Instruction *I, SmallPtrSetImpl< Instruction * > &Insts, unsigned MaxNumUses)
Returns true if instruction I has multiple uses in Insts.
static LLVM_ABI bool isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes, DemandedBits *DB=nullptr, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr, ScalarEvolution *SE=nullptr)
Returns true if Phi is a reduction in TheLoop.
Type * getRecurrenceType() const
Returns the type of the recurrence.
bool hasUsesOutsideReductionChain() const
Returns true if the reduction PHI has any uses outside the reduction chain.
const SmallPtrSet< Instruction *, 8 > & getCastInsts() const
Returns a reference to the instructions used for type-promoting the recurrence.
static LLVM_ABI bool areAllUsesIn(Instruction *I, SmallPtrSetImpl< Instruction * > &Set)
Returns true if all uses of the instruction I is within the Set.
static bool isFindLastRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
TrackingVH< Value > getRecurrenceStartValue() const
LLVM_ABI SmallVector< Instruction *, 4 > getReductionOpChain(PHINode *Phi, Loop *L) const
Attempts to find a chain of operations from Phi to LoopExitInst that can be treated as a set of reduc...
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cond,x,y) where one of (x,...
static LLVM_ABI InstDesc isAnyOfPattern(Loop *Loop, PHINode *OrigPhi, Instruction *I, InstDesc &Prev)
Returns a struct describing whether the instruction is a Select(Cond, X, Y) where one of (X,...
static LLVM_ABI bool isSubRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is for a sub operation.
bool isSigned() const
Returns true if all source operands of the recurrence are SExtInsts.
RecurKind getRecurrenceKind() const
bool isOrdered() const
Expose an ordered FP reduction to the instance users.
StoreInst * IntermediateStore
Reductions may store temporary or final result to an invariant address.
static bool isFindRecurrenceKind(RecurKind Kind)
RecurrenceDescriptor(Value *Start, Instruction *Exit, StoreInst *Store, RecurKind K, FastMathFlags FMF, Instruction *ExactFP, Type *RT, bool IsMultiUse=false)
Simpler constructor for min/max recurrences that don't track cast instructions.
static LLVM_ABI bool isFloatingPointRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating point kind.
static LLVM_ABI InstDesc isRecurrenceInstr(Loop *L, PHINode *Phi, Instruction *I, RecurKind Kind, InstDesc &Prev, ScalarEvolution *SE)
Returns a struct describing if the instruction 'I' can be a recurrence variable of type 'Kind' for a ...
static bool isFindIVRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
static LLVM_ABI bool AddReductionVar(PHINode *Phi, RecurKind Kind, Loop *TheLoop, RecurrenceDescriptor &RedDes, DemandedBits *DB=nullptr, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr, ScalarEvolution *SE=nullptr)
Returns true if Phi is a reduction of type Kind and adds it to the RecurrenceDescriptor.
static LLVM_ABI InstDesc isFindPattern(Loop *TheLoop, PHINode *OrigPhi, Instruction *I, ScalarEvolution &SE)
Returns a struct describing whether the instruction is either a Select(ICmp(A, B),...
static LLVM_ABI bool isIntegerRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is an integer kind.
static bool isIntMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is an integer min/max kind.
static bool isMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is any min/max kind.
This class represents an assumption made using SCEV expressions which can be checked at run-time.
This class represents an analyzed expression in the program.
The main scalar evolution driver.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Value handle that tracks a Value across RAUW.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
@ Store
The extracted value is stored (ExtractElement only).
SCEVUseT< const SCEV * > SCEVUse
Op::Description Desc
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
RecurKind
These are the kinds of recurrences that we support.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ FMinimumNum
FP min with llvm.minimumnum semantics.
@ FindIV
FindIV reduction with select(icmp(),x,y) where one of (x,y) is a loop induction variable (increasing ...
@ FMaxSelectNoFMF
FMax as select(cmp()) lacking nnan or nsz, on IEEE types with IEEE denormals.
@ FMinimum
FP min with llvm.minimum semantics.
@ FMaxNum
FP max with llvm.maxnum semantics including NaNs.
@ Mul
Product of integers.
@ FSub
Subtraction of floats.
@ FAddChainWithSubs
A chain of fadds and fsubs.
@ None
Not a recurrence.
@ AnyOf
AnyOf reduction with select(cond,x,y) where one of (x,y) is loop invariant, and both x and y are inte...
@ Xor
Bitwise or logical XOR of integers.
@ FindLast
FindLast reduction with select(cmp(),x,y) where x and y.
@ FMax
FP max as select(cmp()) with nnan/nsz, or llvm.maxnum.
@ FMinSelectNoFMF
FMin as select(cmp()) lacking nnan or nsz, on IEEE types with IEEE denormals.
@ FMaximum
FP max with llvm.maximum semantics.
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ FMul
Product of floats.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ FMin
FP min as select(cmp()) with nnan/nsz, or llvm.minnum.
@ FMinNum
FP min with llvm.minnum semantics including NaNs.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ AddChainWithSubs
A chain of adds and subs.
@ FAdd
Sum of floats.
@ FMaximumNum
FP max with llvm.maximumnum semantics.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
SCEVFlags
SCEVFlags are bitfield indices into SCEV's SubclassData.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Matching combinators.