Skip to content

Commit 05e838f

Browse files
committed
[VectorCombine] foldExtractedCmps - disable fold on non-commutative binops
The fold needs to be adjusted to correctly track the LHS/RHS operands, which will take some refactoring, for now just disable the fold in this case. Fixes #114901
1 parent a88be11 commit 05e838f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,15 @@ bool VectorCombine::scalarizeBinopOrCmp(Instruction &I) {
10321032
/// a vector into vector operations followed by extract. Note: The SLP pass
10331033
/// may miss this pattern because of implementation problems.
10341034
bool VectorCombine::foldExtractedCmps(Instruction &I) {
1035+
auto *BI = dyn_cast<BinaryOperator>(&I);
1036+
10351037
// We are looking for a scalar binop of booleans.
10361038
// binop i1 (cmp Pred I0, C0), (cmp Pred I1, C1)
1037-
if (!I.isBinaryOp() || !I.getType()->isIntegerTy(1))
1039+
if (!BI || !I.getType()->isIntegerTy(1))
1040+
return false;
1041+
1042+
// TODO: Support non-commutative binary ops.
1043+
if (!BI->isCommutative())
10381044
return false;
10391045

10401046
// The compare predicates should match, and each compare should have a
@@ -1113,8 +1119,7 @@ bool VectorCombine::foldExtractedCmps(Instruction &I) {
11131119
Value *VCmp = Builder.CreateCmp(Pred, X, ConstantVector::get(CmpC));
11141120

11151121
Value *Shuf = createShiftShuffle(VCmp, ExpensiveIndex, CheapIndex, Builder);
1116-
Value *VecLogic = Builder.CreateBinOp(cast<BinaryOperator>(I).getOpcode(),
1117-
VCmp, Shuf);
1122+
Value *VecLogic = Builder.CreateBinOp(BI->getOpcode(), VCmp, Shuf);
11181123
Value *NewExt = Builder.CreateExtractElement(VecLogic, CheapIndex);
11191124
replaceValue(I, *NewExt);
11201125
++NumVecCmpBO;

llvm/test/Transforms/VectorCombine/X86/pr114901.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- -mattr=sse2 | FileCheck %s --check-prefixes=SSE
33
; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- -mattr=avx2 | FileCheck %s --check-prefixes=AVX
44

5-
; FIXME: PR114901 - ensure that the ASHR node doesn't commute the operands.
5+
; PR114901 - ensure that the ASHR node doesn't commute the operands.
66
define i1 @PR114901(<4 x i32> %a) {
77
; SSE-LABEL: define i1 @PR114901(
88
; SSE-SAME: <4 x i32> [[A:%.*]]) #[[ATTR0:[0-9]+]] {
@@ -15,10 +15,11 @@ define i1 @PR114901(<4 x i32> %a) {
1515
;
1616
; AVX-LABEL: define i1 @PR114901(
1717
; AVX-SAME: <4 x i32> [[A:%.*]]) #[[ATTR0:[0-9]+]] {
18-
; AVX-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[A]], <i32 poison, i32 -8, i32 poison, i32 42>
19-
; AVX-NEXT: [[SHIFT:%.*]] = shufflevector <4 x i1> [[TMP1]], <4 x i1> poison, <4 x i32> <i32 poison, i32 3, i32 poison, i32 poison>
20-
; AVX-NEXT: [[TMP2:%.*]] = ashr <4 x i1> [[TMP1]], [[SHIFT]]
21-
; AVX-NEXT: [[R:%.*]] = extractelement <4 x i1> [[TMP2]], i64 1
18+
; AVX-NEXT: [[E1:%.*]] = extractelement <4 x i32> [[A]], i32 1
19+
; AVX-NEXT: [[E3:%.*]] = extractelement <4 x i32> [[A]], i32 3
20+
; AVX-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[E1]], -8
21+
; AVX-NEXT: [[CMP3:%.*]] = icmp sgt i32 [[E3]], 42
22+
; AVX-NEXT: [[R:%.*]] = ashr i1 [[CMP3]], [[CMP1]]
2223
; AVX-NEXT: ret i1 [[R]]
2324
;
2425
%e1 = extractelement <4 x i32> %a, i32 1

0 commit comments

Comments
 (0)