public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][Middle-end]change char type to unsigned char type when expanding strcmp/strncmp
@ 2018-07-19 16:49 Qing Zhao
  2018-07-19 17:31 ` Jakub Jelinek
  0 siblings, 1 reply; 9+ messages in thread
From: Qing Zhao @ 2018-07-19 16:49 UTC (permalink / raw)
  To: gcc Patches; +Cc: jeff Law, richard Biener, jakub Jelinek

[-- Attachment #1: Type: text/plain, Size: 1277 bytes --]

Hi, 

As Wilco mentioned in PR78809 after I checked in the last part of implementation of inline strcmp:

See  http://www.iso-9899.info/n1570.html
 section 7.24.4:

"The sign of a nonzero value returned by the comparison functions memcmp, strcmp, and strncmp is determined 
by the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char)
 that differ in the objects being compared."

currently, in my implementation, I used char type when expanding strcmp/strncmp, and unsigned char when expanding
memcmp.

from the C standard, we should use unsigned char for all strcmp/strncmp/memcmp.

the change is quite simple, and I have tested it on X86, aarch64 and powerPC, no regressions.

Okay for trunk?

Qing

gcc/ChangeLog:

+2018-07-19  Qing Zhao  <qing.zhao@oracle.com>
+
+       * builtins.c (expand_builtin_memcmp): Delete the last parameter for
+       call to inline_expand_builtin_string_cmp.
+       (expand_builtin_strcmp): Likewise.
+       (expand_builtin_strncmp): Likewise.
+       (inline_string_cmp): Delete the last parameter, change char_type_node
+       to unsigned_char_type_node for strcmp/strncmp;
+       (inline_expand_builtin_string_cmp): Delete the last parameter.
+

[-- Attachment #2: 78809C_uchar.patch --]
[-- Type: application/octet-stream, Size: 4560 bytes --]

From 1d87dd8e390d47802d70dee07f1f9c37572c660a Mon Sep 17 00:00:00 2001
From: qing zhao <qing.zhao@oracle.com>
Date: Thu, 19 Jul 2018 09:01:11 -0700
Subject: [PATCH] Change char to unsigned char for strcmp/strncmp when expand
 them to a sequence of byte comparisons.

Due to C standard section 7.24.4:

The sign of a nonzero value returned by the comparison functions memcmp,
strcmp, and strncmp is determined by the sign of the difference between
the values of the first pair of characters (both interpreted as unsigned
char) that differ in the objects being compared.

bootstraped and tested on both X86 and Aarch64. no regression.
---
 gcc/builtins.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/gcc/builtins.c b/gcc/builtins.c
index c069d66..6f58384 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -119,7 +119,7 @@ static rtx expand_builtin_next_arg (void);
 static rtx expand_builtin_va_start (tree);
 static rtx expand_builtin_va_end (tree);
 static rtx expand_builtin_va_copy (tree);
-static rtx inline_expand_builtin_string_cmp (tree, rtx, bool);
+static rtx inline_expand_builtin_string_cmp (tree, rtx);
 static rtx expand_builtin_strcmp (tree, rtx);
 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, scalar_int_mode);
@@ -4472,7 +4472,7 @@ expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
 
   if (!result_eq && fcode != BUILT_IN_BCMP && no_overflow)
     {
-      result = inline_expand_builtin_string_cmp (exp, target, true);
+      result = inline_expand_builtin_string_cmp (exp, target);
       if (result)
 	return result;
     }
@@ -4551,7 +4551,7 @@ expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
 
   /* Due to the performance benefit, always inline the calls first.  */
   rtx result = NULL_RTX;
-  result = inline_expand_builtin_string_cmp (exp, target, false);
+  result = inline_expand_builtin_string_cmp (exp, target);
   if (result)
     return result;
 
@@ -4670,7 +4670,7 @@ expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
 
   /* Due to the performance benefit, always inline the calls first.  */
   rtx result = NULL_RTX;
-  result = inline_expand_builtin_string_cmp (exp, target, false);
+  result = inline_expand_builtin_string_cmp (exp, target);
   if (result)
     return result;
 
@@ -6778,8 +6778,7 @@ expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
 static rtx
 inline_string_cmp (rtx target, tree var_str, const char *const_str,
 		   unsigned HOST_WIDE_INT length,
-		   int const_str_n, machine_mode mode,
-		   bool is_memcmp)
+		   int const_str_n, machine_mode mode)
 {
   HOST_WIDE_INT offset = 0;
   rtx var_rtx_array
@@ -6788,7 +6787,7 @@ inline_string_cmp (rtx target, tree var_str, const char *const_str,
   rtx const_rtx = NULL_RTX;
   rtx result = target ? target : gen_reg_rtx (mode);
   rtx_code_label *ne_label = gen_label_rtx ();
-  tree unit_type_node = is_memcmp ? unsigned_char_type_node : char_type_node;
+  tree unit_type_node = unsigned_char_type_node;
   scalar_int_mode unit_mode
     = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
 
@@ -6803,7 +6802,7 @@ inline_string_cmp (rtx target, tree var_str, const char *const_str,
       rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
 
       result = expand_simple_binop (mode, MINUS, op0, op1,
-				    result, is_memcmp ? 1 : 0, OPTAB_WIDEN);
+				    result, 1, OPTAB_WIDEN);
       if (i < length - 1)
 	emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
 	    			 mode, true, ne_label);
@@ -6822,7 +6821,7 @@ inline_string_cmp (rtx target, tree var_str, const char *const_str,
    TARGET if that's convenient.
    If the call is not been inlined, return NULL_RTX.  */
 static rtx
-inline_expand_builtin_string_cmp (tree exp, rtx target, bool is_memcmp)
+inline_expand_builtin_string_cmp (tree exp, rtx target)
 {
   tree fndecl = get_callee_fndecl (exp);
   enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
@@ -6879,7 +6878,7 @@ inline_expand_builtin_string_cmp (tree exp, rtx target, bool is_memcmp)
   /* Now, start inline expansion the call.  */
   return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
 			    (const_str_n == 1) ? src_str1 : src_str2, length,
-			    const_str_n, mode, is_memcmp);
+			    const_str_n, mode);
 }
 
 /* Expand an expression EXP that calls a built-in function,
-- 
1.9.1

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

end of thread, other threads:[~2018-07-20 18:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-19 16:49 [PATCH][Middle-end]change char type to unsigned char type when expanding strcmp/strncmp Qing Zhao
2018-07-19 17:31 ` Jakub Jelinek
2018-07-19 19:06   ` Qing Zhao
2018-07-19 19:24     ` Jakub Jelinek
2018-07-19 20:49       ` Qing Zhao
2018-07-20 14:53   ` [PATCH][Middle-end][version 2]change " Qing Zhao
2018-07-20 14:59     ` Jakub Jelinek
2018-07-20 15:23       ` Qing Zhao
2018-07-20 18:20       ` Qing Zhao

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