Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions llvm/include/llvm/Analysis/IVDescriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum class RecurKind {
FMinimumNum, ///< FP min with llvm.minimumnum semantics
FMaximumNum, ///< FP max with llvm.maximumnum semantics
FMulAdd, ///< Sum of float products with llvm.fmuladd(a * b + sum).
AnyOf, ///< AnyOf reduction with select(cmp(),x,y) where one of (x,y) is
AnyOf, ///< AnyOf reduction with select(cond,x,y) where one of (x,y) is
///< loop invariant, and both x and y are integer type.
FindIV, ///< FindIV reduction with select(icmp(),x,y) where one of (x,y) is
///< a loop induction variable (increasing or decreasing), and both
Expand Down Expand Up @@ -168,9 +168,8 @@ class RecurrenceDescriptor {
LLVM_ABI static bool areAllUsesIn(Instruction *I,
SmallPtrSetImpl<Instruction *> &Set);

/// Returns a struct describing whether the instruction is either a
/// Select(ICmp(A, B), X, Y), or
/// Select(FCmp(A, B), X, Y)
/// Returns a struct describing whether the instruction is a
/// Select(Cond, X, Y)
/// where one of (X, Y) is a loop invariant integer and the other is a PHI
/// value. \p Prev specifies the description of an already processed select
/// instruction, so its corresponding cmp can be matched to it.
Expand Down Expand Up @@ -277,7 +276,7 @@ class RecurrenceDescriptor {
}

/// Returns true if the recurrence kind is of the form
/// select(cmp(),x,y) where one of (x,y) is loop invariant.
/// select(cond,x,y) where one of (x,y) is loop invariant.
static bool isAnyOfRecurrenceKind(RecurKind Kind) {
return Kind == RecurKind::AnyOf;
}
Expand Down
37 changes: 26 additions & 11 deletions llvm/lib/Analysis/IVDescriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ bool RecurrenceDescriptor::AddReductionVar(
// must include the original PHI.
bool FoundStartPHI = false;

// To recognize AnyOf patterns formed by a icmp select sequence, we store
// the number of instruction we saw to make sure we only see one.
unsigned NumCmpSelectPatternInst = 0;
// To recognize AnyOf patterns formed by a select, we store the select to
// make sure we only see one.
SelectInst *AnyOfSelect = nullptr;
InstDesc ReduxDesc(false, nullptr);

// To recognize find-lasts of conditional operations (such as loads or
Expand Down Expand Up @@ -659,8 +659,11 @@ bool RecurrenceDescriptor::AddReductionVar(
}
}

if (isAnyOfRecurrenceKind(Kind) && IsASelect)
++NumCmpSelectPatternInst;
if (isAnyOfRecurrenceKind(Kind) && IsASelect) {
if (AnyOfSelect)
return false;
AnyOfSelect = cast<SelectInst>(Cur);
}

// Check whether we found a reduction operator.
FoundReduxOp |= (!IsAPhi || FoundFindLastLikePhi) && Cur != Start;
Expand Down Expand Up @@ -740,8 +743,17 @@ bool RecurrenceDescriptor::AddReductionVar(
(Kind == RecurKind::FindLast && NumNonPHIUsers == 0)) &&
"Unexpectedly matched a 'find-last-like' phi");

if (isAnyOfRecurrenceKind(Kind) && NumCmpSelectPatternInst != 1)
return false;
if (isAnyOfRecurrenceKind(Kind)) {
if (!AnyOfSelect)
return false;
// Reject a select whose condition is part of the chain: the vectorizer
// replaces the chain, so the condition would become poison. Compares are
// allowed, since the vectorizer rewrites a compare of the reduction phi
// to use the start value.
auto *Cond = dyn_cast<Instruction>(AnyOfSelect->getCondition());
if (Cond && !isa<CmpInst>(Cond) && VisitedInsts.contains(Cond))
return false;
}

if (IntermediateStore) {
// Check that stored value goes to the phi node again. This way we make sure
Expand Down Expand Up @@ -851,6 +863,9 @@ bool RecurrenceDescriptor::AddReductionVar(
// %cmp = icmp sgt i32 %5, 3
// %spec.select = select i1 %cmp, i32 3, i32 %r
// ...
// The condition can be any i1 value, including one defined outside the loop.
// If it is part of the reduction chain, it has to be a compare (see
// AddReductionVar).
// In general we can support vectorization of loops where 'r' flips between
// any two non-constants, provided they are loop invariant. The only thing
// we actually care about at the end of the loop is whether or not any lane
Expand All @@ -868,10 +883,10 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
return InstDesc(Select, Prev.getRecKind());
}

if (!match(I, m_Select(m_Cmp(), m_Value(), m_Value())))
SelectInst *SI = dyn_cast<SelectInst>(I);
if (!SI)
return InstDesc(false, I);

SelectInst *SI = cast<SelectInst>(I);
Value *NonPhi = nullptr;

if (OrigPhi == dyn_cast<PHINode>(SI->getTrueValue()))
Expand All @@ -882,8 +897,8 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
return InstDesc(false, I);

// We are looking for selects of the form:
// select(cmp(), phi, loop_invariant) or
// select(cmp(), loop_invariant, phi)
// select(cond, phi, loop_invariant) or
// select(cond, loop_invariant, phi)
if (!Loop->isLoopInvariant(NonPhi))
return InstDesc(false, I);

Expand Down
Loading