Issue 115683
Summary Missed optimization: (y != 0 && x > (unsigned)(-1) / y) (multiplication overflow check)
Labels new issue
Assignees
Reporter Explorer09
    ```c
#include <limits.h>
#include <stdbool.h>

bool func1(unsigned long long x, unsigned long long y)
{
    return x > (ULLONG_MAX / y);
}

bool func2(unsigned long long x, unsigned long long y)
{
    return y != 0 && x > (ULLONG_MAX / y);
}

bool func3(unsigned long long x, unsigned long long y)
{
    unsigned long long res;
    return __builtin_umulll_overflow(x, y, &res);
}
```

Expected result: All three functions produce the same code.

Actual result: func1() and func3() optimize to same code, but func2() had a redundant (y != 0) check that is not optimized out.

x86-64 clang with "-Os" option (tested in Compiler Explorer, a.k.a. godbolt.org)

```x86asm
func1:
 movq    %rsi, %rax
        mulq    %rdi
        seto    %al
 retq

func2:
        testq   %rsi, %rsi
        je .LBB1_1
        movq    %rsi, %rax
        mulq    %rdi
        seto %al
        retq
.LBB1_1:
        xorl    %eax, %eax
 retq
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to