diff mbox series

middle-end/101478 - ICE with degenerate address during gimplification

Message ID 20240731090534.358F7385C6C1@sourceware.org
State New
Headers show
Series middle-end/101478 - ICE with degenerate address during gimplification | expand

Commit Message

Richard Biener July 31, 2024, 9:05 a.m. UTC
When we gimplify &MEM[0B + 4] we are re-folding the address in case
types are not canonical which ends up with a constant address that
recompute_tree_invariant_for_addr_expr ICEs on.  Properly guard
that call.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	PR middle-end/101478
	* gimplify.cc (gimplify_addr_expr): Check we still have an
	ADDR_EXPR before calling recompute_tree_invariant_for_addr_expr.

	* gcc.dg/pr101478.c: New testcase.
---
 gcc/gimplify.cc                 |  3 ++-
 gcc/testsuite/gcc.dg/pr101478.c | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr101478.c
diff mbox series

Patch

diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 47c9913b55b..fab5532c54d 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -6984,7 +6984,8 @@  gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
 	*expr_p = build_fold_addr_expr (op0);
 
       /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly.  */
-      recompute_tree_invariant_for_addr_expr (*expr_p);
+      if (TREE_CODE (*expr_p) == ADDR_EXPR)
+	recompute_tree_invariant_for_addr_expr (*expr_p);
 
       /* If we re-built the ADDR_EXPR add a conversion to the original type
          if required.  */
diff --git a/gcc/testsuite/gcc.dg/pr101478.c b/gcc/testsuite/gcc.dg/pr101478.c
new file mode 100644
index 00000000000..527620ea0f1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101478.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+struct obj {
+  int n;
+  int l;
+};
+int main()
+{
+  (struct obj *)((char *)(__SIZE_TYPE__)({ 0; }) - (char *)&((struct obj *)0)->l);
+}