Re: Automatically transform or expand do loop in a subroutine



On 2008-01-31, yaqi <yaqiwang@xxxxxxxxx> wrote:

***, are you sure compilers can handle this case?

I modified your program by making the function internal (which
makes inlining much easier), then feeding ito to gfortran.
Here's the modified program (I had to add the I/O statements
to avoid optimizing the whole program away :-)

program main
integer :: e, c
read (*,*) c
e = ad(c, 10)
print *,e
stop
contains
integer function ad(c,d)
implicit none
integer :: c
integer :: d
integer :: i
do i=1,d
c = c + i
end do
ad = c
return
end function ad
end program main

Here's what "gfortran -O3 -fdump-tree-optimized" made of it:

main ()
{
integer(kind=4) c_lsm.57;
integer(kind=4) c_lsm.56;
struct __st_parameter_dt dt_parm.2;
struct __st_parameter_dt dt_parm.1;
static integer(kind=4) options.0[7] = {68, 127, 0, 0, 0, 1, 0};
integer(kind=4) e;
integer(kind=4) c;

<bb 2>:
_gfortran_set_options (7, &options.0);
dt_parm.1.common.filename = &"foo.f90"[1]{lb: 1 sz: 1};
dt_parm.1.common.line = 3;
dt_parm.1.common.flags = 128;
dt_parm.1.common.unit = 5;
_gfortran_st_read (&dt_parm.1);
_gfortran_transfer_integer (&dt_parm.1, &c, 4);
_gfortran_st_read_done (&dt_parm.1);
c_lsm.56 = c;
c_lsm.57 = c_lsm.56 + 55;
c = c_lsm.57;
e = c_lsm.57;
dt_parm.2.common.filename = &"foo.f90"[1]{lb: 1 sz: 1};
dt_parm.2.common.line = 5;
dt_parm.2.common.flags = 128;
dt_parm.2.common.unit = 6;
_gfortran_st_write (&dt_parm.2);
_gfortran_transfer_integer (&dt_parm.2, &e, 4);
_gfortran_st_write_done (&dt_parm.2);
_gfortran_stop_numeric (-1);

}

The loop is optimized into an addition, which isn't bad. This
doesn't work for non-constant d, though.
.