Skip to content

Commit

Permalink
[EarlyCSE][ConstantFolding] do not constant fold atan2(+/-0.0, +/-0.0)
Browse files Browse the repository at this point in the history
These may raise an error (set errno) as discussed in the post-commit
comments for D127964, so we can't fold away the call and potentially
alter that behavior.
  • Loading branch information
rotateright committed Aug 19, 2022
1 parent fe0f72d commit 7f1262a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 5 additions & 4 deletions llvm/lib/Analysis/ConstantFolding.cpp
Expand Up @@ -3371,10 +3371,11 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
case LibFunc_atan2:
case LibFunc_atan2f:
case LibFunc_atan2l:
// POSIX, GLIBC and MSVC dictate atan2(0,0) is 0 and no error is raised.
// C11 says that a domain error may optionally occur.
// This is consistent with both.
return true;
// Although IEEE-754 says atan2(+/-0.0, +/-0.0) are well-defined, and
// GLIBC and MSVC do not appear to raise an error on those, we
// cannot rely on that behavior. POSIX and C11 say that a domain error
// may occur, so allow for that possibility.
return !Op0.isZero() || !Op1.isZero();

default:
break;
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/Transforms/EarlyCSE/atan.ll
Expand Up @@ -50,32 +50,44 @@ define x86_fp80 @atanl_x86(x86_fp80 %x) {
ret x86_fp80 %call
}

; This is not folded because it is known to set errno on some systems.

define float @callatan2_00() {
; CHECK-LABEL: @callatan2_00(
; CHECK-NEXT: [[CALL:%.*]] = call float @atan2f(float 0.000000e+00, float 0.000000e+00)
; CHECK-NEXT: ret float 0.000000e+00
;
%call = call float @atan2f(float 0.0, float 0.0)
ret float %call
}

; This is not folded because it is known to set errno on some systems.

define float @callatan2_n00() {
; CHECK-LABEL: @callatan2_n00(
; CHECK-NEXT: [[CALL:%.*]] = call float @atan2f(float -0.000000e+00, float 0.000000e+00)
; CHECK-NEXT: ret float -0.000000e+00
;
%call = call float @atan2f(float -0.0, float 0.0)
ret float %call
}

; This is not folded because it is known to set errno on some systems.

define float @callatan2_0n0() {
; CHECK-LABEL: @callatan2_0n0(
; CHECK-NEXT: [[CALL:%.*]] = call float @atan2f(float 0.000000e+00, float -0.000000e+00)
; CHECK-NEXT: ret float 0x400921FB60000000
;
%call = call float @atan2f(float 0.0, float -0.0)
ret float %call
}

; This is not folded because it is known to set errno on some systems.

define float @callatan2_n0n0() {
; CHECK-LABEL: @callatan2_n0n0(
; CHECK-NEXT: [[CALL:%.*]] = call float @atan2f(float -0.000000e+00, float -0.000000e+00)
; CHECK-NEXT: ret float 0xC00921FB60000000
;
%call = call float @atan2f(float -0.0, float -0.0)
Expand Down

0 comments on commit 7f1262a

Please sign in to comment.