[llvm] [TruncInstCombine] Handle sdiv/srem (PR #222391)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 08:05:25 PDT 2026


================
@@ -372,6 +373,27 @@ Type *TruncInstCombine::getBestTruncatedType() {
         llvm_unreachable("Unhandled intrinsic");
       }
     }
+    if (I->getOpcode() == Instruction::SDiv ||
+        I->getOpcode() == Instruction::SRem) {
+      unsigned NumSignBits0 = ComputeNumSignBits(I->getOperand(0));
+      unsigned NumSignBits1 = ComputeNumSignBits(I->getOperand(1));
+      unsigned MinBitWidthOp0 = OrigBitWidth - NumSignBits0 + 1;
+      unsigned MinBitWidthOp1 = OrigBitWidth - NumSignBits1 + 1;
+      unsigned MinBitWidth = std::max(MinBitWidthOp0, MinBitWidthOp1);
+
+      // In two's complement, sdiv and srem produce UB / poison on INT_MIN / -1.
----------------
dtcxzyw wrote:

srem also yields UB.


https://github.com/llvm/llvm-project/pull/222391


More information about the llvm-commits mailing list