[AggressiveInstCombine] convert sqrt libcalls with "nnan" to sqrt intrinsics
authorSanjay Patel <spatel@rotateright.com>
Tue, 26 Jul 2022 19:31:12 +0000 (15:31 -0400)
committerSanjay Patel <spatel@rotateright.com>
Tue, 26 Jul 2022 19:50:14 +0000 (15:50 -0400)
commite3205b87655f07d70c7e7f8a86b2d009c0432e49
tree00b89d6848cba8c4784eb72cf8515a2e1a73e181
parent114df244ec50ce0145702974335965c3aa2c3dcc
[AggressiveInstCombine] convert sqrt libcalls with "nnan" to sqrt intrinsics

This is an alternate to D129155 that uses TTI.haveFastSqrt() to avoid a
potential miscompile for programs with reads of errno. Moving the transform
to AggressiveInstCombine provides access to TTI.

If a sqrt call has "nnan", that implies that the input argument is never
negative because sqrt of {negative number} --> NAN.
If the argument is never negative and the call can be lowered without a
libcall, then we can assume that errno accesses are unchanged after lowering,
so the call can be translated to the LLVM intrinsic (which is expected to
become inline code).

This affects codegen for targets like x86 that have sqrt instructions, but
still have to conservatively assume that a libcall may be needed to set
errno as shown in issue #52620 and issue #56383.

This patch won't solve those examples - we will need to extend this to use
CannotBeOrderedLessThanZero or similar, enhance that analysis for new
operators, and/or deal with llvm.assume too.

Differential Revision: https://reviews.llvm.org/D129167
llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
llvm/test/Transforms/AggressiveInstCombine/X86/sqrt.ll [new file with mode: 0644]