public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
@ 2017-11-15 15:21 Qing Zhao
  2017-11-17  1:31 ` Jeff Law
  0 siblings, 1 reply; 7+ messages in thread
From: Qing Zhao @ 2017-11-15 15:21 UTC (permalink / raw)
  To: gcc-patches; +Cc: Wilco Dijkstra, Richard Biener, richard Earnshaw

Hi,

this is the first patch for PR78809 (totally 3 patches)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78809
inline strcmp with small constant strings

The design doc is at:
https://www.mail-archive.com/gcc@gcc.gnu.org/msg83822.html

this patch is for the first part of change:

A. for strncmp (s1, s2, n)
     if one of "s1" or "s2" is a constant string, "n" is a constant, and
larger than the length of the constant string:
     change strncmp (s1, s2, n) to strcmp (s1, s2);

adding test case strcmpopt_1.c into gcc.dg

bootstraped and tested on both X86 and aarch64. no regression.

Okay for commit?

thanks.

Qing

======================

gcc/ChangeLog

2017-11-15  Qing Zhao  <qing.zhao@oracle.com>

       * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
       of replacing call to strncmp with corresponding call to strcmp when
       meeting conditions.

gcc/testsuite/ChangeLog

2017-11-15  Qing Zhao  <qing.zhao@oracle.com>

       PR middle-end/78809
       * gcc.dg/strcmpopt_1.c: New test.

---
 gcc/gimple-fold.c                  | 15 +++++++++++++++
 gcc/testsuite/gcc.dg/strcmpopt_1.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/strcmpopt_1.c

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index adb6f3b..1ed6383 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -2258,6 +2258,21 @@ gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
       return true;
     }
 
+  /* If length is larger than the length of one constant string, 
+     replace strncmp with corresponding strcmp */ 
+  if (fcode == BUILT_IN_STRNCMP 
+      && length > 0
+      && ((p2 && (size_t) length > strlen (p2)) 
+          || (p1 && (size_t) length > strlen (p1))))
+    {
+      tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
+      if (!fn)
+        return false;
+      gimple *repl = gimple_build_call (fn, 2, str1, str2);
+      replace_call_with_call_and_fold (gsi, repl);
+      return true;
+    }
+
   return false;
 }
 
diff --git a/gcc/testsuite/gcc.dg/strcmpopt_1.c b/gcc/testsuite/gcc.dg/strcmpopt_1.c
new file mode 100644
index 0000000..40596a2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/strcmpopt_1.c
@@ -0,0 +1,28 @@
+/* { dg-do run } */
+/* { dg-options "-fdump-tree-gimple" } */
+
+#include <string.h>
+#include <stdlib.h>
+
+int cmp1 (char *p)
+{
+  return strncmp (p, "fis", 4);
+}
+int cmp2 (char *q)
+{
+  return strncmp ("fis", q, 4);
+}
+
+int main ()
+{
+
+  char *p = "fish";
+  char *q = "fis\0";
+
+  if (cmp1 (p) == 0 || cmp2 (q) != 0)
+    abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "strcmp \\(" 2 "gimple" } } */
-- 
1.9.1

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

* Re: [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
  2017-11-15 15:21 [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings) Qing Zhao
@ 2017-11-17  1:31 ` Jeff Law
  2017-11-17  3:32   ` Qing Zhao
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff Law @ 2017-11-17  1:31 UTC (permalink / raw)
  To: Qing Zhao, gcc-patches; +Cc: Wilco Dijkstra, Richard Biener, richard Earnshaw

On 11/15/2017 08:00 AM, Qing Zhao wrote:
> Hi,
> 
> this is the first patch for PR78809 (totally 3 patches)
> 
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78809
> inline strcmp with small constant strings
> 
> The design doc is at:
> https://www.mail-archive.com/gcc@gcc.gnu.org/msg83822.html
> 
> this patch is for the first part of change:
> 
> A. for strncmp (s1, s2, n)
>      if one of "s1" or "s2" is a constant string, "n" is a constant, and
> larger than the length of the constant string:
>      change strncmp (s1, s2, n) to strcmp (s1, s2);
> 
> adding test case strcmpopt_1.c into gcc.dg
> 
> bootstraped and tested on both X86 and aarch64. no regression.
> 
> Okay for commit?
> 
> thanks.
> 
> Qing
> 
> ======================
> 
> gcc/ChangeLog
> 
> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com>
> 
>        * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
>        of replacing call to strncmp with corresponding call to strcmp when
>        meeting conditions.
> 
> gcc/testsuite/ChangeLog
> 
> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com>
> 
>        PR middle-end/78809
>        * gcc.dg/strcmpopt_1.c: New test.
This is OK for the trunk.

Please put the PR middle-end/78809 marker in the main ChangeLog entry as
well.

Do you have write access to the repository?

jeff

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

* Re: [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
  2017-11-17  1:31 ` Jeff Law
@ 2017-11-17  3:32   ` Qing Zhao
  2017-11-17  7:18     ` Jeff Law
  0 siblings, 1 reply; 7+ messages in thread
