Home | History | Annotate | Download | only in InstCombine
      1 ; Tests to make sure elimination of casts is working correctly
      2 ; RUN: opt < %s -instcombine -S | FileCheck %s
      3 
      4 target datalayout = "p:32:32-p1:32:32-p2:16:16"
      5 
      6 @global = global i8 0
      7 
      8 ; This shouldn't convert to getelementptr because the relationship
      9 ; between the arithmetic and the layout of allocated memory is
     10 ; entirely unknown.
     11 ; CHECK-LABEL: @test1(
     12 ; CHECK: ptrtoint
     13 ; CHECK: add
     14 ; CHECK: inttoptr
     15 define i8* @test1(i8* %t) {
     16         %tmpc = ptrtoint i8* %t to i32          ; <i32> [#uses=1]
     17         %tmpa = add i32 %tmpc, 32               ; <i32> [#uses=1]
     18         %tv = inttoptr i32 %tmpa to i8*         ; <i8*> [#uses=1]
     19         ret i8* %tv
     20 }
     21 
     22 ; These casts should be folded away.
     23 ; CHECK-LABEL: @test2(
     24 ; CHECK: icmp eq i8* %a, %b
     25 define i1 @test2(i8* %a, i8* %b) {
     26         %tmpa = ptrtoint i8* %a to i32          ; <i32> [#uses=1]
     27         %tmpb = ptrtoint i8* %b to i32          ; <i32> [#uses=1]
     28         %r = icmp eq i32 %tmpa, %tmpb           ; <i1> [#uses=1]
     29         ret i1 %r
     30 }
     31 
     32 ; These casts should be folded away.
     33 ; CHECK-LABEL: @test2_as2_same_int(
     34 ; CHECK: icmp eq i8 addrspace(2)* %a, %b
     35 define i1 @test2_as2_same_int(i8 addrspace(2)* %a, i8 addrspace(2)* %b) {
     36   %tmpa = ptrtoint i8 addrspace(2)* %a to i16
     37   %tmpb = ptrtoint i8 addrspace(2)* %b to i16
     38   %r = icmp eq i16 %tmpa, %tmpb
     39   ret i1 %r
     40 }
     41 
     42 ; These casts should be folded away.
     43 ; CHECK-LABEL: @test2_as2_larger(
     44 ; CHECK: icmp eq i8 addrspace(2)* %a, %b
     45 define i1 @test2_as2_larger(i8 addrspace(2)* %a, i8 addrspace(2)* %b) {
     46   %tmpa = ptrtoint i8 addrspace(2)* %a to i32
     47   %tmpb = ptrtoint i8 addrspace(2)* %b to i32
     48   %r = icmp eq i32 %tmpa, %tmpb
     49   ret i1 %r
     50 }
     51 
     52 ; These casts should not be folded away.
     53 ; CHECK-LABEL: @test2_diff_as
     54 ; CHECK: icmp sge i32 %i0, %i1
     55 define i1 @test2_diff_as(i8* %p, i8 addrspace(1)* %q) {
     56   %i0 = ptrtoint i8* %p to i32
     57   %i1 = ptrtoint i8 addrspace(1)* %q to i32
     58   %r0 = icmp sge i32 %i0, %i1
     59   ret i1 %r0
     60 }
     61 
     62 ; These casts should not be folded away.
     63 ; CHECK-LABEL: @test2_diff_as_global
     64 ; CHECK: icmp sge i32 %i1
     65 define i1 @test2_diff_as_global(i8 addrspace(1)* %q) {
     66   %i0 = ptrtoint i8* @global to i32
     67   %i1 = ptrtoint i8 addrspace(1)* %q to i32
     68   %r0 = icmp sge i32 %i1, %i0
     69   ret i1 %r0
     70 }
     71 
     72 ; These casts should also be folded away.
     73 ; CHECK-LABEL: @test3(
     74 ; CHECK: icmp eq i8* %a, @global
     75 define i1 @test3(i8* %a) {
     76         %tmpa = ptrtoint i8* %a to i32
     77         %r = icmp eq i32 %tmpa, ptrtoint (i8* @global to i32)
     78         ret i1 %r
     79 }
     80 
     81 define i1 @test4(i32 %A) {
     82   %B = inttoptr i32 %A to i8*
     83   %C = icmp eq i8* %B, null
     84   ret i1 %C
     85 ; CHECK-LABEL: @test4(
     86 ; CHECK-NEXT: %C = icmp eq i32 %A, 0
     87 ; CHECK-NEXT: ret i1 %C
     88 }
     89 
     90 define i1 @test4_as2(i16 %A) {
     91 ; CHECK-LABEL: @test4_as2(
     92 ; CHECK-NEXT: %C = icmp eq i16 %A, 0
     93 ; CHECK-NEXT: ret i1 %C
     94   %B = inttoptr i16 %A to i8 addrspace(2)*
     95   %C = icmp eq i8 addrspace(2)* %B, null
     96   ret i1 %C
     97 }
     98 
     99 
    100 ; Pulling the cast out of the load allows us to eliminate the load, and then
    101 ; the whole array.
    102 
    103         %op = type { float }
    104         %unop = type { i32 }
    105 @Array = internal constant [1 x %op* (%op*)*] [ %op* (%op*)* @foo ]             ; <[1 x %op* (%op*)*]*> [#uses=1]
    106 
    107 declare %op* @foo(%op* %X)
    108 
    109 define %unop* @test5(%op* %O) {
    110         %tmp = load %unop* (%op*)*, %unop* (%op*)** bitcast ([1 x %op* (%op*)*]* @Array to %unop* (%op*)**); <%unop* (%op*)*> [#uses=1]
    111         %tmp.2 = call %unop* %tmp( %op* %O )            ; <%unop*> [#uses=1]
    112         ret %unop* %tmp.2
    113 ; CHECK-LABEL: @test5(
    114 ; CHECK: call %op* @foo(%op* %O)
    115 }
    116 
    117 
    118 
    119 ; InstCombine can not 'load (cast P)' -> cast (load P)' if the cast changes
    120 ; the address space.
    121 
    122 define i8 @test6(i8 addrspace(1)* %source) {
    123 entry:
    124   %arrayidx223 = addrspacecast i8 addrspace(1)* %source to i8*
    125   %tmp4 = load i8, i8* %arrayidx223
    126   ret i8 %tmp4
    127 ; CHECK-LABEL: @test6(
    128 ; CHECK: load i8, i8* %arrayidx223
    129 }
    130