This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH to fix -Wrestrict ICE (PR middle-end/83463)


On 12/18/2017 08:10 AM, Marek Polacek wrote:
I'm not entirely up to speed with this code, but this one seemed sufficiently
obvious: check INTEGRAL_TYPE_P before looking at a tree's min/max value.
Otherwise, go with maxobjsize.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

Thanks for the fix.  I noticed the problem only comes up when
memcpy is declared without a prototype.  Otherwise, the memcpy
call is eliminated early on (during gimplification?)  So while
avoiding the ICE is obviously a necessary change, I wonder if
this bug also suggests a missing optimization that might still
be relevant (i.e., eliminating memcpy() with a zero size for
a memcpy without a prototype):

  void* memcpy ();

  void p (void *d, const void *s)
  {
    memcpy (d, s, 0);
  }

GCC doesn't warn for this code which means it views the memcpy
as a built-in but it doesn't eliminate the call.  If I declare
memcpy for example like this, it complains about the mismatch
but it does eliminates the call:

  void* memcpy (long, long, long);

Martin


2017-12-18  Marek Polacek  <polacek@redhat.com>

	PR middle-end/83463
	* gimple-ssa-warn-restrict.c (builtin_memref::builtin_memref):
	Check if TYPE is INTEGRAL_TYPE_P before accessing its min/max
	values.

	* gcc.dg/pr83463.c: New test.

diff --git gcc/gimple-ssa-warn-restrict.c gcc/gimple-ssa-warn-restrict.c
index 4d424735d2a..d1a376637a2 100644
--- gcc/gimple-ssa-warn-restrict.c
+++ gcc/gimple-ssa-warn-restrict.c
@@ -287,13 +287,15 @@ builtin_memref::builtin_memref (tree expr, tree size)
 		  else
 		    {
 		      gimple *stmt = SSA_NAME_DEF_STMT (offset);
+		      tree type;
 		      if (is_gimple_assign (stmt)
-			  && gimple_assign_rhs_code (stmt) == NOP_EXPR)
+			  && gimple_assign_rhs_code (stmt) == NOP_EXPR
+			  && (type = TREE_TYPE (gimple_assign_rhs1 (stmt)))
+			  && INTEGRAL_TYPE_P (type))
 			{
 			  /* Use the bounds of the type of the NOP_EXPR operand
 			     even if it's signed.  The result doesn't trigger
 			     warnings but makes their output more readable.  */
-			  tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
 			  offrange[0] = wi::to_offset (TYPE_MIN_VALUE (type));
 			  offrange[1] = wi::to_offset (TYPE_MAX_VALUE (type));
 			}
diff --git gcc/testsuite/gcc.dg/pr83463.c gcc/testsuite/gcc.dg/pr83463.c
index e69de29bb2d..735ef3c6dc8 100644
--- gcc/testsuite/gcc.dg/pr83463.c
+++ gcc/testsuite/gcc.dg/pr83463.c
@@ -0,0 +1,17 @@
+/* PR middle-end/83463 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wrestrict" } */
+
+int *a;
+void *memcpy ();
+void
+m (void *p1)
+{
+  memcpy (0, p1, 0);
+}
+
+void
+p ()
+{
+  m (p + (long) a);
+}

	Marek



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]