24 #define DEBUG_TYPE "instcombine" 28 struct AMDGPUImageDMaskIntrinsic {
32 #define GET_AMDGPUImageDMaskIntrinsicTable_IMPL 33 #include "InstCombineTables.inc" 41 const APInt &Demanded) {
42 assert(I &&
"No instruction?");
43 assert(OpNo < I->getNumOperands() &&
"Operand index too large");
65 bool InstCombiner::SimplifyDemandedInstructionBits(
Instruction &Inst) {
70 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
73 if (V == &Inst)
return true;
74 replaceInstUsesWith(Inst, V);
81 bool InstCombiner::SimplifyDemandedBits(
Instruction *
I,
unsigned OpNo,
82 const APInt &DemandedMask,
86 Value *NewVal = SimplifyDemandedUseBits(U.
get(), DemandedMask, Known,
88 if (!NewVal)
return false;
117 Value *InstCombiner::SimplifyDemandedUseBits(
Value *V,
APInt DemandedMask,
120 assert(V !=
nullptr &&
"Null pointer of Value???");
121 assert(Depth <= 6 &&
"Limit Search Depth");
127 "Value *V, DemandedMask and Known must have same BitWidth");
129 if (isa<Constant>(V)) {
151 return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
153 KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
166 case Instruction::And: {
168 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
169 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.
Zero, LHSKnown,
173 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
176 APInt IKnownZero = RHSKnown.
Zero | LHSKnown.Zero;
178 APInt IKnownOne = RHSKnown.
One & LHSKnown.One;
182 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
196 Known.
Zero = std::move(IKnownZero);
197 Known.
One = std::move(IKnownOne);
200 case Instruction::Or: {
202 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
203 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.
One, LHSKnown,
207 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
210 APInt IKnownZero = RHSKnown.
Zero & LHSKnown.Zero;
212 APInt IKnownOne = RHSKnown.
One | LHSKnown.One;
216 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
230 Known.
Zero = std::move(IKnownZero);
231 Known.
One = std::move(IKnownOne);
234 case Instruction::Xor: {
235 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
236 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
239 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
242 APInt IKnownZero = (RHSKnown.
Zero & LHSKnown.Zero) |
243 (RHSKnown.
One & LHSKnown.One);
245 APInt IKnownOne = (RHSKnown.
Zero & LHSKnown.One) |
246 (RHSKnown.
One & LHSKnown.Zero);
250 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
267 return InsertNewInstWith(Or, *I);
277 ~RHSKnown.
One & DemandedMask);
279 return InsertNewInstWith(And, *I);
292 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
294 isa<ConstantInt>(LHSInst->getOperand(1)) &&
295 (LHSKnown.One & RHSKnown.
One & DemandedMask) != 0) {
296 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
298 APInt NewMask = ~(LHSKnown.One & RHSKnown.
One & DemandedMask);
303 InsertNewInstWith(NewAnd, *I);
307 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
308 return InsertNewInstWith(NewXor, *I);
312 Known.
Zero = std::move(IKnownZero);
314 Known.
One = std::move(IKnownOne);
324 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
325 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
328 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
336 Known.
One = RHSKnown.
One & LHSKnown.One;
337 Known.
Zero = RHSKnown.
Zero & LHSKnown.Zero;
339 case Instruction::ZExt:
340 case Instruction::Trunc: {
345 if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1))
349 if (BitWidth > SrcBitWidth)
354 case Instruction::BitCast:
361 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
371 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
375 case Instruction::SExt: {
379 APInt InputDemandedBits = DemandedMask.
trunc(SrcBitWidth);
384 InputDemandedBits.
setBit(SrcBitWidth-1);
387 if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1))
396 return InsertNewInstWith(NewCast, *I);
401 Known = InputKnown.
sext(BitWidth);
406 case Instruction::Sub: {
414 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
416 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
440 bool NSW = cast<OverflowingBinaryOperator>(
I)->hasNoSignedWrap();
442 NSW, LHSKnown, RHSKnown);
445 case Instruction::Shl: {
451 if (
Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA,
452 DemandedMask, Known))
456 APInt DemandedMaskIn(DemandedMask.
lshr(ShiftAmt));
460 if (IOp->hasNoSignedWrap())
461 DemandedMaskIn.setHighBits(ShiftAmt+1);
462 else if (IOp->hasNoUnsignedWrap())
463 DemandedMaskIn.setHighBits(ShiftAmt);
465 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
468 Known.
Zero <<= ShiftAmt;
469 Known.
One <<= ShiftAmt;
476 case Instruction::LShr: {
482 APInt DemandedMaskIn(DemandedMask.
shl(ShiftAmt));
486 if (cast<LShrOperator>(I)->isExact())
489 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
499 case Instruction::AShr: {
508 return InsertNewInstWith(NewVal, *I);
521 APInt DemandedMaskIn(DemandedMask.
shl(ShiftAmt));
529 if (cast<AShrOperator>(I)->isExact())
530 DemandedMaskIn.setLowBits(ShiftAmt);
532 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
540 BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth)));
546 assert(BitWidth > ShiftAmt &&
"Shift amount not saturated?");
547 if (Known.
Zero[BitWidth-ShiftAmt-1] ||
551 LShr->
setIsExact(cast<BinaryOperator>(I)->isExact());
552 return InsertNewInstWith(LShr, *I);
553 }
else if (Known.
One[BitWidth-ShiftAmt-1]) {
554 Known.
One |= HighBits;
559 case Instruction::UDiv: {
564 if (cast<UDivOperator>(I)->isExact())
569 APInt DemandedMaskIn =
571 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1))
576 BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros));
580 case Instruction::SRem:
584 if (Rem->isMinusOne())
586 APInt RA = Rem->getValue().abs();
588 if (DemandedMask.
ult(RA))
591 APInt LowBits = RA - 1;
593 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
597 Known.
Zero = LHSKnown.Zero & LowBits;
598 Known.
One = LHSKnown.One & LowBits;
602 if (LHSKnown.isNonNegative() || LowBits.
isSubsetOf(LHSKnown.Zero))
603 Known.
Zero |= ~LowBits;
607 if (LHSKnown.isNegative() && LowBits.
intersects(LHSKnown.One))
608 Known.
One |= ~LowBits;
620 if (LHSKnown.isNonNegative())
624 case Instruction::URem: {
627 if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
628 SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
637 switch (II->getIntrinsicID()) {
639 case Intrinsic::bswap: {
651 if (BitWidth-NLZ-NTZ == 8) {
652 unsigned ResultBit = NTZ;
653 unsigned InputBit = BitWidth-NTZ-8;
658 if (InputBit > ResultBit)
659 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
662 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
665 return InsertNewInstWith(NewVal, *I);
671 case Intrinsic::x86_mmx_pmovmskb:
672 case Intrinsic::x86_sse_movmsk_ps:
673 case Intrinsic::x86_sse2_movmsk_pd:
674 case Intrinsic::x86_sse2_pmovmskb_128:
675 case Intrinsic::x86_avx_movmsk_ps_256:
676 case Intrinsic::x86_avx_movmsk_pd_256:
677 case Intrinsic::x86_avx2_pmovmskb: {
681 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
684 auto Arg = II->getArgOperand(0);
685 auto ArgType = cast<VectorType>(
Arg->
getType());
686 ArgWidth = ArgType->getNumElements();
699 case Intrinsic::x86_sse42_crc32_64_64:
719 const APInt &DemandedMask,
734 case Instruction::And: {
747 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
758 Known.
Zero = std::move(IKnownZero);
759 Known.
One = std::move(IKnownOne);
762 case Instruction::Or: {
778 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
789 Known.
Zero = std::move(IKnownZero);
790 Known.
One = std::move(IKnownOne);
793 case Instruction::Xor: {
803 (RHSKnown.
One & LHSKnown.
One);
806 (RHSKnown.
One & LHSKnown.
Zero);
810 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
821 Known.
Zero = std::move(IKnownZero);
823 Known.
One = std::move(IKnownOne);
860 InstCombiner::simplifyShrShlDemandedBits(
Instruction *Shr,
const APInt &ShrOp1,
862 const APInt &DemandedMask,
864 if (!ShlOp1 || !ShrOp1)
870 if (ShlOp1.
uge(BitWidth) || ShrOp1.
uge(BitWidth))
878 Known.
Zero &= DemandedMask;
883 bool isLshr = (Shr->
getOpcode() == Instruction::LShr);
884 BitMask1 = isLshr ? (BitMask1.
lshr(ShrAmt) << ShlAmt) :
885 (BitMask1.
ashr(ShrAmt) << ShlAmt);
887 if (ShrAmt <= ShlAmt) {
888 BitMask2 <<= (ShlAmt - ShrAmt);
890 BitMask2 = isLshr ? BitMask2.
lshr(ShrAmt - ShlAmt):
891 BitMask2.
ashr(ShrAmt - ShlAmt);
895 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
896 if (ShrAmt == ShlAmt)
903 if (ShrAmt < ShlAmt) {
905 New = BinaryOperator::CreateShl(VarX, Amt);
911 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
912 BinaryOperator::CreateAShr(VarX, Amt);
913 if (cast<BinaryOperator>(Shr)->isExact())
914 New->setIsExact(
true);
917 return InsertNewInstWith(New, *Shl);
947 unsigned NewDMaskVal = 0;
948 unsigned OrigLoadIdx = 0;
949 for (
unsigned SrcIdx = 0; SrcIdx < 4; ++SrcIdx) {
950 const unsigned Bit = 1 << SrcIdx;
951 if (!!(DMaskVal & Bit)) {
952 if (!!DemandedElts[OrigLoadIdx])
958 if (DMaskVal != NewDMaskVal)
967 if (NewNumElts >= VWidth && DemandedElts.
isMask()) {
982 for (
unsigned i = 0, e = FTy->
getNumParams(); i != e; ++i)
990 OverloadTys[0] = NewTy;
998 Args[DMaskIdx] = NewDMask;
1001 Builder.SetInsertPoint(II);
1003 CallInst *NewCall = Builder.CreateCall(NewIntrin, Args);
1007 if (NewNumElts == 1) {
1013 unsigned NewLoadIdx = 0;
1014 for (
unsigned OrigLoadIdx = 0; OrigLoadIdx < VWidth; ++OrigLoadIdx) {
1015 if (!!DemandedElts[OrigLoadIdx])
1022 Builder.CreateShuffleVector(NewCall,
UndefValue::get(NewTy), EltMask);
1035 Value *InstCombiner::SimplifyDemandedVectorElts(
Value *V,
APInt DemandedElts,
1040 assert((DemandedElts & ~EltMask) == 0 &&
"Invalid DemandedElts!");
1042 if (isa<UndefValue>(V)) {
1044 UndefElts = EltMask;
1049 UndefElts = EltMask;
1056 if (
Constant *
C = dyn_cast<Constant>(V)) {
1062 Type *EltTy = cast<VectorType>(V->
getType())->getElementType();
1066 for (
unsigned i = 0; i != VWidth; ++i) {
1067 if (!DemandedElts[i]) {
1069 UndefElts.setBit(i);
1073 Constant *Elt =
C->getAggregateElement(i);
1074 if (!Elt)
return nullptr;
1076 if (isa<UndefValue>(Elt)) {
1078 UndefElts.setBit(i);
1086 return NewCV !=
C ? NewCV :
nullptr;
1105 DemandedElts = EltMask;
1109 if (!I)
return nullptr;
1111 bool MadeChange =
false;
1112 APInt UndefElts2(VWidth, 0);
1113 APInt UndefElts3(VWidth, 0);
1118 case Instruction::InsertElement: {
1125 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts,
1126 UndefElts2, Depth + 1);
1127 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1134 APInt PreInsertDemandedElts = DemandedElts;
1136 PreInsertDemandedElts.
clearBit(IdxNo);
1137 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), PreInsertDemandedElts,
1138 UndefElts, Depth + 1);
1139 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1143 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1149 UndefElts.clearBit(IdxNo);
1152 case Instruction::ShuffleVector: {
1154 unsigned LHSVWidth =
1156 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1157 for (
unsigned i = 0; i < VWidth; i++) {
1158 if (DemandedElts[i]) {
1160 if (MaskVal != -1u) {
1161 assert(MaskVal < LHSVWidth * 2 &&
1162 "shufflevector mask index out of range!");
1163 if (MaskVal < LHSVWidth)
1164 LeftDemanded.setBit(MaskVal);
1166 RightDemanded.
setBit(MaskVal - LHSVWidth);
1171 APInt LHSUndefElts(LHSVWidth, 0);
1172 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), LeftDemanded,
1173 LHSUndefElts, Depth + 1);
1174 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1176 APInt RHSUndefElts(LHSVWidth, 0);
1177 TmpV = SimplifyDemandedVectorElts(I->
getOperand(1), RightDemanded,
1178 RHSUndefElts, Depth + 1);
1179 if (TmpV) { I->
setOperand(1, TmpV); MadeChange =
true; }
1181 bool NewUndefElts =
false;
1182 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1183 unsigned RHSIdx = -1u, RHSValIdx = -1u;
1184 bool LHSUniform =
true;
1185 bool RHSUniform =
true;
1186 for (
unsigned i = 0; i < VWidth; i++) {
1188 if (MaskVal == -1u) {
1189 UndefElts.setBit(i);
1190 }
else if (!DemandedElts[i]) {
1191 NewUndefElts =
true;
1192 UndefElts.setBit(i);
1193 }
else if (MaskVal < LHSVWidth) {
1194 if (LHSUndefElts[MaskVal]) {
1195 NewUndefElts =
true;
1196 UndefElts.setBit(i);
1198 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1199 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
1200 LHSUniform = LHSUniform && (MaskVal == i);
1203 if (RHSUndefElts[MaskVal - LHSVWidth]) {
1204 NewUndefElts =
true;
1205 UndefElts.setBit(i);
1207 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1208 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
1209 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
1224 if (LHSIdx < LHSVWidth && RHSUniform) {
1225 if (
auto *CV = dyn_cast<ConstantVector>(Shuffle->
getOperand(0))) {
1231 if (RHSIdx < LHSVWidth && LHSUniform) {
1232 if (
auto *CV = dyn_cast<ConstantVector>(Shuffle->
getOperand(1))) {
1243 InsertNewInstWith(New, *Shuffle);
1250 for (
unsigned i = 0; i < VWidth; ++i) {
1263 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1265 for (
unsigned i = 0; i < VWidth; i++) {
1270 if (isa<ConstantExpr>(CElt))
1273 LeftDemanded.clearBit(i);
1279 TmpV = SimplifyDemandedVectorElts(I->
getOperand(1), LeftDemanded, UndefElts,
1281 if (TmpV) { I->
setOperand(1, TmpV); MadeChange =
true; }
1283 TmpV = SimplifyDemandedVectorElts(I->
getOperand(2), RightDemanded,
1284 UndefElts2, Depth + 1);
1285 if (TmpV) { I->
setOperand(2, TmpV); MadeChange =
true; }
1288 UndefElts &= UndefElts2;
1291 case Instruction::BitCast: {
1296 APInt InputDemandedElts(InVWidth, 0);
1297 UndefElts2 =
APInt(InVWidth, 0);
1300 if (VWidth == InVWidth) {
1304 InputDemandedElts = DemandedElts;
1305 }
else if ((VWidth % InVWidth) == 0) {
1309 Ratio = VWidth / InVWidth;
1310 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1311 if (DemandedElts[OutIdx])
1312 InputDemandedElts.
setBit(OutIdx / Ratio);
1313 }
else if ((InVWidth % VWidth) == 0) {
1317 Ratio = InVWidth / VWidth;
1318 for (
unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1319 if (DemandedElts[InIdx / Ratio])
1320 InputDemandedElts.
setBit(InIdx);
1326 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), InputDemandedElts,
1327 UndefElts2, Depth + 1);
1333 if (VWidth == InVWidth) {
1334 UndefElts = UndefElts2;
1335 }
else if ((VWidth % InVWidth) == 0) {
1339 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1340 if (UndefElts2[OutIdx / Ratio])
1341 UndefElts.setBit(OutIdx);
1342 }
else if ((InVWidth % VWidth) == 0) {
1346 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1349 UndefElts.
setBit(OutIdx);
1356 case Instruction::And:
1357 case Instruction::Or:
1358 case Instruction::Xor:
1360 case Instruction::Sub:
1361 case Instruction::Mul:
1363 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts, UndefElts,
1365 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1366 TmpV = SimplifyDemandedVectorElts(I->
getOperand(1), DemandedElts,
1367 UndefElts2, Depth + 1);
1368 if (TmpV) { I->
setOperand(1, TmpV); MadeChange =
true; }
1372 UndefElts &= UndefElts2;
1374 case Instruction::FPTrunc:
1375 case Instruction::FPExt:
1376 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts, UndefElts,
1378 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1385 case Intrinsic::x86_xop_vfrcz_ss:
1386 case Intrinsic::x86_xop_vfrcz_sd:
1391 if (!DemandedElts[0]) {
1398 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1399 UndefElts, Depth + 1);
1400 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1403 UndefElts = UndefElts[0];
1407 case Intrinsic::x86_sse_rcp_ss:
1408 case Intrinsic::x86_sse_rsqrt_ss:
1409 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1410 UndefElts, Depth + 1);
1411 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1414 if (!DemandedElts[0]) {
1425 case Intrinsic::x86_sse_min_ss:
1426 case Intrinsic::x86_sse_max_ss:
1427 case Intrinsic::x86_sse_cmp_ss:
1428 case Intrinsic::x86_sse2_min_sd:
1429 case Intrinsic::x86_sse2_max_sd:
1430 case Intrinsic::x86_sse2_cmp_sd: {
1431 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1432 UndefElts, Depth + 1);
1433 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1436 if (!DemandedElts[0]) {
1443 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1444 UndefElts2, Depth + 1);
1445 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1450 UndefElts.clearBit(0);
1457 case Intrinsic::x86_sse41_round_ss:
1458 case Intrinsic::x86_sse41_round_sd: {
1460 APInt DemandedElts2 = DemandedElts;
1462 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts2,
1463 UndefElts, Depth + 1);
1464 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1467 if (!DemandedElts[0]) {
1474 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1475 UndefElts2, Depth + 1);
1476 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1480 UndefElts.clearBit(0);
1481 UndefElts |= UndefElts2[0];
1488 case Intrinsic::x86_avx512_mask_add_ss_round:
1489 case Intrinsic::x86_avx512_mask_div_ss_round:
1490 case Intrinsic::x86_avx512_mask_mul_ss_round:
1491 case Intrinsic::x86_avx512_mask_sub_ss_round:
1492 case Intrinsic::x86_avx512_mask_max_ss_round:
1493 case Intrinsic::x86_avx512_mask_min_ss_round:
1494 case Intrinsic::x86_avx512_mask_add_sd_round:
1495 case Intrinsic::x86_avx512_mask_div_sd_round:
1496 case Intrinsic::x86_avx512_mask_mul_sd_round:
1497 case Intrinsic::x86_avx512_mask_sub_sd_round:
1498 case Intrinsic::x86_avx512_mask_max_sd_round:
1499 case Intrinsic::x86_avx512_mask_min_sd_round:
1500 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1501 UndefElts, Depth + 1);
1502 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1505 if (!DemandedElts[0]) {
1512 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1513 UndefElts2, Depth + 1);
1514 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1515 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(2), DemandedElts,
1516 UndefElts3, Depth + 1);
1517 if (TmpV) { II->
setArgOperand(2, TmpV); MadeChange =
true; }
1521 if (!UndefElts2[0] || !UndefElts3[0])
1522 UndefElts.clearBit(0);
1526 case Intrinsic::x86_sse2_packssdw_128:
1527 case Intrinsic::x86_sse2_packsswb_128:
1528 case Intrinsic::x86_sse2_packuswb_128:
1529 case Intrinsic::x86_sse41_packusdw:
1530 case Intrinsic::x86_avx2_packssdw:
1531 case Intrinsic::x86_avx2_packsswb:
1532 case Intrinsic::x86_avx2_packusdw:
1533 case Intrinsic::x86_avx2_packuswb:
1534 case Intrinsic::x86_avx512_packssdw_512:
1535 case Intrinsic::x86_avx512_packsswb_512:
1536 case Intrinsic::x86_avx512_packusdw_512:
1537 case Intrinsic::x86_avx512_packuswb_512: {
1540 assert(VWidth == (InnerVWidth * 2) &&
"Unexpected input size");
1542 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1543 unsigned VWidthPerLane = VWidth / NumLanes;
1544 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1550 for (
int OpNum = 0; OpNum != 2; ++OpNum) {
1551 APInt OpDemandedElts(InnerVWidth, 0);
1552 for (
unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1553 unsigned LaneIdx = Lane * VWidthPerLane;
1554 for (
unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1555 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1556 if (DemandedElts[Idx])
1557 OpDemandedElts.
setBit((Lane * InnerVWidthPerLane) + Elt);
1563 APInt OpUndefElts(InnerVWidth, 0);
1564 TmpV = SimplifyDemandedVectorElts(
Op, OpDemandedElts, OpUndefElts,
1572 OpUndefElts = OpUndefElts.
zext(VWidth);
1573 for (
unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1574 APInt LaneElts = OpUndefElts.
lshr(InnerVWidthPerLane * Lane);
1575 LaneElts = LaneElts.
getLoBits(InnerVWidthPerLane);
1576 LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
1577 UndefElts |= LaneElts;
1584 case Intrinsic::x86_ssse3_pshuf_b_128:
1585 case Intrinsic::x86_avx2_pshuf_b:
1586 case Intrinsic::x86_avx512_pshuf_b_512:
1588 case Intrinsic::x86_avx_vpermilvar_ps:
1589 case Intrinsic::x86_avx_vpermilvar_ps_256:
1590 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1591 case Intrinsic::x86_avx_vpermilvar_pd:
1592 case Intrinsic::x86_avx_vpermilvar_pd_256:
1593 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1595 case Intrinsic::x86_avx2_permd:
1596 case Intrinsic::x86_avx2_permps: {
1598 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1600 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1606 case Intrinsic::x86_sse4a_extrq:
1607 case Intrinsic::x86_sse4a_extrqi:
1608 case Intrinsic::x86_sse4a_insertq:
1609 case Intrinsic::x86_sse4a_insertqi:
1610 UndefElts.setHighBits(VWidth / 2);
1612 case Intrinsic::amdgcn_buffer_load:
1613 case Intrinsic::amdgcn_buffer_load_format:
1614 return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts);
1617 return simplifyAMDGCNMemoryIntrinsicDemanded(II, DemandedElts, 0);
1625 return MadeChange ?
I :
nullptr;
void clearAllBits()
Set every bit to 0.
Type * getVectorElementType() const
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...
void push_back(const T &Elt)
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
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.
uint64_t getZExtValue() const
Get zero extended value.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Compute iterated dominance frontiers using a linear time algorithm.
Type * getParamType(unsigned i) const
Parameter type accessors.
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.
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.
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
This instruction constructs a fixed permutation of two input vectors.
LLVMContext & getContext() const
All values hold a context through their type.
const Use & getOperandUse(unsigned i) const
APInt trunc(unsigned width) const
Truncate to new width.
void setAllBits()
Set every bit to 1.
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.
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.
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.
uint64_t getNumElements() const
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
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.
KnownBits zextOrTrunc(unsigned BitWidth)
Zero extends or truncates the underlying known Zero and One bits.
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.
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.
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...
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.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
bool isMask(unsigned numBits) const
bool isOneValue() const
Determine if this is a value of 1.
void setArgOperand(unsigned i, Value *v)
Constant Vector Declarations.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static int getMaskValue(const Constant *Mask, unsigned Elt)
Return the shuffle mask value for the specified element of the mask.
#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 countPopulation(T Value)
Count the number of set bits in a value.
This is the shared class of boolean and integer constants.
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
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...
Type * getReturnType() const
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.
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.
amdgpu Simplify well known AMD library false Value Value * Arg
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)
unsigned getNumArgOperands() const
Return the number of call arguments.
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...
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
bool matchIntrinsicType(Type *Ty, ArrayRef< IITDescriptor > &Infos, SmallVectorImpl< Type *> &ArgTys)
Match the specified type (which comes from an intrinsic argument or return value) with the type const...
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())
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
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 VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
KnownBits sext(unsigned BitWidth)
Sign extends the underlying known Zero and One bits.
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.
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.
VectorType * getType() const
Overload to return most specific vector type.
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)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
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.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const