File llvm-constant-folding-fp128-instrinsics.patch of Package llvm19

From 83a5c7cb62e404a713a35445b755cf0109650279 Mon Sep 17 00:00:00 2001
From: David Green <david.green@arm.com>
Date: Sat, 24 Aug 2024 14:39:20 +0100
Subject: [PATCH] [ConstantFolding] Ensure TLI is valid when simplifying fp128
 intrinsics.

TLI might not be valid for all contexts that constant folding is performed. Add
a quick guard that it is not null.
---
 llvm/lib/Analysis/ConstantFolding.cpp         |  2 +-
 llvm/test/Transforms/Inline/simplify-fp128.ll | 24 +++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/Inline/simplify-fp128.ll

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index ff30fece5fce9..15c2699ceefbb 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2124,7 +2124,7 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
       }
 
       LibFunc Fp128Func = NotLibFunc;
-      if (TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
+      if (TLI && TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
           Fp128Func == LibFunc_logl)
         return ConstantFoldFP128(logf128, Op->getValueAPF(), Ty);
     }
diff --git a/llvm/test/Transforms/Inline/simplify-fp128.ll b/llvm/test/Transforms/Inline/simplify-fp128.ll
new file mode 100644
index 0000000000000..73e63702cefcb
--- /dev/null
+++ b/llvm/test/Transforms/Inline/simplify-fp128.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=inline -S | FileCheck %s
+
+define void @fli() {
+; CHECK-LABEL: define void @fli() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = call fp128 @llvm.floor.f128(fp128 0xL999999999999999A4001199999999999)
+; CHECK-NEXT:    ret void
+;
+entry:
+  call void @sc()
+  ret void
+}
+
+define void @sc() {
+; CHECK-LABEL: define void @sc() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = tail call fp128 @llvm.floor.f128(fp128 0xL999999999999999A4001199999999999)
+; CHECK-NEXT:    ret void
+;
+entry:
+  %0 = tail call fp128 @llvm.floor.f128(fp128 0xL999999999999999A4001199999999999)
+  ret void
+}
openSUSE Build Service is sponsored by