ctetreau created this revision. Herald added subscribers: llvm-commits, cfe-commits, kerbowa, psnobl, rkruppe, hiraditya, tschuett, nhaehnle, jvesely, arsenm. Herald added a reviewer: efriedma. Herald added projects: clang, LLVM.
This patch is a work in progress Make CompositeType not inherit from Type. Instead, Vecotr, Array, and Struct Types directly inherit from Type. This is in preparation for the introduction of distinct Fixed and Scalable Vector Types Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D75486 Files: clang/lib/CodeGen/CGDecl.cpp clang/lib/CodeGen/CGExprConstant.cpp clang/unittests/CodeGen/CodeGenExternalTest.cpp llvm/include/llvm/IR/Constants.h llvm/include/llvm/IR/DerivedTypes.h llvm/include/llvm/IR/GetElementPtrTypeIterator.h llvm/lib/Analysis/BasicAliasAnalysis.cpp llvm/lib/Analysis/ConstantFolding.cpp llvm/lib/Analysis/ScalarEvolution.cpp llvm/lib/Bitcode/Reader/BitcodeReader.cpp llvm/lib/CodeGen/Analysis.cpp llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp llvm/lib/FuzzMutate/Operations.cpp llvm/lib/IR/ConstantFold.cpp llvm/lib/IR/Constants.cpp llvm/lib/IR/Core.cpp llvm/lib/IR/Instructions.cpp llvm/lib/IR/Type.cpp llvm/lib/Linker/IRMover.cpp llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp llvm/lib/Target/ARM/ARMISelLowering.cpp llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp llvm/lib/Transforms/IPO/ArgumentPromotion.cpp llvm/lib/Transforms/IPO/GlobalOpt.cpp llvm/lib/Transforms/IPO/StripSymbols.cpp llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp llvm/lib/Transforms/InstCombine/InstructionCombining.cpp llvm/lib/Transforms/Scalar/SROA.cpp llvm/lib/Transforms/Utils/FunctionComparator.cpp llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Index: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -3130,7 +3130,7 @@ unsigned N = 1; Type *EltTy = T; - while (isa<CompositeType>(EltTy)) { + while (CompositeType::is(EltTy)) { if (auto *ST = dyn_cast<StructType>(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<SequentialType>(EltTy); + auto *SeqT = cast<SequentialType>(CompositeType::get(EltTy)); N *= SeqT->getNumElements(); EltTy = SeqT->getElementType(); } Index: llvm/lib/Transforms/Utils/FunctionComparator.cpp =================================================================== --- llvm/lib/Transforms/Utils/FunctionComparator.cpp +++ llvm/lib/Transforms/Utils/FunctionComparator.cpp @@ -478,8 +478,8 @@ case Type::ArrayTyID: case Type::VectorTyID: { - auto *STyL = cast<SequentialType>(TyL); - auto *STyR = cast<SequentialType>(TyR); + auto *STyL = cast<SequentialType>(CompositeType::get(TyL)); + auto *STyR = cast<SequentialType>(CompositeType::get(TyR)); if (STyL->getNumElements() != STyR->getNumElements()) return cmpNumbers(STyL->getNumElements(), STyR->getNumElements()); return cmpTypes(STyL->getElementType(), STyR->getElementType()); Index: llvm/lib/Transforms/Scalar/SROA.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SROA.cpp +++ llvm/lib/Transforms/Scalar/SROA.cpp @@ -3511,7 +3511,8 @@ (DL.getTypeAllocSize(Ty) - Offset) < Size) return nullptr; - if (SequentialType *SeqTy = dyn_cast<SequentialType>(Ty)) { + if (SequentialType *SeqTy = dyn_cast_or_null<SequentialType>( + CompositeType::get(Ty, CompositeType::is(Ty)))) { Type *ElementTy = SeqTy->getElementType(); uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); uint64_t NumSkippedElements = Offset / ElementSize; Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1934,7 +1934,8 @@ if (J > 0) { if (J == 1) { CurTy = Op1->getSourceElementType(); - } else if (auto *CT = dyn_cast<CompositeType>(CurTy)) { + } else if (auto *CT = + CompositeType::get(CurTy, CompositeType::is(CurTy))) { CurTy = CT->getTypeAtIndex(Op1->getOperand(J)); } else { CurTy = nullptr; Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -2428,9 +2428,9 @@ // This can enhance SROA and other transforms that want type-safe pointers. unsigned NumZeros = 0; while (SrcElTy != DstElTy && - isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() && + CompositeType::is(SrcElTy) && !SrcElTy->isPointerTy() && SrcElTy->getNumContainedTypes() /* not "{}" */) { - SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(0U); + SrcElTy = CompositeType::get(SrcElTy)->getTypeAtIndex(0U); ++NumZeros; } Index: llvm/lib/Transforms/IPO/StripSymbols.cpp =================================================================== --- llvm/lib/Transforms/IPO/StripSymbols.cpp +++ llvm/lib/Transforms/IPO/StripSymbols.cpp @@ -149,7 +149,7 @@ GV->eraseFromParent(); } else if (!isa<Function>(C)) - if (isa<CompositeType>(C->getType())) + if (CompositeType::is(C->getType())) C->destroyConstant(); // If the constant referenced anything, see if we can delete it as well. Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -131,7 +131,7 @@ case Type::PointerTyID: return true; case Type::ArrayTyID: case Type::VectorTyID: { - SequentialType *STy = cast<SequentialType>(Ty); + SequentialType *STy = cast<SequentialType>(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<PointerType>(InnerTy)) return true; - if (isa<CompositeType>(InnerTy)) + if (CompositeType::is(InnerTy)) Types.push_back(InnerTy); } break; @@ -438,7 +438,9 @@ if (isa<StructType>(Init->getType())) { // nothing to check - } else if (SequentialType *STy = dyn_cast<SequentialType>(Init->getType())) { + } else if (SequentialType *STy = + dyn_cast_or_null<SequentialType>(CompositeType::get( + Init->getType(), CompositeType::is(Init->getType())))) { if (STy->getNumElements() > 16 && GV->hasNUsesOrMore(16)) return false; // It's not worth it. } else @@ -509,7 +511,8 @@ Type *ElTy = nullptr; if (StructType *STy = dyn_cast<StructType>(Ty)) ElTy = STy->getElementType(ElementIdx); - else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) + else if (SequentialType *STy = dyn_cast_or_null<SequentialType>( + CompositeType::get(Ty, CompositeType::is(Ty)))) ElTy = STy->getElementType(); assert(ElTy); @@ -541,7 +544,8 @@ uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx); transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, STy->getNumElements()); - } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) { + } else if (SequentialType *STy = dyn_cast_or_null<SequentialType>( + CompositeType::get(Ty, CompositeType::is(Ty)))) { uint64_t EltSize = DL.getTypeAllocSize(ElTy); Align EltAlign(DL.getABITypeAlignment(ElTy)); uint64_t FragmentSizeInBits = DL.getTypeAllocSizeInBits(ElTy); @@ -2427,7 +2431,8 @@ } ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo)); - SequentialType *InitTy = cast<SequentialType>(Init->getType()); + SequentialType *InitTy = + cast<SequentialType>(CompositeType::get(Init->getType())); uint64_t NumElts = InitTy->getNumElements(); // Break up the array into elements. @@ -2562,7 +2567,8 @@ if (auto *STy = dyn_cast<StructType>(Ty)) NumElts = STy->getNumElements(); else - NumElts = cast<SequentialType>(Ty)->getNumElements(); + NumElts = + cast<SequentialType>(CompositeType::get(Ty))->getNumElements(); for (unsigned i = 0, e = NumElts; i != e; ++i) Elts.push_back(Init->getAggregateElement(i)); } Index: llvm/lib/Transforms/IPO/ArgumentPromotion.cpp =================================================================== --- llvm/lib/Transforms/IPO/ArgumentPromotion.cpp +++ llvm/lib/Transforms/IPO/ArgumentPromotion.cpp @@ -295,7 +295,7 @@ if (auto *ElPTy = dyn_cast<PointerType>(ElTy)) ElTy = ElPTy->getElementType(); else - ElTy = cast<CompositeType>(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<CompositeType>(type)) + if (!CompositeType::is(type)) return true; // For homogenous sequential types, check for padding within members. - if (SequentialType *seqTy = dyn_cast<SequentialType>(type)) + if (SequentialType *seqTy = + dyn_cast<SequentialType>(CompositeType::get(type))) return isDenselyPacked(seqTy->getElementType(), DL); // Check for padding within and between elements of a struct. Index: llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp =================================================================== --- llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp +++ llvm/lib/Target/Hexagon/HexagonCommonGEP.cpp @@ -206,7 +206,7 @@ return PTy->getElementType(); // Advance the type. if (!Ty->isStructTy()) { - Type *NexTy = cast<SequentialType>(Ty)->getElementType(); + Type *NexTy = cast<SequentialType>(CompositeType::get(Ty))->getElementType(); return NexTy; } // Otherwise it is a struct type. Index: llvm/lib/Target/ARM/ARMISelLowering.cpp =================================================================== --- llvm/lib/Target/ARM/ARMISelLowering.cpp +++ 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<CompositeType>(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); Index: llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp +++ llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp @@ -365,7 +365,8 @@ } Type *AT = Alloca->getAllocatedType(); - SequentialType *AllocaTy = dyn_cast<SequentialType>(AT); + SequentialType *AllocaTy = dyn_cast_or_null<SequentialType>( + CompositeType::get(AT, CompositeType::is(AT))); LLVM_DEBUG(dbgs() << "Alloca candidate for vectorization\n"); Index: llvm/lib/Linker/IRMover.cpp =================================================================== --- llvm/lib/Linker/IRMover.cpp +++ 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<SequentialType>(DstTy)) { + } else if (auto *DSeqTy = dyn_cast_or_null<SequentialType>( + CompositeType::get(DstTy, CompositeType::is(DstTy)))) { if (DSeqTy->getNumElements() != - cast<SequentialType>(SrcTy)->getNumElements()) + cast<SequentialType>(CompositeType::get(SrcTy))->getNumElements()) return false; } Index: llvm/lib/IR/Type.cpp =================================================================== --- llvm/lib/IR/Type.cpp +++ llvm/lib/IR/Type.cpp @@ -577,12 +577,42 @@ return true; } +CompositeType *CompositeType::get(Type *TY, bool infallible) { + if (auto *Retval = dyn_cast<ArrayType>(TY)) + return Retval; + if (auto *Retval = dyn_cast<StructType>(TY)) + return Retval; + if (auto *Retval = dyn_cast<VectorType>(TY)) + return Retval; + + assert(!infallible && "Invalid conversion from Type to CompositeType"); + + return nullptr; +} + +CompositeType::operator Type *() { + switch (ID) { + case TypeID::ArrayTyID: + return cast<ArrayType>(this); + case TypeID::VectorTyID: + return cast<VectorType>(this); + case TypeID::StructTyID: + return cast<StructType>(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"); Index: llvm/lib/IR/Instructions.cpp =================================================================== --- llvm/lib/IR/Instructions.cpp +++ llvm/lib/IR/Instructions.cpp @@ -1634,8 +1634,8 @@ unsigned CurIdx = 1; for (; CurIdx != IdxList.size(); ++CurIdx) { - CompositeType *CT = dyn_cast<CompositeType>(Agg); - if (!CT || CT->isPointerTy()) return nullptr; + CompositeType *CT = CompositeType::get(Agg, CompositeType::is(Agg)); + if (!CT) return nullptr; IndexTy Index = IdxList[CurIdx]; if (!CT->indexValid(Index)) return nullptr; Agg = CT->getTypeAtIndex(Index); @@ -2059,7 +2059,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 +2078,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 +2091,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 +2181,7 @@ return nullptr; } - Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); + Agg = CompositeType::get(Agg)->getTypeAtIndex(Index); } return const_cast<Type*>(Agg); } Index: llvm/lib/IR/Core.cpp =================================================================== --- llvm/lib/IR/Core.cpp +++ llvm/lib/IR/Core.cpp @@ -753,7 +753,7 @@ auto *Ty = unwrap<Type>(WrappedTy); if (auto *PTy = dyn_cast<PointerType>(Ty)) return wrap(PTy->getElementType()); - return wrap(cast<SequentialType>(Ty)->getElementType()); + return wrap(cast<SequentialType>(CompositeType::get(Ty))->getElementType()); } unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) { Index: llvm/lib/IR/Constants.cpp =================================================================== --- llvm/lib/IR/Constants.cpp +++ llvm/lib/IR/Constants.cpp @@ -931,13 +931,15 @@ } Constant *ConstantAggregateZero::getElementValue(Constant *C) const { - if (isa<SequentialType>(getType())) + if (isa<SequentialType>( + CompositeType::get(getType(), CompositeType::is(getType())))) return getSequentialElement(); return getStructElement(cast<ConstantInt>(C)->getZExtValue()); } Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { - if (isa<SequentialType>(getType())) + if (isa<SequentialType>( + CompositeType::get(getType(), CompositeType::is(getType())))) return getSequentialElement(); return getStructElement(Idx); } @@ -964,20 +966,22 @@ } UndefValue *UndefValue::getElementValue(Constant *C) const { - if (isa<SequentialType>(getType())) + if (isa<SequentialType>( + CompositeType::get(getType(), CompositeType::is(getType())))) return getSequentialElement(); return getStructElement(cast<ConstantInt>(C)->getZExtValue()); } UndefValue *UndefValue::getElementValue(unsigned Idx) const { - if (isa<SequentialType>(getType())) + if (isa<SequentialType>( + CompositeType::get(getType(), CompositeType::is(getType())))) return getSequentialElement(); return getStructElement(Idx); } unsigned UndefValue::getNumElements() const { Type *Ty = getType(); - if (auto *ST = dyn_cast<SequentialType>(Ty)) + if (auto *ST = dyn_cast<SequentialType>(CompositeType::get(Ty))) return ST->getNumElements(); return Ty->getStructNumElements(); } @@ -1049,7 +1053,7 @@ ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT, ArrayRef<Constant *> V) - : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(), + : Constant((Type *) T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(), V.size()) { llvm::copy(V, op_begin()); @@ -2528,9 +2532,7 @@ } unsigned ConstantDataSequential::getNumElements() const { - if (ArrayType *AT = dyn_cast<ArrayType>(getType())) - return AT->getNumElements(); - return getType()->getVectorNumElements(); + return getType()->getNumElements(); } @@ -2577,7 +2579,7 @@ ConstantDataSequential **Entry = &Slot.second; for (ConstantDataSequential *Node = *Entry; Node; Entry = &Node->Next, Node = *Entry) - if (Node->getType() == Ty) + if ((Type *) Node->getType() == Ty) return Node; // Okay, we didn't get a hit. Create a node of the right class, link it in, @@ -2592,7 +2594,7 @@ void ConstantDataSequential::destroyConstantImpl() { // Remove the constant from the StringMap. StringMap<ConstantDataSequential*> &CDSConstants = - getType()->getContext().pImpl->CDSConstants; + ((Type *)getType())->getContext().pImpl->CDSConstants; StringMap<ConstantDataSequential*>::iterator Slot = CDSConstants.find(getRawDataValues()); Index: llvm/lib/IR/ConstantFold.cpp =================================================================== --- llvm/lib/IR/ConstantFold.cpp +++ 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<SequentialType>(ElTy)) { + } else if (SequentialType *STy = dyn_cast_or_null<SequentialType>( + CompositeType::get(ElTy, CompositeType::is(ElTy)))) { ElTy = STy->getElementType(); IdxList.push_back(Zero); } else { @@ -936,7 +936,7 @@ if (StructType *ST = dyn_cast<StructType>(Agg->getType())) NumElts = ST->getNumElements(); else - NumElts = cast<SequentialType>(Agg->getType())->getNumElements(); + NumElts = cast<SequentialType>(CompositeType::get(Agg->getType()))->getNumElements(); SmallVector<Constant*, 32> Result; for (unsigned i = 0; i != NumElts; ++i) { @@ -2388,7 +2388,7 @@ bool Unknown = !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]); for (unsigned i = 1, e = Idxs.size(); i != e; - Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) { + Prev = Ty, Ty = CompositeType::get(Ty)->getTypeAtIndex(Idxs[i]), ++i) { if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) { // We don't know if it's in range or not. Unknown = true; @@ -2407,7 +2407,7 @@ // The verify makes sure that GEPs into a struct are in range. continue; } - auto *STy = cast<SequentialType>(Ty); + auto *STy = cast<SequentialType>(CompositeType::get(Ty)); if (isa<VectorType>(STy)) { // There can be awkward padding in after a non-power of two vector. Unknown = true; @@ -2448,7 +2448,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. Index: llvm/lib/FuzzMutate/Operations.cpp =================================================================== --- llvm/lib/FuzzMutate/Operations.cpp +++ llvm/lib/FuzzMutate/Operations.cpp @@ -244,7 +244,7 @@ static SourcePred validInsertValueIndex() { auto Pred = [](ArrayRef<Value *> Cur, const Value *V) { - auto *CTy = cast<CompositeType>(Cur[0]->getType()); + auto *CTy = CompositeType::get(Cur[0]->getType()); if (auto *CI = dyn_cast<ConstantInt>(V)) if (CI->getBitWidth() == 32 && CTy->getTypeAtIndex(CI->getZExtValue()) == Cur[1]->getType()) @@ -254,8 +254,8 @@ auto Make = [](ArrayRef<Value *> Cur, ArrayRef<Type *> Ts) { std::vector<Constant *> Result; auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext()); - auto *CTy = cast<CompositeType>(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; Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ 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((Type *) CDS->getType()); // 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((Type *) CDS->getType()); unsigned EmittedSize = DL.getTypeAllocSize(CDS->getType()->getElementType()) * CDS->getNumElements(); assert(EmittedSize <= Size && "Size cannot be less than EmittedSize!"); Index: llvm/lib/CodeGen/Analysis.cpp =================================================================== --- llvm/lib/CodeGen/Analysis.cpp +++ llvm/lib/CodeGen/Analysis.cpp @@ -437,7 +437,7 @@ ++Path.back(); Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back()); while (DeeperType->isAggregateType()) { - CompositeType *CT = cast<CompositeType>(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<CompositeType>(Next), 0)) { - SubTypes.push_back(cast<CompositeType>(Next)); + indexReallyValid(CompositeType::get(Next), 0)) { + SubTypes.push_back(CompositeType::get(Next)); Path.push_back(0); - Next = cast<CompositeType>(Next)->getTypeAtIndex(0U); + Next = CompositeType::get(Next)->getTypeAtIndex(0U); } // If there's no Path now, Next was originally scalar already (or empty Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -2471,7 +2471,8 @@ if (Record.empty()) return error("Invalid record"); - Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); + Type *EltTy = + cast<SequentialType>(CompositeType::get(CurTy))->getElementType(); if (EltTy->isIntegerTy(8)) { SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); if (isa<VectorType>(CurTy)) Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -3519,7 +3519,7 @@ CurTy = STy->getTypeAtIndex(Index); } else { // Update CurTy to its element type. - CurTy = cast<SequentialType>(CurTy)->getElementType(); + CurTy = cast<SequentialType>(CompositeType::get(CurTy))->getElementType(); // For an array, add the element offset, explicitly scaled. const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy); // Getelementptr indices are signed. Index: llvm/lib/Analysis/ConstantFolding.cpp =================================================================== --- llvm/lib/Analysis/ConstantFolding.cpp +++ 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<SequentialType>(Ty)) { + } else if (auto *ATy = dyn_cast<SequentialType>(CompositeType::get(Ty))) { Ty = ATy->getElementType(); } else { // We've reached some non-indexable type. Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp =================================================================== --- llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1145,7 +1145,7 @@ GEP1->getSourceElementType(), IntermediateIndices); StructType *LastIndexedStruct = dyn_cast<StructType>(Ty); - if (isa<SequentialType>(Ty)) { + if (isa_and_nonnull<SequentialType>(CompositeType::get(Ty, CompositeType::is(Ty)))) { // 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<SequentialType>(Ty)->getElementType()); + const uint64_t ElementSize = DL.getTypeStoreSize( + cast<SequentialType>(CompositeType::get(Ty))->getElementType()); if (V1Size != ElementSize || V2Size != ElementSize) return MayAlias; Index: llvm/include/llvm/IR/GetElementPtrTypeIterator.h =================================================================== --- llvm/include/llvm/IR/GetElementPtrTypeIterator.h +++ llvm/include/llvm/IR/GetElementPtrTypeIterator.h @@ -75,7 +75,7 @@ generic_gep_type_iterator& operator++() { // Preincrement Type *Ty = getIndexedType(); - if (auto *STy = dyn_cast<SequentialType>(Ty)) { + if (auto *STy = dyn_cast<SequentialType>(CompositeType::get(Ty))) { CurTy = STy->getElementType(); NumElements = STy->getNumElements(); } else Index: llvm/include/llvm/IR/DerivedTypes.h =================================================================== --- llvm/include/llvm/IR/DerivedTypes.h +++ 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,28 @@ 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; + operator Type *(); + + 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<T> if infallible = true, and behaves like dyn_cast<T> if + /// infallible = false. + /// + /// Alternatively you call CompositeType::get(TY, CompositeType::is(TY)) + /// to get the dyn_cast<T> behavior and CompositeType::get(TY) to get the + /// cast<T> 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 +271,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 +391,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 +420,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 +433,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 +457,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 +468,7 @@ } /// Class to represent vector types. -class VectorType : public SequentialType { +class VectorType : public Type, public SequentialType { /// A fully specified VectorType is of the form <vscale x n x Ty>. '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 +600,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 { Index: llvm/include/llvm/IR/Constants.h =================================================================== --- llvm/include/llvm/IR/Constants.h +++ 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<SequentialType>(Value::getType()); + return cast<SequentialType>(CompositeType::get(Value::getType())); } /// Return the element type of the array/vector. Index: clang/unittests/CodeGen/CodeGenExternalTest.cpp =================================================================== --- clang/unittests/CodeGen/CodeGenExternalTest.cpp +++ clang/unittests/CodeGen/CodeGenExternalTest.cpp @@ -199,7 +199,7 @@ dbgs() << "\n"; } - llvm::CompositeType* structTy = dyn_cast<CompositeType>(llvmTy); + llvm::CompositeType* structTy = CompositeType::get(llvmTy, false); ASSERT_TRUE(structTy != NULL); // Check getLLVMFieldNumber Index: clang/lib/CodeGen/CGExprConstant.cpp =================================================================== --- clang/lib/CodeGen/CGExprConstant.cpp +++ clang/lib/CodeGen/CGExprConstant.cpp @@ -321,7 +321,9 @@ replace(Elems, Index, Index + 1, llvm::map_range(llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) { return CA->getOperand(Op); })); - if (auto *Seq = dyn_cast<llvm::SequentialType>(CA->getType())) { + if (auto *Seq = + dyn_cast_or_null<llvm::SequentialType>(llvm::CompositeType::get( + CA->getType(), llvm::CompositeType::is(CA->getType())))) { // Array or vector. CharUnits ElemSize = getSize(Seq->getElementType()); replace( Index: clang/lib/CodeGen/CGDecl.cpp =================================================================== --- clang/lib/CodeGen/CGDecl.cpp +++ clang/lib/CodeGen/CGDecl.cpp @@ -1050,7 +1050,8 @@ llvm::Type *OrigTy = constant->getType(); if (const auto STy = dyn_cast<llvm::StructType>(OrigTy)) return constStructWithPadding(CGM, isPattern, STy, constant); - if (auto *STy = dyn_cast<llvm::SequentialType>(OrigTy)) { + if (auto *STy = dyn_cast_or_null<llvm::SequentialType>( + llvm::CompositeType::get(OrigTy, llvm::CompositeType::is(OrigTy)))) { llvm::SmallVector<llvm::Constant *, 8> Values; unsigned Size = STy->getNumElements(); if (!Size)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits