Skip to content

Commit 554b9c4

Browse files
Fix tests and handle signed zeros
1 parent 542ae6f commit 554b9c4

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,37 +1962,45 @@ Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
19621962
Value *Real, *Imag;
19631963

19641964
if (CI->arg_size() == 1) {
1965+
1966+
if (!CI->isFast())
1967+
return nullptr;
1968+
19651969
Value *Op = CI->getArgOperand(0);
19661970
assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
1971+
19671972
Real = B.CreateExtractValue(Op, 0, "real");
19681973
Imag = B.CreateExtractValue(Op, 1, "imag");
19691974

19701975
} else {
19711976
assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
1977+
19721978
Real = CI->getArgOperand(0);
19731979
Imag = CI->getArgOperand(1);
1974-
}
19751980

1976-
// if real or imaginary part is zero, simplify to abs(cimag(z))
1977-
// or abs(creal(z))
1978-
if (ConstantFP *ConstReal = dyn_cast<ConstantFP>(Real)) {
1979-
if (ConstReal->isZeroValue()) {
1980-
IRBuilderBase::FastMathFlagGuard Guard(B);
1981-
B.setFastMathFlags(CI->getFastMathFlags());
1982-
return copyFlags(
1983-
*CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, Imag, nullptr, "cabs"));
1981+
// if real or imaginary part is zero, simplify to abs(cimag(z))
1982+
// or abs(creal(z))
1983+
Value *AbsOp = nullptr;
1984+
if (ConstantFP *ConstReal = dyn_cast<ConstantFP>(Real)) {
1985+
if (ConstReal->isZero())
1986+
AbsOp = Imag;
1987+
1988+
} else if (ConstantFP *ConstImag = dyn_cast<ConstantFP>(Imag)) {
1989+
if (ConstImag->isZero())
1990+
AbsOp = Real;
19841991
}
1985-
} else if (ConstantFP *ConstReal = dyn_cast<ConstantFP>(Imag)) {
1986-
if (ConstReal->isZeroValue()) {
1992+
1993+
if (AbsOp) {
19871994
IRBuilderBase::FastMathFlagGuard Guard(B);
19881995
B.setFastMathFlags(CI->getFastMathFlags());
1996+
19891997
return copyFlags(
1990-
*CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, Real, nullptr, "cabs"));
1998+
*CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, AbsOp, nullptr, "cabs"));
19911999
}
1992-
}
19932000

1994-
if (!CI->isFast())
1995-
return nullptr;
2001+
if (!CI->isFast())
2002+
return nullptr;
2003+
}
19962004

19972005
// Propagate fast-math flags from the existing call to new instructions.
19982006
IRBuilderBase::FastMathFlagGuard Guard(B);

llvm/test/Transforms/InstCombine/cabs-discrete.ll

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ define double @fast_cabs_zero_real(double %imag) {
4949
ret double %call
5050
}
5151

52+
define double @fast_cabs_neg_zero_real(double %imag) {
53+
; CHECK-LABEL: @fast_cabs_neg_zero_real(
54+
; CHECK-NEXT: [[CABS:%.*]] = tail call double @llvm.fabs.f64(double [[IMAG:%.*]])
55+
; CHECK-NEXT: ret double [[CABS]]
56+
;
57+
%call = tail call double @cabs(double -0.0, double %imag)
58+
ret double %call
59+
}
60+
5261
define double @fast_cabs_zero_imag(double %real) {
5362
; CHECK-LABEL: @fast_cabs_zero_imag(
5463
; CHECK-NEXT: [[CABS:%.*]] = tail call double @llvm.fabs.f64(double [[REAL:%.*]])
@@ -79,9 +88,18 @@ define float @fast_cabsf_zero_real(float %imag) {
7988
ret float %call
8089
}
8190

91+
define float @fast_cabsf_neg_zero_real(float %imag) {
92+
; CHECK-LABEL: @fast_cabsf_neg_zero_real(
93+
; CHECK-NEXT: [[CABS:%.*]] = tail call float @llvm.fabs.f32(float [[IMAG:%.*]])
94+
; CHECK-NEXT: ret float [[CABS]]
95+
;
96+
%call = tail call float @cabsf(float -0.0, float %imag)
97+
ret float %call
98+
}
99+
82100
define float @fast_cabsf_zero_imag(float %real) {
83101
; CHECK-LABEL: @fast_cabsf_zero_imag(
84-
; CHECK-NEXT: [[CABS:%.*]] = tail call float @llvm.fabs.f64(float [[REAL:%.*]])
102+
; CHECK-NEXT: [[CABS:%.*]] = tail call float @llvm.fabs.f32(float [[REAL:%.*]])
85103
; CHECK-NEXT: ret float [[CABS]]
86104
;
87105
%call = tail call float @cabsf(float %real, float 0.0)
@@ -102,10 +120,10 @@ define fp128 @fast_cabsl(fp128 %real, fp128 %imag) {
102120

103121
define fp128 @fast_cabsl_zero_real(fp128 %imag) {
104122
; CHECK-LABEL: @fast_cabsl_zero_real(
105-
; CHECK-NEXT: [[CABS:%.*]] = tail call fp128 @llvm.fabsl.f128(fp128 [[IMAG:%.*]])
123+
; CHECK-NEXT: [[CABS:%.*]] = tail call fp128 @llvm.fabs.f128(fp128 [[IMAG:%.*]])
106124
; CHECK-NEXT: ret fp128 [[CABS]]
107125
;
108-
%call = tail call fp128 @cabsl(double 0.0, fp128 %imag)
126+
%call = tail call fp128 @cabsl(fp128 0xL00000000000000000000000000000000, fp128 %imag)
109127
ret fp128 %call
110128
}
111129

@@ -114,7 +132,16 @@ define fp128 @fast_cabsl_zero_imag(fp128 %real) {
114132
; CHECK-NEXT: [[CABS:%.*]] = tail call fp128 @llvm.fabs.f128(fp128 [[REAL:%.*]])
115133
; CHECK-NEXT: ret fp128 [[CABS]]
116134
;
117-
%call = tail call fp128 @cabsl(fp128 %real, fp128 0.0)
135+
%call = tail call fp128 @cabsl(fp128 %real, fp128 0xL00000000000000000000000000000000)
136+
ret fp128 %call
137+
}
138+
139+
define fp128 @fast_cabsl_neg_zero_imag(fp128 %real) {
140+
; CHECK-LABEL: @fast_cabsl_neg_zero_imag(
141+
; CHECK-NEXT: [[CABS:%.*]] = tail call fp128 @llvm.fabs.f128(fp128 [[REAL:%.*]])
142+
; CHECK-NEXT: ret fp128 [[CABS]]
143+
;
144+
%call = tail call fp128 @cabsl(fp128 %real, fp128 0xL80000000000000000000000000000000)
118145
ret fp128 %call
119146
}
120147

0 commit comments

Comments
 (0)