i386: Improve vector [GL]E{,U} comparison against vector constants [PR107546]
commitfa271afb58423014e2feef9f15c1a87428e64ddc
authorJakub Jelinek <jakub@redhat.com>
Tue, 8 Nov 2022 11:21:55 +0000 (8 12:21 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 8 Nov 2022 11:21:55 +0000 (8 12:21 +0100)
tree15c51f88c30653991ae0bd1d4a3747e08d660cb4
parentee86bdd1d367bc174d7b50bd2ffa5622c4766322
i386: Improve vector [GL]E{,U} comparison against vector constants [PR107546]

For integer vector comparisons without XOP before AVX512{F,VL} we are
constrained by only GT and EQ being supported in HW.
For GTU we play tricks to implement it using GT or unsigned saturating
subtraction, for LT/LTU we swap the operands and thus turn it into
GT/GTU.  For LE/LEU we handle it by using GT/GTU and negating the
result and for GE/GEU by using GT/GTU on swapped operands and negating
the result.
If the second operand is a CONST_VECTOR, we can usually do better though,
we can avoid the negation.  For LE/LEU cst by doing LT/LTU cst+1 (and
then cst+1 GT/GTU x) and for GE/GEU cst by doing GT/GTU cst-1, provided
there is no wrap-around on those cst+1 or cst-1.
GIMPLE canonicalizes x < cst to x <= cst-1 etc. (the rule is smaller
absolute value on constant), but only for scalars or uniform vectors,
so in some cases this undoes that canonicalization in order to avoid
the extra negation, but it handles also non-uniform constants.
E.g. with -mavx2 the testcase assembly difference is:
-       movl    $47, %eax
+       movl    $48, %eax
        vmovdqa %xmm0, %xmm1
        vmovd   %eax, %xmm0
        vpbroadcastb    %xmm0, %xmm0
-       vpminsb %xmm0, %xmm1, %xmm0
-       vpcmpeqb        %xmm1, %xmm0, %xmm0
+       vpcmpgtb        %xmm1, %xmm0, %xmm0
and
-       vmovdqa %xmm0, %xmm1
-       vmovdqa .LC1(%rip), %xmm0
-       vpminsb %xmm1, %xmm0, %xmm1
-       vpcmpeqb        %xmm1, %xmm0, %xmm0
+       vpcmpgtb        .LC1(%rip), %xmm0, %xmm0
while with just SSE2:
-       pcmpgtb .LC0(%rip), %xmm0
-       pxor    %xmm1, %xmm1
-       pcmpeqb %xmm1, %xmm0
+       movdqa  %xmm0, %xmm1
+       movdqa  .LC0(%rip), %xmm0
+       pcmpgtb %xmm1, %xmm0
and
-       movdqa  %xmm0, %xmm1
-       movdqa  .LC1(%rip), %xmm0
-       pcmpgtb %xmm1, %xmm0
-       pxor    %xmm1, %xmm1
-       pcmpeqb %xmm1, %xmm0
+       pcmpgtb .LC1(%rip), %xmm0

2022-11-08  Jakub Jelinek  <jakub@redhat.com>

PR target/107546
* config/i386/predicates.md (vector_or_const_vector_operand): New
predicate.
* config/i386/sse.md (vec_cmp<mode><sseintvecmodelower>,
vec_cmpv2div2di, vec_cmpu<mode><sseintvecmodelower>,
vec_cmpuv2div2di): Use nonimmediate_or_const_vector_operand
predicate instead of nonimmediate_operand and
vector_or_const_vector_operand instead of vector_operand.
* config/i386/i386-expand.cc (ix86_expand_int_sse_cmp): For
LE/LEU or GE/GEU with CONST_VECTOR cop1 try to transform those
into LE/LEU or GT/GTU with larger or smaller by one cop1 if
there is no wrap-around.  Force CONST_VECTOR cop0 or cop1 into
REG.  Formatting fix.

* gcc.target/i386/pr107546.c: New test.
gcc/config/i386/i386-expand.cc
gcc/config/i386/predicates.md
gcc/config/i386/sse.md
gcc/testsuite/gcc.target/i386/pr107546.c [new file with mode: 0644]