[llvm] 645c53a - [ValueTracking] enhance isKnownNeverInfinity to understand sitofp

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 27 06:02:55 PDT 2020


Author: Sanjay Patel
Date: 2020-09-27T08:40:31-04:00
New Revision: 645c53a9d923c8d7f6a4b49aa02126aec23dc667

URL: https://github.com/llvm/llvm-project/commit/645c53a9d923c8d7f6a4b49aa02126aec23dc667
DIFF: https://github.com/llvm/llvm-project/commit/645c53a9d923c8d7f6a4b49aa02126aec23dc667.diff

LOG: [ValueTracking] enhance isKnownNeverInfinity to understand sitofp

As discussed in D87877, instcombine already has this fold,
but it was missing from the more general ValueTracking logic.

https://alive2.llvm.org/ce/z/PumYZP

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstSimplify/floating-point-compare.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index ac8386900a11..2b8b1efdc30a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3523,11 +3523,20 @@ bool llvm::isKnownNeverInfinity(const Value *V, const TargetLibraryInfo *TLI,
       return isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1) &&
              isKnownNeverInfinity(Inst->getOperand(2), TLI, Depth + 1);
     }
-    case Instruction::UIToFP:
-      // If the input type fits into the floating type the result is finite.
-      return ilogb(APFloat::getLargest(
-                 Inst->getType()->getScalarType()->getFltSemantics())) >=
-             (int)Inst->getOperand(0)->getType()->getScalarSizeInBits();
+    case Instruction::SIToFP:
+    case Instruction::UIToFP: {
+      // Get width of largest magnitude integer (remove a bit if signed).
+      // This still works for a signed minimum value because the largest FP
+      // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
+      int IntSize = Inst->getOperand(0)->getType()->getScalarSizeInBits();
+      if (Inst->getOpcode() == Instruction::SIToFP)
+        --IntSize;
+
+      // If the exponent of the largest finite FP value can hold the largest
+      // integer, the result of the cast must be finite.
+      Type *FPTy = Inst->getType()->getScalarType();
+      return ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize;
+    }
     default:
       break;
     }

diff  --git a/llvm/test/Transforms/InstSimplify/floating-point-compare.ll b/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
index 0a7c15a6bb51..e5184ce5c462 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
@@ -1184,14 +1184,12 @@ define i1 @isNotKnownNeverNegativeInfinity_uitofp(i16 %x) {
   ret i1 %r
 }
 
-; largest signed i16 = 2^15 - 1 = 32767
+; largest magnitude signed i16 = 2^15 - 1 = 32767 --> -32768
 ; largest half (max exponent = 15 -> 2^15 * (1 + 1023/1024) = 65504
 
 define i1 @isKnownNeverInfinity_sitofp(i16 %x) {
 ; CHECK-LABEL: @isKnownNeverInfinity_sitofp(
-; CHECK-NEXT:    [[F:%.*]] = sitofp i16 [[X:%.*]] to half
-; CHECK-NEXT:    [[R:%.*]] = fcmp une half [[F]], 0xH7C00
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 true
 ;
   %f = sitofp i16 %x to half
   %r = fcmp une half %f, 0xH7c00
@@ -1213,9 +1211,7 @@ define i1 @isNotKnownNeverInfinity_sitofp(i17 %x) {
 
 define i1 @isKnownNeverNegativeInfinity_sitofp(i16 %x) {
 ; CHECK-LABEL: @isKnownNeverNegativeInfinity_sitofp(
-; CHECK-NEXT:    [[F:%.*]] = sitofp i16 [[X:%.*]] to half
-; CHECK-NEXT:    [[R:%.*]] = fcmp oeq half [[F]], 0xHFC00
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %f = sitofp i16 %x to half
   %r = fcmp oeq half %f, 0xHfc00


        


More information about the llvm-commits mailing list