gfortran parses c = a + b + c as (assign c (+ (+ a b) c)) which is the same parse tree as for c = (a + b) + c Fortran allows a greater range of optimizations in the former case, but since it is unable to distinguish between those two cases, the former case may be unnecessarily pessimized by optimizers. I have a patch to the parser and parse-tree dumper which makes gfortran record the parentheses. It is covered by my copyright assignment to g95 as I prepared the patch in Oct 2002. Since I have no copyright assignment for gcc I'm not sure if I can update it for the current sources (mostly formatting and function name changes) without running into copyright trouble. Therefore I point you to my old patch, which I reposted to the gfortran list: http://gcc.gnu.org/ml/fortran/2004-03/msg00048.html with the matchexp.c part replaced by the patch from http://gcc.gnu.org/ml/fortran/2004-03/msg00046.html The original postings on g95-develop@lists.sourceforge.net can be found in the thread following this posting: http://www.geocrawler.com/archives/3/3588/2002/10/50/9910846/
(In reply to comment #0) > Fortran allows a greater range of optimizations in the former case, but since it > is unable to distinguish between those two cases, the former case may be > unnecessarily pessimized by optimizers. "it" refers to gfortran, not Fortran. Sorry for being unclear.
Confirmed, I thought I saw a dup of this bug somewhere.
changed subject line to the canonical form
I have updated my patch, copyright assignment is in the works. No need for anyone to work on this.
*** Bug 14741 has been marked as a duplicate of this bug. ***
Not recording parentheses does have correctness implications, see this thread: http://gcc.gnu.org/ml/fortran/2005-04/msg00302.html Upgrading the bug status accordingly. Looks like I have to revive my patch sometime soon.
(In reply to comment #4) > I have updated my patch, copyright assignment is in the works. No need for > anyone to work on this. Hum, Tobias, any news on that one? :)
I'll try to get to it in the next few days, it shouldn't be too hard.
For the record, Walt Brainerd's testcase from #6 is as follows: PROGRAM fc107 ! Submitted by Walt Brainerd, The Fortran Company ! GNU Fortran 95 (GCC 4.1.0 20050322 (experimental)) ! Windows XP ! Output should be 3 ! Output is 999 INTEGER I INTEGER TRYME I = 3 print *, tryme((i),i) END INTEGER FUNCTION TRYME(RTNME,HITME) INTEGER RTNME,HITME HITME = 999 TRYME = RTNME END This together with PR20894 and PR25048 is fixed by a patch I'm about to send, once I've finished ChangeLogs and testcases.
*** Bug 20894 has been marked as a duplicate of this bug. ***
Adding Walt Brainerd, as requested in <http://gcc.gnu.org/ml/fortran/2005-04/msg00344.html>. Patch posted here: <http://gcc.gnu.org/ml/fortran/2006-02/msg00172.html>
*** Bug 25048 has been marked as a duplicate of this bug. ***
Subject: Bug 14771 Author: tobi Date: Fri Feb 10 00:10:47 2006 New Revision: 110819 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=110819 Log: fortran/ 2006-02-09 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de> PR fortran/14771 * gfortran.h (gfc_intrinsic_op): Add INTRINSIC_PARENTHESES. * dump-parse-tree (gfc_show_expr): Handle INTRINSIC_PARENTHESES. * expr.c (simplify_intrinsic_op): Treat INTRINSIC_PARENTHESES as if it were INTRINSIC_UPLUS. * resolve.c (resolve_operator): Handle INTRINSIC_PARENTHESES. * match.c (intrinsic_operators): Add INTRINSIC_PARENTHESES. * matchexp.c (match_primary): Record parentheses surrounding numeric expressions. * module.c (intrinsics): Add INTRINSIC_PARENTHESES for module dumping. * trans-expr.c (gfc_conv_expr_op): Handle INTRINSIC_PARENTHESES. testsuite/ 2006-02-09 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de> Paul Thomas <pault@gcc.gnu.org> PR fortran/14771 * gfortran.dg/parens_1.f90: New. * gfortran.dg/parens_2.f90: New. * gfortran.dg/parens_3.f90: New. Added: trunk/gcc/testsuite/gfortran.dg/parens_1.f90 trunk/gcc/testsuite/gfortran.dg/parens_2.f90 trunk/gcc/testsuite/gfortran.dg/parens_3.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/dump-parse-tree.c trunk/gcc/fortran/expr.c trunk/gcc/fortran/gfortran.h trunk/gcc/fortran/match.c trunk/gcc/fortran/matchexp.c trunk/gcc/fortran/module.c trunk/gcc/fortran/resolve.c trunk/gcc/fortran/trans-expr.c trunk/gcc/testsuite/ChangeLog
Subject: Bug 14771 Author: tobi Date: Fri Feb 10 18:08:27 2006 New Revision: 110840 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=110840 Log: fortran/ PR fortran/14771 * arith.c (eval_intrinsic): Accept INTRINSIC_PARENTHESES. * expr.c (check_intrinsic_op): Likewise. * module.c (mio_expr): Likewise. testsuite/ PR fortran/14771 * gfortran.dg/parens_4.f90: New. * gfortran.dg/parens_5.f90: New. Added: trunk/gcc/testsuite/gfortran.dg/parens_4.f90 trunk/gcc/testsuite/gfortran.dg/parens_5.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/arith.c trunk/gcc/fortran/expr.c trunk/gcc/fortran/module.c trunk/gcc/testsuite/ChangeLog
Subject: Bug 14771 Author: tobi Date: Sun Feb 12 18:31:40 2006 New Revision: 110894 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=110894 Log: fortran/ Backport r110819 and r110840 from the trunk PR fortran/14771 * gfortran.h (gfc_intrinsic_op): Add INTRINSIC_PARENTHESES. * dump-parse-tree (gfc_show_expr): Handle INTRINSIC_PARENTHESES. * expr.c (simplify_intrinsic_op): Treat INTRINSIC_PARENTHESES as if it were INTRINSIC_UPLUS. * resolve.c (resolve_operator): Handle INTRINSIC_PARENTHESES. * match.c (intrinsic_operators): Add INTRINSIC_PARENTHESES. * matchexp.c (match_primary): Record parentheses surrounding numeric expressions. * module.c (intrinsics): Add INTRINSIC_PARENTHESES for module dumping. * trans-expr.c (gfc_conv_expr_op): Handle INTRINSIC_PARENTHESES. PR fortran/14771 * arith.c (eval_intrinsic): Accept INTRINSIC_PARENTHESES. * expr.c (check_intrinsic_op): Likewise. * module.c (mio_expr): Likewise. testsuite/ Backport r110819 and r110840 from the trunk PR fortran/14771 * gfortran.dg/parens_1.f90: New. * gfortran.dg/parens_2.f90: New. * gfortran.dg/parens_3.f90: New. * gfortran.dg/parens_4.f90: New. * gfortran.dg/parens_5.f90: New. Added: branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/parens_1.f90 - copied unchanged from r110819, trunk/gcc/testsuite/gfortran.dg/parens_1.f90 branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/parens_2.f90 - copied unchanged from r110819, trunk/gcc/testsuite/gfortran.dg/parens_2.f90 branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/parens_3.f90 - copied unchanged from r110819, trunk/gcc/testsuite/gfortran.dg/parens_3.f90 branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/parens_4.f90 - copied unchanged from r110840, trunk/gcc/testsuite/gfortran.dg/parens_4.f90 branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/parens_5.f90 - copied unchanged from r110840, trunk/gcc/testsuite/gfortran.dg/parens_5.f90 Modified: branches/gcc-4_1-branch/gcc/fortran/ChangeLog branches/gcc-4_1-branch/gcc/fortran/arith.c branches/gcc-4_1-branch/gcc/fortran/dump-parse-tree.c branches/gcc-4_1-branch/gcc/fortran/expr.c branches/gcc-4_1-branch/gcc/fortran/gfortran.h branches/gcc-4_1-branch/gcc/fortran/match.c branches/gcc-4_1-branch/gcc/fortran/matchexp.c branches/gcc-4_1-branch/gcc/fortran/module.c branches/gcc-4_1-branch/gcc/fortran/resolve.c branches/gcc-4_1-branch/gcc/fortran/trans-expr.c branches/gcc-4_1-branch/gcc/testsuite/ChangeLog
Fixed.