diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp --- a/llvm/lib/Support/KnownBits.cpp +++ b/llvm/lib/Support/KnownBits.cpp @@ -474,9 +474,10 @@ } // The sign bit is the LHS's sign bit, except when the result of the - // remainder is zero. If it's known zero, our sign bit is also zero. - if (LHS.isNonNegative()) - Known.makeNonNegative(); + // remainder is zero. The magnitude of the result should be less than or + // equal to the magnitude of the LHS. Therefore any leading zeros that exist + // in the left hand side must also exist in the result. + Known.Zero.setHighBits(LHS.countMinLeadingZeros()); return Known; } diff --git a/llvm/test/CodeGen/RISCV/rv64m-exhaustive-w-insts.ll b/llvm/test/CodeGen/RISCV/rv64m-exhaustive-w-insts.ll --- a/llvm/test/CodeGen/RISCV/rv64m-exhaustive-w-insts.ll +++ b/llvm/test/CodeGen/RISCV/rv64m-exhaustive-w-insts.ll @@ -1110,8 +1110,6 @@ ; RV64IM-LABEL: sext_i32_remw_zext_sext_i16: ; RV64IM: # %bb.0: ; RV64IM-NEXT: rem a0, a0, a1 -; RV64IM-NEXT: slli a0, a0, 32 -; RV64IM-NEXT: srli a0, a0, 32 ; RV64IM-NEXT: ret %3 = sext i16 %1 to i32 %4 = zext i16 %0 to i32