From c145181f119c62741b601c37e4ac845b558437ea Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Mon, 21 Aug 2023 19:00:57 +0100 Subject: [PATCH] [LLVM][SelectionDAG] Reduce number of ComputeValueVTs variants. --- llvm/include/llvm/CodeGen/Analysis.h | 35 ++++++------- llvm/include/llvm/Support/TypeSize.h | 1 + llvm/lib/CodeGen/Analysis.cpp | 49 +++---------------- .../SelectionDAG/SelectionDAGBuilder.cpp | 4 +- .../InstCombineLoadStoreAlloca.cpp | 4 +- llvm/unittests/Support/TypeSizeTest.cpp | 3 ++ 6 files changed, 29 insertions(+), 67 deletions(-) diff --git a/llvm/include/llvm/CodeGen/Analysis.h b/llvm/include/llvm/CodeGen/Analysis.h index 1c67fe2d003d9..6f7ed22b8ac71 100644 --- a/llvm/include/llvm/CodeGen/Analysis.h +++ b/llvm/include/llvm/CodeGen/Analysis.h @@ -62,36 +62,31 @@ inline unsigned ComputeLinearIndex(Type *Ty, /// If Offsets is non-null, it points to a vector to be filled in /// with the in-memory offsets of each of the individual values. /// -void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, - SmallVectorImpl &ValueVTs, - SmallVectorImpl *Offsets, - TypeSize StartingOffset); -void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, - SmallVectorImpl &ValueVTs, - SmallVectorImpl *Offsets = nullptr, - uint64_t StartingOffset = 0); -void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, - SmallVectorImpl &ValueVTs, - SmallVectorImpl *FixedOffsets, - uint64_t StartingOffset); - -/// Variant of ComputeValueVTs that also produces the memory VTs. -void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, - SmallVectorImpl &ValueVTs, - SmallVectorImpl *MemVTs, - SmallVectorImpl *Offsets, - TypeSize StartingOffset); void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl &ValueVTs, SmallVectorImpl *MemVTs, SmallVectorImpl *Offsets = nullptr, - uint64_t StartingOffset = 0); + TypeSize StartingOffset = TypeSize::getZero()); void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl &ValueVTs, SmallVectorImpl *MemVTs, SmallVectorImpl *FixedOffsets, uint64_t StartingOffset); +/// Variant of ComputeValueVTs that don't produce memory VTs. +inline void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, + Type *Ty, SmallVectorImpl &ValueVTs, + SmallVectorImpl *Offsets = nullptr, + TypeSize StartingOffset = TypeSize::getZero()) { + ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, Offsets, StartingOffset); +} +inline void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, + Type *Ty, SmallVectorImpl &ValueVTs, + SmallVectorImpl *FixedOffsets, + uint64_t StartingOffset) { + ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, FixedOffsets, StartingOffset); +} + /// computeValueLLTs - Given an LLVM IR type, compute a sequence of /// LLTs that represent all the individual underlying /// non-aggregate types that comprise it. diff --git a/llvm/include/llvm/Support/TypeSize.h b/llvm/include/llvm/Support/TypeSize.h index b00ebf9e8c454..1b793b0eccf3c 100644 --- a/llvm/include/llvm/Support/TypeSize.h +++ b/llvm/include/llvm/Support/TypeSize.h @@ -335,6 +335,7 @@ class TypeSize : public details::FixedOrScalableQuantity { static constexpr TypeSize getScalable(ScalarTy MinimumSize) { return TypeSize(MinimumSize, true); } + static constexpr TypeSize getZero() { return TypeSize(0, false); } // All code for this class below this point is needed because of the // temporary implicit conversion to uint64_t. The operator overloads are diff --git a/llvm/lib/CodeGen/Analysis.cpp b/llvm/lib/CodeGen/Analysis.cpp index 1994e6aec84b2..af7643d93591f 100644 --- a/llvm/lib/CodeGen/Analysis.cpp +++ b/llvm/lib/CodeGen/Analysis.cpp @@ -81,6 +81,9 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, SmallVectorImpl *MemVTs, SmallVectorImpl *Offsets, TypeSize StartingOffset) { + assert((Ty->isScalableTy() == StartingOffset.isScalable() || + StartingOffset.isZero()) && + "Offset/TypeSize mismatch!"); // Given a struct type, recursively traverse the elements. if (StructType *STy = dyn_cast(Ty)) { // If the Offsets aren't needed, don't query the struct layout. This allows @@ -92,8 +95,8 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, EE = STy->element_end(); EI != EE; ++EI) { // Don't compute the element offset if we didn't get a StructLayout above. - TypeSize EltOffset = SL ? SL->getElementOffset(EI - EB) - : TypeSize::get(0, StartingOffset.isScalable()); + TypeSize EltOffset = + SL ? SL->getElementOffset(EI - EB) : TypeSize::getZero(); ComputeValueVTs(TLI, DL, *EI, ValueVTs, MemVTs, Offsets, StartingOffset + EltOffset); } @@ -119,52 +122,12 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Offsets->push_back(StartingOffset); } -void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, - Type *Ty, SmallVectorImpl &ValueVTs, - SmallVectorImpl *Offsets, - TypeSize StartingOffset) { - return ComputeValueVTs(TLI, DL, Ty, ValueVTs, /*MemVTs=*/nullptr, Offsets, - StartingOffset); -} - -void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, - Type *Ty, SmallVectorImpl &ValueVTs, - SmallVectorImpl *Offsets, - uint64_t StartingOffset) { - TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy()); - return ComputeValueVTs(TLI, DL, Ty, ValueVTs, Offsets, Offset); -} - -void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, - Type *Ty, SmallVectorImpl &ValueVTs, - SmallVectorImpl *FixedOffsets, - uint64_t StartingOffset) { - TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy()); - if (FixedOffsets) { - SmallVector Offsets; - ComputeValueVTs(TLI, DL, Ty, ValueVTs, &Offsets, Offset); - for (TypeSize Offset : Offsets) - FixedOffsets->push_back(Offset.getFixedValue()); - } else { - ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, Offset); - } -} - -void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, - Type *Ty, SmallVectorImpl &ValueVTs, - SmallVectorImpl *MemVTs, - SmallVectorImpl *Offsets, - uint64_t StartingOffset) { - TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy()); - return ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, Offsets, Offset); -} - void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl &ValueVTs, SmallVectorImpl *MemVTs, SmallVectorImpl *FixedOffsets, uint64_t StartingOffset) { - TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy()); + TypeSize Offset = TypeSize::getFixed(StartingOffset); if (FixedOffsets) { SmallVector Offsets; ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, &Offsets, Offset); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 5ce1013f30fd1..fdeb4beaa95d9 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -4331,7 +4331,7 @@ void SelectionDAGBuilder::visitLoad(const LoadInst &I) { Type *Ty = I.getType(); SmallVector ValueVTs, MemVTs; SmallVector Offsets; - ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets, 0); + ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets); unsigned NumValues = ValueVTs.size(); if (NumValues == 0) return; @@ -4499,7 +4499,7 @@ void SelectionDAGBuilder::visitStore(const StoreInst &I) { SmallVector ValueVTs, MemVTs; SmallVector Offsets; ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), - SrcV->getType(), ValueVTs, &MemVTs, &Offsets, 0); + SrcV->getType(), ValueVTs, &MemVTs, &Offsets); unsigned NumValues = ValueVTs.size(); if (NumValues == 0) return; diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index 1254a050027a4..a222889842f54 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -777,7 +777,7 @@ static Instruction *unpackLoadToAggregate(InstCombinerImpl &IC, LoadInst &LI) { auto *Zero = ConstantInt::get(IdxType, 0); Value *V = PoisonValue::get(T); - TypeSize Offset = TypeSize::get(0, ET->isScalableTy()); + TypeSize Offset = TypeSize::getZero(); for (uint64_t i = 0; i < NumElements; i++) { Value *Indices[2] = { Zero, @@ -1303,7 +1303,7 @@ static bool unpackStoreToAggregate(InstCombinerImpl &IC, StoreInst &SI) { auto *IdxType = Type::getInt64Ty(T->getContext()); auto *Zero = ConstantInt::get(IdxType, 0); - TypeSize Offset = TypeSize::get(0, AT->getElementType()->isScalableTy()); + TypeSize Offset = TypeSize::getZero(); for (uint64_t i = 0; i < NumElements; i++) { Value *Indices[2] = { Zero, diff --git a/llvm/unittests/Support/TypeSizeTest.cpp b/llvm/unittests/Support/TypeSizeTest.cpp index 503dc5d99b182..34fe376989e7b 100644 --- a/llvm/unittests/Support/TypeSizeTest.cpp +++ b/llvm/unittests/Support/TypeSizeTest.cpp @@ -82,9 +82,12 @@ static_assert(UINT64_C(2) * TSFixed32 == TypeSize::getFixed(64)); static_assert(alignTo(TypeSize::getFixed(7), 8) == TypeSize::getFixed(8)); static_assert(TypeSize() == TypeSize::getFixed(0)); +static_assert(TypeSize::getZero() == TypeSize::getFixed(0)); +static_assert(TypeSize::getZero() != TypeSize::getScalable(0)); static_assert(TypeSize::getFixed(0) != TypeSize::getScalable(0)); static_assert(TypeSize::getFixed(0).isZero()); static_assert(TypeSize::getScalable(0).isZero()); +static_assert(TypeSize::getZero().isZero()); static_assert(TypeSize::getFixed(0) == (TypeSize::getFixed(4) - TypeSize::getFixed(4))); static_assert(TypeSize::getScalable(0) ==