diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp index 0f8ffb9aebc9bf461d67511621f21b571c1965d5..050cb48d37927a345c99674d096f80d7f23b7d5a 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -19,14 +19,13 @@ using namespace llvm; /// is to leave as a vector operation. isConstant indicates whether we're /// extracting one known element. If false we're extracting a variable index. static bool CheapToScalarize(Value *V, bool isConstant) { - if (isa(V)) - return true; - if (ConstantVector *C = dyn_cast(V)) { + if (Constant *C = dyn_cast(V)) { if (isConstant) return true; - // If all elts are the same, we can extract. - Constant *Op0 = C->getOperand(0); - for (unsigned i = 1; i < C->getNumOperands(); ++i) - if (C->getOperand(i) != Op0) + + // If all elts are the same, we can extract it and use any of the values. + Constant *Op0 = C->getAggregateElement(0U); + for (unsigned i = 1, e = V->getType()->getVectorNumElements(); i != e; ++i) + if (C->getAggregateElement(i) != Op0) return false; return true; } @@ -54,41 +53,18 @@ static bool CheapToScalarize(Value *V, bool isConstant) { return false; } -/// getShuffleMask - Read and decode a shufflevector mask. -/// Turn undef elements into negative values. -static SmallVector getShuffleMask(const ShuffleVectorInst *SVI) { - unsigned NElts = SVI->getType()->getNumElements(); - if (isa(SVI->getOperand(2))) - return SmallVector(NElts, 0); - if (isa(SVI->getOperand(2))) - return SmallVector(NElts, -1); - - SmallVector Result; - const ConstantVector *CP = cast(SVI->getOperand(2)); - for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) - if (isa(*i)) - Result.push_back(-1); // undef - else - Result.push_back(cast(*i)->getZExtValue()); - return Result; -} - /// FindScalarElement - Given a vector and an element number, see if the scalar /// value is already around as a register, for example if it were inserted then /// extracted from the vector. static Value *FindScalarElement(Value *V, unsigned EltNo) { assert(V->getType()->isVectorTy() && "Not looking at a vector?"); - VectorType *PTy = cast(V->getType()); - unsigned Width = PTy->getNumElements(); + VectorType *VTy = cast(V->getType()); + unsigned Width = VTy->getNumElements(); if (EltNo >= Width) // Out of range access. - return UndefValue::get(PTy->getElementType()); + return UndefValue::get(VTy->getElementType()); - if (isa(V)) - return UndefValue::get(PTy->getElementType()); - if (isa(V)) - return Constant::getNullValue(PTy->getElementType()); - if (ConstantVector *CP = dyn_cast(V)) - return CP->getOperand(EltNo); + if (Constant *C = dyn_cast(V)) + return C->getAggregateElement(EltNo); if (InsertElementInst *III = dyn_cast(V)) { // If this is an insert to a variable element, we don't know what it is. @@ -107,11 +83,10 @@ static Value *FindScalarElement(Value *V, unsigned EltNo) { } if (ShuffleVectorInst *SVI = dyn_cast(V)) { - unsigned LHSWidth = - cast(SVI->getOperand(0)->getType())->getNumElements(); + unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements(); int InEl = SVI->getMaskValue(EltNo); if (InEl < 0) - return UndefValue::get(PTy->getElementType()); + return UndefValue::get(VTy->getElementType()); if (InEl < (int)LHSWidth) return FindScalarElement(SVI->getOperand(0), InEl); return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth); @@ -122,27 +97,11 @@ static Value *FindScalarElement(Value *V, unsigned EltNo) { } Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { - // If vector val is undef, replace extract with scalar undef. - if (isa(EI.getOperand(0))) - return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); - - // If vector val is constant 0, replace extract with scalar 0. - if (isa(EI.getOperand(0))) - return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); - - if (ConstantVector *C = dyn_cast(EI.getOperand(0))) { - // If vector val is constant with all elements the same, replace EI with - // that element. When the elements are not identical, we cannot replace yet - // (we do that below, but only when the index is constant). - Constant *op0 = C->getOperand(0); - for (unsigned i = 1; i != C->getNumOperands(); ++i) - if (C->getOperand(i) != op0) { - op0 = 0; - break; - } - if (op0) - return ReplaceInstUsesWith(EI, op0); - } + // If vector val is constant with all elements the same, replace EI with + // that element. We handle a known element # below. + if (Constant *C = dyn_cast(EI.getOperand(0))) + if (CheapToScalarize(C, false)) + return ReplaceInstUsesWith(EI, C->getAggregateElement(0U)); // If extracting a specified index from the vector, see if we can recursively // find a previously computed scalar that was inserted into the vector. @@ -176,8 +135,7 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { // the same number of elements, see if we can find the source element from // it. In this case, we will end up needing to bitcast the scalars. if (BitCastInst *BCI = dyn_cast(EI.getOperand(0))) { - if (VectorType *VT = - dyn_cast(BCI->getOperand(0)->getType())) + if (VectorType *VT = dyn_cast(BCI->getOperand(0)->getType())) if (VT->getNumElements() == VectorWidth) if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) return new BitCastInst(Elt, EI.getType()); @@ -216,7 +174,7 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { int SrcIdx = SVI->getMaskValue(Elt->getZExtValue()); Value *Src; unsigned LHSWidth = - cast(SVI->getOperand(0)->getType())->getNumElements(); + SVI->getOperand(0)->getType()->getVectorNumElements(); if (SrcIdx < 0) return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); @@ -452,7 +410,7 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { Value *LHS = SVI.getOperand(0); Value *RHS = SVI.getOperand(1); - SmallVector Mask = getShuffleMask(&SVI); + SmallVector Mask = SVI.getShuffleMask(); bool MadeChange = false; @@ -623,12 +581,11 @@ Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { SmallVector LHSMask; SmallVector RHSMask; - if (newLHS != LHS) { - LHSMask = getShuffleMask(LHSShuffle); - } - if (RHSShuffle && newRHS != RHS) { - RHSMask = getShuffleMask(RHSShuffle); - } + if (newLHS != LHS) + LHSMask = LHSShuffle->getShuffleMask(); + if (RHSShuffle && newRHS != RHS) + RHSMask = RHSShuffle->getShuffleMask(); + unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth; SmallVector newMask; bool isSplat = true; diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 08b1818d756991a27e1ad995569c9c41ec275867..c1d6da5465c31efa3ff0c74ae4e07e370f2a78e1 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -170,7 +170,7 @@ Constant *Constant::getAggregateElement(unsigned Elt) const { if (const UndefValue *UV = dyn_cast(this)) return UV->getElementValue(Elt); - if (const ConstantDataSequential *CDS = dyn_cast(this)) + if (const ConstantDataSequential *CDS =dyn_cast(this)) return CDS->getElementAsConstant(Elt); return 0; } @@ -201,8 +201,7 @@ void Constant::destroyConstantImpl() { } #endif assert(isa(V) && "References remain to Constant being destroyed"); - Constant *CV = cast(V); - CV->destroyConstant(); + cast(V)->destroyConstant(); // The constant should remove itself from our use list... assert((use_empty() || use_back() != V) && "Constant not removed!"); @@ -628,15 +627,13 @@ bool ConstantFP::isExactlyValue(const APFloat &V) const { /// getSequentialElement - If this CAZ has array or vector type, return a zero /// with the right element type. Constant *ConstantAggregateZero::getSequentialElement() const { - return Constant::getNullValue( - cast(getType())->getElementType()); + return Constant::getNullValue(getType()->getSequentialElementType()); } /// getStructElement - If this CAZ has struct type, return a zero with the /// right element type for the specified element. Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { - return Constant::getNullValue( - cast(getType())->getElementType(Elt)); + return Constant::getNullValue(getType()->getStructElementType(Elt)); } /// getElementValue - Return a zero of the right value for the specified GEP @@ -663,13 +660,13 @@ Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { /// getSequentialElement - If this undef has array or vector type, return an /// undef with the right element type. UndefValue *UndefValue::getSequentialElement() const { - return UndefValue::get(cast(getType())->getElementType()); + return UndefValue::get(getType()->getSequentialElementType()); } /// getStructElement - If this undef has struct type, return a zero with the /// right element type for the specified element. UndefValue *UndefValue::getStructElement(unsigned Elt) const { - return UndefValue::get(cast(getType())->getElementType(Elt)); + return UndefValue::get(getType()->getStructElementType(Elt)); } /// getElementValue - Return an undef of the right value for the specified GEP @@ -1020,8 +1017,8 @@ getWithOperands(ArrayRef Ops, Type *Ty) const { // isValueValidForType implementations bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { - unsigned NumBits = cast(Ty)->getBitWidth(); // assert okay - if (Ty == Type::getInt1Ty(Ty->getContext())) + unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay + if (Ty->isIntegerTy(1)) return Val == 0 || Val == 1; if (NumBits >= 64) return true; // always true, has to fit in largest type @@ -1030,8 +1027,8 @@ bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { } bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) { - unsigned NumBits = cast(Ty)->getBitWidth(); // assert okay - if (Ty == Type::getInt1Ty(Ty->getContext())) + unsigned NumBits = Ty->getIntegerBitWidth(); + if (Ty->isIntegerTy(1)) return Val == 0 || Val == 1 || Val == -1; if (NumBits >= 64) return true; // always true, has to fit in largest type @@ -1536,8 +1533,7 @@ Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) { "PtrToInt destination must be integer or integer vector"); assert(isa(C->getType()) == isa(DstTy)); if (isa(C->getType())) - assert(cast(C->getType())->getNumElements() == - cast(DstTy)->getNumElements() && + assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& "Invalid cast between a different number of vector elements"); return getFoldedCast(Instruction::PtrToInt, C, DstTy); } @@ -1549,8 +1545,7 @@ Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) { "IntToPtr destination must be a pointer or pointer vector"); assert(isa(C->getType()) == isa(DstTy)); if (isa(C->getType())) - assert(cast(C->getType())->getNumElements() == - cast(DstTy)->getNumElements() && + assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& "Invalid cast between a different number of vector elements"); return getFoldedCast(Instruction::IntToPtr, C, DstTy); } @@ -1731,7 +1726,7 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef Idxs, // Get the result type of the getelementptr! Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), Idxs); assert(Ty && "GEP indices invalid!"); - unsigned AS = cast(C->getType())->getAddressSpace(); + unsigned AS = C->getType()->getPointerAddressSpace(); Type *ReqTy = Ty->getPointerTo(AS); assert(C->getType()->isPointerTy() && @@ -1811,7 +1806,7 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) { const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec); LLVMContextImpl *pImpl = Val->getContext().pImpl; - Type *ReqTy = cast(Val->getType())->getElementType(); + Type *ReqTy = Val->getType()->getVectorElementType(); return pImpl->ExprConstants.getOrCreate(ReqTy, Key); } @@ -1819,8 +1814,8 @@ Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, Constant *Idx) { assert(Val->getType()->isVectorTy() && "Tried to create insertelement operation on non-vector type!"); - assert(Elt->getType() == cast(Val->getType())->getElementType() - && "Insertelement types must match!"); + assert(Elt->getType() == Val->getType()->getVectorElementType() && + "Insertelement types must match!"); assert(Idx->getType()->isIntegerTy(32) && "Insertelement index must be i32 type!"); @@ -1844,8 +1839,8 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) return FC; // Fold a few common cases. - unsigned NElts = cast(Mask->getType())->getNumElements(); - Type *EltTy = cast(V1->getType())->getElementType(); + unsigned NElts = Mask->getType()->getVectorNumElements(); + Type *EltTy = V1->getType()->getVectorElementType(); Type *ShufTy = VectorType::get(EltTy, NElts); // Look up the constant in the table first to ensure uniqueness @@ -2055,7 +2050,7 @@ bool ConstantDataSequential::isElementTypeCompatible(const Type *Ty) { unsigned ConstantDataSequential::getNumElements() const { if (ArrayType *AT = dyn_cast(getType())) return AT->getNumElements(); - return cast(getType())->getNumElements(); + return getType()->getVectorNumElements(); } @@ -2084,7 +2079,7 @@ static bool isAllZeros(StringRef Arr) { /// the correct element type. We take the bytes in as an StringRef because /// we *want* an underlying "char*" to avoid TBAA type punning violations. Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { - assert(isElementTypeCompatible(cast(Ty)->getElementType())); + assert(isElementTypeCompatible(Ty->getSequentialElementType())); // If the elements are all zero or there are no elements, return a CAZ, which // is more dense and canonical. if (isAllZeros(Elements)) @@ -2266,7 +2261,7 @@ uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { // The data is stored in host byte order, make sure to cast back to the right // type to load with the right endianness. - switch (cast(getElementType())->getBitWidth()) { + switch (getElementType()->getIntegerBitWidth()) { default: assert(0 && "Invalid bitwidth for CDS"); case 8: return *(uint8_t*)EltPtr; case 16: return *(uint16_t*)EltPtr;