Home | History | Annotate | Download | only in InstSimplify
      1 ; RUN: opt -S -instsimplify < %s | FileCheck %s
      2 
      3 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
      4 
      5 %struct.A = type { [7 x i8] }
      6 
      7 define %struct.A* @test1(%struct.A* %b, %struct.A* %e) {
      8   %e_ptr = ptrtoint %struct.A* %e to i64
      9   %b_ptr = ptrtoint %struct.A* %b to i64
     10   %sub = sub i64 %e_ptr, %b_ptr
     11   %sdiv = sdiv exact i64 %sub, 7
     12   %gep = getelementptr inbounds %struct.A, %struct.A* %b, i64 %sdiv
     13   ret %struct.A* %gep
     14 ; CHECK-LABEL: @test1
     15 ; CHECK-NEXT: ret %struct.A* %e
     16 }
     17 
     18 define i8* @test2(i8* %b, i8* %e) {
     19   %e_ptr = ptrtoint i8* %e to i64
     20   %b_ptr = ptrtoint i8* %b to i64
     21   %sub = sub i64 %e_ptr, %b_ptr
     22   %gep = getelementptr inbounds i8, i8* %b, i64 %sub
     23   ret i8* %gep
     24 ; CHECK-LABEL: @test2
     25 ; CHECK-NEXT: ret i8* %e
     26 }
     27 
     28 define i64* @test3(i64* %b, i64* %e) {
     29   %e_ptr = ptrtoint i64* %e to i64
     30   %b_ptr = ptrtoint i64* %b to i64
     31   %sub = sub i64 %e_ptr, %b_ptr
     32   %ashr = ashr exact i64 %sub, 3
     33   %gep = getelementptr inbounds i64, i64* %b, i64 %ashr
     34   ret i64* %gep
     35 ; CHECK-LABEL: @test3
     36 ; CHECK-NEXT: ret i64* %e
     37 }
     38 
     39 define %struct.A* @test4(%struct.A* %b) {
     40   %b_ptr = ptrtoint %struct.A* %b to i64
     41   %sub = sub i64 0, %b_ptr
     42   %sdiv = sdiv exact i64 %sub, 7
     43   %gep = getelementptr inbounds %struct.A, %struct.A* %b, i64 %sdiv
     44   ret %struct.A* %gep
     45 ; CHECK-LABEL: @test4
     46 ; CHECK-NEXT: ret %struct.A* null
     47 }
     48 
     49 define i8* @test5(i8* %b) {
     50   %b_ptr = ptrtoint i8* %b to i64
     51   %sub = sub i64 0, %b_ptr
     52   %gep = getelementptr inbounds i8, i8* %b, i64 %sub
     53   ret i8* %gep
     54 ; CHECK-LABEL: @test5
     55 ; CHECK-NEXT: ret i8* null
     56 }
     57 
     58 define i64* @test6(i64* %b) {
     59   %b_ptr = ptrtoint i64* %b to i64
     60   %sub = sub i64 0, %b_ptr
     61   %ashr = ashr exact i64 %sub, 3
     62   %gep = getelementptr inbounds i64, i64* %b, i64 %ashr
     63   ret i64* %gep
     64 ; CHECK-LABEL: @test6
     65 ; CHECK-NEXT: ret i64* null
     66 }
     67 
     68 define i8* @test7(i8* %b, i8** %e) {
     69   %e_ptr = ptrtoint i8** %e to i64
     70   %b_ptr = ptrtoint i8* %b to i64
     71   %sub = sub i64 %e_ptr, %b_ptr
     72   %gep = getelementptr inbounds i8, i8* %b, i64 %sub
     73   ret i8* %gep
     74 ; CHECK-LABEL: @test7
     75 ; CHECK-NEXT: ptrtoint
     76 ; CHECK-NEXT: ptrtoint
     77 ; CHECK-NEXT: sub
     78 ; CHECK-NEXT: getelementptr
     79 ; CHECK-NEXT: ret
     80 }
     81