[PATCH] D99660: Use DL.getIndexType() in Value::getPointerAlignment()

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 07:54:23 PDT 2022


arichardson updated this revision to Diff 465013.
arichardson added a comment.
Herald added a project: All.

Use new APIs


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99660/new/

https://reviews.llvm.org/D99660

Files:
  llvm/include/llvm/IR/DataLayout.h
  llvm/lib/IR/DataLayout.cpp
  llvm/lib/IR/Value.cpp


Index: llvm/lib/IR/Value.cpp
===================================================================
--- llvm/lib/IR/Value.cpp
+++ llvm/lib/IR/Value.cpp
@@ -970,7 +970,7 @@
     // if the only "reduction" is combining a bitcast + ptrtoint.
     CstPtr = CstPtr->stripPointerCasts();
     if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
-            const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
+            const_cast<Constant *>(CstPtr), DL.getPtrToIntResultType(getType()),
             /*OnlyIfReduced=*/true))) {
       size_t TrailingZeros = CstInt->getValue().countTrailingZeros();
       // While the actual alignment may be large, elsewhere we have
Index: llvm/lib/IR/DataLayout.cpp
===================================================================
--- llvm/lib/IR/DataLayout.cpp
+++ llvm/lib/IR/DataLayout.cpp
@@ -876,6 +876,22 @@
   return IntTy;
 }
 
+IntegerType *DataLayout::getPtrToIntResultType(LLVMContext &C,
+                                               unsigned AddressSpace) const {
+  return IntegerType::get(C, getPointerIntegralSizeInBits(AddressSpace));
+}
+
+Type *DataLayout::getPtrToIntResultType(Type *Ty) const {
+  assert(Ty->isPtrOrPtrVectorTy() &&
+         "Expected a pointer or pointer vector type.");
+  unsigned NumBits =
+      getPointerIntegralSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
+  IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
+  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
+    return VectorType::get(IntTy, VecTy);
+  return IntTy;
+}
+
 Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
   for (unsigned LegalIntWidth : LegalIntWidths)
     if (Width <= LegalIntWidth)
Index: llvm/include/llvm/IR/DataLayout.h
===================================================================
--- llvm/include/llvm/IR/DataLayout.h
+++ llvm/include/llvm/IR/DataLayout.h
@@ -582,6 +582,19 @@
   /// big as that of a pointer of the given pointer (vector of pointer) type.
   Type *getIntPtrType(Type *) const;
 
+
+  /// Returns an integer type with size equal to the integral range of a pointer
+  /// in the given address space. This can be used to obtain an integer that
+  /// can be used for the type of a ptrtoint instruction.
+  IntegerType *getPtrToIntResultType(LLVMContext &C,
+                                     unsigned AddressSpace) const;
+
+  /// Returns an integer (or vector of integer) type with size at least as
+  /// big as that of the pointer range for the given pointer (or vector of
+  /// pointers). This can be used to obtain an integer (or vector of integer)
+  /// that can be used for the type of a ptrtoint instruction.
+  Type *getPtrToIntResultType(Type *) const;
+
   /// Returns the smallest integer type with size at least as big as
   /// Width bits.
   Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99660.465013.patch
Type: text/x-patch
Size: 2912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221004/75d820dc/attachment.bin>


More information about the llvm-commits mailing list