1 ; Tests to make sure elimination of casts is working correctly
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "p:32:32-p1:32:32-p2:16:16"
8 ; This shouldn't convert to getelementptr because the relationship
9 ; between the arithmetic and the layout of allocated memory is
11 ; CHECK-LABEL: @test1(
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]
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]
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
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
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
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
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)
81 define i1 @test4(i32 %A) {
82 %B = inttoptr i32 %A to i8*
83 %C = icmp eq i8* %B, null
85 ; CHECK-LABEL: @test4(
86 ; CHECK-NEXT: %C = icmp eq i32 %A, 0
87 ; CHECK-NEXT: ret i1 %C
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
100 ; Pulling the cast out of the load allows us to eliminate the load, and then
105 @Array = internal constant [1 x %op* (%op*)*] [ %op* (%op*)* @foo ] ; <[1 x %op* (%op*)*]*> [#uses=1]
107 declare %op* @foo(%op* %X)
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]
113 ; CHECK-LABEL: @test5(
114 ; CHECK: call %op* @foo(%op* %O)
119 ; InstCombine can not 'load (cast P)' -> cast (load P)' if the cast changes
122 define i8 @test6(i8 addrspace(1)* %source) {
124 %arrayidx223 = addrspacecast i8 addrspace(1)* %source to i8*
125 %tmp4 = load i8, i8* %arrayidx223
127 ; CHECK-LABEL: @test6(
128 ; CHECK: load i8, i8* %arrayidx223