[PATCH] Make gep i8* X, -(ptrtoint Y) transform work with address spaces

Matt Arsenault Matthew.Arsenault at amd.com
Thu Oct 3 10:22:04 PDT 2013


Hi bkramer,

It wasn't specified for getInt8PtrTy(), so it wouldn't happen.

http://llvm-reviews.chandlerc.com/D1821

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

Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1186,14 +1186,16 @@
   // The GEP pattern is emitted by the SCEV expander for certain kinds of
   // pointer arithmetic.
   if (TD && GEP.getNumIndices() == 1 &&
-      match(GEP.getOperand(1), m_Neg(m_PtrToInt(m_Value()))) &&
-      GEP.getType() == Builder->getInt8PtrTy() &&
-      GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
-          TD->getPointerSizeInBits(GEP.getPointerAddressSpace())) {
-    Operator *Index = cast<Operator>(GEP.getOperand(1));
-    Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
-    Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
-    return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
+      match(GEP.getOperand(1), m_Neg(m_PtrToInt(m_Value())))) {
+    unsigned AS = GEP.getPointerAddressSpace();
+    if (GEP.getType() == Builder->getInt8PtrTy(AS) &&
+        GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
+        TD->getPointerSizeInBits(AS)) {
+      Operator *Index = cast<Operator>(GEP.getOperand(1));
+      Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
+      Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
+      return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
+    }
   }
 
   // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
Index: test/Transforms/InstCombine/getelementptr.ll
===================================================================
--- test/Transforms/InstCombine/getelementptr.ll
+++ test/Transforms/InstCombine/getelementptr.ll
@@ -776,4 +776,17 @@
 ; CHECK-NEXT: ret i64 8
 }
 
+define i16 @test41([3 x i32] addrspace(1)* %array) {
+  %gep = getelementptr inbounds [3 x i32] addrspace(1)* %array, i16 0, i16 2
+  %gepi8 = bitcast i32 addrspace(1)* %gep to i8 addrspace(1)*
+  %p = ptrtoint [3 x i32] addrspace(1)* %array to i16
+  %np = sub i16 0, %p
+  %gep2 = getelementptr i8 addrspace(1)* %gepi8, i16 %np
+  %ret = ptrtoint i8 addrspace(1)* %gep2 to i16
+  ret i16 %ret
+
+; CHECK-LABEL: @test41(
+; CHECK-NEXT: ret i16 8
+}
+
 ; CHECK: attributes [[NUW]] = { nounwind }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1821.1.patch
Type: text/x-patch
Size: 2327 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131003/22ccb341/attachment.bin>


More information about the llvm-commits mailing list