A conversion from pow to sqrt shall not call an errno-setting sqrt with -infinity: the sqrt will set EDOM where the pow call need not.
This patch avoids the erroneous (pun not intended) transformation by applying the restrictions discussed in the thread for https://lists.llvm.org/pipermail/llvm-dev/2020-September/145051.html.
The existing tests are updated (depending on emphasis in the checks for library calls, avoidance of overlap, and overall coverage):
- to add ninf, retaining the intended library call,
- to use the intrinsic, retaining the use of select, or
- to expect the replacement to not occur.
The following is tested:
- The pow intrinsic folds to a select instruction to handle -infinity.
- The pow library call folds, with ninf, to sqrt without the select instruction associated with handling -infinity.
- The pow library call does not fold to sqrt without ninf.
@spatel, I've added the isKnownNeverInfinity check here as suggested in your comment below. I am not entirely sure whether it is quite effective though. It seems this query may happening "too early".
Example case I tried (with opt -instcombine):