[llvm] [VPlan] Use llvm.masked.{u, s}{div, rem} for predicated division (PR #191377)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 17 03:34:45 PDT 2026
================
@@ -7442,28 +7442,36 @@ bool VPRecipeBuilder::shouldWiden(Instruction *I, VFRange &Range) const {
Range);
}
-VPWidenRecipe *VPRecipeBuilder::tryToWiden(VPInstruction *VPI) {
+static Intrinsic::ID getMaskedDivRemIntrinsic(unsigned Opcode) {
+ switch (Opcode) {
+ case Instruction::UDiv:
+ return Intrinsic::masked_udiv;
+ case Instruction::SDiv:
+ return Intrinsic::masked_sdiv;
+ case Instruction::URem:
+ return Intrinsic::masked_urem;
+ case Instruction::SRem:
+ return Intrinsic::masked_srem;
+ default:
+ llvm_unreachable("Unexpected opcode");
+ }
+}
+
+VPRecipeWithIRFlags *VPRecipeBuilder::tryToWiden(VPInstruction *VPI) {
auto *I = VPI->getUnderlyingInstr();
switch (VPI->getOpcode()) {
default:
return nullptr;
case Instruction::SDiv:
case Instruction::UDiv:
case Instruction::SRem:
- case Instruction::URem: {
- // If not provably safe, use a select to form a safe divisor before widening the
- // div/rem operation itself. Otherwise fall through to general handling below.
- if (CM.isPredicatedInst(I)) {
- SmallVector<VPValue *> Ops(VPI->operandsWithoutMask());
- VPValue *Mask = VPI->getMask();
- VPValue *One = Plan.getConstantInt(I->getType(), 1u);
- auto *SafeRHS =
- Builder.createSelect(Mask, Ops[1], One, VPI->getDebugLoc());
- Ops[1] = SafeRHS;
- return new VPWidenRecipe(*I, Ops, *VPI, *VPI, VPI->getDebugLoc());
- }
+ case Instruction::URem:
+ // If not provably safe, use a masked intrinsic.
+ if (CM.isPredicatedInst(I))
+ return new VPWidenIntrinsicRecipe(
+ getMaskedDivRemIntrinsic(VPI->getOpcode()), VPI->operands(),
+ I->getType(), {}, {}, VPI->getDebugLoc());
----------------
lukel97 wrote:
The masked intrinsics are always legal, we have a generic lowering and costing for them which is just the select + udiv expansion
https://github.com/llvm/llvm-project/pull/191377
More information about the llvm-commits
mailing list