From f724e34ddc28580da124a3e7bd9dd08f728247d1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 2 Mar 2008 05:28:33 +0000 Subject: [PATCH] Add a new ShuffleVectorInst::getMaskValue method. llvm-svn: 47813 --- llvm/include/llvm/Instructions.h | 5 +++++ llvm/lib/VMCore/Instructions.cpp | 30 ++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/Instructions.h b/llvm/include/llvm/Instructions.h index 3474429f7628..a7fe6a0ac745 100644 --- a/llvm/include/llvm/Instructions.h +++ b/llvm/include/llvm/Instructions.h @@ -1228,6 +1228,11 @@ public: Ops[i] = Val; } unsigned getNumOperands() const { return 3; } + + /// getMaskValue - Return the index from the shuffle mask for the specified + /// output result. This is either -1 if the element is undef or a number less + /// than 2*numelements. + int getMaskValue(unsigned i) const; // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const ShuffleVectorInst *) { return true; } diff --git a/llvm/lib/VMCore/Instructions.cpp b/llvm/lib/VMCore/Instructions.cpp index c0b3413da337..dfd3b830caac 100644 --- a/llvm/lib/VMCore/Instructions.cpp +++ b/llvm/lib/VMCore/Instructions.cpp @@ -1350,16 +1350,34 @@ ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, const Value *Mask) { - if (!isa(V1->getType())) return false; - if (V1->getType() != V2->getType()) return false; - if (!isa(Mask->getType()) || - cast(Mask->getType())->getElementType() != Type::Int32Ty || - cast(Mask->getType())->getNumElements() != - cast(V1->getType())->getNumElements()) + if (!isa(V1->getType()) || + V1->getType() != V2->getType()) + return false; + + const VectorType *MaskTy = dyn_cast(Mask->getType()); + if (!isa(Mask) || MaskTy == 0 || + MaskTy->getElementType() != Type::Int32Ty || + MaskTy->getNumElements() != + cast(V1->getType())->getNumElements()) return false; return true; } +/// getMaskValue - Return the index from the shuffle mask for the specified +/// output result. This is either -1 if the element is undef or a number less +/// than 2*numelements. +int ShuffleVectorInst::getMaskValue(unsigned i) const { + const Constant *Mask = cast(getOperand(2)); + if (isa(Mask)) return -1; + if (isa(Mask)) return 0; + const ConstantVector *MaskCV = cast(Mask); + assert(i < MaskCV->getNumOperands() && "Index out of range"); + + if (isa(MaskCV->getOperand(i))) + return -1; + return cast(MaskCV->getOperand(i))->getZExtValue(); +} + //===----------------------------------------------------------------------===// // BinaryOperator Class