https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55978
--- Comment #33 from anlauf at gcc dot gnu.org ---
I've been repeatedly struggling with the testcase in comment#19.
Since the dump-tree did not reveal anything, I ran a reduced version
under gdb to see why the code crashes at -O0 and -Og but not at higher
optimization. I got them impression that the dump-tree does not tell
the full truth: subroutine one has
{
struct array01_integer(kind=4) * D.4292;
D.4292 = x != 0B ? (struct array01_integer(kind=4) *)
_gfortran_internal_pack (x) : 0B;
two (D.4292);
if (x != 0B && (integer(kind=4)[0:] *) x->data != (integer(kind=4)[0:] *)
D.4292)
{
_gfortran_internal_unpack (x, D.4292);
__builtin_free ((void *) D.4292);
}
}
but to my untrained eye it looked like the test x->data != D.4292 was
executed before x != 0B. This lead me to the following tentative patch:
diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 19d69aec9c0..06bfea1f461 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -9025,7 +9100,7 @@ gfc_conv_array_parameter (gfc_se * se, gfc_expr * expr,
bool g77,
fold_convert (TREE_TYPE (tmp), ptr), tmp);
if (fsym && fsym->attr.optional && sym && sym->attr.optional)
- tmp = fold_build2_loc (input_location, TRUTH_AND_EXPR,
+ tmp = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
logical_type_node,
gfc_conv_expr_present (sym), tmp);
Strangely enough, this gives exactly the same dump-tree!
(If someone knows why, please enlighten me!)
But it fixed the issue.
The I looked at the optimized tree. The reference version has:
<bb 4> [local count: 1073741824]:
# _5 = PHI <_19(3), y_17(D)(2)>
two (_5);
_1 = y_17(D) != 0B;
_2 = y_17(D)->data;
_3 = _2 != _5;
_4 = _1 & _3;
if (_4 != 0)
goto <bb 5>; [33.00%]
else
goto <bb 6>; [67.00%]
<bb 5> [local count: 354334800]:
__builtin_free (_5);
This explains the "boom".
The version with the ANDIF has:
<bb 4> [local count: 1073741824]:
# _2 = PHI <_16(3), y_14(D)(2)>
two (_2);
if (y_14(D) != 0B)
goto <bb 5>; [70.00%]
else
goto <bb 7>; [30.00%]
<bb 5> [local count: 751619280]:
_1 = y_14(D)->data;
if (_1 != _2)
goto <bb 6>; [53.47%]
else
goto <bb 7>; [46.53%]
<bb 6> [local count: 401890828]:
__builtin_free (_2);
This looks sane to me: the data component is not referenced if the dummy
is seen to be absent.
Regtesting...