46 #define DEBUG_TYPE "instcombine" 55 if (
auto *
C = dyn_cast<Constant>(V))
56 return IsConstantExtractIndex ||
C->getSplatValue();
62 return IsConstantExtractIndex;
92 for (
auto U : PN->
users()) {
98 }
else if (!PHIUser) {
99 PHIUser = cast<Instruction>(U);
117 PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith(
125 if (PHIInVal == PHIUser) {
130 unsigned opId = (B0->
getOperand(0) == PN) ? 1 : 0;
135 Value *newPHIUser = InsertNewInstWith(
137 scalarPHI,
Op, B0), *B0);
145 if (pos && !isa<PHINode>(pos)) {
151 InsertNewInstWith(newEI, *InsertPos);
157 for (
auto E : Extracts)
158 replaceInstUsesWith(*
E, scalarPHI);
180 if (NumSrcElts == NumElts)
186 if (NumSrcElts < NumElts) {
197 unsigned NarrowingRatio = NumElts / NumSrcElts;
198 if (ExtIndexC / NarrowingRatio != InsIndexC)
212 unsigned Chunk = ExtIndexC % NarrowingRatio;
214 Chunk = NarrowingRatio - 1 - Chunk;
221 if (NeedSrcBitcast && NeedDestBitcast)
226 unsigned ShAmt = Chunk * DestWidth;
232 if (NeedSrcBitcast || NeedDestBitcast)
235 if (NeedSrcBitcast) {
247 if (NeedDestBitcast) {
261 SQ.getWithInstruction(&EI)))
262 return replaceInstUsesWith(EI, V);
271 if (!IndexC->getValue().ule(NumElts))
277 if (SrcVec->
hasOneUse() && NumElts != 1) {
278 APInt UndefElts(NumElts, 0);
279 APInt DemandedElts(NumElts, 0);
280 DemandedElts.
setBit(IndexC->getZExtValue());
281 if (
Value *V = SimplifyDemandedVectorElts(SrcVec, DemandedElts,
293 if (
auto *Phi = dyn_cast<PHINode>(SrcVec))
294 if (
Instruction *ScalarPHI = scalarizePHI(EI, Phi))
302 Value *E0 = Builder.CreateExtractElement(X, Index);
303 Value *E1 = Builder.CreateExtractElement(
Y, Index);
312 Value *E0 = Builder.CreateExtractElement(X, Index);
313 Value *E1 = Builder.CreateExtractElement(Y, Index);
317 if (
auto *
I = dyn_cast<Instruction>(SrcVec)) {
318 if (
auto *
IE = dyn_cast<InsertElementInst>(
I)) {
320 if (
IE->getOperand(2) ==
Index)
321 return replaceInstUsesWith(EI,
IE->getOperand(1));
324 if (isa<Constant>(
IE->getOperand(2)) && IndexC) {
325 Worklist.AddValue(SrcVec);
329 }
else if (
auto *SVI = dyn_cast<ShuffleVectorInst>(
I)) {
332 if (
auto *Elt = dyn_cast<ConstantInt>(Index)) {
333 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
340 if (SrcIdx < (
int)LHSWidth)
341 Src = SVI->getOperand(0);
344 Src = SVI->getOperand(1);
351 }
else if (
auto *CI = dyn_cast<CastInst>(
I)) {
355 if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) {
356 Value *EE = Builder.CreateExtractElement(CI->getOperand(0),
Index);
357 Worklist.AddValue(EE);
370 "Invalid CollectSingleShuffleElements");
373 if (isa<UndefValue>(V)) {
379 for (
unsigned i = 0; i != NumElts; ++i)
385 for (
unsigned i = 0; i != NumElts; ++i)
393 Value *VecOp = IEI->getOperand(0);
394 Value *ScalarOp = IEI->getOperand(1);
395 Value *IdxOp = IEI->getOperand(2);
397 if (!isa<ConstantInt>(IdxOp))
399 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
401 if (isa<UndefValue>(ScalarOp)) {
411 unsigned ExtractedIdx =
412 cast<ConstantInt>(EI->
getOperand(1))->getZExtValue();
422 Mask[InsertedIdx % NumElts] =
427 Mask[InsertedIdx % NumElts] =
429 ExtractedIdx + NumLHSElts);
454 NumExtElts >= NumInsElts)
463 for (
unsigned i = 0; i < NumExtElts; ++i)
465 for (
unsigned i = NumExtElts; i < NumInsElts; ++i)
470 BasicBlock *InsertionBlock = (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst))
483 if (InsertionBlock != InsElt->
getParent())
501 if (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst))
502 WideVec->insertAfter(ExtVecOpInst);
535 if (isa<UndefValue>(V)) {
537 return std::make_pair(
541 if (isa<ConstantAggregateZero>(V)) {
543 return std::make_pair(V,
nullptr);
548 Value *VecOp = IEI->getOperand(0);
549 Value *ScalarOp = IEI->getOperand(1);
550 Value *IdxOp = IEI->getOperand(2);
553 if (isa<ConstantInt>(EI->
getOperand(1)) && isa<ConstantInt>(IdxOp)) {
554 unsigned ExtractedIdx =
555 cast<ConstantInt>(EI->
getOperand(1))->getZExtValue();
556 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
560 if (EI->
getOperand(0) == PermittedRHS || PermittedRHS ==
nullptr) {
563 assert(LR.second ==
nullptr || LR.second == RHS);
565 if (LR.first->getType() != RHS->
getType()) {
572 for (
unsigned i = 0; i < NumElts; ++i)
574 return std::make_pair(V,
nullptr);
578 Mask[InsertedIdx % NumElts] =
580 NumLHSElts+ExtractedIdx);
581 return std::make_pair(LR.first, RHS);
584 if (VecOp == PermittedRHS) {
587 unsigned NumLHSElts =
589 for (
unsigned i = 0; i != NumElts; ++i)
592 i == InsertedIdx ? ExtractedIdx : NumLHSElts + i));
593 return std::make_pair(EI->
getOperand(0), PermittedRHS);
601 return std::make_pair(EI->
getOperand(0), PermittedRHS);
607 for (
unsigned i = 0; i != NumElts; ++i)
609 return std::make_pair(V,
nullptr);
620 bool IsRedundant =
false;
634 if (UserInsInst->getIndices() == FirstIndices) {
643 return replaceInstUsesWith(I, I.
getOperand(0));
652 if (MaskSize != VecSize)
657 for (
int i = 0; i != MaskSize; ++i) {
659 if (Elt != -1 && Elt != i && Elt != i + VecSize)
681 if (NumElements == 1)
693 if (!Idx || CurrIE->
getOperand(1) != SplatVal)
700 if (CurrIE != &InsElt &&
701 (!CurrIE->
hasOneUse() && (NextIE !=
nullptr || !Idx->isZero())))
704 ElementPresent[Idx->getZExtValue()] =
true;
710 if (
llvm::any_of(ElementPresent, [](
bool Present) {
return !Present; }))
716 InsertFirst = FirstIE;
741 if (!InsElt1 || !InsElt1->hasOneUse())
748 match(InsElt1->getOperand(1),
m_Value(Y)) && !isa<Constant>(Y) &&
765 if (!Inst || !Inst->hasOneUse())
767 if (
auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.
getOperand(0))) {
770 Constant *ShufConstVec, *InsEltScalar;
771 uint64_t InsEltIndex;
797 for (
unsigned I = 0;
I != NumElts; ++
I) {
798 if (
I == InsEltIndex) {
799 NewShufElts[
I] = InsEltScalar;
814 }
else if (
auto *IEI = dyn_cast<InsertElementInst>(Inst)) {
819 uint64_t InsertIdx[2];
832 for (uint64_t
I : InsertIdx) {
842 for (
unsigned I = 0;
I < NumElts; ++
I) {
864 VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE)))
865 return replaceInstUsesWith(IE, V);
868 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
869 replaceInstUsesWith(IE, VecOp);
873 uint64_t InsertedIdx, ExtractedIdx;
880 if (ExtractedIdx >= NumExtractVectorElts)
881 return replaceInstUsesWith(IE, VecOp);
883 if (InsertedIdx >= NumInsertVectorElts)
888 if (ExtVecOp == VecOp && ExtractedIdx == InsertedIdx)
889 return replaceInstUsesWith(IE, VecOp);
906 if (!Insert.hasOneUse())
915 if (isShuffleRootCandidate(IE)) {
921 if (LR.first != &IE && LR.second != &IE) {
923 if (LR.second ==
nullptr)
932 APInt UndefElts(VWidth, 0);
934 if (
Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
936 return replaceInstUsesWith(IE, V);
957 unsigned Depth = 5) {
959 if (isa<Constant>(V))
964 if (!I)
return false;
970 if (
Depth == 0)
return false;
974 case Instruction::FAdd:
975 case Instruction::Sub:
976 case Instruction::FSub:
977 case Instruction::Mul:
978 case Instruction::FMul:
979 case Instruction::UDiv:
980 case Instruction::SDiv:
981 case Instruction::FDiv:
982 case Instruction::URem:
983 case Instruction::SRem:
984 case Instruction::FRem:
985 case Instruction::Shl:
986 case Instruction::LShr:
987 case Instruction::AShr:
988 case Instruction::And:
989 case Instruction::Or:
990 case Instruction::Xor:
991 case Instruction::ICmp:
992 case Instruction::FCmp:
993 case Instruction::Trunc:
994 case Instruction::ZExt:
995 case Instruction::SExt:
996 case Instruction::FPToUI:
997 case Instruction::FPToSI:
998 case Instruction::UIToFP:
999 case Instruction::SIToFP:
1000 case Instruction::FPTrunc:
1001 case Instruction::FPExt:
1002 case Instruction::GetElementPtr: {
1016 case Instruction::InsertElement: {
1018 if (!CI)
return false;
1023 bool SeenOnce =
false;
1024 for (
int i = 0, e = Mask.
size(); i != e; ++i) {
1025 if (Mask[i] == ElementNumber) {
1044 case Instruction::FAdd:
1045 case Instruction::Sub:
1046 case Instruction::FSub:
1047 case Instruction::Mul:
1048 case Instruction::FMul:
1049 case Instruction::UDiv:
1050 case Instruction::SDiv:
1051 case Instruction::FDiv:
1052 case Instruction::URem:
1053 case Instruction::SRem:
1054 case Instruction::FRem:
1055 case Instruction::Shl:
1056 case Instruction::LShr:
1057 case Instruction::AShr:
1058 case Instruction::And:
1059 case Instruction::Or:
1060 case Instruction::Xor: {
1062 assert(NewOps.
size() == 2 &&
"binary operator with #ops != 2");
1065 NewOps[0], NewOps[1],
"", BO);
1066 if (isa<OverflowingBinaryOperator>(BO)) {
1070 if (isa<PossiblyExactOperator>(BO)) {
1071 New->setIsExact(BO->
isExact());
1073 if (isa<FPMathOperator>(BO))
1074 New->copyFastMathFlags(I);
1077 case Instruction::ICmp:
1078 assert(NewOps.
size() == 2 &&
"icmp with #ops != 2");
1080 NewOps[0], NewOps[1]);
1081 case Instruction::FCmp:
1082 assert(NewOps.
size() == 2 &&
"fcmp with #ops != 2");
1084 NewOps[0], NewOps[1]);
1085 case Instruction::Trunc:
1086 case Instruction::ZExt:
1087 case Instruction::SExt:
1088 case Instruction::FPToUI:
1089 case Instruction::FPToSI:
1090 case Instruction::UIToFP:
1091 case Instruction::SIToFP:
1092 case Instruction::FPTrunc:
1093 case Instruction::FPExt: {
1098 NewOps[0]->getType()->getVectorNumElements());
1099 assert(NewOps.
size() == 1 &&
"cast with #ops != 1");
1103 case Instruction::GetElementPtr: {
1104 Value *Ptr = NewOps[0];
1107 cast<GetElementPtrInst>(I)->getSourceElementType(), Ptr, Idx,
"", I);
1108 GEP->
setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds());
1121 if (isa<UndefValue>(V))
1124 if (isa<ConstantAggregateZero>(V))
1127 if (
Constant *
C = dyn_cast<Constant>(V)) {
1129 for (
int i = 0, e = Mask.
size(); i != e; ++i) {
1140 switch (I->getOpcode()) {
1142 case Instruction::FAdd:
1143 case Instruction::Sub:
1144 case Instruction::FSub:
1145 case Instruction::Mul:
1146 case Instruction::FMul:
1147 case Instruction::UDiv:
1148 case Instruction::SDiv:
1149 case Instruction::FDiv:
1150 case Instruction::URem:
1151 case Instruction::SRem:
1152 case Instruction::FRem:
1153 case Instruction::Shl:
1154 case Instruction::LShr:
1155 case Instruction::AShr:
1156 case Instruction::And:
1157 case Instruction::Or:
1158 case Instruction::Xor:
1159 case Instruction::ICmp:
1160 case Instruction::FCmp:
1161 case Instruction::Trunc:
1162 case Instruction::ZExt:
1163 case Instruction::SExt:
1164 case Instruction::FPToUI:
1165 case Instruction::FPToSI:
1166 case Instruction::UIToFP:
1167 case Instruction::SIToFP:
1168 case Instruction::FPTrunc:
1169 case Instruction::FPExt:
1171 case Instruction::GetElementPtr: {
1173 bool NeedsRebuild = (Mask.
size() != I->getType()->getVectorNumElements());
1174 for (
int i = 0, e = I->getNumOperands(); i != e; ++i) {
1177 NeedsRebuild |= (V != I->getOperand(i));
1184 case Instruction::InsertElement: {
1185 int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue();
1192 for (
int e = Mask.
size(); Index != e; ++
Index) {
1193 if (Mask[Index] == Element) {
1213 bool &isLHSID,
bool &isRHSID) {
1214 isLHSID = isRHSID =
true;
1216 for (
unsigned i = 0, e = Mask.
size(); i != e; ++i) {
1217 if (Mask[i] < 0)
continue;
1219 isLHSID &= (Mask[i] == (int)i);
1222 isRHSID &= (Mask[i]-e == i);
1235 unsigned MaskElems = Mask.
size();
1236 unsigned BegIdx = Mask.
front();
1237 unsigned EndIdx = Mask.
back();
1238 if (BegIdx > EndIdx || EndIdx >= LHSElems || EndIdx - BegIdx != MaskElems - 1)
1240 for (
unsigned I = 0;
I != MaskElems; ++
I)
1241 if (static_cast<unsigned>(Mask[
I]) != BegIdx + I)
1254 Opcode(Opc), Op0(V0), Op1(V1) {}
1255 operator bool()
const {
return Opcode != 0; }
1266 case Instruction::Shl: {
1271 return { Instruction::Mul, BO0, ShlOne };
1275 case Instruction::Or: {
1289 assert(Shuf.
isSelect() &&
"Must have select-equivalent shuffle");
1306 auto *BO = cast<BinaryOperator>(Op0IsBinop ? Op0 : Op1);
1320 bool MightCreatePoisonOrUB =
1323 if (MightCreatePoisonOrUB)
1328 Value *
X = Op0IsBinop ? Op1 : Op0;
1357 bool ConstantsAreOp1;
1360 ConstantsAreOp1 =
true;
1363 ConstantsAreOp1 =
false;
1370 bool DropNSW =
false;
1371 if (ConstantsAreOp1 && Opc0 != Opc1) {
1375 if (Opc0 == Instruction::Shl || Opc1 == Instruction::Shl)
1378 assert(isa<Constant>(AltB0.Op1) &&
"Expecting constant with alt binop");
1379 Opc0 = AltB0.Opcode;
1380 C0 = cast<Constant>(AltB0.Op1);
1382 assert(isa<Constant>(AltB1.Op1) &&
"Expecting constant with alt binop");
1383 Opc1 = AltB1.Opcode;
1384 C1 = cast<Constant>(AltB1.Op1);
1401 bool MightCreatePoisonOrUB =
1404 if (MightCreatePoisonOrUB)
1426 if (MightCreatePoisonOrUB && !ConstantsAreOp1)
1480 !cast<ShuffleVectorInst>(Cond)->isIdentityWithPadding())
1516 "Identity with extract must have less elements than its inputs");
1518 for (
unsigned i = 0; i != NumElts; ++i) {
1521 NewMask[i] = isa<UndefValue>(ExtractMaskElt) ? ExtractMaskElt : MaskElt;
1534 int NumElts = Mask.
size();
1547 int NewInsIndex = -1;
1548 for (
int i = 0; i != NumElts; ++i) {
1554 if (Mask[i] == NumElts + i)
1558 if (NewInsIndex != -1 || Mask[i] != IndexC->getSExtValue())
1565 assert(NewInsIndex != -1 &&
"Did not fold shuffle with unused operand?");
1577 if (isShufflingScalarIntoOp1(Scalar, IndexC))
1585 if (isShufflingScalarIntoOp1(Scalar, IndexC))
1595 LHS, RHS, SVI.
getMask(), SVI.
getType(), SQ.getWithInstruction(&SVI)))
1596 return replaceInstUsesWith(SVI, V);
1605 APInt UndefElts(VWidth, 0);
1607 if (
Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
1609 return replaceInstUsesWith(SVI, V);
1624 bool MadeChange =
false;
1628 if (LHS == RHS || isa<UndefValue>(LHS)) {
1631 for (
unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
1637 if ((Mask[i] >= (
int)e && isa<UndefValue>(RHS)) ||
1638 (Mask[i] < (
int)e && isa<UndefValue>(LHS))) {
1642 Mask[i] = Mask[i] % e;
1654 if (VWidth == LHSWidth) {
1656 bool isLHSID, isRHSID;
1660 if (isLHSID)
return replaceInstUsesWith(SVI, LHS);
1661 if (isRHSID)
return replaceInstUsesWith(SVI, RHS);
1666 return replaceInstUsesWith(SVI, V);
1700 unsigned MaskElems = Mask.
size();
1703 unsigned SrcElemBitWidth = DL.getTypeSizeInBits(SrcTy->
getElementType());
1704 assert(SrcElemBitWidth &&
"vector elements must have a bitwidth");
1710 if (!BC->use_empty())
1714 unsigned BegIdx = Mask.
front();
1715 Type *TgtTy = BC->getDestTy();
1716 unsigned TgtElemBitWidth = DL.getTypeSizeInBits(TgtTy);
1717 if (!TgtElemBitWidth)
1719 unsigned TgtNumElems = VecBitWidth / TgtElemBitWidth;
1720 bool VecBitWidthsEqual = VecBitWidth == TgtNumElems * TgtElemBitWidth;
1721 bool BegIsAligned = 0 == ((SrcElemBitWidth * BegIdx) % TgtElemBitWidth);
1722 if (!VecBitWidthsEqual)
1727 if (!BegIsAligned) {
1732 for (
unsigned I = 0,
E = MaskElems, Idx = BegIdx;
I !=
E; ++Idx, ++
I)
1739 unsigned SrcElemsPerTgtElem = TgtElemBitWidth / SrcElemBitWidth;
1740 assert(SrcElemsPerTgtElem);
1741 BegIdx /= SrcElemsPerTgtElem;
1742 bool BCAlreadyExists = NewBCs.
find(CastSrcTy) != NewBCs.
end();
1746 : Builder.CreateBitCast(V, CastSrcTy, SVI.
getName() +
".bc");
1747 if (!BCAlreadyExists)
1748 NewBCs[CastSrcTy] = NewBC;
1749 auto *
Ext = Builder.CreateExtractElement(
1753 replaceInstUsesWith(*BC,
Ext);
1804 if (!isa<UndefValue>(LHSShuffle->
getOperand(1)) && !isa<UndefValue>(RHS))
1805 LHSShuffle =
nullptr;
1807 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
1808 RHSShuffle =
nullptr;
1809 if (!LHSShuffle && !RHSShuffle)
1810 return MadeChange ? &SVI :
nullptr;
1812 Value* LHSOp0 =
nullptr;
1813 Value* LHSOp1 =
nullptr;
1814 Value* RHSOp0 =
nullptr;
1815 unsigned LHSOp0Width = 0;
1816 unsigned RHSOp0Width = 0;
1823 RHSOp0 = RHSShuffle->getOperand(0);
1826 Value* newLHS = LHS;
1827 Value* newRHS = RHS;
1830 if (isa<UndefValue>(RHS)) {
1835 else if (LHSOp0Width == LHSWidth) {
1840 if (RHSShuffle && RHSOp0Width == LHSWidth) {
1844 if (LHSOp0 == RHSOp0) {
1849 if (newLHS == LHS && newRHS == RHS)
1850 return MadeChange ? &SVI :
nullptr;
1856 if (RHSShuffle && newRHS != RHS)
1857 RHSMask = RHSShuffle->getShuffleMask();
1859 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
1865 for (
unsigned i = 0; i < VWidth; ++i) {
1870 }
else if (Mask[i] < (
int)LHSWidth) {
1875 if (newLHS != LHS) {
1876 eltMask = LHSMask[Mask[i]];
1879 if (eltMask >= (
int)LHSOp0Width && isa<UndefValue>(LHSOp1))
1888 if (isa<UndefValue>(RHS))
1892 else if (newRHS != RHS) {
1893 eltMask = RHSMask[Mask[i]-LHSWidth];
1896 if (eltMask >= (
int)RHSOp0Width) {
1897 assert(isa<UndefValue>(RHSShuffle->getOperand(1))
1898 &&
"should have been check above");
1902 eltMask = Mask[i]-LHSWidth;
1910 if (eltMask >= 0 && newRHS !=
nullptr && newLHS != newRHS)
1911 eltMask += newLHSWidth;
1916 if (SplatElt >= 0 && SplatElt != eltMask)
1926 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
1928 for (
unsigned i = 0, e = newMask.
size(); i != e; ++i) {
1929 if (newMask[i] < 0) {
1942 bool isLHSID, isRHSID;
1944 if (isLHSID && VWidth == LHSOp0Width)
return replaceInstUsesWith(SVI, newLHS);
1945 if (isRHSID && VWidth == RHSOp0Width)
return replaceInstUsesWith(SVI, newRHS);
1947 return MadeChange ? &SVI :
nullptr;
static bool isShuffleEquivalentToSelect(ShuffleVectorInst &Shuf)
std::pair< Value *, Value * > ShuffleOps
We are building a shuffle to create V, which is a sequence of insertelement, extractelement pairs...
Instruction * visitInsertValueInst(InsertValueInst &IV)
Try to find redundant insertvalue instructions, like the following ones: %0 = insertvalue { i8...
bool isIdentityWithExtract() const
Return true if this shuffle extracts the first N elements of exactly one source vector.
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Same as InsertNewInstBefore, but also sets the debug loc.
A parsed version of the target data layout string in and methods for querying it. ...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
static Value * evaluateInDifferentElementOrder(Value *V, ArrayRef< int > Mask)
static Instruction * foldSelectShuffle(ShuffleVectorInst &Shuf, InstCombiner::BuilderTy &Builder, const DataLayout &DL)
Try to fold shuffles that are the equivalent of a vector select.
Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register...
This class represents lattice values for constants.
BinaryOps getOpcode() const
static Instruction * foldConstantInsEltIntoShuffle(InsertElementInst &InsElt)
insertelt (shufflevector X, CVec, Mask|insertelt X, C1, CIndex1), C, CIndex –> shufflevector X...
void push_back(const T &Elt)
static bool cheapToScalarize(Value *V, bool IsConstantExtractIndex)
Return true if the value is cheaper to scalarize than it is to leave as a vector operation.
static ConstantAggregateZero * get(Type *Ty)
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Instruction * foldIdentityExtractShuffle(ShuffleVectorInst &Shuf)
Try to combine 2 shuffles into 1 shuffle by concatenating a shuffle mask.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
LLVMContext & getContext() const
All values hold a context through their type.
ThreeOps_match< V1_t, V2_t, Mask_t, Instruction::ShuffleVector > m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m)
Matches ShuffleVectorInst.
static bool isShuffleExtractingFromLHS(ShuffleVectorInst &SVI, SmallVector< int, 16 > &Mask)
bool isVectorTy() const
True if this is an instance of VectorType.
static Instruction * hoistInsEltConst(InsertElementInst &InsElt2, InstCombiner::BuilderTy &Builder)
If we have an insertelement instruction feeding into another insertelement and the 2nd is inserting a...
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
static BinopElts getAlternateBinop(BinaryOperator *BO, const DataLayout &DL)
Binops may be transformed into binops with different opcodes and operands.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
Constant * getMask() const
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
unsigned getBitWidth() const
Return the number of bits in the Vector type.
void setBit(unsigned BitPosition)
Set a given bit to 1.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElement(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
The core instruction combiner logic.
void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs...
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if 'V & Mask' is known to be zero.
uint64_t getNumElements() const
BinopElts(BinaryOperator::BinaryOps Opc=(BinaryOperator::BinaryOps) 0, Value *V0=nullptr, Value *V1=nullptr)
This file implements a class to represent arbitrary precision integral constant values and operations...
void assign(size_type NumElts, const T &Elt)
static Instruction * foldInsSequenceIntoBroadcast(InsertElementInst &InsElt)
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Type * getType() const
All values are typed, get the type of this value.
static Instruction * foldShuffleWithInsert(ShuffleVectorInst &Shuf)
Try to replace a shuffle with an insertelement.
static Constant * getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant)
Some binary operators require special handling to avoid poison and undefined behavior.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
This instruction compares its operands according to the predicate given to the constructor.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
This class represents a no-op cast from one type to another.
static CmpInst * Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Instruction * visitExtractElementInst(ExtractElementInst &EI)
These are the ingredients in an alternate form binary operator as described below.
static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, SmallVectorImpl< Constant *> &Mask)
If V is a shuffle of values that ONLY returns elements from either LHS or RHS, return the shuffle mas...
bool isSelect() const
Return true if this shuffle chooses elements from its source vectors without lane crossings and all o...
VectorType * getType() const
Overload to return most specific vector type.
This class represents a truncation of integer types.
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...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
iterator find(const_arg_type_t< KeyT > Val)
Instruction * visitInsertElementInst(InsertElementInst &IE)
Value * SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
OneUse_match< T > m_OneUse(const T &SubPattern)
This instruction inserts a single (scalar) element into a VectorType value.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
size_t size() const
size - Get the array size.
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false)
Return the identity constant for a binary opcode.
self_iterator getIterator()
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Class to represent integer types.
static void replaceExtractElements(InsertElementInst *InsElt, ExtractElementInst *ExtElt, InstCombiner &IC)
If we have insertion into a vector that is wider than the vector that we are extracting from...
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static wasm::ValType getType(const TargetRegisterClass *RC)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
bool isExact() const
Determine whether the exact flag is set.
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.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Value * SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Iterator for intrusive lists based on ilist_node.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
This is the shared class of boolean and integer constants.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
BinaryOperator::BinaryOps Opcode
bool containsUndefElement() const
Return true if this is a vector constant that includes any undefined elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
static Instruction * foldSelectShuffleWith1Binop(ShuffleVectorInst &Shuf)
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
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.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void setOperand(unsigned i, Value *Val)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned getVectorNumElements() const
Class to represent vector types.
Class for arbitrary precision integers.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
iterator_range< user_iterator > users()
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
static BinaryOperator * CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, BinaryOperator *CopyBO, const Twine &Name="")
static IntegerType * getInt32Ty(LLVMContext &C)
StringRef getName() const
Return a constant reference to the value's name.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
Instruction * visitShuffleVectorInst(ShuffleVectorInst &SVI)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static bool canEvaluateShuffled(Value *V, ArrayRef< int > Mask, unsigned Depth=5)
Return true if we can evaluate the specified expression tree if the vector elements were shuffled in ...
static Instruction * foldBitcastExtElt(ExtractElementInst &Ext, InstCombiner::BuilderTy &Builder, bool IsBigEndian)
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
static Instruction * narrowVectorSelect(ShuffleVectorInst &Shuf, InstCombiner::BuilderTy &Builder)
Match a shuffle-select-shuffle pattern where the shuffles are widening and narrowing (concatenating w...
ArrayRef< unsigned > getIndices() const
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Value * SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
static Value * buildNew(Instruction *I, ArrayRef< Value *> NewOps)
Rebuild a new instruction just like 'I' but with the new operands given.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Type * getElementType() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
static bool isSplat(ArrayRef< Value *> VL)
static void recognizeIdentityMask(const SmallVectorImpl< int > &Mask, bool &isLHSID, bool &isRHSID)
static ShuffleOps collectShuffleElements(Value *V, SmallVectorImpl< Constant *> &Mask, Value *PermittedRHS, InstCombiner &IC)
VectorType * getType() const
Overload to return most specific vector type.
static Constant * get(ArrayRef< Constant *> V)
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
const BasicBlock * getParent() const
This instruction inserts a struct field of array element value into an aggregate value.