From: Qing Zhao @ 2017-11-17  3:32 UTC (permalink / raw)
  To: Jeff Law; +Cc: gcc-patches, Wilco Dijkstra, Richard Biener, richard Earnshaw


> On Nov 16, 2017, at 6:57 PM, Jeff Law <law@redhat.com> wrote:
> 
> On 11/15/2017 08:00 AM, Qing Zhao wrote:
>> Hi,
>> 
>> this is the first patch for PR78809 (totally 3 patches)
>> 
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78809
>> inline strcmp with small constant strings
>> 
>> The design doc is at:
>> https://www.mail-archive.com/gcc@gcc.gnu.org/msg83822.html
>> 
>> this patch is for the first part of change:
>> 
>> A. for strncmp (s1, s2, n)
>>     if one of "s1" or "s2" is a constant string, "n" is a constant, and
>> larger than the length of the constant string:
>>     change strncmp (s1, s2, n) to strcmp (s1, s2);
>> 
>> adding test case strcmpopt_1.c into gcc.dg
>> 
>> bootstraped and tested on both X86 and aarch64. no regression.
>> 
>> Okay for commit?
>> 
>> thanks.
>> 
>> Qing
>> 
>> ======================
>> 
>> gcc/ChangeLog
>> 
>> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com>
>> 
>>       * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
>>       of replacing call to strncmp with corresponding call to strcmp when
>>       meeting conditions.
>> 
>> gcc/testsuite/ChangeLog
>> 
>> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com>
>> 
>>       PR middle-end/78809
>>       * gcc.dg/strcmpopt_1.c: New test.
> This is OK for the trunk.
> 
> Please put the PR middle-end/78809 marker in the main ChangeLog entry as
> well.

Okay.  (as following?)

2017-11-15  Qing Zhao <qing.zhao@oracle.com>

        PR middle-end/78809
        * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
        of replacing call to strncmp with corresponding call to strcmp when
        meeting conditions.


gcc/testsuite/ChangeLog

2017-11-15  Qing Zhao  <qing.zhao@oracle.com <mailto:qing.zhao@oracle.com>>

      PR middle-end/78809
      * gcc.dg/strcmpopt_1.c: New test.

> 
> Do you have write access to the repository?

No, I don’t have. 
Qing


> 
> jeff

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

