https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103559
Bug ID: 103559
Summary: Can't optimize away < 0 check on sqrt
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: llvm at rifkin dot dev
Target Milestone: ---
For a simple invocation of sqrt, gcc inserts a < 0 check to set math errno if
needed. E.g.
float f(float x) {
return sqrt(x);
}
Is generated as
f(float):
vxorps xmm1, xmm1, xmm1
vucomiss xmm1, xmm0
ja .L10
vsqrtss xmm0, xmm0, xmm0
ret
.L10:
jmp sqrtf
Unfortunately, this check is still present when the GCC is able to prove that x
is non-negative:
float f(float x) {
if(x < 0) [[unlikely]] {
__builtin_unreachable();
} else {
return sqrt(x);
}
}
LLVM suffers from the same problem, even with __builtin_assume().
https://godbolt.org/z/ddcoMj3oz
This is a very common pattern, and I'd imagine the argument for sqrt is often
able to be shown to be positive. This would be a helpful enhancement.