llvm.org GIT mirror llvm / 83694a9
add some helper methods to ShuffleVectorInst and enhance its "isValidOperands" and "getMaskValue" methods to allow ConstantDataSequential. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148998 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 7 years ago
2 changed file(s) with 72 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
16691669 /// Transparently provide more efficient getOperand methods.
16701670 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
16711671
1672 Constant *getMask() const {
1673 return reinterpret_cast(getOperand(2));
1674 }
1675
16721676 /// getMaskValue - Return the index from the shuffle mask for the specified
16731677 /// output result. This is either -1 if the element is undef or a number less
16741678 /// than 2*numelements.
16751679 int getMaskValue(unsigned i) const;
1680
1681 /// getShuffleMask - Return the full mask for this instruction, where each
1682 /// element is the element number and undef's are returned as -1.
1683 void getShuffleMask(SmallVectorImpl &Mask) const;
1684
1685 SmallVector getShuffleMask() const {
1686 SmallVector Mask;
1687 getShuffleMask(Mask);
1688 return Mask;
1689 }
1690
16761691
16771692 // Methods for support type inquiry through isa, cast, and dyn_cast:
16781693 static inline bool classof(const ShuffleVectorInst *) { return true; }
15751575
15761576 bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
15771577 const Value *Mask) {
1578 // V1 and V2 must be vectors of the same type.
15781579 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
15791580 return false;
15801581
1582 // Mask must be vector of i32.
15811583 VectorType *MaskTy = dyn_cast(Mask->getType());
15821584 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
15831585 return false;
15841586
15851587 // Check to see if Mask is valid.
1588 if (isa(Mask) || isa(Mask))
1589 return true;
1590
15861591 if (const ConstantVector *MV = dyn_cast(Mask)) {
1587 VectorType *VTy = cast(V1->getType());
1592 unsigned V1Size = cast(V1->getType())->getNumElements();
15881593 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1589 if (ConstantInt* CI = dyn_cast(MV->getOperand(i))) {
1590 if (CI->uge(VTy->getNumElements()*2))
1594 if (ConstantInt *CI = dyn_cast(MV->getOperand(i))) {
1595 if (CI->uge(V1Size*2))
15911596 return false;
15921597 } else if (!isa(MV->getOperand(i))) {
15931598 return false;
15941599 }
15951600 }
1596 } else if (!isa(Mask) && !isa(Mask)) {
1597 // The bitcode reader can create a place holder for a forward reference
1598 // used as the shuffle mask. When this occurs, the shuffle mask will
1599 // fall into this case and fail. To avoid this error, do this bit of
1600 // ugliness to allow such a mask pass.
1601 if (const ConstantExpr* CE = dyn_cast(Mask)) {
1602 if (CE->getOpcode() == Instruction::UserOp1)
1603 return true;
1604 }
1605 return false;
1606 }
1607 return true;
1601 return true;
1602 }
1603
1604 if (const ConstantDataSequential *CDS =
1605 dyn_cast(Mask)) {
1606 unsigned V1Size = cast(V1->getType())->getNumElements();
1607 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1608 if (CDS->getElementAsInteger(i) >= V1Size*2)
1609 return false;
1610 return true;
1611 }
1612
1613 // The bitcode reader can create a place holder for a forward reference
1614 // used as the shuffle mask. When this occurs, the shuffle mask will
1615 // fall into this case and fail. To avoid this error, do this bit of
1616 // ugliness to allow such a mask pass.
1617 if (const ConstantExpr *CE = dyn_cast(Mask))
1618 if (CE->getOpcode() == Instruction::UserOp1)
1619 return true;
1620
1621 return false;
16081622 }
16091623
16101624 /// getMaskValue - Return the index from the shuffle mask for the specified
16111625 /// output result. This is either -1 if the element is undef or a number less
16121626 /// than 2*numelements.
16131627 int ShuffleVectorInst::getMaskValue(unsigned i) const {
1614 const Constant *Mask = cast(getOperand(2));
1615 if (isa(Mask)) return -1;
1616 if (isa(Mask)) return 0;
1617 const ConstantVector *MaskCV = cast(Mask);
1618 assert(i < MaskCV->getNumOperands() && "Index out of range");
1619
1620 if (isa(MaskCV->getOperand(i)))
1628 assert(i < getType()->getNumElements() && "Index out of range");
1629 if (ConstantDataSequential *CDS =dyn_cast(getMask()))
1630 return CDS->getElementAsInteger(i);
1631 Constant *C = getMask()->getAggregateElement(i);
1632 if (isa(C))
16211633 return -1;
1622 return cast(MaskCV->getOperand(i))->getZExtValue();
1623 }
1634 return cast(C)->getZExtValue();
1635 }
1636
1637 /// getShuffleMask - Return the full mask for this instruction, where each
1638 /// element is the element number and undef's are returned as -1.
1639 void ShuffleVectorInst::getShuffleMask(SmallVectorImpl &Result) const {
1640 unsigned NumElts = getType()->getNumElements();
1641
1642 if (ConstantDataSequential *CDS=dyn_cast(getMask())) {
1643 for (unsigned i = 0; i != NumElts; ++i)
1644 Result.push_back(CDS->getElementAsInteger(i));
1645 return;
1646 }
1647 Constant *Mask = getMask();
1648 for (unsigned i = 0; i != NumElts; ++i) {
1649 Constant *C = Mask->getAggregateElement(i);
1650 Result.push_back(isa(C) ? -1 :
1651 cast(Mask)->getZExtValue());
1652 }
1653 }
1654
16241655
16251656 //===----------------------------------------------------------------------===//
16261657 // InsertValueInst Class
34563487 }
34573488
34583489 InsertElementInst *InsertElementInst::clone_impl() const {
3459 return InsertElementInst::Create(getOperand(0),
3460 getOperand(1),
3461 getOperand(2));
3490 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
34623491 }
34633492
34643493 ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3465 return new ShuffleVectorInst(getOperand(0),
3466 getOperand(1),
3467 getOperand(2));
3494 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
34683495 }
34693496
34703497 PHINode *PHINode::clone_impl() const {