[llvm] [X86][CostModel] Cost scalar integer divide/remainder by a constant (PR #211529)

Rito Takeuchi via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 23 07:17:16 PDT 2026


================
@@ -278,6 +278,21 @@ InstructionCost X86TTIImpl::getArithmeticInstrCost(
   int ISD = TLI->InstructionOpcodeToISD(Opcode);
   assert(ISD && "Invalid opcode");
 
+  // A scalar integer divide/remainder by a constant is not a hardware divide;
+  // it lowers to a magic-number multiply-high plus a few fixup ops. Cost it as
+  // that sequence rather than the generic single-instruction divide, so the
+  // vectorizers do not compare against an artificially cheap scalar lane.
+  // Power-of-two divisors lower to a shift and are left to the generic handling.
+  if (!Ty->isVectorTy() && Op2Info.isConstant() && !Op2Info.isPowerOf2() &&
+      !Op2Info.isNegatedPowerOf2() &&
+      (ISD == ISD::UDIV || ISD == ISD::SDIV || ISD == ISD::UREM ||
+       ISD == ISD::SREM)) {
+    unsigned Cost = ISD == ISD::UREM || ISD == ISD::SREM ? 6 : 5;
+    if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency)
+      Cost += 2;
+    return Cost;
----------------
Licht-T wrote:

Thanks @RKSimon. Fixed and added tests for `i128` in `div.ll`/`rem.ll`.

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


More information about the llvm-commits mailing list