diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -1050,7 +1050,8 @@ llvm::Type *OrigTy = constant->getType(); if (const auto STy = dyn_cast(OrigTy)) return constStructWithPadding(CGM, isPattern, STy, constant); - if (auto *STy = dyn_cast(OrigTy)) { + if (auto *STy = dyn_cast_or_null( + llvm::CompositeType::get(OrigTy, false))) { llvm::SmallVector Values; unsigned Size = STy->getNumElements(); if (!Size) diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -321,7 +321,8 @@ replace(Elems, Index, Index + 1, llvm::map_range(llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) { return CA->getOperand(Op); })); - if (auto *Seq = dyn_cast(CA->getType())) { + if (auto *Seq = dyn_cast_or_null( + llvm::CompositeType::get(CA->getType(), false))) { // Array or vector. CharUnits ElemSize = getSize(Seq->getElementType()); replace( diff --git a/clang/unittests/CodeGen/CodeGenExternalTest.cpp b/clang/unittests/CodeGen/CodeGenExternalTest.cpp --- a/clang/unittests/CodeGen/CodeGenExternalTest.cpp +++ b/clang/unittests/CodeGen/CodeGenExternalTest.cpp @@ -199,7 +199,7 @@ dbgs() << "\n"; } - llvm::CompositeType* structTy = dyn_cast(llvmTy); + llvm::CompositeType *structTy = CompositeType::get(llvmTy, false); ASSERT_TRUE(structTy != NULL); // Check getLLVMFieldNumber diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -634,7 +634,7 @@ /// Specialize the getType() method to always return a SequentialType, which /// reduces the amount of casting needed in parts of the compiler. inline SequentialType *getType() const { - return cast(Value::getType()); + return cast(CompositeType::get(Value::getType())); } /// Return the element type of the array/vector. diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h --- a/llvm/include/llvm/IR/DerivedTypes.h +++ b/llvm/include/llvm/IR/DerivedTypes.h @@ -196,9 +196,29 @@ }; /// Common super class of ArrayType, StructType and VectorType. -class CompositeType : public Type { +class CompositeType { +public: + enum class TypeID { + StructTyID, ///< Structures + ArrayTyID, ///< Arrays + VectorTyID ///< SIMD 'packed' format, or other vector type + }; + + TypeID fromTypeID(Type::TypeID ID) { + switch (ID) { + case Type::ArrayTyID: + return TypeID::ArrayTyID; + case Type::StructTyID: + return TypeID::StructTyID; + case Type::VectorTyID: + return TypeID::VectorTyID; + default: + llvm_unreachable("Invalid TypeID for conversion from base TypeID"); + } + } + protected: - explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {} + explicit CompositeType(Type::TypeID tid) : ID(fromTypeID(tid)) {} public: /// Given an index value into the type, return the type of the element. @@ -207,12 +227,29 @@ bool indexValid(const Value *V) const; bool indexValid(unsigned Idx) const; - /// Methods for support type inquiry through isa, cast, and dyn_cast. - static bool classof(const Type *T) { - return T->getTypeID() == ArrayTyID || - T->getTypeID() == StructTyID || - T->getTypeID() == VectorTyID; + /// Convert from a CompositeType to a Type + Type *from(); + + TypeID getTypeID() const { return ID; } + + /// Returns true if the given type is a Composite Type + static bool is(const Type *TY) { + return TY->getTypeID() == Type::ArrayTyID || + TY->getTypeID() == Type::VectorTyID || + TY->getTypeID() == Type::StructTyID; } + + /// Convert a Type pointer to a Composite type pointer. This behaves like + /// cast if infallible = true, and behaves like dyn_cast if + /// infallible = false. + /// + /// Alternatively you call CompositeType::get(TY, CompositeType::is(TY)) + /// to get the dyn_cast behavior and CompositeType::get(TY) to get the + /// cast behavior. + static CompositeType *get(Type *TY, bool infallible = true); + +private: + TypeID ID; }; /// Class to represent struct types. There are two different kinds of struct @@ -235,8 +272,8 @@ /// elements as defined by DataLayout (which is required to match what the code /// generator for a target expects). /// -class StructType : public CompositeType { - StructType(LLVMContext &C) : CompositeType(C, StructTyID) {} +class StructType : public Type, public CompositeType { + StructType(LLVMContext &C) : Type(C, StructTyID), CompositeType(StructTyID) {} enum { /// This is the contents of the SubClassData field. @@ -355,6 +392,10 @@ static bool classof(const Type *T) { return T->getTypeID() == StructTyID; } + + static bool classof(const CompositeType *T) { + return T->getTypeID() == CompositeType::TypeID::StructTyID; + } }; StringRef Type::getStructName() const { @@ -380,12 +421,8 @@ uint64_t NumElements; protected: - SequentialType(TypeID TID, Type *ElType, uint64_t NumElements) - : CompositeType(ElType->getContext(), TID), ContainedType(ElType), - NumElements(NumElements) { - ContainedTys = &ContainedType; - NumContainedTys = 1; - } + SequentialType(Type::TypeID TID, Type *ElType, uint64_t NumElements) + : CompositeType(TID), ContainedType(ElType), NumElements(NumElements) {} public: SequentialType(const SequentialType &) = delete; @@ -397,13 +434,14 @@ Type *getElementType() const { return ContainedType; } /// Methods for support type inquiry through isa, cast, and dyn_cast. - static bool classof(const Type *T) { - return T->getTypeID() == ArrayTyID || T->getTypeID() == VectorTyID; + static bool classof(const CompositeType *T) { + return T->getTypeID() == TypeID::ArrayTyID || + T->getTypeID() == TypeID::VectorTyID; } }; /// Class to represent array types. -class ArrayType : public SequentialType { +class ArrayType : public Type, public SequentialType { ArrayType(Type *ElType, uint64_t NumEl); public: @@ -420,6 +458,10 @@ static bool classof(const Type *T) { return T->getTypeID() == ArrayTyID; } + + static bool classof(const CompositeType *T) { + return T->getTypeID() == CompositeType::TypeID::ArrayTyID; + } }; uint64_t Type::getArrayNumElements() const { @@ -427,7 +469,7 @@ } /// Class to represent vector types. -class VectorType : public SequentialType { +class VectorType : public Type, public SequentialType { /// A fully specified VectorType is of the form . 'n' is the /// minimum number of elements of type Ty contained within the vector, and /// 'vscale x' indicates that the total element count is an integer multiple @@ -559,6 +601,10 @@ static bool classof(const Type *T) { return T->getTypeID() == VectorTyID; } + + static bool classof(const CompositeType *T) { + return T->getTypeID() == CompositeType::TypeID::VectorTyID; + } }; unsigned Type::getVectorNumElements() const { diff --git a/llvm/include/llvm/IR/GetElementPtrTypeIterator.h b/llvm/include/llvm/IR/GetElementPtrTypeIterator.h --- a/llvm/include/llvm/IR/GetElementPtrTypeIterator.h +++ b/llvm/include/llvm/IR/GetElementPtrTypeIterator.h @@ -75,7 +75,7 @@ generic_gep_type_iterator& operator++() { // Preincrement Type *Ty = getIndexedType(); - if (auto *STy = dyn_cast(Ty)) { + if (auto *STy = dyn_cast(CompositeType::get(Ty))) { CurTy = STy->getElementType(); NumElements = STy->getNumElements(); } else diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1145,7 +1145,7 @@ GEP1->getSourceElementType(), IntermediateIndices); StructType *LastIndexedStruct = dyn_cast(Ty); - if (isa(Ty)) { + if (isa_and_nonnull(CompositeType::get(Ty, false))) { // We know that: // - both GEPs begin indexing from the exact same pointer; // - the last indices in both GEPs are constants, indexing into a sequential @@ -1157,8 +1157,8 @@ // GEP1 and GEP2 we cannot guarantee that the last indexed arrays don't // partially overlap. We also need to check that the loaded size matches // the element size, otherwise we could still have overlap. - const uint64_t ElementSize = - DL.getTypeStoreSize(cast(Ty)->getElementType()); + const uint64_t ElementSize = DL.getTypeStoreSize( + cast(CompositeType::get(Ty))->getElementType()); if (V1Size != ElementSize || V2Size != ElementSize) return MayAlias; diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -940,7 +940,7 @@ // Only handle pointers to sized types, not pointers to functions. if (!Ty->isSized()) return nullptr; - } else if (auto *ATy = dyn_cast(Ty)) { + } else if (auto *ATy = dyn_cast(CompositeType::get(Ty))) { Ty = ATy->getElementType(); } else { // We've reached some non-indexable type. diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -3519,7 +3519,7 @@ CurTy = STy->getTypeAtIndex(Index); } else { // Update CurTy to its element type. - CurTy = cast(CurTy)->getElementType(); + CurTy = cast(CompositeType::get(CurTy))->getElementType(); // For an array, add the element offset, explicitly scaled. const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy); // Getelementptr indices are signed. diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -2471,7 +2471,8 @@ if (Record.empty()) return error("Invalid record"); - Type *EltTy = cast(CurTy)->getElementType(); + Type *EltTy = + cast(CompositeType::get(CurTy))->getElementType(); if (EltTy->isIntegerTy(8)) { SmallVector Elts(Record.begin(), Record.end()); if (isa(CurTy)) diff --git a/llvm/lib/CodeGen/Analysis.cpp b/llvm/lib/CodeGen/Analysis.cpp --- a/llvm/lib/CodeGen/Analysis.cpp +++ b/llvm/lib/CodeGen/Analysis.cpp @@ -437,7 +437,7 @@ ++Path.back(); Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back()); while (DeeperType->isAggregateType()) { - CompositeType *CT = cast(DeeperType); + CompositeType *CT = CompositeType::get(DeeperType); if (!indexReallyValid(CT, 0)) return true; @@ -467,10 +467,10 @@ // (i.e. node with no valid sub-type at any index, so {} does count as a leaf // despite nominally being an aggregate). while (Next->isAggregateType() && - indexReallyValid(cast(Next), 0)) { - SubTypes.push_back(cast(Next)); + indexReallyValid(CompositeType::get(Next), 0)) { + SubTypes.push_back(CompositeType::get(Next)); Path.push_back(0); - Next = cast(Next)->getTypeAtIndex(0U); + Next = CompositeType::get(Next)->getTypeAtIndex(0U); } // If there's no Path now, Next was originally scalar already (or empty diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2443,7 +2443,7 @@ // See if we can aggregate this into a .fill, if so, emit it as such. int Value = isRepeatedByteSequence(CDS, DL); if (Value != -1) { - uint64_t Bytes = DL.getTypeAllocSize(CDS->getType()); + uint64_t Bytes = DL.getTypeAllocSize(CDS->getType()->from()); // Don't emit a 1-byte object as a .fill. if (Bytes > 1) return AP.OutStreamer->emitFill(Bytes, Value); @@ -2469,7 +2469,7 @@ emitGlobalConstantFP(CDS->getElementAsAPFloat(I), ET, AP); } - unsigned Size = DL.getTypeAllocSize(CDS->getType()); + unsigned Size = DL.getTypeAllocSize(CDS->getType()->from()); unsigned EmittedSize = DL.getTypeAllocSize(CDS->getType()->getElementType()) * CDS->getNumElements(); assert(EmittedSize <= Size && "Size cannot be less than EmittedSize!"); diff --git a/llvm/lib/FuzzMutate/Operations.cpp b/llvm/lib/FuzzMutate/Operations.cpp --- a/llvm/lib/FuzzMutate/Operations.cpp +++ b/llvm/lib/FuzzMutate/Operations.cpp @@ -244,7 +244,7 @@ static SourcePred validInsertValueIndex() { auto Pred = [](ArrayRef Cur, const Value *V) { - auto *CTy = cast(Cur[0]->getType()); + auto *CTy = CompositeType::get(Cur[0]->getType()); if (auto *CI = dyn_cast(V)) if (CI->getBitWidth() == 32 && CTy->getTypeAtIndex(CI->getZExtValue()) == Cur[1]->getType()) @@ -254,8 +254,8 @@ auto Make = [](ArrayRef Cur, ArrayRef Ts) { std::vector Result; auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext()); - auto *CTy = cast(Cur[0]->getType()); - for (int I = 0, E = getAggregateNumElements(CTy); I < E; ++I) + auto *CTy = CompositeType::get(Cur[0]->getType()); + for (int I = 0, E = getAggregateNumElements(Cur[0]->getType()); I < E; ++I) if (CTy->getTypeAtIndex(I) == Cur[1]->getType()) Result.push_back(ConstantInt::get(Int32Ty, I)); return Result; diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -124,8 +124,8 @@ if (STy->getNumElements() == 0) break; ElTy = STy->getElementType(0); IdxList.push_back(Zero); - } else if (SequentialType *STy = - dyn_cast(ElTy)) { + } else if (SequentialType *STy = dyn_cast_or_null( + CompositeType::get(ElTy, false))) { ElTy = STy->getElementType(); IdxList.push_back(Zero); } else { @@ -936,7 +936,8 @@ if (StructType *ST = dyn_cast(Agg->getType())) NumElts = ST->getNumElements(); else - NumElts = cast(Agg->getType())->getNumElements(); + NumElts = cast(CompositeType::get(Agg->getType())) + ->getNumElements(); SmallVector Result; for (unsigned i = 0; i != NumElts; ++i) { @@ -2388,7 +2389,7 @@ bool Unknown = !isa(Idxs[0]) && !isa(Idxs[0]); for (unsigned i = 1, e = Idxs.size(); i != e; - Prev = Ty, Ty = cast(Ty)->getTypeAtIndex(Idxs[i]), ++i) { + Prev = Ty, Ty = CompositeType::get(Ty)->getTypeAtIndex(Idxs[i]), ++i) { if (!isa(Idxs[i]) && !isa(Idxs[i])) { // We don't know if it's in range or not. Unknown = true; @@ -2407,7 +2408,7 @@ // The verify makes sure that GEPs into a struct are in range. continue; } - auto *STy = cast(Ty); + auto *STy = cast(CompositeType::get(Ty)); if (isa(STy)) { // There can be awkward padding in after a non-power of two vector. Unknown = true; @@ -2448,7 +2449,7 @@ // dimension. NewIdxs.resize(Idxs.size()); // Determine the number of elements in our sequential type. - uint64_t NumElements = STy->getArrayNumElements(); + uint64_t NumElements = STy->getNumElements(); // Expand the current index or the previous index to a vector from a scalar // if necessary. diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -931,13 +931,13 @@ } Constant *ConstantAggregateZero::getElementValue(Constant *C) const { - if (isa(getType())) + if (isa_and_nonnull(CompositeType::get(getType(), false))) return getSequentialElement(); return getStructElement(cast(C)->getZExtValue()); } Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { - if (isa(getType())) + if (isa_and_nonnull(CompositeType::get(getType(), false))) return getSequentialElement(); return getStructElement(Idx); } @@ -964,20 +964,20 @@ } UndefValue *UndefValue::getElementValue(Constant *C) const { - if (isa(getType())) + if (isa_and_nonnull(CompositeType::get(getType(), false))) return getSequentialElement(); return getStructElement(cast(C)->getZExtValue()); } UndefValue *UndefValue::getElementValue(unsigned Idx) const { - if (isa(getType())) + if (isa_and_nonnull(CompositeType::get(getType(), false))) return getSequentialElement(); return getStructElement(Idx); } unsigned UndefValue::getNumElements() const { Type *Ty = getType(); - if (auto *ST = dyn_cast(Ty)) + if (auto *ST = dyn_cast(CompositeType::get(Ty))) return ST->getNumElements(); return Ty->getStructNumElements(); } @@ -1049,7 +1049,8 @@ ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT, ArrayRef V) - : Constant(T, VT, OperandTraits::op_end(this) - V.size(), + : Constant(T->from(), VT, + OperandTraits::op_end(this) - V.size(), V.size()) { llvm::copy(V, op_begin()); @@ -2528,9 +2529,7 @@ } unsigned ConstantDataSequential::getNumElements() const { - if (ArrayType *AT = dyn_cast(getType())) - return AT->getNumElements(); - return getType()->getVectorNumElements(); + return getType()->getNumElements(); } @@ -2577,7 +2576,7 @@ ConstantDataSequential **Entry = &Slot.second; for (ConstantDataSequential *Node = *Entry; Node; Entry = &Node->Next, Node = *Entry) - if (Node->getType() == Ty) + if (Node->getType()->from() == Ty) return Node; // Okay, we didn't get a hit. Create a node of the right class, link it in, @@ -2591,8 +2590,8 @@ void ConstantDataSequential::destroyConstantImpl() { // Remove the constant from the StringMap. - StringMap &CDSConstants = - getType()->getContext().pImpl->CDSConstants; + StringMap &CDSConstants = + getType()->from()->getContext().pImpl->CDSConstants; StringMap::iterator Slot = CDSConstants.find(getRawDataValues()); diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -753,7 +753,7 @@ auto *Ty = unwrap(WrappedTy); if (auto *PTy = dyn_cast(Ty)) return wrap(PTy->getElementType()); - return wrap(cast(Ty)->getElementType()); + return wrap(cast(CompositeType::get(Ty))->getElementType()); } unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) { diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -1634,8 +1634,9 @@ unsigned CurIdx = 1; for (; CurIdx != IdxList.size(); ++CurIdx) { - CompositeType *CT = dyn_cast(Agg); - if (!CT || CT->isPointerTy()) return nullptr; + CompositeType *CT = CompositeType::get(Agg, false); + if (!CT) + return nullptr; IndexTy Index = IdxList[CurIdx]; if (!CT->indexValid(Index)) return nullptr; Agg = CT->getTypeAtIndex(Index); @@ -2059,7 +2060,7 @@ bool ShuffleVectorInst::isIdentityWithPadding() const { int NumOpElts = Op<0>()->getType()->getVectorNumElements(); - int NumMaskElts = getType()->getVectorNumElements(); + int NumMaskElts = getType()->getNumElements(); if (NumMaskElts <= NumOpElts) return false; @@ -2078,7 +2079,7 @@ bool ShuffleVectorInst::isIdentityWithExtract() const { int NumOpElts = Op<0>()->getType()->getVectorNumElements(); - int NumMaskElts = getType()->getVectorNumElements(); + int NumMaskElts = getType()->getNumElements(); if (NumMaskElts >= NumOpElts) return false; @@ -2091,7 +2092,7 @@ return false; int NumOpElts = Op<0>()->getType()->getVectorNumElements(); - int NumMaskElts = getType()->getVectorNumElements(); + int NumMaskElts = getType()->getNumElements(); if (NumMaskElts != NumOpElts * 2) return false; @@ -2181,7 +2182,7 @@ return nullptr; } - Agg = cast(Agg)->getTypeAtIndex(Index); + Agg = CompositeType::get(Agg)->getTypeAtIndex(Index); } return const_cast(Agg); } diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -577,12 +577,42 @@ return true; } +CompositeType *CompositeType::get(Type *TY, bool infallible) { + if (auto *Retval = dyn_cast(TY)) + return Retval; + if (auto *Retval = dyn_cast(TY)) + return Retval; + if (auto *Retval = dyn_cast(TY)) + return Retval; + + assert(!infallible && "Invalid conversion from Type to CompositeType"); + + return nullptr; +} + +Type *CompositeType::from() { + switch (ID) { + case TypeID::ArrayTyID: + return cast(this); + case TypeID::VectorTyID: + return cast(this); + case TypeID::StructTyID: + return cast(this); + default: + llvm_unreachable("CompositeType is not a valid type"); + } +} + //===----------------------------------------------------------------------===// // ArrayType Implementation //===----------------------------------------------------------------------===// ArrayType::ArrayType(Type *ElType, uint64_t NumEl) - : SequentialType(ArrayTyID, ElType, NumEl) {} + : Type(ElType->getContext(), ArrayTyID), + SequentialType(ArrayTyID, ElType, NumEl) { + ContainedTys = &ElType; + NumContainedTys = 1; +} ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) { assert(isValidElementType(ElementType) && "Invalid type for array element!"); @@ -609,7 +639,11 @@ //===----------------------------------------------------------------------===// VectorType::VectorType(Type *ElType, ElementCount EC) - : SequentialType(VectorTyID, ElType, EC.Min), Scalable(EC.Scalable) {} + : Type(ElType->getContext(), VectorTyID), + SequentialType(VectorTyID, ElType, EC.Min), Scalable(EC.Scalable) { + ContainedTys = &ElType; + NumContainedTys = 1; +} VectorType *VectorType::get(Type *ElementType, ElementCount EC) { assert(EC.Min > 0 && "#Elements of a VectorType must be greater than 0"); diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -173,9 +173,10 @@ if (DSTy->isLiteral() != SSTy->isLiteral() || DSTy->isPacked() != SSTy->isPacked()) return false; - } else if (auto *DSeqTy = dyn_cast(DstTy)) { + } else if (auto *DSeqTy = dyn_cast_or_null( + CompositeType::get(DstTy, false))) { if (DSeqTy->getNumElements() != - cast(SrcTy)->getNumElements()) + cast(CompositeType::get(SrcTy))->getNumElements()) return false; } diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp --- a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp @@ -365,7 +365,8 @@ } Type *AT = Alloca->getAllocatedType(); - SequentialType *AllocaTy = dyn_cast(AT); + SequentialType *AllocaTy = + dyn_cast_or_null(CompositeType::get(AT, false)); LLVM_DEBUG(dbgs() << "Alloca candidate for vectorization\n"); diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -16927,7 +16927,7 @@ case Intrinsic::arm_mve_vld4q: { Info.opc = ISD::INTRINSIC_W_CHAIN; // Conservatively set memVT to the entire set of vectors loaded. - Type *VecTy = cast(I.getType())->getTypeAtIndex(1); + Type *VecTy = CompositeType::get(I.getType())->getTypeAtIndex(1); unsigned Factor = Intrinsic == Intrinsic::arm_mve_vld2q ? 2 : 4; Info.memVT = EVT::getVectorVT(VecTy->getContext(), MVT::i64, Factor * 2); Info.ptrVal = I.getArgOperand(0); diff --git a/llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp b/llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp --- a/llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp +++ b/llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp @@ -206,7 +206,8 @@ return PTy->getElementType(); // Advance the type. if (!Ty->isStructTy()) { - Type *NexTy = cast(Ty)->getElementType(); + Type *NexTy = + cast(CompositeType::get(Ty))->getElementType(); return NexTy; } // Otherwise it is a struct type. diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp --- a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp +++ b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp @@ -295,7 +295,7 @@ if (auto *ElPTy = dyn_cast(ElTy)) ElTy = ElPTy->getElementType(); else - ElTy = cast(ElTy)->getTypeAtIndex(II); + ElTy = CompositeType::get(ElTy)->getTypeAtIndex(II); } // And create a GEP to extract those indices. V = IRB.CreateGEP(ArgIndex.first, V, Ops, V->getName() + ".idx"); @@ -784,11 +784,12 @@ if (DL.getTypeSizeInBits(type) != DL.getTypeAllocSizeInBits(type)) return false; - if (!isa(type)) + if (!CompositeType::is(type)) return true; // For homogenous sequential types, check for padding within members. - if (SequentialType *seqTy = dyn_cast(type)) + if (SequentialType *seqTy = + dyn_cast(CompositeType::get(type))) return isDenselyPacked(seqTy->getElementType(), DL); // Check for padding within and between elements of a struct. diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -131,7 +131,7 @@ case Type::PointerTyID: return true; case Type::ArrayTyID: case Type::VectorTyID: { - SequentialType *STy = cast(Ty); + SequentialType *STy = cast(CompositeType::get(Ty)); Types.push_back(STy->getElementType()); break; } @@ -142,7 +142,7 @@ E = STy->element_end(); I != E; ++I) { Type *InnerTy = *I; if (isa(InnerTy)) return true; - if (isa(InnerTy)) + if (CompositeType::is(InnerTy)) Types.push_back(InnerTy); } break; @@ -438,7 +438,8 @@ if (isa(Init->getType())) { // nothing to check - } else if (SequentialType *STy = dyn_cast(Init->getType())) { + } else if (SequentialType *STy = dyn_cast_or_null( + CompositeType::get(Init->getType(), false))) { if (STy->getNumElements() > 16 && GV->hasNUsesOrMore(16)) return false; // It's not worth it. } else @@ -509,7 +510,8 @@ Type *ElTy = nullptr; if (StructType *STy = dyn_cast(Ty)) ElTy = STy->getElementType(ElementIdx); - else if (SequentialType *STy = dyn_cast(Ty)) + else if (SequentialType *STy = dyn_cast_or_null( + CompositeType::get(Ty, false))) ElTy = STy->getElementType(); assert(ElTy); @@ -541,7 +543,8 @@ uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx); transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, STy->getNumElements()); - } else if (SequentialType *STy = dyn_cast(Ty)) { + } else if (SequentialType *STy = dyn_cast_or_null( + CompositeType::get(Ty, false))) { uint64_t EltSize = DL.getTypeAllocSize(ElTy); Align EltAlign(DL.getABITypeAlignment(ElTy)); uint64_t FragmentSizeInBits = DL.getTypeAllocSizeInBits(ElTy); @@ -2427,7 +2430,8 @@ } ConstantInt *CI = cast(Addr->getOperand(OpNo)); - SequentialType *InitTy = cast(Init->getType()); + SequentialType *InitTy = + cast(CompositeType::get(Init->getType())); uint64_t NumElts = InitTy->getNumElements(); // Break up the array into elements. @@ -2562,7 +2566,8 @@ if (auto *STy = dyn_cast(Ty)) NumElts = STy->getNumElements(); else - NumElts = cast(Ty)->getNumElements(); + NumElts = + cast(CompositeType::get(Ty))->getNumElements(); for (unsigned i = 0, e = NumElts; i != e; ++i) Elts.push_back(Init->getAggregateElement(i)); } diff --git a/llvm/lib/Transforms/IPO/StripSymbols.cpp b/llvm/lib/Transforms/IPO/StripSymbols.cpp --- a/llvm/lib/Transforms/IPO/StripSymbols.cpp +++ b/llvm/lib/Transforms/IPO/StripSymbols.cpp @@ -149,7 +149,7 @@ GV->eraseFromParent(); } else if (!isa(C)) - if (isa(C->getType())) + if (CompositeType::is(C->getType())) C->destroyConstant(); // If the constant referenced anything, see if we can delete it as well. diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -2427,10 +2427,10 @@ // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. // This can enhance SROA and other transforms that want type-safe pointers. unsigned NumZeros = 0; - while (SrcElTy != DstElTy && - isa(SrcElTy) && !SrcElTy->isPointerTy() && + while (SrcElTy != DstElTy && CompositeType::is(SrcElTy) && + !SrcElTy->isPointerTy() && SrcElTy->getNumContainedTypes() /* not "{}" */) { - SrcElTy = cast(SrcElTy)->getTypeAtIndex(0U); + SrcElTy = CompositeType::get(SrcElTy)->getTypeAtIndex(0U); ++NumZeros; } diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1934,7 +1934,7 @@ if (J > 0) { if (J == 1) { CurTy = Op1->getSourceElementType(); - } else if (auto *CT = dyn_cast(CurTy)) { + } else if (auto *CT = CompositeType::get(CurTy, false)) { CurTy = CT->getTypeAtIndex(Op1->getOperand(J)); } else { CurTy = nullptr; diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -3511,7 +3511,8 @@ (DL.getTypeAllocSize(Ty) - Offset) < Size) return nullptr; - if (SequentialType *SeqTy = dyn_cast(Ty)) { + if (SequentialType *SeqTy = + dyn_cast_or_null(CompositeType::get(Ty, false))) { Type *ElementTy = SeqTy->getElementType(); uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); uint64_t NumSkippedElements = Offset / ElementSize; diff --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp --- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp +++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp @@ -478,8 +478,8 @@ case Type::ArrayTyID: case Type::VectorTyID: { - auto *STyL = cast(TyL); - auto *STyR = cast(TyR); + auto *STyL = cast(CompositeType::get(TyL)); + auto *STyR = cast(CompositeType::get(TyR)); if (STyL->getNumElements() != STyR->getNumElements()) return cmpNumbers(STyL->getNumElements(), STyR->getNumElements()); return cmpTypes(STyL->getElementType(), STyR->getElementType()); diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -3130,7 +3130,7 @@ unsigned N = 1; Type *EltTy = T; - while (isa(EltTy)) { + while (CompositeType::is(EltTy)) { if (auto *ST = dyn_cast(EltTy)) { // Check that struct is homogeneous. for (const auto *Ty : ST->elements()) @@ -3139,7 +3139,7 @@ N *= ST->getNumElements(); EltTy = *ST->element_begin(); } else { - auto *SeqT = cast(EltTy); + auto *SeqT = cast(CompositeType::get(EltTy)); N *= SeqT->getNumElements(); EltTy = SeqT->getElementType(); }