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
114 changes: 87 additions & 27 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4869,6 +4869,8 @@ class BoUpSLP {
assert(hasValidDependencies() &&
"increment of unscheduled deps would be meaningless");
UnscheduledDeps += Incr;
assert(UnscheduledDeps >= 0 &&
"Expected valid number of unscheduled deps");
return UnscheduledDeps;
}

Expand Down Expand Up @@ -5331,6 +5333,28 @@ class BoUpSLP {
// Check all tree entries, if they have operands replaced by copyable
// data.
for (TreeEntry *TE : Entries) {
unsigned Inc = 0;
bool IsNonSchedulableWithParentPhiNode =
TE->doesNotNeedToSchedule() && TE->UserTreeIndex &&
TE->UserTreeIndex.UserTE->hasState() &&
TE->UserTreeIndex.UserTE->getOpcode() == Instruction::PHI;
// Count the number of unique phi nodes, which are the parent for
// parent entry, and exit, if all the unique phis are processed.
if (IsNonSchedulableWithParentPhiNode) {
SmallPtrSet<Value *, 4> ParentsUniqueUsers;
const TreeEntry *ParentTE = TE->UserTreeIndex.UserTE;
for (Value *V : ParentTE->Scalars) {
auto *PHI = dyn_cast<PHINode>(V);
if (!PHI)
continue;
if (ParentsUniqueUsers.insert(PHI).second &&
is_contained(PHI->incoming_values(), User))
++Inc;
}
} else {
Inc = 1;
}

// Check if the user is commutative.
// The commutatives are handled later, as their operands can be
// reordered.
Expand All @@ -5346,11 +5370,11 @@ class BoUpSLP {
if (!getScheduleCopyableData(EI, Op))
continue;
// Found copyable operand - continue.
++OpCnt;
OpCnt += Inc;
continue;
}
++PotentiallyReorderedEntriesCount.try_emplace(TE, 0)
.first->getSecond();
PotentiallyReorderedEntriesCount.try_emplace(TE, 0)
.first->getSecond() += Inc;
}
}
if (PotentiallyReorderedEntriesCount.empty())
Expand All @@ -5360,21 +5384,44 @@ class BoUpSLP {
});
// Check the commutative/cmp entries.
for (auto &P : PotentiallyReorderedEntriesCount) {
SmallPtrSet<Value *, 4> ParentsUniqueUsers;
bool IsNonSchedulableWithParentPhiNode =
P.first->doesNotNeedToSchedule() && P.first->UserTreeIndex &&
P.first->UserTreeIndex.UserTE->hasState() &&
P.first->UserTreeIndex.UserTE->getOpcode() == Instruction::PHI;
auto *It = find(P.first->Scalars, User);
assert(It != P.first->Scalars.end() && "User is not in the tree entry");
int Lane = std::distance(P.first->Scalars.begin(), It);
assert(Lane >= 0 && "Lane is not found");
if (isa<StoreInst>(User) && !P.first->ReorderIndices.empty())
Lane = P.first->ReorderIndices[Lane];
assert(Lane < static_cast<int>(P.first->Scalars.size()) &&
"Couldn't find extract lane");
for (unsigned OpIdx :
seq<unsigned>(::getNumberOfPotentiallyCommutativeOps(
P.first->getMainOp()))) {
if (P.first->getOperand(OpIdx)[Lane] == Op &&
getScheduleCopyableData(EdgeInfo(P.first, OpIdx), Op))
--P.getSecond();
}
do {
assert(It != P.first->Scalars.end() &&
"User is not in the tree entry");
int Lane = std::distance(P.first->Scalars.begin(), It);
assert(Lane >= 0 && "Lane is not found");
if (isa<StoreInst>(User) && !P.first->ReorderIndices.empty())
Lane = P.first->ReorderIndices[Lane];
assert(Lane < static_cast<int>(P.first->Scalars.size()) &&
"Couldn't find extract lane");
// Count the number of unique phi nodes, which are the parent for
// parent entry, and exit, if all the unique phis are processed.
if (IsNonSchedulableWithParentPhiNode) {
const TreeEntry *ParentTE = P.first->UserTreeIndex.UserTE;
Value *User = ParentTE->Scalars[Lane];
if (!ParentsUniqueUsers.insert(User).second) {
It =
find(make_range(std::next(It), P.first->Scalars.end()), User);
continue;
}
}
for (unsigned OpIdx :
seq<unsigned>(::getNumberOfPotentiallyCommutativeOps(
P.first->getMainOp()))) {
if (P.first->getOperand(OpIdx)[Lane] == Op &&
getScheduleCopyableData(EdgeInfo(P.first, OpIdx), Op))
--P.getSecond();
}
// If parent node is schedulable, it will be handled correctly.
if (!IsNonSchedulableWithParentPhiNode)
break;
It = find(make_range(std::next(It), P.first->Scalars.end()), User);
} while (It != P.first->Scalars.end());
}
return all_of(PotentiallyReorderedEntriesCount,
[&](const std::pair<const TreeEntry *, unsigned> &P) {
Expand Down Expand Up @@ -5648,8 +5695,11 @@ class BoUpSLP {
const TreeEntry *ParentTE =
Bundle->getTreeEntry()->UserTreeIndex.UserTE;
Value *User = ParentTE->Scalars[Lane];
if (!ParentsUniqueUsers.insert(User).second)
break;
if (!ParentsUniqueUsers.insert(User).second) {
It = std::find(std::next(It),
Bundle->getTreeEntry()->Scalars.end(), In);
continue;
}
}

for (unsigned OpIdx :
Expand Down Expand Up @@ -10745,10 +10795,11 @@ class InstructionsCompatibilityAnalysis {
/// Checks if the opcode is supported as the main opcode for copyable
/// elements.
static bool isSupportedOpcode(const unsigned Opcode) {
return Opcode == Instruction::Add || Opcode == Instruction::LShr ||
Opcode == Instruction::Shl || Opcode == Instruction::SDiv ||
Opcode == Instruction::UDiv || Opcode == Instruction::And ||
Opcode == Instruction::Or || Opcode == Instruction::Xor;
return Opcode == Instruction::Add || Opcode == Instruction::Sub ||
Opcode == Instruction::LShr || Opcode == Instruction::Shl ||
Opcode == Instruction::SDiv || Opcode == Instruction::UDiv ||
Opcode == Instruction::And || Opcode == Instruction::Or ||
Opcode == Instruction::Xor;
}

/// Identifies the best candidate value, which represents main opcode
Expand Down Expand Up @@ -10808,8 +10859,12 @@ class InstructionsCompatibilityAnalysis {
for (const auto &P : Candidates) {
if (P.second.size() < BestOpcodeNum)
continue;
// If have inner dependencies - skip.
if (any_of(P.second,
[&](Instruction *I) { return Operands.contains(I); }))
continue;
for (Instruction *I : P.second) {
if (IsSupportedInstruction(I, AnyUndef) && !Operands.contains(I)) {
if (IsSupportedInstruction(I, AnyUndef)) {
MainOp = I;
BestOpcodeNum = P.second.size();
break;
Expand Down Expand Up @@ -11069,6 +11124,7 @@ class InstructionsCompatibilityAnalysis {
getWidenedType(S.getMainOp()->getType(), VL.size());
switch (MainOpcode) {
case Instruction::Add:
case Instruction::Sub:
case Instruction::LShr:
case Instruction::Shl:
case Instruction::SDiv:
Expand Down Expand Up @@ -19670,8 +19726,10 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
V = ::propagateMetadata(I, E->Scalars);
// Drop nuw flags for abs(sub(commutative), true).
if (!MinBWs.contains(E) && ShuffleOrOp == Instruction::Sub &&
any_of(E->Scalars, [](Value *V) {
return isa<PoisonValue>(V) || isCommutative(cast<Instruction>(V));
any_of(E->Scalars, [E](Value *V) {
return isa<PoisonValue>(V) ||
(E->hasCopyableElements() && E->isCopyableElement(V)) ||
isCommutative(cast<Instruction>(V));
}))
I->setHasNoUnsignedWrap(/*b=*/false);
}
Expand Down Expand Up @@ -20075,9 +20133,11 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
// Drop nuw flags for abs(sub(commutative), true).
if (auto *I = dyn_cast<Instruction>(Vec);
I && Opcode == Instruction::Sub && !MinBWs.contains(E) &&
any_of(E->Scalars, [](Value *V) {
any_of(E->Scalars, [E](Value *V) {
if (isa<PoisonValue>(V))
return false;
if (E->hasCopyableElements() && E->isCopyableElement(V))
return false;
auto *IV = cast<Instruction>(V);
return IV->getOpcode() == Instruction::Sub && isCommutative(IV);
}))
Expand Down
11 changes: 2 additions & 9 deletions llvm/test/Transforms/SLPVectorizer/X86/minbw-node-used-twice.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@
define i8 @test() {
; CHECK-LABEL: define i8 @test() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[SUB_I_I79_PEEL_I:%.*]] = sub i16 0, 1
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i16> <i16 poison, i16 0>, i16 [[SUB_I_I79_PEEL_I]], i32 0
; CHECK-NEXT: [[TMP2:%.*]] = zext <2 x i16> [[TMP0]] to <2 x i32>
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> zeroinitializer, [[TMP2]]
; CHECK-NEXT: [[TMP3:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i16>
; CHECK-NEXT: [[TMP4:%.*]] = or <2 x i16> [[TMP3]], [[TMP0]]
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq <2 x i16> [[TMP4]], [[TMP0]]
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i1> [[TMP1]], i32 0
; CHECK-NEXT: [[CONV13_I89_PEEL_I:%.*]] = zext i1 [[TMP5]] to i8
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq <2 x i16> <i16 -1, i16 0>, <i16 -1, i16 0>
; CHECK-NEXT: [[CONV13_I89_PEEL_I:%.*]] = zext i1 false to i8
; CHECK-NEXT: ret i8 [[CONV13_I89_PEEL_I]]
;
entry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ define void @test() {
; CHECK-NEXT: [[BB:.*]]:
; CHECK-NEXT: br i1 false, label %[[BB1:.*]], label %[[BB6:.*]]
; CHECK: [[BB1]]:
; CHECK-NEXT: [[TMP5:%.*]] = insertelement <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>, i32 -1, i32 2
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <4 x i32> [[TMP5]], i32 0, i32 3
; CHECK-NEXT: [[TMP2:%.*]] = add <4 x i32> <i32 0, i32 0, i32 0, i32 -1>, [[TMP6]]
; CHECK-NEXT: [[TMP3:%.*]] = ashr <4 x i32> <i32 0, i32 0, i32 0, i32 -1>, [[TMP6]]
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <4 x i32> [[TMP2]], <4 x i32> [[TMP3]], <4 x i32> <i32 0, i32 5, i32 2, i32 3>
; CHECK-NEXT: br label %[[BB6]]
; CHECK: [[BB6]]:
; CHECK-NEXT: [[TMP0:%.*]] = phi <4 x i32> [ <i32 0, i32 0, i32 poison, i32 0>, %[[BB]] ], [ <i32 0, i32 0, i32 -1, i32 -1>, %[[BB1]] ]
; CHECK-NEXT: [[TMP0:%.*]] = phi <4 x i32> [ <i32 0, i32 0, i32 poison, i32 0>, %[[BB]] ], [ [[TMP4]], %[[BB1]] ]
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i32> [[TMP0]], i32 3
; CHECK-NEXT: [[OR:%.*]] = or i32 [[TMP1]], 0
; CHECK-NEXT: ret void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ define void @addsub1(ptr noalias %dst, ptr noalias %src) {
; CHECK-LABEL: @addsub1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = load <4 x i32>, ptr [[SRC:%.*]], align 4
; CHECK-NEXT: [[TMP1:%.*]] = add nsw <4 x i32> [[TMP0]], <i32 -1, i32 1, i32 0, i32 3>
; CHECK-NEXT: [[TMP1:%.*]] = sub nsw <4 x i32> [[TMP0]], <i32 1, i32 -1, i32 0, i32 -3>
; CHECK-NEXT: store <4 x i32> [[TMP1]], ptr [[DST:%.*]], align 4
; CHECK-NEXT: ret void
;
Expand Down
Loading