[llvm] [ConstantFolding] Add edge cases for llvm.log{, 2, 10} (PR #173304)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 23 13:07:44 PST 2025


================
@@ -2719,11 +2719,29 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
     switch (IntrinsicID) {
       default: break;
       case Intrinsic::log:
+        if (U.isZero())
+          return ConstantFP::getInfinity(Ty, true);
+        if (U.isNegative())
+          return ConstantFP::getNaN(Ty);
+        if (U.isExactlyValue(1))
+          return ConstantFP::getZero(Ty);
         return ConstantFoldFP(log, APF, Ty);
       case Intrinsic::log2:
+        if (U.isZero())
+          return ConstantFP::getInfinity(Ty, true);
+        if (U.isNegative())
+          return ConstantFP::getNaN(Ty);
+        if (U.isExactlyValue(1))
+          return ConstantFP::getZero(Ty);
         // TODO: What about hosts that lack a C99 library?
         return ConstantFoldFP(log2, APF, Ty);
       case Intrinsic::log10:
+        if (U.isZero())
+          return ConstantFP::getInfinity(Ty, true);
+        if (U.isNegative())
+          return ConstantFP::getNaN(Ty);
+        if (U.isExactlyValue(1))
----------------
arsenm wrote:

I've been meaning to add a more efficient isOne, but this is the canonical way to check for now 

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


More information about the llvm-commits mailing list