Lines Matching +full:input +full:- +full:depth

1 //===- InstCombineSimplifyDemanded.cpp ------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
28 VerifyKnownBits("instcombine-verify-known-bits",
39 assert(OpNo < I->getNumOperands() && "Operand index too large");
42 Value *Op = I->getOperand(OpNo);
48 if (C->isSubsetOf(Demanded))
52 I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded));
60 if (unsigned BitWidth = Ty->getScalarSizeInBits())
91 KnownBits &Known, unsigned Depth,
93 Use &U = I->getOperandUse(OpNo);
96 llvm::computeKnownBits(V, Known, Depth, Q);
103 replaceUse(U, UndefValue::get(V->getType()));
107 if (Depth == MaxAnalysisRecursionDepth)
112 llvm::computeKnownBits(V, Known, Depth, Q);
117 if (VInst->hasOneUse()) {
119 NewVal = SimplifyDemandedUseBits(VInst, DemandedMask, Known, Depth, Q);
124 SimplifyMultipleUseDemandedBits(VInst, DemandedMask, Known, Depth, Q);
155 /// some other non-null value if it found out that V is equal to another value
160 unsigned Depth,
163 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
165 Type *VTy = I->getType();
167 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) &&
181 I->setHasNoSignedWrap(false);
182 I->setHasNoUnsignedWrap(false);
187 // If the high-bits of an ADD/SUB/MUL are not demanded, then we do not care
193 DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
195 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1, Q) ||
197 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1, Q)) {
204 switch (I->getOpcode()) {
206 llvm::computeKnownBits(I, Known, Depth, Q);
210 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1, Q) ||
212 Depth + 1, Q))
216 Depth, Q);
226 return I->getOperand(0);
228 return I->getOperand(1);
238 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1, Q) ||
240 Depth + 1, Q)) {
242 I->dropPoisonGeneratingFlags();
247 Depth, Q);
257 return I->getOperand(0);
259 return I->getOperand(1);
266 if (!cast<PossiblyDisjointInst>(I)->isDisjoint()) {
267 WithCache<const Value *> LHSCache(I->getOperand(0), LHSKnown),
268 RHSCache(I->getOperand(1), RHSKnown);
270 cast<PossiblyDisjointInst>(I)->setIsDisjoint(true);
278 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1, Q) ||
279 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1, Q))
283 match(I->getOperand(0), m_Intrinsic<Intrinsic::ctpop>(m_Value(LHS))) &&
284 match(I->getOperand(1), m_Intrinsic<Intrinsic::ctpop>(m_Value(RHS)))) {
285 // (ctpop(X) ^ ctpop(Y)) & 1 --> ctpop(X^Y) & 1
293 Depth, Q);
303 return I->getOperand(0);
305 return I->getOperand(1);
309 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
312 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1));
314 cast<PossiblyDisjointInst>(Or)->setIsDisjoint(true);
315 Or->takeName(I);
316 return InsertNewInstWith(Or, I->getIterator());
322 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
327 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
328 return InsertNewInstWith(And, I->getIterator());
331 // If the RHS is a constant, see if we can change it. Don't alter a -1
335 if (match(I->getOperand(1), m_APInt(C)) && !C->isAllOnes()) {
338 I->setOperand(1, ConstantInt::getAllOnesValue(VTy));
350 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0))) {
352 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
353 match(I->getOperand(1), m_ConstantInt(XorRHS)) &&
354 match(LHSInst->getOperand(1), m_ConstantInt(AndRHS)) &&
358 Constant *AndC = ConstantInt::get(VTy, NewMask & AndRHS->getValue());
359 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
360 InsertNewInstWith(NewAnd, I->getIterator());
362 Constant *XorC = ConstantInt::get(VTy, NewMask & XorRHS->getValue());
364 return InsertNewInstWith(NewXor, I->getIterator());
370 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1, Q) ||
371 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1, Q))
382 if (!match(I->getOperand(OpNo), m_APInt(SelC)))
388 // invert the transform that reduces set bits and infinite-loop.
392 if (!match(I->getOperand(0), m_ICmp(Pred, m_Value(X), m_APInt(CmpC))) ||
393 isa<Constant>(X) || CmpC->getBitWidth() != SelC->getBitWidth())
396 // If the constant is already the same as the ICmp, leave it as-is.
402 I->setOperand(OpNo, ConstantInt::get(I->getType(), *CmpC));
412 adjustKnownBitsForSelectArm(LHSKnown, I->getOperand(0), I->getOperand(1),
413 /*Invert=*/false, Depth, Q);
414 adjustKnownBitsForSelectArm(RHSKnown, I->getOperand(0), I->getOperand(2),
415 /*Invert=*/true, Depth, Q);
420 // If we do not demand the high bits of a right-shifted and truncated value,
424 if (match(I->getOperand(0), m_OneUse(m_LShr(m_Value(X), m_APInt(C))))) {
427 if (C->ult(VTy->getScalarSizeInBits()) &&
428 C->ule(DemandedMask.countl_zero())) {
429 // trunc (lshr X, C) --> lshr (trunc X), C
433 return Builder.CreateLShr(Trunc, C->getZExtValue());
439 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
443 if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1,
446 // input non-negative.
447 I->dropPoisonGeneratingFlags();
451 if (I->getOpcode() == Instruction::ZExt && I->hasNonNeg() &&
459 // Compute the bits in the result that are not present in the input.
460 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
467 InputDemandedBits.setBit(SrcBitWidth-1);
470 if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1, Q))
473 // If the input sign bit is known zero, or if the NewBits are not demanded
478 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy);
479 NewCast->takeName(I);
480 return InsertNewInstWith(NewCast, I->getIterator());
483 // If the sign bit of the input is known set or clear, then we know the
491 // add iN (zext i1 X), (sext i1 Y) --> sext (~X & Y) to iN
495 X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
498 // ----------
500 // Y:1 | -1 | 0 |
501 // ----------
508 // add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
510 X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
511 (I->getOperand(0)->hasOneUse() || I->getOperand(1)->hasOneUse())) {
515 // -----------
516 // Y:0 | -1 | -1 |
517 // Y:1 | -1 | 0 |
518 // -----------
529 APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
531 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1, Q))
541 SimplifyDemandedBits(I, 0, DemandedFromLHS, LHSKnown, Depth + 1, Q))
547 return I->getOperand(0);
549 return I->getOperand(1);
551 // (add X, C) --> (xor X, C) IFF C is equal to the top bit of the DemandMask
554 if (match(I->getOperand(1), m_APInt(C)) &&
555 C->isOneBitSet(DemandedMask.getActiveBits() - 1)) {
558 return Builder.CreateXor(I->getOperand(0), ConstantInt::get(VTy, *C));
563 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
564 bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
572 APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
574 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1, Q))
584 SimplifyDemandedBits(I, 0, DemandedFromLHS, LHSKnown, Depth + 1, Q))
590 return I->getOperand(0);
594 return I->getOperand(1);
597 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
598 bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
610 // odd (has LSB set), then the left-shifted low bit of X is the answer.
613 if (match(I->getOperand(1), m_APInt(C)) && C->countr_zero() == CTZ) {
615 Instruction *Shl = BinaryOperator::CreateShl(I->getOperand(0), ShiftC);
616 return InsertNewInstWith(Shl, I->getIterator());
621 // 'Quadratic Reciprocity': X * X -> 0 for bit[1]
622 if (I->getOperand(0) == I->getOperand(1) && DemandedMask.ult(4)) {
624 Instruction *And1 = BinaryOperator::CreateAnd(I->getOperand(0), One);
625 return InsertNewInstWith(And1, I->getIterator());
628 llvm::computeKnownBits(I, Known, Depth, Q);
633 if (match(I->getOperand(1), m_APInt(SA))) {
635 if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt))))
636 if (Instruction *Shr = dyn_cast<Instruction>(I->getOperand(0)))
641 // Do not simplify if shl is part of funnel-shift pattern
642 if (I->hasOneUse()) {
643 auto *Inst = dyn_cast<Instruction>(I->user_back());
644 if (Inst && Inst->getOpcode() == BinaryOperator::Or) {
649 llvm::computeKnownBits(I, Known, Depth, Q);
658 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth - 1);
660 if (I->hasNoSignedWrap()) {
661 unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
663 ComputeNumSignBits(I->getOperand(0), Depth + 1, Q.CxtI);
664 if (SignBits > ShiftAmt && SignBits - ShiftAmt >= NumHiDemandedBits)
665 return I->getOperand(0);
668 // If we can pre-shift a right-shifted constant to the left without
670 // the left-shift:
671 // (C >> X) << LeftShiftAmtC --> (C << LeftShiftAmtC) >> X
674 if (match(I->getOperand(0), m_LShr(m_ImmConstant(C), m_Value(X)))) {
681 return InsertNewInstWith(Lshr, I->getIterator());
690 if (IOp->hasNoSignedWrap())
692 else if (IOp->hasNoUnsignedWrap())
695 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1, Q))
700 /* NUW */ IOp->hasNoUnsignedWrap(),
701 /* NSW */ IOp->hasNoSignedWrap());
705 // demanding those bits from the pre-shifted operand either.
707 APInt DemandedFromOp(APInt::getLowBitsSet(BitWidth, BitWidth - CTLZ));
708 if (SimplifyDemandedBits(I, 0, DemandedFromOp, Known, Depth + 1, Q)) {
710 I->dropPoisonGeneratingFlags();
714 llvm::computeKnownBits(I, Known, Depth, Q);
720 if (match(I->getOperand(1), m_APInt(SA))) {
721 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
723 // Do not simplify if lshr is part of funnel-shift pattern
724 if (I->hasOneUse()) {
725 auto *Inst = dyn_cast<Instruction>(I->user_back());
726 if (Inst && Inst->getOpcode() == BinaryOperator::Or) {
731 llvm::computeKnownBits(I, Known, Depth, Q);
743 unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
745 ComputeNumSignBits(I->getOperand(0), Depth + 1, Q.CxtI);
747 return I->getOperand(0);
749 // If we can pre-shift a left-shifted constant to the right without
751 // then eliminate the right-shift:
752 // (C << X) >> RightShiftAmtC --> (C >> RightShiftAmtC) << X
755 if (match(I->getOperand(0), m_Shl(m_ImmConstant(C), m_Value(X)))) {
762 return InsertNewInstWith(Shl, I->getIterator());
769 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1, Q)) {
771 I->dropPoisonGeneratingFlags();
779 llvm::computeKnownBits(I, Known, Depth, Q);
784 unsigned SignBits = ComputeNumSignBits(I->getOperand(0), Depth + 1, Q.CxtI);
788 unsigned NumHiDemandedBits = BitWidth - DemandedMask.countr_zero();
790 return I->getOperand(0);
792 // If this is an arithmetic shift right and only the low-bit is set, we can
794 // variable. The low bit of the shift cannot be an input sign bit unless
799 I->getOperand(0), I->getOperand(1), I->getName());
800 return InsertNewInstWith(NewVal, I->getIterator());
804 if (match(I->getOperand(1), m_APInt(SA))) {
805 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
814 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1, Q)) {
816 I->dropPoisonGeneratingFlags();
820 // If the input sign bit is known to be zero, or if none of the shifted in
822 if (Known.Zero[BitWidth - 1] || !ShiftedInBitsDemanded) {
823 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
824 I->getOperand(1));
825 LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
826 LShr->takeName(I);
827 return InsertNewInstWith(LShr, I->getIterator());
832 ShiftAmt != 0, I->isExact());
834 llvm::computeKnownBits(I, Known, Depth, Q);
841 if (match(I->getOperand(1), m_APInt(SA))) {
843 unsigned RHSTrailingZeros = SA->countr_zero();
845 APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros);
846 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1, Q)) {
849 I->dropPoisonGeneratingFlags();
854 cast<BinaryOperator>(I)->isExact());
856 llvm::computeKnownBits(I, Known, Depth, Q);
862 if (match(I->getOperand(1), m_APInt(Rem))) {
863 // X % -1 demands all the bits because we don't want to introduce
864 // INT_MIN % -1 (== undef) by accident.
865 if (Rem->isAllOnes())
867 APInt RA = Rem->abs();
870 return I->getOperand(0);
872 APInt LowBits = RA - 1;
874 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
881 // If LHS is non-negative or has all low bits zero, then the upper bits
895 llvm::computeKnownBits(I, Known, Depth, Q);
901 switch (II->getIntrinsicID()) {
904 return II->getArgOperand(0);
912 if (DemandedMask == 1 && VTy->getScalarSizeInBits() % 2 == 0 &&
913 match(II->getArgOperand(0), m_Not(m_Value(X)))) {
915 II->getModule(), Intrinsic::ctpop, VTy);
916 return InsertNewInstWith(CallInst::Create(Ctpop, {X}), I->getIterator());
922 // just shift the input byte into position to eliminate the bswap.
932 if (BitWidth - NLZ - NTZ == 8) {
938 II->getArgOperand(0), ConstantInt::get(VTy, NLZ - NTZ));
941 II->getArgOperand(0), ConstantInt::get(VTy, NTZ - NLZ));
942 NewVal->takeName(I);
943 return InsertNewInstWith(NewVal, I->getIterator());
948 unsigned MaskWidth = I->getOperand(1)->getType()->getScalarSizeInBits();
951 if (SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1, Q) ||
954 RHSKnown, Depth + 1, Q))
957 // TODO: Should be 1-extend
968 !match(I->getOperand(1), m_Zero()))
970 *I, 1, Constant::getNullValue(I->getOperand(1)->getType()));
977 return I->getOperand(0);
986 // -> (ptrmask (getelementptr i8, ptr p, imm (i & mask)), imm mask)
995 LHSKnown = computeKnownBits(InnerPtr, Depth + 1, I);
998 uint64_t PointerAlignBits = (uint64_t(1) << trailingZeros) - 1;
1007 auto *GEP = cast<GEPOperator>(II->getArgOperand(0));
1010 DL.getIndexType(GEP->getPointerOperand()->getType());
1012 GEP->getSourceElementType(), InnerPtr,
1014 GEP->getName(), GEP->isInBounds());
1028 if (!match(I->getOperand(2), m_APInt(SA)))
1031 // Normalize to funnel shift left. APInt shifts of BitWidth are well-
1032 // defined, so no need to special-case zero shifts here.
1033 uint64_t ShiftAmt = SA->urem(BitWidth);
1034 if (II->getIntrinsicID() == Intrinsic::fshr)
1035 ShiftAmt = BitWidth - ShiftAmt;
1038 APInt DemandedMaskRHS(DemandedMask.shl(BitWidth - ShiftAmt));
1039 if (I->getOperand(0) != I->getOperand(1)) {
1041 Depth + 1, Q) ||
1042 SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1,
1048 LHSKnown = computeKnownBits(I->getOperand(0), Depth + 1, I);
1050 !match(I->getOperand(0), m_SpecificInt(LHSKnown.One))) {
1055 RHSKnown = computeKnownBits(I->getOperand(1), Depth + 1, I);
1057 !match(I->getOperand(1), m_SpecificInt(RHSKnown.One))) {
1064 RHSKnown.Zero.lshr(BitWidth - ShiftAmt);
1066 RHSKnown.One.lshr(BitWidth - ShiftAmt);
1072 // The lowest non-zero bit of DemandMask is higher than the highest
1073 // non-zero bit of C.
1076 if (match(II->getArgOperand(1), m_APInt(C)) &&
1077 CTZ >= C->getActiveBits())
1078 return II->getArgOperand(0);
1083 // The lowest non-zero bit of DemandMask is higher than the highest
1084 // non-one bit of C.
1088 if (match(II->getArgOperand(1), m_APInt(C)) &&
1089 CTZ >= C->getBitWidth() - C->countl_one())
1090 return II->getArgOperand(0);
1105 llvm::computeKnownBits(I, Known, Depth, Q);
1110 if (I->getType()->isPointerTy()) {
1111 Align Alignment = I->getPointerAlignment(DL);
1119 if (!I->getType()->isPointerTy() &&
1124 KnownBits ReferenceKnown = llvm::computeKnownBits(I, Depth, Q);
1127 << I->getFunction()->getName() << "\n";
1141 Instruction *I, const APInt &DemandedMask, KnownBits &Known, unsigned Depth,
1144 Type *ITy = I->getType();
1153 switch (I->getOpcode()) {
1155 llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1156 llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1158 Depth, Q);
1159 computeKnownBitsFromContext(I, Known, Depth, Q);
1169 return I->getOperand(0);
1171 return I->getOperand(1);
1176 llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1177 llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1179 Depth, Q);
1180 computeKnownBitsFromContext(I, Known, Depth, Q);
1187 // We can simplify (X|Y) -> X or Y in the user's context if we know that
1192 return I->getOperand(0);
1194 return I->getOperand(1);
1199 llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1200 llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1202 Depth, Q);
1203 computeKnownBitsFromContext(I, Known, Depth, Q);
1210 // We can simplify (X^Y) -> X or Y in the user's context if we know that
1214 return I->getOperand(0);
1216 return I->getOperand(1);
1222 APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
1226 llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1228 return I->getOperand(0);
1230 llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1232 return I->getOperand(1);
1234 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1235 bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
1238 computeKnownBitsFromContext(I, Known, Depth, Q);
1243 APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
1247 llvm::computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, Q);
1249 return I->getOperand(0);
1251 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1252 bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
1253 llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1256 computeKnownBitsFromContext(I, Known, Depth, Q);
1261 llvm::computeKnownBits(I, Known, Depth, Q);
1278 ShiftLC == ShiftRC && ShiftLC->ult(BitWidth) &&
1280 BitWidth, BitWidth - ShiftRC->getZExtValue()))) {
1288 llvm::computeKnownBits(I, Known, Depth, Q);
1303 /// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign
1304 /// of "C2-C1".
1310 /// 2) We don't care those bits in S, per the input DemandedMask.
1322 return nullptr; // No-op.
1324 Value *VarX = Shr->getOperand(0);
1325 Type *Ty = VarX->getType();
1326 unsigned BitWidth = Ty->getScalarSizeInBits();
1334 Known.Zero.setLowBits(ShlAmt - 1);
1340 bool isLshr = (Shr->getOpcode() == Instruction::LShr);
1345 BitMask2 <<= (ShlAmt - ShrAmt);
1347 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
1348 BitMask2.ashr(ShrAmt - ShlAmt);
1351 // Check if condition-2 (see the comment to this function) is satified.
1356 if (!Shr->hasOneUse())
1361 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
1364 New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
1365 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
1367 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
1370 if (cast<BinaryOperator>(Shr)->isExact())
1371 New->setIsExact(true);
1374 return InsertNewInstWith(New, Shl->getIterator());
1396 unsigned Depth,
1399 // compile-time constant.
1400 if (isa<ScalableVectorType>(V->getType()))
1403 unsigned VWidth = cast<FixedVectorType>(V->getType())->getNumElements();
1415 return PoisonValue::get(V->getType());
1426 Type *EltTy = cast<VectorType>(V->getType())->getElementType();
1436 Constant *Elt = C->getAggregateElement(i);
1449 // Limit search depth.
1450 if (Depth == 10)
1457 if (!V->hasOneUse()) {
1458 // Quit if we find multiple users of a non-root value though.
1461 if (Depth != 0)
1477 Value *Op = II ? II->getArgOperand(OpNum) : Inst->getOperand(OpNum);
1478 if (Value *V = SimplifyDemandedVectorElts(Op, Demanded, Undef, Depth + 1)) {
1486 switch (I->getOpcode()) {
1507 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1508 if (i == 0 ? match(I->getOperand(i), m_Undef())
1509 : match(I->getOperand(i), m_Poison())) {
1514 if (I->getOperand(i)->getType()->isVectorTy()) {
1529 // demand exactly the same input as we produce.
1530 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
1538 // The element inserted overwrites whatever was there, so the input demanded
1540 unsigned IdxNo = Idx->getZExtValue();
1552 match(I->getOperand(1),
1554 Vec->getType() == I->getType()) {
1564 return I->getOperand(0);
1573 assert(Shuffle->getOperand(0)->getType() ==
1574 Shuffle->getOperand(1)->getType() &&
1576 unsigned OpWidth = cast<FixedVectorType>(Shuffle->getOperand(0)->getType())
1577 ->getNumElements();
1580 if (all_of(Shuffle->getShuffleMask(), [](int Elt) { return Elt == 0; }) &&
1582 if (!isa<PoisonValue>(I->getOperand(1))) {
1583 I->setOperand(1, PoisonValue::get(I->getOperand(1)->getType()));
1599 unsigned MaskVal = Shuffle->getMaskValue(i);
1600 if (MaskVal != -1u) {
1606 RightDemanded.setBit(MaskVal - OpWidth);
1631 unsigned MaskVal = Shuffle->getMaskValue(i);
1638 return Shuffle->getOperand(0);
1642 unsigned LHSIdx = -1u, LHSValIdx = -1u;
1643 unsigned RHSIdx = -1u, RHSValIdx = -1u;
1647 unsigned MaskVal = Shuffle->getMaskValue(i);
1648 if (MaskVal == -1u) {
1658 LHSIdx = LHSIdx == -1u ? i : OpWidth;
1659 LHSValIdx = LHSValIdx == -1u ? MaskVal : OpWidth;
1663 if (RHSPoisonElts[MaskVal - OpWidth]) {
1667 RHSIdx = RHSIdx == -1u ? i : OpWidth;
1668 RHSValIdx = RHSValIdx == -1u ? MaskVal - OpWidth : OpWidth;
1669 RHSUniform = RHSUniform && (MaskVal - OpWidth == i);
1676 // shufflevector V, C, <v1, v2, .., ci, .., vm> ->
1677 // insertelement V, C[ci], ci-n
1679 cast<FixedVectorType>(Shuffle->getType())->getNumElements()) {
1682 unsigned Idx = -1u;
1686 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) {
1687 Op = Shuffle->getOperand(1);
1688 Value = CV->getOperand(LHSValIdx);
1693 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) {
1694 Op = Shuffle->getOperand(0);
1695 Value = CV->getOperand(RHSValIdx);
1699 // Found constant vector with single element - convert to insertelement.
1702 Op, Value, ConstantInt::get(Type::getInt64Ty(I->getContext()), Idx),
1703 Shuffle->getName());
1704 InsertNewInstWith(New, Shuffle->getIterator());
1715 Elts.push_back(Shuffle->getMaskValue(i));
1717 Shuffle->setShuffleMask(Elts);
1726 if (Sel->getCondition()->getType()->isVectorTy()) {
1737 if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {
1741 Constant *CElt = CV->getAggregateElement(i);
1747 if (CElt->isNullValue())
1763 // Vector->vector casts only.
1764 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1766 unsigned InVWidth = cast<FixedVectorType>(VTy)->getNumElements();
1772 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1778 // elements in the input then an input element is live if any of the
1785 // If the number of elements in the input is a multiple of the number of
1786 // elements in the output then an input element is live if the
1803 // elements in the input then an output element is undef if the
1804 // corresponding input element is undef.
1809 // If the number of elements in the input is a multiple of the number of
1811 // corresponding input elements are undef.
1830 switch (II->getIntrinsicID()) {
1838 if (auto *CV = dyn_cast<ConstantVector>(II->getOperand(2)))
1840 Constant *CElt = CV->getAggregateElement(i);
1841 if (CElt->isNullValue())
1843 else if (CElt->isAllOnesValue())
1846 if (II->getIntrinsicID() == Intrinsic::masked_gather)
1872 if (match(I, m_BinOp(BO)) && !BO->isIntDivRem() && !BO->isShift()) {
1873 Value *X = BO->getOperand(0);
1874 Value *Y = BO->getOperand(1);
1884 // -->
1892 if (DemandedElts == 1 && !X->hasOneUse() && !Y->hasOneUse() &&
1893 BO->hasOneUse() ) {
1895 auto findShufBO = [&](bool MatchShufAsOp0) -> User * {
1896 // Try to use shuffle-of-operand in place of an operand:
1897 // bo X, Y --> bo (shuf X), Y
1898 // bo X, Y --> bo X, (shuf Y)
1899 BinaryOperator::BinaryOps Opcode = BO->getOpcode();
1902 for (User *U : OtherOp->users()) {
1905 if (BO->isCommutative()
1934 return PoisonValue::get(I->getType());
1939 /// For floating-point classes that resolve to a single bit pattern, return that
1960 unsigned Depth, Instruction *CxtI) {
1961 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1962 Type *VTy = V->getType();
1969 if (Depth == MaxAnalysisRecursionDepth)
1975 Known = computeKnownFPClass(V, fcAllFlags, CxtI, Depth + 1);
1981 if (!I->hasOneUse())
1985 switch (I->getOpcode()) {
1988 Depth + 1))
1995 switch (CI->getIntrinsicID()) {
1998 Depth + 1))
2003 if (SimplifyDemandedFPClass(I, 0, DemandedMask, Known, Depth + 1))
2009 if (SimplifyDemandedFPClass(I, 0, DemandedMaskAnySign, Known, Depth + 1))
2014 I->setOperand(1, ConstantFP::get(VTy, -1.0));
2020 I->setOperand(1, ConstantFP::getZero(VTy));
2025 computeKnownFPClass(I->getOperand(1), fcAllFlags, CxtI, Depth + 1);
2030 Known = computeKnownFPClass(I, ~DemandedMask, CxtI, Depth + 1);
2038 if (SimplifyDemandedFPClass(I, 2, DemandedMask, KnownRHS, Depth + 1) ||
2039 SimplifyDemandedFPClass(I, 1, DemandedMask, KnownLHS, Depth + 1))
2043 return I->getOperand(2);
2045 return I->getOperand(1);
2052 Known = computeKnownFPClass(I, ~DemandedMask, CxtI, Depth + 1);
2062 unsigned Depth) {
2063 Use &U = I->getOperandUse(OpNo);
2065 SimplifyDemandedUseFPClass(U.get(), DemandedMask, Known, Depth, I);