On Wed, May 21, 2025 at 1:20 PM Jakub Jelinek <ja...@redhat.com> wrote:
>
> On Wed, May 21, 2025 at 11:48:34AM +0200, Uros Bizjak wrote:
> > Please introduce "x86_64_neg_const_int_operand" predicate that will
> > allow only const_int operands, and will reject negative endbr (and
> > wide DImode) const_ints. You don't need a constraint if the predicate
> > allows only const_ints (the const_int won't be reloaded).
> >
> > > +         (const_int 0)))]
> > > +  "CONST_INT_P (operands[1])
> > > +   && ix86_match_ccmode (insn, CCGOCmode)
> > > +   && (<MODE>mode != DImode || !mode_signbit_p (SImode, operands[1]))
> > > +   && ((flag_cf_protection & CF_BRANCH) == 0
> > > +       || ((-INTVAL (operands[1]) & 0xffffffffUL)
> > > +          != (TARGET_64BIT ? 0xfa1e0ff3UL : 0xfb1e0ff3UL)))"
> >
> > New predicate will also avoid the above horrible insn condition.
>
> So like this (if it passes bootstrap/regtest)?
> Note, I'm using "n" rather than "<i>" constraints, because with "e" we could
> get into trouble when the non-negated value would match endbr{64,32} - the
> predicate only checks (I think that is desirable) that the negation isn't
> endbr{64,32}.

IIRC, constraint is not needed when only CONST_INT is allowed. See
quite some examples in i386.md.

>
> 2025-05-21  Jakub Jelinek  <ja...@redhat.com>
>
>         PR target/120360
>         * config/i386/predicates.md (x86_64_neg_const_int_operand): New
>         predicate.
>         * config/i386/i386.md (*cmp<mode>_plus_1): New pattern.
>
>         * gcc.target/i386/pr120360.c: New test.

OK.

Thanks,
Uros.

>
> --- gcc/config/i386/predicates.md.jj    2025-05-20 08:14:05.053424802 +0200
> +++ gcc/config/i386/predicates.md       2025-05-21 13:10:09.945333328 +0200
> @@ -393,6 +393,23 @@ (define_predicate "x86_64_zext_immediate
>    return false;
>  })
>
> +;; Return true if VALUE is a constant integer whose negation satisfies
> +;; x86_64_immediate_operand.
> +(define_predicate "x86_64_neg_const_int_operand"
> +  (match_code "const_int")
> +{
> +  HOST_WIDE_INT val = -UINTVAL (op);
> +  if (mode == DImode && trunc_int_for_mode (val, SImode) != val)
> +    return false;
> +  if (flag_cf_protection & CF_BRANCH)
> +    {
> +      unsigned HOST_WIDE_INT endbr = TARGET_64BIT ? 0xfa1e0ff3 : 0xfb1e0ff3;
> +      if ((val & HOST_WIDE_INT_C (0xffffffff)) == endbr)
> +       return false;
> +    }
> +  return true;
> +})
> +
>  ;; Return true if VALUE is a constant integer whose low and high words 
> satisfy
>  ;; x86_64_immediate_operand.
>  (define_predicate "x86_64_hilo_int_operand"
> --- gcc/config/i386/i386.md.jj  2025-05-20 08:14:05.036425035 +0200
> +++ gcc/config/i386/i386.md     2025-05-21 13:04:12.441142398 +0200
> @@ -1599,6 +1599,20 @@ (define_insn "*cmp<mode>_minus_1"
>    [(set_attr "type" "icmp")
>     (set_attr "mode" "<MODE>")])
>
> +(define_insn "*cmp<mode>_plus_1"
> +  [(set (reg FLAGS_REG)
> +       (compare
> +         (plus:SWI (match_operand:SWI 0 "nonimmediate_operand" "<r>m")
> +                   (match_operand:SWI 1 "x86_64_neg_const_int_operand" "n"))
> +         (const_int 0)))]
> +  "ix86_match_ccmode (insn, CCGOCmode)"
> +{
> +  operands[1] = gen_int_mode (-INTVAL (operands[1]), <MODE>mode);
> +  return "cmp{<imodesuffix>}\t{%1, %0|%0, %1}";
> +}
> +  [(set_attr "type" "icmp")
> +   (set_attr "mode" "<MODE>")])
> +
>  (define_insn "*cmpqi_ext<mode>_1"
>    [(set (reg FLAGS_REG)
>         (compare
> --- gcc/testsuite/gcc.target/i386/pr120360.c.jj 2025-05-20 20:40:32.194962716 
> +0200
> +++ gcc/testsuite/gcc.target/i386/pr120360.c    2025-05-20 20:43:03.910930990 
> +0200
> @@ -0,0 +1,36 @@
> +/* PR target/120360 */
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fno-stack-protector -masm=att" } */
> +/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
> +/* { dg-final { scan-assembler-times "\tjn*s\t" 3 } } */
> +/* { dg-final { scan-assembler-times "\tcmp\[lq]\t%" 1 } } */
> +/* { dg-final { scan-assembler-times "\tcmp\[lq]\t\\\$-1234," 1 } } */
> +/* { dg-final { scan-assembler-times "\tcmp\[lq]\t\\\$2345," 1 } } */
> +/* { dg-final { scan-assembler-not "\tadd\[lq]\t" { target { ! *-*-darwin* } 
> } } } */
> +/* { dg-final { scan-assembler-not "\tsub\[lq]\t" { target { ! *-*-darwin* } 
> } } } */
> +
> +void qux (unsigned long);
> +
> +void
> +foo (unsigned long x, unsigned long y)
> +{
> +  unsigned long z = x - y;
> +  if ((long) z < 0)
> +    qux (x);
> +}
> +
> +void
> +bar (unsigned long x)
> +{
> +  unsigned long z = x + 1234;
> +  if ((long) z < 0)
> +    qux (x);
> +}
> +
> +void
> +baz (unsigned long x)
> +{
> +  unsigned long z = x - 2345;
> +  if ((long) z < 0)
> +    qux (x);
> +}
>
>
>         Jakub
>

Reply via email to