Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 1 | //===- llvm/Analysis/IVDescriptors.h - IndVar Descriptors -------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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 | |
| 16 | #include "llvm/ADT/DenseMap.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 20 | #include "llvm/IR/InstrTypes.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 21 | #include "llvm/IR/Instruction.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Operator.h" |
| 23 | #include "llvm/IR/ValueHandle.h" |
| 24 | #include "llvm/Support/Casting.h" |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 28 | class DemandedBits; |
| 29 | class AssumptionCache; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 30 | class Loop; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 31 | class PredicatedScalarEvolution; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 32 | class ScalarEvolution; |
| 33 | class SCEV; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 34 | class DominatorTree; |
| 35 | class ICFLoopSafetyInfo; |
| 36 | |
| 37 | /// These are the kinds of recurrences that we support. |
| 38 | enum class RecurKind { |
| 39 | None, ///< Not a recurrence. |
| 40 | Add, ///< Sum of integers. |
| 41 | Mul, ///< Product of integers. |
| 42 | Or, ///< Bitwise or logical OR of integers. |
| 43 | And, ///< Bitwise or logical AND of integers. |
| 44 | Xor, ///< Bitwise or logical XOR of integers. |
| 45 | SMin, ///< Signed integer min implemented in terms of select(cmp()). |
| 46 | SMax, ///< Signed integer max implemented in terms of select(cmp()). |
| 47 | UMin, ///< Unisgned integer min implemented in terms of select(cmp()). |
| 48 | UMax, ///< Unsigned integer max implemented in terms of select(cmp()). |
| 49 | FAdd, ///< Sum of floats. |
| 50 | FMul, ///< Product of floats. |
| 51 | FMin, ///< FP min implemented in terms of select(cmp()). |
| 52 | FMax ///< FP max implemented in terms of select(cmp()). |
| 53 | }; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 54 | |
| 55 | /// The RecurrenceDescriptor is used to identify recurrences variables in a |
| 56 | /// loop. Reduction is a special case of recurrence that has uses of the |
| 57 | /// recurrence variable outside the loop. The method isReductionPHI identifies |
| 58 | /// reductions that are basic recurrences. |
| 59 | /// |
| 60 | /// Basic recurrences are defined as the summation, product, OR, AND, XOR, min, |
| 61 | /// or max of a set of terms. For example: for(i=0; i<n; i++) { total += |
| 62 | /// array[i]; } is a summation of array elements. Basic recurrences are a |
| 63 | /// special case of chains of recurrences (CR). See ScalarEvolution for CR |
| 64 | /// references. |
| 65 | |
| 66 | /// This struct holds information about recurrence variables. |
| 67 | class RecurrenceDescriptor { |
| 68 | public: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 69 | RecurrenceDescriptor() = default; |
| 70 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 71 | RecurrenceDescriptor(Value *Start, Instruction *Exit, RecurKind K, |
| 72 | FastMathFlags FMF, Instruction *UAI, Type *RT, |
| 73 | bool Signed, SmallPtrSetImpl<Instruction *> &CI) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 74 | : StartValue(Start), LoopExitInstr(Exit), Kind(K), FMF(FMF), |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 75 | UnsafeAlgebraInst(UAI), RecurrenceType(RT), IsSigned(Signed) { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 76 | CastInsts.insert(CI.begin(), CI.end()); |
| 77 | } |
| 78 | |
| 79 | /// This POD struct holds information about a potential recurrence operation. |
| 80 | class InstDesc { |
| 81 | public: |
| 82 | InstDesc(bool IsRecur, Instruction *I, Instruction *UAI = nullptr) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 83 | : IsRecurrence(IsRecur), PatternLastInst(I), |
| 84 | RecKind(RecurKind::None), UnsafeAlgebraInst(UAI) {} |
| 85 | |
| 86 | InstDesc(Instruction *I, RecurKind K, Instruction *UAI = nullptr) |
| 87 | : IsRecurrence(true), PatternLastInst(I), RecKind(K), |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 88 | UnsafeAlgebraInst(UAI) {} |
| 89 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 90 | bool isRecurrence() const { return IsRecurrence; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 91 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 92 | bool hasUnsafeAlgebra() const { return UnsafeAlgebraInst != nullptr; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 93 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 94 | Instruction *getUnsafeAlgebraInst() const { return UnsafeAlgebraInst; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 95 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 96 | RecurKind getRecKind() const { return RecKind; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 97 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 98 | Instruction *getPatternInst() const { return PatternLastInst; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | // Is this instruction a recurrence candidate. |
| 102 | bool IsRecurrence; |
| 103 | // The last instruction in a min/max pattern (select of the select(icmp()) |
| 104 | // pattern), or the current recurrence instruction otherwise. |
| 105 | Instruction *PatternLastInst; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 106 | // If this is a min/max pattern. |
| 107 | RecurKind RecKind; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 108 | // Recurrence has unsafe algebra. |
| 109 | Instruction *UnsafeAlgebraInst; |
| 110 | }; |
| 111 | |
| 112 | /// Returns a struct describing if the instruction 'I' can be a recurrence |
| 113 | /// variable of type 'Kind'. If the recurrence is a min/max pattern of |
| 114 | /// select(icmp()) this function advances the instruction pointer 'I' from the |
| 115 | /// compare instruction to the select instruction and stores this pointer in |
| 116 | /// 'PatternLastInst' member of the returned struct. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 117 | static InstDesc isRecurrenceInstr(Instruction *I, RecurKind Kind, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 118 | InstDesc &Prev, bool HasFunNoNaNAttr); |
| 119 | |
| 120 | /// Returns true if instruction I has multiple uses in Insts |
| 121 | static bool hasMultipleUsesOf(Instruction *I, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 122 | SmallPtrSetImpl<Instruction *> &Insts, |
| 123 | unsigned MaxNumUses); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 124 | |
| 125 | /// Returns true if all uses of the instruction I is within the Set. |
| 126 | static bool areAllUsesIn(Instruction *I, SmallPtrSetImpl<Instruction *> &Set); |
| 127 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 128 | /// Returns a struct describing if the instruction is a |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 129 | /// Select(ICmp(X, Y), X, Y) instruction pattern corresponding to a min(X, Y) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 130 | /// or max(X, Y). \p Prev specifies the description of an already processed |
| 131 | /// select instruction, so its corresponding cmp can be matched to it. |
| 132 | static InstDesc isMinMaxSelectCmpPattern(Instruction *I, |
| 133 | const InstDesc &Prev); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 134 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 135 | /// Returns a struct describing if the instruction is a |
| 136 | /// Select(FCmp(X, Y), (Z = X op PHINode), PHINode) instruction pattern. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 137 | static InstDesc isConditionalRdxPattern(RecurKind Kind, Instruction *I); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 138 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 139 | /// Returns identity corresponding to the RecurrenceKind. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 140 | static Constant *getRecurrenceIdentity(RecurKind K, Type *Tp); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 141 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 142 | /// Returns the opcode corresponding to the RecurrenceKind. |
| 143 | static unsigned getOpcode(RecurKind Kind); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 144 | |
| 145 | /// Returns true if Phi is a reduction of type Kind and adds it to the |
| 146 | /// RecurrenceDescriptor. If either \p DB is non-null or \p AC and \p DT are |
| 147 | /// non-null, the minimal bit width needed to compute the reduction will be |
| 148 | /// computed. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 149 | static bool AddReductionVar(PHINode *Phi, RecurKind Kind, Loop *TheLoop, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 150 | bool HasFunNoNaNAttr, |
| 151 | RecurrenceDescriptor &RedDes, |
| 152 | DemandedBits *DB = nullptr, |
| 153 | AssumptionCache *AC = nullptr, |
| 154 | DominatorTree *DT = nullptr); |
| 155 | |
| 156 | /// Returns true if Phi is a reduction in TheLoop. The RecurrenceDescriptor |
| 157 | /// is returned in RedDes. If either \p DB is non-null or \p AC and \p DT are |
| 158 | /// non-null, the minimal bit width needed to compute the reduction will be |
| 159 | /// computed. |
| 160 | static bool isReductionPHI(PHINode *Phi, Loop *TheLoop, |
| 161 | RecurrenceDescriptor &RedDes, |
| 162 | DemandedBits *DB = nullptr, |
| 163 | AssumptionCache *AC = nullptr, |
| 164 | DominatorTree *DT = nullptr); |
| 165 | |
| 166 | /// Returns true if Phi is a first-order recurrence. A first-order recurrence |
| 167 | /// is a non-reduction recurrence relation in which the value of the |
| 168 | /// recurrence in the current loop iteration equals a value defined in the |
| 169 | /// previous iteration. \p SinkAfter includes pairs of instructions where the |
| 170 | /// first will be rescheduled to appear after the second if/when the loop is |
| 171 | /// vectorized. It may be augmented with additional pairs if needed in order |
| 172 | /// to handle Phi as a first-order recurrence. |
| 173 | static bool |
| 174 | isFirstOrderRecurrence(PHINode *Phi, Loop *TheLoop, |
| 175 | DenseMap<Instruction *, Instruction *> &SinkAfter, |
| 176 | DominatorTree *DT); |
| 177 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 178 | RecurKind getRecurrenceKind() const { return Kind; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 179 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 180 | unsigned getOpcode() const { return getOpcode(getRecurrenceKind()); } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 181 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 182 | FastMathFlags getFastMathFlags() const { return FMF; } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 183 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 184 | TrackingVH<Value> getRecurrenceStartValue() const { return StartValue; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 185 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 186 | Instruction *getLoopExitInstr() const { return LoopExitInstr; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 187 | |
| 188 | /// Returns true if the recurrence has unsafe algebra which requires a relaxed |
| 189 | /// floating-point model. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 190 | bool hasUnsafeAlgebra() const { return UnsafeAlgebraInst != nullptr; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 191 | |
| 192 | /// Returns first unsafe algebra instruction in the PHI node's use-chain. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 193 | Instruction *getUnsafeAlgebraInst() const { return UnsafeAlgebraInst; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 194 | |
| 195 | /// Returns true if the recurrence kind is an integer kind. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 196 | static bool isIntegerRecurrenceKind(RecurKind Kind); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 197 | |
| 198 | /// Returns true if the recurrence kind is a floating point kind. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 199 | static bool isFloatingPointRecurrenceKind(RecurKind Kind); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 200 | |
| 201 | /// Returns true if the recurrence kind is an arithmetic kind. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 202 | static bool isArithmeticRecurrenceKind(RecurKind Kind); |
| 203 | |
| 204 | /// Returns true if the recurrence kind is an integer min/max kind. |
| 205 | static bool isIntMinMaxRecurrenceKind(RecurKind Kind) { |
| 206 | return Kind == RecurKind::UMin || Kind == RecurKind::UMax || |
| 207 | Kind == RecurKind::SMin || Kind == RecurKind::SMax; |
| 208 | } |
| 209 | |
| 210 | /// Returns true if the recurrence kind is a floating-point min/max kind. |
| 211 | static bool isFPMinMaxRecurrenceKind(RecurKind Kind) { |
| 212 | return Kind == RecurKind::FMin || Kind == RecurKind::FMax; |
| 213 | } |
| 214 | |
| 215 | /// Returns true if the recurrence kind is any min/max kind. |
| 216 | static bool isMinMaxRecurrenceKind(RecurKind Kind) { |
| 217 | return isIntMinMaxRecurrenceKind(Kind) || isFPMinMaxRecurrenceKind(Kind); |
| 218 | } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 219 | |
| 220 | /// Returns the type of the recurrence. This type can be narrower than the |
| 221 | /// actual type of the Phi if the recurrence has been type-promoted. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 222 | Type *getRecurrenceType() const { return RecurrenceType; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 223 | |
| 224 | /// Returns a reference to the instructions used for type-promoting the |
| 225 | /// recurrence. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 226 | const SmallPtrSet<Instruction *, 8> &getCastInsts() const { return CastInsts; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 227 | |
| 228 | /// Returns true if all source operands of the recurrence are SExtInsts. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 229 | bool isSigned() const { return IsSigned; } |
| 230 | |
| 231 | /// Attempts to find a chain of operations from Phi to LoopExitInst that can |
| 232 | /// be treated as a set of reductions instructions for in-loop reductions. |
| 233 | SmallVector<Instruction *, 4> getReductionOpChain(PHINode *Phi, |
| 234 | Loop *L) const; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 235 | |
| 236 | private: |
| 237 | // The starting value of the recurrence. |
| 238 | // It does not have to be zero! |
| 239 | TrackingVH<Value> StartValue; |
| 240 | // The instruction who's value is used outside the loop. |
| 241 | Instruction *LoopExitInstr = nullptr; |
| 242 | // The kind of the recurrence. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 243 | RecurKind Kind = RecurKind::None; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 244 | // The fast-math flags on the recurrent instructions. We propagate these |
| 245 | // fast-math flags into the vectorized FP instructions we generate. |
| 246 | FastMathFlags FMF; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 247 | // First occurrence of unasfe algebra in the PHI's use-chain. |
| 248 | Instruction *UnsafeAlgebraInst = nullptr; |
| 249 | // The type of the recurrence. |
| 250 | Type *RecurrenceType = nullptr; |
| 251 | // True if all source operands of the recurrence are SExtInsts. |
| 252 | bool IsSigned = false; |
| 253 | // Instructions used for type-promoting the recurrence. |
| 254 | SmallPtrSet<Instruction *, 8> CastInsts; |
| 255 | }; |
| 256 | |
| 257 | /// A struct for saving information about induction variables. |
| 258 | class InductionDescriptor { |
| 259 | public: |
| 260 | /// This enum represents the kinds of inductions that we support. |
| 261 | enum InductionKind { |
| 262 | IK_NoInduction, ///< Not an induction variable. |
| 263 | IK_IntInduction, ///< Integer induction variable. Step = C. |
| 264 | IK_PtrInduction, ///< Pointer induction var. Step = C / sizeof(elem). |
| 265 | IK_FpInduction ///< Floating point induction variable. |
| 266 | }; |
| 267 | |
| 268 | public: |
| 269 | /// Default constructor - creates an invalid induction. |
| 270 | InductionDescriptor() = default; |
| 271 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 272 | Value *getStartValue() const { return StartValue; } |
| 273 | InductionKind getKind() const { return IK; } |
| 274 | const SCEV *getStep() const { return Step; } |
| 275 | BinaryOperator *getInductionBinOp() const { return InductionBinOp; } |
| 276 | ConstantInt *getConstIntStepValue() const; |
| 277 | |
| 278 | /// Returns true if \p Phi is an induction in the loop \p L. If \p Phi is an |
| 279 | /// induction, the induction descriptor \p D will contain the data describing |
| 280 | /// this induction. If by some other means the caller has a better SCEV |
| 281 | /// expression for \p Phi than the one returned by the ScalarEvolution |
| 282 | /// analysis, it can be passed through \p Expr. If the def-use chain |
| 283 | /// associated with the phi includes casts (that we know we can ignore |
| 284 | /// under proper runtime checks), they are passed through \p CastsToIgnore. |
| 285 | static bool |
| 286 | isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, |
| 287 | InductionDescriptor &D, const SCEV *Expr = nullptr, |
| 288 | SmallVectorImpl<Instruction *> *CastsToIgnore = nullptr); |
| 289 | |
| 290 | /// Returns true if \p Phi is a floating point induction in the loop \p L. |
| 291 | /// If \p Phi is an induction, the induction descriptor \p D will contain |
| 292 | /// the data describing this induction. |
| 293 | static bool isFPInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, |
| 294 | InductionDescriptor &D); |
| 295 | |
| 296 | /// Returns true if \p Phi is a loop \p L induction, in the context associated |
| 297 | /// with the run-time predicate of PSE. If \p Assume is true, this can add |
| 298 | /// further SCEV predicates to \p PSE in order to prove that \p Phi is an |
| 299 | /// induction. |
| 300 | /// If \p Phi is an induction, \p D will contain the data describing this |
| 301 | /// induction. |
| 302 | static bool isInductionPHI(PHINode *Phi, const Loop *L, |
| 303 | PredicatedScalarEvolution &PSE, |
| 304 | InductionDescriptor &D, bool Assume = false); |
| 305 | |
| 306 | /// Returns true if the induction type is FP and the binary operator does |
| 307 | /// not have the "fast-math" property. Such operation requires a relaxed FP |
| 308 | /// mode. |
| 309 | bool hasUnsafeAlgebra() { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 310 | return (IK == IK_FpInduction) && InductionBinOp && |
| 311 | !cast<FPMathOperator>(InductionBinOp)->isFast(); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /// Returns induction operator that does not have "fast-math" property |
| 315 | /// and requires FP unsafe mode. |
| 316 | Instruction *getUnsafeAlgebraInst() { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 317 | if (IK != IK_FpInduction) |
| 318 | return nullptr; |
| 319 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 320 | if (!InductionBinOp || cast<FPMathOperator>(InductionBinOp)->isFast()) |
| 321 | return nullptr; |
| 322 | return InductionBinOp; |
| 323 | } |
| 324 | |
| 325 | /// Returns binary opcode of the induction operator. |
| 326 | Instruction::BinaryOps getInductionOpcode() const { |
| 327 | return InductionBinOp ? InductionBinOp->getOpcode() |
| 328 | : Instruction::BinaryOpsEnd; |
| 329 | } |
| 330 | |
| 331 | /// Returns a reference to the type cast instructions in the induction |
| 332 | /// update chain, that are redundant when guarded with a runtime |
| 333 | /// SCEV overflow check. |
| 334 | const SmallVectorImpl<Instruction *> &getCastInsts() const { |
| 335 | return RedundantCasts; |
| 336 | } |
| 337 | |
| 338 | private: |
| 339 | /// Private constructor - used by \c isInductionPHI. |
| 340 | InductionDescriptor(Value *Start, InductionKind K, const SCEV *Step, |
| 341 | BinaryOperator *InductionBinOp = nullptr, |
| 342 | SmallVectorImpl<Instruction *> *Casts = nullptr); |
| 343 | |
| 344 | /// Start value. |
| 345 | TrackingVH<Value> StartValue; |
| 346 | /// Induction kind. |
| 347 | InductionKind IK = IK_NoInduction; |
| 348 | /// Step value. |
| 349 | const SCEV *Step = nullptr; |
| 350 | // Instruction that advances induction variable. |
| 351 | BinaryOperator *InductionBinOp = nullptr; |
| 352 | // Instructions used for type-casts of the induction variable, |
| 353 | // that are redundant when guarded with a runtime SCEV overflow check. |
| 354 | SmallVector<Instruction *, 2> RedundantCasts; |
| 355 | }; |
| 356 | |
| 357 | } // end namespace llvm |
| 358 | |
| 359 | #endif // LLVM_ANALYSIS_IVDESCRIPTORS_H |