Skip to content

Commit bfa3b62

Browse files
committed
[InstCombine] Erase attribute lists for simplified libcalls
Currently, a transformation like pow(2.0, x) -> exp2(x) copies the pow attribute list verbatim and applies it to exp2. This works out fine when the attribute list is empty, but when it isn't clang may error due due to the mismatch. The source function and destination don't necessarily have anything to do with one another, attribute-wise. So it makes sense to remove the attribute lists (this is similar to what IPO does in this situation). This was discovered after implementing the `noundef` param attribute. Differential Revision: https://reviews.llvm.org/D82820
1 parent 3d52b1e commit bfa3b62

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B) {
14481448
/// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x).
14491449
Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
14501450
Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
1451-
AttributeList Attrs = Pow->getCalledFunction()->getAttributes();
1451+
AttributeList Attrs; // Attributes are only meaningful on the original call
14521452
Module *Mod = Pow->getModule();
14531453
Type *Ty = Pow->getType();
14541454
bool Ignored;
@@ -1615,7 +1615,7 @@ static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
16151615
/// Use square root in place of pow(x, +/-0.5).
16161616
Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
16171617
Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
1618-
AttributeList Attrs = Pow->getCalledFunction()->getAttributes();
1618+
AttributeList Attrs; // Attributes are only meaningful on the original call
16191619
Module *Mod = Pow->getModule();
16201620
Type *Ty = Pow->getType();
16211621

@@ -1785,6 +1785,7 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
17851785

17861786
Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
17871787
Function *Callee = CI->getCalledFunction();
1788+
AttributeList Attrs; // Attributes are only meaningful on the original call
17881789
StringRef Name = Callee->getName();
17891790
Value *Ret = nullptr;
17901791
if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) &&
@@ -1801,7 +1802,7 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
18011802
if (Value *Exp = getIntToFPVal(Op, B))
18021803
return emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), Exp, TLI,
18031804
LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl,
1804-
B, CI->getCalledFunction()->getAttributes());
1805+
B, Attrs);
18051806
}
18061807

18071808
return Ret;
@@ -1836,7 +1837,7 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
18361837

18371838
Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
18381839
Function *LogFn = Log->getCalledFunction();
1839-
AttributeList Attrs = LogFn->getAttributes();
1840+
AttributeList Attrs; // Attributes are only meaningful on the original call
18401841
StringRef LogNm = LogFn->getName();
18411842
Intrinsic::ID LogID = LogFn->getIntrinsicID();
18421843
Module *Mod = Log->getModule();

llvm/test/Transforms/InstCombine/pow_fp_int.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ define double @pow_uitofp_double_const_base_fast(i31 %x) {
5151

5252
define double @pow_sitofp_double_const_base_2_fast(i32 %x) {
5353
; CHECK-LABEL: @pow_sitofp_double_const_base_2_fast(
54-
; CHECK-NEXT: [[LDEXPF:%.*]] = call afn float @ldexpf(float 1.000000e+00, i32 [[X:%.*]]) #1
54+
; CHECK-NEXT: [[LDEXPF:%.*]] = call afn float @ldexpf(float 1.000000e+00, i32 [[X:%.*]])
5555
; CHECK-NEXT: [[RES:%.*]] = fpext float [[LDEXPF]] to double
5656
; CHECK-NEXT: ret double [[RES]]
5757
;
@@ -78,7 +78,7 @@ define double @pow_sitofp_double_const_base_power_of_2_fast(i32 %x) {
7878
define double @pow_uitofp_const_base_2_fast(i31 %x) {
7979
; CHECK-LABEL: @pow_uitofp_const_base_2_fast(
8080
; CHECK-NEXT: [[TMP1:%.*]] = zext i31 [[X:%.*]] to i32
81-
; CHECK-NEXT: [[LDEXPF:%.*]] = call afn float @ldexpf(float 1.000000e+00, i32 [[TMP1]]) #1
81+
; CHECK-NEXT: [[LDEXPF:%.*]] = call afn float @ldexpf(float 1.000000e+00, i32 [[TMP1]])
8282
; CHECK-NEXT: [[RES:%.*]] = fpext float [[LDEXPF]] to double
8383
; CHECK-NEXT: ret double [[RES]]
8484
;
@@ -343,7 +343,7 @@ define double @pow_uitofp_const_base_no_fast(i32 %x) {
343343

344344
define double @pow_sitofp_const_base_2_no_fast(i32 %x) {
345345
; CHECK-LABEL: @pow_sitofp_const_base_2_no_fast(
346-
; CHECK-NEXT: [[LDEXPF:%.*]] = call float @ldexpf(float 1.000000e+00, i32 [[X:%.*]]) #1
346+
; CHECK-NEXT: [[LDEXPF:%.*]] = call float @ldexpf(float 1.000000e+00, i32 [[X:%.*]])
347347
; CHECK-NEXT: [[RES:%.*]] = fpext float [[LDEXPF]] to double
348348
; CHECK-NEXT: ret double [[RES]]
349349
;

llvm/test/Transforms/InstCombine/simplify-libcalls.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,28 @@ define i32 @fake_toascii(i8 %x) {
175175
ret i32 %y
176176
}
177177

178+
declare double @pow(double, double)
179+
declare double @exp2(double)
180+
181+
; check to make sure only the correct libcall attributes are used
182+
define double @fake_exp2(double %x) {
183+
; CHECK-LABEL: @fake_exp2(
184+
; CHECK-NEXT: [[Y:%.*]] = call double @exp2(double %x)
185+
; CHECK-NEXT: ret double [[Y]]
186+
187+
%y = call inreg double @pow(double inreg 2.0, double inreg %x)
188+
ret double %y
189+
}
190+
define double @fake_ldexp(i32 %x) {
191+
; CHECK-LABEL: @fake_ldexp(
192+
; CHECK-NEXT: [[Z:%.*]] = call double @ldexp(double 1.0{{.*}}, i32 %x)
193+
; CHECK-NEXT: ret double [[Z]]
194+
195+
%y = sitofp i32 %x to double
196+
%z = call inreg double @exp2(double %y)
197+
ret double %z
198+
}
199+
178200

179201
attributes #0 = { nobuiltin }
180202
attributes #1 = { builtin }

0 commit comments

Comments
 (0)