Bug 35745 - Divide incorrectly extracted from WHERE block?; run time abort
Summary: Divide incorrectly extracted from WHERE block?; run time abort
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2008-03-28 21:44 UTC by Dick Hendrickson
Modified: 2008-05-17 07:17 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-04-01 07:24:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dick Hendrickson 2008-03-28 21:44:01 UTC
The following program aborts at run-time opening a 
box that says
"a.exe has encountered a problem and needs to close.  
We are sorry for the inconvenience."

And then offers to send an error report to Microsoft.
I believe the problem is the extraction of the 1/NF0
from within the WHERE block.
    
      program RZ0048
      INTEGER IDA(10)
      REAL RDA(10)

      RDA    = 1.0

      nf0 = 3
      WHERE (RDA < -15.0)
        IDA = 1/NF0 + 2
      ENDWHERE
      print *, 'first where completed'

      nf0 = 0

      WHERE (RDA < -15.0)
        IDA = 1/NF0 + 2
      ENDWHERE

      END
Comment 1 Tobias Burnus 2008-03-28 22:05:10 UTC
Confirm.

Valgrind shows:

Process terminating with default action of signal 8 (SIGFPE): dumping core
  Integer divide by zero at address 0x40274EADB
    at 0x4008C6: MAIN__ (ghfhgk.f90:15)

> Divide incorrectly extracted from WHERE block?
Yes and no. The compiler essentially transforms (as -fdump-tree-original shows)

      WHERE (RDA < -15.0)
        IDA = 1/NF0 + 2
      ENDWHERE

into

      TMP = 1/NF0 + 2
      WHERE (RDA < -15.0)
        IDA = TMP
      ENDWHERE

which is the reason for the integer divide by zero and shows that moving constant expressions out of a loop is not always a good idea.
Comment 2 Tobias Burnus 2008-03-29 15:52:04 UTC
See also PR 35756.
Comment 3 Paul Thomas 2008-05-16 21:12:58 UTC
Subject: Bug 35745

Author: pault
Date: Fri May 16 21:12:04 2008
New Revision: 135443

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135443
Log:
2008-05-16  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/35756
	PR fortran/35759
	* trans-stmt.c (gfc_trans_where): Tighten up the dependency
	check for calling gfc_trans_where_3.

	PR fortran/35743
	* trans-stmt.c (gfc_trans_where_2): Set the mask size to zero
	if it is calculated to be negative.

	PR fortran/35745
	* trans-stmt.c (gfc_trans_where_3, gfc_trans_where_assign): Set
	ss->where for scalar right hand sides.
	* trans-array.c (gfc_add_loop_ss_code): If ss->where is set do
	not evaluate scalars outside the loop.  Clean up whitespace.
	* trans.h : Add a bitfield 'where' to gfc_ss.

2008-05-16  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/35756
	PR fortran/35759
	* gfortran.dg/where_1.f90: New test.

	PR fortran/35743
	PR fortran/35745
	* gfortran.dg/where_2.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/where_1.f90
    trunk/gcc/testsuite/gfortran.dg/where_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 4 Paul Thomas 2008-05-17 07:11:12 UTC
Subject: Bug 35745

Author: pault
Date: Sat May 17 07:10:13 2008
New Revision: 135461

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=135461
Log:
2008-05-17  Paul Thomas  <pault@gcc.gnu.org>

	Backport from mainline:
	PR fortran/35756
	PR fortran/35759
	* trans-stmt.c (gfc_trans_where): Tighten up the dependency
	check for calling gfc_trans_where_3.

	PR fortran/35743
	* trans-stmt.c (gfc_trans_where_2): Set the mask size to zero
	if it is calculated to be negative.

	PR fortran/35745
	* trans-stmt.c (gfc_trans_where_3, gfc_trans_where_assign): Set
	ss->where for scalar right hand sides.
	* trans-array.c (gfc_add_loop_ss_code): If ss->where is set do
	not evaluate scalars outside the loop.  Clean up whitespace.
	* trans.h : Add a bitfield 'where' to gfc_ss.

       PR fortran/36233
       * interface.c (compare_actual_formal): Do not check sizes if the
       actual is BT_PROCEDURE.

2008-05-17  Paul Thomas  <pault@gcc.gnu.org>

	Backport from mainline:
	PR fortran/35756
	PR fortran/35759
	* gfortran.dg/where_1.f90: New test.

	PR fortran/35743
	PR fortran/35745
	* gfortran.dg/where_2.f90: New test.

       PR fortran/36233
       * gfortran.dg/actual_procedure_1.f90: New test

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/actual_procedure_1.f90
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/where_1.f90
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/where_2.f90
Modified:
    branches/gcc-4_3-branch/gcc/fortran/ChangeLog
    branches/gcc-4_3-branch/gcc/fortran/interface.c
    branches/gcc-4_3-branch/gcc/fortran/trans-array.c
    branches/gcc-4_3-branch/gcc/fortran/trans-stmt.c
    branches/gcc-4_3-branch/gcc/fortran/trans.h
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 5 Paul Thomas 2008-05-17 07:17:42 UTC
Fixed on trunk and 4.3.

Thanks for the report.

Paul