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 @@ -2148,51 +2148,6 @@ GEP.getName()); } - // Skip if GEP source element type is scalable. The type alloc size is unknown - // at compile-time. - if (GEP.getNumIndices() == 1 && !IsGEPSrcEleScalable) { - unsigned AS = GEP.getPointerAddressSpace(); - if (GEP.getOperand(1)->getType()->getScalarSizeInBits() == - DL.getIndexSizeInBits(AS)) { - uint64_t TyAllocSize = DL.getTypeAllocSize(GEPEltType).getFixedSize(); - - bool Matched = false; - uint64_t C; - Value *V = nullptr; - if (TyAllocSize == 1) { - V = GEP.getOperand(1); - Matched = true; - } else if (match(GEP.getOperand(1), - m_AShr(m_Value(V), m_ConstantInt(C)))) { - if (TyAllocSize == 1ULL << C) - Matched = true; - } else if (match(GEP.getOperand(1), - m_SDiv(m_Value(V), m_ConstantInt(C)))) { - if (TyAllocSize == C) - Matched = true; - } - - 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; - if (match(V, m_Sub(m_PtrToInt(m_Value(Y)), - m_PtrToInt(m_Specific(GEP.getOperand(0)))))) - return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y, GEPType); - } - } - } - // We do not handle pointer-vector geps here. if (GEPType->isVectorTy()) return nullptr; 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 @@ -1074,7 +1073,11 @@ define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) { ; CHECK-LABEL: @test45( -; CHECK-NEXT: [[GEP:%.*]] = bitcast %struct.C** [[C2:%.*]] to %struct.C* +; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint %struct.C* [[C1:%.*]] to i64 +; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint %struct.C** [[C2:%.*]] to i64 +; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]] +; CHECK-NEXT: [[SHR:%.*]] = sdiv i64 [[SUB]], 7 +; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_C:%.*]], %struct.C* [[C1]], i64 [[SHR]] ; CHECK-NEXT: ret %struct.C* [[GEP]] ; %ptrtoint1 = ptrtoint %struct.C* %c1 to i64