Issue 207926
Summary missed fold for bitop(x) | x in bool context
Labels new issue
Assignees
Reporter Disservin
    Here, `bitop(x)` is meant as shorthand for zero-preserving bit-operation builtins, such as `popcount`, `bitreverse`, or similar operations where `bitop(0) == 0` and `bitop(x) != 0` for all nonzero `x`.

For such operations, this _expression_:

```c
bitop(x) | x
```

is nonzero exactly when `x` is nonzero. So in a `bool` context, it can be folded to:

```c
x != 0
```

For example, `__builtin_popcountg(y)` is zero if `y == 0`, so both of these functions are equivalent to `return y != 0;`.

The simple case is optimized as expected:

```c
bool x(unsigned long long y) {
    return __builtin_popcountg(y);
}
```

```asm
x(unsigned long long):
  test rdi, rdi
  setne al
  ret
```

But the slightly more complex form does not appear to fold the same way:

```c
bool x(unsigned long long y) {
 return __builtin_popcountg(y) | y;
}
```

I guess this pattern is probably not common in the real world since the bitop is entirely unnecessary..
https://godbolt.org/z/8MPTbavqK
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to