[PATCH] D33361: [InstCombine] Fix inbounds gep for addrspacecasts

Sven van Haastregt via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 19 09:46:52 PDT 2017


svenvh created this revision.

The inbounds inference added by r277950 ("[InstCombine] Infer inbounds
on geps of allocas", 2016-08-07) could construct a new inbounds gep,
but ignore the address space.

Fix by appending an addrspacecast if needed.


https://reviews.llvm.org/D33361

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/getelementptr.ll


Index: test/Transforms/InstCombine/getelementptr.ll
===================================================================
--- test/Transforms/InstCombine/getelementptr.ll
+++ test/Transforms/InstCombine/getelementptr.ll
@@ -942,4 +942,17 @@
   ret <2 x i32*> %tmp1
 }
 
+; Test that inbounds inference works in the presence of addrspacecasts.
+define i8 addrspace(42)* @gep_inbounds_addrspace() {
+; CHECK-LABEL: @gep_inbounds_addrspace
+; CHECK: getelementptr inbounds
+; CHECK-NEXT: addrspacecast
+; CHECK: ret
+  %A = alloca <8 x half>, align 16
+  %B = bitcast <8 x half>* %A to i8*
+  %C = addrspacecast i8* %B to i8 addrspace(42)*
+  %gep = getelementptr i8, i8 addrspace(42)* %C, i64 12
+  ret i8 addrspace(42)* %gep
+}
+
 ; CHECK: attributes [[NUW]] = { nounwind }
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1912,6 +1912,7 @@
     }
   }
 
+  // Try to infer inbounds on GEPs of allocas.
   if (!GEP.isInBounds()) {
     unsigned PtrWidth =
         DL.getPointerSizeInBits(PtrOp->getType()->getPointerAddressSpace());
@@ -1924,8 +1925,13 @@
           BasePtrOffset.isNonNegative()) {
         APInt AllocSize(PtrWidth, DL.getTypeAllocSize(AI->getAllocatedType()));
         if (BasePtrOffset.ule(AllocSize)) {
-          return GetElementPtrInst::CreateInBounds(
+          Instruction *NewGEP = GetElementPtrInst::CreateInBounds(
               PtrOp, makeArrayRef(Ops).slice(1), GEP.getName());
+
+          if (PtrOp->getType()->getPointerAddressSpace() ==
+              GEP.getAddressSpace())
+            return NewGEP;
+          return new AddrSpaceCastInst(Builder->Insert(NewGEP), GEP.getType());
         }
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33361.99582.patch
Type: text/x-patch
Size: 1842 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170519/5c668a44/attachment-0001.bin>


More information about the llvm-commits mailing list