From b2b4c8721db00644346dcb783c3b07710e2a6d7e Mon Sep 17 00:00:00 2001 From: zhongyunde Date: Tue, 5 Jul 2022 09:14:29 +0800 Subject: [PATCH] [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 --- .../Transforms/InstCombine/InstCombineCasts.cpp | 9 ++++--- llvm/test/Transforms/InstCombine/sitofp.ll | 31 ++++++++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index e9e779b..a9a9305 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 cec3931..5e0cf94 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 -- 2.7.4