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 SCEVNoWrapFlags;
34
35/// These are the kinds of recurrences that we support.
36enum class RecurKind {
37 // clang-format off
38 None, ///< Not a recurrence.
39 Add, ///< Sum of integers.
40 Sub, ///< Subtraction of integers
41 AddChainWithSubs, ///< A chain of adds and subs
42 Mul, ///< Product of integers.
43 Or, ///< Bitwise or logical OR of integers.
44 And, ///< Bitwise or logical AND of integers.
45 Xor, ///< Bitwise or logical XOR of integers.
46 SMin, ///< Signed integer min implemented in terms of select(cmp()).
47 SMax, ///< Signed integer max implemented in terms of select(cmp()).
48 UMin, ///< Unsigned integer min implemented in terms of select(cmp()).
49 UMax, ///< Unsigned integer max implemented in terms of select(cmp()).
50 FAdd, ///< Sum of floats.
51 FAddChainWithSubs, ///< A chain of fadds and fsubs.
52 FSub, ///< Subtraction of floats.
53 FMul, ///< Product of floats.
54 FMin, ///< FP min implemented in terms of select(cmp()).
55 FMax, ///< FP max implemented in terms of select(cmp()).
56 FMinNum, ///< FP min with llvm.minnum semantics including NaNs.
57 FMaxNum, ///< FP max with llvm.maxnum semantics including NaNs.
58 FMinimum, ///< FP min with llvm.minimum semantics
59 FMaximum, ///< FP max with llvm.maximum semantics
60 FMinimumNum, ///< FP min with llvm.minimumnum semantics
61 FMaximumNum, ///< FP max with llvm.maximumnum semantics
62 FMulAdd, ///< Sum of float products with llvm.fmuladd(a * b + sum).
63 AnyOf, ///< AnyOf reduction with select(cmp(),x,y) where one of (x,y) is
64 ///< loop invariant, and both x and y are integer type.
65 FindIV, ///< FindIV reduction with select(icmp(),x,y) where one of (x,y) is
66 ///< a loop induction variable (increasing or decreasing), and both
67 ///< x and y are integer type. The signedness and direction are
68 ///< stored separately.
69 FindLast, ///< FindLast reduction with select(cmp(),x,y) where x and y
70 ///< are an integer type, one is the current recurrence value,
71 ///< and the other is an arbitrary value.
72 // clang-format on
73 // TODO: Any_of and FindLast reduction need not be restricted to integer type
74 // only.
75};
76
77/// The RecurrenceDescriptor is used to identify recurrences variables in a
78/// loop. Reduction is a special case of recurrence that has uses of the
79/// recurrence variable outside the loop. The method isReductionPHI identifies
80/// reductions that are basic recurrences.
81///
82/// Basic recurrences are defined as the summation, product, OR, AND, XOR, min,
83/// or max of a set of terms. For example: for(i=0; i<n; i++) { total +=
84/// array[i]; } is a summation of array elements. Basic recurrences are a
85/// special case of chains of recurrences (CR). See ScalarEvolution for CR
86/// references.
87
88/// This struct holds information about recurrence variables.
90public:
92
94 RecurKind K, FastMathFlags FMF, Instruction *ExactFP,
95 Type *RT, bool Signed, bool Ordered,
97 unsigned MinWidthCastToRecurTy,
98 bool PhiHasUsesOutsideReductionChain = false)
99 : IntermediateStore(Store), StartValue(Start), LoopExitInstr(Exit),
100 Kind(K), FMF(FMF), ExactFPMathInst(ExactFP), RecurrenceType(RT),
101 IsSigned(Signed), IsOrdered(Ordered),
102 PhiHasUsesOutsideReductionChain(PhiHasUsesOutsideReductionChain),
103 MinWidthCastToRecurrenceType(MinWidthCastToRecurTy) {
104 CastInsts.insert_range(CI);
105 assert(
106 (!PhiHasUsesOutsideReductionChain || isMinMaxRecurrenceKind(K)) &&
107 "Only min/max recurrences are allowed to have multiple uses currently");
108 }
109
110 /// Simpler constructor for min/max recurrences that don't track cast
111 /// instructions.
113 RecurKind K, FastMathFlags FMF, Instruction *ExactFP,
114 Type *RT, bool IsMultiUse = false)
115 : IntermediateStore(Store), StartValue(Start), LoopExitInstr(Exit),
116 Kind(K), FMF(FMF), ExactFPMathInst(ExactFP), RecurrenceType(RT),
117 PhiHasUsesOutsideReductionChain(IsMultiUse) {}
118
119 /// This POD struct holds information about a potential recurrence operation.
120 class InstDesc {
121 public:
122 InstDesc(bool IsRecur, Instruction *I, Instruction *ExactFP = nullptr)
123 : IsRecurrence(IsRecur), PatternLastInst(I),
124 RecKind(RecurKind::None), ExactFPMathInst(ExactFP) {}
125
127 : IsRecurrence(true), PatternLastInst(I), RecKind(K),
128 ExactFPMathInst(ExactFP) {}
129
130 bool isRecurrence() const { return IsRecurrence; }
131
132 bool needsExactFPMath() const { return ExactFPMathInst != nullptr; }
133
134 Instruction *getExactFPMathInst() const { return ExactFPMathInst; }
135
136 RecurKind getRecKind() const { return RecKind; }
137
138 Instruction *getPatternInst() const { return PatternLastInst; }
139
140 private:
141 // Is this instruction a recurrence candidate.
142 bool IsRecurrence;
143 // The last instruction in a min/max pattern (select of the select(icmp())
144 // pattern), or the current recurrence instruction otherwise.
145 Instruction *PatternLastInst;
146 // If this is a min/max pattern.
147 RecurKind RecKind;
148 // Recurrence does not allow floating-point reassociation.
149 Instruction *ExactFPMathInst;
150 };
151
152 /// Returns a struct describing if the instruction 'I' can be a recurrence
153 /// variable of type 'Kind' for a Loop \p L and reduction PHI \p Phi.
154 /// If the recurrence is a min/max pattern of select(icmp()) this function
155 /// advances the instruction pointer 'I' from the compare instruction to the
156 /// select instruction and stores this pointer in 'PatternLastInst' member of
157 /// the returned struct.
158 LLVM_ABI static InstDesc isRecurrenceInstr(Loop *L, PHINode *Phi,
159 Instruction *I, RecurKind Kind,
160 InstDesc &Prev,
161 ScalarEvolution *SE);
162
163 /// Returns true if instruction I has multiple uses in Insts
166 unsigned MaxNumUses);
167
168 /// Returns true if all uses of the instruction I is within the Set.
169 LLVM_ABI static bool areAllUsesIn(Instruction *I,
171
172 /// Returns a struct describing whether the instruction is either a
173 /// Select(ICmp(A, B), X, Y), or
174 /// Select(FCmp(A, B), X, Y)
175 /// where one of (X, Y) is a loop invariant integer and the other is a PHI
176 /// value. \p Prev specifies the description of an already processed select
177 /// instruction, so its corresponding cmp can be matched to it.
178 LLVM_ABI static InstDesc isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
179 Instruction *I, InstDesc &Prev);
180
181 /// Returns a struct describing whether the instruction is either a
182 /// Select(ICmp(A, B), X, Y), or
183 /// Select(FCmp(A, B), X, Y)
184 /// where one of (X, Y) is an increasing (FindLastIV) or decreasing
185 /// (FindFirstIV) loop induction variable, or an arbitrary integer value
186 /// (FindLast), and the other is a PHI value.
187 LLVM_ABI static InstDesc isFindPattern(Loop *TheLoop, PHINode *OrigPhi,
189
190 /// Returns a struct describing if the instruction is a
191 /// Select(FCmp(X, Y), (Z = X op PHINode), PHINode) instruction pattern.
193
194 /// Returns the opcode corresponding to the RecurrenceKind.
195 LLVM_ABI static unsigned getOpcode(RecurKind Kind);
196
197 /// Returns true if Phi is a reduction of type Kind and adds it to the
198 /// RecurrenceDescriptor. If either \p DB is non-null or \p AC and \p DT are
199 /// non-null, the minimal bit width needed to compute the reduction will be
200 /// computed.
201 LLVM_ABI static bool
202 AddReductionVar(PHINode *Phi, RecurKind Kind, Loop *TheLoop,
203 RecurrenceDescriptor &RedDes, DemandedBits *DB = nullptr,
204 AssumptionCache *AC = nullptr, DominatorTree *DT = nullptr,
205 ScalarEvolution *SE = nullptr);
206
207 /// Returns true if Phi is a reduction in TheLoop. The RecurrenceDescriptor
208 /// is returned in RedDes. If either \p DB is non-null or \p AC and \p DT are
209 /// non-null, the minimal bit width needed to compute the reduction will be
210 /// computed. If \p SE is non-null, store instructions to loop invariant
211 /// addresses are processed.
212 LLVM_ABI static bool
213 isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes,
214 DemandedBits *DB = nullptr, AssumptionCache *AC = nullptr,
215 DominatorTree *DT = nullptr, ScalarEvolution *SE = nullptr);
216
217 /// Returns true if Phi is a fixed-order recurrence. A fixed-order recurrence
218 /// is a non-reduction recurrence relation in which the value of the
219 /// recurrence in the current loop iteration equals a value defined in a
220 /// previous iteration (e.g. if the value is defined in the previous
221 /// iteration, we refer to it as first-order recurrence, if it is defined in
222 /// the iteration before the previous, we refer to it as second-order
223 /// recurrence and so on). Note that this function optimistically assumes that
224 /// uses of the recurrence can be re-ordered if necessary and users need to
225 /// check and perform the re-ordering.
226 LLVM_ABI static bool isFixedOrderRecurrence(PHINode *Phi, Loop *TheLoop,
227 DominatorTree *DT);
228
229 RecurKind getRecurrenceKind() const { return Kind; }
230
231 unsigned getOpcode() const { return getOpcode(getRecurrenceKind()); }
232
233 FastMathFlags getFastMathFlags() const { return FMF; }
234
235 TrackingVH<Value> getRecurrenceStartValue() const { return StartValue; }
236
237 Instruction *getLoopExitInstr() const { return LoopExitInstr; }
238
239 /// Returns true if the recurrence has floating-point math that requires
240 /// precise (ordered) operations.
241 bool hasExactFPMath() const { return ExactFPMathInst != nullptr; }
242
243 /// Returns 1st non-reassociative FP instruction in the PHI node's use-chain.
244 Instruction *getExactFPMathInst() const { return ExactFPMathInst; }
245
246 /// Returns true if the recurrence kind is an integer kind.
248
249 /// Returns true if the recurrence kind is a floating point kind.
251
252 /// Returns true if the recurrence kind is for a sub operation.
253 LLVM_ABI static bool isSubRecurrenceKind(RecurKind Kind);
254
255 /// Returns true if the recurrence kind is an integer min/max kind.
257 return Kind == RecurKind::UMin || Kind == RecurKind::UMax ||
258 Kind == RecurKind::SMin || Kind == RecurKind::SMax;
259 }
260
261 /// Returns true if the recurrence kind is a floating-point minnum/maxnum
262 /// kind.
264 return Kind == RecurKind::FMinNum || Kind == RecurKind::FMaxNum;
265 }
266
267 /// Returns true if the recurrence kind is a floating-point min/max kind.
269 return Kind == RecurKind::FMin || Kind == RecurKind::FMax ||
270 Kind == RecurKind::FMinimum || Kind == RecurKind::FMaximum ||
273 }
274
275 /// Returns true if the recurrence kind is any min/max kind.
278 }
279
280 /// Returns true if the recurrence kind is of the form
281 /// select(cmp(),x,y) where one of (x,y) is loop invariant.
283 return Kind == RecurKind::AnyOf;
284 }
285
286 /// Returns true if the recurrence kind is of the form
287 /// select(cmp(),x,y) where one of (x,y) is a loop induction variable.
289 return Kind == RecurKind::FindIV;
290 }
291
292 /// Returns true if the recurrence kind is of the form
293 /// select(cmp(),x,y) where one of (x,y) is an arbitrary value and the
294 /// other is a recurrence.
296 return Kind == RecurKind::FindLast;
297 }
298
299 static bool isFindRecurrenceKind(RecurKind Kind) {
301 }
302
303 /// Returns the type of the recurrence. This type can be narrower than the
304 /// actual type of the Phi if the recurrence has been type-promoted.
305 Type *getRecurrenceType() const { return RecurrenceType; }
306
307 /// Returns a reference to the instructions used for type-promoting the
308 /// recurrence.
309 const SmallPtrSet<Instruction *, 8> &getCastInsts() const { return CastInsts; }
310
311 /// Returns the minimum width used by the recurrence in bits.
313 return MinWidthCastToRecurrenceType;
314 }
315
316 /// Returns true if all source operands of the recurrence are SExtInsts.
317 bool isSigned() const { return IsSigned; }
318
319 /// Expose an ordered FP reduction to the instance users.
320 bool isOrdered() const { return IsOrdered; }
321
322 /// Returns true if the reduction PHI has any uses outside the reduction
323 /// chain. This is relevant for min/max reductions that are part of a FindIV
324 /// pattern.
326 return PhiHasUsesOutsideReductionChain;
327 }
328
329 /// Attempts to find a chain of operations from Phi to LoopExitInst that can
330 /// be treated as a set of reductions instructions for in-loop reductions.
332 Loop *L) const;
333
334 /// Returns true if the instruction is a call to the llvm.fmuladd intrinsic.
336 return isa<IntrinsicInst>(I) &&
337 cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fmuladd;
338 }
339
340 /// Reductions may store temporary or final result to an invariant address.
341 /// If there is such a store in the loop then, after successfull run of
342 /// AddReductionVar method, this field will be assigned the last met store.
344
345private:
346 // The starting value of the recurrence.
347 // It does not have to be zero!
348 TrackingVH<Value> StartValue;
349 // The instruction who's value is used outside the loop.
350 Instruction *LoopExitInstr = nullptr;
351 // The kind of the recurrence.
353 // The fast-math flags on the recurrent instructions. We propagate these
354 // fast-math flags into the vectorized FP instructions we generate.
355 FastMathFlags FMF;
356 // First instance of non-reassociative floating-point in the PHI's use-chain.
357 Instruction *ExactFPMathInst = nullptr;
358 // The type of the recurrence.
359 Type *RecurrenceType = nullptr;
360 // True if all source operands of the recurrence are SExtInsts.
361 bool IsSigned = false;
362 // True if this recurrence can be treated as an in-order reduction.
363 // Currently only a non-reassociative FAdd can be considered in-order,
364 // if it is also the only FAdd in the PHI's use chain.
365 bool IsOrdered = false;
366 // True if the reduction PHI has in-loop users outside the reduction chain.
367 // This is relevant for min/max reductions that are part of a FindIV pattern.
368 bool PhiHasUsesOutsideReductionChain = false;
369 // Instructions used for type-promoting the recurrence.
371 // The minimum width used by the recurrence.
372 unsigned MinWidthCastToRecurrenceType;
373};
374
375/// A struct for saving information about induction variables.
377public:
378 /// This enum represents the kinds of inductions that we support.
380 IK_NoInduction, ///< Not an induction variable.
381 IK_IntInduction, ///< Integer induction variable. Step = C.
382 IK_PtrInduction, ///< Pointer induction var. Step = C.
383 IK_FpInduction ///< Floating point induction variable.
384 };
385
386public:
387 /// Default constructor - creates an invalid induction.
389
390 /// Returns the canonical integer induction for type \p Ty with start = 0
391 /// and step = 1.
394
395 Value *getStartValue() const { return StartValue; }
396 InductionKind getKind() const { return IK; }
397 const SCEV *getStep() const { return Step; }
398 BinaryOperator *getInductionBinOp() const { return InductionBinOp; }
400
401 /// Returns true if \p Phi is an induction in the loop \p L. If \p Phi is an
402 /// induction, the induction descriptor \p D will contain the data describing
403 /// this induction. Since Induction Phis can only be present inside loop
404 /// headers, the function will assert if it is passed a Phi whose parent is
405 /// not the loop header. If by some other means the caller has a better SCEV
406 /// expression for \p Phi than the one returned by the ScalarEvolution
407 /// analysis, it can be passed through \p Expr. If the def-use chain
408 /// associated with the phi includes casts (that we know we can ignore
409 /// under proper runtime checks), they are passed through \p CastsToIgnore.
410 /// SCEV predicates checking potential overflow for \p Phi to be an induction,
411 /// if any, are passed via \p NoWrapPreds and recorded.
412 LLVM_ABI static bool
413 isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE,
415 ArrayRef<const SCEVPredicate *> NoWrapPreds = {},
416 const SCEV *Expr = nullptr,
417 SmallVectorImpl<Instruction *> *CastsToIgnore = nullptr);
418
419 /// Returns true if \p Phi is a floating point induction in the loop \p L.
420 /// If \p Phi is an induction, the induction descriptor \p D will contain
421 /// the data describing this induction.
422 LLVM_ABI static bool isFPInductionPHI(PHINode *Phi, const Loop *L,
423 ScalarEvolution *SE,
425
426 /// Returns true if \p Phi is a loop \p L induction, in the context associated
427 /// with the run-time predicate of PSE. If \p Assume is true, this can add
428 /// further SCEV predicates to \p PSE in order to prove that \p Phi is an
429 /// induction.
430 /// If \p Phi is an induction, \p D will contain the data describing this
431 /// induction.
432 LLVM_ABI static bool isInductionPHI(PHINode *Phi, const Loop *L,
433 PredicatedScalarEvolution &PSE,
435 bool Assume = false);
436
437 /// Returns floating-point induction operator that does not allow
438 /// reassociation (transforming the induction requires an override of normal
439 /// floating-point rules).
441 if (IK == IK_FpInduction && InductionBinOp &&
442 !InductionBinOp->hasAllowReassoc())
443 return InductionBinOp;
444 return nullptr;
445 }
446
447 /// Returns binary opcode of the induction operator.
449 return InductionBinOp ? InductionBinOp->getOpcode()
450 : Instruction::BinaryOpsEnd;
451 }
452
453 /// Returns an ArrayRef to the type cast instructions in the induction
454 /// update chain, that are redundant when guarded with a runtime
455 /// SCEV overflow check.
456 ArrayRef<Instruction *> getCastInsts() const { return RedundantCasts; }
457
458 /// Returns the SCEV predicates associated with this induction.
460 return NoWrapPredicates;
461 }
462
463private:
464 /// Private constructor - used by \c isInductionPHI and
465 /// \c getCanonicalIntInduction.
466 InductionDescriptor(Value *Start, InductionKind K, const SCEV *Step,
467 BinaryOperator *InductionBinOp = nullptr,
468 SmallVectorImpl<Instruction *> *Casts = nullptr,
469 ArrayRef<const SCEVPredicate *> NoWrapPreds = {});
470
471 /// Start value.
472 TrackingVH<Value> StartValue;
473 /// Induction kind.
474 InductionKind IK = IK_NoInduction;
475 /// Step value.
476 const SCEV *Step = nullptr;
477 // Instruction that advances induction variable.
478 BinaryOperator *InductionBinOp = nullptr;
479 // Instructions used for type-casts of the induction variable,
480 // that are redundant when guarded with a runtime SCEV overflow check.
481 SmallVector<Instruction *, 2> RedundantCasts;
482 // SCEV predicates checking overflow needed for this induction.
484};
485
486/// A struct for saving information about monotonic variables.
487/// Monotonic variable can be considered as a "conditional" induction variable:
488/// its update happens only on loop iterations for which a certain predicate is
489/// satisfied. The step of the monotonic variable must be loop-invariant.
491public:
493
494 /// Returns true if \p PN is a monotonic variable in the loop \p L. If \p PN
495 /// is monotonic, the monotonic descriptor \p Desc will contain the data
496 /// describing the PHI.
497 LLVM_ABI static bool isMonotonicPHI(PHINode *PN, const Loop *L,
499 ScalarEvolution &SE);
500
501 /// Returns the header PHI described by this descriptor.
502 PHINode *getHeaderPHI() const { return HeaderPHI; }
503
504 /// Returns the backedge PHI that selects between StepInst and the HeaderPHI.
505 PHINode *getBackedgePHI() const { return BackedgePHI; }
506
507 /// Returns the instruction that updates the value of the monotonic PHI.
508 Instruction *getStepInst() const { return StepInst; }
509
510 /// Returns a SCEV expression for the initial value of the monotonic PHI.
511 const SCEV *getStartSCEV() const { return StartSCEV; }
512
513 /// Returns a SCEV expression for the step of the monotonic PHI. This is
514 /// the value the monotonic PHI increments by on loop iterations where the
515 /// predicate is satisfied.
516 const SCEV *getStepSCEV() const { return StepSCEV; }
517
518 /// Returns the SCEV no-wrap flags that apply to StepInst.
519 SCEVNoWrapFlags getSCEVNoWrapFlags() const { return NoWrapFlags; }
520
521private:
522 MonotonicDescriptor(PHINode *HeaderPHI, PHINode *BackedgePHI,
523 Instruction *StepInst, const SCEV *StartSCEV,
524 const SCEV *StepSCEV, SCEVNoWrapFlags NoWrapFlags)
525 : HeaderPHI(HeaderPHI), BackedgePHI(BackedgePHI), StepInst(StepInst),
526 StartSCEV(StartSCEV), StepSCEV(StepSCEV), NoWrapFlags(NoWrapFlags) {}
527
528 /// The header PHI (this is the PHI described by the descriptor).
529 PHINode *HeaderPHI = nullptr;
530
531 /// The backedge PHI that selects between StepInst and the HeaderPHI.
532 PHINode *BackedgePHI = nullptr;
533
534 /// The instruction that updates the value of the monotonic PHI.
535 Instruction *StepInst = nullptr;
536
537 /// SCEV expression representing the start value for the monotonic PHI.
538 const SCEV *StartSCEV = nullptr;
539
540 /// SCEV expression representing the step value for the monotonic PHI.
541 const SCEV *StepSCEV = nullptr;
542
543 /// The SCEV no-wrap flags that apply to StepInst.
544 SCEVNoWrapFlags NoWrapFlags{};
545};
546
547} // end namespace llvm
548
549#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:215
#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.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
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.
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
Instruction * getExactFPMathInst()
Returns floating-point induction operator that does not allow reassociation (transforming the inducti...
InductionDescriptor()=default
Default constructor - creates an invalid induction.
LLVM_ABI ConstantInt * getConstIntStepValue() const
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Instruction * getStepInst() const
Returns the instruction that updates the value of the monotonic PHI.
SCEVNoWrapFlags getSCEVNoWrapFlags() const
Returns the SCEV no-wrap flags that apply to StepInst.
const SCEV * getStartSCEV() const
Returns a SCEV expression for the initial value of the monotonic PHI.
PHINode * getHeaderPHI() const
Returns the header PHI described by this descriptor.
const SCEV * getStepSCEV() const
Returns a SCEV expression for the step of the monotonic PHI.
PHINode * getBackedgePHI() const
Returns the backedge PHI that selects between StepInst and the HeaderPHI.
static LLVM_ABI bool isMonotonicPHI(PHINode *PN, const Loop *L, MonotonicDescriptor &Desc, ScalarEvolution &SE)
Returns true if PN is a monotonic variable in the loop L.
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.
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.
FastMathFlags getFastMathFlags() const
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,...
unsigned getMinWidthCastToRecurrenceTypeInBits() const
Returns the minimum width used by the recurrence in bits.
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(cmp(),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 either a Select(ICmp(A, B),...
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.
RecurrenceDescriptor(Value *Start, Instruction *Exit, StoreInst *Store, RecurKind K, FastMathFlags FMF, Instruction *ExactFP, Type *RT, bool Signed, bool Ordered, SmallPtrSetImpl< Instruction * > &CI, unsigned MinWidthCastToRecurTy, bool PhiHasUsesOutsideReductionChain=false)
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).
Op::Description Desc
SCEVNoWrapFlags
NoWrapFlags are bitfield indices into SCEV's SubclassData.
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 ...
@ 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(cmp(),x,y) where one of (x,y) is loop invariant, and both x and y are int...
@ Xor
Bitwise or logical XOR of integers.
@ FindLast
FindLast reduction with select(cmp(),x,y) where x and y.
@ FMax
FP max implemented in terms of select(cmp()).
@ 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 implemented in terms of select(cmp()).
@ 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()).
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Matching combinators.