[PATCH] D34335: Fix invalid ptrtoint in InstCombine

Yichao Yu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 18 15:09:59 PDT 2017


yuyichao created this revision.

A few things I'm not sure about.

1. Is this the right place to do it? (It seems that this is used in both instcombine and jumpthreading with a similar pattern but I'm not sure if this can be triggerred in jumpthreading)
2. Do I need the address space check? I can't find a different place that checks the address space matching and the caller does not try addrspacecast (and probably shouldn't) though I also can't trigger the code with two address space ....


https://reviews.llvm.org/D34335

Files:
  lib/Analysis/Loads.cpp
  test/Transforms/InstCombine/non-integral-pointers.ll


Index: test/Transforms/InstCombine/non-integral-pointers.ll
===================================================================
--- test/Transforms/InstCombine/non-integral-pointers.ll
+++ test/Transforms/InstCombine/non-integral-pointers.ll
@@ -46,3 +46,21 @@
   store i8 addrspace(3)* %val, i8 addrspace(3)** %ptr1
   ret void
 }
+
+define i64 @g(i8 addrspace(4)** %gp) {
+; CHECK-LABEL: @g(
+  %.pre = load i8 addrspace(4)*, i8 addrspace(4)** %gp, align 8
+  %v74 = call i8 addrspace(4)* @alloc()
+  %v75 = addrspacecast i8 addrspace(4)* %v74 to i8*
+  %v76 = bitcast i8* %v75 to i8 addrspace(4)**
+  %v77 = getelementptr i8 addrspace(4)*, i8 addrspace(4)** %v76, i64 -1
+  store i8 addrspace(4)* %.pre, i8 addrspace(4)** %v77, align 8
+  %v78 = bitcast i8 addrspace(4)* %v74 to i8 addrspace(4)* addrspace(4)*
+  %v79 = addrspacecast i8 addrspace(4)* addrspace(4)* %v78 to i8 addrspace(4)**
+  %v80 = bitcast i8 addrspace(4)** %v77 to i64*
+; CHECK-NOT: ptrtoint
+  %v81 = load i64, i64* %v80, align 8
+  ret i64 %v81
+}
+
+declare i8 addrspace(4)* @alloc()
Index: lib/Analysis/Loads.cpp
===================================================================
--- lib/Analysis/Loads.cpp
+++ lib/Analysis/Loads.cpp
@@ -370,6 +370,21 @@
         if (LI->isAtomic() < AtLeastAtomic)
           return nullptr;
 
+        if (Ptr->getType()->isPointerTy()) {
+          PointerType *Ty1 = cast<PointerType>(Ptr->getType());
+          if (PointerType *Ty2 = dyn_cast<PointerType>(LI->getType())) {
+            if (Ty1->getAddressSpace() != Ty2->getAddressSpace()) {
+              return nullptr;
+            }
+          }
+          else if (DL.isNonIntegralPointerType(Ty1)) {
+            return nullptr;
+          }
+        }
+        else if (DL.isNonIntegralPointerType(LI->getType())) {
+          return nullptr;
+        }
+
         if (IsLoadCSE)
             *IsLoadCSE = true;
         return LI;
@@ -389,6 +404,21 @@
         if (SI->isAtomic() < AtLeastAtomic)
           return nullptr;
 
+        if (Ptr->getType()->isPointerTy()) {
+          PointerType *Ty1 = cast<PointerType>(Ptr->getType());
+          if (PointerType *Ty2 = dyn_cast<PointerType>(SI->getValueOperand()->getType())) {
+            if (Ty1->getAddressSpace() != Ty2->getAddressSpace()) {
+              return nullptr;
+            }
+          }
+          else if (DL.isNonIntegralPointerType(Ty1)) {
+            return nullptr;
+          }
+        }
+        else if (DL.isNonIntegralPointerType(SI->getValueOperand()->getType())) {
+          return nullptr;
+        }
+
         if (IsLoadCSE)
           *IsLoadCSE = false;
         return SI->getOperand(0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34335.102978.patch
Type: text/x-patch
Size: 2659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170618/aa3b3ed0/attachment.bin>


More information about the llvm-commits mailing list