Skip to content

Commit e24e95f

Browse files
committed
Remove CompositeType class.
The existence of the class is more confusing than helpful, I think; the commonality is mostly just "GEP is legal", which can be queried using APIs on GetElementPtrInst. Differential Revision: https://reviews.llvm.org/D75660
1 parent c682a60 commit e24e95f

17 files changed

+126
-149
lines changed

clang/unittests/CodeGen/CodeGenExternalTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static void test_codegen_fns(MyASTConsumer *my) {
199199
dbgs() << "\n";
200200
}
201201

202-
llvm::CompositeType* structTy = dyn_cast<CompositeType>(llvmTy);
202+
auto* structTy = dyn_cast<llvm::StructType>(llvmTy);
203203
ASSERT_TRUE(structTy != NULL);
204204

205205
// Check getLLVMFieldNumber

llvm/include/llvm/IR/Constants.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class ConstantAggregateZero final : public ConstantData {
392392
/// use operands.
393393
class ConstantAggregate : public Constant {
394394
protected:
395-
ConstantAggregate(CompositeType *T, ValueTy VT, ArrayRef<Constant *> V);
395+
ConstantAggregate(Type *T, ValueTy VT, ArrayRef<Constant *> V);
396396

397397
public:
398398
/// Transparently provide more efficient getOperand methods.

llvm/include/llvm/IR/DerivedTypes.h

+10-25
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,6 @@ class FunctionCallee {
195195
Value *Callee = nullptr;
196196
};
197197

198-
/// Common super class of ArrayType, StructType and VectorType.
199-
class CompositeType : public Type {
200-
protected:
201-
explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {}
202-
203-
public:
204-
/// Given an index value into the type, return the type of the element.
205-
Type *getTypeAtIndex(const Value *V) const;
206-
Type *getTypeAtIndex(unsigned Idx) const;
207-
bool indexValid(const Value *V) const;
208-
bool indexValid(unsigned Idx) const;
209-
210-
/// Methods for support type inquiry through isa, cast, and dyn_cast.
211-
static bool classof(const Type *T) {
212-
return T->getTypeID() == ArrayTyID ||
213-
T->getTypeID() == StructTyID ||
214-
T->getTypeID() == VectorTyID;
215-
}
216-
};
217-
218198
/// Class to represent struct types. There are two different kinds of struct
219199
/// types: Literal structs and Identified structs.
220200
///
@@ -235,8 +215,8 @@ class CompositeType : public Type {
235215
/// elements as defined by DataLayout (which is required to match what the code
236216
/// generator for a target expects).
237217
///
238-
class StructType : public CompositeType {
239-
StructType(LLVMContext &C) : CompositeType(C, StructTyID) {}
218+
class StructType : public Type {
219+
StructType(LLVMContext &C) : Type(C, StructTyID) {}
240220

241221
enum {
242222
/// This is the contents of the SubClassData field.
@@ -350,6 +330,11 @@ class StructType : public CompositeType {
350330
assert(N < NumContainedTys && "Element number out of range!");
351331
return ContainedTys[N];
352332
}
333+
/// Given an index value into the type, return the type of the element.
334+
Type *getTypeAtIndex(const Value *V) const;
335+
Type *getTypeAtIndex(unsigned N) const { return getElementType(N); }
336+
bool indexValid(const Value *V) const;
337+
bool indexValid(unsigned Idx) const { return Idx < getNumElements(); }
353338

354339
/// Methods for support type inquiry through isa, cast, and dyn_cast.
355340
static bool classof(const Type *T) {
@@ -375,14 +360,14 @@ Type *Type::getStructElementType(unsigned N) const {
375360
/// for use of SIMD instructions. SequentialType holds the common features of
376361
/// both, which stem from the fact that both lay their components out in memory
377362
/// identically.
378-
class SequentialType : public CompositeType {
363+
class SequentialType : public Type {
379364
Type *ContainedType; ///< Storage for the single contained type.
380365
uint64_t NumElements;
381366

382367
protected:
383368
SequentialType(TypeID TID, Type *ElType, uint64_t NumElements)
384-
: CompositeType(ElType->getContext(), TID), ContainedType(ElType),
385-
NumElements(NumElements) {
369+
: Type(ElType->getContext(), TID), ContainedType(ElType),
370+
NumElements(NumElements) {
386371
ContainedTys = &ContainedType;
387372
NumContainedTys = 1;
388373
}

llvm/include/llvm/IR/Instructions.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,23 @@ class GetElementPtrInst : public Instruction {
10081008
return getPointerAddressSpace();
10091009
}
10101010

1011-
/// Returns the type of the element that would be loaded with
1012-
/// a load instruction with the specified parameters.
1011+
/// Returns the result type of a getelementptr with the given source
1012+
/// element type and indexes.
10131013
///
10141014
/// Null is returned if the indices are invalid for the specified
1015-
/// pointer type.
1016-
///
1015+
/// source element type.
10171016
static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
10181017
static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
10191018
static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
10201019

1020+
/// Return the type of the element at the given index of an indexable
1021+
/// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})".
1022+
///
1023+
/// Returns null if the type can't be indexed, or the given index is not
1024+
/// legal for the given type.
1025+
static Type *getTypeAtIndex(Type *Ty, Value *Idx);
1026+
static Type *getTypeAtIndex(Type *Ty, uint64_t Idx);
1027+
10211028
inline op_iterator idx_begin() { return op_begin()+1; }
10221029
inline const_op_iterator idx_begin() const { return op_begin()+1; }
10231030
inline op_iterator idx_end() { return op_end(); }

llvm/lib/CodeGen/Analysis.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,
395395

396396
/// For an aggregate type, determine whether a given index is within bounds or
397397
/// not.
398-
static bool indexReallyValid(CompositeType *T, unsigned Idx) {
398+
static bool indexReallyValid(Type *T, unsigned Idx) {
399399
if (ArrayType *AT = dyn_cast<ArrayType>(T))
400400
return Idx < AT->getNumElements();
401401

@@ -419,7 +419,7 @@ static bool indexReallyValid(CompositeType *T, unsigned Idx) {
419419
/// function again on a finished iterator will repeatedly return
420420
/// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty
421421
/// aggregate or a non-aggregate
422-
static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes,
422+
static bool advanceToNextLeafType(SmallVectorImpl<Type *> &SubTypes,
423423
SmallVectorImpl<unsigned> &Path) {
424424
// First march back up the tree until we can successfully increment one of the
425425
// coordinates in Path.
@@ -435,16 +435,16 @@ static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes,
435435
// We know there's *some* valid leaf now, so march back down the tree picking
436436
// out the left-most element at each node.
437437
++Path.back();
438-
Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back());
438+
Type *DeeperType =
439+
ExtractValueInst::getIndexedType(SubTypes.back(), Path.back());
439440
while (DeeperType->isAggregateType()) {
440-
CompositeType *CT = cast<CompositeType>(DeeperType);
441-
if (!indexReallyValid(CT, 0))
441+
if (!indexReallyValid(DeeperType, 0))
442442
return true;
443443

444-
SubTypes.push_back(CT);
444+
SubTypes.push_back(DeeperType);
445445
Path.push_back(0);
446446

447-
DeeperType = CT->getTypeAtIndex(0U);
447+
DeeperType = ExtractValueInst::getIndexedType(DeeperType, 0);
448448
}
449449

450450
return true;
@@ -460,17 +460,15 @@ static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes,
460460
/// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup
461461
/// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first
462462
/// i32 in that type.
463-
static bool firstRealType(Type *Next,
464-
SmallVectorImpl<CompositeType *> &SubTypes,
463+
static bool firstRealType(Type *Next, SmallVectorImpl<Type *> &SubTypes,
465464
SmallVectorImpl<unsigned> &Path) {
466465
// First initialise the iterator components to the first "leaf" node
467466
// (i.e. node with no valid sub-type at any index, so {} does count as a leaf
468467
// despite nominally being an aggregate).
469-
while (Next->isAggregateType() &&
470-
indexReallyValid(cast<CompositeType>(Next), 0)) {
471-
SubTypes.push_back(cast<CompositeType>(Next));
468+
while (Type *FirstInner = ExtractValueInst::getIndexedType(Next, 0)) {
469+
SubTypes.push_back(Next);
472470
Path.push_back(0);
473-
Next = cast<CompositeType>(Next)->getTypeAtIndex(0U);
471+
Next = FirstInner;
474472
}
475473

476474
// If there's no Path now, Next was originally scalar already (or empty
@@ -480,7 +478,8 @@ static bool firstRealType(Type *Next,
480478

481479
// Otherwise, use normal iteration to keep looking through the tree until we
482480
// find a non-aggregate type.
483-
while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()) {
481+
while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
482+
->isAggregateType()) {
484483
if (!advanceToNextLeafType(SubTypes, Path))
485484
return false;
486485
}
@@ -490,14 +489,15 @@ static bool firstRealType(Type *Next,
490489

491490
/// Set the iterator data-structures to the next non-empty, non-aggregate
492491
/// subtype.
493-
static bool nextRealType(SmallVectorImpl<CompositeType *> &SubTypes,
492+
static bool nextRealType(SmallVectorImpl<Type *> &SubTypes,
494493
SmallVectorImpl<unsigned> &Path) {
495494
do {
496495
if (!advanceToNextLeafType(SubTypes, Path))
497496
return false;
498497

499498
assert(!Path.empty() && "found a leaf but didn't set the path?");
500-
} while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType());
499+
} while (ExtractValueInst::getIndexedType(SubTypes.back(), Path.back())
500+
->isAggregateType());
501501

502502
return true;
503503
}
@@ -669,7 +669,7 @@ bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
669669
}
670670

671671
SmallVector<unsigned, 4> RetPath, CallPath;
672-
SmallVector<CompositeType *, 4> RetSubTypes, CallSubTypes;
672+
SmallVector<Type *, 4> RetSubTypes, CallSubTypes;
673673

674674
bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);
675675
bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);
@@ -692,7 +692,8 @@ bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
692692
// We've exhausted the values produced by the tail call instruction, the
693693
// rest are essentially undef. The type doesn't really matter, but we need
694694
// *something*.
695-
Type *SlotType = RetSubTypes.back()->getTypeAtIndex(RetPath.back());
695+
Type *SlotType =
696+
ExtractValueInst::getIndexedType(RetSubTypes.back(), RetPath.back());
696697
CallVal = UndefValue::get(SlotType);
697698
}
698699

llvm/lib/FuzzMutate/Operations.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,24 @@ static SourcePred matchScalarInAggregate() {
244244

245245
static SourcePred validInsertValueIndex() {
246246
auto Pred = [](ArrayRef<Value *> Cur, const Value *V) {
247-
auto *CTy = cast<CompositeType>(Cur[0]->getType());
248247
if (auto *CI = dyn_cast<ConstantInt>(V))
249-
if (CI->getBitWidth() == 32 &&
250-
CTy->getTypeAtIndex(CI->getZExtValue()) == Cur[1]->getType())
251-
return true;
248+
if (CI->getBitWidth() == 32) {
249+
Type *Indexed = ExtractValueInst::getIndexedType(Cur[0]->getType(),
250+
CI->getZExtValue());
251+
return Indexed == Cur[1]->getType();
252+
}
252253
return false;
253254
};
254255
auto Make = [](ArrayRef<Value *> Cur, ArrayRef<Type *> Ts) {
255256
std::vector<Constant *> Result;
256257
auto *Int32Ty = Type::getInt32Ty(Cur[0]->getContext());
257-
auto *CTy = cast<CompositeType>(Cur[0]->getType());
258-
for (int I = 0, E = getAggregateNumElements(CTy); I < E; ++I)
259-
if (CTy->getTypeAtIndex(I) == Cur[1]->getType())
258+
auto *BaseTy = Cur[0]->getType();
259+
int I = 0;
260+
while (Type *Indexed = ExtractValueInst::getIndexedType(BaseTy, I)) {
261+
if (Indexed == Cur[1]->getType())
260262
Result.push_back(ConstantInt::get(Int32Ty, I));
263+
++I;
264+
}
261265
return Result;
262266
};
263267
return {Pred, Make};

llvm/lib/IR/ConstantFold.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2389,10 +2389,11 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
23892389
SmallVector<Constant *, 8> NewIdxs;
23902390
Type *Ty = PointeeTy;
23912391
Type *Prev = C->getType();
2392+
auto GEPIter = gep_type_begin(PointeeTy, Idxs);
23922393
bool Unknown =
23932394
!isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
23942395
for (unsigned i = 1, e = Idxs.size(); i != e;
2395-
Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
2396+
Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) {
23962397
if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
23972398
// We don't know if it's in range or not.
23982399
Unknown = true;

llvm/lib/IR/Constants.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1047,19 +1047,20 @@ static Constant *getSequenceIfElementsMatch(Constant *C,
10471047
return nullptr;
10481048
}
10491049

1050-
ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
1050+
ConstantAggregate::ConstantAggregate(Type *T, ValueTy VT,
10511051
ArrayRef<Constant *> V)
10521052
: Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
10531053
V.size()) {
10541054
llvm::copy(V, op_begin());
10551055

10561056
// Check that types match, unless this is an opaque struct.
1057-
if (auto *ST = dyn_cast<StructType>(T))
1057+
if (auto *ST = dyn_cast<StructType>(T)) {
10581058
if (ST->isOpaque())
10591059
return;
1060-
for (unsigned I = 0, E = V.size(); I != E; ++I)
1061-
assert(V[I]->getType() == T->getTypeAtIndex(I) &&
1062-
"Initializer for composite element doesn't match!");
1060+
for (unsigned I = 0, E = V.size(); I != E; ++I)
1061+
assert(V[I]->getType() == ST->getTypeAtIndex(I) &&
1062+
"Initializer for struct element doesn't match!");
1063+
}
10631064
}
10641065

10651066
ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)

llvm/lib/IR/Instructions.cpp

+37-28
Original file line numberDiff line numberDiff line change
@@ -1659,35 +1659,44 @@ GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
16591659
SubclassOptionalData = GEPI.SubclassOptionalData;
16601660
}
16611661

1662-
/// getIndexedType - Returns the type of the element that would be accessed with
1663-
/// a gep instruction with the specified parameters.
1664-
///
1665-
/// The Idxs pointer should point to a continuous piece of memory containing the
1666-
/// indices, either as Value* or uint64_t.
1667-
///
1668-
/// A null type is returned if the indices are invalid for the specified
1669-
/// pointer type.
1670-
///
1671-
template <typename IndexTy>
1672-
static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
1673-
// Handle the special case of the empty set index set, which is always valid.
1674-
if (IdxList.empty())
1675-
return Agg;
1676-
1677-
// If there is at least one index, the top level type must be sized, otherwise
1678-
// it cannot be 'stepped over'.
1679-
if (!Agg->isSized())
1662+
Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, Value *Idx) {
1663+
if (auto Struct = dyn_cast<StructType>(Ty)) {
1664+
if (!Struct->indexValid(Idx))
1665+
return nullptr;
1666+
return Struct->getTypeAtIndex(Idx);
1667+
}
1668+
if (!Idx->getType()->isIntOrIntVectorTy())
16801669
return nullptr;
1670+
if (auto Array = dyn_cast<ArrayType>(Ty))
1671+
return Array->getElementType();
1672+
if (auto Vector = dyn_cast<VectorType>(Ty))
1673+
return Vector->getElementType();
1674+
return nullptr;
1675+
}
16811676

1682-
unsigned CurIdx = 1;
1683-
for (; CurIdx != IdxList.size(); ++CurIdx) {
1684-
CompositeType *CT = dyn_cast<CompositeType>(Agg);
1685-
if (!CT || CT->isPointerTy()) return nullptr;
1686-
IndexTy Index = IdxList[CurIdx];
1687-
if (!CT->indexValid(Index)) return nullptr;
1688-
Agg = CT->getTypeAtIndex(Index);
1677+
Type *GetElementPtrInst::getTypeAtIndex(Type *Ty, uint64_t Idx) {
1678+
if (auto Struct = dyn_cast<StructType>(Ty)) {
1679+
if (Idx >= Struct->getNumElements())
1680+
return nullptr;
1681+
return Struct->getElementType(Idx);
16891682
}
1690-
return CurIdx == IdxList.size() ? Agg : nullptr;
1683+
if (auto Array = dyn_cast<ArrayType>(Ty))
1684+
return Array->getElementType();
1685+
if (auto Vector = dyn_cast<VectorType>(Ty))
1686+
return Vector->getElementType();
1687+
return nullptr;
1688+
}
1689+
1690+
template <typename IndexTy>
1691+
static Type *getIndexedTypeInternal(Type *Ty, ArrayRef<IndexTy> IdxList) {
1692+
if (IdxList.empty())
1693+
return Ty;
1694+
for (IndexTy V : IdxList.slice(1)) {
1695+
Ty = GetElementPtrInst::getTypeAtIndex(Ty, V);
1696+
if (!Ty)
1697+
return Ty;
1698+
}
1699+
return Ty;
16911700
}
16921701

16931702
Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
@@ -2220,15 +2229,15 @@ Type *ExtractValueInst::getIndexedType(Type *Agg,
22202229
if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
22212230
if (Index >= AT->getNumElements())
22222231
return nullptr;
2232+
Agg = AT->getElementType();
22232233
} else if (StructType *ST = dyn_cast<StructType>(Agg)) {
22242234
if (Index >= ST->getNumElements())
22252235
return nullptr;
2236+
Agg = ST->getElementType(Index);
22262237
} else {
22272238
// Not a valid type to index into.
22282239
return nullptr;
22292240
}
2230-
2231-
Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
22322241
}
22332242
return const_cast<Type*>(Agg);
22342243
}

0 commit comments

Comments
 (0)