https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89134
Feng Xue <innat_xue at hotmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |innat_xue at hotmail dot com --- Comment #7 from Feng Xue <innat_xue at hotmail dot com> --- 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.