[llvm] 13302c0 - [ConstantFolding] avoid crashing on a fake math library call

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 20 15:25:29 PDT 2021


Author: Sanjay Patel
Date: 2021-07-20T18:25:21-04:00
New Revision: 13302c06cdae6b03e56e7e68cd7dea604741352b

URL: https://github.com/llvm/llvm-project/commit/13302c06cdae6b03e56e7e68cd7dea604741352b
DIFF: https://github.com/llvm/llvm-project/commit/13302c06cdae6b03e56e7e68cd7dea604741352b.diff

LOG: [ConstantFolding] avoid crashing on a fake math library call

https://llvm.org/PR50960

Added: 
    llvm/test/Transforms/InstSimplify/ConstProp/libfunc.ll

Modified: 
    llvm/lib/Analysis/ConstantFolding.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 1c6709c9fa1d..af25dabb0e17 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3041,10 +3041,18 @@ Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F,
     return nullptr;
   if (!F->hasName())
     return nullptr;
-  StringRef Name = F->getName();
 
-  Type *Ty = F->getReturnType();
+  // If this is not an intrinsic and not recognized as a library call, bail out.
+  if (F->getIntrinsicID() == Intrinsic::not_intrinsic) {
+    if (!TLI)
+      return nullptr;
+    LibFunc LibF;
+    if (!TLI->getLibFunc(*F, LibF))
+      return nullptr;
+  }
 
+  StringRef Name = F->getName();
+  Type *Ty = F->getReturnType();
   if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
     return ConstantFoldFixedVectorCall(
         Name, F->getIntrinsicID(), FVTy, Operands,
@@ -3055,6 +3063,9 @@ Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F,
         Name, F->getIntrinsicID(), SVTy, Operands,
         F->getParent()->getDataLayout(), TLI, Call);
 
+  // TODO: If this is a library function, we already discovered that above,
+  //       so we should pass the LibFunc, not the name (and it might be better
+  //       still to separate intrinsic handling from libcalls).
   return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI,
                                 Call);
 }

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/libfunc.ll b/llvm/test/Transforms/InstSimplify/ConstProp/libfunc.ll
new file mode 100644
index 000000000000..59ab959a4688
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/libfunc.ll
@@ -0,0 +1,15 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+; This is not the mathlib call you were looking for.
+
+declare double @sin(x86_fp80)
+
+define double @PR50960(x86_fp80 %0) {
+; CHECK-LABEL: @PR50960(
+; CHECK-NEXT:    [[CALL:%.*]] = call double @sin(x86_fp80 0xK3FFF8000000000000000)
+; CHECK-NEXT:    ret double [[CALL]]
+;
+  %call = call double @sin(x86_fp80 0xK3FFF8000000000000000)
+  ret double %call
+}


        


More information about the llvm-commits mailing list