Consider program main integer :: a(100), b(100), c(100) a = 2 do i=1,10 b = a c = 1/b print *,c end do end program main
The statements b = a c = 1/b could be moved out of the loop. (The second one still has the problem that we currently don't know that print doesn't read values). Currently, this doesn't happen, as a dump shows: <bb 4>: # ivtmp.13_40 = PHI <10(3), ivtmp.13_39(6)> MEM[(c_char * {ref-all})&b] = MEM[(c_char * {ref-all})&a]; <bb 5>: # ivtmp.24_8 = PHI <ivtmp.24_45(5), 0(4)> D.1598_11 = MEM[symbol: b, index: ivtmp.24_8, offset: 0B]; D.1599_12 = 1 / D.1598_11; MEM[symbol: c, index: ivtmp.24_8, offset: 0B] = D.1599_12; ivtmp.24_45 = ivtmp.24_8 + 4; if (ivtmp.24_45 == 400) goto <bb 6>; else goto <bb 5>; <bb 6>: dt_parm.2.common.filename = &"foo.f90"[1]{lb: 1 sz: 1}; dt_parm.2.common.line = 7; dt_parm.2.common.flags = 128; dt_parm.2.common.unit = 6; _gfortran_st_write (&dt_parm.2); parm.3.dtype = 265; parm.3.dim[0].lbound = 1; parm.3.dim[0].ubound = 100; parm.3.dim[0].stride = 1; parm.3.data = &c[0]; parm.3.offset = -1; _gfortran_transfer_array (&dt_parm.2, &parm.3, 4, 0); _gfortran_st_write_done (&dt_parm.2); ivtmp.13_39 = ivtmp.13_40 - 1; if (ivtmp.13_39 == 0) goto <bb 7>; else goto <bb 4>; <bb 7>: return; Not sure if this is better handled in the front or middle end, though. -- Summary: Move array assignments out of loop Product: gcc Version: 4.6.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: enhancement Priority: P3 Component: fortran AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: tkoenig at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45676