Home | History | Annotate | Download | only in InstCombine
      1 ; RUN: opt -S -instcombine %s -o - | FileCheck %s
      2 target datalayout = "e-p:32:32:32-p1:64:64:64-p2:8:8:8-p3:16:16:16-p4:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:32"
      3 
      4 
      5 define i32 @test_as0(i32 addrspace(0)* %a) {
      6 ; CHECK-LABEL: @test_as0(
      7 ; CHECK: %arrayidx = getelementptr i32* %a, i32 1
      8   %arrayidx = getelementptr i32 addrspace(0)* %a, i64 1
      9   %y = load i32 addrspace(0)* %arrayidx, align 4
     10   ret i32 %y
     11 }
     12 
     13 define i32 @test_as1(i32 addrspace(1)* %a) {
     14 ; CHECK-LABEL: @test_as1(
     15 ; CHECK: %arrayidx = getelementptr i32 addrspace(1)* %a, i64 1
     16   %arrayidx = getelementptr i32 addrspace(1)* %a, i32 1
     17   %y = load i32 addrspace(1)* %arrayidx, align 4
     18   ret i32 %y
     19 }
     20 
     21 define i32 @test_as2(i32 addrspace(2)* %a) {
     22 ; CHECK-LABEL: @test_as2(
     23 ; CHECK: %arrayidx = getelementptr i32 addrspace(2)* %a, i8 1
     24   %arrayidx = getelementptr i32 addrspace(2)* %a, i32 1
     25   %y = load i32 addrspace(2)* %arrayidx, align 4
     26   ret i32 %y
     27 }
     28 
     29 define i32 @test_as3(i32 addrspace(3)* %a) {
     30 ; CHECK-LABEL: @test_as3(
     31 ; CHECK: %arrayidx = getelementptr i32 addrspace(3)* %a, i16 1
     32   %arrayidx = getelementptr i32 addrspace(3)* %a, i32 1
     33   %y = load i32 addrspace(3)* %arrayidx, align 4
     34   ret i32 %y
     35 }
     36 
     37 define i32 @test_combine_ptrtoint(i32 addrspace(2)* %a) {
     38 ; CHECK-LABEL: @test_combine_ptrtoint(
     39 ; CHECK-NEXT: %y = load i32 addrspace(2)* %a
     40 ; CHECK-NEXT: ret i32 %y
     41   %cast = ptrtoint i32 addrspace(2)* %a to i8
     42   %castback = inttoptr i8 %cast to i32 addrspace(2)*
     43   %y = load i32 addrspace(2)* %castback, align 4
     44   ret i32 %y
     45 }
     46 
     47 define i8 @test_combine_inttoptr(i8 %a) {
     48 ; CHECK-LABEL: @test_combine_inttoptr(
     49 ; CHECK-NEXT: ret i8 %a
     50   %cast = inttoptr i8 %a to i32 addrspace(2)*
     51   %castback = ptrtoint i32 addrspace(2)* %cast to i8
     52   ret i8 %castback
     53 }
     54 
     55 define i32 @test_combine_vector_ptrtoint(<2 x i32 addrspace(2)*> %a) {
     56 ; CHECK-LABEL: @test_combine_vector_ptrtoint(
     57 ; CHECK-NEXT: %p = extractelement <2 x i32 addrspace(2)*> %a, i32 0
     58 ; CHECK-NEXT: %y = load i32 addrspace(2)* %p, align 4
     59 ; CHECK-NEXT: ret i32 %y
     60   %cast = ptrtoint <2 x i32 addrspace(2)*> %a to <2 x i8>
     61   %castback = inttoptr <2 x i8> %cast to <2 x i32 addrspace(2)*>
     62   %p = extractelement <2 x i32 addrspace(2)*> %castback, i32 0
     63   %y = load i32 addrspace(2)* %p, align 4
     64   ret i32 %y
     65 }
     66 
     67 define <2 x i8> @test_combine_vector_inttoptr(<2 x i8> %a) {
     68 ; CHECK-LABEL: @test_combine_vector_inttoptr(
     69 ; CHECK-NEXT: ret <2 x i8> %a
     70   %cast = inttoptr <2 x i8> %a to <2 x i32 addrspace(2)*>
     71   %castback = ptrtoint <2 x i32 addrspace(2)*> %cast to <2 x i8>
     72   ret <2 x i8> %castback
     73 }
     74 
     75 ; Check that the GEP index is changed to the address space integer type (i64 -> i8)
     76 define i32 addrspace(2)* @shrink_gep_constant_index_64_as2(i32 addrspace(2)* %p) {
     77 ; CHECK-LABEL: @shrink_gep_constant_index_64_as2(
     78 ; CHECK-NEXT: getelementptr i32 addrspace(2)* %p, i8 1
     79   %ret = getelementptr i32 addrspace(2)* %p, i64 1
     80   ret i32 addrspace(2)* %ret
     81 }
     82 
     83 define i32 addrspace(2)* @shrink_gep_constant_index_32_as2(i32 addrspace(2)* %p) {
     84 ; CHECK-LABEL: @shrink_gep_constant_index_32_as2(
     85 ; CHECK-NEXT: getelementptr i32 addrspace(2)* %p, i8 1
     86   %ret = getelementptr i32 addrspace(2)* %p, i32 1
     87   ret i32 addrspace(2)* %ret
     88 }
     89 
     90 define i32 addrspace(3)* @shrink_gep_constant_index_64_as3(i32 addrspace(3)* %p) {
     91 ; CHECK-LABEL: @shrink_gep_constant_index_64_as3(
     92 ; CHECK-NEXT: getelementptr i32 addrspace(3)* %p, i16 1
     93   %ret = getelementptr i32 addrspace(3)* %p, i64 1
     94   ret i32 addrspace(3)* %ret
     95 }
     96 
     97 define i32 addrspace(2)* @shrink_gep_variable_index_64_as2(i32 addrspace(2)* %p, i64 %idx) {
     98 ; CHECK-LABEL: @shrink_gep_variable_index_64_as2(
     99 ; CHECK-NEXT: %1 = trunc i64 %idx to i8
    100 ; CHECK-NEXT: getelementptr i32 addrspace(2)* %p, i8 %1
    101   %ret = getelementptr i32 addrspace(2)* %p, i64 %idx
    102   ret i32 addrspace(2)* %ret
    103 }
    104 
    105 define i32 addrspace(1)* @grow_gep_variable_index_8_as1(i32 addrspace(1)* %p, i8 %idx) {
    106 ; CHECK-LABEL: @grow_gep_variable_index_8_as1(
    107 ; CHECK-NEXT: %1 = sext i8 %idx to i64
    108 ; CHECK-NEXT: getelementptr i32 addrspace(1)* %p, i64 %1
    109   %ret = getelementptr i32 addrspace(1)* %p, i8 %idx
    110   ret i32 addrspace(1)* %ret
    111 }
    112 
    113