[llvm] b2b4c87 - [InstCombine] Make use of low zero bits to determine exact int->fp cast

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 4 18:27:28 PDT 2022


Author: zhongyunde
Date: 2022-07-05T09:15:12+08:00
New Revision: b2b4c8721db00644346dcb783c3b07710e2a6d7e

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

LOG: [InstCombine] Make use of low zero bits to determine exact int->fp cast

According the comment https://reviews.llvm.org/D127854#inline-1226805,
We could also make use of these low zero bits, https://alive2.llvm.org/ce/z/GYxTRu

Reviewed By: spatel, nikic, xbolva00

Differential Revision: https://reviews.llvm.org/D128895

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/test/Transforms/InstCombine/sitofp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index e9e779b8619b6..a9a930555b3c6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1756,11 +1756,12 @@ static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC) {
 
   // TODO:
   // Try harder to find if the source integer type has less significant bits.
-  // For example, compute number of sign bits or compute low bit mask.
+  // For example, compute number of sign bits.
   KnownBits SrcKnown = IC.computeKnownBits(Src, 0, &I);
-  int LowBits =
-      (int)SrcTy->getScalarSizeInBits() - SrcKnown.countMinLeadingZeros();
-  if (LowBits <= DestNumSigBits)
+  int SigBits = (int)SrcTy->getScalarSizeInBits() -
+                SrcKnown.countMinLeadingZeros() -
+                SrcKnown.countMinTrailingZeros();
+  if (SigBits <= DestNumSigBits)
     return true;
 
   return false;

diff  --git a/llvm/test/Transforms/InstCombine/sitofp.ll b/llvm/test/Transforms/InstCombine/sitofp.ll
index cec3931c0eda1..5e0cf94488007 100644
--- a/llvm/test/Transforms/InstCombine/sitofp.ll
+++ b/llvm/test/Transforms/InstCombine/sitofp.ll
@@ -242,27 +242,36 @@ define i25 @max_masked_input(i25 %A) {
   ret i25 %C
 }
 
-define i25 @overflow_masked_input(i25 %A) {
-; CHECK-LABEL: @overflow_masked_input(
-; CHECK-NEXT:    [[M:%.*]] = and i25 [[A:%.*]], -16777216
-; CHECK-NEXT:    [[B:%.*]] = uitofp i25 [[M]] to float
-; CHECK-NEXT:    [[C:%.*]] = fptoui float [[B]] to i25
-; CHECK-NEXT:    ret i25 [[C]]
+define i25 @consider_lowbits_masked_input(i25 %A) {
+; CHECK-LABEL: @consider_lowbits_masked_input(
+; CHECK-NEXT:    [[M:%.*]] = and i25 [[A:%.*]], -16777214
+; CHECK-NEXT:    ret i25 [[M]]
 ;
-  %m = and i25 %A, 16777216  ; Negative test - intermediate 16777216 (= 1 << 24)
+  %m = and i25 %A, 16777218  ; Make use of the low zero bits - intermediate 16777218 (= 1 << 24 + 2)
   %B = uitofp i25 %m to float
   %C = fptoui float %B to i25
   ret i25 %C
 }
 
-; TODO: Clear the low bit - guarantees that the input is converted to FP without rounding.
+define i32 @overflow_masked_input(i32 %A) {
+; CHECK-LABEL: @overflow_masked_input(
+; CHECK-NEXT:    [[M:%.*]] = and i32 [[A:%.*]], 16777217
+; CHECK-NEXT:    [[B:%.*]] = uitofp i32 [[M]] to float
+; CHECK-NEXT:    [[C:%.*]] = fptoui float [[B]] to i32
+; CHECK-NEXT:    ret i32 [[C]]
+;
+  %m = and i32 %A, 16777217  ; Negative test - intermediate 16777217 (= 1 << 24 + 1)
+  %B = uitofp i32 %m to float
+  %C = fptoui float %B to i32
+  ret i32 %C
+}
+
+; Clear the low bit - guarantees that the input is converted to FP without rounding.
 
 define i25 @low_masked_input(i25 %A) {
 ; CHECK-LABEL: @low_masked_input(
 ; CHECK-NEXT:    [[M:%.*]] = and i25 [[A:%.*]], -2
-; CHECK-NEXT:    [[B:%.*]] = uitofp i25 [[M]] to float
-; CHECK-NEXT:    [[C:%.*]] = fptoui float [[B]] to i25
-; CHECK-NEXT:    ret i25 [[C]]
+; CHECK-NEXT:    ret i25 [[M]]
 ;
   %m = and i25 %A, -2
   %B = uitofp i25 %m to float


        


More information about the llvm-commits mailing list