diff options
author | Chris Lattner <sabre@nondot.org> | 2007-12-11 07:49:37 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-12-11 07:49:37 +0000 |
commit | b93bdf6e2e9f024f504f10c4f01cc54bf1a7ca80 (patch) | |
tree | 0a22bdc9b4944b7bd215ae812c8bca6fb3b676e2 /lib/VMCore | |
parent | 09d481bdf44c730f8eb60633dc636e48e4ccd6c3 (diff) | |
download | external_llvm-b93bdf6e2e9f024f504f10c4f01cc54bf1a7ca80.zip external_llvm-b93bdf6e2e9f024f504f10c4f01cc54bf1a7ca80.tar.gz external_llvm-b93bdf6e2e9f024f504f10c4f01cc54bf1a7ca80.tar.bz2 |
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 6660c8d..a99ae9d 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -396,11 +396,54 @@ Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, return 0; } +/// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef +/// return the specified element value. Otherwise return null. +static Constant *GetVectorElement(const Constant *C, unsigned EltNo) { + if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) + return const_cast<Constant*>(CV->getOperand(EltNo)); + + const Type *EltTy = cast<VectorType>(C->getType())->getElementType(); + if (isa<ConstantAggregateZero>(C)) + return Constant::getNullValue(EltTy); + if (isa<UndefValue>(C)) + return UndefValue::get(EltTy); + return 0; +} + Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1, const Constant *V2, const Constant *Mask) { - // TODO: - return 0; + // Undefined shuffle mask -> undefined value. + if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType()); + + unsigned NumElts = cast<VectorType>(V1->getType())->getNumElements(); + const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); + + // Loop over the shuffle mask, evaluating each element. + SmallVector<Constant*, 32> Result; + for (unsigned i = 0; i != NumElts; ++i) { + Constant *InElt = GetVectorElement(Mask, i); + if (InElt == 0) return 0; + + if (isa<UndefValue>(InElt)) + InElt = UndefValue::get(EltTy); + else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) { + unsigned Elt = CI->getZExtValue(); + if (Elt >= NumElts*2) + InElt = UndefValue::get(EltTy); + else if (Elt >= NumElts) + InElt = GetVectorElement(V2, Elt-NumElts); + else + InElt = GetVectorElement(V1, Elt); + if (InElt == 0) return 0; + } else { + // Unknown value. + return 0; + } + Result.push_back(InElt); + } + + return ConstantVector::get(&Result[0], Result.size()); } /// EvalVectorOp - Given two vector constants and a function pointer, apply the |