Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[X86] combinePTESTCC - fold PTESTC(PCMPEQ(X,0),-1) == PTESTZ(X,X) #123466

Merged
merged 1 commit into from
Jan 19, 2025

Conversation

RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented Jan 18, 2025

Simplifies the hidden "all_of(X == 0)" pattern

Fixes #123456

Simplifies the hidden "all_of(X == 0)" pattern

Fixes llvm#123456
@llvmbot
Copy link
Member

llvmbot commented Jan 18, 2025

@llvm/pr-subscribers-backend-x86

Author: Simon Pilgrim (RKSimon)

Changes

Simplifies the hidden "all_of(X == 0)" pattern

Fixes #123456


Full diff: https://github.com/llvm/llvm-project/pull/123466.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+12)
  • (modified) llvm/test/CodeGen/X86/combine-ptest.ll (+9-31)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 410b08912a5e24..33ddcb57e9b08b 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -48054,6 +48054,18 @@ static SDValue combinePTESTCC(SDValue EFLAGS, X86::CondCode &CC,
                            DAG.getAllOnesConstant(DL, NotOp1.getValueType())));
       }
     }
+    // PTESTC(PCMPEQ(X,0),-1) == PTESTZ(X,X)
+    if (EFLAGS.getOpcode() == X86ISD::PTEST &&
+        ISD::isBuildVectorAllOnes(Op1.getNode())) {
+      SDValue BC0 = peekThroughBitcasts(Op0);
+      if (BC0.getOpcode() == X86ISD::PCMPEQ &&
+          ISD::isBuildVectorAllZeros(BC0.getOperand(1).getNode())) {
+        SDLoc DL(EFLAGS);
+        CC = (CC == X86::COND_B ? X86::COND_E : X86::COND_NE);
+        SDValue X = DAG.getBitcast(OpVT, BC0.getOperand(0));
+        return DAG.getNode(EFLAGS.getOpcode(), DL, VT, X, X);
+      }
+    }
   }
 
   if (CC == X86::COND_E || CC == X86::COND_NE) {
diff --git a/llvm/test/CodeGen/X86/combine-ptest.ll b/llvm/test/CodeGen/X86/combine-ptest.ll
index f5ab700caea467..fda14027e994e3 100644
--- a/llvm/test/CodeGen/X86/combine-ptest.ll
+++ b/llvm/test/CodeGen/X86/combine-ptest.ll
@@ -376,20 +376,14 @@ define i32 @ptestz_v2i64_concat(<4 x i64> %c, <4 x i64> %d, i32 %a, i32 %b) {
 define i1 @ptestc_v4i32_eq0(<4 x i32> %a0) {
 ; SSE-LABEL: ptestc_v4i32_eq0:
 ; SSE:       # %bb.0:
-; SSE-NEXT:    pxor %xmm1, %xmm1
-; SSE-NEXT:    pcmpeqd %xmm0, %xmm1
-; SSE-NEXT:    pcmpeqd %xmm0, %xmm0
-; SSE-NEXT:    ptest %xmm0, %xmm1
-; SSE-NEXT:    setb %al
+; SSE-NEXT:    ptest %xmm0, %xmm0
+; SSE-NEXT:    sete %al
 ; SSE-NEXT:    retq
 ;
 ; AVX-LABEL: ptestc_v4i32_eq0:
 ; AVX:       # %bb.0:
-; AVX-NEXT:    vpxor %xmm1, %xmm1, %xmm1
-; AVX-NEXT:    vpcmpeqd %xmm1, %xmm0, %xmm0
-; AVX-NEXT:    vpcmpeqd %xmm1, %xmm1, %xmm1
-; AVX-NEXT:    vptest %xmm1, %xmm0
-; AVX-NEXT:    setb %al
+; AVX-NEXT:    vptest %xmm0, %xmm0
+; AVX-NEXT:    sete %al
 ; AVX-NEXT:    retq
   %icmp = icmp eq <4 x i32> %a0, zeroinitializer
   %sext = sext <4 x i1> %icmp to <4 x i32>
@@ -403,22 +397,14 @@ define i1 @ptestc_v4i32_eq0(<4 x i32> %a0) {
 define i1 @ptestc_v4i32_and_eq0(<4 x i32> %a0, <4 x i32> %a1) {
 ; SSE-LABEL: ptestc_v4i32_and_eq0:
 ; SSE:       # %bb.0:
-; SSE-NEXT:    pand %xmm1, %xmm0
-; SSE-NEXT:    pxor %xmm1, %xmm1
-; SSE-NEXT:    pcmpeqd %xmm0, %xmm1
-; SSE-NEXT:    pcmpeqd %xmm0, %xmm0
 ; SSE-NEXT:    ptest %xmm0, %xmm1
-; SSE-NEXT:    setb %al
+; SSE-NEXT:    sete %al
 ; SSE-NEXT:    retq
 ;
 ; AVX-LABEL: ptestc_v4i32_and_eq0:
 ; AVX:       # %bb.0:
-; AVX-NEXT:    vpand %xmm0, %xmm1, %xmm0
-; AVX-NEXT:    vpxor %xmm1, %xmm1, %xmm1
-; AVX-NEXT:    vpcmpeqd %xmm1, %xmm0, %xmm0
-; AVX-NEXT:    vpcmpeqd %xmm1, %xmm1, %xmm1
-; AVX-NEXT:    vptest %xmm1, %xmm0
-; AVX-NEXT:    setb %al
+; AVX-NEXT:    vptest %xmm0, %xmm1
+; AVX-NEXT:    sete %al
 ; AVX-NEXT:    retq
   %and = and <4 x i32> %a1, %a0
   %icmp = icmp eq <4 x i32> %and, zeroinitializer
@@ -433,21 +419,13 @@ define i1 @ptestc_v4i32_and_eq0(<4 x i32> %a0, <4 x i32> %a1) {
 define i1 @ptestc_v4i32_andnot_eq0(<4 x i32> %a0, <4 x i32> %a1) {
 ; SSE-LABEL: ptestc_v4i32_andnot_eq0:
 ; SSE:       # %bb.0:
-; SSE-NEXT:    pandn %xmm0, %xmm1
-; SSE-NEXT:    pxor %xmm0, %xmm0
-; SSE-NEXT:    pcmpeqd %xmm1, %xmm0
-; SSE-NEXT:    pcmpeqd %xmm1, %xmm1
-; SSE-NEXT:    ptest %xmm1, %xmm0
+; SSE-NEXT:    ptest %xmm0, %xmm1
 ; SSE-NEXT:    setae %al
 ; SSE-NEXT:    retq
 ;
 ; AVX-LABEL: ptestc_v4i32_andnot_eq0:
 ; AVX:       # %bb.0:
-; AVX-NEXT:    vpandn %xmm0, %xmm1, %xmm0
-; AVX-NEXT:    vpxor %xmm1, %xmm1, %xmm1
-; AVX-NEXT:    vpcmpeqd %xmm1, %xmm0, %xmm0
-; AVX-NEXT:    vpcmpeqd %xmm1, %xmm1, %xmm1
-; AVX-NEXT:    vptest %xmm1, %xmm0
+; AVX-NEXT:    vptest %xmm0, %xmm1
 ; AVX-NEXT:    setae %al
 ; AVX-NEXT:    retq
   %not = xor <4 x i32> %a1, splat (i32 -1)

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@RKSimon RKSimon merged commit 6adeda8 into llvm:main Jan 19, 2025
10 checks passed
@RKSimon RKSimon deleted the x86-combine-ptest-cmp0 branch January 19, 2025 13:09
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 19, 2025

LLVM Buildbot has detected a new failure on builder clang-armv7-global-isel running on linaro-clang-armv7-global-isel while building llvm at step 13 "test-suite".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/39/builds/4025

Here is the relevant piece of the build log for the reference
Step 13 (test-suite) failure: test (failure)
...
**********
*** MICRO-TEST: BENCHMARK_DILATE/1024
    exec_time:  71420.3200 
*** MICRO-TEST: BENCHMARK_DILATE/128
    exec_time:  1114.9104 
*** MICRO-TEST: BENCHMARK_DILATE/256
    exec_time:  4465.4698 
*** MICRO-TEST: BENCHMARK_DILATE/512
    exec_time:  17843.2092 
NOEXE: test-suite :: MultiSource/Benchmarks/ASCI_Purple/SMG2000/smg2000.test (1405 of 3429)
******************** TEST 'test-suite :: MultiSource/Benchmarks/ASCI_Purple/SMG2000/smg2000.test' FAILED ********************
Executable '/home/tcwg-buildbot/worker/clang-armv7-global-isel/test/sandbox/build/MultiSource/Benchmarks/ASCI_Purple/SMG2000/smg2000' is missing
********************
PASS: test-suite :: MicroBenchmarks/ImageProcessing/Interpolation/Interpolation.test (1406 of 3429)
********** TEST 'test-suite :: MicroBenchmarks/ImageProcessing/Interpolation/Interpolation.test' RESULTS **********
MicroBenchmarks: 10 
compile_time: 2.0564 
hash: "abb4e85688cbacfc56d6364550c6aa7f" 
link_time: 0.3840 
size: 1260456 
size..ARM.attributes: 65 
size..ARM.exidx: 18456 
size..ARM.extab: 20264 
size..bss: 1968 
size..comment: 151 
size..data: 520 
size..data.rel.ro: 644 
size..dynamic: 264 
size..dynstr: 8934 
size..dynsym: 3760 
size..eh_frame: 4 
size..fini: 8 
size..fini_array: 4 
size..gnu.hash: 48 
size..gnu.version: 470 
size..gnu.version_r: 384 
size..got: 1004 
size..init: 12 
size..init_array: 88 
size..interp: 25 
size..note.ABI-tag: 32 
size..plt: 2468 
size..rel.dyn: 2664 
size..rel.plt: 1632 
size..rodata: 9116 
size..text: 535368 
**********
*** MICRO-TEST: BENCHMARK_BICUBIC_INTERPOLATION/128
    exec_time:  35537.9420 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants