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

            Bug ID: 98703
           Summary: Failure to optimize out non-zero check relative to
                    multiplication overflow check
           Product: gcc
           Version: 11.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: ---

bool f1(unsigned x, unsigned y, unsigned *res)
{
    return __builtin_mul_overflow(x, y, res) && x;
}

This can be optimized to `return __builtin_mul_overflow(x, y, res);`. This
transformation is done by LLVM, but not by GCC.

PS: I originally found this while looking at the code generation for this code
(from https://gcc.gnu.org/PR95852) :

bool f2(unsigned x, unsigned y, unsigned *res)
{
    *res = x * y;
    return x && ((*res / x) != y);
}

f2 is equivalent to `return __builtin_mul_overflow(x, y, res);`, but the code
emitted is more like f1.

Reply via email to