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

            Bug ID: 122935
           Summary: Missed optimization: Optimize (a * b) ==/!= 0 to
                    logical checks for signed/widening multiplications.
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chendongyan at isrc dot iscas.ac.cn
  Target Milestone: ---

This is a missed optimization feature report.

GCC currently misses an optimization opportunity to transform multiplication
equality checks (a * b) == 0 (and != 0) into logical checks of the operands.

Generally, multiplication operations have a significantly higher latency (cost)
than logical operations. Implementing this optimization can improve performance
by reducing the critical path latency and increasing instruction-level
parallelism.

Architectures that are known to benefit from this include x86-64 and RISC-V.

Compile the following code with -O3

```c
#include <stdbool.h>
#include <stdint.h>

bool foo(int32_t a, int32_t b) {
    return ((int32_t)a * b) == 0;
}

bool foo2(uint8_t a, uint8_t b) {
    return ((uint32_t)a * b) == 0;
}

bool foo3(int32_t a, int32_t b) {
    return ((int32_t)a * b) != 0;
}

```

x86-64:https://godbolt.org/z/xzEaq7Wa4
risc-v:https://godbolt.org/z/WvTEvohc5
(https://gcc.gnu.org/pipermail/gcc-patches/2025-November/702158.html)

This optimization is already implemented in Clang/LLVM.

Reply via email to