[llvm] f086ef3 - [Constants] Handle addrspacecast with opaque pointer type

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 23 11:58:20 PDT 2021


Author: Nikita Popov
Date: 2021-06-23T20:58:08+02:00
New Revision: f086ef3e83fd466fef2fbd1f9184d540675db0f6

URL: https://github.com/llvm/llvm-project/commit/f086ef3e83fd466fef2fbd1f9184d540675db0f6
DIFF: https://github.com/llvm/llvm-project/commit/f086ef3e83fd466fef2fbd1f9184d540675db0f6.diff

LOG: [Constants] Handle addrspacecast with opaque pointer type

This is the same change as D104668, but for constant expression
addrspacecasts.

Added: 
    

Modified: 
    llvm/lib/IR/Constants.cpp
    llvm/test/Transforms/InstCombine/opaque-ptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 2823a1eab436..6dcf8b5b6a67 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2238,9 +2238,9 @@ Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
   // bitcasting the pointer type and then converting the address space.
   PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
   PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
-  Type *DstElemTy = DstScalarTy->getElementType();
-  if (SrcScalarTy->getElementType() != DstElemTy) {
-    Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
+  if (!SrcScalarTy->hasSameElementTypeAs(DstScalarTy)) {
+    Type *MidTy = PointerType::getWithSamePointeeType(
+        DstScalarTy, SrcScalarTy->getAddressSpace());
     if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
       // Handle vectors of pointers.
       MidTy = FixedVectorType::get(MidTy,

diff  --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index 45b699855dd7..6a83587a3fca 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -74,6 +74,13 @@ define ptr addrspace(1) @bitcast_and_addrspacecast_eliminable(ptr %a) {
   ret ptr addrspace(1) %c
 }
 
+define ptr addrspace(1) @addrspacecast_typed_to_opaque_constexpr() {
+; CHECK-LABEL: @addrspacecast_typed_to_opaque_constexpr(
+; CHECK-NEXT:    ret ptr addrspace(1) addrspacecast (ptr bitcast (i8* @g to ptr) to ptr addrspace(1))
+;
+  ret ptr addrspace(1) addrspacecast (i8* @g to ptr addrspace(1))
+}
+
 define ptr @gep_constexpr_1(ptr %a) {
 ; CHECK-LABEL: @gep_constexpr_1(
 ; CHECK-NEXT:    ret ptr inttoptr (i64 6 to ptr)


        


More information about the llvm-commits mailing list