[llvm] [ConstantFold] Special case log1p +/-0.0 (PR #114635)

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 1 20:32:12 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Hubert Tong (hubert-reinterpretcast)

<details>
<summary>Changes</summary>

C's Annex F specifies that log1p +/-0.0 returns the input value;
however, this behavior is optional and host C libraries may behave
differently. This change applies the Annex F behavior to constant
folding by LLVM.

---
Full diff: https://github.com/llvm/llvm-project/pull/114635.diff


2 Files Affected:

- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+3) 
- (modified) llvm/test/Transforms/InstCombine/log1p.ll (-1) 


``````````diff
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index c5a2c2f52f8dc2..88db315ffd0bcb 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2407,6 +2407,9 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
       break;
     case LibFunc_log1p:
     case LibFunc_log1pf:
+      // Implement optional behavior from C's Annex F for +/-0.0.
+      if (U.isZero())
+        return ConstantFP::get(Ty->getContext(), U);
       if (APF > APFloat::getOne(APF.getSemantics(), true) && TLI->has(Func))
         return ConstantFoldFP(log1p, APF, Ty);
       break;
diff --git a/llvm/test/Transforms/InstCombine/log1p.ll b/llvm/test/Transforms/InstCombine/log1p.ll
index bbf89db8c34105..81d3cc8a4f7ac9 100644
--- a/llvm/test/Transforms/InstCombine/log1p.ll
+++ b/llvm/test/Transforms/InstCombine/log1p.ll
@@ -1,4 +1,3 @@
-; XFAIL: target={{.*}}-aix{{.*}}
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/114635


More information about the llvm-commits mailing list