18 using namespace PatternMatch;
24 if (
Constant *
C = dyn_cast<Constant>(V)) {
25 if (isConstant)
return true;
28 Constant *Op0 =
C->getAggregateElement(0U);
30 if (
C->getAggregateElement(i) != Op0)
39 if (I->
getOpcode() == Instruction::InsertElement && isConstant &&
45 if (BO->hasOneUse() &&
49 if (
CmpInst *CI = dyn_cast<CmpInst>(I))
50 if (CI->hasOneUse() &&
69 return C->getAggregateElement(EltNo);
73 if (!isa<ConstantInt>(III->getOperand(2)))
75 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
80 return III->getOperand(1);
89 int InEl = SVI->getMaskValue(EltNo);
92 if (InEl < (
int)LHSWidth)
121 if (PHIUser == cast<Instruction>(&EI))
122 PHIUser = cast<Instruction>(*(++iu));
133 PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith(
141 if (PHIInVal == PHIUser) {
146 unsigned opId = (B0->
getOperand(0) == PN) ? 1 : 0;
147 Value *Op = InsertNewInstWith(
151 Value *newPHIUser = InsertNewInstWith(
160 if (pos && !isa<PHINode>(pos)) {
167 InsertNewInstWith(newEI, *InsertPos);
172 return ReplaceInstUsesWith(EI, scalarPHI);
180 return ReplaceInstUsesWith(EI,
C->getAggregateElement(0U));
185 unsigned IndexVal = IdxC->getZExtValue();
190 if (IndexVal >= VectorWidth)
197 APInt UndefElts(VectorWidth, 0);
198 APInt DemandedMask(VectorWidth, 0);
199 DemandedMask.
setBit(IndexVal);
201 DemandedMask, UndefElts)) {
208 return ReplaceInstUsesWith(EI, Elt);
214 if (
VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
215 if (VT->getNumElements() == VectorWidth)
233 if (
I->hasOneUse() &&
236 Builder->CreateExtractElement(BO->getOperand(0), EI.
getOperand(1),
239 Builder->CreateExtractElement(BO->getOperand(1), EI.
getOperand(1),
246 return ReplaceInstUsesWith(EI,
IE->getOperand(1));
249 if (isa<Constant>(
IE->getOperand(2)) && isa<Constant>(EI.
getOperand(1))) {
258 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
265 if (SrcIdx < (
int)LHSWidth)
266 Src = SVI->getOperand(0);
269 Src = SVI->getOperand(1);
276 }
else if (
CastInst *CI = dyn_cast<CastInst>(
I)) {
279 if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) {
280 Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
282 Worklist.AddValue(EE);
285 }
else if (
SelectInst *SI = dyn_cast<SelectInst>(
I)) {
286 if (SI->hasOneUse()) {
293 Value *TrueVal = SI->getTrueValue();
294 Value *FalseVal = SI->getFalseValue();
296 Value *Cond = SI->getCondition();
298 Cond = Builder->CreateExtractElement(Cond,
304 = Builder->CreateExtractElement(TrueVal,
309 = Builder->CreateExtractElement(FalseVal,
328 "Invalid CollectSingleShuffleElements");
331 if (isa<UndefValue>(V)) {
337 for (
unsigned i = 0; i != NumElts; ++i)
343 for (
unsigned i = 0; i != NumElts; ++i)
351 Value *VecOp = IEI->getOperand(0);
352 Value *ScalarOp = IEI->getOperand(1);
353 Value *IdxOp = IEI->getOperand(2);
355 if (!isa<ConstantInt>(IdxOp))
357 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
359 if (isa<UndefValue>(ScalarOp)) {
370 unsigned ExtractedIdx =
371 cast<ConstantInt>(EI->
getOperand(1))->getZExtValue();
380 Mask[InsertedIdx % NumElts] =
385 Mask[InsertedIdx % NumElts] =
387 ExtractedIdx+NumElts);
408 unsigned NumElts = cast<VectorType>(V->
getType())->getNumElements();
410 if (isa<UndefValue>(V)) {
415 if (isa<ConstantAggregateZero>(V)) {
422 Value *VecOp = IEI->getOperand(0);
423 Value *ScalarOp = IEI->getOperand(1);
424 Value *IdxOp = IEI->getOperand(2);
427 if (isa<ConstantInt>(EI->
getOperand(1)) && isa<ConstantInt>(IdxOp) &&
429 unsigned ExtractedIdx =
430 cast<ConstantInt>(EI->
getOperand(1))->getZExtValue();
431 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
438 Mask[InsertedIdx % NumElts] =
440 NumElts+ExtractedIdx);
448 Mask[InsertedIdx % NumElts] = Mask[ExtractedIdx];
451 for (
unsigned i = 0; i != NumElts; ++i) {
452 if (i != InsertedIdx)
469 for (
unsigned i = 0; i != NumElts; ++i)
480 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
481 ReplaceInstUsesWith(IE, VecOp);
486 if (isa<ConstantInt>(EI->
getOperand(1)) && isa<ConstantInt>(IdxOp) &&
489 unsigned ExtractedIdx =
490 cast<ConstantInt>(EI->
getOperand(1))->getZExtValue();
491 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
493 if (ExtractedIdx >= NumVectorElts)
494 return ReplaceInstUsesWith(IE, VecOp);
496 if (InsertedIdx >= NumVectorElts)
501 if (EI->
getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
502 return ReplaceInstUsesWith(IE, VecOp);
517 unsigned VWidth = cast<VectorType>(VecOp->
getType())->getNumElements();
518 APInt UndefElts(VWidth, 0);
520 if (
Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
522 return ReplaceInstUsesWith(IE, V);
532 unsigned Depth = 5) {
534 if (isa<Constant>(V))
539 if (!I)
return false;
545 if (Depth == 0)
return false;
548 case Instruction::Add:
549 case Instruction::FAdd:
550 case Instruction::Sub:
551 case Instruction::FSub:
552 case Instruction::Mul:
553 case Instruction::FMul:
554 case Instruction::UDiv:
555 case Instruction::SDiv:
556 case Instruction::FDiv:
557 case Instruction::URem:
558 case Instruction::SRem:
559 case Instruction::FRem:
560 case Instruction::Shl:
561 case Instruction::LShr:
562 case Instruction::AShr:
566 case Instruction::ICmp:
567 case Instruction::FCmp:
568 case Instruction::Trunc:
569 case Instruction::ZExt:
570 case Instruction::SExt:
571 case Instruction::FPToUI:
572 case Instruction::FPToSI:
573 case Instruction::UIToFP:
574 case Instruction::SIToFP:
575 case Instruction::FPTrunc:
576 case Instruction::FPExt:
577 case Instruction::GetElementPtr: {
584 case Instruction::InsertElement: {
586 if (!CI)
return false;
591 bool SeenOnce =
false;
592 for (
int i = 0, e = Mask.
size(); i != e; ++i) {
593 if (Mask[i] == ElementNumber) {
611 case Instruction::Add:
612 case Instruction::FAdd:
613 case Instruction::Sub:
614 case Instruction::FSub:
615 case Instruction::Mul:
616 case Instruction::FMul:
617 case Instruction::UDiv:
618 case Instruction::SDiv:
619 case Instruction::FDiv:
620 case Instruction::URem:
621 case Instruction::SRem:
622 case Instruction::FRem:
623 case Instruction::Shl:
624 case Instruction::LShr:
625 case Instruction::AShr:
630 assert(NewOps.
size() == 2 &&
"binary operator with #ops != 2");
633 NewOps[0], NewOps[1],
"", BO);
634 if (isa<OverflowingBinaryOperator>(BO)) {
638 if (isa<PossiblyExactOperator>(BO)) {
639 New->setIsExact(BO->
isExact());
643 case Instruction::ICmp:
644 assert(NewOps.
size() == 2 &&
"icmp with #ops != 2");
645 return new ICmpInst(I, cast<ICmpInst>(I)->getPredicate(),
646 NewOps[0], NewOps[1]);
647 case Instruction::FCmp:
648 assert(NewOps.
size() == 2 &&
"fcmp with #ops != 2");
649 return new FCmpInst(I, cast<FCmpInst>(I)->getPredicate(),
650 NewOps[0], NewOps[1]);
651 case Instruction::Trunc:
652 case Instruction::ZExt:
653 case Instruction::SExt:
654 case Instruction::FPToUI:
655 case Instruction::FPToSI:
656 case Instruction::UIToFP:
657 case Instruction::SIToFP:
658 case Instruction::FPTrunc:
659 case Instruction::FPExt: {
664 NewOps[0]->getType()->getVectorNumElements());
665 assert(NewOps.
size() == 1 &&
"cast with #ops != 1");
669 case Instruction::GetElementPtr: {
670 Value *Ptr = NewOps[0];
673 GEP->
setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds());
685 if (isa<UndefValue>(V)) {
689 if (isa<ConstantAggregateZero>(V)) {
694 if (
Constant *
C = dyn_cast<Constant>(V)) {
696 for (
int i = 0, e = Mask.
size(); i != e; ++i) {
700 MaskValues.
push_back(Builder->getInt32(Mask[i]));
708 case Instruction::Add:
709 case Instruction::FAdd:
710 case Instruction::Sub:
711 case Instruction::FSub:
712 case Instruction::Mul:
713 case Instruction::FMul:
714 case Instruction::UDiv:
715 case Instruction::SDiv:
716 case Instruction::FDiv:
717 case Instruction::URem:
718 case Instruction::SRem:
719 case Instruction::FRem:
720 case Instruction::Shl:
721 case Instruction::LShr:
722 case Instruction::AShr:
726 case Instruction::ICmp:
727 case Instruction::FCmp:
728 case Instruction::Trunc:
729 case Instruction::ZExt:
730 case Instruction::SExt:
731 case Instruction::FPToUI:
732 case Instruction::FPToSI:
733 case Instruction::UIToFP:
734 case Instruction::SIToFP:
735 case Instruction::FPTrunc:
736 case Instruction::FPExt:
738 case Instruction::GetElementPtr: {
751 case Instruction::InsertElement: {
752 int Element = cast<ConstantInt>(I->
getOperand(2))->getLimitedValue();
759 for (
int e = Mask.
size(); Index != e; ++Index) {
760 if (Mask[Index] == Element) {
772 Builder->getInt32(Index),
"",
I);
783 bool MadeChange =
false;
789 unsigned VWidth = cast<VectorType>(SVI.
getType())->getNumElements();
791 APInt UndefElts(VWidth, 0);
793 if (
Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
795 return ReplaceInstUsesWith(SVI, V);
801 unsigned LHSWidth = cast<VectorType>(LHS->
getType())->getNumElements();
805 if (LHS == RHS || isa<UndefValue>(LHS)) {
806 if (isa<UndefValue>(LHS) && LHS == RHS) {
808 Value *Result = (VWidth == LHSWidth)
810 return ReplaceInstUsesWith(SVI, Result);
815 for (
unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
821 if ((Mask[i] >= (
int)e && isa<UndefValue>(RHS)) ||
822 (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
826 Mask[i] = Mask[i] % e;
839 if (VWidth == LHSWidth) {
841 bool isLHSID =
true, isRHSID =
true;
843 for (
unsigned i = 0, e = Mask.
size(); i != e; ++i) {
844 if (Mask[i] < 0)
continue;
846 isLHSID &= (Mask[i] == (int)i);
849 isRHSID &= (Mask[i]-e == i);
853 if (isLHSID)
return ReplaceInstUsesWith(SVI, LHS);
854 if (isRHSID)
return ReplaceInstUsesWith(SVI, RHS);
858 Value *V = EvaluateInDifferentElementOrder(LHS, Mask);
859 return ReplaceInstUsesWith(SVI, V);
908 if (!isa<UndefValue>(LHSShuffle->
getOperand(1)) && !isa<UndefValue>(RHS))
911 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
913 if (!LHSShuffle && !RHSShuffle)
914 return MadeChange ? &SVI : 0;
916 Value* LHSOp0 = NULL;
917 Value* LHSOp1 = NULL;
918 Value* RHSOp0 = NULL;
919 unsigned LHSOp0Width = 0;
920 unsigned RHSOp0Width = 0;
924 LHSOp0Width = cast<VectorType>(LHSOp0->
getType())->getNumElements();
927 RHSOp0 = RHSShuffle->getOperand(0);
928 RHSOp0Width = cast<VectorType>(RHSOp0->
getType())->getNumElements();
934 if (isa<UndefValue>(RHS)) {
939 else if (LHSOp0Width == LHSWidth) {
944 if (RHSShuffle && RHSOp0Width == LHSWidth) {
948 if (LHSOp0 == RHSOp0) {
953 if (newLHS == LHS && newRHS == RHS)
954 return MadeChange ? &SVI : 0;
960 if (RHSShuffle && newRHS != RHS)
961 RHSMask = RHSShuffle->getShuffleMask();
963 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
969 for (
unsigned i = 0; i < VWidth; ++i) {
974 }
else if (Mask[i] < (
int)LHSWidth) {
980 eltMask = LHSMask[Mask[i]];
983 if (eltMask >= (
int)LHSOp0Width && isa<UndefValue>(LHSOp1))
992 if (isa<UndefValue>(RHS))
996 else if (newRHS != RHS) {
997 eltMask = RHSMask[Mask[i]-LHSWidth];
1000 if (eltMask >= (
int)RHSOp0Width) {
1001 assert(isa<UndefValue>(RHSShuffle->getOperand(1))
1002 &&
"should have been check above");
1006 eltMask = Mask[i]-LHSWidth;
1014 if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS)
1015 eltMask += newLHSWidth;
1020 if (SplatElt >= 0 && SplatElt != eltMask)
1030 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
1033 for (
unsigned i = 0, e = newMask.
size(); i != e; ++i) {
1034 if (newMask[i] < 0) {
1045 return MadeChange ? &SVI : 0;
void push_back(const T &Elt)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Abstract base class of comparison instructions.
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask)
void addIncoming(Value *V, BasicBlock *BB)
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
bool hasNUses(unsigned N) const
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=0)
void setBit(unsigned bitPosition)
Set a given bit to 1.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getNumOperands() const
static ConstantAggregateZero * get(Type *Ty)
class_match< Constant > m_Constant()
StringRef getName() const
bool match(Val *V, const Pattern &P)
Base class of casting instructions.
static Value * BuildNew(Instruction *I, ArrayRef< Value * > NewOps)
static Value * FindScalarElement(Value *V, unsigned EltNo)
#define llvm_unreachable(msg)
void setIsInBounds(bool b=true)
static Constant * get(ArrayRef< Constant * > V)
void assign(unsigned NumElts, const T &Elt)
static bool CanEvaluateShuffled(Value *V, ArrayRef< int > Mask, unsigned Depth=5)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
VectorType * getType() const
ArrayRef< T > slice(unsigned N) const
slice(n) - Chop off the first N elements of the array.
Represents a floating point comparison operator.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
This class represents a no-op cast from one type to another.
Instruction * visitExtractElementInst(ExtractElementInst &EI)
unsigned getNumElements() const
Return the number of elements in the Vector type.
size_t size() const
size - Get the array size.
unsigned getNumIncomingValues() const
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Construct any of the CastInst subclasses.
Instruction * visitInsertElementInst(InsertElementInst &IE)
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=0)
LLVM Basic Block Representation.
LLVM Constant Representation.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
BasicBlock * getIncomingBlock(unsigned i) const
Represent an integer comparison operator.
static bool CheapToScalarize(Value *V, bool isConstant)
Value * getOperand(unsigned i) const
static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, SmallVectorImpl< Constant * > &Mask)
Constant * getAggregateElement(unsigned Elt) const
static UndefValue * get(Type *T)
LLVMContext & getContext() const
All values hold a context through their type.
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
BinaryOps getOpcode() const
Class for constant integers.
Value * getIncomingValue(unsigned i) const
unsigned getVectorNumElements() const
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static GetElementPtrInst * Create(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=0)
bool isExact() const
isExact - Determine whether the exact flag is set.
void setOperand(unsigned i, Value *Val)
Class for arbitrary precision integers.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static IntegerType * getInt32Ty(LLVMContext &C)
Instruction * visitShuffleVectorInst(ShuffleVectorInst &SVI)
VectorType * getType() const
const Type * getScalarType() const
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=0)
static Value * CollectShuffleElements(Value *V, SmallVectorImpl< Constant * > &Mask, Value *&RHS)
LLVM Value Representation.
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
static VectorType * get(Type *ElementType, unsigned NumElements)
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)