Skip to content

Commit 68b03ae

Browse files
committed
Remove SequentialType from the type heirarchy.
Now that we have scalable vectors, there's a distinction that isn't getting captured in the original SequentialType: some vectors don't have a known element count, so counting the number of elements doesn't make sense. In some cases, there's a better way to express the commonality using other methods. If we're dealing with GEPs, there's GEP methods; if we're dealing with a ConstantDataSequential, we can query its element type directly. In the relatively few remaining cases, I just decided to write out the type checks. We're talking about relatively few places, and I think the abstraction doesn't really carry its weight. (See thread "[RFC] Refactor class hierarchy of VectorType in the IR" on llvmdev.) Differential Revision: https://reviews.llvm.org/D75661
1 parent 8f2d2a7 commit 68b03ae

File tree

25 files changed

+239
-176
lines changed

25 files changed

+239
-176
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,17 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
318318
CharUnits Offset = Offsets[Index];
319319

320320
if (auto *CA = dyn_cast<llvm::ConstantAggregate>(C)) {
321+
// Expand the sequence into its contained elements.
322+
// FIXME: This assumes vector elements are byte-sized.
321323
replace(Elems, Index, Index + 1,
322324
llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
323325
[&](unsigned Op) { return CA->getOperand(Op); }));
324-
if (auto *Seq = dyn_cast<llvm::SequentialType>(CA->getType())) {
326+
if (isa<llvm::ArrayType>(CA->getType()) ||
327+
isa<llvm::VectorType>(CA->getType())) {
325328
// Array or vector.
326-
CharUnits ElemSize = getSize(Seq->getElementType());
329+
llvm::Type *ElemTy =
330+
llvm::GetElementPtrInst::getTypeAtIndex(CA->getType(), (uint64_t)0);
331+
CharUnits ElemSize = getSize(ElemTy);
327332
replace(
328333
Offsets, Index, Index + 1,
329334
llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
@@ -344,6 +349,8 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
344349
}
345350

346351
if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) {
352+
// Expand the sequence into its contained elements.
353+
// FIXME: This assumes vector elements are byte-sized.
347354
// FIXME: If possible, split into two ConstantDataSequentials at Hint.
348355
CharUnits ElemSize = getSize(CDS->getElementType());
349356
replace(Elems, Index, Index + 1,
@@ -359,6 +366,7 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
359366
}
360367

361368
if (isa<llvm::ConstantAggregateZero>(C)) {
369+
// Split into two zeros at the hinted offset.
362370
CharUnits ElemSize = getSize(C);
363371
assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split");
364372
replace(Elems, Index, Index + 1,
@@ -368,6 +376,7 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
368376
}
369377

370378
if (isa<llvm::UndefValue>(C)) {
379+
// Drop undef; it doesn't contribute to the final layout.
371380
replace(Elems, Index, Index + 1, {});
372381
replace(Offsets, Index, Index + 1, {});
373382
return true;

llvm/include/llvm/IR/Constants.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ namespace llvm {
4444
class ArrayType;
4545
class IntegerType;
4646
class PointerType;
47-
class SequentialType;
4847
class StructType;
4948
class VectorType;
5049
template <class ConstantClass> struct ConstantAggrKeyType;
@@ -631,12 +630,6 @@ class ConstantDataSequential : public ConstantData {
631630
/// efficient as getElementAsInteger/Float/Double.
632631
Constant *getElementAsConstant(unsigned i) const;
633632

634-
/// Specialize the getType() method to always return a SequentialType, which
635-
/// reduces the amount of casting needed in parts of the compiler.
636-
inline SequentialType *getType() const {
637-
return cast<SequentialType>(Value::getType());
638-
}
639-
640633
/// Return the element type of the array/vector.
641634
Type *getElementType() const;
642635

llvm/include/llvm/IR/DerivedTypes.h

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -354,47 +354,22 @@ Type *Type::getStructElementType(unsigned N) const {
354354
return cast<StructType>(this)->getElementType(N);
355355
}
356356

357-
/// This is the superclass of the array and vector type classes. Both of these
358-
/// represent "arrays" in memory. The array type represents a specifically sized
359-
/// array, and the vector type represents a specifically sized array that allows
360-
/// for use of SIMD instructions. SequentialType holds the common features of
361-
/// both, which stem from the fact that both lay their components out in memory
362-
/// identically.
363-
class SequentialType : public Type {
364-
Type *ContainedType; ///< Storage for the single contained type.
357+
/// Class to represent array types.
358+
class ArrayType : public Type {
359+
/// The element type of the array.
360+
Type *ContainedType;
361+
/// Number of elements in the array.
365362
uint64_t NumElements;
366363

367-
protected:
368-
SequentialType(TypeID TID, Type *ElType, uint64_t NumElements)
369-
: Type(ElType->getContext(), TID), ContainedType(ElType),
370-
NumElements(NumElements) {
371-
ContainedTys = &ContainedType;
372-
NumContainedTys = 1;
373-
}
374-
375-
public:
376-
SequentialType(const SequentialType &) = delete;
377-
SequentialType &operator=(const SequentialType &) = delete;
378-
379-
/// For scalable vectors, this will return the minimum number of elements
380-
/// in the vector.
381-
uint64_t getNumElements() const { return NumElements; }
382-
Type *getElementType() const { return ContainedType; }
383-
384-
/// Methods for support type inquiry through isa, cast, and dyn_cast.
385-
static bool classof(const Type *T) {
386-
return T->getTypeID() == ArrayTyID || T->getTypeID() == VectorTyID;
387-
}
388-
};
389-
390-
/// Class to represent array types.
391-
class ArrayType : public SequentialType {
392364
ArrayType(Type *ElType, uint64_t NumEl);
393365

394366
public:
395367
ArrayType(const ArrayType &) = delete;
396368
ArrayType &operator=(const ArrayType &) = delete;
397369

370+
uint64_t getNumElements() const { return NumElements; }
371+
Type *getElementType() const { return ContainedType; }
372+
398373
/// This static method is the primary way to construct an ArrayType
399374
static ArrayType *get(Type *ElementType, uint64_t NumElements);
400375

@@ -412,7 +387,7 @@ uint64_t Type::getArrayNumElements() const {
412387
}
413388

414389
/// Class to represent vector types.
415-
class VectorType : public SequentialType {
390+
class VectorType : public Type {
416391
/// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the
417392
/// minimum number of elements of type Ty contained within the vector, and
418393
/// 'vscale x' indicates that the total element count is an integer multiple
@@ -426,18 +401,28 @@ class VectorType : public SequentialType {
426401
/// <vscale x 4 x i32> - a vector containing an unknown integer multiple
427402
/// of 4 i32s
428403

404+
/// The element type of the vector.
405+
Type *ContainedType;
406+
/// Minumum number of elements in the vector.
407+
uint64_t NumElements;
408+
429409
VectorType(Type *ElType, unsigned NumEl, bool Scalable = false);
430410
VectorType(Type *ElType, ElementCount EC);
431411

432412
// If true, the total number of elements is an unknown multiple of the
433-
// minimum 'NumElements' from SequentialType. Otherwise the total number
434-
// of elements is exactly equal to 'NumElements'.
413+
// minimum 'NumElements'. Otherwise the total number of elements is exactly
414+
// equal to 'NumElements'.
435415
bool Scalable;
436416

437417
public:
438418
VectorType(const VectorType &) = delete;
439419
VectorType &operator=(const VectorType &) = delete;
440420

421+
/// For scalable vectors, this will return the minimum number of elements
422+
/// in the vector.
423+
uint64_t getNumElements() const { return NumElements; }
424+
Type *getElementType() const { return ContainedType; }
425+
441426
/// This static method is the primary way to construct an VectorType.
442427
static VectorType *get(Type *ElementType, ElementCount EC);
443428
static VectorType *get(Type *ElementType, unsigned NumElements,

llvm/include/llvm/IR/GetElementPtrTypeIterator.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ namespace llvm {
7575

7676
generic_gep_type_iterator& operator++() { // Preincrement
7777
Type *Ty = getIndexedType();
78-
if (auto *STy = dyn_cast<SequentialType>(Ty)) {
79-
CurTy = STy->getElementType();
80-
NumElements = STy->getNumElements();
78+
if (auto *ATy = dyn_cast<ArrayType>(Ty)) {
79+
CurTy = ATy->getElementType();
80+
NumElements = ATy->getNumElements();
81+
} else if (auto *VTy = dyn_cast<VectorType>(Ty)) {
82+
CurTy = VTy->getElementType();
83+
if (VTy->isScalable())
84+
NumElements = Unbounded;
85+
else
86+
NumElements = VTy->getNumElements();
8187
} else
8288
CurTy = dyn_cast<StructType>(Ty);
8389
++OpIt;

llvm/include/llvm/IR/Type.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ class Type {
110110
/// Float).
111111
Type * const *ContainedTys = nullptr;
112112

113-
static bool isSequentialType(TypeID TyID) {
114-
return TyID == ArrayTyID || TyID == VectorTyID;
115-
}
116-
117113
public:
118114
/// Print the current type.
119115
/// Omit the type details if \p NoDetails == true.
@@ -358,11 +354,6 @@ class Type {
358354
inline unsigned getStructNumElements() const;
359355
inline Type *getStructElementType(unsigned N) const;
360356

361-
inline Type *getSequentialElementType() const {
362-
assert(isSequentialType(getTypeID()) && "Not a sequential type!");
363-
return ContainedTys[0];
364-
}
365-
366357
inline uint64_t getArrayNumElements() const;
367358

368359
Type *getArrayElementType() const {

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,20 +1145,20 @@ static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1,
11451145
GEP1->getSourceElementType(), IntermediateIndices);
11461146
StructType *LastIndexedStruct = dyn_cast<StructType>(Ty);
11471147

1148-
if (isa<SequentialType>(Ty)) {
1148+
if (isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
11491149
// We know that:
11501150
// - both GEPs begin indexing from the exact same pointer;
11511151
// - the last indices in both GEPs are constants, indexing into a sequential
1152-
// type (array or pointer);
1152+
// type (array or vector);
11531153
// - both GEPs only index through arrays prior to that.
11541154
//
11551155
// Because array indices greater than the number of elements are valid in
11561156
// GEPs, unless we know the intermediate indices are identical between
11571157
// GEP1 and GEP2 we cannot guarantee that the last indexed arrays don't
11581158
// partially overlap. We also need to check that the loaded size matches
11591159
// the element size, otherwise we could still have overlap.
1160-
const uint64_t ElementSize =
1161-
DL.getTypeStoreSize(cast<SequentialType>(Ty)->getElementType());
1160+
Type *LastElementTy = GetElementPtrInst::getTypeAtIndex(Ty, (uint64_t)0);
1161+
const uint64_t ElementSize = DL.getTypeStoreSize(LastElementTy);
11621162
if (V1Size != ElementSize || V2Size != ElementSize)
11631163
return MayAlias;
11641164

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,18 @@ bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
463463

464464
if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
465465
isa<ConstantDataSequential>(C)) {
466-
Type *EltTy = C->getType()->getSequentialElementType();
467-
uint64_t EltSize = DL.getTypeAllocSize(EltTy);
468-
uint64_t Index = ByteOffset / EltSize;
469-
uint64_t Offset = ByteOffset - Index * EltSize;
470466
uint64_t NumElts;
471-
if (auto *AT = dyn_cast<ArrayType>(C->getType()))
467+
Type *EltTy;
468+
if (auto *AT = dyn_cast<ArrayType>(C->getType())) {
472469
NumElts = AT->getNumElements();
473-
else
470+
EltTy = AT->getElementType();
471+
} else {
474472
NumElts = C->getType()->getVectorNumElements();
473+
EltTy = C->getType()->getVectorElementType();
474+
}
475+
uint64_t EltSize = DL.getTypeAllocSize(EltTy);
476+
uint64_t Index = ByteOffset / EltSize;
477+
uint64_t Offset = ByteOffset - Index * EltSize;
475478

476479
for (; Index != NumElts; ++Index) {
477480
if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
@@ -936,11 +939,11 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
936939
// Only handle pointers to sized types, not pointers to functions.
937940
if (!Ty->isSized())
938941
return nullptr;
939-
} else if (auto *ATy = dyn_cast<SequentialType>(Ty)) {
940-
Ty = ATy->getElementType();
941942
} else {
942-
// We've reached some non-indexable type.
943-
break;
943+
Type *NextTy = GetElementPtrInst::getTypeAtIndex(Ty, (uint64_t)0);
944+
if (!NextTy)
945+
break;
946+
Ty = NextTy;
944947
}
945948

946949
// Determine which element of the array the offset points into.

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3555,7 +3555,7 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
35553555
CurTy = GEP->getSourceElementType();
35563556
FirstIter = false;
35573557
} else {
3558-
CurTy = cast<SequentialType>(CurTy)->getElementType();
3558+
CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0);
35593559
}
35603560
// For an array, add the element offset, explicitly scaled.
35613561
const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2505,7 +2505,11 @@ Error BitcodeReader::parseConstants() {
25052505
if (Record.empty())
25062506
return error("Invalid record");
25072507

2508-
Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
2508+
Type *EltTy;
2509+
if (auto *Array = dyn_cast<ArrayType>(CurTy))
2510+
EltTy = Array->getElementType();
2511+
else
2512+
EltTy = cast<VectorType>(CurTy)->getElementType();
25092513
if (EltTy->isIntegerTy(8)) {
25102514
SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
25112515
if (isa<VectorType>(CurTy))

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
24232423
} else if (const ConstantDataSequential *CDS =
24242424
dyn_cast<ConstantDataSequential>(C)) {
24252425
Code = bitc::CST_CODE_DATA;
2426-
Type *EltTy = CDS->getType()->getElementType();
2426+
Type *EltTy = CDS->getElementType();
24272427
if (isa<IntegerType>(EltTy)) {
24282428
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
24292429
Record.push_back(CDS->getElementAsInteger(i));

0 commit comments

Comments
 (0)