}
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<Operator>(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;
define i8* @test42i(i8* %c1, i8* %c2) {
; CHECK-LABEL: @test42i(
; 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
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 i8, i8* [[C2:%.*]], i64 [[SUB]]
; CHECK-NEXT: ret i8* [[GEP]]
;
%ptrtoint = ptrtoint i8* %c1 to i64
define i16* @test43i(i16* %c1, i16* %c2) {
; CHECK-LABEL: @test43i(
; 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
define %struct.C* @test44i(%struct.C* %c1, %struct.C* %c2) {
; CHECK-LABEL: @test44i(
; 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