diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2173,16 +2173,6 @@ } if (Matched) { - // Canonicalize (gep i8* X, -(ptrtoint Y)) - // to (inttoptr (sub (ptrtoint X), (ptrtoint Y))) - // The GEP pattern is emitted by the SCEV expander for certain kinds of - // pointer arithmetic. - if (match(V, m_Neg(m_PtrToInt(m_Value())))) { - Operator *Index = cast(V); - Value *PtrToInt = Builder.CreatePtrToInt(PtrOp, Index->getType()); - Value *NewSub = Builder.CreateSub(PtrToInt, Index->getOperand(1)); - return CastInst::Create(Instruction::IntToPtr, NewSub, GEPType); - } // Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) // to (bitcast Y) Value *Y; diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll --- a/llvm/test/Transforms/InstCombine/getelementptr.ll +++ b/llvm/test/Transforms/InstCombine/getelementptr.ll @@ -1028,9 +1028,8 @@ define i8* @test42(i8* %c1, i8* %c2) { ; CHECK-LABEL: @test42( ; CHECK-NEXT: [[PTRTOINT:%.*]] = ptrtoint i8* [[C1:%.*]] to i64 -; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint i8* [[C2:%.*]] to i64 -; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[TMP1]], [[PTRTOINT]] -; CHECK-NEXT: [[GEP:%.*]] = inttoptr i64 [[TMP2]] to i8* +; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[PTRTOINT]] +; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* [[C2:%.*]], i64 [[SUB]] ; CHECK-NEXT: ret i8* [[GEP]] ; %ptrtoint = ptrtoint i8* %c1 to i64 @@ -1043,9 +1042,9 @@ define i16* @test43(i16* %c1, i16* %c2) { ; CHECK-LABEL: @test43( ; CHECK-NEXT: [[PTRTOINT:%.*]] = ptrtoint i16* [[C1:%.*]] to i64 -; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint i16* [[C2:%.*]] to i64 -; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[TMP1]], [[PTRTOINT]] -; CHECK-NEXT: [[GEP:%.*]] = inttoptr i64 [[TMP2]] to i16* +; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[PTRTOINT]] +; CHECK-NEXT: [[SHR:%.*]] = ashr i64 [[SUB]], 1 +; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i16, i16* [[C2:%.*]], i64 [[SHR]] ; CHECK-NEXT: ret i16* [[GEP]] ; %ptrtoint = ptrtoint i16* %c1 to i64 @@ -1059,9 +1058,9 @@ define %struct.C* @test44(%struct.C* %c1, %struct.C* %c2) { ; CHECK-LABEL: @test44( ; CHECK-NEXT: [[PTRTOINT:%.*]] = ptrtoint %struct.C* [[C1:%.*]] to i64 -; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint %struct.C* [[C2:%.*]] to i64 -; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[TMP1]], [[PTRTOINT]] -; CHECK-NEXT: [[GEP:%.*]] = inttoptr i64 [[TMP2]] to %struct.C* +; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[PTRTOINT]] +; CHECK-NEXT: [[SHR:%.*]] = sdiv i64 [[SUB]], 7 +; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_C:%.*]], %struct.C* [[C2:%.*]], i64 [[SHR]] ; CHECK-NEXT: ret %struct.C* [[GEP]] ; %ptrtoint = ptrtoint %struct.C* %c1 to i64