https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89134
--- Comment #8 from Feng Xue <innat_xue at hotmail dot com> --- My mistake, transformation should be: void f (std::map<int, int> m) { for (auto it = m.begin (); it != m.end (); ++it) { if (b) { b = do_something(); } else { ++it; break; } } for (; it != m.end (); ++it); /* get an empty loop */ } (In reply to Feng Xue from comment #7) > Even loop contains calls with side effects, but only in one condition branch > path, we can still do some kind of optimization. > > Suppose a loop as: > > void f (std::map<int, int> m) > { > for (auto it = m.begin (); it != m.end (); ++it) { > if (b) { > b = do_something(); /* Has effect on b */ > } else { > /* No effect on b */ > } > } > } > > We can transform it to: > > void f (std::map<int, int> m) > { > for (auto it = m.begin (); it != m.end (); ++it) { > if (b) { > b = do_something(); > ++it; > break; > } > } > > for (; it != m.end (); ++it); /* get an empty loop */ > } > > This code takes less computation, especially when 'b' is always evaluated to > be false.