* Re: [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
  2017-11-17  3:32   ` Qing Zhao
@ 2017-11-17  7:18     ` Jeff Law
  2017-11-17  9:18       ` Paolo Carlini
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff Law @ 2017-11-17  7:18 UTC (permalink / raw)
  To: Qing Zhao; +Cc: gcc-patches, Wilco Dijkstra, Richard Biener, richard Earnshaw

On 11/16/2017 06:53 PM, Qing Zhao wrote:
> 
>> On Nov 16, 2017, at 6:57 PM, Jeff Law <law@redhat.com
>> <mailto:law@redhat.com>> wrote:
>>
>> On 11/15/2017 08:00 AM, Qing Zhao wrote:
>>> Hi,
>>>
>>> this is the first patch for PR78809 (totally 3 patches)
>>>
>>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78809
>>> inline strcmp with small constant strings
>>>
>>> The design doc is at:
>>> https://www.mail-archive.com/gcc@gcc.gnu.org/msg83822.html
>>>
>>> this patch is for the first part of change:
>>>
>>> A. for strncmp (s1, s2, n)
>>>     if one of "s1" or "s2" is a constant string, "n" is a constant, and
>>> larger than the length of the constant string:
>>>     change strncmp (s1, s2, n) to strcmp (s1, s2);
>>>
>>> adding test case strcmpopt_1.c into gcc.dg
>>>
>>> bootstraped and tested on both X86 and aarch64. no regression.
>>>
>>> Okay for commit?
>>>
>>> thanks.
>>>
>>> Qing
>>>
>>> ======================
>>>
>>> gcc/ChangeLog
>>>
>>> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com>
>>>
>>>       * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
>>>       of replacing call to strncmp with corresponding call to strcmp when
>>>       meeting conditions.
>>>
>>> gcc/testsuite/ChangeLog
>>>
>>> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com>
>>>
>>>       PR middle-end/78809
>>>       * gcc.dg/strcmpopt_1.c: New test.
>> This is OK for the trunk.
>>
>> Please put the PR middle-end/78809 marker in the main ChangeLog entry as
>> well.
> 
> Okay.  (as following?)
> 
> 2017-11-15  Qing Zhao <qing.zhao@oracle.com <mailto:qing.zhao@oracle.com>>
> 
>         PR middle-end/78809
>         * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
>         of replacing call to strncmp with corresponding call to strcmp when
>         meeting conditions.
> 
> 
> gcc/testsuite/ChangeLog
> 
> 2017-11-15  Qing Zhao  <qing.zhao@oracle.com <mailto:qing.zhao@oracle.com>>
> 
>       PR middle-end/78809
>       * gcc.dg/strcmpopt_1.c: New test.
> 
>>
>> Do you have write access to the repository?
> 
> No, I don’t have. 
OK.  I'll go ahead and commit for you.

I think this patch is small enough to not require a copyright
assignment.  However further work likely will.  I don't offhand know if
Oracle has a blanket assignment in place.  Can you work with Paolo to
ensure that the proper paperwork is in place so that future
contributions don't get held up on legal stuff.



Thanks,
jeff

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

* Re: [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
  2017-11-17  7:18     ` Jeff Law
@ 2017-11-17  9:18       ` Paolo Carlini
  2017-11-17 15:13         ` Qing Zhao
  2017-11-17 16:03         ` Jeff Law
  0 siblings, 2 replies; 7+ messages in thread
From: Paolo Carlini @ 2017-11-17  9:18 UTC (permalink / raw)
  To: Jeff Law, Qing Zhao
  Cc: gcc-patches, Wilco Dijkstra, Richard Biener, richard Earnshaw

Hi,

On 17/11/2017 06:29, Jeff Law wrote:
> OK. I'll go ahead and commit for you.
Beautiful. Thanks Jeff.
> I think this patch is small enough to not require a copyright
> assignment.  However further work likely will.  I don't offhand know if
> Oracle has a blanket assignment in place.  Can you work with Paolo to
> ensure that the proper paperwork is in place so that future
> contributions don't get held up on legal stuff.
Oracle is fine, has a blanket assignment, thus Qing is already covered 
and I think this one is her third small (but useful ;) contribution so far.

While we are at it, I'm thinking that probably Qing could be added to 
sourceware.org, if you don't disagree. Qing, if nobody objects over the 
next day or so, please go ahead, submit this form (include my name as 
sponsor):

     https://sourceware.org/cgi-bin/pdw/ps_form.cgi

Thanks again!
Paolo.

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

* Re: [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
  2017-11-17  9:18       ` Paolo Carlini
@ 2017-11-17 15:13         ` Qing Zhao
  2017-11-17 16:03         ` Jeff Law
  1 sibling, 0 replies; 7+ messages in thread
From: Qing Zhao @ 2017-11-17 15:13 UTC (permalink / raw)
  To: Paolo Carlini
  Cc: Jeff Law, gcc-patches, Wilco Dijkstra, Richard Biener, richard Earnshaw

thanks Jeff and Paolo. 

really appreciate  for all the help so far.

Qing
> On Nov 17, 2017, at 3:17 AM, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> 
> Hi,
> 
> On 17/11/2017 06:29, Jeff Law wrote:
>> OK. I'll go ahead and commit for you.
> Beautiful. Thanks Jeff.
>> I think this patch is small enough to not require a copyright
>> assignment.  However further work likely will.  I don't offhand know if
>> Oracle has a blanket assignment in place.  Can you work with Paolo to
>> ensure that the proper paperwork is in place so that future
>> contributions don't get held up on legal stuff.
> Oracle is fine, has a blanket assignment, thus Qing is already covered and I think this one is her third small (but useful ;) contribution so far.
> 
> While we are at it, I'm thinking that probably Qing could be added to sourceware.org, if you don't disagree. Qing, if nobody objects over the next day or so, please go ahead, submit this form (include my name as sponsor):
> 
>     https://sourceware.org/cgi-bin/pdw/ps_form.cgi
> 
> Thanks again!
> Paolo.

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

* Re: [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings)
  2017-11-17  9:18       ` Paolo Carlini
  2017-11-17 15:13         ` Qing Zhao
@ 2017-11-17 16:03         ` Jeff Law
  1 sibling, 0 replies; 7+ messages in thread
From: Jeff Law @ 2017-11-17 16:03 UTC (permalink / raw)
  To: Paolo Carlini, Qing Zhao
  Cc: gcc-patches, Wilco Dijkstra, Richard Biener, richard Earnshaw

On 11/17/2017 02:17 AM, Paolo Carlini wrote:
> Hi,
> 
> On 17/11/2017 06:29, Jeff Law wrote:
>> OK. I'll go ahead and commit for you.
> Beautiful. Thanks Jeff.
>> I think this patch is small enough to not require a copyright
>> assignment.  However further work likely will.  I don't offhand know if
>> Oracle has a blanket assignment in place.  Can you work with Paolo to
>> ensure that the proper paperwork is in place so that future
>> contributions don't get held up on legal stuff.
> Oracle is fine, has a blanket assignment, thus Qing is already covered
> and I think this one is her third small (but useful ;) contribution so far.
> 
> While we are at it, I'm thinking that probably Qing could be added to
> sourceware.org, if you don't disagree. Qing, if nobody objects over the
> next day or so, please go ahead, submit this form (include my name as
> sponsor):
> 
>     https://sourceware.org/cgi-bin/pdw/ps_form.cgi
Seems quite reasonable to me.

jeff

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

end of thread, other threads:[~2017-11-17 15:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-15 15:21 [PATCH 1/3][middle-end]PR78809 (Inline strcmp with small constant strings) Qing Zhao
2017-11-17  1:31 ` Jeff Law
2017-11-17  3:32   ` Qing Zhao
2017-11-17  7:18     ` Jeff Law
2017-11-17  9:18       ` Paolo Carlini
2017-11-17 15:13         ` Qing Zhao
2017-11-17 16:03         ` Jeff Law

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).