InstCombine: Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y))
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 20 Sep 2013 14:38:44 +0000 (14:38 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 20 Sep 2013 14:38:44 +0000 (14:38 +0000)
The GEP pattern is what SCEV expander emits for "ugly geps". The latter is what
you get for pointer subtraction in C code. The rest of instcombine already
knows how to deal with that so just canonicalize on that.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191090 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 803c7279bbc6ce1557373ad5fdd1d4dd3b51bc70..fcb26ab82af888ab1a43a3887e022950dce9d3bc 100644 (file)
@@ -1182,6 +1182,20 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
         GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
   }
 
+  // Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y))
+  // 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());
+  }
+
   // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
   Value *StrippedPtr = PtrOp->stripPointerCasts();
   PointerType *StrippedPtrTy = dyn_cast<PointerType>(StrippedPtr->getType());
index ff8ad63cef3695bdca68ca56fc2304e1ec50337d..191a151b6b094159eb4db976a2bdcba472029ae5 100644 (file)
@@ -762,4 +762,18 @@ define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addr
   ret i8 %x
 }
 
+define i64 @test40() {
+  %array = alloca [3 x i32], align 4
+  %gep = getelementptr inbounds [3 x i32]* %array, i64 0, i64 2
+  %gepi8 = bitcast i32* %gep to i8*
+  %p = ptrtoint [3 x i32]* %array to i64
+  %np = sub i64 0, %p
+  %gep2 = getelementptr i8* %gepi8, i64 %np
+  %ret = ptrtoint i8* %gep2 to i64
+  ret i64 %ret
+
+; CHECK-LABEL: @test40
+; CHECK-NEXT: ret i64 8
+}
+
 ; CHECK: attributes [[NUW]] = { nounwind }