aeubanks created this revision.
aeubanks added reviewers: dblaikie, rsmith.
aeubanks published this revision for review.
aeubanks added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is definitely WIP, but I'd like to make sure that this is the right 
direction before continuing. And are there any other obvious places that also 
require this?


With pointee types going away in llvm::PointerType, the frontend needs
to keep track of pointee types.

Add a PointeeType to Address and LValue.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103465

Files:
  clang/lib/CodeGen/Address.h
  clang/lib/CodeGen/CGBlocks.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGValue.h

Index: clang/lib/CodeGen/CGValue.h
===================================================================
--- clang/lib/CodeGen/CGValue.h
+++ clang/lib/CodeGen/CGValue.h
@@ -175,6 +175,7 @@
   } LVType;
 
   llvm::Value *V;
+  llvm::Type *PointeeType;
 
   union {
     // Index into a vector subscript: V[i]
@@ -327,7 +328,7 @@
     return V;
   }
   Address getAddress(CodeGenFunction &CGF) const {
-    return Address(getPointer(CGF), getAlignment());
+    return Address(getPointer(CGF), PointeeType, getAlignment());
   }
   void setAddress(Address address) {
     assert(isSimple());
@@ -395,6 +396,7 @@
     R.LVType = Simple;
     assert(address.getPointer()->getType()->isPointerTy());
     R.V = address.getPointer();
+    R.PointeeType = address.getElementType();
     R.Initialize(type, qs, address.getAlignment(), BaseInfo, TBAAInfo);
     return R;
   }
@@ -405,6 +407,7 @@
     LValue R;
     R.LVType = VectorElt;
     R.V = vecAddress.getPointer();
+    R.PointeeType = vecAddress.getElementType();
     R.VectorIdx = Idx;
     R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
                  BaseInfo, TBAAInfo);
@@ -417,6 +420,7 @@
     LValue R;
     R.LVType = ExtVectorElt;
     R.V = vecAddress.getPointer();
+    R.PointeeType = vecAddress.getElementType();
     R.VectorElts = Elts;
     R.Initialize(type, type.getQualifiers(), vecAddress.getAlignment(),
                  BaseInfo, TBAAInfo);
@@ -435,6 +439,7 @@
     LValue R;
     R.LVType = BitField;
     R.V = Addr.getPointer();
+    R.PointeeType = Addr.getElementType();
     R.BitFieldInfo = &Info;
     R.Initialize(type, type.getQualifiers(), Addr.getAlignment(), BaseInfo,
                  TBAAInfo);
@@ -445,6 +450,7 @@
     LValue R;
     R.LVType = GlobalReg;
     R.V = Reg.getPointer();
+    R.PointeeType = Reg.getElementType();
     R.Initialize(type, type.getQualifiers(), Reg.getAlignment(),
                  LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
     return R;
@@ -456,6 +462,7 @@
     LValue R;
     R.LVType = MatrixElt;
     R.V = matAddress.getPointer();
+    R.PointeeType = matAddress.getElementType();
     R.VectorIdx = Idx;
     R.Initialize(type, type.getQualifiers(), matAddress.getAlignment(),
                  BaseInfo, TBAAInfo);
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -70,7 +70,7 @@
                                                      llvm::Value *ArraySize) {
   auto Alloca = CreateTempAlloca(Ty, Name, ArraySize);
   Alloca->setAlignment(Align.getAsAlign());
-  return Address(Alloca, Align);
+  return Address(Alloca, Ty, Align);
 }
 
 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
@@ -100,7 +100,7 @@
         Ty->getPointerTo(DestAddrSpace), /*non-null*/ true);
   }
 
-  return Address(V, Align);
+  return Address(V, Ty, Align);
 }
 
 /// CreateTempAlloca - This creates an alloca and inserts it into the entry
@@ -156,7 +156,7 @@
                                     /*ArraySize=*/nullptr, Alloca);
 
   if (Ty->isConstantMatrixType()) {
-    auto *ArrayTy = cast<llvm::ArrayType>(Result.getType()->getElementType());
+    auto *ArrayTy = cast<llvm::ArrayType>(Result.getElementType());
     auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
                                                 ArrayTy->getNumElements());
 
@@ -1790,8 +1790,7 @@
 // MatrixType), if it points to a array (the memory type of MatrixType).
 static Address MaybeConvertMatrixAddress(Address Addr, CodeGenFunction &CGF,
                                          bool IsVector = true) {
-  auto *ArrayTy = dyn_cast<llvm::ArrayType>(
-      cast<llvm::PointerType>(Addr.getPointer()->getType())->getElementType());
+  auto *ArrayTy = dyn_cast<llvm::ArrayType>(Addr.getElementType());
   if (ArrayTy && IsVector) {
     auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
                                                 ArrayTy->getNumElements());
@@ -1799,7 +1798,7 @@
     return Address(CGF.Builder.CreateElementBitCast(Addr, VectorTy));
   }
   auto *VectorTy = dyn_cast<llvm::VectorType>(
-      cast<llvm::PointerType>(Addr.getPointer()->getType())->getElementType());
+      cast<llvm::PointerType>(Addr.getElementType()));
   if (VectorTy && !IsVector) {
     auto *ArrayTy = llvm::ArrayType::get(
         VectorTy->getElementType(),
Index: clang/lib/CodeGen/CGBlocks.cpp
===================================================================
--- clang/lib/CodeGen/CGBlocks.cpp
+++ clang/lib/CodeGen/CGBlocks.cpp
@@ -2750,8 +2750,7 @@
   Address addr = emission.Addr;
 
   // That's an alloca of the byref structure type.
-  llvm::StructType *byrefType = cast<llvm::StructType>(
-    cast<llvm::PointerType>(addr.getPointer()->getType())->getElementType());
+  llvm::StructType *byrefType = cast<llvm::StructType>(addr.getElementType());
 
   unsigned nextHeaderIndex = 0;
   CharUnits nextHeaderOffset;
Index: clang/lib/CodeGen/Address.h
===================================================================
--- clang/lib/CodeGen/Address.h
+++ clang/lib/CodeGen/Address.h
@@ -23,12 +23,19 @@
 /// An aligned address.
 class Address {
   llvm::Value *Pointer;
+  llvm::Type *PointeeType;
   CharUnits Alignment;
 public:
   Address(llvm::Value *pointer, CharUnits alignment)
-      : Pointer(pointer), Alignment(alignment) {
+      : Address(pointer, nullptr, alignment) {}
+  Address(llvm::Value *pointer, llvm::Type *PointeeType, CharUnits alignment)
+      : Pointer(pointer), PointeeType(PointeeType), Alignment(alignment) {
     assert((!alignment.isZero() || pointer == nullptr) &&
            "creating valid address with invalid alignment");
+    assert((pointer == nullptr ||
+            llvm::cast<llvm::PointerType>(pointer->getType())
+                ->isOpaqueOrPointeeTypeMatches(PointeeType)) &&
+           "pointee type should match pointer type's pointee type");
   }
 
   static Address invalid() { return Address(nullptr, CharUnits()); }
@@ -48,9 +55,7 @@
   ///
   /// When IR pointer types lose their element type, we should simply
   /// store it in Address instead for the convenience of writing code.
-  llvm::Type *getElementType() const {
-    return getType()->getElementType();
-  }
+  llvm::Type *getElementType() const { return PointeeType; }
 
   /// Return the address space that this address resides in.
   unsigned getAddressSpace() const {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D103465: [OpaquePtr... Arthur Eubanks via Phabricator via cfe-commits

Reply via email to