diff options
author | Chris Lattner <sabre@nondot.org> | 2012-01-27 03:08:05 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2012-01-27 03:08:05 +0000 |
commit | a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7a (patch) | |
tree | 86baf632ff9cede6ae0504249b7075282f9fa227 /lib/VMCore | |
parent | 2b343702aac08ddff4191890a8745616022c831f (diff) | |
download | external_llvm-a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7a.zip external_llvm-a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7a.tar.gz external_llvm-a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7a.tar.bz2 |
continue making the world safe for ConstantDataVector. At this point,
we should (theoretically optimize and codegen ConstantDataVector as well
as ConstantVector.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149116 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 66 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 12 | ||||
-rw-r--r-- | lib/VMCore/Instructions.cpp | 6 |
3 files changed, 46 insertions, 38 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index f1b97df..36bd4ab 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -547,18 +547,17 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, // If the cast operand is a constant vector, perform the cast by // operating on each element. In the cast of bitcasts, the element // count may be mismatched; don't attempt to handle that here. - if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) - if (DestTy->isVectorTy() && - cast<VectorType>(DestTy)->getNumElements() == - CV->getType()->getNumElements()) { - std::vector<Constant*> res; - VectorType *DestVecTy = cast<VectorType>(DestTy); - Type *DstEltTy = DestVecTy->getElementType(); - for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) - res.push_back(ConstantExpr::getCast(opc, - CV->getOperand(i), DstEltTy)); - return ConstantVector::get(res); - } + if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) && + DestTy->isVectorTy() && + DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) { + SmallVector<Constant*, 16> res; + VectorType *DestVecTy = cast<VectorType>(DestTy); + Type *DstEltTy = DestVecTy->getElementType(); + for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) + res.push_back(ConstantExpr::getCast(opc, + V->getAggregateElement(i), DstEltTy)); + return ConstantVector::get(res); + } // We actually have to do a cast now. Perform the cast according to the // opcode specified. @@ -694,7 +693,7 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, if (Cond->isNullValue()) return V2; if (Cond->isAllOnesValue()) return V1; - // FIXME: CDV Condition. + // FIXME: Remove ConstantVector // If the condition is a vector constant, fold the result elementwise. if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { SmallVector<Constant*, 16> Result; @@ -711,6 +710,20 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, if (Result.size() == V1->getType()->getVectorNumElements()) return ConstantVector::get(Result); } + if (ConstantDataVector *CondV = dyn_cast<ConstantDataVector>(Cond)) { + SmallVector<Constant*, 16> Result; + for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){ + uint64_t Cond = CondV->getElementAsInteger(i); + + Constant *Res = (Cond ? V2 : V1)->getAggregateElement(i); + if (Res == 0) break; + Result.push_back(Res); + } + + // If we were able to build the vector, return it. + if (Result.size() == V1->getType()->getVectorNumElements()) + return ConstantVector::get(Result); + } if (isa<UndefValue>(Cond)) { @@ -738,22 +751,19 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx) { if (isa<UndefValue>(Val)) // ee(undef, x) -> undef - return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); + return UndefValue::get(Val->getType()->getVectorElementType()); if (Val->isNullValue()) // ee(zero, x) -> zero - return Constant::getNullValue( - cast<VectorType>(Val->getType())->getElementType()); - - if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { - if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { - uint64_t Index = CIdx->getZExtValue(); - if (Index >= CVal->getNumOperands()) - // ee({w,x,y,z}, wrong_value) -> undef - return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); - return CVal->getOperand(CIdx->getZExtValue()); - } else if (isa<UndefValue>(Idx)) { - // ee({w,x,y,z}, undef) -> undef - return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); - } + return Constant::getNullValue(Val->getType()->getVectorElementType()); + // ee({w,x,y,z}, undef) -> undef + if (isa<UndefValue>(Idx)) + return UndefValue::get(Val->getType()->getVectorElementType()); + + if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { + uint64_t Index = CIdx->getZExtValue(); + // ee({w,x,y,z}, wrong_value) -> undef + if (Index >= Val->getType()->getVectorNumElements()) + return UndefValue::get(Val->getType()->getVectorElementType()); + return Val->getAggregateElement(Index); } return 0; } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index d12acb5..282bfbc 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -441,12 +441,12 @@ Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { return C; } -ConstantInt* ConstantInt::get(IntegerType* Ty, uint64_t V, +ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) { return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); } -ConstantInt* ConstantInt::getSigned(IntegerType* Ty, int64_t V) { +ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) { return get(Ty, V, true); } @@ -454,7 +454,7 @@ Constant *ConstantInt::getSigned(Type *Ty, int64_t V) { return get(Ty, V, true); } -Constant *ConstantInt::get(Type* Ty, const APInt& V) { +Constant *ConstantInt::get(Type *Ty, const APInt& V) { ConstantInt *C = get(Ty->getContext(), V); assert(C->getType() == Ty->getScalarType() && "ConstantInt type doesn't match the type implied by its value!"); @@ -466,7 +466,7 @@ Constant *ConstantInt::get(Type* Ty, const APInt& V) { return C; } -ConstantInt* ConstantInt::get(IntegerType* Ty, StringRef Str, +ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) { return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix)); } @@ -496,7 +496,7 @@ void ConstantFP::anchor() { } /// get() - This returns a constant fp for the specified value in the /// specified type. This should only be used for simple constant values like /// 2.0/1.0 etc, that are known-valid both as double and as the target format. -Constant *ConstantFP::get(Type* Ty, double V) { +Constant *ConstantFP::get(Type *Ty, double V) { LLVMContext &Context = Ty->getContext(); APFloat FV(V); @@ -513,7 +513,7 @@ Constant *ConstantFP::get(Type* Ty, double V) { } -Constant *ConstantFP::get(Type* Ty, StringRef Str) { +Constant *ConstantFP::get(Type *Ty, StringRef Str) { LLVMContext &Context = Ty->getContext(); APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str); diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index a0fb91a..301791d 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -1932,10 +1932,8 @@ BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, // isConstantAllOnes - Helper function for several functions below static inline bool isConstantAllOnes(const Value *V) { - if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) - return CI->isAllOnesValue(); - if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) - return CV->isAllOnesValue(); + if (const Constant *C = dyn_cast<Constant>(V)) + return C->isAllOnesValue(); return false; } |