https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115924

            Bug ID: 115924
           Summary: Failure to optimize back and forth shifts of several
                    operands by the same amount with operations in the
                    middle to ands
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

#include <stdint.h>

int32_t f(int32_t i2, int32_t aa)
{
    return ((i2 >> 17) + (aa >> 17)) << 17;
}

int32_t f2(int32_t i2, int32_t aa)
{
    return ((i2 & -(1 << 17)) + aa) & -(1 << 17);
}

bool f3(int32_t i2, int32_t aa)
{
    return f(i2, aa) == f2(i2, aa);
}

f can be optimized to f2, and consequently f3 to `return true;`. This
transformation is done by LLVM, but not by GCC.

(note: this seems logically generalizable to amounts other than 17, and pretty
much any operation after right shifting the two operands before shifting them
back (e.g. replacing `+` with `-`, `^` or many other more complex operations))

Reply via email to