[PATCH] Check address space when optimizing gep+cast

Jingyue Wu jingyue at google.com
Fri Mar 28 12:01:11 PDT 2014


Fixing PR19270. This issue is blocking a waiting patch of mine that
implements the optimization we discussed in
http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-March/071440.html.

visitGetElementPtr in InstructionCombining.cpp gets rid of unnecessary
pointer casts in gep (cast X). However, this optimization may change the
address space of the result pointer type, and cause type mismatch.

e.g.,
getelementptr [256 x float]* addrspacecast ([256 x float] addrspace(3)*
@array to [256 x float]*), i64 0, i64 %i
returns a float*, but the optimized instruction
getelementptr [256 x float] addrspace(3)* @array, i64 0, i64 %i
returns a float addrspace(3)*

The attached patch disables this optimization when the address space of the
source is different from that of the destination.

Jingyue
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140328/be425e9c/attachment.html>
-------------- next part --------------
diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp
index 0cab81b..e287de7 100644
--- a/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1218,7 +1218,9 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
   if (!StrippedPtrTy)
     return 0;
 
-  if (StrippedPtr != PtrOp) {
+  if (StrippedPtr != PtrOp &&
+      StrippedPtrTy->getAddressSpace() ==
+        cast<PointerType>(PtrOp->getType())->getAddressSpace()) {
     bool HasZeroPointerIndex = false;
     if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1)))
       HasZeroPointerIndex = C->isZero();
diff --git a/test/Transforms/InstCombine/gep-addrspace.ll b/test/Transforms/InstCombine/gep-addrspace.ll
index 24c355d..ef420a9 100644
--- a/test/Transforms/InstCombine/gep-addrspace.ll
+++ b/test/Transforms/InstCombine/gep-addrspace.ll
@@ -17,3 +17,13 @@ ST:
   ret void
 }
 
+ at array = internal addrspace(3) global [256 x float] zeroinitializer, align 4
+
+define float* @keep_necessary_addrspacecast(i64 %i) {
+entry:
+; CHECK-LABEL: @keep_necessary_addrspacecast
+; CHECK: addrspacecast ([256 x float] addrspace(3)* @array to [256 x float]*)
+  %0 = getelementptr [256 x float]* addrspacecast ([256 x float] addrspace(3)* @array to [256 x float]*), i64 0, i64 %i
+  ret float* %0
+}
+


More information about the llvm-commits mailing list