Hi Mikael, thanks a lot for your comments! >> + actual_arglist->expr = gfc_copy_expr (e); >> + actual_arglist->next = gfc_get_actual_arglist (); > Another one is needed. I get a segmentation fault with SUM. Fixed by using gfc_build_intrisic_call. I have also put SUM into the test case. >> + if (dim != NULL) >> + { > Minor, but I think you can assume dim != NULL. Same for mask. Fixed. >> + /* We changed things from under the expression walker. Walking the >> + old tree would mess up things, so let's not do that. */ >> + return 1; > I think this prevents any further reduction optimization. The following > variant of your test case doesn't avoid the temporary: You're right; I also could not come up with a test case where this didn't work. I have put this > do i=1,3 > if (any([abs(a(i,1) - b(i,1)) > acc, & > (j==i+1,j=3,8)])) cycle > if (any([abs(a(i,2) - b(i,2)) > acc, & > abs(a(i,3) - b(i,3)) > acc, lo(i,:)])) cycle > c = c + i > end do into the test case. Updated test case and patch attached. OK for trunk? Thomas 2013-01-13 Thomas Koenig PR fortran/55806 * frontend-passes.c (optimize_reduction): New function, including prototype. (callback_reduction): Likewise. (gfc_run_passes): Also run optimize_reduction. (copy_walk_reduction_arg): New function. (dummy_code_callback): New function. 2013-01-13 Thomas Koenig PR fortran/55806 * gfortran.dg/array_constructor_40.f90: New test.