Skip to content

Commit a67ba80

Browse files
committed
[SimplifyLibCalls] Constant fold fdim
Signed-off-by: Kushal Pal <[email protected]>
1 parent 29168e8 commit a67ba80

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class LibCallSimplifier {
212212
Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
213213
Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
214214
Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
215+
Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
215216
// Wrapper for all floating point library call optimizations
216217
Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
217218
IRBuilderBase &B);

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,50 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
31093109
return ConstantFP::get(CI->getType(), Rem);
31103110
}
31113111

3112+
/// Constant folds fdim
3113+
Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
3114+
// Cannot perform the fold unless the call has attribute memory(none)
3115+
if (!CI->doesNotAccessMemory())
3116+
return nullptr;
3117+
3118+
// TODO : Handle undef values
3119+
// propagate poison if any
3120+
if (isa<PoisonValue>(CI->getArgOperand(0)))
3121+
return CI->getArgOperand(0);
3122+
if (isa<PoisonValue>(CI->getArgOperand(1)))
3123+
return CI->getArgOperand(1);
3124+
3125+
const APFloat *X, *Y;
3126+
// Check if both values are constants
3127+
if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3128+
!match(CI->getArgOperand(1), m_APFloat(Y)))
3129+
return nullptr;
3130+
3131+
// If either argument is NaN, NaN is returned
3132+
if (X->isNaN())
3133+
return ConstantFP::get(CI->getType(), X->makeQuiet());
3134+
if (Y->isNaN())
3135+
return ConstantFP::get(CI->getType(), Y->makeQuiet());
3136+
3137+
// if X - Y overflows, it will set the errno, so we avoid the fold
3138+
APFloat Difference = *X;
3139+
APFloat::opStatus Status =
3140+
Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
3141+
switch (Status) {
3142+
case APFloat::opStatus::opOK:
3143+
case APFloat::opStatus::opInexact:
3144+
case APFloat::opStatus::opUnderflow:
3145+
break;
3146+
case APFloat::opStatus::opOverflow:
3147+
case APFloat::opStatus::opInvalidOp:
3148+
case APFloat::opStatus::opDivByZero:
3149+
return nullptr;
3150+
}
3151+
APFloat MaxVal =
3152+
maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
3153+
return ConstantFP::get(CI->getType(), MaxVal);
3154+
}
3155+
31123156
//===----------------------------------------------------------------------===//
31133157
// Integer Library Call Optimizations
31143158
//===----------------------------------------------------------------------===//
@@ -4042,6 +4086,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
40424086
if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
40434087
return optimizeBinaryDoubleFP(CI, Builder, TLI);
40444088
return nullptr;
4089+
case LibFunc_fdim:
4090+
case LibFunc_fdimf:
4091+
case LibFunc_fdiml:
4092+
return optimizeFdim(CI, Builder);
40454093
case LibFunc_fminf:
40464094
case LibFunc_fmin:
40474095
case LibFunc_fminl:
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
define double @fdim_double() {
5+
; CHECK-LABEL: define double @fdim_double() {
6+
; CHECK-NEXT: ret double 2.500000e+00
7+
;
8+
%dim = call double @fdim(double 10.5, double 8.0)
9+
ret double %dim
10+
}
11+
12+
define double @fdim_double1() {
13+
; CHECK-LABEL: define double @fdim_double1() {
14+
; CHECK-NEXT: ret double 0.000000e+00
15+
;
16+
%dim = call double @fdim(double 7.0, double 8.0)
17+
ret double %dim
18+
}
19+
20+
define float @fdim_float() {
21+
; CHECK-LABEL: define float @fdim_float() {
22+
; CHECK-NEXT: ret float 0.000000e+00
23+
;
24+
%dim = call float @fdimf(float 1.500000e+00, float 8.0)
25+
ret float %dim
26+
}
27+
28+
define float @fdim_float1() {
29+
; CHECK-LABEL: define float @fdim_float1() {
30+
; CHECK-NEXT: ret float 2.000000e+00
31+
;
32+
%dim = call float @fdimf(float 1.000000e+01, float 8.0)
33+
ret float %dim
34+
}
35+
36+
define double @fdim_poison1() {
37+
; CHECK-LABEL: define double @fdim_poison1() {
38+
; CHECK-NEXT: ret double poison
39+
;
40+
%dim = call double @fdim(double poison, double 1.0)
41+
ret double %dim
42+
}
43+
44+
define double @fdim_poison2() {
45+
; CHECK-LABEL: define double @fdim_poison2() {
46+
; CHECK-NEXT: ret double poison
47+
;
48+
%dim = call double @fdim(double 1.0, double poison)
49+
ret double %dim
50+
}
51+
52+
define double @fdim_poison3() {
53+
; CHECK-LABEL: define double @fdim_poison3() {
54+
; CHECK-NEXT: ret double poison
55+
;
56+
%dim = call double @fdim(double poison, double poison)
57+
ret double %dim
58+
}
59+
60+
; undef folding is not imiplemented yet
61+
define double @fdim_undef1() {
62+
; CHECK-LABEL: define double @fdim_undef1() {
63+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double undef, double 1.000000e+00)
64+
; CHECK-NEXT: ret double [[DIM]]
65+
;
66+
%dim = call double @fdim(double undef, double 1.0)
67+
ret double %dim
68+
}
69+
70+
define double @fdim_inf_ninf(){
71+
; CHECK-LABEL: define double @fdim_inf_ninf() {
72+
; CHECK-NEXT: ret double 0x7FF0000000000000
73+
;
74+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x8000000000000000 )
75+
ret double %dim
76+
}
77+
78+
define double @fdim_inf(){
79+
; CHECK-LABEL: define double @fdim_inf() {
80+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 0x7FF0000000000000, double 0x7FF0000000000000)
81+
; CHECK-NEXT: ret double [[DIM]]
82+
;
83+
%dim = call double @fdim(double 0x7FF0000000000000, double 0x7FF0000000000000)
84+
ret double %dim
85+
}
86+
87+
define double @fdim_nzero(){
88+
; CHECK-LABEL: define double @fdim_nzero() {
89+
; CHECK-NEXT: ret double 0.000000e+00
90+
;
91+
%dim = call double @fdim(double -0.0, double +0.0)
92+
ret double %dim
93+
}
94+
95+
define double @fdim_strictfp(){
96+
; CHECK-LABEL: define double @fdim_strictfp() {
97+
; CHECK-NEXT: [[DIM:%.*]] = call double @fdim(double 1.000000e+01, double 8.000000e+00) #[[ATTR1:[0-9]+]]
98+
; CHECK-NEXT: ret double [[DIM]]
99+
;
100+
%dim = call double @fdim(double 10.0, double 8.0) strictfp
101+
ret double %dim
102+
}
103+
104+
declare double @fdim(double, double) #0
105+
declare float @fdimf(float, float) #0
106+
107+
attributes #0 = { memory(none)}

0 commit comments

Comments
 (0)