diff --git a/clang/lib/CodeGen/Address.h b/clang/lib/CodeGen/Address.h --- a/clang/lib/CodeGen/Address.h +++ b/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(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 { diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/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( - cast(addr.getPointer()->getType())->getElementType()); + llvm::StructType *byrefType = cast(addr.getElementType()); unsigned nextHeaderIndex = 0; CharUnits nextHeaderOffset; diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/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(Result.getType()->getElementType()); + auto *ArrayTy = cast(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( - cast(Addr.getPointer()->getType())->getElementType()); + auto *ArrayTy = dyn_cast(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( - cast(Addr.getPointer()->getType())->getElementType()); + cast(Addr.getElementType())); if (VectorTy && !IsVector) { auto *ArrayTy = llvm::ArrayType::get( VectorTy->getElementType(), diff --git a/clang/lib/CodeGen/CGValue.h b/clang/lib/CodeGen/CGValue.h --- a/clang/lib/CodeGen/CGValue.h +++ b/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);