1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6 ; %a is negative, %b is positive
7 define i16 @oppositesign(i16 %x, i16 %y) {
8 ; CHECK-LABEL: @oppositesign(
9 ; CHECK-NEXT: [[A:%.*]] = or i16 [[X:%.*]], -32768
10 ; CHECK-NEXT: [[B:%.*]] = and i16 [[Y:%.*]], 32767
11 ; CHECK-NEXT: [[C:%.*]] = add nsw i16 [[A]], [[B]]
12 ; CHECK-NEXT: ret i16 [[C]]
15 %b = and i16 %y, 32767
20 define i16 @zero_sign_bit(i16 %a) {
21 ; CHECK-LABEL: @zero_sign_bit(
22 ; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[A:%.*]], 32767
23 ; CHECK-NEXT: [[TMP2:%.*]] = add nuw i16 [[TMP1]], 512
24 ; CHECK-NEXT: ret i16 [[TMP2]]
26 %1 = and i16 %a, 32767
31 define i16 @zero_sign_bit2(i16 %a, i16 %b) {
32 ; CHECK-LABEL: @zero_sign_bit2(
33 ; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[A:%.*]], 32767
34 ; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[B:%.*]], 32767
35 ; CHECK-NEXT: [[TMP3:%.*]] = add nuw i16 [[TMP1]], [[TMP2]]
36 ; CHECK-NEXT: ret i16 [[TMP3]]
38 %1 = and i16 %a, 32767
39 %2 = and i16 %b, 32767
44 declare i16 @bounded(i16 %input);
45 declare i32 @__gxx_personality_v0(...);
46 !0 = !{i16 0, i16 32768} ; [0, 32767]
47 !1 = !{i16 0, i16 32769} ; [0, 32768]
49 define i16 @add_bounded_values(i16 %a, i16 %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
50 ; CHECK-LABEL: @add_bounded_values(
52 ; CHECK-NEXT: [[C:%.*]] = call i16 @bounded(i16 [[A:%.*]]), !range [[RNG0:![0-9]+]]
53 ; CHECK-NEXT: [[D:%.*]] = invoke i16 @bounded(i16 [[B:%.*]])
54 ; CHECK-NEXT: to label [[CONT:%.*]] unwind label [[LPAD:%.*]], !range [[RNG0]]
56 ; CHECK-NEXT: [[E:%.*]] = add nuw i16 [[C]], [[D]]
57 ; CHECK-NEXT: ret i16 [[E]]
59 ; CHECK-NEXT: [[TMP0:%.*]] = landingpad { i8*, i32 }
60 ; CHECK-NEXT: filter [0 x i8*] zeroinitializer
61 ; CHECK-NEXT: ret i16 42
64 %c = call i16 @bounded(i16 %a), !range !0
65 %d = invoke i16 @bounded(i16 %b) to label %cont unwind label %lpad, !range !0
67 ; %c and %d are in [0, 32767]. Therefore, %c + %d doesn't unsigned overflow.
71 %0 = landingpad { i8*, i32 }
72 filter [0 x i8*] zeroinitializer
76 define i16 @add_bounded_values_2(i16 %a, i16 %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
77 ; CHECK-LABEL: @add_bounded_values_2(
79 ; CHECK-NEXT: [[C:%.*]] = call i16 @bounded(i16 [[A:%.*]]), !range [[RNG1:![0-9]+]]
80 ; CHECK-NEXT: [[D:%.*]] = invoke i16 @bounded(i16 [[B:%.*]])
81 ; CHECK-NEXT: to label [[CONT:%.*]] unwind label [[LPAD:%.*]], !range [[RNG1]]
83 ; CHECK-NEXT: [[E:%.*]] = add i16 [[C]], [[D]]
84 ; CHECK-NEXT: ret i16 [[E]]
86 ; CHECK-NEXT: [[TMP0:%.*]] = landingpad { i8*, i32 }
87 ; CHECK-NEXT: filter [0 x i8*] zeroinitializer
88 ; CHECK-NEXT: ret i16 42
91 %c = call i16 @bounded(i16 %a), !range !1
92 %d = invoke i16 @bounded(i16 %b) to label %cont unwind label %lpad, !range !1
94 ; Similar to add_bounded_values, but %c and %d are in [0, 32768]. Therefore,
95 ; %c + %d may unsigned overflow and we cannot add NUW.
99 %0 = landingpad { i8*, i32 }
100 filter [0 x i8*] zeroinitializer
104 ; %a has at most one bit set
105 ; %b has a 0 bit other than the sign bit
106 define i16 @ripple_nsw1(i16 %x, i16 %y) {
107 ; CHECK-LABEL: @ripple_nsw1(
108 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], 1
109 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], -16385
110 ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i16 [[A]], [[B]]
111 ; CHECK-NEXT: ret i16 [[C]]
114 %b = and i16 %x, 49151
119 ; Like the previous test, but flip %a and %b
120 define i16 @ripple_nsw2(i16 %x, i16 %y) {
121 ; CHECK-LABEL: @ripple_nsw2(
122 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], 1
123 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], -16385
124 ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i16 [[B]], [[A]]
125 ; CHECK-NEXT: ret i16 [[C]]
128 %b = and i16 %x, 49151
133 define i16 @ripple_nsw3(i16 %x, i16 %y) {
134 ; CHECK-LABEL: @ripple_nsw3(
135 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], -21845
136 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], 21843
137 ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i16 [[A]], [[B]]
138 ; CHECK-NEXT: ret i16 [[C]]
140 %a = and i16 %y, 43691
141 %b = and i16 %x, 21843
146 ; Like the previous test, but flip %a and %b
147 define i16 @ripple_nsw4(i16 %x, i16 %y) {
148 ; CHECK-LABEL: @ripple_nsw4(
149 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], -21845
150 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], 21843
151 ; CHECK-NEXT: [[C:%.*]] = add nuw nsw i16 [[B]], [[A]]
152 ; CHECK-NEXT: ret i16 [[C]]
154 %a = and i16 %y, 43691
155 %b = and i16 %x, 21843
160 define i16 @ripple_nsw5(i16 %x, i16 %y) {
161 ; CHECK-LABEL: @ripple_nsw5(
162 ; CHECK-NEXT: [[A:%.*]] = or i16 [[Y:%.*]], -21845
163 ; CHECK-NEXT: [[B:%.*]] = or i16 [[X:%.*]], -10923
164 ; CHECK-NEXT: [[C:%.*]] = add nsw i16 [[A]], [[B]]
165 ; CHECK-NEXT: ret i16 [[C]]
167 %a = or i16 %y, 43691
168 %b = or i16 %x, 54613
173 ; Like the previous test, but flip %a and %b
174 define i16 @ripple_nsw6(i16 %x, i16 %y) {
175 ; CHECK-LABEL: @ripple_nsw6(
176 ; CHECK-NEXT: [[A:%.*]] = or i16 [[Y:%.*]], -21845
177 ; CHECK-NEXT: [[B:%.*]] = or i16 [[X:%.*]], -10923
178 ; CHECK-NEXT: [[C:%.*]] = add nsw i16 [[B]], [[A]]
179 ; CHECK-NEXT: ret i16 [[C]]
181 %a = or i16 %y, 43691
182 %b = or i16 %x, 54613
187 ; We know nothing about %x
188 define i32 @ripple_no_nsw1(i32 %x, i32 %y) {
189 ; CHECK-LABEL: @ripple_no_nsw1(
190 ; CHECK-NEXT: [[A:%.*]] = and i32 [[Y:%.*]], 1
191 ; CHECK-NEXT: [[B:%.*]] = add i32 [[A]], [[X:%.*]]
192 ; CHECK-NEXT: ret i32 [[B]]
199 ; %a has at most one bit set
200 ; %b has a 0 bit, but it is the sign bit
201 define i16 @ripple_no_nsw2(i16 %x, i16 %y) {
202 ; CHECK-LABEL: @ripple_no_nsw2(
203 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], 1
204 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], 32767
205 ; CHECK-NEXT: [[C:%.*]] = add nuw i16 [[A]], [[B]]
206 ; CHECK-NEXT: ret i16 [[C]]
209 %b = and i16 %x, 32767
214 define i16 @ripple_no_nsw3(i16 %x, i16 %y) {
215 ; CHECK-LABEL: @ripple_no_nsw3(
216 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], -21845
217 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], 21845
218 ; CHECK-NEXT: [[C:%.*]] = add i16 [[A]], [[B]]
219 ; CHECK-NEXT: ret i16 [[C]]
221 %a = and i16 %y, 43691
222 %b = and i16 %x, 21845
227 ; Like the previous test, but flip %a and %b
228 define i16 @ripple_no_nsw4(i16 %x, i16 %y) {
229 ; CHECK-LABEL: @ripple_no_nsw4(
230 ; CHECK-NEXT: [[A:%.*]] = and i16 [[Y:%.*]], -21845
231 ; CHECK-NEXT: [[B:%.*]] = and i16 [[X:%.*]], 21845
232 ; CHECK-NEXT: [[C:%.*]] = add i16 [[B]], [[A]]
233 ; CHECK-NEXT: ret i16 [[C]]
235 %a = and i16 %y, 43691
236 %b = and i16 %x, 21845
241 define i16 @ripple_no_nsw5(i16 %x, i16 %y) {
242 ; CHECK-LABEL: @ripple_no_nsw5(
243 ; CHECK-NEXT: [[A:%.*]] = or i16 [[Y:%.*]], -21847
244 ; CHECK-NEXT: [[B:%.*]] = or i16 [[X:%.*]], -10923
245 ; CHECK-NEXT: [[C:%.*]] = add i16 [[A]], [[B]]
246 ; CHECK-NEXT: ret i16 [[C]]
248 %a = or i16 %y, 43689
249 %b = or i16 %x, 54613
254 ; Like the previous test, but flip %a and %b
255 define i16 @ripple_no_nsw6(i16 %x, i16 %y) {
256 ; CHECK-LABEL: @ripple_no_nsw6(
257 ; CHECK-NEXT: [[A:%.*]] = or i16 [[Y:%.*]], -21847
258 ; CHECK-NEXT: [[B:%.*]] = or i16 [[X:%.*]], -10923
259 ; CHECK-NEXT: [[C:%.*]] = add i16 [[B]], [[A]]
260 ; CHECK-NEXT: ret i16 [[C]]
262 %a = or i16 %y, 43689
263 %b = or i16 %x, 54613
268 define i8 @PR38021(i8 %x) {
269 ; CHECK-LABEL: @PR38021(
270 ; CHECK-NEXT: [[CLEAR_TOP_3_BITS:%.*]] = lshr i8 [[X:%.*]], 3
271 ; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i8 [[CLEAR_TOP_3_BITS]], -63
272 ; CHECK-NEXT: ret i8 [[ADD]]
274 %clear_top_3_bits = lshr i8 %x, 3
275 %add = add i8 %clear_top_3_bits, 193 ; 0b11000001