Along with some special handling of calloc calls, r272717 introduced
the assumption into stmt_kills_ref_p that the value returned from
the call is used.  The code triggers an ICE when invoked during DCE
in an attempt to determine whether a calloc call can be eliminated.

To avoid the ICE the attached patch avoids using the return value if
it's not set.

Tested on x86_64-linux.

Martin
PR tree-optimization/93683 - ICE on calloc with unused return value in ao_ref_init_from_ptr_and_size

gcc/testsuite/ChangeLog:

	PR tree-optimization/93683
	* gcc.dg/tree-ssa/ssa-dse-39.c: New test.

gcc/ChangeLog:

	PR tree-optimization/93683
	* tree-ssa-alias.c (stmt_kills_ref_p): Avoid using LHS when not set.

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-39.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-39.c
new file mode 100644
index 00000000000..29b1a480ecd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-39.c
@@ -0,0 +1,18 @@
+/* PR tree-optimization/93683 - ICE on calloc with unused return value
+   in ao_ref_init_from_ptr_and_size
+   { dg-do compile }
+   { dg-options "-O2 -Wall -Wno-unused-result -fdump-tree-cddce1" } */
+
+void f0 (int *a)
+{
+  *a = 0;
+  __builtin_calloc (1, 1);
+}
+
+void f1 (int *a, unsigned n)
+{
+  *a = n;
+  __builtin_calloc (n, n);
+}
+
+/* { dg-final { scan-tree-dump-not "calloc" "cddce1" } } */
diff --git a/gcc/tree-ssa-alias.c b/gcc/tree-ssa-alias.c
index c7e6679f990..fd781050668 100644
--- a/gcc/tree-ssa-alias.c
+++ b/gcc/tree-ssa-alias.c
@@ -3265,6 +3265,8 @@ stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
 		    return false;
 
 		  dest = gimple_call_lhs (stmt);
+		  if (!dest)
+		    return false;
 		  len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
 		}
 	      else

Reply via email to