Skip to content

Commit d46bf63

Browse files
committed
[SimplifyLibCalls] fix crash with empty function name (PR43347)
...and improve some variable names while here. https://bugs.llvm.org/show_bug.cgi?id=43347 llvm-svn: 372227
1 parent 40fdacb commit d46bf63

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,21 +1258,18 @@ static Value *optimizeDoubleFP(CallInst *CI, IRBuilder<> &B,
12581258
if (!V[0] || (isBinary && !V[1]))
12591259
return nullptr;
12601260

1261-
StringRef CalleeNm = CalleeFn->getName();
1262-
AttributeList CalleeAt = CalleeFn->getAttributes();
1263-
bool CalleeIn = CalleeFn->isIntrinsic();
1264-
12651261
// If call isn't an intrinsic, check that it isn't within a function with the
12661262
// same name as the float version of this call, otherwise the result is an
12671263
// infinite loop. For example, from MinGW-w64:
12681264
//
12691265
// float expf(float val) { return (float) exp((double) val); }
1270-
if (!CalleeIn) {
1271-
const Function *Fn = CI->getFunction();
1272-
StringRef FnName = Fn->getName();
1273-
if (FnName.back() == 'f' &&
1274-
FnName.size() == (CalleeNm.size() + 1) &&
1275-
FnName.startswith(CalleeNm))
1266+
StringRef CalleeName = CalleeFn->getName();
1267+
bool IsIntrinsic = CalleeFn->isIntrinsic();
1268+
if (!IsIntrinsic) {
1269+
StringRef CallerName = CI->getFunction()->getName();
1270+
if (!CallerName.empty() && CallerName.back() == 'f' &&
1271+
CallerName.size() == (CalleeName.size() + 1) &&
1272+
CallerName.startswith(CalleeName))
12761273
return nullptr;
12771274
}
12781275

@@ -1282,16 +1279,16 @@ static Value *optimizeDoubleFP(CallInst *CI, IRBuilder<> &B,
12821279

12831280
// g((double) float) -> (double) gf(float)
12841281
Value *R;
1285-
if (CalleeIn) {
1282+
if (IsIntrinsic) {
12861283
Module *M = CI->getModule();
12871284
Intrinsic::ID IID = CalleeFn->getIntrinsicID();
12881285
Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
12891286
R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
1287+
} else {
1288+
AttributeList CalleeAttrs = CalleeFn->getAttributes();
1289+
R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], CalleeName, B, CalleeAttrs)
1290+
: emitUnaryFloatFnCall(V[0], CalleeName, B, CalleeAttrs);
12901291
}
1291-
else
1292-
R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], CalleeNm, B, CalleeAt)
1293-
: emitUnaryFloatFnCall(V[0], CalleeNm, B, CalleeAt);
1294-
12951292
return B.CreateFPExt(R, B.getDoubleTy());
12961293
}
12971294

llvm/test/Transforms/InstCombine/sqrt.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,17 @@ define float @test3(float* %v) nounwind uwtable ssp {
4747
ret float %conv38
4848
}
4949

50+
; PR43347 - https://bugs.llvm.org/show_bug.cgi?id=43347
51+
52+
define void @0(float %f) {
53+
; CHECK-LABEL: @0(
54+
; CHECK-NEXT: [[SQRTF:%.*]] = call float @sqrtf(float [[F:%.*]]) #2
55+
; CHECK-NEXT: ret void
56+
;
57+
%d = fpext float %f to double
58+
%r = call double @sqrt(double %d)
59+
ret void
60+
}
61+
5062
declare i32 @foo(double)
5163
declare double @sqrt(double) readnone

0 commit comments

Comments
 (0)