22 using namespace llvm::PatternMatch;
24 #define DEBUG_TYPE "instcombine"
30 const APInt &Demanded) {
31 assert(I &&
"No instruction?");
32 assert(OpNo < I->getNumOperands() &&
"Operand index too large");
54 bool InstCombiner::SimplifyDemandedInstructionBits(
Instruction &Inst) {
59 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known,
62 if (V == &Inst)
return true;
63 replaceInstUsesWith(Inst, V);
70 bool InstCombiner::SimplifyDemandedBits(
Instruction *
I,
unsigned OpNo,
71 const APInt &DemandedMask,
75 Value *NewVal = SimplifyDemandedUseBits(U.
get(), DemandedMask, Known,
77 if (!NewVal)
return false;
106 Value *InstCombiner::SimplifyDemandedUseBits(
Value *V,
APInt DemandedMask,
109 assert(V !=
nullptr &&
"Null pointer of Value???");
110 assert(Depth <= 6 &&
"Limit Search Depth");
116 "Value *V, DemandedMask and Known must have same BitWidth");
118 if (isa<Constant>(V)) {
140 return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI);
142 KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth);
155 case Instruction::And: {
157 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
158 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown,
161 assert(!RHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
162 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
165 APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
167 APInt IKnownOne = RHSKnown.One & LHSKnown.One;
171 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
176 if (DemandedMask.
isSubsetOf(LHSKnown.Zero | RHSKnown.One))
178 if (DemandedMask.
isSubsetOf(RHSKnown.Zero | LHSKnown.One))
185 Known.
Zero = std::move(IKnownZero);
186 Known.
One = std::move(IKnownOne);
189 case Instruction::Or: {
191 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
192 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown,
195 assert(!RHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
196 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
199 APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
201 APInt IKnownOne = RHSKnown.One | LHSKnown.One;
205 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
210 if (DemandedMask.
isSubsetOf(LHSKnown.One | RHSKnown.Zero))
212 if (DemandedMask.
isSubsetOf(RHSKnown.One | LHSKnown.Zero))
219 Known.
Zero = std::move(IKnownZero);
220 Known.
One = std::move(IKnownOne);
223 case Instruction::Xor: {
224 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) ||
225 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1))
227 assert(!RHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
228 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
231 APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
232 (RHSKnown.One & LHSKnown.One);
234 APInt IKnownOne = (RHSKnown.Zero & LHSKnown.One) |
235 (RHSKnown.One & LHSKnown.Zero);
239 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
252 if (DemandedMask.
isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
256 return InsertNewInstWith(Or, *I);
263 if (DemandedMask.
isSubsetOf(RHSKnown.Zero|RHSKnown.One) &&
264 RHSKnown.One.isSubsetOf(LHSKnown.One)) {
266 ~RHSKnown.One & DemandedMask);
268 return InsertNewInstWith(And, *I);
281 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
283 isa<ConstantInt>(LHSInst->getOperand(1)) &&
284 (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) {
285 ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
287 APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask);
292 InsertNewInstWith(NewAnd, *I);
296 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
297 return InsertNewInstWith(NewXor, *I);
301 Known.
Zero = std::move(IKnownZero);
303 Known.
One = std::move(IKnownOne);
313 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) ||
314 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1))
316 assert(!RHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
317 assert(!LHSKnown.hasConflict() &&
"Bits known to be one AND zero?");
325 Known.
One = RHSKnown.One & LHSKnown.One;
326 Known.
Zero = RHSKnown.Zero & LHSKnown.Zero;
328 case Instruction::ZExt:
329 case Instruction::Trunc: {
334 if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1))
338 if (BitWidth > SrcBitWidth)
343 case Instruction::BitCast:
350 if (DstVTy->getNumElements() != SrcVTy->getNumElements())
360 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1))
364 case Instruction::SExt: {
368 APInt InputDemandedBits = DemandedMask.
trunc(SrcBitWidth);
373 InputDemandedBits.
setBit(SrcBitWidth-1);
376 if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1))
381 if (InputKnown.isNonNegative() ||
385 return InsertNewInstWith(NewCast, *I);
390 Known = InputKnown.
sext(BitWidth);
395 case Instruction::Sub: {
404 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) ||
406 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) {
418 if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
423 DemandedFromOps.isOneValue()) &&
424 DemandedFromOps.isSubsetOf(LHSKnown.Zero))
433 case Instruction::Shl: {
439 if (
Value *R = simplifyShrShlDemandedBits(
440 Shr, *ShrAmt, I, *SA, DemandedMask, Known))
445 APInt DemandedMaskIn(DemandedMask.
lshr(ShiftAmt));
449 if (IOp->hasNoSignedWrap())
450 DemandedMaskIn.setHighBits(ShiftAmt+1);
451 else if (IOp->hasNoUnsignedWrap())
452 DemandedMaskIn.setHighBits(ShiftAmt);
454 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
457 Known.
Zero <<= ShiftAmt;
458 Known.
One <<= ShiftAmt;
465 case Instruction::LShr: {
471 APInt DemandedMaskIn(DemandedMask.
shl(ShiftAmt));
475 if (cast<LShrOperator>(I)->isExact())
478 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
488 case Instruction::AShr: {
497 return InsertNewInstWith(NewVal, *I);
510 APInt DemandedMaskIn(DemandedMask.
shl(ShiftAmt));
518 if (cast<AShrOperator>(I)->isExact())
519 DemandedMaskIn.setLowBits(ShiftAmt);
521 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1))
533 SignMask.lshrInPlace(ShiftAmt);
537 if (BitWidth <= ShiftAmt || Known.
Zero[BitWidth-ShiftAmt-1] ||
541 LShr->
setIsExact(cast<BinaryOperator>(I)->isExact());
542 return InsertNewInstWith(LShr, *I);
544 Known.
One |= HighBits;
549 case Instruction::SRem:
553 if (Rem->isMinusOne())
557 if (DemandedMask.
ult(RA))
560 APInt LowBits = RA - 1;
562 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1))
566 Known.
Zero = LHSKnown.Zero & LowBits;
567 Known.
One = LHSKnown.One & LowBits;
571 if (LHSKnown.isNonNegative() || LowBits.
isSubsetOf(LHSKnown.Zero))
572 Known.
Zero |= ~LowBits;
576 if (LHSKnown.isNegative() && LowBits.
intersects(LHSKnown.One))
577 Known.
One |= ~LowBits;
589 if (LHSKnown.isNonNegative())
593 case Instruction::URem: {
596 if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) ||
597 SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1))
600 unsigned Leaders = Known2.countMinLeadingZeros();
606 switch (II->getIntrinsicID()) {
608 case Intrinsic::bswap: {
620 if (BitWidth-NLZ-NTZ == 8) {
621 unsigned ResultBit = NTZ;
622 unsigned InputBit = BitWidth-NTZ-8;
627 if (InputBit > ResultBit)
628 NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
631 NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
634 return InsertNewInstWith(NewVal, *I);
640 case Intrinsic::x86_mmx_pmovmskb:
641 case Intrinsic::x86_sse_movmsk_ps:
642 case Intrinsic::x86_sse2_movmsk_pd:
643 case Intrinsic::x86_sse2_pmovmskb_128:
644 case Intrinsic::x86_avx_movmsk_ps_256:
645 case Intrinsic::x86_avx_movmsk_pd_256:
646 case Intrinsic::x86_avx2_pmovmskb: {
650 if (II->getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) {
653 auto Arg = II->getArgOperand(0);
654 auto ArgType = cast<VectorType>(Arg->getType());
655 ArgWidth = ArgType->getNumElements();
668 case Intrinsic::x86_sse42_crc32_64_64:
688 const APInt &DemandedMask,
703 case Instruction::And: {
710 APInt IKnownZero = RHSKnown.Zero | LHSKnown.Zero;
712 APInt IKnownOne = RHSKnown.One & LHSKnown.One;
716 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
722 if (DemandedMask.
isSubsetOf(LHSKnown.Zero | RHSKnown.One))
724 if (DemandedMask.
isSubsetOf(RHSKnown.Zero | LHSKnown.One))
727 Known.
Zero = std::move(IKnownZero);
728 Known.
One = std::move(IKnownOne);
731 case Instruction::Or: {
741 APInt IKnownZero = RHSKnown.Zero & LHSKnown.Zero;
743 APInt IKnownOne = RHSKnown.One | LHSKnown.One;
747 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
753 if (DemandedMask.
isSubsetOf(LHSKnown.One | RHSKnown.Zero))
755 if (DemandedMask.
isSubsetOf(RHSKnown.One | LHSKnown.Zero))
758 Known.
Zero = std::move(IKnownZero);
759 Known.
One = std::move(IKnownOne);
762 case Instruction::Xor: {
771 APInt IKnownZero = (RHSKnown.Zero & LHSKnown.Zero) |
772 (RHSKnown.One & LHSKnown.One);
774 APInt IKnownOne = (RHSKnown.Zero & LHSKnown.One) |
775 (RHSKnown.One & LHSKnown.Zero);
779 if (DemandedMask.
isSubsetOf(IKnownZero|IKnownOne))
790 Known.
Zero = std::move(IKnownZero);
792 Known.
One = std::move(IKnownOne);
829 InstCombiner::simplifyShrShlDemandedBits(
Instruction *Shr,
const APInt &ShrOp1,
831 const APInt &DemandedMask,
833 if (!ShlOp1 || !ShrOp1)
839 if (ShlOp1.
uge(BitWidth) || ShrOp1.
uge(BitWidth))
847 Known.
Zero &= DemandedMask;
852 bool isLshr = (Shr->
getOpcode() == Instruction::LShr);
853 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
854 (BitMask1.ashr(ShrAmt) << ShlAmt);
856 if (ShrAmt <= ShlAmt) {
857 BitMask2 <<= (ShlAmt - ShrAmt);
859 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
860 BitMask2.ashr(ShrAmt - ShlAmt);
864 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
865 if (ShrAmt == ShlAmt)
872 if (ShrAmt < ShlAmt) {
874 New = BinaryOperator::CreateShl(VarX, Amt);
880 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
882 if (cast<BinaryOperator>(Shr)->isExact())
886 return InsertNewInstWith(New, *Shl);
900 Value *InstCombiner::SimplifyDemandedVectorElts(
Value *V,
APInt DemandedElts,
905 assert((DemandedElts & ~EltMask) == 0 &&
"Invalid DemandedElts!");
907 if (isa<UndefValue>(V)) {
921 if (
Constant *
C = dyn_cast<Constant>(V)) {
927 Type *EltTy = cast<VectorType>(V->
getType())->getElementType();
931 for (
unsigned i = 0; i != VWidth; ++i) {
932 if (!DemandedElts[i]) {
938 Constant *Elt =
C->getAggregateElement(i);
939 if (!Elt)
return nullptr;
941 if (isa<UndefValue>(Elt)) {
951 return NewCV !=
C ? NewCV :
nullptr;
970 DemandedElts = EltMask;
974 if (!I)
return nullptr;
976 bool MadeChange =
false;
977 APInt UndefElts2(VWidth, 0);
978 APInt UndefElts3(VWidth, 0);
983 case Instruction::InsertElement: {
990 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts,
991 UndefElts2, Depth + 1);
992 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
999 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1006 APInt DemandedElts2 = DemandedElts;
1008 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts2,
1009 UndefElts, Depth + 1);
1010 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1013 UndefElts.clearBit(IdxNo);
1016 case Instruction::ShuffleVector: {
1018 unsigned LHSVWidth =
1020 APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1021 for (
unsigned i = 0; i < VWidth; i++) {
1022 if (DemandedElts[i]) {
1024 if (MaskVal != -1u) {
1025 assert(MaskVal < LHSVWidth * 2 &&
1026 "shufflevector mask index out of range!");
1027 if (MaskVal < LHSVWidth)
1028 LeftDemanded.setBit(MaskVal);
1030 RightDemanded.setBit(MaskVal - LHSVWidth);
1035 APInt LHSUndefElts(LHSVWidth, 0);
1036 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), LeftDemanded,
1037 LHSUndefElts, Depth + 1);
1038 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1040 APInt RHSUndefElts(LHSVWidth, 0);
1041 TmpV = SimplifyDemandedVectorElts(I->
getOperand(1), RightDemanded,
1042 RHSUndefElts, Depth + 1);
1043 if (TmpV) { I->
setOperand(1, TmpV); MadeChange =
true; }
1045 bool NewUndefElts =
false;
1046 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1047 unsigned RHSIdx = -1u, RHSValIdx = -1u;
1048 bool LHSUniform =
true;
1049 bool RHSUniform =
true;
1050 for (
unsigned i = 0; i < VWidth; i++) {
1052 if (MaskVal == -1u) {
1053 UndefElts.setBit(i);
1054 }
else if (!DemandedElts[i]) {
1055 NewUndefElts =
true;
1056 UndefElts.setBit(i);
1057 }
else if (MaskVal < LHSVWidth) {
1058 if (LHSUndefElts[MaskVal]) {
1059 NewUndefElts =
true;
1060 UndefElts.setBit(i);
1062 LHSIdx = LHSIdx == -1u ? i : LHSVWidth;
1063 LHSValIdx = LHSValIdx == -1u ? MaskVal : LHSVWidth;
1064 LHSUniform = LHSUniform && (MaskVal == i);
1067 if (RHSUndefElts[MaskVal - LHSVWidth]) {
1068 NewUndefElts =
true;
1069 UndefElts.setBit(i);
1071 RHSIdx = RHSIdx == -1u ? i : LHSVWidth;
1072 RHSValIdx = RHSValIdx == -1u ? MaskVal - LHSVWidth : LHSVWidth;
1073 RHSUniform = RHSUniform && (MaskVal - LHSVWidth == i);
1088 if (LHSIdx < LHSVWidth && RHSUniform) {
1089 if (
auto *CV = dyn_cast<ConstantVector>(Shuffle->
getOperand(0))) {
1095 if (RHSIdx < LHSVWidth && LHSUniform) {
1096 if (
auto *CV = dyn_cast<ConstantVector>(Shuffle->
getOperand(1))) {
1107 InsertNewInstWith(New, *Shuffle);
1114 for (
unsigned i = 0; i < VWidth; ++i) {
1127 APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1129 for (
unsigned i = 0; i < VWidth; i++) {
1134 if (isa<ConstantExpr>(CElt))
1137 LeftDemanded.clearBit(i);
1139 RightDemanded.clearBit(i);
1143 TmpV = SimplifyDemandedVectorElts(I->
getOperand(1), LeftDemanded, UndefElts,
1145 if (TmpV) { I->
setOperand(1, TmpV); MadeChange =
true; }
1147 TmpV = SimplifyDemandedVectorElts(I->
getOperand(2), RightDemanded,
1148 UndefElts2, Depth + 1);
1149 if (TmpV) { I->
setOperand(2, TmpV); MadeChange =
true; }
1152 UndefElts &= UndefElts2;
1155 case Instruction::BitCast: {
1160 APInt InputDemandedElts(InVWidth, 0);
1161 UndefElts2 =
APInt(InVWidth, 0);
1164 if (VWidth == InVWidth) {
1168 InputDemandedElts = DemandedElts;
1169 }
else if ((VWidth % InVWidth) == 0) {
1173 Ratio = VWidth / InVWidth;
1174 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1175 if (DemandedElts[OutIdx])
1176 InputDemandedElts.setBit(OutIdx / Ratio);
1177 }
else if ((InVWidth % VWidth) == 0) {
1181 Ratio = InVWidth / VWidth;
1182 for (
unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1183 if (DemandedElts[InIdx / Ratio])
1184 InputDemandedElts.setBit(InIdx);
1191 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), InputDemandedElts,
1192 UndefElts2, Depth + 1);
1198 if (VWidth == InVWidth) {
1199 UndefElts = UndefElts2;
1200 }
else if ((VWidth % InVWidth) == 0) {
1204 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1205 if (UndefElts2[OutIdx / Ratio])
1206 UndefElts.setBit(OutIdx);
1207 }
else if ((InVWidth % VWidth) == 0) {
1211 for (
unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1214 UndefElts.
setBit(OutIdx);
1221 case Instruction::And:
1222 case Instruction::Or:
1223 case Instruction::Xor:
1225 case Instruction::Sub:
1226 case Instruction::Mul:
1228 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts, UndefElts,
1230 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1231 TmpV = SimplifyDemandedVectorElts(I->
getOperand(1), DemandedElts,
1232 UndefElts2, Depth + 1);
1233 if (TmpV) { I->
setOperand(1, TmpV); MadeChange =
true; }
1237 UndefElts &= UndefElts2;
1239 case Instruction::FPTrunc:
1240 case Instruction::FPExt:
1241 TmpV = SimplifyDemandedVectorElts(I->
getOperand(0), DemandedElts, UndefElts,
1243 if (TmpV) { I->
setOperand(0, TmpV); MadeChange =
true; }
1252 case Intrinsic::x86_xop_vfrcz_ss:
1253 case Intrinsic::x86_xop_vfrcz_sd:
1258 if (!DemandedElts[0]) {
1265 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1266 UndefElts, Depth + 1);
1267 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1270 UndefElts = UndefElts[0];
1274 case Intrinsic::x86_sse_rcp_ss:
1275 case Intrinsic::x86_sse_rsqrt_ss:
1276 case Intrinsic::x86_sse_sqrt_ss:
1277 case Intrinsic::x86_sse2_sqrt_sd:
1278 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1279 UndefElts, Depth + 1);
1280 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1283 if (!DemandedElts[0]) {
1294 case Intrinsic::x86_sse_min_ss:
1295 case Intrinsic::x86_sse_max_ss:
1296 case Intrinsic::x86_sse_cmp_ss:
1297 case Intrinsic::x86_sse2_min_sd:
1298 case Intrinsic::x86_sse2_max_sd:
1299 case Intrinsic::x86_sse2_cmp_sd: {
1300 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1301 UndefElts, Depth + 1);
1302 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1305 if (!DemandedElts[0]) {
1312 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1313 UndefElts2, Depth + 1);
1314 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1319 UndefElts.clearBit(0);
1326 case Intrinsic::x86_sse41_round_ss:
1327 case Intrinsic::x86_sse41_round_sd: {
1329 APInt DemandedElts2 = DemandedElts;
1331 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts2,
1332 UndefElts, Depth + 1);
1333 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1336 if (!DemandedElts[0]) {
1343 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1344 UndefElts2, Depth + 1);
1345 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1349 UndefElts.clearBit(0);
1350 UndefElts |= UndefElts2[0];
1357 case Intrinsic::x86_avx512_mask_add_ss_round:
1358 case Intrinsic::x86_avx512_mask_div_ss_round:
1359 case Intrinsic::x86_avx512_mask_mul_ss_round:
1360 case Intrinsic::x86_avx512_mask_sub_ss_round:
1361 case Intrinsic::x86_avx512_mask_max_ss_round:
1362 case Intrinsic::x86_avx512_mask_min_ss_round:
1363 case Intrinsic::x86_avx512_mask_add_sd_round:
1364 case Intrinsic::x86_avx512_mask_div_sd_round:
1365 case Intrinsic::x86_avx512_mask_mul_sd_round:
1366 case Intrinsic::x86_avx512_mask_sub_sd_round:
1367 case Intrinsic::x86_avx512_mask_max_sd_round:
1368 case Intrinsic::x86_avx512_mask_min_sd_round:
1369 case Intrinsic::x86_fma_vfmadd_ss:
1370 case Intrinsic::x86_fma_vfmsub_ss:
1371 case Intrinsic::x86_fma_vfnmadd_ss:
1372 case Intrinsic::x86_fma_vfnmsub_ss:
1373 case Intrinsic::x86_fma_vfmadd_sd:
1374 case Intrinsic::x86_fma_vfmsub_sd:
1375 case Intrinsic::x86_fma_vfnmadd_sd:
1376 case Intrinsic::x86_fma_vfnmsub_sd:
1377 case Intrinsic::x86_avx512_mask_vfmadd_ss:
1378 case Intrinsic::x86_avx512_mask_vfmadd_sd:
1379 case Intrinsic::x86_avx512_maskz_vfmadd_ss:
1380 case Intrinsic::x86_avx512_maskz_vfmadd_sd:
1381 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1382 UndefElts, Depth + 1);
1383 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1386 if (!DemandedElts[0]) {
1393 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1394 UndefElts2, Depth + 1);
1395 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1396 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(2), DemandedElts,
1397 UndefElts3, Depth + 1);
1398 if (TmpV) { II->
setArgOperand(2, TmpV); MadeChange =
true; }
1402 if (!UndefElts2[0] || !UndefElts3[0])
1403 UndefElts.clearBit(0);
1407 case Intrinsic::x86_avx512_mask3_vfmadd_ss:
1408 case Intrinsic::x86_avx512_mask3_vfmadd_sd:
1409 case Intrinsic::x86_avx512_mask3_vfmsub_ss:
1410 case Intrinsic::x86_avx512_mask3_vfmsub_sd:
1411 case Intrinsic::x86_avx512_mask3_vfnmsub_ss:
1412 case Intrinsic::x86_avx512_mask3_vfnmsub_sd:
1414 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(2), DemandedElts,
1415 UndefElts, Depth + 1);
1416 if (TmpV) { II->
setArgOperand(2, TmpV); MadeChange =
true; }
1419 if (!DemandedElts[0]) {
1426 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(0), DemandedElts,
1427 UndefElts2, Depth + 1);
1428 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1429 TmpV = SimplifyDemandedVectorElts(II->
getArgOperand(1), DemandedElts,
1430 UndefElts3, Depth + 1);
1431 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1435 if (!UndefElts2[0] || !UndefElts3[0])
1436 UndefElts.clearBit(0);
1440 case Intrinsic::x86_sse2_pmulu_dq:
1441 case Intrinsic::x86_sse41_pmuldq:
1442 case Intrinsic::x86_avx2_pmul_dq:
1443 case Intrinsic::x86_avx2_pmulu_dq:
1444 case Intrinsic::x86_avx512_pmul_dq_512:
1445 case Intrinsic::x86_avx512_pmulu_dq_512: {
1449 assert((VWidth * 2) == InnerVWidth &&
"Unexpected input size");
1451 APInt InnerDemandedElts(InnerVWidth, 0);
1452 for (
unsigned i = 0; i != VWidth; ++i)
1453 if (DemandedElts[i])
1454 InnerDemandedElts.setBit(i * 2);
1456 UndefElts2 =
APInt(InnerVWidth, 0);
1457 TmpV = SimplifyDemandedVectorElts(Op0, InnerDemandedElts, UndefElts2,
1459 if (TmpV) { II->
setArgOperand(0, TmpV); MadeChange =
true; }
1461 UndefElts3 =
APInt(InnerVWidth, 0);
1462 TmpV = SimplifyDemandedVectorElts(Op1, InnerDemandedElts, UndefElts3,
1464 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1469 case Intrinsic::x86_sse2_packssdw_128:
1470 case Intrinsic::x86_sse2_packsswb_128:
1471 case Intrinsic::x86_sse2_packuswb_128:
1472 case Intrinsic::x86_sse41_packusdw:
1473 case Intrinsic::x86_avx2_packssdw:
1474 case Intrinsic::x86_avx2_packsswb:
1475 case Intrinsic::x86_avx2_packusdw:
1476 case Intrinsic::x86_avx2_packuswb:
1477 case Intrinsic::x86_avx512_packssdw_512:
1478 case Intrinsic::x86_avx512_packsswb_512:
1479 case Intrinsic::x86_avx512_packusdw_512:
1480 case Intrinsic::x86_avx512_packuswb_512: {
1482 unsigned InnerVWidth = Ty0->getVectorNumElements();
1483 assert(VWidth == (InnerVWidth * 2) &&
"Unexpected input size");
1485 unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128;
1486 unsigned VWidthPerLane = VWidth / NumLanes;
1487 unsigned InnerVWidthPerLane = InnerVWidth / NumLanes;
1493 for (
int OpNum = 0; OpNum != 2; ++OpNum) {
1494 APInt OpDemandedElts(InnerVWidth, 0);
1495 for (
unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1496 unsigned LaneIdx = Lane * VWidthPerLane;
1497 for (
unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) {
1498 unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum;
1499 if (DemandedElts[Idx])
1500 OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt);
1506 APInt OpUndefElts(InnerVWidth, 0);
1507 TmpV = SimplifyDemandedVectorElts(Op, OpDemandedElts, OpUndefElts,
1515 OpUndefElts = OpUndefElts.zext(VWidth);
1516 for (
unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1517 APInt LaneElts = OpUndefElts.
lshr(InnerVWidthPerLane * Lane);
1518 LaneElts = LaneElts.
getLoBits(InnerVWidthPerLane);
1519 LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum);
1520 UndefElts |= LaneElts;
1527 case Intrinsic::x86_ssse3_pshuf_b_128:
1528 case Intrinsic::x86_avx2_pshuf_b:
1529 case Intrinsic::x86_avx512_pshuf_b_512:
1531 case Intrinsic::x86_avx_vpermilvar_ps:
1532 case Intrinsic::x86_avx_vpermilvar_ps_256:
1533 case Intrinsic::x86_avx512_vpermilvar_ps_512:
1534 case Intrinsic::x86_avx_vpermilvar_pd:
1535 case Intrinsic::x86_avx_vpermilvar_pd_256:
1536 case Intrinsic::x86_avx512_vpermilvar_pd_512:
1538 case Intrinsic::x86_avx2_permd:
1539 case Intrinsic::x86_avx2_permps: {
1541 TmpV = SimplifyDemandedVectorElts(Op1, DemandedElts, UndefElts,
1543 if (TmpV) { II->
setArgOperand(1, TmpV); MadeChange =
true; }
1549 case Intrinsic::x86_sse4a_extrq:
1550 case Intrinsic::x86_sse4a_extrqi:
1551 case Intrinsic::x86_sse4a_insertq:
1552 case Intrinsic::x86_sse4a_insertqi:
1553 UndefElts.setHighBits(VWidth / 2);
1555 case Intrinsic::amdgcn_buffer_load:
1556 case Intrinsic::amdgcn_buffer_load_format:
1557 case Intrinsic::amdgcn_image_sample:
1558 case Intrinsic::amdgcn_image_sample_cl:
1559 case Intrinsic::amdgcn_image_sample_d:
1560 case Intrinsic::amdgcn_image_sample_d_cl:
1561 case Intrinsic::amdgcn_image_sample_l:
1562 case Intrinsic::amdgcn_image_sample_b:
1563 case Intrinsic::amdgcn_image_sample_b_cl:
1564 case Intrinsic::amdgcn_image_sample_lz:
1565 case Intrinsic::amdgcn_image_sample_cd:
1566 case Intrinsic::amdgcn_image_sample_cd_cl:
1568 case Intrinsic::amdgcn_image_sample_c:
1569 case Intrinsic::amdgcn_image_sample_c_cl:
1570 case Intrinsic::amdgcn_image_sample_c_d:
1571 case Intrinsic::amdgcn_image_sample_c_d_cl:
1572 case Intrinsic::amdgcn_image_sample_c_l:
1573 case Intrinsic::amdgcn_image_sample_c_b:
1574 case Intrinsic::amdgcn_image_sample_c_b_cl:
1575 case Intrinsic::amdgcn_image_sample_c_lz:
1576 case Intrinsic::amdgcn_image_sample_c_cd:
1577 case Intrinsic::amdgcn_image_sample_c_cd_cl:
1579 case Intrinsic::amdgcn_image_sample_o:
1580 case Intrinsic::amdgcn_image_sample_cl_o:
1581 case Intrinsic::amdgcn_image_sample_d_o:
1582 case Intrinsic::amdgcn_image_sample_d_cl_o:
1583 case Intrinsic::amdgcn_image_sample_l_o:
1584 case Intrinsic::amdgcn_image_sample_b_o:
1585 case Intrinsic::amdgcn_image_sample_b_cl_o:
1586 case Intrinsic::amdgcn_image_sample_lz_o:
1587 case Intrinsic::amdgcn_image_sample_cd_o:
1588 case Intrinsic::amdgcn_image_sample_cd_cl_o:
1590 case Intrinsic::amdgcn_image_sample_c_o:
1591 case Intrinsic::amdgcn_image_sample_c_cl_o:
1592 case Intrinsic::amdgcn_image_sample_c_d_o:
1593 case Intrinsic::amdgcn_image_sample_c_d_cl_o:
1594 case Intrinsic::amdgcn_image_sample_c_l_o:
1595 case Intrinsic::amdgcn_image_sample_c_b_o:
1596 case Intrinsic::amdgcn_image_sample_c_b_cl_o:
1597 case Intrinsic::amdgcn_image_sample_c_lz_o:
1598 case Intrinsic::amdgcn_image_sample_c_cd_o:
1599 case Intrinsic::amdgcn_image_sample_c_cd_cl_o:
1601 case Intrinsic::amdgcn_image_getlod: {
1602 if (VWidth == 1 || !DemandedElts.
isMask())
1607 if (NewNumElts == VWidth)
1613 Type *NewTy = (NewNumElts == 1) ? EltTy :
1618 bool IsBuffer = IID == Intrinsic::amdgcn_buffer_load ||
1619 IID == Intrinsic::amdgcn_buffer_load_format;
1633 Builder.SetInsertPoint(II);
1635 CallInst *NewCall = Builder.CreateCall(NewIntrin, Args);
1644 unsigned PopCnt = 0;
1645 unsigned NewDMask = 0;
1646 for (
unsigned I = 0; I < 4; ++
I) {
1647 const unsigned Bit = 1 <<
I;
1648 if (!!(DMaskVal & Bit)) {
1649 if (++PopCnt > NewNumElts)
1661 if (NewNumElts == 1) {
1662 return Builder.CreateInsertElement(UndefValue::get(V->
getType()),
1663 NewCall, static_cast<uint64_t>(0));
1667 for (
unsigned I = 0; I < VWidth; ++
I)
1670 Value *Shuffle = Builder.CreateShuffleVector(
1671 NewCall, UndefValue::get(NewTy), EltMask);
1680 return MadeChange ? I :
nullptr;
void clearAllBits()
Set every bit to 0.
const Use & getOperandUse(unsigned i) const
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
void push_back(const T &Elt)
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.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
uint64_t getZExtValue() const
Get zero extended value.
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
A Module instance is used to store all the information related to an LLVM module. ...
bool isOneValue() const
Determine if this is a value of 1.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This class represents zero extension of integer types.
static ConstantAggregateZero * get(Type *Ty)
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
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.
This instruction constructs a fixed permutation of two input vectors.
const Function * getParent() const
Return the enclosing method, or null if none.
void setAllBits()
Set every bit to 1.
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
bool match(Val *V, const Pattern &P)
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
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.
const APInt & getValue() const
Return the constant as an APInt value reference.
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...
A Use represents the edge between a Value definition and its users.
unsigned getNumArgOperands() const
Return the number of call arguments.
static Constant * get(ArrayRef< Constant * > V)
bool isMask(unsigned numBits) const
Type * getVectorElementType() const
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
APInt shl(unsigned shiftAmt) const
Left-shift function.
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
void clearBit(unsigned BitPosition)
Set a given bit to 0.
unsigned getActiveBits() const
Compute the number of active bits in the value.
KnownBits zextOrTrunc(unsigned BitWidth)
Zero extends or truncates the underlying known Zero and One bits.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
unsigned getBitWidth() const
Get the bit width of this value.
void takeName(Value *V)
Transfer the name from V to this value.
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
* 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...
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
APInt trunc(unsigned width) const
Truncate to new width.
The instances of the Type class are immutable: once they are created, they are never changed...
bool isVectorTy() const
True if this is an instance of VectorType.
This is an important base class in LLVM.
void resetAll()
Resets the known state of all bits.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
bool hasConflict() const
Returns true if there is conflicting information.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
uint64_t getNumElements() const
Value * getOperand(unsigned i) const
Constant Vector Declarations.
unsigned countPopulation() const
Count the number of bits set.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
LLVMContext & getContext() const
All values hold a context through their type.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
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...
void makeNonNegative()
Make this value negative.
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
This is the shared class of boolean and integer constants.
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
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 isNullValue() const
Return true if this is the value that would be returned by getNullValue.
void setOperand(unsigned i, Value *Val)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class to represent vector types.
Class for arbitrary precision integers.
unsigned getVectorNumElements() const
llvm_unreachable("Unknown RegBank!")
bool isAllOnesValue() const
Determine if all bits are set.
static int getMaskValue(Constant *Mask, unsigned Elt)
Return the shuffle mask value for the specified element of the mask.
static IntegerType * getInt32Ty(LLVMContext &C)
bool hasOneUse() const
Return true if there is exactly one user of this value.
void setArgOperand(unsigned i, Value *v)
unsigned countTrailingOnes() const
Count the number of trailing one bits.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
VectorType * getType() const
Overload to return most specific vector type.
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw 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.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
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.
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
bool isNullValue() const
Determine if all bits are clear.
APInt abs() const
Get the absolute value;.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
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...
const BasicBlock * getParent() const
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
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.