17 #include "llvm/IR/IntrinsicsAMDGPU.h" 18 #include "llvm/IR/IntrinsicsX86.h" 25 #define DEBUG_TYPE "instcombine" 29 struct AMDGPUImageDMaskIntrinsic {
33 #define GET_AMDGPUImageDMaskIntrinsicTable_IMPL 34 #include "InstCombineTables.inc" 42 const APInt &Demanded) {
43 assert(I &&
"No instruction?");
44 assert(OpNo < I->getNumOperands() &&
"Operand index too large");
66 bool InstCombiner::SimplifyDemandedInstructionBits(
Instruction &Inst) {
71 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
74 if (V == &Inst)
return true;
75 replaceInstUsesWith(Inst, V);
82 bool InstCombiner::SimplifyDemandedBits(
Instruction *
I,
unsigned OpNo,
83 const APInt &DemandedMask,
87 Value *NewVal = SimplifyDemandedUseBits(U.
get(), DemandedMask, Known,
89 if (!NewVal)
return false;
118 Value *InstCombiner::SimplifyDemandedUseBits(
Value *V,
APInt DemandedMask,
121 assert(V !=
nullptr &&
"Null pointer of Value???");
122 assert(Depth <= 6 &&
"Limit Search Depth");
128 "Value *V, DemandedMask and Known must have same BitWidth");
130 if (isa<Constant>(V)) {
152 return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
154 KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
167 case Instruction::And: {
169 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
170 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.
Zero, LHSKnown,
174 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
177 APInt IKnownZero = RHSKnown.
Zero | LHSKnown.Zero;
179 APInt IKnownOne = RHSKnown.
One & LHSKnown.One;
183 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
197 Known.
Zero = std::move(IKnownZero);
198 Known.
One = std::move(IKnownOne);
201 case Instruction::Or: {
203 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
204 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.
One, LHSKnown,
208 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
211 APInt IKnownZero = RHSKnown.
Zero & LHSKnown.Zero;
213 APInt IKnownOne = RHSKnown.
One | LHSKnown.One;
217 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
231 Known.
Zero = std::move(IKnownZero);
232 Known.
One = std::move(IKnownOne);
235 case Instruction::Xor: {
236 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
237 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
240 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
243 APInt IKnownZero = (RHSKnown.
Zero & LHSKnown.Zero) |
244 (RHSKnown.
One & LHSKnown.One);
246 APInt IKnownOne = (RHSKnown.
Zero & LHSKnown.One) |
247 (RHSKnown.
One & LHSKnown.Zero);
251 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
268 return InsertNewInstWith(Or, *I);
278 ~RHSKnown.
One & DemandedMask);
280 return InsertNewInstWith(And, *I);
293 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
295 isa<ConstantInt>(LHSInst->getOperand(1)) &&
296 (LHSKnown.One & RHSKnown.
One & DemandedMask) != 0) {
297 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
299 APInt NewMask = ~(LHSKnown.One & RHSKnown.
One & DemandedMask);
304 InsertNewInstWith(NewAnd, *I);
308 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
309 return InsertNewInstWith(NewXor, *I);
313 Known.
Zero = std::move(IKnownZero);
315 Known.
One = std::move(IKnownOne);
346 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
347 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
350 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
357 auto CanonicalizeSelectConstant = [](
Instruction *
I,
unsigned OpNo,
358 APInt DemandedMask) {
375 if ((*CmpC & DemandedMask) == (*SelC & DemandedMask)) {
381 if (CanonicalizeSelectConstant(I, 1, DemandedMask) ||
382 CanonicalizeSelectConstant(I, 2, DemandedMask))
386 Known.
One = RHSKnown.
One & LHSKnown.One;
387 Known.
Zero = RHSKnown.
Zero & LHSKnown.Zero;
390 case Instruction::ZExt:
391 case Instruction::Trunc: {
396 if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1))
401 assert(!Known.hasConflict() &&
"Bits known to be one AND zero?");
404 case Instruction::BitCast:
411 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
421 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
425 case Instruction::SExt: {
429 APInt InputDemandedBits = DemandedMask.
trunc(SrcBitWidth);
434 InputDemandedBits.
setBit(SrcBitWidth-1);
437 if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1))
446 return InsertNewInstWith(NewCast, *I);
451 Known = InputKnown.
sext(BitWidth);
456 case Instruction::Sub: {
464 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
466 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
492 NSW, LHSKnown, RHSKnown);
495 case Instruction::Shl: {
501 if (
Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA,
502 DemandedMask, Known))
506 APInt DemandedMaskIn(DemandedMask.
lshr(ShiftAmt));
510 if (IOp->hasNoSignedWrap())
511 DemandedMaskIn.setHighBits(ShiftAmt+1);
512 else if (IOp->hasNoUnsignedWrap())
513 DemandedMaskIn.setHighBits(ShiftAmt);
515 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
518 Known.
Zero <<= ShiftAmt;
519 Known.
One <<= ShiftAmt;
526 case Instruction::LShr: {
532 APInt DemandedMaskIn(DemandedMask.
shl(ShiftAmt));
536 if (cast<LShrOperator>(I)->isExact())
539 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
549 case Instruction::AShr: {
558 return InsertNewInstWith(NewVal, *I);
571 APInt DemandedMaskIn(DemandedMask.
shl(ShiftAmt));
579 if (cast<AShrOperator>(I)->isExact())
580 DemandedMaskIn.setLowBits(ShiftAmt);
582 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
590 BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
596 assert(BitWidth > ShiftAmt &&
"Shift amount not saturated?");
597 if (Known.
Zero[BitWidth-ShiftAmt-1] ||
601 LShr->
setIsExact(cast<BinaryOperator>(I)->isExact());
602 return InsertNewInstWith(LShr, *I);
603 }
else if (Known.
One[BitWidth-ShiftAmt-1]) {
604 Known.
One |= HighBits;
609 case Instruction::UDiv: {
614 if (cast<UDivOperator>(I)->isExact())
619 APInt DemandedMaskIn =
621 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1))
626 BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros));
630 case Instruction::SRem:
634 if (Rem->isMinusOne())
636 APInt RA = Rem->getValue().abs();
638 if (DemandedMask.
ult(RA))
641 APInt LowBits = RA - 1;
643 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
647 Known.
Zero = LHSKnown.Zero & LowBits;
648 Known.
One = LHSKnown.One & LowBits;
652 if (LHSKnown.isNonNegative() || LowBits.
isSubsetOf(LHSKnown.Zero))
653 Known.
Zero |= ~LowBits;
657 if (LHSKnown.isNegative() && LowBits.
intersects(LHSKnown.One))
658 Known.
One |= ~LowBits;
670 if (LHSKnown.isNonNegative())
674 case Instruction::URem: {
677 if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
678 SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
687 switch (II->getIntrinsicID()) {
689 case Intrinsic::bswap: {
701 if (BitWidth-NLZ-NTZ == 8) {
702 unsigned ResultBit = NTZ;
703 unsigned InputBit = BitWidth-NTZ-8;
708 if (InputBit > ResultBit)
709 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
712 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
715 return InsertNewInstWith(NewVal, *I);
721 case Intrinsic::fshr:
722 case Intrinsic::fshl: {
729 uint64_t ShiftAmt = SA->
urem(BitWidth);
730 if (II->getIntrinsicID() == Intrinsic::fshr)
731 ShiftAmt = BitWidth - ShiftAmt;
733 APInt DemandedMaskLHS(DemandedMask.
lshr(ShiftAmt));
734 APInt DemandedMaskRHS(DemandedMask.
shl(BitWidth - ShiftAmt));
735 if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown, Depth + 1) ||
736 SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1))
739 Known.
Zero = LHSKnown.Zero.
shl(ShiftAmt) |
740 RHSKnown.
Zero.
lshr(BitWidth - ShiftAmt);
741 Known.
One = LHSKnown.One.
shl(ShiftAmt) |
742 RHSKnown.
One.
lshr(BitWidth - ShiftAmt);
745 case Intrinsic::x86_mmx_pmovmskb:
746 case Intrinsic::x86_sse_movmsk_ps:
747 case Intrinsic::x86_sse2_movmsk_pd:
748 case Intrinsic::x86_sse2_pmovmskb_128:
749 case Intrinsic::x86_avx_movmsk_ps_256:
750 case Intrinsic::x86_avx_movmsk_pd_256:
751 case Intrinsic::x86_avx2_pmovmskb: {
755 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
758 auto Arg = II->getArgOperand(0);
759 auto ArgType = cast<VectorType>(
Arg->getType());
760 ArgWidth = ArgType->getNumElements();
773 case Intrinsic::x86_sse42_crc32_64_64:
793 const APInt &DemandedMask,
808 case Instruction::And: {
821 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
832 Known.
Zero = std::move(IKnownZero);
833 Known.
One = std::move(IKnownOne);
836 case Instruction::Or: {
852 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
863 Known.
Zero = std::move(IKnownZero);
864 Known.
One = std::move(IKnownOne);
867 case Instruction::Xor: {
877 (RHSKnown.
One & LHSKnown.
One);
880 (RHSKnown.
One & LHSKnown.
Zero);
884 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
895 Known.
Zero = std::move(IKnownZero);
897 Known.
One = std::move(IKnownOne);
934 InstCombiner::simplifyShrShlDemandedBits(
Instruction *Shr,
const APInt &ShrOp1,
936 const APInt &DemandedMask,
938 if (!ShlOp1 || !ShrOp1)
944 if (ShlOp1.
uge(BitWidth) || ShrOp1.
uge(BitWidth))
952 Known.
Zero &= DemandedMask;
957 bool isLshr = (Shr->
getOpcode() == Instruction::LShr);
958 BitMask1 = isLshr ? (BitMask1.
lshr(ShrAmt) << ShlAmt) :
959 (BitMask1.
ashr(ShrAmt) << ShlAmt);
961 if (ShrAmt <= ShlAmt) {
962 BitMask2 <<= (ShlAmt - ShrAmt);
964 BitMask2 = isLshr ? BitMask2.
lshr(ShrAmt - ShlAmt):
965 BitMask2.
ashr(ShrAmt - ShlAmt);
969 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
970 if (ShrAmt == ShlAmt)
977 if (ShrAmt < ShlAmt) {
979 New = BinaryOperator::CreateShl(VarX, Amt);
985 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
986 BinaryOperator::CreateAShr(VarX, Amt);
987 if (cast<BinaryOperator>(Shr)->isExact())
988 New->setIsExact(
true);
991 return InsertNewInstWith(New, *Shl);
1028 unsigned NewDMaskVal = 0;
1029 unsigned OrigLoadIdx = 0;
1030 for (
unsigned SrcIdx = 0; SrcIdx < 4; ++SrcIdx) {
1031 const unsigned Bit = 1 << SrcIdx;
1032 if (!!(DMaskVal & Bit)) {
1033 if (!!DemandedElts[OrigLoadIdx])
1039 if (DMaskVal != NewDMaskVal)
1047 if (NewNumElts >= VWidth && DemandedElts.
isMask()) {
1069 OverloadTys[0] = NewTy;
1077 Args[DMaskIdx] = NewDMask;
1080 Builder.SetInsertPoint(II);
1082 CallInst *NewCall = Builder.CreateCall(NewIntrin, Args);
1086 if (NewNumElts == 1) {
1092 unsigned NewLoadIdx = 0;
1093 for (
unsigned OrigLoadIdx = 0; OrigLoadIdx < VWidth; ++OrigLoadIdx) {
1094 if (!!DemandedElts[OrigLoadIdx])
1101 Builder.CreateShuffleVector(NewCall,
UndefValue::get(NewTy), EltMask);
1119 Value *InstCombiner::SimplifyDemandedVectorElts(
Value *V,
APInt DemandedElts,
1122 bool AllowMultipleUsers) {
1125 assert((DemandedElts & ~EltMask) == 0 &&
"Invalid DemandedElts!");
1127 if (isa<UndefValue>(V)) {
1129 UndefElts = EltMask;
1134 UndefElts = EltMask;
1140 if (
auto *
C = dyn_cast<Constant>(V)) {
1146 Type *EltTy = cast<VectorType>(V->
getType())->getElementType();
1149 for (
unsigned i = 0; i != VWidth; ++i) {
1150 if (!DemandedElts[i]) {
1152 UndefElts.setBit(i);
1156 Constant *Elt =
C->getAggregateElement(i);
1157 if (!Elt)
return nullptr;
1159 if (isa<UndefValue>(Elt)) {
1161 UndefElts.setBit(i);
1169 return NewCV !=
C ? NewCV :
nullptr;
1176 if (!AllowMultipleUsers) {
1189 DemandedElts = EltMask;
1194 if (!I)
return nullptr;
1196 bool MadeChange =
false;
1197 auto simplifyAndSetOp = [&](
Instruction *Inst,
unsigned OpNum,
1201 if (
Value *V = SimplifyDemandedVectorElts(Op, Demanded,
Undef, Depth + 1)) {
1210 APInt UndefElts2(VWidth, 0);
1211 APInt UndefElts3(VWidth, 0);
1215 case Instruction::GetElementPtr: {
1225 if (mayIndexStructType(cast<GetElementPtrInst>(*I)))
1236 UndefElts = EltMask;
1240 APInt UndefEltsOp(VWidth, 0);
1241 simplifyAndSetOp(I, i, DemandedElts, UndefEltsOp);
1242 UndefElts |= UndefEltsOp;
1248 case Instruction::InsertElement: {
1255 simplifyAndSetOp(I, 0, DemandedElts, UndefElts2);
1262 APInt PreInsertDemandedElts = DemandedElts;
1264 PreInsertDemandedElts.
clearBit(IdxNo);
1266 simplifyAndSetOp(I, 0, PreInsertDemandedElts, UndefElts);
1270 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1276 UndefElts.clearBit(IdxNo);
1279 case Instruction::ShuffleVector: {
1280 auto *Shuffle = cast<ShuffleVectorInst>(
I);
1281 assert(Shuffle->getOperand(0)->getType() ==
1282 Shuffle->getOperand(1)->getType() &&
1283 "Expected shuffle operands to have same type");
1285 Shuffle->getOperand(0)->getType()->getVectorNumElements();
1286 APInt LeftDemanded(OpWidth, 0), RightDemanded(OpWidth, 0);
1287 for (
unsigned i = 0; i < VWidth; i++) {
1288 if (DemandedElts[i]) {
1289 unsigned MaskVal = Shuffle->getMaskValue(i);
1290 if (MaskVal != -1u) {
1291 assert(MaskVal < OpWidth * 2 &&
1292 "shufflevector mask index out of range!");
1293 if (MaskVal < OpWidth)
1294 LeftDemanded.setBit(MaskVal);
1296 RightDemanded.
setBit(MaskVal - OpWidth);
1301 APInt LHSUndefElts(OpWidth, 0);
1302 simplifyAndSetOp(I, 0, LeftDemanded, LHSUndefElts);
1304 APInt RHSUndefElts(OpWidth, 0);
1305 simplifyAndSetOp(I, 1, RightDemanded, RHSUndefElts);
1318 if (VWidth == OpWidth) {
1319 bool IsIdentityShuffle =
true;
1320 for (
unsigned i = 0; i < VWidth; i++) {
1321 unsigned MaskVal = Shuffle->getMaskValue(i);
1322 if (DemandedElts[i] && i != MaskVal) {
1323 IsIdentityShuffle =
false;
1327 if (IsIdentityShuffle)
1328 return Shuffle->getOperand(0);
1331 bool NewUndefElts =
false;
1332 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1333 unsigned RHSIdx = -1u, RHSValIdx = -1u;
1334 bool LHSUniform =
true;
1335 bool RHSUniform =
true;
1336 for (
unsigned i = 0; i < VWidth; i++) {
1337 unsigned MaskVal = Shuffle->getMaskValue(i);
1338 if (MaskVal == -1u) {
1339 UndefElts.setBit(i);
1340 }
else if (!DemandedElts[i]) {
1341 NewUndefElts =
true;
1342 UndefElts.setBit(i);
1343 }
else if (MaskVal < OpWidth) {
1344 if (LHSUndefElts[MaskVal]) {
1345 NewUndefElts =
true;
1346 UndefElts.setBit(i);
1348 LHSIdx = LHSIdx == -1u ? i : OpWidth;
1349 LHSValIdx = LHSValIdx == -1u ? MaskVal : OpWidth;
1350 LHSUniform = LHSUniform && (MaskVal == i);
1353 if (RHSUndefElts[MaskVal - OpWidth]) {
1354 NewUndefElts =
true;
1355 UndefElts.setBit(i);
1357 RHSIdx = RHSIdx == -1u ? i : OpWidth;
1358 RHSValIdx = RHSValIdx == -1u ? MaskVal - OpWidth : OpWidth;
1359 RHSUniform = RHSUniform && (MaskVal - OpWidth == i);
1368 if (OpWidth == Shuffle->getType()->getNumElements()) {
1374 if (LHSIdx < OpWidth && RHSUniform) {
1375 if (
auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1376 Op = Shuffle->getOperand(1);
1381 if (RHSIdx < OpWidth && LHSUniform) {
1382 if (
auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1383 Op = Shuffle->getOperand(0);
1393 InsertNewInstWith(New, *Shuffle);
1400 for (
unsigned i = 0; i < VWidth; ++i) {
1405 Shuffle->getMaskValue(i)));
1422 simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1426 APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);
1427 if (
auto *CV = dyn_cast<ConstantVector>(Sel->
getCondition())) {
1428 for (
unsigned i = 0; i < VWidth; i++) {
1432 if (isa<ConstantExpr>(CElt))
1438 DemandedLHS.clearBit(i);
1444 simplifyAndSetOp(I, 1, DemandedLHS, UndefElts2);
1445 simplifyAndSetOp(I, 2, DemandedRHS, UndefElts3);
1449 UndefElts = UndefElts2 & UndefElts3;
1452 case Instruction::BitCast: {
1457 APInt InputDemandedElts(InVWidth, 0);
1458 UndefElts2 =
APInt(InVWidth, 0);
1461 if (VWidth == InVWidth) {
1465 InputDemandedElts = DemandedElts;
1466 }
else if ((VWidth % InVWidth) == 0) {
1470 Ratio = VWidth / InVWidth;
1471 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1472 if (DemandedElts[OutIdx])
1473 InputDemandedElts.
setBit(OutIdx / Ratio);
1474 }
else if ((InVWidth % VWidth) == 0) {
1478 Ratio = InVWidth / VWidth;
1479 for (
unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1480 if (DemandedElts[InIdx / Ratio])
1481 InputDemandedElts.
setBit(InIdx);
1487 simplifyAndSetOp(I, 0, InputDemandedElts, UndefElts2);
1489 if (VWidth == InVWidth) {
1490 UndefElts = UndefElts2;
1491 }
else if ((VWidth % InVWidth) == 0) {
1495 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1496 if (UndefElts2[OutIdx / Ratio])
1497 UndefElts.
setBit(OutIdx);
1498 }
else if ((InVWidth % VWidth) == 0) {
1502 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1505 UndefElts.
setBit(OutIdx);
1512 case Instruction::FPTrunc:
1513 case Instruction::FPExt:
1514 simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1521 case Intrinsic::masked_gather:
1522 case Intrinsic::masked_load: {
1527 DemandedPassThrough(DemandedElts);
1528 if (
auto *CV = dyn_cast<ConstantVector>(II->
getOperand(2)))
1529 for (
unsigned i = 0; i < VWidth; i++) {
1532 DemandedPtrs.clearBit(i);
1537 simplifyAndSetOp(II, 0, DemandedPtrs, UndefElts2);
1538 simplifyAndSetOp(II, 3, DemandedPassThrough, UndefElts3);
1542 UndefElts = UndefElts2 & UndefElts3;
1545 case Intrinsic::x86_xop_vfrcz_ss:
1546 case Intrinsic::x86_xop_vfrcz_sd:
1551 if (!DemandedElts[0]) {
1558 simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1561 UndefElts = UndefElts[0];
1565 case Intrinsic::x86_sse_rcp_ss:
1566 case Intrinsic::x86_sse_rsqrt_ss:
1567 simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1570 if (!DemandedElts[0]) {
1581 case Intrinsic::x86_sse_min_ss:
1582 case Intrinsic::x86_sse_max_ss:
1583 case Intrinsic::x86_sse_cmp_ss:
1584 case Intrinsic::x86_sse2_min_sd:
1585 case Intrinsic::x86_sse2_max_sd:
1586 case Intrinsic::x86_sse2_cmp_sd: {
1587 simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1590 if (!DemandedElts[0]) {
1597 simplifyAndSetOp(II, 1, DemandedElts, UndefElts2);
1602 UndefElts.clearBit(0);
1609 case Intrinsic::x86_sse41_round_ss:
1610 case Intrinsic::x86_sse41_round_sd: {
1612 APInt DemandedElts2 = DemandedElts;
1614 simplifyAndSetOp(II, 0, DemandedElts2, UndefElts);
1617 if (!DemandedElts[0]) {
1624 simplifyAndSetOp(II, 1, DemandedElts, UndefElts2);
1628 UndefElts.clearBit(0);
1629 UndefElts |= UndefElts2[0];
1636 case Intrinsic::x86_avx512_mask_add_ss_round:
1637 case Intrinsic::x86_avx512_mask_div_ss_round:
1638 case Intrinsic::x86_avx512_mask_mul_ss_round:
1639 case Intrinsic::x86_avx512_mask_sub_ss_round:
1640 case Intrinsic::x86_avx512_mask_max_ss_round:
1641 case Intrinsic::x86_avx512_mask_min_ss_round:
1642 case Intrinsic::x86_avx512_mask_add_sd_round:
1643 case Intrinsic::x86_avx512_mask_div_sd_round:
1644 case Intrinsic::x86_avx512_mask_mul_sd_round:
1645 case Intrinsic::x86_avx512_mask_sub_sd_round:
1646 case Intrinsic::x86_avx512_mask_max_sd_round:
1647 case Intrinsic::x86_avx512_mask_min_sd_round:
1648 simplifyAndSetOp(II, 0, DemandedElts, UndefElts);
1651 if (!DemandedElts[0]) {
1658 simplifyAndSetOp(II, 1, DemandedElts, UndefElts2);
1659 simplifyAndSetOp(II, 2, DemandedElts, UndefElts3);
1663 if (!UndefElts2[0] || !UndefElts3[0])
1664 UndefElts.clearBit(0);
1668 case Intrinsic::x86_sse2_packssdw_128:
1669 case Intrinsic::x86_sse2_packsswb_128:
1670 case Intrinsic::x86_sse2_packuswb_128:
1671 case Intrinsic::x86_sse41_packusdw:
1672 case Intrinsic::x86_avx2_packssdw:
1673 case Intrinsic::x86_avx2_packsswb:
1674 case Intrinsic::x86_avx2_packusdw:
1675 case Intrinsic::x86_avx2_packuswb:
1676 case Intrinsic::x86_avx512_packssdw_512:
1677 case Intrinsic::x86_avx512_packsswb_512:
1678 case Intrinsic::x86_avx512_packusdw_512:
1679 case Intrinsic::x86_avx512_packuswb_512: {
1682 assert(VWidth == (InnerVWidth * 2) &&
"Unexpected input size");
1684 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1685 unsigned VWidthPerLane = VWidth / NumLanes;
1686 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1692 for (
int OpNum = 0; OpNum != 2; ++OpNum) {
1693 APInt OpDemandedElts(InnerVWidth, 0);
1694 for (
unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1695 unsigned LaneIdx = Lane * VWidthPerLane;
1696 for (
unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1697 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1698 if (DemandedElts[Idx])
1699 OpDemandedElts.
setBit((Lane * InnerVWidthPerLane) + Elt);
1704 APInt OpUndefElts(InnerVWidth, 0);
1705 simplifyAndSetOp(II, OpNum, OpDemandedElts, OpUndefElts);
1708 OpUndefElts = OpUndefElts.
zext(VWidth);
1709 for (
unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1710 APInt LaneElts = OpUndefElts.
lshr(InnerVWidthPerLane * Lane);
1711 LaneElts = LaneElts.
getLoBits(InnerVWidthPerLane);
1712 LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
1713 UndefElts |= LaneElts;
1720 case Intrinsic::x86_ssse3_pshuf_b_128:
1721 case Intrinsic::x86_avx2_pshuf_b:
1722 case Intrinsic::x86_avx512_pshuf_b_512:
1724 case Intrinsic::x86_avx_vpermilvar_ps:
1725 case Intrinsic::x86_avx_vpermilvar_ps_256:
1726 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1727 case Intrinsic::x86_avx_vpermilvar_pd:
1728 case Intrinsic::x86_avx_vpermilvar_pd_256:
1729 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1731 case Intrinsic::x86_avx2_permd:
1732 case Intrinsic::x86_avx2_permps: {
1733 simplifyAndSetOp(II, 1, DemandedElts, UndefElts);
1739 case Intrinsic::x86_sse4a_extrq:
1740 case Intrinsic::x86_sse4a_extrqi:
1741 case Intrinsic::x86_sse4a_insertq:
1742 case Intrinsic::x86_sse4a_insertqi:
1743 UndefElts.setHighBits(VWidth / 2);
1745 case Intrinsic::amdgcn_buffer_load:
1746 case Intrinsic::amdgcn_buffer_load_format:
1747 case Intrinsic::amdgcn_raw_buffer_load:
1748 case Intrinsic::amdgcn_raw_buffer_load_format:
1749 case Intrinsic::amdgcn_raw_tbuffer_load:
1750 case Intrinsic::amdgcn_struct_buffer_load:
1751 case Intrinsic::amdgcn_struct_buffer_load_format:
1752 case Intrinsic::amdgcn_struct_tbuffer_load:
1753 case Intrinsic::amdgcn_tbuffer_load:
1754 return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts);
1757 return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts, 0);
1770 simplifyAndSetOp(I, 0, DemandedElts, UndefElts);
1771 simplifyAndSetOp(I, 1, DemandedElts, UndefElts2);
1781 UndefElts &= UndefElts2;
1786 if (UndefElts.isAllOnesValue())
1789 return MadeChange ?
I :
nullptr;
void clearAllBits()
Set every bit to 0.
Type * getVectorElementType() const
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
void setSignBit()
Set the sign bit to 1.
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
uint64_t getZExtValue() const
Get zero extended value.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
bool hasConflict() const
Returns true if there is conflicting information.
This class represents zero extension of integer types.
void push_back(const T &Elt)
static ConstantAggregateZero * get(Type *Ty)
APInt zext(unsigned width) const
Zero extend to a new width.
This class represents a function call, abstracting a target machine's calling convention.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
gep_type_iterator gep_type_end(const User *GEP)
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
LLVMContext & getContext() const
All values hold a context through their type.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
const Use & getOperandUse(unsigned i) const
APInt trunc(unsigned width) const
Truncate to new width.
void setAllBits()
Set every bit to 1.
void setArgOperand(unsigned i, Value *v)
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
bool isVectorTy() const
True if this is an instance of VectorType.
unsigned getBitWidth() const
Get the bit width of this value.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Value * getArgOperand(unsigned i) const
SI optimize exec mask operations pre RA
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
bool match(Val *V, const Pattern &P)
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
void setBit(unsigned BitPosition)
Set a given bit to 1.
This class represents the LLVM 'select' instruction.
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
This is the base class for all instructions that perform data casts.
static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS, KnownBits RHS)
Compute known bits resulting from adding LHS and RHS.
APInt shl(unsigned shiftAmt) const
Left-shift function.
A Use represents the edge between a Value definition and its users.
void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs...
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
KnownBits zextOrTrunc(unsigned BitWidth, bool ExtendedBitsAreKnownZero) const
Extends or truncates the underlying known Zero and One bits.
unsigned getActiveBits() const
Compute the number of active bits in the value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Class to represent function types.
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
Type * getType() const
All values are typed, get the type of this value.
void clearBit(unsigned BitPosition)
Set a given bit to 0.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
const APInt & getValue() const
Return the constant as an APInt value reference.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
static bool hasNoSignedWrap(BinaryOperator &I)
void takeName(Value *V)
Transfer the name from V to this value.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Value * getOperand(unsigned i) const
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits...
bool isAllOnesValue() const
Determine if all bits are set.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned countPopulation() const
Count the number of bits set.
The instances of the Type class are immutable: once they are created, they are never changed...
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
void resetAll()
Resets the known state of all bits.
bool isMask(unsigned numBits) const
bool isOneValue() const
Determine if this is a value of 1.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
const Value * getCondition() const
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
void makeNonNegative()
Make this value non-negative.
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
unsigned getNumOperands() const
unsigned countPopulation(T Value)
Count the number of set bits in a value.
This is the shared class of boolean and integer constants.
SelectPatternFlavor Flavor
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl< IITDescriptor > &T)
Return the IIT table descriptor for the specified intrinsic into an array of IITDescriptors.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
SelectPatternFlavor
Specific patterns of select instructions we can match.
MatchIntrinsicTypesResult matchIntrinsicSignature(FunctionType *FTy, ArrayRef< IITDescriptor > &Infos, SmallVectorImpl< Type *> &ArgTys)
Match the specified function type with the type constraints specified by the .td file.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
KnownBits sext(unsigned BitWidth) const
Sign extends the underlying known Zero and One bits.
void setOperand(unsigned i, Value *Val)
unsigned getVectorNumElements() const
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Class to represent vector types.
Class for arbitrary precision integers.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
unsigned getNumArgOperands() const
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
static IntegerType * getInt32Ty(LLVMContext &C)
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
StringRef getName() const
Return a constant reference to the value's name.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
const Function * getParent() const
Return the enclosing method, or null if none.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, const APInt &Demanded)
Check to see if the specified operand of the specified instruction is a constant integer.
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
bool hasOneUse() const
Return true if there is exactly one user of this value.
unsigned countLeadingOnes() const
Count the number of leading one bits.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
static Constant * get(ArrayRef< Constant *> V)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
bool isNullValue() const
Determine if all bits are clear.
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const
gep_type_iterator gep_type_begin(const User *GEP)