Rewrite loops to use ireduce where possible