1; RUN: opt < %s -instsimplify -S | FileCheck %s 2target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" 3target triple = "x86_64-unknown-linux-gnu" 4 5define i64 @ptrdiff1(i8* %ptr) { 6; CHECK: @ptrdiff1 7; CHECK-NEXT: ret i64 42 8 9 %first = getelementptr inbounds i8* %ptr, i32 0 10 %last = getelementptr inbounds i8* %ptr, i32 42 11 %first.int = ptrtoint i8* %first to i64 12 %last.int = ptrtoint i8* %last to i64 13 %diff = sub i64 %last.int, %first.int 14 ret i64 %diff 15} 16 17define i64 @ptrdiff2(i8* %ptr) { 18; CHECK: @ptrdiff2 19; CHECK-NEXT: ret i64 42 20 21 %first1 = getelementptr inbounds i8* %ptr, i32 0 22 %first2 = getelementptr inbounds i8* %first1, i32 1 23 %first3 = getelementptr inbounds i8* %first2, i32 2 24 %first4 = getelementptr inbounds i8* %first3, i32 4 25 %last1 = getelementptr inbounds i8* %first2, i32 48 26 %last2 = getelementptr inbounds i8* %last1, i32 8 27 %last3 = getelementptr inbounds i8* %last2, i32 -4 28 %last4 = getelementptr inbounds i8* %last3, i32 -4 29 %first.int = ptrtoint i8* %first4 to i64 30 %last.int = ptrtoint i8* %last4 to i64 31 %diff = sub i64 %last.int, %first.int 32 ret i64 %diff 33} 34 35define i64 @ptrdiff3(i8* %ptr) { 36; Don't bother with non-inbounds GEPs. 37; CHECK: @ptrdiff3 38; CHECK: getelementptr 39; CHECK: sub 40; CHECK: ret 41 42 %first = getelementptr i8* %ptr, i32 0 43 %last = getelementptr i8* %ptr, i32 42 44 %first.int = ptrtoint i8* %first to i64 45 %last.int = ptrtoint i8* %last to i64 46 %diff = sub i64 %last.int, %first.int 47 ret i64 %diff 48} 49 50define <4 x i32> @ptrdiff4(<4 x i8*> %arg) nounwind { 51; Handle simple cases of vectors of pointers. 52; CHECK: @ptrdiff4 53; CHECK: ret <4 x i32> zeroinitializer 54 %p1 = ptrtoint <4 x i8*> %arg to <4 x i32> 55 %bc = bitcast <4 x i8*> %arg to <4 x i32*> 56 %p2 = ptrtoint <4 x i32*> %bc to <4 x i32> 57 %sub = sub <4 x i32> %p1, %p2 58 ret <4 x i32> %sub 59} 60 61%struct.ham = type { i32, [2 x [2 x i32]] } 62 63@global = internal global %struct.ham zeroinitializer, align 4 64 65define i32 @ptrdiff5() nounwind { 66bb: 67 %tmp = getelementptr inbounds %struct.ham* @global, i32 0, i32 1 68 %tmp1 = getelementptr inbounds [2 x [2 x i32]]* %tmp, i32 0, i32 0 69 %tmp2 = bitcast [2 x i32]* %tmp1 to i32* 70 %tmp3 = ptrtoint i32* %tmp2 to i32 71 %tmp4 = getelementptr inbounds %struct.ham* @global, i32 0, i32 1 72 %tmp5 = getelementptr inbounds [2 x [2 x i32]]* %tmp4, i32 0, i32 0 73 %tmp6 = ptrtoint [2 x i32]* %tmp5 to i32 74 %tmp7 = sub i32 %tmp3, %tmp6 75 ret i32 %tmp7 76; CHECK: @ptrdiff5 77; CHECK: ret i32 0 78} 79