| Issue |
235785
|
| Summary |
[SimpleLoopUnswitch] Branchless select is unswitched into a branch at -O3
|
| Labels |
loopoptim,
missed-optimization
|
| Assignees |
|
| Reporter |
chfast
|
```c
typedef unsigned long u64;
// r = x < m ? x : x - m
void reduce(u64 *r, const u64 *x, const u64 *m)
{
u64 d[2];
u64 c = 0;
for (int i = 0; i < 2; ++i)
d[i] = __builtin_subcl(x[i], m[i], c, &c);
u64 mask = 0 - c;
for (int i = 0; i < 2; ++i)
r[i] = (x[i] & mask) | (d[i] & ~mask);
}
```
https://godbolt.org/z/Mh471drbK
`-O2` gives `sub; sbb; cmovb; cmovb`. `-O3` branches on the borrow (`jae`) instead, and the borrow chain gets a `setb; bt` in the middle.
The masking in the second loop becomes a `select` on the loop-invariant borrow, and non-trivial loop unswitching (enabled only at -O3) unswitches the loop on it before the loop is unrolled. With `-mllvm -unswitch-threshold=0` the -O3 code is the same as at -O2.
When the borrow is unpredictable this adds a mispredicted branch. Found in intx (multi-precision integers): a 256-bit modular addition with random x, y < m gets 0.43 branch misses and 51 instead of 39 cycles per op at -O3.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs