[llvm] r372227 - [SimplifyLibCalls] fix crash with empty function name (PR43347)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 07:33:41 PDT 2019


Author: spatel
Date: Wed Sep 18 07:33:40 2019
New Revision: 372227

URL: http://llvm.org/viewvc/llvm-project?rev=372227&view=rev
Log:
[SimplifyLibCalls] fix crash with empty function name (PR43347)

...and improve some variable names while here.

https://bugs.llvm.org/show_bug.cgi?id=43347

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/sqrt.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=372227&r1=372226&r2=372227&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Wed Sep 18 07:33:40 2019
@@ -1258,21 +1258,18 @@ static Value *optimizeDoubleFP(CallInst
   if (!V[0] || (isBinary && !V[1]))
     return nullptr;
 
-  StringRef CalleeNm = CalleeFn->getName();
-  AttributeList CalleeAt = CalleeFn->getAttributes();
-  bool CalleeIn = CalleeFn->isIntrinsic();
-
   // If call isn't an intrinsic, check that it isn't within a function with the
   // same name as the float version of this call, otherwise the result is an
   // infinite loop.  For example, from MinGW-w64:
   //
   // float expf(float val) { return (float) exp((double) val); }
-  if (!CalleeIn) {
-    const Function *Fn = CI->getFunction();
-    StringRef FnName = Fn->getName();
-    if (FnName.back() == 'f' &&
-        FnName.size() == (CalleeNm.size() + 1) &&
-        FnName.startswith(CalleeNm))
+  StringRef CalleeName = CalleeFn->getName();
+  bool IsIntrinsic = CalleeFn->isIntrinsic();
+  if (!IsIntrinsic) {
+    StringRef CallerName = CI->getFunction()->getName();
+    if (!CallerName.empty() && CallerName.back() == 'f' &&
+        CallerName.size() == (CalleeName.size() + 1) &&
+        CallerName.startswith(CalleeName))
       return nullptr;
   }
 
@@ -1282,16 +1279,16 @@ static Value *optimizeDoubleFP(CallInst
 
   // g((double) float) -> (double) gf(float)
   Value *R;
-  if (CalleeIn) {
+  if (IsIntrinsic) {
     Module *M = CI->getModule();
     Intrinsic::ID IID = CalleeFn->getIntrinsicID();
     Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
     R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
+  } else {
+    AttributeList CalleeAttrs = CalleeFn->getAttributes();
+    R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], CalleeName, B, CalleeAttrs)
+                 : emitUnaryFloatFnCall(V[0], CalleeName, B, CalleeAttrs);
   }
-  else
-    R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], CalleeNm, B, CalleeAt)
-                 : emitUnaryFloatFnCall(V[0], CalleeNm, B, CalleeAt);
-
   return B.CreateFPExt(R, B.getDoubleTy());
 }
 

Modified: llvm/trunk/test/Transforms/InstCombine/sqrt.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sqrt.ll?rev=372227&r1=372226&r2=372227&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/sqrt.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/sqrt.ll Wed Sep 18 07:33:40 2019
@@ -47,5 +47,17 @@ define float @test3(float* %v) nounwind
   ret float %conv38
 }
 
+; PR43347 - https://bugs.llvm.org/show_bug.cgi?id=43347
+
+define void @0(float %f) {
+; CHECK-LABEL: @0(
+; CHECK-NEXT:    [[SQRTF:%.*]] = call float @sqrtf(float [[F:%.*]]) #2
+; CHECK-NEXT:    ret void
+;
+  %d = fpext float %f to double
+  %r = call double @sqrt(double %d)
+  ret void
+}
+
 declare i32 @foo(double)
 declare double @sqrt(double) readnone




More information about the llvm-commits mailing list