1 //===- llvm/Analysis/IVDescriptors.cpp - IndVar Descriptors -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file "describes" induction and recurrence variables.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/IVDescriptors.h"
14 #include "llvm/Analysis/DemandedBits.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/ScalarEvolution.h"
17 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/PatternMatch.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/KnownBits.h"
28 using namespace llvm::PatternMatch
;
30 #define DEBUG_TYPE "iv-descriptors"
32 bool RecurrenceDescriptor::areAllUsesIn(Instruction
*I
,
33 SmallPtrSetImpl
<Instruction
*> &Set
) {
34 for (const Use
&Use
: I
->operands())
35 if (!Set
.count(dyn_cast
<Instruction
>(Use
)))
40 bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind
) {
53 case RecurKind::IAnyOf
:
54 case RecurKind::FAnyOf
:
60 bool RecurrenceDescriptor::isFloatingPointRecurrenceKind(RecurKind Kind
) {
61 return (Kind
!= RecurKind::None
) && !isIntegerRecurrenceKind(Kind
);
64 /// Determines if Phi may have been type-promoted. If Phi has a single user
65 /// that ANDs the Phi with a type mask, return the user. RT is updated to
66 /// account for the narrower bit width represented by the mask, and the AND
67 /// instruction is added to CI.
68 static Instruction
*lookThroughAnd(PHINode
*Phi
, Type
*&RT
,
69 SmallPtrSetImpl
<Instruction
*> &Visited
,
70 SmallPtrSetImpl
<Instruction
*> &CI
) {
71 if (!Phi
->hasOneUse())
74 const APInt
*M
= nullptr;
75 Instruction
*I
, *J
= cast
<Instruction
>(Phi
->use_begin()->getUser());
77 // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT
78 // with a new integer type of the corresponding bit width.
79 if (match(J
, m_c_And(m_Instruction(I
), m_APInt(M
)))) {
80 int32_t Bits
= (*M
+ 1).exactLogBase2();
82 RT
= IntegerType::get(Phi
->getContext(), Bits
);
91 /// Compute the minimal bit width needed to represent a reduction whose exit
92 /// instruction is given by Exit.
93 static std::pair
<Type
*, bool> computeRecurrenceType(Instruction
*Exit
,
97 bool IsSigned
= false;
98 const DataLayout
&DL
= Exit
->getModule()->getDataLayout();
99 uint64_t MaxBitWidth
= DL
.getTypeSizeInBits(Exit
->getType());
102 // Use the demanded bits analysis to determine the bits that are live out
103 // of the exit instruction, rounding up to the nearest power of two. If the
104 // use of demanded bits results in a smaller bit width, we know the value
105 // must be positive (i.e., IsSigned = false), because if this were not the
106 // case, the sign bit would have been demanded.
107 auto Mask
= DB
->getDemandedBits(Exit
);
108 MaxBitWidth
= Mask
.getBitWidth() - Mask
.countl_zero();
111 if (MaxBitWidth
== DL
.getTypeSizeInBits(Exit
->getType()) && AC
&& DT
) {
112 // If demanded bits wasn't able to limit the bit width, we can try to use
113 // value tracking instead. This can be the case, for example, if the value
115 auto NumSignBits
= ComputeNumSignBits(Exit
, DL
, 0, AC
, nullptr, DT
);
116 auto NumTypeBits
= DL
.getTypeSizeInBits(Exit
->getType());
117 MaxBitWidth
= NumTypeBits
- NumSignBits
;
118 KnownBits Bits
= computeKnownBits(Exit
, DL
);
119 if (!Bits
.isNonNegative()) {
120 // If the value is not known to be non-negative, we set IsSigned to true,
121 // meaning that we will use sext instructions instead of zext
122 // instructions to restore the original type.
124 // Make sure at least one sign bit is included in the result, so it
125 // will get properly sign-extended.
129 MaxBitWidth
= llvm::bit_ceil(MaxBitWidth
);
131 return std::make_pair(Type::getIntNTy(Exit
->getContext(), MaxBitWidth
),
135 /// Collect cast instructions that can be ignored in the vectorizer's cost
136 /// model, given a reduction exit value and the minimal type in which the
137 // reduction can be represented. Also search casts to the recurrence type
138 // to find the minimum width used by the recurrence.
139 static void collectCastInstrs(Loop
*TheLoop
, Instruction
*Exit
,
140 Type
*RecurrenceType
,
141 SmallPtrSetImpl
<Instruction
*> &Casts
,
142 unsigned &MinWidthCastToRecurTy
) {
144 SmallVector
<Instruction
*, 8> Worklist
;
145 SmallPtrSet
<Instruction
*, 8> Visited
;
146 Worklist
.push_back(Exit
);
147 MinWidthCastToRecurTy
= -1U;
149 while (!Worklist
.empty()) {
150 Instruction
*Val
= Worklist
.pop_back_val();
152 if (auto *Cast
= dyn_cast
<CastInst
>(Val
)) {
153 if (Cast
->getSrcTy() == RecurrenceType
) {
154 // If the source type of a cast instruction is equal to the recurrence
155 // type, it will be eliminated, and should be ignored in the vectorizer
160 if (Cast
->getDestTy() == RecurrenceType
) {
161 // The minimum width used by the recurrence is found by checking for
162 // casts on its operands. The minimum width is used by the vectorizer
163 // when finding the widest type for in-loop reductions without any
165 MinWidthCastToRecurTy
= std::min
<unsigned>(
166 MinWidthCastToRecurTy
, Cast
->getSrcTy()->getScalarSizeInBits());
170 // Add all operands to the work list if they are loop-varying values that
171 // we haven't yet visited.
172 for (Value
*O
: cast
<User
>(Val
)->operands())
173 if (auto *I
= dyn_cast
<Instruction
>(O
))
174 if (TheLoop
->contains(I
) && !Visited
.count(I
))
175 Worklist
.push_back(I
);
179 // Check if a given Phi node can be recognized as an ordered reduction for
180 // vectorizing floating point operations without unsafe math.
181 static bool checkOrderedReduction(RecurKind Kind
, Instruction
*ExactFPMathInst
,
182 Instruction
*Exit
, PHINode
*Phi
) {
183 // Currently only FAdd and FMulAdd are supported.
184 if (Kind
!= RecurKind::FAdd
&& Kind
!= RecurKind::FMulAdd
)
187 if (Kind
== RecurKind::FAdd
&& Exit
->getOpcode() != Instruction::FAdd
)
190 if (Kind
== RecurKind::FMulAdd
&&
191 !RecurrenceDescriptor::isFMulAddIntrinsic(Exit
))
194 // Ensure the exit instruction has only one user other than the reduction PHI
195 if (Exit
!= ExactFPMathInst
|| Exit
->hasNUsesOrMore(3))
198 // The only pattern accepted is the one in which the reduction PHI
199 // is used as one of the operands of the exit instruction
200 auto *Op0
= Exit
->getOperand(0);
201 auto *Op1
= Exit
->getOperand(1);
202 if (Kind
== RecurKind::FAdd
&& Op0
!= Phi
&& Op1
!= Phi
)
204 if (Kind
== RecurKind::FMulAdd
&& Exit
->getOperand(2) != Phi
)
207 LLVM_DEBUG(dbgs() << "LV: Found an ordered reduction: Phi: " << *Phi
208 << ", ExitInst: " << *Exit
<< "\n");
213 bool RecurrenceDescriptor::AddReductionVar(
214 PHINode
*Phi
, RecurKind Kind
, Loop
*TheLoop
, FastMathFlags FuncFMF
,
215 RecurrenceDescriptor
&RedDes
, DemandedBits
*DB
, AssumptionCache
*AC
,
216 DominatorTree
*DT
, ScalarEvolution
*SE
) {
217 if (Phi
->getNumIncomingValues() != 2)
220 // Reduction variables are only found in the loop header block.
221 if (Phi
->getParent() != TheLoop
->getHeader())
224 // Obtain the reduction start value from the value that comes from the loop
226 Value
*RdxStart
= Phi
->getIncomingValueForBlock(TheLoop
->getLoopPreheader());
228 // ExitInstruction is the single value which is used outside the loop.
229 // We only allow for a single reduction value to be used outside the loop.
230 // This includes users of the reduction, variables (which form a cycle
231 // which ends in the phi node).
232 Instruction
*ExitInstruction
= nullptr;
234 // Variable to keep last visited store instruction. By the end of the
235 // algorithm this variable will be either empty or having intermediate
236 // reduction value stored in invariant address.
237 StoreInst
*IntermediateStore
= nullptr;
239 // Indicates that we found a reduction operation in our scan.
240 bool FoundReduxOp
= false;
242 // We start with the PHI node and scan for all of the users of this
243 // instruction. All users must be instructions that can be used as reduction
244 // variables (such as ADD). We must have a single out-of-block user. The cycle
245 // must include the original PHI.
246 bool FoundStartPHI
= false;
248 // To recognize min/max patterns formed by a icmp select sequence, we store
249 // the number of instruction we saw from the recognized min/max pattern,
250 // to make sure we only see exactly the two instructions.
251 unsigned NumCmpSelectPatternInst
= 0;
252 InstDesc
ReduxDesc(false, nullptr);
254 // Data used for determining if the recurrence has been type-promoted.
255 Type
*RecurrenceType
= Phi
->getType();
256 SmallPtrSet
<Instruction
*, 4> CastInsts
;
257 unsigned MinWidthCastToRecurrenceType
;
258 Instruction
*Start
= Phi
;
259 bool IsSigned
= false;
261 SmallPtrSet
<Instruction
*, 8> VisitedInsts
;
262 SmallVector
<Instruction
*, 8> Worklist
;
264 // Return early if the recurrence kind does not match the type of Phi. If the
265 // recurrence kind is arithmetic, we attempt to look through AND operations
266 // resulting from the type promotion performed by InstCombine. Vector
267 // operations are not limited to the legal integer widths, so we may be able
268 // to evaluate the reduction in the narrower width.
269 if (RecurrenceType
->isFloatingPointTy()) {
270 if (!isFloatingPointRecurrenceKind(Kind
))
272 } else if (RecurrenceType
->isIntegerTy()) {
273 if (!isIntegerRecurrenceKind(Kind
))
275 if (!isMinMaxRecurrenceKind(Kind
))
276 Start
= lookThroughAnd(Phi
, RecurrenceType
, VisitedInsts
, CastInsts
);
278 // Pointer min/max may exist, but it is not supported as a reduction op.
282 Worklist
.push_back(Start
);
283 VisitedInsts
.insert(Start
);
285 // Start with all flags set because we will intersect this with the reduction
286 // flags from all the reduction operations.
287 FastMathFlags FMF
= FastMathFlags::getFast();
289 // The first instruction in the use-def chain of the Phi node that requires
290 // exact floating point operations.
291 Instruction
*ExactFPMathInst
= nullptr;
293 // A value in the reduction can be used:
294 // - By the reduction:
295 // - Reduction operation:
296 // - One use of reduction value (safe).
297 // - Multiple use of reduction value (not safe).
299 // - All uses of the PHI must be the reduction (safe).
300 // - Otherwise, not safe.
301 // - By instructions outside of the loop (safe).
302 // * One value may have several outside users, but all outside
303 // uses must be of the same value.
304 // - By store instructions with a loop invariant address (safe with
305 // the following restrictions):
306 // * If there are several stores, all must have the same address.
307 // * Final value should be stored in that loop invariant address.
308 // - By an instruction that is not part of the reduction (not safe).
310 // * An instruction type other than PHI or the reduction operation.
311 // * A PHI in the header other than the initial PHI.
312 while (!Worklist
.empty()) {
313 Instruction
*Cur
= Worklist
.pop_back_val();
315 // Store instructions are allowed iff it is the store of the reduction
316 // value to the same loop invariant memory location.
317 if (auto *SI
= dyn_cast
<StoreInst
>(Cur
)) {
319 LLVM_DEBUG(dbgs() << "Store instructions are not processed without "
320 << "Scalar Evolution Analysis\n");
324 const SCEV
*PtrScev
= SE
->getSCEV(SI
->getPointerOperand());
325 // Check it is the same address as previous stores
326 if (IntermediateStore
) {
327 const SCEV
*OtherScev
=
328 SE
->getSCEV(IntermediateStore
->getPointerOperand());
330 if (OtherScev
!= PtrScev
) {
331 LLVM_DEBUG(dbgs() << "Storing reduction value to different addresses "
332 << "inside the loop: " << *SI
->getPointerOperand()
334 << *IntermediateStore
->getPointerOperand() << '\n');
339 // Check the pointer is loop invariant
340 if (!SE
->isLoopInvariant(PtrScev
, TheLoop
)) {
341 LLVM_DEBUG(dbgs() << "Storing reduction value to non-uniform address "
342 << "inside the loop: " << *SI
->getPointerOperand()
347 // IntermediateStore is always the last store in the loop.
348 IntermediateStore
= SI
;
353 // If the instruction has no users then this is a broken chain and can't be
354 // a reduction variable.
355 if (Cur
->use_empty())
358 bool IsAPhi
= isa
<PHINode
>(Cur
);
360 // A header PHI use other than the original PHI.
361 if (Cur
!= Phi
&& IsAPhi
&& Cur
->getParent() == Phi
->getParent())
364 // Reductions of instructions such as Div, and Sub is only possible if the
365 // LHS is the reduction variable.
366 if (!Cur
->isCommutative() && !IsAPhi
&& !isa
<SelectInst
>(Cur
) &&
367 !isa
<ICmpInst
>(Cur
) && !isa
<FCmpInst
>(Cur
) &&
368 !VisitedInsts
.count(dyn_cast
<Instruction
>(Cur
->getOperand(0))))
371 // Any reduction instruction must be of one of the allowed kinds. We ignore
372 // the starting value (the Phi or an AND instruction if the Phi has been
376 isRecurrenceInstr(TheLoop
, Phi
, Cur
, Kind
, ReduxDesc
, FuncFMF
);
377 ExactFPMathInst
= ExactFPMathInst
== nullptr
378 ? ReduxDesc
.getExactFPMathInst()
380 if (!ReduxDesc
.isRecurrence())
382 // FIXME: FMF is allowed on phi, but propagation is not handled correctly.
383 if (isa
<FPMathOperator
>(ReduxDesc
.getPatternInst()) && !IsAPhi
) {
384 FastMathFlags CurFMF
= ReduxDesc
.getPatternInst()->getFastMathFlags();
385 if (auto *Sel
= dyn_cast
<SelectInst
>(ReduxDesc
.getPatternInst())) {
386 // Accept FMF on either fcmp or select of a min/max idiom.
387 // TODO: This is a hack to work-around the fact that FMF may not be
388 // assigned/propagated correctly. If that problem is fixed or we
389 // standardize on fmin/fmax via intrinsics, this can be removed.
390 if (auto *FCmp
= dyn_cast
<FCmpInst
>(Sel
->getCondition()))
391 CurFMF
|= FCmp
->getFastMathFlags();
395 // Update this reduction kind if we matched a new instruction.
396 // TODO: Can we eliminate the need for a 2nd InstDesc by keeping 'Kind'
397 // state accurate while processing the worklist?
398 if (ReduxDesc
.getRecKind() != RecurKind::None
)
399 Kind
= ReduxDesc
.getRecKind();
402 bool IsASelect
= isa
<SelectInst
>(Cur
);
404 // A conditional reduction operation must only have 2 or less uses in
406 if (IsASelect
&& (Kind
== RecurKind::FAdd
|| Kind
== RecurKind::FMul
) &&
407 hasMultipleUsesOf(Cur
, VisitedInsts
, 2))
410 // A reduction operation must only have one use of the reduction value.
411 if (!IsAPhi
&& !IsASelect
&& !isMinMaxRecurrenceKind(Kind
) &&
412 !isAnyOfRecurrenceKind(Kind
) && hasMultipleUsesOf(Cur
, VisitedInsts
, 1))
415 // All inputs to a PHI node must be a reduction value.
416 if (IsAPhi
&& Cur
!= Phi
&& !areAllUsesIn(Cur
, VisitedInsts
))
419 if ((isIntMinMaxRecurrenceKind(Kind
) || Kind
== RecurKind::IAnyOf
) &&
420 (isa
<ICmpInst
>(Cur
) || isa
<SelectInst
>(Cur
)))
421 ++NumCmpSelectPatternInst
;
422 if ((isFPMinMaxRecurrenceKind(Kind
) || Kind
== RecurKind::FAnyOf
) &&
423 (isa
<FCmpInst
>(Cur
) || isa
<SelectInst
>(Cur
)))
424 ++NumCmpSelectPatternInst
;
426 // Check whether we found a reduction operator.
427 FoundReduxOp
|= !IsAPhi
&& Cur
!= Start
;
429 // Process users of current instruction. Push non-PHI nodes after PHI nodes
430 // onto the stack. This way we are going to have seen all inputs to PHI
431 // nodes once we get to them.
432 SmallVector
<Instruction
*, 8> NonPHIs
;
433 SmallVector
<Instruction
*, 8> PHIs
;
434 for (User
*U
: Cur
->users()) {
435 Instruction
*UI
= cast
<Instruction
>(U
);
437 // If the user is a call to llvm.fmuladd then the instruction can only be
438 // the final operand.
439 if (isFMulAddIntrinsic(UI
))
440 if (Cur
== UI
->getOperand(0) || Cur
== UI
->getOperand(1))
443 // Check if we found the exit user.
444 BasicBlock
*Parent
= UI
->getParent();
445 if (!TheLoop
->contains(Parent
)) {
446 // If we already know this instruction is used externally, move on to
448 if (ExitInstruction
== Cur
)
451 // Exit if you find multiple values used outside or if the header phi
452 // node is being used. In this case the user uses the value of the
453 // previous iteration, in which case we would loose "VF-1" iterations of
454 // the reduction operation if we vectorize.
455 if (ExitInstruction
!= nullptr || Cur
== Phi
)
458 // The instruction used by an outside user must be the last instruction
459 // before we feed back to the reduction phi. Otherwise, we loose VF-1
460 // operations on the value.
461 if (!is_contained(Phi
->operands(), Cur
))
464 ExitInstruction
= Cur
;
468 // Process instructions only once (termination). Each reduction cycle
469 // value must only be used once, except by phi nodes and min/max
470 // reductions which are represented as a cmp followed by a select.
471 InstDesc
IgnoredVal(false, nullptr);
472 if (VisitedInsts
.insert(UI
).second
) {
473 if (isa
<PHINode
>(UI
)) {
476 StoreInst
*SI
= dyn_cast
<StoreInst
>(UI
);
477 if (SI
&& SI
->getPointerOperand() == Cur
) {
478 // Reduction variable chain can only be stored somewhere but it
479 // can't be used as an address.
482 NonPHIs
.push_back(UI
);
484 } else if (!isa
<PHINode
>(UI
) &&
485 ((!isa
<FCmpInst
>(UI
) && !isa
<ICmpInst
>(UI
) &&
486 !isa
<SelectInst
>(UI
)) ||
487 (!isConditionalRdxPattern(Kind
, UI
).isRecurrence() &&
488 !isAnyOfPattern(TheLoop
, Phi
, UI
, IgnoredVal
)
490 !isMinMaxPattern(UI
, Kind
, IgnoredVal
).isRecurrence())))
493 // Remember that we completed the cycle.
495 FoundStartPHI
= true;
497 Worklist
.append(PHIs
.begin(), PHIs
.end());
498 Worklist
.append(NonPHIs
.begin(), NonPHIs
.end());
501 // This means we have seen one but not the other instruction of the
502 // pattern or more than just a select and cmp. Zero implies that we saw a
503 // llvm.min/max intrinsic, which is always OK.
504 if (isMinMaxRecurrenceKind(Kind
) && NumCmpSelectPatternInst
!= 2 &&
505 NumCmpSelectPatternInst
!= 0)
508 if (isAnyOfRecurrenceKind(Kind
) && NumCmpSelectPatternInst
!= 1)
511 if (IntermediateStore
) {
512 // Check that stored value goes to the phi node again. This way we make sure
513 // that the value stored in IntermediateStore is indeed the final reduction
515 if (!is_contained(Phi
->operands(), IntermediateStore
->getValueOperand())) {
516 LLVM_DEBUG(dbgs() << "Not a final reduction value stored: "
517 << *IntermediateStore
<< '\n');
521 // If there is an exit instruction it's value should be stored in
523 if (ExitInstruction
&&
524 IntermediateStore
->getValueOperand() != ExitInstruction
) {
525 LLVM_DEBUG(dbgs() << "Last store Instruction of reduction value does not "
526 "store last calculated value of the reduction: "
527 << *IntermediateStore
<< '\n');
531 // If all uses are inside the loop (intermediate stores), then the
532 // reduction value after the loop will be the one used in the last store.
533 if (!ExitInstruction
)
534 ExitInstruction
= cast
<Instruction
>(IntermediateStore
->getValueOperand());
537 if (!FoundStartPHI
|| !FoundReduxOp
|| !ExitInstruction
)
540 const bool IsOrdered
=
541 checkOrderedReduction(Kind
, ExactFPMathInst
, ExitInstruction
, Phi
);
544 // If the starting value is not the same as the phi node, we speculatively
545 // looked through an 'and' instruction when evaluating a potential
546 // arithmetic reduction to determine if it may have been type-promoted.
548 // We now compute the minimal bit width that is required to represent the
549 // reduction. If this is the same width that was indicated by the 'and', we
550 // can represent the reduction in the smaller type. The 'and' instruction
551 // will be eliminated since it will essentially be a cast instruction that
552 // can be ignore in the cost model. If we compute a different type than we
553 // did when evaluating the 'and', the 'and' will not be eliminated, and we
554 // will end up with different kinds of operations in the recurrence
555 // expression (e.g., IntegerAND, IntegerADD). We give up if this is
558 // The vectorizer relies on InstCombine to perform the actual
559 // type-shrinking. It does this by inserting instructions to truncate the
560 // exit value of the reduction to the width indicated by RecurrenceType and
561 // then extend this value back to the original width. If IsSigned is false,
562 // a 'zext' instruction will be generated; otherwise, a 'sext' will be
565 // TODO: We should not rely on InstCombine to rewrite the reduction in the
566 // smaller type. We should just generate a correctly typed expression
569 std::tie(ComputedType
, IsSigned
) =
570 computeRecurrenceType(ExitInstruction
, DB
, AC
, DT
);
571 if (ComputedType
!= RecurrenceType
)
575 // Collect cast instructions and the minimum width used by the recurrence.
576 // If the starting value is not the same as the phi node and the computed
577 // recurrence type is equal to the recurrence type, the recurrence expression
578 // will be represented in a narrower or wider type. If there are any cast
579 // instructions that will be unnecessary, collect them in CastsFromRecurTy.
580 // Note that the 'and' instruction was already included in this list.
582 // TODO: A better way to represent this may be to tag in some way all the
583 // instructions that are a part of the reduction. The vectorizer cost
584 // model could then apply the recurrence type to these instructions,
585 // without needing a white list of instructions to ignore.
586 // This may also be useful for the inloop reductions, if it can be
587 // kept simple enough.
588 collectCastInstrs(TheLoop
, ExitInstruction
, RecurrenceType
, CastInsts
,
589 MinWidthCastToRecurrenceType
);
591 // We found a reduction var if we have reached the original phi node and we
592 // only have a single instruction with out-of-loop users.
594 // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
595 // is saved as part of the RecurrenceDescriptor.
597 // Save the description of this reduction variable.
598 RecurrenceDescriptor
RD(RdxStart
, ExitInstruction
, IntermediateStore
, Kind
,
599 FMF
, ExactFPMathInst
, RecurrenceType
, IsSigned
,
600 IsOrdered
, CastInsts
, MinWidthCastToRecurrenceType
);
606 // We are looking for loops that do something like this:
608 // for (int i = 0; i < n; i++) {
612 // where the reduction value (r) only has two states, in this example 0 or 3.
613 // The generated LLVM IR for this type of loop will be like this:
615 // %r = phi i32 [ %spec.select, %for.body ], [ 0, %entry ]
617 // %cmp = icmp sgt i32 %5, 3
618 // %spec.select = select i1 %cmp, i32 3, i32 %r
620 // In general we can support vectorization of loops where 'r' flips between
621 // any two non-constants, provided they are loop invariant. The only thing
622 // we actually care about at the end of the loop is whether or not any lane
623 // in the selected vector is different from the start value. The final
624 // across-vector reduction after the loop simply involves choosing the start
625 // value if nothing changed (0 in the example above) or the other selected
626 // value (3 in the example above).
627 RecurrenceDescriptor::InstDesc
628 RecurrenceDescriptor::isAnyOfPattern(Loop
*Loop
, PHINode
*OrigPhi
,
629 Instruction
*I
, InstDesc
&Prev
) {
630 // We must handle the select(cmp(),x,y) as a single instruction. Advance to
632 CmpInst::Predicate Pred
;
633 if (match(I
, m_OneUse(m_Cmp(Pred
, m_Value(), m_Value())))) {
634 if (auto *Select
= dyn_cast
<SelectInst
>(*I
->user_begin()))
635 return InstDesc(Select
, Prev
.getRecKind());
638 // Only match select with single use cmp condition.
639 if (!match(I
, m_Select(m_OneUse(m_Cmp(Pred
, m_Value(), m_Value())), m_Value(),
641 return InstDesc(false, I
);
643 SelectInst
*SI
= cast
<SelectInst
>(I
);
644 Value
*NonPhi
= nullptr;
646 if (OrigPhi
== dyn_cast
<PHINode
>(SI
->getTrueValue()))
647 NonPhi
= SI
->getFalseValue();
648 else if (OrigPhi
== dyn_cast
<PHINode
>(SI
->getFalseValue()))
649 NonPhi
= SI
->getTrueValue();
651 return InstDesc(false, I
);
653 // We are looking for selects of the form:
654 // select(cmp(), phi, loop_invariant) or
655 // select(cmp(), loop_invariant, phi)
656 if (!Loop
->isLoopInvariant(NonPhi
))
657 return InstDesc(false, I
);
659 return InstDesc(I
, isa
<ICmpInst
>(I
->getOperand(0)) ? RecurKind::IAnyOf
660 : RecurKind::FAnyOf
);
663 RecurrenceDescriptor::InstDesc
664 RecurrenceDescriptor::isMinMaxPattern(Instruction
*I
, RecurKind Kind
,
665 const InstDesc
&Prev
) {
666 assert((isa
<CmpInst
>(I
) || isa
<SelectInst
>(I
) || isa
<CallInst
>(I
)) &&
667 "Expected a cmp or select or call instruction");
668 if (!isMinMaxRecurrenceKind(Kind
))
669 return InstDesc(false, I
);
671 // We must handle the select(cmp()) as a single instruction. Advance to the
673 CmpInst::Predicate Pred
;
674 if (match(I
, m_OneUse(m_Cmp(Pred
, m_Value(), m_Value())))) {
675 if (auto *Select
= dyn_cast
<SelectInst
>(*I
->user_begin()))
676 return InstDesc(Select
, Prev
.getRecKind());
679 // Only match select with single use cmp condition, or a min/max intrinsic.
680 if (!isa
<IntrinsicInst
>(I
) &&
681 !match(I
, m_Select(m_OneUse(m_Cmp(Pred
, m_Value(), m_Value())), m_Value(),
683 return InstDesc(false, I
);
685 // Look for a min/max pattern.
686 if (match(I
, m_UMin(m_Value(), m_Value())))
687 return InstDesc(Kind
== RecurKind::UMin
, I
);
688 if (match(I
, m_UMax(m_Value(), m_Value())))
689 return InstDesc(Kind
== RecurKind::UMax
, I
);
690 if (match(I
, m_SMax(m_Value(), m_Value())))
691 return InstDesc(Kind
== RecurKind::SMax
, I
);
692 if (match(I
, m_SMin(m_Value(), m_Value())))
693 return InstDesc(Kind
== RecurKind::SMin
, I
);
694 if (match(I
, m_OrdFMin(m_Value(), m_Value())))
695 return InstDesc(Kind
== RecurKind::FMin
, I
);
696 if (match(I
, m_OrdFMax(m_Value(), m_Value())))
697 return InstDesc(Kind
== RecurKind::FMax
, I
);
698 if (match(I
, m_UnordFMin(m_Value(), m_Value())))
699 return InstDesc(Kind
== RecurKind::FMin
, I
);
700 if (match(I
, m_UnordFMax(m_Value(), m_Value())))
701 return InstDesc(Kind
== RecurKind::FMax
, I
);
702 if (match(I
, m_Intrinsic
<Intrinsic::minnum
>(m_Value(), m_Value())))
703 return InstDesc(Kind
== RecurKind::FMin
, I
);
704 if (match(I
, m_Intrinsic
<Intrinsic::maxnum
>(m_Value(), m_Value())))
705 return InstDesc(Kind
== RecurKind::FMax
, I
);
706 if (match(I
, m_Intrinsic
<Intrinsic::minimum
>(m_Value(), m_Value())))
707 return InstDesc(Kind
== RecurKind::FMinimum
, I
);
708 if (match(I
, m_Intrinsic
<Intrinsic::maximum
>(m_Value(), m_Value())))
709 return InstDesc(Kind
== RecurKind::FMaximum
, I
);
711 return InstDesc(false, I
);
714 /// Returns true if the select instruction has users in the compare-and-add
715 /// reduction pattern below. The select instruction argument is the last one
720 /// %cmp = fcmp pred %0, %CFP
721 /// %add = fadd %0, %sum.1
722 /// %sum.2 = select %cmp, %add, %sum.1
723 RecurrenceDescriptor::InstDesc
724 RecurrenceDescriptor::isConditionalRdxPattern(RecurKind Kind
, Instruction
*I
) {
725 SelectInst
*SI
= dyn_cast
<SelectInst
>(I
);
727 return InstDesc(false, I
);
729 CmpInst
*CI
= dyn_cast
<CmpInst
>(SI
->getCondition());
730 // Only handle single use cases for now.
731 if (!CI
|| !CI
->hasOneUse())
732 return InstDesc(false, I
);
734 Value
*TrueVal
= SI
->getTrueValue();
735 Value
*FalseVal
= SI
->getFalseValue();
736 // Handle only when either of operands of select instruction is a PHI
738 if ((isa
<PHINode
>(*TrueVal
) && isa
<PHINode
>(*FalseVal
)) ||
739 (!isa
<PHINode
>(*TrueVal
) && !isa
<PHINode
>(*FalseVal
)))
740 return InstDesc(false, I
);
743 isa
<PHINode
>(*TrueVal
) ? dyn_cast
<Instruction
>(FalseVal
)
744 : dyn_cast
<Instruction
>(TrueVal
);
745 if (!I1
|| !I1
->isBinaryOp())
746 return InstDesc(false, I
);
749 if (!(((m_FAdd(m_Value(Op1
), m_Value(Op2
)).match(I1
) ||
750 m_FSub(m_Value(Op1
), m_Value(Op2
)).match(I1
)) &&
752 (m_FMul(m_Value(Op1
), m_Value(Op2
)).match(I1
) && (I1
->isFast())) ||
753 ((m_Add(m_Value(Op1
), m_Value(Op2
)).match(I1
) ||
754 m_Sub(m_Value(Op1
), m_Value(Op2
)).match(I1
))) ||
755 (m_Mul(m_Value(Op1
), m_Value(Op2
)).match(I1
))))
756 return InstDesc(false, I
);
758 Instruction
*IPhi
= isa
<PHINode
>(*Op1
) ? dyn_cast
<Instruction
>(Op1
)
759 : dyn_cast
<Instruction
>(Op2
);
760 if (!IPhi
|| IPhi
!= FalseVal
)
761 return InstDesc(false, I
);
763 return InstDesc(true, SI
);
766 RecurrenceDescriptor::InstDesc
767 RecurrenceDescriptor::isRecurrenceInstr(Loop
*L
, PHINode
*OrigPhi
,
768 Instruction
*I
, RecurKind Kind
,
769 InstDesc
&Prev
, FastMathFlags FuncFMF
) {
770 assert(Prev
.getRecKind() == RecurKind::None
|| Prev
.getRecKind() == Kind
);
771 switch (I
->getOpcode()) {
773 return InstDesc(false, I
);
774 case Instruction::PHI
:
775 return InstDesc(I
, Prev
.getRecKind(), Prev
.getExactFPMathInst());
776 case Instruction::Sub
:
777 case Instruction::Add
:
778 return InstDesc(Kind
== RecurKind::Add
, I
);
779 case Instruction::Mul
:
780 return InstDesc(Kind
== RecurKind::Mul
, I
);
781 case Instruction::And
:
782 return InstDesc(Kind
== RecurKind::And
, I
);
783 case Instruction::Or
:
784 return InstDesc(Kind
== RecurKind::Or
, I
);
785 case Instruction::Xor
:
786 return InstDesc(Kind
== RecurKind::Xor
, I
);
787 case Instruction::FDiv
:
788 case Instruction::FMul
:
789 return InstDesc(Kind
== RecurKind::FMul
, I
,
790 I
->hasAllowReassoc() ? nullptr : I
);
791 case Instruction::FSub
:
792 case Instruction::FAdd
:
793 return InstDesc(Kind
== RecurKind::FAdd
, I
,
794 I
->hasAllowReassoc() ? nullptr : I
);
795 case Instruction::Select
:
796 if (Kind
== RecurKind::FAdd
|| Kind
== RecurKind::FMul
||
797 Kind
== RecurKind::Add
|| Kind
== RecurKind::Mul
)
798 return isConditionalRdxPattern(Kind
, I
);
800 case Instruction::FCmp
:
801 case Instruction::ICmp
:
802 case Instruction::Call
:
803 if (isAnyOfRecurrenceKind(Kind
))
804 return isAnyOfPattern(L
, OrigPhi
, I
, Prev
);
805 auto HasRequiredFMF
= [&]() {
806 if (FuncFMF
.noNaNs() && FuncFMF
.noSignedZeros())
808 if (isa
<FPMathOperator
>(I
) && I
->hasNoNaNs() && I
->hasNoSignedZeros())
810 // minimum and maximum intrinsics do not require nsz and nnan flags since
811 // NaN and signed zeroes are propagated in the intrinsic implementation.
812 return match(I
, m_Intrinsic
<Intrinsic::minimum
>(m_Value(), m_Value())) ||
813 match(I
, m_Intrinsic
<Intrinsic::maximum
>(m_Value(), m_Value()));
815 if (isIntMinMaxRecurrenceKind(Kind
) ||
816 (HasRequiredFMF() && isFPMinMaxRecurrenceKind(Kind
)))
817 return isMinMaxPattern(I
, Kind
, Prev
);
818 else if (isFMulAddIntrinsic(I
))
819 return InstDesc(Kind
== RecurKind::FMulAdd
, I
,
820 I
->hasAllowReassoc() ? nullptr : I
);
821 return InstDesc(false, I
);
825 bool RecurrenceDescriptor::hasMultipleUsesOf(
826 Instruction
*I
, SmallPtrSetImpl
<Instruction
*> &Insts
,
827 unsigned MaxNumUses
) {
828 unsigned NumUses
= 0;
829 for (const Use
&U
: I
->operands()) {
830 if (Insts
.count(dyn_cast
<Instruction
>(U
)))
832 if (NumUses
> MaxNumUses
)
839 bool RecurrenceDescriptor::isReductionPHI(PHINode
*Phi
, Loop
*TheLoop
,
840 RecurrenceDescriptor
&RedDes
,
841 DemandedBits
*DB
, AssumptionCache
*AC
,
843 ScalarEvolution
*SE
) {
844 BasicBlock
*Header
= TheLoop
->getHeader();
845 Function
&F
= *Header
->getParent();
848 F
.getFnAttribute("no-nans-fp-math").getValueAsBool());
849 FMF
.setNoSignedZeros(
850 F
.getFnAttribute("no-signed-zeros-fp-math").getValueAsBool());
852 if (AddReductionVar(Phi
, RecurKind::Add
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
854 LLVM_DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi
<< "\n");
857 if (AddReductionVar(Phi
, RecurKind::Mul
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
859 LLVM_DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi
<< "\n");
862 if (AddReductionVar(Phi
, RecurKind::Or
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
864 LLVM_DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi
<< "\n");
867 if (AddReductionVar(Phi
, RecurKind::And
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
869 LLVM_DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi
<< "\n");
872 if (AddReductionVar(Phi
, RecurKind::Xor
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
874 LLVM_DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi
<< "\n");
877 if (AddReductionVar(Phi
, RecurKind::SMax
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
879 LLVM_DEBUG(dbgs() << "Found a SMAX reduction PHI." << *Phi
<< "\n");
882 if (AddReductionVar(Phi
, RecurKind::SMin
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
884 LLVM_DEBUG(dbgs() << "Found a SMIN reduction PHI." << *Phi
<< "\n");
887 if (AddReductionVar(Phi
, RecurKind::UMax
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
889 LLVM_DEBUG(dbgs() << "Found a UMAX reduction PHI." << *Phi
<< "\n");
892 if (AddReductionVar(Phi
, RecurKind::UMin
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
894 LLVM_DEBUG(dbgs() << "Found a UMIN reduction PHI." << *Phi
<< "\n");
897 if (AddReductionVar(Phi
, RecurKind::IAnyOf
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
899 LLVM_DEBUG(dbgs() << "Found an integer conditional select reduction PHI."
903 if (AddReductionVar(Phi
, RecurKind::FMul
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
905 LLVM_DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi
<< "\n");
908 if (AddReductionVar(Phi
, RecurKind::FAdd
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
910 LLVM_DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi
<< "\n");
913 if (AddReductionVar(Phi
, RecurKind::FMax
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
915 LLVM_DEBUG(dbgs() << "Found a float MAX reduction PHI." << *Phi
<< "\n");
918 if (AddReductionVar(Phi
, RecurKind::FMin
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
920 LLVM_DEBUG(dbgs() << "Found a float MIN reduction PHI." << *Phi
<< "\n");
923 if (AddReductionVar(Phi
, RecurKind::FAnyOf
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
925 LLVM_DEBUG(dbgs() << "Found a float conditional select reduction PHI."
926 << " PHI." << *Phi
<< "\n");
929 if (AddReductionVar(Phi
, RecurKind::FMulAdd
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
931 LLVM_DEBUG(dbgs() << "Found an FMulAdd reduction PHI." << *Phi
<< "\n");
934 if (AddReductionVar(Phi
, RecurKind::FMaximum
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
936 LLVM_DEBUG(dbgs() << "Found a float MAXIMUM reduction PHI." << *Phi
<< "\n");
939 if (AddReductionVar(Phi
, RecurKind::FMinimum
, TheLoop
, FMF
, RedDes
, DB
, AC
, DT
,
941 LLVM_DEBUG(dbgs() << "Found a float MINIMUM reduction PHI." << *Phi
<< "\n");
944 // Not a reduction of known type.
948 bool RecurrenceDescriptor::isFixedOrderRecurrence(PHINode
*Phi
, Loop
*TheLoop
,
951 // Ensure the phi node is in the loop header and has two incoming values.
952 if (Phi
->getParent() != TheLoop
->getHeader() ||
953 Phi
->getNumIncomingValues() != 2)
956 // Ensure the loop has a preheader and a single latch block. The loop
957 // vectorizer will need the latch to set up the next iteration of the loop.
958 auto *Preheader
= TheLoop
->getLoopPreheader();
959 auto *Latch
= TheLoop
->getLoopLatch();
960 if (!Preheader
|| !Latch
)
963 // Ensure the phi node's incoming blocks are the loop preheader and latch.
964 if (Phi
->getBasicBlockIndex(Preheader
) < 0 ||
965 Phi
->getBasicBlockIndex(Latch
) < 0)
968 // Get the previous value. The previous value comes from the latch edge while
969 // the initial value comes from the preheader edge.
970 auto *Previous
= dyn_cast
<Instruction
>(Phi
->getIncomingValueForBlock(Latch
));
972 // If Previous is a phi in the header, go through incoming values from the
973 // latch until we find a non-phi value. Use this as the new Previous, all uses
974 // in the header will be dominated by the original phi, but need to be moved
975 // after the non-phi previous value.
976 SmallPtrSet
<PHINode
*, 4> SeenPhis
;
977 while (auto *PrevPhi
= dyn_cast_or_null
<PHINode
>(Previous
)) {
978 if (PrevPhi
->getParent() != Phi
->getParent())
980 if (!SeenPhis
.insert(PrevPhi
).second
)
982 Previous
= dyn_cast
<Instruction
>(PrevPhi
->getIncomingValueForBlock(Latch
));
985 if (!Previous
|| !TheLoop
->contains(Previous
) || isa
<PHINode
>(Previous
))
988 // Ensure every user of the phi node (recursively) is dominated by the
989 // previous value. The dominance requirement ensures the loop vectorizer will
990 // not need to vectorize the initial value prior to the first iteration of the
992 // TODO: Consider extending this sinking to handle memory instructions.
994 SmallPtrSet
<Value
*, 8> Seen
;
995 BasicBlock
*PhiBB
= Phi
->getParent();
996 SmallVector
<Instruction
*, 8> WorkList
;
997 auto TryToPushSinkCandidate
= [&](Instruction
*SinkCandidate
) {
998 // Cyclic dependence.
999 if (Previous
== SinkCandidate
)
1002 if (!Seen
.insert(SinkCandidate
).second
)
1004 if (DT
->dominates(Previous
,
1005 SinkCandidate
)) // We already are good w/o sinking.
1008 if (SinkCandidate
->getParent() != PhiBB
||
1009 SinkCandidate
->mayHaveSideEffects() ||
1010 SinkCandidate
->mayReadFromMemory() || SinkCandidate
->isTerminator())
1013 // If we reach a PHI node that is not dominated by Previous, we reached a
1014 // header PHI. No need for sinking.
1015 if (isa
<PHINode
>(SinkCandidate
))
1018 // Sink User tentatively and check its users
1019 WorkList
.push_back(SinkCandidate
);
1023 WorkList
.push_back(Phi
);
1024 // Try to recursively sink instructions and their users after Previous.
1025 while (!WorkList
.empty()) {
1026 Instruction
*Current
= WorkList
.pop_back_val();
1027 for (User
*User
: Current
->users()) {
1028 if (!TryToPushSinkCandidate(cast
<Instruction
>(User
)))
1036 /// This function returns the identity element (or neutral element) for
1037 /// the operation K.
1038 Value
*RecurrenceDescriptor::getRecurrenceIdentity(RecurKind K
, Type
*Tp
,
1039 FastMathFlags FMF
) const {
1041 case RecurKind::Xor
:
1042 case RecurKind::Add
:
1044 // Adding, Xoring, Oring zero to a number does not change it.
1045 return ConstantInt::get(Tp
, 0);
1046 case RecurKind::Mul
:
1047 // Multiplying a number by 1 does not change it.
1048 return ConstantInt::get(Tp
, 1);
1049 case RecurKind::And
:
1050 // AND-ing a number with an all-1 value does not change it.
1051 return ConstantInt::get(Tp
, -1, true);
1052 case RecurKind::FMul
:
1053 // Multiplying a number by 1 does not change it.
1054 return ConstantFP::get(Tp
, 1.0L);
1055 case RecurKind::FMulAdd
:
1056 case RecurKind::FAdd
:
1057 // Adding zero to a number does not change it.
1058 // FIXME: Ideally we should not need to check FMF for FAdd and should always
1059 // use -0.0. However, this will currently result in mixed vectors of 0.0/-0.0.
1060 // Instead, we should ensure that 1) the FMF from FAdd are propagated to the PHI
1061 // nodes where possible, and 2) PHIs with the nsz flag + -0.0 use 0.0. This would
1062 // mean we can then remove the check for noSignedZeros() below (see D98963).
1063 if (FMF
.noSignedZeros())
1064 return ConstantFP::get(Tp
, 0.0L);
1065 return ConstantFP::get(Tp
, -0.0L);
1066 case RecurKind::UMin
:
1067 return ConstantInt::get(Tp
, -1, true);
1068 case RecurKind::UMax
:
1069 return ConstantInt::get(Tp
, 0);
1070 case RecurKind::SMin
:
1071 return ConstantInt::get(Tp
,
1072 APInt::getSignedMaxValue(Tp
->getIntegerBitWidth()));
1073 case RecurKind::SMax
:
1074 return ConstantInt::get(Tp
,
1075 APInt::getSignedMinValue(Tp
->getIntegerBitWidth()));
1076 case RecurKind::FMin
:
1077 assert((FMF
.noNaNs() && FMF
.noSignedZeros()) &&
1078 "nnan, nsz is expected to be set for FP min reduction.");
1079 return ConstantFP::getInfinity(Tp
, false /*Negative*/);
1080 case RecurKind::FMax
:
1081 assert((FMF
.noNaNs() && FMF
.noSignedZeros()) &&
1082 "nnan, nsz is expected to be set for FP max reduction.");
1083 return ConstantFP::getInfinity(Tp
, true /*Negative*/);
1084 case RecurKind::FMinimum
:
1085 return ConstantFP::getInfinity(Tp
, false /*Negative*/);
1086 case RecurKind::FMaximum
:
1087 return ConstantFP::getInfinity(Tp
, true /*Negative*/);
1088 case RecurKind::IAnyOf
:
1089 case RecurKind::FAnyOf
:
1090 return getRecurrenceStartValue();
1093 llvm_unreachable("Unknown recurrence kind");
1097 unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind
) {
1099 case RecurKind::Add
:
1100 return Instruction::Add
;
1101 case RecurKind::Mul
:
1102 return Instruction::Mul
;
1104 return Instruction::Or
;
1105 case RecurKind::And
:
1106 return Instruction::And
;
1107 case RecurKind::Xor
:
1108 return Instruction::Xor
;
1109 case RecurKind::FMul
:
1110 return Instruction::FMul
;
1111 case RecurKind::FMulAdd
:
1112 case RecurKind::FAdd
:
1113 return Instruction::FAdd
;
1114 case RecurKind::SMax
:
1115 case RecurKind::SMin
:
1116 case RecurKind::UMax
:
1117 case RecurKind::UMin
:
1118 case RecurKind::IAnyOf
:
1119 return Instruction::ICmp
;
1120 case RecurKind::FMax
:
1121 case RecurKind::FMin
:
1122 case RecurKind::FMaximum
:
1123 case RecurKind::FMinimum
:
1124 case RecurKind::FAnyOf
:
1125 return Instruction::FCmp
;
1127 llvm_unreachable("Unknown recurrence operation");
1131 SmallVector
<Instruction
*, 4>
1132 RecurrenceDescriptor::getReductionOpChain(PHINode
*Phi
, Loop
*L
) const {
1133 SmallVector
<Instruction
*, 4> ReductionOperations
;
1134 unsigned RedOp
= getOpcode(Kind
);
1136 // Search down from the Phi to the LoopExitInstr, looking for instructions
1137 // with a single user of the correct type for the reduction.
1139 // Note that we check that the type of the operand is correct for each item in
1140 // the chain, including the last (the loop exit value). This can come up from
1141 // sub, which would otherwise be treated as an add reduction. MinMax also need
1142 // to check for a pair of icmp/select, for which we use getNextInstruction and
1143 // isCorrectOpcode functions to step the right number of instruction, and
1144 // check the icmp/select pair.
1145 // FIXME: We also do not attempt to look through Select's yet, which might
1146 // be part of the reduction chain, or attempt to looks through And's to find a
1147 // smaller bitwidth. Subs are also currently not allowed (which are usually
1148 // treated as part of a add reduction) as they are expected to generally be
1149 // more expensive than out-of-loop reductions, and need to be costed more
1151 unsigned ExpectedUses
= 1;
1152 if (RedOp
== Instruction::ICmp
|| RedOp
== Instruction::FCmp
)
1155 auto getNextInstruction
= [&](Instruction
*Cur
) -> Instruction
* {
1156 for (auto *User
: Cur
->users()) {
1157 Instruction
*UI
= cast
<Instruction
>(User
);
1158 if (isa
<PHINode
>(UI
))
1160 if (RedOp
== Instruction::ICmp
|| RedOp
== Instruction::FCmp
) {
1161 // We are expecting a icmp/select pair, which we go to the next select
1162 // instruction if we can. We already know that Cur has 2 uses.
1163 if (isa
<SelectInst
>(UI
))
1171 auto isCorrectOpcode
= [&](Instruction
*Cur
) {
1172 if (RedOp
== Instruction::ICmp
|| RedOp
== Instruction::FCmp
) {
1174 return SelectPatternResult::isMinOrMax(
1175 matchSelectPattern(Cur
, LHS
, RHS
).Flavor
);
1177 // Recognize a call to the llvm.fmuladd intrinsic.
1178 if (isFMulAddIntrinsic(Cur
))
1181 return Cur
->getOpcode() == RedOp
;
1184 // Attempt to look through Phis which are part of the reduction chain
1185 unsigned ExtraPhiUses
= 0;
1186 Instruction
*RdxInstr
= LoopExitInstr
;
1187 if (auto ExitPhi
= dyn_cast
<PHINode
>(LoopExitInstr
)) {
1188 if (ExitPhi
->getNumIncomingValues() != 2)
1191 Instruction
*Inc0
= dyn_cast
<Instruction
>(ExitPhi
->getIncomingValue(0));
1192 Instruction
*Inc1
= dyn_cast
<Instruction
>(ExitPhi
->getIncomingValue(1));
1194 Instruction
*Chain
= nullptr;
1197 else if (Inc1
== Phi
)
1206 // The loop exit instruction we check first (as a quick test) but add last. We
1207 // check the opcode is correct (and dont allow them to be Subs) and that they
1208 // have expected to have the expected number of uses. They will have one use
1209 // from the phi and one from a LCSSA value, no matter the type.
1210 if (!isCorrectOpcode(RdxInstr
) || !LoopExitInstr
->hasNUses(2))
1213 // Check that the Phi has one (or two for min/max) uses, plus an extra use
1214 // for conditional reductions.
1215 if (!Phi
->hasNUses(ExpectedUses
+ ExtraPhiUses
))
1218 Instruction
*Cur
= getNextInstruction(Phi
);
1220 // Each other instruction in the chain should have the expected number of uses
1221 // and be the correct opcode.
1222 while (Cur
!= RdxInstr
) {
1223 if (!Cur
|| !isCorrectOpcode(Cur
) || !Cur
->hasNUses(ExpectedUses
))
1226 ReductionOperations
.push_back(Cur
);
1227 Cur
= getNextInstruction(Cur
);
1230 ReductionOperations
.push_back(Cur
);
1231 return ReductionOperations
;
1234 InductionDescriptor::InductionDescriptor(Value
*Start
, InductionKind K
,
1235 const SCEV
*Step
, BinaryOperator
*BOp
,
1236 SmallVectorImpl
<Instruction
*> *Casts
)
1237 : StartValue(Start
), IK(K
), Step(Step
), InductionBinOp(BOp
) {
1238 assert(IK
!= IK_NoInduction
&& "Not an induction");
1240 // Start value type should match the induction kind and the value
1241 // itself should not be null.
1242 assert(StartValue
&& "StartValue is null");
1243 assert((IK
!= IK_PtrInduction
|| StartValue
->getType()->isPointerTy()) &&
1244 "StartValue is not a pointer for pointer induction");
1245 assert((IK
!= IK_IntInduction
|| StartValue
->getType()->isIntegerTy()) &&
1246 "StartValue is not an integer for integer induction");
1248 // Check the Step Value. It should be non-zero integer value.
1249 assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) &&
1250 "Step value is zero");
1252 assert((IK
== IK_FpInduction
|| Step
->getType()->isIntegerTy()) &&
1253 "StepValue is not an integer");
1255 assert((IK
!= IK_FpInduction
|| Step
->getType()->isFloatingPointTy()) &&
1256 "StepValue is not FP for FpInduction");
1257 assert((IK
!= IK_FpInduction
||
1259 (InductionBinOp
->getOpcode() == Instruction::FAdd
||
1260 InductionBinOp
->getOpcode() == Instruction::FSub
))) &&
1261 "Binary opcode should be specified for FP induction");
1264 for (auto &Inst
: *Casts
) {
1265 RedundantCasts
.push_back(Inst
);
1270 ConstantInt
*InductionDescriptor::getConstIntStepValue() const {
1271 if (isa
<SCEVConstant
>(Step
))
1272 return dyn_cast
<ConstantInt
>(cast
<SCEVConstant
>(Step
)->getValue());
1276 bool InductionDescriptor::isFPInductionPHI(PHINode
*Phi
, const Loop
*TheLoop
,
1277 ScalarEvolution
*SE
,
1278 InductionDescriptor
&D
) {
1280 // Here we only handle FP induction variables.
1281 assert(Phi
->getType()->isFloatingPointTy() && "Unexpected Phi type");
1283 if (TheLoop
->getHeader() != Phi
->getParent())
1286 // The loop may have multiple entrances or multiple exits; we can analyze
1287 // this phi if it has a unique entry value and a unique backedge value.
1288 if (Phi
->getNumIncomingValues() != 2)
1290 Value
*BEValue
= nullptr, *StartValue
= nullptr;
1291 if (TheLoop
->contains(Phi
->getIncomingBlock(0))) {
1292 BEValue
= Phi
->getIncomingValue(0);
1293 StartValue
= Phi
->getIncomingValue(1);
1295 assert(TheLoop
->contains(Phi
->getIncomingBlock(1)) &&
1296 "Unexpected Phi node in the loop");
1297 BEValue
= Phi
->getIncomingValue(1);
1298 StartValue
= Phi
->getIncomingValue(0);
1301 BinaryOperator
*BOp
= dyn_cast
<BinaryOperator
>(BEValue
);
1305 Value
*Addend
= nullptr;
1306 if (BOp
->getOpcode() == Instruction::FAdd
) {
1307 if (BOp
->getOperand(0) == Phi
)
1308 Addend
= BOp
->getOperand(1);
1309 else if (BOp
->getOperand(1) == Phi
)
1310 Addend
= BOp
->getOperand(0);
1311 } else if (BOp
->getOpcode() == Instruction::FSub
)
1312 if (BOp
->getOperand(0) == Phi
)
1313 Addend
= BOp
->getOperand(1);
1318 // The addend should be loop invariant
1319 if (auto *I
= dyn_cast
<Instruction
>(Addend
))
1320 if (TheLoop
->contains(I
))
1323 // FP Step has unknown SCEV
1324 const SCEV
*Step
= SE
->getUnknown(Addend
);
1325 D
= InductionDescriptor(StartValue
, IK_FpInduction
, Step
, BOp
);
1329 /// This function is called when we suspect that the update-chain of a phi node
1330 /// (whose symbolic SCEV expression sin \p PhiScev) contains redundant casts,
1331 /// that can be ignored. (This can happen when the PSCEV rewriter adds a runtime
1332 /// predicate P under which the SCEV expression for the phi can be the
1333 /// AddRecurrence \p AR; See createAddRecFromPHIWithCast). We want to find the
1334 /// cast instructions that are involved in the update-chain of this induction.
1335 /// A caller that adds the required runtime predicate can be free to drop these
1336 /// cast instructions, and compute the phi using \p AR (instead of some scev
1337 /// expression with casts).
1339 /// For example, without a predicate the scev expression can take the following
1341 /// (Ext ix (Trunc iy ( Start + i*Step ) to ix) to iy)
1343 /// It corresponds to the following IR sequence:
1345 /// %x = phi i64 [ 0, %ph ], [ %add, %for.body ]
1346 /// %casted_phi = "ExtTrunc i64 %x"
1347 /// %add = add i64 %casted_phi, %step
1349 /// where %x is given in \p PN,
1350 /// PSE.getSCEV(%x) is equal to PSE.getSCEV(%casted_phi) under a predicate,
1351 /// and the IR sequence that "ExtTrunc i64 %x" represents can take one of
1352 /// several forms, for example, such as:
1353 /// ExtTrunc1: %casted_phi = and %x, 2^n-1
1355 /// ExtTrunc2: %t = shl %x, m
1356 /// %casted_phi = ashr %t, m
1358 /// If we are able to find such sequence, we return the instructions
1359 /// we found, namely %casted_phi and the instructions on its use-def chain up
1360 /// to the phi (not including the phi).
1361 static bool getCastsForInductionPHI(PredicatedScalarEvolution
&PSE
,
1362 const SCEVUnknown
*PhiScev
,
1363 const SCEVAddRecExpr
*AR
,
1364 SmallVectorImpl
<Instruction
*> &CastInsts
) {
1366 assert(CastInsts
.empty() && "CastInsts is expected to be empty.");
1367 auto *PN
= cast
<PHINode
>(PhiScev
->getValue());
1368 assert(PSE
.getSCEV(PN
) == AR
&& "Unexpected phi node SCEV expression");
1369 const Loop
*L
= AR
->getLoop();
1371 // Find any cast instructions that participate in the def-use chain of
1372 // PhiScev in the loop.
1373 // FORNOW/TODO: We currently expect the def-use chain to include only
1374 // two-operand instructions, where one of the operands is an invariant.
1375 // createAddRecFromPHIWithCasts() currently does not support anything more
1376 // involved than that, so we keep the search simple. This can be
1377 // extended/generalized as needed.
1379 auto getDef
= [&](const Value
*Val
) -> Value
* {
1380 const BinaryOperator
*BinOp
= dyn_cast
<BinaryOperator
>(Val
);
1383 Value
*Op0
= BinOp
->getOperand(0);
1384 Value
*Op1
= BinOp
->getOperand(1);
1385 Value
*Def
= nullptr;
1386 if (L
->isLoopInvariant(Op0
))
1388 else if (L
->isLoopInvariant(Op1
))
1393 // Look for the instruction that defines the induction via the
1395 BasicBlock
*Latch
= L
->getLoopLatch();
1398 Value
*Val
= PN
->getIncomingValueForBlock(Latch
);
1402 // Follow the def-use chain until the induction phi is reached.
1403 // If on the way we encounter a Value that has the same SCEV Expr as the
1404 // phi node, we can consider the instructions we visit from that point
1405 // as part of the cast-sequence that can be ignored.
1406 bool InCastSequence
= false;
1407 auto *Inst
= dyn_cast
<Instruction
>(Val
);
1409 // If we encountered a phi node other than PN, or if we left the loop,
1411 if (!Inst
|| !L
->contains(Inst
)) {
1414 auto *AddRec
= dyn_cast
<SCEVAddRecExpr
>(PSE
.getSCEV(Val
));
1415 if (AddRec
&& PSE
.areAddRecsEqualWithPreds(AddRec
, AR
))
1416 InCastSequence
= true;
1417 if (InCastSequence
) {
1418 // Only the last instruction in the cast sequence is expected to have
1419 // uses outside the induction def-use chain.
1420 if (!CastInsts
.empty())
1421 if (!Inst
->hasOneUse())
1423 CastInsts
.push_back(Inst
);
1428 Inst
= dyn_cast
<Instruction
>(Val
);
1431 return InCastSequence
;
1434 bool InductionDescriptor::isInductionPHI(PHINode
*Phi
, const Loop
*TheLoop
,
1435 PredicatedScalarEvolution
&PSE
,
1436 InductionDescriptor
&D
, bool Assume
) {
1437 Type
*PhiTy
= Phi
->getType();
1439 // Handle integer and pointer inductions variables.
1440 // Now we handle also FP induction but not trying to make a
1441 // recurrent expression from the PHI node in-place.
1443 if (!PhiTy
->isIntegerTy() && !PhiTy
->isPointerTy() && !PhiTy
->isFloatTy() &&
1444 !PhiTy
->isDoubleTy() && !PhiTy
->isHalfTy())
1447 if (PhiTy
->isFloatingPointTy())
1448 return isFPInductionPHI(Phi
, TheLoop
, PSE
.getSE(), D
);
1450 const SCEV
*PhiScev
= PSE
.getSCEV(Phi
);
1451 const auto *AR
= dyn_cast
<SCEVAddRecExpr
>(PhiScev
);
1453 // We need this expression to be an AddRecExpr.
1455 AR
= PSE
.getAsAddRec(Phi
);
1458 LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
1462 // Record any Cast instructions that participate in the induction update
1463 const auto *SymbolicPhi
= dyn_cast
<SCEVUnknown
>(PhiScev
);
1464 // If we started from an UnknownSCEV, and managed to build an addRecurrence
1465 // only after enabling Assume with PSCEV, this means we may have encountered
1466 // cast instructions that required adding a runtime check in order to
1467 // guarantee the correctness of the AddRecurrence respresentation of the
1469 if (PhiScev
!= AR
&& SymbolicPhi
) {
1470 SmallVector
<Instruction
*, 2> Casts
;
1471 if (getCastsForInductionPHI(PSE
, SymbolicPhi
, AR
, Casts
))
1472 return isInductionPHI(Phi
, TheLoop
, PSE
.getSE(), D
, AR
, &Casts
);
1475 return isInductionPHI(Phi
, TheLoop
, PSE
.getSE(), D
, AR
);
1478 bool InductionDescriptor::isInductionPHI(
1479 PHINode
*Phi
, const Loop
*TheLoop
, ScalarEvolution
*SE
,
1480 InductionDescriptor
&D
, const SCEV
*Expr
,
1481 SmallVectorImpl
<Instruction
*> *CastsToIgnore
) {
1482 Type
*PhiTy
= Phi
->getType();
1483 // We only handle integer and pointer inductions variables.
1484 if (!PhiTy
->isIntegerTy() && !PhiTy
->isPointerTy())
1487 // Check that the PHI is consecutive.
1488 const SCEV
*PhiScev
= Expr
? Expr
: SE
->getSCEV(Phi
);
1489 const SCEVAddRecExpr
*AR
= dyn_cast
<SCEVAddRecExpr
>(PhiScev
);
1492 LLVM_DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
1496 if (AR
->getLoop() != TheLoop
) {
1497 // FIXME: We should treat this as a uniform. Unfortunately, we
1498 // don't currently know how to handled uniform PHIs.
1500 dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n");
1504 // This function assumes that InductionPhi is called only on Phi nodes
1505 // present inside loop headers. Check for the same, and throw an assert if
1506 // the current Phi is not present inside the loop header.
1507 assert(Phi
->getParent() == AR
->getLoop()->getHeader()
1508 && "Invalid Phi node, not present in loop header");
1511 Phi
->getIncomingValueForBlock(AR
->getLoop()->getLoopPreheader());
1513 BasicBlock
*Latch
= AR
->getLoop()->getLoopLatch();
1517 const SCEV
*Step
= AR
->getStepRecurrence(*SE
);
1518 // Calculate the pointer stride and check if it is consecutive.
1519 // The stride may be a constant or a loop invariant integer value.
1520 const SCEVConstant
*ConstStep
= dyn_cast
<SCEVConstant
>(Step
);
1521 if (!ConstStep
&& !SE
->isLoopInvariant(Step
, TheLoop
))
1524 if (PhiTy
->isIntegerTy()) {
1525 BinaryOperator
*BOp
=
1526 dyn_cast
<BinaryOperator
>(Phi
->getIncomingValueForBlock(Latch
));
1527 D
= InductionDescriptor(StartValue
, IK_IntInduction
, Step
, BOp
,
1532 assert(PhiTy
->isPointerTy() && "The PHI must be a pointer");
1534 // This allows induction variables w/non-constant steps.
1535 D
= InductionDescriptor(StartValue
, IK_PtrInduction
, Step
);