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

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 3 18:09:23 PDT 2022


Allen updated this revision to Diff 441989.
Allen marked 2 inline comments as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128895/new/

https://reviews.llvm.org/D128895

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


Index: llvm/test/Transforms/InstCombine/sitofp.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sitofp.ll
+++ llvm/test/Transforms/InstCombine/sitofp.ll
@@ -242,27 +242,36 @@
   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
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1756,11 +1756,12 @@
 
   // 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128895.441989.patch
Type: text/x-patch
Size: 2887 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220704/562f9261/attachment.bin>


More information about the llvm-commits mailing list