[Bug tree-optimization/89134] A missing optimization opportunity for a simple branch in loop

innat_xue at hotmail dot com gcc-bugzilla@gcc.gnu.org
Fri Feb 1 02:20:00 GMT 2019


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.


More information about the Gcc-bugs mailing list