https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30409
--- Comment #10 from kargl at gcc dot gnu.org --- (In reply to anlauf from comment #8) > The suggested optimization needs to take into account that the evaluation > of the temporary expression might trap, or that allocatable variables are > not allocated, etc. > > The trap etc. would not occur if the trip count of the loop is zero for the > non-hoisted variant, so we need to make sure not to generate failing code > for the hoisted one. > > Similarly, for conditional code in the loop body, like > > if (cond) then > expression1 (..., 1/y) > else > expression2 (..., 1/z) > end if > > where cond protects from traps even for finite trip counts, these conditions > may also need to be identified, and an appropriate block generated. I'm not sure what you are worried about here. If one has do i = 1, n ... = expression1(..., 1/y) end do then this is equivalent to do i = 1, n tmp = 1 / y ... = expression1(..., tmp) end do which is equivalent to tmp = 1 / y do i = 1, n ... = expression1(..., tmp) end do I suppose I could do something exceedingly stupid such as function expression1(..., xxx) common /foo/y y = 0 ... end but this would then lead to invalid Fortran when i = 2 in the above initial loops as (1/y) is invalid Fortran if y = 0.