http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38318
--- Comment #9 from Marc Glisse <glisse at gcc dot gnu.org> --- (In reply to Joost VandeVondele from comment #8) > Marc, looks like the fortran FE changed a lot since this bug was filed, and > there is no explicit allocate anymore, in fact the variable is created on > stack by the frontend... Cool, the best optimizations are those you don't need to do ;-) > So now, the proper testcase would be: > > cat PR38318-3.f90 > SUBROUTINE S1(N,A) > REAL :: A(3) > REAL, DIMENSION(:), ALLOCATABLE :: B > DO I=1,N > ALLOCATE(B(3)) > B=-A > CALL S2(B) > DEALLOCATE(B) > ENDDO > END SUBROUTINE > > which really should contain any call to _gfortran_runtime_error_at, > _gfortran_os_error, __builtin_malloc, __builtin_free if all were perfect, > and certainly not in the loop Ok. If you used __builtin_abort instead of _gfortran_os_error, I think my current patch would handle it. It is hard for gcc to guess that _gfortran_os_error is safe. On the other hand, if I special case the test if(VAR==0) as mentioned in a comment in my patch, it won't look at that branch anymore and the optimization should apply. Er, no, I missed the call to s2. I would also need some attribute on s2 so the compiler knows that s2 doesn't do anything too weird. Hopefully, when the compiler has the sources for s2, we could later let it guess those attributes...