From: Benjamin Kramer Date: Fri, 20 Sep 2013 14:38:44 +0000 (+0000) Subject: InstCombine: Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y)) X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=4c332fa5a5a650cd779935d78b505f84cef4db69;p=oota-llvm.git InstCombine: Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y)) 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 --- diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 803c7279bbc..fcb26ab82af 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -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(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(StrippedPtr->getType()); diff --git a/test/Transforms/InstCombine/getelementptr.ll b/test/Transforms/InstCombine/getelementptr.ll index ff8ad63cef3..191a151b6b0 100644 --- a/test/Transforms/InstCombine/getelementptr.ll +++ b/test/Transforms/InstCombine/getelementptr.ll @@ -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 }