[clang] [llvm] [clang][OpenCL] Defined integer / and % through scalar llvm.masked.{s,u}{div,rem} (PR #233902)
Igor Gorban via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 05:57:18 PST 2026
https://github.com/igorban-intel updated https://github.com/llvm/llvm-project/pull/233902
>From ee3baa729229dfb17d86be11eeb4dad735b87fe2 Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 11:38:24 +0100
Subject: [PATCH 1/9] [IR] Allow scalar operands in masked integer div/rem
intrinsics
Extend llvm.masked.{u,s}{div,rem} to scalar integer types with an i1
mask. The semantics match a one-lane vector: an enabled operation
behaves like the ordinary instruction, including its undefined behavior;
a disabled operation returns poison without undefined behavior; a poison
mask returns poison and has undefined behavior only where the ordinary
operation would. Together with a frontend freeze of the poison result,
this lets languages whose exceptional integer divisions (x / 0,
INT_MIN / -1) yield an unspecified value, such as OpenCL C, express them
in IR so follow-up target lowering can elide the divisor guard on
hardware whose division cannot trap.
Add Intrinsic::getMaskedIntDivRemOpcode() to map a masked div/rem
intrinsic to its binary opcode, and use it in the existing callers.
Cost scalar masked div/rem like the guarded expansion, and provide the
conservative default scalar lowering (op(a, select(freeze(mask), b, 1)),
or false for signed i1) in PreISelIntrinsicLowering.
Assisted-by: Claude
---
llvm/docs/LangRef.md | 30 +++--
llvm/docs/ReleaseNotes.md | 4 +
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 9 +-
llvm/include/llvm/IR/Intrinsics.h | 4 +
llvm/include/llvm/IR/Intrinsics.td | 8 +-
llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp | 39 +++++++
llvm/lib/IR/Intrinsics.cpp | 16 +++
.../Target/RISCV/RISCVTargetTransformInfo.cpp | 12 +-
.../Transforms/Vectorize/VPlanTransforms.cpp | 23 +---
.../Analysis/CostModel/RISCV/masked-divrem.ll | 15 +++
.../Analysis/CostModel/X86/masked-divrem.ll | 36 ++++--
llvm/test/Assembler/masked-divrem-scalar.ll | 36 ++++++
.../CodeGen/SPIRV/masked-divrem-scalar.ll | 53 +++++++++
llvm/test/CodeGen/X86/masked-divrem-scalar.ll | 83 ++++++++++++++
.../MemorySanitizer/masked-divrem.ll | 23 ++++
.../masked-divrem-scalar.ll | 103 ++++++++++++++++++
.../Verifier/invalid_intrinsic_signatures.ll | 24 +++-
llvm/unittests/IR/IntrinsicsTest.cpp | 13 +++
18 files changed, 469 insertions(+), 62 deletions(-)
create mode 100644 llvm/test/Assembler/masked-divrem-scalar.ll
create mode 100644 llvm/test/CodeGen/SPIRV/masked-divrem-scalar.ll
create mode 100644 llvm/test/CodeGen/X86/masked-divrem-scalar.ll
create mode 100644 llvm/test/Transforms/PreISelIntrinsicLowering/masked-divrem-scalar.ll
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index 477f03c36b59cc..f3e8b2c8f84540 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -24485,7 +24485,7 @@ call <void> @llvm.masked.compressstore.v8f64(<8 x double> %Tmp, ptr %Bptr, <8 x
Other targets may support this intrinsic differently, for example, by lowering it into a sequence of branches that guard scalar store operations.
-### Masked Vector Arithmetic Intrinsics
+### Masked Integer Arithmetic Intrinsics
#### '`llvm.masked.udiv.*`' Intrinsics
@@ -24493,21 +24493,24 @@ Other targets may support this intrinsic differently, for example, by lowering i
This is an overloaded intrinsic.
```
+declare i32 @llvm.masked.udiv.i32(i32 <op1>, i32 <op2>, i1 <mask>)
declare <8 x i32> @llvm.masked.udiv.v8i32(<8 x i32> <op1>, <8 x i32> <op2>, <8 x i1> <mask>)
declare <vscale x 2 x i64> @llvm.masked.udiv.nxv2i64(<vscale x 2 x i64> <op1>, <vscale x 2 x i64> <op2>, <vscale x 2 x i1> <mask>)
```
##### Overview:
-Performs unsigned division ({ref}`udiv <i_udiv>`) of two vectors of integers, but only on enabled lanes.
+Performs unsigned division ({ref}`udiv <i_udiv>`) of two integers or vectors of integers, but only when enabled by the mask.
##### Arguments:
-The first two arguments and the result have the same vector of integer type. The third argument is the vector mask and has the same number of elements as the result vector type.
+The first two arguments and the result have the same integer or vector of integer type.
+The third argument is an `i1` mask for scalar operands, or a vector of `i1` with the same number of elements as the result vector type.
##### Semantics:
Follows the same semantics as {ref}`udiv <i_udiv>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
+For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`udiv <i_udiv>` would.
#### '`llvm.masked.sdiv.*`' Intrinsics
@@ -24515,21 +24518,24 @@ Follows the same semantics as {ref}`udiv <i_udiv>` with the exception that disab
This is an overloaded intrinsic.
```
+declare i32 @llvm.masked.sdiv.i32(i32 <op1>, i32 <op2>, i1 <mask>)
declare <8 x i32> @llvm.masked.sdiv.v8i32(<8 x i32> <op1>, <8 x i32> <op2>, <8 x i1> <mask>)
declare <vscale x 2 x i64> @llvm.masked.sdiv.nxv2i64(<vscale x 2 x i64> <op1>, <vscale x 2 x i64> <op2>, <vscale x 2 x i1> <mask>)
```
##### Overview:
-Performs signed division ({ref}`sdiv <i_sdiv>`) of two vectors of integers, but only on enabled lanes.
+Performs signed division ({ref}`sdiv <i_sdiv>`) of two integers or vectors of integers, but only when enabled by the mask.
##### Arguments:
-The first two arguments and the result have the same vector of integer type. The third argument is the vector mask and has the same number of elements as the result vector type.
+The first two arguments and the result have the same integer or vector of integer type.
+The third argument is an `i1` mask for scalar operands, or a vector of `i1` with the same number of elements as the result vector type.
##### Semantics:
Follows the same semantics as {ref}`sdiv <i_sdiv>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
+For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`sdiv <i_sdiv>` would.
#### '`llvm.masked.urem.*`' Intrinsics
@@ -24537,21 +24543,24 @@ Follows the same semantics as {ref}`sdiv <i_sdiv>` with the exception that disab
This is an overloaded intrinsic.
```
+declare i32 @llvm.masked.urem.i32(i32 <op1>, i32 <op2>, i1 <mask>)
declare <8 x i32> @llvm.masked.urem.v8i32(<8 x i32> <op1>, <8 x i32> <op2>, <8 x i1> <mask>)
declare <vscale x 2 x i64> @llvm.masked.urem.nxv2i64(<vscale x 2 x i64> <op1>, <vscale x 2 x i64> <op2>, <vscale x 2 x i1> <mask>)
```
##### Overview:
-Computes the remainder from the unsigned division ({ref}`urem <i_urem>`) of two vectors of integers, but only on enabled lanes.
+Computes the remainder from the unsigned division ({ref}`urem <i_urem>`) of two integers or vectors of integers, but only when enabled by the mask.
##### Arguments:
-The first two arguments and the result have the same vector of integer type. The third argument is the vector mask and has the same number of elements as the result vector type.
+The first two arguments and the result have the same integer or vector of integer type.
+The third argument is an `i1` mask for scalar operands, or a vector of `i1` with the same number of elements as the result vector type.
##### Semantics:
Follows the same semantics as {ref}`urem <i_urem>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
+For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`urem <i_urem>` would.
#### '`llvm.masked.srem.*`' Intrinsics
@@ -24559,21 +24568,24 @@ Follows the same semantics as {ref}`urem <i_urem>` with the exception that disab
This is an overloaded intrinsic.
```
+declare i32 @llvm.masked.srem.i32(i32 <op1>, i32 <op2>, i1 <mask>)
declare <8 x i32> @llvm.masked.srem.v8i32(<8 x i32> <op1>, <8 x i32> <op2>, <8 x i1> <mask>)
declare <vscale x 2 x i64> @llvm.masked.srem.nxv2i64(<vscale x 2 x i64> <op1>, <vscale x 2 x i64> <op2>, <vscale x 2 x i1> <mask>)
```
##### Overview:
-Computes the remainder from the signed division ({ref}`srem <i_srem>`) of two vectors of integers, but only on enabled lanes.
+Computes the remainder from the signed division ({ref}`srem <i_srem>`) of two integers or vectors of integers, but only when enabled by the mask.
##### Arguments:
-The first two arguments and the result have the same vector of integer type. The third argument is the vector mask and has the same number of elements as the result vector type.
+The first two arguments and the result have the same integer or vector of integer type.
+The third argument is an `i1` mask for scalar operands, or a vector of `i1` with the same number of elements as the result vector type.
##### Semantics:
Follows the same semantics as {ref}`srem <i_srem>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
+For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`srem <i_srem>` would.
### Speculative Load Intrinsics
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 6d1d23266c372a..17b9ff836b16f2 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -59,6 +59,10 @@ Makes programs 10x faster by doing Special New Thing.
`CallBase::isNoInline()` to query whether a function or call should not be
inlined.
+* The `llvm.masked.{u,s}{div,rem}` intrinsics now also accept scalar integer
+ operands with an `i1` mask. Disabled operations return `poison` without
+ undefined behavior.
+
* LLVM now assigns persistent print IDs to metadata nodes. Reusing these IDs
avoids repeated module-wide scans to rebuild metadata numbering, which can
significantly speed up debug and pass printing on large modules. Keeping
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 3cae4102bc97f1..f8d9701fc26f79 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -2849,35 +2849,30 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
case Intrinsic::masked_sdiv:
case Intrinsic::masked_urem:
case Intrinsic::masked_srem: {
- unsigned UnmaskedOpc;
switch (IID) {
case Intrinsic::masked_udiv:
ISD = ISD::MASKED_UDIV;
- UnmaskedOpc = Instruction::UDiv;
break;
case Intrinsic::masked_sdiv:
ISD = ISD::MASKED_SDIV;
- UnmaskedOpc = Instruction::SDiv;
break;
case Intrinsic::masked_urem:
ISD = ISD::MASKED_UREM;
- UnmaskedOpc = Instruction::URem;
break;
case Intrinsic::masked_srem:
ISD = ISD::MASKED_SREM;
- UnmaskedOpc = Instruction::SRem;
break;
default:
llvm_unreachable("Unexpected intrinsic ID");
}
+ unsigned UnmaskedOpc = Intrinsic::getMaskedIntDivRemOpcode(IID);
InstructionCost Cost =
thisT()->getArithmeticInstrCost(UnmaskedOpc, RetTy, CostKind);
// Expansion generates a (select %mask, %rhs, 1) for the divisor.
MVT LT = getTypeLegalizationCost(RetTy).second;
if (!getTLI()->isOperationLegalOrCustom(ISD, LT)) {
- Type *CondTy = cast<VectorType>(RetTy)->getWithNewType(
- IntegerType::getInt1Ty(RetTy->getContext()));
+ Type *CondTy = RetTy->getWithNewBitWidth(1);
Cost += thisT()->getCmpSelInstrCost(
BinaryOperator::Select, RetTy, CondTy, CmpInst::BAD_ICMP_PREDICATE,
CostKind, {}, {TTI::OK_UniformConstantValue, TTI::OP_PowerOf2});
diff --git a/llvm/include/llvm/IR/Intrinsics.h b/llvm/include/llvm/IR/Intrinsics.h
index 801111c2cfacbd..739c8971a8d09d 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -337,6 +337,10 @@ LLVM_ABI Intrinsic::ID getInterleaveIntrinsicID(unsigned Factor);
/// N.
LLVM_ABI Intrinsic::ID getDeinterleaveIntrinsicID(unsigned Factor);
+/// Return the binary instruction opcode for masked integer div/rem, or zero
+/// if \p IID is not a masked integer division or remainder.
+LLVM_ABI unsigned getMaskedIntDivRemOpcode(ID IID);
+
/// Print the argument info for the arguments with ArgInfo.
LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS,
const Constant *ImmArgVal);
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index 43caed2bf46c37..5f1fd2f1321719 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -2501,28 +2501,28 @@ def int_experimental_vector_compress:
[IntrNoMem]>;
def int_masked_udiv:
- DefaultAttrsIntrinsic<[llvm_any_vector_int_ty],
+ DefaultAttrsIntrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>,
LLVMMatchType<0>,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
[IntrNoMem]>;
def int_masked_sdiv:
- DefaultAttrsIntrinsic<[llvm_any_vector_int_ty],
+ DefaultAttrsIntrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>,
LLVMMatchType<0>,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
[IntrNoMem]>;
def int_masked_urem:
- DefaultAttrsIntrinsic<[llvm_any_vector_int_ty],
+ DefaultAttrsIntrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>,
LLVMMatchType<0>,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
[IntrNoMem]>;
def int_masked_srem:
- DefaultAttrsIntrinsic<[llvm_any_vector_int_ty],
+ DefaultAttrsIntrinsic<[llvm_anyint_ty],
[LLVMMatchType<0>,
LLVMMatchType<0>,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
diff --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
index 26682c67b7ac84..472015d66b351d 100644
--- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
+++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
@@ -110,6 +110,39 @@ template <class T> static bool forEachCall(Function &Intrin, T Callback) {
return Changed;
}
+// Lower scalar llvm.masked.{u,s}{div,rem} to a guarded division before ISel.
+static bool lowerScalarMaskedDivRem(Function &F) {
+ Type *RetTy = F.getReturnType();
+ if (!RetTy->isIntegerTy())
+ return false;
+
+ Intrinsic::ID ID = F.getIntrinsicID();
+ bool IsSigned = ID == Intrinsic::masked_sdiv || ID == Intrinsic::masked_srem;
+ Instruction::BinaryOps Opcode = static_cast<Instruction::BinaryOps>(
+ Intrinsic::getMaskedIntDivRemOpcode(ID));
+ return forEachCall(F, [&](CallInst *CI) {
+ IRBuilder<> Builder(CI);
+ Value *Result;
+ if (IsSigned && RetTy->isIntegerTy(1)) {
+ // In 1-bit two's complement (0 and -1), divisor 0 is UB and -1 / -1
+ // (and -1 % -1) overflows INT_MIN / -1; 0 / -1 == 0 is the sole defined
+ // case, so false refines every outcome without emitting a division.
+ Result = Builder.getFalse();
+ } else {
+ // Freezing the mask prevents a poison mask from turning into a poison
+ // divisor (immediate UB) when the division itself would be safe.
+ Value *Mask = Builder.CreateFreeze(CI->getArgOperand(2));
+ Value *Divisor = Builder.CreateSelect(Mask, CI->getArgOperand(1),
+ ConstantInt::get(RetTy, 1));
+ Result = Builder.CreateBinOp(Opcode, CI->getArgOperand(0), Divisor);
+ }
+ Result->takeName(CI);
+ CI->replaceAllUsesWith(Result);
+ CI->eraseFromParent();
+ return true;
+ });
+}
+
static bool lowerLoadRelative(Function &F) {
if (F.use_empty())
return false;
@@ -731,6 +764,12 @@ bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
case Intrinsic::experimental_memset_pattern:
Changed |= expandMemIntrinsicUses(F, CMap);
break;
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem:
+ Changed |= lowerScalarMaskedDivRem(F);
+ break;
case Intrinsic::load_relative:
Changed |= lowerLoadRelative(F);
break;
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
index cb5e0c41c2590c..d19b7bd0ae32d1 100644
--- a/llvm/lib/IR/Intrinsics.cpp
+++ b/llvm/lib/IR/Intrinsics.cpp
@@ -15,6 +15,7 @@
#include "llvm/ADT/StringTable.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicsAArch64.h"
#include "llvm/IR/IntrinsicsAMDGPU.h"
#include "llvm/IR/IntrinsicsARM.h"
@@ -1515,6 +1516,21 @@ Intrinsic::ID Intrinsic::getDeinterleaveIntrinsicID(unsigned Factor) {
return InterleaveIntrinsics[Factor - 2].Deinterleave;
}
+unsigned Intrinsic::getMaskedIntDivRemOpcode(ID IID) {
+ switch (IID) {
+ case masked_udiv:
+ return Instruction::UDiv;
+ case masked_sdiv:
+ return Instruction::SDiv;
+ case masked_urem:
+ return Instruction::URem;
+ case masked_srem:
+ return Instruction::SRem;
+ default:
+ return 0;
+ }
+}
+
LLVM_ABI void Intrinsic::printFPClassMask(raw_ostream &OS,
const Constant *ImmArgVal) {
uint64_t Val = cast<ConstantInt>(ImmArgVal)->getZExtValue();
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 9d574817d13859..29a1a557ea4e2f 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -1941,17 +1941,13 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
break;
}
case Intrinsic::masked_udiv:
- return getArithmeticInstrCost(Instruction::UDiv, ICA.getReturnType(),
- CostKind);
case Intrinsic::masked_sdiv:
- return getArithmeticInstrCost(Instruction::SDiv, ICA.getReturnType(),
- CostKind);
case Intrinsic::masked_urem:
- return getArithmeticInstrCost(Instruction::URem, ICA.getReturnType(),
- CostKind);
case Intrinsic::masked_srem:
- return getArithmeticInstrCost(Instruction::SRem, ICA.getReturnType(),
- CostKind);
+ if (RetTy->isIntegerTy())
+ break;
+ return getArithmeticInstrCost(
+ Intrinsic::getMaskedIntDivRemOpcode(ICA.getID()), RetTy, CostKind);
case Intrinsic::get_active_lane_mask: {
if (ST->hasVInstructions()) {
Type *ExpRetTy = VectorType::get(
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index b6f59eb5d4d664..877fe8ccbef995 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1827,22 +1827,6 @@ static void reassociateHeaderMask(VPlan &Plan) {
}
}
-static std::optional<Instruction::BinaryOps>
-getUnmaskedDivRemOpcode(Intrinsic::ID ID) {
- switch (ID) {
- case Intrinsic::masked_udiv:
- return Instruction::UDiv;
- case Intrinsic::masked_sdiv:
- return Instruction::SDiv;
- case Intrinsic::masked_urem:
- return Instruction::URem;
- case Intrinsic::masked_srem:
- return Instruction::SRem;
- default:
- return {};
- }
-}
-
static void narrowToSingleScalarRecipes(VPlan &Plan) {
if (Plan.hasScalarVFOnly())
return;
@@ -1881,7 +1865,8 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
if (auto *IntrR = dyn_cast<VPWidenIntrinsicRecipe>(RepOrWidenR)) {
if (!vputils::onlyFirstLaneUsed(IntrR))
continue;
- auto Opc = getUnmaskedDivRemOpcode(IntrR->getVectorIntrinsicID());
+ unsigned Opc =
+ Intrinsic::getMaskedIntDivRemOpcode(IntrR->getVectorIntrinsicID());
if (!Opc)
continue;
VPBuilder Builder(IntrR);
@@ -1889,8 +1874,8 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
IntrR->getOperand(2), IntrR->getOperand(1),
Plan.getConstantInt(IntrR->getScalarType(), 1));
VPValue *Clone = Builder.createNaryOp(
- *Opc, {IntrR->getOperand(0), SafeDivisor},
- VPIRFlags::getDefaultFlags(*Opc), IntrR->getDebugLoc());
+ Opc, {IntrR->getOperand(0), SafeDivisor},
+ VPIRFlags::getDefaultFlags(Opc), IntrR->getDebugLoc());
IntrR->replaceAllUsesWith(Clone);
IntrR->eraseFromParent();
continue;
diff --git a/llvm/test/Analysis/CostModel/RISCV/masked-divrem.ll b/llvm/test/Analysis/CostModel/RISCV/masked-divrem.ll
index 2dcaf3eac01711..c156209dd1fc96 100644
--- a/llvm/test/Analysis/CostModel/RISCV/masked-divrem.ll
+++ b/llvm/test/Analysis/CostModel/RISCV/masked-divrem.ll
@@ -293,3 +293,18 @@ define void @srem() {
ret void
}
+
+define void @scalar(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: 'scalar'
+; CHECK-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %udiv = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %sdiv = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %urem = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %srem = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+ %udiv = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ %sdiv = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ %urem = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ %srem = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret void
+}
diff --git a/llvm/test/Analysis/CostModel/X86/masked-divrem.ll b/llvm/test/Analysis/CostModel/X86/masked-divrem.ll
index 60b95fd2e6f367..31742453560432 100644
--- a/llvm/test/Analysis/CostModel/X86/masked-divrem.ll
+++ b/llvm/test/Analysis/CostModel/X86/masked-divrem.ll
@@ -1,15 +1,15 @@
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+sse2 2>&1 | FileCheck %s --check-prefix=SSE2
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+ssse3 2>&1 | FileCheck %s --check-prefix=SSE2
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+sse4.2 2>&1 | FileCheck %s --check-prefix=SSE42
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx 2>&1 | FileCheck %s --check-prefix=AVX1
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx2 2>&1 | FileCheck %s --check-prefix=AVX2
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx512f 2>&1 | FileCheck %s --check-prefix=AVX512F
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx512f,+avx512bw 2>&1 | FileCheck %s --check-prefix=AVX512BW
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx512f,+avx512dq 2>&1 | FileCheck %s --check-prefix=AVX512DQ
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mcpu=slm 2>&1 | FileCheck %s --check-prefix=SLM
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mcpu=goldmont 2>&1 | FileCheck %s --check-prefix=SSE42
-; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mcpu=btver2 2>&1 | FileCheck %s --check-prefix=AVX1
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+sse2 2>&1 | FileCheck %s --check-prefixes=CHECK,SSE2
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+ssse3 2>&1 | FileCheck %s --check-prefixes=CHECK,SSE2
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+sse4.2 2>&1 | FileCheck %s --check-prefixes=CHECK,SSE42
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx 2>&1 | FileCheck %s --check-prefixes=CHECK,AVX1
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx2 2>&1 | FileCheck %s --check-prefixes=CHECK,AVX2
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx512f 2>&1 | FileCheck %s --check-prefixes=CHECK,AVX512F
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx512f,+avx512bw 2>&1 | FileCheck %s --check-prefixes=CHECK,AVX512BW
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mattr=+avx512f,+avx512dq 2>&1 | FileCheck %s --check-prefixes=CHECK,AVX512DQ
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mcpu=slm 2>&1 | FileCheck %s --check-prefixes=CHECK,SLM
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mcpu=goldmont 2>&1 | FileCheck %s --check-prefixes=CHECK,SSE42
+; RUN: opt < %s -mtriple=x86_64-unknown-linux-gnu -passes="print<cost-model>" -disable-output -cost-kind=all -mcpu=btver2 2>&1 | FileCheck %s --check-prefixes=CHECK,AVX1
define void @udiv() {
; SSE2-LABEL: 'udiv'
@@ -567,3 +567,17 @@ define void @srem() {
ret void
}
+define void @scalar(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: 'scalar'
+; CHECK-NEXT: Cost Model: Found costs of RThru:26 CodeSize:3 Lat:23 SizeLat:5 for: %udiv = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:26 CodeSize:3 Lat:23 SizeLat:5 for: %sdiv = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:26 CodeSize:3 Lat:23 SizeLat:5 for: %urem = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:26 CodeSize:3 Lat:23 SizeLat:5 for: %srem = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+; CHECK-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+ %udiv = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ %sdiv = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ %urem = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ %srem = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret void
+}
diff --git a/llvm/test/Assembler/masked-divrem-scalar.ll b/llvm/test/Assembler/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..68da9d38e018f0
--- /dev/null
+++ b/llvm/test/Assembler/masked-divrem-scalar.ll
@@ -0,0 +1,36 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+; CHECK-LABEL: define i1 @udiv_i1(
+; CHECK: %r = call i1 @llvm.masked.udiv.i1(i1 %a, i1 %b, i1 %m)
+define i1 @udiv_i1(i1 %a, i1 %b, i1 %m) {
+ %r = call i1 @llvm.masked.udiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+; CHECK-LABEL: define i8 @sdiv_i8(
+; CHECK: %r = call i8 @llvm.masked.sdiv.i8(i8 %a, i8 %b, i1 %m)
+define i8 @sdiv_i8(i8 %a, i8 %b, i1 %m) {
+ %r = call i8 @llvm.masked.sdiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+; CHECK-LABEL: define i32 @urem_i32(
+; CHECK: %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+; CHECK-LABEL: define i64 @srem_i64(
+; CHECK: %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+define i64 @srem_i64(i64 %a, i64 %b, i1 %m) {
+ %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+; CHECK-LABEL: define i128 @sdiv_i128(
+; CHECK: %r = call i128 @llvm.masked.sdiv.i128(i128 %a, i128 %b, i1 %m)
+define i128 @sdiv_i128(i128 %a, i128 %b, i1 %m) {
+ %r = call i128 @llvm.masked.sdiv.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
diff --git a/llvm/test/CodeGen/SPIRV/masked-divrem-scalar.ll b/llvm/test/CodeGen/SPIRV/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..a636311206e4ad
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/masked-divrem-scalar.ll
@@ -0,0 +1,53 @@
+; RUN: llc -mtriple=spirv64 -O0 < %s | FileCheck %s
+
+; CHECK-DAG: %[[#I64:]] = OpTypeInt 64 0
+; CHECK-DAG: %[[#BOOL:]] = OpTypeBool
+; CHECK-DAG: %[[#I32:]] = OpTypeInt 32 0
+; CHECK-DAG: %[[#ONE64:]] = OpConstant %[[#I64]] 1
+; CHECK-DAG: %[[#ONE32:]] = OpConstant %[[#I32]] 1
+; CHECK: OpFunction
+; CHECK-NEXT: %[[#P32:]] = OpFunctionParameter
+; CHECK-NEXT: %[[#P64:]] = OpFunctionParameter
+; CHECK-NEXT: %[[#A32:]] = OpFunctionParameter %[[#I32]]
+; CHECK-NEXT: %[[#B32:]] = OpFunctionParameter %[[#I32]]
+; CHECK-NEXT: %[[#A64:]] = OpFunctionParameter %[[#I64]]
+; CHECK-NEXT: %[[#B64:]] = OpFunctionParameter %[[#I64]]
+; CHECK-NEXT: %[[#M:]] = OpFunctionParameter %[[#BOOL]]
+; CHECK-NEXT: OpLabel
+; CHECK-NEXT: %[[#D1:]] = OpSelect %[[#I32]] %[[#M]] %[[#B32]] %[[#ONE32]]
+; CHECK-NEXT: %[[#]] = OpUDiv %[[#I32]] %[[#A32]] %[[#D1]]
+; CHECK-NEXT: %[[#D2:]] = OpSelect %[[#I32]] %[[#M]] %[[#B32]] %[[#ONE32]]
+; CHECK-NEXT: %[[#]] = OpSDiv %[[#I32]] %[[#A32]] %[[#D2]]
+; CHECK-NEXT: %[[#D3:]] = OpSelect %[[#I32]] %[[#M]] %[[#B32]] %[[#ONE32]]
+; CHECK-NEXT: %[[#]] = OpUMod %[[#I32]] %[[#A32]] %[[#D3]]
+; CHECK-NEXT: %[[#D4:]] = OpSelect %[[#I32]] %[[#M]] %[[#B32]] %[[#ONE32]]
+; CHECK-NEXT: %[[#]] = OpSRem %[[#I32]] %[[#A32]] %[[#D4]]
+; CHECK: %[[#D5:]] = OpSelect %[[#I64]] %[[#M]] %[[#B64]] %[[#ONE64]]
+; CHECK-NEXT: %[[#]] = OpUDiv %[[#I64]] %[[#A64]] %[[#D5]]
+; CHECK-NEXT: %[[#D6:]] = OpSelect %[[#I64]] %[[#M]] %[[#B64]] %[[#ONE64]]
+; CHECK-NEXT: %[[#]] = OpSDiv %[[#I64]] %[[#A64]] %[[#D6]]
+; CHECK-NEXT: %[[#D7:]] = OpSelect %[[#I64]] %[[#M]] %[[#B64]] %[[#ONE64]]
+; CHECK-NEXT: %[[#]] = OpUMod %[[#I64]] %[[#A64]] %[[#D7]]
+; CHECK-NEXT: %[[#D8:]] = OpSelect %[[#I64]] %[[#M]] %[[#B64]] %[[#ONE64]]
+; CHECK-NEXT: %[[#]] = OpSRem %[[#I64]] %[[#A64]] %[[#D8]]
+; CHECK: OpFunctionEnd
+
+define void @masked_divrem(ptr %0, ptr %1, i32 %2, i32 %3, i64 %4, i64 %5, i1 %6) {
+ %8 = call i32 @llvm.masked.udiv.i32(i32 %2, i32 %3, i1 %6)
+ %9 = call i32 @llvm.masked.sdiv.i32(i32 %2, i32 %3, i1 %6)
+ %10 = call i32 @llvm.masked.urem.i32(i32 %2, i32 %3, i1 %6)
+ %11 = call i32 @llvm.masked.srem.i32(i32 %2, i32 %3, i1 %6)
+ store i32 %8, ptr %0
+ store i32 %9, ptr %0
+ store i32 %10, ptr %0
+ store i32 %11, ptr %0
+ %12 = call i64 @llvm.masked.udiv.i64(i64 %4, i64 %5, i1 %6)
+ %13 = call i64 @llvm.masked.sdiv.i64(i64 %4, i64 %5, i1 %6)
+ %14 = call i64 @llvm.masked.urem.i64(i64 %4, i64 %5, i1 %6)
+ %15 = call i64 @llvm.masked.srem.i64(i64 %4, i64 %5, i1 %6)
+ store i64 %12, ptr %1
+ store i64 %13, ptr %1
+ store i64 %14, ptr %1
+ store i64 %15, ptr %1
+ ret void
+}
diff --git a/llvm/test/CodeGen/X86/masked-divrem-scalar.ll b/llvm/test/CodeGen/X86/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..a0f178e22826f8
--- /dev/null
+++ b/llvm/test/CodeGen/X86/masked-divrem-scalar.ll
@@ -0,0 +1,83 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=x86_64 < %s | FileCheck %s
+
+define i1 @sdiv_i1(i1 %a, i1 %b, i1 %m) {
+; CHECK-LABEL: sdiv_i1:
+; CHECK: # %bb.0:
+; CHECK-NEXT: xorl %eax, %eax
+; CHECK-NEXT: retq
+ %r = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i32 @udiv_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: udiv_i32:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: testb $1, %dl
+; CHECK-NEXT: movl $1, %ecx
+; CHECK-NEXT: cmovnel %esi, %ecx
+; CHECK-NEXT: xorl %edx, %edx
+; CHECK-NEXT: divl %ecx
+; CHECK-NEXT: retq
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: sdiv_i32:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: testb $1, %dl
+; CHECK-NEXT: movl $1, %ecx
+; CHECK-NEXT: cmovnel %esi, %ecx
+; CHECK-NEXT: cltd
+; CHECK-NEXT: idivl %ecx
+; CHECK-NEXT: retq
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: urem_i32:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: testb $1, %dl
+; CHECK-NEXT: movl $1, %ecx
+; CHECK-NEXT: cmovnel %esi, %ecx
+; CHECK-NEXT: xorl %edx, %edx
+; CHECK-NEXT: divl %ecx
+; CHECK-NEXT: movl %edx, %eax
+; CHECK-NEXT: retq
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @srem_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: srem_i32:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: testb $1, %dl
+; CHECK-NEXT: movl $1, %ecx
+; CHECK-NEXT: cmovnel %esi, %ecx
+; CHECK-NEXT: cltd
+; CHECK-NEXT: idivl %ecx
+; CHECK-NEXT: movl %edx, %eax
+; CHECK-NEXT: retq
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @mask_poison(i32 %a, i32 %b) {
+; CHECK-LABEL: mask_poison:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: testb $1, %al
+; CHECK-NEXT: movl $1, %ecx
+; CHECK-NEXT: cmovnel %esi, %ecx
+; CHECK-NEXT: cltd
+; CHECK-NEXT: idivl %ecx
+; CHECK-NEXT: retq
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 poison)
+ ret i32 %r
+}
diff --git a/llvm/test/Instrumentation/MemorySanitizer/masked-divrem.ll b/llvm/test/Instrumentation/MemorySanitizer/masked-divrem.ll
index 5fac18ab75a180..3bb58fd86b9981 100644
--- a/llvm/test/Instrumentation/MemorySanitizer/masked-divrem.ll
+++ b/llvm/test/Instrumentation/MemorySanitizer/masked-divrem.ll
@@ -299,3 +299,26 @@ entry:
store <vscale x 4 x i32> %r, ptr %pr
ret void
}
+
+define i32 @scalar(i32 %a, i32 %b, i1 %mask) sanitize_memory {
+; CHECK-LABEL: @scalar(
+; CHECK-NEXT: [[TMP1:%.*]] = load i1, ptr getelementptr (i8, ptr @__msan_param_tls, i64 16), align 8
+; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr getelementptr (i8, ptr @__msan_param_tls, i64 8), align 8
+; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[_MSMASKEDDIVISOR:%.*]] = select i1 [[MASK:%.*]], i32 [[TMP2]], i32 0
+; CHECK-NEXT: [[_MSMASKEDDIV:%.*]] = select i1 [[MASK]], i32 [[TMP3]], i32 -1
+; CHECK-NEXT: [[_MSCMP:%.*]] = icmp ne i32 [[_MSMASKEDDIVISOR]], 0
+; CHECK-NEXT: [[_MSOR:%.*]] = or i1 [[TMP1]], [[_MSCMP]]
+; CHECK-NEXT: br i1 [[_MSOR]], label [[TMP4:%.*]], label [[TMP5:%.*]], !prof [[PROF1]]
+; CHECK: 4:
+; CHECK-NEXT: call void @__msan_warning_noreturn() #[[ATTR5]]
+; CHECK-NEXT: unreachable
+; CHECK: 5:
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A:%.*]], i32 [[B:%.*]], i1 [[MASK]])
+; CHECK-NEXT: store i32 [[_MSMASKEDDIV]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %mask)
+ ret i32 %r
+}
diff --git a/llvm/test/Transforms/PreISelIntrinsicLowering/masked-divrem-scalar.ll b/llvm/test/Transforms/PreISelIntrinsicLowering/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..53fbb0b809f46d
--- /dev/null
+++ b/llvm/test/Transforms/PreISelIntrinsicLowering/masked-divrem-scalar.ll
@@ -0,0 +1,103 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; REQUIRES: x86-registered-target
+; RUN: opt -passes=pre-isel-intrinsic-lowering -mtriple=x86_64 -S < %s | FileCheck %s
+
+define i1 @udiv_i1(i1 %a, i1 %b, i1 %m) {
+; CHECK-LABEL: define i1 @udiv_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i1 [[B]], i1 true
+; CHECK-NEXT: [[R:%.*]] = udiv i1 [[A]], [[TMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %r = call i1 @llvm.masked.udiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i1 @sdiv_i1(i1 %a, i1 %b, i1 %m) {
+; CHECK-LABEL: define i1 @sdiv_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 false
+;
+ %r = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i1 @srem_i1(i1 %a, i1 %b, i1 %m) {
+; CHECK-LABEL: define i1 @srem_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 false
+;
+ %r = call i1 @llvm.masked.srem.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i32 @udiv_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @udiv_i32(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[B]], i32 1
+; CHECK-NEXT: [[R:%.*]] = udiv i32 [[A]], [[TMP2]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @sdiv_i32(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[B]], i32 1
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], [[TMP2]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @urem_i32(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[B]], i32 1
+; CHECK-NEXT: [[R:%.*]] = urem i32 [[A]], [[TMP2]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i128 @srem_i128(i128 %a, i128 %b, i1 %m) {
+; CHECK-LABEL: define i128 @srem_i128(
+; CHECK-SAME: i128 [[A:%.*]], i128 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i128 [[B]], i128 1
+; CHECK-NEXT: [[R:%.*]] = srem i128 [[A]], [[TMP2]]
+; CHECK-NEXT: ret i128 [[R]]
+;
+ %r = call i128 @llvm.masked.srem.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
+
+define i32 @mask_poison(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @mask_poison(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 poison
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[B]], i32 1
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], [[TMP2]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 poison)
+ ret i32 %r
+}
+
+define <2 x i32> @vector_control(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+; CHECK-LABEL: define <2 x i32> @vector_control(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i1> [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[A]], <2 x i32> [[B]], <2 x i1> [[M]])
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m)
+ ret <2 x i32> %r
+}
diff --git a/llvm/test/Verifier/invalid_intrinsic_signatures.ll b/llvm/test/Verifier/invalid_intrinsic_signatures.ll
index 03d8bd6fd7fb42..e0cd8704a2c75a 100644
--- a/llvm/test/Verifier/invalid_intrinsic_signatures.ll
+++ b/llvm/test/Verifier/invalid_intrinsic_signatures.ll
@@ -116,22 +116,38 @@ declare <4 x i1> @llvm.experimental.vector.match.v4i32.v4i32(<4 x i32>, <4 x i32
; CHECK-NEXT: declare <8 x i1> @llvm.experimental.vector.match.v8i32.v8i32(<8 x i32>, <8 x i32>, <8 x i32>)
declare <8 x i1> @llvm.experimental.vector.match.v8i32.v8i32(<8 x i32>, <8 x i32>, <8 x i32>)
-; CHECK: intrinsic return type (overload type 0) expected any integer vector, but got <4 x float>
+; CHECK: intrinsic return type (overload type 0) expected any integer or integer vector, but got <4 x float>
; CHECK-NEXT: declare <4 x float> @llvm.masked.udiv.v4f32(<4 x float>, <4 x float>, <4 x i1>)
declare <4 x float> @llvm.masked.udiv.v4f32(<4 x float>, <4 x float>, <4 x i1>)
-; CHECK: intrinsic return type (overload type 0) expected any integer vector, but got <4 x float>
+; CHECK: intrinsic return type (overload type 0) expected any integer or integer vector, but got <4 x float>
; CHECK-NEXT: declare <4 x float> @llvm.masked.sdiv.v4f32(<4 x float>, <4 x float>, <4 x i1>)
declare <4 x float> @llvm.masked.sdiv.v4f32(<4 x float>, <4 x float>, <4 x i1>)
-; CHECK: intrinsic return type (overload type 0) expected any integer vector, but got <4 x float>
+; CHECK: intrinsic return type (overload type 0) expected any integer or integer vector, but got <4 x float>
; CHECK-NEXT: declare <4 x float> @llvm.masked.urem.v4f32(<4 x float>, <4 x float>, <4 x i1>)
declare <4 x float> @llvm.masked.urem.v4f32(<4 x float>, <4 x float>, <4 x i1>)
-; CHECK: intrinsic return type (overload type 0) expected any integer vector, but got <4 x float>
+; CHECK: intrinsic return type (overload type 0) expected any integer or integer vector, but got <4 x float>
; CHECK-NEXT: declare <4 x float> @llvm.masked.srem.v4f32(<4 x float>, <4 x float>, <4 x i1>)
declare <4 x float> @llvm.masked.srem.v4f32(<4 x float>, <4 x float>, <4 x i1>)
+; CHECK: intrinsic argument 2 type (same vector width of overload type 0) expected scalar (overload type 0 is i32), but got <1 x i1>
+; CHECK-NEXT: declare i32 @llvm.masked.udiv.i32(i32, i32, <1 x i1>)
+declare i32 @llvm.masked.udiv.i32(i32, i32, <1 x i1>)
+
+; CHECK: intrinsic argument 2 type expected i1, but got i32
+; CHECK-NEXT: declare i32 @llvm.masked.sdiv.i32(i32, i32, i32)
+declare i32 @llvm.masked.sdiv.i32(i32, i32, i32)
+
+; CHECK: intrinsic argument 2 type (same vector width of overload type 0) expected vector (overload type 0 is <2 x i32>), but got i1
+; CHECK-NEXT: declare <2 x i32> @llvm.masked.urem.v2i32(<2 x i32>, <2 x i32>, i1)
+declare <2 x i32> @llvm.masked.urem.v2i32(<2 x i32>, <2 x i32>, i1)
+
+; CHECK: intrinsic return type (overload type 0) expected any integer or integer vector, but got float
+; CHECK-NEXT: declare float @llvm.masked.srem.f32(float, float, i1)
+declare float @llvm.masked.srem.f32(float, float, i1)
+
; CHECK: intrinsic argument 1 type (same vector width of overload type 0) expected vector with vscale x 4 elements (overload type 0 is <vscale x 4 x i32>), but got <4 x i1>
; CHECK-NEXT: declare <vscale x 4 x i32> @llvm.masked.load.nxv4i32.p0(ptr, <4 x i1>, <vscale x 4 x i32>)
declare <vscale x 4 x i32> @llvm.masked.load.nxv4i32.p0(ptr, <4 x i1>, <vscale x 4 x i32>)
diff --git a/llvm/unittests/IR/IntrinsicsTest.cpp b/llvm/unittests/IR/IntrinsicsTest.cpp
index aaf10a92e24c66..14e3d82c58dca4 100644
--- a/llvm/unittests/IR/IntrinsicsTest.cpp
+++ b/llvm/unittests/IR/IntrinsicsTest.cpp
@@ -428,4 +428,17 @@ TEST_F(IntrinsicsTest, IRBuilderCreateIntrinsicVarArg) {
<< "experimental_gc_statepoint must be vararg";
}
+TEST_F(IntrinsicsTest, MaskedIntDivRemOpcode) {
+ EXPECT_EQ(Intrinsic::getMaskedIntDivRemOpcode(Intrinsic::masked_udiv),
+ unsigned(Instruction::UDiv));
+ EXPECT_EQ(Intrinsic::getMaskedIntDivRemOpcode(Intrinsic::masked_sdiv),
+ unsigned(Instruction::SDiv));
+ EXPECT_EQ(Intrinsic::getMaskedIntDivRemOpcode(Intrinsic::masked_urem),
+ unsigned(Instruction::URem));
+ EXPECT_EQ(Intrinsic::getMaskedIntDivRemOpcode(Intrinsic::masked_srem),
+ unsigned(Instruction::SRem));
+ EXPECT_EQ(Intrinsic::getMaskedIntDivRemOpcode(Intrinsic::not_intrinsic), 0u);
+ EXPECT_EQ(Intrinsic::getMaskedIntDivRemOpcode(Intrinsic::umax), 0u);
+}
+
} // end namespace
>From 7e7f600249056b4d11a829980b373de80c10861a Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 11:41:06 +0100
Subject: [PATCH 2/9] [CodeGen] Add TargetLowering::isIntDivRemTrapFree(EVT)
Add TargetLoweringBase::isIntDivRemTrapFree(EVT) for scalar integer
types, false by default: targets return true in subsequent patches when
their complete lowering of div/rem of the type cannot trap or have side
effects for any operands. On such types the mask of a scalar or
scalarized masked div/rem is dropped and the ordinary operation is
selected, because its result refines the poison of a disabled operation.
Non-trap-free scalar types keep the guarded lowering from the previous
patch. BasicTTIImpl omits the divisor-select cost on trap-free scalar
and scalarized fixed-vector types and returns zero cost for signed i1
masked div/rem.
Also fix masked div/rem lowering in general and clarify poison mask
lanes for the vector intrinsics in LangRef (matching
https://github.com/llvm/llvm-project/pull/240755 for llvm.vp.{u,s}{div,rem}):
* Freeze vector masks in SelectionDAGBuilder before splitting, widening,
promotion and scalarization turn them into divisor selects, so a
poison mask cannot introduce UB where the ordinary division would be
safe.
* Fold signed i1 vector masked div/rem directly to zero during generic
vector expansion and scalarization, since 1 is -1 in 1-bit two's
complement and is not a safe fallback divisor.
* GlobalISel lowers fixed-vector masked div/rem (including single-lane
fixed vectors) one lane at a time when the target has no legal or
custom plain vector operation, and spreads result G_FREEZE across
per-lane G_BUILD_VECTOR results. This fixes an assertion for
llvm.masked.sdiv.v3i8 on AArch64 at -O0.
* Fixed vectors scalarized on trap-free element types without a legal or
custom vector lowering emit one plain scalar operation per active
lane, folding constant-false mask lanes and known-zero or undef
divisors to 0.
* CodeGenPrepare applies the short-division bypass to bundle-free scalar
masked operations when both widths are trap-free.
A trap-free operation or scalarized lane whose mask is constant false or
whose divisor is undef or known zero at lowering, by IR or DAG known
bits, gets the constant 0 rather than a division that SelectionDAG would
fold to undef. Generic selection lowers freeze(undef) as a copy of an
implicit def, which is not stable across uses; a zero divisor that no
known-bits query proves before mask elision still reaches that generic
limitation.
Assisted-by: Claude
---
llvm/docs/LangRef.md | 56 +-
llvm/docs/ReleaseNotes.md | 5 +-
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 28 +-
llvm/include/llvm/CodeGen/ISDOpcodes.h | 3 +-
llvm/include/llvm/CodeGen/TargetLowering.h | 9 +
.../Transforms/Utils/BypassSlowDivision.h | 21 +-
llvm/lib/CodeGen/CodeGenPrepare.cpp | 6 +-
llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp | 112 ++++
llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp | 36 +-
.../SelectionDAG/LegalizeVectorOps.cpp | 37 +-
.../SelectionDAG/LegalizeVectorTypes.cpp | 24 +
.../SelectionDAG/SelectionDAGBuilder.cpp | 100 +++-
.../Transforms/Utils/BypassSlowDivision.cpp | 133 +++--
.../GlobalISel/masked-divrem-vectors.ll | 115 ++++
.../masked-divrem-vector-legalization.ll | 133 +++++
.../AArch64/masked-sdiv-fixed-length.ll | 30 +
.../AArch64/masked-udiv-fixed-length.ll | 28 +
llvm/test/CodeGen/PowerPC/masked-sdiv.ll | 24 +-
llvm/test/CodeGen/PowerPC/masked-srem.ll | 24 +-
llvm/test/CodeGen/PowerPC/masked-udiv.ll | 16 +-
llvm/test/CodeGen/PowerPC/masked-urem.ll | 16 +-
.../SPIRV/masked-divrem-vector-unsupported.ll | 9 +
llvm/test/CodeGen/X86/masked-sdiv.ll | 534 ++++++++++++++++++
llvm/test/CodeGen/X86/masked-udiv.ll | 202 +++++++
.../CodeGenPrepare/X86/masked-divrem.ll | 42 ++
25 files changed, 1620 insertions(+), 123 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll
create mode 100644 llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll
create mode 100644 llvm/test/CodeGen/SPIRV/masked-divrem-vector-unsupported.ll
create mode 100644 llvm/test/Transforms/CodeGenPrepare/X86/masked-divrem.ll
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index f3e8b2c8f84540..52846427783686 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -24509,8 +24509,18 @@ The third argument is an `i1` mask for scalar operands, or a vector of `i1` with
##### Semantics:
-Follows the same semantics as {ref}`udiv <i_udiv>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
-For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`udiv <i_udiv>` would.
+Performs {ref}`udiv <i_udiv>` on each enabled or poison lane.
+The result on disabled and poison lanes is a {ref}`poison value <poisonvalues>`.
+Disabled lanes do not produce undefined behavior.
+For scalar operands, the mask describes a single lane.
+An undef mask lane may be either enabled or disabled.
+
+Undefined behavior in an enabled or poison lane follows the rules of {ref}`udiv <i_udiv>`.
+In particular, a poison divisor in such a lane causes undefined behavior.
+
+:::{note}
+A transform that keeps the masked operation may only introduce a poison divisor into a previously disabled lane if it keeps that lane's mask false.
+:::
#### '`llvm.masked.sdiv.*`' Intrinsics
@@ -24534,8 +24544,18 @@ The third argument is an `i1` mask for scalar operands, or a vector of `i1` with
##### Semantics:
-Follows the same semantics as {ref}`sdiv <i_sdiv>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
-For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`sdiv <i_sdiv>` would.
+Performs {ref}`sdiv <i_sdiv>` on each enabled or poison lane.
+The result on disabled and poison lanes is a {ref}`poison value <poisonvalues>`.
+Disabled lanes do not produce undefined behavior.
+For scalar operands, the mask describes a single lane.
+An undef mask lane may be either enabled or disabled.
+
+Undefined behavior in an enabled or poison lane follows the rules of {ref}`sdiv <i_sdiv>`.
+In particular, a poison divisor in such a lane causes undefined behavior.
+
+:::{note}
+A transform that keeps the masked operation may only introduce a poison divisor into a previously disabled lane if it keeps that lane's mask false.
+:::
#### '`llvm.masked.urem.*`' Intrinsics
@@ -24559,8 +24579,18 @@ The third argument is an `i1` mask for scalar operands, or a vector of `i1` with
##### Semantics:
-Follows the same semantics as {ref}`urem <i_urem>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
-For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`urem <i_urem>` would.
+Performs {ref}`urem <i_urem>` on each enabled or poison lane.
+The result on disabled and poison lanes is a {ref}`poison value <poisonvalues>`.
+Disabled lanes do not produce undefined behavior.
+For scalar operands, the mask describes a single lane.
+An undef mask lane may be either enabled or disabled.
+
+Undefined behavior in an enabled or poison lane follows the rules of {ref}`urem <i_urem>`.
+In particular, a poison divisor in such a lane causes undefined behavior.
+
+:::{note}
+A transform that keeps the masked operation may only introduce a poison divisor into a previously disabled lane if it keeps that lane's mask false.
+:::
#### '`llvm.masked.srem.*`' Intrinsics
@@ -24584,8 +24614,18 @@ The third argument is an `i1` mask for scalar operands, or a vector of `i1` with
##### Semantics:
-Follows the same semantics as {ref}`srem <i_srem>` with the exception that disabled lanes cannot produce undefined behaviour and always result in poison.
-For scalar operands, the mask describes a single lane, and a poison mask gives a {ref}`poison value <poisonvalues>` and has undefined behavior only where {ref}`srem <i_srem>` would.
+Performs {ref}`srem <i_srem>` on each enabled or poison lane.
+The result on disabled and poison lanes is a {ref}`poison value <poisonvalues>`.
+Disabled lanes do not produce undefined behavior.
+For scalar operands, the mask describes a single lane.
+An undef mask lane may be either enabled or disabled.
+
+Undefined behavior in an enabled or poison lane follows the rules of {ref}`srem <i_srem>`.
+In particular, a poison divisor in such a lane causes undefined behavior.
+
+:::{note}
+A transform that keeps the masked operation may only introduce a poison divisor into a previously disabled lane if it keeps that lane's mask false.
+:::
### Speculative Load Intrinsics
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 17b9ff836b16f2..202b18bcf1fd9b 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -61,7 +61,10 @@ Makes programs 10x faster by doing Special New Thing.
* The `llvm.masked.{u,s}{div,rem}` intrinsics now also accept scalar integer
operands with an `i1` mask. Disabled operations return `poison` without
- undefined behavior.
+ undefined behavior. Their vector forms now define `poison` mask lanes
+ consistently with VP division and remainder: the operation is performed on
+ enabled or `poison` lanes, and disabled and `poison` lanes return `poison`
+ when the operation does not otherwise have undefined behavior.
* LLVM now assigns persistent print IDs to metadata nodes. Reusing these IDs
avoids repeated module-wide scans to rebuild metadata numbering, which can
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index f8d9701fc26f79..d27eaaf4b4e703 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -2865,13 +2865,37 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
default:
llvm_unreachable("Unexpected intrinsic ID");
}
+ if (RetTy->getScalarSizeInBits() == 1 &&
+ (IID == Intrinsic::masked_sdiv || IID == Intrinsic::masked_srem))
+ return 0;
unsigned UnmaskedOpc = Intrinsic::getMaskedIntDivRemOpcode(IID);
InstructionCost Cost =
thisT()->getArithmeticInstrCost(UnmaskedOpc, RetTy, CostKind);
- // Expansion generates a (select %mask, %rhs, 1) for the divisor.
+ // Expansion generates a (select %mask, %rhs, 1) for the divisor unless
+ // the target handles the masked operation, or scalarizes a fixed vector
+ // on a trap-free element type without a vector div/rem lowering.
+ const TargetLoweringBase *TLI = getTLI();
MVT LT = getTypeLegalizationCost(RetTy).second;
- if (!getTLI()->isOperationLegalOrCustom(ISD, LT)) {
+ bool NeedsMask;
+ if (RetTy->isIntegerTy()) {
+ NeedsMask = !TLI->isIntDivRemTrapFree(EVT::getEVT(RetTy));
+ } else if (TLI->isOperationLegalOrCustom(ISD, LT)) {
+ NeedsMask = false;
+ } else if (isa<FixedVectorType>(RetTy) &&
+ TLI->isIntDivRemTrapFree(
+ EVT::getEVT(RetTy->getScalarType()))) {
+ unsigned UnmaskedISD = ISD::getUnmaskedBinOpOpcode(ISD);
+ unsigned DivISD = (UnmaskedISD == ISD::SREM) ? ISD::SDIV
+ : (UnmaskedISD == ISD::UREM) ? ISD::UDIV
+ : UnmaskedISD;
+ NeedsMask =
+ LT.isVector() && (TLI->isOperationLegalOrCustom(UnmaskedISD, LT) ||
+ TLI->isOperationLegalOrCustom(DivISD, LT));
+ } else {
+ NeedsMask = true;
+ }
+ if (NeedsMask) {
Type *CondTy = RetTy->getWithNewBitWidth(1);
Cost += thisT()->getCmpSelInstrCost(
BinaryOperator::Select, RetTy, CondTy, CmpInst::BAD_ICMP_PREDICATE,
diff --git a/llvm/include/llvm/CodeGen/ISDOpcodes.h b/llvm/include/llvm/CodeGen/ISDOpcodes.h
index d894b3c54dfda8..012c00ac3d8869 100644
--- a/llvm/include/llvm/CodeGen/ISDOpcodes.h
+++ b/llvm/include/llvm/CodeGen/ISDOpcodes.h
@@ -1665,7 +1665,8 @@ enum NodeType {
/// Masked vector arithmetic that returns poison on disabled lanes. Disabled
/// lanes do not have undefined behaviour on division by zero or overflow. The
- /// first two operands are input vectors, the third operand is the mask.
+ /// first two operands are input vectors, the third operand is a non-poison
+ /// mask (frozen before type legalization).
MASKED_UDIV,
MASKED_SDIV,
MASKED_UREM,
diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index 7ff1c3084c3036..95329aa335338a 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -605,6 +605,15 @@ class LLVM_ABI TargetLoweringBase {
/// for speed or for size.
virtual bool isIntDivCheap(EVT VT, AttributeList Attr) const { return false; }
+ /// Return true if integer div/rem of scalar integer type VT never traps or
+ /// has side effects, even for a zero divisor or signed overflow. Exceptional
+ /// operands produce some value of VT. This must cover the complete lowering,
+ /// including any libcalls; support for native widths does not imply support
+ /// for wider types. Callers lowering vector masked operations must still
+ /// preserve valid lanes when folding exceptional operands, for example by
+ /// using target nodes rather than ordinary vector DAG div/rem nodes.
+ virtual bool isIntDivRemTrapFree(EVT VT) const { return false; }
+
/// Return true if the target can handle a standalone remainder operation.
virtual bool hasStandaloneRem(EVT VT) const {
return true;
diff --git a/llvm/include/llvm/Transforms/Utils/BypassSlowDivision.h b/llvm/include/llvm/Transforms/Utils/BypassSlowDivision.h
index 7b4b728dc6a8d1..774b1bb1cb1a9e 100644
--- a/llvm/include/llvm/Transforms/Utils/BypassSlowDivision.h
+++ b/llvm/include/llvm/Transforms/Utils/BypassSlowDivision.h
@@ -19,6 +19,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/IR/ValueHandle.h"
#include <cstdint>
@@ -28,30 +29,36 @@ class BasicBlock;
class BranchProbabilityInfo;
class DomTreeUpdater;
class LoopInfo;
+class Type;
class Value;
struct DivRemMapKey {
bool SignedOp;
AssertingVH<Value> Dividend;
AssertingVH<Value> Divisor;
+ AssertingVH<Value> Mask;
DivRemMapKey() = default;
- DivRemMapKey(bool InSignedOp, Value *InDividend, Value *InDivisor)
- : SignedOp(InSignedOp), Dividend(InDividend), Divisor(InDivisor) {}
+ DivRemMapKey(bool InSignedOp, Value *InDividend, Value *InDivisor,
+ Value *InMask = nullptr)
+ : SignedOp(InSignedOp), Dividend(InDividend), Divisor(InDivisor),
+ Mask(InMask) {}
};
template <> struct DenseMapInfo<DivRemMapKey> {
static bool isEqual(const DivRemMapKey &Val1, const DivRemMapKey &Val2) {
return Val1.SignedOp == Val2.SignedOp && Val1.Dividend == Val2.Dividend &&
- Val1.Divisor == Val2.Divisor;
+ Val1.Divisor == Val2.Divisor && Val1.Mask == Val2.Mask;
}
static unsigned getHashValue(const DivRemMapKey &Val) {
return (unsigned)(reinterpret_cast<uintptr_t>(
static_cast<Value *>(Val.Dividend)) ^
reinterpret_cast<uintptr_t>(
- static_cast<Value *>(Val.Divisor))) ^
+ static_cast<Value *>(Val.Divisor)) ^
+ reinterpret_cast<uintptr_t>(
+ static_cast<Value *>(Val.Mask))) ^
(unsigned)Val.SignedOp;
}
};
@@ -59,13 +66,17 @@ template <> struct DenseMapInfo<DivRemMapKey> {
/// This optimization identifies DIV instructions in a BB that can be
/// profitably bypassed and carried out with a shorter, faster divide.
///
+/// Scalar masked div/rem is also handled when IsIntDivRemTrapFree accepts both
+/// the original and bypass types.
+///
/// This optimization may add basic blocks immediately after BB; for obvious
/// reasons, you shouldn't pass those blocks to bypassSlowDivision.
LLVM_ABI bool
bypassSlowDivision(BasicBlock *BB,
const DenseMap<unsigned int, unsigned int> &BypassWidth,
DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
- BranchProbabilityInfo *BPI = nullptr);
+ BranchProbabilityInfo *BPI = nullptr,
+ function_ref<bool(Type *)> IsIntDivRemTrapFree = {});
} // end namespace llvm
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 968d9207aec07a..bcb3777ba88791 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -483,7 +483,11 @@ bool CodeGenPrepare::_run(Function &F) {
// optimization to those blocks.
BasicBlock *Next = BB->getNextNode();
if (!llvm::shouldOptimizeForSize(BB, PSI, BFI))
- EverMadeChange |= bypassSlowDivision(BB, BypassWidths, DTU, LI, BPI);
+ EverMadeChange |= bypassSlowDivision(
+ BB, BypassWidths, DTU, LI, BPI, [this, &F](Type *Ty) {
+ return TLI->isIntDivRemTrapFree(
+ TLI->getValueType(F.getDataLayout(), Ty));
+ });
BB = Next;
}
}
diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index 66c8611de687f9..29de90247d29ab 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -19,6 +19,7 @@
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
+#include "llvm/Analysis/SimplifyQuery.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/CodeGen/Analysis.h"
@@ -29,6 +30,7 @@
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
#include "llvm/CodeGen/GlobalISel/InlineAsmLowering.h"
+#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/CodeGen/LowLevelTypeUtils.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -83,6 +85,7 @@
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/KnownBits.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
@@ -3066,6 +3069,97 @@ bool IRTranslatorImpl::translateKnownIntrinsic(const CallInst &CI,
switch (ID) {
default:
break;
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem: {
+ bool IsSigned =
+ ID == Intrinsic::masked_sdiv || ID == Intrinsic::masked_srem;
+ unsigned Opcode = ID == Intrinsic::masked_udiv ? TargetOpcode::G_UDIV
+ : ID == Intrinsic::masked_sdiv ? TargetOpcode::G_SDIV
+ : ID == Intrinsic::masked_urem ? TargetOpcode::G_UREM
+ : TargetOpcode::G_SREM;
+
+ if (auto *VT = dyn_cast<FixedVectorType>(CI.getType())) {
+ const LegalizerInfo *LI = MF->getSubtarget().getLegalizerInfo();
+ LLT Ty = getLLTForType(*CI.getType(), *DL);
+ if (!LI || (Ty.isVector() && LI->isLegalOrCustom({Opcode, {Ty}})))
+ return false;
+ LLT EltTy = getLLTForType(*VT->getElementType(), *DL);
+ bool TrapFree = TLI->isIntDivRemTrapFree(
+ TLI->getValueType(*DL, VT->getElementType()));
+ SmallVector<Register> LHS, RHS, Mask;
+ auto UnmergeOrScalar = [&](Value *V, LLT LaneTy,
+ SmallVectorImpl<Register> &Out) {
+ Register Reg = getOrCreateVReg(*V);
+ if (!Ty.isVector()) {
+ Out.push_back(Reg);
+ return;
+ }
+ auto Unmerge = MIRBuilder.buildUnmerge(LaneTy, Reg);
+ for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I)
+ Out.push_back(Unmerge.getReg(I));
+ };
+ UnmergeOrScalar(CI.getArgOperand(0), EltTy, LHS);
+ UnmergeOrScalar(CI.getArgOperand(1), EltTy, RHS);
+ if (!TrapFree)
+ UnmergeOrScalar(CI.getArgOperand(2), LLT::integer(1), Mask);
+
+ const auto *ConstRHS = dyn_cast<Constant>(CI.getArgOperand(1));
+ const auto *ConstMask = dyn_cast<Constant>(CI.getArgOperand(2));
+ SmallVector<Register> Results;
+ for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I) {
+ if (IsSigned && EltTy == LLT::integer(1)) {
+ Results.push_back(MIRBuilder.buildConstant(EltTy, 0).getReg(0));
+ continue;
+ }
+ Constant *MaskElt =
+ ConstMask ? ConstMask->getAggregateElement(I) : nullptr;
+ Register Divisor = RHS[I];
+ if (TrapFree &&
+ ((MaskElt && MaskElt->isNullValue()) ||
+ (ConstRHS &&
+ isa_and_present<UndefValue>(ConstRHS->getAggregateElement(I))) ||
+ computeKnownBits(CI.getArgOperand(1), APInt::getOneBitSet(E, I),
+ SimplifyQuery(*DL, &CI))
+ .isZero())) {
+ Results.push_back(MIRBuilder.buildConstant(EltTy, 0).getReg(0));
+ continue;
+ }
+ if (!TrapFree) {
+ auto FrozenMask = MIRBuilder.buildFreeze(LLT::integer(1), Mask[I]);
+ auto One = MIRBuilder.buildConstant(EltTy, 1);
+ Divisor =
+ MIRBuilder.buildSelect(EltTy, FrozenMask, Divisor, One).getReg(0);
+ }
+ Results.push_back(
+ MIRBuilder.buildInstr(Opcode, {EltTy}, {LHS[I], Divisor})
+ .getReg(0));
+ }
+ if (!Ty.isVector())
+ MIRBuilder.buildCopy(getOrCreateVReg(CI), Results[0]);
+ else
+ MIRBuilder.buildBuildVector(getOrCreateVReg(CI), Results);
+ return true;
+ }
+ if (!CI.getType()->isIntegerTy() ||
+ !TLI->isIntDivRemTrapFree(TLI->getValueType(*DL, CI.getType())))
+ return false;
+ // On a trap-free type, computing an inactive operation refines its poison
+ // result without adding observable behavior.
+ const auto *ConstMask = dyn_cast<Constant>(CI.getArgOperand(2));
+ if ((ConstMask && ConstMask->isNullValue()) ||
+ isa<UndefValue>(CI.getArgOperand(1)) ||
+ computeKnownBits(CI.getArgOperand(1), SimplifyQuery(*DL, &CI))
+ .isZero()) {
+ MIRBuilder.buildConstant(getOrCreateVReg(CI), 0);
+ return true;
+ }
+ MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(CI)},
+ {getOrCreateVReg(*CI.getArgOperand(0)),
+ getOrCreateVReg(*CI.getArgOperand(1))});
+ return true;
+ }
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end: {
// No stack colouring in O0, discard region information.
@@ -4617,6 +4711,24 @@ bool IRTranslatorImpl::translateFreeze(const User &U,
assert(DstRegs.size() == SrcRegs.size() &&
"Freeze with different source and destination type?");
+ if (auto *II = dyn_cast<IntrinsicInst>(U.getOperand(0));
+ II && Intrinsic::getMaskedIntDivRemOpcode(II->getIntrinsicID()) &&
+ isa<FixedVectorType>(U.getType()) &&
+ MRI->getVRegDef(SrcRegs.front())->getOpcode() ==
+ TargetOpcode::G_BUILD_VECTOR) {
+ // Distribute freeze only for the scalarized masked result. Keeping these
+ // lane freezes explicit lets unmerge/build artifacts combine at O0 without
+ // splitting unrelated legal vector arithmetic.
+ LLT EltTy = getLLTForType(*U.getType()->getScalarType(), *DL);
+ auto Parts = MIRBuilder.buildUnmerge(EltTy, SrcRegs.front());
+ SmallVector<Register> Frozen;
+ for (unsigned I = 0; I != Parts->getNumDefs(); ++I)
+ Frozen.push_back(
+ MIRBuilder.buildFreeze(EltTy, Parts.getReg(I)).getReg(0));
+ MIRBuilder.buildBuildVector(DstRegs.front(), Frozen);
+ return true;
+ }
+
for (unsigned I = 0; I < DstRegs.size(); ++I) {
MIRBuilder.buildFreeze(DstRegs[I], SrcRegs[I]);
}
diff --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
index 472015d66b351d..3b04080364b3cc 100644
--- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
+++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
@@ -110,10 +110,14 @@ template <class T> static bool forEachCall(Function &Intrin, T Callback) {
return Changed;
}
-// Lower scalar llvm.masked.{u,s}{div,rem} to a guarded division before ISel.
-static bool lowerScalarMaskedDivRem(Function &F) {
+// Expand scalar masked div/rem before either instruction selector unless the
+// target's complete lowering is trap-free for this type. Vector operations stay
+// masked: an ordinary vector DAG division may fold the whole vector to undef
+// when just one disabled lane has a zero divisor. On trap-free types, a call
+// whose divisor is known zero in every lane gets the result zero here.
+static bool lowerMaskedDivRem(Function &F, const TargetMachine &TM) {
Type *RetTy = F.getReturnType();
- if (!RetTy->isIntegerTy())
+ if (!RetTy->isIntegerTy() && !isa<FixedVectorType>(RetTy))
return false;
Intrinsic::ID ID = F.getIntrinsicID();
@@ -121,9 +125,29 @@ static bool lowerScalarMaskedDivRem(Function &F) {
Instruction::BinaryOps Opcode = static_cast<Instruction::BinaryOps>(
Intrinsic::getMaskedIntDivRemOpcode(ID));
return forEachCall(F, [&](CallInst *CI) {
+ Type *Ty = CI->getType();
+ const TargetLowering *TLI =
+ TM.getSubtargetImpl(*CI->getFunction())->getTargetLowering();
+ if (TLI->isIntDivRemTrapFree(
+ TLI->getValueType(CI->getDataLayout(), Ty->getScalarType()))) {
+ if (CI->hasOperandBundles())
+ return false;
+ Value *Divisor = CI->getArgOperand(1);
+ if (!isa<UndefValue>(Divisor) &&
+ !computeKnownBits(Divisor, CI->getDataLayout(), nullptr, CI).isZero())
+ return false;
+ // Choose the inactive result before IR simplification can turn the
+ // call into poison and lose its masked origin before selection.
+ CI->replaceAllUsesWith(Constant::getNullValue(Ty));
+ CI->eraseFromParent();
+ return true;
+ }
+ if (Ty->isVectorTy())
+ return false;
+
IRBuilder<> Builder(CI);
Value *Result;
- if (IsSigned && RetTy->isIntegerTy(1)) {
+ if (IsSigned && Ty->isIntegerTy(1)) {
// In 1-bit two's complement (0 and -1), divisor 0 is UB and -1 / -1
// (and -1 % -1) overflows INT_MIN / -1; 0 / -1 == 0 is the sole defined
// case, so false refines every outcome without emitting a division.
@@ -133,7 +157,7 @@ static bool lowerScalarMaskedDivRem(Function &F) {
// divisor (immediate UB) when the division itself would be safe.
Value *Mask = Builder.CreateFreeze(CI->getArgOperand(2));
Value *Divisor = Builder.CreateSelect(Mask, CI->getArgOperand(1),
- ConstantInt::get(RetTy, 1));
+ ConstantInt::get(Ty, 1));
Result = Builder.CreateBinOp(Opcode, CI->getArgOperand(0), Divisor);
}
Result->takeName(CI);
@@ -768,7 +792,7 @@ bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
case Intrinsic::masked_sdiv:
case Intrinsic::masked_urem:
case Intrinsic::masked_srem:
- Changed |= lowerScalarMaskedDivRem(F);
+ Changed |= lowerMaskedDivRem(F, TM);
break;
case Intrinsic::load_relative:
Changed |= lowerLoadRelative(F);
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
index bb05ba0c1ebcef..d4f0d177697d38 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
@@ -42,6 +42,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/KnownBits.h"
#include <cassert>
#include <cstdint>
#include <iterator>
@@ -1841,10 +1842,42 @@ SDValue VectorLegalizer::ExpandMaskedBinOp(SDNode *N) {
// disabled lanes with 1 to avoid division by zero or overflow.
SDLoc dl(N);
EVT VT = N->getValueType(0);
+ // In signed i1, 1 is -1 and is not a safe fallback divisor.
+ if (VT.getScalarType() == MVT::i1 && (N->getOpcode() == ISD::MASKED_SDIV ||
+ N->getOpcode() == ISD::MASKED_SREM))
+ return DAG.getConstant(0, dl, VT);
+
+ unsigned Opcode = ISD::getUnmaskedBinOpOpcode(N->getOpcode());
+ unsigned DivOpcode = Opcode == ISD::SREM ? ISD::SDIV
+ : Opcode == ISD::UREM ? ISD::UDIV
+ : Opcode;
+ if (VT.isFixedLengthVector() && TLI.isIntDivRemTrapFree(VT.getScalarType()) &&
+ !TLI.isOperationLegalOrCustom(Opcode, VT) &&
+ !TLI.isOperationLegalOrCustom(DivOpcode, VT)) {
+ // Build scalar operations directly. Creating an ordinary vector node first
+ // could fold every lane to undef when one inactive divisor is zero.
+ EVT EltVT = VT.getScalarType();
+ EVT MaskEltVT = N->getOperand(2).getValueType().getVectorElementType();
+ SmallVector<SDValue> Results;
+ for (unsigned I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
+ SDValue MaskElt =
+ DAG.getExtractVectorElt(dl, MaskEltVT, N->getOperand(2), I);
+ SDValue Divisor = DAG.getExtractVectorElt(dl, EltVT, N->getOperand(1), I);
+ Results.push_back(isNullConstant(MaskElt) || Divisor.isUndef() ||
+ DAG.computeKnownBits(Divisor).isZero()
+ ? DAG.getConstant(0, dl, EltVT)
+ : DAG.getNode(Opcode, dl, EltVT,
+ DAG.getExtractVectorElt(
+ dl, EltVT, N->getOperand(0), I),
+ Divisor));
+ }
+ return DAG.getBuildVector(VT, dl, Results);
+ }
+
+ // The mask was frozen before type legalization.
SDValue SafeDivisor = DAG.getSelect(
dl, VT, N->getOperand(2), N->getOperand(1), DAG.getConstant(1, dl, VT));
- return DAG.getNode(ISD::getUnmaskedBinOpOpcode(N->getOpcode()), dl, VT,
- N->getOperand(0), SafeDivisor);
+ return DAG.getNode(Opcode, dl, VT, N->getOperand(0), SafeDivisor);
}
void VectorLegalizer::ExpandFP_TO_UINT(SDNode *Node,
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 392d11e9b13a3c..2ff33e00d22137 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -26,6 +26,7 @@
#include "llvm/CodeGen/ISDOpcodes.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/KnownBits.h"
#include "llvm/Support/TypeSize.h"
#include "llvm/Support/raw_ostream.h"
#include <numeric>
@@ -331,6 +332,12 @@ SDValue DAGTypeLegalizer::ScalarizeVecRes_MaskedBinOp(SDNode *N) {
SDLoc DL(N);
SDValue LHS = GetScalarizedVector(N->getOperand(0));
SDValue RHS = GetScalarizedVector(N->getOperand(1));
+ EVT VT = LHS.getValueType();
+ // The only defined signed i1 div/rem result is zero. In particular, 1 is
+ // -1 and would overflow for an inactive lane with dividend -1.
+ if (VT == MVT::i1 && (N->getOpcode() == ISD::MASKED_SDIV ||
+ N->getOpcode() == ISD::MASKED_SREM))
+ return DAG.getConstant(0, DL, MVT::i1);
SDValue Mask = N->getOperand(2);
EVT MaskVT = Mask.getValueType();
// The vselect result and input vectors need scalarizing, but it's
@@ -340,6 +347,13 @@ SDValue DAGTypeLegalizer::ScalarizeVecRes_MaskedBinOp(SDNode *N) {
Mask = GetScalarizedVector(Mask);
else
Mask = DAG.getExtractVectorElt(DL, MaskVT.getVectorElementType(), Mask, 0);
+ if (TLI.isIntDivRemTrapFree(VT)) {
+ if (isNullConstant(Mask) || RHS.isUndef() ||
+ DAG.computeKnownBits(RHS).isZero())
+ return DAG.getConstant(0, DL, VT);
+ return DAG.getNode(ISD::getUnmaskedBinOpOpcode(N->getOpcode()), DL, VT, LHS,
+ RHS);
+ }
// Vectors may have a different boolean contents to scalars, so truncate to i1
// and let type legalization promote appropriately.
Mask = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Mask);
@@ -1471,9 +1485,19 @@ SDValue DAGTypeLegalizer::ScalarizeVecOp_MaskedBinOp(SDNode *N, unsigned OpNo) {
SDValue LHS = DAG.getExtractVectorElt(DL, VT, N->getOperand(0), 0);
SDValue RHS = DAG.getExtractVectorElt(DL, VT, N->getOperand(1), 0);
SDValue Mask = GetScalarizedVector(N->getOperand(2));
+ if (TLI.isIntDivRemTrapFree(VT)) {
+ SDValue BinOp =
+ isNullConstant(Mask) || RHS.isUndef() ||
+ DAG.computeKnownBits(RHS).isZero()
+ ? DAG.getConstant(0, DL, VT)
+ : DAG.getNode(ISD::getUnmaskedBinOpOpcode(N->getOpcode()), DL, VT,
+ LHS, RHS);
+ return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, N->getValueType(0), BinOp);
+ }
// Vectors may have a different boolean contents to scalars, so truncate to i1
// and let type legalization promote appropriately.
Mask = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Mask);
+ // The mask was frozen before type legalization.
// Masked binary ops don't have UB on disabled lanes but produce poison, so
// use 1 as the divisor to avoid division by zero and overflow.
SDValue BinOp =
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 437342e8cba3b9..f8f9576bcd0055 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -8823,30 +8823,90 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
DAG.getConstant(0, sdl, MVT::i64)));
return;
case Intrinsic::masked_udiv:
- setValue(&I,
- DAG.getNode(ISD::MASKED_UDIV, sdl, EVT::getEVT(I.getType()),
- getValue(I.getOperand(0)), getValue(I.getOperand(1)),
- getValue(I.getOperand(2))));
- return;
case Intrinsic::masked_sdiv:
- setValue(&I,
- DAG.getNode(ISD::MASKED_SDIV, sdl, EVT::getEVT(I.getType()),
- getValue(I.getOperand(0)), getValue(I.getOperand(1)),
- getValue(I.getOperand(2))));
- return;
case Intrinsic::masked_urem:
- setValue(&I,
- DAG.getNode(ISD::MASKED_UREM, sdl, EVT::getEVT(I.getType()),
- getValue(I.getOperand(0)), getValue(I.getOperand(1)),
- getValue(I.getOperand(2))));
- return;
- case Intrinsic::masked_srem:
- setValue(&I,
- DAG.getNode(ISD::MASKED_SREM, sdl, EVT::getEVT(I.getType()),
- getValue(I.getOperand(0)), getValue(I.getOperand(1)),
- getValue(I.getOperand(2))));
+ case Intrinsic::masked_srem: {
+ EVT VT = EVT::getEVT(I.getType());
+ unsigned Opcode = Intrinsic == Intrinsic::masked_udiv ? ISD::MASKED_UDIV
+ : Intrinsic == Intrinsic::masked_sdiv ? ISD::MASKED_SDIV
+ : Intrinsic == Intrinsic::masked_urem ? ISD::MASKED_UREM
+ : ISD::MASKED_SREM;
+ SDValue LHS = getValue(I.getArgOperand(0));
+ SDValue RHS = getValue(I.getArgOperand(1));
+ bool TrapFree = TLI.isIntDivRemTrapFree(VT.getScalarType());
+ bool HasVectorLowering = false;
+ if (VT.isFixedLengthVector() && TrapFree) {
+ unsigned UnmaskedOpcode = ISD::getUnmaskedBinOpOpcode(Opcode);
+ unsigned DivOpcode = UnmaskedOpcode == ISD::SREM ? ISD::SDIV
+ : UnmaskedOpcode == ISD::UREM ? ISD::UDIV
+ : UnmaskedOpcode;
+ EVT LegalVT = VT;
+ while (LegalVT.isVector() &&
+ TLI.getTypeAction(*DAG.getContext(), LegalVT) !=
+ TargetLowering::TypeLegal)
+ LegalVT = TLI.getTypeToTransformTo(*DAG.getContext(), LegalVT);
+ // Remainder expansion can preserve vector arithmetic through a legal
+ // vector quotient even when the remainder operation itself is expanded.
+ HasVectorLowering =
+ LegalVT.isVector() &&
+ (TLI.isOperationLegalOrCustom(UnmaskedOpcode, LegalVT) ||
+ TLI.isOperationLegalOrCustom(DivOpcode, LegalVT));
+ }
+ if (!VT.isVector()) {
+ assert(TrapFree &&
+ "Non-trap-free scalar masked div/rem must be lowered before ISel");
+ // The trap-free hardware result refines poison in a disabled lane.
+ // PreISelIntrinsicLowering folds IR-known-zero divisors to 0 before IR
+ // simplification can lose the masked call; also check DAG known bits.
+ const auto *ConstMask = dyn_cast<Constant>(I.getArgOperand(2));
+ if ((ConstMask && ConstMask->isNullValue()) || RHS.isUndef() ||
+ DAG.computeKnownBits(RHS).isZero())
+ setValue(&I, DAG.getConstant(0, sdl, VT));
+ else
+ setValue(&I, DAG.getNode(ISD::getUnmaskedBinOpOpcode(Opcode), sdl, VT,
+ LHS, RHS));
+ } else if (VT.isFixedLengthVector() && TrapFree && !HasVectorLowering) {
+ // Scalarize before type legalization can pad odd vectors. An ordinary
+ // vector div/rem node could fold valid lanes away for an inactive zero
+ // divisor, so build only scalar operations. A lane whose mask is
+ // constant false or whose divisor is known zero gets a defined constant:
+ // the scalar DAG would otherwise fold zero divisors to undef, and codegen
+ // does not keep a frozen undef stable across uses. Zeros that neither
+ // known-bits query proves here still reach that generic freeze
+ // limitation.
+ EVT EltVT = VT.getScalarType();
+ const auto *ConstMask = dyn_cast<Constant>(I.getArgOperand(2));
+ SmallVector<SDValue> Results;
+ for (unsigned Lane = 0, NumElts = VT.getVectorNumElements();
+ Lane != NumElts; ++Lane) {
+ Constant *MaskElt =
+ ConstMask ? ConstMask->getAggregateElement(Lane) : nullptr;
+ SDValue Divisor = DAG.getExtractVectorElt(sdl, EltVT, RHS, Lane);
+ // DAG known bits also see through a bitcast from a scalar, which IR
+ // known bits do not.
+ if ((MaskElt && MaskElt->isNullValue()) || Divisor.isUndef() ||
+ computeKnownBits(I.getArgOperand(1),
+ APInt::getOneBitSet(NumElts, Lane),
+ SimplifyQuery(DAG.getDataLayout(), &I))
+ .isZero() ||
+ DAG.computeKnownBits(Divisor).isZero()) {
+ Results.push_back(DAG.getConstant(0, sdl, EltVT));
+ continue;
+ }
+ Results.push_back(DAG.getNode(
+ ISD::getUnmaskedBinOpOpcode(Opcode), sdl, EltVT,
+ DAG.getExtractVectorElt(sdl, EltVT, LHS, Lane), Divisor));
+ }
+ setValue(&I, DAG.getBuildVector(VT, sdl, Results));
+ } else {
+ // Freeze before type legalization so predicate combines can eliminate
+ // it, and every expansion can safely use the mask in a divisor select.
+ setValue(&I, DAG.getNode(Opcode, sdl, VT, LHS, RHS,
+ DAG.getFreeze(getValue(I.getArgOperand(2)))));
+ }
return;
}
+ }
}
void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
diff --git a/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp b/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp
index a78a81e2c09744..cae70f80ddc3bc 100644
--- a/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp
+++ b/llvm/lib/Transforms/Utils/BypassSlowDivision.cpp
@@ -29,6 +29,7 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
@@ -78,6 +79,10 @@ enum ValueRange {
class FastDivInsertionTask {
bool IsValidTask = false;
Instruction *SlowDivOrRem = nullptr;
+ unsigned Opcode = 0;
+ Value *Mask = nullptr;
+ Value *Dividend = nullptr;
+ Value *Divisor = nullptr;
IntegerType *BypassType = nullptr;
BasicBlock *MainBB = nullptr;
DomTreeUpdater *DTU = nullptr;
@@ -95,43 +100,79 @@ class FastDivInsertionTask {
std::optional<QuotRemPair> insertFastDivAndRem();
bool isSignedOp() {
- return SlowDivOrRem->getOpcode() == Instruction::SDiv ||
- SlowDivOrRem->getOpcode() == Instruction::SRem;
+ return Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
}
bool isDivisionOp() {
- return SlowDivOrRem->getOpcode() == Instruction::SDiv ||
- SlowDivOrRem->getOpcode() == Instruction::UDiv;
+ return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
}
Type *getSlowType() { return SlowDivOrRem->getType(); }
+ Value *createDivRem(IRBuilder<> &Builder, bool Signed, bool Div, Value *LHS,
+ Value *RHS) {
+ unsigned Op = Signed ? (Div ? Instruction::SDiv : Instruction::SRem)
+ : (Div ? Instruction::UDiv : Instruction::URem);
+ if (!Mask)
+ return Builder.CreateBinOp(static_cast<Instruction::BinaryOps>(Op), LHS,
+ RHS);
+ Value *OpMask = Mask;
+ if (Signed && !isa<ZExtInst>(LHS) && LHS != SlowDivOrRem->getOperand(0)) {
+ // A frozen poison dividend may become MIN. Keep that refinement from
+ // introducing signed overflow when the defined divisor is -1.
+ unsigned Bits = LHS->getType()->getIntegerBitWidth();
+ Value *LHSNotMin = Builder.CreateICmpNE(
+ LHS,
+ ConstantInt::get(LHS->getType(), APInt::getSignedMinValue(Bits)));
+ Value *RHSNotMinusOne =
+ Builder.CreateICmpNE(RHS, ConstantInt::getSigned(RHS->getType(), -1));
+ Value *NoOverflow = Builder.CreateOr(LHSNotMin, RHSNotMinusOne);
+ OpMask = Builder.CreateSelect(NoOverflow, Mask, Builder.getFalse());
+ }
+ Intrinsic::ID ID =
+ Signed ? (Div ? Intrinsic::masked_sdiv : Intrinsic::masked_srem)
+ : (Div ? Intrinsic::masked_udiv : Intrinsic::masked_urem);
+ return Builder.CreateIntrinsic(ID, {LHS->getType()}, {LHS, RHS, OpMask});
+ }
+
public:
FastDivInsertionTask(Instruction *I, const BypassWidthsTy &BypassWidths,
DomTreeUpdater *DTU, LoopInfo *LI,
- BranchProbabilityInfo *BPI);
+ BranchProbabilityInfo *BPI,
+ function_ref<bool(Type *)> IsIntDivRemTrapFree);
Value *getReplacement(DivCacheTy &Cache);
};
} // end anonymous namespace
-FastDivInsertionTask::FastDivInsertionTask(Instruction *I,
- const BypassWidthsTy &BypassWidths,
- DomTreeUpdater *DTU, LoopInfo *LI,
- BranchProbabilityInfo *BPI)
+FastDivInsertionTask::FastDivInsertionTask(
+ Instruction *I, const BypassWidthsTy &BypassWidths, DomTreeUpdater *DTU,
+ LoopInfo *LI, BranchProbabilityInfo *BPI,
+ function_ref<bool(Type *)> IsIntDivRemTrapFree)
: DTU(DTU), LI(LI), BPI(BPI) {
switch (I->getOpcode()) {
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::URem:
case Instruction::SRem:
- SlowDivOrRem = I;
+ Opcode = I->getOpcode();
+ break;
+ default: {
+ auto *II = dyn_cast<IntrinsicInst>(I);
+ if (!II || !I->getType()->isIntegerTy() || !IsIntDivRemTrapFree ||
+ II->hasOperandBundles() || II->isMustTailCall())
+ return;
+ Opcode = Intrinsic::getMaskedIntDivRemOpcode(II->getIntrinsicID());
+ if (!Opcode || !IsIntDivRemTrapFree(I->getType()))
+ return;
+ Mask = II->getArgOperand(2);
break;
- default:
- // I is not a div/rem operation.
- return;
}
+ }
+ SlowDivOrRem = I;
+ Dividend = I->getOperand(0);
+ Divisor = I->getOperand(1);
// Skip division on vector types. Only optimize integer instructions.
IntegerType *SlowType = dyn_cast<IntegerType>(SlowDivOrRem->getType());
@@ -146,6 +187,8 @@ FastDivInsertionTask::FastDivInsertionTask(Instruction *I,
// Get type for div/rem instruction with bypass bitwidth.
IntegerType *BT = IntegerType::get(I->getContext(), BI->second);
BypassType = BT;
+ if (Mask && !IsIntDivRemTrapFree(BT))
+ return;
// The original basic block.
MainBB = I->getParent();
@@ -164,9 +207,7 @@ Value *FastDivInsertionTask::getReplacement(DivCacheTy &Cache) {
return nullptr;
// Then, look for a value in Cache.
- Value *Dividend = SlowDivOrRem->getOperand(0);
- Value *Divisor = SlowDivOrRem->getOperand(1);
- DivRemMapKey Key(isSignedOp(), Dividend, Divisor);
+ DivRemMapKey Key(isSignedOp(), Dividend, Divisor, Mask);
auto CacheI = Cache.find(Key);
if (CacheI == Cache.end()) {
@@ -293,16 +334,10 @@ QuotRemWithBB FastDivInsertionTask::createSlowBB(BasicBlock *SuccessorBB) {
IRBuilder<> Builder(DivRemPair.BB->begin());
Builder.SetCurrentDebugLocation(SlowDivOrRem->getDebugLoc());
- Value *Dividend = SlowDivOrRem->getOperand(0);
- Value *Divisor = SlowDivOrRem->getOperand(1);
-
- if (isSignedOp()) {
- DivRemPair.Quotient = Builder.CreateSDiv(Dividend, Divisor);
- DivRemPair.Remainder = Builder.CreateSRem(Dividend, Divisor);
- } else {
- DivRemPair.Quotient = Builder.CreateUDiv(Dividend, Divisor);
- DivRemPair.Remainder = Builder.CreateURem(Dividend, Divisor);
- }
+ DivRemPair.Quotient =
+ createDivRem(Builder, isSignedOp(), true, Dividend, Divisor);
+ DivRemPair.Remainder =
+ createDivRem(Builder, isSignedOp(), false, Dividend, Divisor);
Builder.CreateBr(SuccessorBB);
return DivRemPair;
@@ -317,16 +352,16 @@ QuotRemWithBB FastDivInsertionTask::createFastBB(BasicBlock *SuccessorBB) {
IRBuilder<> Builder(DivRemPair.BB->begin());
Builder.SetCurrentDebugLocation(SlowDivOrRem->getDebugLoc());
- Value *Dividend = SlowDivOrRem->getOperand(0);
- Value *Divisor = SlowDivOrRem->getOperand(1);
Value *ShortDivisorV =
Builder.CreateCast(Instruction::Trunc, Divisor, BypassType);
Value *ShortDividendV =
Builder.CreateCast(Instruction::Trunc, Dividend, BypassType);
// udiv/urem because this optimization only handles positive numbers.
- Value *ShortQV = Builder.CreateUDiv(ShortDividendV, ShortDivisorV);
- Value *ShortRV = Builder.CreateURem(ShortDividendV, ShortDivisorV);
+ Value *ShortQV =
+ createDivRem(Builder, false, true, ShortDividendV, ShortDivisorV);
+ Value *ShortRV =
+ createDivRem(Builder, false, false, ShortDividendV, ShortDivisorV);
DivRemPair.Quotient =
Builder.CreateCast(Instruction::ZExt, ShortQV, getSlowType());
DivRemPair.Remainder =
@@ -379,9 +414,6 @@ Value *FastDivInsertionTask::insertOperandRuntimeCheck(Value *Op1, Value *Op2) {
/// Substitutes the div/rem instruction with code that checks the value of the
/// operands and uses a shorter-faster div/rem instruction when possible.
std::optional<QuotRemPair> FastDivInsertionTask::insertFastDivAndRem() {
- Value *Dividend = SlowDivOrRem->getOperand(0);
- Value *Divisor = SlowDivOrRem->getOperand(1);
-
VisitedSetTy SetL;
ValueRange DividendRange = getValueRange(Dividend, SetL);
if (DividendRange == VALRNG_LIKELY_LONG)
@@ -404,8 +436,10 @@ std::optional<QuotRemPair> FastDivInsertionTask::insertFastDivAndRem() {
IRBuilder<> Builder(SlowDivOrRem);
Value *TruncDividend = Builder.CreateTrunc(Dividend, BypassType);
Value *TruncDivisor = Builder.CreateTrunc(Divisor, BypassType);
- Value *TruncDiv = Builder.CreateUDiv(TruncDividend, TruncDivisor);
- Value *TruncRem = Builder.CreateURem(TruncDividend, TruncDivisor);
+ Value *TruncDiv =
+ createDivRem(Builder, false, true, TruncDividend, TruncDivisor);
+ Value *TruncRem =
+ createDivRem(Builder, false, false, TruncDividend, TruncDivisor);
Value *ExtDiv = Builder.CreateZExt(TruncDiv, getSlowType());
Value *ExtRem = Builder.CreateZExt(TruncRem, getSlowType());
return QuotRemPair(ExtDiv, ExtRem);
@@ -427,6 +461,29 @@ std::optional<QuotRemPair> FastDivInsertionTask::insertFastDivAndRem() {
isa<ConstantInt>(BCI->getOperand(0)))
return std::nullopt;
+ if (Mask) {
+ // The runtime check and both paths must use the same defined operands.
+ // Freezing only the condition could choose a fast path whose truncated
+ // divisor is zero, even though the original enabled operation was safe.
+ IRBuilder<> Builder(SlowDivOrRem);
+ if (!isGuaranteedNotToBeUndefOrPoison(Dividend, nullptr, SlowDivOrRem))
+ Dividend = DividendShort
+ ? Builder.CreateZExt(
+ Builder.CreateFreeze(
+ Builder.CreateTrunc(Dividend, BypassType),
+ "dividend.frozen"),
+ getSlowType())
+ : Builder.CreateFreeze(Dividend, "dividend.frozen");
+ if (!isGuaranteedNotToBeUndefOrPoison(Divisor, nullptr, SlowDivOrRem))
+ Divisor =
+ DivisorShort
+ ? Builder.CreateZExt(Builder.CreateFreeze(
+ Builder.CreateTrunc(Divisor, BypassType),
+ "divisor.frozen"),
+ getSlowType())
+ : Builder.CreateFreeze(Divisor, "divisor.frozen");
+ }
+
IRBuilder<> Builder(MainBB->end());
Builder.SetCurrentDebugLocation(SlowDivOrRem->getDebugLoc());
@@ -497,7 +554,8 @@ std::optional<QuotRemPair> FastDivInsertionTask::insertFastDivAndRem() {
bool llvm::bypassSlowDivision(BasicBlock *BB,
const BypassWidthsTy &BypassWidths,
DomTreeUpdater *DTU, LoopInfo *LI,
- BranchProbabilityInfo *BPI) {
+ BranchProbabilityInfo *BPI,
+ function_ref<bool(Type *)> IsIntDivRemTrapFree) {
DivCacheTy PerBBDivCache;
bool MadeChange = false;
@@ -512,7 +570,8 @@ bool llvm::bypassSlowDivision(BasicBlock *BB,
if (I->use_empty())
continue;
- FastDivInsertionTask Task(I, BypassWidths, DTU, LI, BPI);
+ FastDivInsertionTask Task(I, BypassWidths, DTU, LI, BPI,
+ IsIntDivRemTrapFree);
if (Value *Replacement = Task.getReplacement(PerBBDivCache)) {
I->replaceAllUsesWith(Replacement);
I->eraseFromParent();
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll b/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll
new file mode 100644
index 00000000000000..3c9f6b2ebfd230
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll
@@ -0,0 +1,115 @@
+; RUN: llc -O0 -mtriple=aarch64 -global-isel -global-isel-abort=1 -verify-machineinstrs < %s -o /dev/null
+; RUN: llc -O0 -mtriple=aarch64 -global-isel -global-isel-abort=1 -stop-after=ir-translator < %s | FileCheck %s
+
+; CHECK-LABEL: name: sdiv_v3i8
+; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-COUNT-3: G_SDIV
+; CHECK-COUNT-3: G_FREEZE
+define void @sdiv_v3i8(ptr %p, <3 x i8> %a, <3 x i8> %b, <3 x i1> %m) {
+ %r = call <3 x i8> @llvm.masked.sdiv.v3i8(<3 x i8> %a, <3 x i8> %b, <3 x i1> %m)
+ %f = freeze <3 x i8> %r
+ store <3 x i8> %f, ptr %p
+ ret void
+}
+
+; CHECK-LABEL: name: urem_v3i8
+; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-COUNT-3: G_UREM
+; CHECK-COUNT-3: G_FREEZE
+define void @urem_v3i8(ptr %p, <3 x i8> %a, <3 x i8> %b, <3 x i1> %m) {
+ %r = call <3 x i8> @llvm.masked.urem.v3i8(<3 x i8> %a, <3 x i8> %b, <3 x i1> %m)
+ %f = freeze <3 x i8> %r
+ store <3 x i8> %f, ptr %p
+ ret void
+}
+
+; CHECK-LABEL: name: srem_v2i64
+; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-COUNT-2: G_SREM
+; CHECK-COUNT-2: G_FREEZE
+define void @srem_v2i64(ptr %p, <2 x i64> %a, <2 x i64> %b, <2 x i1> %m) {
+ %r = call <2 x i64> @llvm.masked.srem.v2i64(<2 x i64> %a, <2 x i64> %b, <2 x i1> %m)
+ %f = freeze <2 x i64> %r
+ store <2 x i64> %f, ptr %p
+ ret void
+}
+
+; CHECK-LABEL: name: sdiv_v2i1
+; CHECK-NOT: G_SDIV
+; CHECK: [[ZERO:%[0-9]+]]:_(i1) = G_CONSTANT i1 false
+; CHECK-NEXT: G_BUILD_VECTOR [[ZERO]](i1), [[ZERO]](i1)
+define <2 x i1> @sdiv_v2i1(<2 x i1> %a, <2 x i1> %b, <2 x i1> %m) {
+ %r = call <2 x i1> @llvm.masked.sdiv.v2i1(<2 x i1> %a, <2 x i1> %b, <2 x i1> %m)
+ ret <2 x i1> %r
+}
+
+; CHECK-LABEL: name: freeze_add_v4i32
+; CHECK: [[ADD:%[0-9]+]]:_(<4 x i32>) = G_ADD
+; CHECK-NEXT: {{%[0-9]+}}:_(<4 x i32>) = G_FREEZE [[ADD]]
+; CHECK-NOT: G_FREEZE
+define void @freeze_add_v4i32(ptr %p, <4 x i32> %a, <4 x i32> %b) {
+ %s = add <4 x i32> %a, %b
+ %f = freeze <4 x i32> %s
+ store <4 x i32> %f, ptr %p
+ ret void
+}
+
+; CHECK-LABEL: name: freeze_abs_v4i32
+; CHECK: [[ABS:%[0-9]+]]:_(<4 x i32>) = G_ABS
+; CHECK-NEXT: {{%[0-9]+}}:_(<4 x i32>) = G_FREEZE [[ABS]]
+; CHECK-NOT: G_FREEZE
+define void @freeze_abs_v4i32(ptr %p, <4 x i32> %a) {
+ %s = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %a, i1 false)
+ %f = freeze <4 x i32> %s
+ store <4 x i32> %f, ptr %p
+ ret void
+}
+
+; CHECK-LABEL: name: freeze_scalar_sdiv
+; CHECK: [[DIV:%[0-9]+]]:_(i32) = G_SDIV
+; CHECK-NEXT: {{%[0-9]+}}:_(i32) = G_FREEZE [[DIV]]
+define i32 @freeze_scalar_sdiv(i32 %a, i32 %b, i1 %m) {
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+; CHECK-LABEL: name: sdiv_v1i32
+; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_BUILD_VECTOR
+; CHECK: G_SDIV
+define <1 x i32> @sdiv_v1i32(<1 x i32> %a, <1 x i32> %b, <1 x i1> %m) {
+ %r = call <1 x i32> @llvm.masked.sdiv.v1i32(<1 x i32> %a, <1 x i32> %b, <1 x i1> %m)
+ ret <1 x i32> %r
+}
+
+; CHECK-LABEL: name: udiv_v1i8
+; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_BUILD_VECTOR
+; CHECK: G_UDIV
+define <1 x i8> @udiv_v1i8(<1 x i8> %a, <1 x i8> %b, <1 x i1> %m) {
+ %r = call <1 x i8> @llvm.masked.udiv.v1i8(<1 x i8> %a, <1 x i8> %b, <1 x i1> %m)
+ ret <1 x i8> %r
+}
+
+; CHECK-LABEL: name: sdiv_v1i1
+; CHECK-NOT: G_SDIV
+; CHECK-NOT: G_BUILD_VECTOR
+; CHECK: G_CONSTANT i1 false
+define <1 x i1> @sdiv_v1i1(<1 x i1> %a, <1 x i1> %b, <1 x i1> %m) {
+ %r = call <1 x i1> @llvm.masked.sdiv.v1i1(<1 x i1> %a, <1 x i1> %b, <1 x i1> %m)
+ ret <1 x i1> %r
+}
+
+; CHECK-LABEL: name: udiv_v2i128
+; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK: [[FM0:%[0-9]+]]:_(i1) = G_FREEZE
+; CHECK: [[DIV0:%[0-9]+]]:_(i128) = G_SELECT [[FM0]](i1)
+; CHECK-NEXT: G_UDIV {{%[0-9]+}}, [[DIV0]]
+; CHECK: [[FM1:%[0-9]+]]:_(i1) = G_FREEZE
+; CHECK: [[DIV1:%[0-9]+]]:_(i128) = G_SELECT [[FM1]](i1)
+; CHECK-NEXT: G_UDIV {{%[0-9]+}}, [[DIV1]]
+define <2 x i128> @udiv_v2i128(<2 x i128> %a, <2 x i128> %b, <2 x i1> %m) {
+ %r = call <2 x i128> @llvm.masked.udiv.v2i128(<2 x i128> %a, <2 x i128> %b, <2 x i1> %m)
+ ret <2 x i128> %r
+}
diff --git a/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll b/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll
new file mode 100644
index 00000000000000..8498af8aa81ff9
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll
@@ -0,0 +1,133 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -O2 -mtriple=aarch64 -mattr=+sve -aarch64-sve-vector-bits-min=256 < %s | FileCheck %s
+
+define <1 x i64> @v1(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
+; CHECK-LABEL: v1:
+; CHECK: // %bb.0:
+; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
+; CHECK-NEXT: fmov x8, d1
+; CHECK-NEXT: // kill: def $d0 killed $d0 def $q0
+; CHECK-NEXT: fmov x9, d0
+; CHECK-NEXT: tst w0, #0x1
+; CHECK-NEXT: csinc x8, x8, xzr, ne
+; CHECK-NEXT: sdiv x8, x9, x8
+; CHECK-NEXT: fmov d0, x8
+; CHECK-NEXT: ret
+ %r = call <1 x i64> @llvm.masked.sdiv.v1i64(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m)
+ ret <1 x i64> %r
+}
+
+define void @nofrz(ptr %a, ptr %b) vscale_range(2,0) #0 {
+; CHECK-LABEL: nofrz:
+; CHECK: // %bb.0:
+; CHECK-NEXT: ptrue p0.b, vl32
+; CHECK-NEXT: mov z1.b, #1 // =0x1
+; CHECK-NEXT: ld1b { z0.b }, p0/z, [x0]
+; CHECK-NEXT: ld1b { z2.b }, p0/z, [x1]
+; CHECK-NEXT: cmpne p1.b, p0/z, z0.b, #0
+; CHECK-NEXT: mov z1.b, p1/m, z2.b
+; CHECK-NEXT: sunpklo z2.h, z0.b
+; CHECK-NEXT: ext z0.b, z0.b, z0.b, #16
+; CHECK-NEXT: ptrue p1.s, vl8
+; CHECK-NEXT: sunpklo z3.h, z1.b
+; CHECK-NEXT: ext z1.b, z1.b, z1.b, #16
+; CHECK-NEXT: sunpklo z0.h, z0.b
+; CHECK-NEXT: sunpklo z4.s, z2.h
+; CHECK-NEXT: ext z2.b, z2.b, z2.b, #16
+; CHECK-NEXT: sunpklo z1.h, z1.b
+; CHECK-NEXT: sunpklo z5.s, z3.h
+; CHECK-NEXT: ext z3.b, z3.b, z3.b, #16
+; CHECK-NEXT: sunpklo z2.s, z2.h
+; CHECK-NEXT: sunpklo z3.s, z3.h
+; CHECK-NEXT: sdiv z4.s, p1/m, z4.s, z5.s
+; CHECK-NEXT: sunpklo z5.s, z1.h
+; CHECK-NEXT: ext z1.b, z1.b, z1.b, #16
+; CHECK-NEXT: sunpklo z1.s, z1.h
+; CHECK-NEXT: sdiv z2.s, p1/m, z2.s, z3.s
+; CHECK-NEXT: sunpklo z3.s, z0.h
+; CHECK-NEXT: ext z0.b, z0.b, z0.b, #16
+; CHECK-NEXT: sunpklo z0.s, z0.h
+; CHECK-NEXT: sdiv z3.s, p1/m, z3.s, z5.s
+; CHECK-NEXT: uzp1 z2.h, z2.h, z2.h
+; CHECK-NEXT: sdiv z0.s, p1/m, z0.s, z1.s
+; CHECK-NEXT: uzp1 z1.h, z4.h, z4.h
+; CHECK-NEXT: ptrue p1.h, vl8
+; CHECK-NEXT: uzp1 z3.h, z3.h, z3.h
+; CHECK-NEXT: splice z1.h, p1, z1.h, z2.h
+; CHECK-NEXT: uzp1 z0.h, z0.h, z0.h
+; CHECK-NEXT: splice z3.h, p1, z3.h, z0.h
+; CHECK-NEXT: uzp1 z0.b, z1.b, z1.b
+; CHECK-NEXT: ptrue p1.b, vl16
+; CHECK-NEXT: uzp1 z1.b, z3.b, z3.b
+; CHECK-NEXT: splice z0.b, p1, z0.b, z1.b
+; CHECK-NEXT: st1b { z0.b }, p0, [x0]
+; CHECK-NEXT: ret
+ %op1 = load <32 x i8>, ptr %a
+ %op2 = load <32 x i8>, ptr %b
+ %mask = icmp ne <32 x i8> %op1, zeroinitializer
+ %res = call <32 x i8> @llvm.masked.sdiv(<32 x i8> %op1, <32 x i8> %op2, <32 x i1> %mask)
+ store <32 x i8> %res, ptr %a
+ ret void
+}
+define void @irfrz(ptr %a, ptr %b) vscale_range(2,0) #0 {
+; CHECK-LABEL: irfrz:
+; CHECK: // %bb.0:
+; CHECK-NEXT: ptrue p0.b, vl32
+; CHECK-NEXT: mov z1.b, #1 // =0x1
+; CHECK-NEXT: ld1b { z0.b }, p0/z, [x0]
+; CHECK-NEXT: ld1b { z2.b }, p0/z, [x1]
+; CHECK-NEXT: cmpne p1.b, p0/z, z0.b, #0
+; CHECK-NEXT: mov z1.b, p1/m, z2.b
+; CHECK-NEXT: sunpklo z2.h, z0.b
+; CHECK-NEXT: ext z0.b, z0.b, z0.b, #16
+; CHECK-NEXT: ptrue p1.s, vl8
+; CHECK-NEXT: sunpklo z3.h, z1.b
+; CHECK-NEXT: ext z1.b, z1.b, z1.b, #16
+; CHECK-NEXT: sunpklo z0.h, z0.b
+; CHECK-NEXT: sunpklo z4.s, z2.h
+; CHECK-NEXT: ext z2.b, z2.b, z2.b, #16
+; CHECK-NEXT: sunpklo z1.h, z1.b
+; CHECK-NEXT: sunpklo z5.s, z3.h
+; CHECK-NEXT: ext z3.b, z3.b, z3.b, #16
+; CHECK-NEXT: sunpklo z2.s, z2.h
+; CHECK-NEXT: sunpklo z3.s, z3.h
+; CHECK-NEXT: sdiv z4.s, p1/m, z4.s, z5.s
+; CHECK-NEXT: sunpklo z5.s, z1.h
+; CHECK-NEXT: ext z1.b, z1.b, z1.b, #16
+; CHECK-NEXT: sunpklo z1.s, z1.h
+; CHECK-NEXT: sdiv z2.s, p1/m, z2.s, z3.s
+; CHECK-NEXT: sunpklo z3.s, z0.h
+; CHECK-NEXT: ext z0.b, z0.b, z0.b, #16
+; CHECK-NEXT: sunpklo z0.s, z0.h
+; CHECK-NEXT: sdiv z3.s, p1/m, z3.s, z5.s
+; CHECK-NEXT: uzp1 z2.h, z2.h, z2.h
+; CHECK-NEXT: sdiv z0.s, p1/m, z0.s, z1.s
+; CHECK-NEXT: uzp1 z1.h, z4.h, z4.h
+; CHECK-NEXT: ptrue p1.h, vl8
+; CHECK-NEXT: uzp1 z3.h, z3.h, z3.h
+; CHECK-NEXT: splice z1.h, p1, z1.h, z2.h
+; CHECK-NEXT: uzp1 z0.h, z0.h, z0.h
+; CHECK-NEXT: splice z3.h, p1, z3.h, z0.h
+; CHECK-NEXT: uzp1 z0.b, z1.b, z1.b
+; CHECK-NEXT: ptrue p1.b, vl16
+; CHECK-NEXT: uzp1 z1.b, z3.b, z3.b
+; CHECK-NEXT: splice z0.b, p1, z0.b, z1.b
+; CHECK-NEXT: st1b { z0.b }, p0, [x0]
+; CHECK-NEXT: ret
+ %op1 = load <32 x i8>, ptr %a
+ %op2 = load <32 x i8>, ptr %b
+ %mask = icmp ne <32 x i8> %op1, zeroinitializer
+ %fm = freeze <32 x i1> %mask
+ %res = call <32 x i8> @llvm.masked.sdiv(<32 x i8> %op1, <32 x i8> %op2, <32 x i1> %fm)
+ store <32 x i8> %res, ptr %a
+ ret void
+}
+
+define <1 x i1> @signed_i1(<1 x i1> %x, <1 x i1> %y, <1 x i1> %m) {
+; CHECK-LABEL: signed_i1:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ %r = call <1 x i1> @llvm.masked.sdiv.v1i1(<1 x i1> %x, <1 x i1> %y, <1 x i1> %m)
+ ret <1 x i1> %r
+}
diff --git a/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll b/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll
index 0c0da0bc241135..62fb0529ffe61a 100644
--- a/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll
+++ b/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll
@@ -391,3 +391,33 @@ define <3 x i10> @sdiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
%res = call <3 x i10> @llvm.masked.sdiv(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m)
ret <3 x i10> %res
}
+
+define <2 x i32> @sdiv_poison_mask(<2 x i32> %x) {
+; NEON-LABEL: sdiv_poison_mask:
+; NEON: // %bb.0:
+; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
+; NEON-NEXT: fmov w9, s0
+; NEON-NEXT: mov w8, #21846 // =0x5556
+; NEON-NEXT: mov w10, v0.s[1]
+; NEON-NEXT: movk w8, #21845, lsl #16
+; NEON-NEXT: smull x8, w9, w8
+; NEON-NEXT: add w10, w10, w10, lsr #31
+; NEON-NEXT: lsr x9, x8, #32
+; NEON-NEXT: add x8, x9, x8, lsr #63
+; NEON-NEXT: asr w9, w10, #1
+; NEON-NEXT: fmov s0, w8
+; NEON-NEXT: mov v0.s[1], w9
+; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
+; NEON-NEXT: ret
+;
+; SVE-LABEL: sdiv_poison_mask:
+; SVE: // %bb.0:
+; SVE-NEXT: index z1.s, #3, #-1
+; SVE-NEXT: ptrue p0.s, vl2
+; SVE-NEXT: // kill: def $d0 killed $d0 def $z0
+; SVE-NEXT: sdiv z0.s, p0/m, z0.s, z1.s
+; SVE-NEXT: // kill: def $d0 killed $d0 killed $z0
+; SVE-NEXT: ret
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 2>, <2 x i1> <i1 poison, i1 true>)
+ ret <2 x i32> %r
+}
diff --git a/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll b/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll
index 3e53acabf81ee7..f732e0da84f2f1 100644
--- a/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll
+++ b/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll
@@ -390,3 +390,31 @@ define <3 x i10> @udiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
%res = call <3 x i10> @llvm.masked.udiv(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m)
ret <3 x i10> %res
}
+
+define <2 x i32> @udiv_poison_mask(<2 x i32> %x) {
+; NEON-LABEL: udiv_poison_mask:
+; NEON: // %bb.0:
+; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
+; NEON-NEXT: fmov w9, s0
+; NEON-NEXT: mov w8, #43691 // =0xaaab
+; NEON-NEXT: mov w10, v0.s[1]
+; NEON-NEXT: movk w8, #43690, lsl #16
+; NEON-NEXT: umull x8, w9, w8
+; NEON-NEXT: lsr w9, w10, #1
+; NEON-NEXT: lsr x8, x8, #33
+; NEON-NEXT: fmov s0, w8
+; NEON-NEXT: mov v0.s[1], w9
+; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
+; NEON-NEXT: ret
+;
+; SVE-LABEL: udiv_poison_mask:
+; SVE: // %bb.0:
+; SVE-NEXT: index z1.s, #3, #-1
+; SVE-NEXT: ptrue p0.s, vl2
+; SVE-NEXT: // kill: def $d0 killed $d0 def $z0
+; SVE-NEXT: udiv z0.s, p0/m, z0.s, z1.s
+; SVE-NEXT: // kill: def $d0 killed $d0 killed $z0
+; SVE-NEXT: ret
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 2>, <2 x i1> <i1 poison, i1 true>)
+ ret <2 x i32> %r
+}
diff --git a/llvm/test/CodeGen/PowerPC/masked-sdiv.ll b/llvm/test/CodeGen/PowerPC/masked-sdiv.ll
index f2bebdcd2d4fe8..9c75e02fc435b7 100644
--- a/llvm/test/CodeGen/PowerPC/masked-sdiv.ll
+++ b/llvm/test/CodeGen/PowerPC/masked-sdiv.ll
@@ -331,23 +331,23 @@ define <2 x i128> @sdiv_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @sdiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-LABEL: sdiv_v3i10:
; CHECK: # %bb.0:
-; CHECK-NEXT: lbz 12, 96(1)
-; CHECK-NEXT: li 11, 0
-; CHECK-NEXT: mtfprwz 2, 7
; CHECK-NEXT: rldimi 9, 10, 32, 0
+; CHECK-NEXT: addis 10, 2, .LCPI7_0 at toc@ha
+; CHECK-NEXT: mtfprwz 2, 7
; CHECK-NEXT: mtvsrwz 33, 8
; CHECK-NEXT: vspltisw 4, 11
; CHECK-NEXT: xxleqv 38, 38, 38
-; CHECK-NEXT: vadduwm 4, 4, 4
-; CHECK-NEXT: rldimi 12, 11, 32, 0
-; CHECK-NEXT: addis 11, 2, .LCPI7_0 at toc@ha
-; CHECK-NEXT: addi 11, 11, .LCPI7_0 at toc@l
-; CHECK-NEXT: mtfprd 1, 12
-; CHECK-NEXT: lxvd2x 0, 0, 11
-; CHECK-NEXT: xxswapd 34, 0
-; CHECK-NEXT: mtfprwz 0, 6
-; CHECK-NEXT: xxmrghw 35, 2, 0
+; CHECK-NEXT: addi 10, 10, .LCPI7_0 at toc@l
; CHECK-NEXT: mtfprd 0, 9
+; CHECK-NEXT: lbz 9, 96(1)
+; CHECK-NEXT: vadduwm 4, 4, 4
+; CHECK-NEXT: lxvd2x 1, 0, 10
+; CHECK-NEXT: li 10, 0
+; CHECK-NEXT: rldimi 9, 10, 32, 0
+; CHECK-NEXT: xxswapd 34, 1
+; CHECK-NEXT: mtfprwz 1, 6
+; CHECK-NEXT: xxmrghw 35, 2, 1
+; CHECK-NEXT: mtfprd 1, 9
; CHECK-NEXT: vperm 3, 1, 3, 2
; CHECK-NEXT: mtvsrwz 33, 5
; CHECK-NEXT: vslw 3, 3, 4
diff --git a/llvm/test/CodeGen/PowerPC/masked-srem.ll b/llvm/test/CodeGen/PowerPC/masked-srem.ll
index da94c22cc0ace6..261bee52550c4a 100644
--- a/llvm/test/CodeGen/PowerPC/masked-srem.ll
+++ b/llvm/test/CodeGen/PowerPC/masked-srem.ll
@@ -389,23 +389,23 @@ define <2 x i128> @srem_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @srem_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-LABEL: srem_v3i10:
; CHECK: # %bb.0:
-; CHECK-NEXT: lbz 12, 96(1)
-; CHECK-NEXT: li 11, 0
-; CHECK-NEXT: mtfprwz 2, 7
; CHECK-NEXT: rldimi 9, 10, 32, 0
+; CHECK-NEXT: addis 10, 2, .LCPI7_0 at toc@ha
+; CHECK-NEXT: mtfprwz 2, 7
; CHECK-NEXT: mtvsrwz 33, 8
; CHECK-NEXT: vspltisw 4, 11
; CHECK-NEXT: xxleqv 38, 38, 38
-; CHECK-NEXT: vadduwm 4, 4, 4
-; CHECK-NEXT: rldimi 12, 11, 32, 0
-; CHECK-NEXT: addis 11, 2, .LCPI7_0 at toc@ha
-; CHECK-NEXT: addi 11, 11, .LCPI7_0 at toc@l
-; CHECK-NEXT: mtfprd 1, 12
-; CHECK-NEXT: lxvd2x 0, 0, 11
-; CHECK-NEXT: xxswapd 34, 0
-; CHECK-NEXT: mtfprwz 0, 6
-; CHECK-NEXT: xxmrghw 35, 2, 0
+; CHECK-NEXT: addi 10, 10, .LCPI7_0 at toc@l
; CHECK-NEXT: mtfprd 0, 9
+; CHECK-NEXT: lbz 9, 96(1)
+; CHECK-NEXT: vadduwm 4, 4, 4
+; CHECK-NEXT: lxvd2x 1, 0, 10
+; CHECK-NEXT: li 10, 0
+; CHECK-NEXT: rldimi 9, 10, 32, 0
+; CHECK-NEXT: xxswapd 34, 1
+; CHECK-NEXT: mtfprwz 1, 6
+; CHECK-NEXT: xxmrghw 35, 2, 1
+; CHECK-NEXT: mtfprd 1, 9
; CHECK-NEXT: vperm 3, 1, 3, 2
; CHECK-NEXT: mtvsrwz 33, 5
; CHECK-NEXT: vslw 3, 3, 4
diff --git a/llvm/test/CodeGen/PowerPC/masked-udiv.ll b/llvm/test/CodeGen/PowerPC/masked-udiv.ll
index 74716b4eeb5084..33af79ad74e377 100644
--- a/llvm/test/CodeGen/PowerPC/masked-udiv.ll
+++ b/llvm/test/CodeGen/PowerPC/masked-udiv.ll
@@ -331,25 +331,25 @@ define <2 x i128> @udiv_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @udiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-LABEL: udiv_v3i10:
; CHECK: # %bb.0:
-; CHECK-NEXT: lbz 11, 96(1)
-; CHECK-NEXT: li 12, 0
; CHECK-NEXT: rldimi 9, 10, 32, 0
-; CHECK-NEXT: addis 10, 2, .LCPI7_0 at toc@ha
+; CHECK-NEXT: lbz 11, 96(1)
+; CHECK-NEXT: li 10, 0
; CHECK-NEXT: mtvsrwz 33, 8
; CHECK-NEXT: vspltisw 4, -10
; CHECK-NEXT: xxleqv 38, 38, 38
-; CHECK-NEXT: addi 10, 10, .LCPI7_0 at toc@l
; CHECK-NEXT: mtfprd 0, 9
+; CHECK-NEXT: addis 9, 2, .LCPI7_0 at toc@ha
; CHECK-NEXT: vsrw 4, 4, 4
-; CHECK-NEXT: rldimi 11, 12, 32, 0
-; CHECK-NEXT: lxvd2x 1, 0, 10
+; CHECK-NEXT: rldimi 11, 10, 32, 0
+; CHECK-NEXT: addi 9, 9, .LCPI7_0 at toc@l
; CHECK-NEXT: mtfprd 2, 11
-; CHECK-NEXT: xxswapd 34, 1
-; CHECK-NEXT: mtfprwz 1, 7
+; CHECK-NEXT: lxvd2x 1, 0, 9
; CHECK-NEXT: xxmrghd 35, 2, 0
; CHECK-NEXT: mtfprwz 0, 6
; CHECK-NEXT: vslw 3, 3, 6
; CHECK-NEXT: vsraw 3, 3, 6
+; CHECK-NEXT: xxswapd 34, 1
+; CHECK-NEXT: mtfprwz 1, 7
; CHECK-NEXT: xxmrghw 37, 1, 0
; CHECK-NEXT: mtfprwz 0, 3
; CHECK-NEXT: mtfprwz 1, 4
diff --git a/llvm/test/CodeGen/PowerPC/masked-urem.ll b/llvm/test/CodeGen/PowerPC/masked-urem.ll
index e7631494389a9f..a5c60c72d2db7f 100644
--- a/llvm/test/CodeGen/PowerPC/masked-urem.ll
+++ b/llvm/test/CodeGen/PowerPC/masked-urem.ll
@@ -389,25 +389,25 @@ define <2 x i128> @urem_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @urem_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-LABEL: urem_v3i10:
; CHECK: # %bb.0:
-; CHECK-NEXT: lbz 11, 96(1)
-; CHECK-NEXT: li 12, 0
; CHECK-NEXT: rldimi 9, 10, 32, 0
-; CHECK-NEXT: addis 10, 2, .LCPI7_0 at toc@ha
+; CHECK-NEXT: lbz 11, 96(1)
+; CHECK-NEXT: li 10, 0
; CHECK-NEXT: mtvsrwz 33, 8
; CHECK-NEXT: vspltisw 4, -10
; CHECK-NEXT: xxleqv 38, 38, 38
-; CHECK-NEXT: addi 10, 10, .LCPI7_0 at toc@l
; CHECK-NEXT: mtfprd 0, 9
+; CHECK-NEXT: addis 9, 2, .LCPI7_0 at toc@ha
; CHECK-NEXT: vsrw 4, 4, 4
-; CHECK-NEXT: rldimi 11, 12, 32, 0
-; CHECK-NEXT: lxvd2x 1, 0, 10
+; CHECK-NEXT: rldimi 11, 10, 32, 0
+; CHECK-NEXT: addi 9, 9, .LCPI7_0 at toc@l
; CHECK-NEXT: mtfprd 2, 11
-; CHECK-NEXT: xxswapd 34, 1
-; CHECK-NEXT: mtfprwz 1, 7
+; CHECK-NEXT: lxvd2x 1, 0, 9
; CHECK-NEXT: xxmrghd 35, 2, 0
; CHECK-NEXT: mtfprwz 0, 6
; CHECK-NEXT: vslw 3, 3, 6
; CHECK-NEXT: vsraw 3, 3, 6
+; CHECK-NEXT: xxswapd 34, 1
+; CHECK-NEXT: mtfprwz 1, 7
; CHECK-NEXT: xxmrghw 37, 1, 0
; CHECK-NEXT: mtfprwz 0, 3
; CHECK-NEXT: mtfprwz 1, 4
diff --git a/llvm/test/CodeGen/SPIRV/masked-divrem-vector-unsupported.ll b/llvm/test/CodeGen/SPIRV/masked-divrem-vector-unsupported.ll
new file mode 100644
index 00000000000000..29e816fa2a0a71
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/masked-divrem-vector-unsupported.ll
@@ -0,0 +1,9 @@
+; RUN: not llc -O0 -mtriple=spirv64-unknown-unknown < %s -o - 2>&1 | FileCheck %s
+; RUN: not llc -O2 -mtriple=spirv64-unknown-unknown < %s -o - 2>&1 | FileCheck %s
+; CHECK: error: {{.*}}intrinsic selection not implemented
+; CHECK-NOT: Assertion
+
+define spir_func <2 x i32> @sdiv_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m)
+ ret <2 x i32> %r
+}
diff --git a/llvm/test/CodeGen/X86/masked-sdiv.ll b/llvm/test/CodeGen/X86/masked-sdiv.ll
index 72104a7a87388a..59f47caa0593b5 100644
--- a/llvm/test/CodeGen/X86/masked-sdiv.ll
+++ b/llvm/test/CodeGen/X86/masked-sdiv.ll
@@ -775,3 +775,537 @@ define <6 x i32> @sdiv_v6i32(<6 x i32> %x, <6 x i32> %y, <6 x i1> %m) {
%res = call <6 x i32> @llvm.masked.sdiv(<6 x i32> %x, <6 x i32> %y, <6 x i1> %m)
ret <6 x i32> %res
}
+
+define <1 x i32> @sdiv_poison_mask_scalarize(<1 x i32> %x) {
+; SSE-LABEL: sdiv_poison_mask_scalarize:
+; SSE: # %bb.0:
+; SSE-NEXT: movl %edi, %eax
+; SSE-NEXT: xorl %ecx, %ecx
+; SSE-NEXT: testb $1, %al
+; SSE-NEXT: setne %cl
+; SSE-NEXT: leal 1(,%rcx,2), %ecx
+; SSE-NEXT: cltd
+; SSE-NEXT: idivl %ecx
+; SSE-NEXT: retq
+;
+; AVX-LABEL: sdiv_poison_mask_scalarize:
+; AVX: # %bb.0:
+; AVX-NEXT: movl %edi, %eax
+; AVX-NEXT: xorl %ecx, %ecx
+; AVX-NEXT: testb $1, %al
+; AVX-NEXT: setne %cl
+; AVX-NEXT: leal 1(,%rcx,2), %ecx
+; AVX-NEXT: cltd
+; AVX-NEXT: idivl %ecx
+; AVX-NEXT: retq
+ %r = call <1 x i32> @llvm.masked.sdiv.v1i32(<1 x i32> %x, <1 x i32> <i32 3>, <1 x i1> <i1 poison>)
+ ret <1 x i32> %r
+}
+
+define <2 x i32> @sdiv_poison_mask(<2 x i32> %x) {
+; SSE2-LABEL: sdiv_poison_mask:
+; SSE2: # %bb.0:
+; SSE2-NEXT: pshufd {{.*#+}} xmm1 = xmm0[1,1,1,1]
+; SSE2-NEXT: movd %xmm1, %eax
+; SSE2-NEXT: movl %eax, %ecx
+; SSE2-NEXT: shrl $31, %ecx
+; SSE2-NEXT: addl %eax, %ecx
+; SSE2-NEXT: movd %ecx, %xmm1
+; SSE2-NEXT: psrad $1, %xmm1
+; SSE2-NEXT: movd %xmm0, %eax
+; SSE2-NEXT: cltq
+; SSE2-NEXT: imulq $1431655766, %rax, %rax # imm = 0x55555556
+; SSE2-NEXT: movq %rax, %rcx
+; SSE2-NEXT: shrq $63, %rcx
+; SSE2-NEXT: shrq $32, %rax
+; SSE2-NEXT: addl %ecx, %eax
+; SSE2-NEXT: movd %eax, %xmm2
+; SSE2-NEXT: punpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm2[0]
+; SSE2-NEXT: shufps {{.*#+}} xmm1 = xmm1[2,0],xmm0[2,3]
+; SSE2-NEXT: movaps %xmm1, %xmm0
+; SSE2-NEXT: retq
+;
+; SSE42-LABEL: sdiv_poison_mask:
+; SSE42: # %bb.0:
+; SSE42-NEXT: movd %xmm0, %eax
+; SSE42-NEXT: cltq
+; SSE42-NEXT: imulq $1431655766, %rax, %rax # imm = 0x55555556
+; SSE42-NEXT: movq %rax, %rcx
+; SSE42-NEXT: shrq $63, %rcx
+; SSE42-NEXT: shrq $32, %rax
+; SSE42-NEXT: addl %ecx, %eax
+; SSE42-NEXT: pextrd $1, %xmm0, %ecx
+; SSE42-NEXT: pinsrd $0, %eax, %xmm0
+; SSE42-NEXT: movl %ecx, %eax
+; SSE42-NEXT: shrl $31, %eax
+; SSE42-NEXT: addl %ecx, %eax
+; SSE42-NEXT: sarl %eax
+; SSE42-NEXT: pinsrd $1, %eax, %xmm0
+; SSE42-NEXT: retq
+;
+; AVX-LABEL: sdiv_poison_mask:
+; AVX: # %bb.0:
+; AVX-NEXT: vmovd %xmm0, %eax
+; AVX-NEXT: cltq
+; AVX-NEXT: imulq $1431655766, %rax, %rax # imm = 0x55555556
+; AVX-NEXT: movq %rax, %rcx
+; AVX-NEXT: shrq $63, %rcx
+; AVX-NEXT: shrq $32, %rax
+; AVX-NEXT: addl %ecx, %eax
+; AVX-NEXT: vpinsrd $0, %eax, %xmm0, %xmm1
+; AVX-NEXT: vpextrd $1, %xmm0, %eax
+; AVX-NEXT: movl %eax, %ecx
+; AVX-NEXT: shrl $31, %ecx
+; AVX-NEXT: addl %eax, %ecx
+; AVX-NEXT: sarl %ecx
+; AVX-NEXT: vpinsrd $1, %ecx, %xmm1, %xmm0
+; AVX-NEXT: retq
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 2>, <2 x i1> <i1 poison, i1 true>)
+ ret <2 x i32> %r
+}
+
+define <3 x i32> @sdiv_disabled_zero_widen(<3 x i32> %x) {
+; SSE2-LABEL: sdiv_disabled_zero_widen:
+; SSE2: # %bb.0:
+; SSE2-NEXT: pshufd {{.*#+}} xmm1 = xmm0[2,3,2,3]
+; SSE2-NEXT: movd %xmm1, %eax
+; SSE2-NEXT: leal 3(%rax), %ecx
+; SSE2-NEXT: testl %eax, %eax
+; SSE2-NEXT: cmovnsl %eax, %ecx
+; SSE2-NEXT: movd %ecx, %xmm2
+; SSE2-NEXT: psrad $2, %xmm2
+; SSE2-NEXT: shufps {{.*#+}} xmm2 = xmm2[0,0],xmm0[3,0]
+; SSE2-NEXT: pshufd {{.*#+}} xmm1 = xmm0[1,1,1,1]
+; SSE2-NEXT: movd %xmm1, %eax
+; SSE2-NEXT: movl %eax, %ecx
+; SSE2-NEXT: shrl $31, %ecx
+; SSE2-NEXT: addl %eax, %ecx
+; SSE2-NEXT: movd %ecx, %xmm1
+; SSE2-NEXT: psrad $1, %xmm1
+; SSE2-NEXT: punpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm0[0]
+; SSE2-NEXT: shufps {{.*#+}} xmm1 = xmm1[2,0],xmm2[0,2]
+; SSE2-NEXT: movaps %xmm1, %xmm0
+; SSE2-NEXT: retq
+;
+; SSE42-LABEL: sdiv_disabled_zero_widen:
+; SSE42: # %bb.0:
+; SSE42-NEXT: pextrd $2, %xmm0, %eax
+; SSE42-NEXT: leal 3(%rax), %ecx
+; SSE42-NEXT: testl %eax, %eax
+; SSE42-NEXT: cmovnsl %eax, %ecx
+; SSE42-NEXT: pextrd $1, %xmm0, %eax
+; SSE42-NEXT: movl %eax, %edx
+; SSE42-NEXT: shrl $31, %edx
+; SSE42-NEXT: addl %eax, %edx
+; SSE42-NEXT: sarl %edx
+; SSE42-NEXT: pinsrd $1, %edx, %xmm0
+; SSE42-NEXT: sarl $2, %ecx
+; SSE42-NEXT: pinsrd $2, %ecx, %xmm0
+; SSE42-NEXT: retq
+;
+; AVX2-LABEL: sdiv_disabled_zero_widen:
+; AVX2: # %bb.0:
+; AVX2-NEXT: vpextrd $2, %xmm0, %eax
+; AVX2-NEXT: leal 3(%rax), %ecx
+; AVX2-NEXT: testl %eax, %eax
+; AVX2-NEXT: cmovnsl %eax, %ecx
+; AVX2-NEXT: vpextrd $1, %xmm0, %eax
+; AVX2-NEXT: sarl $2, %ecx
+; AVX2-NEXT: movl %eax, %edx
+; AVX2-NEXT: shrl $31, %edx
+; AVX2-NEXT: addl %eax, %edx
+; AVX2-NEXT: sarl %edx
+; AVX2-NEXT: vpinsrd $1, %edx, %xmm0, %xmm0
+; AVX2-NEXT: vpinsrd $2, %ecx, %xmm0, %xmm0
+; AVX2-NEXT: retq
+;
+; AVX512-LABEL: sdiv_disabled_zero_widen:
+; AVX512: # %bb.0:
+; AVX512-NEXT: vpextrd $2, %xmm0, %eax
+; AVX512-NEXT: leal 3(%rax), %ecx
+; AVX512-NEXT: testl %eax, %eax
+; AVX512-NEXT: cmovnsl %eax, %ecx
+; AVX512-NEXT: sarl $2, %ecx
+; AVX512-NEXT: vpextrd $1, %xmm0, %eax
+; AVX512-NEXT: movl %eax, %edx
+; AVX512-NEXT: shrl $31, %edx
+; AVX512-NEXT: addl %eax, %edx
+; AVX512-NEXT: sarl %edx
+; AVX512-NEXT: vpinsrd $1, %edx, %xmm0, %xmm0
+; AVX512-NEXT: vpinsrd $2, %ecx, %xmm0, %xmm0
+; AVX512-NEXT: retq
+ %r = call <3 x i32> @llvm.masked.sdiv.v3i32(<3 x i32> %x, <3 x i32> <i32 0, i32 2, i32 4>, <3 x i1> <i1 false, i1 true, i1 true>)
+ ret <3 x i32> %r
+}
+
+define <1 x i1> @sdiv_disabled_i1(<1 x i1> %x) {
+; SSE-LABEL: sdiv_disabled_i1:
+; SSE: # %bb.0:
+; SSE-NEXT: xorl %eax, %eax
+; SSE-NEXT: retq
+;
+; AVX-LABEL: sdiv_disabled_i1:
+; AVX: # %bb.0:
+; AVX-NEXT: xorl %eax, %eax
+; AVX-NEXT: retq
+ %r = call <1 x i1> @llvm.masked.sdiv.v1i1(<1 x i1> %x, <1 x i1> <i1 true>, <1 x i1> zeroinitializer)
+ ret <1 x i1> %r
+}
+
+define <1 x i1> @srem_disabled_i1(<1 x i1> %x) {
+; SSE-LABEL: srem_disabled_i1:
+; SSE: # %bb.0:
+; SSE-NEXT: xorl %eax, %eax
+; SSE-NEXT: retq
+;
+; AVX-LABEL: srem_disabled_i1:
+; AVX: # %bb.0:
+; AVX-NEXT: xorl %eax, %eax
+; AVX-NEXT: retq
+ %r = call <1 x i1> @llvm.masked.srem.v1i1(<1 x i1> %x, <1 x i1> <i1 true>, <1 x i1> zeroinitializer)
+ ret <1 x i1> %r
+}
+
+define <16 x i1> @sdiv_v16i1(<16 x i1> %x, <16 x i1> %y, <16 x i1> %m) {
+; SSE2-LABEL: sdiv_v16i1:
+; SSE2: # %bb.0:
+; SSE2-NEXT: movdqa %xmm0, %xmm4
+; SSE2-NEXT: punpcklbw {{.*#+}} xmm4 = xmm4[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
+; SSE2-NEXT: movdqa %xmm4, %xmm2
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm2 = xmm2[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: movdqa %xmm1, %xmm5
+; SSE2-NEXT: punpcklbw {{.*#+}} xmm5 = xmm5[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
+; SSE2-NEXT: movdqa %xmm5, %xmm3
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm3 = xmm3[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm4 = xmm4[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm5 = xmm5[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: punpckhbw {{.*#+}} xmm0 = xmm0[8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]
+; SSE2-NEXT: movdqa %xmm0, %xmm6
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm6 = xmm6[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: punpckhbw {{.*#+}} xmm1 = xmm1[8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]
+; SSE2-NEXT: movdqa %xmm1, %xmm7
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm7 = xmm7[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm0 = xmm0[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm1 = xmm1[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: pslld $31, %xmm1
+; SSE2-NEXT: psrad $31, %xmm1
+; SSE2-NEXT: cvtdq2ps %xmm1, %xmm1
+; SSE2-NEXT: pslld $31, %xmm0
+; SSE2-NEXT: psrad $31, %xmm0
+; SSE2-NEXT: cvtdq2ps %xmm0, %xmm0
+; SSE2-NEXT: divps %xmm1, %xmm0
+; SSE2-NEXT: cvttps2dq %xmm0, %xmm0
+; SSE2-NEXT: pslld $31, %xmm7
+; SSE2-NEXT: psrad $31, %xmm7
+; SSE2-NEXT: cvtdq2ps %xmm7, %xmm1
+; SSE2-NEXT: pslld $31, %xmm6
+; SSE2-NEXT: psrad $31, %xmm6
+; SSE2-NEXT: cvtdq2ps %xmm6, %xmm6
+; SSE2-NEXT: divps %xmm1, %xmm6
+; SSE2-NEXT: cvttps2dq %xmm6, %xmm1
+; SSE2-NEXT: packssdw %xmm0, %xmm1
+; SSE2-NEXT: pslld $31, %xmm5
+; SSE2-NEXT: psrad $31, %xmm5
+; SSE2-NEXT: cvtdq2ps %xmm5, %xmm0
+; SSE2-NEXT: pslld $31, %xmm4
+; SSE2-NEXT: psrad $31, %xmm4
+; SSE2-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE2-NEXT: divps %xmm0, %xmm4
+; SSE2-NEXT: cvttps2dq %xmm4, %xmm4
+; SSE2-NEXT: pslld $31, %xmm3
+; SSE2-NEXT: psrad $31, %xmm3
+; SSE2-NEXT: cvtdq2ps %xmm3, %xmm0
+; SSE2-NEXT: pslld $31, %xmm2
+; SSE2-NEXT: psrad $31, %xmm2
+; SSE2-NEXT: cvtdq2ps %xmm2, %xmm2
+; SSE2-NEXT: divps %xmm0, %xmm2
+; SSE2-NEXT: cvttps2dq %xmm2, %xmm0
+; SSE2-NEXT: packssdw %xmm4, %xmm0
+; SSE2-NEXT: packsswb %xmm1, %xmm0
+; SSE2-NEXT: retq
+;
+; SSE42-LABEL: sdiv_v16i1:
+; SSE42: # %bb.0:
+; SSE42-NEXT: pshufd {{.*#+}} xmm2 = xmm0[1,1,1,1]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm2 = xmm2[0],zero,zero,zero,xmm2[1],zero,zero,zero,xmm2[2],zero,zero,zero,xmm2[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm3 = xmm1[1,1,1,1]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm3 = xmm3[0],zero,zero,zero,xmm3[1],zero,zero,zero,xmm3[2],zero,zero,zero,xmm3[3],zero,zero,zero
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm4 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm5 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm6 = xmm0[2,3,2,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm6 = xmm6[0],zero,zero,zero,xmm6[1],zero,zero,zero,xmm6[2],zero,zero,zero,xmm6[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm7 = xmm1[2,3,2,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm7 = xmm7[0],zero,zero,zero,xmm7[1],zero,zero,zero,xmm7[2],zero,zero,zero,xmm7[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm0 = xmm0[3,3,3,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm0 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm1 = xmm1[3,3,3,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm1 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero
+; SSE42-NEXT: pslld $31, %xmm1
+; SSE42-NEXT: psrad $31, %xmm1
+; SSE42-NEXT: cvtdq2ps %xmm1, %xmm1
+; SSE42-NEXT: pslld $31, %xmm0
+; SSE42-NEXT: psrad $31, %xmm0
+; SSE42-NEXT: cvtdq2ps %xmm0, %xmm0
+; SSE42-NEXT: divps %xmm1, %xmm0
+; SSE42-NEXT: cvttps2dq %xmm0, %xmm0
+; SSE42-NEXT: pslld $31, %xmm7
+; SSE42-NEXT: psrad $31, %xmm7
+; SSE42-NEXT: cvtdq2ps %xmm7, %xmm1
+; SSE42-NEXT: pslld $31, %xmm6
+; SSE42-NEXT: psrad $31, %xmm6
+; SSE42-NEXT: cvtdq2ps %xmm6, %xmm6
+; SSE42-NEXT: divps %xmm1, %xmm6
+; SSE42-NEXT: cvttps2dq %xmm6, %xmm1
+; SSE42-NEXT: packssdw %xmm0, %xmm1
+; SSE42-NEXT: pslld $31, %xmm5
+; SSE42-NEXT: psrad $31, %xmm5
+; SSE42-NEXT: cvtdq2ps %xmm5, %xmm0
+; SSE42-NEXT: pslld $31, %xmm4
+; SSE42-NEXT: psrad $31, %xmm4
+; SSE42-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE42-NEXT: divps %xmm0, %xmm4
+; SSE42-NEXT: cvttps2dq %xmm4, %xmm0
+; SSE42-NEXT: pslld $31, %xmm3
+; SSE42-NEXT: psrad $31, %xmm3
+; SSE42-NEXT: cvtdq2ps %xmm3, %xmm3
+; SSE42-NEXT: pslld $31, %xmm2
+; SSE42-NEXT: psrad $31, %xmm2
+; SSE42-NEXT: cvtdq2ps %xmm2, %xmm2
+; SSE42-NEXT: divps %xmm3, %xmm2
+; SSE42-NEXT: cvttps2dq %xmm2, %xmm2
+; SSE42-NEXT: packssdw %xmm2, %xmm0
+; SSE42-NEXT: packsswb %xmm1, %xmm0
+; SSE42-NEXT: retq
+;
+; AVX2-LABEL: sdiv_v16i1:
+; AVX2: # %bb.0:
+; AVX2-NEXT: vpmovzxbw {{.*#+}} ymm0 = xmm0[0],zero,xmm0[1],zero,xmm0[2],zero,xmm0[3],zero,xmm0[4],zero,xmm0[5],zero,xmm0[6],zero,xmm0[7],zero,xmm0[8],zero,xmm0[9],zero,xmm0[10],zero,xmm0[11],zero,xmm0[12],zero,xmm0[13],zero,xmm0[14],zero,xmm0[15],zero
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm2 = xmm0[0],zero,xmm0[1],zero,xmm0[2],zero,xmm0[3],zero,xmm0[4],zero,xmm0[5],zero,xmm0[6],zero,xmm0[7],zero
+; AVX2-NEXT: vpmovzxbw {{.*#+}} ymm1 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero,xmm1[4],zero,xmm1[5],zero,xmm1[6],zero,xmm1[7],zero,xmm1[8],zero,xmm1[9],zero,xmm1[10],zero,xmm1[11],zero,xmm1[12],zero,xmm1[13],zero,xmm1[14],zero,xmm1[15],zero
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm3 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero,xmm1[4],zero,xmm1[5],zero,xmm1[6],zero,xmm1[7],zero
+; AVX2-NEXT: vextracti128 $1, %ymm0, %xmm0
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm0 = xmm0[0],zero,xmm0[1],zero,xmm0[2],zero,xmm0[3],zero,xmm0[4],zero,xmm0[5],zero,xmm0[6],zero,xmm0[7],zero
+; AVX2-NEXT: vextracti128 $1, %ymm1, %xmm1
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm1 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero,xmm1[4],zero,xmm1[5],zero,xmm1[6],zero,xmm1[7],zero
+; AVX2-NEXT: vpslld $31, %ymm1, %ymm1
+; AVX2-NEXT: vpsrad $31, %ymm1, %ymm1
+; AVX2-NEXT: vcvtdq2ps %ymm1, %ymm1
+; AVX2-NEXT: vpslld $31, %ymm0, %ymm0
+; AVX2-NEXT: vpsrad $31, %ymm0, %ymm0
+; AVX2-NEXT: vcvtdq2ps %ymm0, %ymm0
+; AVX2-NEXT: vdivps %ymm1, %ymm0, %ymm0
+; AVX2-NEXT: vcvttps2dq %ymm0, %ymm0
+; AVX2-NEXT: vextracti128 $1, %ymm0, %xmm1
+; AVX2-NEXT: vpackssdw %xmm1, %xmm0, %xmm0
+; AVX2-NEXT: vpslld $31, %ymm3, %ymm1
+; AVX2-NEXT: vpsrad $31, %ymm1, %ymm1
+; AVX2-NEXT: vcvtdq2ps %ymm1, %ymm1
+; AVX2-NEXT: vpslld $31, %ymm2, %ymm2
+; AVX2-NEXT: vpsrad $31, %ymm2, %ymm2
+; AVX2-NEXT: vcvtdq2ps %ymm2, %ymm2
+; AVX2-NEXT: vdivps %ymm1, %ymm2, %ymm1
+; AVX2-NEXT: vcvttps2dq %ymm1, %ymm1
+; AVX2-NEXT: vextracti128 $1, %ymm1, %xmm2
+; AVX2-NEXT: vpackssdw %xmm2, %xmm1, %xmm1
+; AVX2-NEXT: vpacksswb %xmm0, %xmm1, %xmm0
+; AVX2-NEXT: vzeroupper
+; AVX2-NEXT: retq
+;
+; AVX512-LABEL: sdiv_v16i1:
+; AVX512: # %bb.0:
+; AVX512-NEXT: vpmovzxbd {{.*#+}} zmm0 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero,xmm0[4],zero,zero,zero,xmm0[5],zero,zero,zero,xmm0[6],zero,zero,zero,xmm0[7],zero,zero,zero,xmm0[8],zero,zero,zero,xmm0[9],zero,zero,zero,xmm0[10],zero,zero,zero,xmm0[11],zero,zero,zero,xmm0[12],zero,zero,zero,xmm0[13],zero,zero,zero,xmm0[14],zero,zero,zero,xmm0[15],zero,zero,zero
+; AVX512-NEXT: vpmovzxbd {{.*#+}} zmm1 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero,xmm1[4],zero,zero,zero,xmm1[5],zero,zero,zero,xmm1[6],zero,zero,zero,xmm1[7],zero,zero,zero,xmm1[8],zero,zero,zero,xmm1[9],zero,zero,zero,xmm1[10],zero,zero,zero,xmm1[11],zero,zero,zero,xmm1[12],zero,zero,zero,xmm1[13],zero,zero,zero,xmm1[14],zero,zero,zero,xmm1[15],zero,zero,zero
+; AVX512-NEXT: vpslld $31, %zmm1, %zmm1
+; AVX512-NEXT: vpsrad $31, %zmm1, %zmm1
+; AVX512-NEXT: vcvtdq2ps %zmm1, %zmm1
+; AVX512-NEXT: vpslld $31, %zmm0, %zmm0
+; AVX512-NEXT: vpsrad $31, %zmm0, %zmm0
+; AVX512-NEXT: vcvtdq2ps %zmm0, %zmm0
+; AVX512-NEXT: vdivps %zmm1, %zmm0, %zmm0
+; AVX512-NEXT: vcvttps2dq %zmm0, %zmm0
+; AVX512-NEXT: vpmovd2m %zmm0, %k0
+; AVX512-NEXT: vpmovm2b %k0, %xmm0
+; AVX512-NEXT: vzeroupper
+; AVX512-NEXT: retq
+ %r = call <16 x i1> @llvm.masked.sdiv.v16i1(<16 x i1> %x, <16 x i1> %y, <16 x i1> %m)
+ ret <16 x i1> %r
+}
+
+define <16 x i1> @srem_v16i1(<16 x i1> %x, <16 x i1> %y, <16 x i1> %m) {
+; SSE2-LABEL: srem_v16i1:
+; SSE2: # %bb.0:
+; SSE2-NEXT: movdqa %xmm0, %xmm4
+; SSE2-NEXT: punpcklbw {{.*#+}} xmm4 = xmm4[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
+; SSE2-NEXT: movdqa %xmm4, %xmm2
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm2 = xmm2[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: movdqa %xmm1, %xmm5
+; SSE2-NEXT: punpcklbw {{.*#+}} xmm5 = xmm5[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
+; SSE2-NEXT: movdqa %xmm5, %xmm3
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm3 = xmm3[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm4 = xmm4[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm5 = xmm5[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: movdqa %xmm0, %xmm6
+; SSE2-NEXT: punpckhbw {{.*#+}} xmm6 = xmm6[8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]
+; SSE2-NEXT: movdqa %xmm6, %xmm7
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm7 = xmm7[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: movdqa %xmm1, %xmm8
+; SSE2-NEXT: punpckhbw {{.*#+}} xmm8 = xmm8[8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]
+; SSE2-NEXT: movdqa %xmm8, %xmm9
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm9 = xmm9[0,0,1,1,2,2,3,3]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm6 = xmm6[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm8 = xmm8[4,4,5,5,6,6,7,7]
+; SSE2-NEXT: pslld $31, %xmm8
+; SSE2-NEXT: psrad $31, %xmm8
+; SSE2-NEXT: cvtdq2ps %xmm8, %xmm8
+; SSE2-NEXT: pslld $31, %xmm6
+; SSE2-NEXT: psrad $31, %xmm6
+; SSE2-NEXT: cvtdq2ps %xmm6, %xmm6
+; SSE2-NEXT: divps %xmm8, %xmm6
+; SSE2-NEXT: cvttps2dq %xmm6, %xmm6
+; SSE2-NEXT: pslld $31, %xmm9
+; SSE2-NEXT: psrad $31, %xmm9
+; SSE2-NEXT: cvtdq2ps %xmm9, %xmm8
+; SSE2-NEXT: pslld $31, %xmm7
+; SSE2-NEXT: psrad $31, %xmm7
+; SSE2-NEXT: cvtdq2ps %xmm7, %xmm7
+; SSE2-NEXT: divps %xmm8, %xmm7
+; SSE2-NEXT: cvttps2dq %xmm7, %xmm7
+; SSE2-NEXT: packssdw %xmm6, %xmm7
+; SSE2-NEXT: pslld $31, %xmm5
+; SSE2-NEXT: psrad $31, %xmm5
+; SSE2-NEXT: cvtdq2ps %xmm5, %xmm5
+; SSE2-NEXT: pslld $31, %xmm4
+; SSE2-NEXT: psrad $31, %xmm4
+; SSE2-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE2-NEXT: divps %xmm5, %xmm4
+; SSE2-NEXT: cvttps2dq %xmm4, %xmm4
+; SSE2-NEXT: pslld $31, %xmm3
+; SSE2-NEXT: psrad $31, %xmm3
+; SSE2-NEXT: cvtdq2ps %xmm3, %xmm3
+; SSE2-NEXT: pslld $31, %xmm2
+; SSE2-NEXT: psrad $31, %xmm2
+; SSE2-NEXT: cvtdq2ps %xmm2, %xmm2
+; SSE2-NEXT: divps %xmm3, %xmm2
+; SSE2-NEXT: cvttps2dq %xmm2, %xmm2
+; SSE2-NEXT: packssdw %xmm4, %xmm2
+; SSE2-NEXT: packsswb %xmm7, %xmm2
+; SSE2-NEXT: pand %xmm1, %xmm2
+; SSE2-NEXT: pxor %xmm2, %xmm0
+; SSE2-NEXT: retq
+;
+; SSE42-LABEL: srem_v16i1:
+; SSE42: # %bb.0:
+; SSE42-NEXT: pshufd {{.*#+}} xmm2 = xmm0[1,1,1,1]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm2 = xmm2[0],zero,zero,zero,xmm2[1],zero,zero,zero,xmm2[2],zero,zero,zero,xmm2[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm3 = xmm1[1,1,1,1]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm3 = xmm3[0],zero,zero,zero,xmm3[1],zero,zero,zero,xmm3[2],zero,zero,zero,xmm3[3],zero,zero,zero
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm4 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm5 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm6 = xmm0[2,3,2,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm6 = xmm6[0],zero,zero,zero,xmm6[1],zero,zero,zero,xmm6[2],zero,zero,zero,xmm6[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm7 = xmm1[2,3,2,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm7 = xmm7[0],zero,zero,zero,xmm7[1],zero,zero,zero,xmm7[2],zero,zero,zero,xmm7[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm8 = xmm0[3,3,3,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm8 = xmm8[0],zero,zero,zero,xmm8[1],zero,zero,zero,xmm8[2],zero,zero,zero,xmm8[3],zero,zero,zero
+; SSE42-NEXT: pshufd {{.*#+}} xmm9 = xmm1[3,3,3,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm9 = xmm9[0],zero,zero,zero,xmm9[1],zero,zero,zero,xmm9[2],zero,zero,zero,xmm9[3],zero,zero,zero
+; SSE42-NEXT: pslld $31, %xmm9
+; SSE42-NEXT: psrad $31, %xmm9
+; SSE42-NEXT: cvtdq2ps %xmm9, %xmm9
+; SSE42-NEXT: pslld $31, %xmm8
+; SSE42-NEXT: psrad $31, %xmm8
+; SSE42-NEXT: cvtdq2ps %xmm8, %xmm8
+; SSE42-NEXT: divps %xmm9, %xmm8
+; SSE42-NEXT: cvttps2dq %xmm8, %xmm8
+; SSE42-NEXT: pslld $31, %xmm7
+; SSE42-NEXT: psrad $31, %xmm7
+; SSE42-NEXT: cvtdq2ps %xmm7, %xmm7
+; SSE42-NEXT: pslld $31, %xmm6
+; SSE42-NEXT: psrad $31, %xmm6
+; SSE42-NEXT: cvtdq2ps %xmm6, %xmm6
+; SSE42-NEXT: divps %xmm7, %xmm6
+; SSE42-NEXT: cvttps2dq %xmm6, %xmm6
+; SSE42-NEXT: packssdw %xmm8, %xmm6
+; SSE42-NEXT: pslld $31, %xmm5
+; SSE42-NEXT: psrad $31, %xmm5
+; SSE42-NEXT: cvtdq2ps %xmm5, %xmm5
+; SSE42-NEXT: pslld $31, %xmm4
+; SSE42-NEXT: psrad $31, %xmm4
+; SSE42-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE42-NEXT: divps %xmm5, %xmm4
+; SSE42-NEXT: cvttps2dq %xmm4, %xmm4
+; SSE42-NEXT: pslld $31, %xmm3
+; SSE42-NEXT: psrad $31, %xmm3
+; SSE42-NEXT: cvtdq2ps %xmm3, %xmm3
+; SSE42-NEXT: pslld $31, %xmm2
+; SSE42-NEXT: psrad $31, %xmm2
+; SSE42-NEXT: cvtdq2ps %xmm2, %xmm2
+; SSE42-NEXT: divps %xmm3, %xmm2
+; SSE42-NEXT: cvttps2dq %xmm2, %xmm2
+; SSE42-NEXT: packssdw %xmm2, %xmm4
+; SSE42-NEXT: packsswb %xmm6, %xmm4
+; SSE42-NEXT: pand %xmm1, %xmm4
+; SSE42-NEXT: pxor %xmm4, %xmm0
+; SSE42-NEXT: retq
+;
+; AVX2-LABEL: srem_v16i1:
+; AVX2: # %bb.0:
+; AVX2-NEXT: vpmovzxbw {{.*#+}} ymm2 = xmm0[0],zero,xmm0[1],zero,xmm0[2],zero,xmm0[3],zero,xmm0[4],zero,xmm0[5],zero,xmm0[6],zero,xmm0[7],zero,xmm0[8],zero,xmm0[9],zero,xmm0[10],zero,xmm0[11],zero,xmm0[12],zero,xmm0[13],zero,xmm0[14],zero,xmm0[15],zero
+; AVX2-NEXT: vpmovzxbw {{.*#+}} ymm3 = xmm1[0],zero,xmm1[1],zero,xmm1[2],zero,xmm1[3],zero,xmm1[4],zero,xmm1[5],zero,xmm1[6],zero,xmm1[7],zero,xmm1[8],zero,xmm1[9],zero,xmm1[10],zero,xmm1[11],zero,xmm1[12],zero,xmm1[13],zero,xmm1[14],zero,xmm1[15],zero
+; AVX2-NEXT: vextracti128 $1, %ymm2, %xmm4
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm4 = xmm4[0],zero,xmm4[1],zero,xmm4[2],zero,xmm4[3],zero,xmm4[4],zero,xmm4[5],zero,xmm4[6],zero,xmm4[7],zero
+; AVX2-NEXT: vextracti128 $1, %ymm3, %xmm5
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero,xmm5[4],zero,xmm5[5],zero,xmm5[6],zero,xmm5[7],zero
+; AVX2-NEXT: vpslld $31, %ymm5, %ymm5
+; AVX2-NEXT: vpsrad $31, %ymm5, %ymm5
+; AVX2-NEXT: vcvtdq2ps %ymm5, %ymm5
+; AVX2-NEXT: vpslld $31, %ymm4, %ymm4
+; AVX2-NEXT: vpsrad $31, %ymm4, %ymm4
+; AVX2-NEXT: vcvtdq2ps %ymm4, %ymm4
+; AVX2-NEXT: vdivps %ymm5, %ymm4, %ymm4
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm2 = xmm2[0],zero,xmm2[1],zero,xmm2[2],zero,xmm2[3],zero,xmm2[4],zero,xmm2[5],zero,xmm2[6],zero,xmm2[7],zero
+; AVX2-NEXT: vpmovzxwd {{.*#+}} ymm3 = xmm3[0],zero,xmm3[1],zero,xmm3[2],zero,xmm3[3],zero,xmm3[4],zero,xmm3[5],zero,xmm3[6],zero,xmm3[7],zero
+; AVX2-NEXT: vcvttps2dq %ymm4, %ymm4
+; AVX2-NEXT: vpslld $31, %ymm3, %ymm3
+; AVX2-NEXT: vpsrad $31, %ymm3, %ymm3
+; AVX2-NEXT: vcvtdq2ps %ymm3, %ymm3
+; AVX2-NEXT: vpslld $31, %ymm2, %ymm2
+; AVX2-NEXT: vpsrad $31, %ymm2, %ymm2
+; AVX2-NEXT: vcvtdq2ps %ymm2, %ymm2
+; AVX2-NEXT: vdivps %ymm3, %ymm2, %ymm2
+; AVX2-NEXT: vextracti128 $1, %ymm4, %xmm3
+; AVX2-NEXT: vpackssdw %xmm3, %xmm4, %xmm3
+; AVX2-NEXT: vcvttps2dq %ymm2, %ymm2
+; AVX2-NEXT: vextracti128 $1, %ymm2, %xmm4
+; AVX2-NEXT: vpackssdw %xmm4, %xmm2, %xmm2
+; AVX2-NEXT: vpacksswb %xmm3, %xmm2, %xmm2
+; AVX2-NEXT: vpand %xmm1, %xmm2, %xmm1
+; AVX2-NEXT: vpxor %xmm1, %xmm0, %xmm0
+; AVX2-NEXT: vzeroupper
+; AVX2-NEXT: retq
+;
+; AVX512-LABEL: srem_v16i1:
+; AVX512: # %bb.0:
+; AVX512-NEXT: vpsllw $7, %xmm0, %xmm2
+; AVX512-NEXT: vpmovb2m %xmm2, %k0
+; AVX512-NEXT: vpsllw $7, %xmm1, %xmm2
+; AVX512-NEXT: vpmovzxbd {{.*#+}} zmm0 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero,xmm0[4],zero,zero,zero,xmm0[5],zero,zero,zero,xmm0[6],zero,zero,zero,xmm0[7],zero,zero,zero,xmm0[8],zero,zero,zero,xmm0[9],zero,zero,zero,xmm0[10],zero,zero,zero,xmm0[11],zero,zero,zero,xmm0[12],zero,zero,zero,xmm0[13],zero,zero,zero,xmm0[14],zero,zero,zero,xmm0[15],zero,zero,zero
+; AVX512-NEXT: vpmovzxbd {{.*#+}} zmm1 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero,xmm1[4],zero,zero,zero,xmm1[5],zero,zero,zero,xmm1[6],zero,zero,zero,xmm1[7],zero,zero,zero,xmm1[8],zero,zero,zero,xmm1[9],zero,zero,zero,xmm1[10],zero,zero,zero,xmm1[11],zero,zero,zero,xmm1[12],zero,zero,zero,xmm1[13],zero,zero,zero,xmm1[14],zero,zero,zero,xmm1[15],zero,zero,zero
+; AVX512-NEXT: vpslld $31, %zmm1, %zmm1
+; AVX512-NEXT: vpsrad $31, %zmm1, %zmm1
+; AVX512-NEXT: vcvtdq2ps %zmm1, %zmm1
+; AVX512-NEXT: vpslld $31, %zmm0, %zmm0
+; AVX512-NEXT: vpsrad $31, %zmm0, %zmm0
+; AVX512-NEXT: vcvtdq2ps %zmm0, %zmm0
+; AVX512-NEXT: vdivps %zmm1, %zmm0, %zmm0
+; AVX512-NEXT: vxorps %xmm1, %xmm1, %xmm1
+; AVX512-NEXT: vcvttps2dq %zmm0, %zmm0
+; AVX512-NEXT: vpmovd2m %zmm0, %k1
+; AVX512-NEXT: vpcmpgtb %xmm2, %xmm1, %k1 {%k1}
+; AVX512-NEXT: kxorw %k1, %k0, %k0
+; AVX512-NEXT: vpmovm2b %k0, %xmm0
+; AVX512-NEXT: vzeroupper
+; AVX512-NEXT: retq
+ %r = call <16 x i1> @llvm.masked.srem.v16i1(<16 x i1> %x, <16 x i1> %y, <16 x i1> %m)
+ ret <16 x i1> %r
+}
diff --git a/llvm/test/CodeGen/X86/masked-udiv.ll b/llvm/test/CodeGen/X86/masked-udiv.ll
index 0aa539448d9745..ced22c6a15630d 100644
--- a/llvm/test/CodeGen/X86/masked-udiv.ll
+++ b/llvm/test/CodeGen/X86/masked-udiv.ll
@@ -1072,3 +1072,205 @@ define <8 x i64> @udiv_v8i64(<8 x i64> %x, <8 x i64> %y, <8 x i1> %m) {
%res = call <8 x i64> @llvm.masked.udiv(<8 x i64> %x, <8 x i64> %y, <8 x i1> %m)
ret <8 x i64> %res
}
+
+define <2 x i32> @udiv_poison_mask(<2 x i32> %x) {
+; SSE2-LABEL: udiv_poison_mask:
+; SSE2: # %bb.0:
+; SSE2-NEXT: movd %xmm0, %eax
+; SSE2-NEXT: movl $2863311531, %ecx # imm = 0xAAAAAAAB
+; SSE2-NEXT: imulq %rax, %rcx
+; SSE2-NEXT: shrq $33, %rcx
+; SSE2-NEXT: movd %ecx, %xmm2
+; SSE2-NEXT: pshufd {{.*#+}} xmm1 = xmm0[1,1,1,1]
+; SSE2-NEXT: psrld $1, %xmm1
+; SSE2-NEXT: punpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm2[0]
+; SSE2-NEXT: shufps {{.*#+}} xmm1 = xmm1[2,0],xmm0[2,3]
+; SSE2-NEXT: movaps %xmm1, %xmm0
+; SSE2-NEXT: retq
+;
+; SSE42-LABEL: udiv_poison_mask:
+; SSE42: # %bb.0:
+; SSE42-NEXT: movd %xmm0, %eax
+; SSE42-NEXT: movl $2863311531, %ecx # imm = 0xAAAAAAAB
+; SSE42-NEXT: imulq %rax, %rcx
+; SSE42-NEXT: shrq $33, %rcx
+; SSE42-NEXT: pextrd $1, %xmm0, %eax
+; SSE42-NEXT: pinsrd $0, %ecx, %xmm0
+; SSE42-NEXT: shrl %eax
+; SSE42-NEXT: pinsrd $1, %eax, %xmm0
+; SSE42-NEXT: retq
+;
+; AVX-LABEL: udiv_poison_mask:
+; AVX: # %bb.0:
+; AVX-NEXT: vmovd %xmm0, %eax
+; AVX-NEXT: movl $2863311531, %ecx # imm = 0xAAAAAAAB
+; AVX-NEXT: imulq %rax, %rcx
+; AVX-NEXT: shrq $33, %rcx
+; AVX-NEXT: vpinsrd $0, %ecx, %xmm0, %xmm1
+; AVX-NEXT: vpextrd $1, %xmm0, %eax
+; AVX-NEXT: shrl %eax
+; AVX-NEXT: vpinsrd $1, %eax, %xmm1, %xmm0
+; AVX-NEXT: retq
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 2>, <2 x i1> <i1 poison, i1 true>)
+ ret <2 x i32> %r
+}
+
+define <1 x i1> @udiv_disabled_i1(<1 x i1> %x, <1 x i1> %y, <1 x i1> %m) {
+; SSE-LABEL: udiv_disabled_i1:
+; SSE: # %bb.0:
+; SSE-NEXT: andl $1, %esi
+; SSE-NEXT: cvtsi2ss %esi, %xmm0
+; SSE-NEXT: andl $1, %edi
+; SSE-NEXT: cvtsi2ss %edi, %xmm1
+; SSE-NEXT: divss %xmm0, %xmm1
+; SSE-NEXT: cvttss2si %xmm1, %eax
+; SSE-NEXT: # kill: def $al killed $al killed $eax
+; SSE-NEXT: retq
+;
+; AVX-LABEL: udiv_disabled_i1:
+; AVX: # %bb.0:
+; AVX-NEXT: andl $1, %esi
+; AVX-NEXT: vcvtsi2ss %esi, %xmm15, %xmm0
+; AVX-NEXT: andl $1, %edi
+; AVX-NEXT: vcvtsi2ss %edi, %xmm15, %xmm1
+; AVX-NEXT: vdivss %xmm0, %xmm1, %xmm0
+; AVX-NEXT: vcvttss2si %xmm0, %eax
+; AVX-NEXT: # kill: def $al killed $al killed $eax
+; AVX-NEXT: retq
+ %r = call <1 x i1> @llvm.masked.udiv.v1i1(<1 x i1> %x, <1 x i1> %y, <1 x i1> %m)
+ ret <1 x i1> %r
+}
+
+define <16 x i1> @udiv_v16i1(<16 x i1> %x, <16 x i1> %y, <16 x i1> %m) {
+; SSE2-LABEL: udiv_v16i1:
+; SSE2: # %bb.0:
+; SSE2-NEXT: movdqa {{.*#+}} xmm3 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
+; SSE2-NEXT: pand %xmm3, %xmm1
+; SSE2-NEXT: pxor %xmm2, %xmm2
+; SSE2-NEXT: movdqa %xmm1, %xmm4
+; SSE2-NEXT: punpckhbw {{.*#+}} xmm4 = xmm4[8],xmm2[8],xmm4[9],xmm2[9],xmm4[10],xmm2[10],xmm4[11],xmm2[11],xmm4[12],xmm2[12],xmm4[13],xmm2[13],xmm4[14],xmm2[14],xmm4[15],xmm2[15]
+; SSE2-NEXT: movdqa %xmm4, %xmm5
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm5 = xmm5[4],xmm2[4],xmm5[5],xmm2[5],xmm5[6],xmm2[6],xmm5[7],xmm2[7]
+; SSE2-NEXT: cvtdq2ps %xmm5, %xmm5
+; SSE2-NEXT: pand %xmm3, %xmm0
+; SSE2-NEXT: movdqa %xmm0, %xmm3
+; SSE2-NEXT: punpckhbw {{.*#+}} xmm3 = xmm3[8],xmm2[8],xmm3[9],xmm2[9],xmm3[10],xmm2[10],xmm3[11],xmm2[11],xmm3[12],xmm2[12],xmm3[13],xmm2[13],xmm3[14],xmm2[14],xmm3[15],xmm2[15]
+; SSE2-NEXT: movdqa %xmm3, %xmm6
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm6 = xmm6[4],xmm2[4],xmm6[5],xmm2[5],xmm6[6],xmm2[6],xmm6[7],xmm2[7]
+; SSE2-NEXT: cvtdq2ps %xmm6, %xmm6
+; SSE2-NEXT: divps %xmm5, %xmm6
+; SSE2-NEXT: cvttps2dq %xmm6, %xmm5
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1],xmm4[2],xmm2[2],xmm4[3],xmm2[3]
+; SSE2-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1],xmm3[2],xmm2[2],xmm3[3],xmm2[3]
+; SSE2-NEXT: cvtdq2ps %xmm3, %xmm3
+; SSE2-NEXT: divps %xmm4, %xmm3
+; SSE2-NEXT: cvttps2dq %xmm3, %xmm3
+; SSE2-NEXT: packssdw %xmm5, %xmm3
+; SSE2-NEXT: punpcklbw {{.*#+}} xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3],xmm1[4],xmm2[4],xmm1[5],xmm2[5],xmm1[6],xmm2[6],xmm1[7],xmm2[7]
+; SSE2-NEXT: movdqa %xmm1, %xmm4
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm4 = xmm4[4],xmm2[4],xmm4[5],xmm2[5],xmm4[6],xmm2[6],xmm4[7],xmm2[7]
+; SSE2-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE2-NEXT: punpcklbw {{.*#+}} xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1],xmm0[2],xmm2[2],xmm0[3],xmm2[3],xmm0[4],xmm2[4],xmm0[5],xmm2[5],xmm0[6],xmm2[6],xmm0[7],xmm2[7]
+; SSE2-NEXT: movdqa %xmm0, %xmm5
+; SSE2-NEXT: punpckhwd {{.*#+}} xmm5 = xmm5[4],xmm2[4],xmm5[5],xmm2[5],xmm5[6],xmm2[6],xmm5[7],xmm2[7]
+; SSE2-NEXT: cvtdq2ps %xmm5, %xmm5
+; SSE2-NEXT: divps %xmm4, %xmm5
+; SSE2-NEXT: cvttps2dq %xmm5, %xmm4
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
+; SSE2-NEXT: cvtdq2ps %xmm1, %xmm1
+; SSE2-NEXT: punpcklwd {{.*#+}} xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1],xmm0[2],xmm2[2],xmm0[3],xmm2[3]
+; SSE2-NEXT: cvtdq2ps %xmm0, %xmm0
+; SSE2-NEXT: divps %xmm1, %xmm0
+; SSE2-NEXT: cvttps2dq %xmm0, %xmm0
+; SSE2-NEXT: packssdw %xmm4, %xmm0
+; SSE2-NEXT: packuswb %xmm3, %xmm0
+; SSE2-NEXT: retq
+;
+; SSE42-LABEL: udiv_v16i1:
+; SSE42: # %bb.0:
+; SSE42-NEXT: movdqa {{.*#+}} xmm2 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
+; SSE42-NEXT: pand %xmm2, %xmm1
+; SSE42-NEXT: pshufd {{.*#+}} xmm3 = xmm1[3,3,3,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm3 = xmm3[0],zero,zero,zero,xmm3[1],zero,zero,zero,xmm3[2],zero,zero,zero,xmm3[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm3, %xmm3
+; SSE42-NEXT: pand %xmm2, %xmm0
+; SSE42-NEXT: pshufd {{.*#+}} xmm2 = xmm0[3,3,3,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm2 = xmm2[0],zero,zero,zero,xmm2[1],zero,zero,zero,xmm2[2],zero,zero,zero,xmm2[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm2, %xmm2
+; SSE42-NEXT: divps %xmm3, %xmm2
+; SSE42-NEXT: cvttps2dq %xmm2, %xmm2
+; SSE42-NEXT: pshufd {{.*#+}} xmm3 = xmm1[2,3,2,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm3 = xmm3[0],zero,zero,zero,xmm3[1],zero,zero,zero,xmm3[2],zero,zero,zero,xmm3[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm3, %xmm3
+; SSE42-NEXT: pshufd {{.*#+}} xmm4 = xmm0[2,3,2,3]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm4 = xmm4[0],zero,zero,zero,xmm4[1],zero,zero,zero,xmm4[2],zero,zero,zero,xmm4[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE42-NEXT: divps %xmm3, %xmm4
+; SSE42-NEXT: cvttps2dq %xmm4, %xmm3
+; SSE42-NEXT: packssdw %xmm2, %xmm3
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm2 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm2, %xmm2
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm4 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm4, %xmm4
+; SSE42-NEXT: divps %xmm2, %xmm4
+; SSE42-NEXT: cvttps2dq %xmm4, %xmm2
+; SSE42-NEXT: pshufd {{.*#+}} xmm1 = xmm1[1,1,1,1]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm1 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm1, %xmm1
+; SSE42-NEXT: pshufd {{.*#+}} xmm0 = xmm0[1,1,1,1]
+; SSE42-NEXT: pmovzxbd {{.*#+}} xmm0 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero
+; SSE42-NEXT: cvtdq2ps %xmm0, %xmm0
+; SSE42-NEXT: divps %xmm1, %xmm0
+; SSE42-NEXT: cvttps2dq %xmm0, %xmm0
+; SSE42-NEXT: packssdw %xmm0, %xmm2
+; SSE42-NEXT: packuswb %xmm3, %xmm2
+; SSE42-NEXT: movdqa %xmm2, %xmm0
+; SSE42-NEXT: retq
+;
+; AVX2-LABEL: udiv_v16i1:
+; AVX2: # %bb.0:
+; AVX2-NEXT: vpbroadcastd {{.*#+}} xmm2 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
+; AVX2-NEXT: vpand %xmm2, %xmm1, %xmm1
+; AVX2-NEXT: vpmovzxbd {{.*#+}} ymm3 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero,xmm1[4],zero,zero,zero,xmm1[5],zero,zero,zero,xmm1[6],zero,zero,zero,xmm1[7],zero,zero,zero
+; AVX2-NEXT: vcvtdq2ps %ymm3, %ymm3
+; AVX2-NEXT: vpand %xmm2, %xmm0, %xmm0
+; AVX2-NEXT: vpmovzxbd {{.*#+}} ymm2 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero,xmm0[4],zero,zero,zero,xmm0[5],zero,zero,zero,xmm0[6],zero,zero,zero,xmm0[7],zero,zero,zero
+; AVX2-NEXT: vcvtdq2ps %ymm2, %ymm2
+; AVX2-NEXT: vdivps %ymm3, %ymm2, %ymm2
+; AVX2-NEXT: vcvttps2dq %ymm2, %ymm2
+; AVX2-NEXT: vextracti128 $1, %ymm2, %xmm3
+; AVX2-NEXT: vpackssdw %xmm3, %xmm2, %xmm2
+; AVX2-NEXT: vpshufd {{.*#+}} xmm1 = xmm1[2,3,2,3]
+; AVX2-NEXT: vpmovzxbd {{.*#+}} ymm1 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero,xmm1[4],zero,zero,zero,xmm1[5],zero,zero,zero,xmm1[6],zero,zero,zero,xmm1[7],zero,zero,zero
+; AVX2-NEXT: vcvtdq2ps %ymm1, %ymm1
+; AVX2-NEXT: vpshufd {{.*#+}} xmm0 = xmm0[2,3,2,3]
+; AVX2-NEXT: vpmovzxbd {{.*#+}} ymm0 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero,xmm0[4],zero,zero,zero,xmm0[5],zero,zero,zero,xmm0[6],zero,zero,zero,xmm0[7],zero,zero,zero
+; AVX2-NEXT: vcvtdq2ps %ymm0, %ymm0
+; AVX2-NEXT: vdivps %ymm1, %ymm0, %ymm0
+; AVX2-NEXT: vcvttps2dq %ymm0, %ymm0
+; AVX2-NEXT: vextracti128 $1, %ymm0, %xmm1
+; AVX2-NEXT: vpackssdw %xmm1, %xmm0, %xmm0
+; AVX2-NEXT: vpackuswb %xmm0, %xmm2, %xmm0
+; AVX2-NEXT: vzeroupper
+; AVX2-NEXT: retq
+;
+; AVX512-LABEL: udiv_v16i1:
+; AVX512: # %bb.0:
+; AVX512-NEXT: vpbroadcastd {{.*#+}} xmm2 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
+; AVX512-NEXT: vpand %xmm2, %xmm1, %xmm1
+; AVX512-NEXT: vpmovzxbd {{.*#+}} zmm1 = xmm1[0],zero,zero,zero,xmm1[1],zero,zero,zero,xmm1[2],zero,zero,zero,xmm1[3],zero,zero,zero,xmm1[4],zero,zero,zero,xmm1[5],zero,zero,zero,xmm1[6],zero,zero,zero,xmm1[7],zero,zero,zero,xmm1[8],zero,zero,zero,xmm1[9],zero,zero,zero,xmm1[10],zero,zero,zero,xmm1[11],zero,zero,zero,xmm1[12],zero,zero,zero,xmm1[13],zero,zero,zero,xmm1[14],zero,zero,zero,xmm1[15],zero,zero,zero
+; AVX512-NEXT: vcvtdq2ps %zmm1, %zmm1
+; AVX512-NEXT: vpand %xmm2, %xmm0, %xmm0
+; AVX512-NEXT: vpmovzxbd {{.*#+}} zmm0 = xmm0[0],zero,zero,zero,xmm0[1],zero,zero,zero,xmm0[2],zero,zero,zero,xmm0[3],zero,zero,zero,xmm0[4],zero,zero,zero,xmm0[5],zero,zero,zero,xmm0[6],zero,zero,zero,xmm0[7],zero,zero,zero,xmm0[8],zero,zero,zero,xmm0[9],zero,zero,zero,xmm0[10],zero,zero,zero,xmm0[11],zero,zero,zero,xmm0[12],zero,zero,zero,xmm0[13],zero,zero,zero,xmm0[14],zero,zero,zero,xmm0[15],zero,zero,zero
+; AVX512-NEXT: vcvtdq2ps %zmm0, %zmm0
+; AVX512-NEXT: vdivps %zmm1, %zmm0, %zmm0
+; AVX512-NEXT: vcvttps2dq %zmm0, %zmm0
+; AVX512-NEXT: vpslld $31, %zmm0, %zmm0
+; AVX512-NEXT: vpmovd2m %zmm0, %k0
+; AVX512-NEXT: vpmovm2b %k0, %xmm0
+; AVX512-NEXT: vzeroupper
+; AVX512-NEXT: retq
+ %r = call <16 x i1> @llvm.masked.udiv.v16i1(<16 x i1> %x, <16 x i1> %y, <16 x i1> %m)
+ ret <16 x i1> %r
+}
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/masked-divrem.ll b/llvm/test/Transforms/CodeGenPrepare/X86/masked-divrem.ll
new file mode 100644
index 00000000000000..7220170bca28ed
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/masked-divrem.ll
@@ -0,0 +1,42 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -mtriple=i386 -mcpu=atom < %s | FileCheck %s
+
+define i32 @udiv(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @udiv(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @sdiv(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @urem(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @srem(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @srem(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
>From 73c68b5ab21e682fd484f754b603b199470530ec Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 12:36:30 +0100
Subject: [PATCH 3/9] [Analysis][Transforms] Optimize masked integer div/rem
Teach the middle end about scalar and vector llvm.masked.{u,s}{div,rem}:
* ConstantFolding folds fixed vectors lane-wise and scalable splat calls
when all operands are splats, refining disabled, undef and poison mask
lanes as well as exceptional enabled divisions to poison. InstSimplify
folds disabled or poison masks, poison operands, and signed i1 calls,
and delegates all-true masks (excluding poison mask lanes) to ordinary
simplifyBinOp. InstCombine replaces a bundle-free masked operation by
the ordinary binary operator when every lane is provably enabled or
when all lanes have nonzero, non-poison divisors and signed overflow
is impossible, so existing division folds (such as magic-number
division) keep working.
* ValueTracking computes known bits and signed division/remainder sign
bits for defined result lanes like the ordinary operations, without
deriving "divisor is not zero" or non-poison mask guarantees from a
masked call.
* LoopVectorize widens scalar masked operations and merges block/tail
predicates using select(blk, m, false), accounting for the select in
call-widening cost. SLP packs compatible scalar masked calls without
introducing poison padding lanes. Narrowing a single-scalar vector
masked recipe freezes the mask before guarding the fallback divisor
and folds signed i1 directly to zero. The intrinsics stay out of
isTriviallyVectorizable so generic lane-wise transforms cannot create
poison mask or divisor lanes or speculate inactive lanes.
* DivRemPairs pairs bundle-free, live masked div and rem calls with
identical operands, mask and signedness when the target has no
combined div/rem op and the quotient dominates the remainder or can
safely move before it in the same block. It freezes shared operands
and mask at most once as needed and excludes zero divisors and signed
overflow from the quotient's mask so frozen poison cannot introduce UB.
Assisted-by: Claude
---
llvm/lib/Analysis/ConstantFolding.cpp | 20 +-
llvm/lib/Analysis/InstructionSimplify.cpp | 20 +
llvm/lib/Analysis/ValueTracking.cpp | 149 ++++---
llvm/lib/Analysis/VectorUtils.cpp | 8 +-
.../InstCombine/InstCombineCalls.cpp | 30 ++
llvm/lib/Transforms/Scalar/DivRemPairs.cpp | 137 +++++-
.../Vectorize/LoopVectorizationLegality.cpp | 3 +-
.../Transforms/Vectorize/SLPVectorizer.cpp | 8 +-
.../SLPCompatibilityAnalysis.cpp | 6 +-
.../SLPVectorizer/SLPCompatibilityAnalysis.h | 4 +
.../Transforms/Vectorize/VPlanTransforms.cpp | 26 +-
.../DivRemPairs/RISCV/masked-divrem.ll | 284 +++++++++++++
.../DivRemPairs/X86/masked-divrem.ll | 16 +
.../InstCombine/masked-divrem-demanded.ll | 71 ++++
.../InstCombine/masked-divrem-knownbits.ll | 132 ++++++
.../Transforms/InstCombine/masked-divrem.ll | 395 ++++++++++++++++++
.../InstSimplify/ConstProp/masked-divrem.ll | 215 ++++++++++
.../InstSimplify/masked-divrem-knownbits.ll | 138 ++++++
.../Transforms/InstSimplify/masked-divrem.ll | 255 +++++++++++
.../AArch64/divs-with-scalable-vfs.ll | 3 +-
.../X86/divs-with-tail-folding.ll | 6 +-
.../X86/masked-divrem-tail-folded.ll | 255 +++++++++++
.../LoopVectorize/X86/masked-divrem.ll | 313 ++++++++++++++
.../masked-divrem-defined-effects.ll | 67 +++
.../SLPVectorizer/X86/masked-divrem-scalar.ll | 174 ++++++++
.../Transforms/VectorCombine/masked-divrem.ll | 46 ++
26 files changed, 2720 insertions(+), 61 deletions(-)
create mode 100644 llvm/test/Transforms/DivRemPairs/RISCV/masked-divrem.ll
create mode 100644 llvm/test/Transforms/DivRemPairs/X86/masked-divrem.ll
create mode 100644 llvm/test/Transforms/InstCombine/masked-divrem-demanded.ll
create mode 100644 llvm/test/Transforms/InstCombine/masked-divrem-knownbits.ll
create mode 100644 llvm/test/Transforms/InstCombine/masked-divrem.ll
create mode 100644 llvm/test/Transforms/InstSimplify/ConstProp/masked-divrem.ll
create mode 100644 llvm/test/Transforms/InstSimplify/masked-divrem-knownbits.ll
create mode 100644 llvm/test/Transforms/InstSimplify/masked-divrem.ll
create mode 100644 llvm/test/Transforms/LoopVectorize/X86/masked-divrem-tail-folded.ll
create mode 100644 llvm/test/Transforms/LoopVectorize/X86/masked-divrem.ll
create mode 100644 llvm/test/Transforms/PhaseOrdering/masked-divrem-defined-effects.ll
create mode 100644 llvm/test/Transforms/SLPVectorizer/X86/masked-divrem-scalar.ll
create mode 100644 llvm/test/Transforms/VectorCombine/masked-divrem.ll
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 144047e46565ae..2506a934561d55 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1915,6 +1915,10 @@ static bool canConstantFoldIntrinsic(Intrinsic::ID ID, bool IsStrictFP) {
case Intrinsic::pdep:
case Intrinsic::pext:
case Intrinsic::launder_invariant_group:
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem:
case Intrinsic::masked_load:
case Intrinsic::get_active_lane_mask:
case Intrinsic::mask_beforefirst:
@@ -4325,6 +4329,19 @@ static Constant *ConstantFoldScalarCall3(StringRef Name,
const CallBase *Call = nullptr) {
assert(Operands.size() == 3 && "Wrong number of operands.");
+ if (unsigned Opcode = Intrinsic::getMaskedIntDivRemOpcode(IntrinsicID)) {
+ // Choose an inactive lane for undef masks. Disabled and poison mask lanes
+ // fold to poison (refining UB if the source lane had an exceptional
+ // divisor).
+ if (isa<UndefValue>(Operands[2]) || Operands[2]->isNullValue())
+ return PoisonValue::get(Ty);
+ if (!Operands[2]->isOneValue())
+ return nullptr;
+ // Ordinary constant folding also refines exceptional enabled operations
+ // to poison. Fixed vectors reach this point one lane at a time.
+ return ConstantFoldBinaryInstruction(Opcode, Operands[0], Operands[1]);
+ }
+
if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
@@ -4848,7 +4865,8 @@ static Constant *ConstantFoldScalableVectorCall(
// operands are splats.
// TODO: ConstantFoldFixedVectorCall should probably check this too?
- if (!isTriviallyVectorizable(IntrinsicID))
+ if (!isTriviallyVectorizable(IntrinsicID) &&
+ !Intrinsic::getMaskedIntDivRemOpcode(IntrinsicID))
return nullptr;
SmallVector<Constant *, 4> SplatOps;
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 161d8c628e3d20..673723de4a414c 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -7580,6 +7580,26 @@ Value *llvm::simplifyIntrinsic(Intrinsic::ID IID, Type *ReturnType,
// Handle intrinsics with 3 or more arguments.
switch (IID) {
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem: {
+ if (match(Args[2], m_ZeroOrPoison()) || isa<PoisonValue>(Args[0]) ||
+ isa<PoisonValue>(Args[1]))
+ return PoisonValue::get(ReturnType);
+ if (ReturnType->getScalarSizeInBits() == 1 &&
+ (IID == Intrinsic::masked_sdiv || IID == Intrinsic::masked_srem))
+ return Constant::getNullValue(ReturnType);
+
+ // Scalar value simplifications refine an inactive poison result. For
+ // vectors, ordinary UB folds may discard valid neighboring lanes, so use
+ // them only when every lane is enabled.
+ if (ReturnType->isIntegerTy() || match(Args[2], m_AllOnesForbidPoison())) {
+ unsigned Opcode = Intrinsic::getMaskedIntDivRemOpcode(IID);
+ return simplifyBinOp(Opcode, Args[0], Args[1], Q);
+ }
+ return nullptr;
+ }
case Intrinsic::masked_load:
case Intrinsic::masked_gather: {
Value *MaskArg = Args[1];
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e0df7f74ca74ca..c430d98606121f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2263,6 +2263,35 @@ static void computeKnownBitsFromOperator(const Operator *I,
switch (II->getIntrinsicID()) {
default:
break;
+ // Disabled lanes are poison, so every defined lane obeys the ordinary
+ // division and remainder known-bits rules.
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem: {
+ computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
+ KnownBits Known3(BitWidth);
+ computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
+ KnownBits Res(BitWidth);
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::masked_udiv:
+ Res = KnownBits::udiv(Known2, Known3, /*Exact=*/false);
+ break;
+ case Intrinsic::masked_sdiv:
+ Res = KnownBits::sdiv(Known2, Known3, /*Exact=*/false);
+ break;
+ case Intrinsic::masked_urem:
+ Res = KnownBits::urem(Known2, Known3);
+ break;
+ case Intrinsic::masked_srem:
+ Res = KnownBits::srem(Known2, Known3);
+ break;
+ default:
+ llvm_unreachable("Unexpected intrinsic ID");
+ }
+ Known = Known.unionWith(Res);
+ break;
+ }
case Intrinsic::abs: {
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
@@ -4600,6 +4629,62 @@ static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
return Result;
}
+/// Compute the number of sign bits in a signed division result, or return 0
+/// if nothing better than the generic fallback is known.
+static unsigned computeNumSignBitsSDiv(const Value *Op0, const Value *Op1,
+ const APInt &DemandedElts,
+ const SimplifyQuery &Q, unsigned Depth,
+ unsigned TyBits) {
+ const APInt *Denominator;
+ // sdiv X, C -> adds log(C) sign bits.
+ if (match(Op1, m_APInt(Denominator))) {
+ // Ignore non-positive denominator.
+ if (!Denominator->isStrictlyPositive())
+ return 0;
+
+ // Calculate the incoming numerator bits.
+ unsigned NumBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
+
+ // Add floor(log(C)) bits to the numerator bits.
+ return std::min(TyBits, NumBits + Denominator->logBase2());
+ }
+ return 0;
+}
+
+/// Compute the number of sign bits in a signed remainder result.
+static unsigned computeNumSignBitsSRem(const Value *Op0, const Value *Op1,
+ const APInt &DemandedElts,
+ const SimplifyQuery &Q, unsigned Depth,
+ unsigned TyBits) {
+ unsigned Tmp = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
+
+ const APInt *Denominator;
+ // srem X, C -> we know that the result is within [-C+1,C) when C is a
+ // positive constant. This let us put a lower bound on the number of sign
+ // bits.
+ if (match(Op1, m_APInt(Denominator))) {
+ // Ignore non-positive denominator.
+ if (Denominator->isStrictlyPositive()) {
+ // Calculate the leading sign bit constraints by examining the
+ // denominator. Given that the denominator is positive, there are two
+ // cases:
+ //
+ // 1. The numerator is positive. The result range is [0,C) and
+ // [0,C) u< (1 << ceilLogBase2(C)).
+ //
+ // 2. The numerator is negative. Then the result range is (-C,0] and
+ // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
+ //
+ // Thus a lower bound on the number of sign bits is `TyBits -
+ // ceilLogBase2(C)`.
+
+ unsigned ResBits = TyBits - Denominator->ceilLogBase2();
+ Tmp = std::max(Tmp, ResBits);
+ }
+ }
+ return Tmp;
+}
+
/// Return the number of times the sign bit of the register is replicated into
/// the other bits. We know that at least 1 bit is always equal to the sign bit
/// (itself), but other cases can give us information. For example, immediately
@@ -4678,55 +4763,16 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
Tmp;
- case Instruction::SDiv: {
- const APInt *Denominator;
- // sdiv X, C -> adds log(C) sign bits.
- if (match(U->getOperand(1), m_APInt(Denominator))) {
-
- // Ignore non-positive denominator.
- if (!Denominator->isStrictlyPositive())
- break;
-
- // Calculate the incoming numerator bits.
- unsigned NumBits =
- ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
-
- // Add floor(log(C)) bits to the numerator bits.
- return std::min(TyBits, NumBits + Denominator->logBase2());
- }
+ case Instruction::SDiv:
+ Tmp = computeNumSignBitsSDiv(U->getOperand(0), U->getOperand(1),
+ DemandedElts, Q, Depth, TyBits);
+ if (Tmp)
+ return Tmp;
break;
- }
- case Instruction::SRem: {
- Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
-
- const APInt *Denominator;
- // srem X, C -> we know that the result is within [-C+1,C) when C is a
- // positive constant. This let us put a lower bound on the number of sign
- // bits.
- if (match(U->getOperand(1), m_APInt(Denominator))) {
-
- // Ignore non-positive denominator.
- if (Denominator->isStrictlyPositive()) {
- // Calculate the leading sign bit constraints by examining the
- // denominator. Given that the denominator is positive, there are two
- // cases:
- //
- // 1. The numerator is positive. The result range is [0,C) and
- // [0,C) u< (1 << ceilLogBase2(C)).
- //
- // 2. The numerator is negative. Then the result range is (-C,0] and
- // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
- //
- // Thus a lower bound on the number of sign bits is `TyBits -
- // ceilLogBase2(C)`.
-
- unsigned ResBits = TyBits - Denominator->ceilLogBase2();
- Tmp = std::max(Tmp, ResBits);
- }
- }
- return Tmp;
- }
+ case Instruction::SRem:
+ return computeNumSignBitsSRem(U->getOperand(0), U->getOperand(1),
+ DemandedElts, Q, Depth, TyBits);
case Instruction::AShr: {
Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
@@ -4954,6 +5000,15 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
switch (II->getIntrinsicID()) {
default:
break;
+ case Intrinsic::masked_sdiv:
+ Tmp = computeNumSignBitsSDiv(U->getOperand(0), U->getOperand(1),
+ DemandedElts, Q, Depth, TyBits);
+ if (Tmp)
+ return Tmp;
+ break;
+ case Intrinsic::masked_srem:
+ return computeNumSignBitsSRem(U->getOperand(0), U->getOperand(1),
+ DemandedElts, Q, Depth, TyBits);
case Intrinsic::abs:
Tmp =
ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 161c70f5113020..0877be24ebef79 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -283,8 +283,12 @@ Intrinsic::ID llvm::getVectorIntrinsicIDForCall(const CallInst *CI,
if (ID == Intrinsic::not_intrinsic)
return Intrinsic::not_intrinsic;
- if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
- ID == Intrinsic::lifetime_end || ID == Intrinsic::assume ||
+ // Masked div/rem may be widened by vectorizers, but generic lane-wise
+ // transforms must not introduce poison mask/divisor lanes or execute new
+ // lanes.
+ if (Intrinsic::getMaskedIntDivRemOpcode(ID) || isTriviallyVectorizable(ID) ||
+ ID == Intrinsic::lifetime_start || ID == Intrinsic::lifetime_end ||
+ ID == Intrinsic::assume ||
ID == Intrinsic::experimental_noalias_scope_decl ||
ID == Intrinsic::sideeffect || ID == Intrinsic::pseudoprobe)
return ID;
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index b85bbb4896b166..9e6f55c221e560 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2899,6 +2899,36 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
break;
}
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem: {
+ if (II->hasOperandBundles() || II->isMustTailCall())
+ break;
+ Value *Op0 = II->getArgOperand(0), *Op1 = II->getArgOperand(1);
+ if (!match(II->getArgOperand(2), m_AllOnesForbidPoison())) {
+ // A plain division also executes inactive lanes, so its divisor must
+ // be nonzero and non-poison, and signed overflow must be impossible.
+ if (!isKnownNonZero(Op1, SQ.getWithInstruction(&CI)) ||
+ !isGuaranteedNotToBePoison(Op1, SQ.AC, &CI, SQ.DT))
+ break;
+ if ((IID == Intrinsic::masked_sdiv || IID == Intrinsic::masked_srem) &&
+ computeKnownBits(Op1, II).Zero.isZero()) {
+ KnownBits KnownDividend = computeKnownBits(Op0, II);
+ if (!KnownDividend.isNonNegative() &&
+ !KnownDividend.One.intersects(
+ ~APInt::getSignMask(KnownDividend.getBitWidth())))
+ break;
+ // A poison dividend could be MIN when the divisor is -1.
+ if (!isGuaranteedNotToBePoison(Op0, SQ.AC, &CI, SQ.DT))
+ break;
+ }
+ }
+ Instruction::BinaryOps Opcode = static_cast<Instruction::BinaryOps>(
+ Intrinsic::getMaskedIntDivRemOpcode(IID));
+ return BinaryOperator::Create(Opcode, Op0, Op1, II->getName());
+ }
+
case Intrinsic::smulh: {
Value *Arg0 = II->getArgOperand(0);
Value *Arg1 = II->getArgOperand(1);
diff --git a/llvm/lib/Transforms/Scalar/DivRemPairs.cpp b/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
index 3adb3539f5890c..0b2e092e4b852a 100644
--- a/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
+++ b/llvm/lib/Transforms/Scalar/DivRemPairs.cpp
@@ -20,6 +20,8 @@
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/DebugCounter.h"
#include "llvm/Transforms/Utils/BypassSlowDivision.h"
@@ -164,6 +166,139 @@ static DivRemWorklistTy getWorklist(Function &F) {
return Worklist;
}
+// Reuse a quotient with the identical mask when it dominates the remainder,
+// or can be moved before it in the same block.
+static bool optimizeMaskedDivRem(Function &F, const TargetTransformInfo &TTI,
+ const DominatorTree &DT) {
+ DenseMap<DivRemMapKey, SmallVector<IntrinsicInst *, 2>> Divs;
+ struct Pair {
+ AssertingVH<IntrinsicInst> Div;
+ AssertingVH<Instruction> Rem;
+ };
+ SmallVector<Pair> Pairs;
+ for (BasicBlock &BB : F)
+ for (Instruction &I : BB)
+ if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
+ if (II->hasOperandBundles() || II->isMustTailCall() || II->use_empty())
+ continue;
+ unsigned Opc =
+ Intrinsic::getMaskedIntDivRemOpcode(II->getIntrinsicID());
+ if (Opc == Instruction::UDiv || Opc == Instruction::SDiv)
+ Divs[DivRemMapKey(Opc == Instruction::SDiv, II->getArgOperand(0),
+ II->getArgOperand(1), II->getArgOperand(2))]
+ .push_back(II);
+ }
+ if (Divs.empty())
+ return false;
+ for (BasicBlock &BB : F)
+ for (Instruction &I : BB)
+ if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
+ if (II->hasOperandBundles() || II->isMustTailCall() || II->use_empty())
+ continue;
+ unsigned Opc =
+ Intrinsic::getMaskedIntDivRemOpcode(II->getIntrinsicID());
+ if (Opc != Instruction::URem && Opc != Instruction::SRem)
+ continue;
+ bool Signed = Opc == Instruction::SRem;
+ if (TTI.hasDivRemOp(II->getType(), Signed))
+ continue;
+ auto It =
+ Divs.find(DivRemMapKey(Signed, II->getArgOperand(0),
+ II->getArgOperand(1), II->getArgOperand(2)));
+ if (It == Divs.end())
+ continue;
+ auto CanMoveBeforeRem = [](Instruction *Rem, Instruction *Div) {
+ return llvm::all_of(
+ make_range(Rem->getIterator(), Div->getIterator()),
+ [](const Instruction &Inst) {
+ return isGuaranteedToTransferExecutionToSuccessor(&Inst);
+ });
+ };
+ bool FailedSameBlockMove = false;
+ for (IntrinsicInst *Div : It->second) {
+ if (DT.dominates(Div, II)) {
+ Pairs.push_back({Div, II});
+ break;
+ }
+ if (Div->getParent() == II->getParent()) {
+ if (!FailedSameBlockMove && CanMoveBeforeRem(II, Div)) {
+ Pairs.push_back({Div, II});
+ break;
+ }
+ FailedSameBlockMove = true;
+ }
+ }
+ }
+ // Drop AssertingVH keys before erasing decomposed remainders that may also be
+ // operands of other masked divisions in Divs.
+ Divs.clear();
+
+ // Complete motion before creating operands at each quotient's final position.
+ // Block layout can put a dominated remainder ahead of an earlier same-block
+ // remainder in Pairs.
+ llvm::erase_if(Pairs, [](const Pair &) {
+ return !DebugCounter::shouldExecute(DRPCounter);
+ });
+ for (Pair &P : Pairs) {
+ IntrinsicInst *Div = P.Div;
+ Instruction *Rem = P.Rem;
+ if (Div->getParent() == Rem->getParent() && Rem->comesBefore(Div))
+ Div->moveBefore(Rem->getIterator());
+ }
+
+ bool Changed = false;
+ SmallPtrSet<IntrinsicInst *, 8> PreparedDivs;
+ for (Pair &P : Pairs) {
+ IntrinsicInst *Div = P.Div;
+ Instruction *Rem = P.Rem;
+ IRBuilder<> Builder(Div);
+ if (PreparedDivs.insert(Div).second) {
+ auto Freeze = [&](Value *V) {
+ return isGuaranteedNotToBeUndefOrPoison(V, nullptr, Div, &DT)
+ ? V
+ : Builder.CreateFreeze(V);
+ };
+ Value *X = Freeze(Div->getArgOperand(0));
+ Value *Y = Freeze(Div->getArgOperand(1));
+ Value *Mask = Freeze(Div->getArgOperand(2));
+ // Correlate undef operands across the quotient and reconstruction. A
+ // frozen poison dividend may become MIN, so exclude exceptional operands
+ // from the new mask rather than introducing UB in the masked division.
+ Value *Safe =
+ Builder.CreateICmpNE(Y, Constant::getNullValue(Y->getType()));
+ if (Div->getIntrinsicID() == Intrinsic::masked_sdiv) {
+ unsigned Bits = X->getType()->getScalarSizeInBits();
+ Value *LHSNotMin = Builder.CreateICmpNE(
+ X, ConstantInt::get(X->getType(), APInt::getSignedMinValue(Bits)));
+ Value *RHSNotMinusOne =
+ Builder.CreateICmpNE(Y, ConstantInt::getSigned(Y->getType(), -1));
+ Value *NoOverflow = Builder.CreateOr(LHSNotMin, RHSNotMinusOne);
+ Safe = Builder.CreateAnd(Safe, NoOverflow);
+ }
+ Mask = Builder.CreateSelect(Safe, Mask,
+ Constant::getNullValue(Mask->getType()));
+ Div->setArgOperand(0, X);
+ Div->setArgOperand(1, Y);
+ Div->setArgOperand(2, Mask);
+ Div->dropPoisonGeneratingAttributes();
+ Div->dropUBImplyingAttrsAndUnknownMetadata();
+ }
+ Value *X = Div->getArgOperand(0);
+ Value *Y = Div->getArgOperand(1);
+
+ Builder.SetInsertPoint(Rem);
+ Value *Result = Builder.CreateSub(X, Builder.CreateMul(Div, Y),
+ Rem->getName() + ".decomposed");
+ Rem->replaceAllUsesWith(Result);
+ P.Rem = nullptr;
+ Rem->eraseFromParent();
+ ++NumPairs;
+ ++NumDecomposed;
+ Changed = true;
+ }
+ return Changed;
+}
+
/// Find matching pairs of integer div/rem ops (they have the same numerator,
/// denominator, and signedness). If they exist in different basic blocks, bring
/// them together by hoisting or replace the common division operation that is
@@ -180,7 +315,7 @@ static DivRemWorklistTy getWorklist(Function &F) {
/// that it doesn't quite match the stated objectives of those passes.
static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
const DominatorTree &DT) {
- bool Changed = false;
+ bool Changed = optimizeMaskedDivRem(F, TTI, DT);
// Get the matching pairs of div-rem instructions. We want this extra
// indirection to avoid dealing with having to RAUW the keys of the maps.
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 35c0049664b137..ee1e0cc944e3cb 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1431,7 +1431,8 @@ bool LoopVectorizationLegality::blockCanBePredicated(
// TODO: Allow other calls if they have appropriate attributes... readonly
// and argmemonly?
if (CallInst *CI = dyn_cast<CallInst>(&I))
- if (VFDatabase::hasMaskedVariant(*CI)) {
+ if (VFDatabase::hasMaskedVariant(*CI) ||
+ Intrinsic::getMaskedIntDivRemOpcode(CI->getIntrinsicID())) {
MaskedOp.insert(CI);
continue;
}
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 3f6fde357c93bd..b43067fa436929 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -9460,7 +9460,7 @@ static std::pair<size_t, size_t> generateKeySubkey(
hash_value(CI->getOperand(0)->getType()));
} else if (auto *Call = dyn_cast<CallInst>(I)) {
Intrinsic::ID ID = getVectorIntrinsicIDForCall(Call, TLI);
- if (isTriviallyVectorizable(ID)) {
+ if (isSLPVectorizableIntrinsic(ID)) {
if (ID == Intrinsic::fmuladd)
ID = Intrinsic::fma;
SubKey = hash_combine(hash_value(I->getOpcode()), hash_value(ID),
@@ -9975,7 +9975,7 @@ BoUpSLP::TreeEntry::EntryState BoUpSLP::getScalarsVectorizationState(
false /*HasGlobalPred*/);
Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);
- if (!VecFunc && !isTriviallyVectorizable(ID)) {
+ if (!VecFunc && !isSLPVectorizableIntrinsic(ID)) {
LLVM_DEBUG(dbgs() << "SLP: Non-vectorizable call.\n");
return TreeEntry::NeedToGather;
}
@@ -13974,7 +13974,7 @@ uint64_t BoUpSLP::getNumScalarInsts(bool HasTreeLoop) {
++Count;
if (auto *CI = dyn_cast<CallInst>(I)) {
Intrinsic::ID BaseID = getVectorIntrinsicIDForCall(CI, TLI);
- if (!isTriviallyVectorizable(BaseID))
+ if (!isSLPVectorizableIntrinsic(BaseID))
++Count;
}
}
@@ -37145,7 +37145,7 @@ static bool isNonVectorizableInst(const VectorizeOptions &Opts,
}
if (const auto *CI = dyn_cast<CallInst>(CB)) {
Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
- if (isTriviallyVectorizable(ID))
+ if (isSLPVectorizableIntrinsic(ID))
return false;
if (!VFDatabase::getMappings(*CI).empty())
return false;
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.cpp
index 17ff6a7d3f41be..043545d8de2630 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.cpp
@@ -45,6 +45,10 @@ bool isValidForAlternation(unsigned Opcode) {
return !Instruction::isIntDivRem(Opcode);
}
+bool isSLPVectorizableIntrinsic(Intrinsic::ID ID) {
+ return isTriviallyVectorizable(ID) || Intrinsic::getMaskedIntDivRemOpcode(ID);
+}
+
std::pair<Constant *, unsigned>
BinOpSameOpcodeHelper::isBinOpWithConstant(const Instruction *I) {
[[maybe_unused]] unsigned Opcode = I->getOpcode();
@@ -875,7 +879,7 @@ InstructionsState getSameOpcode(ArrayRef<Value *> VL,
if (auto *CallBase = dyn_cast<CallInst>(MainOp)) {
BaseID = getVectorIntrinsicIDForCall(CallBase, &TLI);
BaseMappings = VFDatabase(*CallBase).getMappings(*CallBase);
- if (!isTriviallyVectorizable(BaseID) && BaseMappings.empty())
+ if (!isSLPVectorizableIntrinsic(BaseID) && BaseMappings.empty())
return InstructionsState::invalid();
}
bool AnyPoison = InstCnt != VL.size();
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.h b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.h
index a5d3bc5df88ba1..e0e9b49e07c9f6 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.h
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPCompatibilityAnalysis.h
@@ -23,6 +23,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Intrinsics.h"
#include <cstdint>
#include <utility>
@@ -44,6 +45,9 @@ namespace llvm::slpvectorizer {
/// "shuffled out" lane would result in division by zero.
bool isValidForAlternation(unsigned Opcode);
+/// Returns true if \p ID may be widened as a lane-preserving SLP intrinsic.
+bool isSLPVectorizableIntrinsic(Intrinsic::ID ID);
+
/// Helper class that determines VL can use the same opcode.
/// Alternate instruction is supported. In addition, it supports interchangeable
/// instruction. An interchangeable instruction is an instruction that can be
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 877fe8ccbef995..be40df570b7e0c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1870,8 +1870,20 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
if (!Opc)
continue;
VPBuilder Builder(IntrR);
+ if (IntrR->getScalarType()->isIntegerTy(1) &&
+ (Opc == Instruction::SDiv || Opc == Instruction::SRem)) {
+ // Signed i1 masked div/rem is always 0 when defined (0 / 1 or 0 % 1);
+ // 1 is -1 in i1 and would not be a safe fallback divisor.
+ IntrR->replaceAllUsesWith(
+ Plan.getConstantInt(IntrR->getScalarType(), 0));
+ IntrR->eraseFromParent();
+ continue;
+ }
+ // A poison mask cannot make a safe operation undefined, even when
+ // narrowing a vector recipe to a single scalar operation.
+ VPValue *Mask = Builder.createFreeze(IntrR->getOperand(2));
VPValue *SafeDivisor = Builder.createSelect(
- IntrR->getOperand(2), IntrR->getOperand(1),
+ Mask, IntrR->getOperand(1),
Plan.getConstantInt(IntrR->getScalarType(), 1));
VPValue *Clone = Builder.createNaryOp(
Opc, {IntrR->getOperand(0), SafeDivisor},
@@ -6268,6 +6280,12 @@ static CallWideningDecision decideCallWidening(VPInstruction &VPI,
if (ID) {
InstructionCost IntrinsicCost =
VPWidenIntrinsicRecipe::computeCallCost(ID, Ops, VPI, VF, CostCtx);
+ if (VPI.isMasked() && Intrinsic::getMaskedIntDivRemOpcode(ID)) {
+ Type *MaskTy = toVectorTy(Type::getInt1Ty(CI->getContext()), VF);
+ IntrinsicCost += CostCtx.TTI.getCmpSelInstrCost(
+ Instruction::Select, MaskTy, MaskTy, CmpInst::BAD_ICMP_PREDICATE,
+ CostCtx.CostKind);
+ }
if (IntrinsicCost.isValid() && ScalarCost >= IntrinsicCost &&
(!VecFunc || VecCallCost >= IntrinsicCost))
return CallWideningDecision::KindTy::Intrinsic;
@@ -6311,6 +6329,12 @@ bool VPlanTransforms::makeCallWideningDecisions(VPlan &Plan, VFRange &Range,
case CallWideningDecision::KindTy::Intrinsic: {
Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, &CostCtx.TLI);
Type *ResultTy = VPI.getScalarType();
+ if (VPI.isMasked() && Intrinsic::getMaskedIntDivRemOpcode(ID)) {
+ VPBuilder Builder(&VPI);
+ // Use logical AND (select) so a disabled block also suppresses a
+ // poison intrinsic mask, rather than enabling UB in that lane.
+ Ops[2] = Builder.createLogicalAnd(VPI.getMask(), Ops[2]);
+ }
Replacement = new VPWidenIntrinsicRecipe(*CI, ID, Ops, ResultTy, VPI,
VPI, VPI.getDebugLoc());
Widened = true;
diff --git a/llvm/test/Transforms/DivRemPairs/RISCV/masked-divrem.ll b/llvm/test/Transforms/DivRemPairs/RISCV/masked-divrem.ll
new file mode 100644
index 00000000000000..6d9cd9911afbd7
--- /dev/null
+++ b/llvm/test/Transforms/DivRemPairs/RISCV/masked-divrem.ll
@@ -0,0 +1,284 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; REQUIRES: riscv-registered-target
+; RUN: opt -S -passes=div-rem-pairs -mtriple=riscv64 -mattr=+m -verify-each < %s | FileCheck %s
+
+define i32 @unsigned_pair_noundef(i32 noundef %a, i32 noundef %b, i1 noundef %m) {
+; CHECK-LABEL: define i32 @unsigned_pair_noundef(
+; CHECK-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]], i1 noundef [[M:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i1 [[M]], i1 false
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[TMP2]])
+; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[Q]], [[B]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub i32 [[A]], [[TMP3]]
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R_DECOMPOSED]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ %s = add i32 %q, %r
+ ret i32 %s
+}
+
+define i32 @signed_pair(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @signed_pair(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i32 [[A]]
+; CHECK-NEXT: [[TMP2:%.*]] = freeze i32 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[TMP2]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ne i32 [[TMP1]], -2147483648
+; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i32 [[TMP2]], -1
+; CHECK-NEXT: [[TMP7:%.*]] = or i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT: [[TMP8:%.*]] = and i1 [[TMP4]], [[TMP7]]
+; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP8]], i1 [[TMP3]], i1 false
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[TMP9]])
+; CHECK-NEXT: [[TMP10:%.*]] = mul i32 [[Q]], [[TMP2]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub i32 [[TMP1]], [[TMP10]]
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R_DECOMPOSED]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %q = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ %s = add i32 %q, %r
+ ret i32 %s
+}
+
+define i32 @different_masks(i32 %a, i32 %b, i1 %m, i1 %n) {
+; CHECK-LABEL: define i32 @different_masks(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], i1 [[N:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[A]], i32 [[B]], i1 [[N]])
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %n)
+ %s = add i32 %q, %r
+ ret i32 %s
+}
+
+define i32 @dominating_quotient(i32 %a, i32 %b, i1 %m, i1 %c) {
+; CHECK-LABEL: define i32 @dominating_quotient(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], i1 [[C:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i32 [[A]]
+; CHECK-NEXT: [[TMP2:%.*]] = freeze i32 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[TMP2]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP4]], i1 [[TMP3]], i1 false
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[TMP5]])
+; CHECK-NEXT: br i1 [[C]], label %[[REM:.*]], label %[[EXIT:.*]]
+; CHECK: [[REM]]:
+; CHECK-NEXT: [[TMP6:%.*]] = mul i32 [[Q]], [[TMP2]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub i32 [[TMP1]], [[TMP6]]
+; CHECK-NEXT: ret i32 [[R_DECOMPOSED]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret i32 [[Q]]
+;
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ br i1 %c, label %rem, label %exit
+rem:
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+exit:
+ ret i32 %q
+}
+
+define i32 @remainder_first_different_bb(i32 %a, i32 %b, i1 %m, i1 %c) {
+; CHECK-LABEL: define i32 @remainder_first_different_bb(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], i1 [[C:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: br i1 [[C]], label %[[DIV:.*]], label %[[EXIT:.*]]
+; CHECK: [[DIV]]:
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[Q]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ br i1 %c, label %div, label %exit
+div:
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %q
+exit:
+ ret i32 %r
+}
+
+define void @two_rems_before_quot_and_across_blocks(i32 %a, i32 %b, i1 %m, ptr %p) {
+; CHECK-LABEL: define void @two_rems_before_quot_and_across_blocks(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[QUOT:.*]]
+; CHECK: [[LAST:.*]]:
+; CHECK-NEXT: [[TMP0:%.*]] = mul i32 [[D:%.*]], [[TMP2:%.*]]
+; CHECK-NEXT: [[R2_DECOMPOSED:%.*]] = sub i32 [[TMP1:%.*]], [[TMP0]]
+; CHECK-NEXT: store volatile i32 [[R2_DECOMPOSED]], ptr [[P]], align 4
+; CHECK-NEXT: ret void
+; CHECK: [[QUOT]]:
+; CHECK-NEXT: [[R0:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: store volatile i32 [[R0]], ptr [[P]], align 4
+; CHECK-NEXT: [[R1:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: store volatile i32 [[R1]], ptr [[P]], align 4
+; CHECK-NEXT: [[TMP1]] = freeze i32 [[A]]
+; CHECK-NEXT: [[TMP2]] = freeze i32 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[TMP2]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ne i32 [[TMP1]], -2147483648
+; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i32 [[TMP2]], -1
+; CHECK-NEXT: [[TMP7:%.*]] = or i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT: [[TMP8:%.*]] = and i1 [[TMP4]], [[TMP7]]
+; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP8]], i1 [[TMP3]], i1 false
+; CHECK-NEXT: [[D]] = call i32 @llvm.masked.sdiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[TMP9]])
+; CHECK-NEXT: store volatile i32 [[D]], ptr [[P]], align 4
+; CHECK-NEXT: br label %[[LAST]]
+;
+entry:
+ br label %quot
+last:
+ %r2 = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ store volatile i32 %r2, ptr %p
+ ret void
+quot:
+ %r0 = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ store volatile i32 %r0, ptr %p
+ %r1 = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ store volatile i32 %r1, ptr %p
+ %d = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ store volatile i32 %d, ptr %p
+ br label %last
+}
+
+define <4 x i32> @vector_pair(<4 x i32> %a, <4 x i32> %b, <4 x i1> %m) {
+; CHECK-LABEL: define <4 x i32> @vector_pair(
+; CHECK-SAME: <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze <4 x i32> [[A]]
+; CHECK-NEXT: [[TMP2:%.*]] = freeze <4 x i32> [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze <4 x i1> [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne <4 x i32> [[TMP2]], zeroinitializer
+; CHECK-NEXT: [[TMP5:%.*]] = select <4 x i1> [[TMP4]], <4 x i1> [[TMP3]], <4 x i1> zeroinitializer
+; CHECK-NEXT: [[Q:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[TMP1]], <4 x i32> [[TMP2]], <4 x i1> [[TMP5]])
+; CHECK-NEXT: [[TMP6:%.*]] = mul <4 x i32> [[Q]], [[TMP2]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub <4 x i32> [[TMP1]], [[TMP6]]
+; CHECK-NEXT: [[S:%.*]] = add <4 x i32> [[Q]], [[R_DECOMPOSED]]
+; CHECK-NEXT: ret <4 x i32> [[S]]
+;
+ %q = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> %a, <4 x i32> %b, <4 x i1> %m)
+ %r = call <4 x i32> @llvm.masked.urem.v4i32(<4 x i32> %a, <4 x i32> %b, <4 x i1> %m)
+ %s = add <4 x i32> %q, %r
+ ret <4 x i32> %s
+}
+
+define i32 @poison_dividend(i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @poison_dividend(
+; CHECK-SAME: i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i32 poison
+; CHECK-NEXT: [[TMP2:%.*]] = freeze i32 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[TMP2]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ne i32 [[TMP1]], -2147483648
+; CHECK-NEXT: [[TMP6:%.*]] = icmp ne i32 [[TMP2]], -1
+; CHECK-NEXT: [[TMP7:%.*]] = or i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT: [[TMP8:%.*]] = and i1 [[TMP4]], [[TMP7]]
+; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP8]], i1 [[TMP3]], i1 false
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[TMP9]])
+; CHECK-NEXT: [[TMP10:%.*]] = mul i32 [[Q]], [[TMP2]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub i32 [[TMP1]], [[TMP10]]
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R_DECOMPOSED]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %q = call i32 @llvm.masked.sdiv.i32(i32 poison, i32 %b, i1 %m)
+ %r = call i32 @llvm.masked.srem.i32(i32 poison, i32 %b, i1 %m)
+ %s = add i32 %q, %r
+ ret i32 %s
+}
+
+declare void @may_not_return()
+
+define i32 @no_hoist_across_may_not_return(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @no_hoist_across_may_not_return(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[A]], i32 [[B]], i1 false)
+; CHECK-NEXT: call void @may_not_return()
+; CHECK-NEXT: [[Q:%.*]] = call noundef i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 false)
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 false)
+ call void @may_not_return()
+ %q = call noundef i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 false)
+ %s = add i32 %q, %r
+ ret i32 %s
+}
+
+define i32 @same_block_hoist_div(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @same_block_hoist_div(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i32 [[A]]
+; CHECK-NEXT: [[TMP2:%.*]] = freeze i32 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[TMP2]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP4]], i1 [[TMP3]], i1 false
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[TMP5]])
+; CHECK-NEXT: [[TMP6:%.*]] = mul i32 [[Q]], [[TMP2]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub i32 [[TMP1]], [[TMP6]]
+; CHECK-NEXT: [[INTERVENING:%.*]] = add i32 [[R_DECOMPOSED]], 1
+; CHECK-NEXT: [[S:%.*]] = add i32 [[INTERVENING]], [[Q]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ %intervening = add i32 %r, 1
+ %q = call noundef range(i32 0, 100) i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ %s = add i32 %intervening, %q
+ ret i32 %s
+}
+
+define i32 @rem_used_by_another_masked_div(i32 %a, i32 %b, i32 %c, i1 %m) {
+; CHECK-LABEL: define i32 @rem_used_by_another_masked_div(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i32 [[A]]
+; CHECK-NEXT: [[TMP2:%.*]] = freeze i32 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = freeze i1 [[M]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32 [[TMP2]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP4]], i1 [[TMP3]], i1 false
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[TMP5]])
+; CHECK-NEXT: [[TMP6:%.*]] = mul i32 [[Q]], [[TMP2]]
+; CHECK-NEXT: [[R_DECOMPOSED:%.*]] = sub i32 [[TMP1]], [[TMP6]]
+; CHECK-NEXT: [[Q2:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[R_DECOMPOSED]], i32 [[C]], i1 [[M]])
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[Q2]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ %q2 = call i32 @llvm.masked.udiv.i32(i32 %r, i32 %c, i1 %m)
+ %s = add i32 %q, %q2
+ ret i32 %s
+}
+
+define i32 @operand_bundle_not_paired(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @operand_bundle_not_paired(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: [[TAG:%.*]] = add i32 [[A]], 1
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]]) [ "unknown"(i32 [[TAG]]) ]
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ %tag = add i32 %a, 1
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m) [ "unknown"(i32 %tag) ]
+ %s = add i32 %q, %r
+ ret i32 %s
+}
+
+define i32 @musttail_not_paired(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @musttail_not_paired(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: store volatile i32 [[R]], ptr null, align 4
+; CHECK-NEXT: [[Q:%.*]] = musttail call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[Q]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ store volatile i32 %r, ptr null, align 4
+ %q = musttail call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %q
+}
diff --git a/llvm/test/Transforms/DivRemPairs/X86/masked-divrem.ll b/llvm/test/Transforms/DivRemPairs/X86/masked-divrem.ll
new file mode 100644
index 00000000000000..de93b86610f928
--- /dev/null
+++ b/llvm/test/Transforms/DivRemPairs/X86/masked-divrem.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=div-rem-pairs -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
+
+define i32 @has_divrem_op(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @has_divrem_op(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; CHECK-NEXT: [[S:%.*]] = add i32 [[Q]], [[R]]
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %q = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ %s = add i32 %q, %r
+ ret i32 %s
+}
diff --git a/llvm/test/Transforms/InstCombine/masked-divrem-demanded.ll b/llvm/test/Transforms/InstCombine/masked-divrem-demanded.ll
new file mode 100644
index 00000000000000..ce1aaa58580e38
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/masked-divrem-demanded.ll
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+; Demanded-elements simplification must not turn a false mask lane into poison
+; when the inactive lane's divisor may be zero.
+
+define i32 @sdiv_demanded_lane0(i32 %a) {
+; CHECK-LABEL: define i32 @sdiv_demanded_lane0(
+; CHECK-SAME: i32 [[A:%.*]]) {
+; CHECK-NEXT: [[X:%.*]] = insertelement <2 x i32> <i32 poison, i32 7>, i32 [[A]], i64 0
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: [[E:%.*]] = extractelement <2 x i32> [[R]], i64 0
+; CHECK-NEXT: ret i32 [[E]]
+;
+ %x = insertelement <2 x i32> <i32 poison, i32 7>, i32 %a, i64 0
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+ %e = extractelement <2 x i32> %r, i64 0
+ ret i32 %e
+}
+
+define i32 @udiv_demanded_lane0(<2 x i32> %x) {
+; CHECK-LABEL: define i32 @udiv_demanded_lane0(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: [[E:%.*]] = extractelement <2 x i32> [[R]], i64 0
+; CHECK-NEXT: ret i32 [[E]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+ %e = extractelement <2 x i32> %r, i64 0
+ ret i32 %e
+}
+
+define i32 @variable_divisor(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @variable_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: [[X:%.*]] = insertelement <2 x i32> <i32 poison, i32 7>, i32 [[A]], i64 0
+; CHECK-NEXT: [[Y:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[B]], i64 0
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]], <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: [[E:%.*]] = extractelement <2 x i32> [[R]], i64 0
+; CHECK-NEXT: ret i32 [[E]]
+;
+ %x = insertelement <2 x i32> <i32 poison, i32 7>, i32 %a, i64 0
+ %y = insertelement <2 x i32> <i32 poison, i32 0>, i32 %b, i64 0
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> <i1 true, i1 false>)
+ %e = extractelement <2 x i32> %r, i64 0
+ ret i32 %e
+}
+
+define i32 @urem(<2 x i32> %x) {
+; CHECK-LABEL: define i32 @urem(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: [[E:%.*]] = extractelement <2 x i32> [[R]], i64 0
+; CHECK-NEXT: ret i32 [[E]]
+;
+ %r = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+ %e = extractelement <2 x i32> %r, i64 0
+ ret i32 %e
+}
+
+define i32 @srem(<2 x i32> %x) {
+; CHECK-LABEL: define i32 @srem(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.srem.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: [[E:%.*]] = extractelement <2 x i32> [[R]], i64 0
+; CHECK-NEXT: ret i32 [[E]]
+;
+ %r = call <2 x i32> @llvm.masked.srem.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 0>, <2 x i1> <i1 true, i1 false>)
+ %e = extractelement <2 x i32> %r, i64 0
+ ret i32 %e
+}
diff --git a/llvm/test/Transforms/InstCombine/masked-divrem-knownbits.ll b/llvm/test/Transforms/InstCombine/masked-divrem-knownbits.ll
new file mode 100644
index 00000000000000..944348944d1673
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/masked-divrem-knownbits.ll
@@ -0,0 +1,132 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+
+define i32 @srem_roundtrip(i8 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i32 @srem_roundtrip(
+; CHECK-SAME: i8 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[EXT:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[EXT]], i32 [[Y]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %ext = sext i8 %x to i32
+ %r = call i32 @llvm.masked.srem.i32(i32 %ext, i32 %y, i1 %m)
+ %trunc = trunc i32 %r to i8
+ %sext = sext i8 %trunc to i32
+ ret i32 %sext
+}
+
+define i32 @srem_roundtrip_negative(i10 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i32 @srem_roundtrip_negative(
+; CHECK-SAME: i10 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[EXT:%.*]] = sext i10 [[X]] to i32
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[EXT]], i32 [[Y]], i1 [[M]])
+; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[R]], 24
+; CHECK-NEXT: [[SEXT:%.*]] = ashr exact i32 [[TMP1]], 24
+; CHECK-NEXT: ret i32 [[SEXT]]
+;
+ %ext = sext i10 %x to i32
+ %r = call i32 @llvm.masked.srem.i32(i32 %ext, i32 %y, i1 %m)
+ %trunc = trunc i32 %r to i8
+ %sext = sext i8 %trunc to i32
+ ret i32 %sext
+}
+
+define i1 @udiv_bound(i32 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @udiv_bound(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %masked = and i32 %x, 15
+ %r = call i32 @llvm.masked.udiv.i32(i32 %masked, i32 %y, i1 %m)
+ %cmp = icmp ult i32 %r, 16
+ ret i1 %cmp
+}
+
+define i1 @udiv_bound_negative(i32 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @udiv_bound_negative(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[X]], 31
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[MASKED]], i32 [[Y]], i1 [[M]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i32 [[R]], 16
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %masked = and i32 %x, 31
+ %r = call i32 @llvm.masked.udiv.i32(i32 %masked, i32 %y, i1 %m)
+ %cmp = icmp ult i32 %r, 16
+ ret i1 %cmp
+}
+
+define i1 @urem_bound(i32 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @urem_bound(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %masked = and i32 %y, 7
+ %r = call i32 @llvm.masked.urem.i32(i32 %x, i32 %masked, i1 %m)
+ %cmp = icmp ult i32 %r, 8
+ ret i1 %cmp
+}
+
+define i1 @urem_bound_negative(i32 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @urem_bound_negative(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[MASKED:%.*]] = and i32 [[Y]], 15
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[X]], i32 [[MASKED]], i1 [[M]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i32 [[R]], 8
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %masked = and i32 %y, 15
+ %r = call i32 @llvm.masked.urem.i32(i32 %x, i32 %masked, i1 %m)
+ %cmp = icmp ult i32 %r, 8
+ ret i1 %cmp
+}
+
+define <2 x i1> @udiv_vector_bound(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
+;
+; CHECK-LABEL: define <2 x i1> @udiv_vector_bound(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]], <2 x i1> [[M:%.*]]) {
+; CHECK-NEXT: ret <2 x i1> splat (i1 true)
+;
+ %masked = and <2 x i32> %x, <i32 15, i32 7>
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %masked, <2 x i32> %y, <2 x i1> %m)
+ %cmp = icmp ult <2 x i32> %r, <i32 16, i32 16>
+ ret <2 x i1> %cmp
+}
+
+define <2 x i1> @udiv_vector_bound_negative(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
+;
+; CHECK-LABEL: define <2 x i1> @udiv_vector_bound_negative(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]], <2 x i1> [[M:%.*]]) {
+; CHECK-NEXT: [[MASKED:%.*]] = and <2 x i32> [[X]], <i32 15, i32 31>
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[MASKED]], <2 x i32> [[Y]], <2 x i1> [[M]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult <2 x i32> [[R]], splat (i32 16)
+; CHECK-NEXT: ret <2 x i1> [[CMP]]
+;
+ %masked = and <2 x i32> %x, <i32 15, i32 31>
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %masked, <2 x i32> %y, <2 x i1> %m)
+ %cmp = icmp ult <2 x i32> %r, <i32 16, i32 16>
+ ret <2 x i1> %cmp
+}
+
+define i1 @frozen_bound(i32 %x, i32 %y, i1 %m) {
+; CHECK-LABEL: define i1 @frozen_bound(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[A:%.*]] = and i32 [[X]], 15
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[Y]], i1 [[M]])
+; CHECK-NEXT: [[F:%.*]] = freeze i32 [[R]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[F]], 16
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %a = and i32 %x, 15
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %y, i1 %m)
+ %f = freeze i32 %r
+ %cmp = icmp ult i32 %f, 16
+ ret i1 %cmp
+}
diff --git a/llvm/test/Transforms/InstCombine/masked-divrem.ll b/llvm/test/Transforms/InstCombine/masked-divrem.ll
new file mode 100644
index 00000000000000..90bb46b6d84a36
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/masked-divrem.ll
@@ -0,0 +1,395 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i32 @udiv_enabled(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_enabled(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = udiv i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 true)
+ ret i32 %r
+}
+
+define i32 @udiv_safe_constant(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_safe_constant(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = udiv i32 [[A]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 3, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_minus_one(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_minus_one(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[A]], -1
+; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_nonzero_nonpoison(i32 %a, i32 noundef %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_nonzero_nonpoison(
+; CHECK-SAME: i32 [[A:%.*]], i32 noundef [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[D:%.*]] = or i32 [[B]], 1
+; CHECK-NEXT: [[R:%.*]] = udiv i32 [[A]], [[D]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %d = or i32 %b, 1
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %d, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_nonzero_possibly_poison(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_nonzero_possibly_poison(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[D:%.*]] = or i32 [[B]], 1
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[D]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %d = or i32 %b, 1
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %d, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_not_min_nonpoison(i32 noundef %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_not_min_nonpoison(
+; CHECK-SAME: i32 noundef [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[A]], -3
+; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %x = or i32 %a, 1
+ %r = call i32 @llvm.masked.udiv.i32(i32 %x, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_not_min_possibly_poison(i32 %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_not_min_possibly_poison(
+; CHECK-SAME: i32 [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[A]], -3
+; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %x = or i32 %a, 1
+ %r = call i32 @llvm.masked.udiv.i32(i32 %x, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_freeze_nonpoison(i32 noundef %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_freeze_nonpoison(
+; CHECK-SAME: i32 noundef [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = udiv i32 [[A]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 3, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define i32 @udiv_freeze_possibly_poison(i32 %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_freeze_possibly_poison(
+; CHECK-SAME: i32 [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[A_FR:%.*]] = freeze i32 [[A]]
+; CHECK-NEXT: [[R:%.*]] = udiv i32 [[A_FR]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 3, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define <2 x i32> @udiv_safe_vector(<2 x i32> %a, <2 x i1> %m) {
+;
+; CHECK-LABEL: define <2 x i32> @udiv_safe_vector(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x i1> [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = udiv <2 x i32> [[A]], <i32 2, i32 3>
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> <i32 2, i32 3>, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @udiv_unsafe_vector(<2 x i32> %a) {
+;
+; CHECK-LABEL: define <2 x i32> @udiv_unsafe_vector(
+; CHECK-SAME: <2 x i32> [[A:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[A]], <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+; CHECK-NEXT: [[F:%.*]] = freeze <2 x i32> [[R]]
+; CHECK-NEXT: ret <2 x i32> [[F]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+ %f = freeze <2 x i32> %r
+ ret <2 x i32> %f
+}
+
+define i32 @sdiv_enabled(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_enabled(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 true)
+ ret i32 %r
+}
+
+define i32 @sdiv_safe_constant(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_safe_constant(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 3, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_minus_one(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_minus_one(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 -1, i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_nonzero_nonpoison(i32 %a, i32 noundef %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_nonzero_nonpoison(
+; CHECK-SAME: i32 [[A:%.*]], i32 noundef [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[D:%.*]] = or i32 [[B]], 1
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[D]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %d = or i32 %b, 1
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %d, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_nonzero_possibly_poison(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_nonzero_possibly_poison(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[D:%.*]] = or i32 [[B]], 1
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[D]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %d = or i32 %b, 1
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %d, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_not_min_nonpoison(i32 noundef %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_not_min_nonpoison(
+; CHECK-SAME: i32 noundef [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[X:%.*]] = or i32 [[A]], 1
+; CHECK-NEXT: [[R:%.*]] = sub nsw i32 0, [[X]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %x = or i32 %a, 1
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %x, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_not_min_possibly_poison(i32 %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_not_min_possibly_poison(
+; CHECK-SAME: i32 [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[X:%.*]] = or i32 [[A]], 1
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[X]], i32 -1, i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %x = or i32 %a, 1
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %x, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_freeze_nonpoison(i32 noundef %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_freeze_nonpoison(
+; CHECK-SAME: i32 noundef [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 3, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define i32 @sdiv_freeze_possibly_poison(i32 %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_freeze_possibly_poison(
+; CHECK-SAME: i32 [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[A_FR:%.*]] = freeze i32 [[A]]
+; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A_FR]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 3, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define <2 x i32> @sdiv_safe_vector(<2 x i32> %a, <2 x i1> %m) {
+;
+; CHECK-LABEL: define <2 x i32> @sdiv_safe_vector(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x i1> [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = sdiv <2 x i32> [[A]], <i32 2, i32 3>
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> <i32 2, i32 3>, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @sdiv_unsafe_vector(<2 x i32> %a) {
+;
+; CHECK-LABEL: define <2 x i32> @sdiv_unsafe_vector(
+; CHECK-SAME: <2 x i32> [[A:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[A]], <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+; CHECK-NEXT: [[F:%.*]] = freeze <2 x i32> [[R]]
+; CHECK-NEXT: ret <2 x i32> [[F]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+ %f = freeze <2 x i32> %r
+ ret <2 x i32> %f
+}
+
+define i32 @urem_safe_constant(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @urem_safe_constant(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = urem i32 [[A]], 3
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 3, i1 %m)
+ ret i32 %r
+}
+
+define i32 @srem_not_min_nonpoison(i32 noundef %a, i1 %m) {
+;
+; CHECK-LABEL: define i32 @srem_not_min_nonpoison(
+; CHECK-SAME: i32 noundef [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 0
+;
+ %x = or i32 %a, 1
+ %r = call i32 @llvm.masked.srem.i32(i32 %x, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define <2 x i32> @udiv_disabled_poison_divisor(<2 x i32> %x) {
+;
+; CHECK-LABEL: define <2 x i32> @udiv_disabled_poison_divisor(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+; Disabled mask lanes are not proof that every result lane is enabled;
+; m_AllOnesForbidPoison() also conservatively rejects poison mask lanes.
+
+define <2 x i32> @udiv_partial_mask(<2 x i32> %x, <2 x i32> %y) {
+; CHECK-LABEL: define <2 x i32> @udiv_partial_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]], <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @udiv_partial_poison_mask(<2 x i32> %x, <2 x i32> %y) {
+; CHECK-LABEL: define <2 x i32> @udiv_partial_poison_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]], <2 x i1> <i1 true, i1 poison>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> <i1 true, i1 poison>)
+ ret <2 x i32> %r
+}
+
+; A disabled poison divisor must not discard the defined neighboring lane.
+
+define <2 x i32> @sdiv_disabled_poison_divisor(<2 x i32> %x) {
+;
+; CHECK-LABEL: define <2 x i32> @sdiv_disabled_poison_divisor(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+; Disabled mask lanes are not proof that every result lane is enabled;
+; m_AllOnesForbidPoison() also conservatively rejects poison mask lanes.
+
+define <2 x i32> @sdiv_partial_mask(<2 x i32> %x, <2 x i32> %y) {
+; CHECK-LABEL: define <2 x i32> @sdiv_partial_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]], <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @sdiv_partial_poison_mask(<2 x i32> %x, <2 x i32> %y) {
+; CHECK-LABEL: define <2 x i32> @sdiv_partial_poison_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]], <2 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> [[Y]], <2 x i1> <i1 true, i1 poison>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> <i1 true, i1 poison>)
+ ret <2 x i32> %r
+}
+
+define i32 @sdiv_nonneg_dividend(i32 noundef %a, i1 %m) {
+; CHECK-LABEL: define i32 @sdiv_nonneg_dividend(
+; CHECK-SAME: i32 noundef [[A:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[X:%.*]] = lshr i32 [[A]], 1
+; CHECK-NEXT: [[R:%.*]] = sub nsw i32 0, [[X]]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %x = lshr i32 %a, 1
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %x, i32 -1, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_operand_bundle(i32 %x, ptr %p) {
+; CHECK-LABEL: define i32 @udiv_operand_bundle(
+; CHECK-SAME: i32 [[X:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[X]], i32 3, i1 true) [ "unknown"(ptr [[P]]) ]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %x, i32 3, i1 true) [ "unknown"(ptr %p) ]
+ ret i32 %r
+}
+
+define i32 @udiv_musttail(i32 %x, i32 %y, i1 %m) {
+; CHECK-LABEL: define i32 @udiv_musttail(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = musttail call i32 @llvm.masked.udiv.i32(i32 [[X]], i32 3, i1 true)
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = musttail call i32 @llvm.masked.udiv.i32(i32 %x, i32 3, i1 true)
+ ret i32 %r
+}
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/masked-divrem.ll b/llvm/test/Transforms/InstSimplify/ConstProp/masked-divrem.ll
new file mode 100644
index 00000000000000..4b2f7e5b9c8f09
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/masked-divrem.ll
@@ -0,0 +1,215 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=instsimplify -S < %s | FileCheck %s
+
+define i32 @udiv_valid() {
+;
+; CHECK-LABEL: define i32 @udiv_valid() {
+; CHECK-NEXT: ret i32 4
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 13, i32 3, i1 true)
+ ret i32 %r
+}
+
+define i32 @udiv_disabled_zero() {
+;
+; CHECK-LABEL: define i32 @udiv_disabled_zero() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 13, i32 0, i1 false)
+ ret i32 %r
+}
+
+define i32 @udiv_enabled_zero() {
+;
+; CHECK-LABEL: define i32 @udiv_enabled_zero() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 13, i32 0, i1 true)
+ ret i32 %r
+}
+
+define i32 @udiv_poison_mask() {
+;
+; CHECK-LABEL: define i32 @udiv_poison_mask() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 13, i32 3, i1 poison)
+ ret i32 %r
+}
+
+define i32 @udiv_large() {
+; CHECK-LABEL: define i32 @udiv_large() {
+; CHECK-NEXT: ret i32 0
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 -2147483648, i32 -1, i1 true)
+ ret i32 %r
+}
+
+define i1 @udiv_i1() {
+;
+; CHECK-LABEL: define i1 @udiv_i1() {
+; CHECK-NEXT: ret i1 true
+;
+ %r = call i1 @llvm.masked.udiv.i1(i1 true, i1 true, i1 true)
+ ret i1 %r
+}
+
+define i128 @udiv_wide() {
+;
+; CHECK-LABEL: define i128 @udiv_wide() {
+; CHECK-NEXT: ret i128 56713727820156410577229101238628035242
+;
+ %r = call i128 @llvm.masked.udiv.i128(i128 170141183460469231731687303715884105727, i128 3, i1 true)
+ ret i128 %r
+}
+
+define <4 x i32> @udiv_vector_mixed() {
+;
+; CHECK-LABEL: define <4 x i32> @udiv_vector_mixed() {
+; CHECK-NEXT: ret <4 x i32> <i32 poison, i32 4, i32 poison, i32 poison>
+;
+ %r = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> <i32 5, i32 8, i32 7, i32 -2147483648>, <4 x i32> <i32 0, i32 2, i32 2, i32 -1>, <4 x i1> <i1 false, i1 true, i1 poison, i1 false>)
+ ret <4 x i32> %r
+}
+
+define <4 x i32> @udiv_vector_enabled() {
+;
+; CHECK-LABEL: define <4 x i32> @udiv_vector_enabled() {
+; CHECK-NEXT: ret <4 x i32> <i32 poison, i32 4, i32 3, i32 0>
+;
+ %r = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> <i32 5, i32 8, i32 7, i32 -2147483648>, <4 x i32> <i32 0, i32 2, i32 2, i32 -1>, <4 x i1> <i1 true, i1 true, i1 true, i1 true>)
+ ret <4 x i32> %r
+}
+
+define <vscale x 2 x i32> @udiv_scalable() {
+;
+; CHECK-LABEL: define <vscale x 2 x i32> @udiv_scalable() {
+; CHECK-NEXT: ret <vscale x 2 x i32> splat (i32 4)
+;
+ %r = call <vscale x 2 x i32> @llvm.masked.udiv.nxv2i32(<vscale x 2 x i32> splat (i32 8), <vscale x 2 x i32> splat (i32 2), <vscale x 2 x i1> splat (i1 true))
+ ret <vscale x 2 x i32> %r
+}
+
+define i32 @sdiv_valid() {
+;
+; CHECK-LABEL: define i32 @sdiv_valid() {
+; CHECK-NEXT: ret i32 4
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 13, i32 3, i1 true)
+ ret i32 %r
+}
+
+define i32 @sdiv_disabled_zero() {
+;
+; CHECK-LABEL: define i32 @sdiv_disabled_zero() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 13, i32 0, i1 false)
+ ret i32 %r
+}
+
+define i32 @sdiv_enabled_zero() {
+;
+; CHECK-LABEL: define i32 @sdiv_enabled_zero() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 13, i32 0, i1 true)
+ ret i32 %r
+}
+
+define i32 @sdiv_poison_mask() {
+;
+; CHECK-LABEL: define i32 @sdiv_poison_mask() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 13, i32 3, i1 poison)
+ ret i32 %r
+}
+
+define i32 @sdiv_overflow() {
+;
+; CHECK-LABEL: define i32 @sdiv_overflow() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 -2147483648, i32 -1, i1 true)
+ ret i32 %r
+}
+
+define i1 @sdiv_i1() {
+;
+; CHECK-LABEL: define i1 @sdiv_i1() {
+; CHECK-NEXT: ret i1 true
+;
+ %r = call i1 @llvm.masked.sdiv.i1(i1 true, i1 true, i1 true)
+ ret i1 %r
+}
+
+define i128 @sdiv_wide() {
+;
+; CHECK-LABEL: define i128 @sdiv_wide() {
+; CHECK-NEXT: ret i128 56713727820156410577229101238628035242
+;
+ %r = call i128 @llvm.masked.sdiv.i128(i128 170141183460469231731687303715884105727, i128 3, i1 true)
+ ret i128 %r
+}
+
+define <4 x i32> @sdiv_vector_mixed() {
+;
+; CHECK-LABEL: define <4 x i32> @sdiv_vector_mixed() {
+; CHECK-NEXT: ret <4 x i32> <i32 poison, i32 4, i32 poison, i32 poison>
+;
+ %r = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> <i32 5, i32 8, i32 7, i32 -2147483648>, <4 x i32> <i32 0, i32 2, i32 2, i32 -1>, <4 x i1> <i1 false, i1 true, i1 poison, i1 false>)
+ ret <4 x i32> %r
+}
+
+define <4 x i32> @sdiv_vector_enabled() {
+;
+; CHECK-LABEL: define <4 x i32> @sdiv_vector_enabled() {
+; CHECK-NEXT: ret <4 x i32> <i32 poison, i32 4, i32 3, i32 poison>
+;
+ %r = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> <i32 5, i32 8, i32 7, i32 -2147483648>, <4 x i32> <i32 0, i32 2, i32 2, i32 -1>, <4 x i1> <i1 true, i1 true, i1 true, i1 true>)
+ ret <4 x i32> %r
+}
+
+define <vscale x 2 x i32> @sdiv_scalable() {
+;
+; CHECK-LABEL: define <vscale x 2 x i32> @sdiv_scalable() {
+; CHECK-NEXT: ret <vscale x 2 x i32> splat (i32 4)
+;
+ %r = call <vscale x 2 x i32> @llvm.masked.sdiv.nxv2i32(<vscale x 2 x i32> splat (i32 8), <vscale x 2 x i32> splat (i32 2), <vscale x 2 x i1> splat (i1 true))
+ ret <vscale x 2 x i32> %r
+}
+
+define i32 @urem_valid() {
+;
+; CHECK-LABEL: define i32 @urem_valid() {
+; CHECK-NEXT: ret i32 1
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 13, i32 3, i1 true)
+ ret i32 %r
+}
+
+define i32 @srem_valid() {
+;
+; CHECK-LABEL: define i32 @srem_valid() {
+; CHECK-NEXT: ret i32 1
+;
+ %r = call i32 @llvm.masked.srem.i32(i32 13, i32 3, i1 true)
+ ret i32 %r
+}
+
+define i32 @srem_overflow() {
+; CHECK-LABEL: define i32 @srem_overflow() {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.srem.i32(i32 -2147483648, i32 -1, i1 true)
+ ret i32 %r
+}
+
+define <vscale x 2 x i32> @udiv_scalable_enabled_zero() {
+; CHECK-LABEL: define <vscale x 2 x i32> @udiv_scalable_enabled_zero() {
+; CHECK-NEXT: ret <vscale x 2 x i32> poison
+;
+ %r = call <vscale x 2 x i32> @llvm.masked.udiv.nxv2i32(<vscale x 2 x i32> splat (i32 8), <vscale x 2 x i32> zeroinitializer, <vscale x 2 x i1> splat (i1 true))
+ ret <vscale x 2 x i32> %r
+}
diff --git a/llvm/test/Transforms/InstSimplify/masked-divrem-knownbits.ll b/llvm/test/Transforms/InstSimplify/masked-divrem-knownbits.ll
new file mode 100644
index 00000000000000..13d4b40c422452
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/masked-divrem-knownbits.ll
@@ -0,0 +1,138 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+
+define i1 @sdiv_sign(i8 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @sdiv_sign(
+; CHECK-SAME: i8 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %masked = and i8 %x, 15
+ %negative = or i8 %masked, -128
+ %ext = sext i8 %negative to i32
+ %divisor = and i32 %y, 15
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %ext, i32 %divisor, i1 %m)
+ %cmp = icmp slt i32 %r, 0
+ ret i1 %cmp
+}
+
+define i1 @sdiv_sign_negative(i8 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @sdiv_sign_negative(
+; CHECK-SAME: i8 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[MASKED:%.*]] = and i8 [[X]], 15
+; CHECK-NEXT: [[NEGATIVE:%.*]] = or i8 [[MASKED]], -128
+; CHECK-NEXT: [[EXT:%.*]] = sext i8 [[NEGATIVE]] to i32
+; CHECK-NEXT: [[DIVISOR:%.*]] = and i32 [[Y]], 255
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[EXT]], i32 [[DIVISOR]], i1 [[M]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[R]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %masked = and i8 %x, 15
+ %negative = or i8 %masked, -128
+ %ext = sext i8 %negative to i32
+ %divisor = and i32 %y, 255
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %ext, i32 %divisor, i1 %m)
+ %cmp = icmp slt i32 %r, 0
+ ret i1 %cmp
+}
+
+define i1 @srem_sign(i8 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @srem_sign(
+; CHECK-SAME: i8 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %ext = sext i8 %x to i32
+ %nonnegative = and i32 %ext, 127
+ %divisor = and i32 %y, 15
+ %r = call i32 @llvm.masked.srem.i32(i32 %nonnegative, i32 %divisor, i1 %m)
+ %cmp = icmp sge i32 %r, 0
+ ret i1 %cmp
+}
+
+define i1 @srem_sign_negative(i8 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i1 @srem_sign_negative(
+; CHECK-SAME: i8 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[EXT:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT: [[DIVISOR:%.*]] = and i32 [[Y]], 15
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[EXT]], i32 [[DIVISOR]], i1 [[M]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[R]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %ext = sext i8 %x to i32
+ %divisor = and i32 %y, 15
+ %r = call i32 @llvm.masked.srem.i32(i32 %ext, i32 %divisor, i1 %m)
+ %cmp = icmp sge i32 %r, 0
+ ret i1 %cmp
+}
+
+define i32 @sdiv_sign_bits(i16 %x, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_sign_bits(
+; CHECK-SAME: i16 [[X:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 0
+;
+ %e = sext i16 %x to i32
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %e, i32 65536, i1 %m)
+ %s = ashr i32 %r, 31
+ ret i32 %s
+}
+
+define i32 @sdiv_sign_bits_negative(i16 %x, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_sign_bits_negative(
+; CHECK-SAME: i16 [[X:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[E:%.*]] = sext i16 [[X]] to i32
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[E]], i32 32767, i1 [[M]])
+; CHECK-NEXT: [[S:%.*]] = ashr i32 [[R]], 31
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %e = sext i16 %x to i32
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %e, i32 32767, i1 %m)
+ %s = ashr i32 %r, 31
+ ret i32 %s
+}
+
+define i32 @sdiv_sign_bits_negative_divisor(i16 %x, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_sign_bits_negative_divisor(
+; CHECK-SAME: i16 [[X:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 0
+;
+ %e = sext i16 %x to i32
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %e, i32 -65536, i1 %m)
+ %s = ashr i32 %r, 31
+ ret i32 %s
+}
+
+define i32 @srem_sign_bits(i1 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i32 @srem_sign_bits(
+; CHECK-SAME: i1 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[E:%.*]] = sext i1 [[X]] to i32
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[E]], i32 [[Y]], i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %e = sext i1 %x to i32
+ %r = call i32 @llvm.masked.srem.i32(i32 %e, i32 %y, i1 %m)
+ %s = ashr i32 %r, 31
+ ret i32 %s
+}
+
+define i32 @srem_sign_bits_negative(i2 %x, i32 %y, i1 %m) {
+;
+; CHECK-LABEL: define i32 @srem_sign_bits_negative(
+; CHECK-SAME: i2 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: [[E:%.*]] = sext i2 [[X]] to i32
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[E]], i32 [[Y]], i1 [[M]])
+; CHECK-NEXT: [[S:%.*]] = ashr i32 [[R]], 31
+; CHECK-NEXT: ret i32 [[S]]
+;
+ %e = sext i2 %x to i32
+ %r = call i32 @llvm.masked.srem.i32(i32 %e, i32 %y, i1 %m)
+ %s = ashr i32 %r, 31
+ ret i32 %s
+}
diff --git a/llvm/test/Transforms/InstSimplify/masked-divrem.ll b/llvm/test/Transforms/InstSimplify/masked-divrem.ll
new file mode 100644
index 00000000000000..5807f026210079
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/masked-divrem.ll
@@ -0,0 +1,255 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=instsimplify -S < %s | FileCheck %s
+
+define i32 @udiv_disabled(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_disabled(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 false)
+ ret i32 %r
+}
+
+define i32 @udiv_poison_mask(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_poison_mask(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 poison)
+ ret i32 %r
+}
+
+define i32 @udiv_poison_dividend(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_poison_dividend(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 poison, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_poison_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_poison_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 poison, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_zero_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_zero_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 0, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_one_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @udiv_one_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 [[A]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 1, i1 %m)
+ ret i32 %r
+}
+
+define <2 x i32> @udiv_disabled_invalid_lane(<2 x i32> %a) {
+;
+; CHECK-LABEL: define <2 x i32> @udiv_disabled_invalid_lane(
+; CHECK-SAME: <2 x i32> [[A:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[A]], <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+ ret <2 x i32> %r
+}
+
+define i32 @sdiv_disabled(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_disabled(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 false)
+ ret i32 %r
+}
+
+define i32 @sdiv_poison_mask(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_poison_mask(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 poison)
+ ret i32 %r
+}
+
+define i32 @sdiv_poison_dividend(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_poison_dividend(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 poison, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_poison_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_poison_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 poison, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_zero_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_zero_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 poison
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 0, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_one_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @sdiv_one_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 [[A]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 1, i1 %m)
+ ret i32 %r
+}
+
+define <2 x i32> @sdiv_disabled_invalid_lane(<2 x i32> %a) {
+;
+; CHECK-LABEL: define <2 x i32> @sdiv_disabled_invalid_lane(
+; CHECK-SAME: <2 x i32> [[A:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[A]], <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> <i32 0, i32 2>, <2 x i1> <i1 false, i1 true>)
+ ret <2 x i32> %r
+}
+
+define i32 @urem_one_divisor(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i32 @urem_one_divisor(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i32 0
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 1, i1 %m)
+ ret i32 %r
+}
+
+define i1 @sdiv_signed_i1(i1 %a, i1 %b, i1 %m) {
+;
+; CHECK-LABEL: define i1 @sdiv_signed_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 false
+;
+ %r = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i1 @srem_signed_i1(i1 %a, i1 %b, i1 %m) {
+;
+; CHECK-LABEL: define i1 @srem_signed_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]]) {
+; CHECK-NEXT: ret i1 false
+;
+ %r = call i1 @llvm.masked.srem.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+; A disabled poison divisor must not discard the defined neighboring lane.
+
+define <2 x i32> @udiv_disabled_poison_divisor(<2 x i32> %x) {
+;
+; CHECK-LABEL: define <2 x i32> @udiv_disabled_poison_divisor(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+; Disabled mask lanes are not proof that every result lane is enabled;
+; m_AllOnesForbidPoison() also conservatively rejects poison mask lanes.
+
+define <2 x i32> @udiv_partial_mask(<2 x i32> %x) {
+; CHECK-LABEL: define <2 x i32> @udiv_partial_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @udiv_partial_poison_mask(<2 x i32> %x) {
+; CHECK-LABEL: define <2 x i32> @udiv_partial_poison_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[X]], <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 poison>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %x, <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 poison>)
+ ret <2 x i32> %r
+}
+
+; A disabled poison divisor must not discard the defined neighboring lane.
+
+define <2 x i32> @sdiv_disabled_poison_divisor(<2 x i32> %x) {
+;
+; CHECK-LABEL: define <2 x i32> @sdiv_disabled_poison_divisor(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> <i32 3, i32 poison>, <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+; Disabled mask lanes are not proof that every result lane is enabled;
+; m_AllOnesForbidPoison() also conservatively rejects poison mask lanes.
+
+define <2 x i32> @sdiv_partial_mask(<2 x i32> %x) {
+; CHECK-LABEL: define <2 x i32> @sdiv_partial_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 false>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 false>)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @sdiv_partial_poison_mask(<2 x i32> %x) {
+; CHECK-LABEL: define <2 x i32> @sdiv_partial_poison_mask(
+; CHECK-SAME: <2 x i32> [[X:%.*]]) {
+; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[X]], <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 poison>)
+; CHECK-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %x, <2 x i32> splat (i32 1), <2 x i1> <i1 true, i1 poison>)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @vec_allones_mask(<2 x i32> %a) {
+; CHECK-LABEL: define <2 x i32> @vec_allones_mask(
+; CHECK-SAME: <2 x i32> [[A:%.*]]) {
+; CHECK-NEXT: ret <2 x i32> [[A]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> splat (i32 1), <2 x i1> splat (i1 true))
+ ret <2 x i32> %r
+}
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/divs-with-scalable-vfs.ll b/llvm/test/Transforms/LoopVectorize/AArch64/divs-with-scalable-vfs.ll
index b1aea48b61a531..ad404027fd3481 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/divs-with-scalable-vfs.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/divs-with-scalable-vfs.ll
@@ -120,7 +120,8 @@ define void @sdiv_feeding_gep_predicated(ptr %dst, i32 %x, i64 %M, i64 %conv6, i
; CHECK-NEXT: [[TMP22:%.*]] = icmp ule <vscale x 2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
; CHECK-NEXT: [[TMP23:%.*]] = select <vscale x 2 x i1> [[ACTIVE_LANE_MASK]], <vscale x 2 x i1> [[TMP22]], <vscale x 2 x i1> zeroinitializer
; CHECK-NEXT: [[TMP24:%.*]] = extractelement <vscale x 2 x i1> [[TMP23]], i64 0
-; CHECK-NEXT: [[TMP25:%.*]] = select i1 [[TMP24]], i64 [[CONV6]], i64 1
+; CHECK-NEXT: [[TMP12:%.*]] = freeze i1 [[TMP24]]
+; CHECK-NEXT: [[TMP25:%.*]] = select i1 [[TMP12]], i64 [[CONV6]], i64 1
; CHECK-NEXT: [[TMP26:%.*]] = sdiv i64 [[M]], [[TMP25]]
; CHECK-NEXT: [[TMP27:%.*]] = trunc i64 [[TMP26]] to i32
; CHECK-NEXT: [[TMP28:%.*]] = mul i64 [[TMP26]], [[CONV61]]
diff --git a/llvm/test/Transforms/LoopVectorize/X86/divs-with-tail-folding.ll b/llvm/test/Transforms/LoopVectorize/X86/divs-with-tail-folding.ll
index fada3253c14717..55a5c851f6998e 100644
--- a/llvm/test/Transforms/LoopVectorize/X86/divs-with-tail-folding.ll
+++ b/llvm/test/Transforms/LoopVectorize/X86/divs-with-tail-folding.ll
@@ -28,7 +28,8 @@ define void @sdiv_feeding_gep(ptr %dst, i32 %x, i64 %M, i64 %conv6, i64 %N) {
; CHECK-NEXT: [[VEC_IV:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP6:%.*]] = icmp ule <4 x i64> [[VEC_IV]], [[BROADCAST_SPLAT2]]
; CHECK-NEXT: [[TMP16:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
-; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP16]], i64 [[CONV6]], i64 1
+; CHECK-NEXT: [[TMP10:%.*]] = freeze i1 [[TMP16]]
+; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP10]], i64 [[CONV6]], i64 1
; CHECK-NEXT: [[TMP8:%.*]] = sdiv i64 [[M]], [[TMP9]]
; CHECK-NEXT: [[TMP15:%.*]] = trunc i64 [[TMP8]] to i32
; CHECK-NEXT: [[TMP20:%.*]] = mul i64 [[TMP8]], [[CONV61]]
@@ -119,7 +120,8 @@ define void @sdiv_feeding_gep_predicated(ptr %dst, i32 %x, i64 %M, i64 %conv6, i
; CHECK-NEXT: [[TMP7:%.*]] = icmp ule <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT2]]
; CHECK-NEXT: [[TMP8:%.*]] = select <4 x i1> [[TMP6]], <4 x i1> [[TMP7]], <4 x i1> zeroinitializer
; CHECK-NEXT: [[TMP9:%.*]] = extractelement <4 x i1> [[TMP8]], i64 0
-; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[TMP9]], i64 [[CONV6]], i64 1
+; CHECK-NEXT: [[TMP12:%.*]] = freeze i1 [[TMP9]]
+; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[TMP12]], i64 [[CONV6]], i64 1
; CHECK-NEXT: [[TMP11:%.*]] = sdiv i64 [[M]], [[TMP10]]
; CHECK-NEXT: [[TMP21:%.*]] = trunc i64 [[TMP11]] to i32
; CHECK-NEXT: [[TMP22:%.*]] = mul i64 [[TMP11]], [[CONV61]]
diff --git a/llvm/test/Transforms/LoopVectorize/X86/masked-divrem-tail-folded.ll b/llvm/test/Transforms/LoopVectorize/X86/masked-divrem-tail-folded.ll
new file mode 100644
index 00000000000000..9fff978359d39b
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/X86/masked-divrem-tail-folded.ll
@@ -0,0 +1,255 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -tail-folding-policy=must-fold-tail -mtriple=x86_64 -mattr=+avx2 -S < %s | FileCheck %s
+
+define void @udiv_tail(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask, i64 %n) {
+;
+; CHECK-LABEL: define void @udiv_tail(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]], i64 [[N:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[EMPTY:%.*]] = icmp eq i64 [[N]], 0
+; CHECK-NEXT: br i1 [[EMPTY]], label %[[EXIT:.*]], label %[[LOOP_PREHEADER:.*]]
+; CHECK: [[LOOP_PREHEADER]]:
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[N_RND_UP:%.*]] = add i64 [[N]], 3
+; CHECK-NEXT: [[TMP0:%.*]] = and i64 [[N_RND_UP]], 3
+; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[TMP0]]
+; CHECK-NEXT: [[TRIP_COUNT_MINUS_1:%.*]] = sub i64 [[N]], 1
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[TRIP_COUNT_MINUS_1]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_LOAD_CONTINUE7:.*]] ]
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[PRED_LOAD_CONTINUE7]] ]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ule <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[OUT]], i64 [[INDEX]]
+; CHECK-NEXT: [[WIDE_MASKED_LOAD:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0(ptr align 4 [[TMP2]], <4 x i1> [[TMP1]], <4 x i32> poison)
+; CHECK-NEXT: [[WIDE_MASKED_LOAD1:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0(ptr align 4 [[TMP3]], <4 x i1> [[TMP1]], <4 x i32> poison)
+; CHECK-NEXT: [[TMP5:%.*]] = extractelement <4 x i1> [[TMP1]], i64 0
+; CHECK-NEXT: br i1 [[TMP5]], label %[[PRED_LOAD_IF:.*]], label %[[PRED_LOAD_CONTINUE:.*]]
+; CHECK: [[PRED_LOAD_IF]]:
+; CHECK-NEXT: [[TMP6:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP7:%.*]] = load i8, ptr [[TMP6]], align 1
+; CHECK-NEXT: [[TMP8:%.*]] = insertelement <4 x i8> poison, i8 [[TMP7]], i64 0
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE]]
+; CHECK: [[PRED_LOAD_CONTINUE]]:
+; CHECK-NEXT: [[TMP9:%.*]] = phi <4 x i8> [ poison, %[[VECTOR_BODY]] ], [ [[TMP8]], %[[PRED_LOAD_IF]] ]
+; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i1> [[TMP1]], i64 1
+; CHECK-NEXT: br i1 [[TMP10]], label %[[PRED_LOAD_IF2:.*]], label %[[PRED_LOAD_CONTINUE3:.*]]
+; CHECK: [[PRED_LOAD_IF2]]:
+; CHECK-NEXT: [[TMP11:%.*]] = add i64 [[INDEX]], 1
+; CHECK-NEXT: [[TMP12:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = load i8, ptr [[TMP12]], align 1
+; CHECK-NEXT: [[TMP14:%.*]] = insertelement <4 x i8> [[TMP9]], i8 [[TMP13]], i64 1
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE3]]
+; CHECK: [[PRED_LOAD_CONTINUE3]]:
+; CHECK-NEXT: [[TMP15:%.*]] = phi <4 x i8> [ [[TMP9]], %[[PRED_LOAD_CONTINUE]] ], [ [[TMP14]], %[[PRED_LOAD_IF2]] ]
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <4 x i1> [[TMP1]], i64 2
+; CHECK-NEXT: br i1 [[TMP16]], label %[[PRED_LOAD_IF4:.*]], label %[[PRED_LOAD_CONTINUE5:.*]]
+; CHECK: [[PRED_LOAD_IF4]]:
+; CHECK-NEXT: [[TMP17:%.*]] = add i64 [[INDEX]], 2
+; CHECK-NEXT: [[TMP18:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[TMP17]]
+; CHECK-NEXT: [[TMP19:%.*]] = load i8, ptr [[TMP18]], align 1
+; CHECK-NEXT: [[TMP20:%.*]] = insertelement <4 x i8> [[TMP15]], i8 [[TMP19]], i64 2
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE5]]
+; CHECK: [[PRED_LOAD_CONTINUE5]]:
+; CHECK-NEXT: [[TMP21:%.*]] = phi <4 x i8> [ [[TMP15]], %[[PRED_LOAD_CONTINUE3]] ], [ [[TMP20]], %[[PRED_LOAD_IF4]] ]
+; CHECK-NEXT: [[TMP22:%.*]] = extractelement <4 x i1> [[TMP1]], i64 3
+; CHECK-NEXT: br i1 [[TMP22]], label %[[PRED_LOAD_IF6:.*]], label %[[PRED_LOAD_CONTINUE7]]
+; CHECK: [[PRED_LOAD_IF6]]:
+; CHECK-NEXT: [[TMP23:%.*]] = add i64 [[INDEX]], 3
+; CHECK-NEXT: [[TMP24:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[TMP23]]
+; CHECK-NEXT: [[TMP25:%.*]] = load i8, ptr [[TMP24]], align 1
+; CHECK-NEXT: [[TMP26:%.*]] = insertelement <4 x i8> [[TMP21]], i8 [[TMP25]], i64 3
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE7]]
+; CHECK: [[PRED_LOAD_CONTINUE7]]:
+; CHECK-NEXT: [[TMP27:%.*]] = phi <4 x i8> [ [[TMP21]], %[[PRED_LOAD_CONTINUE5]] ], [ [[TMP26]], %[[PRED_LOAD_IF6]] ]
+; CHECK-NEXT: [[TMP28:%.*]] = icmp ne <4 x i8> [[TMP27]], zeroinitializer
+; CHECK-NEXT: [[TMP29:%.*]] = select <4 x i1> [[TMP1]], <4 x i1> [[TMP28]], <4 x i1> zeroinitializer
+; CHECK-NEXT: [[TMP30:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[WIDE_MASKED_LOAD]], <4 x i32> [[WIDE_MASKED_LOAD1]], <4 x i1> [[TMP29]])
+; CHECK-NEXT: [[TMP31:%.*]] = freeze <4 x i32> [[TMP30]]
+; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> [[TMP31]], ptr align 4 [[TMP4]], <4 x i1> [[TMP1]])
+; CHECK-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], 4
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add nuw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT: [[TMP32:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT: br i1 [[TMP32]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT_LOOPEXIT:.*]]
+; CHECK: [[EXIT_LOOPEXIT]]:
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ %empty = icmp eq i64 %n, 0
+ br i1 %empty, label %exit, label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %loop ]
+ %ap = getelementptr i32, ptr %a, i64 %iv
+ %bp = getelementptr i32, ptr %b, i64 %iv
+ %mp = getelementptr i8, ptr %mask, i64 %iv
+ %op = getelementptr i32, ptr %out, i64 %iv
+ %x = load i32, ptr %ap, align 4
+ %y = load i32, ptr %bp, align 4
+ %mb = load i8, ptr %mp
+ %m = icmp ne i8 %mb, 0
+ %q = call i32 @llvm.masked.udiv.i32(i32 %x, i32 %y, i1 %m)
+ %f = freeze i32 %q
+ store i32 %f, ptr %op, align 4
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, %n
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @srem_tail(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask, i64 %n) {
+;
+; CHECK-LABEL: define void @srem_tail(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[EMPTY:%.*]] = icmp eq i64 [[N]], 0
+; CHECK-NEXT: br i1 [[EMPTY]], label %[[EXIT:.*]], label %[[LOOP_PREHEADER:.*]]
+; CHECK: [[LOOP_PREHEADER]]:
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[N_RND_UP:%.*]] = add i64 [[N]], 3
+; CHECK-NEXT: [[TMP0:%.*]] = and i64 [[N_RND_UP]], 3
+; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[TMP0]]
+; CHECK-NEXT: [[TRIP_COUNT_MINUS_1:%.*]] = sub i64 [[N]], 1
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[TRIP_COUNT_MINUS_1]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_LOAD_CONTINUE7:.*]] ]
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[PRED_LOAD_CONTINUE7]] ]
+; CHECK-NEXT: [[TMP1:%.*]] = icmp ule <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[OUT]], i64 [[INDEX]]
+; CHECK-NEXT: [[WIDE_MASKED_LOAD:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0(ptr align 4 [[TMP2]], <4 x i1> [[TMP1]], <4 x i32> poison)
+; CHECK-NEXT: [[WIDE_MASKED_LOAD1:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0(ptr align 4 [[TMP3]], <4 x i1> [[TMP1]], <4 x i32> poison)
+; CHECK-NEXT: [[TMP5:%.*]] = extractelement <4 x i1> [[TMP1]], i64 0
+; CHECK-NEXT: br i1 [[TMP5]], label %[[PRED_LOAD_IF:.*]], label %[[PRED_LOAD_CONTINUE:.*]]
+; CHECK: [[PRED_LOAD_IF]]:
+; CHECK-NEXT: [[TMP6:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP7:%.*]] = load i8, ptr [[TMP6]], align 1
+; CHECK-NEXT: [[TMP8:%.*]] = insertelement <4 x i8> poison, i8 [[TMP7]], i64 0
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE]]
+; CHECK: [[PRED_LOAD_CONTINUE]]:
+; CHECK-NEXT: [[TMP9:%.*]] = phi <4 x i8> [ poison, %[[VECTOR_BODY]] ], [ [[TMP8]], %[[PRED_LOAD_IF]] ]
+; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i1> [[TMP1]], i64 1
+; CHECK-NEXT: br i1 [[TMP10]], label %[[PRED_LOAD_IF2:.*]], label %[[PRED_LOAD_CONTINUE3:.*]]
+; CHECK: [[PRED_LOAD_IF2]]:
+; CHECK-NEXT: [[TMP11:%.*]] = add i64 [[INDEX]], 1
+; CHECK-NEXT: [[TMP12:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = load i8, ptr [[TMP12]], align 1
+; CHECK-NEXT: [[TMP14:%.*]] = insertelement <4 x i8> [[TMP9]], i8 [[TMP13]], i64 1
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE3]]
+; CHECK: [[PRED_LOAD_CONTINUE3]]:
+; CHECK-NEXT: [[TMP15:%.*]] = phi <4 x i8> [ [[TMP9]], %[[PRED_LOAD_CONTINUE]] ], [ [[TMP14]], %[[PRED_LOAD_IF2]] ]
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <4 x i1> [[TMP1]], i64 2
+; CHECK-NEXT: br i1 [[TMP16]], label %[[PRED_LOAD_IF4:.*]], label %[[PRED_LOAD_CONTINUE5:.*]]
+; CHECK: [[PRED_LOAD_IF4]]:
+; CHECK-NEXT: [[TMP17:%.*]] = add i64 [[INDEX]], 2
+; CHECK-NEXT: [[TMP18:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[TMP17]]
+; CHECK-NEXT: [[TMP19:%.*]] = load i8, ptr [[TMP18]], align 1
+; CHECK-NEXT: [[TMP20:%.*]] = insertelement <4 x i8> [[TMP15]], i8 [[TMP19]], i64 2
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE5]]
+; CHECK: [[PRED_LOAD_CONTINUE5]]:
+; CHECK-NEXT: [[TMP21:%.*]] = phi <4 x i8> [ [[TMP15]], %[[PRED_LOAD_CONTINUE3]] ], [ [[TMP20]], %[[PRED_LOAD_IF4]] ]
+; CHECK-NEXT: [[TMP22:%.*]] = extractelement <4 x i1> [[TMP1]], i64 3
+; CHECK-NEXT: br i1 [[TMP22]], label %[[PRED_LOAD_IF6:.*]], label %[[PRED_LOAD_CONTINUE7]]
+; CHECK: [[PRED_LOAD_IF6]]:
+; CHECK-NEXT: [[TMP23:%.*]] = add i64 [[INDEX]], 3
+; CHECK-NEXT: [[TMP24:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[TMP23]]
+; CHECK-NEXT: [[TMP25:%.*]] = load i8, ptr [[TMP24]], align 1
+; CHECK-NEXT: [[TMP26:%.*]] = insertelement <4 x i8> [[TMP21]], i8 [[TMP25]], i64 3
+; CHECK-NEXT: br label %[[PRED_LOAD_CONTINUE7]]
+; CHECK: [[PRED_LOAD_CONTINUE7]]:
+; CHECK-NEXT: [[TMP27:%.*]] = phi <4 x i8> [ [[TMP21]], %[[PRED_LOAD_CONTINUE5]] ], [ [[TMP26]], %[[PRED_LOAD_IF6]] ]
+; CHECK-NEXT: [[TMP28:%.*]] = icmp ne <4 x i8> [[TMP27]], zeroinitializer
+; CHECK-NEXT: [[TMP29:%.*]] = select <4 x i1> [[TMP1]], <4 x i1> [[TMP28]], <4 x i1> zeroinitializer
+; CHECK-NEXT: [[TMP30:%.*]] = call <4 x i32> @llvm.masked.srem.v4i32(<4 x i32> [[WIDE_MASKED_LOAD]], <4 x i32> [[WIDE_MASKED_LOAD1]], <4 x i1> [[TMP29]])
+; CHECK-NEXT: [[TMP31:%.*]] = freeze <4 x i32> [[TMP30]]
+; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> [[TMP31]], ptr align 4 [[TMP4]], <4 x i1> [[TMP1]])
+; CHECK-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], 4
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add nuw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT: [[TMP32:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT: br i1 [[TMP32]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT_LOOPEXIT:.*]]
+; CHECK: [[EXIT_LOOPEXIT]]:
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ %empty = icmp eq i64 %n, 0
+ br i1 %empty, label %exit, label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %loop ]
+ %ap = getelementptr i32, ptr %a, i64 %iv
+ %bp = getelementptr i32, ptr %b, i64 %iv
+ %mp = getelementptr i8, ptr %mask, i64 %iv
+ %op = getelementptr i32, ptr %out, i64 %iv
+ %x = load i32, ptr %ap, align 4
+ %y = load i32, ptr %bp, align 4
+ %mb = load i8, ptr %mp
+ %m = icmp ne i8 %mb, 0
+ %q = call i32 @llvm.masked.srem.i32(i32 %x, i32 %y, i1 %m)
+ %f = freeze i32 %q
+ store i32 %f, ptr %op, align 4
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, %n
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @tail_fold_uniform_signed_i1(i1 %a, i1 %b, i1 %m, ptr noalias %out) {
+; CHECK-LABEL: define void @tail_fold_uniform_signed_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]], ptr noalias [[OUT:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i16> [ <i16 0, i16 1, i16 2, i16 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = icmp ule <4 x i16> [[VEC_IND]], splat (i16 1024)
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[OUT]], i64 [[INDEX]]
+; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> splat (i32 1), ptr align 4 [[TMP1]], <4 x i1> [[TMP0]])
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add nuw <4 x i16> [[VEC_IND]], splat (i16 4)
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1028
+; CHECK-NEXT: br i1 [[TMP2]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %loop ]
+ %q = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ %idx = zext i1 %q to i64
+ %base = getelementptr i32, ptr %out, i64 %idx
+ %op = getelementptr i32, ptr %base, i64 %iv
+ store i32 1, ptr %op, align 4
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1025
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+;.
+; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
+; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
+; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
+; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META1]], [[META2]]}
+; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
+;.
diff --git a/llvm/test/Transforms/LoopVectorize/X86/masked-divrem.ll b/llvm/test/Transforms/LoopVectorize/X86/masked-divrem.ll
new file mode 100644
index 00000000000000..77a324184d3b74
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/X86/masked-divrem.ll
@@ -0,0 +1,313 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -mtriple=x86_64 -mattr=+avx2 -S < %s | FileCheck %s
+
+define void @udiv_unconditional(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask, ptr noalias %pred) {
+;
+; CHECK-LABEL: define void @udiv_unconditional(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]], ptr noalias [[PRED:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, ptr [[OUT]], i64 [[INDEX]]
+; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4
+; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <4 x i32>, ptr [[TMP1]], align 4
+; CHECK-NEXT: [[WIDE_LOAD2:%.*]] = load <4 x i8>, ptr [[TMP2]], align 1
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne <4 x i8> [[WIDE_LOAD2]], zeroinitializer
+; CHECK-NEXT: [[TMP5:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[WIDE_LOAD]], <4 x i32> [[WIDE_LOAD1]], <4 x i1> [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = freeze <4 x i32> [[TMP5]]
+; CHECK-NEXT: store <4 x i32> [[TMP6]], ptr [[TMP3]], align 4
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %latch ]
+ %ap = getelementptr i32, ptr %a, i64 %iv
+ %bp = getelementptr i32, ptr %b, i64 %iv
+ %mp = getelementptr i8, ptr %mask, i64 %iv
+ %pp = getelementptr i8, ptr %pred, i64 %iv
+ %op = getelementptr i32, ptr %out, i64 %iv
+ %x = load i32, ptr %ap, align 4
+ %y = load i32, ptr %bp, align 4
+ %mb = load i8, ptr %mp
+ %m = icmp ne i8 %mb, 0
+ %q = call i32 @llvm.masked.udiv.i32(i32 %x, i32 %y, i1 %m)
+ %f = freeze i32 %q
+ store i32 %f, ptr %op, align 4
+ br label %latch
+latch:
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1024
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @udiv_conditional(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask, ptr noalias %pred) {
+;
+; CHECK-LABEL: define void @udiv_conditional(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]], ptr noalias [[PRED:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i8, ptr [[PRED]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[OUT]], i64 [[INDEX]]
+; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4
+; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <4 x i32>, ptr [[TMP1]], align 4
+; CHECK-NEXT: [[WIDE_LOAD2:%.*]] = load <4 x i8>, ptr [[TMP2]], align 1
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ne <4 x i8> [[WIDE_LOAD2]], zeroinitializer
+; CHECK-NEXT: [[WIDE_LOAD3:%.*]] = load <4 x i8>, ptr [[TMP3]], align 1
+; CHECK-NEXT: [[TMP6:%.*]] = icmp ne <4 x i8> [[WIDE_LOAD3]], zeroinitializer
+; CHECK-NEXT: [[TMP7:%.*]] = select <4 x i1> [[TMP6]], <4 x i1> [[TMP5]], <4 x i1> zeroinitializer
+; CHECK-NEXT: [[TMP8:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[WIDE_LOAD]], <4 x i32> [[WIDE_LOAD1]], <4 x i1> [[TMP7]])
+; CHECK-NEXT: [[TMP9:%.*]] = freeze <4 x i32> [[TMP8]]
+; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> [[TMP9]], ptr align 4 [[TMP4]], <4 x i1> [[TMP6]])
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP10]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %latch ]
+ %ap = getelementptr i32, ptr %a, i64 %iv
+ %bp = getelementptr i32, ptr %b, i64 %iv
+ %mp = getelementptr i8, ptr %mask, i64 %iv
+ %pp = getelementptr i8, ptr %pred, i64 %iv
+ %op = getelementptr i32, ptr %out, i64 %iv
+ %x = load i32, ptr %ap, align 4
+ %y = load i32, ptr %bp, align 4
+ %mb = load i8, ptr %mp
+ %m = icmp ne i8 %mb, 0
+ %pb = load i8, ptr %pp
+ %p = icmp ne i8 %pb, 0
+ br i1 %p, label %divide, label %latch
+divide:
+ %q = call i32 @llvm.masked.udiv.i32(i32 %x, i32 %y, i1 %m)
+ %f = freeze i32 %q
+ store i32 %f, ptr %op, align 4
+ br label %latch
+latch:
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1024
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @sdiv_conditional(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask, ptr noalias %pred) {
+;
+; CHECK-LABEL: define void @sdiv_conditional(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]], ptr noalias [[PRED:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[MASK]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i8, ptr [[PRED]], i64 [[INDEX]]
+; CHECK-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[OUT]], i64 [[INDEX]]
+; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4
+; CHECK-NEXT: [[WIDE_LOAD1:%.*]] = load <4 x i32>, ptr [[TMP1]], align 4
+; CHECK-NEXT: [[WIDE_LOAD2:%.*]] = load <4 x i8>, ptr [[TMP2]], align 1
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ne <4 x i8> [[WIDE_LOAD2]], zeroinitializer
+; CHECK-NEXT: [[WIDE_LOAD3:%.*]] = load <4 x i8>, ptr [[TMP3]], align 1
+; CHECK-NEXT: [[TMP6:%.*]] = icmp ne <4 x i8> [[WIDE_LOAD3]], zeroinitializer
+; CHECK-NEXT: [[TMP7:%.*]] = select <4 x i1> [[TMP6]], <4 x i1> [[TMP5]], <4 x i1> zeroinitializer
+; CHECK-NEXT: [[TMP8:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> [[WIDE_LOAD]], <4 x i32> [[WIDE_LOAD1]], <4 x i1> [[TMP7]])
+; CHECK-NEXT: [[TMP9:%.*]] = freeze <4 x i32> [[TMP8]]
+; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> [[TMP9]], ptr align 4 [[TMP4]], <4 x i1> [[TMP6]])
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP10]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %latch ]
+ %ap = getelementptr i32, ptr %a, i64 %iv
+ %bp = getelementptr i32, ptr %b, i64 %iv
+ %mp = getelementptr i8, ptr %mask, i64 %iv
+ %pp = getelementptr i8, ptr %pred, i64 %iv
+ %op = getelementptr i32, ptr %out, i64 %iv
+ %x = load i32, ptr %ap, align 4
+ %y = load i32, ptr %bp, align 4
+ %mb = load i8, ptr %mp
+ %m = icmp ne i8 %mb, 0
+ %pb = load i8, ptr %pp
+ %p = icmp ne i8 %pb, 0
+ br i1 %p, label %divide, label %latch
+divide:
+ %q = call i32 @llvm.masked.sdiv.i32(i32 %x, i32 %y, i1 %m)
+ %f = freeze i32 %q
+ store i32 %f, ptr %op, align 4
+ br label %latch
+latch:
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1024
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @uniform_mask(i32 %a, i32 %b, i1 %m, ptr %out) {
+;
+; CHECK-LABEL: define void @uniform_mask(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], ptr [[OUT:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i1> poison, i1 [[M]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i1> [[BROADCAST_SPLATINSERT]], <4 x i1> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i32> poison, i32 [[B]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT1]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <4 x i32> poison, i32 [[A]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT4:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT3]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: [[TMP0:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> [[BROADCAST_SPLAT4]], <4 x i32> [[BROADCAST_SPLAT2]], <4 x i1> [[BROADCAST_SPLAT]])
+; CHECK-NEXT: [[TMP1:%.*]] = freeze <4 x i32> [[TMP0]]
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i32> [[TMP1]], i64 3
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: store i32 [[TMP2]], ptr [[OUT]], align 4
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %latch ]
+ %q = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ %f = freeze i32 %q
+ store i32 %f, ptr %out, align 4
+ br label %latch
+latch:
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1024
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @uniform_i1(i1 %a, i1 %b, i1 %m, ptr %out) {
+;
+; CHECK-LABEL: define void @uniform_i1(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[M:%.*]], ptr [[OUT:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 false
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP1]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: store i1 [[TMP0]], ptr [[OUT]], align 1
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %latch ]
+ %q = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ %f = freeze i1 %q
+ store i1 %f, ptr %out
+ br label %latch
+latch:
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1024
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+define void @uniform_mask_no_result_freeze(i32 %a, i32 %b, i1 %m, ptr %out) {
+;
+; CHECK-LABEL: define void @uniform_mask_no_result_freeze(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], ptr [[OUT:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i1> poison, i1 [[M]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i1> [[BROADCAST_SPLATINSERT]], <4 x i1> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i32> poison, i32 [[B]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT1]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <4 x i32> poison, i32 [[A]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT4:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT3]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: [[TMP0:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> [[BROADCAST_SPLAT4]], <4 x i32> [[BROADCAST_SPLAT2]], <4 x i1> [[BROADCAST_SPLAT]])
+; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i32> [[TMP0]], i64 3
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP2]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP7:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: store i32 [[TMP1]], ptr [[OUT]], align 4
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %next, %latch ]
+ %q = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ store i32 %q, ptr %out, align 4
+ br label %latch
+latch:
+ %next = add nuw i64 %iv, 1
+ %done = icmp eq i64 %next, 1024
+ br i1 %done, label %exit, label %loop
+exit:
+ ret void
+}
+
+;.
+; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
+; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
+; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
+; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META1]], [[META2]]}
+; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
+; CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[META1]], [[META2]]}
+; CHECK: [[LOOP6]] = distinct !{[[LOOP6]], [[META1]], [[META2]]}
+; CHECK: [[LOOP7]] = distinct !{[[LOOP7]], [[META1]], [[META2]]}
+;.
diff --git a/llvm/test/Transforms/PhaseOrdering/masked-divrem-defined-effects.ll b/llvm/test/Transforms/PhaseOrdering/masked-divrem-defined-effects.ll
new file mode 100644
index 00000000000000..cef33acbec8da0
--- /dev/null
+++ b/llvm/test/Transforms/PhaseOrdering/masked-divrem-defined-effects.ll
@@ -0,0 +1,67 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes='default<O2>' -S < %s | FileCheck %s
+
+define void @zero_divisor_effect(ptr %out, i32 %a, i32 %b) {
+; CHECK-LABEL: define void @zero_divisor_effect(
+; CHECK-SAME: ptr nofree writeonly captures(none) initializes((0, 4)) [[OUT:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[SAFE:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT: [[Q:%.*]] = tail call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[SAFE]])
+; CHECK-NEXT: [[F:%.*]] = freeze i32 [[Q]]
+; CHECK-NEXT: store i32 [[F]], ptr [[OUT]], align 4
+; CHECK-NEXT: br i1 [[SAFE]], label %[[EXIT:.*]], label %[[THEN:.*]]
+; CHECK: [[THEN]]:
+; CHECK-NEXT: [[P:%.*]] = getelementptr i8, ptr [[OUT]], i64 4
+; CHECK-NEXT: store i32 7, ptr [[P]], align 4
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+ %safe = icmp ne i32 %b, 0
+ %q = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %safe)
+ %f = freeze i32 %q
+ store i32 %f, ptr %out
+ %zero = icmp eq i32 %b, 0
+ br i1 %zero, label %then, label %exit
+then:
+ %p = getelementptr i32, ptr %out, i64 1
+ store i32 7, ptr %p
+ br label %exit
+exit:
+ ret void
+}
+
+define void @signed_overflow_effect(ptr %out, i32 %a, i32 %b) {
+; CHECK-LABEL: define void @signed_overflow_effect(
+; CHECK-SAME: ptr nofree writeonly captures(none) initializes((0, 4)) [[OUT:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) local_unnamed_addr #[[ATTR0]] {
+; CHECK-NEXT: [[NZ:%.*]] = icmp ne i32 [[B]], 0
+; CHECK-NEXT: [[NMIN:%.*]] = icmp ne i32 [[A]], -2147483648
+; CHECK-NEXT: [[NM1:%.*]] = icmp ne i32 [[B]], -1
+; CHECK-NEXT: [[NOV:%.*]] = select i1 [[NM1]], i1 true, i1 [[NMIN]]
+; CHECK-NEXT: [[SAFE:%.*]] = and i1 [[NZ]], [[NOV]]
+; CHECK-NEXT: [[Q:%.*]] = tail call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[B]], i1 [[SAFE]])
+; CHECK-NEXT: [[F:%.*]] = freeze i32 [[Q]]
+; CHECK-NEXT: store i32 [[F]], ptr [[OUT]], align 4
+; CHECK-NEXT: br i1 [[NOV]], label %[[EXIT:.*]], label %[[THEN:.*]]
+; CHECK: [[THEN]]:
+; CHECK-NEXT: [[P:%.*]] = getelementptr i8, ptr [[OUT]], i64 4
+; CHECK-NEXT: store i32 7, ptr [[P]], align 4
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+ %nz = icmp ne i32 %b, 0
+ %nmin = icmp ne i32 %a, -2147483648
+ %nm1 = icmp ne i32 %b, -1
+ %nov = select i1 %nm1, i1 true, i1 %nmin
+ %safe = select i1 %nz, i1 %nov, i1 false
+ %q = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %safe)
+ %f = freeze i32 %q
+ store i32 %f, ptr %out
+ br i1 %nov, label %exit, label %then
+then:
+ %p = getelementptr i32, ptr %out, i64 1
+ store i32 7, ptr %p
+ br label %exit
+exit:
+ ret void
+}
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/masked-divrem-scalar.ll b/llvm/test/Transforms/SLPVectorizer/X86/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..e6a6d2eb193de5
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/masked-divrem-scalar.ll
@@ -0,0 +1,174 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=slp-vectorizer -slp-threshold=-100000 -mtriple=x86_64 -mattr=+avx2 -S < %s | FileCheck %s
+
+define void @sdiv_four(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask) {
+; CHECK-LABEL: define void @sdiv_four(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[AP0:%.*]] = getelementptr i32, ptr [[A]], i64 0
+; CHECK-NEXT: [[BP0:%.*]] = getelementptr i32, ptr [[B]], i64 0
+; CHECK-NEXT: [[OP0:%.*]] = getelementptr i32, ptr [[OUT]], i64 0
+; CHECK-NEXT: [[MP0:%.*]] = getelementptr i8, ptr [[MASK]], i64 0
+; CHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr [[AP0]], align 4
+; CHECK-NEXT: [[TMP2:%.*]] = load <4 x i32>, ptr [[BP0]], align 4
+; CHECK-NEXT: [[TMP3:%.*]] = load <4 x i8>, ptr [[MP0]], align 1
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne <4 x i8> [[TMP3]], zeroinitializer
+; CHECK-NEXT: [[TMP5:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> [[TMP1]], <4 x i32> [[TMP2]], <4 x i1> [[TMP4]])
+; CHECK-NEXT: store <4 x i32> [[TMP5]], ptr [[OP0]], align 4
+; CHECK-NEXT: ret void
+;
+ %ap0 = getelementptr i32, ptr %a, i64 0
+ %bp0 = getelementptr i32, ptr %b, i64 0
+ %op0 = getelementptr i32, ptr %out, i64 0
+ %mp0 = getelementptr i8, ptr %mask, i64 0
+ %a0 = load i32, ptr %ap0, align 4
+ %b0 = load i32, ptr %bp0, align 4
+ %m0 = load i8, ptr %mp0, align 1
+ %mask0 = icmp ne i8 %m0, 0
+ %r0 = call i32 @llvm.masked.sdiv.i32(i32 %a0, i32 %b0, i1 %mask0)
+ store i32 %r0, ptr %op0, align 4
+ %ap1 = getelementptr i32, ptr %a, i64 1
+ %bp1 = getelementptr i32, ptr %b, i64 1
+ %op1 = getelementptr i32, ptr %out, i64 1
+ %mp1 = getelementptr i8, ptr %mask, i64 1
+ %a1 = load i32, ptr %ap1, align 4
+ %b1 = load i32, ptr %bp1, align 4
+ %m1 = load i8, ptr %mp1, align 1
+ %mask1 = icmp ne i8 %m1, 0
+ %r1 = call i32 @llvm.masked.sdiv.i32(i32 %a1, i32 %b1, i1 %mask1)
+ store i32 %r1, ptr %op1, align 4
+ %ap2 = getelementptr i32, ptr %a, i64 2
+ %bp2 = getelementptr i32, ptr %b, i64 2
+ %op2 = getelementptr i32, ptr %out, i64 2
+ %mp2 = getelementptr i8, ptr %mask, i64 2
+ %a2 = load i32, ptr %ap2, align 4
+ %b2 = load i32, ptr %bp2, align 4
+ %m2 = load i8, ptr %mp2, align 1
+ %mask2 = icmp ne i8 %m2, 0
+ %r2 = call i32 @llvm.masked.sdiv.i32(i32 %a2, i32 %b2, i1 %mask2)
+ store i32 %r2, ptr %op2, align 4
+ %ap3 = getelementptr i32, ptr %a, i64 3
+ %bp3 = getelementptr i32, ptr %b, i64 3
+ %op3 = getelementptr i32, ptr %out, i64 3
+ %mp3 = getelementptr i8, ptr %mask, i64 3
+ %a3 = load i32, ptr %ap3, align 4
+ %b3 = load i32, ptr %bp3, align 4
+ %m3 = load i8, ptr %mp3, align 1
+ %mask3 = icmp ne i8 %m3, 0
+ %r3 = call i32 @llvm.masked.sdiv.i32(i32 %a3, i32 %b3, i1 %mask3)
+ store i32 %r3, ptr %op3, align 4
+ ret void
+}
+
+define void @udiv_three_nonpow2(ptr noalias %a, ptr noalias %b, ptr noalias %out, ptr noalias %mask) {
+; CHECK-LABEL: define void @udiv_three_nonpow2(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[OUT:%.*]], ptr noalias [[MASK:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AP0:%.*]] = getelementptr i32, ptr [[A]], i64 0
+; CHECK-NEXT: [[BP0:%.*]] = getelementptr i32, ptr [[B]], i64 0
+; CHECK-NEXT: [[OP0:%.*]] = getelementptr i32, ptr [[OUT]], i64 0
+; CHECK-NEXT: [[MP0:%.*]] = getelementptr i8, ptr [[MASK]], i64 0
+; CHECK-NEXT: [[TMP1:%.*]] = load <2 x i32>, ptr [[AP0]], align 4
+; CHECK-NEXT: [[TMP2:%.*]] = load <2 x i32>, ptr [[BP0]], align 4
+; CHECK-NEXT: [[TMP3:%.*]] = load <2 x i8>, ptr [[MP0]], align 1
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne <2 x i8> [[TMP3]], zeroinitializer
+; CHECK-NEXT: [[TMP5:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[TMP1]], <2 x i32> [[TMP2]], <2 x i1> [[TMP4]])
+; CHECK-NEXT: store <2 x i32> [[TMP5]], ptr [[OP0]], align 4
+; CHECK-NEXT: [[AP2:%.*]] = getelementptr i32, ptr [[A]], i64 2
+; CHECK-NEXT: [[BP2:%.*]] = getelementptr i32, ptr [[B]], i64 2
+; CHECK-NEXT: [[OP2:%.*]] = getelementptr i32, ptr [[OUT]], i64 2
+; CHECK-NEXT: [[MP2:%.*]] = getelementptr i8, ptr [[MASK]], i64 2
+; CHECK-NEXT: [[A2:%.*]] = load i32, ptr [[AP2]], align 4
+; CHECK-NEXT: [[B2:%.*]] = load i32, ptr [[BP2]], align 4
+; CHECK-NEXT: [[M2:%.*]] = load i8, ptr [[MP2]], align 1
+; CHECK-NEXT: [[MASK2:%.*]] = icmp ne i8 [[M2]], 0
+; CHECK-NEXT: [[R2:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A2]], i32 [[B2]], i1 [[MASK2]])
+; CHECK-NEXT: store i32 [[R2]], ptr [[OP2]], align 4
+; CHECK-NEXT: ret void
+;
+ %ap0 = getelementptr i32, ptr %a, i64 0
+ %bp0 = getelementptr i32, ptr %b, i64 0
+ %op0 = getelementptr i32, ptr %out, i64 0
+ %mp0 = getelementptr i8, ptr %mask, i64 0
+ %a0 = load i32, ptr %ap0, align 4
+ %b0 = load i32, ptr %bp0, align 4
+ %m0 = load i8, ptr %mp0, align 1
+ %mask0 = icmp ne i8 %m0, 0
+ %r0 = call i32 @llvm.masked.udiv.i32(i32 %a0, i32 %b0, i1 %mask0)
+ store i32 %r0, ptr %op0, align 4
+ %ap1 = getelementptr i32, ptr %a, i64 1
+ %bp1 = getelementptr i32, ptr %b, i64 1
+ %op1 = getelementptr i32, ptr %out, i64 1
+ %mp1 = getelementptr i8, ptr %mask, i64 1
+ %a1 = load i32, ptr %ap1, align 4
+ %b1 = load i32, ptr %bp1, align 4
+ %m1 = load i8, ptr %mp1, align 1
+ %mask1 = icmp ne i8 %m1, 0
+ %r1 = call i32 @llvm.masked.udiv.i32(i32 %a1, i32 %b1, i1 %mask1)
+ store i32 %r1, ptr %op1, align 4
+ %ap2 = getelementptr i32, ptr %a, i64 2
+ %bp2 = getelementptr i32, ptr %b, i64 2
+ %op2 = getelementptr i32, ptr %out, i64 2
+ %mp2 = getelementptr i8, ptr %mask, i64 2
+ %a2 = load i32, ptr %ap2, align 4
+ %b2 = load i32, ptr %bp2, align 4
+ %m2 = load i8, ptr %mp2, align 1
+ %mask2 = icmp ne i8 %m2, 0
+ %r2 = call i32 @llvm.masked.udiv.i32(i32 %a2, i32 %b2, i1 %mask2)
+ store i32 %r2, ptr %op2, align 4
+ ret void
+}
+
+; SLP must not pad a 3-lane bundle with a poison 4th lane, since a poison
+; divisor or mask+divisor lane in the widened call could introduce UB.
+define <4 x i32> @udiv_three_poison_fourth_lane(ptr noalias %a, ptr noalias %b, ptr noalias %mask) {
+; CHECK-LABEL: define <4 x i32> @udiv_three_poison_fourth_lane(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[MASK:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AP0:%.*]] = getelementptr i32, ptr [[A]], i64 0
+; CHECK-NEXT: [[BP0:%.*]] = getelementptr i32, ptr [[B]], i64 0
+; CHECK-NEXT: [[MP0:%.*]] = getelementptr i8, ptr [[MASK]], i64 0
+; CHECK-NEXT: [[A2:%.*]] = load i32, ptr [[AP0]], align 4
+; CHECK-NEXT: [[B2:%.*]] = load i32, ptr [[BP0]], align 4
+; CHECK-NEXT: [[M2:%.*]] = load i8, ptr [[MP0]], align 1
+; CHECK-NEXT: [[MASK2:%.*]] = icmp ne i8 [[M2]], 0
+; CHECK-NEXT: [[R2:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A2]], i32 [[B2]], i1 [[MASK2]])
+; CHECK-NEXT: [[AP1:%.*]] = getelementptr i32, ptr [[A]], i64 1
+; CHECK-NEXT: [[BP1:%.*]] = getelementptr i32, ptr [[B]], i64 1
+; CHECK-NEXT: [[MP1:%.*]] = getelementptr i8, ptr [[MASK]], i64 1
+; CHECK-NEXT: [[TMP1:%.*]] = load <2 x i32>, ptr [[AP1]], align 4
+; CHECK-NEXT: [[TMP2:%.*]] = load <2 x i32>, ptr [[BP1]], align 4
+; CHECK-NEXT: [[TMP3:%.*]] = load <2 x i8>, ptr [[MP1]], align 1
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ne <2 x i8> [[TMP3]], zeroinitializer
+; CHECK-NEXT: [[TMP5:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[TMP1]], <2 x i32> [[TMP2]], <2 x i1> [[TMP4]])
+; CHECK-NEXT: [[V0:%.*]] = insertelement <4 x i32> poison, i32 [[R2]], i64 0
+; CHECK-NEXT: [[TMP6:%.*]] = shufflevector <2 x i32> [[TMP5]], <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
+; CHECK-NEXT: [[V2:%.*]] = shufflevector <4 x i32> [[V0]], <4 x i32> [[TMP6]], <4 x i32> <i32 0, i32 4, i32 5, i32 poison>
+; CHECK-NEXT: ret <4 x i32> [[V2]]
+;
+ %ap0 = getelementptr i32, ptr %a, i64 0
+ %bp0 = getelementptr i32, ptr %b, i64 0
+ %mp0 = getelementptr i8, ptr %mask, i64 0
+ %a0 = load i32, ptr %ap0, align 4
+ %b0 = load i32, ptr %bp0, align 4
+ %m0 = load i8, ptr %mp0, align 1
+ %mask0 = icmp ne i8 %m0, 0
+ %r0 = call i32 @llvm.masked.udiv.i32(i32 %a0, i32 %b0, i1 %mask0)
+ %ap1 = getelementptr i32, ptr %a, i64 1
+ %bp1 = getelementptr i32, ptr %b, i64 1
+ %mp1 = getelementptr i8, ptr %mask, i64 1
+ %a1 = load i32, ptr %ap1, align 4
+ %b1 = load i32, ptr %bp1, align 4
+ %m1 = load i8, ptr %mp1, align 1
+ %mask1 = icmp ne i8 %m1, 0
+ %r1 = call i32 @llvm.masked.udiv.i32(i32 %a1, i32 %b1, i1 %mask1)
+ %ap2 = getelementptr i32, ptr %a, i64 2
+ %bp2 = getelementptr i32, ptr %b, i64 2
+ %mp2 = getelementptr i8, ptr %mask, i64 2
+ %a2 = load i32, ptr %ap2, align 4
+ %b2 = load i32, ptr %bp2, align 4
+ %m2 = load i8, ptr %mp2, align 1
+ %mask2 = icmp ne i8 %m2, 0
+ %r2 = call i32 @llvm.masked.udiv.i32(i32 %a2, i32 %b2, i1 %mask2)
+ %v0 = insertelement <4 x i32> poison, i32 %r0, i64 0
+ %v1 = insertelement <4 x i32> %v0, i32 %r1, i64 1
+ %v2 = insertelement <4 x i32> %v1, i32 %r2, i64 2
+ ret <4 x i32> %v2
+}
diff --git a/llvm/test/Transforms/VectorCombine/masked-divrem.ll b/llvm/test/Transforms/VectorCombine/masked-divrem.ll
new file mode 100644
index 00000000000000..5fbd44b887f114
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/masked-divrem.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=vector-combine -S < %s | FileCheck %s
+
+define <4 x i32> @shuf(<4 x i32> %x, <4 x i32> %y) {
+; CHECK-LABEL: define <4 x i32> @shuf(
+; CHECK-SAME: <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]]) {
+; CHECK-NEXT: [[A:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[X]], <4 x i32> <i32 3, i32 0, i32 0, i32 0>, <4 x i1> <i1 true, i1 false, i1 false, i1 false>)
+; CHECK-NEXT: [[B:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[Y]], <4 x i32> <i32 5, i32 0, i32 0, i32 0>, <4 x i1> <i1 true, i1 false, i1 false, i1 false>)
+; CHECK-NEXT: [[R:%.*]] = shufflevector <4 x i32> [[A]], <4 x i32> [[B]], <4 x i32> <i32 0, i32 4, i32 poison, i32 poison>
+; CHECK-NEXT: ret <4 x i32> [[R]]
+;
+ %a = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> %x, <4 x i32> <i32 3, i32 0, i32 0, i32 0>, <4 x i1> <i1 true, i1 false, i1 false, i1 false>)
+ %b = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> %y, <4 x i32> <i32 5, i32 0, i32 0, i32 0>, <4 x i1> <i1 true, i1 false, i1 false, i1 false>)
+ %r = shufflevector <4 x i32> %a, <4 x i32> %b, <4 x i32> <i32 0, i32 4, i32 poison, i32 poison>
+ ret <4 x i32> %r
+}
+
+define <4 x i32> @ident(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
+; CHECK-LABEL: define <4 x i32> @ident(
+; CHECK-SAME: <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <4 x i1> [[M:%.*]]) {
+; CHECK-NEXT: [[XL:%.*]] = shufflevector <4 x i32> [[X]], <4 x i32> poison, <2 x i32> <i32 0, i32 1>
+; CHECK-NEXT: [[YL:%.*]] = shufflevector <4 x i32> [[Y]], <4 x i32> poison, <2 x i32> <i32 0, i32 1>
+; CHECK-NEXT: [[ML:%.*]] = shufflevector <4 x i1> [[M]], <4 x i1> poison, <2 x i32> <i32 0, i32 1>
+; CHECK-NEXT: [[D:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[XL]], <2 x i32> [[YL]], <2 x i1> [[ML]])
+; CHECK-NEXT: [[R:%.*]] = shufflevector <2 x i32> [[D]], <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
+; CHECK-NEXT: ret <4 x i32> [[R]]
+;
+ %xl = shufflevector <4 x i32> %x, <4 x i32> poison, <2 x i32> <i32 0, i32 1>
+ %yl = shufflevector <4 x i32> %y, <4 x i32> poison, <2 x i32> <i32 0, i32 1>
+ %ml = shufflevector <4 x i1> %m, <4 x i1> poison, <2 x i32> <i32 0, i32 1>
+ %d = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %xl, <2 x i32> %yl, <2 x i1> %ml)
+ %r = shufflevector <2 x i32> %d, <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
+ ret <4 x i32> %r
+}
+
+define <4 x i32> @perm(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
+; CHECK-LABEL: define <4 x i32> @perm(
+; CHECK-SAME: <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <4 x i1> [[M:%.*]]) {
+; CHECK-NEXT: [[A:%.*]] = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> [[X]], <4 x i32> [[Y]], <4 x i1> [[M]])
+; CHECK-NEXT: [[S:%.*]] = shufflevector <4 x i32> [[A]], <4 x i32> poison, <4 x i32> <i32 1, i32 0, i32 poison, i32 2>
+; CHECK-NEXT: ret <4 x i32> [[S]]
+;
+ %a = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m)
+ %s = shufflevector <4 x i32> %a, <4 x i32> poison, <4 x i32> <i32 1, i32 0, i32 poison, i32 2>
+ ret <4 x i32> %s
+}
>From 23ae079604e7ccfa0ad0bc45c578b6ef9eb1d6cb Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 12:37:51 +0100
Subject: [PATCH 4/9] [NVPTX] Recognize trap-free integer div/rem
PTX integer `div` and `rem` produce an unspecified machine-specific
value on division or remainder by zero, and signed two's-complement
`INT_MIN / -1` wraps to `INT_MIN` (`INT_MIN % -1` yields `0`) without
trapping. Treat native `i16`/`i32`/`i64` widths and `i8` (promoted to
`i16`) as trap-free, so scalar and scalarized masked div/rem use
ordinary PTX operations and enable the 64-to-32-bit slow-division
bypass on masked `i64` operations. Keep divisor guards for `i1` and
wider or non-power-of-2 integer expansions, and record the remaining
generic SelectionDAG `freeze(undef)` limitation for late-discovered zero
divisors in `masked-divrem-late-zero-limitation.ll`.
Assisted-by: Claude
---
llvm/lib/Target/NVPTX/NVPTXISelLowering.h | 11 +
.../masked-divrem-late-zero-limitation.ll | 73 ++++
.../CodeGen/NVPTX/masked-divrem-scalar.ll | 354 ++++++++++++++++
llvm/test/CodeGen/NVPTX/masked-divrem.ll | 7 +-
.../CodeGenPrepare/NVPTX/masked-divrem.ll | 394 ++++++++++++++++++
5 files changed, 836 insertions(+), 3 deletions(-)
create mode 100644 llvm/test/CodeGen/NVPTX/masked-divrem-late-zero-limitation.ll
create mode 100644 llvm/test/CodeGen/NVPTX/masked-divrem-scalar.ll
create mode 100644 llvm/test/Transforms/CodeGenPrepare/NVPTX/masked-divrem.ll
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
index 6bdd9ff3c6db21..34929c68edf7c1 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
@@ -32,6 +32,17 @@ class NVPTXTargetLowering : public TargetLowering {
public:
explicit NVPTXTargetLowering(const NVPTXTargetMachine &TM,
const NVPTXSubtarget &STI);
+
+ /// PTX integer `div` and `rem` produce an unspecified machine-specific value
+ /// on division or remainder by zero, and signed two's-complement `INT_MIN /
+ /// -1` wraps to `INT_MIN` (`INT_MIN % -1` yields `0`) without trapping; see
+ /// PTX ISA, Integer Arithmetic Instructions (`div`, `rem`). Restrict this to
+ /// native `i16`/`i32`/`i64` widths and `i8` (promoted to `i16`), excluding
+ /// `i1` and wider or non-power-of-2 integer expansions.
+ bool isIntDivRemTrapFree(EVT VT) const override {
+ return VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64;
+ }
+
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
void getTgtMemIntrinsic(SmallVectorImpl<IntrinsicInfo> &Infos,
diff --git a/llvm/test/CodeGen/NVPTX/masked-divrem-late-zero-limitation.ll b/llvm/test/CodeGen/NVPTX/masked-divrem-late-zero-limitation.ll
new file mode 100644
index 00000000000000..49b9a6ec5c712f
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/masked-divrem-late-zero-limitation.ll
@@ -0,0 +1,73 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_80 -O2 < %s | FileCheck %s
+
+; FIXME: These disabled lanes become zero only after lowering, beyond what IR
+; and DAG known bits prove when the masked operation is lowered. The DAG folds
+; the lane's division to undef, and freeze(undef) is selected as a copy of an
+; implicit def, so the two stores read unrelated registers. A machine-level
+; frozen undef would make them consistent.
+
+; Six nested additions exceed the known-bits depth limit; opt -O2 keeps them.
+define i32 @deep_sdiv(<4 x i32> %a, <4 x i32> %b0, <4 x i32> %b1, <4 x i32> %b2, <4 x i32> %b3, <4 x i32> %b4, <4 x i32> %b5, <4 x i32> %b6, ptr %p, ptr %p2) {
+; CHECK-LABEL: deep_sdiv(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-NEXT: .reg .b64 %rd<3>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [deep_sdiv_param_8];
+; CHECK-NEXT: ld.param.b64 %rd2, [deep_sdiv_param_9];
+; CHECK-NEXT: st.b32 [%rd1], %r1;
+; CHECK-NEXT: xor.b32 %r3, %r2, 85;
+; CHECK-NEXT: st.b32 [%rd2], %r3;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r1;
+; CHECK-NEXT: ret;
+ %z0 = and <4 x i32> %b0, <i32 -1, i32 0, i32 -1, i32 -1>
+ %z1 = and <4 x i32> %b1, <i32 -1, i32 0, i32 -1, i32 -1>
+ %d1 = add <4 x i32> %z0, %z1
+ %z2 = and <4 x i32> %b2, <i32 -1, i32 0, i32 -1, i32 -1>
+ %d2 = add <4 x i32> %d1, %z2
+ %z3 = and <4 x i32> %b3, <i32 -1, i32 0, i32 -1, i32 -1>
+ %d3 = add <4 x i32> %d2, %z3
+ %z4 = and <4 x i32> %b4, <i32 -1, i32 0, i32 -1, i32 -1>
+ %d4 = add <4 x i32> %d3, %z4
+ %z5 = and <4 x i32> %b5, <i32 -1, i32 0, i32 -1, i32 -1>
+ %d5 = add <4 x i32> %d4, %z5
+ %z6 = and <4 x i32> %b6, <i32 -1, i32 0, i32 -1, i32 -1>
+ %d6 = add <4 x i32> %d5, %z6
+ %m = icmp ne <4 x i32> %d6, zeroinitializer
+ %r = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> %a, <4 x i32> %d6, <4 x i1> %m)
+ %f = freeze <4 x i32> %r
+ %e = extractelement <4 x i32> %f, i64 1
+ store i32 %e, ptr %p
+ %x = xor i32 %e, 85
+ store i32 %x, ptr %p2
+ ret i32 %e
+}
+
+; Neither known-bits query uses a constant vector select condition per lane.
+; InstCombine rewrites this to an insertelement, which is handled.
+define i32 @select_sdiv(<4 x i32> %a, <4 x i32> %b, ptr %p, ptr %p2) {
+; CHECK-LABEL: select_sdiv(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-NEXT: .reg .b64 %rd<3>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [select_sdiv_param_2];
+; CHECK-NEXT: ld.param.b64 %rd2, [select_sdiv_param_3];
+; CHECK-NEXT: st.b32 [%rd1], %r1;
+; CHECK-NEXT: xor.b32 %r3, %r2, 85;
+; CHECK-NEXT: st.b32 [%rd2], %r3;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r1;
+; CHECK-NEXT: ret;
+ %d = select <4 x i1> <i1 false, i1 true, i1 false, i1 false>, <4 x i32> zeroinitializer, <4 x i32> %b
+ %m = icmp ne <4 x i32> %d, zeroinitializer
+ %r = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> %a, <4 x i32> %d, <4 x i1> %m)
+ %f = freeze <4 x i32> %r
+ %e = extractelement <4 x i32> %f, i64 1
+ store i32 %e, ptr %p
+ %x = xor i32 %e, 85
+ store i32 %x, ptr %p2
+ ret i32 %e
+}
diff --git a/llvm/test/CodeGen/NVPTX/masked-divrem-scalar.ll b/llvm/test/CodeGen/NVPTX/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..4df1a9d27a0905
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/masked-divrem-scalar.ll
@@ -0,0 +1,354 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_80 -O2 < %s | FileCheck %s
+
+define i8 @udiv_i8(i8 %a, i8 %b, i1 %m) {
+; CHECK-LABEL: udiv_i8(
+; CHECK: {
+; CHECK-NEXT: .reg .b16 %rs<4>;
+; CHECK-NEXT: .reg .b32 %r<2>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b8 %rs1, [udiv_i8_param_0];
+; CHECK-NEXT: ld.param.b8 %rs2, [udiv_i8_param_1];
+; CHECK-NEXT: div.u16 %rs3, %rs1, %rs2;
+; CHECK-NEXT: cvt.u32.u16 %r1, %rs3;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r1;
+; CHECK-NEXT: ret;
+ %r = call i8 @llvm.masked.udiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i16 @sdiv_i16(i16 %a, i16 %b, i1 %m) {
+; CHECK-LABEL: sdiv_i16(
+; CHECK: {
+; CHECK-NEXT: .reg .b16 %rs<4>;
+; CHECK-NEXT: .reg .b32 %r<2>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b16 %rs1, [sdiv_i16_param_0];
+; CHECK-NEXT: ld.param.b16 %rs2, [sdiv_i16_param_1];
+; CHECK-NEXT: div.s16 %rs3, %rs1, %rs2;
+; CHECK-NEXT: cvt.u32.u16 %r1, %rs3;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r1;
+; CHECK-NEXT: ret;
+ %r = call i16 @llvm.masked.sdiv.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i32 @udiv_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: udiv_i32(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b32 %r1, [udiv_i32_param_0];
+; CHECK-NEXT: ld.param.b32 %r2, [udiv_i32_param_1];
+; CHECK-NEXT: div.u32 %r3, %r1, %r2;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r3;
+; CHECK-NEXT: ret;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: sdiv_i32(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b32 %r1, [sdiv_i32_param_0];
+; CHECK-NEXT: ld.param.b32 %r2, [sdiv_i32_param_1];
+; CHECK-NEXT: div.s32 %r3, %r1, %r2;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r3;
+; CHECK-NEXT: ret;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: urem_i32(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b32 %r1, [urem_i32_param_0];
+; CHECK-NEXT: ld.param.b32 %r2, [urem_i32_param_1];
+; CHECK-NEXT: rem.u32 %r3, %r1, %r2;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r3;
+; CHECK-NEXT: ret;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i64 @srem_i64(i64 %a, i64 %b, i1 %m) {
+; CHECK-LABEL: srem_i64(
+; CHECK: {
+; CHECK-NEXT: .reg .pred %p<2>;
+; CHECK-NEXT: .reg .b32 %r<4>;
+; CHECK-NEXT: .reg .b64 %rd<6>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [srem_i64_param_0];
+; CHECK-NEXT: ld.param.b64 %rd2, [srem_i64_param_1];
+; CHECK-NEXT: or.b64 %rd3, %rd1, %rd2;
+; CHECK-NEXT: and.b64 %rd4, %rd3, -4294967296;
+; CHECK-NEXT: setp.ne.b64 %p1, %rd4, 0;
+; CHECK-NEXT: @%p1 bra $L__BB5_2;
+; CHECK-NEXT: // %bb.1:
+; CHECK-NEXT: cvt.u32.u64 %r1, %rd2;
+; CHECK-NEXT: cvt.u32.u64 %r2, %rd1;
+; CHECK-NEXT: rem.u32 %r3, %r2, %r1;
+; CHECK-NEXT: cvt.u64.u32 %rd5, %r3;
+; CHECK-NEXT: bra.uni $L__BB5_3;
+; CHECK-NEXT: $L__BB5_2:
+; CHECK-NEXT: rem.s64 %rd5, %rd1, %rd2;
+; CHECK-NEXT: $L__BB5_3: // %.split
+; CHECK-NEXT: st.param.b64 [func_retval0], %rd5;
+; CHECK-NEXT: ret;
+ %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i128 @wide_guard(i128 %a, i128 %b, i1 %m) {
+; CHECK-LABEL: wide_guard(
+; CHECK: {
+; CHECK-NEXT: .reg .pred %p<15>;
+; CHECK-NEXT: .reg .b16 %rs<3>;
+; CHECK-NEXT: .reg .b32 %r<32>;
+; CHECK-NEXT: .reg .b64 %rd<53>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0: // %_udiv-special-cases
+; CHECK-NEXT: ld.param.v2.b64 {%rd5, %rd6}, [wide_guard_param_0];
+; CHECK-NEXT: ld.param.b8 %rs1, [wide_guard_param_2];
+; CHECK-NEXT: and.b16 %rs2, %rs1, 1;
+; CHECK-NEXT: setp.ne.b16 %p1, %rs2, 0;
+; CHECK-NEXT: ld.param.v2.b64 {%rd7, %rd8}, [wide_guard_param_1];
+; CHECK-NEXT: selp.b64 %rd2, %rd8, 0, %p1;
+; CHECK-NEXT: selp.b64 %rd1, %rd7, 1, %p1;
+; CHECK-NEXT: or.b64 %rd9, %rd1, %rd2;
+; CHECK-NEXT: or.b64 %rd10, %rd5, %rd6;
+; CHECK-NEXT: setp.eq.b64 %p2, %rd10, 0;
+; CHECK-NEXT: setp.eq.or.b64 %p3, %rd9, 0, %p2;
+; CHECK-NEXT: setp.ne.b64 %p4, %rd2, 0;
+; CHECK-NEXT: clz.b64 %r1, %rd2;
+; CHECK-NEXT: cvt.u64.u32 %rd11, %r1;
+; CHECK-NEXT: clz.b64 %r2, %rd1;
+; CHECK-NEXT: cvt.u64.u32 %rd12, %r2;
+; CHECK-NEXT: add.s64 %rd13, %rd12, 64;
+; CHECK-NEXT: selp.b64 %rd14, %rd11, %rd13, %p4;
+; CHECK-NEXT: setp.ne.b64 %p5, %rd6, 0;
+; CHECK-NEXT: clz.b64 %r3, %rd6;
+; CHECK-NEXT: cvt.u64.u32 %rd15, %r3;
+; CHECK-NEXT: clz.b64 %r4, %rd5;
+; CHECK-NEXT: cvt.u64.u32 %rd16, %r4;
+; CHECK-NEXT: add.s64 %rd17, %rd16, 64;
+; CHECK-NEXT: selp.b64 %rd18, %rd15, %rd17, %p5;
+; CHECK-NEXT: sub.cc.s64 %rd19, %rd14, %rd18;
+; CHECK-NEXT: subc.cc.s64 %rd20, 0, 0;
+; CHECK-NEXT: setp.gt.u64 %p6, %rd19, 127;
+; CHECK-NEXT: setp.eq.and.b64 %p7, %rd20, 0, %p6;
+; CHECK-NEXT: setp.ne.or.b64 %p8, %rd20, 0, %p7;
+; CHECK-NEXT: or.pred %p9, %p3, %p8;
+; CHECK-NEXT: xor.b64 %rd21, %rd19, 127;
+; CHECK-NEXT: or.b64 %rd22, %rd21, %rd20;
+; CHECK-NEXT: selp.b64 %rd52, 0, %rd6, %p9;
+; CHECK-NEXT: selp.b64 %rd51, 0, %rd5, %p9;
+; CHECK-NEXT: setp.eq.or.b64 %p10, %rd22, 0, %p9;
+; CHECK-NEXT: @%p10 bra $L__BB6_5;
+; CHECK-NEXT: // %bb.1: // %udiv-bb1
+; CHECK-NEXT: add.cc.s64 %rd45, %rd19, 1;
+; CHECK-NEXT: addc.cc.s64 %rd46, %rd20, 0;
+; CHECK-NEXT: or.b64 %rd23, %rd45, %rd46;
+; CHECK-NEXT: setp.eq.b64 %p11, %rd23, 0;
+; CHECK-NEXT: cvt.u32.u64 %r5, %rd19;
+; CHECK-NEXT: sub.s32 %r6, 127, %r5;
+; CHECK-NEXT: shl.b64 %rd24, %rd6, %r6;
+; CHECK-NEXT: sub.s32 %r7, 64, %r6;
+; CHECK-NEXT: shr.u64 %rd25, %rd5, %r7;
+; CHECK-NEXT: or.b64 %rd26, %rd24, %rd25;
+; CHECK-NEXT: sub.s32 %r8, 63, %r5;
+; CHECK-NEXT: shl.b64 %rd27, %rd5, %r8;
+; CHECK-NEXT: setp.gt.s32 %p12, %r6, 63;
+; CHECK-NEXT: selp.b64 %rd50, %rd27, %rd26, %p12;
+; CHECK-NEXT: shl.b64 %rd49, %rd5, %r6;
+; CHECK-NEXT: mov.b64 %rd44, 0;
+; CHECK-NEXT: mov.b64 %rd43, %rd44;
+; CHECK-NEXT: @%p11 bra $L__BB6_4;
+; CHECK-NEXT: // %bb.2: // %udiv-preheader
+; CHECK-NEXT: cvt.u32.u64 %r9, %rd45;
+; CHECK-NEXT: shr.u64 %rd28, %rd5, %r9;
+; CHECK-NEXT: sub.s32 %r10, 64, %r9;
+; CHECK-NEXT: shl.b64 %rd29, %rd6, %r10;
+; CHECK-NEXT: or.b64 %rd30, %rd28, %rd29;
+; CHECK-NEXT: add.s32 %r11, %r9, -64;
+; CHECK-NEXT: shr.u64 %rd31, %rd6, %r11;
+; CHECK-NEXT: setp.gt.s32 %p13, %r9, 63;
+; CHECK-NEXT: selp.b64 %rd47, %rd31, %rd30, %p13;
+; CHECK-NEXT: shr.u64 %rd48, %rd6, %r9;
+; CHECK-NEXT: add.cc.s64 %rd3, %rd1, -1;
+; CHECK-NEXT: addc.cc.s64 %rd4, %rd2, -1;
+; CHECK-NEXT: mov.b64 %rd43, %rd44;
+; CHECK-NEXT: $L__BB6_3: // %udiv-do-while
+; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: mov.b64 {%r12, %r13}, %rd48;
+; CHECK-NEXT: mov.b64 {%r14, %r15}, %rd47;
+; CHECK-NEXT: shf.l.wrap.b32 %r16, %r15, %r12, 1;
+; CHECK-NEXT: shf.l.wrap.b32 %r17, %r12, %r13, 1;
+; CHECK-NEXT: mov.b64 %rd32, {%r16, %r17};
+; CHECK-NEXT: mov.b64 {%r18, %r19}, %rd50;
+; CHECK-NEXT: shf.l.wrap.b32 %r20, %r19, %r14, 1;
+; CHECK-NEXT: shf.l.wrap.b32 %r21, %r14, %r15, 1;
+; CHECK-NEXT: mov.b64 %rd33, {%r20, %r21};
+; CHECK-NEXT: mov.b64 {%r22, %r23}, %rd49;
+; CHECK-NEXT: shf.l.wrap.b32 %r24, %r23, %r18, 1;
+; CHECK-NEXT: shf.l.wrap.b32 %r25, %r18, %r19, 1;
+; CHECK-NEXT: mov.b64 %rd34, {%r24, %r25};
+; CHECK-NEXT: shl.b64 %rd35, %rd49, 1;
+; CHECK-NEXT: or.b64 %rd49, %rd43, %rd35;
+; CHECK-NEXT: or.b64 %rd50, %rd44, %rd34;
+; CHECK-NEXT: sub.cc.s64 %rd36, %rd3, %rd33;
+; CHECK-NEXT: subc.cc.s64 %rd37, %rd4, %rd32;
+; CHECK-NEXT: shr.s64 %rd38, %rd37, 63;
+; CHECK-NEXT: and.b64 %rd43, %rd38, 1;
+; CHECK-NEXT: and.b64 %rd39, %rd38, %rd1;
+; CHECK-NEXT: and.b64 %rd40, %rd38, %rd2;
+; CHECK-NEXT: sub.cc.s64 %rd47, %rd33, %rd39;
+; CHECK-NEXT: subc.cc.s64 %rd48, %rd32, %rd40;
+; CHECK-NEXT: add.cc.s64 %rd45, %rd45, -1;
+; CHECK-NEXT: addc.cc.s64 %rd46, %rd46, -1;
+; CHECK-NEXT: or.b64 %rd41, %rd45, %rd46;
+; CHECK-NEXT: setp.eq.b64 %p14, %rd41, 0;
+; CHECK-NEXT: @!%p14 bra $L__BB6_3;
+; CHECK-NEXT: $L__BB6_4: // %udiv-loop-exit
+; CHECK-NEXT: mov.b64 {%r26, %r27}, %rd49;
+; CHECK-NEXT: mov.b64 {%r28, %r29}, %rd50;
+; CHECK-NEXT: shf.l.wrap.b32 %r30, %r27, %r28, 1;
+; CHECK-NEXT: shf.l.wrap.b32 %r31, %r28, %r29, 1;
+; CHECK-NEXT: mov.b64 %rd52, {%r30, %r31};
+; CHECK-NEXT: shl.b64 %rd42, %rd49, 1;
+; CHECK-NEXT: or.b64 %rd51, %rd43, %rd42;
+; CHECK-NEXT: $L__BB6_5: // %udiv-end
+; CHECK-NEXT: st.param.v2.b64 [func_retval0], {%rd51, %rd52};
+; CHECK-NEXT: ret;
+ %r = call i128 @llvm.masked.udiv.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
+
+define i32 @scalar_disabled_zero(i32 %a, ptr %p, ptr %p2) {
+; CHECK-LABEL: scalar_disabled_zero(
+; CHECK: {
+; CHECK-NEXT: .reg .b64 %rd<3>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [scalar_disabled_zero_param_1];
+; CHECK-NEXT: st.b32 [%rd1], 0;
+; CHECK-NEXT: ld.param.b64 %rd2, [scalar_disabled_zero_param_2];
+; CHECK-NEXT: st.b32 [%rd2], 1234;
+; CHECK-NEXT: st.param.b32 [func_retval0], 0;
+; CHECK-NEXT: ret;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 0, i1 false)
+ %f = freeze i32 %r
+ store i32 %f, ptr %p
+ %x = xor i32 %f, 1234
+ store i32 %x, ptr %p2
+ ret i32 %f
+}
+
+define i32 @known_zero_lane_ir(<4 x i32> %a, <4 x i32> %b, ptr %p, ptr %p2) {
+; CHECK-LABEL: known_zero_lane_ir(
+; CHECK: {
+; CHECK-NEXT: .reg .b64 %rd<3>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [known_zero_lane_ir_param_2];
+; CHECK-NEXT: ld.param.b64 %rd2, [known_zero_lane_ir_param_3];
+; CHECK-NEXT: st.b32 [%rd1], 0;
+; CHECK-NEXT: st.b32 [%rd2], 1234;
+; CHECK-NEXT: st.param.b32 [func_retval0], 0;
+; CHECK-NEXT: ret;
+ %z = and <4 x i32> %b, <i32 -1, i32 0, i32 -1, i32 -1>
+ %m = icmp ne <4 x i32> %z, zeroinitializer
+ %r = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> %a, <4 x i32> %z, <4 x i1> %m)
+ %f = freeze <4 x i32> %r
+ %e = extractelement <4 x i32> %f, i64 1
+ store i32 %e, ptr %p
+ %x = xor i32 %e, 1234
+ store i32 %x, ptr %p2
+ ret i32 %e
+}
+
+define i32 @known_zero_lane_bitcast(<2 x i32> %a, i64 %b, ptr %p, ptr %p2) {
+; CHECK-LABEL: known_zero_lane_bitcast(
+; CHECK: {
+; CHECK-NEXT: .reg .b64 %rd<3>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [known_zero_lane_bitcast_param_2];
+; CHECK-NEXT: ld.param.b64 %rd2, [known_zero_lane_bitcast_param_3];
+; CHECK-NEXT: st.b32 [%rd1], 0;
+; CHECK-NEXT: st.b32 [%rd2], 1234;
+; CHECK-NEXT: st.param.b32 [func_retval0], 0;
+; CHECK-NEXT: ret;
+ %masked = and i64 %b, 4294967295
+ %v = bitcast i64 %masked to <2 x i32>
+ %m = icmp ne <2 x i32> %v, zeroinitializer
+ %r = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> %a, <2 x i32> %v, <2 x i1> %m)
+ %f = freeze <2 x i32> %r
+ %e = extractelement <2 x i32> %f, i64 1
+ store i32 %e, ptr %p
+ %x = xor i32 %e, 1234
+ store i32 %x, ptr %p2
+ ret i32 %e
+}
+
+define <4 x i32> @poison_lane(<4 x i32> %a) {
+; CHECK-LABEL: poison_lane(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<9>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.v4.b32 {%r1, %r2, %r3, %r4}, [poison_lane_param_0];
+; CHECK-NEXT: mul.hi.u32 %r5, %r3, -858993459;
+; CHECK-NEXT: shr.u32 %r6, %r5, 2;
+; CHECK-NEXT: mul.hi.u32 %r7, %r1, -1431655765;
+; CHECK-NEXT: shr.u32 %r8, %r7, 1;
+; CHECK-NEXT: st.param.v4.b32 [func_retval0], {%r8, 0, %r6, 0};
+; CHECK-NEXT: ret;
+ %r = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> %a, <4 x i32> <i32 3, i32 poison, i32 5, i32 0>, <4 x i1> <i1 true, i1 false, i1 true, i1 false>)
+ %f = freeze <4 x i32> %r
+ ret <4 x i32> %f
+}
+
+define i8 @sdiv_i8(i8 %a, i8 %b, i1 %m) {
+; CHECK-LABEL: sdiv_i8(
+; CHECK: {
+; CHECK-NEXT: .reg .b16 %rs<4>;
+; CHECK-NEXT: .reg .b32 %r<2>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.s8 %rs1, [sdiv_i8_param_0];
+; CHECK-NEXT: ld.param.s8 %rs2, [sdiv_i8_param_1];
+; CHECK-NEXT: div.s16 %rs3, %rs1, %rs2;
+; CHECK-NEXT: cvt.u32.u16 %r1, %rs3;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r1;
+; CHECK-NEXT: ret;
+ %r = call i8 @llvm.masked.sdiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i32 @sdiv_poison_mask(i32 %a, ptr %p) {
+; CHECK-LABEL: sdiv_poison_mask(
+; CHECK: {
+; CHECK-NEXT: .reg .b32 %r<2>;
+; CHECK-NEXT: .reg .b64 %rd<2>;
+; CHECK-EMPTY:
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: ld.param.b64 %rd1, [sdiv_poison_mask_param_1];
+; CHECK-NEXT: st.b32 [%rd1], %r1;
+; CHECK-NEXT: st.param.b32 [func_retval0], %r1;
+; CHECK-NEXT: ret;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 3, i1 poison)
+ %f = freeze i32 %r
+ store i32 %f, ptr %p
+ ret i32 %f
+}
diff --git a/llvm/test/CodeGen/NVPTX/masked-divrem.ll b/llvm/test/CodeGen/NVPTX/masked-divrem.ll
index 763c44b523aa63..9958b8ef9d4f5d 100644
--- a/llvm/test/CodeGen/NVPTX/masked-divrem.ll
+++ b/llvm/test/CodeGen/NVPTX/masked-divrem.ll
@@ -1,5 +1,5 @@
-; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_90a < %s | FileCheck %s
-; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_100a < %s | FileCheck %s
+; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_90a < %s | FileCheck %s --implicit-check-not=selp
+; RUN: llc -mtriple=nvptx64-nvidia-cuda -mcpu=sm_100a < %s | FileCheck %s --implicit-check-not=selp
target triple = "nvptx64-nvidia-cuda"
@@ -11,7 +11,7 @@ declare <8 x i32> @llvm.masked.srem.v8i32(<8 x i32>, <8 x i32>, <8 x i1>)
define <2 x i32> @masked_divrem_v2i32(<2 x i32> %lhs, <2 x i32> %rhs) {
; CHECK-LABEL: masked_divrem_v2i32(
-; CHECK-DAG: selp.b32
+; CHECK-NOT: selp
; CHECK-DAG: div.s32
; CHECK-DAG: div.u32
; CHECK-DAG: rem.s32
@@ -37,6 +37,7 @@ define <2 x i32> @masked_divrem_v2i32(<2 x i32> %lhs, <2 x i32> %rhs) {
define <8 x i32> @masked_srem_v8i32(<8 x i32> %lhs, <8 x i32> %rhs) {
; CHECK-LABEL: masked_srem_v8i32(
; CHECK-COUNT-5: rem.s32
+; CHECK-NOT: rem.s32
; CHECK: ret;
%result = call <8 x i32> @llvm.masked.srem.v8i32(
<8 x i32> %lhs, <8 x i32> %rhs,
diff --git a/llvm/test/Transforms/CodeGenPrepare/NVPTX/masked-divrem.ll b/llvm/test/Transforms/CodeGenPrepare/NVPTX/masked-divrem.ll
new file mode 100644
index 00000000000000..d5f91a72405c0f
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/NVPTX/masked-divrem.ll
@@ -0,0 +1,394 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -mtriple=nvptx64-nvidia-cuda -mcpu=sm_80 < %s | FileCheck %s
+
+define i64 @udiv_unknown(i64 %a, i64 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @udiv_unknown(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i64 [[A]]
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i64 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[DIVIDEND_FROZEN]], [[DIVISOR_FROZEN]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[DIVISOR_FROZEN]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[DIVIDEND_FROZEN]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[DIVIDEND_FROZEN]], i64 [[DIVISOR_FROZEN]], i1 [[M]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP11:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP10]], %[[BB9]] ]
+; CHECK-NEXT: ret i64 [[TMP11]]
+;
+ %r = call i64 @llvm.masked.udiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @udiv_known_short(i32 %a, i32 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @udiv_known_short(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AZ:%.*]] = zext i32 [[A]] to i64
+; CHECK-NEXT: [[BZ:%.*]] = zext i32 [[B]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[AZ]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[BZ]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[M]])
+; CHECK-NEXT: [[TMP4:%.*]] = zext i32 [[TMP3]] to i64
+; CHECK-NEXT: ret i64 [[TMP4]]
+;
+ %az = zext i32 %a to i64
+ %bz = zext i32 %b to i64
+ %r = call i64 @llvm.masked.udiv.i64(i64 %az, i64 %bz, i1 %m)
+ ret i64 %r
+}
+
+define i64 @sdiv_unknown(i64 %a, i64 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @sdiv_unknown(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i64 [[A]]
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i64 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[DIVIDEND_FROZEN]], [[DIVISOR_FROZEN]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[DIVISOR_FROZEN]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[DIVIDEND_FROZEN]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = icmp ne i64 [[DIVIDEND_FROZEN]], -9223372036854775808
+; CHECK-NEXT: [[TMP11:%.*]] = icmp ne i64 [[DIVISOR_FROZEN]], -1
+; CHECK-NEXT: [[TMP12:%.*]] = or i1 [[TMP10]], [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = select i1 [[TMP12]], i1 [[M]], i1 false
+; CHECK-NEXT: [[TMP14:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[DIVIDEND_FROZEN]], i64 [[DIVISOR_FROZEN]], i1 [[TMP13]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP15:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP14]], %[[BB9]] ]
+; CHECK-NEXT: ret i64 [[TMP15]]
+;
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @sdiv_short_dividend(i32 %a, i64 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @sdiv_short_dividend(
+; CHECK-SAME: i32 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AZ:%.*]] = zext i32 [[A]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[AZ]] to i32
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i32 [[TMP1]]
+; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[DIVIDEND_FROZEN]] to i64
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i64 [[B]]
+; CHECK-NEXT: [[TMP3:%.*]] = and i64 [[DIVISOR_FROZEN]], -4294967296
+; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i64 [[TMP3]], 0
+; CHECK-NEXT: br i1 [[TMP4]], label %[[BB5:.*]], label %[[BB10:.*]]
+; CHECK: [[BB5]]:
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[DIVISOR_FROZEN]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[TMP2]] to i32
+; CHECK-NEXT: [[TMP8:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP7]], i32 [[TMP6]], i1 [[M]])
+; CHECK-NEXT: [[TMP9:%.*]] = zext i32 [[TMP8]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB10]]:
+; CHECK-NEXT: [[TMP11:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[TMP2]], i64 [[DIVISOR_FROZEN]], i1 [[M]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP16:%.*]] = phi i64 [ [[TMP9]], %[[BB5]] ], [ [[TMP11]], %[[BB10]] ]
+; CHECK-NEXT: ret i64 [[TMP16]]
+;
+ %az = zext i32 %a to i64
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %az, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @urem_noundef(i64 noundef %a, i64 noundef %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @urem_noundef(
+; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[A]], [[B]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[B]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[A]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[A]], i64 [[B]], i1 [[M]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP11:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP10]], %[[BB9]] ]
+; CHECK-NEXT: ret i64 [[TMP11]]
+;
+ %r = call i64 @llvm.masked.urem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @srem_unknown(i64 %a, i64 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @srem_unknown(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i64 [[A]]
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i64 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[DIVIDEND_FROZEN]], [[DIVISOR_FROZEN]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[DIVISOR_FROZEN]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[DIVIDEND_FROZEN]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = icmp ne i64 [[DIVIDEND_FROZEN]], -9223372036854775808
+; CHECK-NEXT: [[TMP11:%.*]] = icmp ne i64 [[DIVISOR_FROZEN]], -1
+; CHECK-NEXT: [[TMP12:%.*]] = or i1 [[TMP10]], [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = select i1 [[TMP12]], i1 [[M]], i1 false
+; CHECK-NEXT: [[TMP14:%.*]] = call i64 @llvm.masked.srem.i64(i64 [[DIVIDEND_FROZEN]], i64 [[DIVISOR_FROZEN]], i1 [[TMP13]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP15:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP14]], %[[BB9]] ]
+; CHECK-NEXT: ret i64 [[TMP15]]
+;
+ %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+; Cache entries with different masks or ordinary semantics must stay distinct.
+
+define i64 @different_masks(i64 noundef %a, i64 noundef %b, i1 %m, i1 %n) {
+;
+; CHECK-LABEL: define i64 @different_masks(
+; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]], i1 [[M:%.*]], i1 [[N:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[A]], [[B]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[B]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[A]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[A]], i64 [[B]], i1 [[M]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP11:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP10]], %[[BB9]] ]
+; CHECK-NEXT: [[TMP12:%.*]] = or i64 [[A]], [[B]]
+; CHECK-NEXT: [[TMP13:%.*]] = and i64 [[TMP12]], -4294967296
+; CHECK-NEXT: [[TMP14:%.*]] = icmp eq i64 [[TMP13]], 0
+; CHECK-NEXT: br i1 [[TMP14]], label %[[BB15:.*]], label %[[BB20:.*]]
+; CHECK: [[BB15]]:
+; CHECK-NEXT: [[TMP16:%.*]] = trunc i64 [[B]] to i32
+; CHECK-NEXT: [[TMP17:%.*]] = trunc i64 [[A]] to i32
+; CHECK-NEXT: [[TMP18:%.*]] = call i32 @llvm.masked.urem.i32(i32 [[TMP17]], i32 [[TMP16]], i1 [[N]])
+; CHECK-NEXT: [[TMP19:%.*]] = zext i32 [[TMP18]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT_SPLIT:.*]]
+; CHECK: [[BB20]]:
+; CHECK-NEXT: [[TMP21:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[A]], i64 [[B]], i1 [[N]])
+; CHECK-NEXT: br label %[[DOTSPLIT_SPLIT]]
+; CHECK: [[DOTSPLIT_SPLIT]]:
+; CHECK-NEXT: [[TMP22:%.*]] = phi i64 [ [[TMP19]], %[[BB15]] ], [ [[TMP21]], %[[BB20]] ]
+; CHECK-NEXT: [[TMP23:%.*]] = or i64 [[A]], [[B]]
+; CHECK-NEXT: [[TMP24:%.*]] = and i64 [[TMP23]], -4294967296
+; CHECK-NEXT: [[TMP25:%.*]] = icmp eq i64 [[TMP24]], 0
+; CHECK-NEXT: br i1 [[TMP25]], label %[[BB26:.*]], label %[[BB31:.*]]
+; CHECK: [[BB26]]:
+; CHECK-NEXT: [[TMP27:%.*]] = trunc i64 [[B]] to i32
+; CHECK-NEXT: [[TMP28:%.*]] = trunc i64 [[A]] to i32
+; CHECK-NEXT: [[TMP29:%.*]] = udiv i32 [[TMP28]], [[TMP27]]
+; CHECK-NEXT: [[TMP30:%.*]] = zext i32 [[TMP29]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT_SPLIT_SPLIT:.*]]
+; CHECK: [[BB31]]:
+; CHECK-NEXT: [[TMP32:%.*]] = udiv i64 [[A]], [[B]]
+; CHECK-NEXT: br label %[[DOTSPLIT_SPLIT_SPLIT]]
+; CHECK: [[DOTSPLIT_SPLIT_SPLIT]]:
+; CHECK-NEXT: [[TMP33:%.*]] = phi i64 [ [[TMP30]], %[[BB26]] ], [ [[TMP32]], %[[BB31]] ]
+; CHECK-NEXT: [[QR:%.*]] = add i64 [[TMP11]], [[TMP22]]
+; CHECK-NEXT: [[RES:%.*]] = add i64 [[QR]], [[TMP33]]
+; CHECK-NEXT: ret i64 [[RES]]
+;
+ %q = call i64 @llvm.masked.udiv.i64(i64 %a, i64 %b, i1 %m)
+ %r = call i64 @llvm.masked.urem.i64(i64 %a, i64 %b, i1 %n)
+ %plain = udiv i64 %a, %b
+ %qr = add i64 %q, %r
+ %res = add i64 %qr, %plain
+ ret i64 %res
+}
+
+; Freezing the branch condition alone allowed a zero truncated divisor on the
+; fast path even though a poison dividend and a nonzero i64 divisor are safe.
+
+define i64 @poison_dividend(i64 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @poison_dividend(
+; CHECK-SAME: i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i64 poison
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i64 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[DIVIDEND_FROZEN]], [[DIVISOR_FROZEN]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[DIVISOR_FROZEN]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[DIVIDEND_FROZEN]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[DIVIDEND_FROZEN]], i64 [[DIVISOR_FROZEN]], i1 [[M]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP11:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP10]], %[[BB9]] ]
+; CHECK-NEXT: ret i64 [[TMP11]]
+;
+ %r = call i64 @llvm.masked.udiv.i64(i64 poison, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+; A frozen poison dividend can become MIN. The residual signed operation must
+; not introduce overflow UB when the original dividend only produced poison.
+
+define i64 @signed_poison_dividend(i64 %b, i1 %m) {
+;
+; CHECK-LABEL: define i64 @signed_poison_dividend(
+; CHECK-SAME: i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i64 poison
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i64 [[B]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[DIVIDEND_FROZEN]], [[DIVISOR_FROZEN]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -4294967296
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
+; CHECK-NEXT: br i1 [[TMP3]], label %[[BB4:.*]], label %[[BB9:.*]]
+; CHECK: [[BB4]]:
+; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[DIVISOR_FROZEN]] to i32
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[DIVIDEND_FROZEN]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP6]], i32 [[TMP5]], i1 [[M]])
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP7]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = icmp ne i64 [[DIVIDEND_FROZEN]], -9223372036854775808
+; CHECK-NEXT: [[TMP11:%.*]] = icmp ne i64 [[DIVISOR_FROZEN]], -1
+; CHECK-NEXT: [[TMP12:%.*]] = or i1 [[TMP10]], [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = select i1 [[TMP12]], i1 [[M]], i1 false
+; CHECK-NEXT: [[TMP14:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[DIVIDEND_FROZEN]], i64 [[DIVISOR_FROZEN]], i1 [[TMP13]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP15:%.*]] = phi i64 [ [[TMP8]], %[[BB4]] ], [ [[TMP14]], %[[BB9]] ]
+; CHECK-NEXT: ret i64 [[TMP15]]
+;
+ %r = call i64 @llvm.masked.sdiv.i64(i64 poison, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @m_short(i64 %x, i64 %y, i1 %m) {
+;
+; CHECK-LABEL: define i64 @m_short(
+; CHECK-SAME: i64 [[X:%.*]], i64 [[Y:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = and i64 [[X]], 4294967295
+; CHECK-NEXT: [[B:%.*]] = and i64 [[Y]], 4294967295
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[A]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[B]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP1]], i32 [[TMP2]], i1 [[M]])
+; CHECK-NEXT: [[TMP4:%.*]] = zext i32 [[TMP3]] to i64
+; CHECK-NEXT: ret i64 [[TMP4]]
+;
+ %a = and i64 %x, 4294967295
+ %b = and i64 %y, 4294967295
+ %r = call i64 @llvm.masked.udiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @m_long(i64 %x, i64 %y, i1 %m) {
+;
+; CHECK-LABEL: define i64 @m_long(
+; CHECK-SAME: i64 [[X:%.*]], i64 [[Y:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = or i64 [[X]], 1099511627776
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[A]], i64 [[Y]], i1 [[M]])
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %a = or i64 %x, 1099511627776
+ %r = call i64 @llvm.masked.udiv.i64(i64 %a, i64 %y, i1 %m)
+ ret i64 %r
+}
+
+define i64 @m_hash(i64 %x, i64 %z, i64 %y, i1 %m) {
+;
+; CHECK-LABEL: define i64 @m_hash(
+; CHECK-SAME: i64 [[X:%.*]], i64 [[Z:%.*]], i64 [[Y:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[A:%.*]] = xor i64 [[X]], [[Z]]
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[A]], i64 [[Y]], i1 [[M]])
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %a = xor i64 %x, %z
+ %r = call i64 @llvm.masked.urem.i64(i64 %a, i64 %y, i1 %m)
+ ret i64 %r
+}
+
+define i64 @m_short_divisor(i64 %x, i32 %y, i1 %m) {
+; CHECK-LABEL: define i64 @m_short_divisor(
+; CHECK-SAME: i64 [[X:%.*]], i32 [[Y:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[B:%.*]] = zext i32 [[Y]] to i64
+; CHECK-NEXT: [[DIVIDEND_FROZEN:%.*]] = freeze i64 [[X]]
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[B]] to i32
+; CHECK-NEXT: [[DIVISOR_FROZEN:%.*]] = freeze i32 [[TMP1]]
+; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[DIVISOR_FROZEN]] to i64
+; CHECK-NEXT: [[TMP3:%.*]] = and i64 [[DIVIDEND_FROZEN]], -4294967296
+; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i64 [[TMP3]], 0
+; CHECK-NEXT: br i1 [[TMP4]], label %[[BB5:.*]], label %[[BB10:.*]]
+; CHECK: [[BB5]]:
+; CHECK-NEXT: [[TMP6:%.*]] = trunc i64 [[TMP2]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[DIVIDEND_FROZEN]] to i32
+; CHECK-NEXT: [[TMP8:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP7]], i32 [[TMP6]], i1 [[M]])
+; CHECK-NEXT: [[TMP9:%.*]] = zext i32 [[TMP8]] to i64
+; CHECK-NEXT: br label %[[DOTSPLIT:.*]]
+; CHECK: [[BB10]]:
+; CHECK-NEXT: [[TMP11:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[DIVIDEND_FROZEN]], i64 [[TMP2]], i1 [[M]])
+; CHECK-NEXT: br label %[[DOTSPLIT]]
+; CHECK: [[DOTSPLIT]]:
+; CHECK-NEXT: [[TMP12:%.*]] = phi i64 [ [[TMP9]], %[[BB5]] ], [ [[TMP11]], %[[BB10]] ]
+; CHECK-NEXT: ret i64 [[TMP12]]
+;
+ %b = zext i32 %y to i64
+ %r = call i64 @llvm.masked.udiv.i64(i64 %x, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @non_divrem_intrinsic(i64 %x) {
+; CHECK-LABEL: define i64 @non_divrem_intrinsic(
+; CHECK-SAME: i64 [[X:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.abs.i64(i64 [[X]], i1 false)
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %r = call i64 @llvm.abs.i64(i64 %x, i1 false)
+ ret i64 %r
+}
+
+define i64 @udiv_operand_bundle(i64 %a, i64 %b, i1 %m, ptr %p) {
+; CHECK-LABEL: define i64 @udiv_operand_bundle(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]], ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[A]], i64 [[B]], i1 [[M]]) [ "unknown"(ptr [[P]]) ]
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %r = call i64 @llvm.masked.udiv.i64(i64 %a, i64 %b, i1 %m) [ "unknown"(ptr %p) ]
+ ret i64 %r
+}
+
+define i64 @udiv_musttail(i64 %a, i64 %b, i1 %m) {
+; CHECK-LABEL: define i64 @udiv_musttail(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = musttail call i64 @llvm.masked.udiv.i64(i64 [[A]], i64 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %r = musttail call i64 @llvm.masked.udiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
>From 7405609e00b2303a55107d93768bcc15a68c39f4 Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 12:40:34 +0100
Subject: [PATCH 5/9] [AMDGPU] Recognize trap-free integer div/rem and expand
masked forms
GCN expands `i32`/`i64` integer div/rem, and promotes `i8`/`i16` to
`i32`, into non-trapping ALU sequences without libcalls under the
default FP environment (`EXCPEnable == 0`). Treat scalar `i8`, `i16`,
`i32` and `i64` as trap-free on GCN so masked operations omit the
divisor guard and enable the 64-to-32-bit slow-division bypass, while
R600, `i1`, and wider or non-power-of-2 integer widths stay guarded.
`AMDGPUCodeGenPrepare` expands bundle-free scalar and fixed-vector
masked div/rem using the existing reciprocal and shrinkable `i64`
expansions, which produce a value or poison for exceptional inputs
without operand-dependent branches. Residual operations stay masked (and
fixed-vector residual lanes scalarize like ordinary division so generic
`CodeGenPrepare` can still apply the slow-division bypass), and the
optional general `i64` IR expansion is never used on masked operations.
Constant-false mask lanes and undef or known-zero divisors, including
little-endian bitcasts from scalar integers, fold directly to `0`; late
zeros discovered only after mask elision still reach the generic
`freeze(undef)` limitation.
Assisted-by: Claude
---
.../Target/AMDGPU/AMDGPUCodeGenPrepare.cpp | 160 ++++-
llvm/lib/Target/AMDGPU/SIISelLowering.h | 9 +
.../amdgpu-codegenprepare-masked-divrem.ll | 580 ++++++++++++++++++
llvm/test/CodeGen/AMDGPU/masked-divrem.ll | 496 +++++++++++++++
.../AMDGPU/masked-divrem-vector-zero.ll | 87 +++
5 files changed, 1303 insertions(+), 29 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-masked-divrem.ll
create mode 100644 llvm/test/CodeGen/AMDGPU/masked-divrem.ll
create mode 100644 llvm/test/Transforms/PreISelIntrinsicLowering/AMDGPU/masked-divrem-vector-zero.ll
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp b/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
index 5eb700dc476f69..e8d2b99890d1d7 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
@@ -136,27 +136,27 @@ class AMDGPUCodeGenPrepareImpl
/// Fold select (X == 0 || Y == 0), +0.0, X * Y into fmul_legacy(X, Y).
bool foldSelectToFMulLegacy(SelectInst &I) const;
- bool divHasSpecialOptimization(BinaryOperator &I,
- Value *Num, Value *Den) const;
- unsigned getDivNumBits(BinaryOperator &I, Value *Num, Value *Den,
+ bool divHasSpecialOptimization(Instruction &I, Value *Num, Value *Den) const;
+ unsigned getDivNumBits(Instruction &I, Value *Num, Value *Den,
unsigned MaxDivBits, bool Signed) const;
/// Expands div or rem by using floating-point operations.
/// Operands must be in the range [-0x400000,0x3FFFFF]
- Value *expandDivRemToFloat(IRBuilder<> &Builder, BinaryOperator &I,
- Value *Num, Value *Den, bool IsDiv,
- bool IsSigned) const;
+ Value *expandDivRemToFloat(IRBuilder<> &Builder, Instruction &I, Value *Num,
+ Value *Den, bool IsDiv, bool IsSigned) const;
- Value *expandDivRemToFloatImpl(IRBuilder<> &Builder, BinaryOperator &I,
+ Value *expandDivRemToFloatImpl(IRBuilder<> &Builder, Instruction &I,
Value *Num, Value *Den, unsigned NumBits,
bool IsDiv, bool IsSigned) const;
/// Expands 32 bit div or rem.
- Value* expandDivRem32(IRBuilder<> &Builder, BinaryOperator &I,
- Value *Num, Value *Den) const;
+ Value *expandDivRem32(IRBuilder<> &Builder, Instruction &I,
+ Instruction::BinaryOps Opc, Value *Num,
+ Value *Den) const;
- Value *shrinkDivRem64(IRBuilder<> &Builder, BinaryOperator &I,
- Value *Num, Value *Den) const;
+ Value *shrinkDivRem64(IRBuilder<> &Builder, Instruction &I,
+ Instruction::BinaryOps Opc, Value *Num,
+ Value *Den) const;
void expandDivRem64(BinaryOperator &I) const;
/// Widen a scalar load.
@@ -989,7 +989,7 @@ static Value* getMulHu(IRBuilder<> &Builder, Value *LHS, Value *RHS) {
/// \p MaxDivBits is an optimization hint to bypass the second
/// ComputeNumSignBits/computeKnownBits call if the first one is
/// insufficient.
-unsigned AMDGPUCodeGenPrepareImpl::getDivNumBits(BinaryOperator &I, Value *Num,
+unsigned AMDGPUCodeGenPrepareImpl::getDivNumBits(Instruction &I, Value *Num,
Value *Den,
unsigned MaxDivBits,
bool IsSigned) const {
@@ -1025,9 +1025,8 @@ unsigned AMDGPUCodeGenPrepareImpl::getDivNumBits(BinaryOperator &I, Value *Num,
}
Value *AMDGPUCodeGenPrepareImpl::expandDivRemToFloat(IRBuilder<> &Builder,
- BinaryOperator &I,
- Value *Num, Value *Den,
- bool IsDiv,
+ Instruction &I, Value *Num,
+ Value *Den, bool IsDiv,
bool IsSigned) const {
unsigned DivBits = getDivNumBits(I, Num, Den, 23, IsSigned);
@@ -1038,7 +1037,7 @@ Value *AMDGPUCodeGenPrepareImpl::expandDivRemToFloat(IRBuilder<> &Builder,
}
Value *AMDGPUCodeGenPrepareImpl::expandDivRemToFloatImpl(
- IRBuilder<> &Builder, BinaryOperator &I, Value *Num, Value *Den,
+ IRBuilder<> &Builder, Instruction &I, Value *Num, Value *Den,
unsigned DivBits, bool IsDiv, bool IsSigned) const {
// v_rcp_f32(float(X)) can have an error of 1 ulp.
@@ -1120,7 +1119,7 @@ Value *AMDGPUCodeGenPrepareImpl::expandDivRemToFloatImpl(
// than the general expansion we do here.
// TODO: It would be better to just directly handle those optimizations here.
-bool AMDGPUCodeGenPrepareImpl::divHasSpecialOptimization(BinaryOperator &I,
+bool AMDGPUCodeGenPrepareImpl::divHasSpecialOptimization(Instruction &I,
Value *Num,
Value *Den) const {
if (Constant *C = dyn_cast<Constant>(Den)) {
@@ -1164,9 +1163,9 @@ static Value *getSign32(Value *V, IRBuilder<> &Builder, const DataLayout DL) {
}
Value *AMDGPUCodeGenPrepareImpl::expandDivRem32(IRBuilder<> &Builder,
- BinaryOperator &I, Value *X,
- Value *Y) const {
- Instruction::BinaryOps Opc = I.getOpcode();
+ Instruction &I,
+ Instruction::BinaryOps Opc,
+ Value *X, Value *Y) const {
assert(Opc == Instruction::URem || Opc == Instruction::UDiv ||
Opc == Instruction::SRem || Opc == Instruction::SDiv);
@@ -1288,13 +1287,12 @@ Value *AMDGPUCodeGenPrepareImpl::expandDivRem32(IRBuilder<> &Builder,
}
Value *AMDGPUCodeGenPrepareImpl::shrinkDivRem64(IRBuilder<> &Builder,
- BinaryOperator &I, Value *Num,
- Value *Den) const {
+ Instruction &I,
+ Instruction::BinaryOps Opc,
+ Value *Num, Value *Den) const {
if (!ExpandDiv64InIR && divHasSpecialOptimization(I, Num, Den))
return nullptr; // Keep it for later optimization.
- Instruction::BinaryOps Opc = I.getOpcode();
-
bool IsDiv = Opc == Instruction::SDiv || Opc == Instruction::UDiv;
bool IsSigned = Opc == Instruction::SDiv || Opc == Instruction::SRem;
@@ -1311,7 +1309,7 @@ Value *AMDGPUCodeGenPrepareImpl::shrinkDivRem64(IRBuilder<> &Builder,
// Otherwise 32-bit division cannot be used safely.
// -2147483648/1 and -2147483648/-1 are not equal,
// but they produce the same lower 32-bit result.
- Narrowed = expandDivRem32(Builder, I, Num, Den);
+ Narrowed = expandDivRem32(Builder, I, Opc, Num, Den);
}
if (Narrowed) {
@@ -1438,13 +1436,13 @@ bool AMDGPUCodeGenPrepareImpl::visitBinaryOperator(BinaryOperator &I) {
Value *NewElt;
if (ScalarSize <= 32) {
- NewElt = expandDivRem32(Builder, I, NumEltN, DenEltN);
+ NewElt = expandDivRem32(Builder, I, Opc, NumEltN, DenEltN);
if (!NewElt)
NewElt = Builder.CreateBinOp(Opc, NumEltN, DenEltN);
} else {
// See if this 64-bit division can be shrunk to 32/24-bits before
// producing the general expansion.
- NewElt = shrinkDivRem64(Builder, I, NumEltN, DenEltN);
+ NewElt = shrinkDivRem64(Builder, I, Opc, NumEltN, DenEltN);
if (!NewElt) {
// The general 64-bit expansion introduces control flow and doesn't
// return the new value. Just insert a scalar copy and defer
@@ -1464,9 +1462,9 @@ bool AMDGPUCodeGenPrepareImpl::visitBinaryOperator(BinaryOperator &I) {
}
} else {
if (ScalarSize <= 32)
- NewDiv = expandDivRem32(Builder, I, Num, Den);
+ NewDiv = expandDivRem32(Builder, I, Opc, Num, Den);
else {
- NewDiv = shrinkDivRem64(Builder, I, Num, Den);
+ NewDiv = shrinkDivRem64(Builder, I, Opc, Num, Den);
if (!NewDiv)
Div64ToExpand.push_back(&I);
}
@@ -2063,6 +2061,110 @@ bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
bool AMDGPUCodeGenPrepareImpl::visitIntrinsicInst(IntrinsicInst &I) {
Intrinsic::ID IID = I.getIntrinsicID();
switch (IID) {
+ case Intrinsic::masked_udiv:
+ case Intrinsic::masked_sdiv:
+ case Intrinsic::masked_urem:
+ case Intrinsic::masked_srem: {
+ Type *Ty = I.getType();
+ Type *EltTy = Ty->getScalarType();
+ if (!CLOpts.codegenprepare_idiv_expansion || I.hasOperandBundles() ||
+ I.isMustTailCall() ||
+ (!Ty->isIntegerTy() && !isa<FixedVectorType>(Ty)) ||
+ !ST.getTargetLowering()->isIntDivRemTrapFree(EVT::getEVT(EltTy)))
+ return false;
+ Instruction::BinaryOps Opc = static_cast<Instruction::BinaryOps>(
+ Intrinsic::getMaskedIntDivRemOpcode(IID));
+ IRBuilder<> Builder(&I);
+ Value *Num = I.getArgOperand(0), *Den = I.getArgOperand(1);
+ unsigned EltBits = EltTy->getIntegerBitWidth();
+ SimplifyQuery InstSQ = SQ.getWithInstruction(&I);
+ auto IsBitcastKnownZeroLane = [&](Value *Vec, uint64_t Lane) {
+ Value *Src;
+ if (!InstSQ.DL.isLittleEndian() || !match(Vec, m_BitCast(m_Value(Src))) ||
+ !Src->getType()->isIntegerTy())
+ return false;
+ unsigned SrcBits = Src->getType()->getIntegerBitWidth();
+ if (Lane >= SrcBits / EltBits)
+ return false;
+ return computeKnownBits(Src, InstSQ)
+ .extractBits(EltBits, Lane * EltBits)
+ .isZero();
+ };
+ // Reciprocal expansions use wrapping arithmetic, FP conversions and
+ // selects. Exceptional operands yield a value or poison, never IR UB.
+ auto Expand = [&](Value *X, Value *Y) -> Value * {
+ // Avoid the reciprocal expansion when a scalar or extracted divisor is
+ // known zero, so later lowering does not fold it to undef beneath freeze.
+ if (isa<UndefValue>(Y) || computeKnownBits(Y, InstSQ).isZero())
+ return Constant::getNullValue(EltTy);
+ if (auto *EE = dyn_cast<ExtractElementInst>(Y))
+ if (auto *CI = dyn_cast<ConstantInt>(EE->getIndexOperand());
+ CI && CI->getValue().ult(UINT64_MAX) &&
+ IsBitcastKnownZeroLane(EE->getVectorOperand(), CI->getZExtValue()))
+ return Constant::getNullValue(EltTy);
+ return EltBits <= 32 ? expandDivRem32(Builder, I, Opc, X, Y)
+ : shrinkDivRem64(Builder, I, Opc, X, Y);
+ };
+ Value *NewDiv;
+ if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
+ // IR known bits do not look through a bitcast from a scalar integer, such
+ // as OpenCL as_typeN() or an ABI-coerced vector argument, but later
+ // lowering does and can turn that lane into undef.
+ std::optional<KnownBits> BitcastDenBits;
+ if (Value *Src; InstSQ.DL.isLittleEndian() &&
+ match(Den, m_BitCast(m_Value(Src))) &&
+ Src->getType()->isIntegerTy())
+ BitcastDenBits = computeKnownBits(Src, InstSQ);
+ const auto *ConstDen = dyn_cast<Constant>(Den);
+ const auto *ConstMask = dyn_cast<Constant>(I.getArgOperand(2));
+ NewDiv = PoisonValue::get(VT);
+ for (unsigned N = 0, E = VT->getNumElements(); N != E; ++N) {
+ Constant *MaskElt =
+ ConstMask ? ConstMask->getAggregateElement(N) : nullptr;
+ Constant *DenElt =
+ ConstDen ? ConstDen->getAggregateElement(N) : nullptr;
+ bool KnownZeroLane =
+ (MaskElt && MaskElt->isNullValue()) ||
+ (DenElt && (isa<UndefValue>(DenElt) || DenElt->isNullValue())) ||
+ (BitcastDenBits &&
+ BitcastDenBits->extractBits(EltBits, N * EltBits).isZero()) ||
+ computeKnownBits(Den, APInt::getOneBitSet(E, N), InstSQ).isZero();
+ Value *Elt = nullptr;
+ if (KnownZeroLane) {
+ Elt = Constant::getNullValue(EltTy);
+ } else {
+ Value *Y = Builder.CreateExtractElement(Den, N);
+ if (isa<UndefValue>(Y) || computeKnownBits(Y, InstSQ).isZero()) {
+ if (auto *YI = dyn_cast<Instruction>(Y); YI && YI->use_empty())
+ YI->eraseFromParent();
+ Elt = Constant::getNullValue(EltTy);
+ } else {
+ Value *X = Builder.CreateExtractElement(Num, N);
+ Elt = Expand(X, Y);
+ if (!Elt) {
+ // Scalarize residual lanes like visitBinaryOperator so generic
+ // CodeGenPrepare can still apply the 64-to-32-bit bypass.
+ Value *Mask = Builder.CreateExtractElement(I.getArgOperand(2), N);
+ Elt = Builder.CreateIntrinsic(IID, {EltTy}, {X, Y, Mask});
+ }
+ }
+ }
+ NewDiv = Builder.CreateInsertElement(NewDiv, Elt, N);
+ }
+ } else {
+ const auto *ConstMask = dyn_cast<Constant>(I.getArgOperand(2));
+ NewDiv = (ConstMask && ConstMask->isNullValue())
+ ? Constant::getNullValue(EltTy)
+ : Expand(Num, Den);
+ }
+ if (!NewDiv)
+ return false;
+ // Never use expandDivRem64 here: its operand-dependent branches are unsafe
+ // for poison operands in disabled lanes.
+ I.replaceAllUsesWith(NewDiv);
+ DeadVals.push_back(&I);
+ return true;
+ }
case Intrinsic::minnum:
case Intrinsic::minimumnum:
case Intrinsic::minimum:
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.h b/llvm/lib/Target/AMDGPU/SIISelLowering.h
index f55a4fc68f1ca5..b1b846d23219a3 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.h
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.h
@@ -42,6 +42,15 @@ class SITargetLowering final : public AMDGPUTargetLowering {
std::bitset<MVT::VALUETYPE_SIZE> SBufferLoadDiagnosticVTs;
public:
+ /// GCN expands `i32`/`i64` integer div/rem, and promotes `i8`/`i16` to `i32`,
+ /// into non-trapping ALU sequences without libcalls. The reciprocal
+ /// instruction can set the FP divide-by-zero status bit; under the default FP
+ /// environment status flags are unobservable and kernel exception enables are
+ /// zero (`AMDGPUAsmPrinter::getSIProgramInfo`).
+ bool isIntDivRemTrapFree(EVT VT) const override {
+ return VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64;
+ }
+
MVT getRegisterTypeForCallingConv(LLVMContext &Context,
CallingConv::ID CC,
EVT VT) const override;
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-masked-divrem.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-masked-divrem.ll
new file mode 100644
index 00000000000000..8f5ae79300c1a2
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-masked-divrem.ll
@@ -0,0 +1,580 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -mtriple=amdgpu9.0a-amd-amdhsa -mcpu=gfx90a -passes=amdgpu-codegenprepare -verify-each < %s | FileCheck %s
+; RUN: opt -S -mtriple=amdgpu9.0a-amd-amdhsa -mcpu=gfx90a -passes=amdgpu-codegenprepare -amdgpu-codegenprepare-expand-div64 -verify-each < %s | FileCheck %s
+
+define i32 @udiv_32_unknown(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @udiv_32_unknown(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[TMP1:%.*]] = uitofp fast i32 [[B]] to float
+; CHECK-NEXT: [[TMP2:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP1]])
+; CHECK-NEXT: [[TMP3:%.*]] = fmul fast float [[TMP2]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP4:%.*]] = fptoui float [[TMP3]] to i32
+; CHECK-NEXT: [[TMP5:%.*]] = sub i32 0, [[B]]
+; CHECK-NEXT: [[TMP6:%.*]] = mul i32 [[TMP5]], [[TMP4]]
+; CHECK-NEXT: [[TMP7:%.*]] = zext i32 [[TMP4]] to i64
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP6]] to i64
+; CHECK-NEXT: [[TMP9:%.*]] = mul i64 [[TMP7]], [[TMP8]]
+; CHECK-NEXT: [[TMP10:%.*]] = trunc i64 [[TMP9]] to i32
+; CHECK-NEXT: [[TMP11:%.*]] = lshr i64 [[TMP9]], 32
+; CHECK-NEXT: [[TMP12:%.*]] = trunc i64 [[TMP11]] to i32
+; CHECK-NEXT: [[TMP13:%.*]] = add i32 [[TMP4]], [[TMP12]]
+; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[A]] to i64
+; CHECK-NEXT: [[TMP15:%.*]] = zext i32 [[TMP13]] to i64
+; CHECK-NEXT: [[TMP16:%.*]] = mul i64 [[TMP14]], [[TMP15]]
+; CHECK-NEXT: [[TMP17:%.*]] = trunc i64 [[TMP16]] to i32
+; CHECK-NEXT: [[TMP18:%.*]] = lshr i64 [[TMP16]], 32
+; CHECK-NEXT: [[TMP19:%.*]] = trunc i64 [[TMP18]] to i32
+; CHECK-NEXT: [[TMP20:%.*]] = mul i32 [[TMP19]], [[B]]
+; CHECK-NEXT: [[TMP21:%.*]] = sub i32 [[A]], [[TMP20]]
+; CHECK-NEXT: [[TMP22:%.*]] = icmp uge i32 [[TMP21]], [[B]]
+; CHECK-NEXT: [[TMP23:%.*]] = add i32 [[TMP19]], 1
+; CHECK-NEXT: [[TMP24:%.*]] = select i1 [[TMP22]], i32 [[TMP23]], i32 [[TMP19]]
+; CHECK-NEXT: [[TMP25:%.*]] = sub i32 [[TMP21]], [[B]]
+; CHECK-NEXT: [[TMP26:%.*]] = select i1 [[TMP22]], i32 [[TMP25]], i32 [[TMP21]]
+; CHECK-NEXT: [[TMP27:%.*]] = icmp uge i32 [[TMP26]], [[B]]
+; CHECK-NEXT: [[TMP28:%.*]] = add i32 [[TMP24]], 1
+; CHECK-NEXT: [[TMP29:%.*]] = select i1 [[TMP27]], i32 [[TMP28]], i32 [[TMP24]]
+; CHECK-NEXT: ret i32 [[TMP29]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i64 @udiv_64_narrow(i16 %a, i16 %b, i1 %m) {
+; CHECK-LABEL: define i64 @udiv_64_narrow(
+; CHECK-SAME: i16 [[A:%.*]], i16 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AZ:%.*]] = zext i16 [[A]] to i64
+; CHECK-NEXT: [[BZ:%.*]] = zext i16 [[B]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[AZ]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[BZ]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = uitofp i32 [[TMP1]] to float
+; CHECK-NEXT: [[TMP4:%.*]] = uitofp i32 [[TMP2]] to float
+; CHECK-NEXT: [[TMP5:%.*]] = call float @llvm.amdgcn.rcp.f32(float [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = bitcast float [[TMP3]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 1
+; CHECK-NEXT: [[TMP8:%.*]] = bitcast i32 [[TMP7]] to float
+; CHECK-NEXT: [[TMP9:%.*]] = fmul float [[TMP8]], [[TMP5]]
+; CHECK-NEXT: [[TMP10:%.*]] = call float @llvm.trunc.f32(float [[TMP9]])
+; CHECK-NEXT: [[TMP11:%.*]] = fptoui float [[TMP10]] to i32
+; CHECK-NEXT: [[TMP12:%.*]] = zext i32 [[TMP11]] to i64
+; CHECK-NEXT: ret i64 [[TMP12]]
+;
+ %az = zext i16 %a to i64
+ %bz = zext i16 %b to i64
+ %r = call i64 @llvm.masked.udiv.i64(i64 %az, i64 %bz, i1 %m)
+ ret i64 %r
+}
+
+define i32 @sdiv_32_unknown(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @sdiv_32_unknown(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A]], 31
+; CHECK-NEXT: [[TMP2:%.*]] = ashr i32 [[B]], 31
+; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP1]], [[TMP2]]
+; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[A]], [[TMP1]]
+; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[B]], [[TMP2]]
+; CHECK-NEXT: [[TMP6:%.*]] = xor i32 [[TMP4]], [[TMP1]]
+; CHECK-NEXT: [[TMP7:%.*]] = xor i32 [[TMP5]], [[TMP2]]
+; CHECK-NEXT: [[TMP8:%.*]] = uitofp fast i32 [[TMP7]] to float
+; CHECK-NEXT: [[TMP9:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP8]])
+; CHECK-NEXT: [[TMP10:%.*]] = fmul fast float [[TMP9]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP11:%.*]] = fptoui float [[TMP10]] to i32
+; CHECK-NEXT: [[TMP12:%.*]] = sub i32 0, [[TMP7]]
+; CHECK-NEXT: [[TMP13:%.*]] = mul i32 [[TMP12]], [[TMP11]]
+; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[TMP11]] to i64
+; CHECK-NEXT: [[TMP15:%.*]] = zext i32 [[TMP13]] to i64
+; CHECK-NEXT: [[TMP16:%.*]] = mul i64 [[TMP14]], [[TMP15]]
+; CHECK-NEXT: [[TMP17:%.*]] = trunc i64 [[TMP16]] to i32
+; CHECK-NEXT: [[TMP18:%.*]] = lshr i64 [[TMP16]], 32
+; CHECK-NEXT: [[TMP19:%.*]] = trunc i64 [[TMP18]] to i32
+; CHECK-NEXT: [[TMP20:%.*]] = add i32 [[TMP11]], [[TMP19]]
+; CHECK-NEXT: [[TMP21:%.*]] = zext i32 [[TMP6]] to i64
+; CHECK-NEXT: [[TMP22:%.*]] = zext i32 [[TMP20]] to i64
+; CHECK-NEXT: [[TMP23:%.*]] = mul i64 [[TMP21]], [[TMP22]]
+; CHECK-NEXT: [[TMP24:%.*]] = trunc i64 [[TMP23]] to i32
+; CHECK-NEXT: [[TMP25:%.*]] = lshr i64 [[TMP23]], 32
+; CHECK-NEXT: [[TMP26:%.*]] = trunc i64 [[TMP25]] to i32
+; CHECK-NEXT: [[TMP27:%.*]] = mul i32 [[TMP26]], [[TMP7]]
+; CHECK-NEXT: [[TMP28:%.*]] = sub i32 [[TMP6]], [[TMP27]]
+; CHECK-NEXT: [[TMP29:%.*]] = icmp uge i32 [[TMP28]], [[TMP7]]
+; CHECK-NEXT: [[TMP30:%.*]] = add i32 [[TMP26]], 1
+; CHECK-NEXT: [[TMP31:%.*]] = select i1 [[TMP29]], i32 [[TMP30]], i32 [[TMP26]]
+; CHECK-NEXT: [[TMP32:%.*]] = sub i32 [[TMP28]], [[TMP7]]
+; CHECK-NEXT: [[TMP33:%.*]] = select i1 [[TMP29]], i32 [[TMP32]], i32 [[TMP28]]
+; CHECK-NEXT: [[TMP34:%.*]] = icmp uge i32 [[TMP33]], [[TMP7]]
+; CHECK-NEXT: [[TMP35:%.*]] = add i32 [[TMP31]], 1
+; CHECK-NEXT: [[TMP36:%.*]] = select i1 [[TMP34]], i32 [[TMP35]], i32 [[TMP31]]
+; CHECK-NEXT: [[TMP37:%.*]] = xor i32 [[TMP36]], [[TMP3]]
+; CHECK-NEXT: [[TMP38:%.*]] = sub i32 [[TMP37]], [[TMP3]]
+; CHECK-NEXT: ret i32 [[TMP38]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_32_narrow(i16 %a, i16 %b, i1 %m) {
+; CHECK-LABEL: define i32 @sdiv_32_narrow(
+; CHECK-SAME: i16 [[A:%.*]], i16 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AZ:%.*]] = zext i16 [[A]] to i32
+; CHECK-NEXT: [[BZ:%.*]] = zext i16 [[B]] to i32
+; CHECK-NEXT: [[TMP1:%.*]] = sitofp fast i32 [[AZ]] to float
+; CHECK-NEXT: [[TMP2:%.*]] = sitofp fast i32 [[BZ]] to float
+; CHECK-NEXT: [[TMP3:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP2]])
+; CHECK-NEXT: [[TMP4:%.*]] = bitcast float [[TMP1]] to i32
+; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[TMP4]], 1
+; CHECK-NEXT: [[TMP6:%.*]] = bitcast i32 [[TMP5]] to float
+; CHECK-NEXT: [[TMP7:%.*]] = fmul fast float [[TMP6]], [[TMP3]]
+; CHECK-NEXT: [[TMP8:%.*]] = call fast float @llvm.trunc.f32(float [[TMP7]])
+; CHECK-NEXT: [[TMP9:%.*]] = fptosi float [[TMP8]] to i32
+; CHECK-NEXT: ret i32 [[TMP9]]
+;
+ %az = zext i16 %a to i32
+ %bz = zext i16 %b to i32
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %az, i32 %bz, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_32_special(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @sdiv_32_special(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 3, i1 [[M]])
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 3, i1 %m)
+ ret i32 %r
+}
+
+define i64 @sdiv_64_unknown(i64 %a, i64 %b, i1 %m) {
+; CHECK-LABEL: define i64 @sdiv_64_unknown(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[A]], i64 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @sdiv_64_narrow(i16 %a, i16 %b, i1 %m) {
+; CHECK-LABEL: define i64 @sdiv_64_narrow(
+; CHECK-SAME: i16 [[A:%.*]], i16 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AZ:%.*]] = zext i16 [[A]] to i64
+; CHECK-NEXT: [[BZ:%.*]] = zext i16 [[B]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[AZ]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[BZ]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = sitofp i32 [[TMP1]] to float
+; CHECK-NEXT: [[TMP4:%.*]] = sitofp i32 [[TMP2]] to float
+; CHECK-NEXT: [[TMP5:%.*]] = call float @llvm.amdgcn.rcp.f32(float [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = bitcast float [[TMP3]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 1
+; CHECK-NEXT: [[TMP8:%.*]] = bitcast i32 [[TMP7]] to float
+; CHECK-NEXT: [[TMP9:%.*]] = fmul float [[TMP8]], [[TMP5]]
+; CHECK-NEXT: [[TMP10:%.*]] = call float @llvm.trunc.f32(float [[TMP9]])
+; CHECK-NEXT: [[TMP11:%.*]] = fptosi float [[TMP10]] to i32
+; CHECK-NEXT: [[TMP12:%.*]] = sext i32 [[TMP11]] to i64
+; CHECK-NEXT: ret i64 [[TMP12]]
+;
+ %az = zext i16 %a to i64
+ %bz = zext i16 %b to i64
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %az, i64 %bz, i1 %m)
+ ret i64 %r
+}
+
+define i64 @sdiv_64_special(i64 %a, i64 %b, i1 %m) {
+; CHECK-LABEL: define i64 @sdiv_64_special(
+; CHECK-SAME: i64 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[A]], i64 3, i1 [[M]])
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 3, i1 %m)
+ ret i64 %r
+}
+
+define i32 @urem_32_unknown(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @urem_32_unknown(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = uitofp fast i32 [[B]] to float
+; CHECK-NEXT: [[TMP2:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP1]])
+; CHECK-NEXT: [[TMP3:%.*]] = fmul fast float [[TMP2]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP4:%.*]] = fptoui float [[TMP3]] to i32
+; CHECK-NEXT: [[TMP5:%.*]] = sub i32 0, [[B]]
+; CHECK-NEXT: [[TMP6:%.*]] = mul i32 [[TMP5]], [[TMP4]]
+; CHECK-NEXT: [[TMP7:%.*]] = zext i32 [[TMP4]] to i64
+; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP6]] to i64
+; CHECK-NEXT: [[TMP9:%.*]] = mul i64 [[TMP7]], [[TMP8]]
+; CHECK-NEXT: [[TMP10:%.*]] = trunc i64 [[TMP9]] to i32
+; CHECK-NEXT: [[TMP11:%.*]] = lshr i64 [[TMP9]], 32
+; CHECK-NEXT: [[TMP12:%.*]] = trunc i64 [[TMP11]] to i32
+; CHECK-NEXT: [[TMP13:%.*]] = add i32 [[TMP4]], [[TMP12]]
+; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[A]] to i64
+; CHECK-NEXT: [[TMP15:%.*]] = zext i32 [[TMP13]] to i64
+; CHECK-NEXT: [[TMP16:%.*]] = mul i64 [[TMP14]], [[TMP15]]
+; CHECK-NEXT: [[TMP17:%.*]] = trunc i64 [[TMP16]] to i32
+; CHECK-NEXT: [[TMP18:%.*]] = lshr i64 [[TMP16]], 32
+; CHECK-NEXT: [[TMP19:%.*]] = trunc i64 [[TMP18]] to i32
+; CHECK-NEXT: [[TMP20:%.*]] = mul i32 [[TMP19]], [[B]]
+; CHECK-NEXT: [[TMP21:%.*]] = sub i32 [[A]], [[TMP20]]
+; CHECK-NEXT: [[TMP22:%.*]] = icmp uge i32 [[TMP21]], [[B]]
+; CHECK-NEXT: [[TMP23:%.*]] = sub i32 [[TMP21]], [[B]]
+; CHECK-NEXT: [[TMP24:%.*]] = select i1 [[TMP22]], i32 [[TMP23]], i32 [[TMP21]]
+; CHECK-NEXT: [[TMP25:%.*]] = icmp uge i32 [[TMP24]], [[B]]
+; CHECK-NEXT: [[TMP26:%.*]] = sub i32 [[TMP24]], [[B]]
+; CHECK-NEXT: [[TMP27:%.*]] = select i1 [[TMP25]], i32 [[TMP26]], i32 [[TMP24]]
+; CHECK-NEXT: ret i32 [[TMP27]]
+;
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i64 @urem_64_narrow(i16 %a, i16 %b, i1 %m) {
+; CHECK-LABEL: define i64 @urem_64_narrow(
+; CHECK-SAME: i16 [[A:%.*]], i16 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[AZ:%.*]] = zext i16 [[A]] to i64
+; CHECK-NEXT: [[BZ:%.*]] = zext i16 [[B]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[AZ]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[BZ]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = uitofp i32 [[TMP1]] to float
+; CHECK-NEXT: [[TMP4:%.*]] = uitofp i32 [[TMP2]] to float
+; CHECK-NEXT: [[TMP5:%.*]] = call float @llvm.amdgcn.rcp.f32(float [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = bitcast float [[TMP3]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 1
+; CHECK-NEXT: [[TMP8:%.*]] = bitcast i32 [[TMP7]] to float
+; CHECK-NEXT: [[TMP9:%.*]] = fmul float [[TMP8]], [[TMP5]]
+; CHECK-NEXT: [[TMP10:%.*]] = call float @llvm.trunc.f32(float [[TMP9]])
+; CHECK-NEXT: [[TMP11:%.*]] = fptoui float [[TMP10]] to i32
+; CHECK-NEXT: [[TMP12:%.*]] = mul i32 [[TMP11]], [[TMP2]]
+; CHECK-NEXT: [[TMP13:%.*]] = sub i32 [[TMP1]], [[TMP12]]
+; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[TMP13]] to i64
+; CHECK-NEXT: ret i64 [[TMP14]]
+;
+ %az = zext i16 %a to i64
+ %bz = zext i16 %b to i64
+ %r = call i64 @llvm.masked.urem.i64(i64 %az, i64 %bz, i1 %m)
+ ret i64 %r
+}
+
+define i32 @srem_32_unknown(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @srem_32_unknown(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A]], 31
+; CHECK-NEXT: [[TMP2:%.*]] = ashr i32 [[B]], 31
+; CHECK-NEXT: [[TMP3:%.*]] = add i32 [[A]], [[TMP1]]
+; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[B]], [[TMP2]]
+; CHECK-NEXT: [[TMP5:%.*]] = xor i32 [[TMP3]], [[TMP1]]
+; CHECK-NEXT: [[TMP6:%.*]] = xor i32 [[TMP4]], [[TMP2]]
+; CHECK-NEXT: [[TMP7:%.*]] = uitofp fast i32 [[TMP6]] to float
+; CHECK-NEXT: [[TMP8:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP7]])
+; CHECK-NEXT: [[TMP9:%.*]] = fmul fast float [[TMP8]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP10:%.*]] = fptoui float [[TMP9]] to i32
+; CHECK-NEXT: [[TMP11:%.*]] = sub i32 0, [[TMP6]]
+; CHECK-NEXT: [[TMP12:%.*]] = mul i32 [[TMP11]], [[TMP10]]
+; CHECK-NEXT: [[TMP13:%.*]] = zext i32 [[TMP10]] to i64
+; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[TMP12]] to i64
+; CHECK-NEXT: [[TMP15:%.*]] = mul i64 [[TMP13]], [[TMP14]]
+; CHECK-NEXT: [[TMP16:%.*]] = trunc i64 [[TMP15]] to i32
+; CHECK-NEXT: [[TMP17:%.*]] = lshr i64 [[TMP15]], 32
+; CHECK-NEXT: [[TMP18:%.*]] = trunc i64 [[TMP17]] to i32
+; CHECK-NEXT: [[TMP19:%.*]] = add i32 [[TMP10]], [[TMP18]]
+; CHECK-NEXT: [[TMP20:%.*]] = zext i32 [[TMP5]] to i64
+; CHECK-NEXT: [[TMP21:%.*]] = zext i32 [[TMP19]] to i64
+; CHECK-NEXT: [[TMP22:%.*]] = mul i64 [[TMP20]], [[TMP21]]
+; CHECK-NEXT: [[TMP23:%.*]] = trunc i64 [[TMP22]] to i32
+; CHECK-NEXT: [[TMP24:%.*]] = lshr i64 [[TMP22]], 32
+; CHECK-NEXT: [[TMP25:%.*]] = trunc i64 [[TMP24]] to i32
+; CHECK-NEXT: [[TMP26:%.*]] = mul i32 [[TMP25]], [[TMP6]]
+; CHECK-NEXT: [[TMP27:%.*]] = sub i32 [[TMP5]], [[TMP26]]
+; CHECK-NEXT: [[TMP28:%.*]] = icmp uge i32 [[TMP27]], [[TMP6]]
+; CHECK-NEXT: [[TMP29:%.*]] = sub i32 [[TMP27]], [[TMP6]]
+; CHECK-NEXT: [[TMP30:%.*]] = select i1 [[TMP28]], i32 [[TMP29]], i32 [[TMP27]]
+; CHECK-NEXT: [[TMP31:%.*]] = icmp uge i32 [[TMP30]], [[TMP6]]
+; CHECK-NEXT: [[TMP32:%.*]] = sub i32 [[TMP30]], [[TMP6]]
+; CHECK-NEXT: [[TMP33:%.*]] = select i1 [[TMP31]], i32 [[TMP32]], i32 [[TMP30]]
+; CHECK-NEXT: [[TMP34:%.*]] = xor i32 [[TMP33]], [[TMP1]]
+; CHECK-NEXT: [[TMP35:%.*]] = sub i32 [[TMP34]], [[TMP1]]
+; CHECK-NEXT: ret i32 [[TMP35]]
+;
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+; Residual i64 lanes stay masked after vector expansion.
+define <4 x i64> @vector_sdiv(<4 x i64> %a, <4 x i64> %b, <4 x i1> %m) {
+; CHECK-LABEL: define <4 x i64> @vector_sdiv(
+; CHECK-SAME: <4 x i64> [[A:%.*]], <4 x i64> [[B:%.*]], <4 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i64> [[B]], i64 0
+; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i64> [[A]], i64 0
+; CHECK-NEXT: [[TMP3:%.*]] = extractelement <4 x i1> [[M]], i64 0
+; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[TMP1]], i64 [[TMP2]], i1 [[TMP3]])
+; CHECK-NEXT: [[TMP5:%.*]] = insertelement <4 x i64> poison, i64 [[TMP4]], i64 0
+; CHECK-NEXT: [[TMP7:%.*]] = extractelement <4 x i64> [[B]], i64 1
+; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i64> [[A]], i64 1
+; CHECK-NEXT: [[TMP8:%.*]] = extractelement <4 x i1> [[M]], i64 1
+; CHECK-NEXT: [[TMP9:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[TMP6]], i64 [[TMP7]], i1 [[TMP8]])
+; CHECK-NEXT: [[TMP10:%.*]] = insertelement <4 x i64> [[TMP5]], i64 [[TMP9]], i64 1
+; CHECK-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[B]], i64 2
+; CHECK-NEXT: [[TMP11:%.*]] = extractelement <4 x i64> [[A]], i64 2
+; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i1> [[M]], i64 2
+; CHECK-NEXT: [[TMP14:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[TMP11]], i64 [[TMP12]], i1 [[TMP13]])
+; CHECK-NEXT: [[TMP15:%.*]] = insertelement <4 x i64> [[TMP10]], i64 [[TMP14]], i64 2
+; CHECK-NEXT: [[TMP17:%.*]] = extractelement <4 x i64> [[B]], i64 3
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <4 x i64> [[A]], i64 3
+; CHECK-NEXT: [[TMP18:%.*]] = extractelement <4 x i1> [[M]], i64 3
+; CHECK-NEXT: [[TMP19:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 [[TMP16]], i64 [[TMP17]], i1 [[TMP18]])
+; CHECK-NEXT: [[TMP20:%.*]] = insertelement <4 x i64> [[TMP15]], i64 [[TMP19]], i64 3
+; CHECK-NEXT: ret <4 x i64> [[TMP20]]
+;
+ %r = call <4 x i64> @llvm.masked.sdiv.v4i64(<4 x i64> %a, <4 x i64> %b, <4 x i1> %m)
+ ret <4 x i64> %r
+}
+
+; Residual i64 lanes stay masked after vector expansion.
+define <4 x i64> @vector_urem(<4 x i64> %a, <4 x i64> %b, <4 x i1> %m) {
+; CHECK-LABEL: define <4 x i64> @vector_urem(
+; CHECK-SAME: <4 x i64> [[A:%.*]], <4 x i64> [[B:%.*]], <4 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i64> [[B]], i64 0
+; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i64> [[A]], i64 0
+; CHECK-NEXT: [[TMP3:%.*]] = extractelement <4 x i1> [[M]], i64 0
+; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[TMP1]], i64 [[TMP2]], i1 [[TMP3]])
+; CHECK-NEXT: [[TMP5:%.*]] = insertelement <4 x i64> poison, i64 [[TMP4]], i64 0
+; CHECK-NEXT: [[TMP7:%.*]] = extractelement <4 x i64> [[B]], i64 1
+; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i64> [[A]], i64 1
+; CHECK-NEXT: [[TMP8:%.*]] = extractelement <4 x i1> [[M]], i64 1
+; CHECK-NEXT: [[TMP9:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[TMP6]], i64 [[TMP7]], i1 [[TMP8]])
+; CHECK-NEXT: [[TMP10:%.*]] = insertelement <4 x i64> [[TMP5]], i64 [[TMP9]], i64 1
+; CHECK-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[B]], i64 2
+; CHECK-NEXT: [[TMP11:%.*]] = extractelement <4 x i64> [[A]], i64 2
+; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i1> [[M]], i64 2
+; CHECK-NEXT: [[TMP14:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[TMP11]], i64 [[TMP12]], i1 [[TMP13]])
+; CHECK-NEXT: [[TMP15:%.*]] = insertelement <4 x i64> [[TMP10]], i64 [[TMP14]], i64 2
+; CHECK-NEXT: [[TMP17:%.*]] = extractelement <4 x i64> [[B]], i64 3
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <4 x i64> [[A]], i64 3
+; CHECK-NEXT: [[TMP18:%.*]] = extractelement <4 x i1> [[M]], i64 3
+; CHECK-NEXT: [[TMP19:%.*]] = call i64 @llvm.masked.urem.i64(i64 [[TMP16]], i64 [[TMP17]], i1 [[TMP18]])
+; CHECK-NEXT: [[TMP20:%.*]] = insertelement <4 x i64> [[TMP15]], i64 [[TMP19]], i64 3
+; CHECK-NEXT: ret <4 x i64> [[TMP20]]
+;
+ %r = call <4 x i64> @llvm.masked.urem.v4i64(<4 x i64> %a, <4 x i64> %b, <4 x i1> %m)
+ ret <4 x i64> %r
+}
+
+define <2 x i32> @bitcast_zero_lane(<2 x i32> %a, i64 %b, <2 x i1> %m) {
+; CHECK-LABEL: define <2 x i32> @bitcast_zero_lane(
+; CHECK-SAME: <2 x i32> [[A:%.*]], i64 [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[MASKED:%.*]] = and i64 [[B]], 4294967295
+; CHECK-NEXT: [[DEN:%.*]] = bitcast i64 [[MASKED]] to <2 x i32>
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x i32> [[DEN]], i64 0
+; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[A]], i64 0
+; CHECK-NEXT: [[TMP3:%.*]] = uitofp fast i32 [[TMP2]] to float
+; CHECK-NEXT: [[TMP4:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP3]])
+; CHECK-NEXT: [[TMP5:%.*]] = fmul fast float [[TMP4]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP6:%.*]] = fptoui float [[TMP5]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = sub i32 0, [[TMP2]]
+; CHECK-NEXT: [[TMP8:%.*]] = mul i32 [[TMP7]], [[TMP6]]
+; CHECK-NEXT: [[TMP9:%.*]] = zext i32 [[TMP6]] to i64
+; CHECK-NEXT: [[TMP10:%.*]] = zext i32 [[TMP8]] to i64
+; CHECK-NEXT: [[TMP11:%.*]] = mul i64 [[TMP9]], [[TMP10]]
+; CHECK-NEXT: [[TMP12:%.*]] = trunc i64 [[TMP11]] to i32
+; CHECK-NEXT: [[TMP13:%.*]] = lshr i64 [[TMP11]], 32
+; CHECK-NEXT: [[TMP14:%.*]] = trunc i64 [[TMP13]] to i32
+; CHECK-NEXT: [[TMP15:%.*]] = add i32 [[TMP6]], [[TMP14]]
+; CHECK-NEXT: [[TMP16:%.*]] = zext i32 [[TMP1]] to i64
+; CHECK-NEXT: [[TMP17:%.*]] = zext i32 [[TMP15]] to i64
+; CHECK-NEXT: [[TMP18:%.*]] = mul i64 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: [[TMP19:%.*]] = trunc i64 [[TMP18]] to i32
+; CHECK-NEXT: [[TMP20:%.*]] = lshr i64 [[TMP18]], 32
+; CHECK-NEXT: [[TMP21:%.*]] = trunc i64 [[TMP20]] to i32
+; CHECK-NEXT: [[TMP22:%.*]] = mul i32 [[TMP21]], [[TMP2]]
+; CHECK-NEXT: [[TMP23:%.*]] = sub i32 [[TMP1]], [[TMP22]]
+; CHECK-NEXT: [[TMP24:%.*]] = icmp uge i32 [[TMP23]], [[TMP2]]
+; CHECK-NEXT: [[TMP25:%.*]] = add i32 [[TMP21]], 1
+; CHECK-NEXT: [[TMP26:%.*]] = select i1 [[TMP24]], i32 [[TMP25]], i32 [[TMP21]]
+; CHECK-NEXT: [[TMP27:%.*]] = sub i32 [[TMP23]], [[TMP2]]
+; CHECK-NEXT: [[TMP28:%.*]] = select i1 [[TMP24]], i32 [[TMP27]], i32 [[TMP23]]
+; CHECK-NEXT: [[TMP29:%.*]] = icmp uge i32 [[TMP28]], [[TMP2]]
+; CHECK-NEXT: [[TMP30:%.*]] = add i32 [[TMP26]], 1
+; CHECK-NEXT: [[TMP31:%.*]] = select i1 [[TMP29]], i32 [[TMP30]], i32 [[TMP26]]
+; CHECK-NEXT: [[TMP32:%.*]] = insertelement <2 x i32> poison, i32 [[TMP31]], i64 0
+; CHECK-NEXT: [[TMP35:%.*]] = insertelement <2 x i32> [[TMP32]], i32 0, i64 1
+; CHECK-NEXT: ret <2 x i32> [[TMP35]]
+;
+ %masked = and i64 %b, 4294967295
+ %den = bitcast i64 %masked to <2 x i32>
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> %den, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @bitcast_float_vec(<2 x i32> %a, <2 x float> %f, <2 x i1> %m) {
+; CHECK-LABEL: define <2 x i32> @bitcast_float_vec(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x float> [[F:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[DEN:%.*]] = bitcast <2 x float> [[F]] to <2 x i32>
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x i32> [[DEN]], i64 0
+; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[A]], i64 0
+; CHECK-NEXT: [[TMP3:%.*]] = uitofp fast i32 [[TMP2]] to float
+; CHECK-NEXT: [[TMP4:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP3]])
+; CHECK-NEXT: [[TMP5:%.*]] = fmul fast float [[TMP4]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP6:%.*]] = fptoui float [[TMP5]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = sub i32 0, [[TMP2]]
+; CHECK-NEXT: [[TMP8:%.*]] = mul i32 [[TMP7]], [[TMP6]]
+; CHECK-NEXT: [[TMP9:%.*]] = zext i32 [[TMP6]] to i64
+; CHECK-NEXT: [[TMP10:%.*]] = zext i32 [[TMP8]] to i64
+; CHECK-NEXT: [[TMP11:%.*]] = mul i64 [[TMP9]], [[TMP10]]
+; CHECK-NEXT: [[TMP12:%.*]] = trunc i64 [[TMP11]] to i32
+; CHECK-NEXT: [[TMP13:%.*]] = lshr i64 [[TMP11]], 32
+; CHECK-NEXT: [[TMP14:%.*]] = trunc i64 [[TMP13]] to i32
+; CHECK-NEXT: [[TMP15:%.*]] = add i32 [[TMP6]], [[TMP14]]
+; CHECK-NEXT: [[TMP16:%.*]] = zext i32 [[TMP1]] to i64
+; CHECK-NEXT: [[TMP17:%.*]] = zext i32 [[TMP15]] to i64
+; CHECK-NEXT: [[TMP18:%.*]] = mul i64 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: [[TMP19:%.*]] = trunc i64 [[TMP18]] to i32
+; CHECK-NEXT: [[TMP20:%.*]] = lshr i64 [[TMP18]], 32
+; CHECK-NEXT: [[TMP21:%.*]] = trunc i64 [[TMP20]] to i32
+; CHECK-NEXT: [[TMP22:%.*]] = mul i32 [[TMP21]], [[TMP2]]
+; CHECK-NEXT: [[TMP23:%.*]] = sub i32 [[TMP1]], [[TMP22]]
+; CHECK-NEXT: [[TMP24:%.*]] = icmp uge i32 [[TMP23]], [[TMP2]]
+; CHECK-NEXT: [[TMP25:%.*]] = add i32 [[TMP21]], 1
+; CHECK-NEXT: [[TMP26:%.*]] = select i1 [[TMP24]], i32 [[TMP25]], i32 [[TMP21]]
+; CHECK-NEXT: [[TMP27:%.*]] = sub i32 [[TMP23]], [[TMP2]]
+; CHECK-NEXT: [[TMP28:%.*]] = select i1 [[TMP24]], i32 [[TMP27]], i32 [[TMP23]]
+; CHECK-NEXT: [[TMP29:%.*]] = icmp uge i32 [[TMP28]], [[TMP2]]
+; CHECK-NEXT: [[TMP30:%.*]] = add i32 [[TMP26]], 1
+; CHECK-NEXT: [[TMP31:%.*]] = select i1 [[TMP29]], i32 [[TMP30]], i32 [[TMP26]]
+; CHECK-NEXT: [[TMP32:%.*]] = insertelement <2 x i32> poison, i32 [[TMP31]], i64 0
+; CHECK-NEXT: [[TMP34:%.*]] = extractelement <2 x i32> [[DEN]], i64 1
+; CHECK-NEXT: [[TMP33:%.*]] = extractelement <2 x i32> [[A]], i64 1
+; CHECK-NEXT: [[TMP35:%.*]] = uitofp fast i32 [[TMP34]] to float
+; CHECK-NEXT: [[TMP36:%.*]] = call fast float @llvm.amdgcn.rcp.f32(float [[TMP35]])
+; CHECK-NEXT: [[TMP37:%.*]] = fmul fast float [[TMP36]], f0x4F7FFFFE
+; CHECK-NEXT: [[TMP38:%.*]] = fptoui float [[TMP37]] to i32
+; CHECK-NEXT: [[TMP39:%.*]] = sub i32 0, [[TMP34]]
+; CHECK-NEXT: [[TMP40:%.*]] = mul i32 [[TMP39]], [[TMP38]]
+; CHECK-NEXT: [[TMP41:%.*]] = zext i32 [[TMP38]] to i64
+; CHECK-NEXT: [[TMP42:%.*]] = zext i32 [[TMP40]] to i64
+; CHECK-NEXT: [[TMP43:%.*]] = mul i64 [[TMP41]], [[TMP42]]
+; CHECK-NEXT: [[TMP44:%.*]] = trunc i64 [[TMP43]] to i32
+; CHECK-NEXT: [[TMP45:%.*]] = lshr i64 [[TMP43]], 32
+; CHECK-NEXT: [[TMP46:%.*]] = trunc i64 [[TMP45]] to i32
+; CHECK-NEXT: [[TMP47:%.*]] = add i32 [[TMP38]], [[TMP46]]
+; CHECK-NEXT: [[TMP48:%.*]] = zext i32 [[TMP33]] to i64
+; CHECK-NEXT: [[TMP49:%.*]] = zext i32 [[TMP47]] to i64
+; CHECK-NEXT: [[TMP50:%.*]] = mul i64 [[TMP48]], [[TMP49]]
+; CHECK-NEXT: [[TMP51:%.*]] = trunc i64 [[TMP50]] to i32
+; CHECK-NEXT: [[TMP52:%.*]] = lshr i64 [[TMP50]], 32
+; CHECK-NEXT: [[TMP53:%.*]] = trunc i64 [[TMP52]] to i32
+; CHECK-NEXT: [[TMP54:%.*]] = mul i32 [[TMP53]], [[TMP34]]
+; CHECK-NEXT: [[TMP55:%.*]] = sub i32 [[TMP33]], [[TMP54]]
+; CHECK-NEXT: [[TMP56:%.*]] = icmp uge i32 [[TMP55]], [[TMP34]]
+; CHECK-NEXT: [[TMP57:%.*]] = add i32 [[TMP53]], 1
+; CHECK-NEXT: [[TMP58:%.*]] = select i1 [[TMP56]], i32 [[TMP57]], i32 [[TMP53]]
+; CHECK-NEXT: [[TMP59:%.*]] = sub i32 [[TMP55]], [[TMP34]]
+; CHECK-NEXT: [[TMP60:%.*]] = select i1 [[TMP56]], i32 [[TMP59]], i32 [[TMP55]]
+; CHECK-NEXT: [[TMP61:%.*]] = icmp uge i32 [[TMP60]], [[TMP34]]
+; CHECK-NEXT: [[TMP62:%.*]] = add i32 [[TMP58]], 1
+; CHECK-NEXT: [[TMP63:%.*]] = select i1 [[TMP61]], i32 [[TMP62]], i32 [[TMP58]]
+; CHECK-NEXT: [[TMP64:%.*]] = insertelement <2 x i32> [[TMP32]], i32 [[TMP63]], i64 1
+; CHECK-NEXT: ret <2 x i32> [[TMP64]]
+;
+ %den = bitcast <2 x float> %f to <2 x i32>
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> %den, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @poison_lane(<2 x i32> %a, <2 x i1> %m) {
+; CHECK-LABEL: define <2 x i32> @poison_lane(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x i32> [[A]], i64 1
+; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i1> [[M]], i64 1
+; CHECK-NEXT: [[TMP4:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[TMP2]], i32 3, i1 [[TMP3]])
+; CHECK-NEXT: [[TMP5:%.*]] = insertelement <2 x i32> <i32 0, i32 poison>, i32 [[TMP4]], i64 1
+; CHECK-NEXT: ret <2 x i32> [[TMP5]]
+;
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> <i32 poison, i32 3>, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define i64 @srem_64_narrow(i16 %a, i16 %b, i1 %m) {
+; CHECK-LABEL: define i64 @srem_64_narrow(
+; CHECK-SAME: i16 [[A:%.*]], i16 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[SA:%.*]] = sext i16 [[A]] to i64
+; CHECK-NEXT: [[SB:%.*]] = sext i16 [[B]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[SA]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[SB]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = sitofp i32 [[TMP1]] to float
+; CHECK-NEXT: [[TMP4:%.*]] = sitofp i32 [[TMP2]] to float
+; CHECK-NEXT: [[TMP5:%.*]] = call float @llvm.amdgcn.rcp.f32(float [[TMP4]])
+; CHECK-NEXT: [[TMP6:%.*]] = bitcast float [[TMP3]] to i32
+; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 1
+; CHECK-NEXT: [[TMP8:%.*]] = bitcast i32 [[TMP7]] to float
+; CHECK-NEXT: [[TMP9:%.*]] = fmul float [[TMP8]], [[TMP5]]
+; CHECK-NEXT: [[TMP10:%.*]] = call float @llvm.trunc.f32(float [[TMP9]])
+; CHECK-NEXT: [[TMP11:%.*]] = fptosi float [[TMP10]] to i32
+; CHECK-NEXT: [[TMP12:%.*]] = mul i32 [[TMP11]], [[TMP2]]
+; CHECK-NEXT: [[TMP13:%.*]] = sub i32 [[TMP1]], [[TMP12]]
+; CHECK-NEXT: [[TMP14:%.*]] = sext i32 [[TMP13]] to i64
+; CHECK-NEXT: ret i64 [[TMP14]]
+;
+ %sa = sext i16 %a to i64
+ %sb = sext i16 %b to i64
+ %r = call i64 @llvm.masked.srem.i64(i64 %sa, i64 %sb, i1 %m)
+ ret i64 %r
+}
+
+define i64 @udiv_64_const_pow2(i64 %a, i1 %m) {
+; CHECK-LABEL: define i64 @udiv_64_const_pow2(
+; CHECK-SAME: i64 [[A:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i64 @llvm.masked.udiv.i64(i64 [[A]], i64 4, i1 [[M]])
+; CHECK-NEXT: ret i64 [[R]]
+;
+ %r = call i64 @llvm.masked.udiv.i64(i64 %a, i64 4, i1 %m)
+ ret i64 %r
+}
+
+define i128 @udiv_128_unsupported(i128 %a, i128 %b, i1 %m) {
+; CHECK-LABEL: define i128 @udiv_128_unsupported(
+; CHECK-SAME: i128 [[A:%.*]], i128 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i128 @llvm.masked.udiv.i128(i128 [[A]], i128 [[B]], i1 [[M]])
+; CHECK-NEXT: ret i128 [[R]]
+;
+ %r = call i128 @llvm.masked.udiv.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
+
+define <vscale x 2 x i32> @udiv_scalable_unsupported(<vscale x 2 x i32> %a, <vscale x 2 x i32> %b, <vscale x 2 x i1> %m) {
+; CHECK-LABEL: define <vscale x 2 x i32> @udiv_scalable_unsupported(
+; CHECK-SAME: <vscale x 2 x i32> [[A:%.*]], <vscale x 2 x i32> [[B:%.*]], <vscale x 2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call <vscale x 2 x i32> @llvm.masked.udiv.nxv2i32(<vscale x 2 x i32> [[A]], <vscale x 2 x i32> [[B]], <vscale x 2 x i1> [[M]])
+; CHECK-NEXT: ret <vscale x 2 x i32> [[R]]
+;
+ %r = call <vscale x 2 x i32> @llvm.masked.udiv.nxv2i32(<vscale x 2 x i32> %a, <vscale x 2 x i32> %b, <vscale x 2 x i1> %m)
+ ret <vscale x 2 x i32> %r
+}
+
+define i32 @scalar_extract_bitcast_zero(i32 %a, i64 %b, i1 %m) {
+; CHECK-LABEL: define i32 @scalar_extract_bitcast_zero(
+; CHECK-SAME: i32 [[A:%.*]], i64 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: ret i32 0
+;
+ %masked = and i64 %b, 4294967295
+ %v = bitcast i64 %masked to <2 x i32>
+ %den = extractelement <2 x i32> %v, i64 1
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %den, i1 %m)
+ ret i32 %r
+}
+
+define i32 @udiv_operand_bundle(i32 %a, i32 %b, i1 %m, ptr %p) {
+; CHECK-LABEL: define i32 @udiv_operand_bundle(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]], ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]]) [ "unknown"(ptr [[P]]) ]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m) [ "unknown"(ptr %p) ]
+ ret i32 %r
+}
+
+define i32 @udiv_musttail(i32 %a, i32 %b, i1 %m) {
+; CHECK-LABEL: define i32 @udiv_musttail(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[R:%.*]] = musttail call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]]) [ "unknown"() ]
+; CHECK-NEXT: ret i32 [[R]]
+;
+ %r = musttail call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m) [ "unknown"() ]
+ ret i32 %r
+}
diff --git a/llvm/test/CodeGen/AMDGPU/masked-divrem.ll b/llvm/test/CodeGen/AMDGPU/masked-divrem.ll
new file mode 100644
index 00000000000000..1a4f9999c50b12
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/masked-divrem.ll
@@ -0,0 +1,496 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a < %s | FileCheck %s --check-prefix=SDAG
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=GISEL
+
+define i8 @udiv_i8(i8 %a, i8 %b, i1 %m) {
+; SDAG-LABEL: udiv_i8:
+; SDAG: ; %bb.0:
+; SDAG-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; SDAG-NEXT: v_cvt_f32_ubyte0_e32 v1, v1
+; SDAG-NEXT: v_rcp_f32_e32 v1, v1
+; SDAG-NEXT: v_cvt_f32_ubyte0_e32 v0, v0
+; SDAG-NEXT: v_add_u32_e32 v0, 1, v0
+; SDAG-NEXT: v_mul_f32_e32 v0, v0, v1
+; SDAG-NEXT: v_cvt_u32_f32_e32 v0, v0
+; SDAG-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: udiv_i8:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_cvt_f32_ubyte0_sdwa v1, v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
+; GISEL-NEXT: v_rcp_f32_e32 v1, v1
+; GISEL-NEXT: v_cvt_f32_ubyte0_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
+; GISEL-NEXT: v_add_u32_e32 v0, 1, v0
+; GISEL-NEXT: v_mul_f32_e32 v0, v0, v1
+; GISEL-NEXT: v_trunc_f32_e32 v0, v0
+; GISEL-NEXT: v_cvt_u32_f32_e32 v0, v0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %r = call i8 @llvm.masked.udiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; SDAG-LABEL: sdiv_i32:
+; SDAG: ; %bb.0:
+; SDAG-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; SDAG-NEXT: v_sub_u32_e32 v2, 0, v1
+; SDAG-NEXT: v_max_i32_e32 v2, v2, v1
+; SDAG-NEXT: v_cvt_f32_u32_e32 v3, v2
+; SDAG-NEXT: v_sub_u32_e32 v4, 0, v0
+; SDAG-NEXT: v_xor_b32_e32 v1, v0, v1
+; SDAG-NEXT: v_max_i32_e32 v0, v4, v0
+; SDAG-NEXT: v_rcp_f32_e32 v3, v3
+; SDAG-NEXT: v_sub_u32_e32 v4, 0, v2
+; SDAG-NEXT: v_ashrrev_i32_e32 v1, 31, v1
+; SDAG-NEXT: v_mul_f32_e32 v3, 0x4f7ffffe, v3
+; SDAG-NEXT: v_cvt_u32_f32_e32 v3, v3
+; SDAG-NEXT: v_mul_lo_u32 v4, v4, v3
+; SDAG-NEXT: v_mul_hi_u32 v4, v3, v4
+; SDAG-NEXT: v_add_u32_e32 v3, v3, v4
+; SDAG-NEXT: v_mul_hi_u32 v3, v0, v3
+; SDAG-NEXT: v_mul_lo_u32 v4, v3, v2
+; SDAG-NEXT: v_sub_u32_e32 v0, v0, v4
+; SDAG-NEXT: v_add_u32_e32 v5, 1, v3
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v0, v2
+; SDAG-NEXT: v_sub_u32_e32 v4, v0, v2
+; SDAG-NEXT: v_cndmask_b32_e32 v3, v3, v5, vcc
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v0, v4, vcc
+; SDAG-NEXT: v_add_u32_e32 v4, 1, v3
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v0, v2
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v3, v4, vcc
+; SDAG-NEXT: v_xor_b32_e32 v0, v0, v1
+; SDAG-NEXT: v_sub_u32_e32 v0, v0, v1
+; SDAG-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: sdiv_i32:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_ashrrev_i32_e32 v2, 31, v1
+; GISEL-NEXT: v_add_u32_e32 v1, v1, v2
+; GISEL-NEXT: v_xor_b32_e32 v3, v1, v2
+; GISEL-NEXT: v_cvt_f32_u32_e32 v1, v3
+; GISEL-NEXT: v_ashrrev_i32_e32 v4, 31, v0
+; GISEL-NEXT: v_add_u32_e32 v0, v0, v4
+; GISEL-NEXT: v_xor_b32_e32 v2, v4, v2
+; GISEL-NEXT: v_rcp_f32_e32 v1, v1
+; GISEL-NEXT: v_xor_b32_e32 v4, v0, v4
+; GISEL-NEXT: v_sub_u32_e32 v0, 0, v3
+; GISEL-NEXT: v_mul_f32_e32 v1, 0x4f7ffffe, v1
+; GISEL-NEXT: v_cvt_u32_f32_e32 v5, v1
+; GISEL-NEXT: v_mul_lo_u32 v6, v0, v5
+; GISEL-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v5, v6, 0
+; GISEL-NEXT: v_add_u32_e32 v5, v5, v1
+; GISEL-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v4, v5, 0
+; GISEL-NEXT: v_mul_lo_u32 v0, v1, v3
+; GISEL-NEXT: v_sub_u32_e32 v0, v4, v0
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v0, v3
+; GISEL-NEXT: v_sub_u32_e32 v4, v0, v3
+; GISEL-NEXT: v_cndmask_b32_e32 v0, v0, v4, vcc
+; GISEL-NEXT: v_addc_co_u32_e64 v1, s[4:5], 0, v1, vcc
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v0, v3
+; GISEL-NEXT: v_addc_co_u32_e32 v0, vcc, 0, v1, vcc
+; GISEL-NEXT: v_xor_b32_e32 v0, v0, v2
+; GISEL-NEXT: v_sub_u32_e32 v0, v0, v2
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define <2 x i32> @urem_v2i32_known_zero_lane(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+; SDAG-LABEL: urem_v2i32_known_zero_lane:
+; SDAG: ; %bb.0:
+; SDAG-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; SDAG-NEXT: v_cvt_f32_u32_e32 v1, v2
+; SDAG-NEXT: v_sub_u32_e32 v3, 0, v2
+; SDAG-NEXT: v_rcp_f32_e32 v1, v1
+; SDAG-NEXT: v_mul_f32_e32 v1, 0x4f7ffffe, v1
+; SDAG-NEXT: v_cvt_u32_f32_e32 v1, v1
+; SDAG-NEXT: v_mul_lo_u32 v3, v3, v1
+; SDAG-NEXT: v_mul_hi_u32 v3, v1, v3
+; SDAG-NEXT: v_add_u32_e32 v1, v1, v3
+; SDAG-NEXT: v_mul_hi_u32 v1, v0, v1
+; SDAG-NEXT: v_mul_lo_u32 v1, v1, v2
+; SDAG-NEXT: v_sub_u32_e32 v0, v0, v1
+; SDAG-NEXT: v_sub_u32_e32 v1, v0, v2
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v0, v2
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v0, v1, vcc
+; SDAG-NEXT: v_sub_u32_e32 v1, v0, v2
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v0, v2
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v0, v1, vcc
+; SDAG-NEXT: v_mov_b32_e32 v1, 0
+; SDAG-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: urem_v2i32_known_zero_lane:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_cvt_f32_u32_e32 v1, v2
+; GISEL-NEXT: v_sub_u32_e32 v3, 0, v2
+; GISEL-NEXT: v_rcp_f32_e32 v1, v1
+; GISEL-NEXT: v_mul_f32_e32 v1, 0x4f7ffffe, v1
+; GISEL-NEXT: v_cvt_u32_f32_e32 v1, v1
+; GISEL-NEXT: v_mul_lo_u32 v3, v3, v1
+; GISEL-NEXT: v_mad_u64_u32 v[4:5], s[4:5], v1, v3, 0
+; GISEL-NEXT: v_add_u32_e32 v1, v1, v5
+; GISEL-NEXT: v_mad_u64_u32 v[4:5], s[4:5], v0, v1, 0
+; GISEL-NEXT: v_mul_lo_u32 v1, v5, v2
+; GISEL-NEXT: v_sub_u32_e32 v0, v0, v1
+; GISEL-NEXT: v_sub_u32_e32 v1, v0, v2
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v0, v2
+; GISEL-NEXT: v_cndmask_b32_e32 v0, v0, v1, vcc
+; GISEL-NEXT: v_sub_u32_e32 v1, v0, v2
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v0, v2
+; GISEL-NEXT: v_cndmask_b32_e32 v0, v0, v1, vcc
+; GISEL-NEXT: v_mov_b32_e32 v1, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %z = insertelement <2 x i32> %b, i32 0, i32 1
+ %r = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> %a, <2 x i32> %z, <2 x i1> %m)
+ %f = freeze <2 x i32> %r
+ ret <2 x i32> %f
+}
+
+define i16 @srem_i16(i16 %a, i16 %b, i1 %m) {
+; SDAG-LABEL: srem_i16:
+; SDAG: ; %bb.0:
+; SDAG-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; SDAG-NEXT: v_bfe_i32 v1, v1, 0, 16
+; SDAG-NEXT: v_cvt_f32_i32_e32 v2, v1
+; SDAG-NEXT: v_cvt_f32_i32_sdwa v3, sext(v0) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0
+; SDAG-NEXT: v_rcp_f32_e32 v2, v2
+; SDAG-NEXT: v_add_u32_e32 v3, 1, v3
+; SDAG-NEXT: v_mul_f32_e32 v2, v3, v2
+; SDAG-NEXT: v_cvt_i32_f32_e32 v2, v2
+; SDAG-NEXT: v_mul_lo_u32 v1, v2, v1
+; SDAG-NEXT: v_sub_u32_sdwa v0, sext(v0), v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
+; SDAG-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: srem_i16:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_bfe_i32 v1, v1, 0, 16
+; GISEL-NEXT: v_cvt_f32_i32_e32 v2, v1
+; GISEL-NEXT: v_cvt_f32_i32_sdwa v3, sext(v0) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0
+; GISEL-NEXT: v_rcp_f32_e32 v2, v2
+; GISEL-NEXT: v_add_u32_e32 v3, 1, v3
+; GISEL-NEXT: v_mul_f32_e32 v2, v3, v2
+; GISEL-NEXT: v_trunc_f32_e32 v2, v2
+; GISEL-NEXT: v_cvt_i32_f32_e32 v2, v2
+; GISEL-NEXT: v_mul_lo_u32 v1, v2, v1
+; GISEL-NEXT: v_sub_u32_sdwa v0, sext(v0), v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %r = call i16 @llvm.masked.srem.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i64 @sdiv_i64(i64 %a, i64 %b, i1 %m) {
+; SDAG-LABEL: sdiv_i64:
+; SDAG: ; %bb.0:
+; SDAG-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; SDAG-NEXT: v_mov_b32_e32 v5, v1
+; SDAG-NEXT: v_mov_b32_e32 v4, v0
+; SDAG-NEXT: v_or_b32_e32 v0, v5, v3
+; SDAG-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0
+; SDAG-NEXT: ; implicit-def: $vgpr0_vgpr1
+; SDAG-NEXT: s_and_saveexec_b64 s[4:5], vcc
+; SDAG-NEXT: s_xor_b64 s[6:7], exec, s[4:5]
+; SDAG-NEXT: s_cbranch_execnz .LBB4_3
+; SDAG-NEXT: ; %bb.1: ; %Flow
+; SDAG-NEXT: s_andn2_saveexec_b64 s[4:5], s[6:7]
+; SDAG-NEXT: s_cbranch_execnz .LBB4_4
+; SDAG-NEXT: .LBB4_2: ; %.split
+; SDAG-NEXT: s_or_b64 exec, exec, s[4:5]
+; SDAG-NEXT: s_setpc_b64 s[30:31]
+; SDAG-NEXT: .LBB4_3:
+; SDAG-NEXT: v_ashrrev_i32_e32 v8, 31, v3
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v2, v8
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, v3, v8, vcc
+; SDAG-NEXT: v_xor_b32_e32 v9, v1, v8
+; SDAG-NEXT: v_xor_b32_e32 v10, v0, v8
+; SDAG-NEXT: v_cvt_f32_u32_e32 v0, v10
+; SDAG-NEXT: v_cvt_f32_u32_e32 v1, v9
+; SDAG-NEXT: v_sub_co_u32_e32 v11, vcc, 0, v10
+; SDAG-NEXT: v_subb_co_u32_e32 v12, vcc, 0, v9, vcc
+; SDAG-NEXT: v_madmk_f32 v0, v1, 0x4f800000, v0
+; SDAG-NEXT: v_rcp_f32_e32 v0, v0
+; SDAG-NEXT: v_mul_f32_e32 v0, 0x5f7ffffc, v0
+; SDAG-NEXT: v_mul_f32_e32 v1, 0x2f800000, v0
+; SDAG-NEXT: v_trunc_f32_e32 v2, v1
+; SDAG-NEXT: v_madmk_f32 v0, v2, 0xcf800000, v0
+; SDAG-NEXT: v_cvt_u32_f32_e32 v6, v1
+; SDAG-NEXT: v_cvt_u32_f32_e32 v7, v0
+; SDAG-NEXT: v_mul_lo_u32 v3, v11, v6
+; SDAG-NEXT: v_mul_lo_u32 v2, v12, v7
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v11, v7, 0
+; SDAG-NEXT: v_add3_u32 v13, v1, v3, v2
+; SDAG-NEXT: v_mad_u64_u32 v[2:3], s[4:5], v7, v13, 0
+; SDAG-NEXT: v_mul_hi_u32 v1, v7, v0
+; SDAG-NEXT: v_add_co_u32_e32 v2, vcc, v1, v2
+; SDAG-NEXT: v_addc_co_u32_e32 v3, vcc, 0, v3, vcc
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v6, v0, 0
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v2, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v2, vcc, v3, v1, vcc
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v6, v13, 0
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v2, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; SDAG-NEXT: v_add_co_u32_e32 v13, vcc, v7, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v14, vcc, v6, v1, vcc
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v11, v13, 0
+; SDAG-NEXT: v_mul_lo_u32 v6, v11, v14
+; SDAG-NEXT: v_mul_lo_u32 v7, v12, v13
+; SDAG-NEXT: v_add3_u32 v1, v1, v6, v7
+; SDAG-NEXT: v_mad_u64_u32 v[2:3], s[4:5], v14, v0, 0
+; SDAG-NEXT: v_mad_u64_u32 v[6:7], s[4:5], v13, v1, 0
+; SDAG-NEXT: v_mul_hi_u32 v0, v13, v0
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v0, v6
+; SDAG-NEXT: v_addc_co_u32_e32 v6, vcc, 0, v7, vcc
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v0, v2
+; SDAG-NEXT: v_addc_co_u32_e32 v2, vcc, v6, v3, vcc
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v14, v1, 0
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v2, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; SDAG-NEXT: v_add_co_u32_e32 v2, vcc, v13, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v3, vcc, v14, v1, vcc
+; SDAG-NEXT: v_ashrrev_i32_e32 v6, 31, v5
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v4, v6
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, v5, v6, vcc
+; SDAG-NEXT: v_xor_b32_e32 v5, v0, v6
+; SDAG-NEXT: v_xor_b32_e32 v4, v1, v6
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v5, v3, 0
+; SDAG-NEXT: v_mul_hi_u32 v7, v5, v2
+; SDAG-NEXT: v_add_co_u32_e32 v7, vcc, v7, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v11, vcc, 0, v1, vcc
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v4, v2, 0
+; SDAG-NEXT: v_add_co_u32_e32 v0, vcc, v7, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v2, vcc, v11, v1, vcc
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v4, v3, 0
+; SDAG-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; SDAG-NEXT: v_add_co_u32_e32 v2, vcc, v2, v0
+; SDAG-NEXT: v_addc_co_u32_e32 v3, vcc, 0, v1, vcc
+; SDAG-NEXT: v_mul_lo_u32 v7, v9, v2
+; SDAG-NEXT: v_mul_lo_u32 v11, v10, v3
+; SDAG-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v10, v2, 0
+; SDAG-NEXT: v_add3_u32 v1, v1, v11, v7
+; SDAG-NEXT: v_sub_u32_e32 v7, v4, v1
+; SDAG-NEXT: v_sub_co_u32_e32 v0, vcc, v5, v0
+; SDAG-NEXT: v_subb_co_u32_e64 v5, s[4:5], v7, v9, vcc
+; SDAG-NEXT: v_sub_co_u32_e64 v7, s[4:5], v0, v10
+; SDAG-NEXT: v_subbrev_co_u32_e64 v5, s[4:5], 0, v5, s[4:5]
+; SDAG-NEXT: v_cmp_ge_u32_e64 s[4:5], v5, v9
+; SDAG-NEXT: v_cndmask_b32_e64 v11, 0, -1, s[4:5]
+; SDAG-NEXT: v_cmp_ge_u32_e64 s[4:5], v7, v10
+; SDAG-NEXT: v_cndmask_b32_e64 v7, 0, -1, s[4:5]
+; SDAG-NEXT: v_cmp_eq_u32_e64 s[4:5], v5, v9
+; SDAG-NEXT: v_cndmask_b32_e64 v5, v11, v7, s[4:5]
+; SDAG-NEXT: v_add_co_u32_e64 v7, s[4:5], 2, v2
+; SDAG-NEXT: v_subb_co_u32_e32 v1, vcc, v4, v1, vcc
+; SDAG-NEXT: v_addc_co_u32_e64 v11, s[4:5], 0, v3, s[4:5]
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v1, v9
+; SDAG-NEXT: v_add_co_u32_e64 v12, s[4:5], 1, v2
+; SDAG-NEXT: v_cndmask_b32_e64 v4, 0, -1, vcc
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v0, v10
+; SDAG-NEXT: v_addc_co_u32_e64 v13, s[4:5], 0, v3, s[4:5]
+; SDAG-NEXT: v_cndmask_b32_e64 v0, 0, -1, vcc
+; SDAG-NEXT: v_cmp_eq_u32_e32 vcc, v1, v9
+; SDAG-NEXT: v_cmp_ne_u32_e64 s[4:5], 0, v5
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v4, v0, vcc
+; SDAG-NEXT: v_cndmask_b32_e64 v5, v13, v11, s[4:5]
+; SDAG-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0
+; SDAG-NEXT: v_cndmask_b32_e64 v1, v12, v7, s[4:5]
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v3, v5, vcc
+; SDAG-NEXT: v_cndmask_b32_e32 v1, v2, v1, vcc
+; SDAG-NEXT: v_xor_b32_e32 v2, v6, v8
+; SDAG-NEXT: v_xor_b32_e32 v3, v0, v2
+; SDAG-NEXT: v_xor_b32_e32 v0, v1, v2
+; SDAG-NEXT: v_sub_co_u32_e32 v0, vcc, v0, v2
+; SDAG-NEXT: v_subb_co_u32_e32 v1, vcc, v3, v2, vcc
+; SDAG-NEXT: ; implicit-def: $vgpr2
+; SDAG-NEXT: ; implicit-def: $vgpr4
+; SDAG-NEXT: s_andn2_saveexec_b64 s[4:5], s[6:7]
+; SDAG-NEXT: s_cbranch_execz .LBB4_2
+; SDAG-NEXT: .LBB4_4:
+; SDAG-NEXT: v_cvt_f32_u32_e32 v0, v2
+; SDAG-NEXT: v_sub_u32_e32 v1, 0, v2
+; SDAG-NEXT: v_rcp_iflag_f32_e32 v0, v0
+; SDAG-NEXT: v_mul_f32_e32 v0, 0x4f7ffffe, v0
+; SDAG-NEXT: v_cvt_u32_f32_e32 v0, v0
+; SDAG-NEXT: v_mul_lo_u32 v1, v1, v0
+; SDAG-NEXT: v_mul_hi_u32 v1, v0, v1
+; SDAG-NEXT: v_add_u32_e32 v0, v0, v1
+; SDAG-NEXT: v_mul_hi_u32 v0, v4, v0
+; SDAG-NEXT: v_mul_lo_u32 v1, v0, v2
+; SDAG-NEXT: v_sub_u32_e32 v1, v4, v1
+; SDAG-NEXT: v_add_u32_e32 v3, 1, v0
+; SDAG-NEXT: v_sub_u32_e32 v4, v1, v2
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v1, v2
+; SDAG-NEXT: v_cndmask_b32_e32 v1, v1, v4, vcc
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v0, v3, vcc
+; SDAG-NEXT: v_add_u32_e32 v3, 1, v0
+; SDAG-NEXT: v_cmp_ge_u32_e32 vcc, v1, v2
+; SDAG-NEXT: v_cndmask_b32_e32 v0, v0, v3, vcc
+; SDAG-NEXT: v_mov_b32_e32 v1, 0
+; SDAG-NEXT: s_or_b64 exec, exec, s[4:5]
+; SDAG-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: sdiv_i64:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mov_b32_e32 v5, v1
+; GISEL-NEXT: v_mov_b32_e32 v4, v0
+; GISEL-NEXT: v_or_b32_e32 v1, v5, v3
+; GISEL-NEXT: v_mov_b32_e32 v0, 0
+; GISEL-NEXT: v_cmp_ne_u64_e32 vcc, 0, v[0:1]
+; GISEL-NEXT: ; implicit-def: $vgpr0_vgpr1
+; GISEL-NEXT: s_and_saveexec_b64 s[4:5], vcc
+; GISEL-NEXT: s_xor_b64 s[6:7], exec, s[4:5]
+; GISEL-NEXT: s_cbranch_execnz .LBB4_3
+; GISEL-NEXT: ; %bb.1: ; %Flow
+; GISEL-NEXT: s_andn2_saveexec_b64 s[6:7], s[6:7]
+; GISEL-NEXT: s_cbranch_execnz .LBB4_4
+; GISEL-NEXT: .LBB4_2: ; %.split
+; GISEL-NEXT: s_or_b64 exec, exec, s[6:7]
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+; GISEL-NEXT: .LBB4_3:
+; GISEL-NEXT: v_ashrrev_i32_e32 v6, 31, v3
+; GISEL-NEXT: v_add_co_u32_e32 v0, vcc, v2, v6
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, v3, v6, vcc
+; GISEL-NEXT: v_xor_b32_e32 v8, v1, v6
+; GISEL-NEXT: v_xor_b32_e32 v7, v0, v6
+; GISEL-NEXT: v_cvt_f32_u32_e32 v0, v8
+; GISEL-NEXT: v_cvt_f32_u32_e32 v1, v7
+; GISEL-NEXT: v_ashrrev_i32_e32 v9, 31, v5
+; GISEL-NEXT: v_mul_f32_e32 v0, 0x4f800000, v0
+; GISEL-NEXT: v_add_f32_e32 v0, v0, v1
+; GISEL-NEXT: v_rcp_iflag_f32_e32 v0, v0
+; GISEL-NEXT: v_add_co_u32_e32 v1, vcc, v4, v9
+; GISEL-NEXT: v_xor_b32_e32 v10, v1, v9
+; GISEL-NEXT: v_mul_f32_e32 v0, 0x5f7ffffc, v0
+; GISEL-NEXT: v_mul_f32_e32 v1, 0x2f800000, v0
+; GISEL-NEXT: v_addc_co_u32_e32 v2, vcc, v5, v9, vcc
+; GISEL-NEXT: v_trunc_f32_e32 v1, v1
+; GISEL-NEXT: v_xor_b32_e32 v11, v2, v9
+; GISEL-NEXT: v_mul_f32_e32 v2, 0xcf800000, v1
+; GISEL-NEXT: v_add_f32_e32 v0, v2, v0
+; GISEL-NEXT: v_cvt_u32_f32_e32 v13, v1
+; GISEL-NEXT: v_cvt_u32_f32_e32 v12, v0
+; GISEL-NEXT: v_sub_co_u32_e32 v14, vcc, 0, v7
+; GISEL-NEXT: v_subb_co_u32_e32 v15, vcc, 0, v8, vcc
+; GISEL-NEXT: v_mad_u64_u32 v[2:3], s[4:5], v14, v13, 0
+; GISEL-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v14, v12, 0
+; GISEL-NEXT: v_mad_u64_u32 v[4:5], s[4:5], v15, v12, v[2:3]
+; GISEL-NEXT: v_add_u32_e32 v1, v1, v4
+; GISEL-NEXT: v_mul_lo_u32 v2, v12, v1
+; GISEL-NEXT: v_mul_hi_u32 v3, v12, v0
+; GISEL-NEXT: v_mul_hi_u32 v4, v12, v1
+; GISEL-NEXT: v_add_co_u32_e32 v2, vcc, v2, v3
+; GISEL-NEXT: v_addc_co_u32_e32 v3, vcc, 0, v4, vcc
+; GISEL-NEXT: v_mul_lo_u32 v4, v13, v0
+; GISEL-NEXT: v_mul_lo_u32 v5, v13, v1
+; GISEL-NEXT: v_mul_hi_u32 v0, v13, v0
+; GISEL-NEXT: v_mul_hi_u32 v1, v13, v1
+; GISEL-NEXT: v_add_co_u32_e32 v0, vcc, v5, v0
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; GISEL-NEXT: v_add_co_u32_e32 v2, vcc, v2, v4
+; GISEL-NEXT: v_addc_co_u32_e32 v0, vcc, v3, v0, vcc
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; GISEL-NEXT: v_add_co_u32_e32 v12, vcc, v12, v0
+; GISEL-NEXT: v_addc_co_u32_e32 v13, vcc, v13, v1, vcc
+; GISEL-NEXT: v_mad_u64_u32 v[2:3], s[4:5], v14, v13, 0
+; GISEL-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v14, v12, 0
+; GISEL-NEXT: v_mad_u64_u32 v[4:5], s[4:5], v15, v12, v[2:3]
+; GISEL-NEXT: v_add_u32_e32 v1, v1, v4
+; GISEL-NEXT: v_mul_lo_u32 v2, v12, v1
+; GISEL-NEXT: v_mul_hi_u32 v3, v12, v0
+; GISEL-NEXT: v_mul_hi_u32 v4, v12, v1
+; GISEL-NEXT: v_add_co_u32_e32 v2, vcc, v2, v3
+; GISEL-NEXT: v_addc_co_u32_e32 v3, vcc, 0, v4, vcc
+; GISEL-NEXT: v_mul_lo_u32 v4, v13, v0
+; GISEL-NEXT: v_mul_lo_u32 v5, v13, v1
+; GISEL-NEXT: v_mul_hi_u32 v0, v13, v0
+; GISEL-NEXT: v_mul_hi_u32 v1, v13, v1
+; GISEL-NEXT: v_add_co_u32_e32 v0, vcc, v5, v0
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; GISEL-NEXT: v_add_co_u32_e32 v2, vcc, v2, v4
+; GISEL-NEXT: v_addc_co_u32_e32 v0, vcc, v3, v0, vcc
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; GISEL-NEXT: v_add_co_u32_e32 v0, vcc, v12, v0
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, v13, v1, vcc
+; GISEL-NEXT: v_mul_lo_u32 v2, v10, v1
+; GISEL-NEXT: v_mul_hi_u32 v3, v10, v0
+; GISEL-NEXT: v_mul_hi_u32 v4, v10, v1
+; GISEL-NEXT: v_add_co_u32_e32 v2, vcc, v2, v3
+; GISEL-NEXT: v_addc_co_u32_e32 v3, vcc, 0, v4, vcc
+; GISEL-NEXT: v_mul_lo_u32 v4, v11, v0
+; GISEL-NEXT: v_mul_lo_u32 v5, v11, v1
+; GISEL-NEXT: v_mul_hi_u32 v0, v11, v0
+; GISEL-NEXT: v_mul_hi_u32 v1, v11, v1
+; GISEL-NEXT: v_add_co_u32_e32 v0, vcc, v5, v0
+; GISEL-NEXT: v_addc_co_u32_e32 v1, vcc, 0, v1, vcc
+; GISEL-NEXT: v_add_co_u32_e32 v2, vcc, v2, v4
+; GISEL-NEXT: v_addc_co_u32_e32 v12, vcc, v3, v0, vcc
+; GISEL-NEXT: v_addc_co_u32_e32 v13, vcc, 0, v1, vcc
+; GISEL-NEXT: v_mad_u64_u32 v[2:3], s[4:5], v7, v13, 0
+; GISEL-NEXT: v_mad_u64_u32 v[0:1], s[4:5], v7, v12, 0
+; GISEL-NEXT: v_mad_u64_u32 v[4:5], s[4:5], v8, v12, v[2:3]
+; GISEL-NEXT: v_add_u32_e32 v1, v1, v4
+; GISEL-NEXT: v_sub_co_u32_e32 v0, vcc, v10, v0
+; GISEL-NEXT: v_subb_co_u32_e64 v2, s[4:5], v11, v1, vcc
+; GISEL-NEXT: v_sub_u32_e32 v1, v11, v1
+; GISEL-NEXT: v_cmp_ge_u32_e64 s[4:5], v2, v8
+; GISEL-NEXT: v_subb_co_u32_e32 v1, vcc, v1, v8, vcc
+; GISEL-NEXT: v_cndmask_b32_e64 v3, 0, -1, s[4:5]
+; GISEL-NEXT: v_cmp_ge_u32_e64 s[4:5], v0, v7
+; GISEL-NEXT: v_sub_co_u32_e32 v0, vcc, v0, v7
+; GISEL-NEXT: v_cndmask_b32_e64 v4, 0, -1, s[4:5]
+; GISEL-NEXT: v_cmp_eq_u32_e64 s[4:5], v2, v8
+; GISEL-NEXT: v_subbrev_co_u32_e32 v1, vcc, 0, v1, vcc
+; GISEL-NEXT: v_cndmask_b32_e64 v2, v3, v4, s[4:5]
+; GISEL-NEXT: v_add_co_u32_e32 v3, vcc, 1, v12
+; GISEL-NEXT: v_addc_co_u32_e32 v4, vcc, 0, v13, vcc
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v1, v8
+; GISEL-NEXT: v_cndmask_b32_e64 v5, 0, -1, vcc
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v0, v7
+; GISEL-NEXT: v_cndmask_b32_e64 v0, 0, -1, vcc
+; GISEL-NEXT: v_cmp_eq_u32_e32 vcc, v1, v8
+; GISEL-NEXT: v_cndmask_b32_e32 v0, v5, v0, vcc
+; GISEL-NEXT: v_add_co_u32_e32 v1, vcc, 1, v3
+; GISEL-NEXT: v_addc_co_u32_e32 v5, vcc, 0, v4, vcc
+; GISEL-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0
+; GISEL-NEXT: v_cndmask_b32_e32 v0, v3, v1, vcc
+; GISEL-NEXT: v_cndmask_b32_e32 v1, v4, v5, vcc
+; GISEL-NEXT: v_cmp_ne_u32_e32 vcc, 0, v2
+; GISEL-NEXT: v_cndmask_b32_e32 v0, v12, v0, vcc
+; GISEL-NEXT: v_xor_b32_e32 v2, v9, v6
+; GISEL-NEXT: v_cndmask_b32_e32 v1, v13, v1, vcc
+; GISEL-NEXT: v_xor_b32_e32 v0, v0, v2
+; GISEL-NEXT: v_xor_b32_e32 v1, v1, v2
+; GISEL-NEXT: v_sub_co_u32_e32 v0, vcc, v0, v2
+; GISEL-NEXT: v_subb_co_u32_e32 v1, vcc, v1, v2, vcc
+; GISEL-NEXT: ; implicit-def: $vgpr2
+; GISEL-NEXT: ; implicit-def: $vgpr4
+; GISEL-NEXT: s_andn2_saveexec_b64 s[6:7], s[6:7]
+; GISEL-NEXT: s_cbranch_execz .LBB4_2
+; GISEL-NEXT: .LBB4_4:
+; GISEL-NEXT: v_cvt_f32_u32_e32 v0, v2
+; GISEL-NEXT: v_sub_u32_e32 v1, 0, v2
+; GISEL-NEXT: v_rcp_iflag_f32_e32 v0, v0
+; GISEL-NEXT: v_mul_f32_e32 v0, 0x4f7ffffe, v0
+; GISEL-NEXT: v_cvt_u32_f32_e32 v0, v0
+; GISEL-NEXT: v_mul_lo_u32 v1, v1, v0
+; GISEL-NEXT: v_mul_hi_u32 v1, v0, v1
+; GISEL-NEXT: v_add_u32_e32 v0, v0, v1
+; GISEL-NEXT: v_mul_hi_u32 v0, v4, v0
+; GISEL-NEXT: v_mul_lo_u32 v1, v0, v2
+; GISEL-NEXT: v_sub_u32_e32 v1, v4, v1
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v1, v2
+; GISEL-NEXT: v_sub_u32_e32 v3, v1, v2
+; GISEL-NEXT: v_cndmask_b32_e32 v1, v1, v3, vcc
+; GISEL-NEXT: v_addc_co_u32_e64 v0, s[4:5], 0, v0, vcc
+; GISEL-NEXT: v_cmp_ge_u32_e32 vcc, v1, v2
+; GISEL-NEXT: v_addc_co_u32_e32 v0, vcc, 0, v0, vcc
+; GISEL-NEXT: v_mov_b32_e32 v1, 0
+; GISEL-NEXT: s_or_b64 exec, exec, s[6:7]
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
diff --git a/llvm/test/Transforms/PreISelIntrinsicLowering/AMDGPU/masked-divrem-vector-zero.ll b/llvm/test/Transforms/PreISelIntrinsicLowering/AMDGPU/masked-divrem-vector-zero.ll
new file mode 100644
index 00000000000000..7cc66023d7e04d
--- /dev/null
+++ b/llvm/test/Transforms/PreISelIntrinsicLowering/AMDGPU/masked-divrem-vector-zero.ll
@@ -0,0 +1,87 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=pre-isel-intrinsic-lowering -mtriple=amdgpu9.0a-amd-amdhsa -mcpu=gfx90a -S < %s | FileCheck %s --check-prefix=GCN
+; RUN: opt -passes=pre-isel-intrinsic-lowering -mtriple=r600 -mcpu=redwood -S < %s | FileCheck %s --check-prefix=R600
+
+define i32 @scalar_udiv(i32 %a, i32 %b, i1 %m) {
+; GCN-LABEL: define i32 @scalar_udiv(
+; GCN-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0:[0-9]+]] {
+; GCN-NEXT: [[R:%.*]] = call i32 @llvm.masked.udiv.i32(i32 [[A]], i32 [[B]], i1 [[M]])
+; GCN-NEXT: ret i32 [[R]]
+;
+; R600-LABEL: define i32 @scalar_udiv(
+; R600-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i1 [[M:%.*]]) #[[ATTR0:[0-9]+]] {
+; R600-NEXT: [[TMP1:%.*]] = freeze i1 [[M]]
+; R600-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[B]], i32 1
+; R600-NEXT: [[R:%.*]] = udiv i32 [[A]], [[TMP2]]
+; R600-NEXT: ret i32 [[R]]
+;
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define <2 x i32> @all_zero(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+; GCN-LABEL: define <2 x i32> @all_zero(
+; GCN-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; GCN-NEXT: [[D:%.*]] = and <2 x i32> [[B]], zeroinitializer
+; GCN-NEXT: ret <2 x i32> zeroinitializer
+;
+; R600-LABEL: define <2 x i32> @all_zero(
+; R600-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; R600-NEXT: [[D:%.*]] = and <2 x i32> [[B]], zeroinitializer
+; R600-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> [[A]], <2 x i32> [[D]], <2 x i1> [[M]])
+; R600-NEXT: ret <2 x i32> [[R]]
+;
+ %d = and <2 x i32> %b, zeroinitializer
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> %d, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @poison_divisor(<2 x i32> %a, <2 x i1> %m) {
+; GCN-LABEL: define <2 x i32> @poison_divisor(
+; GCN-SAME: <2 x i32> [[A:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; GCN-NEXT: ret <2 x i32> zeroinitializer
+;
+; R600-LABEL: define <2 x i32> @poison_divisor(
+; R600-SAME: <2 x i32> [[A:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; R600-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> [[A]], <2 x i32> poison, <2 x i1> [[M]])
+; R600-NEXT: ret <2 x i32> [[R]]
+;
+ %r = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> %a, <2 x i32> poison, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @one_lane_zero(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+; GCN-LABEL: define <2 x i32> @one_lane_zero(
+; GCN-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; GCN-NEXT: [[D:%.*]] = and <2 x i32> [[B]], <i32 0, i32 -1>
+; GCN-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[A]], <2 x i32> [[D]], <2 x i1> [[M]])
+; GCN-NEXT: ret <2 x i32> [[R]]
+;
+; R600-LABEL: define <2 x i32> @one_lane_zero(
+; R600-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; R600-NEXT: [[D:%.*]] = and <2 x i32> [[B]], <i32 0, i32 -1>
+; R600-NEXT: [[R:%.*]] = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> [[A]], <2 x i32> [[D]], <2 x i1> [[M]])
+; R600-NEXT: ret <2 x i32> [[R]]
+;
+ %d = and <2 x i32> %b, <i32 0, i32 -1>
+ %r = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> %a, <2 x i32> %d, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i128> @not_trap_free(<2 x i128> %a, <2 x i128> %b, <2 x i1> %m) {
+; GCN-LABEL: define <2 x i128> @not_trap_free(
+; GCN-SAME: <2 x i128> [[A:%.*]], <2 x i128> [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; GCN-NEXT: [[D:%.*]] = and <2 x i128> [[B]], zeroinitializer
+; GCN-NEXT: [[R:%.*]] = call <2 x i128> @llvm.masked.srem.v2i128(<2 x i128> [[A]], <2 x i128> [[D]], <2 x i1> [[M]])
+; GCN-NEXT: ret <2 x i128> [[R]]
+;
+; R600-LABEL: define <2 x i128> @not_trap_free(
+; R600-SAME: <2 x i128> [[A:%.*]], <2 x i128> [[B:%.*]], <2 x i1> [[M:%.*]]) #[[ATTR0]] {
+; R600-NEXT: [[D:%.*]] = and <2 x i128> [[B]], zeroinitializer
+; R600-NEXT: [[R:%.*]] = call <2 x i128> @llvm.masked.srem.v2i128(<2 x i128> [[A]], <2 x i128> [[D]], <2 x i1> [[M]])
+; R600-NEXT: ret <2 x i128> [[R]]
+;
+ %d = and <2 x i128> %b, zeroinitializer
+ %r = call <2 x i128> @llvm.masked.srem.v2i128(<2 x i128> %a, <2 x i128> %d, <2 x i1> %m)
+ ret <2 x i128> %r
+}
>From 5e2d76df310e48ac54e405000e726416d1c2e361 Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 13:17:56 +0100
Subject: [PATCH 6/9] [AArch64] Recognize trap-free integer div/rem
AArch64 integer divide defines the results for a zero divisor and for
signed overflow. Treat native i32/i64 and promoted i8/i16 as trap-free,
so masked div/rem uses the ordinary divide sequence, also for NEON and
other fixed-length vectors scalarized into lanes. Wider libcall types
and unpromoted widths stay guarded.
Assisted-by: Claude
---
llvm/lib/Target/AArch64/AArch64ISelLowering.h | 7 +
.../CostModel/AArch64/masked-divrem.ll | 154 +++---
.../GlobalISel/masked-divrem-vectors.ll | 6 +
.../CodeGen/AArch64/masked-divrem-scalar.ll | 477 ++++++++++++++++++
.../masked-divrem-vector-legalization.ll | 22 +-
.../AArch64/masked-sdiv-fixed-length.ll | 161 +++---
.../AArch64/masked-srem-fixed-length.ll | 201 +++-----
.../AArch64/masked-udiv-fixed-length.ll | 162 ++----
.../AArch64/masked-urem-fixed-length.ll | 196 +++----
.../AArch64/sve-fixed-length-masked-div.ll | 8 +-
.../AArch64/sve-fixed-length-masked-rem.ll | 8 +-
11 files changed, 870 insertions(+), 532 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/masked-divrem-scalar.ll
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.h b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
index 1f4419000824c1..674cdc23b382e9 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.h
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
@@ -413,6 +413,13 @@ class AArch64TargetLowering : public TargetLowering {
getExceptionSelectorRegister(ExceptionHandling EH,
const Constant *PersonalityFn) const override;
+ // A64 SDIV/UDIV return zero for division by zero and do not trap on signed
+ // overflow. Remainder uses these operations and ordinary ALU instructions.
+ // Wider integers can use libcalls and must keep their guards.
+ bool isIntDivRemTrapFree(EVT VT) const override {
+ return VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64;
+ }
+
bool isIntDivCheap(EVT VT, AttributeList Attr) const override;
bool canMergeStoresTo(unsigned AddressSpace, EVT MemVT,
diff --git a/llvm/test/Analysis/CostModel/AArch64/masked-divrem.ll b/llvm/test/Analysis/CostModel/AArch64/masked-divrem.ll
index 106cf2d1ecc232..2cee1f70909291 100644
--- a/llvm/test/Analysis/CostModel/AArch64/masked-divrem.ll
+++ b/llvm/test/Analysis/CostModel/AArch64/masked-divrem.ll
@@ -6,32 +6,32 @@
define void @udiv() {
; NEON-LABEL: 'udiv'
-; NEON-NEXT: Cost Model: Found costs of RThru:21 CodeSize:14 Lat:19 SizeLat:19 for: %V1I16 = call <1 x i16> @llvm.masked.udiv.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:34 CodeSize:16 Lat:22 SizeLat:22 for: %V2I16 = call <2 x i16> @llvm.masked.udiv.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:60 CodeSize:20 Lat:28 SizeLat:28 for: %V4I16 = call <4 x i16> @llvm.masked.udiv.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:112 CodeSize:28 Lat:40 SizeLat:40 for: %V8I16 = call <8 x i16> @llvm.masked.udiv.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:184 CodeSize:28 Lat:32 SizeLat:32 for: %V16I16 = call <16 x i16> @llvm.masked.udiv.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:424 CodeSize:76 Lat:112 SizeLat:112 for: %V32I16 = call <32 x i16> @llvm.masked.udiv.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:18 CodeSize:12 Lat:16 SizeLat:16 for: %V1I16 = call <1 x i16> @llvm.masked.udiv.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:12 Lat:16 SizeLat:16 for: %V2I16 = call <2 x i16> @llvm.masked.udiv.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:48 CodeSize:12 Lat:16 SizeLat:16 for: %V4I16 = call <4 x i16> @llvm.masked.udiv.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:88 CodeSize:12 Lat:16 SizeLat:16 for: %V8I16 = call <8 x i16> @llvm.masked.udiv.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:168 CodeSize:12 Lat:16 SizeLat:16 for: %V16I16 = call <16 x i16> @llvm.masked.udiv.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:328 CodeSize:12 Lat:16 SizeLat:16 for: %V32I16 = call <32 x i16> @llvm.masked.udiv.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I16 = call <vscale x 1 x i16> @llvm.masked.udiv.nxv1i16(<vscale x 1 x i16> poison, <vscale x 1 x i16> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I16 = call <vscale x 2 x i16> @llvm.masked.udiv.nxv2i16(<vscale x 2 x i16> poison, <vscale x 2 x i16> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I16 = call <vscale x 4 x i16> @llvm.masked.udiv.nxv4i16(<vscale x 4 x i16> poison, <vscale x 4 x i16> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I16 = call <vscale x 8 x i16> @llvm.masked.udiv.nxv8i16(<vscale x 8 x i16> poison, <vscale x 8 x i16> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I16 = call <vscale x 16 x i16> @llvm.masked.udiv.nxv16i16(<vscale x 16 x i16> poison, <vscale x 16 x i16> poison, <vscale x 16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV32I16 = call <vscale x 32 x i16> @llvm.masked.udiv.nxv32i16(<vscale x 32 x i16> poison, <vscale x 32 x i16> poison, <vscale x 32 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:21 CodeSize:14 Lat:19 SizeLat:19 for: %V1I32 = call <1 x i32> @llvm.masked.udiv.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:34 CodeSize:16 Lat:22 SizeLat:22 for: %V2I32 = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:60 CodeSize:20 Lat:28 SizeLat:28 for: %V4I32 = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:96 CodeSize:20 Lat:24 SizeLat:24 for: %V8I32 = call <8 x i32> @llvm.masked.udiv.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:184 CodeSize:28 Lat:32 SizeLat:32 for: %V16I32 = call <16 x i32> @llvm.masked.udiv.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:18 CodeSize:12 Lat:16 SizeLat:16 for: %V1I32 = call <1 x i32> @llvm.masked.udiv.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:12 Lat:16 SizeLat:16 for: %V2I32 = call <2 x i32> @llvm.masked.udiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:48 CodeSize:12 Lat:16 SizeLat:16 for: %V4I32 = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:88 CodeSize:12 Lat:16 SizeLat:16 for: %V8I32 = call <8 x i32> @llvm.masked.udiv.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:168 CodeSize:12 Lat:16 SizeLat:16 for: %V16I32 = call <16 x i32> @llvm.masked.udiv.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I32 = call <vscale x 1 x i32> @llvm.masked.udiv.nxv1i32(<vscale x 1 x i32> poison, <vscale x 1 x i32> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I32 = call <vscale x 2 x i32> @llvm.masked.udiv.nxv2i32(<vscale x 2 x i32> poison, <vscale x 2 x i32> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I32 = call <vscale x 4 x i32> @llvm.masked.udiv.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I32 = call <vscale x 8 x i32> @llvm.masked.udiv.nxv8i32(<vscale x 8 x i32> poison, <vscale x 8 x i32> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I32 = call <vscale x 16 x i32> @llvm.masked.udiv.nxv16i32(<vscale x 16 x i32> poison, <vscale x 16 x i32> poison, <vscale x 16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:21 CodeSize:14 Lat:19 SizeLat:19 for: %V1I64 = call <1 x i64> @llvm.masked.udiv.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:34 CodeSize:16 Lat:22 SizeLat:22 for: %V2I64 = call <2 x i64> @llvm.masked.udiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:128 CodeSize:92 Lat:96 SizeLat:96 for: %V4I64 = call <4 x i64> @llvm.masked.udiv.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:248 CodeSize:172 Lat:176 SizeLat:176 for: %V8I64 = call <8 x i64> @llvm.masked.udiv.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:18 CodeSize:12 Lat:16 SizeLat:16 for: %V1I64 = call <1 x i64> @llvm.masked.udiv.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:12 Lat:16 SizeLat:16 for: %V2I64 = call <2 x i64> @llvm.masked.udiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:48 CodeSize:12 Lat:16 SizeLat:16 for: %V4I64 = call <4 x i64> @llvm.masked.udiv.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:88 CodeSize:12 Lat:16 SizeLat:16 for: %V8I64 = call <8 x i64> @llvm.masked.udiv.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I64 = call <vscale x 1 x i64> @llvm.masked.udiv.nxv1i64(<vscale x 1 x i64> poison, <vscale x 1 x i64> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I64 = call <vscale x 2 x i64> @llvm.masked.udiv.nxv2i64(<vscale x 2 x i64> poison, <vscale x 2 x i64> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I64 = call <vscale x 4 x i64> @llvm.masked.udiv.nxv4i64(<vscale x 4 x i64> poison, <vscale x 4 x i64> poison, <vscale x 4 x i1> poison)
@@ -112,32 +112,32 @@ define void @udiv() {
define void @sdiv() {
; NEON-LABEL: 'sdiv'
-; NEON-NEXT: Cost Model: Found costs of RThru:21 CodeSize:14 Lat:19 SizeLat:19 for: %V1I16 = call <1 x i16> @llvm.masked.sdiv.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:34 CodeSize:16 Lat:22 SizeLat:22 for: %V2I16 = call <2 x i16> @llvm.masked.sdiv.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:60 CodeSize:20 Lat:28 SizeLat:28 for: %V4I16 = call <4 x i16> @llvm.masked.sdiv.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:112 CodeSize:28 Lat:40 SizeLat:40 for: %V8I16 = call <8 x i16> @llvm.masked.sdiv.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:184 CodeSize:28 Lat:32 SizeLat:32 for: %V16I16 = call <16 x i16> @llvm.masked.sdiv.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:424 CodeSize:76 Lat:112 SizeLat:112 for: %V32I16 = call <32 x i16> @llvm.masked.sdiv.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:18 CodeSize:12 Lat:16 SizeLat:16 for: %V1I16 = call <1 x i16> @llvm.masked.sdiv.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:12 Lat:16 SizeLat:16 for: %V2I16 = call <2 x i16> @llvm.masked.sdiv.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:48 CodeSize:12 Lat:16 SizeLat:16 for: %V4I16 = call <4 x i16> @llvm.masked.sdiv.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:88 CodeSize:12 Lat:16 SizeLat:16 for: %V8I16 = call <8 x i16> @llvm.masked.sdiv.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:168 CodeSize:12 Lat:16 SizeLat:16 for: %V16I16 = call <16 x i16> @llvm.masked.sdiv.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:328 CodeSize:12 Lat:16 SizeLat:16 for: %V32I16 = call <32 x i16> @llvm.masked.sdiv.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I16 = call <vscale x 1 x i16> @llvm.masked.sdiv.nxv1i16(<vscale x 1 x i16> poison, <vscale x 1 x i16> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I16 = call <vscale x 2 x i16> @llvm.masked.sdiv.nxv2i16(<vscale x 2 x i16> poison, <vscale x 2 x i16> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I16 = call <vscale x 4 x i16> @llvm.masked.sdiv.nxv4i16(<vscale x 4 x i16> poison, <vscale x 4 x i16> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I16 = call <vscale x 8 x i16> @llvm.masked.sdiv.nxv8i16(<vscale x 8 x i16> poison, <vscale x 8 x i16> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I16 = call <vscale x 16 x i16> @llvm.masked.sdiv.nxv16i16(<vscale x 16 x i16> poison, <vscale x 16 x i16> poison, <vscale x 16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV32I16 = call <vscale x 32 x i16> @llvm.masked.sdiv.nxv32i16(<vscale x 32 x i16> poison, <vscale x 32 x i16> poison, <vscale x 32 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:21 CodeSize:14 Lat:19 SizeLat:19 for: %V1I32 = call <1 x i32> @llvm.masked.sdiv.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:34 CodeSize:16 Lat:22 SizeLat:22 for: %V2I32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:60 CodeSize:20 Lat:28 SizeLat:28 for: %V4I32 = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:96 CodeSize:20 Lat:24 SizeLat:24 for: %V8I32 = call <8 x i32> @llvm.masked.sdiv.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:184 CodeSize:28 Lat:32 SizeLat:32 for: %V16I32 = call <16 x i32> @llvm.masked.sdiv.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:18 CodeSize:12 Lat:16 SizeLat:16 for: %V1I32 = call <1 x i32> @llvm.masked.sdiv.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:12 Lat:16 SizeLat:16 for: %V2I32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:48 CodeSize:12 Lat:16 SizeLat:16 for: %V4I32 = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:88 CodeSize:12 Lat:16 SizeLat:16 for: %V8I32 = call <8 x i32> @llvm.masked.sdiv.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:168 CodeSize:12 Lat:16 SizeLat:16 for: %V16I32 = call <16 x i32> @llvm.masked.sdiv.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I32 = call <vscale x 1 x i32> @llvm.masked.sdiv.nxv1i32(<vscale x 1 x i32> poison, <vscale x 1 x i32> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I32 = call <vscale x 2 x i32> @llvm.masked.sdiv.nxv2i32(<vscale x 2 x i32> poison, <vscale x 2 x i32> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I32 = call <vscale x 4 x i32> @llvm.masked.sdiv.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I32 = call <vscale x 8 x i32> @llvm.masked.sdiv.nxv8i32(<vscale x 8 x i32> poison, <vscale x 8 x i32> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I32 = call <vscale x 16 x i32> @llvm.masked.sdiv.nxv16i32(<vscale x 16 x i32> poison, <vscale x 16 x i32> poison, <vscale x 16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:21 CodeSize:14 Lat:19 SizeLat:19 for: %V1I64 = call <1 x i64> @llvm.masked.sdiv.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:34 CodeSize:16 Lat:22 SizeLat:22 for: %V2I64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:128 CodeSize:92 Lat:96 SizeLat:96 for: %V4I64 = call <4 x i64> @llvm.masked.sdiv.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:248 CodeSize:172 Lat:176 SizeLat:176 for: %V8I64 = call <8 x i64> @llvm.masked.sdiv.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:18 CodeSize:12 Lat:16 SizeLat:16 for: %V1I64 = call <1 x i64> @llvm.masked.sdiv.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:12 Lat:16 SizeLat:16 for: %V2I64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:48 CodeSize:12 Lat:16 SizeLat:16 for: %V4I64 = call <4 x i64> @llvm.masked.sdiv.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:88 CodeSize:12 Lat:16 SizeLat:16 for: %V8I64 = call <8 x i64> @llvm.masked.sdiv.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I64 = call <vscale x 1 x i64> @llvm.masked.sdiv.nxv1i64(<vscale x 1 x i64> poison, <vscale x 1 x i64> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I64 = call <vscale x 2 x i64> @llvm.masked.sdiv.nxv2i64(<vscale x 2 x i64> poison, <vscale x 2 x i64> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I64 = call <vscale x 4 x i64> @llvm.masked.sdiv.nxv4i64(<vscale x 4 x i64> poison, <vscale x 4 x i64> poison, <vscale x 4 x i1> poison)
@@ -218,32 +218,32 @@ define void @sdiv() {
define void @urem() {
; NEON-LABEL: 'urem'
-; NEON-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:7 SizeLat:7 for: %V1I16 = call <1 x i16> @llvm.masked.urem.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:20 CodeSize:8 Lat:10 SizeLat:10 for: %V2I16 = call <2 x i16> @llvm.masked.urem.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:40 CodeSize:12 Lat:16 SizeLat:16 for: %V4I16 = call <4 x i16> @llvm.masked.urem.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:80 CodeSize:20 Lat:28 SizeLat:28 for: %V8I16 = call <8 x i16> @llvm.masked.urem.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:128 CodeSize:20 Lat:20 SizeLat:20 for: %V16I16 = call <16 x i16> @llvm.masked.urem.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:320 CodeSize:68 Lat:100 SizeLat:100 for: %V32I16 = call <32 x i16> @llvm.masked.urem.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:7 CodeSize:4 Lat:4 SizeLat:4 for: %V1I16 = call <1 x i16> @llvm.masked.urem.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:14 CodeSize:4 Lat:4 SizeLat:4 for: %V2I16 = call <2 x i16> @llvm.masked.urem.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:4 Lat:4 SizeLat:4 for: %V4I16 = call <4 x i16> @llvm.masked.urem.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:56 CodeSize:4 Lat:4 SizeLat:4 for: %V8I16 = call <8 x i16> @llvm.masked.urem.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:112 CodeSize:4 Lat:4 SizeLat:4 for: %V16I16 = call <16 x i16> @llvm.masked.urem.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:224 CodeSize:4 Lat:4 SizeLat:4 for: %V32I16 = call <32 x i16> @llvm.masked.urem.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I16 = call <vscale x 1 x i16> @llvm.masked.urem.nxv1i16(<vscale x 1 x i16> poison, <vscale x 1 x i16> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I16 = call <vscale x 2 x i16> @llvm.masked.urem.nxv2i16(<vscale x 2 x i16> poison, <vscale x 2 x i16> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I16 = call <vscale x 4 x i16> @llvm.masked.urem.nxv4i16(<vscale x 4 x i16> poison, <vscale x 4 x i16> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I16 = call <vscale x 8 x i16> @llvm.masked.urem.nxv8i16(<vscale x 8 x i16> poison, <vscale x 8 x i16> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I16 = call <vscale x 16 x i16> @llvm.masked.urem.nxv16i16(<vscale x 16 x i16> poison, <vscale x 16 x i16> poison, <vscale x 16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV32I16 = call <vscale x 32 x i16> @llvm.masked.urem.nxv32i16(<vscale x 32 x i16> poison, <vscale x 32 x i16> poison, <vscale x 32 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:7 SizeLat:7 for: %V1I32 = call <1 x i32> @llvm.masked.urem.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:20 CodeSize:8 Lat:10 SizeLat:10 for: %V2I32 = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:40 CodeSize:12 Lat:16 SizeLat:16 for: %V4I32 = call <4 x i32> @llvm.masked.urem.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:64 CodeSize:12 Lat:12 SizeLat:12 for: %V8I32 = call <8 x i32> @llvm.masked.urem.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:128 CodeSize:20 Lat:20 SizeLat:20 for: %V16I32 = call <16 x i32> @llvm.masked.urem.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:7 CodeSize:4 Lat:4 SizeLat:4 for: %V1I32 = call <1 x i32> @llvm.masked.urem.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:14 CodeSize:4 Lat:4 SizeLat:4 for: %V2I32 = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:4 Lat:4 SizeLat:4 for: %V4I32 = call <4 x i32> @llvm.masked.urem.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:56 CodeSize:4 Lat:4 SizeLat:4 for: %V8I32 = call <8 x i32> @llvm.masked.urem.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:112 CodeSize:4 Lat:4 SizeLat:4 for: %V16I32 = call <16 x i32> @llvm.masked.urem.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I32 = call <vscale x 1 x i32> @llvm.masked.urem.nxv1i32(<vscale x 1 x i32> poison, <vscale x 1 x i32> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I32 = call <vscale x 2 x i32> @llvm.masked.urem.nxv2i32(<vscale x 2 x i32> poison, <vscale x 2 x i32> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I32 = call <vscale x 4 x i32> @llvm.masked.urem.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I32 = call <vscale x 8 x i32> @llvm.masked.urem.nxv8i32(<vscale x 8 x i32> poison, <vscale x 8 x i32> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I32 = call <vscale x 16 x i32> @llvm.masked.urem.nxv16i32(<vscale x 16 x i32> poison, <vscale x 16 x i32> poison, <vscale x 16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:7 SizeLat:7 for: %V1I64 = call <1 x i64> @llvm.masked.urem.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:20 CodeSize:8 Lat:10 SizeLat:10 for: %V2I64 = call <2 x i64> @llvm.masked.urem.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:108 CodeSize:84 Lat:84 SizeLat:84 for: %V4I64 = call <4 x i64> @llvm.masked.urem.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:216 CodeSize:164 Lat:164 SizeLat:164 for: %V8I64 = call <8 x i64> @llvm.masked.urem.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:7 CodeSize:4 Lat:4 SizeLat:4 for: %V1I64 = call <1 x i64> @llvm.masked.urem.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:14 CodeSize:4 Lat:4 SizeLat:4 for: %V2I64 = call <2 x i64> @llvm.masked.urem.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:4 Lat:4 SizeLat:4 for: %V4I64 = call <4 x i64> @llvm.masked.urem.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:56 CodeSize:4 Lat:4 SizeLat:4 for: %V8I64 = call <8 x i64> @llvm.masked.urem.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I64 = call <vscale x 1 x i64> @llvm.masked.urem.nxv1i64(<vscale x 1 x i64> poison, <vscale x 1 x i64> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I64 = call <vscale x 2 x i64> @llvm.masked.urem.nxv2i64(<vscale x 2 x i64> poison, <vscale x 2 x i64> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I64 = call <vscale x 4 x i64> @llvm.masked.urem.nxv4i64(<vscale x 4 x i64> poison, <vscale x 4 x i64> poison, <vscale x 4 x i1> poison)
@@ -324,32 +324,32 @@ define void @urem() {
define void @srem() {
; NEON-LABEL: 'srem'
-; NEON-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:7 SizeLat:7 for: %V1I16 = call <1 x i16> @llvm.masked.srem.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:20 CodeSize:8 Lat:10 SizeLat:10 for: %V2I16 = call <2 x i16> @llvm.masked.srem.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:40 CodeSize:12 Lat:16 SizeLat:16 for: %V4I16 = call <4 x i16> @llvm.masked.srem.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:80 CodeSize:20 Lat:28 SizeLat:28 for: %V8I16 = call <8 x i16> @llvm.masked.srem.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:128 CodeSize:20 Lat:20 SizeLat:20 for: %V16I16 = call <16 x i16> @llvm.masked.srem.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:320 CodeSize:68 Lat:100 SizeLat:100 for: %V32I16 = call <32 x i16> @llvm.masked.srem.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:7 CodeSize:4 Lat:4 SizeLat:4 for: %V1I16 = call <1 x i16> @llvm.masked.srem.v1i16(<1 x i16> poison, <1 x i16> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:14 CodeSize:4 Lat:4 SizeLat:4 for: %V2I16 = call <2 x i16> @llvm.masked.srem.v2i16(<2 x i16> poison, <2 x i16> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:4 Lat:4 SizeLat:4 for: %V4I16 = call <4 x i16> @llvm.masked.srem.v4i16(<4 x i16> poison, <4 x i16> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:56 CodeSize:4 Lat:4 SizeLat:4 for: %V8I16 = call <8 x i16> @llvm.masked.srem.v8i16(<8 x i16> poison, <8 x i16> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:112 CodeSize:4 Lat:4 SizeLat:4 for: %V16I16 = call <16 x i16> @llvm.masked.srem.v16i16(<16 x i16> poison, <16 x i16> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:224 CodeSize:4 Lat:4 SizeLat:4 for: %V32I16 = call <32 x i16> @llvm.masked.srem.v32i16(<32 x i16> poison, <32 x i16> poison, <32 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I16 = call <vscale x 1 x i16> @llvm.masked.srem.nxv1i16(<vscale x 1 x i16> poison, <vscale x 1 x i16> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I16 = call <vscale x 2 x i16> @llvm.masked.srem.nxv2i16(<vscale x 2 x i16> poison, <vscale x 2 x i16> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I16 = call <vscale x 4 x i16> @llvm.masked.srem.nxv4i16(<vscale x 4 x i16> poison, <vscale x 4 x i16> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I16 = call <vscale x 8 x i16> @llvm.masked.srem.nxv8i16(<vscale x 8 x i16> poison, <vscale x 8 x i16> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I16 = call <vscale x 16 x i16> @llvm.masked.srem.nxv16i16(<vscale x 16 x i16> poison, <vscale x 16 x i16> poison, <vscale x 16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV32I16 = call <vscale x 32 x i16> @llvm.masked.srem.nxv32i16(<vscale x 32 x i16> poison, <vscale x 32 x i16> poison, <vscale x 32 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:7 SizeLat:7 for: %V1I32 = call <1 x i32> @llvm.masked.srem.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:20 CodeSize:8 Lat:10 SizeLat:10 for: %V2I32 = call <2 x i32> @llvm.masked.srem.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:40 CodeSize:12 Lat:16 SizeLat:16 for: %V4I32 = call <4 x i32> @llvm.masked.srem.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:64 CodeSize:12 Lat:12 SizeLat:12 for: %V8I32 = call <8 x i32> @llvm.masked.srem.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:128 CodeSize:20 Lat:20 SizeLat:20 for: %V16I32 = call <16 x i32> @llvm.masked.srem.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:7 CodeSize:4 Lat:4 SizeLat:4 for: %V1I32 = call <1 x i32> @llvm.masked.srem.v1i32(<1 x i32> poison, <1 x i32> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:14 CodeSize:4 Lat:4 SizeLat:4 for: %V2I32 = call <2 x i32> @llvm.masked.srem.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:4 Lat:4 SizeLat:4 for: %V4I32 = call <4 x i32> @llvm.masked.srem.v4i32(<4 x i32> poison, <4 x i32> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:56 CodeSize:4 Lat:4 SizeLat:4 for: %V8I32 = call <8 x i32> @llvm.masked.srem.v8i32(<8 x i32> poison, <8 x i32> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:112 CodeSize:4 Lat:4 SizeLat:4 for: %V16I32 = call <16 x i32> @llvm.masked.srem.v16i32(<16 x i32> poison, <16 x i32> poison, <16 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I32 = call <vscale x 1 x i32> @llvm.masked.srem.nxv1i32(<vscale x 1 x i32> poison, <vscale x 1 x i32> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I32 = call <vscale x 2 x i32> @llvm.masked.srem.nxv2i32(<vscale x 2 x i32> poison, <vscale x 2 x i32> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I32 = call <vscale x 4 x i32> @llvm.masked.srem.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> poison, <vscale x 4 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV8I32 = call <vscale x 8 x i32> @llvm.masked.srem.nxv8i32(<vscale x 8 x i32> poison, <vscale x 8 x i32> poison, <vscale x 8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV16I32 = call <vscale x 16 x i32> @llvm.masked.srem.nxv16i32(<vscale x 16 x i32> poison, <vscale x 16 x i32> poison, <vscale x 16 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:7 SizeLat:7 for: %V1I64 = call <1 x i64> @llvm.masked.srem.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:20 CodeSize:8 Lat:10 SizeLat:10 for: %V2I64 = call <2 x i64> @llvm.masked.srem.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:108 CodeSize:84 Lat:84 SizeLat:84 for: %V4I64 = call <4 x i64> @llvm.masked.srem.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
-; NEON-NEXT: Cost Model: Found costs of RThru:216 CodeSize:164 Lat:164 SizeLat:164 for: %V8I64 = call <8 x i64> @llvm.masked.srem.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:7 CodeSize:4 Lat:4 SizeLat:4 for: %V1I64 = call <1 x i64> @llvm.masked.srem.v1i64(<1 x i64> poison, <1 x i64> poison, <1 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:14 CodeSize:4 Lat:4 SizeLat:4 for: %V2I64 = call <2 x i64> @llvm.masked.srem.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:28 CodeSize:4 Lat:4 SizeLat:4 for: %V4I64 = call <4 x i64> @llvm.masked.srem.v4i64(<4 x i64> poison, <4 x i64> poison, <4 x i1> poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:56 CodeSize:4 Lat:4 SizeLat:4 for: %V8I64 = call <8 x i64> @llvm.masked.srem.v8i64(<8 x i64> poison, <8 x i64> poison, <8 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of Invalid for: %NXV1I64 = call <vscale x 1 x i64> @llvm.masked.srem.nxv1i64(<vscale x 1 x i64> poison, <vscale x 1 x i64> poison, <vscale x 1 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV2I64 = call <vscale x 2 x i64> @llvm.masked.srem.nxv2i64(<vscale x 2 x i64> poison, <vscale x 2 x i64> poison, <vscale x 2 x i1> poison)
; NEON-NEXT: Cost Model: Found costs of RThru:Invalid CodeSize:Invalid Lat:Invalid SizeLat:Invalid for: %NXV4I64 = call <vscale x 4 x i64> @llvm.masked.srem.nxv4i64(<vscale x 4 x i64> poison, <vscale x 4 x i64> poison, <vscale x 4 x i1> poison)
@@ -427,3 +427,37 @@ define void @srem() {
ret void
}
+
+define void @scalar() {
+; NEON-LABEL: 'scalar'
+; NEON-NEXT: Cost Model: Found costs of 0 for: %S1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %U1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:1 CodeSize:4 Lat:4 SizeLat:4 for: %I8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:3 CodeSize:4 Lat:4 SizeLat:4 for: %I16 = call i16 @llvm.masked.urem.i16(i16 poison, i16 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:1 CodeSize:4 Lat:4 SizeLat:4 for: %I32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:3 CodeSize:4 Lat:4 SizeLat:4 for: %I64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %I10 = call i10 @llvm.masked.sdiv.i10(i10 poison, i10 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of 12 for: %I128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+; NEON-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+; SVE-LABEL: 'scalar'
+; SVE-NEXT: Cost Model: Found costs of 0 for: %S1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %U1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:1 CodeSize:4 Lat:4 SizeLat:4 for: %I8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:3 CodeSize:4 Lat:4 SizeLat:4 for: %I16 = call i16 @llvm.masked.urem.i16(i16 poison, i16 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:1 CodeSize:4 Lat:4 SizeLat:4 for: %I32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:3 CodeSize:4 Lat:4 SizeLat:4 for: %I64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %I10 = call i10 @llvm.masked.sdiv.i10(i10 poison, i10 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of 12 for: %I128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+; SVE-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+ %S1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+ %U1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+ %I8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+ %I16 = call i16 @llvm.masked.urem.i16(i16 poison, i16 poison, i1 poison)
+ %I32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+ %I64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+ %I10 = call i10 @llvm.masked.sdiv.i10(i10 poison, i10 poison, i1 poison)
+ %I128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+ ret void
+}
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll b/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll
index 3c9f6b2ebfd230..27803a7e2f87f3 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/masked-divrem-vectors.ll
@@ -3,6 +3,7 @@
; CHECK-LABEL: name: sdiv_v3i8
; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_SELECT
; CHECK-COUNT-3: G_SDIV
; CHECK-COUNT-3: G_FREEZE
define void @sdiv_v3i8(ptr %p, <3 x i8> %a, <3 x i8> %b, <3 x i1> %m) {
@@ -14,6 +15,7 @@ define void @sdiv_v3i8(ptr %p, <3 x i8> %a, <3 x i8> %b, <3 x i1> %m) {
; CHECK-LABEL: name: urem_v3i8
; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_SELECT
; CHECK-COUNT-3: G_UREM
; CHECK-COUNT-3: G_FREEZE
define void @urem_v3i8(ptr %p, <3 x i8> %a, <3 x i8> %b, <3 x i1> %m) {
@@ -25,6 +27,7 @@ define void @urem_v3i8(ptr %p, <3 x i8> %a, <3 x i8> %b, <3 x i1> %m) {
; CHECK-LABEL: name: srem_v2i64
; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_SELECT
; CHECK-COUNT-2: G_SREM
; CHECK-COUNT-2: G_FREEZE
define void @srem_v2i64(ptr %p, <2 x i64> %a, <2 x i64> %b, <2 x i1> %m) {
@@ -66,6 +69,7 @@ define void @freeze_abs_v4i32(ptr %p, <4 x i32> %a) {
}
; CHECK-LABEL: name: freeze_scalar_sdiv
+; CHECK-NOT: G_SELECT
; CHECK: [[DIV:%[0-9]+]]:_(i32) = G_SDIV
; CHECK-NEXT: {{%[0-9]+}}:_(i32) = G_FREEZE [[DIV]]
define i32 @freeze_scalar_sdiv(i32 %a, i32 %b, i1 %m) {
@@ -76,6 +80,7 @@ define i32 @freeze_scalar_sdiv(i32 %a, i32 %b, i1 %m) {
; CHECK-LABEL: name: sdiv_v1i32
; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_SELECT
; CHECK-NOT: G_BUILD_VECTOR
; CHECK: G_SDIV
define <1 x i32> @sdiv_v1i32(<1 x i32> %a, <1 x i32> %b, <1 x i1> %m) {
@@ -85,6 +90,7 @@ define <1 x i32> @sdiv_v1i32(<1 x i32> %a, <1 x i32> %b, <1 x i1> %m) {
; CHECK-LABEL: name: udiv_v1i8
; CHECK-NOT: G_INTRINSIC intrinsic(@llvm.masked
+; CHECK-NOT: G_SELECT
; CHECK-NOT: G_BUILD_VECTOR
; CHECK: G_UDIV
define <1 x i8> @udiv_v1i8(<1 x i8> %a, <1 x i8> %b, <1 x i1> %m) {
diff --git a/llvm/test/CodeGen/AArch64/masked-divrem-scalar.ll b/llvm/test/CodeGen/AArch64/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..edaeec43d4d8b6
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/masked-divrem-scalar.ll
@@ -0,0 +1,477 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=aarch64 < %s | FileCheck %s --check-prefix=SDAG
+; RUN: llc -mtriple=aarch64 -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=GISEL
+; RUN: llc -O0 -mtriple=aarch64 -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=GISEL-O0
+
+define i1 @sdiv_i1(i1 %a, i1 %b, i1 %m) {
+; SDAG-LABEL: sdiv_i1:
+; SDAG: // %bb.0:
+; SDAG-NEXT: mov w0, wzr
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: sdiv_i1:
+; GISEL: // %bb.0:
+; GISEL-NEXT: mov w0, wzr
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: sdiv_i1:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: mov w8, wzr
+; GISEL-O0-NEXT: and w0, w8, #0x1
+; GISEL-O0-NEXT: ret
+ %r = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i1 @udiv_i1(i1 %a, i1 %b, i1 %m) {
+; SDAG-LABEL: udiv_i1:
+; SDAG: // %bb.0:
+; SDAG-NEXT: and w0, w0, #0x1
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: udiv_i1:
+; GISEL: // %bb.0:
+; GISEL-NEXT: eor w8, w2, #0x1
+; GISEL-NEXT: and w9, w0, #0x1
+; GISEL-NEXT: orr w8, w8, w1
+; GISEL-NEXT: and w8, w8, #0x1
+; GISEL-NEXT: udiv w0, w9, w8
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: udiv_i1:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: and w8, w2, #0x1
+; GISEL-O0-NEXT: ands w8, w8, #0x1
+; GISEL-O0-NEXT: csinc w9, w1, wzr, ne
+; GISEL-O0-NEXT: and w8, w0, #0x1
+; GISEL-O0-NEXT: and w9, w9, #0x1
+; GISEL-O0-NEXT: udiv w8, w8, w9
+; GISEL-O0-NEXT: and w0, w8, #0x1
+; GISEL-O0-NEXT: ret
+ %r = call i1 @llvm.masked.udiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i8 @udiv_i8(i8 %a, i8 %b, i1 %m) {
+; SDAG-LABEL: udiv_i8:
+; SDAG: // %bb.0:
+; SDAG-NEXT: and w8, w1, #0xff
+; SDAG-NEXT: and w9, w0, #0xff
+; SDAG-NEXT: udiv w0, w9, w8
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: udiv_i8:
+; GISEL: // %bb.0:
+; GISEL-NEXT: and w8, w0, #0xff
+; GISEL-NEXT: and w9, w1, #0xff
+; GISEL-NEXT: udiv w0, w8, w9
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: udiv_i8:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: and w8, w0, #0xff
+; GISEL-O0-NEXT: and w9, w1, #0xff
+; GISEL-O0-NEXT: udiv w0, w8, w9
+; GISEL-O0-NEXT: ret
+ %r = call i8 @llvm.masked.udiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i8 @srem_i8(i8 %a, i8 %b, i1 %m) {
+; SDAG-LABEL: srem_i8:
+; SDAG: // %bb.0:
+; SDAG-NEXT: sxtb w8, w1
+; SDAG-NEXT: sxtb w9, w0
+; SDAG-NEXT: sdiv w10, w9, w8
+; SDAG-NEXT: msub w0, w10, w8, w9
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: srem_i8:
+; GISEL: // %bb.0:
+; GISEL-NEXT: sxtb w8, w0
+; GISEL-NEXT: sxtb w9, w1
+; GISEL-NEXT: sdiv w8, w8, w9
+; GISEL-NEXT: msub w0, w8, w1, w0
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: srem_i8:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: sxtb w8, w0
+; GISEL-O0-NEXT: sxtb w9, w1
+; GISEL-O0-NEXT: sdiv w8, w8, w9
+; GISEL-O0-NEXT: mul w8, w8, w1
+; GISEL-O0-NEXT: subs w0, w0, w8
+; GISEL-O0-NEXT: ret
+ %r = call i8 @llvm.masked.srem.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i16 @sdiv_i16(i16 %a, i16 %b, i1 %m) {
+; SDAG-LABEL: sdiv_i16:
+; SDAG: // %bb.0:
+; SDAG-NEXT: sxth w8, w1
+; SDAG-NEXT: sxth w9, w0
+; SDAG-NEXT: sdiv w0, w9, w8
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: sdiv_i16:
+; GISEL: // %bb.0:
+; GISEL-NEXT: sxth w8, w0
+; GISEL-NEXT: sxth w9, w1
+; GISEL-NEXT: sdiv w0, w8, w9
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: sdiv_i16:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: sxth w8, w0
+; GISEL-O0-NEXT: sxth w9, w1
+; GISEL-O0-NEXT: sdiv w0, w8, w9
+; GISEL-O0-NEXT: ret
+ %r = call i16 @llvm.masked.sdiv.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i16 @urem_i16(i16 %a, i16 %b, i1 %m) {
+; SDAG-LABEL: urem_i16:
+; SDAG: // %bb.0:
+; SDAG-NEXT: and w8, w1, #0xffff
+; SDAG-NEXT: and w9, w0, #0xffff
+; SDAG-NEXT: udiv w10, w9, w8
+; SDAG-NEXT: msub w0, w10, w8, w9
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: urem_i16:
+; GISEL: // %bb.0:
+; GISEL-NEXT: and w8, w0, #0xffff
+; GISEL-NEXT: and w9, w1, #0xffff
+; GISEL-NEXT: udiv w8, w8, w9
+; GISEL-NEXT: msub w0, w8, w1, w0
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: urem_i16:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: and w8, w0, #0xffff
+; GISEL-O0-NEXT: and w9, w1, #0xffff
+; GISEL-O0-NEXT: udiv w8, w8, w9
+; GISEL-O0-NEXT: mul w8, w8, w1
+; GISEL-O0-NEXT: subs w0, w0, w8
+; GISEL-O0-NEXT: ret
+ %r = call i16 @llvm.masked.urem.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i32 @udiv_i32(i32 %a, i32 %b, i1 %m) {
+; SDAG-LABEL: udiv_i32:
+; SDAG: // %bb.0:
+; SDAG-NEXT: udiv w0, w0, w1
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: udiv_i32:
+; GISEL: // %bb.0:
+; GISEL-NEXT: udiv w0, w0, w1
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: udiv_i32:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: udiv w0, w0, w1
+; GISEL-O0-NEXT: ret
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; SDAG-LABEL: sdiv_i32:
+; SDAG: // %bb.0:
+; SDAG-NEXT: sdiv w0, w0, w1
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: sdiv_i32:
+; GISEL: // %bb.0:
+; GISEL-NEXT: sdiv w0, w0, w1
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: sdiv_i32:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: sdiv w0, w0, w1
+; GISEL-O0-NEXT: ret
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+; SDAG-LABEL: urem_i32:
+; SDAG: // %bb.0:
+; SDAG-NEXT: udiv w8, w0, w1
+; SDAG-NEXT: msub w0, w8, w1, w0
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: urem_i32:
+; GISEL: // %bb.0:
+; GISEL-NEXT: udiv w8, w0, w1
+; GISEL-NEXT: msub w0, w8, w1, w0
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: urem_i32:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: udiv w8, w0, w1
+; GISEL-O0-NEXT: mul w8, w8, w1
+; GISEL-O0-NEXT: subs w0, w0, w8
+; GISEL-O0-NEXT: ret
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i64 @srem_i64(i64 %a, i64 %b, i1 %m) {
+; SDAG-LABEL: srem_i64:
+; SDAG: // %bb.0:
+; SDAG-NEXT: sdiv x8, x0, x1
+; SDAG-NEXT: msub x0, x8, x1, x0
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: srem_i64:
+; GISEL: // %bb.0:
+; GISEL-NEXT: sdiv x8, x0, x1
+; GISEL-NEXT: msub x0, x8, x1, x0
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: srem_i64:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: sdiv x8, x0, x1
+; GISEL-O0-NEXT: mul x8, x8, x1
+; GISEL-O0-NEXT: subs x0, x0, x8
+; GISEL-O0-NEXT: ret
+ %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i32 @sdiv_false_mask(i32 %a, i32 %b) {
+; SDAG-LABEL: sdiv_false_mask:
+; SDAG: // %bb.0:
+; SDAG-NEXT: mov w0, wzr
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: sdiv_false_mask:
+; GISEL: // %bb.0:
+; GISEL-NEXT: mov w0, wzr
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: sdiv_false_mask:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: mov w0, wzr
+; GISEL-O0-NEXT: ret
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 false)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define i32 @udiv_known_zero(i32 %a, i32 %b, i1 %m) {
+; SDAG-LABEL: udiv_known_zero:
+; SDAG: // %bb.0:
+; SDAG-NEXT: mov w0, wzr
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: udiv_known_zero:
+; GISEL: // %bb.0:
+; GISEL-NEXT: mov w0, wzr
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: udiv_known_zero:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: mov w0, wzr
+; GISEL-O0-NEXT: ret
+ %z = and i32 %b, 0
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %z, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define i10 @odd_guard_i10(i10 %a, i10 %b, i1 %m) {
+; SDAG-LABEL: odd_guard_i10:
+; SDAG: // %bb.0:
+; SDAG-NEXT: tst w2, #0x1
+; SDAG-NEXT: sbfx w9, w0, #0, #10
+; SDAG-NEXT: csinc w8, w1, wzr, ne
+; SDAG-NEXT: sbfx w8, w8, #0, #10
+; SDAG-NEXT: sdiv w0, w9, w8
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: odd_guard_i10:
+; GISEL: // %bb.0:
+; GISEL-NEXT: and w8, w2, #0x1
+; GISEL-NEXT: sbfx w9, w0, #0, #10
+; GISEL-NEXT: tst w8, #0x1
+; GISEL-NEXT: csinc w8, w1, wzr, ne
+; GISEL-NEXT: sbfx w8, w8, #0, #10
+; GISEL-NEXT: sdiv w0, w9, w8
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: odd_guard_i10:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: and w8, w2, #0x1
+; GISEL-O0-NEXT: ands w8, w8, #0x1
+; GISEL-O0-NEXT: csinc w9, w1, wzr, ne
+; GISEL-O0-NEXT: sbfx w8, w0, #0, #10
+; GISEL-O0-NEXT: sbfx w9, w9, #0, #10
+; GISEL-O0-NEXT: sdiv w0, w8, w9
+; GISEL-O0-NEXT: ret
+ %r = call i10 @llvm.masked.sdiv.i10(i10 %a, i10 %b, i1 %m)
+ ret i10 %r
+}
+
+define i128 @wide_guard_sdiv(i128 %a, i128 %b, i1 %m) {
+; SDAG-LABEL: wide_guard_sdiv:
+; SDAG: // %bb.0:
+; SDAG-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
+; SDAG-NEXT: .cfi_def_cfa_offset 16
+; SDAG-NEXT: .cfi_offset w30, -16
+; SDAG-NEXT: tst w4, #0x1
+; SDAG-NEXT: csel x3, x3, xzr, ne
+; SDAG-NEXT: csinc x2, x2, xzr, ne
+; SDAG-NEXT: bl __divti3
+; SDAG-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: wide_guard_sdiv:
+; GISEL: // %bb.0:
+; GISEL-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
+; GISEL-NEXT: .cfi_def_cfa_offset 16
+; GISEL-NEXT: .cfi_offset w30, -16
+; GISEL-NEXT: and w8, w4, #0x1
+; GISEL-NEXT: tst w8, #0x1
+; GISEL-NEXT: csinc x2, x2, xzr, ne
+; GISEL-NEXT: csel x3, x3, xzr, ne
+; GISEL-NEXT: bl __divti3
+; GISEL-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: wide_guard_sdiv:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
+; GISEL-O0-NEXT: .cfi_def_cfa_offset 16
+; GISEL-O0-NEXT: .cfi_offset w30, -16
+; GISEL-O0-NEXT: mov x8, xzr
+; GISEL-O0-NEXT: mov w9, w4
+; GISEL-O0-NEXT: and w9, w9, #0x1
+; GISEL-O0-NEXT: ands w9, w9, #0x1
+; GISEL-O0-NEXT: csinc x2, x2, xzr, ne
+; GISEL-O0-NEXT: and w9, w4, #0x1
+; GISEL-O0-NEXT: ands w9, w9, #0x1
+; GISEL-O0-NEXT: csel x3, x3, x8, ne
+; GISEL-O0-NEXT: bl __divti3
+; GISEL-O0-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; GISEL-O0-NEXT: ret
+ %r = call i128 @llvm.masked.sdiv.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
+
+define i128 @wide_guard_urem(i128 %a, i128 %b, i1 %m) {
+; SDAG-LABEL: wide_guard_urem:
+; SDAG: // %bb.0:
+; SDAG-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
+; SDAG-NEXT: .cfi_def_cfa_offset 16
+; SDAG-NEXT: .cfi_offset w30, -16
+; SDAG-NEXT: tst w4, #0x1
+; SDAG-NEXT: csel x3, x3, xzr, ne
+; SDAG-NEXT: csinc x2, x2, xzr, ne
+; SDAG-NEXT: bl __umodti3
+; SDAG-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: wide_guard_urem:
+; GISEL: // %bb.0:
+; GISEL-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
+; GISEL-NEXT: .cfi_def_cfa_offset 16
+; GISEL-NEXT: .cfi_offset w30, -16
+; GISEL-NEXT: and w8, w4, #0x1
+; GISEL-NEXT: tst w8, #0x1
+; GISEL-NEXT: csinc x2, x2, xzr, ne
+; GISEL-NEXT: csel x3, x3, xzr, ne
+; GISEL-NEXT: bl __umodti3
+; GISEL-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: wide_guard_urem:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill
+; GISEL-O0-NEXT: .cfi_def_cfa_offset 16
+; GISEL-O0-NEXT: .cfi_offset w30, -16
+; GISEL-O0-NEXT: mov x8, xzr
+; GISEL-O0-NEXT: mov w9, w4
+; GISEL-O0-NEXT: and w9, w9, #0x1
+; GISEL-O0-NEXT: ands w9, w9, #0x1
+; GISEL-O0-NEXT: csinc x2, x2, xzr, ne
+; GISEL-O0-NEXT: and w9, w4, #0x1
+; GISEL-O0-NEXT: ands w9, w9, #0x1
+; GISEL-O0-NEXT: csel x3, x3, x8, ne
+; GISEL-O0-NEXT: bl __umodti3
+; GISEL-O0-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload
+; GISEL-O0-NEXT: ret
+ %r = call i128 @llvm.masked.urem.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
+
+define <4 x i32> @vec_known_zero_and_poison(<4 x i32> %a, <4 x i32> %b, <4 x i1> %m) {
+; SDAG-LABEL: vec_known_zero_and_poison:
+; SDAG: // %bb.0:
+; SDAG-NEXT: fmov w8, s1
+; SDAG-NEXT: fmov w9, s0
+; SDAG-NEXT: movi v0.2d, #0000000000000000
+; SDAG-NEXT: sdiv w8, w9, w8
+; SDAG-NEXT: mov w9, #43691 // =0xaaab
+; SDAG-NEXT: movk w9, #43690, lsl #16
+; SDAG-NEXT: umull x8, w8, w9
+; SDAG-NEXT: lsr x8, x8, #33
+; SDAG-NEXT: mov v0.s[0], w8
+; SDAG-NEXT: ret
+;
+; GISEL-LABEL: vec_known_zero_and_poison:
+; GISEL: // %bb.0:
+; GISEL-NEXT: adrp x8, .LCPI15_0
+; GISEL-NEXT: ldr q2, [x8, :lo12:.LCPI15_0]
+; GISEL-NEXT: fmov w8, s0
+; GISEL-NEXT: and v1.16b, v1.16b, v2.16b
+; GISEL-NEXT: fmov w9, s1
+; GISEL-NEXT: sdiv w8, w8, w9
+; GISEL-NEXT: mov w9, #43691 // =0xaaab
+; GISEL-NEXT: movk w9, #43690, lsl #16
+; GISEL-NEXT: umull x8, w8, w9
+; GISEL-NEXT: lsr x8, x8, #33
+; GISEL-NEXT: fmov s0, w8
+; GISEL-NEXT: mov v0.s[1], wzr
+; GISEL-NEXT: mov v0.s[2], wzr
+; GISEL-NEXT: mov v0.s[3], wzr
+; GISEL-NEXT: ret
+;
+; GISEL-O0-LABEL: vec_known_zero_and_poison:
+; GISEL-O0: // %bb.0:
+; GISEL-O0-NEXT: sub sp, sp, #16
+; GISEL-O0-NEXT: .cfi_def_cfa_offset 16
+; GISEL-O0-NEXT: str q0, [sp] // 16-byte Spill
+; GISEL-O0-NEXT: mov v0.16b, v1.16b
+; GISEL-O0-NEXT: ldr q1, [sp] // 16-byte Reload
+; GISEL-O0-NEXT: adrp x8, .LCPI15_0
+; GISEL-O0-NEXT: ldr q2, [x8, :lo12:.LCPI15_0]
+; GISEL-O0-NEXT: mov w11, #3 // =0x3
+; GISEL-O0-NEXT: mov w9, #7 // =0x7
+; GISEL-O0-NEXT: and v0.16b, v0.16b, v2.16b
+; GISEL-O0-NEXT: // kill: def $s1 killed $s1 killed $q1
+; GISEL-O0-NEXT: // kill: def $s0 killed $s0 killed $q0
+; GISEL-O0-NEXT: fmov w8, s1
+; GISEL-O0-NEXT: fmov w10, s0
+; GISEL-O0-NEXT: sdiv w10, w8, w10
+; GISEL-O0-NEXT: mov w8, wzr
+; GISEL-O0-NEXT: udiv w10, w10, w11
+; GISEL-O0-NEXT: udiv w9, w8, w9
+; GISEL-O0-NEXT: // implicit-def: $q0
+; GISEL-O0-NEXT: fmov s0, w10
+; GISEL-O0-NEXT: mov v0.s[1], w9
+; GISEL-O0-NEXT: mov v0.s[2], w8
+; GISEL-O0-NEXT: mov v0.s[3], w8
+; GISEL-O0-NEXT: add sp, sp, #16
+; GISEL-O0-NEXT: ret
+ %z = and <4 x i32> %b, <i32 -1, i32 0, i32 -1, i32 -1>
+ %r1 = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> %a, <4 x i32> %z, <4 x i1> %m)
+ %r2 = call <4 x i32> @llvm.masked.udiv.v4i32(<4 x i32> %r1, <4 x i32> <i32 3, i32 7, i32 0, i32 poison>, <4 x i1> %m)
+ %f = freeze <4 x i32> %r2
+ ret <4 x i32> %f
+}
diff --git a/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll b/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll
index 8498af8aa81ff9..526d7f59919095 100644
--- a/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll
+++ b/llvm/test/CodeGen/AArch64/masked-divrem-vector-legalization.ll
@@ -5,11 +5,9 @@ define <1 x i64> @v1(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; CHECK-LABEL: v1:
; CHECK: // %bb.0:
; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
-; CHECK-NEXT: fmov x8, d1
; CHECK-NEXT: // kill: def $d0 killed $d0 def $q0
+; CHECK-NEXT: fmov x8, d1
; CHECK-NEXT: fmov x9, d0
-; CHECK-NEXT: tst w0, #0x1
-; CHECK-NEXT: csinc x8, x8, xzr, ne
; CHECK-NEXT: sdiv x8, x9, x8
; CHECK-NEXT: fmov d0, x8
; CHECK-NEXT: ret
@@ -131,3 +129,21 @@ define <1 x i1> @signed_i1(<1 x i1> %x, <1 x i1> %y, <1 x i1> %m) {
%r = call <1 x i1> @llvm.masked.sdiv.v1i1(<1 x i1> %x, <1 x i1> %y, <1 x i1> %m)
ret <1 x i1> %r
}
+
+define <2 x i64> @v2_zero_lane(<2 x i64> %x, i64 %y, <2 x i1> %m) {
+; CHECK-LABEL: v2_zero_lane:
+; CHECK: // %bb.0:
+; CHECK-NEXT: ushll v1.2d, v1.2s, #0
+; CHECK-NEXT: movi v2.2d, #0000000000000000
+; CHECK-NEXT: // kill: def $q0 killed $q0 def $z0
+; CHECK-NEXT: ptrue p0.d, vl2
+; CHECK-NEXT: shl v1.2d, v1.2d, #63
+; CHECK-NEXT: mov v2.d[1], x0
+; CHECK-NEXT: cmpne p1.d, p0/z, z1.d, #0
+; CHECK-NEXT: sdiv z0.d, p1/m, z0.d, z2.d
+; CHECK-NEXT: // kill: def $q0 killed $q0 killed $z0
+; CHECK-NEXT: ret
+ %ins = insertelement <2 x i64> <i64 0, i64 poison>, i64 %y, i64 1
+ %r = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> %x, <2 x i64> %ins, <2 x i1> %m)
+ ret <2 x i64> %r
+}
diff --git a/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll b/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll
index 62fb0529ffe61a..0674351f75cffa 100644
--- a/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll
+++ b/llvm/test/CodeGen/AArch64/masked-sdiv-fixed-length.ll
@@ -6,25 +6,19 @@
define <4 x i32> @sdiv_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; NEON-LABEL: sdiv_v4i32:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.4s, v2.4h, #0
-; NEON-NEXT: fmov w8, s0
+; NEON-NEXT: mov w8, v1.s[1]
+; NEON-NEXT: mov w9, v0.s[1]
+; NEON-NEXT: fmov w10, s0
+; NEON-NEXT: mov w11, v0.s[2]
; NEON-NEXT: mov w12, v0.s[3]
-; NEON-NEXT: shl v2.4s, v2.4s, #31
-; NEON-NEXT: cmlt v2.4s, v2.4s, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.4s, v1.4s, v2.4s
+; NEON-NEXT: sdiv w8, w9, w8
; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w10, v1.s[1]
-; NEON-NEXT: mov w11, v1.s[2]
-; NEON-NEXT: sdiv w8, w8, w9
-; NEON-NEXT: mov w9, v0.s[1]
-; NEON-NEXT: sdiv w9, w9, w10
-; NEON-NEXT: mov w10, v0.s[2]
-; NEON-NEXT: fmov s0, w8
-; NEON-NEXT: sdiv w10, w10, w11
+; NEON-NEXT: sdiv w9, w10, w9
+; NEON-NEXT: mov w10, v1.s[2]
+; NEON-NEXT: sdiv w10, w11, w10
; NEON-NEXT: mov w11, v1.s[3]
-; NEON-NEXT: mov v0.s[1], w9
+; NEON-NEXT: fmov s0, w9
+; NEON-NEXT: mov v0.s[1], w8
; NEON-NEXT: sdiv w8, w12, w11
; NEON-NEXT: mov v0.s[2], w10
; NEON-NEXT: mov v0.s[3], w8
@@ -48,18 +42,12 @@ define <4 x i32> @sdiv_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
define <2 x i64> @sdiv_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; NEON-LABEL: sdiv_v2i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.2d, v2.2s, #0
-; NEON-NEXT: fmov x8, d0
-; NEON-NEXT: shl v2.2d, v2.2d, #63
-; NEON-NEXT: cmlt v2.2d, v2.2d, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.2d, v1.2d, v2.2d
-; NEON-NEXT: fmov x9, d1
-; NEON-NEXT: mov x10, v1.d[1]
-; NEON-NEXT: sdiv x8, x8, x9
-; NEON-NEXT: mov x9, v0.d[1]
-; NEON-NEXT: sdiv x9, x9, x10
+; NEON-NEXT: fmov x8, d1
+; NEON-NEXT: fmov x9, d0
+; NEON-NEXT: mov x10, v0.d[1]
+; NEON-NEXT: sdiv x8, x9, x8
+; NEON-NEXT: mov x9, v1.d[1]
+; NEON-NEXT: sdiv x9, x10, x9
; NEON-NEXT: fmov d0, x8
; NEON-NEXT: mov v0.d[1], x9
; NEON-NEXT: ret
@@ -83,30 +71,17 @@ define <2 x i64> @sdiv_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
define <4 x i64> @sdiv_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; NEON-LABEL: sdiv_v4i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v4.4s, v4.4h, #0
-; NEON-NEXT: mov x8, v1.d[1]
+; NEON-NEXT: mov x8, v3.d[1]
+; NEON-NEXT: mov x9, v1.d[1]
+; NEON-NEXT: fmov x10, d1
; NEON-NEXT: fmov x11, d0
; NEON-NEXT: mov x12, v0.d[1]
-; NEON-NEXT: ushll2 v5.2d, v4.4s, #0
-; NEON-NEXT: shl v5.2d, v5.2d, #63
-; NEON-NEXT: cmlt v5.2d, v5.2d, #0
-; NEON-NEXT: and v3.16b, v3.16b, v5.16b
-; NEON-NEXT: mvn v5.16b, v5.16b
-; NEON-NEXT: sub v3.2d, v3.2d, v5.2d
-; NEON-NEXT: mov x9, v3.d[1]
-; NEON-NEXT: fmov x10, d3
-; NEON-NEXT: sdiv x8, x8, x9
-; NEON-NEXT: fmov x9, d1
-; NEON-NEXT: ushll v1.2d, v4.2s, #0
-; NEON-NEXT: shl v1.2d, v1.2d, #63
-; NEON-NEXT: cmlt v1.2d, v1.2d, #0
-; NEON-NEXT: and v2.16b, v2.16b, v1.16b
-; NEON-NEXT: mvn v1.16b, v1.16b
-; NEON-NEXT: sub v1.2d, v2.2d, v1.2d
-; NEON-NEXT: sdiv x9, x9, x10
-; NEON-NEXT: fmov x10, d1
+; NEON-NEXT: sdiv x8, x9, x8
+; NEON-NEXT: fmov x9, d3
+; NEON-NEXT: sdiv x9, x10, x9
+; NEON-NEXT: fmov x10, d2
; NEON-NEXT: sdiv x10, x11, x10
-; NEON-NEXT: mov x11, v1.d[1]
+; NEON-NEXT: mov x11, v2.d[1]
; NEON-NEXT: fmov d1, x9
; NEON-NEXT: mov v1.d[1], x8
; NEON-NEXT: sdiv x11, x12, x11
@@ -141,18 +116,14 @@ define <4 x i64> @sdiv_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
define <2 x i32> @sdiv_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; NEON-LABEL: sdiv_v2i32:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.2s, v2.2s, #31
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: fmov w8, s0
-; NEON-NEXT: cmlt v2.2s, v2.2s, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.2s, v1.2s, v2.2s
-; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w10, v1.s[1]
-; NEON-NEXT: sdiv w8, w8, w9
-; NEON-NEXT: mov w9, v0.s[1]
-; NEON-NEXT: sdiv w9, w9, w10
+; NEON-NEXT: fmov w8, s1
+; NEON-NEXT: fmov w9, s0
+; NEON-NEXT: mov w10, v0.s[1]
+; NEON-NEXT: sdiv w8, w9, w8
+; NEON-NEXT: mov w9, v1.s[1]
+; NEON-NEXT: sdiv w9, w10, w9
; NEON-NEXT: fmov s0, w8
; NEON-NEXT: mov v0.s[1], w9
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
@@ -176,26 +147,22 @@ define <2 x i32> @sdiv_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
define <4 x i16> @sdiv_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; NEON-LABEL: sdiv_v4i16:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.4h, v2.4h, #15
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: smov w8, v0.h[1]
-; NEON-NEXT: cmlt v2.4h, v2.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.4h, v1.4h, v2.4h
-; NEON-NEXT: smov w9, v1.h[1]
-; NEON-NEXT: smov w10, v1.h[0]
-; NEON-NEXT: smov w11, v1.h[2]
-; NEON-NEXT: smov w12, v1.h[3]
-; NEON-NEXT: sdiv w8, w8, w9
-; NEON-NEXT: smov w9, v0.h[0]
-; NEON-NEXT: sdiv w9, w9, w10
-; NEON-NEXT: smov w10, v0.h[2]
-; NEON-NEXT: sdiv w10, w10, w11
-; NEON-NEXT: smov w11, v0.h[3]
+; NEON-NEXT: smov w8, v1.h[1]
+; NEON-NEXT: smov w9, v0.h[1]
+; NEON-NEXT: smov w10, v0.h[0]
+; NEON-NEXT: smov w11, v0.h[2]
+; NEON-NEXT: smov w12, v0.h[3]
+; NEON-NEXT: sdiv w8, w9, w8
+; NEON-NEXT: smov w9, v1.h[0]
+; NEON-NEXT: sdiv w9, w10, w9
+; NEON-NEXT: smov w10, v1.h[2]
+; NEON-NEXT: sdiv w10, w11, w10
+; NEON-NEXT: smov w11, v1.h[3]
; NEON-NEXT: fmov s0, w9
; NEON-NEXT: mov v0.h[1], w8
-; NEON-NEXT: sdiv w8, w11, w12
+; NEON-NEXT: sdiv w8, w12, w11
; NEON-NEXT: mov v0.h[2], w10
; NEON-NEXT: mov v0.h[3], w8
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
@@ -223,11 +190,9 @@ define <1 x i64> @sdiv_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; NEON-LABEL: sdiv_v1i164:
; NEON: // %bb.0:
; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
-; NEON-NEXT: fmov x8, d1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
+; NEON-NEXT: fmov x8, d1
; NEON-NEXT: fmov x9, d0
-; NEON-NEXT: tst w0, #0x1
-; NEON-NEXT: csinc x8, x8, xzr, ne
; NEON-NEXT: sdiv x8, x9, x8
; NEON-NEXT: fmov d0, x8
; NEON-NEXT: ret
@@ -235,11 +200,9 @@ define <1 x i64> @sdiv_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; SVE-LABEL: sdiv_v1i164:
; SVE: // %bb.0:
; SVE-NEXT: // kill: def $d1 killed $d1 def $q1
-; SVE-NEXT: fmov x8, d1
; SVE-NEXT: // kill: def $d0 killed $d0 def $q0
+; SVE-NEXT: fmov x8, d1
; SVE-NEXT: fmov x9, d0
-; SVE-NEXT: tst w0, #0x1
-; SVE-NEXT: csinc x8, x8, xzr, ne
; SVE-NEXT: sdiv x8, x9, x8
; SVE-NEXT: fmov d0, x8
; SVE-NEXT: ret
@@ -326,35 +289,25 @@ define <2 x i128> @sdiv_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @sdiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; NEON-LABEL: sdiv_v3i10:
; NEON: // %bb.0:
-; NEON-NEXT: movi v0.2d, #0000000000000000
+; NEON-NEXT: fmov s0, w0
; NEON-NEXT: fmov s1, w3
-; NEON-NEXT: ldr w8, [sp]
-; NEON-NEXT: fmov s2, w0
+; NEON-NEXT: mov v0.h[1], w1
; NEON-NEXT: mov v1.h[1], w4
-; NEON-NEXT: mov v0.h[0], w6
-; NEON-NEXT: mov v2.h[1], w1
+; NEON-NEXT: mov v0.h[2], w2
; NEON-NEXT: mov v1.h[2], w5
-; NEON-NEXT: mov v0.h[1], w7
-; NEON-NEXT: mov v2.h[2], w2
+; NEON-NEXT: shl v0.4h, v0.4h, #6
; NEON-NEXT: shl v1.4h, v1.4h, #6
-; NEON-NEXT: mov v0.h[2], w8
-; NEON-NEXT: shl v2.4h, v2.4h, #6
+; NEON-NEXT: sshr v0.4h, v0.4h, #6
; NEON-NEXT: sshr v1.4h, v1.4h, #6
-; NEON-NEXT: sshr v2.4h, v2.4h, #6
-; NEON-NEXT: shl v0.4h, v0.4h, #15
-; NEON-NEXT: smov w8, v2.h[0]
-; NEON-NEXT: cmlt v0.4h, v0.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v0.8b
-; NEON-NEXT: mvn v0.8b, v0.8b
-; NEON-NEXT: sub v0.4h, v1.4h, v0.4h
+; NEON-NEXT: smov w8, v1.h[0]
; NEON-NEXT: smov w9, v0.h[0]
-; NEON-NEXT: sdiv w0, w8, w9
-; NEON-NEXT: smov w8, v2.h[1]
+; NEON-NEXT: sdiv w0, w9, w8
+; NEON-NEXT: smov w8, v1.h[1]
; NEON-NEXT: smov w9, v0.h[1]
-; NEON-NEXT: sdiv w1, w8, w9
-; NEON-NEXT: smov w8, v2.h[2]
+; NEON-NEXT: sdiv w1, w9, w8
+; NEON-NEXT: smov w8, v1.h[2]
; NEON-NEXT: smov w9, v0.h[2]
-; NEON-NEXT: sdiv w2, w8, w9
+; NEON-NEXT: sdiv w2, w9, w8
; NEON-NEXT: ret
;
; SVE-LABEL: sdiv_v3i10:
diff --git a/llvm/test/CodeGen/AArch64/masked-srem-fixed-length.ll b/llvm/test/CodeGen/AArch64/masked-srem-fixed-length.ll
index 44b746d06f7e39..09ad49e74fba37 100644
--- a/llvm/test/CodeGen/AArch64/masked-srem-fixed-length.ll
+++ b/llvm/test/CodeGen/AArch64/masked-srem-fixed-length.ll
@@ -6,31 +6,25 @@
define <4 x i32> @srem_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; NEON-LABEL: srem_v4i32:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.4s, v2.4h, #0
-; NEON-NEXT: fmov w8, s0
-; NEON-NEXT: mov w11, v0.s[1]
-; NEON-NEXT: mov w14, v0.s[2]
-; NEON-NEXT: mov w18, v0.s[3]
-; NEON-NEXT: shl v2.4s, v2.4s, #31
-; NEON-NEXT: cmlt v2.4s, v2.4s, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.4s, v1.4s, v2.4s
-; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w12, v1.s[1]
-; NEON-NEXT: mov w15, v1.s[2]
+; NEON-NEXT: fmov w11, s1
+; NEON-NEXT: fmov w12, s0
+; NEON-NEXT: mov w8, v1.s[1]
+; NEON-NEXT: mov w9, v0.s[1]
+; NEON-NEXT: mov w14, v1.s[2]
+; NEON-NEXT: mov w15, v0.s[2]
; NEON-NEXT: mov w17, v1.s[3]
-; NEON-NEXT: sdiv w10, w8, w9
-; NEON-NEXT: sdiv w13, w11, w12
-; NEON-NEXT: msub w8, w10, w9, w8
-; NEON-NEXT: fmov s0, w8
-; NEON-NEXT: sdiv w16, w14, w15
-; NEON-NEXT: msub w9, w13, w12, w11
-; NEON-NEXT: mov v0.s[1], w9
-; NEON-NEXT: sdiv w10, w18, w17
-; NEON-NEXT: msub w8, w16, w15, w14
+; NEON-NEXT: mov w18, v0.s[3]
+; NEON-NEXT: sdiv w13, w12, w11
+; NEON-NEXT: sdiv w10, w9, w8
+; NEON-NEXT: msub w11, w13, w11, w12
+; NEON-NEXT: fmov s0, w11
+; NEON-NEXT: sdiv w16, w15, w14
+; NEON-NEXT: msub w8, w10, w8, w9
+; NEON-NEXT: mov v0.s[1], w8
+; NEON-NEXT: sdiv w9, w18, w17
+; NEON-NEXT: msub w8, w16, w14, w15
; NEON-NEXT: mov v0.s[2], w8
-; NEON-NEXT: msub w8, w10, w17, w18
+; NEON-NEXT: msub w8, w9, w17, w18
; NEON-NEXT: mov v0.s[3], w8
; NEON-NEXT: ret
;
@@ -56,21 +50,15 @@ define <4 x i32> @srem_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
define <2 x i64> @srem_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; NEON-LABEL: srem_v2i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.2d, v2.2s, #0
-; NEON-NEXT: fmov x8, d0
-; NEON-NEXT: mov x11, v0.d[1]
-; NEON-NEXT: shl v2.2d, v2.2d, #63
-; NEON-NEXT: cmlt v2.2d, v2.2d, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.2d, v1.2d, v2.2d
-; NEON-NEXT: fmov x9, d1
-; NEON-NEXT: mov x12, v1.d[1]
-; NEON-NEXT: sdiv x10, x8, x9
-; NEON-NEXT: sdiv x13, x11, x12
-; NEON-NEXT: msub x8, x10, x9, x8
+; NEON-NEXT: fmov x8, d1
+; NEON-NEXT: fmov x9, d0
+; NEON-NEXT: mov x11, v1.d[1]
+; NEON-NEXT: mov x12, v0.d[1]
+; NEON-NEXT: sdiv x10, x9, x8
+; NEON-NEXT: sdiv x13, x12, x11
+; NEON-NEXT: msub x8, x10, x8, x9
; NEON-NEXT: fmov d0, x8
-; NEON-NEXT: msub x9, x13, x12, x11
+; NEON-NEXT: msub x9, x13, x11, x12
; NEON-NEXT: mov v0.d[1], x9
; NEON-NEXT: ret
;
@@ -97,32 +85,19 @@ define <2 x i64> @srem_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
define <4 x i64> @srem_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; NEON-LABEL: srem_v4i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v4.4s, v4.4h, #0
-; NEON-NEXT: mov x8, v1.d[1]
-; NEON-NEXT: fmov x11, d1
+; NEON-NEXT: mov x8, v3.d[1]
+; NEON-NEXT: mov x9, v1.d[1]
+; NEON-NEXT: fmov x11, d3
+; NEON-NEXT: fmov x12, d1
+; NEON-NEXT: fmov x14, d2
; NEON-NEXT: fmov x15, d0
+; NEON-NEXT: mov x17, v2.d[1]
; NEON-NEXT: mov x18, v0.d[1]
-; NEON-NEXT: ushll2 v5.2d, v4.4s, #0
-; NEON-NEXT: ushll v1.2d, v4.2s, #0
-; NEON-NEXT: shl v5.2d, v5.2d, #63
-; NEON-NEXT: shl v1.2d, v1.2d, #63
-; NEON-NEXT: cmlt v5.2d, v5.2d, #0
-; NEON-NEXT: cmlt v1.2d, v1.2d, #0
-; NEON-NEXT: and v3.16b, v3.16b, v5.16b
-; NEON-NEXT: mvn v5.16b, v5.16b
-; NEON-NEXT: and v2.16b, v2.16b, v1.16b
-; NEON-NEXT: mvn v1.16b, v1.16b
-; NEON-NEXT: sub v3.2d, v3.2d, v5.2d
-; NEON-NEXT: sub v1.2d, v2.2d, v1.2d
-; NEON-NEXT: mov x9, v3.d[1]
-; NEON-NEXT: fmov x12, d3
-; NEON-NEXT: fmov x14, d1
-; NEON-NEXT: mov x17, v1.d[1]
-; NEON-NEXT: sdiv x13, x11, x12
-; NEON-NEXT: sdiv x10, x8, x9
+; NEON-NEXT: sdiv x10, x9, x8
+; NEON-NEXT: sdiv x13, x12, x11
+; NEON-NEXT: msub x8, x10, x8, x9
; NEON-NEXT: sdiv x16, x15, x14
-; NEON-NEXT: msub x8, x10, x9, x8
-; NEON-NEXT: msub x9, x13, x12, x11
+; NEON-NEXT: msub x9, x13, x11, x12
; NEON-NEXT: fmov d1, x9
; NEON-NEXT: mov v1.d[1], x8
; NEON-NEXT: sdiv x0, x18, x17
@@ -167,21 +142,17 @@ define <4 x i64> @srem_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
define <2 x i32> @srem_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; NEON-LABEL: srem_v2i32:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.2s, v2.2s, #31
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: fmov w8, s0
-; NEON-NEXT: mov w11, v0.s[1]
-; NEON-NEXT: cmlt v2.2s, v2.2s, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.2s, v1.2s, v2.2s
-; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w12, v1.s[1]
-; NEON-NEXT: sdiv w10, w8, w9
-; NEON-NEXT: sdiv w13, w11, w12
-; NEON-NEXT: msub w8, w10, w9, w8
+; NEON-NEXT: fmov w8, s1
+; NEON-NEXT: fmov w9, s0
+; NEON-NEXT: mov w11, v1.s[1]
+; NEON-NEXT: mov w12, v0.s[1]
+; NEON-NEXT: sdiv w10, w9, w8
+; NEON-NEXT: sdiv w13, w12, w11
+; NEON-NEXT: msub w8, w10, w8, w9
; NEON-NEXT: fmov s0, w8
-; NEON-NEXT: msub w9, w13, w12, w11
+; NEON-NEXT: msub w9, w13, w11, w12
; NEON-NEXT: mov v0.s[1], w9
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
; NEON-NEXT: ret
@@ -208,31 +179,27 @@ define <2 x i32> @srem_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
define <4 x i16> @srem_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; NEON-LABEL: srem_v4i16:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.4h, v2.4h, #15
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: smov w11, v0.h[0]
-; NEON-NEXT: smov w8, v0.h[1]
-; NEON-NEXT: smov w14, v0.h[2]
-; NEON-NEXT: smov w17, v0.h[3]
-; NEON-NEXT: cmlt v2.4h, v2.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.4h, v1.4h, v2.4h
-; NEON-NEXT: smov w12, v1.h[0]
-; NEON-NEXT: smov w9, v1.h[1]
-; NEON-NEXT: smov w15, v1.h[2]
-; NEON-NEXT: smov w18, v1.h[3]
-; NEON-NEXT: sdiv w13, w11, w12
-; NEON-NEXT: sdiv w10, w8, w9
-; NEON-NEXT: msub w11, w13, w12, w11
+; NEON-NEXT: smov w11, v1.h[0]
+; NEON-NEXT: smov w12, v0.h[0]
+; NEON-NEXT: smov w8, v1.h[1]
+; NEON-NEXT: smov w9, v0.h[1]
+; NEON-NEXT: smov w14, v1.h[2]
+; NEON-NEXT: smov w15, v0.h[2]
+; NEON-NEXT: smov w17, v1.h[3]
+; NEON-NEXT: smov w18, v0.h[3]
+; NEON-NEXT: sdiv w13, w12, w11
+; NEON-NEXT: sdiv w10, w9, w8
+; NEON-NEXT: msub w11, w13, w11, w12
; NEON-NEXT: fmov s0, w11
-; NEON-NEXT: sdiv w16, w14, w15
-; NEON-NEXT: msub w8, w10, w9, w8
+; NEON-NEXT: sdiv w16, w15, w14
+; NEON-NEXT: msub w8, w10, w8, w9
; NEON-NEXT: mov v0.h[1], w8
-; NEON-NEXT: sdiv w9, w17, w18
-; NEON-NEXT: msub w8, w16, w15, w14
+; NEON-NEXT: sdiv w9, w18, w17
+; NEON-NEXT: msub w8, w16, w14, w15
; NEON-NEXT: mov v0.h[2], w8
-; NEON-NEXT: msub w8, w9, w18, w17
+; NEON-NEXT: msub w8, w9, w17, w18
; NEON-NEXT: mov v0.h[3], w8
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
; NEON-NEXT: ret
@@ -260,11 +227,9 @@ define <1 x i64> @srem_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; NEON-LABEL: srem_v1i164:
; NEON: // %bb.0:
; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
-; NEON-NEXT: fmov x8, d1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
+; NEON-NEXT: fmov x8, d1
; NEON-NEXT: fmov x9, d0
-; NEON-NEXT: tst w0, #0x1
-; NEON-NEXT: csinc x8, x8, xzr, ne
; NEON-NEXT: sdiv x10, x9, x8
; NEON-NEXT: msub x8, x10, x8, x9
; NEON-NEXT: fmov d0, x8
@@ -273,11 +238,9 @@ define <1 x i64> @srem_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; SVE-LABEL: srem_v1i164:
; SVE: // %bb.0:
; SVE-NEXT: // kill: def $d1 killed $d1 def $q1
-; SVE-NEXT: fmov x8, d1
; SVE-NEXT: // kill: def $d0 killed $d0 def $q0
+; SVE-NEXT: fmov x8, d1
; SVE-NEXT: fmov x9, d0
-; SVE-NEXT: tst w0, #0x1
-; SVE-NEXT: csinc x8, x8, xzr, ne
; SVE-NEXT: sdiv x10, x9, x8
; SVE-NEXT: msub x8, x10, x8, x9
; SVE-NEXT: fmov d0, x8
@@ -365,38 +328,28 @@ define <2 x i128> @srem_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @srem_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; NEON-LABEL: srem_v3i10:
; NEON: // %bb.0:
-; NEON-NEXT: movi v0.2d, #0000000000000000
+; NEON-NEXT: fmov s0, w0
; NEON-NEXT: fmov s1, w3
-; NEON-NEXT: ldr w8, [sp]
-; NEON-NEXT: fmov s2, w0
+; NEON-NEXT: mov v0.h[1], w1
; NEON-NEXT: mov v1.h[1], w4
-; NEON-NEXT: mov v0.h[0], w6
-; NEON-NEXT: mov v2.h[1], w1
+; NEON-NEXT: mov v0.h[2], w2
; NEON-NEXT: mov v1.h[2], w5
-; NEON-NEXT: mov v0.h[1], w7
-; NEON-NEXT: mov v2.h[2], w2
+; NEON-NEXT: shl v0.4h, v0.4h, #6
; NEON-NEXT: shl v1.4h, v1.4h, #6
-; NEON-NEXT: mov v0.h[2], w8
-; NEON-NEXT: shl v2.4h, v2.4h, #6
+; NEON-NEXT: sshr v0.4h, v0.4h, #6
; NEON-NEXT: sshr v1.4h, v1.4h, #6
-; NEON-NEXT: sshr v2.4h, v2.4h, #6
-; NEON-NEXT: shl v0.4h, v0.4h, #15
-; NEON-NEXT: smov w8, v2.h[0]
-; NEON-NEXT: smov w11, v2.h[1]
-; NEON-NEXT: smov w14, v2.h[2]
-; NEON-NEXT: cmlt v0.4h, v0.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v0.8b
-; NEON-NEXT: mvn v0.8b, v0.8b
-; NEON-NEXT: sub v0.4h, v1.4h, v0.4h
+; NEON-NEXT: smov w8, v1.h[0]
; NEON-NEXT: smov w9, v0.h[0]
+; NEON-NEXT: smov w11, v1.h[1]
; NEON-NEXT: smov w12, v0.h[1]
+; NEON-NEXT: smov w14, v1.h[2]
; NEON-NEXT: smov w15, v0.h[2]
-; NEON-NEXT: sdiv w10, w8, w9
-; NEON-NEXT: sdiv w13, w11, w12
-; NEON-NEXT: msub w0, w10, w9, w8
-; NEON-NEXT: sdiv w16, w14, w15
-; NEON-NEXT: msub w1, w13, w12, w11
-; NEON-NEXT: msub w2, w16, w15, w14
+; NEON-NEXT: sdiv w10, w9, w8
+; NEON-NEXT: sdiv w13, w12, w11
+; NEON-NEXT: msub w0, w10, w8, w9
+; NEON-NEXT: sdiv w16, w15, w14
+; NEON-NEXT: msub w1, w13, w11, w12
+; NEON-NEXT: msub w2, w16, w14, w15
; NEON-NEXT: ret
;
; SVE-LABEL: srem_v3i10:
diff --git a/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll b/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll
index f732e0da84f2f1..30dbb3c4713d5e 100644
--- a/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll
+++ b/llvm/test/CodeGen/AArch64/masked-udiv-fixed-length.ll
@@ -6,25 +6,19 @@
define <4 x i32> @udiv_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; NEON-LABEL: udiv_v4i32:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.4s, v2.4h, #0
-; NEON-NEXT: fmov w8, s0
+; NEON-NEXT: mov w8, v1.s[1]
+; NEON-NEXT: mov w9, v0.s[1]
+; NEON-NEXT: fmov w10, s0
+; NEON-NEXT: mov w11, v0.s[2]
; NEON-NEXT: mov w12, v0.s[3]
-; NEON-NEXT: shl v2.4s, v2.4s, #31
-; NEON-NEXT: cmlt v2.4s, v2.4s, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.4s, v1.4s, v2.4s
+; NEON-NEXT: udiv w8, w9, w8
; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w10, v1.s[1]
-; NEON-NEXT: mov w11, v1.s[2]
-; NEON-NEXT: udiv w8, w8, w9
-; NEON-NEXT: mov w9, v0.s[1]
-; NEON-NEXT: udiv w9, w9, w10
-; NEON-NEXT: mov w10, v0.s[2]
-; NEON-NEXT: fmov s0, w8
-; NEON-NEXT: udiv w10, w10, w11
+; NEON-NEXT: udiv w9, w10, w9
+; NEON-NEXT: mov w10, v1.s[2]
+; NEON-NEXT: udiv w10, w11, w10
; NEON-NEXT: mov w11, v1.s[3]
-; NEON-NEXT: mov v0.s[1], w9
+; NEON-NEXT: fmov s0, w9
+; NEON-NEXT: mov v0.s[1], w8
; NEON-NEXT: udiv w8, w12, w11
; NEON-NEXT: mov v0.s[2], w10
; NEON-NEXT: mov v0.s[3], w8
@@ -48,18 +42,12 @@ define <4 x i32> @udiv_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
define <2 x i64> @udiv_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; NEON-LABEL: udiv_v2i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.2d, v2.2s, #0
-; NEON-NEXT: fmov x8, d0
-; NEON-NEXT: shl v2.2d, v2.2d, #63
-; NEON-NEXT: cmlt v2.2d, v2.2d, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.2d, v1.2d, v2.2d
-; NEON-NEXT: fmov x9, d1
-; NEON-NEXT: mov x10, v1.d[1]
-; NEON-NEXT: udiv x8, x8, x9
-; NEON-NEXT: mov x9, v0.d[1]
-; NEON-NEXT: udiv x9, x9, x10
+; NEON-NEXT: fmov x8, d1
+; NEON-NEXT: fmov x9, d0
+; NEON-NEXT: mov x10, v0.d[1]
+; NEON-NEXT: udiv x8, x9, x8
+; NEON-NEXT: mov x9, v1.d[1]
+; NEON-NEXT: udiv x9, x10, x9
; NEON-NEXT: fmov d0, x8
; NEON-NEXT: mov v0.d[1], x9
; NEON-NEXT: ret
@@ -83,30 +71,17 @@ define <2 x i64> @udiv_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
define <4 x i64> @udiv_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; NEON-LABEL: udiv_v4i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v4.4s, v4.4h, #0
-; NEON-NEXT: mov x8, v1.d[1]
+; NEON-NEXT: mov x8, v3.d[1]
+; NEON-NEXT: mov x9, v1.d[1]
+; NEON-NEXT: fmov x10, d1
; NEON-NEXT: fmov x11, d0
; NEON-NEXT: mov x12, v0.d[1]
-; NEON-NEXT: ushll2 v5.2d, v4.4s, #0
-; NEON-NEXT: shl v5.2d, v5.2d, #63
-; NEON-NEXT: cmlt v5.2d, v5.2d, #0
-; NEON-NEXT: and v3.16b, v3.16b, v5.16b
-; NEON-NEXT: mvn v5.16b, v5.16b
-; NEON-NEXT: sub v3.2d, v3.2d, v5.2d
-; NEON-NEXT: mov x9, v3.d[1]
-; NEON-NEXT: fmov x10, d3
-; NEON-NEXT: udiv x8, x8, x9
-; NEON-NEXT: fmov x9, d1
-; NEON-NEXT: ushll v1.2d, v4.2s, #0
-; NEON-NEXT: shl v1.2d, v1.2d, #63
-; NEON-NEXT: cmlt v1.2d, v1.2d, #0
-; NEON-NEXT: and v2.16b, v2.16b, v1.16b
-; NEON-NEXT: mvn v1.16b, v1.16b
-; NEON-NEXT: sub v1.2d, v2.2d, v1.2d
-; NEON-NEXT: udiv x9, x9, x10
-; NEON-NEXT: fmov x10, d1
+; NEON-NEXT: udiv x8, x9, x8
+; NEON-NEXT: fmov x9, d3
+; NEON-NEXT: udiv x9, x10, x9
+; NEON-NEXT: fmov x10, d2
; NEON-NEXT: udiv x10, x11, x10
-; NEON-NEXT: mov x11, v1.d[1]
+; NEON-NEXT: mov x11, v2.d[1]
; NEON-NEXT: fmov d1, x9
; NEON-NEXT: mov v1.d[1], x8
; NEON-NEXT: udiv x11, x12, x11
@@ -141,18 +116,14 @@ define <4 x i64> @udiv_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
define <2 x i32> @udiv_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; NEON-LABEL: udiv_v2i32:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.2s, v2.2s, #31
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: fmov w8, s0
-; NEON-NEXT: cmlt v2.2s, v2.2s, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.2s, v1.2s, v2.2s
-; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w10, v1.s[1]
-; NEON-NEXT: udiv w8, w8, w9
-; NEON-NEXT: mov w9, v0.s[1]
-; NEON-NEXT: udiv w9, w9, w10
+; NEON-NEXT: fmov w8, s1
+; NEON-NEXT: fmov w9, s0
+; NEON-NEXT: mov w10, v0.s[1]
+; NEON-NEXT: udiv w8, w9, w8
+; NEON-NEXT: mov w9, v1.s[1]
+; NEON-NEXT: udiv w9, w10, w9
; NEON-NEXT: fmov s0, w8
; NEON-NEXT: mov v0.s[1], w9
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
@@ -176,26 +147,22 @@ define <2 x i32> @udiv_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
define <4 x i16> @udiv_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; NEON-LABEL: udiv_v4i16:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.4h, v2.4h, #15
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: umov w8, v0.h[1]
-; NEON-NEXT: cmlt v2.4h, v2.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.4h, v1.4h, v2.4h
-; NEON-NEXT: umov w9, v1.h[1]
-; NEON-NEXT: umov w10, v1.h[0]
-; NEON-NEXT: umov w11, v1.h[2]
-; NEON-NEXT: umov w12, v1.h[3]
-; NEON-NEXT: udiv w8, w8, w9
-; NEON-NEXT: umov w9, v0.h[0]
-; NEON-NEXT: udiv w9, w9, w10
-; NEON-NEXT: umov w10, v0.h[2]
-; NEON-NEXT: udiv w10, w10, w11
-; NEON-NEXT: umov w11, v0.h[3]
+; NEON-NEXT: umov w8, v1.h[1]
+; NEON-NEXT: umov w9, v0.h[1]
+; NEON-NEXT: umov w10, v0.h[0]
+; NEON-NEXT: umov w11, v0.h[2]
+; NEON-NEXT: umov w12, v0.h[3]
+; NEON-NEXT: udiv w8, w9, w8
+; NEON-NEXT: umov w9, v1.h[0]
+; NEON-NEXT: udiv w9, w10, w9
+; NEON-NEXT: umov w10, v1.h[2]
+; NEON-NEXT: udiv w10, w11, w10
+; NEON-NEXT: umov w11, v1.h[3]
; NEON-NEXT: fmov s0, w9
; NEON-NEXT: mov v0.h[1], w8
-; NEON-NEXT: udiv w8, w11, w12
+; NEON-NEXT: udiv w8, w12, w11
; NEON-NEXT: mov v0.h[2], w10
; NEON-NEXT: mov v0.h[3], w8
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
@@ -223,11 +190,9 @@ define <1 x i64> @udiv_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; NEON-LABEL: udiv_v1i164:
; NEON: // %bb.0:
; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
-; NEON-NEXT: fmov x8, d1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
+; NEON-NEXT: fmov x8, d1
; NEON-NEXT: fmov x9, d0
-; NEON-NEXT: tst w0, #0x1
-; NEON-NEXT: csinc x8, x8, xzr, ne
; NEON-NEXT: udiv x8, x9, x8
; NEON-NEXT: fmov d0, x8
; NEON-NEXT: ret
@@ -235,11 +200,9 @@ define <1 x i64> @udiv_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; SVE-LABEL: udiv_v1i164:
; SVE: // %bb.0:
; SVE-NEXT: // kill: def $d1 killed $d1 def $q1
-; SVE-NEXT: fmov x8, d1
; SVE-NEXT: // kill: def $d0 killed $d0 def $q0
+; SVE-NEXT: fmov x8, d1
; SVE-NEXT: fmov x9, d0
-; SVE-NEXT: tst w0, #0x1
-; SVE-NEXT: csinc x8, x8, xzr, ne
; SVE-NEXT: udiv x8, x9, x8
; SVE-NEXT: fmov d0, x8
; SVE-NEXT: ret
@@ -326,35 +289,22 @@ define <2 x i128> @udiv_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @udiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; NEON-LABEL: udiv_v3i10:
; NEON: // %bb.0:
-; NEON-NEXT: movi v0.2d, #0000000000000000
+; NEON-NEXT: fmov s0, w0
; NEON-NEXT: fmov s1, w3
-; NEON-NEXT: ldr w8, [sp]
-; NEON-NEXT: fmov s2, w0
+; NEON-NEXT: mov v0.h[1], w1
; NEON-NEXT: mov v1.h[1], w4
-; NEON-NEXT: mov v0.h[0], w6
-; NEON-NEXT: mov v2.h[1], w1
+; NEON-NEXT: mov v0.h[2], w2
; NEON-NEXT: mov v1.h[2], w5
-; NEON-NEXT: mov v0.h[1], w7
-; NEON-NEXT: mov v2.h[2], w2
+; NEON-NEXT: bic v0.4h, #252, lsl #8
; NEON-NEXT: bic v1.4h, #252, lsl #8
-; NEON-NEXT: mov v0.h[2], w8
-; NEON-NEXT: bic v2.4h, #252, lsl #8
-; NEON-NEXT: umov w9, v2.h[0]
-; NEON-NEXT: shl v0.4h, v0.4h, #15
-; NEON-NEXT: cmlt v0.4h, v0.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v0.8b
-; NEON-NEXT: mvn v0.8b, v0.8b
-; NEON-NEXT: sub v0.4h, v1.4h, v0.4h
-; NEON-NEXT: umov w8, v0.h[0]
-; NEON-NEXT: and w8, w8, #0x3ff
+; NEON-NEXT: umov w8, v1.h[0]
+; NEON-NEXT: umov w9, v0.h[0]
; NEON-NEXT: udiv w0, w9, w8
-; NEON-NEXT: umov w8, v0.h[1]
-; NEON-NEXT: umov w9, v2.h[1]
-; NEON-NEXT: and w8, w8, #0x3ff
+; NEON-NEXT: umov w8, v1.h[1]
+; NEON-NEXT: umov w9, v0.h[1]
; NEON-NEXT: udiv w1, w9, w8
-; NEON-NEXT: umov w8, v0.h[2]
-; NEON-NEXT: umov w9, v2.h[2]
-; NEON-NEXT: and w8, w8, #0x3ff
+; NEON-NEXT: umov w8, v1.h[2]
+; NEON-NEXT: umov w9, v0.h[2]
; NEON-NEXT: udiv w2, w9, w8
; NEON-NEXT: ret
;
diff --git a/llvm/test/CodeGen/AArch64/masked-urem-fixed-length.ll b/llvm/test/CodeGen/AArch64/masked-urem-fixed-length.ll
index 5eaa420588b05b..5fe675ffeb28bb 100644
--- a/llvm/test/CodeGen/AArch64/masked-urem-fixed-length.ll
+++ b/llvm/test/CodeGen/AArch64/masked-urem-fixed-length.ll
@@ -6,31 +6,25 @@
define <4 x i32> @urem_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; NEON-LABEL: urem_v4i32:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.4s, v2.4h, #0
-; NEON-NEXT: fmov w8, s0
-; NEON-NEXT: mov w11, v0.s[1]
-; NEON-NEXT: mov w14, v0.s[2]
-; NEON-NEXT: mov w18, v0.s[3]
-; NEON-NEXT: shl v2.4s, v2.4s, #31
-; NEON-NEXT: cmlt v2.4s, v2.4s, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.4s, v1.4s, v2.4s
-; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w12, v1.s[1]
-; NEON-NEXT: mov w15, v1.s[2]
+; NEON-NEXT: fmov w11, s1
+; NEON-NEXT: fmov w12, s0
+; NEON-NEXT: mov w8, v1.s[1]
+; NEON-NEXT: mov w9, v0.s[1]
+; NEON-NEXT: mov w14, v1.s[2]
+; NEON-NEXT: mov w15, v0.s[2]
; NEON-NEXT: mov w17, v1.s[3]
-; NEON-NEXT: udiv w10, w8, w9
-; NEON-NEXT: udiv w13, w11, w12
-; NEON-NEXT: msub w8, w10, w9, w8
-; NEON-NEXT: fmov s0, w8
-; NEON-NEXT: udiv w16, w14, w15
-; NEON-NEXT: msub w9, w13, w12, w11
-; NEON-NEXT: mov v0.s[1], w9
-; NEON-NEXT: udiv w10, w18, w17
-; NEON-NEXT: msub w8, w16, w15, w14
+; NEON-NEXT: mov w18, v0.s[3]
+; NEON-NEXT: udiv w13, w12, w11
+; NEON-NEXT: udiv w10, w9, w8
+; NEON-NEXT: msub w11, w13, w11, w12
+; NEON-NEXT: fmov s0, w11
+; NEON-NEXT: udiv w16, w15, w14
+; NEON-NEXT: msub w8, w10, w8, w9
+; NEON-NEXT: mov v0.s[1], w8
+; NEON-NEXT: udiv w9, w18, w17
+; NEON-NEXT: msub w8, w16, w14, w15
; NEON-NEXT: mov v0.s[2], w8
-; NEON-NEXT: msub w8, w10, w17, w18
+; NEON-NEXT: msub w8, w9, w17, w18
; NEON-NEXT: mov v0.s[3], w8
; NEON-NEXT: ret
;
@@ -56,21 +50,15 @@ define <4 x i32> @urem_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
define <2 x i64> @urem_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; NEON-LABEL: urem_v2i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v2.2d, v2.2s, #0
-; NEON-NEXT: fmov x8, d0
-; NEON-NEXT: mov x11, v0.d[1]
-; NEON-NEXT: shl v2.2d, v2.2d, #63
-; NEON-NEXT: cmlt v2.2d, v2.2d, #0
-; NEON-NEXT: and v1.16b, v1.16b, v2.16b
-; NEON-NEXT: mvn v2.16b, v2.16b
-; NEON-NEXT: sub v1.2d, v1.2d, v2.2d
-; NEON-NEXT: fmov x9, d1
-; NEON-NEXT: mov x12, v1.d[1]
-; NEON-NEXT: udiv x10, x8, x9
-; NEON-NEXT: udiv x13, x11, x12
-; NEON-NEXT: msub x8, x10, x9, x8
+; NEON-NEXT: fmov x8, d1
+; NEON-NEXT: fmov x9, d0
+; NEON-NEXT: mov x11, v1.d[1]
+; NEON-NEXT: mov x12, v0.d[1]
+; NEON-NEXT: udiv x10, x9, x8
+; NEON-NEXT: udiv x13, x12, x11
+; NEON-NEXT: msub x8, x10, x8, x9
; NEON-NEXT: fmov d0, x8
-; NEON-NEXT: msub x9, x13, x12, x11
+; NEON-NEXT: msub x9, x13, x11, x12
; NEON-NEXT: mov v0.d[1], x9
; NEON-NEXT: ret
;
@@ -97,32 +85,19 @@ define <2 x i64> @urem_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
define <4 x i64> @urem_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; NEON-LABEL: urem_v4i64:
; NEON: // %bb.0:
-; NEON-NEXT: ushll v4.4s, v4.4h, #0
-; NEON-NEXT: mov x8, v1.d[1]
-; NEON-NEXT: fmov x11, d1
+; NEON-NEXT: mov x8, v3.d[1]
+; NEON-NEXT: mov x9, v1.d[1]
+; NEON-NEXT: fmov x11, d3
+; NEON-NEXT: fmov x12, d1
+; NEON-NEXT: fmov x14, d2
; NEON-NEXT: fmov x15, d0
+; NEON-NEXT: mov x17, v2.d[1]
; NEON-NEXT: mov x18, v0.d[1]
-; NEON-NEXT: ushll2 v5.2d, v4.4s, #0
-; NEON-NEXT: ushll v1.2d, v4.2s, #0
-; NEON-NEXT: shl v5.2d, v5.2d, #63
-; NEON-NEXT: shl v1.2d, v1.2d, #63
-; NEON-NEXT: cmlt v5.2d, v5.2d, #0
-; NEON-NEXT: cmlt v1.2d, v1.2d, #0
-; NEON-NEXT: and v3.16b, v3.16b, v5.16b
-; NEON-NEXT: mvn v5.16b, v5.16b
-; NEON-NEXT: and v2.16b, v2.16b, v1.16b
-; NEON-NEXT: mvn v1.16b, v1.16b
-; NEON-NEXT: sub v3.2d, v3.2d, v5.2d
-; NEON-NEXT: sub v1.2d, v2.2d, v1.2d
-; NEON-NEXT: mov x9, v3.d[1]
-; NEON-NEXT: fmov x12, d3
-; NEON-NEXT: fmov x14, d1
-; NEON-NEXT: mov x17, v1.d[1]
-; NEON-NEXT: udiv x13, x11, x12
-; NEON-NEXT: udiv x10, x8, x9
+; NEON-NEXT: udiv x10, x9, x8
+; NEON-NEXT: udiv x13, x12, x11
+; NEON-NEXT: msub x8, x10, x8, x9
; NEON-NEXT: udiv x16, x15, x14
-; NEON-NEXT: msub x8, x10, x9, x8
-; NEON-NEXT: msub x9, x13, x12, x11
+; NEON-NEXT: msub x9, x13, x11, x12
; NEON-NEXT: fmov d1, x9
; NEON-NEXT: mov v1.d[1], x8
; NEON-NEXT: udiv x0, x18, x17
@@ -167,21 +142,17 @@ define <4 x i64> @urem_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
define <2 x i32> @urem_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; NEON-LABEL: urem_v2i32:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.2s, v2.2s, #31
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: fmov w8, s0
-; NEON-NEXT: mov w11, v0.s[1]
-; NEON-NEXT: cmlt v2.2s, v2.2s, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.2s, v1.2s, v2.2s
-; NEON-NEXT: fmov w9, s1
-; NEON-NEXT: mov w12, v1.s[1]
-; NEON-NEXT: udiv w10, w8, w9
-; NEON-NEXT: udiv w13, w11, w12
-; NEON-NEXT: msub w8, w10, w9, w8
+; NEON-NEXT: fmov w8, s1
+; NEON-NEXT: fmov w9, s0
+; NEON-NEXT: mov w11, v1.s[1]
+; NEON-NEXT: mov w12, v0.s[1]
+; NEON-NEXT: udiv w10, w9, w8
+; NEON-NEXT: udiv w13, w12, w11
+; NEON-NEXT: msub w8, w10, w8, w9
; NEON-NEXT: fmov s0, w8
-; NEON-NEXT: msub w9, w13, w12, w11
+; NEON-NEXT: msub w9, w13, w11, w12
; NEON-NEXT: mov v0.s[1], w9
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
; NEON-NEXT: ret
@@ -208,31 +179,27 @@ define <2 x i32> @urem_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
define <4 x i16> @urem_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; NEON-LABEL: urem_v4i16:
; NEON: // %bb.0:
-; NEON-NEXT: shl v2.4h, v2.4h, #15
+; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
-; NEON-NEXT: umov w11, v0.h[0]
-; NEON-NEXT: umov w8, v0.h[1]
-; NEON-NEXT: umov w14, v0.h[2]
-; NEON-NEXT: umov w17, v0.h[3]
-; NEON-NEXT: cmlt v2.4h, v2.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v2.8b
-; NEON-NEXT: mvn v2.8b, v2.8b
-; NEON-NEXT: sub v1.4h, v1.4h, v2.4h
-; NEON-NEXT: umov w12, v1.h[0]
-; NEON-NEXT: umov w9, v1.h[1]
-; NEON-NEXT: umov w15, v1.h[2]
-; NEON-NEXT: umov w18, v1.h[3]
-; NEON-NEXT: udiv w13, w11, w12
-; NEON-NEXT: udiv w10, w8, w9
-; NEON-NEXT: msub w11, w13, w12, w11
+; NEON-NEXT: umov w11, v1.h[0]
+; NEON-NEXT: umov w12, v0.h[0]
+; NEON-NEXT: umov w8, v1.h[1]
+; NEON-NEXT: umov w9, v0.h[1]
+; NEON-NEXT: umov w14, v1.h[2]
+; NEON-NEXT: umov w15, v0.h[2]
+; NEON-NEXT: umov w17, v1.h[3]
+; NEON-NEXT: umov w18, v0.h[3]
+; NEON-NEXT: udiv w13, w12, w11
+; NEON-NEXT: udiv w10, w9, w8
+; NEON-NEXT: msub w11, w13, w11, w12
; NEON-NEXT: fmov s0, w11
-; NEON-NEXT: udiv w16, w14, w15
-; NEON-NEXT: msub w8, w10, w9, w8
+; NEON-NEXT: udiv w16, w15, w14
+; NEON-NEXT: msub w8, w10, w8, w9
; NEON-NEXT: mov v0.h[1], w8
-; NEON-NEXT: udiv w9, w17, w18
-; NEON-NEXT: msub w8, w16, w15, w14
+; NEON-NEXT: udiv w9, w18, w17
+; NEON-NEXT: msub w8, w16, w14, w15
; NEON-NEXT: mov v0.h[2], w8
-; NEON-NEXT: msub w8, w9, w18, w17
+; NEON-NEXT: msub w8, w9, w17, w18
; NEON-NEXT: mov v0.h[3], w8
; NEON-NEXT: // kill: def $d0 killed $d0 killed $q0
; NEON-NEXT: ret
@@ -260,11 +227,9 @@ define <1 x i64> @urem_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; NEON-LABEL: urem_v1i164:
; NEON: // %bb.0:
; NEON-NEXT: // kill: def $d1 killed $d1 def $q1
-; NEON-NEXT: fmov x8, d1
; NEON-NEXT: // kill: def $d0 killed $d0 def $q0
+; NEON-NEXT: fmov x8, d1
; NEON-NEXT: fmov x9, d0
-; NEON-NEXT: tst w0, #0x1
-; NEON-NEXT: csinc x8, x8, xzr, ne
; NEON-NEXT: udiv x10, x9, x8
; NEON-NEXT: msub x8, x10, x8, x9
; NEON-NEXT: fmov d0, x8
@@ -273,11 +238,9 @@ define <1 x i64> @urem_v1i164(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; SVE-LABEL: urem_v1i164:
; SVE: // %bb.0:
; SVE-NEXT: // kill: def $d1 killed $d1 def $q1
-; SVE-NEXT: fmov x8, d1
; SVE-NEXT: // kill: def $d0 killed $d0 def $q0
+; SVE-NEXT: fmov x8, d1
; SVE-NEXT: fmov x9, d0
-; SVE-NEXT: tst w0, #0x1
-; SVE-NEXT: csinc x8, x8, xzr, ne
; SVE-NEXT: udiv x10, x9, x8
; SVE-NEXT: msub x8, x10, x8, x9
; SVE-NEXT: fmov d0, x8
@@ -365,33 +328,20 @@ define <2 x i128> @urem_v2i128(<2 x i128> %x, <2 x i128> %y, <2 x i1> %m) nounwi
define <3 x i10> @urem_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; NEON-LABEL: urem_v3i10:
; NEON: // %bb.0:
-; NEON-NEXT: movi v0.2d, #0000000000000000
+; NEON-NEXT: fmov s0, w0
; NEON-NEXT: fmov s1, w3
-; NEON-NEXT: ldr w8, [sp]
-; NEON-NEXT: fmov s2, w0
+; NEON-NEXT: mov v0.h[1], w1
; NEON-NEXT: mov v1.h[1], w4
-; NEON-NEXT: mov v0.h[0], w6
-; NEON-NEXT: mov v2.h[1], w1
+; NEON-NEXT: mov v0.h[2], w2
; NEON-NEXT: mov v1.h[2], w5
-; NEON-NEXT: mov v0.h[1], w7
-; NEON-NEXT: mov v2.h[2], w2
+; NEON-NEXT: bic v0.4h, #252, lsl #8
; NEON-NEXT: bic v1.4h, #252, lsl #8
-; NEON-NEXT: mov v0.h[2], w8
-; NEON-NEXT: bic v2.4h, #252, lsl #8
-; NEON-NEXT: umov w9, v2.h[0]
-; NEON-NEXT: umov w12, v2.h[1]
-; NEON-NEXT: umov w15, v2.h[2]
-; NEON-NEXT: shl v0.4h, v0.4h, #15
-; NEON-NEXT: cmlt v0.4h, v0.4h, #0
-; NEON-NEXT: and v1.8b, v1.8b, v0.8b
-; NEON-NEXT: mvn v0.8b, v0.8b
-; NEON-NEXT: sub v0.4h, v1.4h, v0.4h
-; NEON-NEXT: umov w8, v0.h[0]
-; NEON-NEXT: umov w11, v0.h[1]
-; NEON-NEXT: umov w14, v0.h[2]
-; NEON-NEXT: and w8, w8, #0x3ff
-; NEON-NEXT: and w11, w11, #0x3ff
-; NEON-NEXT: and w14, w14, #0x3ff
+; NEON-NEXT: umov w8, v1.h[0]
+; NEON-NEXT: umov w9, v0.h[0]
+; NEON-NEXT: umov w11, v1.h[1]
+; NEON-NEXT: umov w12, v0.h[1]
+; NEON-NEXT: umov w14, v1.h[2]
+; NEON-NEXT: umov w15, v0.h[2]
; NEON-NEXT: udiv w10, w9, w8
; NEON-NEXT: udiv w13, w12, w11
; NEON-NEXT: msub w0, w10, w8, w9
diff --git a/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-div.ll b/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-div.ll
index a3093684c145a9..603dd1a316d562 100644
--- a/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-div.ll
+++ b/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-div.ll
@@ -644,12 +644,10 @@ define void @sdiv_v64i32(ptr %a, ptr %b) vscale_range(16,0) #0 {
define <1 x i64> @sdiv_v1i64(<1 x i64> %op1, <1 x i64> %op2) vscale_range(1,0) #0 {
; CHECK-LABEL: sdiv_v1i64:
; CHECK: // %bb.0:
+; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: // kill: def $d0 killed $d0 def $q0
; CHECK-NEXT: fmov x8, d0
-; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: fmov x9, d1
-; CHECK-NEXT: cmp x8, #0
-; CHECK-NEXT: csinc x9, x9, xzr, ne
; CHECK-NEXT: sdiv x8, x8, x9
; CHECK-NEXT: fmov d0, x8
; CHECK-NEXT: ret
@@ -1421,12 +1419,10 @@ define void @udiv_v64i32(ptr %a, ptr %b) vscale_range(16,0) #0 {
define <1 x i64> @udiv_v1i64(<1 x i64> %op1, <1 x i64> %op2) vscale_range(1,0) #0 {
; CHECK-LABEL: udiv_v1i64:
; CHECK: // %bb.0:
+; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: // kill: def $d0 killed $d0 def $q0
; CHECK-NEXT: fmov x8, d0
-; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: fmov x9, d1
-; CHECK-NEXT: cmp x8, #0
-; CHECK-NEXT: csinc x9, x9, xzr, ne
; CHECK-NEXT: udiv x8, x8, x9
; CHECK-NEXT: fmov d0, x8
; CHECK-NEXT: ret
diff --git a/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-rem.ll b/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-rem.ll
index 3042d7f6b9593d..0e1fc3741f9204 100644
--- a/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-rem.ll
+++ b/llvm/test/CodeGen/AArch64/sve-fixed-length-masked-rem.ll
@@ -722,12 +722,10 @@ define void @srem_v64i32(ptr %a, ptr %b) vscale_range(16,0) #0 {
define <1 x i64> @srem_v1i64(<1 x i64> %op1, <1 x i64> %op2) vscale_range(1,0) #0 {
; CHECK-LABEL: srem_v1i64:
; CHECK: // %bb.0:
+; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: // kill: def $d0 killed $d0 def $q0
; CHECK-NEXT: fmov x8, d0
-; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: fmov x9, d1
-; CHECK-NEXT: cmp x8, #0
-; CHECK-NEXT: csinc x9, x9, xzr, ne
; CHECK-NEXT: sdiv x10, x8, x9
; CHECK-NEXT: msub x8, x10, x9, x8
; CHECK-NEXT: fmov d0, x8
@@ -1618,12 +1616,10 @@ define void @urem_v64i32(ptr %a, ptr %b) vscale_range(16,0) #0 {
define <1 x i64> @urem_v1i64(<1 x i64> %op1, <1 x i64> %op2) vscale_range(1,0) #0 {
; CHECK-LABEL: urem_v1i64:
; CHECK: // %bb.0:
+; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: // kill: def $d0 killed $d0 def $q0
; CHECK-NEXT: fmov x8, d0
-; CHECK-NEXT: // kill: def $d1 killed $d1 def $q1
; CHECK-NEXT: fmov x9, d1
-; CHECK-NEXT: cmp x8, #0
-; CHECK-NEXT: csinc x9, x9, xzr, ne
; CHECK-NEXT: udiv x10, x8, x9
; CHECK-NEXT: msub x8, x10, x9, x8
; CHECK-NEXT: fmov d0, x8
>From f12bcfa7a6a6e0ff613d0af8a97ce050b605cd35 Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 13:20:48 +0100
Subject: [PATCH 7/9] [RISCV] Recognize trap-free integer div/rem with M and V
The M extension defines exceptional integer division results without
trapping. Treat i8/i16/i32, and i64 on RV64, as trap-free. Zmmul-only
configurations and wider software division types keep their guards.
Lower native masked vector operations on RVV to
RISCVISD::{S,U}{DIV,REM}_VL with an all-ones mask: vdiv and friends do
not trap either, so computing disabled lanes refines their poison. This
requires vector support for the element width, including i64. Drop the
RISCVTTIImpl vector override so BasicTTIImpl handles native RVV,
trap-free scalarization, signed i1, and guarded fallbacks uniformly.
Assisted-by: Claude
---
llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 43 +-
llvm/lib/Target/RISCV/RISCVISelLowering.h | 2 +
.../Target/RISCV/RISCVInstrInfoVSDPatterns.td | 25 -
.../Target/RISCV/RISCVTargetTransformInfo.cpp | 8 -
.../RISCV/masked-divrem-scalar-zve.ll | 71 ++
.../RISCV/GlobalISel/masked-divrem-scalar.ll | 282 ++++++
llvm/test/CodeGen/RISCV/masked-divrem-i128.ll | 130 +++
.../CodeGen/RISCV/masked-divrem-scalar.ll | 802 ++++++++++++++++++
.../CodeGen/RISCV/rvv/masked-divrem-zve.ll | 208 +++++
llvm/test/CodeGen/RISCV/rvv/masked-sdiv.ll | 30 +-
llvm/test/CodeGen/RISCV/rvv/masked-srem.ll | 30 +-
llvm/test/CodeGen/RISCV/rvv/masked-udiv.ll | 32 +-
llvm/test/CodeGen/RISCV/rvv/masked-urem.ll | 30 +-
13 files changed, 1584 insertions(+), 109 deletions(-)
create mode 100644 llvm/test/Analysis/CostModel/RISCV/masked-divrem-scalar-zve.ll
create mode 100644 llvm/test/CodeGen/RISCV/GlobalISel/masked-divrem-scalar.ll
create mode 100644 llvm/test/CodeGen/RISCV/masked-divrem-i128.ll
create mode 100644 llvm/test/CodeGen/RISCV/masked-divrem-scalar.ll
create mode 100644 llvm/test/CodeGen/RISCV/rvv/masked-divrem-zve.ll
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index cba289900f30b5..0475e8637a3e55 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -1183,7 +1183,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
setOperationAction(ISD::VECTOR_COMPRESS, VT, Custom);
setOperationAction({ISD::MASKED_UDIV, ISD::MASKED_SDIV, ISD::MASKED_UREM,
ISD::MASKED_SREM},
- VT, Legal);
+ VT, Custom);
}
for (MVT VT : VecTupleVTs) {
@@ -9767,17 +9767,28 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
case ISD::MASKED_SREM: {
SDLoc DL(Op);
MVT VT = Op.getSimpleValueType();
- MVT ContainerVT = getContainerForFixedLengthVector(VT);
- SDValue VL = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget).second;
+ MVT ContainerVT =
+ VT.isFixedLengthVector() ? getContainerForFixedLengthVector(VT) : VT;
+ auto [DefaultMask, VL] =
+ getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
+ // Keep a target vector node so an exceptional inactive lane cannot trigger
+ // a whole-vector fold of an ordinary DAG div/rem. RVV integer division does
+ // not trap, so computing disabled lanes with DefaultMask refines poison.
+ assert(Subtarget.hasVInstructions() &&
+ (ContainerVT.getVectorElementType() != MVT::i64 ||
+ Subtarget.hasVInstructionsI64()) &&
+ "Expected RVV integer division support for custom MASKED_DIV/REM");
+ auto Convert = [&](SDValue V) {
+ return VT.isFixedLengthVector()
+ ? convertToScalableVector(ContainerVT, V, DAG, Subtarget)
+ : V;
+ };
SDValue Res = DAG.getNode(
- getRISCVVLOp(Op), DL, ContainerVT,
- convertToScalableVector(ContainerVT, Op.getOperand(0), DAG, Subtarget),
- convertToScalableVector(ContainerVT, Op.getOperand(1), DAG, Subtarget),
- DAG.getUNDEF(ContainerVT),
- convertToScalableVector(getMaskTypeFor(ContainerVT), Op.getOperand(2),
- DAG, Subtarget),
- VL);
- return convertFromScalableVector(VT, Res, DAG, Subtarget);
+ getRISCVVLOp(Op), DL, ContainerVT, Convert(Op.getOperand(0)),
+ Convert(Op.getOperand(1)), DAG.getUNDEF(ContainerVT), DefaultMask, VL);
+ return VT.isFixedLengthVector()
+ ? convertFromScalableVector(VT, Res, DAG, Subtarget)
+ : Res;
}
case ISD::FABS:
case ISD::FNEG:
@@ -31269,6 +31280,16 @@ SDValue RISCVTargetLowering::joinRegisterPartsIntoValue(
return SDValue();
}
+bool RISCVTargetLowering::isIntDivRemTrapFree(EVT VT) const {
+ // The M extension defines results for zero divisors and signed overflow
+ // without trapping, including RV64 word operations. Zmmul alone does not
+ // provide division; software division and wider types must keep guards.
+ // Small integers use native or promoted non-trapping division.
+ return Subtarget.hasStdExtM() &&
+ (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
+ (Subtarget.is64Bit() && VT == MVT::i64));
+}
+
bool RISCVTargetLowering::isIntDivCheap(EVT VT, AttributeList Attr) const {
// When aggressively optimizing for code size, we prefer to use a div
// instruction, as it is usually smaller than the alternative sequence.
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index 42b92af2880714..b89c43cfc0f0d1 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -69,6 +69,8 @@ class RISCVTargetLowering : public TargetLowering {
ExtractSubvectorCost getExtractSubvectorCost(EVT ResVT, EVT SrcVT,
unsigned Index) const override;
+ bool isIntDivRemTrapFree(EVT VT) const override;
+
bool isIntDivCheap(EVT VT, AttributeList Attr) const override;
bool preferScalarizeSplat(SDNode *N) const override;
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td b/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td
index 33650b52dee858..0435a2c2169bbb 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td
@@ -168,27 +168,6 @@ multiclass VPatBinarySDNode_VV_VX_VI<SDPatternOperator vop, string instruction_n
}
}
-multiclass VPatBinaryMaskedSDNode_VV<SDPatternOperator vop, string instruction_name> {
- foreach vti = AllIntegerVectors in {
- def : Pat<(vti.Vector (vop vti.RegClass:$vs2, vti.RegClass:$vs1, VMV0:$vm)),
- (!cast<Instruction>(instruction_name#"_VV_"#vti.LMul.MX#"_E"#!shl(1, vti.Log2SEW)#"_MASK")
- (IMPLICIT_DEF), vti.RegClass:$vs2, vti.RegClass:$vs1, VMV0:$vm,
- vti.AVL, vti.Log2SEW, TA_MA)>;
- }
-}
-
-multiclass VPatBinaryMaskedSDNode_VV_VX<SDPatternOperator vop, string instruction_name>
- : VPatBinaryMaskedSDNode_VV<vop, instruction_name> {
- foreach vti = AllIntegerVectors in {
- def : Pat<(vti.Vector (vop vti.RegClass:$vs2,
- (SplatPat (XLenVT GPR:$rs1)),
- VMV0:$vm)),
- (!cast<Instruction>(instruction_name#"_VX_"#vti.LMul.MX#"_E"#!shl(1, vti.Log2SEW)#"_MASK")
- (IMPLICIT_DEF), vti.RegClass:$vs2, GPR:$rs1, VMV0:$vm,
- vti.AVL, vti.Log2SEW, TA_MA)>;
- }
-}
-
class VPatBinarySDNode_VF<SDPatternOperator vop,
string instruction_name,
ValueType result_type,
@@ -1115,10 +1094,6 @@ defm : VPatBinarySDNode_VV_VX<udiv, "PseudoVDIVU", isSEWAware=1>;
defm : VPatBinarySDNode_VV_VX<sdiv, "PseudoVDIV", isSEWAware=1>;
defm : VPatBinarySDNode_VV_VX<urem, "PseudoVREMU", isSEWAware=1>;
defm : VPatBinarySDNode_VV_VX<srem, "PseudoVREM", isSEWAware=1>;
-defm : VPatBinaryMaskedSDNode_VV_VX<masked_udiv, "PseudoVDIVU">;
-defm : VPatBinaryMaskedSDNode_VV_VX<masked_sdiv, "PseudoVDIV">;
-defm : VPatBinaryMaskedSDNode_VV_VX<masked_urem, "PseudoVREMU">;
-defm : VPatBinaryMaskedSDNode_VV_VX<masked_srem, "PseudoVREM">;
foreach vtiTowti = AllWidenableIntVectors in {
defvar vti = vtiTowti.Vti;
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 29a1a557ea4e2f..0fb65c848f6e25 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -1940,14 +1940,6 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
}
break;
}
- case Intrinsic::masked_udiv:
- case Intrinsic::masked_sdiv:
- case Intrinsic::masked_urem:
- case Intrinsic::masked_srem:
- if (RetTy->isIntegerTy())
- break;
- return getArithmeticInstrCost(
- Intrinsic::getMaskedIntDivRemOpcode(ICA.getID()), RetTy, CostKind);
case Intrinsic::get_active_lane_mask: {
if (ST->hasVInstructions()) {
Type *ExpRetTy = VectorType::get(
diff --git a/llvm/test/Analysis/CostModel/RISCV/masked-divrem-scalar-zve.ll b/llvm/test/Analysis/CostModel/RISCV/masked-divrem-scalar-zve.ll
new file mode 100644
index 00000000000000..6091fe9cd6bf8f
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/RISCV/masked-divrem-scalar-zve.ll
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes="print<cost-model>" -cost-kind=all -disable-output -mtriple=riscv64 -mattr=+m < %s 2>&1 | FileCheck %s --check-prefix=RV64M
+; RUN: opt -passes="print<cost-model>" -cost-kind=all -disable-output -mtriple=riscv64 -mattr=+zmmul < %s 2>&1 | FileCheck %s --check-prefix=ZMMUL
+; RUN: opt -passes="print<cost-model>" -cost-kind=all -disable-output -mtriple=riscv64 -mattr=+zve32x < %s 2>&1 | FileCheck %s --check-prefix=ZVE32X
+; RUN: opt -passes="print<cost-model>" -cost-kind=all -disable-output -mtriple=riscv64 -mattr=+m,+zve32x < %s 2>&1 | FileCheck %s --check-prefix=MZVE32X
+
+define void @masked_divrem_costs() {
+; RV64M-LABEL: 'masked_divrem_costs'
+; RV64M-NEXT: Cost Model: Found costs of 0 for: %s1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+; RV64M-NEXT: Cost Model: Found costs of 5 for: %u1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+; RV64M-NEXT: Cost Model: Found costs of 4 for: %i8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+; RV64M-NEXT: Cost Model: Found costs of 4 for: %i32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+; RV64M-NEXT: Cost Model: Found costs of 4 for: %i64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+; RV64M-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:6 SizeLat:6 for: %i128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+; RV64M-NEXT: Cost Model: Found costs of 0 for: %vs1 = call <2 x i1> @llvm.masked.sdiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; RV64M-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %vu1 = call <2 x i1> @llvm.masked.udiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; RV64M-NEXT: Cost Model: Found costs of RThru:2 CodeSize:4 Lat:4 SizeLat:4 for: %v2i32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; RV64M-NEXT: Cost Model: Found costs of RThru:2 CodeSize:4 Lat:4 SizeLat:4 for: %v2i64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; RV64M-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+; ZMMUL-LABEL: 'masked_divrem_costs'
+; ZMMUL-NEXT: Cost Model: Found costs of 0 for: %s1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %u1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %i8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %i32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %i64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:3 CodeSize:6 Lat:6 SizeLat:6 for: %i128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+; ZMMUL-NEXT: Cost Model: Found costs of 0 for: %vs1 = call <2 x i1> @llvm.masked.sdiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %vu1 = call <2 x i1> @llvm.masked.udiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %v2i32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %v2i64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; ZMMUL-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+; ZVE32X-LABEL: 'masked_divrem_costs'
+; ZVE32X-NEXT: Cost Model: Found costs of 0 for: %s1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %u1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %i8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %i32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:2 CodeSize:5 Lat:5 SizeLat:5 for: %i64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:3 CodeSize:6 Lat:6 SizeLat:6 for: %i128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+; ZVE32X-NEXT: Cost Model: Found costs of 0 for: %vs1 = call <2 x i1> @llvm.masked.sdiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %vu1 = call <2 x i1> @llvm.masked.udiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %v2i32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %v2i64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; ZVE32X-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+; MZVE32X-LABEL: 'masked_divrem_costs'
+; MZVE32X-NEXT: Cost Model: Found costs of 0 for: %s1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+; MZVE32X-NEXT: Cost Model: Found costs of 5 for: %u1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+; MZVE32X-NEXT: Cost Model: Found costs of 4 for: %i8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+; MZVE32X-NEXT: Cost Model: Found costs of 4 for: %i32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+; MZVE32X-NEXT: Cost Model: Found costs of 4 for: %i64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+; MZVE32X-NEXT: Cost Model: Found costs of RThru:10 CodeSize:6 Lat:6 SizeLat:6 for: %i128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+; MZVE32X-NEXT: Cost Model: Found costs of 0 for: %vs1 = call <2 x i1> @llvm.masked.sdiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; MZVE32X-NEXT: Cost Model: Found costs of RThru:4 CodeSize:6 Lat:6 SizeLat:6 for: %vu1 = call <2 x i1> @llvm.masked.udiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+; MZVE32X-NEXT: Cost Model: Found costs of RThru:2 CodeSize:4 Lat:4 SizeLat:4 for: %v2i32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+; MZVE32X-NEXT: Cost Model: Found costs of RThru:2 CodeSize:4 Lat:4 SizeLat:4 for: %v2i64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+; MZVE32X-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret void
+;
+ %s1 = call i1 @llvm.masked.sdiv.i1(i1 poison, i1 poison, i1 poison)
+ %u1 = call i1 @llvm.masked.udiv.i1(i1 poison, i1 poison, i1 poison)
+ %i8 = call i8 @llvm.masked.sdiv.i8(i8 poison, i8 poison, i1 poison)
+ %i32 = call i32 @llvm.masked.udiv.i32(i32 poison, i32 poison, i1 poison)
+ %i64 = call i64 @llvm.masked.srem.i64(i64 poison, i64 poison, i1 poison)
+ %i128 = call i128 @llvm.masked.sdiv.i128(i128 poison, i128 poison, i1 poison)
+ %vs1 = call <2 x i1> @llvm.masked.sdiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+ %vu1 = call <2 x i1> @llvm.masked.udiv.v2i1(<2 x i1> poison, <2 x i1> poison, <2 x i1> poison)
+ %v2i32 = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> poison, <2 x i32> poison, <2 x i1> poison)
+ %v2i64 = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> poison, <2 x i64> poison, <2 x i1> poison)
+ ret void
+}
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/masked-divrem-scalar.ll b/llvm/test/CodeGen/RISCV/GlobalISel/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..fc19a0043a33b0
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/masked-divrem-scalar.ll
@@ -0,0 +1,282 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv32 -mattr=+m -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=RV32-GISEL
+; RUN: llc -mtriple=riscv64 -mattr=+m -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=RV64-GISEL
+; RUN: llc -mtriple=riscv64 -mattr=-m,+zmmul -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=ZMMUL-GISEL
+
+define i8 @udiv_i8(i8 %a, i8 %b, i1 %m) {
+; RV32-GISEL-LABEL: udiv_i8:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: zext.b a0, a0
+; RV32-GISEL-NEXT: zext.b a1, a1
+; RV32-GISEL-NEXT: divu a0, a0, a1
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: udiv_i8:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: zext.b a0, a0
+; RV64-GISEL-NEXT: zext.b a1, a1
+; RV64-GISEL-NEXT: divuw a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: udiv_i8:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB0_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB0_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: zext.b a0, a0
+; ZMMUL-GISEL-NEXT: zext.b a1, a1
+; ZMMUL-GISEL-NEXT: call __udivdi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i8 @llvm.masked.udiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i16 @srem_i16(i16 %a, i16 %b, i1 %m) {
+; RV32-GISEL-LABEL: srem_i16:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: slli a0, a0, 16
+; RV32-GISEL-NEXT: slli a1, a1, 16
+; RV32-GISEL-NEXT: srai a0, a0, 16
+; RV32-GISEL-NEXT: srai a1, a1, 16
+; RV32-GISEL-NEXT: rem a0, a0, a1
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: srem_i16:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: slli a0, a0, 48
+; RV64-GISEL-NEXT: slli a1, a1, 48
+; RV64-GISEL-NEXT: srai a0, a0, 48
+; RV64-GISEL-NEXT: srai a1, a1, 48
+; RV64-GISEL-NEXT: remw a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: srem_i16:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB1_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB1_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: slli a0, a0, 48
+; ZMMUL-GISEL-NEXT: slli a1, a1, 48
+; ZMMUL-GISEL-NEXT: srai a0, a0, 48
+; ZMMUL-GISEL-NEXT: srai a1, a1, 48
+; ZMMUL-GISEL-NEXT: call __moddi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i16 @llvm.masked.srem.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-GISEL-LABEL: sdiv_i32:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: div a0, a0, a1
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: sdiv_i32:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: divw a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: sdiv_i32:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB2_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB2_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: sext.w a0, a0
+; ZMMUL-GISEL-NEXT: sext.w a1, a1
+; ZMMUL-GISEL-NEXT: call __divdi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-GISEL-LABEL: urem_i32:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: remu a0, a0, a1
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: urem_i32:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: remuw a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: urem_i32:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB3_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB3_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: slli a0, a0, 32
+; ZMMUL-GISEL-NEXT: slli a1, a1, 32
+; ZMMUL-GISEL-NEXT: srli a0, a0, 32
+; ZMMUL-GISEL-NEXT: srli a1, a1, 32
+; ZMMUL-GISEL-NEXT: call __umoddi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @srem_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-GISEL-LABEL: srem_i32:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: rem a0, a0, a1
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: srem_i32:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: remw a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: srem_i32:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB4_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB4_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: sext.w a0, a0
+; ZMMUL-GISEL-NEXT: sext.w a1, a1
+; ZMMUL-GISEL-NEXT: call __moddi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i64 @sdiv_i64(i64 %a, i64 %b, i1 %m) {
+; RV32-GISEL-LABEL: sdiv_i64:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: andi a4, a4, 1
+; RV32-GISEL-NEXT: bnez a4, .LBB5_2
+; RV32-GISEL-NEXT: # %bb.1:
+; RV32-GISEL-NEXT: li a3, 0
+; RV32-GISEL-NEXT: li a2, 1
+; RV32-GISEL-NEXT: .LBB5_2:
+; RV32-GISEL-NEXT: addi sp, sp, -16
+; RV32-GISEL-NEXT: .cfi_def_cfa_offset 16
+; RV32-GISEL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-GISEL-NEXT: .cfi_offset ra, -4
+; RV32-GISEL-NEXT: call __divdi3
+; RV32-GISEL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-GISEL-NEXT: .cfi_restore ra
+; RV32-GISEL-NEXT: addi sp, sp, 16
+; RV32-GISEL-NEXT: .cfi_def_cfa_offset 0
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: sdiv_i64:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: div a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: sdiv_i64:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB5_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB5_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: call __divdi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @srem_i64(i64 %a, i64 %b, i1 %m) {
+; RV32-GISEL-LABEL: srem_i64:
+; RV32-GISEL: # %bb.0:
+; RV32-GISEL-NEXT: andi a4, a4, 1
+; RV32-GISEL-NEXT: bnez a4, .LBB6_2
+; RV32-GISEL-NEXT: # %bb.1:
+; RV32-GISEL-NEXT: li a3, 0
+; RV32-GISEL-NEXT: li a2, 1
+; RV32-GISEL-NEXT: .LBB6_2:
+; RV32-GISEL-NEXT: addi sp, sp, -16
+; RV32-GISEL-NEXT: .cfi_def_cfa_offset 16
+; RV32-GISEL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-GISEL-NEXT: .cfi_offset ra, -4
+; RV32-GISEL-NEXT: call __moddi3
+; RV32-GISEL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-GISEL-NEXT: .cfi_restore ra
+; RV32-GISEL-NEXT: addi sp, sp, 16
+; RV32-GISEL-NEXT: .cfi_def_cfa_offset 0
+; RV32-GISEL-NEXT: ret
+;
+; RV64-GISEL-LABEL: srem_i64:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: rem a0, a0, a1
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-GISEL-LABEL: srem_i64:
+; ZMMUL-GISEL: # %bb.0:
+; ZMMUL-GISEL-NEXT: andi a2, a2, 1
+; ZMMUL-GISEL-NEXT: bnez a2, .LBB6_2
+; ZMMUL-GISEL-NEXT: # %bb.1:
+; ZMMUL-GISEL-NEXT: li a1, 1
+; ZMMUL-GISEL-NEXT: .LBB6_2:
+; ZMMUL-GISEL-NEXT: addi sp, sp, -16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-GISEL-NEXT: .cfi_offset ra, -8
+; ZMMUL-GISEL-NEXT: call __moddi3
+; ZMMUL-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-GISEL-NEXT: .cfi_restore ra
+; ZMMUL-GISEL-NEXT: addi sp, sp, 16
+; ZMMUL-GISEL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-GISEL-NEXT: ret
+ %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
diff --git a/llvm/test/CodeGen/RISCV/masked-divrem-i128.ll b/llvm/test/CodeGen/RISCV/masked-divrem-i128.ll
new file mode 100644
index 00000000000000..97c8af742ab24b
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/masked-divrem-i128.ll
@@ -0,0 +1,130 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv64 -mattr=+m < %s | FileCheck %s --check-prefix=RV64-SDAG
+; RUN: llc -mtriple=riscv64 -mattr=+m -global-isel -global-isel-abort=1 < %s | FileCheck %s --check-prefix=RV64-GISEL
+; RUN: llc -mtriple=riscv64 -mattr=-m,+zmmul < %s | FileCheck %s --check-prefix=ZMMUL-SDAG
+
+define i128 @wide_guard_udiv_i128(i128 %a, i128 %b, i1 %m) {
+; RV64-SDAG-LABEL: wide_guard_udiv_i128:
+; RV64-SDAG: # %bb.0:
+; RV64-SDAG-NEXT: andi a4, a4, 1
+; RV64-SDAG-NEXT: bnez a4, .LBB0_2
+; RV64-SDAG-NEXT: # %bb.1:
+; RV64-SDAG-NEXT: li a2, 1
+; RV64-SDAG-NEXT: .LBB0_2:
+; RV64-SDAG-NEXT: addi sp, sp, -16
+; RV64-SDAG-NEXT: .cfi_def_cfa_offset 16
+; RV64-SDAG-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; RV64-SDAG-NEXT: .cfi_offset ra, -8
+; RV64-SDAG-NEXT: neg a4, a4
+; RV64-SDAG-NEXT: and a3, a4, a3
+; RV64-SDAG-NEXT: call __udivti3
+; RV64-SDAG-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; RV64-SDAG-NEXT: .cfi_restore ra
+; RV64-SDAG-NEXT: addi sp, sp, 16
+; RV64-SDAG-NEXT: .cfi_def_cfa_offset 0
+; RV64-SDAG-NEXT: ret
+;
+; RV64-GISEL-LABEL: wide_guard_udiv_i128:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: andi a4, a4, 1
+; RV64-GISEL-NEXT: bnez a4, .LBB0_2
+; RV64-GISEL-NEXT: # %bb.1:
+; RV64-GISEL-NEXT: li a3, 0
+; RV64-GISEL-NEXT: li a2, 1
+; RV64-GISEL-NEXT: .LBB0_2:
+; RV64-GISEL-NEXT: addi sp, sp, -16
+; RV64-GISEL-NEXT: .cfi_def_cfa_offset 16
+; RV64-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; RV64-GISEL-NEXT: .cfi_offset ra, -8
+; RV64-GISEL-NEXT: call __udivti3
+; RV64-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; RV64-GISEL-NEXT: .cfi_restore ra
+; RV64-GISEL-NEXT: addi sp, sp, 16
+; RV64-GISEL-NEXT: .cfi_def_cfa_offset 0
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-SDAG-LABEL: wide_guard_udiv_i128:
+; ZMMUL-SDAG: # %bb.0:
+; ZMMUL-SDAG-NEXT: andi a4, a4, 1
+; ZMMUL-SDAG-NEXT: bnez a4, .LBB0_2
+; ZMMUL-SDAG-NEXT: # %bb.1:
+; ZMMUL-SDAG-NEXT: li a2, 1
+; ZMMUL-SDAG-NEXT: .LBB0_2:
+; ZMMUL-SDAG-NEXT: addi sp, sp, -16
+; ZMMUL-SDAG-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-SDAG-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-SDAG-NEXT: .cfi_offset ra, -8
+; ZMMUL-SDAG-NEXT: neg a4, a4
+; ZMMUL-SDAG-NEXT: and a3, a4, a3
+; ZMMUL-SDAG-NEXT: call __udivti3
+; ZMMUL-SDAG-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-SDAG-NEXT: .cfi_restore ra
+; ZMMUL-SDAG-NEXT: addi sp, sp, 16
+; ZMMUL-SDAG-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-SDAG-NEXT: ret
+ %r = call i128 @llvm.masked.udiv.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
+
+define i128 @wide_guard_srem_i128(i128 %a, i128 %b, i1 %m) {
+; RV64-SDAG-LABEL: wide_guard_srem_i128:
+; RV64-SDAG: # %bb.0:
+; RV64-SDAG-NEXT: andi a4, a4, 1
+; RV64-SDAG-NEXT: bnez a4, .LBB1_2
+; RV64-SDAG-NEXT: # %bb.1:
+; RV64-SDAG-NEXT: li a2, 1
+; RV64-SDAG-NEXT: .LBB1_2:
+; RV64-SDAG-NEXT: addi sp, sp, -16
+; RV64-SDAG-NEXT: .cfi_def_cfa_offset 16
+; RV64-SDAG-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; RV64-SDAG-NEXT: .cfi_offset ra, -8
+; RV64-SDAG-NEXT: neg a4, a4
+; RV64-SDAG-NEXT: and a3, a4, a3
+; RV64-SDAG-NEXT: call __modti3
+; RV64-SDAG-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; RV64-SDAG-NEXT: .cfi_restore ra
+; RV64-SDAG-NEXT: addi sp, sp, 16
+; RV64-SDAG-NEXT: .cfi_def_cfa_offset 0
+; RV64-SDAG-NEXT: ret
+;
+; RV64-GISEL-LABEL: wide_guard_srem_i128:
+; RV64-GISEL: # %bb.0:
+; RV64-GISEL-NEXT: andi a4, a4, 1
+; RV64-GISEL-NEXT: bnez a4, .LBB1_2
+; RV64-GISEL-NEXT: # %bb.1:
+; RV64-GISEL-NEXT: li a3, 0
+; RV64-GISEL-NEXT: li a2, 1
+; RV64-GISEL-NEXT: .LBB1_2:
+; RV64-GISEL-NEXT: addi sp, sp, -16
+; RV64-GISEL-NEXT: .cfi_def_cfa_offset 16
+; RV64-GISEL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; RV64-GISEL-NEXT: .cfi_offset ra, -8
+; RV64-GISEL-NEXT: call __modti3
+; RV64-GISEL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; RV64-GISEL-NEXT: .cfi_restore ra
+; RV64-GISEL-NEXT: addi sp, sp, 16
+; RV64-GISEL-NEXT: .cfi_def_cfa_offset 0
+; RV64-GISEL-NEXT: ret
+;
+; ZMMUL-SDAG-LABEL: wide_guard_srem_i128:
+; ZMMUL-SDAG: # %bb.0:
+; ZMMUL-SDAG-NEXT: andi a4, a4, 1
+; ZMMUL-SDAG-NEXT: bnez a4, .LBB1_2
+; ZMMUL-SDAG-NEXT: # %bb.1:
+; ZMMUL-SDAG-NEXT: li a2, 1
+; ZMMUL-SDAG-NEXT: .LBB1_2:
+; ZMMUL-SDAG-NEXT: addi sp, sp, -16
+; ZMMUL-SDAG-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-SDAG-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-SDAG-NEXT: .cfi_offset ra, -8
+; ZMMUL-SDAG-NEXT: neg a4, a4
+; ZMMUL-SDAG-NEXT: and a3, a4, a3
+; ZMMUL-SDAG-NEXT: call __modti3
+; ZMMUL-SDAG-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-SDAG-NEXT: .cfi_restore ra
+; ZMMUL-SDAG-NEXT: addi sp, sp, 16
+; ZMMUL-SDAG-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-SDAG-NEXT: ret
+ %r = call i128 @llvm.masked.srem.i128(i128 %a, i128 %b, i1 %m)
+ ret i128 %r
+}
diff --git a/llvm/test/CodeGen/RISCV/masked-divrem-scalar.ll b/llvm/test/CodeGen/RISCV/masked-divrem-scalar.ll
new file mode 100644
index 00000000000000..da55ccb95a70fd
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/masked-divrem-scalar.ll
@@ -0,0 +1,802 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv32 -mattr=+m < %s | FileCheck %s --check-prefix=RV32
+; RUN: llc -mtriple=riscv32 -mattr=-m,+zmmul < %s | FileCheck %s --check-prefix=RV32-ZMMUL
+; RUN: llc -mtriple=riscv64 -mattr=+m < %s | FileCheck %s --check-prefix=RV64
+; RUN: llc -mtriple=riscv64 -mattr=-m,+zmmul < %s | FileCheck %s --check-prefix=ZMMUL
+
+define i1 @sdiv_i1(i1 %a, i1 %b, i1 %m) {
+; RV32-LABEL: sdiv_i1:
+; RV32: # %bb.0:
+; RV32-NEXT: li a0, 0
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: sdiv_i1:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: li a0, 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: sdiv_i1:
+; RV64: # %bb.0:
+; RV64-NEXT: li a0, 0
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: sdiv_i1:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: li a0, 0
+; ZMMUL-NEXT: ret
+ %r = call i1 @llvm.masked.sdiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i1 @udiv_i1(i1 %a, i1 %b, i1 %m) {
+; RV32-LABEL: udiv_i1:
+; RV32: # %bb.0:
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: udiv_i1:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: udiv_i1:
+; RV64: # %bb.0:
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: udiv_i1:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: ret
+ %r = call i1 @llvm.masked.udiv.i1(i1 %a, i1 %b, i1 %m)
+ ret i1 %r
+}
+
+define i8 @udiv_i8(i8 %a, i8 %b, i1 %m) {
+; RV32-LABEL: udiv_i8:
+; RV32: # %bb.0:
+; RV32-NEXT: zext.b a1, a1
+; RV32-NEXT: zext.b a0, a0
+; RV32-NEXT: divu a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: udiv_i8:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: zext.b a0, a0
+; RV32-ZMMUL-NEXT: bnez a2, .LBB2_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB2_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: zext.b a1, a1
+; RV32-ZMMUL-NEXT: call __udivsi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: udiv_i8:
+; RV64: # %bb.0:
+; RV64-NEXT: zext.b a1, a1
+; RV64-NEXT: zext.b a0, a0
+; RV64-NEXT: divuw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: udiv_i8:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: zext.b a0, a0
+; ZMMUL-NEXT: bnez a2, .LBB2_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB2_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: zext.b a1, a1
+; ZMMUL-NEXT: call __udivdi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i8 @llvm.masked.udiv.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i8 @srem_i8(i8 %a, i8 %b, i1 %m) {
+; RV32-LABEL: srem_i8:
+; RV32: # %bb.0:
+; RV32-NEXT: slli a1, a1, 24
+; RV32-NEXT: slli a0, a0, 24
+; RV32-NEXT: srai a1, a1, 24
+; RV32-NEXT: srai a0, a0, 24
+; RV32-NEXT: rem a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: srem_i8:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: slli a0, a0, 24
+; RV32-ZMMUL-NEXT: srai a0, a0, 24
+; RV32-ZMMUL-NEXT: bnez a2, .LBB3_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB3_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: slli a1, a1, 24
+; RV32-ZMMUL-NEXT: srai a1, a1, 24
+; RV32-ZMMUL-NEXT: call __modsi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: srem_i8:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a1, a1, 56
+; RV64-NEXT: slli a0, a0, 56
+; RV64-NEXT: srai a1, a1, 56
+; RV64-NEXT: srai a0, a0, 56
+; RV64-NEXT: remw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: srem_i8:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: slli a0, a0, 56
+; ZMMUL-NEXT: srai a0, a0, 56
+; ZMMUL-NEXT: bnez a2, .LBB3_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB3_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: slli a1, a1, 56
+; ZMMUL-NEXT: srai a1, a1, 56
+; ZMMUL-NEXT: call __moddi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i8 @llvm.masked.srem.i8(i8 %a, i8 %b, i1 %m)
+ ret i8 %r
+}
+
+define i16 @sdiv_i16(i16 %a, i16 %b, i1 %m) {
+; RV32-LABEL: sdiv_i16:
+; RV32: # %bb.0:
+; RV32-NEXT: slli a1, a1, 16
+; RV32-NEXT: slli a0, a0, 16
+; RV32-NEXT: srai a1, a1, 16
+; RV32-NEXT: srai a0, a0, 16
+; RV32-NEXT: div a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: sdiv_i16:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: slli a0, a0, 16
+; RV32-ZMMUL-NEXT: srai a0, a0, 16
+; RV32-ZMMUL-NEXT: bnez a2, .LBB4_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB4_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: slli a1, a1, 16
+; RV32-ZMMUL-NEXT: srai a1, a1, 16
+; RV32-ZMMUL-NEXT: call __divsi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: sdiv_i16:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a1, a1, 48
+; RV64-NEXT: slli a0, a0, 48
+; RV64-NEXT: srai a1, a1, 48
+; RV64-NEXT: srai a0, a0, 48
+; RV64-NEXT: divw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: sdiv_i16:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: slli a0, a0, 48
+; ZMMUL-NEXT: srai a0, a0, 48
+; ZMMUL-NEXT: bnez a2, .LBB4_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB4_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: slli a1, a1, 48
+; ZMMUL-NEXT: srai a1, a1, 48
+; ZMMUL-NEXT: call __divdi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i16 @llvm.masked.sdiv.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i16 @urem_i16(i16 %a, i16 %b, i1 %m) {
+; RV32-LABEL: urem_i16:
+; RV32: # %bb.0:
+; RV32-NEXT: lui a2, 16
+; RV32-NEXT: addi a2, a2, -1
+; RV32-NEXT: and a1, a1, a2
+; RV32-NEXT: and a0, a0, a2
+; RV32-NEXT: remu a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: urem_i16:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: lui a3, 16
+; RV32-ZMMUL-NEXT: addi a3, a3, -1
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: and a0, a0, a3
+; RV32-ZMMUL-NEXT: bnez a2, .LBB5_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB5_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: and a1, a1, a3
+; RV32-ZMMUL-NEXT: call __umodsi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: urem_i16:
+; RV64: # %bb.0:
+; RV64-NEXT: lui a2, 16
+; RV64-NEXT: addi a2, a2, -1
+; RV64-NEXT: and a1, a1, a2
+; RV64-NEXT: and a0, a0, a2
+; RV64-NEXT: remuw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: urem_i16:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: lui a3, 16
+; ZMMUL-NEXT: addi a3, a3, -1
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: and a0, a0, a3
+; ZMMUL-NEXT: bnez a2, .LBB5_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB5_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: and a1, a1, a3
+; ZMMUL-NEXT: call __umoddi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i16 @llvm.masked.urem.i16(i16 %a, i16 %b, i1 %m)
+ ret i16 %r
+}
+
+define i32 @udiv_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-LABEL: udiv_i32:
+; RV32: # %bb.0:
+; RV32-NEXT: divu a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: udiv_i32:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: bnez a2, .LBB6_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB6_2:
+; RV32-ZMMUL-NEXT: tail __udivsi3
+;
+; RV64-LABEL: udiv_i32:
+; RV64: # %bb.0:
+; RV64-NEXT: divuw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: udiv_i32:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: slli a0, a0, 32
+; ZMMUL-NEXT: srli a0, a0, 32
+; ZMMUL-NEXT: bnez a2, .LBB6_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB6_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: slli a1, a1, 32
+; ZMMUL-NEXT: srli a1, a1, 32
+; ZMMUL-NEXT: call __udivdi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @sdiv_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-LABEL: sdiv_i32:
+; RV32: # %bb.0:
+; RV32-NEXT: div a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: sdiv_i32:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: bnez a2, .LBB7_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB7_2:
+; RV32-ZMMUL-NEXT: tail __divsi3
+;
+; RV64-LABEL: sdiv_i32:
+; RV64: # %bb.0:
+; RV64-NEXT: divw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: sdiv_i32:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: sext.w a0, a0
+; ZMMUL-NEXT: bnez a2, .LBB7_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB7_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: sext.w a1, a1
+; ZMMUL-NEXT: call __divdi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @urem_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-LABEL: urem_i32:
+; RV32: # %bb.0:
+; RV32-NEXT: remu a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: urem_i32:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: bnez a2, .LBB8_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB8_2:
+; RV32-ZMMUL-NEXT: tail __umodsi3
+;
+; RV64-LABEL: urem_i32:
+; RV64: # %bb.0:
+; RV64-NEXT: remuw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: urem_i32:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: slli a0, a0, 32
+; ZMMUL-NEXT: srli a0, a0, 32
+; ZMMUL-NEXT: bnez a2, .LBB8_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB8_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: slli a1, a1, 32
+; ZMMUL-NEXT: srli a1, a1, 32
+; ZMMUL-NEXT: call __umoddi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i32 @llvm.masked.urem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i32 @srem_i32(i32 %a, i32 %b, i1 %m) {
+; RV32-LABEL: srem_i32:
+; RV32: # %bb.0:
+; RV32-NEXT: rem a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: srem_i32:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: bnez a2, .LBB9_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB9_2:
+; RV32-ZMMUL-NEXT: tail __modsi3
+;
+; RV64-LABEL: srem_i32:
+; RV64: # %bb.0:
+; RV64-NEXT: remw a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: srem_i32:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: sext.w a0, a0
+; ZMMUL-NEXT: bnez a2, .LBB9_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB9_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: sext.w a1, a1
+; ZMMUL-NEXT: call __moddi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 %m)
+ ret i32 %r
+}
+
+define i64 @sdiv_i64(i64 %a, i64 %b, i1 %m) {
+; RV32-LABEL: sdiv_i64:
+; RV32: # %bb.0:
+; RV32-NEXT: andi a4, a4, 1
+; RV32-NEXT: bnez a4, .LBB10_2
+; RV32-NEXT: # %bb.1:
+; RV32-NEXT: li a2, 1
+; RV32-NEXT: .LBB10_2:
+; RV32-NEXT: addi sp, sp, -16
+; RV32-NEXT: .cfi_def_cfa_offset 16
+; RV32-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-NEXT: .cfi_offset ra, -4
+; RV32-NEXT: neg a4, a4
+; RV32-NEXT: and a3, a4, a3
+; RV32-NEXT: call __divdi3
+; RV32-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-NEXT: .cfi_restore ra
+; RV32-NEXT: addi sp, sp, 16
+; RV32-NEXT: .cfi_def_cfa_offset 0
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: sdiv_i64:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a4, a4, 1
+; RV32-ZMMUL-NEXT: bnez a4, .LBB10_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a2, 1
+; RV32-ZMMUL-NEXT: .LBB10_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: neg a4, a4
+; RV32-ZMMUL-NEXT: and a3, a4, a3
+; RV32-ZMMUL-NEXT: call __divdi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: sdiv_i64:
+; RV64: # %bb.0:
+; RV64-NEXT: div a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: sdiv_i64:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: bnez a2, .LBB10_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB10_2:
+; ZMMUL-NEXT: tail __divdi3
+ %r = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @urem_i64(i64 %a, i64 %b, i1 %m) {
+; RV32-LABEL: urem_i64:
+; RV32: # %bb.0:
+; RV32-NEXT: andi a4, a4, 1
+; RV32-NEXT: bnez a4, .LBB11_2
+; RV32-NEXT: # %bb.1:
+; RV32-NEXT: li a2, 1
+; RV32-NEXT: .LBB11_2:
+; RV32-NEXT: addi sp, sp, -16
+; RV32-NEXT: .cfi_def_cfa_offset 16
+; RV32-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-NEXT: .cfi_offset ra, -4
+; RV32-NEXT: neg a4, a4
+; RV32-NEXT: and a3, a4, a3
+; RV32-NEXT: call __umoddi3
+; RV32-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-NEXT: .cfi_restore ra
+; RV32-NEXT: addi sp, sp, 16
+; RV32-NEXT: .cfi_def_cfa_offset 0
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: urem_i64:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a4, a4, 1
+; RV32-ZMMUL-NEXT: bnez a4, .LBB11_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a2, 1
+; RV32-ZMMUL-NEXT: .LBB11_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: neg a4, a4
+; RV32-ZMMUL-NEXT: and a3, a4, a3
+; RV32-ZMMUL-NEXT: call __umoddi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: urem_i64:
+; RV64: # %bb.0:
+; RV64-NEXT: remu a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: urem_i64:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: bnez a2, .LBB11_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB11_2:
+; ZMMUL-NEXT: tail __umoddi3
+ %r = call i64 @llvm.masked.urem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i64 @srem_i64(i64 %a, i64 %b, i1 %m) {
+; RV32-LABEL: srem_i64:
+; RV32: # %bb.0:
+; RV32-NEXT: andi a4, a4, 1
+; RV32-NEXT: bnez a4, .LBB12_2
+; RV32-NEXT: # %bb.1:
+; RV32-NEXT: li a2, 1
+; RV32-NEXT: .LBB12_2:
+; RV32-NEXT: addi sp, sp, -16
+; RV32-NEXT: .cfi_def_cfa_offset 16
+; RV32-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-NEXT: .cfi_offset ra, -4
+; RV32-NEXT: neg a4, a4
+; RV32-NEXT: and a3, a4, a3
+; RV32-NEXT: call __moddi3
+; RV32-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-NEXT: .cfi_restore ra
+; RV32-NEXT: addi sp, sp, 16
+; RV32-NEXT: .cfi_def_cfa_offset 0
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: srem_i64:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a4, a4, 1
+; RV32-ZMMUL-NEXT: bnez a4, .LBB12_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a2, 1
+; RV32-ZMMUL-NEXT: .LBB12_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: neg a4, a4
+; RV32-ZMMUL-NEXT: and a3, a4, a3
+; RV32-ZMMUL-NEXT: call __moddi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: srem_i64:
+; RV64: # %bb.0:
+; RV64-NEXT: rem a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: srem_i64:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: bnez a2, .LBB12_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB12_2:
+; ZMMUL-NEXT: tail __moddi3
+ %r = call i64 @llvm.masked.srem.i64(i64 %a, i64 %b, i1 %m)
+ ret i64 %r
+}
+
+define i32 @sdiv_false_mask(i32 %a, i32 %b) {
+; RV32-LABEL: sdiv_false_mask:
+; RV32: # %bb.0:
+; RV32-NEXT: li a0, 0
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: sdiv_false_mask:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: sdiv_false_mask:
+; RV64: # %bb.0:
+; RV64-NEXT: li a0, 0
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: sdiv_false_mask:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: ret
+ %r = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1 false)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define i32 @udiv_known_zero(i32 %a, i32 %b, i1 %m) {
+; RV32-LABEL: udiv_known_zero:
+; RV32: # %bb.0:
+; RV32-NEXT: li a0, 0
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: udiv_known_zero:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: not a1, a2
+; RV32-ZMMUL-NEXT: andi a1, a1, 1
+; RV32-ZMMUL-NEXT: call __udivsi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: udiv_known_zero:
+; RV64: # %bb.0:
+; RV64-NEXT: li a0, 0
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: udiv_known_zero:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: slli a0, a0, 32
+; ZMMUL-NEXT: not a1, a2
+; ZMMUL-NEXT: srli a0, a0, 32
+; ZMMUL-NEXT: andi a1, a1, 1
+; ZMMUL-NEXT: call __udivdi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %z = and i32 %b, 0
+ %r = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %z, i1 %m)
+ %f = freeze i32 %r
+ ret i32 %f
+}
+
+define i24 @odd_guard_i24(i24 %a, i24 %b, i1 %m) {
+; RV32-LABEL: odd_guard_i24:
+; RV32: # %bb.0:
+; RV32-NEXT: andi a2, a2, 1
+; RV32-NEXT: slli a0, a0, 8
+; RV32-NEXT: srai a0, a0, 8
+; RV32-NEXT: bnez a2, .LBB15_2
+; RV32-NEXT: # %bb.1:
+; RV32-NEXT: li a1, 1
+; RV32-NEXT: .LBB15_2:
+; RV32-NEXT: slli a1, a1, 8
+; RV32-NEXT: srai a1, a1, 8
+; RV32-NEXT: div a0, a0, a1
+; RV32-NEXT: ret
+;
+; RV32-ZMMUL-LABEL: odd_guard_i24:
+; RV32-ZMMUL: # %bb.0:
+; RV32-ZMMUL-NEXT: andi a2, a2, 1
+; RV32-ZMMUL-NEXT: slli a0, a0, 8
+; RV32-ZMMUL-NEXT: srai a0, a0, 8
+; RV32-ZMMUL-NEXT: bnez a2, .LBB15_2
+; RV32-ZMMUL-NEXT: # %bb.1:
+; RV32-ZMMUL-NEXT: li a1, 1
+; RV32-ZMMUL-NEXT: .LBB15_2:
+; RV32-ZMMUL-NEXT: addi sp, sp, -16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; RV32-ZMMUL-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
+; RV32-ZMMUL-NEXT: .cfi_offset ra, -4
+; RV32-ZMMUL-NEXT: slli a1, a1, 8
+; RV32-ZMMUL-NEXT: srai a1, a1, 8
+; RV32-ZMMUL-NEXT: call __divsi3
+; RV32-ZMMUL-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
+; RV32-ZMMUL-NEXT: .cfi_restore ra
+; RV32-ZMMUL-NEXT: addi sp, sp, 16
+; RV32-ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; RV32-ZMMUL-NEXT: ret
+;
+; RV64-LABEL: odd_guard_i24:
+; RV64: # %bb.0:
+; RV64-NEXT: andi a2, a2, 1
+; RV64-NEXT: slli a0, a0, 40
+; RV64-NEXT: srai a0, a0, 40
+; RV64-NEXT: bnez a2, .LBB15_2
+; RV64-NEXT: # %bb.1:
+; RV64-NEXT: li a1, 1
+; RV64-NEXT: .LBB15_2:
+; RV64-NEXT: slli a1, a1, 40
+; RV64-NEXT: srai a1, a1, 40
+; RV64-NEXT: div a0, a0, a1
+; RV64-NEXT: ret
+;
+; ZMMUL-LABEL: odd_guard_i24:
+; ZMMUL: # %bb.0:
+; ZMMUL-NEXT: andi a2, a2, 1
+; ZMMUL-NEXT: slli a0, a0, 40
+; ZMMUL-NEXT: srai a0, a0, 40
+; ZMMUL-NEXT: bnez a2, .LBB15_2
+; ZMMUL-NEXT: # %bb.1:
+; ZMMUL-NEXT: li a1, 1
+; ZMMUL-NEXT: .LBB15_2:
+; ZMMUL-NEXT: addi sp, sp, -16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 16
+; ZMMUL-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
+; ZMMUL-NEXT: .cfi_offset ra, -8
+; ZMMUL-NEXT: slli a1, a1, 40
+; ZMMUL-NEXT: srai a1, a1, 40
+; ZMMUL-NEXT: call __divdi3
+; ZMMUL-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
+; ZMMUL-NEXT: .cfi_restore ra
+; ZMMUL-NEXT: addi sp, sp, 16
+; ZMMUL-NEXT: .cfi_def_cfa_offset 0
+; ZMMUL-NEXT: ret
+ %r = call i24 @llvm.masked.sdiv.i24(i24 %a, i24 %b, i1 %m)
+ ret i24 %r
+}
diff --git a/llvm/test/CodeGen/RISCV/rvv/masked-divrem-zve.ll b/llvm/test/CodeGen/RISCV/rvv/masked-divrem-zve.ll
new file mode 100644
index 00000000000000..a90403e91abde4
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/masked-divrem-zve.ll
@@ -0,0 +1,208 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv64 -mattr=+zve32x -riscv-v-vector-bits-min=128 < %s | FileCheck %s --check-prefix=ZVE32X
+; RUN: llc -mtriple=riscv64 -mattr=+m,+zve32x -riscv-v-vector-bits-min=128 < %s | FileCheck %s --check-prefix=RV64MZVE32X
+; RUN: llc -mtriple=riscv64 -mattr=+zve64x -riscv-v-vector-bits-min=128 < %s | FileCheck %s --check-prefix=ZVE64X
+; RUN: llc -mtriple=riscv32 -mattr=+m,+v -riscv-v-vector-bits-min=128 < %s | FileCheck %s --check-prefix=RV32MV
+
+define <2 x i32> @sdiv_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+; ZVE32X-LABEL: sdiv_v2i32:
+; ZVE32X: # %bb.0:
+; ZVE32X-NEXT: vsetivli zero, 2, e32, m1, ta, ma
+; ZVE32X-NEXT: vdiv.vv v8, v8, v9
+; ZVE32X-NEXT: ret
+;
+; RV64MZVE32X-LABEL: sdiv_v2i32:
+; RV64MZVE32X: # %bb.0:
+; RV64MZVE32X-NEXT: vsetivli zero, 2, e32, m1, ta, ma
+; RV64MZVE32X-NEXT: vdiv.vv v8, v8, v9
+; RV64MZVE32X-NEXT: ret
+;
+; ZVE64X-LABEL: sdiv_v2i32:
+; ZVE64X: # %bb.0:
+; ZVE64X-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
+; ZVE64X-NEXT: vdiv.vv v8, v8, v9
+; ZVE64X-NEXT: ret
+;
+; RV32MV-LABEL: sdiv_v2i32:
+; RV32MV: # %bb.0:
+; RV32MV-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
+; RV32MV-NEXT: vdiv.vv v8, v8, v9
+; RV32MV-NEXT: ret
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i32> @urem_v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m) {
+; ZVE32X-LABEL: urem_v2i32:
+; ZVE32X: # %bb.0:
+; ZVE32X-NEXT: vsetivli zero, 2, e32, m1, ta, ma
+; ZVE32X-NEXT: vremu.vv v8, v8, v9
+; ZVE32X-NEXT: ret
+;
+; RV64MZVE32X-LABEL: urem_v2i32:
+; RV64MZVE32X: # %bb.0:
+; RV64MZVE32X-NEXT: vsetivli zero, 2, e32, m1, ta, ma
+; RV64MZVE32X-NEXT: vremu.vv v8, v8, v9
+; RV64MZVE32X-NEXT: ret
+;
+; ZVE64X-LABEL: urem_v2i32:
+; ZVE64X: # %bb.0:
+; ZVE64X-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
+; ZVE64X-NEXT: vremu.vv v8, v8, v9
+; ZVE64X-NEXT: ret
+;
+; RV32MV-LABEL: urem_v2i32:
+; RV32MV: # %bb.0:
+; RV32MV-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
+; RV32MV-NEXT: vremu.vv v8, v8, v9
+; RV32MV-NEXT: ret
+ %r = call <2 x i32> @llvm.masked.urem.v2i32(<2 x i32> %a, <2 x i32> %b, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <vscale x 2 x i32> @udiv_nxv2i32(<vscale x 2 x i32> %a, <vscale x 2 x i32> %b, <vscale x 2 x i1> %m) {
+; ZVE32X-LABEL: udiv_nxv2i32:
+; ZVE32X: # %bb.0:
+; ZVE32X-NEXT: vsetvli a0, zero, e32, m1, ta, ma
+; ZVE32X-NEXT: vdivu.vv v8, v8, v9
+; ZVE32X-NEXT: ret
+;
+; RV64MZVE32X-LABEL: udiv_nxv2i32:
+; RV64MZVE32X: # %bb.0:
+; RV64MZVE32X-NEXT: vsetvli a0, zero, e32, m1, ta, ma
+; RV64MZVE32X-NEXT: vdivu.vv v8, v8, v9
+; RV64MZVE32X-NEXT: ret
+;
+; ZVE64X-LABEL: udiv_nxv2i32:
+; ZVE64X: # %bb.0:
+; ZVE64X-NEXT: vsetvli a0, zero, e32, m1, ta, ma
+; ZVE64X-NEXT: vdivu.vv v8, v8, v9
+; ZVE64X-NEXT: ret
+;
+; RV32MV-LABEL: udiv_nxv2i32:
+; RV32MV: # %bb.0:
+; RV32MV-NEXT: vsetvli a0, zero, e32, m1, ta, ma
+; RV32MV-NEXT: vdivu.vv v8, v8, v9
+; RV32MV-NEXT: ret
+ %r = call <vscale x 2 x i32> @llvm.masked.udiv.nxv2i32(<vscale x 2 x i32> %a, <vscale x 2 x i32> %b, <vscale x 2 x i1> %m)
+ ret <vscale x 2 x i32> %r
+}
+
+define <2 x i32> @sdiv_v2i32_zero_lane(<2 x i32> %a, i32 %y, <2 x i1> %m) {
+; ZVE32X-LABEL: sdiv_v2i32_zero_lane:
+; ZVE32X: # %bb.0:
+; ZVE32X-NEXT: vsetivli zero, 2, e32, m1, ta, ma
+; ZVE32X-NEXT: vmv.v.i v9, 0
+; ZVE32X-NEXT: vslide1down.vx v9, v9, a0
+; ZVE32X-NEXT: vdiv.vv v8, v8, v9
+; ZVE32X-NEXT: ret
+;
+; RV64MZVE32X-LABEL: sdiv_v2i32_zero_lane:
+; RV64MZVE32X: # %bb.0:
+; RV64MZVE32X-NEXT: vsetivli zero, 2, e32, m1, ta, ma
+; RV64MZVE32X-NEXT: vmv.v.i v9, 0
+; RV64MZVE32X-NEXT: vslide1down.vx v9, v9, a0
+; RV64MZVE32X-NEXT: vdiv.vv v8, v8, v9
+; RV64MZVE32X-NEXT: ret
+;
+; ZVE64X-LABEL: sdiv_v2i32_zero_lane:
+; ZVE64X: # %bb.0:
+; ZVE64X-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
+; ZVE64X-NEXT: vmv.v.i v9, 0
+; ZVE64X-NEXT: vslide1down.vx v9, v9, a0
+; ZVE64X-NEXT: vdiv.vv v8, v8, v9
+; ZVE64X-NEXT: ret
+;
+; RV32MV-LABEL: sdiv_v2i32_zero_lane:
+; RV32MV: # %bb.0:
+; RV32MV-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
+; RV32MV-NEXT: vmv.v.i v9, 0
+; RV32MV-NEXT: vslide1down.vx v9, v9, a0
+; RV32MV-NEXT: vdiv.vv v8, v8, v9
+; RV32MV-NEXT: ret
+ %ins = insertelement <2 x i32> <i32 0, i32 poison>, i32 %y, i64 1
+ %r = call <2 x i32> @llvm.masked.sdiv.v2i32(<2 x i32> %a, <2 x i32> %ins, <2 x i1> %m)
+ ret <2 x i32> %r
+}
+
+define <2 x i64> @sdiv_v2i64(<2 x i64> %a, <2 x i64> %b, <2 x i1> %m) {
+; ZVE32X-LABEL: sdiv_v2i64:
+; ZVE32X: # %bb.0:
+; ZVE32X-NEXT: addi sp, sp, -48
+; ZVE32X-NEXT: .cfi_def_cfa_offset 48
+; ZVE32X-NEXT: sd ra, 40(sp) # 8-byte Folded Spill
+; ZVE32X-NEXT: sd s0, 32(sp) # 8-byte Folded Spill
+; ZVE32X-NEXT: sd s1, 24(sp) # 8-byte Folded Spill
+; ZVE32X-NEXT: sd s2, 16(sp) # 8-byte Folded Spill
+; ZVE32X-NEXT: .cfi_offset ra, -8
+; ZVE32X-NEXT: .cfi_offset s0, -16
+; ZVE32X-NEXT: .cfi_offset s1, -24
+; ZVE32X-NEXT: .cfi_offset s2, -32
+; ZVE32X-NEXT: csrr a4, vlenb
+; ZVE32X-NEXT: sub sp, sp, a4
+; ZVE32X-NEXT: .cfi_escape 0x0f, 0x0d, 0x72, 0x00, 0x11, 0x30, 0x22, 0x11, 0x01, 0x92, 0xa2, 0x38, 0x00, 0x1e, 0x22 # sp + 48 + 1 * vlenb
+; ZVE32X-NEXT: mv s0, a3
+; ZVE32X-NEXT: mv s1, a1
+; ZVE32X-NEXT: addi a1, sp, 16
+; ZVE32X-NEXT: vs1r.v v0, (a1) # vscale x 8-byte Folded Spill
+; ZVE32X-NEXT: vsetivli zero, 2, e8, mf4, ta, ma
+; ZVE32X-NEXT: vfirst.m a1, v0
+; ZVE32X-NEXT: beqz a1, .LBB4_2
+; ZVE32X-NEXT: # %bb.1:
+; ZVE32X-NEXT: li a2, 1
+; ZVE32X-NEXT: .LBB4_2:
+; ZVE32X-NEXT: mv a1, a2
+; ZVE32X-NEXT: call __divdi3
+; ZVE32X-NEXT: vsetivli zero, 2, e8, mf4, ta, ma
+; ZVE32X-NEXT: vmv.v.i v8, 0
+; ZVE32X-NEXT: addi a1, sp, 16
+; ZVE32X-NEXT: vl1r.v v0, (a1) # vscale x 8-byte Folded Reload
+; ZVE32X-NEXT: vmerge.vim v8, v8, 1, v0
+; ZVE32X-NEXT: vslidedown.vi v8, v8, 1
+; ZVE32X-NEXT: vmv.x.s a1, v8
+; ZVE32X-NEXT: andi a1, a1, 1
+; ZVE32X-NEXT: mv s2, a0
+; ZVE32X-NEXT: bnez a1, .LBB4_4
+; ZVE32X-NEXT: # %bb.3:
+; ZVE32X-NEXT: li s0, 1
+; ZVE32X-NEXT: .LBB4_4:
+; ZVE32X-NEXT: mv a0, s1
+; ZVE32X-NEXT: mv a1, s0
+; ZVE32X-NEXT: call __divdi3
+; ZVE32X-NEXT: mv a1, a0
+; ZVE32X-NEXT: mv a0, s2
+; ZVE32X-NEXT: csrr a2, vlenb
+; ZVE32X-NEXT: add sp, sp, a2
+; ZVE32X-NEXT: .cfi_def_cfa sp, 48
+; ZVE32X-NEXT: ld ra, 40(sp) # 8-byte Folded Reload
+; ZVE32X-NEXT: ld s0, 32(sp) # 8-byte Folded Reload
+; ZVE32X-NEXT: ld s1, 24(sp) # 8-byte Folded Reload
+; ZVE32X-NEXT: ld s2, 16(sp) # 8-byte Folded Reload
+; ZVE32X-NEXT: .cfi_restore ra
+; ZVE32X-NEXT: .cfi_restore s0
+; ZVE32X-NEXT: .cfi_restore s1
+; ZVE32X-NEXT: .cfi_restore s2
+; ZVE32X-NEXT: addi sp, sp, 48
+; ZVE32X-NEXT: .cfi_def_cfa_offset 0
+; ZVE32X-NEXT: ret
+;
+; RV64MZVE32X-LABEL: sdiv_v2i64:
+; RV64MZVE32X: # %bb.0:
+; RV64MZVE32X-NEXT: div a0, a0, a2
+; RV64MZVE32X-NEXT: div a1, a1, a3
+; RV64MZVE32X-NEXT: ret
+;
+; ZVE64X-LABEL: sdiv_v2i64:
+; ZVE64X: # %bb.0:
+; ZVE64X-NEXT: vsetivli zero, 2, e64, m1, ta, ma
+; ZVE64X-NEXT: vdiv.vv v8, v8, v9
+; ZVE64X-NEXT: ret
+;
+; RV32MV-LABEL: sdiv_v2i64:
+; RV32MV: # %bb.0:
+; RV32MV-NEXT: vsetivli zero, 2, e64, m1, ta, ma
+; RV32MV-NEXT: vdiv.vv v8, v8, v9
+; RV32MV-NEXT: ret
+ %r = call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> %a, <2 x i64> %b, <2 x i1> %m)
+ ret <2 x i64> %r
+}
diff --git a/llvm/test/CodeGen/RISCV/rvv/masked-sdiv.ll b/llvm/test/CodeGen/RISCV/rvv/masked-sdiv.ll
index a0becb9b11dc0e..4ab6b02e748ca5 100644
--- a/llvm/test/CodeGen/RISCV/rvv/masked-sdiv.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/masked-sdiv.ll
@@ -5,7 +5,7 @@ define <vscale x 8 x i8> @sdiv_nxv8i8(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y
; CHECK-LABEL: sdiv_nxv8i8:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e8, m1, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 8 x i8> @llvm.masked.sdiv(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i8> %res
@@ -15,7 +15,7 @@ define <vscale x 4 x i16> @sdiv_nxv4i16(<vscale x 4 x i16> %x, <vscale x 4 x i16
; CHECK-LABEL: sdiv_nxv4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m1, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 4 x i16> @llvm.masked.sdiv(<vscale x 4 x i16> %x, <vscale x 4 x i16> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i16> %res
@@ -25,7 +25,7 @@ define <vscale x 4 x i32> @sdiv_nxv4i32(<vscale x 4 x i32> %x, <vscale x 4 x i32
; CHECK-LABEL: sdiv_nxv4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m2, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 4 x i32> @llvm.masked.sdiv(<vscale x 4 x i32> %x, <vscale x 4 x i32> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i32> %res
@@ -35,7 +35,7 @@ define <vscale x 8 x i32> @sdiv_nxv8i32(<vscale x 8 x i32> %x, <vscale x 8 x i32
; CHECK-LABEL: sdiv_nxv8i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v12, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v12
; CHECK-NEXT: ret
%res = call <vscale x 8 x i32> @llvm.masked.sdiv(<vscale x 8 x i32> %x, <vscale x 8 x i32> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i32> %res
@@ -45,7 +45,7 @@ define <vscale x 8 x i32> @sdiv_nxv8i32_splat_rhs(<vscale x 8 x i32> %x, i32 %y,
; CHECK-LABEL: sdiv_nxv8i32_splat_rhs:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a1, zero, e32, m4, ta, ma
-; CHECK-NEXT: vdiv.vx v8, v8, a0, v0.t
+; CHECK-NEXT: vdiv.vx v8, v8, a0
; CHECK-NEXT: ret
%head = insertelement <vscale x 8 x i32> poison, i32 %y, i32 0
%splat = shufflevector <vscale x 8 x i32> %head, <vscale x 8 x i32> poison, <vscale x 8 x i32> zeroinitializer
@@ -57,7 +57,7 @@ define <vscale x 8 x i16> @sdiv_nxv8i16(<vscale x 8 x i16> %x, <vscale x 8 x i16
; CHECK-LABEL: sdiv_nxv8i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m2, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 8 x i16> @llvm.masked.sdiv(<vscale x 8 x i16> %x, <vscale x 8 x i16> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i16> %res
@@ -67,7 +67,7 @@ define <vscale x 2 x i64> @sdiv_nxv2i64(<vscale x 2 x i64> %x, <vscale x 2 x i64
; CHECK-LABEL: sdiv_nxv2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 2 x i64> @llvm.masked.sdiv(<vscale x 2 x i64> %x, <vscale x 2 x i64> %y, <vscale x 2 x i1> %m)
ret <vscale x 2 x i64> %res
@@ -77,7 +77,7 @@ define <4 x i32> @sdiv_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; CHECK-LABEL: sdiv_v4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i32> @llvm.masked.sdiv(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m)
ret <4 x i32> %res
@@ -87,7 +87,7 @@ define <2 x i64> @sdiv_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; CHECK-LABEL: sdiv_v2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i64> @llvm.masked.sdiv(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m)
ret <2 x i64> %res
@@ -97,7 +97,7 @@ define <4 x i64> @sdiv_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; CHECK-LABEL: sdiv_v4i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e64, m2, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <4 x i64> @llvm.masked.sdiv(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m)
ret <4 x i64> %res
@@ -107,7 +107,7 @@ define <2 x i32> @sdiv_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; CHECK-LABEL: sdiv_v2i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i32> @llvm.masked.sdiv(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m)
ret <2 x i32> %res
@@ -117,7 +117,7 @@ define <4 x i16> @sdiv_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; CHECK-LABEL: sdiv_v4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i16> @llvm.masked.sdiv(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m)
ret <4 x i16> %res
@@ -127,7 +127,7 @@ define <1 x i64> @sdiv_v1i64(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; CHECK-LABEL: sdiv_v1i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 1, e64, m1, ta, ma
-; CHECK-NEXT: vdiv.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdiv.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <1 x i64> @llvm.masked.sdiv(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m)
ret <1 x i64> %res
@@ -229,15 +229,13 @@ define <3 x i10> @sdiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-NEXT: vslide1down.vx v9, v9, a3
; CHECK-NEXT: vslide1down.vx v8, v8, a2
; CHECK-NEXT: vslide1down.vx v9, v9, a1
-; CHECK-NEXT: vmv.v.i v10, 7
; CHECK-NEXT: vslidedown.vi v8, v8, 1
; CHECK-NEXT: vslidedown.vi v9, v9, 1
-; CHECK-NEXT: vmand.mm v0, v0, v10
; CHECK-NEXT: vsll.vi v8, v8, 6
; CHECK-NEXT: vsll.vi v9, v9, 6
; CHECK-NEXT: vsra.vi v8, v8, 6
; CHECK-NEXT: vsra.vi v9, v9, 6
-; CHECK-NEXT: vdiv.vv v8, v9, v8, v0.t
+; CHECK-NEXT: vdiv.vv v8, v9, v8
; CHECK-NEXT: vmv.x.s a1, v8
; CHECK-NEXT: vslidedown.vi v9, v8, 1
; CHECK-NEXT: vmv.x.s a2, v9
diff --git a/llvm/test/CodeGen/RISCV/rvv/masked-srem.ll b/llvm/test/CodeGen/RISCV/rvv/masked-srem.ll
index 6dcba35a110ee6..cd7cb92b722c45 100644
--- a/llvm/test/CodeGen/RISCV/rvv/masked-srem.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/masked-srem.ll
@@ -5,7 +5,7 @@ define <vscale x 8 x i8> @srem_nxv8i8(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y
; CHECK-LABEL: srem_nxv8i8:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e8, m1, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 8 x i8> @llvm.masked.srem(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i8> %res
@@ -15,7 +15,7 @@ define <vscale x 4 x i16> @srem_nxv4i16(<vscale x 4 x i16> %x, <vscale x 4 x i16
; CHECK-LABEL: srem_nxv4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m1, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 4 x i16> @llvm.masked.srem(<vscale x 4 x i16> %x, <vscale x 4 x i16> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i16> %res
@@ -25,7 +25,7 @@ define <vscale x 4 x i32> @srem_nxv4i32(<vscale x 4 x i32> %x, <vscale x 4 x i32
; CHECK-LABEL: srem_nxv4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m2, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 4 x i32> @llvm.masked.srem(<vscale x 4 x i32> %x, <vscale x 4 x i32> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i32> %res
@@ -35,7 +35,7 @@ define <vscale x 8 x i32> @srem_nxv8i32(<vscale x 8 x i32> %x, <vscale x 8 x i32
; CHECK-LABEL: srem_nxv8i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v12, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v12
; CHECK-NEXT: ret
%res = call <vscale x 8 x i32> @llvm.masked.srem(<vscale x 8 x i32> %x, <vscale x 8 x i32> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i32> %res
@@ -45,7 +45,7 @@ define <vscale x 8 x i32> @srem_nxv8i32_splat_rhs(<vscale x 8 x i32> %x, i32 %y,
; CHECK-LABEL: srem_nxv8i32_splat_rhs:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a1, zero, e32, m4, ta, ma
-; CHECK-NEXT: vrem.vx v8, v8, a0, v0.t
+; CHECK-NEXT: vrem.vx v8, v8, a0
; CHECK-NEXT: ret
%head = insertelement <vscale x 8 x i32> poison, i32 %y, i32 0
%splat = shufflevector <vscale x 8 x i32> %head, <vscale x 8 x i32> poison, <vscale x 8 x i32> zeroinitializer
@@ -57,7 +57,7 @@ define <vscale x 8 x i16> @srem_nxv8i16(<vscale x 8 x i16> %x, <vscale x 8 x i16
; CHECK-LABEL: srem_nxv8i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m2, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 8 x i16> @llvm.masked.srem(<vscale x 8 x i16> %x, <vscale x 8 x i16> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i16> %res
@@ -67,7 +67,7 @@ define <vscale x 2 x i64> @srem_nxv2i64(<vscale x 2 x i64> %x, <vscale x 2 x i64
; CHECK-LABEL: srem_nxv2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 2 x i64> @llvm.masked.srem(<vscale x 2 x i64> %x, <vscale x 2 x i64> %y, <vscale x 2 x i1> %m)
ret <vscale x 2 x i64> %res
@@ -77,7 +77,7 @@ define <4 x i32> @srem_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; CHECK-LABEL: srem_v4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i32> @llvm.masked.srem(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m)
ret <4 x i32> %res
@@ -87,7 +87,7 @@ define <2 x i64> @srem_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; CHECK-LABEL: srem_v2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i64> @llvm.masked.srem(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m)
ret <2 x i64> %res
@@ -97,7 +97,7 @@ define <4 x i64> @srem_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; CHECK-LABEL: srem_v4i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e64, m2, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <4 x i64> @llvm.masked.srem(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m)
ret <4 x i64> %res
@@ -107,7 +107,7 @@ define <2 x i32> @srem_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; CHECK-LABEL: srem_v2i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i32> @llvm.masked.srem(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m)
ret <2 x i32> %res
@@ -117,7 +117,7 @@ define <4 x i16> @srem_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; CHECK-LABEL: srem_v4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i16> @llvm.masked.srem(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m)
ret <4 x i16> %res
@@ -127,7 +127,7 @@ define <1 x i64> @srem_v1i64(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; CHECK-LABEL: srem_v1i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 1, e64, m1, ta, ma
-; CHECK-NEXT: vrem.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vrem.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <1 x i64> @llvm.masked.srem(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m)
ret <1 x i64> %res
@@ -229,15 +229,13 @@ define <3 x i10> @srem_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-NEXT: vslide1down.vx v9, v9, a3
; CHECK-NEXT: vslide1down.vx v8, v8, a2
; CHECK-NEXT: vslide1down.vx v9, v9, a1
-; CHECK-NEXT: vmv.v.i v10, 7
; CHECK-NEXT: vslidedown.vi v8, v8, 1
; CHECK-NEXT: vslidedown.vi v9, v9, 1
-; CHECK-NEXT: vmand.mm v0, v0, v10
; CHECK-NEXT: vsll.vi v8, v8, 6
; CHECK-NEXT: vsll.vi v9, v9, 6
; CHECK-NEXT: vsra.vi v8, v8, 6
; CHECK-NEXT: vsra.vi v9, v9, 6
-; CHECK-NEXT: vrem.vv v8, v9, v8, v0.t
+; CHECK-NEXT: vrem.vv v8, v9, v8
; CHECK-NEXT: vmv.x.s a1, v8
; CHECK-NEXT: vslidedown.vi v9, v8, 1
; CHECK-NEXT: vmv.x.s a2, v9
diff --git a/llvm/test/CodeGen/RISCV/rvv/masked-udiv.ll b/llvm/test/CodeGen/RISCV/rvv/masked-udiv.ll
index 111353e3d5419f..34f7b8adfa0703 100644
--- a/llvm/test/CodeGen/RISCV/rvv/masked-udiv.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/masked-udiv.ll
@@ -5,7 +5,7 @@ define <vscale x 8 x i8> @udiv_nxv8i8(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y
; CHECK-LABEL: udiv_nxv8i8:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e8, m1, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 8 x i8> @llvm.masked.udiv(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i8> %res
@@ -15,7 +15,7 @@ define <vscale x 4 x i16> @udiv_nxv4i16(<vscale x 4 x i16> %x, <vscale x 4 x i16
; CHECK-LABEL: udiv_nxv4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m1, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 4 x i16> @llvm.masked.udiv(<vscale x 4 x i16> %x, <vscale x 4 x i16> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i16> %res
@@ -25,7 +25,7 @@ define <vscale x 4 x i32> @udiv_nxv4i32(<vscale x 4 x i32> %x, <vscale x 4 x i32
; CHECK-LABEL: udiv_nxv4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m2, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 4 x i32> @llvm.masked.udiv(<vscale x 4 x i32> %x, <vscale x 4 x i32> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i32> %res
@@ -35,7 +35,7 @@ define <vscale x 8 x i32> @udiv_nxv8i32(<vscale x 8 x i32> %x, <vscale x 8 x i32
; CHECK-LABEL: udiv_nxv8i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v12, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v12
; CHECK-NEXT: ret
%res = call <vscale x 8 x i32> @llvm.masked.udiv(<vscale x 8 x i32> %x, <vscale x 8 x i32> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i32> %res
@@ -45,7 +45,7 @@ define <vscale x 8 x i32> @udiv_nxv8i32_splat_rhs(<vscale x 8 x i32> %x, i32 %y,
; CHECK-LABEL: udiv_nxv8i32_splat_rhs:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a1, zero, e32, m4, ta, ma
-; CHECK-NEXT: vdivu.vx v8, v8, a0, v0.t
+; CHECK-NEXT: vdivu.vx v8, v8, a0
; CHECK-NEXT: ret
%head = insertelement <vscale x 8 x i32> poison, i32 %y, i32 0
%splat = shufflevector <vscale x 8 x i32> %head, <vscale x 8 x i32> poison, <vscale x 8 x i32> zeroinitializer
@@ -57,7 +57,7 @@ define <vscale x 8 x i16> @udiv_nxv8i16(<vscale x 8 x i16> %x, <vscale x 8 x i16
; CHECK-LABEL: udiv_nxv8i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m2, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 8 x i16> @llvm.masked.udiv(<vscale x 8 x i16> %x, <vscale x 8 x i16> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i16> %res
@@ -67,7 +67,7 @@ define <vscale x 2 x i64> @udiv_nxv2i64(<vscale x 2 x i64> %x, <vscale x 2 x i64
; CHECK-LABEL: udiv_nxv2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 2 x i64> @llvm.masked.udiv(<vscale x 2 x i64> %x, <vscale x 2 x i64> %y, <vscale x 2 x i1> %m)
ret <vscale x 2 x i64> %res
@@ -77,7 +77,7 @@ define <4 x i32> @udiv_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; CHECK-LABEL: udiv_v4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i32> @llvm.masked.udiv(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m)
ret <4 x i32> %res
@@ -87,7 +87,7 @@ define <2 x i64> @udiv_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; CHECK-LABEL: udiv_v2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i64> @llvm.masked.udiv(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m)
ret <2 x i64> %res
@@ -97,7 +97,7 @@ define <4 x i64> @udiv_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; CHECK-LABEL: udiv_v4i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e64, m2, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <4 x i64> @llvm.masked.udiv(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m)
ret <4 x i64> %res
@@ -107,7 +107,7 @@ define <2 x i32> @udiv_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; CHECK-LABEL: udiv_v2i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i32> @llvm.masked.udiv(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m)
ret <2 x i32> %res
@@ -117,7 +117,7 @@ define <4 x i16> @udiv_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; CHECK-LABEL: udiv_v4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i16> @llvm.masked.udiv(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m)
ret <4 x i16> %res
@@ -127,7 +127,7 @@ define <1 x i64> @udiv_v1i64(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; CHECK-LABEL: udiv_v1i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 1, e64, m1, ta, ma
-; CHECK-NEXT: vdivu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vdivu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <1 x i64> @llvm.masked.udiv(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m)
ret <1 x i64> %res
@@ -227,16 +227,14 @@ define <3 x i10> @udiv_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-NEXT: vmv.v.x v9, a4
; CHECK-NEXT: vslide1down.vx v8, v8, a5
; CHECK-NEXT: vslide1down.vx v9, v9, a3
-; CHECK-NEXT: vmv.v.i v10, 7
; CHECK-NEXT: vslide1down.vx v8, v8, a2
; CHECK-NEXT: vslide1down.vx v9, v9, a1
-; CHECK-NEXT: vmand.mm v0, v0, v10
; CHECK-NEXT: vslidedown.vi v8, v8, 1
; CHECK-NEXT: vslidedown.vi v9, v9, 1
; CHECK-NEXT: li a1, 1023
; CHECK-NEXT: vand.vx v8, v8, a1
; CHECK-NEXT: vand.vx v9, v9, a1
-; CHECK-NEXT: vdivu.vv v8, v9, v8, v0.t
+; CHECK-NEXT: vdivu.vv v8, v9, v8
; CHECK-NEXT: vmv.x.s a1, v8
; CHECK-NEXT: vslidedown.vi v9, v8, 1
; CHECK-NEXT: vmv.x.s a2, v9
@@ -261,7 +259,7 @@ define <8 x i8> @udiv_trunc_select(<8 x i16> %x, i16 %y, <8 x i1> %m, i1 %c) {
; CHECK-LABEL: udiv_trunc_select:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma
-; CHECK-NEXT: vdivu.vx v8, v8, a0, v0.t
+; CHECK-NEXT: vdivu.vx v8, v8, a0
; CHECK-NEXT: andi a1, a1, 1
; CHECK-NEXT: vsetvli zero, zero, e8, mf2, ta, ma
; CHECK-NEXT: vmv.v.x v9, a1
diff --git a/llvm/test/CodeGen/RISCV/rvv/masked-urem.ll b/llvm/test/CodeGen/RISCV/rvv/masked-urem.ll
index dc35ac5d8567a3..abf40b9879440d 100644
--- a/llvm/test/CodeGen/RISCV/rvv/masked-urem.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/masked-urem.ll
@@ -5,7 +5,7 @@ define <vscale x 8 x i8> @urem_nxv8i8(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y
; CHECK-LABEL: urem_nxv8i8:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e8, m1, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 8 x i8> @llvm.masked.urem(<vscale x 8 x i8> %x, <vscale x 8 x i8> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i8> %res
@@ -15,7 +15,7 @@ define <vscale x 4 x i16> @urem_nxv4i16(<vscale x 4 x i16> %x, <vscale x 4 x i16
; CHECK-LABEL: urem_nxv4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m1, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <vscale x 4 x i16> @llvm.masked.urem(<vscale x 4 x i16> %x, <vscale x 4 x i16> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i16> %res
@@ -25,7 +25,7 @@ define <vscale x 4 x i32> @urem_nxv4i32(<vscale x 4 x i32> %x, <vscale x 4 x i32
; CHECK-LABEL: urem_nxv4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m2, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 4 x i32> @llvm.masked.urem(<vscale x 4 x i32> %x, <vscale x 4 x i32> %y, <vscale x 4 x i1> %m)
ret <vscale x 4 x i32> %res
@@ -35,7 +35,7 @@ define <vscale x 8 x i32> @urem_nxv8i32(<vscale x 8 x i32> %x, <vscale x 8 x i32
; CHECK-LABEL: urem_nxv8i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v12, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v12
; CHECK-NEXT: ret
%res = call <vscale x 8 x i32> @llvm.masked.urem(<vscale x 8 x i32> %x, <vscale x 8 x i32> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i32> %res
@@ -45,7 +45,7 @@ define <vscale x 8 x i32> @urem_nxv8i32_splat_rhs(<vscale x 8 x i32> %x, i32 %y,
; CHECK-LABEL: urem_nxv8i32_splat_rhs:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a1, zero, e32, m4, ta, ma
-; CHECK-NEXT: vremu.vx v8, v8, a0, v0.t
+; CHECK-NEXT: vremu.vx v8, v8, a0
; CHECK-NEXT: ret
%head = insertelement <vscale x 8 x i32> poison, i32 %y, i32 0
%splat = shufflevector <vscale x 8 x i32> %head, <vscale x 8 x i32> poison, <vscale x 8 x i32> zeroinitializer
@@ -57,7 +57,7 @@ define <vscale x 8 x i16> @urem_nxv8i16(<vscale x 8 x i16> %x, <vscale x 8 x i16
; CHECK-LABEL: urem_nxv8i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e16, m2, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 8 x i16> @llvm.masked.urem(<vscale x 8 x i16> %x, <vscale x 8 x i16> %y, <vscale x 8 x i1> %m)
ret <vscale x 8 x i16> %res
@@ -67,7 +67,7 @@ define <vscale x 2 x i64> @urem_nxv2i64(<vscale x 2 x i64> %x, <vscale x 2 x i64
; CHECK-LABEL: urem_nxv2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <vscale x 2 x i64> @llvm.masked.urem(<vscale x 2 x i64> %x, <vscale x 2 x i64> %y, <vscale x 2 x i1> %m)
ret <vscale x 2 x i64> %res
@@ -77,7 +77,7 @@ define <4 x i32> @urem_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m) {
; CHECK-LABEL: urem_v4i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i32> @llvm.masked.urem(<4 x i32> %x, <4 x i32> %y, <4 x i1> %m)
ret <4 x i32> %res
@@ -87,7 +87,7 @@ define <2 x i64> @urem_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m) {
; CHECK-LABEL: urem_v2i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i64> @llvm.masked.urem(<2 x i64> %x, <2 x i64> %y, <2 x i1> %m)
ret <2 x i64> %res
@@ -97,7 +97,7 @@ define <4 x i64> @urem_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m) {
; CHECK-LABEL: urem_v4i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e64, m2, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v10, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v10
; CHECK-NEXT: ret
%res = call <4 x i64> @llvm.masked.urem(<4 x i64> %x, <4 x i64> %y, <4 x i1> %m)
ret <4 x i64> %res
@@ -107,7 +107,7 @@ define <2 x i32> @urem_v2i32(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m) {
; CHECK-LABEL: urem_v2i32:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <2 x i32> @llvm.masked.urem(<2 x i32> %x, <2 x i32> %y, <2 x i1> %m)
ret <2 x i32> %res
@@ -117,7 +117,7 @@ define <4 x i16> @urem_v4i16(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m) {
; CHECK-LABEL: urem_v4i16:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <4 x i16> @llvm.masked.urem(<4 x i16> %x, <4 x i16> %y, <4 x i1> %m)
ret <4 x i16> %res
@@ -127,7 +127,7 @@ define <1 x i64> @urem_v1i64(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m) {
; CHECK-LABEL: urem_v1i64:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 1, e64, m1, ta, ma
-; CHECK-NEXT: vremu.vv v8, v8, v9, v0.t
+; CHECK-NEXT: vremu.vv v8, v8, v9
; CHECK-NEXT: ret
%res = call <1 x i64> @llvm.masked.urem(<1 x i64> %x, <1 x i64> %y, <1 x i1> %m)
ret <1 x i64> %res
@@ -227,16 +227,14 @@ define <3 x i10> @urem_v3i10(<3 x i10> %x, <3 x i10> %y, <3 x i1> %m) {
; CHECK-NEXT: vmv.v.x v9, a4
; CHECK-NEXT: vslide1down.vx v8, v8, a5
; CHECK-NEXT: vslide1down.vx v9, v9, a3
-; CHECK-NEXT: vmv.v.i v10, 7
; CHECK-NEXT: vslide1down.vx v8, v8, a2
; CHECK-NEXT: vslide1down.vx v9, v9, a1
-; CHECK-NEXT: vmand.mm v0, v0, v10
; CHECK-NEXT: vslidedown.vi v8, v8, 1
; CHECK-NEXT: vslidedown.vi v9, v9, 1
; CHECK-NEXT: li a1, 1023
; CHECK-NEXT: vand.vx v8, v8, a1
; CHECK-NEXT: vand.vx v9, v9, a1
-; CHECK-NEXT: vremu.vv v8, v9, v8, v0.t
+; CHECK-NEXT: vremu.vv v8, v9, v8
; CHECK-NEXT: vmv.x.s a1, v8
; CHECK-NEXT: vslidedown.vi v9, v8, 1
; CHECK-NEXT: vmv.x.s a2, v9
>From 309225a87738cbc532cb5ba7acaad559fe636bab Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 13:28:10 +0100
Subject: [PATCH 8/9] [clang][UBSan] Split signed-integer-overflow into -arith
and -divide
The signed-integer-overflow check covers two unrelated cases: overflow
of +, -, *, unary -, ++, -- and abs, and signed division overflow, i.e.
INT_MIN / -1 and INT_MIN % -1, which is emitted together with
integer-divide-by-zero. There is no way to select one without the
other. Languages that give integer division a defined result (OpenCL C
makes x / 0 and INT_MIN / -1 an unspecified value) need to treat the
division case like integer-divide-by-zero, and keep the arithmetic
overflow checks.
Following shift and implicit-integer-truncation, make
signed-integer-overflow a group of two sanitizers:
* signed-integer-overflow-arith: every signed integer overflow check
other than division, i.e. all checks that -ftrapv covers, including
__builtin_abs and friends.
* signed-integer-overflow-divide: INT_MIN / -1 and INT_MIN % -1.
Every existing spelling keeps its meaning: undefined, integer and
signed-integer-overflow still enable both checks; -fwrapv still removes
both from the expansion of undefined; -fsanitize-recover=, -trap=,
-merge=, -skip-hot-cutoff= and -annotate-debug-info= accept the group;
no_sanitize("signed-integer-overflow") and [signed-integer-overflow]
ignorelist sections still cover both, since both already expand groups.
Clear group marker bits when finalizing SanitizerArgs::Sanitizers so
disabling both leaves via an enclosing group leaves an empty set.
__has_feature(signed_integer_overflow_sanitizer) is now true if either
check is enabled, like shift_sanitizer, and there are features for the
two new checks.
The driver passes the two leaves to -cc1, so cc1 command lines change.
cc1 does not accept groups, but it keeps accepting
signed-integer-overflow, which used to be a single sanitizer, so direct
cc1 users are not broken. The runtime is unchanged: both checks keep
using their existing handlers, and the full runtime still reports both
cases under the signed-integer-overflow suppression name.
The sanitizer ordinals after signed-integer-overflow shift by two,
which allow-ubsan-check.c and cfi-vtable-check.cpp pin.
Assisted-by: Claude
---
clang/docs/ReleaseNotes.md | 7 ++
clang/docs/UndefinedBehaviorSanitizer.md | 26 ++--
clang/include/clang/Basic/Features.def | 6 +-
clang/include/clang/Basic/Sanitizers.def | 8 +-
clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 3 +-
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 12 +-
clang/lib/CodeGen/CGBuiltin.cpp | 7 +-
clang/lib/CodeGen/CGExprScalar.cpp | 50 ++++----
clang/lib/Driver/SanitizerArgs.cpp | 2 +-
clang/lib/Frontend/CompilerInvocation.cpp | 17 ++-
clang/test/CodeGen/allow-ubsan-check.c | 20 ++--
.../ubsan-signed-integer-overflow-divide.c | 113 ++++++++++++++++++
clang/test/CodeGenCXX/cfi-vtable-check.cpp | 2 +-
clang/test/Driver/amdgpu-validate-sanitize.cl | 2 +-
.../Driver/fsanitize-annotate-debug-info.c | 4 +-
clang/test/Driver/fsanitize-merge.c | 4 +-
clang/test/Driver/fsanitize-minimal-runtime.c | 12 +-
clang/test/Driver/fsanitize-recover.c | 2 +-
.../fsanitize-signed-integer-overflow.c | 50 ++++++--
clang/test/Driver/fsanitize-skip-hot-cutoff.c | 8 +-
clang/test/Driver/fsanitize-trap.c | 4 +-
clang/test/Driver/fsanitize-undefined.c | 12 +-
clang/test/Driver/fsanitize.c | 4 +-
...s_feature_undefined_behavior_sanitizer.cpp | 20 +++-
24 files changed, 300 insertions(+), 95 deletions(-)
create mode 100644 clang/test/CodeGen/ubsan-signed-integer-overflow-divide.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 5f132de54e5e3b..d7a84eface425e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1731,6 +1731,13 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
vtable, member-pointer, and `-fexperimental-call-graph-section` metadata
canonical so whole-program devirtualization works reliably. (#GH242466)
+- `-fsanitize=signed-integer-overflow` is now a group of two checks:
+ `signed-integer-overflow-divide` for signed division and remainder overflow
+ (`INT_MIN/-1` and `INT_MIN%-1`), and `signed-integer-overflow-arith` for all
+ other signed integer overflow. Every existing use of
+ `signed-integer-overflow` keeps its meaning, but the driver now passes the
+ two checks to `-cc1` instead of `signed-integer-overflow`.
+
### Python Binding Changes
- Fixed a crash (`SIGFPE`) when traversing an AST via the visitor callbacks
diff --git a/clang/docs/UndefinedBehaviorSanitizer.md b/clang/docs/UndefinedBehaviorSanitizer.md
index 25fdb741c6a2b2..f133eb8658bc2b 100644
--- a/clang/docs/UndefinedBehaviorSanitizer.md
+++ b/clang/docs/UndefinedBehaviorSanitizer.md
@@ -184,14 +184,17 @@ Available checks are:
- `-fsanitize=unsigned-shift-base`: check that an unsigned left-hand side of
a left shift operation doesn't overflow. Issues caught by this sanitizer are
not undefined behavior, but are often unintentional.
-- `-fsanitize=signed-integer-overflow`: Signed integer overflow, where the
- result of a signed integer computation cannot be represented in its type.
- This includes all the checks covered by `-ftrapv`, as well as checks for
- signed division overflow (`INT_MIN/-1`). Note that checks are still
- added even when `-fwrapv` is enabled. This sanitizer does not check for
- lossy implicit conversions performed before the computation (see
- `-fsanitize=implicit-integer-conversion`). Both of these two issues are handled
- by `-fsanitize=implicit-integer-conversion` group of checks.
+- `-fsanitize=signed-integer-overflow-arith`,
+ `-fsanitize=signed-integer-overflow-divide`: Signed integer overflow, where
+ the result of a signed integer computation cannot be represented in its type.
+ `-fsanitize=signed-integer-overflow-divide` checks signed division and
+ remainder overflow (`INT_MIN/-1` and `INT_MIN%-1`), while
+ `-fsanitize=signed-integer-overflow-arith` checks every other signed overflow
+ (all the checks covered by `-ftrapv`, i.e. `+`, `-`, `*`, unary `-`, `++`,
+ `--`, and `abs`). Note that operator checks are still added even when
+ `-fwrapv` is enabled. These sanitizers do not check for lossy implicit
+ conversions performed before the computation (see
+ `-fsanitize=implicit-integer-conversion`).
- `-fsanitize=unreachable`: If control flow reaches an unreachable
program point.
- `-fsanitize=unsigned-integer-overflow`: Unsigned integer overflow, where
@@ -242,6 +245,8 @@ You can also use the following check groups:
`nullability-assign`, and `nullability-return`. While violating
nullability does not have undefined behavior, it is often unintentional,
so UBSan offers to catch it.
+ - `-fsanitize=signed-integer-overflow`: Enables
+ `signed-integer-overflow-arith` and `signed-integer-overflow-divide`.
### Volatile
@@ -457,7 +462,10 @@ There are several limitations:
`-fsanitize-recover=signed-integer-overflow,alignment,vptr`, although
most of UBSan checks are recoverable by default.
- Check groups (like `undefined`) can't be used in suppressions file, only
- fine-grained checks are supported.
+ fine-grained checks are supported (`signed-integer-overflow` remains the
+ runtime suppression name for both `signed-integer-overflow-arith` and
+ `signed-integer-overflow-divide`, whereas compile-time ignorelists accept the
+ individual check names as well as the group).
## Security Considerations
diff --git a/clang/include/clang/Basic/Features.def b/clang/include/clang/Basic/Features.def
index cfd5bf69f938cf..532e7d0a9b66e8 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -86,8 +86,12 @@ FEATURE(shift_base_sanitizer, LangOpts.Sanitize.has(SanitizerKind::ShiftBase))
FEATURE(shift_exponent_sanitizer, LangOpts.Sanitize.has(SanitizerKind::ShiftExponent))
FEATURE(shift_sanitizer,
LangOpts.Sanitize.hasOneOf(SanitizerKind::Shift))
+FEATURE(signed_integer_overflow_arith_sanitizer,
+ LangOpts.Sanitize.has(SanitizerKind::SignedIntegerOverflowArith))
+FEATURE(signed_integer_overflow_divide_sanitizer,
+ LangOpts.Sanitize.has(SanitizerKind::SignedIntegerOverflowDivide))
FEATURE(signed_integer_overflow_sanitizer,
- LangOpts.Sanitize.has(SanitizerKind::SignedIntegerOverflow))
+ LangOpts.Sanitize.hasOneOf(SanitizerKind::SignedIntegerOverflow))
FEATURE(unreachable_sanitizer,
LangOpts.Sanitize.has(SanitizerKind::Unreachable))
FEATURE(vla_bound_sanitizer,
diff --git a/clang/include/clang/Basic/Sanitizers.def b/clang/include/clang/Basic/Sanitizers.def
index 8120c5842919b4..ceb064b7c98c9b 100644
--- a/clang/include/clang/Basic/Sanitizers.def
+++ b/clang/include/clang/Basic/Sanitizers.def
@@ -112,7 +112,13 @@ SANITIZER("returns-nonnull-attribute", ReturnsNonnullAttribute)
SANITIZER("shift-base", ShiftBase)
SANITIZER("shift-exponent", ShiftExponent)
SANITIZER_GROUP("shift", Shift, ShiftBase | ShiftExponent)
-SANITIZER("signed-integer-overflow", SignedIntegerOverflow)
+// Signed integer overflow: INT_MIN / -1 and INT_MIN % -1 (divide), and every
+// other signed integer computation (arith): +, -, *, negation, increment,
+// decrement, and the builtins that use these checks, such as __builtin_abs.
+SANITIZER("signed-integer-overflow-arith", SignedIntegerOverflowArith)
+SANITIZER("signed-integer-overflow-divide", SignedIntegerOverflowDivide)
+SANITIZER_GROUP("signed-integer-overflow", SignedIntegerOverflow,
+ SignedIntegerOverflowArith | SignedIntegerOverflowDivide)
SANITIZER("unreachable", Unreachable)
SANITIZER("vla-bound", VLABound)
SANITIZER("vptr", Vptr)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index ca8a601946ab52..20a21ec420c5f8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -1379,7 +1379,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
case Builtin::BI__builtin_abs:
case Builtin::BI__builtin_labs:
case Builtin::BI__builtin_llabs: {
- bool sanitizeOverflow = sanOpts.has(SanitizerKind::SignedIntegerOverflow);
+ bool sanitizeOverflow =
+ sanOpts.has(SanitizerKind::SignedIntegerOverflowArith);
mlir::Value arg = emitScalarExpr(e->getArg(0));
mlir::Value result;
switch (getLangOpts().getSignedOverflowBehavior()) {
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index c49459c5020fbe..1aa8a6f433e9ed 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -2310,11 +2310,11 @@ mlir::Value ScalarExprEmitter::emitMul(const BinOpInfo &ops) {
ops.compType->isSignedIntegerOrEnumerationType()) {
switch (cgf.getLangOpts().getSignedOverflowBehavior()) {
case LangOptions::SOB_Defined:
- if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflow))
+ if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflowArith))
return builder.createMul(loc, ops.lhs, ops.rhs);
[[fallthrough]];
case LangOptions::SOB_Undefined:
- if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflow))
+ if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflowArith))
return builder.createNSWMul(loc, ops.lhs, ops.rhs);
[[fallthrough]];
case LangOptions::SOB_Trapping:
@@ -2498,11 +2498,11 @@ mlir::Value ScalarExprEmitter::emitAdd(const BinOpInfo &ops) {
ops.compType->isSignedIntegerOrEnumerationType()) {
switch (cgf.getLangOpts().getSignedOverflowBehavior()) {
case LangOptions::SOB_Defined:
- if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflow))
+ if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflowArith))
return builder.createAdd(loc, ops.lhs, ops.rhs);
[[fallthrough]];
case LangOptions::SOB_Undefined:
- if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflow))
+ if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflowArith))
return builder.createNSWAdd(loc, ops.lhs, ops.rhs);
[[fallthrough]];
case LangOptions::SOB_Trapping:
@@ -2550,12 +2550,12 @@ mlir::Value ScalarExprEmitter::emitSub(const BinOpInfo &ops) {
ops.compType->isSignedIntegerOrEnumerationType()) {
switch (cgf.getLangOpts().getSignedOverflowBehavior()) {
case LangOptions::SOB_Defined: {
- if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflow))
+ if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflowArith))
return builder.createSub(loc, ops.lhs, ops.rhs);
[[fallthrough]];
}
case LangOptions::SOB_Undefined:
- if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflow))
+ if (!cgf.sanOpts.has(SanitizerKind::SignedIntegerOverflowArith))
return builder.createNSWSub(loc, ops.lhs, ops.rhs);
[[fallthrough]];
case LangOptions::SOB_Trapping:
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index fc699e37bd3147..0aff1f3b7c5bd0 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -2053,7 +2053,7 @@ static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
SmallVector<SanitizerKind::SanitizerOrdinal, 1> Ordinals;
SanitizerHandler CheckHandler;
if (SanitizeOverflow) {
- Ordinals.push_back(SanitizerKind::SO_SignedIntegerOverflow);
+ Ordinals.push_back(SanitizerKind::SO_SignedIntegerOverflowArith);
CheckHandler = SanitizerHandler::NegateOverflow;
} else
CheckHandler = SanitizerHandler::SubOverflow;
@@ -2069,7 +2069,7 @@ static Value *EmitOverflowCheckedAbs(CodeGenFunction &CGF, const CallExpr *E,
// TODO: support -ftrapv-handler.
if (SanitizeOverflow) {
- CGF.EmitCheck({{NotOverflow, SanitizerKind::SO_SignedIntegerOverflow}},
+ CGF.EmitCheck({{NotOverflow, SanitizerKind::SO_SignedIntegerOverflowArith}},
CheckHandler,
{CGF.EmitCheckSourceLocation(E->getArg(0)->getExprLoc()),
CGF.EmitCheckTypeDescriptor(E->getType())},
@@ -3328,7 +3328,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin_abs:
case Builtin::BI__builtin_labs:
case Builtin::BI__builtin_llabs: {
- bool SanitizeOverflow = SanOpts.has(SanitizerKind::SignedIntegerOverflow);
+ bool SanitizeOverflow =
+ SanOpts.has(SanitizerKind::SignedIntegerOverflowArith);
Value *Result;
switch (getLangOpts().getSignedOverflowBehavior()) {
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff79..98ad65c50722d2 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -264,7 +264,7 @@ static bool CanElideOverflowCheck(ASTContext &Ctx, const BinOpInfo &Op) {
return false;
if (Op.Ty->isSignedIntegerType() &&
- Ctx.isTypeIgnoredBySanitizer(SanitizerKind::SignedIntegerOverflow,
+ Ctx.isTypeIgnoredBySanitizer(SanitizerKind::SignedIntegerOverflowArith,
Op.Ty)) {
return true;
}
@@ -851,7 +851,7 @@ class ScalarExprEmitter
Ops.Ty->isUnsignedIntegerType()) {
const bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType();
const bool hasSan =
- isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
+ isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowArith)
: CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
switch (getOverflowBehaviorConsideringType(CGF, Ops.Ty)) {
case LangOptions::OB_Wrap:
@@ -1977,7 +1977,7 @@ void ScalarExprEmitter::EmitBinOpCheck(
if (CGF.CGM.getCodeGenOpts().SanitizeTrap.has(
SanitizerKind::UnsignedIntegerOverflow) ||
CGF.CGM.getCodeGenOpts().SanitizeTrap.has(
- SanitizerKind::SignedIntegerOverflow)) {
+ SanitizerKind::SignedIntegerOverflowArith)) {
// Only pay the cost for constructing the trap diagnostic if they are
// going to be used.
CGF.CGM.BuildTrapReason(diag::trap_ubsan_arith_overflow, TR)
@@ -3297,7 +3297,7 @@ llvm::Value *ScalarExprEmitter::EmitIncDecConsiderOverflowBehavior(
QualType Ty = E->getType();
const bool isSigned = Ty->isSignedIntegerOrEnumerationType();
const bool hasSan =
- isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
+ isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowArith)
: CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
switch (getOverflowBehaviorConsideringType(CGF, Ty)) {
@@ -4306,12 +4306,12 @@ void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
}
const auto *BO = cast<BinaryOperator>(Ops.E);
- if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow) &&
+ if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowDivide) &&
Ops.Ty->hasSignedIntegerRepresentation() &&
!IsWidenedIntegerOp(CGF.getContext(), BO->getLHS()) &&
Ops.mayHaveIntegerOverflow() &&
!CGF.getContext().isTypeIgnoredBySanitizer(
- SanitizerKind::SignedIntegerOverflow, Ops.Ty)) {
+ SanitizerKind::SignedIntegerOverflowDivide, Ops.Ty)) {
llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType());
llvm::Value *IntMin =
@@ -4321,8 +4321,8 @@ void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin);
llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne);
llvm::Value *NotOverflow = Builder.CreateOr(LHSCmp, RHSCmp, "or");
- Checks.push_back(
- std::make_pair(NotOverflow, SanitizerKind::SO_SignedIntegerOverflow));
+ Checks.push_back(std::make_pair(
+ NotOverflow, SanitizerKind::SO_SignedIntegerOverflowDivide));
}
if (Checks.size() > 0)
@@ -4331,13 +4331,14 @@ void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
{
- SanitizerDebugLocation SanScope(&CGF,
- {SanitizerKind::SO_IntegerDivideByZero,
- SanitizerKind::SO_SignedIntegerOverflow,
- SanitizerKind::SO_FloatDivideByZero},
- SanitizerHandler::DivremOverflow);
+ SanitizerDebugLocation SanScope(
+ &CGF,
+ {SanitizerKind::SO_IntegerDivideByZero,
+ SanitizerKind::SO_SignedIntegerOverflowDivide,
+ SanitizerKind::SO_FloatDivideByZero},
+ SanitizerHandler::DivremOverflow);
if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
- CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
+ CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowDivide)) &&
Ops.Ty->isIntegerType() &&
(Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) {
llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
@@ -4387,13 +4388,14 @@ Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
// Rem in C can't be a floating point type: C99 6.5.5p2.
if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
- CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
+ CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowDivide)) &&
Ops.Ty->isIntegerType() &&
(Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) {
- SanitizerDebugLocation SanScope(&CGF,
- {SanitizerKind::SO_IntegerDivideByZero,
- SanitizerKind::SO_SignedIntegerOverflow},
- SanitizerHandler::DivremOverflow);
+ SanitizerDebugLocation SanScope(
+ &CGF,
+ {SanitizerKind::SO_IntegerDivideByZero,
+ SanitizerKind::SO_SignedIntegerOverflowDivide},
+ SanitizerHandler::DivremOverflow);
llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false);
}
@@ -4443,7 +4445,7 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
OpID |= 1;
SanitizerDebugLocation SanScope(&CGF,
- {SanitizerKind::SO_SignedIntegerOverflow,
+ {SanitizerKind::SO_SignedIntegerOverflowArith,
SanitizerKind::SO_UnsignedIntegerOverflow},
OverflowKind);
llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
@@ -4463,10 +4465,10 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
// arithmetic to result in a trap due to the OverflowBehaviorType attribute
// which describes overflow behavior on a per-type basis.
if (isSigned) {
- if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) {
+ if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowArith)) {
llvm::Value *NotOf = Builder.CreateNot(overflow);
EmitBinOpCheck(
- std::make_pair(NotOf, SanitizerKind::SO_SignedIntegerOverflow),
+ std::make_pair(NotOf, SanitizerKind::SO_SignedIntegerOverflowArith),
Ops);
} else
CGF.EmitTrapCheck(Builder.CreateNot(overflow), OverflowKind);
@@ -4803,7 +4805,7 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) {
op.Ty->isUnsignedIntegerType()) {
const bool isSigned = op.Ty->isSignedIntegerOrEnumerationType();
const bool hasSan =
- isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
+ isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowArith)
: CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
switch (getOverflowBehaviorConsideringType(CGF, op.Ty)) {
case LangOptions::OB_Wrap:
@@ -4963,7 +4965,7 @@ Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
op.Ty->isUnsignedIntegerType()) {
const bool isSigned = op.Ty->isSignedIntegerOrEnumerationType();
const bool hasSan =
- isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
+ isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflowArith)
: CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
switch (getOverflowBehaviorConsideringType(CGF, op.Ty)) {
case LangOptions::OB_Wrap:
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index 5df712cb9578d3..f65e606caf6fbf 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -1358,7 +1358,7 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
Args.hasFlag(options::OPT_fcopyprof, options::OPT_fno_copyprof, false);
// Finally, initialize the set of available and recoverable sanitizers.
- Sanitizers.Mask |= Kinds;
+ Sanitizers.Mask |= clearGroupBits(Kinds);
RecoverableSanitizers.Mask |= RecoverableKinds;
TrapSanitizers.Mask |= TrappingKinds;
assert(!(RecoverableKinds & TrappingKinds) &&
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 9af6f6c41eb756..a28a130f000500 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1414,13 +1414,24 @@ static bool parseDiagnosticLevelMask(StringRef FlagName,
return Success;
}
+/// The driver expands sanitizer groups, so cc1 only accepts single sanitizers,
+/// and the groups that used to be single sanitizers.
+static bool isFormerSingleSanitizer(StringRef Name) {
+ // Split into signed-integer-overflow-arith and
+ // signed-integer-overflow-divide.
+ return Name == "signed-integer-overflow";
+}
+
static void parseSanitizerKinds(StringRef FlagName,
const std::vector<std::string> &Sanitizers,
DiagnosticsEngine &Diags, SanitizerSet &S) {
for (const auto &Sanitizer : Sanitizers) {
- SanitizerMask K = parseSanitizerValue(Sanitizer, /*AllowGroups=*/false);
+ bool IsGroup = isFormerSingleSanitizer(Sanitizer);
+ SanitizerMask K = parseSanitizerValue(Sanitizer, /*AllowGroups=*/IsGroup);
if (K == SanitizerMask())
Diags.Report(diag::err_drv_invalid_value) << FlagName << Sanitizer;
+ else if (IsGroup)
+ S.Mask |= expandSanitizerGroups(K) & ~K;
else
S.set(K, true);
}
@@ -1438,7 +1449,9 @@ parseSanitizerWeightedKinds(StringRef FlagName,
DiagnosticsEngine &Diags) {
SanitizerMaskCutoffs Cutoffs;
for (const auto &Sanitizer : Sanitizers) {
- if (!parseSanitizerWeightedValue(Sanitizer, /*AllowGroups=*/false, Cutoffs))
+ bool AllowGroups =
+ isFormerSingleSanitizer(StringRef(Sanitizer).split('=').first);
+ if (!parseSanitizerWeightedValue(Sanitizer, AllowGroups, Cutoffs))
Diags.Report(diag::err_drv_invalid_value) << FlagName << Sanitizer;
}
return Cutoffs;
diff --git a/clang/test/CodeGen/allow-ubsan-check.c b/clang/test/CodeGen/allow-ubsan-check.c
index 53fb5a47bef831..f457a3b7b32b25 100644
--- a/clang/test/CodeGen/allow-ubsan-check.c
+++ b/clang/test/CodeGen/allow-ubsan-check.c
@@ -10,7 +10,7 @@
//
// 27 == SO_IntegerDivideByZero
//
-// 41 == SO_SignedIntegerOverflow
+// 42 == SO_SignedIntegerOverflowDivide
// CHECK-LABEL: define dso_local noundef i32 @div(
// CHECK-SAME: i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
@@ -21,7 +21,7 @@
// CHECK-NEXT: [[TMP3:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 27), !nosanitize [[META6]]
// CHECK-NEXT: [[TMP4:%.*]] = xor i1 [[TMP3]], true, !nosanitize [[META6]]
// CHECK-NEXT: [[TMP5:%.*]] = or i1 [[TMP0]], [[TMP4]], !nosanitize [[META6]]
-// CHECK-NEXT: [[TMP6:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 41), !nosanitize [[META6]]
+// CHECK-NEXT: [[TMP6:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 42), !nosanitize [[META6]]
// CHECK-NEXT: [[TMP7:%.*]] = xor i1 [[TMP6]], true, !nosanitize [[META6]]
// CHECK-NEXT: [[TMP8:%.*]] = or i1 [[OR]], [[TMP7]], !nosanitize [[META6]]
// CHECK-NEXT: [[TMP9:%.*]] = and i1 [[TMP5]], [[TMP8]], !nosanitize [[META6]]
@@ -45,7 +45,7 @@
// TR-NEXT: [[TMP3:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 27), !nosanitize [[META6]]
// TR-NEXT: [[TMP4:%.*]] = xor i1 [[TMP3]], true, !nosanitize [[META6]]
// TR-NEXT: [[TMP5:%.*]] = or i1 [[TMP0]], [[TMP4]], !nosanitize [[META6]]
-// TR-NEXT: [[TMP6:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 41), !nosanitize [[META6]]
+// TR-NEXT: [[TMP6:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 42), !nosanitize [[META6]]
// TR-NEXT: [[TMP7:%.*]] = xor i1 [[TMP6]], true, !nosanitize [[META6]]
// TR-NEXT: [[TMP8:%.*]] = or i1 [[OR]], [[TMP7]], !nosanitize [[META6]]
// TR-NEXT: [[TMP9:%.*]] = and i1 [[TMP5]], [[TMP8]], !nosanitize [[META6]]
@@ -67,7 +67,7 @@
// REC-NEXT: [[TMP3:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 27), !nosanitize [[META6]]
// REC-NEXT: [[TMP4:%.*]] = xor i1 [[TMP3]], true, !nosanitize [[META6]]
// REC-NEXT: [[TMP5:%.*]] = or i1 [[TMP0]], [[TMP4]], !nosanitize [[META6]]
-// REC-NEXT: [[TMP6:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 41), !nosanitize [[META6]]
+// REC-NEXT: [[TMP6:%.*]] = tail call i1 @llvm.allow.ubsan.check(i8 42), !nosanitize [[META6]]
// REC-NEXT: [[TMP7:%.*]] = xor i1 [[TMP6]], true, !nosanitize [[META6]]
// REC-NEXT: [[TMP8:%.*]] = or i1 [[OR]], [[TMP7]], !nosanitize [[META6]]
// REC-NEXT: [[TMP9:%.*]] = and i1 [[TMP5]], [[TMP8]], !nosanitize [[META6]]
@@ -134,7 +134,7 @@ int null(int* x) {
}
//
-// 41 == SO_SignedIntegerOverflow
+// 41 == SO_SignedIntegerOverflowArith
// CHECK-LABEL: define dso_local noundef i32 @overflow(
// CHECK-SAME: i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] {
// CHECK-NEXT: [[ENTRY:.*:]]
@@ -191,7 +191,7 @@ int overflow(int x, int y) {
void use(double*);
//
-// 71 == SO_LocalBounds
+// 73 == SO_LocalBounds
// CHECK-LABEL: define dso_local double @lbounds(
// CHECK-SAME: i32 noundef [[B:%.*]], i32 noundef [[I:%.*]]) local_unnamed_addr #[[ATTR0]] {
// CHECK-NEXT: [[ENTRY:.*:]]
@@ -200,7 +200,7 @@ void use(double*);
// CHECK-NEXT: call void @use(ptr noundef nonnull [[VLA]]) #[[ATTR7:[0-9]+]]
// CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
// CHECK-NEXT: [[TMP1:%.*]] = icmp ule i64 [[TMP0]], [[IDXPROM]]
-// CHECK-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 71), !nosanitize [[META6]]
+// CHECK-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 73), !nosanitize [[META6]]
// CHECK-NEXT: [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]], !nosanitize [[META6]]
// CHECK-NEXT: br i1 [[TMP3]], label %[[TRAP:.*]], label %[[BB4:.*]]
// CHECK: [[BB4]]:
@@ -219,7 +219,7 @@ void use(double*);
// TR-NEXT: call void @use(ptr noundef nonnull [[VLA]]) #[[ATTR8:[0-9]+]]
// TR-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
// TR-NEXT: [[TMP1:%.*]] = icmp ule i64 [[TMP0]], [[IDXPROM]]
-// TR-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 71), !nosanitize [[META6]]
+// TR-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 73), !nosanitize [[META6]]
// TR-NEXT: [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]], !nosanitize [[META6]]
// TR-NEXT: br i1 [[TMP3]], label %[[TRAP:.*]], label %[[BB4:.*]]
// TR: [[BB4]]:
@@ -227,7 +227,7 @@ void use(double*);
// TR-NEXT: [[TMP5:%.*]] = load double, ptr [[ARRAYIDX]], align 8, !tbaa [[DOUBLE_TBAA10:![0-9]+]]
// TR-NEXT: ret double [[TMP5]]
// TR: [[TRAP]]:
-// TR-NEXT: call void @llvm.ubsantrap(i8 71) #[[ATTR9:[0-9]+]], !nosanitize [[META6]]
+// TR-NEXT: call void @llvm.ubsantrap(i8 73) #[[ATTR9:[0-9]+]], !nosanitize [[META6]]
// TR-NEXT: unreachable, !nosanitize [[META6]]
//
// REC-LABEL: define dso_local double @lbounds(
@@ -238,7 +238,7 @@ void use(double*);
// REC-NEXT: call void @use(ptr noundef nonnull [[VLA]]) #[[ATTR5:[0-9]+]]
// REC-NEXT: [[IDXPROM:%.*]] = sext i32 [[I]] to i64
// REC-NEXT: [[TMP1:%.*]] = icmp ule i64 [[TMP0]], [[IDXPROM]]
-// REC-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 71), !nosanitize [[META6]]
+// REC-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 73), !nosanitize [[META6]]
// REC-NEXT: [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]], !nosanitize [[META6]]
// REC-NEXT: br i1 [[TMP3]], label %[[TRAP:.*]], label %[[BB4:.*]]
// REC: [[BB4]]:
diff --git a/clang/test/CodeGen/ubsan-signed-integer-overflow-divide.c b/clang/test/CodeGen/ubsan-signed-integer-overflow-divide.c
new file mode 100644
index 00000000000000..7f38f831ab04fa
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-signed-integer-overflow-divide.c
@@ -0,0 +1,113 @@
+// signed-integer-overflow is a group of signed-integer-overflow-divide, which
+// checks INT_MIN / -1 and INT_MIN % -1, and signed-integer-overflow-arith,
+// which checks every other signed overflow.
+
+// RUN: rm -rf %t && split-file %s %t
+
+// cc1 still accepts signed-integer-overflow, which was a single sanitizer.
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow | FileCheck %t/test.c --check-prefixes=CHECK,ABS,ARITH,DIVIDE,AFATAL,DFATAL
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow-arith,signed-integer-overflow-divide | FileCheck %t/test.c --check-prefixes=CHECK,ABS,ARITH,DIVIDE,AFATAL,DFATAL
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow-arith | FileCheck %t/test.c --check-prefixes=CHECK,ABS,ARITH,NO-DIVIDE,AFATAL
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow-divide | FileCheck %t/test.c --check-prefixes=CHECK,NO-ARITH,DIVIDE,DFATAL,NO-ABS
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-recover=signed-integer-overflow | FileCheck %t/test.c --check-prefixes=CHECK,ABS,ARITH,DIVIDE,RECOVER
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow | FileCheck %t/test.c --check-prefixes=CHECK,TRAP
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-recover=signed-integer-overflow-divide -fsanitize-trap=signed-integer-overflow-arith | FileCheck %t/test.c --check-prefixes=CHECK,MIXED
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-ignorelist=%t/group.ignorelist | FileCheck %t/test.c --check-prefixes=CHECK,ABS,NO-ARITH,NO-DIVIDE
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-ignorelist=%t/divide.ignorelist | FileCheck %t/test.c --check-prefixes=CHECK,ABS,ARITH,NO-DIVIDE,AFATAL
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -O1 -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-skip-hot-cutoff=signed-integer-overflow=1.0,signed-integer-overflow-divide=0.0 | FileCheck %t/test.c --check-prefixes=CHECK,DIVIDE,DFATAL,CUTOFF
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -debug-info-kind=limited -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow -fsanitize-annotate-debug-info=signed-integer-overflow | FileCheck %t/test.c --check-prefixes=CHECK,ABS,ARITH,DIVIDE,ANNOTATE
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -ftrapv -emit-llvm -o - %t/test.c -fsanitize=signed-integer-overflow-divide | FileCheck %t/test.c --check-prefixes=TRAPV,DIVIDE,DFATAL
+
+//--- group.ignorelist
+[signed-integer-overflow]
+type:int
+
+//--- divide.ignorelist
+[signed-integer-overflow-divide]
+type:int
+
+//--- test.c
+// CHECK-LABEL: define {{.*}} @div(
+// TRAPV-LABEL: define {{.*}} @div(
+// DIVIDE: icmp ne i32 %{{.*}}, -2147483648
+// DFATAL: call void @__ubsan_handle_divrem_overflow_abort(
+// RECOVER: call void @__ubsan_handle_divrem_overflow(
+// TRAP: call void @llvm.ubsantrap(i8 3)
+// MIXED: call void @__ubsan_handle_divrem_overflow(
+// NO-DIVIDE-NOT: __ubsan_handle_divrem_overflow
+// CHECK: sdiv i32
+// TRAPV: sdiv i32
+int div(int x, int y) { return x / y; }
+
+// CHECK-LABEL: define {{.*}} @rem(
+// TRAPV-LABEL: define {{.*}} @rem(
+// DIVIDE: icmp ne i32 %{{.*}}, -2147483648
+// DFATAL: call void @__ubsan_handle_divrem_overflow_abort(
+// RECOVER: call void @__ubsan_handle_divrem_overflow(
+// TRAP: call void @llvm.ubsantrap(i8 3)
+// MIXED: call void @__ubsan_handle_divrem_overflow(
+// NO-DIVIDE-NOT: __ubsan_handle_divrem_overflow
+// CHECK: srem i32
+// TRAPV: srem i32
+int rem(int x, int y) { return x % y; }
+
+// CHECK-LABEL: define {{.*}} @add(
+// TRAPV-LABEL: define {{.*}} @add(
+// ARITH: call { i32, i1 } @llvm.sadd.with.overflow.i32(
+// AFATAL: call void @__ubsan_handle_add_overflow_abort(
+// RECOVER: call void @__ubsan_handle_add_overflow(
+// TRAP: call void @llvm.ubsantrap(i8 0)
+// MIXED: call void @llvm.ubsantrap(i8 0)
+// TRAPV: call void @llvm.ubsantrap(i8 0)
+// NO-ARITH: add nsw i32
+// NO-ARITH-NOT: __ubsan_handle
+// CUTOFF-NOT: __ubsan_handle
+// CHECK: ret i32
+int add(int x, int y) { return x + y; }
+
+// CHECK-LABEL: define {{.*}} @neg(
+// TRAPV-LABEL: define {{.*}} @neg(
+// ARITH: call { i32, i1 } @llvm.ssub.with.overflow.i32(
+// AFATAL: call void @__ubsan_handle_negate_overflow_abort(
+// TRAPV: call void @llvm.ubsantrap(i8 21)
+// NO-ARITH: sub nsw i32 0,
+// NO-ARITH-NOT: __ubsan_handle
+// CUTOFF-NOT: __ubsan_handle
+// CHECK: ret i32
+int neg(int x) { return -x; }
+
+// Type ignorelists do not apply to __builtin_abs.
+// CHECK-LABEL: define {{.*}} @abs_(
+// TRAPV-LABEL: define {{.*}} @abs_(
+// ABS: call { i32, i1 } @llvm.ssub.with.overflow.i32(
+// AFATAL: call void @__ubsan_handle_negate_overflow_abort(
+// ANNOTATE: call void @__ubsan_handle_negate_overflow_abort({{.*}} !dbg
+// TRAPV: call void @llvm.ubsantrap(i8 21)
+// NO-ABS: call i32 @llvm.abs.i32(i32 %{{.*}}, i1 true)
+// NO-ABS-NOT: __ubsan_handle
+// CUTOFF-NOT: __ubsan_handle
+// CHECK: ret i32
+int abs_(int x) { return __builtin_abs(x); }
+
+// The group in no_sanitize turns off both leaves.
+// CHECK-LABEL: define {{.*}} @div_no_sanitize_group(
+// TRAPV-LABEL: define {{.*}} @div_no_sanitize_group(
+// CHECK-NOT: __ubsan_handle
+// CHECK-NOT: ubsantrap
+// TRAPV-NOT: __ubsan_handle
+// CHECK: ret i32
+int div_no_sanitize_group(int x, int y) __attribute__((no_sanitize("signed-integer-overflow")));
+__attribute__((no_sanitize("signed-integer-overflow")))
+int div_no_sanitize_group(int x, int y) { return x / y + x * y; }
+
+// A leaf in no_sanitize turns off just that leaf.
+// CHECK-LABEL: define {{.*}} @div_no_sanitize_divide(
+// TRAPV-LABEL: define {{.*}} @div_no_sanitize_divide(
+// NO-DIVIDE-NOT: __ubsan_handle_divrem_overflow
+// DIVIDE-NOT: __ubsan_handle_divrem_overflow
+// CHECK: sdiv i32
+// TRAPV: sdiv i32
+// ARITH: @llvm.smul.with.overflow.i32(
+// CHECK: ret i32
+__attribute__((no_sanitize("signed-integer-overflow-divide")))
+int div_no_sanitize_divide(int x, int y) { return x / y + x * y; }
diff --git a/clang/test/CodeGenCXX/cfi-vtable-check.cpp b/clang/test/CodeGenCXX/cfi-vtable-check.cpp
index 8074350f6a51eb..0cf341efcf7b95 100644
--- a/clang/test/CodeGenCXX/cfi-vtable-check.cpp
+++ b/clang/test/CodeGenCXX/cfi-vtable-check.cpp
@@ -42,7 +42,7 @@ struct S {
// TRAP_CUTOFF-NEXT: [[TMP0:%.*]] = load ptr, ptr [[S_ADDR]], align 8
// TRAP_CUTOFF-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[TMP0]], align 8
// TRAP_CUTOFF-NEXT: [[TMP1:%.*]] = call i1 @llvm.type.test(ptr [[VTABLE]], metadata !"_ZTS1S"), !nosanitize [[META1:![0-9]+]]
-// TRAP_CUTOFF-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 54), !nosanitize [[META1]]
+// TRAP_CUTOFF-NEXT: [[TMP2:%.*]] = call i1 @llvm.allow.ubsan.check(i8 56), !nosanitize [[META1]]
// TRAP_CUTOFF-NEXT: [[TMP3:%.*]] = xor i1 [[TMP2]], true, !nosanitize [[META1]]
// TRAP_CUTOFF-NEXT: [[TMP4:%.*]] = or i1 [[TMP1]], [[TMP3]], !nosanitize [[META1]]
// TRAP_CUTOFF-NEXT: br i1 [[TMP4]], label %[[CONT:.*]], label %[[TRAP:.*]], !prof [[PROF2:![0-9]+]], !nosanitize [[META1]]
diff --git a/clang/test/Driver/amdgpu-validate-sanitize.cl b/clang/test/Driver/amdgpu-validate-sanitize.cl
index 53379f9558a0f5..07056f469b2af6 100644
--- a/clang/test/Driver/amdgpu-validate-sanitize.cl
+++ b/clang/test/Driver/amdgpu-validate-sanitize.cl
@@ -33,7 +33,7 @@
// CHECK-SAME: "-fsanitize=address"
-// GENERIC: "-fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion,local-bounds,alloc-token" "-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion" "-fsanitize-trap=local-bounds" "-fsanitize-merge=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound"
+// GENERIC: "-fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion,local-bounds,alloc-token" "-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion" "-fsanitize-trap=local-bounds" "-fsanitize-merge=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// FIXME: Should not be forwarding argument
diff --git a/clang/test/Driver/fsanitize-annotate-debug-info.c b/clang/test/Driver/fsanitize-annotate-debug-info.c
index 27ffe950579d37..e71efbedd1d3ce 100644
--- a/clang/test/Driver/fsanitize-annotate-debug-info.c
+++ b/clang/test/Driver/fsanitize-annotate-debug-info.c
@@ -8,7 +8,7 @@
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-annotate-debug-info=signed-integer-overflow -fsanitize-annotate-debug-info %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fsanitize-annotate-debug-info=bool -fsanitize-annotate-debug-info=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fsanitize-annotate-debug-info=undefined -fsanitize-annotate-debug-info=bool %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO
-// CHECK-UNDEFINED-PSEUDO: "-fsanitize-annotate-debug-info=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound"
+// CHECK-UNDEFINED-PSEUDO: "-fsanitize-annotate-debug-info=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// The trailing arguments (-fsanitize-annotate-debug-info -fno-sanitize-annotate-debug-info=signed-integer-overflow) take precedence
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fsanitize-annotate-debug-info -fno-sanitize-annotate-debug-info=signed-integer-overflow %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO2
@@ -37,7 +37,7 @@
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-annotate-debug-info -fsanitize-annotate-debug-info=undefined -fno-sanitize-annotate-debug-info=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO4
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-annotate-debug-info=signed-integer-overflow -fsanitize-annotate-debug-info -fno-sanitize-annotate-debug-info=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO4
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-annotate-debug-info=signed-integer-overflow -fsanitize-annotate-debug-info=undefined -fno-sanitize-annotate-debug-info=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO4
-// CHECK-UNDEFINED-PSEUDO4: "-fsanitize-annotate-debug-info=array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound"
+// CHECK-UNDEFINED-PSEUDO4: "-fsanitize-annotate-debug-info=array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// The trailing arguments (-fno-sanitize-annotate-debug-info -fsanitize-annotate-debug-info=alignment,null) take precedence
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-annotate-debug-info=undefined -fsanitize-annotate-debug-info=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-PSEUDO5
diff --git a/clang/test/Driver/fsanitize-merge.c b/clang/test/Driver/fsanitize-merge.c
index 936da5bd34a801..3936f1391f5954 100644
--- a/clang/test/Driver/fsanitize-merge.c
+++ b/clang/test/Driver/fsanitize-merge.c
@@ -9,7 +9,7 @@
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-merge=signed-integer-overflow -fsanitize-merge %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fsanitize-merge=bool -fsanitize-merge=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fsanitize-merge=undefined -fsanitize-merge=bool %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE
-// CHECK-UNDEFINED-MERGE: "-fsanitize-merge=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound"
+// CHECK-UNDEFINED-MERGE: "-fsanitize-merge=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// The trailing arguments (-fsanitize-merge -fno-sanitize-merge=signed-integer-overflow) take precedence
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-merge=signed-integer-overflow %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE2
@@ -40,7 +40,7 @@
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-merge -fsanitize-merge=undefined -fno-sanitize-merge=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE4
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-merge=signed-integer-overflow -fsanitize-merge -fno-sanitize-merge=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE4
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-merge=signed-integer-overflow -fsanitize-merge=undefined -fno-sanitize-merge=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE4
-// CHECK-UNDEFINED-MERGE4: "-fsanitize-merge=array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound"
+// CHECK-UNDEFINED-MERGE4: "-fsanitize-merge=array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// The trailing arguments (-fno-sanitize-merge -fsanitize-merge=alignment,null) take precedence
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-trap=undefined -fno-sanitize-merge -fsanitize-merge=alignment,null %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-MERGE5
diff --git a/clang/test/Driver/fsanitize-minimal-runtime.c b/clang/test/Driver/fsanitize-minimal-runtime.c
index 32714dfb806dd7..55b79f26c8be9a 100644
--- a/clang/test/Driver/fsanitize-minimal-runtime.c
+++ b/clang/test/Driver/fsanitize-minimal-runtime.c
@@ -5,29 +5,29 @@
// CHECK-TSAN-MINIMAL: error: invalid argument '-fsanitize-minimal-runtime' not allowed with '-fsanitize=thread'
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-MINIMAL
-// CHECK-UBSAN-MINIMAL: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
+// CHECK-UBSAN-MINIMAL: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
// CHECK-UBSAN-MINIMAL: "-fsanitize-minimal-runtime"
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-minimal-runtime -fsanitize-handler-preserve-all-regs %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-MINIMAL-PRESERVE-X86-64
-// CHECK-UBSAN-MINIMAL-PRESERVE-X86-64: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
+// CHECK-UBSAN-MINIMAL-PRESERVE-X86-64: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
// CHECK-UBSAN-MINIMAL-PRESERVE-X86-64: "-fsanitize-minimal-runtime"
// CHECK-UBSAN-MINIMAL-PRESERVE-X86-64: "-fsanitize-handler-preserve-all-regs
// RUN: %clang --target=aarch64-linux-gnu -fsanitize=undefined -fsanitize-minimal-runtime -fsanitize-handler-preserve-all-regs %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-MINIMAL-PRESERVE-AARCH64
-// CHECK-UBSAN-MINIMAL-PRESERVE-AARCH64: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
+// CHECK-UBSAN-MINIMAL-PRESERVE-AARCH64: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
// CHECK-UBSAN-MINIMAL-PRESERVE-AARCH64: "-fsanitize-minimal-runtime"
// CHECK-UBSAN-MINIMAL-PRESERVE-AARCH64: "-fsanitize-handler-preserve-all-regs
// RUN: %clang --target=i386-linux-gnu -fsanitize=undefined -fsanitize-minimal-runtime -fsanitize-handler-preserve-all-regs %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-MINIMAL-PRESERVE-I386
-// CHECK-UBSAN-MINIMAL-PRESERVE-I386: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
+// CHECK-UBSAN-MINIMAL-PRESERVE-I386: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
// CHECK-UBSAN-MINIMAL-PRESERVE-I386: "-fsanitize-minimal-runtime"
// CHECK-UBSAN-MINIMAL-PRESERVE-I386-NOT: "-fsanitize-handler-preserve-all-regs
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=integer -fsanitize-trap=integer %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-INTSAN-TRAP
-// CHECK-INTSAN-TRAP: "-fsanitize-trap=integer-divide-by-zero,shift-base,shift-exponent,signed-integer-overflow,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change"
+// CHECK-INTSAN-TRAP: "-fsanitize-trap=integer-divide-by-zero,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change"
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=integer -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-INTSAN-MINIMAL
-// CHECK-INTSAN-MINIMAL: "-fsanitize=integer-divide-by-zero,shift-base,shift-exponent,signed-integer-overflow,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change"
+// CHECK-INTSAN-MINIMAL: "-fsanitize=integer-divide-by-zero,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change"
// CHECK-INTSAN-MINIMAL: "-fsanitize-minimal-runtime"
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=implicit-conversion -fsanitize-trap=implicit-conversion %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-IMPL-CONV-TRAP
diff --git a/clang/test/Driver/fsanitize-recover.c b/clang/test/Driver/fsanitize-recover.c
index 2f2d793ddd3792..7cb52cca489566 100644
--- a/clang/test/Driver/fsanitize-recover.c
+++ b/clang/test/Driver/fsanitize-recover.c
@@ -5,7 +5,7 @@
// RUN: %clang --target=x86_64-linux-gnu %s -fsanitize=undefined -fno-sanitize-recover=undefined -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-RECOVER-UBSAN
// RUN: %clang --target=x86_64-linux-gnu %s -fsanitize=undefined -fno-sanitize-recover=all -fsanitize-recover=thread -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-RECOVER-UBSAN
// RUN: %clang --target=x86_64-linux-gnu %s -fsanitize=undefined -fsanitize-recover=all -fno-sanitize-recover=undefined -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-RECOVER-UBSAN
-// CHECK-RECOVER-UBSAN: "-fsanitize-recover={{((signed-integer-overflow|integer-divide-by-zero|function|shift-base|shift-exponent|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){16}"}}
+// CHECK-RECOVER-UBSAN: "-fsanitize-recover={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|function|shift-base|shift-exponent|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){17}"}}
// CHECK-NO-RECOVER-UBSAN-NOT: -fsanitize-recover
// RUN: %clang --target=x86_64-linux-gnu %s -fsanitize=undefined -fno-sanitize-recover=all -fsanitize-recover=object-size,shift-base -### 2>&1 | FileCheck %s --check-prefix=CHECK-PARTIAL-RECOVER
diff --git a/clang/test/Driver/fsanitize-signed-integer-overflow.c b/clang/test/Driver/fsanitize-signed-integer-overflow.c
index 4a9345493b6aa8..7fbbb5db7cb814 100644
--- a/clang/test/Driver/fsanitize-signed-integer-overflow.c
+++ b/clang/test/Driver/fsanitize-signed-integer-overflow.c
@@ -1,28 +1,60 @@
+/// -fsanitize=signed-integer-overflow is a group of
+/// signed-integer-overflow-arith and signed-integer-overflow-divide.
+
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow-arith,signed-integer-overflow-divide %s 2>&1 | FileCheck %s
+// CHECK: "-fsanitize=signed-integer-overflow-arith,signed-integer-overflow-divide"
+// CHECK: "-fsanitize-recover=signed-integer-overflow-arith,signed-integer-overflow-divide"
+
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow-arith %s 2>&1 | FileCheck %s --check-prefix=ARITH
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow -fno-sanitize=signed-integer-overflow-divide %s 2>&1 | FileCheck %s --check-prefix=ARITH
+// ARITH: "-fsanitize=signed-integer-overflow-arith"
+// ARITH: "-fsanitize-recover=signed-integer-overflow-arith"
+
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow-divide %s 2>&1 | FileCheck %s --check-prefix=DIVIDE
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow -fno-sanitize=signed-integer-overflow-arith %s 2>&1 | FileCheck %s --check-prefix=DIVIDE
+// DIVIDE: "-fsanitize=signed-integer-overflow-divide"
+// DIVIDE: "-fsanitize-recover=signed-integer-overflow-divide"
+
+/// The recover, trap and merge lists accept the group and the leaves.
+// RUN: %clang -### --target=x86_64-linux -fsanitize=signed-integer-overflow -fno-sanitize-recover=signed-integer-overflow -fsanitize-recover=signed-integer-overflow-divide -fsanitize-trap=signed-integer-overflow-arith -fno-sanitize-merge=signed-integer-overflow %s 2>&1 | FileCheck %s --check-prefix=MIXED
+// MIXED: "-fsanitize=signed-integer-overflow-arith,signed-integer-overflow-divide"
+// MIXED: "-fsanitize-recover=signed-integer-overflow-divide"
+// MIXED: "-fsanitize-trap=signed-integer-overflow-arith"
+// MIXED-NOT: "-fsanitize-merge
+
/// When -fwrapv (implied by -fno-strict-overflow) is enabled,
/// -fsanitize=undefined does not expand to signed-integer-overflow.
/// -fsanitize=signed-integer-overflow is unaffected by -fwrapv.
// RUN: %clang -### --target=x86_64-linux -fwrapv -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s
-// CHECK: -fsanitize=signed-integer-overflow
-// CHECK: -fsanitize-recover=signed-integer-overflow
// RUN: %clang -### --target=x86_64-linux -fno-strict-overflow -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=EXCLUDE
// RUN: %clang -### --target=x86_64-linux -fstrict-overflow -fwrapv -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=EXCLUDE
// EXCLUDE: -fsanitize=alignment,array-bounds,
-// EXCLUDE-NOT: signed-integer-overflow,
-// EXCLUDE: -fsanitize-recover=alignment,array-bounds,
-// EXCLUDE-SAME: signed-integer-overflow
+// EXCLUDE-NOT: {{[=,]}}signed-integer-overflow
+// EXCLUDE: -fsanitize-recover=alignment,array-bounds,
+// EXCLUDE-NOT: {{[=,]}}signed-integer-overflow
// RUN: %clang -### --target=x86_64-linux -fwrapv -fsanitize=undefined -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s --check-prefix=INCLUDE
// RUN: %clang -### --target=x86_64-linux -fno-strict-overflow -fno-sanitize=signed-integer-overflow -fsanitize=undefined -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s --check-prefix=INCLUDE
// INCLUDE: -fsanitize=alignment,array-bounds,
-// INCLUDE-SAME: signed-integer-overflow
+// INCLUDE-SAME: signed-integer-overflow-arith,signed-integer-overflow-divide,
// INCLUDE: -fsanitize-recover=alignment,array-bounds,
-// INCLUDE-SAME: signed-integer-overflow
+// INCLUDE-SAME: signed-integer-overflow-arith,signed-integer-overflow-divide,
/// -fsanitize-trap=undefined expands to signed-integer-overflow regardless of -fwrapv.
// RUN: %clang -### --target=x86_64-linux -fwrapv -fsanitize=undefined -fsanitize=signed-integer-overflow -fsanitize-trap=undefined %s 2>&1 | FileCheck %s --check-prefix=INCLUDE-TRAP
// INCLUDE-TRAP: -fsanitize=alignment,array-bounds,
-// INCLUDE-TRAP-SAME: signed-integer-overflow
+// INCLUDE-TRAP-SAME: signed-integer-overflow-arith,signed-integer-overflow-divide,
// INCLUDE-TRAP: -fsanitize-trap=alignment,array-bounds,
-// INCLUDE-TRAP-SAME: signed-integer-overflow
+// INCLUDE-TRAP-SAME: signed-integer-overflow-arith,signed-integer-overflow-divide,
+
+/// Disabling both leaves clears the group so no empty -fsanitize= or sanitizer
+/// preprocessor define remains.
+// RUN: %clang -### --target=x86_64-unknown-netbsd -fsanitize=signed-integer-overflow -fno-sanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DISABLED
+// RUN: %clang -### --target=x86_64-unknown-netbsd -fsanitize=signed-integer-overflow -fno-sanitize=integer %s 2>&1 | FileCheck %s --check-prefix=DISABLED
+// RUN: %clang -### --target=x86_64-unknown-netbsd -fsanitize=signed-integer-overflow -fno-sanitize=signed-integer-overflow-arith,signed-integer-overflow-divide %s 2>&1 | FileCheck %s --check-prefix=DISABLED
+// DISABLED: "-cc1"
+// DISABLED-NOT: "-fsanitize=
+// DISABLED-NOT: "-D_REENTRANT"
diff --git a/clang/test/Driver/fsanitize-skip-hot-cutoff.c b/clang/test/Driver/fsanitize-skip-hot-cutoff.c
index 313ec69ca74d15..6a23ab60b37494 100644
--- a/clang/test/Driver/fsanitize-skip-hot-cutoff.c
+++ b/clang/test/Driver/fsanitize-skip-hot-cutoff.c
@@ -1,6 +1,6 @@
// -fsanitize-skip-hot-cutoff=undefined=0.5
// RUN: %clang -Werror --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-skip-hot-cutoff=undefined=0.5 %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SKIP-HOT-CUTOFF1
-// CHECK-SKIP-HOT-CUTOFF1: "-fsanitize-skip-hot-cutoff={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function)=0.5(0*),?){18}"}}
+// CHECK-SKIP-HOT-CUTOFF1: "-fsanitize-skip-hot-cutoff={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function)=0.5(0*),?){19}"}}
// No-op: no sanitizers are specified
// RUN: %clang -Werror --target=x86_64-linux-gnu -fsanitize-skip-hot-cutoff=undefined=0.5 %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SKIP-HOT-CUTOFF2
@@ -13,15 +13,15 @@
// Enable undefined, then cancel out integer using a cutoff of 0.0, then re-enable signed-integer-overflow
// RUN: %clang -Werror --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-skip-hot-cutoff=undefined=0.5,integer=0.0,signed-integer-overflow=0.7 %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SKIP-HOT-CUTOFF4
-// CHECK-SKIP-HOT-CUTOFF4: "-fsanitize-skip-hot-cutoff={{((signed-integer-overflow|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function)=0.[57]0*,?){15}"}}
+// CHECK-SKIP-HOT-CUTOFF4: "-fsanitize-skip-hot-cutoff={{((signed-integer-overflow-arith|signed-integer-overflow-divide|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function)=0.[57]0*,?){16}"}}
// Check that -fsanitize-skip-hot-cutoff=undefined=0.4 does not widen the set of -fsanitize=integer checks.
// RUN: %clang -Werror --target=x86_64-linux-gnu -fsanitize=integer -fsanitize-skip-hot-cutoff=undefined=0.4 %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SKIP-HOT-CUTOFF5
-// CHECK-SKIP-HOT-CUTOFF5: "-fsanitize-skip-hot-cutoff={{((integer-divide-by-zero|shift-base|shift-exponent|signed-integer-overflow)=0.40*,?){4}"}}
+// CHECK-SKIP-HOT-CUTOFF5: "-fsanitize-skip-hot-cutoff={{((integer-divide-by-zero|shift-base|shift-exponent|signed-integer-overflow-arith|signed-integer-overflow-divide)=0.40*,?){5}"}}
// No-op: it's allowed for the user to specify a cutoff of 0.0, though the argument is not passed along by the driver.
// RUN: %clang -Werror --target=x86_64-linux-gnu -fsanitize=undefined -fsanitize-skip-hot-cutoff=undefined=0.0 %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SKIP-HOT-CUTOFF6
-// CHECK-SKIP-HOT-CUTOFF6: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
+// CHECK-SKIP-HOT-CUTOFF6: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
// CHECK-SKIP-HOT-CUTOFF6-NOT: "-fsanitize-skip-hot-cutoff"
// Invalid: bad sanitizer
diff --git a/clang/test/Driver/fsanitize-trap.c b/clang/test/Driver/fsanitize-trap.c
index 10f17b52636cbd..8ab791d36ab4e0 100644
--- a/clang/test/Driver/fsanitize-trap.c
+++ b/clang/test/Driver/fsanitize-trap.c
@@ -5,8 +5,8 @@
// RUN: %clang --target=x86_64-linux-gnu -fsanitize-undefined-trap-on-error -fsanitize=undefined-trap %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP
// RUN: %clang --target=x86_64-linux-gnu -fsanitize-trap -fsanitize=undefined-trap %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-TRAP
// CHECK-UNDEFINED-TRAP-NOT: -fsanitize-recover
-// CHECK-UNDEFINED-TRAP: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
-// CHECK-UNDEFINED-TRAP: "-fsanitize-trap=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound"
+// CHECK-UNDEFINED-TRAP: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
+// CHECK-UNDEFINED-TRAP: "-fsanitize-trap=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// CHECK-UNDEFINED-TRAP2: "-fsanitize-trap=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,unreachable,vla-bound"
// Disabling every member of an enabled group must not leave a stale group bit
diff --git a/clang/test/Driver/fsanitize-undefined.c b/clang/test/Driver/fsanitize-undefined.c
index 79b8f06fa78cfe..b3ecaec3b55999 100644
--- a/clang/test/Driver/fsanitize-undefined.c
+++ b/clang/test/Driver/fsanitize-undefined.c
@@ -1,11 +1,11 @@
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED
-// CHECK-UNDEFINED: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){18}"}}
+// CHECK-UNDEFINED: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){19}"}}
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=thread,undefined -fno-sanitize=thread -fno-sanitize=float-cast-overflow,vptr,bool,builtin,enum %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-PARTIAL-UNDEFINED
-// CHECK-PARTIAL-UNDEFINED: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|array-bounds|returns-nonnull-attribute|nonnull-attribute),?){14}"}}
+// CHECK-PARTIAL-UNDEFINED: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|array-bounds|returns-nonnull-attribute|nonnull-attribute),?){15}"}}
// RUN: %clang --target=x86_64-apple-darwin10 -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UNDEFINED-DARWIN
-// CHECK-UNDEFINED-DARWIN: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){18}"}}
+// CHECK-UNDEFINED-DARWIN: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){19}"}}
// RUN: %clang --target=i386-pc-win32 -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefixes=CHECK-UNDEFINED-WIN32,CHECK-UNDEFINED-MSVC
// RUN: %clang --target=i386-pc-win32 -fsanitize=undefined -x c++ %s -### 2>&1 | FileCheck %s --check-prefixes=CHECK-UNDEFINED-WIN32,CHECK-UNDEFINED-WIN-CXX,CHECK-UNDEFINED-MSVC
@@ -16,8 +16,8 @@
// CHECK-UNDEFINED-WIN64: "--dependent-lib={{[^"]*}}ubsan_standalone{{(-x86_64)?}}.lib"
// CHECK-UNDEFINED-WIN64-MINGW: "--dependent-lib={{[^"]*}}libclang_rt.ubsan_standalone{{(-x86_64)?}}.a"
// CHECK-UNDEFINED-WIN-CXX: "--dependent-lib={{[^"]*}}ubsan_standalone_cxx{{[^"]*}}.lib"
-// CHECK-UNDEFINED-MSVC-SAME: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
-// CHECK-UNDEFINED-WIN64-MINGW-SAME: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){18}"}}
+// CHECK-UNDEFINED-MSVC-SAME: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
+// CHECK-UNDEFINED-WIN64-MINGW-SAME: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute|function),?){19}"}}
// RUN: %clang --target=i386-pc-win32 -fsanitize-coverage=bb %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-COVERAGE-WIN32
// CHECK-COVERAGE-WIN32: "--dependent-lib={{[^"]*}}ubsan_standalone{{(-i386)?}}.lib"
@@ -39,7 +39,7 @@
// CHECK-UNDEFINED-NO-RTTI-NOT: vptr
// RUN: %clang --target=%itanium_abi_triple -fsanitize=integer %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-INTEGER -implicit-check-not="-fsanitize-address-use-after-scope"
-// CHECK-INTEGER: "-fsanitize={{((signed-integer-overflow|unsigned-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|implicit-unsigned-integer-truncation|implicit-signed-integer-truncation|implicit-integer-sign-change|unsigned-shift-base),?){9}"}}
+// CHECK-INTEGER: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|unsigned-integer-overflow|integer-divide-by-zero|shift-base|shift-exponent|implicit-unsigned-integer-truncation|implicit-signed-integer-truncation|implicit-integer-sign-change|unsigned-shift-base),?){10}"}}
// RUN: %clang -fsanitize=implicit-integer-conversion %s -### 2>&1 | FileCheck %s --check-prefixes=CHECK-implicit-integer-conversion,CHECK-implicit-integer-conversion-RECOVER
// RUN: %clang -fsanitize=implicit-integer-conversion -fsanitize-recover=implicit-integer-conversion %s -### 2>&1 | FileCheck %s --check-prefixes=CHECK-implicit-integer-conversion,CHECK-implicit-integer-conversion-RECOVER
diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c
index 32f212d6dcfbc3..c871a90ec5aff7 100644
--- a/clang/test/Driver/fsanitize.c
+++ b/clang/test/Driver/fsanitize.c
@@ -400,7 +400,7 @@
// CHECK-ASAN-IOS: -fsanitize=address
// RUN: %clang --target=i386-pc-openbsd -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-OPENBSD
-// CHECK-UBSAN-OPENBSD: "-fsanitize={{((signed-integer-overflow|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){18}"}}
+// CHECK-UBSAN-OPENBSD: "-fsanitize={{((signed-integer-overflow-arith|signed-integer-overflow-divide|integer-divide-by-zero|function|shift-base|shift-exponent|unreachable|return|vla-bound|alignment|null|pointer-overflow|float-cast-overflow|array-bounds|enum|bool|builtin|returns-nonnull-attribute|nonnull-attribute),?){19}"}}
// RUN: not %clang --target=i386-pc-openbsd -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-OPENBSD
// CHECK-ASAN-OPENBSD: unsupported option '-fsanitize=address' for target 'i386-pc-openbsd'
@@ -645,4 +645,4 @@
// CHECK-UBSAN-FUNCTION-TARGET-DAG: error: unsupported option '-fsanitize=function' for target 'x86_64-sie-ps5'
// CHECK-UBSAN-FUNCTION-MEXECUTE-ONLY-DAG: error: invalid argument '-fsanitize=function' not allowed with '-mexecute-only'
// CHECK-UBSAN-FUNCTION-MPURE-CODE-DAG: error: invalid argument '-fsanitize=function' not allowed with '-mpure-code'
-// CHECK-UBSAN-UNDEFINED: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound),?){17}"}}
+// CHECK-UBSAN-UNDEFINED: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow-arith|signed-integer-overflow-divide|unreachable|vla-bound),?){18}"}}
diff --git a/clang/test/Lexer/has_feature_undefined_behavior_sanitizer.cpp b/clang/test/Lexer/has_feature_undefined_behavior_sanitizer.cpp
index 8e668745a58444..89660426e09ba0 100644
--- a/clang/test/Lexer/has_feature_undefined_behavior_sanitizer.cpp
+++ b/clang/test/Lexer/has_feature_undefined_behavior_sanitizer.cpp
@@ -16,7 +16,9 @@
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=shift-base %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT-BASE,CHECK-SHIFT %s
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=shift-exponent %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT-EXPONENT,CHECK-SHIFT %s
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=shift %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT %s
-// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SIGNED-INTEGER-OVERFLOW %s
+// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow-arith %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SIGNED-INTEGER-OVERFLOW-ARITH,CHECK-NO-SIGNED-INTEGER-OVERFLOW-DIVIDE,CHECK-SIGNED-INTEGER-OVERFLOW %s
+// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow-divide %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-NO-SIGNED-INTEGER-OVERFLOW-ARITH,CHECK-SIGNED-INTEGER-OVERFLOW-DIVIDE,CHECK-SIGNED-INTEGER-OVERFLOW %s
+// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SIGNED-INTEGER-OVERFLOW-ARITH,CHECK-SIGNED-INTEGER-OVERFLOW-DIVIDE,CHECK-SIGNED-INTEGER-OVERFLOW %s
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=unreachable %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-UNREACHABLE %s
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=vla-bound %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-VLA-BOUND %s
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=function %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-FUNCTION %s
@@ -161,6 +163,18 @@ int ShiftSanitizerEnabled();
int ShiftSanitizerDisabled();
#endif
+#if __has_feature(signed_integer_overflow_arith_sanitizer)
+int SignedIntegerOverflowArithSanitizerEnabled();
+#else
+int SignedIntegerOverflowArithSanitizerDisabled();
+#endif
+
+#if __has_feature(signed_integer_overflow_divide_sanitizer)
+int SignedIntegerOverflowDivideSanitizerEnabled();
+#else
+int SignedIntegerOverflowDivideSanitizerDisabled();
+#endif
+
#if __has_feature(signed_integer_overflow_sanitizer)
int SignedIntegerOverflowSanitizerEnabled();
#else
@@ -202,6 +216,10 @@ int FunctionSanitizerDisabled();
// CHECK-SHIFT-BASE: ShiftBaseSanitizerEnabled
// CHECK-SHIFT-EXPONENT: ShiftExponentSanitizerEnabled
// CHECK-SHIFT: ShiftSanitizerEnabled
+// CHECK-SIGNED-INTEGER-OVERFLOW-ARITH: SignedIntegerOverflowArithSanitizerEnabled
+// CHECK-NO-SIGNED-INTEGER-OVERFLOW-ARITH: SignedIntegerOverflowArithSanitizerDisabled
+// CHECK-SIGNED-INTEGER-OVERFLOW-DIVIDE: SignedIntegerOverflowDivideSanitizerEnabled
+// CHECK-NO-SIGNED-INTEGER-OVERFLOW-DIVIDE: SignedIntegerOverflowDivideSanitizerDisabled
// CHECK-SIGNED-INTEGER-OVERFLOW: SignedIntegerOverflowSanitizerEnabled
// CHECK-UNREACHABLE: UnreachableSanitizerEnabled
// CHECK-VLA-BOUND: VLABoundSanitizerEnabled
>From b4d3ea3ab4c228ffdeb7f3d43cab4de6df906867 Mon Sep 17 00:00:00 2001
From: "Gorban, Igor" <igor.gorban at intel.com>
Date: Wed, 25 Nov 2026 13:33:30 +0100
Subject: [PATCH 9/9] [clang][OpenCL] Add -f[no-]defined-int-divrem, on by
default for OpenCL
OpenCL C v3.0 s6.5.1 says that integer division by zero, and integer
division whose result is out of range (INT_MIN / -1), "will not cause an
exception but will result in an unspecified value". Clang emits a plain
sdiv/udiv/srem/urem, which LLVM IR makes immediate undefined behavior
for exactly these operands, and the optimizer uses that to delete
conforming code: in
int q = a / b;
if (b == 0)
out[1] = 7;
the store is removed at -O2, because the division lets ValueTracking
assume b != 0.
Add -f[no-]defined-int-divrem, a LangOption that is on by default for
OpenCL C and C++ for OpenCL and off for every other language. When it is
on, an integer (scalar or vector) / or % whose operands are not known to
be safe is emitted as
%nov = select (icmp ne %y, -1), true, (icmp ne %x, MIN)
%safe = select (icmp ne %y, 0), %nov, false
%q = call @llvm.masked.sdiv(%x, %y, %safe) ; unsigned: %safe = y != 0
%r = freeze %q
Checking %y before %x with select ensures a poison dividend lane cannot
poison %safe when %y is not -1, preserving refinement over the plain
instructions (especially on the guarded select(%safe, %y, 1) path where
a poison %safe would poison a non-zero divisor; a poison divisor, or a
poison dividend with %y == -1, remains undefined as in plain LLVM IR). A
masked-off operation has no undefined behavior and returns poison, and
the freeze turns that into an arbitrary but fixed value, which is what
"unspecified value" means. Targets whose division does not trap can
select the call as the plain division. The conditions that constant
operands rule out are left out of the mask, and when the divisor is a
constant that rules out both cases (for vectors, in every lane), the
plain instruction is emitted, so x / 4 is unchanged. Any other constant,
such as the address of an object, is not known to be safe. UBSan checks
are emitted exactly as before, ahead of the operation. Division of a
matrix by a scalar, complex integer division and fixed-point division
are not changed.
SPIR and SPIR-V get op(x, select(safe, y, 1)) instead. Their division
instructions (OpSDiv and friends) are undefined for these operands too,
so they cannot express the masked operation, and the consumers of SPIR
and SPIR-V modules (such as the SPIRV-LLVM-Translator) do not know the
masked intrinsics. This is decided in the frontend because the module
must not contain the intrinsics when it leaves Clang.
ClangIR gets the same guarded divisor on every target: it is the
simplest lowering CIR can express directly (cir.cmp, cir.select, and for
vectors cir.vec.cmp and cir.vec.ternary), and replacing an unspecified
value by a particular one is a valid refinement. Only the code quality
differs from the classic CodeGen on targets whose division does not
trap.
The driver forwards an explicit -f[no-]defined-int-divrem; cc1 computes
the default from the language.
Under the option, -fsanitize=undefined (and undefined-trap) no longer
enables integer-divide-by-zero and signed-integer-overflow-divide, since
the operations are no longer undefined; an explicit request, also
through another group (integer, signed-integer-overflow), still enables
them, and the recover, trap and other per-check lists are unchanged. The
driver decides whether the option is in effect for a job like cc1 does:
the last -Xclang -f[no-]defined-int-divrem, else the last
-f[no-]defined-int-divrem, else whether the input is OpenCL.
-Wdivision-by-zero says "results in an unspecified value" instead of
"is undefined" for integer operations when the option is in effect.
Constant evaluation is not changed: a division by zero is still not a
constant expression, also in OpenCL program-scope __constant
initializers.
Document the behaviour in OpenCLSupport, UsersManual and
UndefinedBehaviorSanitizer.
Assisted-by: Claude
---
clang/docs/OpenCLSupport.md | 20 ++
clang/docs/ReleaseNotes.md | 23 ++
clang/docs/UndefinedBehaviorSanitizer.md | 10 +-
clang/docs/UsersManual.md | 24 ++
.../clang/Basic/DiagnosticSemaKinds.td | 3 +-
clang/include/clang/Basic/LangOptions.def | 1 +
clang/include/clang/Driver/SanitizerArgs.h | 4 +
clang/include/clang/Options/Options.td | 9 +
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 78 ++++-
clang/lib/CodeGen/CGExprScalar.cpp | 131 +++++++-
clang/lib/Driver/SanitizerArgs.cpp | 56 +++-
clang/lib/Driver/ToolChains/Clang.cpp | 4 +
clang/lib/Sema/SemaExpr.cpp | 19 +-
.../test/CIR/CodeGen/defined-int-divrem-sve.c | 43 +++
.../CIR/CodeGenOpenCL/defined-int-divrem.cl | 173 +++++++++++
.../CodeGen/AArch64/defined-int-divrem-sve.c | 29 ++
clang/test/CodeGen/defined-int-divrem.c | 86 ++++++
.../CodeGenOpenCL/defined-int-divrem-O2.cl | 34 +++
.../test/CodeGenOpenCL/defined-int-divrem.cl | 279 ++++++++++++++++++
.../CodeGenOpenCL/ubsan-defined-int-divrem.cl | 32 ++
clang/test/Driver/amdgpu-validate-sanitize.cl | 4 +-
clang/test/Driver/defined-int-divrem.c | 17 ++
.../Driver/fsanitize-defined-int-divrem.c | 101 +++++++
clang/test/Sema/defined-int-divrem.c | 32 ++
clang/test/SemaOpenCL/division-by-zero.cl | 36 +++
25 files changed, 1231 insertions(+), 17 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/defined-int-divrem-sve.c
create mode 100644 clang/test/CIR/CodeGenOpenCL/defined-int-divrem.cl
create mode 100644 clang/test/CodeGen/AArch64/defined-int-divrem-sve.c
create mode 100644 clang/test/CodeGen/defined-int-divrem.c
create mode 100644 clang/test/CodeGenOpenCL/defined-int-divrem-O2.cl
create mode 100644 clang/test/CodeGenOpenCL/defined-int-divrem.cl
create mode 100644 clang/test/CodeGenOpenCL/ubsan-defined-int-divrem.cl
create mode 100644 clang/test/Driver/defined-int-divrem.c
create mode 100644 clang/test/Driver/fsanitize-defined-int-divrem.c
create mode 100644 clang/test/Sema/defined-int-divrem.c
create mode 100644 clang/test/SemaOpenCL/division-by-zero.cl
diff --git a/clang/docs/OpenCLSupport.md b/clang/docs/OpenCLSupport.md
index f72258743ec22b..ac27671df7ba0e 100644
--- a/clang/docs/OpenCLSupport.md
+++ b/clang/docs/OpenCLSupport.md
@@ -147,6 +147,26 @@ also {ref}`the section on the address space attribute <opencl_addrsp>`).
> $ clang -cc1 -ffake-address-space-map test.cl
> ```
+### Integer division and remainder
+
+OpenCL C v3.0 s6.5.1 says that integer division by zero, and integer division
+whose result is out of range (`INT_MIN / -1`), do not cause an exception but
+result in an unspecified value. LLVM IR `sdiv`, `udiv`, `srem` and `urem` have
+undefined behavior for these operands, so for OpenCL C and C++ for OpenCL
+Clang enables {option}`-fdefined-int-divrem` by default and emits integer `/`
+and `%` whose operands are not known to be safe as a frozen
+`llvm.masked.sdiv`, `llvm.masked.udiv`, `llvm.masked.srem` or
+`llvm.masked.urem`, whose mask is false exactly for these operands. On SPIR
+and SPIR-V targets, whose integer division instructions (such as `OpSDiv`)
+have the same undefined behavior and whose consumers do not know these
+intrinsics, Clang instead replaces the divisor by 1 for these operands. ClangIR
+(`-fclangir`) does the same on every target. Under
+{option}`-fdefined-int-divrem`, `-fsanitize=undefined` does not include the
+`integer-divide-by-zero` and `signed-integer-overflow-divide` checks; they can
+still be enabled explicitly. Constant evaluation is not affected: a division
+by zero is not a constant expression. `-fno-defined-int-divrem` restores the
+plain instructions.
+
(opencl_builtins)=
(opencl-builtins)=
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index d7a84eface425e..bed37911d4a33c 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -423,6 +423,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- New option `-fdefined-pointer-subtraction` added to preserve stable semantics
when subtracting pointers to unrelated objects.
+- Added `-f[no-]defined-int-divrem` (enabled by default for OpenCL C and C++
+ for OpenCL; see the OpenCL section below) to give integer `/` and `%` an
+ unspecified value instead of undefined behavior on division by zero and
+ `INT_MIN / -1`.
+
- Added `--print-cxx-stdlib` and `--print-cxx-stdlib-include-dirs` to print
the C++ standard library selected by the driver and the include directories
added for it.
@@ -556,6 +561,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed `-Wheader-guard` warnings pointing at `if` instead of the macro name
in `#if !defined(...)` header guards.
+- Under `-fdefined-int-divrem`, the default for OpenCL C and C++ for OpenCL,
+ `-Wdivision-by-zero` now says that an integer division or remainder by zero
+ results in an unspecified value instead of calling it undefined.
+
- Fixed a false-positive `-Wimplicit-fallthrough` warning when
consecutive switch labels share a body and are annotated with
attributes like `[[unlikely]]`. (#GH230802)
@@ -1429,6 +1438,15 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### OpenCL Specific Changes
+- Integer division and remainder by zero, and signed division overflow
+ (`INT_MIN / -1`), now give an unspecified value instead of undefined behavior,
+ as OpenCL C v3.0 s6.5.1 requires. The integer `/` and `%` operators are
+ lowered to frozen `llvm.masked.{s,u}{div,rem}` intrinsics unless the
+ operands are known to be safe. On SPIR and SPIR-V targets, whose division
+ instructions are undefined for these operands too and whose consumers do not
+ know these intrinsics, the divisor is replaced by 1 for these operands
+ instead; ClangIR (`-fclangir`) does that on every target.
+ `-fno-defined-int-divrem` restores the previous behavior.
- Extensions ``cl_khr_extended_bit_ops``, ``cl_khr_integer_dot_product``,
``cl_khr_subgroup_extended_types``, ``cl_khr_subgroup_rotate``,
``cl_khr_subgroup_shuffle``, and ``cl_khr_subgroup_shuffle_relative`` are
@@ -1737,6 +1755,11 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
other signed integer overflow. Every existing use of
`signed-integer-overflow` keeps its meaning, but the driver now passes the
two checks to `-cc1` instead of `signed-integer-overflow`.
+- Under `-fdefined-int-divrem`, which is the default for OpenCL C and C++ for
+ OpenCL, `-fsanitize=undefined` no longer includes `integer-divide-by-zero`
+ and `signed-integer-overflow-divide`, since these operations are not
+ undefined there. Enabling the checks explicitly still works as before,
+ including `-fno-sanitize-recover` and `-fsanitize-trap`.
### Python Binding Changes
diff --git a/clang/docs/UndefinedBehaviorSanitizer.md b/clang/docs/UndefinedBehaviorSanitizer.md
index f133eb8658bc2b..b573bc4260f283 100644
--- a/clang/docs/UndefinedBehaviorSanitizer.md
+++ b/clang/docs/UndefinedBehaviorSanitizer.md
@@ -139,7 +139,8 @@ Available checks are:
or the original value was positive, and the new value is negative.
Issues caught by this sanitizer are not undefined behavior,
but are often unintentional.
-- `-fsanitize=integer-divide-by-zero`: Integer division by zero.
+- `-fsanitize=integer-divide-by-zero`: Integer division by zero. Not part of
+ `-fsanitize=undefined` under `-fdefined-int-divrem`.
- `-fsanitize=implicit-bitfield-conversion`: Implicit conversion from
integer of larger bit width to smaller bitfield, if that results in data
loss. This includes unsigned/signed truncations and sign changes, similarly
@@ -216,7 +217,12 @@ You can also use the following check groups:
: - `-fsanitize=undefined`: All of the checks listed above other than
`float-divide-by-zero`, `unsigned-integer-overflow`,
`implicit-conversion`, `local-bounds`, `vptr` and the
- `nullability-*` group of checks.
+ `nullability-*` group of checks. Under `-fdefined-int-divrem`, which is
+ the default for OpenCL, integer division by zero and `INT_MIN/-1` are not
+ undefined, so `integer-divide-by-zero` and `signed-integer-overflow-divide`
+ are not included either; enabling them in any other way, e.g. with
+ `-fsanitize=undefined,integer-divide-by-zero` or `-fsanitize=integer`,
+ still works, with the usual recover and trap behavior.
- `-fsanitize=undefined-trap`: Deprecated alias of
`-fsanitize=undefined`.
- `-fsanitize=implicit-integer-truncation`: Catches lossy integral
diff --git a/clang/docs/UsersManual.md b/clang/docs/UsersManual.md
index 4b34149564ddb6..75bb7035f6cd26 100644
--- a/clang/docs/UsersManual.md
+++ b/clang/docs/UsersManual.md
@@ -2998,6 +2998,30 @@ such as kernels and boot loaders, that performs pointer arithmetic over
externally defined memory layouts rather than ordinary C or C++ objects.
:::
+:::{option} -fdefined-int-divrem, -fno-defined-int-divrem
+
+Integer division and remainder by zero, and signed division whose result does
+not fit (`INT_MIN / -1` and `INT_MIN % -1`), are undefined behavior in C and
+C++. OpenCL C v3.0 s6.5.1 instead requires them to give an unspecified value.
+With `-fdefined-int-divrem`, such an operation gives some value of its type
+and has no other effect, and the optimizer cannot assume that the operands
+avoid these cases. Clang lowers an integer `/` or `%` whose operands are not
+known to be safe to a frozen `llvm.masked.sdiv`, `llvm.masked.udiv`,
+`llvm.masked.srem` or `llvm.masked.urem`, which a target whose division does
+not trap can select as its plain division instruction. On SPIR and SPIR-V
+targets, the divisor is replaced by 1 for these operands instead, because their
+division instructions (such as `OpSDiv`) are undefined for these operands too
+and their consumers do not know the masked intrinsics. ClangIR (`-fclangir`)
+replaces the divisor on every target. Division of a matrix by a scalar
+(`-fenable-matrix`), complex integer division and fixed-point division are not
+affected.
+
+This is the default for OpenCL C and C++ for OpenCL, and
+`-fno-defined-int-divrem` turns it off. It does not change constant
+evaluation, so a division by zero is still not a constant expression.
+
+:::
+
:::{option} -ftrivial-auto-var-init=[uninitialized, zero, pattern]
Initialize automatic variables that would otherwise be left uninitialized.
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index f0b0d661712766..014f9c2536d415 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7636,7 +7636,8 @@ def err_setting_eval_method_used_in_unsafe_context : Error <
"%select{option 'fapprox-func'|option 'mreassociate'|option 'freciprocal'|option 'ffp-eval-method'|'#pragma clang fp reassociate'|'#pragma clang fp reciprocal'}1">;
def warn_remainder_division_by_zero : Warning<
- "%select{remainder|division}0 by zero is undefined">,
+ "%select{remainder|division}0 by zero "
+ "%select{is undefined|results in an unspecified value}1">,
InGroup<DivZero>;
def warn_shift_lhs_negative : Warning<"shifting a negative signed value is undefined">,
InGroup<DiagGroup<"shift-negative-value">>;
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 1fef2c5d881c77..948cd38a636ea1 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -540,6 +540,7 @@ LANGOPT(PreserveVec3Type, 1, 0, NotCompatible, "Preserve 3-component vector type
LANGOPT(Reflection , 1, 0, NotCompatible, "C++26 Reflection")
LANGOPT(StablePointerSubtraction, 1, 0, NotCompatible, "Make unaligned pointer subtraction stable")
+LANGOPT(DefinedIntDivRem, 1, 0, NotCompatible, "Give integer division and remainder by zero and signed division overflow an unspecified result")
#undef LANGOPT
#undef ENUM_LANGOPT
diff --git a/clang/include/clang/Driver/SanitizerArgs.h b/clang/include/clang/Driver/SanitizerArgs.h
index 145df2d94c61f3..1348a78ccfdb26 100644
--- a/clang/include/clang/Driver/SanitizerArgs.h
+++ b/clang/include/clang/Driver/SanitizerArgs.h
@@ -32,6 +32,10 @@ class SanitizerArgs {
SanitizerMaskCutoffs SkipHotCutoffs;
SanitizerSet AnnotateDebugInfo;
SanitizerSet SuppressUBSanFeature;
+ /// The integer-divide-by-zero and signed-integer-overflow-divide checks if
+ /// they are enabled only through -fsanitize=undefined. They are not part of
+ /// it under -fdefined-int-divrem, which is decided per input in addArgs.
+ SanitizerMask DivRemOnlyFromUndefined;
std::vector<std::string> UserIgnorelistFiles;
std::vector<std::string> SystemIgnorelistFiles;
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index f23958cdd548d7..663495e596db09 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -5096,6 +5096,15 @@ def fdefined_pointer_subtraction :
"to `char *` before subtracting them rather than relying on this flag."
>,
MarshallingInfoFlag<LangOpts<"StablePointerSubtraction">>;
+defm defined_int_divrem : BoolFOption<"defined-int-divrem",
+ LangOpts<"DefinedIntDivRem">, Default<"LangOpts->OpenCL">,
+ PosFlag<SetTrue, [], [ClangOption, CC1Option],
+ "Give integer division and remainder by zero, and signed division "
+ "overflow, an unspecified result instead of undefined behavior "
+ "(default for OpenCL)">,
+ NegFlag<SetFalse, [], [ClangOption, CC1Option],
+ "Integer division and remainder by zero, and signed division "
+ "overflow, have undefined behavior (default except for OpenCL)">>;
def fno_wrapv_pointer : Flag<["-"], "fno-wrapv-pointer">, Group<f_Group>,
Visibility<[ClangOption, CLOption, FlangOption]>;
def fwritable_strings : Flag<["-"], "fwritable-strings">, Group<f_Group>,
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 1aa8a6f433e9ed..1a3437a1be0158 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1213,6 +1213,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
mlir::Value emitMul(const BinOpInfo &ops);
mlir::Value emitDiv(const BinOpInfo &ops);
mlir::Value emitRem(const BinOpInfo &ops);
+ mlir::Value emitIntDivRemDivisor(const BinOpInfo &ops);
mlir::Value emitAdd(const BinOpInfo &ops);
mlir::Value emitSub(const BinOpInfo &ops);
mlir::Value emitShl(const BinOpInfo &ops);
@@ -2354,6 +2355,79 @@ mlir::Value ScalarExprEmitter::emitMul(const BinOpInfo &ops) {
return cir::MulOp::create(builder, cgf.getLoc(ops.loc),
cgf.convertType(ops.fullType), ops.lhs, ops.rhs);
}
+
+/// Returns the divisor for an integer (scalar or vector) division or
+/// remainder. Under -fdefined-int-divrem, operands that would make the
+/// operation undefined (a zero divisor, or INT_MIN / -1) give an unspecified
+/// value instead: the divisor is replaced by 1 for them. Classic CodeGen emits
+/// a frozen llvm.masked.{s,u}{div,rem} instead, except on SPIR and SPIR-V.
+mlir::Value ScalarExprEmitter::emitIntDivRemDivisor(const BinOpInfo &ops) {
+ if (!cgf.getLangOpts().DefinedIntDivRem)
+ return ops.rhs;
+
+ bool isSigned = ops.compType->hasSignedIntegerRepresentation();
+ auto isSafeConst = [isSigned](mlir::Value v) {
+ if (auto c = v.getDefiningOp<cir::ConstantOp>())
+ if (auto i = mlir::dyn_cast<cir::IntAttr>(c.getValue()))
+ return !i.getValue().isZero() &&
+ (!isSigned || !i.getValue().isAllOnes());
+ return false;
+ };
+ mlir::Value rhsVal = ops.rhs;
+ if (auto splat = rhsVal.getDefiningOp<cir::VecSplatOp>())
+ rhsVal = splat.getValue();
+ if (isSafeConst(rhsVal))
+ return ops.rhs;
+ if (auto vec = rhsVal.getDefiningOp<cir::VecCreateOp>())
+ if (llvm::all_of(vec.getElements(), isSafeConst))
+ return ops.rhs;
+
+ const mlir::Location loc = cgf.getLoc(ops.loc);
+ mlir::Type ty = ops.lhs.getType();
+ auto vecTy = mlir::dyn_cast<cir::VectorType>(ty);
+ auto intTy = mlir::cast<cir::IntType>(vecTy ? vecTy.getElementType() : ty);
+ unsigned width = intTy.getWidth();
+ assert((!isSigned || width != 1) && "unexpected signed 1-bit integer");
+ auto getConst = [&](const llvm::APInt &v) -> mlir::Value {
+ mlir::Value c = builder.getConstAPInt(loc, intTy, v);
+ return vecTy ? cir::VecSplatOp::create(builder, loc, vecTy, c) : c;
+ };
+ // Vector comparisons give a vector of signed integers of the same width.
+ mlir::Type cmpTy;
+ if (vecTy)
+ cmpTy = cir::VectorType::get(
+ cir::IntType::get(builder.getContext(), width, /*is_signed=*/true),
+ vecTy.getSize(), vecTy.getKind());
+ auto createNE = [&](mlir::Value lhs, mlir::Value rhs) -> mlir::Value {
+ if (vecTy)
+ return cir::VecCmpOp::create(builder, loc, cmpTy, cir::CmpOpKind::ne, lhs,
+ rhs);
+ return builder.createCompare(loc, cir::CmpOpKind::ne, lhs, rhs);
+ };
+
+ auto createSelect = [&](mlir::Value cond, mlir::Value trueVal,
+ mlir::Value falseVal) -> mlir::Value {
+ if (vecTy)
+ return cir::VecTernaryOp::create(builder, loc, cond, trueVal, falseVal);
+ return builder.createSelect(loc, cond, trueVal, falseVal);
+ };
+
+ mlir::Value one = getConst(llvm::APInt(width, 1));
+ mlir::Value divisor = ops.rhs;
+ if (isSigned) {
+ mlir::Value lhsNotMin =
+ createNE(ops.lhs, getConst(llvm::APInt::getSignedMinValue(width)));
+ mlir::Value rhsNotMinusOne =
+ createNE(ops.rhs, getConst(llvm::APInt::getAllOnes(width)));
+ // Check rhs != -1 via select so a poison dividend lane does not poison
+ // the divisor when rhs is not -1.
+ divisor = createSelect(rhsNotMinusOne, divisor,
+ createSelect(lhsNotMin, divisor, one));
+ }
+ mlir::Value rhsNotZero = createNE(ops.rhs, getConst(llvm::APInt(width, 0)));
+ return createSelect(rhsNotZero, divisor, one);
+}
+
mlir::Value ScalarExprEmitter::emitDiv(const BinOpInfo &ops) {
const mlir::Location loc = cgf.getLoc(ops.loc);
if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
@@ -2380,7 +2454,7 @@ mlir::Value ScalarExprEmitter::emitDiv(const BinOpInfo &ops) {
return emitFixedPointBinOp(ops);
return cir::DivOp::create(builder, loc, cgf.convertType(ops.fullType),
- ops.lhs, ops.rhs);
+ ops.lhs, emitIntDivRemDivisor(ops));
}
mlir::Value ScalarExprEmitter::emitRem(const BinOpInfo &ops) {
const mlir::Location loc = cgf.getLoc(ops.loc);
@@ -2389,7 +2463,7 @@ mlir::Value ScalarExprEmitter::emitRem(const BinOpInfo &ops) {
return builder.createFRem(loc, ops.lhs, ops.rhs);
}
return cir::RemOp::create(builder, loc, cgf.convertType(ops.fullType),
- ops.lhs, ops.rhs);
+ ops.lhs, emitIntDivRemDivisor(ops));
}
mlir::Value ScalarExprEmitter::emitFixedPointBinOp(const BinOpInfo &ops) {
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 98ad65c50722d2..19626ea5a5f251 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -914,6 +914,11 @@ class ScalarExprEmitter
// non powers of two.
Value *ConstrainShiftValue(Value *LHS, Value *RHS, const Twine &Name);
+ /// Emit the integer (scalar or vector) division or remainder \p Opc. Under
+ /// -fdefined-int-divrem, operands that would make it undefined (a zero
+ /// divisor, or INT_MIN / -1) give an unspecified value instead.
+ Value *EmitIntDivRem(const BinOpInfo &Ops, llvm::Instruction::BinaryOps Opc,
+ const Twine &Name);
Value *EmitDiv(const BinOpInfo &Ops);
Value *EmitRem(const BinOpInfo &Ops);
Value *EmitAdd(const BinOpInfo &Ops);
@@ -4329,6 +4334,124 @@ void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
EmitBinOpCheck(Checks, Ops);
}
+/// Returns true if every element of the integer constant (or constant vector)
+/// \p C satisfies \p Pred.
+static bool
+allElementsSatisfy(llvm::Constant *C,
+ llvm::function_ref<bool(const llvm::APInt &)> Pred) {
+ if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
+ return Pred(CI->getValue());
+ // Any other scalar, e.g. a ptrtoint ConstantExpr or poison, is not known.
+ if (!C->getType()->isVectorTy())
+ return false;
+ if (auto *VTy = dyn_cast<llvm::FixedVectorType>(C->getType())) {
+ for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
+ auto *Elt =
+ dyn_cast_or_null<llvm::ConstantInt>(C->getAggregateElement(I));
+ if (!Elt || !Pred(Elt->getValue()))
+ return false;
+ }
+ return true;
+ }
+ if (auto *Splat = dyn_cast_or_null<llvm::ConstantInt>(C->getSplatValue()))
+ return Pred(Splat->getValue());
+ return false;
+}
+
+Value *ScalarExprEmitter::EmitIntDivRem(const BinOpInfo &Ops,
+ llvm::Instruction::BinaryOps Opc,
+ const Twine &Name) {
+ Value *LHS = Ops.LHS;
+ Value *RHS = Ops.RHS;
+ if (!CGF.getLangOpts().DefinedIntDivRem)
+ return Builder.CreateBinOp(Opc, LHS, RHS, Name);
+
+ // The operation is undefined for a zero divisor, and for a signed
+ // INT_MIN / -1. Compute a mask that is false exactly for these operands,
+ // leaving out the conditions that constant operands already rule out.
+ using llvm::APInt;
+ auto IsConstantWhere = [](Value *V, bool (*Pred)(const APInt &)) {
+ auto *C = dyn_cast<llvm::Constant>(V);
+ return C && allElementsSatisfy(C, Pred);
+ };
+ auto IsNonZero = [](const APInt &V) { return !V.isZero(); };
+ auto IsMinusOne = [](const APInt &V) { return V.isAllOnes(); };
+ auto IsNotMinusOne = [](const APInt &V) { return !V.isAllOnes(); };
+ auto IsMin = [](const APInt &V) { return V.isMinSignedValue(); };
+ auto IsNotMin = [](const APInt &V) { return !V.isMinSignedValue(); };
+
+ llvm::Type *Ty = LHS->getType();
+ Value *Mask = nullptr;
+ if (!IsConstantWhere(RHS, IsNonZero))
+ Mask = Builder.CreateICmpNE(RHS, llvm::Constant::getNullValue(Ty));
+ bool IsSigned =
+ Opc == llvm::Instruction::SDiv || Opc == llvm::Instruction::SRem;
+ if (IsSigned && !IsConstantWhere(RHS, IsNotMinusOne) &&
+ !IsConstantWhere(LHS, IsNotMin)) {
+ Value *Min = llvm::ConstantInt::get(
+ Ty, APInt::getSignedMinValue(Ty->getScalarSizeInBits()));
+ Value *MinusOne = llvm::Constant::getAllOnesValue(Ty);
+ Value *NotOverflow;
+ if (IsConstantWhere(RHS, IsMinusOne)) {
+ NotOverflow = Builder.CreateICmpNE(LHS, Min);
+ } else if (IsConstantWhere(LHS, IsMin)) {
+ NotOverflow = Builder.CreateICmpNE(RHS, MinusOne);
+ } else {
+ Value *LHSNotMin = Builder.CreateICmpNE(LHS, Min);
+ Value *RHSNotMinusOne = Builder.CreateICmpNE(RHS, MinusOne);
+ // Check RHS != -1 via select before LHS != MIN so a poison dividend lane
+ // cannot poison the mask when RHS is not -1.
+ Value *AllTrue =
+ llvm::Constant::getAllOnesValue(RHSNotMinusOne->getType());
+ NotOverflow = Builder.CreateSelect(RHSNotMinusOne, AllTrue, LHSNotMin);
+ }
+ if (Mask) {
+ Value *AllFalse = llvm::Constant::getNullValue(Mask->getType());
+ Mask = Builder.CreateSelect(Mask, NotOverflow, AllFalse);
+ } else {
+ Mask = NotOverflow;
+ }
+ }
+ if (!Mask)
+ return Builder.CreateBinOp(Opc, LHS, RHS, Name);
+
+ // SPIR-V has no instruction for this: OpSDiv and friends are undefined for
+ // these operands, and the consumers of SPIR and SPIR-V modules (such as the
+ // SPIRV-LLVM-Translator) do not know the masked intrinsics. Replace the
+ // divisor by 1 where the mask is false.
+ if (CGF.getTarget().getTriple().isSPIROrSPIRV()) {
+ assert((!IsSigned || Ty->getScalarSizeInBits() != 1) &&
+ "unexpected signed 1-bit integer");
+ Value *SafeRHS =
+ Builder.CreateSelect(Mask, RHS, llvm::ConstantInt::get(Ty, 1));
+ return Builder.CreateBinOp(Opc, LHS, SafeRHS, Name);
+ }
+
+ // The masked intrinsic returns poison where the mask is false, and freezing
+ // it turns that into an arbitrary but fixed value: an unspecified result.
+ llvm::Intrinsic::ID IID;
+ switch (Opc) {
+ case llvm::Instruction::UDiv:
+ IID = llvm::Intrinsic::masked_udiv;
+ break;
+ case llvm::Instruction::SDiv:
+ IID = llvm::Intrinsic::masked_sdiv;
+ break;
+ case llvm::Instruction::URem:
+ IID = llvm::Intrinsic::masked_urem;
+ break;
+ case llvm::Instruction::SRem:
+ IID = llvm::Intrinsic::masked_srem;
+ break;
+ default:
+ llvm_unreachable("not an integer division or remainder");
+ }
+ Value *Result =
+ Builder.CreateIntrinsic(IID, Ty, {LHS, RHS, Mask},
+ /*FMFSource=*/nullptr, Name + ".masked");
+ return Builder.CreateFreeze(Result, Name);
+}
+
Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
{
SanitizerDebugLocation SanScope(
@@ -4380,9 +4503,9 @@ Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
else if (Ops.isFixedPointOp())
return EmitFixedPointBinOp(Ops);
else if (Ops.Ty->hasUnsignedIntegerRepresentation())
- return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
+ return EmitIntDivRem(Ops, llvm::Instruction::UDiv, "div");
else
- return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
+ return EmitIntDivRem(Ops, llvm::Instruction::SDiv, "div");
}
Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
@@ -4401,12 +4524,12 @@ Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
}
if (Ops.Ty->hasUnsignedIntegerRepresentation())
- return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
+ return EmitIntDivRem(Ops, llvm::Instruction::URem, "rem");
if (CGF.getLangOpts().HLSL && Ops.Ty->hasFloatingRepresentation())
return Builder.CreateFRem(Ops.LHS, Ops.RHS, "rem");
- return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
+ return EmitIntDivRem(Ops, llvm::Instruction::SRem, "rem");
}
Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index f65e606caf6fbf..495dae0836928a 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -458,6 +458,9 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
// Used to deduplicate diagnostics.
SanitizerMask IgnoreForUbsanFeature; // Accumulated set of values passed to
// `-fsanitize-ignore-for-ubsan-feature`.
+ SanitizerMask NotFromUndefined; // Kinds enabled by -fsanitize= other than
+ // through the undefined group, and not
+ // disabled by a later -fno-sanitize=.
SanitizerMask Kinds;
// Figure out the base toolchain's sanitizer support so we can diagnose the
@@ -499,6 +502,10 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
Arg->claim();
SanitizerMask Add = parseArgValues(D, Arg, DiagnoseErrors);
+ NotFromUndefined |=
+ expandSanitizerGroups(Add & ~(SanitizerKind::UndefinedGroup |
+ SanitizerKind::UndefinedTrapGroup)) &
+ ~AllRemove;
if (RemoveObjectSizeAtO0) {
AllRemove |= SanitizerKind::ObjectSize;
@@ -1359,6 +1366,9 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
// Finally, initialize the set of available and recoverable sanitizers.
Sanitizers.Mask |= clearGroupBits(Kinds);
+ DivRemOnlyFromUndefined = Kinds & ~NotFromUndefined &
+ (SanitizerKind::IntegerDivideByZero |
+ SanitizerKind::SignedIntegerOverflowDivide);
RecoverableSanitizers.Mask |= RecoverableKinds;
TrapSanitizers.Mask |= TrappingKinds;
assert(!(RecoverableKinds & TrappingKinds) &&
@@ -1442,6 +1452,35 @@ static bool hasTargetFeatureMTE(const llvm::opt::ArgStringList &CmdArgs) {
return false;
}
+/// Returns whether -fdefined-int-divrem is in effect for a cc1 job for an input
+/// of type \p InputType, i.e. what cc1 will decide.
+static bool isDefinedIntDivRem(const Driver &D, const llvm::opt::ArgList &Args,
+ types::ID InputType) {
+ // cc1 gets -Xclang arguments after the driver's -f[no-]defined-int-divrem,
+ // so parse them with CC1 visibility to avoid mistaking option values (such as
+ // -Xclang -main-file-name -Xclang -fdefined-int-divrem) for flags.
+ SmallVector<const char *, 8> XclangVals;
+ for (const Arg *A : Args.filtered(options::OPT_Xclang))
+ XclangVals.push_back(A->getValue());
+ bool XclangCLStd = false;
+ if (!XclangVals.empty()) {
+ unsigned MissingArgIndex, MissingArgCount;
+ llvm::opt::InputArgList XArgs =
+ D.getOpts().ParseArgs(XclangVals, MissingArgIndex, MissingArgCount,
+ llvm::opt::Visibility(options::CC1Option));
+ if (const Arg *A = XArgs.getLastArg(options::OPT_fdefined_int_divrem,
+ options::OPT_fno_defined_int_divrem))
+ return A->getOption().matches(options::OPT_fdefined_int_divrem);
+ XclangCLStd = XArgs.hasArg(options::OPT_cl_std_EQ);
+ }
+ // cc1 turns it on for OpenCL (including cl-header), and -cl-std= makes any
+ // source OpenCL.
+ return Args.hasFlagNoClaim(
+ options::OPT_fdefined_int_divrem, options::OPT_fno_defined_int_divrem,
+ types::isOpenCL(InputType) || InputType == types::TY_CLHeader ||
+ Args.hasArgNoClaim(options::OPT_cl_std_EQ) || XclangCLStd);
+}
+
void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs,
types::ID InputType) const {
@@ -1550,7 +1589,22 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
if (Sanitizers.empty())
return;
- CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
+
+ // Under -fdefined-int-divrem, which is the default for OpenCL, integer
+ // division by zero and INT_MIN / -1 give an unspecified value instead of
+ // undefined behavior, so -fsanitize=undefined does not check for them. An
+ // explicit request (e.g. -fsanitize=integer or -fsanitize=undefined,
+ // integer-divide-by-zero) still does. Only the enabled set changes: the
+ // recover, trap and other lists keep the two checks, which has no effect
+ // unless they are enabled, so that a request that only cc1 sees
+ // (-Xclang -fsanitize=integer-divide-by-zero) recovers or traps as usual.
+ SanitizerSet Enabled = Sanitizers;
+ if (DivRemOnlyFromUndefined &&
+ isDefinedIntDivRem(TC.getDriver(), Args, InputType))
+ Enabled.clear(DivRemOnlyFromUndefined);
+ std::string EnabledStr = toString(Enabled);
+ if (!EnabledStr.empty())
+ CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + EnabledStr));
if (!SuppressUBSanFeature.empty())
CmdArgs.push_back(
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 41f2e85a20c87c..4a95702707bcda 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5520,6 +5520,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (Args.hasArg(options::OPT_fdefined_pointer_subtraction))
CmdArgs.push_back("-fdefined-pointer-subtraction");
+ // cc1 computes the default (on for OpenCL) itself.
+ Args.addLastArg(CmdArgs, options::OPT_fdefined_int_divrem,
+ options::OPT_fno_defined_int_divrem);
+
// If CollectArgsForIntegratedAssembler() isn't called below, claim the args
// it claims when not running an assembler. Otherwise, clang would emit
// "argument unused" warnings for assembler flags when e.g. adding "-E" to
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index bc3f6444b88bae..5ebd38d2372a1a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11488,17 +11488,23 @@ static void DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS,
}
}
-static void DiagnoseBadDivideOrRemainderValues(Sema& S, ExprResult &LHS,
- ExprResult &RHS,
+static void DiagnoseBadDivideOrRemainderValues(Sema &S, ExprResult &RHS,
+ QualType CompType,
SourceLocation Loc, bool IsDiv) {
// Check for division/remainder by zero.
Expr::EvalResult RHSValue;
if (!RHS.get()->isValueDependent() &&
RHS.get()->EvaluateAsInt(RHSValue, S.Context) &&
- RHSValue.Val.getInt() == 0)
+ RHSValue.Val.getInt() == 0) {
+ // -fdefined-int-divrem covers integer operations only, not, for example,
+ // fixed-point or complex division by an integer zero.
+ bool Defined = S.getLangOpts().DefinedIntDivRem &&
+ CompType->hasIntegerRepresentation();
S.DiagRuntimeBehavior(Loc, RHS.get(),
S.PDiag(diag::warn_remainder_division_by_zero)
- << IsDiv << RHS.get()->getSourceRange());
+ << IsDiv << Defined
+ << RHS.get()->getSourceRange());
+ }
}
static void diagnoseScopedEnums(Sema &S, const SourceLocation Loc,
@@ -11582,7 +11588,7 @@ QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS,
}
if (IsDiv) {
DetectPrecisionLossInComplexDivision(*this, RHS.get()->getType(), Loc);
- DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, IsDiv);
+ DiagnoseBadDivideOrRemainderValues(*this, RHS, compType, Loc, IsDiv);
DiagnoseDivisionSizeofPointerOrArray(*this, LHS.get(), RHS.get(), Loc);
}
return compType;
@@ -11647,7 +11653,8 @@ QualType Sema::CheckRemainderOperands(
IsCompAssign ? BO_RemAssign : BO_Rem);
return ResultTy;
}
- DiagnoseBadDivideOrRemainderValues(*this, LHS, RHS, Loc, false /* IsDiv */);
+ DiagnoseBadDivideOrRemainderValues(*this, RHS, compType, Loc,
+ false /* IsDiv */);
return compType;
}
diff --git a/clang/test/CIR/CodeGen/defined-int-divrem-sve.c b/clang/test/CIR/CodeGen/defined-int-divrem-sve.c
new file mode 100644
index 00000000000000..787c21a4232c1d
--- /dev/null
+++ b/clang/test/CIR/CodeGen/defined-int-divrem-sve.c
@@ -0,0 +1,43 @@
+// CIR calling-convention lowering does not handle SVE scalable vector types
+// yet, so the -fclangir runs disable it.
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sve -mvscale-min=1 -mvscale-max=1 -disable-O0-optnone -fclangir -fno-clangir-call-conv-lowering -fdefined-int-divrem -emit-cir -o - %s | FileCheck --check-prefix=CIR %s
+// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sve -mvscale-min=1 -mvscale-max=1 -disable-O0-optnone -fclangir -fno-clangir-call-conv-lowering -fdefined-int-divrem -emit-llvm -o - %s | opt -S -passes=sroa | FileCheck --check-prefix=LLVM %s
+
+typedef __SVInt32_t svint32_t;
+typedef __SVUint32_t svuint32_t;
+typedef svint32_t fixed_svint32_t __attribute__((arm_sve_vector_bits(128)));
+
+// CIR-LABEL: cir.func {{.*}} @sve_sdiv(
+// CIR: cir.vec.cmp(ne, {{.*}}) : !cir.vector<[4] x !s32i, sve_data>, !cir.vector<[4] x !s32i, sve_data>
+// CIR: cir.vec.ternary({{.*}}) : !cir.vector<[4] x !s32i, sve_data>, !cir.vector<[4] x !s32i, sve_data>
+// CIR: cir.div {{.*}} : !cir.vector<[4] x !s32i, sve_data>
+// LLVM-LABEL: define {{.*}} <vscale x 4 x i32> @sve_sdiv(
+// LLVM: icmp ne <vscale x 4 x i32> %{{.*}}, splat (i32 -1)
+// LLVM: select <vscale x 4 x i1>
+// LLVM: icmp ne <vscale x 4 x i32> %{{.*}}, zeroinitializer
+// LLVM: select <vscale x 4 x i1>
+// LLVM: sdiv <vscale x 4 x i32>
+svint32_t sve_sdiv(svint32_t a, svint32_t b) {
+ return a / b;
+}
+
+// CIR-LABEL: cir.func {{.*}} @sve_urem(
+// CIR: cir.vec.cmp(ne, {{.*}}) : !cir.vector<[4] x !u32i, sve_data>, !cir.vector<[4] x !s32i, sve_data>
+// CIR: cir.vec.ternary({{.*}}) : !cir.vector<[4] x !s32i, sve_data>, !cir.vector<[4] x !u32i, sve_data>
+// CIR: cir.rem {{.*}} : !cir.vector<[4] x !u32i, sve_data>
+// LLVM-LABEL: define {{.*}} <vscale x 4 x i32> @sve_urem(
+// LLVM: icmp ne <vscale x 4 x i32> %{{.*}}, zeroinitializer
+// LLVM: select <vscale x 4 x i1> {{.*}}, <vscale x 4 x i32> %{{.*}}, <vscale x 4 x i32> splat (i32 1)
+// LLVM: urem <vscale x 4 x i32>
+svuint32_t sve_urem(svuint32_t a, svuint32_t b) {
+ return a % b;
+}
+
+// CIR-LABEL: cir.func {{.*}} @sve_fixed_srem(
+// CIR: cir.vec.cmp(ne, {{.*}}) : !cir.vector<4 x !s32i, sve_data>, !cir.vector<4 x !s32i, sve_data>
+// CIR: cir.rem {{.*}} : !cir.vector<4 x !s32i, sve_data>
+// LLVM-LABEL: define {{.*}} <4 x i32> @sve_fixed_srem(
+// LLVM: srem <4 x i32>
+fixed_svint32_t sve_fixed_srem(fixed_svint32_t a, fixed_svint32_t b) {
+ return a % b;
+}
diff --git a/clang/test/CIR/CodeGenOpenCL/defined-int-divrem.cl b/clang/test/CIR/CodeGenOpenCL/defined-int-divrem.cl
new file mode 100644
index 00000000000000..7ea32e8ef932ab
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenCL/defined-int-divrem.cl
@@ -0,0 +1,173 @@
+// OpenCL C v3.0 s6.5.1: integer division and remainder by zero, and INT_MIN /
+// -1, give an unspecified value. ClangIR implements -fdefined-int-divrem (the
+// OpenCL default) by replacing the divisor by 1 for these operands.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -O0 -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -O0 -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=clc++2021 -O0 -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefixes=LLVM,CLCXX
+// RUN: %clang_cc1 -triple spirv64-unknown-unknown -cl-std=CL3.0 -O0 -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -x c -O0 -fdefined-int-divrem -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -O0 -fno-defined-int-divrem -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=OFF
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -x c -O0 -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=OFF
+
+typedef int int4 __attribute__((ext_vector_type(4)));
+typedef unsigned int uint4 __attribute__((ext_vector_type(4)));
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// CIR-LABEL: cir.func {{.*}} @sdiv(
+// CIR: %[[NMIN:.*]] = cir.cmp ne %[[A:.*]], %{{.*}} : !s32i
+// CIR: %[[NM1:.*]] = cir.cmp ne %[[B:.*]], %{{.*}} : !s32i
+// CIR: %[[D1:.*]] = cir.select if %[[NMIN]] then %[[B]] else %[[ONE:.*]] :
+// CIR: %[[D2:.*]] = cir.select if %[[NM1]] then %[[B]] else %[[D1]] :
+// CIR: %[[NZ:.*]] = cir.cmp ne %[[B]], %{{.*}} : !s32i
+// CIR: %[[D:.*]] = cir.select if %[[NZ]] then %[[D2]] else %[[ONE]] :
+// CIR: cir.div %[[A]], %[[D]] : !s32i
+// LLVM-LABEL: @sdiv(
+// LLVM: %[[NMIN:.*]] = icmp ne i32 %[[A:.*]], -2147483648
+// LLVM: %[[NM1:.*]] = icmp ne i32 %[[B:.*]], -1
+// LLVM: %[[D1:.*]] = select i1 %[[NMIN]], i32 %[[B]], i32 1
+// LLVM: %[[D2:.*]] = select i1 %[[NM1]], i32 %[[B]], i32 %[[D1]]
+// LLVM: %[[NZ:.*]] = icmp ne i32 %[[B]], 0
+// LLVM: %[[D:.*]] = select i1 %[[NZ]], i32 %[[D2]], i32 1
+// LLVM: sdiv i32 %[[A]], %[[D]]
+// OFF-LABEL: @sdiv(
+// OFF-NOT: select
+// OFF: sdiv i32
+int sdiv(int a, int b) { return a / b; }
+
+// LLVM-LABEL: @srem(
+// LLVM: %[[NZ:.*]] = icmp ne i32 %[[B:.*]], 0
+// LLVM-NEXT: %[[D:.*]] = select i1 %[[NZ]], i32 %{{.*}}, i32 1
+// LLVM-NEXT: srem i32 %{{.*}}, %[[D]]
+// OFF-LABEL: @srem(
+// OFF-NOT: select
+// OFF: srem i32
+int srem(int a, int b) { return a % b; }
+
+// CIR-LABEL: cir.func {{.*}} @udiv(
+// CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !u32i
+// CIR: %[[NZ:.*]] = cir.cmp ne %[[B:.*]], %{{.*}} : !u32i
+// CIR-NEXT: %[[D:.*]] = cir.select if %[[NZ]] then %[[B]] else %[[ONE]]
+// CIR-NEXT: cir.div %{{.*}}, %[[D]] : !u32i
+// LLVM-LABEL: @udiv(
+// LLVM: %[[NZ:.*]] = icmp ne i32 %[[B:.*]], 0
+// LLVM-NEXT: %[[D:.*]] = select i1 %[[NZ]], i32 %[[B]], i32 1
+// LLVM-NEXT: udiv i32 %{{.*}}, %[[D]]
+unsigned udiv(unsigned a, unsigned b) { return a / b; }
+
+// LLVM-LABEL: @urem_assign(
+// LLVM: %[[NZ:.*]] = icmp ne i64 %[[B:.*]], 0
+// LLVM-NEXT: %[[D:.*]] = select i1 %[[NZ]], i64 %[[B]], i64 1
+// LLVM-NEXT: urem i64 %{{.*}}, %[[D]]
+unsigned long urem_assign(unsigned long a, unsigned long b) {
+ a %= b;
+ return a;
+}
+
+// CIR-LABEL: cir.func {{.*}} @vdiv(
+// CIR: %[[NMIN:.*]] = cir.vec.cmp(ne, %[[A:.*]], %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+// CIR: %[[NM1:.*]] = cir.vec.cmp(ne, %[[B:.*]], %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+// CIR: %[[D1:.*]] = cir.vec.ternary(%[[NMIN]], %[[B]], %[[ONE:.*]])
+// CIR: %[[D2:.*]] = cir.vec.ternary(%[[NM1]], %[[B]], %[[D1]])
+// CIR: %[[NZ:.*]] = cir.vec.cmp(ne, %[[B]], %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+// CIR: %[[D:.*]] = cir.vec.ternary(%[[NZ]], %[[D2]], %[[ONE]])
+// CIR: cir.div %[[A]], %[[D]] : !cir.vector<4 x !s32i>
+// LLVM-LABEL: @vdiv(
+// LLVM: icmp ne <4 x i32> %[[A:.*]], splat (i32 -2147483648)
+// LLVM: icmp ne <4 x i32> %[[B:.*]], splat (i32 -1)
+// LLVM: icmp ne <4 x i32> %[[B]], zeroinitializer
+// LLVM: %[[D:.*]] = select <4 x i1> %{{.*}}, <4 x i32> %{{.*}}, <4 x i32> splat (i32 1)
+// LLVM-NEXT: sdiv <4 x i32> %[[A]], %[[D]]
+// OFF-LABEL: @vdiv(
+// OFF-NOT: select
+// OFF: sdiv <4 x i32>
+int4 vdiv(int4 a, int4 b) { return a / b; }
+
+// LLVM-LABEL: @vurem(
+// LLVM: %[[D:.*]] = select <4 x i1> %{{.*}}, <4 x i32> %[[B:.*]], <4 x i32> splat (i32 1)
+// LLVM-NEXT: urem <4 x i32> %{{.*}}, %[[D]]
+uint4 vurem(uint4 a, uint4 b) { return a % b; }
+
+// CIR-LABEL: cir.func {{.*}} @sdiv_const(
+// CIR-NOT: cir.select
+// CIR: cir.div %{{.*}}, %{{.*}} : !s32i
+// LLVM-LABEL: @sdiv_const(
+// LLVM-NOT: select
+// LLVM: sdiv i32 %{{.*}}, 4
+int sdiv_const(int a) { return a / 4; }
+
+// CIR-LABEL: cir.func {{.*}} @udiv_allones(
+// CIR-NOT: cir.select
+// CIR: cir.div %{{.*}}, %{{.*}} : !u32i
+// LLVM-LABEL: @udiv_allones(
+// LLVM-NOT: select
+// LLVM: udiv i32 %{{.*}}, -1
+unsigned udiv_allones(unsigned a) { return a / 0xffffffffu; }
+
+// CIR-LABEL: cir.func {{.*}} @sdiv_minus_one(
+// CIR: cir.select
+// LLVM-LABEL: @sdiv_minus_one(
+// LLVM: select i1
+// LLVM: sdiv i32
+int sdiv_minus_one(int a) { return a / -1; }
+
+// CIR-LABEL: cir.func {{.*}} @vdiv_splat(
+// CIR-NOT: cir.vec.ternary
+// CIR: cir.div %{{.*}}, %{{.*}} : !cir.vector<4 x !s32i>
+// LLVM-LABEL: @vdiv_splat(
+// LLVM-NOT: select
+// LLVM: sdiv <4 x i32> %{{.*}}, splat (i32 4)
+int4 vdiv_splat(int4 a) { return a / 4; }
+
+// CIR-LABEL: cir.func {{.*}} @vdiv_zero(
+// CIR: cir.vec.ternary
+// LLVM-LABEL: @vdiv_zero(
+// LLVM: select <4 x i1>
+// LLVM: sdiv <4 x i32>
+int4 vdiv_zero(int4 a) { return a / (int4)(0); }
+
+#ifdef __cplusplus
+// CLCXX-LABEL: @vdiv_const_vec(
+// CLCXX-NOT: select
+// CLCXX: sdiv <4 x i32> %{{.*}}, <i32 2, i32 3, i32 4, i32 5>
+int4 vdiv_const_vec(int4 a) { return a / int4{2, 3, 4, 5}; }
+
+// CLCXX-LABEL: @vdiv_mixed_zero_vec(
+// CLCXX: select <4 x i1>
+// CLCXX: sdiv <4 x i32>
+int4 vdiv_mixed_zero_vec(int4 a) { return a / int4{2, 0, 4, 5}; }
+}
+#endif
+
+#ifdef __OPENCL_C_VERSION__
+// The kernels of CodeGenOpenCL/defined-int-divrem-O2.cl: without the defined
+// lowering, the optimizer removes the stores to out[1].
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -O2 -Wno-division-by-zero -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=O2
+
+// O2-LABEL: define {{.*}} void @divisor_checked_after(
+// O2: store i32 7,
+// O2: ret void
+__kernel void divisor_checked_after(__global int *out, int a, int b) {
+ int q = a / b;
+ if (b == 0)
+ out[1] = 7;
+ out[0] = q;
+}
+
+// O2-LABEL: define {{.*}} void @constant_zero_divisor(
+// O2: store i32 11, ptr
+// O2: [[P1:%.*]] = getelementptr {{.*}}i8, ptr %{{.*}}, i64 4
+// O2: store i32 {{.*}}, ptr [[P1]]
+// O2: [[P2:%.*]] = getelementptr {{.*}}i8, ptr %{{.*}}, i64 8
+// O2: store i32 22, ptr [[P2]]
+// O2: ret void
+__kernel void constant_zero_divisor(__global int *out) {
+ out[0] = 11;
+ int v = 5 / 0;
+ out[1] = v == 0 ? 33 : 44;
+ out[2] = 22;
+}
+#endif
diff --git a/clang/test/CodeGen/AArch64/defined-int-divrem-sve.c b/clang/test/CodeGen/AArch64/defined-int-divrem-sve.c
new file mode 100644
index 00000000000000..09f2199b003660
--- /dev/null
+++ b/clang/test/CodeGen/AArch64/defined-int-divrem-sve.c
@@ -0,0 +1,29 @@
+// REQUIRES: aarch64-registered-target
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature +sve -fdefined-int-divrem -disable-O0-optnone -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s
+
+// -fdefined-int-divrem on scalable vectors: a splat constant divisor that is
+// safe in every lane keeps the plain instruction.
+
+// CHECK-LABEL: @sdiv_splat(
+// CHECK: [[R:%.*]] = sdiv <vscale x 4 x i32> %a, splat (i32 4)
+// CHECK-NEXT: ret <vscale x 4 x i32> [[R]]
+__SVInt32_t sdiv_splat(__SVInt32_t a) { return a / 4; }
+
+// CHECK-LABEL: @srem_minus_one(
+// CHECK: [[NMIN:%.*]] = icmp ne <vscale x 4 x i32> %a, splat (i32 -2147483648)
+// CHECK-NEXT: [[Q:%.*]] = call <vscale x 4 x i32> @llvm.masked.srem.nxv4i32(<vscale x 4 x i32> %a, <vscale x 4 x i32> splat (i32 -1), <vscale x 4 x i1> [[NMIN]])
+// CHECK-NEXT: freeze <vscale x 4 x i32> [[Q]]
+__SVInt32_t srem_minus_one(__SVInt32_t a) { return a % -1; }
+
+// CHECK-LABEL: @udiv_var(
+// CHECK: [[NZ:%.*]] = icmp ne <vscale x 4 x i32> %b, zeroinitializer
+// CHECK-NEXT: [[Q:%.*]] = call <vscale x 4 x i32> @llvm.masked.udiv.nxv4i32(<vscale x 4 x i32> %a, <vscale x 4 x i32> %b, <vscale x 4 x i1> [[NZ]])
+// CHECK-NEXT: freeze <vscale x 4 x i32> [[Q]]
+__SVUint32_t udiv_var(__SVUint32_t a, __SVUint32_t b) { return a / b; }
+
+static int obj;
+
+// CHECK-LABEL: @sdiv_ptr(
+// CHECK: [[Q:%.*]] = call <vscale x 2 x i64> @llvm.masked.sdiv.nxv2i64(<vscale x 2 x i64> %a, <vscale x 2 x i64> {{.*}}, <vscale x 2 x i1> {{.*}})
+// CHECK-NEXT: freeze <vscale x 2 x i64> [[Q]]
+__SVInt64_t sdiv_ptr(__SVInt64_t a) { return a / (long)&obj; }
diff --git a/clang/test/CodeGen/defined-int-divrem.c b/clang/test/CodeGen/defined-int-divrem.c
new file mode 100644
index 00000000000000..f48855b4d2d240
--- /dev/null
+++ b/clang/test/CodeGen/defined-int-divrem.c
@@ -0,0 +1,86 @@
+// Integer division and remainder by zero and INT_MIN / -1 stay undefined in C
+// by default; -fdefined-int-divrem gives them an unspecified result instead.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fenable-matrix -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OFF --implicit-check-not=llvm.masked
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fenable-matrix -fdefined-int-divrem -fno-defined-int-divrem -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OFF --implicit-check-not=llvm.masked
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fenable-matrix -fdefined-int-divrem -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,ON
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fenable-matrix -fdefined-int-divrem -x c++ -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,ON,CXXON
+
+// The UBSan checks are emitted as before, ahead of the operation.
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fenable-matrix -fdefined-int-divrem -fsanitize=integer-divide-by-zero,signed-integer-overflow-divide -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,UBSAN
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// CHECK-LABEL: define {{.*}} @sdiv(
+// OFF: sdiv i32
+// ON: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 {{.*}}, i32 {{.*}}, i1 {{.*}})
+// ON-NEXT: freeze i32 [[Q]]
+// UBSAN: call void @__ubsan_handle_divrem_overflow_abort(
+// UBSAN: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 {{.*}}, i32 {{.*}}, i1 {{.*}})
+// UBSAN-NEXT: freeze i32 [[Q]]
+int sdiv(int a, int b) { return a / b; }
+
+// CHECK-LABEL: define {{.*}} @urem(
+// OFF: urem i32
+// ON: [[Q:%.*]] = call i32 @llvm.masked.urem.i32(i32 {{.*}}, i32 {{.*}}, i1 {{.*}})
+// ON-NEXT: freeze i32 [[Q]]
+// UBSAN: call void @__ubsan_handle_divrem_overflow_abort(
+// UBSAN: [[Q:%.*]] = call i32 @llvm.masked.urem.i32(i32 {{.*}}, i32 {{.*}}, i1 {{.*}})
+unsigned urem(unsigned a, unsigned b) { return a % b; }
+
+// CHECK-LABEL: define {{.*}} @div128(
+// OFF: sdiv i128
+// ON: [[Q:%.*]] = call i128 @llvm.masked.sdiv.i128(i128 {{.*}}, i128 {{.*}}, i1 {{.*}})
+// ON-NEXT: freeze i128 [[Q]]
+_BitInt(128) div128(_BitInt(128) a, _BitInt(128) b) { return a / b; }
+
+// CHECK-LABEL: define {{.*}} @compound(
+// OFF: srem i64
+// ON: [[Q:%.*]] = call i64 @llvm.masked.srem.i64(i64 {{.*}}, i64 {{.*}}, i1 {{.*}})
+// ON-NEXT: freeze i64 [[Q]]
+long long compound(long long a, long long b) {
+ a %= b;
+ return a;
+}
+
+typedef int v4i __attribute__((vector_size(16)));
+// CHECK-LABEL: define {{.*}} @vdiv(
+// OFF: sdiv <4 x i32>
+// ON: [[Q:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> {{.*}}, <4 x i32> {{.*}}, <4 x i1> {{.*}})
+// ON-NEXT: freeze <4 x i32> [[Q]]
+v4i vdiv(v4i a, v4i b) { return a / b; }
+
+// The address of an object is a constant, but not one known to be safe.
+static int obj;
+// CHECK-LABEL: define {{.*}} @div_by_address(
+// OFF: sdiv i64 {{.*}}, ptrtoint (ptr @{{.*}}obj to i64)
+// ON: [[Q:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 {{.*}}, i64 ptrtoint (ptr @{{.*}}obj to i64), i1 {{.*}})
+// ON-NEXT: freeze i64 [[Q]]
+long div_by_address(long a) { return a / (long)&obj; }
+
+// Matrix division by a scalar is not changed.
+typedef int m2x2 __attribute__((matrix_type(2, 2)));
+// CHECK-LABEL: define {{.*}} @matdiv(
+// CHECK-NOT: llvm.masked
+// CHECK: sdiv <4 x i32>
+m2x2 matdiv(m2x2 m, int b) { return m / b; }
+
+#ifdef __cplusplus
+typedef long v2l __attribute__((vector_size(16)));
+
+// CXXON-LABEL: define {{.*}} @vdiv_safe_const(
+// CXXON-NOT: llvm.masked
+// CXXON: sdiv <4 x i32> {{.*}}, <i32 1, i32 -2, i32 3, i32 -4>
+v4i vdiv_safe_const(v4i a) { return a / v4i{1, -2, 3, -4}; }
+
+// CXXON-LABEL: define {{.*}} @vdiv_mixed_zero(
+// CXXON: call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> {{.*}}, <4 x i32> <i32 2, i32 0, i32 4, i32 5>, <4 x i1>
+v4i vdiv_mixed_zero(v4i a) { return a / v4i{2, 0, 4, 5}; }
+
+// CXXON-LABEL: define {{.*}} @vdiv_ptr_lane(
+// CXXON: call <2 x i64> @llvm.masked.sdiv.v2i64(<2 x i64> {{.*}}, <2 x i64> <i64 ptrtoint (ptr @{{.*}}obj to i64), i64 2>, <2 x i1>
+v2l vdiv_ptr_lane(v2l a) { return a / v2l{(long)&obj, 2}; }
+}
+#endif
diff --git a/clang/test/CodeGenOpenCL/defined-int-divrem-O2.cl b/clang/test/CodeGenOpenCL/defined-int-divrem-O2.cl
new file mode 100644
index 00000000000000..484113d1ff4fdc
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/defined-int-divrem-O2.cl
@@ -0,0 +1,34 @@
+// OpenCL C v3.0 s6.5.1 gives integer division by zero an unspecified value,
+// so the optimizer must not treat it as undefined behavior and delete code
+// that a conforming kernel runs.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -O2 -Wno-division-by-zero -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple spirv64-unknown-unknown -cl-std=CL3.0 -O2 -Wno-division-by-zero -emit-llvm -o - %s | FileCheck %s
+
+// Without the defined lowering, a / b lets the optimizer assume b != 0 and
+// remove the store of 7.
+// CHECK-LABEL: define {{.*}} void @divisor_checked_after(
+// CHECK: store i32 7,
+// CHECK: ret void
+__kernel void divisor_checked_after(__global int *out, int a, int b) {
+ int q = a / b;
+ if (b == 0)
+ out[1] = 7;
+ out[0] = q;
+}
+
+// Without the defined lowering, 5 / 0 is undefined and the store to out[1] is
+// removed. Whatever value the division gives, out[1] is 33 or 44.
+// CHECK-LABEL: define {{.*}} void @constant_zero_divisor(
+// CHECK: store i32 11, ptr{{.*}} %out
+// CHECK: [[P1:%.*]] = getelementptr inbounds nuw i8, ptr{{.*}} %out, i64 4
+// CHECK: store i32 {{.*}}, ptr{{.*}} [[P1]]
+// CHECK: [[P2:%.*]] = getelementptr inbounds nuw i8, ptr{{.*}} %out, i64 8
+// CHECK: store i32 22, ptr{{.*}} [[P2]]
+// CHECK: ret void
+__kernel void constant_zero_divisor(__global int *out) {
+ out[0] = 11;
+ int v = 5 / 0;
+ out[1] = v == 0 ? 33 : 44;
+ out[2] = 22;
+}
diff --git a/clang/test/CodeGenOpenCL/defined-int-divrem.cl b/clang/test/CodeGenOpenCL/defined-int-divrem.cl
new file mode 100644
index 00000000000000..1e094252461262
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/defined-int-divrem.cl
@@ -0,0 +1,279 @@
+// OpenCL C v3.0 s6.5.1: integer division and remainder by zero, and signed
+// division overflow (INT_MIN / -1), result in an unspecified value. By default
+// for OpenCL C and C++ for OpenCL, integer / and % that may hit one of these
+// cases are emitted as a frozen llvm.masked.{s,u}{div,rem} whose mask is false
+// exactly for those operands. SPIR and SPIR-V get the guarded form instead.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,ON
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=clc++2021 -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,ON
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -fno-defined-int-divrem -fdefined-int-divrem -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,ON
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,GUARD
+// RUN: %clang_cc1 -triple spir-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,GUARD
+// RUN: %clang_cc1 -triple spirv64-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,GUARD
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,GUARD
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -fno-defined-int-divrem -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,OFF
+// RUN: %clang_cc1 -triple spirv64-unknown-unknown -cl-std=CL3.0 -fno-defined-int-divrem -disable-llvm-passes -emit-llvm -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,OFF
+
+typedef unsigned int uint;
+typedef unsigned long ulong;
+typedef unsigned short ushort;
+typedef int int4 __attribute__((ext_vector_type(4)));
+typedef uint uint4 __attribute__((ext_vector_type(4)));
+
+// Check the divisor via select before the dividend so a poison dividend lane
+// cannot poison the mask when the divisor is not -1.
+// CHECK-LABEL: @{{.*}}f_sdiv
+// ON: [[NZ:%.*]] = icmp ne i32 [[B:%.*]], 0
+// ON-NEXT: [[NMIN:%.*]] = icmp ne i32 [[A:%.*]], -2147483648
+// ON-NEXT: [[NM1:%.*]] = icmp ne i32 [[B]], -1
+// ON-NEXT: [[NOV:%.*]] = select i1 [[NM1]], i1 true, i1 [[NMIN]]
+// ON-NEXT: [[SAFE:%.*]] = select i1 [[NZ]], i1 [[NOV]], i1 false
+// ON-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[B]], i1 [[SAFE]])
+// ON-NEXT: [[R:%.*]] = freeze i32 [[Q]]
+// ON-NEXT: ret i32 [[R]]
+// GUARD: [[NZ:%.*]] = icmp ne i32 [[B:%.*]], 0
+// GUARD-NEXT: [[NMIN:%.*]] = icmp ne i32 [[A:%.*]], -2147483648
+// GUARD-NEXT: [[NM1:%.*]] = icmp ne i32 [[B]], -1
+// GUARD-NEXT: [[NOV:%.*]] = select i1 [[NM1]], i1 true, i1 [[NMIN]]
+// GUARD-NEXT: [[SAFE:%.*]] = select i1 [[NZ]], i1 [[NOV]], i1 false
+// GUARD-NEXT: [[D:%.*]] = select i1 [[SAFE]], i32 [[B]], i32 1
+// GUARD-NEXT: [[R:%.*]] = sdiv i32 [[A]], [[D]]
+// GUARD-NEXT: ret i32 [[R]]
+// OFF: [[R:%.*]] = sdiv i32 %a, %b
+// OFF-NEXT: ret i32 [[R]]
+int f_sdiv(int a, int b) { return a / b; }
+
+// CHECK-LABEL: @{{.*}}f_srem
+// ON: [[SAFE:%.*]] = select i1 {{.*}}, i1 {{.*}}, i1 false
+// ON-NEXT: [[Q:%.*]] = call i32 @llvm.masked.srem.i32(i32 %a, i32 %b, i1 [[SAFE]])
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[SAFE:%.*]] = select i1 {{.*}}, i1 {{.*}}, i1 false
+// GUARD-NEXT: [[D:%.*]] = select i1 [[SAFE]], i32 %b, i32 1
+// GUARD-NEXT: srem i32 %a, [[D]]
+// OFF: srem i32 %a, %b
+int f_srem(int a, int b) { return a % b; }
+
+// Unsigned operations only need a non-zero divisor.
+// CHECK-LABEL: @{{.*}}f_udiv
+// ON: [[NZ:%.*]] = icmp ne i32 %b, 0
+// ON-NEXT: [[Q:%.*]] = call i32 @llvm.masked.udiv.i32(i32 %a, i32 %b, i1 [[NZ]])
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[NZ:%.*]] = icmp ne i32 %b, 0
+// GUARD-NEXT: [[D:%.*]] = select i1 [[NZ]], i32 %b, i32 1
+// GUARD-NEXT: udiv i32 %a, [[D]]
+// OFF: udiv i32 %a, %b
+uint f_udiv(uint a, uint b) { return a / b; }
+
+// CHECK-LABEL: @{{.*}}f_urem
+// ON: [[NZ:%.*]] = icmp ne i64 %b, 0
+// ON-NEXT: [[Q:%.*]] = call i64 @llvm.masked.urem.i64(i64 %a, i64 %b, i1 [[NZ]])
+// ON-NEXT: freeze i64 [[Q]]
+// GUARD: [[NZ:%.*]] = icmp ne i64 %b, 0
+// GUARD-NEXT: [[D:%.*]] = select i1 [[NZ]], i64 %b, i64 1
+// GUARD-NEXT: urem i64 %a, [[D]]
+// OFF: urem i64 %a, %b
+ulong f_urem(ulong a, ulong b) { return a % b; }
+
+// CHECK-LABEL: @{{.*}}f_ldiv
+// ON: icmp ne i64 %a, -9223372036854775808
+// ON: [[Q:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 %b, i1
+// ON-NEXT: freeze i64 [[Q]]
+// GUARD: [[D:%.*]] = select i1 {{.*}}, i64 %b, i64 1
+// GUARD-NEXT: sdiv i64 %a, [[D]]
+// OFF: sdiv i64 %a, %b
+long f_ldiv(long a, long b) { return a / b; }
+
+// char and short are promoted to int first.
+// CHECK-LABEL: @{{.*}}f_char
+// CHECK: [[A:%.*]] = sext i8 %a to i32
+// CHECK: [[B:%.*]] = sext i8 %b to i32
+// ON: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 [[A]], i32 [[B]], i1
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[D:%.*]] = select i1 {{.*}}, i32 [[B]], i32 1
+// GUARD-NEXT: sdiv i32 [[A]], [[D]]
+// OFF: sdiv i32 [[A]], [[B]]
+char f_char(char a, char b) { return a / b; }
+
+// CHECK-LABEL: @{{.*}}f_ushort
+// CHECK: [[A:%.*]] = zext i16 %a to i32
+// CHECK: [[B:%.*]] = zext i16 %b to i32
+// ON: [[Q:%.*]] = call i32 @llvm.masked.srem.i32(i32 [[A]], i32 [[B]], i1
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[D:%.*]] = select i1 {{.*}}, i32 [[B]], i32 1
+// GUARD-NEXT: srem i32 [[A]], [[D]]
+// OFF: srem i32 [[A]], [[B]]
+ushort f_ushort(ushort a, ushort b) { return a % b; }
+
+// Vectors are checked lane by lane.
+// CHECK-LABEL: @{{.*}}f_vdiv
+// ON: [[NZ:%.*]] = icmp ne <4 x i32> [[B:%.*]], zeroinitializer
+// ON-NEXT: [[NMIN:%.*]] = icmp ne <4 x i32> [[A:%.*]], splat (i32 -2147483648)
+// ON-NEXT: [[NM1:%.*]] = icmp ne <4 x i32> [[B]], splat (i32 -1)
+// ON-NEXT: [[NOV:%.*]] = select <4 x i1> [[NM1]], <4 x i1> splat (i1 true), <4 x i1> [[NMIN]]
+// ON-NEXT: [[SAFE:%.*]] = select <4 x i1> [[NZ]], <4 x i1> [[NOV]], <4 x i1> zeroinitializer
+// ON-NEXT: [[Q:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> [[A]], <4 x i32> [[B]], <4 x i1> [[SAFE]])
+// ON-NEXT: freeze <4 x i32> [[Q]]
+// GUARD: [[SAFE:%.*]] = select <4 x i1> {{.*}}, <4 x i1> {{.*}}, <4 x i1> zeroinitializer
+// GUARD-NEXT: [[D:%.*]] = select <4 x i1> [[SAFE]], <4 x i32> [[B:%.*]], <4 x i32> splat (i32 1)
+// GUARD-NEXT: sdiv <4 x i32> [[A:%.*]], [[D]]
+// OFF: sdiv <4 x i32>
+int4 f_vdiv(int4 a, int4 b) { return a / b; }
+
+// CHECK-LABEL: @{{.*}}f_vurem
+// ON: [[NZ:%.*]] = icmp ne <4 x i32> [[B:%.*]], zeroinitializer
+// ON-NEXT: [[Q:%.*]] = call <4 x i32> @llvm.masked.urem.v4i32(<4 x i32> [[A:%.*]], <4 x i32> [[B]], <4 x i1> [[NZ]])
+// ON-NEXT: freeze <4 x i32> [[Q]]
+// GUARD: [[NZ:%.*]] = icmp ne <4 x i32> [[B:%.*]], zeroinitializer
+// GUARD-NEXT: [[D:%.*]] = select <4 x i1> [[NZ]], <4 x i32> [[B]], <4 x i32> splat (i32 1)
+// GUARD-NEXT: urem <4 x i32> [[A:%.*]], [[D]]
+// OFF: urem <4 x i32>
+uint4 f_vurem(uint4 a, uint4 b) { return a % b; }
+
+// A scalar divisor is splat first.
+// CHECK-LABEL: @{{.*}}f_vdiv_scalar
+// CHECK: [[SPLAT:%.*]] = shufflevector <4 x i32>
+// ON: icmp ne <4 x i32> [[SPLAT]], zeroinitializer
+// ON: [[Q:%.*]] = call <4 x i32> @llvm.masked.sdiv.v4i32(<4 x i32> %a, <4 x i32> [[SPLAT]], <4 x i1>
+// ON-NEXT: freeze <4 x i32> [[Q]]
+// GUARD: select <4 x i1> {{.*}}, <4 x i32> [[SPLAT]], <4 x i32> splat (i32 1)
+// OFF: sdiv <4 x i32> %a, [[SPLAT]]
+int4 f_vdiv_scalar(int4 a, int b) { return a / b; }
+
+// Compound assignments go through the same path.
+// CHECK-LABEL: @{{.*}}f_divassign
+// ON: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 %b, i1
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[D:%.*]] = select i1 {{.*}}, i32 %b, i32 1
+// GUARD-NEXT: sdiv i32 %a, [[D]]
+// OFF: sdiv i32 %a, %b
+int f_divassign(int a, int b) {
+ a /= b;
+ return a;
+}
+
+// CHECK-LABEL: @{{.*}}f_remassign
+// ON: [[Q:%.*]] = call <4 x i32> @llvm.masked.urem.v4i32(<4 x i32> %a, <4 x i32> %b, <4 x i1>
+// ON-NEXT: freeze <4 x i32> [[Q]]
+// GUARD: [[D:%.*]] = select <4 x i1> {{.*}}, <4 x i32> %b, <4 x i32> splat (i32 1)
+// GUARD-NEXT: urem <4 x i32> %a, [[D]]
+// OFF: urem <4 x i32> %a, %b
+uint4 f_remassign(uint4 a, uint4 b) {
+ a %= b;
+ return a;
+}
+
+// A constant divisor that rules out both cases keeps the plain instruction.
+// CHECK-LABEL: @{{.*}}f_div_const
+// CHECK-NOT: select
+// CHECK: sdiv i32 %a, 4
+// CHECK-NEXT: ret
+int f_div_const(int a) { return a / 4; }
+
+// A vector literal is emitted through a temporary, so the divisor is not a
+// constant yet when the operation is emitted.
+// CHECK-LABEL: @{{.*}}f_vrem_literal
+// ON: [[Q:%.*]] = call <4 x i32> @llvm.masked.srem.v4i32(<4 x i32> %a, <4 x i32> <i32 1, i32 -2, i32 3, i32 -4>, <4 x i1>
+// ON-NEXT: freeze <4 x i32> [[Q]]
+// GUARD: [[D:%.*]] = select <4 x i1> {{.*}}, <4 x i32> <i32 1, i32 -2, i32 3, i32 -4>, <4 x i32> splat (i32 1)
+// GUARD-NEXT: srem <4 x i32> %a, [[D]]
+// OFF: srem <4 x i32> %a, <i32 1, i32 -2, i32 3, i32 -4>
+int4 f_vrem_literal(int4 a) { return a % (int4)(1, -2, 3, -4); }
+
+// CHECK-LABEL: @{{.*}}f_vdiv_splat
+// CHECK-NOT: select
+// CHECK: sdiv <4 x i32> %a, splat (i32 4)
+// CHECK-NEXT: ret
+int4 f_vdiv_splat(int4 a) { return a / 4; }
+
+// For unsigned operations, all-ones is a safe divisor.
+// CHECK-LABEL: @{{.*}}f_udiv_allones
+// CHECK-NOT: select
+// CHECK: udiv i32 %a, -1
+// CHECK-NEXT: ret
+uint f_udiv_allones(uint a) { return a / 0xffffffffu; }
+
+// A constant dividend other than INT_MIN rules out overflow, but not a zero
+// divisor.
+// CHECK-LABEL: @{{.*}}f_div_const_dividend
+// ON: [[NZ:%.*]] = icmp ne i32 %b, 0
+// ON-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 7, i32 %b, i1 [[NZ]])
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[NZ:%.*]] = icmp ne i32 %b, 0
+// GUARD-NEXT: [[D:%.*]] = select i1 [[NZ]], i32 %b, i32 1
+// GUARD-NEXT: sdiv i32 7, [[D]]
+// OFF: sdiv i32 7, %b
+int f_div_const_dividend(int b) { return 7 / b; }
+
+// A constant INT_MIN dividend only overflows when the divisor is -1.
+// CHECK-LABEL: @{{.*}}f_div_min_dividend
+// ON: [[NZ:%.*]] = icmp ne i32 %b, 0
+// ON-NEXT: [[NM1:%.*]] = icmp ne i32 %b, -1
+// ON-NEXT: [[SAFE:%.*]] = select i1 [[NZ]], i1 [[NM1]], i1 false
+// ON-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 -2147483648, i32 %b, i1 [[SAFE]])
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[NZ:%.*]] = icmp ne i32 %b, 0
+// GUARD-NEXT: [[NM1:%.*]] = icmp ne i32 %b, -1
+// GUARD-NEXT: [[SAFE:%.*]] = select i1 [[NZ]], i1 [[NM1]], i1 false
+// GUARD-NEXT: [[D:%.*]] = select i1 [[SAFE]], i32 %b, i32 1
+// GUARD-NEXT: sdiv i32 -2147483648, [[D]]
+// OFF: sdiv i32 -2147483648, %b
+int f_div_min_dividend(int b) { return (-2147483647 - 1) / b; }
+
+// A constant divisor of -1 can overflow, and with a dividend other than
+// INT_MIN it cannot.
+// CHECK-LABEL: @{{.*}}f_div_minus_one
+// ON: [[NMIN:%.*]] = icmp ne i32 %a, -2147483648
+// ON-NEXT: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 -1, i1 [[NMIN]])
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: [[NMIN:%.*]] = icmp ne i32 %a, -2147483648
+// GUARD-NEXT: [[D:%.*]] = select i1 [[NMIN]], i32 -1, i32 1
+// GUARD-NEXT: sdiv i32 %a, [[D]]
+// OFF: sdiv i32 %a, -1
+int f_div_minus_one(int a) { return a / -1; }
+
+// CHECK-LABEL: @{{.*}}f_div_minus_one_const
+// CHECK-NOT: select
+// CHECK: ret i32 -5
+int f_div_minus_one_const(void) { return 5 / -1; }
+
+// Division by a constant zero has an unspecified result too.
+// CHECK-LABEL: @{{.*}}f_div_zero
+// ON: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 %a, i32 0, i1 false)
+// ON-NEXT: freeze i32 [[Q]]
+// GUARD: sdiv i32 %a, 1
+// OFF: sdiv i32 %a, 0
+int f_div_zero(int a) { return a / 0; }
+
+// A constant operand that is not a plain integer, such as the address of an
+// object, is not known to be safe.
+__constant int obj = 1;
+
+// CHECK-LABEL: @{{.*}}f_sdiv_by_address
+// ON: [[Q:%.*]] = call i64 @llvm.masked.sdiv.i64(i64 %a, i64 ptrtoint (ptr{{.*}} @obj to i64), i1 {{.*}})
+// ON-NEXT: freeze i64 [[Q]]
+// GUARD: [[D:%.*]] = select i1 {{.*}}, i64 ptrtoint (ptr{{.*}} @obj to i64), i64 1
+// GUARD-NEXT: sdiv i64 %a, [[D]]
+// OFF: sdiv i64 %a, ptrtoint (ptr{{.*}} @obj to i64)
+long f_sdiv_by_address(long a) { return a / (long)&obj; }
+
+// CHECK-LABEL: @{{.*}}f_srem_of_address
+// ON: [[Q:%.*]] = call i64 @llvm.masked.srem.i64(i64 ptrtoint (ptr{{.*}} @obj to i64), i64 %b, i1 {{.*}})
+// ON-NEXT: freeze i64 [[Q]]
+// GUARD: [[D:%.*]] = select i1 {{.*}}, i64 %b, i64 1
+// GUARD-NEXT: srem i64 ptrtoint (ptr{{.*}} @obj to i64), [[D]]
+// OFF: srem i64 ptrtoint (ptr{{.*}} @obj to i64), %b
+long f_srem_of_address(long b) { return (long)&obj % b; }
+
+// CHECK-LABEL: @{{.*}}f_udiv_by_address
+// ON: [[Q:%.*]] = call i64 @llvm.masked.udiv.i64(i64 %a, i64 ptrtoint (ptr{{.*}} @obj to i64), i1 {{.*}})
+// ON-NEXT: freeze i64 [[Q]]
+// GUARD: udiv i64 %a,
+// OFF: udiv i64 %a, ptrtoint (ptr{{.*}} @obj to i64)
+ulong f_udiv_by_address(ulong a) { return a / (ulong)&obj; }
+
+// CHECK-LABEL: @{{.*}}f_urem_by_address
+// ON: [[Q:%.*]] = call i64 @llvm.masked.urem.i64(i64 %a, i64 ptrtoint (ptr{{.*}} @obj to i64), i1 {{.*}})
+// ON-NEXT: freeze i64 [[Q]]
+// GUARD: urem i64 %a,
+// OFF: urem i64 %a, ptrtoint (ptr{{.*}} @obj to i64)
+ulong f_urem_by_address(ulong a) { return a % (ulong)&obj; }
diff --git a/clang/test/CodeGenOpenCL/ubsan-defined-int-divrem.cl b/clang/test/CodeGenOpenCL/ubsan-defined-int-divrem.cl
new file mode 100644
index 00000000000000..cacdcbabc09bc1
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/ubsan-defined-int-divrem.cl
@@ -0,0 +1,32 @@
+// Under -fdefined-int-divrem (the OpenCL default), the driver leaves
+// integer-divide-by-zero and signed-integer-overflow-divide out of
+// -fsanitize=undefined. When they are enabled explicitly, they are emitted as
+// always, with their configured recover and trap behavior, ahead of the
+// defined division.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s -fsanitize=integer-divide-by-zero,signed-integer-overflow-divide | FileCheck %s --check-prefixes=CHECK,FATAL
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s -fsanitize=integer-divide-by-zero,signed-integer-overflow-divide -fsanitize-recover=integer-divide-by-zero,signed-integer-overflow-divide | FileCheck %s --check-prefixes=CHECK,RECOVER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL3.0 -disable-llvm-passes -emit-llvm -o - %s -fsanitize=integer-divide-by-zero,signed-integer-overflow-divide -fsanitize-trap=integer-divide-by-zero,signed-integer-overflow-divide | FileCheck %s --check-prefixes=CHECK,TRAP
+
+// CHECK-LABEL: @f_sdiv(
+// CHECK: [[NZ:%.*]] = icmp ne i32 [[B:%.*]], 0
+// CHECK: [[NMIN:%.*]] = icmp ne i32 [[A:%.*]], -2147483648
+// CHECK: [[NM1:%.*]] = icmp ne i32 [[B]], -1
+// CHECK: [[OR:%.*]] = or i1 [[NMIN]], [[NM1]]
+// CHECK: and i1 [[NZ]], [[OR]]
+// FATAL: call void @__ubsan_handle_divrem_overflow_abort(
+// FATAL-NEXT: unreachable
+// RECOVER: call void @__ubsan_handle_divrem_overflow(
+// TRAP: call void @llvm.ubsantrap(i8 3)
+// TRAP-NEXT: unreachable
+// CHECK: [[Q:%.*]] = call i32 @llvm.masked.sdiv.i32(i32 {{.*}}, i32 {{.*}}, i1 {{.*}})
+// CHECK-NEXT: freeze i32 [[Q]]
+int f_sdiv(int a, int b) { return a / b; }
+
+// CHECK-LABEL: @f_urem(
+// FATAL: call void @__ubsan_handle_divrem_overflow_abort(
+// RECOVER: call void @__ubsan_handle_divrem_overflow(
+// TRAP: call void @llvm.ubsantrap(i8 3)
+// CHECK: [[Q:%.*]] = call i32 @llvm.masked.urem.i32(i32 {{.*}}, i32 {{.*}}, i1 {{.*}})
+// CHECK-NEXT: freeze i32 [[Q]]
+unsigned f_urem(unsigned a, unsigned b) { return a % b; }
diff --git a/clang/test/Driver/amdgpu-validate-sanitize.cl b/clang/test/Driver/amdgpu-validate-sanitize.cl
index 07056f469b2af6..68a3a82f1ee3fa 100644
--- a/clang/test/Driver/amdgpu-validate-sanitize.cl
+++ b/clang/test/Driver/amdgpu-validate-sanitize.cl
@@ -33,7 +33,9 @@
// CHECK-SAME: "-fsanitize=address"
-// GENERIC: "-fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion,local-bounds,alloc-token" "-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion" "-fsanitize-trap=local-bounds" "-fsanitize-merge=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+// This is OpenCL, where -fdefined-int-divrem is the default, so undefined
+// does not enable integer-divide-by-zero and signed-integer-overflow-divide.
+// GENERIC: "-fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,unreachable,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion,local-bounds,alloc-token" "-fsanitize-recover=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,float-divide-by-zero,function,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,vla-bound,unsigned-integer-overflow,unsigned-shift-base,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,implicit-bitfield-conversion" "-fsanitize-trap=local-bounds" "-fsanitize-merge=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
// FIXME: Should not be forwarding argument
diff --git a/clang/test/Driver/defined-int-divrem.c b/clang/test/Driver/defined-int-divrem.c
new file mode 100644
index 00000000000000..9c6e3e7b8355a4
--- /dev/null
+++ b/clang/test/Driver/defined-int-divrem.c
@@ -0,0 +1,17 @@
+// The driver forwards the last of -f[no-]defined-int-divrem to cc1; cc1
+// computes the default (on for OpenCL) itself.
+
+// RUN: %clang -### -c --target=x86_64-unknown-linux -fdefined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=ON
+// RUN: %clang -### -c --target=x86_64-unknown-linux -fno-defined-int-divrem -fdefined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=ON
+// RUN: %clang -### -c --target=x86_64-unknown-linux -fno-defined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=OFF
+// RUN: %clang -### -c --target=x86_64-unknown-linux -fdefined-int-divrem -fno-defined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=OFF
+// RUN: %clang -### -c --target=x86_64-unknown-linux -x cl -fno-defined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=OFF
+// RUN: %clang -### -c --target=x86_64-unknown-linux %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// RUN: %clang -### -c --target=x86_64-unknown-linux -x cl %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// RUN: %clang -### -c --target=spirv64 -x cl %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+
+// ON: "-fdefined-int-divrem"
+// ON-NOT: "-fno-defined-int-divrem"
+// OFF: "-fno-defined-int-divrem"
+// OFF-NOT: "-fdefined-int-divrem"
+// DEFAULT-NOT: defined-int-divrem"
diff --git a/clang/test/Driver/fsanitize-defined-int-divrem.c b/clang/test/Driver/fsanitize-defined-int-divrem.c
new file mode 100644
index 00000000000000..f7099899373a0c
--- /dev/null
+++ b/clang/test/Driver/fsanitize-defined-int-divrem.c
@@ -0,0 +1,101 @@
+/// Under -fdefined-int-divrem, which is the default for OpenCL C and C++ for
+/// OpenCL, integer division by zero and INT_MIN / -1 are not undefined, so
+/// -fsanitize=undefined leaves out integer-divide-by-zero and
+/// signed-integer-overflow-divide. Asking for them in any other way still
+/// enables them, and recover and trap work as always.
+
+/// -fsanitize=undefined, -fdefined-int-divrem in effect.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -x cl-header -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -x clcpp -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -cl-std=CL3.0 -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -fdefined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -fdefined-int-divrem -x c++ -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fno-defined-int-divrem -fdefined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined-trap %s 2>&1 | FileCheck %s --check-prefix=DROP
+/// cc1 gets -Xclang arguments after the driver's -f[no-]defined-int-divrem,
+/// so they decide.
+// RUN: %clang -### -c --target=x86_64-linux -fsanitize=undefined -Xclang -fdefined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -fsanitize=undefined -Xclang=-fdefined-int-divrem %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fno-defined-int-divrem -Xclang -fdefined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+// RUN: %clang -### -c --target=x86_64-linux -Xclang -cl-std=CL3.0 -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+/// A request that a later -fno-sanitize= cancels does not count.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=integer-divide-by-zero,signed-integer-overflow -fno-sanitize=integer-divide-by-zero,signed-integer-overflow -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=DROP
+/// Only the enabled set changes. The recover and merge lists still have the two
+/// checks, which does nothing unless they are enabled.
+// DROP: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+// DROP: "-fsanitize-recover={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,vla-bound"
+// DROP: "-fsanitize-merge={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+
+/// -fsanitize=undefined, -fdefined-int-divrem not in effect.
+// RUN: %clang -### -c --target=x86_64-linux -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -x c++ -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -fdefined-int-divrem -fno-defined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fno-defined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -Xclang -fno-defined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -Xclang=-fno-defined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -Xclang -fno-defined-int-divrem -fdefined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+/// -Xclang option values that happen to match flag names are not flags.
+// RUN: %clang -### -c --target=x86_64-linux -Xclang -main-file-name -Xclang -fdefined-int-divrem -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -Xclang -main-file-name -Xclang -cl-std=CL3.0 -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+/// Explicit requests, also in the same list as undefined.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined,integer-divide-by-zero,signed-integer-overflow-divide %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=integer-divide-by-zero,signed-integer-overflow -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=KEEP
+// KEEP: "-fsanitize={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+// KEEP: "-fsanitize-recover={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,vla-bound"
+
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined -fsanitize=integer %s 2>&1 | FileCheck %s --check-prefix=INTEGER
+// INTEGER: "-fsanitize={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,{{[^"]*}}"
+
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined,integer-divide-by-zero %s 2>&1 | FileCheck %s --check-prefix=ZERO
+// ZERO: "-fsanitize={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s --check-prefix=OVERFLOW
+// OVERFLOW: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+
+/// The trap, merge, skip-hot-cutoff and annotate-debug-info lists are computed
+/// as without -fdefined-int-divrem; an explicit check can trap.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined,integer-divide-by-zero -fsanitize-trap=undefined -fsanitize-skip-hot-cutoff=undefined=0.5 -fsanitize-annotate-debug-info=undefined %s 2>&1 | FileCheck %s --check-prefix=TRAP
+// TRAP: "-fsanitize={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+// TRAP-NOT: -fsanitize-recover
+// TRAP: "-fsanitize-trap={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+// TRAP: "-fsanitize-merge={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+// TRAP: "-fsanitize-skip-hot-cutoff={{[^"]*}},function=0.5,integer-divide-by-zero=0.5,nonnull-attribute=0.5,{{[^"]*}},signed-integer-overflow-arith=0.5,signed-integer-overflow-divide=0.5,unreachable=0.5,vla-bound=0.5"
+// TRAP: "-fsanitize-annotate-debug-info={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+
+/// A check requested only from cc1 (-Xclang -fsanitize=; cc1 adds up all
+/// -fsanitize= arguments) recovers, traps or aborts like the driver spelling.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined -Xclang -fsanitize=integer-divide-by-zero %s 2>&1 | FileCheck %s --check-prefix=XCLANG-RECOVER
+// XCLANG-RECOVER: "-fsanitize=integer-divide-by-zero"
+// XCLANG-RECOVER-SAME: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+// XCLANG-RECOVER-SAME: "-fsanitize-recover={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}}"
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined -fsanitize-trap=undefined -Xclang -fsanitize=integer-divide-by-zero %s 2>&1 | FileCheck %s --check-prefix=XCLANG-TRAP
+// XCLANG-TRAP: "-fsanitize=integer-divide-by-zero"
+// XCLANG-TRAP-SAME: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+// XCLANG-TRAP-NOT: -fsanitize-recover
+// XCLANG-TRAP-SAME: "-fsanitize-trap={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}}"
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined -fno-sanitize-recover=integer-divide-by-zero -Xclang -fsanitize=integer-divide-by-zero %s 2>&1 | FileCheck %s --check-prefix=XCLANG-FATAL
+// XCLANG-FATAL: "-fsanitize=integer-divide-by-zero"
+// XCLANG-FATAL-SAME: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+// XCLANG-FATAL-SAME: "-fsanitize-recover={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,vla-bound"
+
+/// -fno-sanitize-recover keeps working as always.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined,signed-integer-overflow-divide -fno-sanitize-recover=all %s 2>&1 | FileCheck %s --check-prefix=FATAL
+// FATAL: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+// FATAL-NOT: -fsanitize-recover
+
+/// The decision is made per input.
+// RUN: %clang -### -c --target=x86_64-linux -fsanitize=undefined -x c %s -x cl %s 2>&1 | FileCheck %s --check-prefix=MIXED
+// MIXED: "-cc1"
+// MIXED-SAME: "-fsanitize={{[^"]*}},function,integer-divide-by-zero,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,signed-integer-overflow-divide,unreachable,vla-bound"
+// MIXED-SAME: "-x" "c"
+// MIXED: "-cc1"
+// MIXED-SAME: "-fsanitize={{[^"]*}},function,nonnull-attribute,{{[^"]*}},signed-integer-overflow-arith,unreachable,vla-bound"
+// MIXED-SAME: "-x" "cl"
+
+/// When every other check of -fsanitize=undefined is disabled, dropping the two
+/// division checks leaves no -fsanitize= flag on cc1.
+// RUN: %clang -### -c --target=x86_64-linux -x cl -fsanitize=undefined -fno-sanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,function,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift,signed-integer-overflow-arith,unreachable,vla-bound %s 2>&1 | FileCheck %s --check-prefix=EMPTY
+// EMPTY: "-cc1"
+// EMPTY-NOT: "-fsanitize="
+// EMPTY-SAME: "-x" "cl"
diff --git a/clang/test/Sema/defined-int-divrem.c b/clang/test/Sema/defined-int-divrem.c
new file mode 100644
index 00000000000000..be3de302ec19a4
--- /dev/null
+++ b/clang/test/Sema/defined-int-divrem.c
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,undefined,c %s
+// RUN: %clang_cc1 -fsyntax-only -fdefined-int-divrem -verify=expected,defined,c %s
+// RUN: %clang_cc1 -fsyntax-only -ffixed-point -DFIXED -fdefined-int-divrem -verify=expected,defined,c,fixed %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -fdefined-int-divrem -verify=expected,defined,cxx %s
+
+// -Wdivision-by-zero follows -fdefined-int-divrem.
+
+void f(int x) {
+ (void)(x / 0); // defined-warning {{division by zero results in an unspecified value}} \
+ undefined-warning {{division by zero is undefined}}
+ (void)(x % 0); // defined-warning {{remainder by zero results in an unspecified value}} \
+ undefined-warning {{remainder by zero is undefined}}
+}
+
+#ifdef FIXED
+// Fixed-point division is not covered by -fdefined-int-divrem.
+void fx(_Accum x) {
+ (void)(x / 0); // fixed-warning {{division by zero is undefined}}
+ (void)(1 / 0.0r);
+}
+#endif
+
+// Constant evaluation is not changed.
+int g(int x) {
+ switch (x) {
+ case 1 / 0: // c-error {{expression is not an integer constant expression}} \
+ cxx-error {{case value is not a constant expression}} \
+ expected-note {{division by zero}}
+ return 1;
+ }
+ return 0;
+}
diff --git a/clang/test/SemaOpenCL/division-by-zero.cl b/clang/test/SemaOpenCL/division-by-zero.cl
new file mode 100644
index 00000000000000..28649cb41d94f8
--- /dev/null
+++ b/clang/test/SemaOpenCL/division-by-zero.cl
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -cl-std=CL3.0 -verify=expected,defined,c %s
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -cl-std=CL1.2 -verify=expected,defined,c %s
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -cl-std=clc++2021 -verify=expected,defined,cxx %s
+// RUN: %clang_cc1 -triple spir64-unknown-unknown -cl-std=CL3.0 -fno-defined-int-divrem -verify=expected,undefined,c %s
+
+// OpenCL C v3.0 s6.5.1: integer division and remainder by zero result in an
+// unspecified value. The warning stays, since a zero divisor is almost always
+// a bug, but it does not call the operation undefined.
+
+void f(int x) {
+ (void)(x / 0); // defined-warning {{division by zero results in an unspecified value}} \
+ undefined-warning {{division by zero is undefined}}
+ (void)(x % 0); // defined-warning {{remainder by zero results in an unspecified value}} \
+ undefined-warning {{remainder by zero is undefined}}
+ x /= 0; // defined-warning {{division by zero results in an unspecified value}} \
+ undefined-warning {{division by zero is undefined}}
+ x %= 0; // defined-warning {{remainder by zero results in an unspecified value}} \
+ undefined-warning {{remainder by zero is undefined}}
+}
+
+// Constant evaluation is not changed: a division by zero is still not a
+// constant expression, and INT_MIN / -1 still folds to INT_MIN.
+__constant int c = 1 / 0; // c-error {{initializer element is not a compile-time constant}} \
+ defined-warning {{division by zero results in an unspecified value}} \
+ undefined-warning {{division by zero is undefined}}
+__constant int m = (-2147483647 - 1) / -1;
+
+int g(int x) {
+ switch (x) {
+ case 1 / 0: // c-error {{expression is not an integer constant expression}} \
+ cxx-error {{case value is not a constant expression}} \
+ expected-note {{division by zero}}
+ return 1;
+ }
+ return 0;
+}
More information about the cfe-commits
mailing list