public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
@ 2012-02-08 12:38 rguenth at gcc dot gnu.org
  2012-02-08 13:06 ` [Bug tree-optimization/52171] " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-02-08 12:38 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

             Bug #: 52171
           Summary: memcmp/strcmp/strncmp can be optimized when the result
                    is tested for [in]equality with 0
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: rguenth@gcc.gnu.org
            Blocks: 43052


On GIMPLE we should expand memcmp/strcmp/strncmp to inline integral compares
if the result is only tested against equality with 0.

See PR43052 where we hint at that these may be the only profitable cases
to inline for memcmp at least.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
@ 2012-02-08 13:06 ` rguenth at gcc dot gnu.org
  2012-02-08 13:50 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-02-08 13:06 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2012-02-08
         AssignedTo|unassigned at gcc dot       |rguenth at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-02-08 13:05:23 UTC ---
Created attachment 26610
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26610
prototype for memcmp

Patch that handles

int cmp (char *p, char *q)
{
  char *pa = __builtin_assume_aligned (p, 4);
  char *qa = __builtin_assume_aligned (q, 4);
  if (__builtin_memcmp (pa, qa, 4) != 0)
    return 1;
  return 0;
}

from forwprops simplify_builtin_call.

Possibly we could consider vector modes / compares (for greater size) and
allow misaligned accesses on !STRICT_ALIGNMENT targets.  We can also
handle non-power-of-two sizes by using (A ^ B) & mask, but we have to
think about endianess there.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
  2012-02-08 13:06 ` [Bug tree-optimization/52171] " rguenth at gcc dot gnu.org
@ 2012-02-08 13:50 ` jakub at gcc dot gnu.org
  2012-02-08 14:38 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-02-08 13:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-02-08 13:50:18 UTC ---
IMHO you should also handle the case when one of the operand is STRING_CST,
then you don't need to test its alignment, you compare the memory content with
something read from the string at compile time.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
  2012-02-08 13:06 ` [Bug tree-optimization/52171] " rguenth at gcc dot gnu.org
  2012-02-08 13:50 ` jakub at gcc dot gnu.org
@ 2012-02-08 14:38 ` rguenth at gcc dot gnu.org
  2012-05-06  5:03 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-02-08 14:38 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-02-08 14:37:24 UTC ---
For comparison against constants it might be worthwhile to still allow two
loads to improve the number of alignment cases we can handle.  Well, I'm not
sure we can expect anything more than 1-byte alignment for the most cases.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2012-02-08 14:38 ` rguenth at gcc dot gnu.org
@ 2012-05-06  5:03 ` pinskia at gcc dot gnu.org
  2012-05-07  9:25 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-05-06  5:03 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-05-06 04:55:13 UTC ---
This really sounds like the same as PR 12086.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2012-05-06  5:03 ` pinskia at gcc dot gnu.org
@ 2012-05-07  9:25 ` rguenth at gcc dot gnu.org
  2013-02-19 14:51 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-05-07  9:25 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at redhat dot com

--- Comment #5 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-05-07 09:22:57 UTC ---
*** Bug 53253 has been marked as a duplicate of this bug. ***


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2012-05-07  9:25 ` rguenth at gcc dot gnu.org
@ 2013-02-19 14:51 ` rguenth at gcc dot gnu.org
  2013-02-19 15:05 ` jakub at gcc dot gnu.org
  2022-05-24 13:32 ` cvs-commit at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-02-19 14:51 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> 2013-02-19 14:50:52 UTC ---
Happens in insn-preds.c quite often:

    case 'Y':
      if (!strncmp (str, "Yi", 2))
        return CONSTRAINT_Yi;
      if (!strncmp (str, "Ym", 2))
        return CONSTRAINT_Ym;
      if (!strncmp (str, "Yp", 2))
        return CONSTRAINT_Yp;
      if (!strncmp (str, "Ya", 2))
        return CONSTRAINT_Ya;
...

and later calls are predicted as cold and thus not expanded inline by

(define_expand "cmpstrnsi"
  [(set (match_operand:SI 0 "register_operand" "")
        (compare:SI (match_operand:BLK 1 "general_operand" "")
                    (match_operand:BLK 2 "general_operand" "")))
   (use (match_operand 3 "general_operand" ""))
   (use (match_operand 4 "immediate_operand" ""))]
  ""
{
  rtx addr1, addr2, out, outlow, count, countreg, align;

  if (optimize_insn_for_size_p () && !TARGET_INLINE_ALL_STRINGOPS)
    FAIL;

the insn-preds.c code could also be optimized, for length 2 a simple
char equality test would be enough ...


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2013-02-19 14:51 ` rguenth at gcc dot gnu.org
@ 2013-02-19 15:05 ` jakub at gcc dot gnu.org
  2022-05-24 13:32 ` cvs-commit at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-19 15:05 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-19 15:05:01 UTC ---
(In reply to comment #6)
> Happens in insn-preds.c quite often:
> 
>     case 'Y':
>       if (!strncmp (str, "Yi", 2))
>         return CONSTRAINT_Yi;
>       if (!strncmp (str, "Ym", 2))
>         return CONSTRAINT_Ym;
>       if (!strncmp (str, "Yp", 2))
>         return CONSTRAINT_Yp;
>       if (!strncmp (str, "Ya", 2))
>         return CONSTRAINT_Ya;

Well, for insn-preds.c we could also argue that the generator should emit for
this
  case 'Y':
    if (str[1] == 'i') return CONSTRAINT_Yi;
    if (str[1] == 'm') return CONSTRAINT_Ym;
etc.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/52171] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0
  2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2013-02-19 15:05 ` jakub at gcc dot gnu.org
@ 2022-05-24 13:32 ` cvs-commit at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-24 13:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52171

--- Comment #17 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:9e7a0e42a15eb53850496e91f2e484ed74ac3617

commit r13-738-g9e7a0e42a15eb53850496e91f2e484ed74ac3617
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Tue May 24 14:29:27 2022 +0100

    Minor improvement to genpreds.cc

    This simple patch implements Richard Biener's suggestion in comment #6
    of PR tree-optimization/52171 (from February 2013) that the insn-preds
    code generated by genpreds can avoid using strncmp when matching constant
    strings of length one.

    The effect of this patch is best explained by the diff of insn-preds.cc:
    <       if (!strncmp (str + 1, "g", 1))
    ---
    >       if (str[1] == 'g')
    3104c3104
    <       if (!strncmp (str + 1, "m", 1))
    ---
    >       if (str[1] == 'm')
    3106c3106
    <       if (!strncmp (str + 1, "c", 1))
    ---
    >       if (str[1] == 'c')
    ...

    The equivalent optimization is performed by GCC (but perhaps not by the
    host compiler), but generating simpler/smaller code may encourage further
    optimizations (such as use of a switch statement).

    2022-05-24  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            * genpreds.cc (write_lookup_constraint_1): Avoid generating a call
            to strncmp for strings of length one.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-05-24 13:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-08 12:38 [Bug tree-optimization/52171] New: memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 rguenth at gcc dot gnu.org
2012-02-08 13:06 ` [Bug tree-optimization/52171] " rguenth at gcc dot gnu.org
2012-02-08 13:50 ` jakub at gcc dot gnu.org
2012-02-08 14:38 ` rguenth at gcc dot gnu.org
2012-05-06  5:03 ` pinskia at gcc dot gnu.org
2012-05-07  9:25 ` rguenth at gcc dot gnu.org
2013-02-19 14:51 ` rguenth at gcc dot gnu.org
2013-02-19 15:05 ` jakub at gcc dot gnu.org
2022-05-24 13:32 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).