simplifcation and parenthesis

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

simplifcation and parenthesis

Steve Kargl
All,

A quick peek at bugzilla reveals that Gerhard Steinmetz has awaken,
and he's submitted a number of high quality bug reports.  Unfortunately,
PR fortran/91553 and PR fortran/91566 suggest that simplification when
the operator is INTRINSIC_PARENTHESES broken.

   program pr91553
   print *, (1.0, 2.0) * [real :: (3.0 + 4.0)]
   end

% gfcx -c pr91553.f90
f951: internal compiler error: Segmentation fault
0xafee2f crash_signal
        ../../gcc9/gcc/toplev.c:326
0x5cd5c2 gfc_real2complex(gfc_expr*, int)
        ../../gcc9/gcc/fortran/arith.c:2258

% gdb path/to/f951
(gdb) b simplify.c:8508
(gdb) call debug(c->expr)       ! Thanks, Thomas for the debug() facility!
(parens 7.00000000) (REAL 4)

Notice the (parens ) here.  This is unexpected.

(gdb) p c->expr->expr_type
$1 = EXPR_OP
(gdb) call debug(c->expr->value.op.op1)
7.00000000 (REAL 4)

This is what we want.  This patch fixes the problem.

troutmask:sgk[214] svn diff ~/gcc/gccx/gcc/fortran/simplify.c
Index: /safe/sgk/gcc/gccx/gcc/fortran/simplify.c
===================================================================
--- /safe/sgk/gcc/gccx/gcc/fortran/simplify.c   (revision 275008)
+++ /safe/sgk/gcc/gccx/gcc/fortran/simplify.c   (working copy)
@@ -8504,7 +8508,12 @@ gfc_convert_constant (gfc_expr *e, bt type, int kind)
              if (c->expr->expr_type == EXPR_ARRAY)
                tmp = gfc_convert_constant (c->expr, type, kind);
              else
-               tmp = f (c->expr, kind);
+               {
+                 if (c->expr->expr_type == EXPR_OP
+                      && c->expr->value.op.op == INTRINSIC_PARENTHESES)
+                   gfc_simplify_expr (c->expr, 1);
+                 tmp = f (c->expr, kind);
+               }
            }
          else
            tmp = gfc_convert_constant (c->expr, type, kind);

So, now consider

   program pr91566
   print *, -merge([3,4], 0, [.false.,.true.])
   end

% gfcx -c pr91566.f90
f951: internal compiler error: Segmentation fault
0xafee2f crash_signal
        ../../gcc9/gcc/toplev.c:326
0x138b41c splay_tree_foreach
        ../../gcc9/libiberty/splay-tree.c:577
0x5e2114 gfc_constructor_copy(splay_tree_s*)
        ../../gcc9/gcc/fortran/constructor.c:103
0x5ca51b reduce_unary
        ../../gcc9/gcc/fortran/arith.c:1267
0x5ca53f reduce_unary

Debugging this one has been a bigger adventure.  One
ends up in gfc_simplify_merge().

(gdb) b simplify.c:4785
(gdb) run pr91566.f90
(gdb) call debug(result)
(parens 0) (INTEGER 4)

where again the parens are unexpected.  This is solved by this
patch

Index: /safe/sgk/gcc/gccx/gcc/fortran/simplify.c
===================================================================
--- /safe/sgk/gcc/gccx/gcc/fortran/simplify.c   (revision 275008)
+++ /safe/sgk/gcc/gccx/gcc/fortran/simplify.c   (working copy)
@@ -4780,8 +4780,12 @@ gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsour
   gfc_constructor *tsource_ctor, *fsource_ctor, *mask_ctor;
 
   if (mask->expr_type == EXPR_CONSTANT)
-    return gfc_get_parentheses (gfc_copy_expr (mask->value.logical
-                                              ? tsource : fsource));
+    {
+      result = gfc_copy_expr (mask->value.logical ? tsource : fsource);
+      result = gfc_get_parentheses (result);
+      gfc_simplify_expr (result, 1);
+      return result;
+    }
 
So, anyone have ideas how INTRINSIC_PARENTHESES got broken?

--
Steve