[opaque pointer type] Add textual IR support for explicit type parameter for global...
[oota-llvm.git] / test / Transforms / InstCombine / range-check.ll
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2
3 ; Check simplification of
4 ; (icmp sgt x, -1) & (icmp sgt/sge n, x) --> icmp ugt/uge n, x
5
6 ; CHECK-LABEL: define i1 @test_and1
7 ; CHECK: [[R:%[0-9]+]] = icmp ugt i32 %nn, %x
8 ; CHECK: ret i1 [[R]]
9 define i1 @test_and1(i32 %x, i32 %n) {
10   %nn = and i32 %n, 2147483647
11   %a = icmp sge i32 %x, 0
12   %b = icmp slt i32 %x, %nn
13   %c = and i1 %a, %b
14   ret i1 %c
15 }
16
17 ; CHECK-LABEL: define i1 @test_and2
18 ; CHECK: [[R:%[0-9]+]] = icmp uge i32 %nn, %x
19 ; CHECK: ret i1 [[R]]
20 define i1 @test_and2(i32 %x, i32 %n) {
21   %nn = and i32 %n, 2147483647
22   %a = icmp sgt i32 %x, -1
23   %b = icmp sle i32 %x, %nn
24   %c = and i1 %a, %b
25   ret i1 %c
26 }
27
28 ; CHECK-LABEL: define i1 @test_and3
29 ; CHECK: [[R:%[0-9]+]] = icmp ugt i32 %nn, %x
30 ; CHECK: ret i1 [[R]]
31 define i1 @test_and3(i32 %x, i32 %n) {
32   %nn = and i32 %n, 2147483647
33   %a = icmp sgt i32 %nn, %x
34   %b = icmp sge i32 %x, 0
35   %c = and i1 %a, %b
36   ret i1 %c
37 }
38
39 ; CHECK-LABEL: define i1 @test_and4
40 ; CHECK: [[R:%[0-9]+]] = icmp uge i32 %nn, %x
41 ; CHECK: ret i1 [[R]]
42 define i1 @test_and4(i32 %x, i32 %n) {
43   %nn = and i32 %n, 2147483647
44   %a = icmp sge i32 %nn, %x
45   %b = icmp sge i32 %x, 0
46   %c = and i1 %a, %b
47   ret i1 %c
48 }
49
50 ; CHECK-LABEL: define i1 @test_or1
51 ; CHECK: [[R:%[0-9]+]] = icmp ule i32 %nn, %x
52 ; CHECK: ret i1 [[R]]
53 define i1 @test_or1(i32 %x, i32 %n) {
54   %nn = and i32 %n, 2147483647
55   %a = icmp slt i32 %x, 0
56   %b = icmp sge i32 %x, %nn
57   %c = or i1 %a, %b
58   ret i1 %c
59 }
60
61 ; CHECK-LABEL: define i1 @test_or2
62 ; CHECK: [[R:%[0-9]+]] = icmp ult i32 %nn, %x
63 ; CHECK: ret i1 [[R]]
64 define i1 @test_or2(i32 %x, i32 %n) {
65   %nn = and i32 %n, 2147483647
66   %a = icmp sle i32 %x, -1
67   %b = icmp sgt i32 %x, %nn
68   %c = or i1 %a, %b
69   ret i1 %c
70 }
71
72 ; CHECK-LABEL: define i1 @test_or3
73 ; CHECK: [[R:%[0-9]+]] = icmp ule i32 %nn, %x
74 ; CHECK: ret i1 [[R]]
75 define i1 @test_or3(i32 %x, i32 %n) {
76   %nn = and i32 %n, 2147483647
77   %a = icmp sle i32 %nn, %x
78   %b = icmp slt i32 %x, 0
79   %c = or i1 %a, %b
80   ret i1 %c
81 }
82
83 ; CHECK-LABEL: define i1 @test_or4
84 ; CHECK: [[R:%[0-9]+]] = icmp ult i32 %nn, %x
85 ; CHECK: ret i1 [[R]]
86 define i1 @test_or4(i32 %x, i32 %n) {
87   %nn = and i32 %n, 2147483647
88   %a = icmp slt i32 %nn, %x
89   %b = icmp slt i32 %x, 0
90   %c = or i1 %a, %b
91   ret i1 %c
92 }
93
94 ; Negative tests
95
96 ; CHECK-LABEL: define i1 @negative1
97 ; CHECK: %a = icmp
98 ; CHECK: %b = icmp
99 ; CHECK: %c = and i1 %a, %b
100 ; CHECK: ret i1 %c
101 define i1 @negative1(i32 %x, i32 %n) {
102   %nn = and i32 %n, 2147483647
103   %a = icmp slt i32 %x, %nn
104   %b = icmp sgt i32 %x, 0      ; should be: icmp sge
105   %c = and i1 %a, %b
106   ret i1 %c
107 }
108
109 ; CHECK-LABEL: define i1 @negative2
110 ; CHECK: %a = icmp
111 ; CHECK: %b = icmp
112 ; CHECK: %c = and i1 %a, %b
113 ; CHECK: ret i1 %c
114 define i1 @negative2(i32 %x, i32 %n) {
115   %a = icmp slt i32 %x, %n     ; n can be negative
116   %b = icmp sge i32 %x, 0
117   %c = and i1 %a, %b
118   ret i1 %c
119 }
120
121 ; CHECK-LABEL: define i1 @negative3
122 ; CHECK: %a = icmp
123 ; CHECK: %b = icmp
124 ; CHECK: %c = and i1 %a, %b
125 ; CHECK: ret i1 %c
126 define i1 @negative3(i32 %x, i32 %y, i32 %n) {
127   %nn = and i32 %n, 2147483647
128   %a = icmp slt i32 %x, %nn
129   %b = icmp sge i32 %y, 0      ; should compare %x and not %y
130   %c = and i1 %a, %b
131   ret i1 %c
132 }
133
134 ; CHECK-LABEL: define i1 @negative4
135 ; CHECK: %a = icmp
136 ; CHECK: %b = icmp
137 ; CHECK: %c = and i1 %a, %b
138 ; CHECK: ret i1 %c
139 define i1 @negative4(i32 %x, i32 %n) {
140   %nn = and i32 %n, 2147483647
141   %a = icmp ne i32 %x, %nn     ; should be: icmp slt/sle
142   %b = icmp sge i32 %x, 0
143   %c = and i1 %a, %b
144   ret i1 %c
145 }
146
147 ; CHECK-LABEL: define i1 @negative5
148 ; CHECK: %a = icmp
149 ; CHECK: %b = icmp
150 ; CHECK: %c = or i1 %a, %b
151 ; CHECK: ret i1 %c
152 define i1 @negative5(i32 %x, i32 %n) {
153   %nn = and i32 %n, 2147483647
154   %a = icmp slt i32 %x, %nn
155   %b = icmp sge i32 %x, 0
156   %c = or i1 %a, %b            ; should be: and
157   ret i1 %c
158 }
159