PR tree-optimization/87112 - ICE in fold_binary_loc on strnlen of mixed integer types
authorMartin Sebor <msebor@redhat.com>
Tue, 28 Aug 2018 00:10:46 +0000 (00:10 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Tue, 28 Aug 2018 00:10:46 +0000 (18:10 -0600)
gcc/ChangeLog:

PR tree-optimization/87112
* builtins.c (expand_builtin_strnlen): Convert c_strlen result to
the type of the bound argument.

gcc/testsuite/ChangeLog:

PR tree-optimization/87112
* gcc.dg/pr87112.c: New test.

From-SVN: r263900

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr87112.c [new file with mode: 0644]

index a8ab83580b5038c78e2b1a141e54442f9852e842..2449672c62ff0e2a15fab5378272bc2a90e414c5 100644 (file)
@@ -1,3 +1,9 @@
+2018-08-27  Martin Sebor  <msebor@redhat.com>
+
+       PR tree-optimization/87112
+       * builtins.c (expand_builtin_strnlen): Convert c_strlen result to
+       the type of the bound argument.
+
 2018-08-27  Jeff Law  <law@redhat.com>
 
        * tree-ssa-dse.c (compute_trims): Handle case where the reference's
index c4b52b9c62755a671c585c13ed08f59dab6991ef..eb69a409272c2cd1aaeb8f56831c632f6bc1820c 100644 (file)
@@ -2970,6 +2970,10 @@ expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
   tree func = get_callee_fndecl (exp);
 
   tree len = c_strlen (src, 0);
+  /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
+     so these conversions aren't necessary.  */
+  if (len)
+    len = fold_convert_loc (loc, TREE_TYPE (bound), len);
 
   if (TREE_CODE (bound) == INTEGER_CST)
     {
@@ -2984,7 +2988,6 @@ expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
       if (!len || TREE_CODE (len) != INTEGER_CST)
        return NULL_RTX;
 
-      len = fold_convert_loc (loc, size_type_node, len);
       len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
       return expand_expr (len, target, target_mode, EXPAND_NORMAL);
     }
index 8ecb60b2e170e3606458c5faf5b4f3b2582c0f8b..92f15401a22712d293f6de9d3ae83a453176675a 100644 (file)
@@ -1,3 +1,8 @@
+2018-08-27  Martin Sebor  <msebor@redhat.com>
+
+       PR tree-optimization/87112
+       * gcc.dg/pr87112.c: New test.
+
 2018-08-27  David Malcolm  <dmalcolm@redhat.com>
 
        PR c++/63392
diff --git a/gcc/testsuite/gcc.dg/pr87112.c b/gcc/testsuite/gcc.dg/pr87112.c
new file mode 100644 (file)
index 0000000..7510956
--- /dev/null
@@ -0,0 +1,31 @@
+/* PR tree-optimization/87112 - ICE due to strnlen mixing integer types
+   { dg-do compile }
+   { dg-options "-Os -Wall" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+extern size_t strnlen (const char*, size_t);
+
+size_t fi (int i)
+{
+  int n = i & 3;
+  return strnlen ("int", n);
+}
+
+size_t fui (unsigned i)
+{
+  unsigned n = i & 3;
+  return strnlen ("unsigned", n);
+}
+
+size_t fl (long i)
+{
+  long n = i & 3;
+  return strnlen ("long", n);
+}
+
+size_t fsz (size_t i)
+{
+  size_t n = i & 3;
+  return strnlen ("size_t", n);
+}