[llvm] [IR] Use `const Type*` in DataLayout (PR #70009)

Jannik Silvanus via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 24 00:34:04 PDT 2023


https://github.com/jasilvanus created https://github.com/llvm/llvm-project/pull/70009

DataLayout functions that are used to query infos about types used to work on `Type*`.
Change these to use `const Type*` instead.

This is an easy mechanical change that does not need any follup changes elsewhere.

Most of LLVM does not care a lot about constness of Types, but requiring non-const `Type*` in DL forces everyone to use non-const `Type*`, or add ugly `const_cast`s.

>From 00e12f4becbea051b2a4c14380c5908fca21052c Mon Sep 17 00:00:00 2001
From: Jannik Silvanus <jannik.silvanus at amd.com>
Date: Tue, 24 Oct 2023 08:59:49 +0200
Subject: [PATCH] [IR] Use const Type* in DataLayout

DataLayout functions that are used to query infos
about types used to work on Type*.
Change these to use const Type* instead.

This is an easy mechanical change that does not need
any follup changes elsewhere.

Most of LLVM does not care a lot about constness of Types,
but requiring non-const Type* in DL forces everyone to use
non-const Type*, or add ugly const_casts.
---
 llvm/include/llvm/IR/DataLayout.h | 46 +++++++++++++++----------------
 llvm/lib/IR/DataLayout.cpp        | 28 +++++++++----------
 2 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index b3633b67b9debda..30311f3ff46a3c3 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -179,7 +179,7 @@ class DataLayout {
   Align getIntegerAlignment(uint32_t BitWidth, bool abi_or_pref) const;
 
   /// Internal helper method that returns requested alignment for type.
-  Align getAlignment(Type *Ty, bool abi_or_pref) const;
+  Align getAlignment(const Type *Ty, bool abi_or_pref) const;
 
   /// Attempts to parse a target data specification string and reports an error
   /// if the string is malformed.
@@ -395,11 +395,11 @@ class DataLayout {
     return is_contained(NonIntegralSpaces, AddrSpace);
   }
 
-  bool isNonIntegralPointerType(PointerType *PT) const {
+  bool isNonIntegralPointerType(const PointerType *PT) const {
     return isNonIntegralAddressSpace(PT->getAddressSpace());
   }
 
-  bool isNonIntegralPointerType(Type *Ty) const {
+  bool isNonIntegralPointerType(const Type *Ty) const {
     auto *PTy = dyn_cast<PointerType>(Ty);
     return PTy && isNonIntegralPointerType(PTy);
   }
@@ -426,13 +426,13 @@ class DataLayout {
   /// If this function is called with a vector of pointers, then the type size
   /// of the pointer is returned.  This should only be called with a pointer or
   /// vector of pointers.
-  unsigned getPointerTypeSizeInBits(Type *) const;
+  unsigned getPointerTypeSizeInBits(const Type *) const;
 
   /// Layout size of the index used in GEP calculation.
   /// The function should be called with pointer or vector of pointers type.
-  unsigned getIndexTypeSizeInBits(Type *Ty) const;
+  unsigned getIndexTypeSizeInBits(const Type *Ty) const;
 
-  unsigned getPointerTypeSize(Type *Ty) const {
+  unsigned getPointerTypeSize(const Type *Ty) const {
     return getPointerTypeSizeInBits(Ty) / 8;
   }
 
@@ -460,7 +460,7 @@ class DataLayout {
   ///
   /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
   /// have a size (Type::isSized() must return true).
-  TypeSize getTypeSizeInBits(Type *Ty) const;
+  TypeSize getTypeSizeInBits(const Type *Ty) const;
 
   /// Returns the maximum number of bytes that may be overwritten by
   /// storing the specified type.
@@ -469,7 +469,7 @@ class DataLayout {
   /// the runtime size will be a positive integer multiple of the base size.
   ///
   /// For example, returns 5 for i36 and 10 for x86_fp80.
-  TypeSize getTypeStoreSize(Type *Ty) const {
+  TypeSize getTypeStoreSize(const Type *Ty) const {
     TypeSize BaseSize = getTypeSizeInBits(Ty);
     return {divideCeil(BaseSize.getKnownMinValue(), 8), BaseSize.isScalable()};
   }
@@ -481,7 +481,7 @@ class DataLayout {
   /// the runtime size will be a positive integer multiple of the base size.
   ///
   /// For example, returns 40 for i36 and 80 for x86_fp80.
-  TypeSize getTypeStoreSizeInBits(Type *Ty) const {
+  TypeSize getTypeStoreSizeInBits(const Type *Ty) const {
     return 8 * getTypeStoreSize(Ty);
   }
 
@@ -489,7 +489,7 @@ class DataLayout {
   /// specified type.
   ///
   /// For example, returns false for i19 that has a 24-bit store size.
-  bool typeSizeEqualsStoreSize(Type *Ty) const {
+  bool typeSizeEqualsStoreSize(const Type *Ty) const {
     return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty);
   }
 
@@ -501,7 +501,7 @@ class DataLayout {
   ///
   /// This is the amount that alloca reserves for this type. For example,
   /// returns 12 or 16 for x86_fp80, depending on alignment.
-  TypeSize getTypeAllocSize(Type *Ty) const {
+  TypeSize getTypeAllocSize(const Type *Ty) const {
     // Round up to the next alignment boundary.
     return alignTo(getTypeStoreSize(Ty), getABITypeAlign(Ty).value());
   }
@@ -514,17 +514,17 @@ class DataLayout {
   ///
   /// This is the amount that alloca reserves for this type. For example,
   /// returns 96 or 128 for x86_fp80, depending on alignment.
-  TypeSize getTypeAllocSizeInBits(Type *Ty) const {
+  TypeSize getTypeAllocSizeInBits(const Type *Ty) const {
     return 8 * getTypeAllocSize(Ty);
   }
 
   /// Returns the minimum ABI-required alignment for the specified type.
-  Align getABITypeAlign(Type *Ty) const;
+  Align getABITypeAlign(const Type *Ty) const;
 
   /// Helper function to return `Alignment` if it's set or the result of
   /// `getABITypeAlign(Ty)`, in any case the result is a valid alignment.
   inline Align getValueOrABITypeAlignment(MaybeAlign Alignment,
-                                          Type *Ty) const {
+                                          const Type *Ty) const {
     return Alignment ? *Alignment : getABITypeAlign(Ty);
   }
 
@@ -540,13 +540,13 @@ class DataLayout {
   /// This is always at least as good as the ABI alignment.
   /// FIXME: Deprecate this function once migration to Align is over.
   LLVM_DEPRECATED("use getPrefTypeAlign instead", "getPrefTypeAlign")
-  uint64_t getPrefTypeAlignment(Type *Ty) const;
+  uint64_t getPrefTypeAlignment(const Type *Ty) const;
 
   /// Returns the preferred stack/global alignment for the specified
   /// type.
   ///
   /// This is always at least as good as the ABI alignment.
-  Align getPrefTypeAlign(Type *Ty) const;
+  Align getPrefTypeAlign(const Type *Ty) const;
 
   /// Returns an integer type with size at least as big as that of a
   /// pointer in the given address space.
@@ -554,7 +554,7 @@ class DataLayout {
 
   /// Returns an integer (vector of integer) type with size at least as
   /// big as that of a pointer of the given pointer (vector of pointer) type.
-  Type *getIntPtrType(Type *) const;
+  Type *getIntPtrType(const Type *) const;
 
   /// Returns the smallest integer type with size at least as big as
   /// Width bits.
@@ -578,7 +578,7 @@ class DataLayout {
   /// Returns the type of a GEP index.
   /// If it was not specified explicitly, it will be the integer type of the
   /// pointer width - IntPtrType.
-  Type *getIndexType(Type *PtrTy) const;
+  Type *getIndexType(const Type *PtrTy) const;
 
   /// Returns the offset from the beginning of the type for the specified
   /// indices.
@@ -601,7 +601,7 @@ class DataLayout {
   /// struct, its size, and the offsets of its fields.
   ///
   /// Note that this information is lazily cached.
-  const StructLayout *getStructLayout(StructType *Ty) const;
+  const StructLayout *getStructLayout(const StructType *Ty) const;
 
   /// Returns the preferred alignment of the specified global.
   ///
@@ -660,7 +660,7 @@ class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
 private:
   friend class DataLayout; // Only DataLayout can create this class
 
-  StructLayout(StructType *ST, const DataLayout &DL);
+  StructLayout(const StructType *ST, const DataLayout &DL);
 
   size_t numTrailingObjects(OverloadToken<TypeSize>) const {
     return NumElements;
@@ -669,7 +669,7 @@ class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
 
 // The implementation of this method is provided inline as it is particularly
 // well suited to constant folding when called on a specific Type subclass.
-inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
+inline TypeSize DataLayout::getTypeSizeInBits(const Type *Ty) const {
   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
   switch (Ty->getTypeID()) {
   case Type::LabelTyID:
@@ -677,7 +677,7 @@ inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
   case Type::PointerTyID:
     return TypeSize::Fixed(getPointerSizeInBits(Ty->getPointerAddressSpace()));
   case Type::ArrayTyID: {
-    ArrayType *ATy = cast<ArrayType>(Ty);
+    const ArrayType *ATy = cast<ArrayType>(Ty);
     return ATy->getNumElements() *
            getTypeAllocSizeInBits(ATy->getElementType());
   }
@@ -705,7 +705,7 @@ inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
     return TypeSize::Fixed(80);
   case Type::FixedVectorTyID:
   case Type::ScalableVectorTyID: {
-    VectorType *VTy = cast<VectorType>(Ty);
+    const VectorType *VTy = cast<VectorType>(Ty);
     auto EltCnt = VTy->getElementCount();
     uint64_t MinBits = EltCnt.getKnownMinValue() *
                        getTypeSizeInBits(VTy->getElementType()).getFixedValue();
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index d324c4b488f7294..fb26897e01b6614 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -45,7 +45,7 @@ using namespace llvm;
 // Support for StructLayout
 //===----------------------------------------------------------------------===//
 
-StructLayout::StructLayout(StructType *ST, const DataLayout &DL)
+StructLayout::StructLayout(const StructType *ST, const DataLayout &DL)
     : StructSize(TypeSize::Fixed(0)) {
   assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
   IsPadded = false;
@@ -681,7 +681,7 @@ Align DataLayout::getIntegerAlignment(uint32_t BitWidth,
 namespace {
 
 class StructLayoutMap {
-  using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
+  using LayoutInfoTy = DenseMap<const StructType*, StructLayout*>;
   LayoutInfoTy LayoutInfo;
 
 public:
@@ -694,7 +694,7 @@ class StructLayoutMap {
     }
   }
 
-  StructLayout *&operator[](StructType *STy) {
+  StructLayout *&operator[](const StructType *STy) {
     return LayoutInfo[STy];
   }
 };
@@ -715,7 +715,7 @@ DataLayout::~DataLayout() {
   clear();
 }
 
-const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
+const StructLayout *DataLayout::getStructLayout(const StructType *Ty) const {
   if (!LayoutMap)
     LayoutMap = new StructLayoutMap();
 
@@ -758,7 +758,7 @@ unsigned DataLayout::getMaxIndexSize() const {
   return MaxIndexSize;
 }
 
-unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
+unsigned DataLayout::getPointerTypeSizeInBits(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "This should only be called with a pointer or pointer vector type");
   Ty = Ty->getScalarType();
@@ -769,7 +769,7 @@ unsigned DataLayout::getIndexSize(unsigned AS) const {
   return divideCeil(getPointerAlignElem(AS).IndexBitWidth, 8);
 }
 
-unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
+unsigned DataLayout::getIndexTypeSizeInBits(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "This should only be called with a pointer or pointer vector type");
   Ty = Ty->getScalarType();
@@ -784,7 +784,7 @@ unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
   == false) for the requested type \a Ty.
  */
-Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
+Align DataLayout::getAlignment(const Type *Ty, bool abi_or_pref) const {
   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
   switch (Ty->getTypeID()) {
   // Early escape for the non-numeric types.
@@ -860,16 +860,16 @@ Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
   }
 }
 
-Align DataLayout::getABITypeAlign(Type *Ty) const {
+Align DataLayout::getABITypeAlign(const Type *Ty) const {
   return getAlignment(Ty, true);
 }
 
 /// TODO: Remove this function once the transition to Align is over.
-uint64_t DataLayout::getPrefTypeAlignment(Type *Ty) const {
+uint64_t DataLayout::getPrefTypeAlignment(const Type *Ty) const {
   return getPrefTypeAlign(Ty).value();
 }
 
-Align DataLayout::getPrefTypeAlign(Type *Ty) const {
+Align DataLayout::getPrefTypeAlign(const Type *Ty) const {
   return getAlignment(Ty, false);
 }
 
@@ -878,12 +878,12 @@ IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
   return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
 }
 
-Type *DataLayout::getIntPtrType(Type *Ty) const {
+Type *DataLayout::getIntPtrType(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "Expected a pointer or pointer vector type.");
   unsigned NumBits = getPointerTypeSizeInBits(Ty);
   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
-  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
+  if (const VectorType *VecTy = dyn_cast<VectorType>(Ty))
     return VectorType::get(IntTy, VecTy);
   return IntTy;
 }
@@ -905,12 +905,12 @@ IntegerType *DataLayout::getIndexType(LLVMContext &C,
   return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
 }
 
-Type *DataLayout::getIndexType(Type *Ty) const {
+Type *DataLayout::getIndexType(const Type *Ty) const {
   assert(Ty->isPtrOrPtrVectorTy() &&
          "Expected a pointer or pointer vector type.");
   unsigned NumBits = getIndexTypeSizeInBits(Ty);
   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
-  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
+  if (const VectorType *VecTy = dyn_cast<VectorType>(Ty))
     return VectorType::get(IntTy, VecTy);
   return IntTy;
 }



More information about the llvm-commits mailing list