1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 declare void @use_v2i4(<2 x i4>)
7 declare i1 @use32gen1(i32)
9 ; Constant can be freely negated.
10 define i8 @t0(i8 %x) {
12 ; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], 42
13 ; CHECK-NEXT: ret i8 [[T0]]
19 ; Negation can be negated for free
20 define i8 @t1(i8 %x, i8 %y) {
22 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
23 ; CHECK-NEXT: call void @use8(i8 [[T0]])
24 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[X:%.*]], [[Y]]
25 ; CHECK-NEXT: ret i8 [[T1]]
28 call void @use8(i8 %t0)
33 ; Shift-left can be negated if all uses can be updated
34 define i8 @t2(i8 %x, i8 %y) {
36 ; CHECK-NEXT: [[T0_NEG:%.*]] = shl i8 42, [[Y:%.*]]
37 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
38 ; CHECK-NEXT: ret i8 [[T1]]
44 define i8 @n2(i8 %x, i8 %y) {
46 ; CHECK-NEXT: [[T0:%.*]] = shl i8 -42, [[Y:%.*]]
47 ; CHECK-NEXT: call void @use8(i8 [[T0]])
48 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
49 ; CHECK-NEXT: ret i8 [[T1]]
52 call void @use8(i8 %t0)
56 define i8 @t3(i8 %x, i8 %y, i8 %z) {
58 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]]
59 ; CHECK-NEXT: call void @use8(i8 [[T0]])
60 ; CHECK-NEXT: [[T1_NEG:%.*]] = shl i8 [[Z]], [[Y:%.*]]
61 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
62 ; CHECK-NEXT: ret i8 [[T2]]
65 call void @use8(i8 %t0)
70 define i8 @n3(i8 %x, i8 %y, i8 %z) {
72 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]]
73 ; CHECK-NEXT: call void @use8(i8 [[T0]])
74 ; CHECK-NEXT: [[T1:%.*]] = shl i8 [[T0]], [[Y:%.*]]
75 ; CHECK-NEXT: call void @use8(i8 [[T1]])
76 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
77 ; CHECK-NEXT: ret i8 [[T2]]
80 call void @use8(i8 %t0)
82 call void @use8(i8 %t1)
87 ; Select can be negated if all it's operands can be negated and all the users of select can be updated
88 define i8 @t4(i8 %x, i1 %y) {
90 ; CHECK-NEXT: [[T0_NEG:%.*]] = select i1 [[Y:%.*]], i8 42, i8 -44
91 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
92 ; CHECK-NEXT: ret i8 [[T1]]
94 %t0 = select i1 %y, i8 -42, i8 44
99 define i8 @select_of_constants_multi_use(i1 %b) {
100 ; CHECK-LABEL: @select_of_constants_multi_use(
101 ; CHECK-NEXT: [[S_NEG:%.*]] = select i1 [[B:%.*]], i8 -42, i8 2
102 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B]], i8 42, i8 -2
103 ; CHECK-NEXT: call void @use8(i8 [[S]])
104 ; CHECK-NEXT: ret i8 [[S_NEG]]
106 %s = select i1 %b, i8 42, i8 -2
107 call void @use8(i8 %s)
112 define i32 @PR52261(i1 %b) {
113 ; CHECK-LABEL: @PR52261(
114 ; CHECK-NEXT: ret i32 2
116 %s = select i1 %b, i32 2, i32 -2
117 %n = sub nsw i32 0, %s
122 define i8 @n4(i8 %x, i1 %y) {
124 ; CHECK-NEXT: [[T0:%.*]] = select i1 [[Y:%.*]], i8 -42, i8 44
125 ; CHECK-NEXT: call void @use8(i8 [[T0]])
126 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
127 ; CHECK-NEXT: ret i8 [[T1]]
129 %t0 = select i1 %y, i8 -42, i8 44
130 call void @use8(i8 %t0)
134 define i8 @n5(i8 %x, i1 %y, i8 %z) {
136 ; CHECK-NEXT: [[T0:%.*]] = select i1 [[Y:%.*]], i8 -42, i8 [[Z:%.*]]
137 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
138 ; CHECK-NEXT: ret i8 [[T1]]
140 %t0 = select i1 %y, i8 -42, i8 %z
144 define i8 @t6(i8 %x, i1 %y, i8 %z) {
146 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]]
147 ; CHECK-NEXT: call void @use8(i8 [[T0]])
148 ; CHECK-NEXT: [[T1_NEG:%.*]] = select i1 [[Y:%.*]], i8 42, i8 [[Z]]
149 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
150 ; CHECK-NEXT: ret i8 [[T2]]
153 call void @use8(i8 %t0)
154 %t1 = select i1 %y, i8 -42, i8 %t0
158 define i8 @t7(i8 %x, i1 %y, i8 %z) {
160 ; CHECK-NEXT: [[T0_NEG:%.*]] = shl i8 -1, [[Z:%.*]]
161 ; CHECK-NEXT: [[T1_NEG:%.*]] = select i1 [[Y:%.*]], i8 0, i8 [[T0_NEG]]
162 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
163 ; CHECK-NEXT: ret i8 [[T2]]
166 %t1 = select i1 %y, i8 0, i8 %t0
170 define i8 @n8(i8 %x, i1 %y, i8 %z) {
172 ; CHECK-NEXT: [[T0:%.*]] = shl i8 1, [[Z:%.*]]
173 ; CHECK-NEXT: call void @use8(i8 [[T0]])
174 ; CHECK-NEXT: [[T1:%.*]] = select i1 [[Y:%.*]], i8 0, i8 [[T0]]
175 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
176 ; CHECK-NEXT: ret i8 [[T2]]
179 call void @use8(i8 %t0)
180 %t1 = select i1 %y, i8 0, i8 %t0
185 ; Subtraction can be negated by swapping its operands.
186 ; x - (y - z) -> x - y + z -> x + (z - y)
187 define i8 @t9(i8 %x, i8 %y) {
189 ; CHECK-NEXT: [[T0_NEG:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
190 ; CHECK-NEXT: ret i8 [[T0_NEG]]
197 define i8 @n10(i8 %x, i8 %y, i8 %z) {
199 ; CHECK-NEXT: [[T0:%.*]] = sub i8 [[Y:%.*]], [[X:%.*]]
200 ; CHECK-NEXT: call void @use8(i8 [[T0]])
201 ; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[T0]]
202 ; CHECK-NEXT: ret i8 [[T1]]
205 call void @use8(i8 %t0)
210 define i8 @neg_of_sub_from_constant(i8 %x) {
211 ; CHECK-LABEL: @neg_of_sub_from_constant(
212 ; CHECK-NEXT: [[S_NEG:%.*]] = add i8 [[X:%.*]], -42
213 ; CHECK-NEXT: ret i8 [[S_NEG]]
220 define i8 @neg_of_sub_from_constant_multi_use(i8 %x) {
221 ; CHECK-LABEL: @neg_of_sub_from_constant_multi_use(
222 ; CHECK-NEXT: [[S_NEG:%.*]] = add i8 [[X:%.*]], -42
223 ; CHECK-NEXT: [[S:%.*]] = sub i8 42, [[X]]
224 ; CHECK-NEXT: call void @use8(i8 [[S]])
225 ; CHECK-NEXT: ret i8 [[S_NEG]]
228 call void @use8(i8 %s)
233 define i8 @sub_from_constant_of_sub_from_constant(i8 %x) {
234 ; CHECK-LABEL: @sub_from_constant_of_sub_from_constant(
235 ; CHECK-NEXT: [[R:%.*]] = add i8 [[X:%.*]], -31
236 ; CHECK-NEXT: ret i8 [[R]]
243 define i8 @sub_from_constant_of_sub_from_constant_multi_use(i8 %x) {
244 ; CHECK-LABEL: @sub_from_constant_of_sub_from_constant_multi_use(
245 ; CHECK-NEXT: [[S:%.*]] = sub i8 42, [[X:%.*]]
246 ; CHECK-NEXT: call void @use8(i8 [[S]])
247 ; CHECK-NEXT: [[R:%.*]] = add i8 [[X]], -31
248 ; CHECK-NEXT: ret i8 [[R]]
251 call void @use8(i8 %s)
256 define i8 @sub_from_variable_of_sub_from_constant(i8 %x, i8 %y) {
257 ; CHECK-LABEL: @sub_from_variable_of_sub_from_constant(
258 ; CHECK-NEXT: [[S_NEG:%.*]] = add i8 [[X:%.*]], -42
259 ; CHECK-NEXT: [[R:%.*]] = add i8 [[S_NEG]], [[Y:%.*]]
260 ; CHECK-NEXT: ret i8 [[R]]
267 define i8 @sub_from_variable_of_sub_from_constant_multi_use(i8 %x, i8 %y) {
268 ; CHECK-LABEL: @sub_from_variable_of_sub_from_constant_multi_use(
269 ; CHECK-NEXT: [[S:%.*]] = sub i8 42, [[X:%.*]]
270 ; CHECK-NEXT: call void @use8(i8 [[S]])
271 ; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y:%.*]], [[S]]
272 ; CHECK-NEXT: ret i8 [[R]]
275 call void @use8(i8 %s)
280 ; Addition can be negated if both operands can be negated
281 ; x - (y + z) -> x - y - z -> x + ((-y) + (-z)))
282 define i8 @t12(i8 %x, i8 %y, i8 %z) {
284 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
285 ; CHECK-NEXT: call void @use8(i8 [[T0]])
286 ; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[Z:%.*]]
287 ; CHECK-NEXT: call void @use8(i8 [[T1]])
288 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y]], [[Z]]
289 ; CHECK-NEXT: [[T3:%.*]] = add i8 [[TMP1]], [[X:%.*]]
290 ; CHECK-NEXT: ret i8 [[T3]]
293 call void @use8(i8 %t0)
295 call void @use8(i8 %t1)
296 %t2 = add i8 %t0, %t1
300 define i8 @n13(i8 %x, i8 %y, i8 %z) {
302 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
303 ; CHECK-NEXT: call void @use8(i8 [[T0]])
304 ; CHECK-NEXT: [[T1_NEG:%.*]] = sub i8 [[Y]], [[Z:%.*]]
305 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
306 ; CHECK-NEXT: ret i8 [[T2]]
309 call void @use8(i8 %t0)
314 define i8 @n14(i8 %x, i8 %y, i8 %z) {
316 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
317 ; CHECK-NEXT: call void @use8(i8 [[T0]])
318 ; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[Z:%.*]]
319 ; CHECK-NEXT: call void @use8(i8 [[T1]])
320 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y]], [[Z]]
321 ; CHECK-NEXT: [[T2:%.*]] = sub i8 0, [[TMP1]]
322 ; CHECK-NEXT: call void @use8(i8 [[T2]])
323 ; CHECK-NEXT: [[T3:%.*]] = add i8 [[TMP1]], [[X:%.*]]
324 ; CHECK-NEXT: ret i8 [[T3]]
327 call void @use8(i8 %t0)
329 call void @use8(i8 %t1)
330 %t2 = add i8 %t0, %t1
331 call void @use8(i8 %t2)
336 define i8 @neg_of_add_with_constant(i8 %x) {
337 ; CHECK-LABEL: @neg_of_add_with_constant(
338 ; CHECK-NEXT: [[R:%.*]] = sub i8 -42, [[X:%.*]]
339 ; CHECK-NEXT: ret i8 [[R]]
346 define i8 @neg_of_add_with_constant_multi_use(i8 %x) {
347 ; CHECK-LABEL: @neg_of_add_with_constant_multi_use(
348 ; CHECK-NEXT: [[S:%.*]] = add i8 [[X:%.*]], 42
349 ; CHECK-NEXT: call void @use8(i8 [[S]])
350 ; CHECK-NEXT: [[R:%.*]] = sub i8 -42, [[X]]
351 ; CHECK-NEXT: ret i8 [[R]]
354 call void @use8(i8 %s)
359 define i8 @sub_from_constant_of_add_with_constant(i8 %x) {
360 ; CHECK-LABEL: @sub_from_constant_of_add_with_constant(
361 ; CHECK-NEXT: [[R:%.*]] = sub i8 -31, [[X:%.*]]
362 ; CHECK-NEXT: ret i8 [[R]]
369 define i8 @sub_from_constant_of_add_with_constant_multi_use(i8 %x) {
370 ; CHECK-LABEL: @sub_from_constant_of_add_with_constant_multi_use(
371 ; CHECK-NEXT: [[S:%.*]] = add i8 [[X:%.*]], 42
372 ; CHECK-NEXT: call void @use8(i8 [[S]])
373 ; CHECK-NEXT: [[R:%.*]] = sub i8 -31, [[X]]
374 ; CHECK-NEXT: ret i8 [[R]]
377 call void @use8(i8 %s)
382 define i8 @sub_from_variable_of_add_with_constant(i8 %x, i8 %y) {
383 ; CHECK-LABEL: @sub_from_variable_of_add_with_constant(
384 ; CHECK-NEXT: [[S:%.*]] = add i8 [[X:%.*]], 42
385 ; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y:%.*]], [[S]]
386 ; CHECK-NEXT: ret i8 [[R]]
393 define i8 @sub_from_variable_of_add_with_constant_multi_use(i8 %x, i8 %y) {
394 ; CHECK-LABEL: @sub_from_variable_of_add_with_constant_multi_use(
395 ; CHECK-NEXT: [[S:%.*]] = add i8 [[X:%.*]], 42
396 ; CHECK-NEXT: call void @use8(i8 [[S]])
397 ; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y:%.*]], [[S]]
398 ; CHECK-NEXT: ret i8 [[R]]
401 call void @use8(i8 %s)
406 ; Multiplication can be negated if either one of operands can be negated
407 ; x - (y * z) -> x + ((-y) * z) or x + ((-z) * y)
408 define i8 @t15(i8 %x, i8 %y, i8 %z) {
410 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
411 ; CHECK-NEXT: call void @use8(i8 [[T0]])
412 ; CHECK-NEXT: [[T1_NEG:%.*]] = mul i8 [[Y]], [[Z:%.*]]
413 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
414 ; CHECK-NEXT: ret i8 [[T2]]
417 call void @use8(i8 %t0)
422 define i8 @n16(i8 %x, i8 %y, i8 %z) {
424 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
425 ; CHECK-NEXT: call void @use8(i8 [[T0]])
426 ; CHECK-NEXT: [[T1:%.*]] = mul i8 [[T0]], [[Z:%.*]]
427 ; CHECK-NEXT: call void @use8(i8 [[T1]])
428 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
429 ; CHECK-NEXT: ret i8 [[T2]]
432 call void @use8(i8 %t0)
434 call void @use8(i8 %t1)
439 ; Phi can be negated if all incoming values can be negated
440 define i8 @t16(i1 %c, i8 %x) {
443 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
445 ; CHECK-NEXT: br label [[END:%.*]]
447 ; CHECK-NEXT: br label [[END]]
449 ; CHECK-NEXT: [[Z_NEG:%.*]] = phi i8 [ [[X:%.*]], [[THEN]] ], [ 42, [[ELSE]] ]
450 ; CHECK-NEXT: ret i8 [[Z_NEG]]
453 br i1 %c, label %then, label %else
460 %z = phi i8 [ %y, %then], [ -42, %else ]
464 define i8 @n17(i1 %c, i8 %x) {
467 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
469 ; CHECK-NEXT: [[Y:%.*]] = sub i8 0, [[X:%.*]]
470 ; CHECK-NEXT: br label [[END:%.*]]
472 ; CHECK-NEXT: br label [[END]]
474 ; CHECK-NEXT: [[Z:%.*]] = phi i8 [ [[Y]], [[THEN]] ], [ -42, [[ELSE]] ]
475 ; CHECK-NEXT: call void @use8(i8 [[Z]])
476 ; CHECK-NEXT: [[N:%.*]] = sub i8 0, [[Z]]
477 ; CHECK-NEXT: ret i8 [[N]]
480 br i1 %c, label %then, label %else
487 %z = phi i8 [ %y, %then], [ -42, %else ]
488 call void @use8(i8 %z)
492 define i8 @n19(i1 %c, i8 %x, i8 %y) {
495 ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]]
497 ; CHECK-NEXT: [[Z:%.*]] = sub i8 0, [[X:%.*]]
498 ; CHECK-NEXT: br label [[END:%.*]]
500 ; CHECK-NEXT: br label [[END]]
502 ; CHECK-NEXT: [[R:%.*]] = phi i8 [ [[Z]], [[THEN]] ], [ [[Y:%.*]], [[ELSE]] ]
503 ; CHECK-NEXT: [[N:%.*]] = sub i8 0, [[R]]
504 ; CHECK-NEXT: ret i8 [[N]]
507 br i1 %c, label %then, label %else
514 %r = phi i8 [ %z, %then], [ %y, %else ]
518 define void @phi_with_duplicate_incoming_basic_blocks(i32 %x, i32 %y, i1 %should_lookup, i32 %z) {
519 ; CHECK-LABEL: @phi_with_duplicate_incoming_basic_blocks(
521 ; CHECK-NEXT: [[X_INC_NEG:%.*]] = xor i32 [[X:%.*]], -1
522 ; CHECK-NEXT: br i1 [[SHOULD_LOOKUP:%.*]], label [[LOOKUP:%.*]], label [[LOOP:%.*]]
524 ; CHECK-NEXT: [[TO_LOOKUP:%.*]] = phi i32 [ [[Y:%.*]], [[ENTRY:%.*]] ], [ [[METAVAL_NEG:%.*]], [[LOOP]] ]
525 ; CHECK-NEXT: switch i32 [[TO_LOOKUP]], label [[END:%.*]] [
526 ; CHECK-NEXT: i32 0, label [[LOOP]]
527 ; CHECK-NEXT: i32 42, label [[LOOP]]
530 ; CHECK-NEXT: [[METAVAL_NEG]] = phi i32 [ [[X_INC_NEG]], [[LOOKUP]] ], [ [[X_INC_NEG]], [[LOOKUP]] ], [ -84, [[ENTRY]] ]
531 ; CHECK-NEXT: [[REPEAT:%.*]] = call i1 @use32gen1(i32 [[METAVAL_NEG]])
532 ; CHECK-NEXT: br i1 [[REPEAT]], label [[LOOKUP]], label [[END]]
534 ; CHECK-NEXT: ret void
537 %x_inc = add i32 %x, 1
538 br i1 %should_lookup, label %lookup, label %loop
541 %to_lookup = phi i32 [ %y, %entry ], [ %negated_metaval, %loop ]
542 switch i32 %to_lookup, label %end [
548 %metaval = phi i32 [ %x_inc, %lookup ], [ %x_inc, %lookup ], [ 84, %entry ]
549 %negated_metaval = sub i32 0, %metaval
550 %repeat = call i1 @use32gen1(i32 %negated_metaval)
551 br i1 %repeat, label %lookup, label %end
557 ; truncation can be negated if it's operand can be negated
558 define i8 @t20(i8 %x, i16 %y) {
560 ; CHECK-NEXT: [[T0_NEG:%.*]] = shl i16 42, [[Y:%.*]]
561 ; CHECK-NEXT: [[T1_NEG:%.*]] = trunc i16 [[T0_NEG]] to i8
562 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
563 ; CHECK-NEXT: ret i8 [[T2]]
565 %t0 = shl i16 -42, %y
566 %t1 = trunc i16 %t0 to i8
570 define i8 @n21(i8 %x, i16 %y) {
572 ; CHECK-NEXT: [[T0:%.*]] = shl i16 -42, [[Y:%.*]]
573 ; CHECK-NEXT: [[T1:%.*]] = trunc i16 [[T0]] to i8
574 ; CHECK-NEXT: call void @use8(i8 [[T1]])
575 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
576 ; CHECK-NEXT: ret i8 [[T2]]
578 %t0 = shl i16 -42, %y
579 %t1 = trunc i16 %t0 to i8
580 call void @use8(i8 %t1)
585 define i4 @negate_xor(i4 %x) {
586 ; CHECK-LABEL: @negate_xor(
587 ; CHECK-NEXT: [[TMP1:%.*]] = xor i4 [[X:%.*]], -6
588 ; CHECK-NEXT: [[O_NEG:%.*]] = add i4 [[TMP1]], 1
589 ; CHECK-NEXT: ret i4 [[O_NEG]]
596 define <2 x i4> @negate_xor_vec(<2 x i4> %x) {
597 ; CHECK-LABEL: @negate_xor_vec(
598 ; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i4> [[X:%.*]], <i4 -6, i4 5>
599 ; CHECK-NEXT: [[O_NEG:%.*]] = add <2 x i4> [[TMP1]], <i4 1, i4 1>
600 ; CHECK-NEXT: ret <2 x i4> [[O_NEG]]
602 %o = xor <2 x i4> %x, <i4 5, i4 10>
603 %r = sub <2 x i4> zeroinitializer, %o
607 define i8 @negate_xor_use(i8 %x) {
608 ; CHECK-LABEL: @negate_xor_use(
609 ; CHECK-NEXT: [[O:%.*]] = xor i8 [[X:%.*]], 5
610 ; CHECK-NEXT: call void @use8(i8 [[O]])
611 ; CHECK-NEXT: [[R:%.*]] = sub i8 0, [[O]]
612 ; CHECK-NEXT: ret i8 [[R]]
615 call void @use8(i8 %o)
620 define i4 @negate_shl_xor(i4 %x, i4 %y) {
621 ; CHECK-LABEL: @negate_shl_xor(
622 ; CHECK-NEXT: [[TMP1:%.*]] = xor i4 [[X:%.*]], -6
623 ; CHECK-NEXT: [[O_NEG:%.*]] = add i4 [[TMP1]], 1
624 ; CHECK-NEXT: [[S_NEG:%.*]] = shl i4 [[O_NEG]], [[Y:%.*]]
625 ; CHECK-NEXT: ret i4 [[S_NEG]]
633 define i8 @negate_shl_not_uses(i8 %x, i8 %y) {
634 ; CHECK-LABEL: @negate_shl_not_uses(
635 ; CHECK-NEXT: [[O_NEG:%.*]] = add i8 [[X:%.*]], 1
636 ; CHECK-NEXT: [[O:%.*]] = xor i8 [[X]], -1
637 ; CHECK-NEXT: call void @use8(i8 [[O]])
638 ; CHECK-NEXT: [[S_NEG:%.*]] = shl i8 [[O_NEG]], [[Y:%.*]]
639 ; CHECK-NEXT: ret i8 [[S_NEG]]
642 call void @use8(i8 %o)
648 define <2 x i4> @negate_mul_not_uses_vec(<2 x i4> %x, <2 x i4> %y) {
649 ; CHECK-LABEL: @negate_mul_not_uses_vec(
650 ; CHECK-NEXT: [[O_NEG:%.*]] = add <2 x i4> [[X:%.*]], <i4 1, i4 1>
651 ; CHECK-NEXT: [[O:%.*]] = xor <2 x i4> [[X]], <i4 -1, i4 -1>
652 ; CHECK-NEXT: call void @use_v2i4(<2 x i4> [[O]])
653 ; CHECK-NEXT: [[S_NEG:%.*]] = mul <2 x i4> [[O_NEG]], [[Y:%.*]]
654 ; CHECK-NEXT: ret <2 x i4> [[S_NEG]]
656 %o = xor <2 x i4> %x, <i4 -1, i4 -1>
657 call void @use_v2i4(<2 x i4> %o)
658 %s = mul <2 x i4> %o, %y
659 %r = sub <2 x i4> zeroinitializer, %s
663 ; signed division can be negated if divisor can be negated and is not 1/-1
664 define i8 @negate_sdiv(i8 %x, i8 %y) {
665 ; CHECK-LABEL: @negate_sdiv(
666 ; CHECK-NEXT: [[T0_NEG:%.*]] = sdiv i8 [[Y:%.*]], -42
667 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
668 ; CHECK-NEXT: ret i8 [[T1]]
674 define i8 @negate_sdiv_extrause(i8 %x, i8 %y) {
675 ; CHECK-LABEL: @negate_sdiv_extrause(
676 ; CHECK-NEXT: [[T0:%.*]] = sdiv i8 [[Y:%.*]], 42
677 ; CHECK-NEXT: call void @use8(i8 [[T0]])
678 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
679 ; CHECK-NEXT: ret i8 [[T1]]
682 call void @use8(i8 %t0)
686 define i8 @negate_sdiv_extrause2(i8 %x, i8 %y) {
687 ; CHECK-LABEL: @negate_sdiv_extrause2(
688 ; CHECK-NEXT: [[T0:%.*]] = sdiv i8 [[Y:%.*]], 42
689 ; CHECK-NEXT: call void @use8(i8 [[T0]])
690 ; CHECK-NEXT: [[T1:%.*]] = sub nsw i8 0, [[T0]]
691 ; CHECK-NEXT: ret i8 [[T1]]
694 call void @use8(i8 %t0)
699 ; Right-shift sign bit smear is negatible.
700 define i8 @negate_ashr(i8 %x, i8 %y) {
701 ; CHECK-LABEL: @negate_ashr(
702 ; CHECK-NEXT: [[T0_NEG:%.*]] = lshr i8 [[Y:%.*]], 7
703 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
704 ; CHECK-NEXT: ret i8 [[T1]]
710 define i8 @negate_lshr(i8 %x, i8 %y) {
711 ; CHECK-LABEL: @negate_lshr(
712 ; CHECK-NEXT: [[T0_NEG:%.*]] = ashr i8 [[Y:%.*]], 7
713 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
714 ; CHECK-NEXT: ret i8 [[T1]]
720 define i8 @negate_ashr_extrause(i8 %x, i8 %y) {
721 ; CHECK-LABEL: @negate_ashr_extrause(
722 ; CHECK-NEXT: [[T0:%.*]] = ashr i8 [[Y:%.*]], 7
723 ; CHECK-NEXT: call void @use8(i8 [[T0]])
724 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
725 ; CHECK-NEXT: ret i8 [[T1]]
728 call void @use8(i8 %t0)
732 define i8 @negate_lshr_extrause(i8 %x, i8 %y) {
733 ; CHECK-LABEL: @negate_lshr_extrause(
734 ; CHECK-NEXT: [[T0:%.*]] = lshr i8 [[Y:%.*]], 7
735 ; CHECK-NEXT: call void @use8(i8 [[T0]])
736 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
737 ; CHECK-NEXT: ret i8 [[T1]]
740 call void @use8(i8 %t0)
744 define i8 @negate_ashr_wrongshift(i8 %x, i8 %y) {
745 ; CHECK-LABEL: @negate_ashr_wrongshift(
746 ; CHECK-NEXT: [[T0:%.*]] = ashr i8 [[Y:%.*]], 6
747 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
748 ; CHECK-NEXT: ret i8 [[T1]]
754 define i8 @negate_lshr_wrongshift(i8 %x, i8 %y) {
755 ; CHECK-LABEL: @negate_lshr_wrongshift(
756 ; CHECK-NEXT: [[T0:%.*]] = lshr i8 [[Y:%.*]], 6
757 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
758 ; CHECK-NEXT: ret i8 [[T1]]
765 ; *ext of i1 is always negatible
766 define i8 @negate_sext(i8 %x, i1 %y) {
767 ; CHECK-LABEL: @negate_sext(
768 ; CHECK-NEXT: [[T0_NEG:%.*]] = zext i1 [[Y:%.*]] to i8
769 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
770 ; CHECK-NEXT: ret i8 [[T1]]
772 %t0 = sext i1 %y to i8
776 define i8 @negate_zext(i8 %x, i1 %y) {
777 ; CHECK-LABEL: @negate_zext(
778 ; CHECK-NEXT: [[T0_NEG:%.*]] = sext i1 [[Y:%.*]] to i8
779 ; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0_NEG]], [[X:%.*]]
780 ; CHECK-NEXT: ret i8 [[T1]]
782 %t0 = zext i1 %y to i8
786 define i8 @negate_sext_extrause(i8 %x, i1 %y) {
787 ; CHECK-LABEL: @negate_sext_extrause(
788 ; CHECK-NEXT: [[T0:%.*]] = sext i1 [[Y:%.*]] to i8
789 ; CHECK-NEXT: call void @use8(i8 [[T0]])
790 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
791 ; CHECK-NEXT: ret i8 [[T1]]
793 %t0 = sext i1 %y to i8
794 call void @use8(i8 %t0)
798 define i8 @negate_zext_extrause(i8 %x, i1 %y) {
799 ; CHECK-LABEL: @negate_zext_extrause(
800 ; CHECK-NEXT: [[T0:%.*]] = zext i1 [[Y:%.*]] to i8
801 ; CHECK-NEXT: call void @use8(i8 [[T0]])
802 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
803 ; CHECK-NEXT: ret i8 [[T1]]
805 %t0 = zext i1 %y to i8
806 call void @use8(i8 %t0)
810 define i8 @negate_sext_wrongwidth(i8 %x, i2 %y) {
811 ; CHECK-LABEL: @negate_sext_wrongwidth(
812 ; CHECK-NEXT: [[T0:%.*]] = sext i2 [[Y:%.*]] to i8
813 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
814 ; CHECK-NEXT: ret i8 [[T1]]
816 %t0 = sext i2 %y to i8
820 define i8 @negate_zext_wrongwidth(i8 %x, i2 %y) {
821 ; CHECK-LABEL: @negate_zext_wrongwidth(
822 ; CHECK-NEXT: [[T0:%.*]] = zext i2 [[Y:%.*]] to i8
823 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]]
824 ; CHECK-NEXT: ret i8 [[T1]]
826 %t0 = zext i2 %y to i8
831 define <2 x i4> @negate_shufflevector_oneinput_reverse(<2 x i4> %x, <2 x i4> %y) {
832 ; CHECK-LABEL: @negate_shufflevector_oneinput_reverse(
833 ; CHECK-NEXT: [[T0_NEG:%.*]] = shl <2 x i4> <i4 6, i4 -5>, [[X:%.*]]
834 ; CHECK-NEXT: [[T1_NEG:%.*]] = shufflevector <2 x i4> [[T0_NEG]], <2 x i4> undef, <2 x i32> <i32 1, i32 0>
835 ; CHECK-NEXT: [[T2:%.*]] = add <2 x i4> [[T1_NEG]], [[Y:%.*]]
836 ; CHECK-NEXT: ret <2 x i4> [[T2]]
838 %t0 = shl <2 x i4> <i4 -6, i4 5>, %x
839 %t1 = shufflevector <2 x i4> %t0, <2 x i4> undef, <2 x i32> <i32 1, i32 0>
840 %t2 = sub <2 x i4> %y, %t1
843 define <2 x i4> @negate_shufflevector_oneinput_second_lane_is_undef(<2 x i4> %x, <2 x i4> %y) {
844 ; CHECK-LABEL: @negate_shufflevector_oneinput_second_lane_is_undef(
845 ; CHECK-NEXT: [[T0_NEG:%.*]] = shl <2 x i4> <i4 6, i4 -5>, [[X:%.*]]
846 ; CHECK-NEXT: [[T1_NEG:%.*]] = shufflevector <2 x i4> [[T0_NEG]], <2 x i4> undef, <2 x i32> <i32 0, i32 undef>
847 ; CHECK-NEXT: [[T2:%.*]] = add <2 x i4> [[T1_NEG]], [[Y:%.*]]
848 ; CHECK-NEXT: ret <2 x i4> [[T2]]
850 %t0 = shl <2 x i4> <i4 -6, i4 5>, %x
851 %t1 = shufflevector <2 x i4> %t0, <2 x i4> undef, <2 x i32> <i32 0, i32 2>
852 %t2 = sub <2 x i4> %y, %t1
855 define <2 x i4> @negate_shufflevector_twoinputs(<2 x i4> %x, <2 x i4> %y, <2 x i4> %z) {
856 ; CHECK-LABEL: @negate_shufflevector_twoinputs(
857 ; CHECK-NEXT: [[T0_NEG:%.*]] = shl <2 x i4> <i4 6, i4 -5>, [[X:%.*]]
858 ; CHECK-NEXT: [[T1_NEG:%.*]] = add <2 x i4> [[Y:%.*]], <i4 poison, i4 1>
859 ; CHECK-NEXT: [[T2_NEG:%.*]] = shufflevector <2 x i4> [[T0_NEG]], <2 x i4> [[T1_NEG]], <2 x i32> <i32 0, i32 3>
860 ; CHECK-NEXT: [[T3:%.*]] = add <2 x i4> [[T2_NEG]], [[Z:%.*]]
861 ; CHECK-NEXT: ret <2 x i4> [[T3]]
863 %t0 = shl <2 x i4> <i4 -6, i4 5>, %x
864 %t1 = xor <2 x i4> %y, <i4 -1, i4 -1>
865 %t2 = shufflevector <2 x i4> %t0, <2 x i4> %t1, <2 x i32> <i32 0, i32 3>
866 %t3 = sub <2 x i4> %z, %t2
869 define <2 x i4> @negate_shufflevector_oneinput_extrause(<2 x i4> %x, <2 x i4> %y) {
870 ; CHECK-LABEL: @negate_shufflevector_oneinput_extrause(
871 ; CHECK-NEXT: [[T0:%.*]] = shl <2 x i4> <i4 -6, i4 5>, [[X:%.*]]
872 ; CHECK-NEXT: [[T1:%.*]] = shufflevector <2 x i4> [[T0]], <2 x i4> undef, <2 x i32> <i32 1, i32 0>
873 ; CHECK-NEXT: call void @use_v2i4(<2 x i4> [[T1]])
874 ; CHECK-NEXT: [[T2:%.*]] = sub <2 x i4> [[Y:%.*]], [[T1]]
875 ; CHECK-NEXT: ret <2 x i4> [[T2]]
877 %t0 = shl <2 x i4> <i4 -6, i4 5>, %x
878 %t1 = shufflevector <2 x i4> %t0, <2 x i4> undef, <2 x i32> <i32 1, i32 0>
879 call void @use_v2i4(<2 x i4> %t1)
880 %t2 = sub <2 x i4> %y, %t1
884 ; zext of non-negative can be negated
885 ; sext of non-positive can be negated
886 define i16 @negation_of_zeroext_of_nonnegative(i8 %x) {
887 ; CHECK-LABEL: @negation_of_zeroext_of_nonnegative(
888 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
889 ; CHECK-NEXT: [[T1:%.*]] = icmp sgt i8 [[T0]], -1
890 ; CHECK-NEXT: br i1 [[T1]], label [[NONNEG_BB:%.*]], label [[NEG_BB:%.*]]
892 ; CHECK-NEXT: [[T2:%.*]] = zext i8 [[T0]] to i16
893 ; CHECK-NEXT: [[T3:%.*]] = sub nsw i16 0, [[T2]]
894 ; CHECK-NEXT: ret i16 [[T3]]
896 ; CHECK-NEXT: ret i16 0
899 %t1 = icmp sge i8 %t0, 0
900 br i1 %t1, label %nonneg_bb, label %neg_bb
903 %t2 = zext i8 %t0 to i16
910 define i16 @negation_of_zeroext_of_positive(i8 %x) {
911 ; CHECK-LABEL: @negation_of_zeroext_of_positive(
912 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
913 ; CHECK-NEXT: [[T1:%.*]] = icmp sgt i8 [[T0]], 0
914 ; CHECK-NEXT: br i1 [[T1]], label [[NONNEG_BB:%.*]], label [[NEG_BB:%.*]]
916 ; CHECK-NEXT: [[T2:%.*]] = zext i8 [[T0]] to i16
917 ; CHECK-NEXT: [[T3:%.*]] = sub nsw i16 0, [[T2]]
918 ; CHECK-NEXT: ret i16 [[T3]]
920 ; CHECK-NEXT: ret i16 0
923 %t1 = icmp sgt i8 %t0, 0
924 br i1 %t1, label %nonneg_bb, label %neg_bb
927 %t2 = zext i8 %t0 to i16
934 define i16 @negation_of_signext_of_negative(i8 %x) {
935 ; CHECK-LABEL: @negation_of_signext_of_negative(
936 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
937 ; CHECK-NEXT: [[T1:%.*]] = icmp slt i8 [[T0]], 0
938 ; CHECK-NEXT: br i1 [[T1]], label [[NEG_BB:%.*]], label [[NONNEG_BB:%.*]]
940 ; CHECK-NEXT: [[T2:%.*]] = sext i8 [[T0]] to i16
941 ; CHECK-NEXT: [[T3:%.*]] = sub nsw i16 0, [[T2]]
942 ; CHECK-NEXT: ret i16 [[T3]]
944 ; CHECK-NEXT: ret i16 0
947 %t1 = icmp slt i8 %t0, 0
948 br i1 %t1, label %neg_bb, label %nonneg_bb
951 %t2 = sext i8 %t0 to i16
958 define i16 @negation_of_signext_of_nonpositive(i8 %x) {
959 ; CHECK-LABEL: @negation_of_signext_of_nonpositive(
960 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
961 ; CHECK-NEXT: [[T1:%.*]] = icmp slt i8 [[T0]], 1
962 ; CHECK-NEXT: br i1 [[T1]], label [[NEG_BB:%.*]], label [[NONNEG_BB:%.*]]
964 ; CHECK-NEXT: [[T2:%.*]] = sext i8 [[T0]] to i16
965 ; CHECK-NEXT: [[T3:%.*]] = sub nsw i16 0, [[T2]]
966 ; CHECK-NEXT: ret i16 [[T3]]
968 ; CHECK-NEXT: ret i16 0
971 %t1 = icmp sle i8 %t0, 0
972 br i1 %t1, label %neg_bb, label %nonneg_bb
975 %t2 = sext i8 %t0 to i16
982 define i16 @negation_of_signext_of_nonnegative__wrong_cast(i8 %x) {
983 ; CHECK-LABEL: @negation_of_signext_of_nonnegative__wrong_cast(
984 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
985 ; CHECK-NEXT: [[T1:%.*]] = icmp sgt i8 [[T0]], -1
986 ; CHECK-NEXT: br i1 [[T1]], label [[NONNEG_BB:%.*]], label [[NEG_BB:%.*]]
988 ; CHECK-NEXT: [[T2:%.*]] = sext i8 [[T0]] to i16
989 ; CHECK-NEXT: [[T3:%.*]] = sub nsw i16 0, [[T2]]
990 ; CHECK-NEXT: ret i16 [[T3]]
992 ; CHECK-NEXT: ret i16 0
995 %t1 = icmp sge i8 %t0, 0
996 br i1 %t1, label %nonneg_bb, label %neg_bb
999 %t2 = sext i8 %t0 to i16
1000 %t3 = sub i16 0, %t2
1006 define i16 @negation_of_zeroext_of_negative_wrongcast(i8 %x) {
1007 ; CHECK-LABEL: @negation_of_zeroext_of_negative_wrongcast(
1008 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
1009 ; CHECK-NEXT: [[T1:%.*]] = icmp slt i8 [[T0]], 0
1010 ; CHECK-NEXT: br i1 [[T1]], label [[NEG_BB:%.*]], label [[NONNEG_BB:%.*]]
1012 ; CHECK-NEXT: [[T2:%.*]] = zext i8 [[T0]] to i16
1013 ; CHECK-NEXT: [[T3:%.*]] = sub nsw i16 0, [[T2]]
1014 ; CHECK-NEXT: ret i16 [[T3]]
1016 ; CHECK-NEXT: ret i16 0
1019 %t1 = icmp slt i8 %t0, 0
1020 br i1 %t1, label %neg_bb, label %nonneg_bb
1023 %t2 = zext i8 %t0 to i16
1024 %t3 = sub i16 0, %t2
1031 ; 'or' of 1 and operand with no lowest bit set is 'inc'
1032 define i8 @negation_of_increment_via_or_with_no_common_bits_set(i8 %x, i8 %y) {
1033 ; CHECK-LABEL: @negation_of_increment_via_or_with_no_common_bits_set(
1034 ; CHECK-NEXT: [[T0:%.*]] = shl i8 [[Y:%.*]], 1
1035 ; CHECK-NEXT: [[T1_NEG:%.*]] = xor i8 [[T0]], -1
1036 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
1037 ; CHECK-NEXT: ret i8 [[T2]]
1041 %t2 = sub i8 %x, %t1
1044 define i8 @negation_of_increment_via_or_with_no_common_bits_set_extrause(i8 %x, i8 %y) {
1045 ; CHECK-LABEL: @negation_of_increment_via_or_with_no_common_bits_set_extrause(
1046 ; CHECK-NEXT: [[T0:%.*]] = shl i8 [[Y:%.*]], 1
1047 ; CHECK-NEXT: [[T1:%.*]] = or i8 [[T0]], 1
1048 ; CHECK-NEXT: call void @use8(i8 [[T1]])
1049 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
1050 ; CHECK-NEXT: ret i8 [[T2]]
1054 call void @use8(i8 %t1)
1055 %t2 = sub i8 %x, %t1
1058 define i8 @negation_of_increment_via_or_common_bits_set(i8 %x, i8 %y) {
1059 ; CHECK-LABEL: @negation_of_increment_via_or_common_bits_set(
1060 ; CHECK-NEXT: [[T0:%.*]] = shl i8 [[Y:%.*]], 1
1061 ; CHECK-NEXT: [[T1:%.*]] = or i8 [[T0]], 3
1062 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
1063 ; CHECK-NEXT: ret i8 [[T2]]
1067 %t2 = sub i8 %x, %t1
1071 ; 'or' of operands with no common bits set is 'add'
1072 define i8 @add_via_or_with_no_common_bits_set(i8 %x, i8 %y) {
1073 ; CHECK-LABEL: @add_via_or_with_no_common_bits_set(
1074 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
1075 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1076 ; CHECK-NEXT: [[T1_NEG:%.*]] = shl i8 [[Y]], 2
1077 ; CHECK-NEXT: [[T2_NEG:%.*]] = add i8 [[T1_NEG]], -3
1078 ; CHECK-NEXT: [[T3:%.*]] = add i8 [[T2_NEG]], [[X:%.*]]
1079 ; CHECK-NEXT: ret i8 [[T3]]
1082 call void @use8(i8 %t0)
1085 %t3 = sub i8 %x, %t2
1088 define i8 @add_via_or_with_common_bit_maybe_set(i8 %x, i8 %y) {
1089 ; CHECK-LABEL: @add_via_or_with_common_bit_maybe_set(
1090 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
1091 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1092 ; CHECK-NEXT: [[T1:%.*]] = shl i8 [[T0]], 2
1093 ; CHECK-NEXT: [[T2:%.*]] = or i8 [[T1]], 4
1094 ; CHECK-NEXT: [[T3:%.*]] = sub i8 [[X:%.*]], [[T2]]
1095 ; CHECK-NEXT: ret i8 [[T3]]
1098 call void @use8(i8 %t0)
1101 %t3 = sub i8 %x, %t2
1104 define i8 @add_via_or_with_no_common_bits_set_extrause(i8 %x, i8 %y) {
1105 ; CHECK-LABEL: @add_via_or_with_no_common_bits_set_extrause(
1106 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]]
1107 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1108 ; CHECK-NEXT: [[T1:%.*]] = shl i8 [[T0]], 2
1109 ; CHECK-NEXT: [[T2:%.*]] = or i8 [[T1]], 3
1110 ; CHECK-NEXT: call void @use8(i8 [[T2]])
1111 ; CHECK-NEXT: [[T3:%.*]] = sub i8 [[X:%.*]], [[T2]]
1112 ; CHECK-NEXT: ret i8 [[T3]]
1115 call void @use8(i8 %t0)
1118 call void @use8(i8 %t2)
1119 %t3 = sub i8 %x, %t2
1123 ; `extractelement` is negatible if source operand is negatible.
1124 define i4 @negate_extractelement(<2 x i4> %x, i32 %y, i4 %z) {
1125 ; CHECK-LABEL: @negate_extractelement(
1126 ; CHECK-NEXT: [[T0:%.*]] = sub <2 x i4> zeroinitializer, [[X:%.*]]
1127 ; CHECK-NEXT: call void @use_v2i4(<2 x i4> [[T0]])
1128 ; CHECK-NEXT: [[T1_NEG:%.*]] = extractelement <2 x i4> [[X]], i32 [[Y:%.*]]
1129 ; CHECK-NEXT: [[T2:%.*]] = add i4 [[T1_NEG]], [[Z:%.*]]
1130 ; CHECK-NEXT: ret i4 [[T2]]
1132 %t0 = sub <2 x i4> zeroinitializer, %x
1133 call void @use_v2i4(<2 x i4> %t0)
1134 %t1 = extractelement <2 x i4> %t0, i32 %y
1135 %t2 = sub i4 %z, %t1
1138 define i4 @negate_extractelement_extrause(<2 x i4> %x, i32 %y, i4 %z) {
1139 ; CHECK-LABEL: @negate_extractelement_extrause(
1140 ; CHECK-NEXT: [[T0:%.*]] = sub <2 x i4> zeroinitializer, [[X:%.*]]
1141 ; CHECK-NEXT: call void @use_v2i4(<2 x i4> [[T0]])
1142 ; CHECK-NEXT: [[T1:%.*]] = extractelement <2 x i4> [[T0]], i32 [[Y:%.*]]
1143 ; CHECK-NEXT: call void @use4(i4 [[T1]])
1144 ; CHECK-NEXT: [[T2:%.*]] = sub i4 [[Z:%.*]], [[T1]]
1145 ; CHECK-NEXT: ret i4 [[T2]]
1147 %t0 = sub <2 x i4> zeroinitializer, %x
1148 call void @use_v2i4(<2 x i4> %t0)
1149 %t1 = extractelement <2 x i4> %t0, i32 %y
1150 call void @use4(i4 %t1)
1151 %t2 = sub i4 %z, %t1
1155 ; `insertelement` is negatible if both source vector and element-to-be-inserted are negatible.
1156 define <2 x i4> @negate_insertelement(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
1157 ; CHECK-LABEL: @negate_insertelement(
1158 ; CHECK-NEXT: [[T2_NEG:%.*]] = insertelement <2 x i4> [[SRC:%.*]], i4 [[A:%.*]], i32 [[X:%.*]]
1159 ; CHECK-NEXT: [[T3:%.*]] = add <2 x i4> [[T2_NEG]], [[B:%.*]]
1160 ; CHECK-NEXT: ret <2 x i4> [[T3]]
1162 %t0 = sub <2 x i4> zeroinitializer, %src
1163 %t1 = sub i4 zeroinitializer, %a
1164 %t2 = insertelement <2 x i4> %t0, i4 %t1, i32 %x
1165 %t3 = sub <2 x i4> %b, %t2
1168 define <2 x i4> @negate_insertelement_extrause(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
1169 ; CHECK-LABEL: @negate_insertelement_extrause(
1170 ; CHECK-NEXT: [[T0:%.*]] = sub <2 x i4> zeroinitializer, [[SRC:%.*]]
1171 ; CHECK-NEXT: [[T1:%.*]] = sub i4 0, [[A:%.*]]
1172 ; CHECK-NEXT: [[T2:%.*]] = insertelement <2 x i4> [[T0]], i4 [[T1]], i32 [[X:%.*]]
1173 ; CHECK-NEXT: call void @use_v2i4(<2 x i4> [[T2]])
1174 ; CHECK-NEXT: [[T3:%.*]] = sub <2 x i4> [[B:%.*]], [[T2]]
1175 ; CHECK-NEXT: ret <2 x i4> [[T3]]
1177 %t0 = sub <2 x i4> zeroinitializer, %src
1178 %t1 = sub i4 zeroinitializer, %a
1179 %t2 = insertelement <2 x i4> %t0, i4 %t1, i32 %x
1180 call void @use_v2i4(<2 x i4> %t2)
1181 %t3 = sub <2 x i4> %b, %t2
1184 define <2 x i4> @negate_insertelement_nonnegatible_base(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
1185 ; CHECK-LABEL: @negate_insertelement_nonnegatible_base(
1186 ; CHECK-NEXT: [[T1:%.*]] = sub i4 0, [[A:%.*]]
1187 ; CHECK-NEXT: [[T2:%.*]] = insertelement <2 x i4> [[SRC:%.*]], i4 [[T1]], i32 [[X:%.*]]
1188 ; CHECK-NEXT: [[T3:%.*]] = sub <2 x i4> [[B:%.*]], [[T2]]
1189 ; CHECK-NEXT: ret <2 x i4> [[T3]]
1191 %t1 = sub i4 zeroinitializer, %a
1192 %t2 = insertelement <2 x i4> %src, i4 %t1, i32 %x
1193 %t3 = sub <2 x i4> %b, %t2
1196 define <2 x i4> @negate_insertelement_nonnegatible_insert(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
1197 ; CHECK-LABEL: @negate_insertelement_nonnegatible_insert(
1198 ; CHECK-NEXT: [[T0:%.*]] = sub <2 x i4> zeroinitializer, [[SRC:%.*]]
1199 ; CHECK-NEXT: [[T2:%.*]] = insertelement <2 x i4> [[T0]], i4 [[A:%.*]], i32 [[X:%.*]]
1200 ; CHECK-NEXT: [[T3:%.*]] = sub <2 x i4> [[B:%.*]], [[T2]]
1201 ; CHECK-NEXT: ret <2 x i4> [[T3]]
1203 %t0 = sub <2 x i4> zeroinitializer, %src
1204 %t2 = insertelement <2 x i4> %t0, i4 %a, i32 %x
1205 %t3 = sub <2 x i4> %b, %t2
1209 ; left-shift by constant can always be negated
1210 define i8 @negate_left_shift_by_constant_prefer_keeping_shl(i8 %x, i8 %y, i8 %z) {
1211 ; CHECK-LABEL: @negate_left_shift_by_constant_prefer_keeping_shl(
1212 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]]
1213 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1214 ; CHECK-NEXT: [[T1_NEG:%.*]] = shl i8 [[Z]], 4
1215 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
1216 ; CHECK-NEXT: ret i8 [[T2]]
1219 call void @use8(i8 %t0)
1221 %t2 = sub i8 %x, %t1
1224 define i8 @negate_left_shift_by_constant_prefer_keeping_shl_extrause(i8 %x, i8 %y, i8 %z) {
1225 ; CHECK-LABEL: @negate_left_shift_by_constant_prefer_keeping_shl_extrause(
1226 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]]
1227 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1228 ; CHECK-NEXT: [[T1:%.*]] = shl i8 [[T0]], 4
1229 ; CHECK-NEXT: call void @use8(i8 [[T1]])
1230 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
1231 ; CHECK-NEXT: ret i8 [[T2]]
1234 call void @use8(i8 %t0)
1236 call void @use8(i8 %t1)
1237 %t2 = sub i8 %x, %t1
1240 define i8 @negate_left_shift_by_constant(i8 %x, i8 %y, i8 %z, i8 %k) {
1241 ; CHECK-LABEL: @negate_left_shift_by_constant(
1242 ; CHECK-NEXT: [[T0:%.*]] = sub i8 [[K:%.*]], [[Z:%.*]]
1243 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1244 ; CHECK-NEXT: [[T1_NEG:%.*]] = mul i8 [[T0]], -16
1245 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
1246 ; CHECK-NEXT: ret i8 [[T2]]
1249 call void @use8(i8 %t0)
1251 %t2 = sub i8 %x, %t1
1254 define i8 @negate_left_shift_by_constant_extrause(i8 %x, i8 %y, i8 %z, i8 %k) {
1255 ; CHECK-LABEL: @negate_left_shift_by_constant_extrause(
1256 ; CHECK-NEXT: [[T0:%.*]] = sub i8 [[K:%.*]], [[Z:%.*]]
1257 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1258 ; CHECK-NEXT: [[T1:%.*]] = shl i8 [[T0]], 4
1259 ; CHECK-NEXT: call void @use8(i8 [[T1]])
1260 ; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]]
1261 ; CHECK-NEXT: ret i8 [[T2]]
1264 call void @use8(i8 %t0)
1266 call void @use8(i8 %t1)
1267 %t2 = sub i8 %x, %t1
1271 ; `add` with single negatible operand is still negatible
1272 define i8 @negate_add_with_single_negatible_operand(i8 %x, i8 %y) {
1273 ; CHECK-LABEL: @negate_add_with_single_negatible_operand(
1274 ; CHECK-NEXT: [[T1:%.*]] = sub i8 -42, [[X:%.*]]
1275 ; CHECK-NEXT: ret i8 [[T1]]
1281 ; do so even if we are two levels deep
1282 define i8 @negate_add_with_single_negatible_operand_depth2(i8 %x, i8 %y) {
1283 ; CHECK-LABEL: @negate_add_with_single_negatible_operand_depth2(
1284 ; CHECK-NEXT: [[T0_NEG:%.*]] = sub i8 -21, [[X:%.*]]
1285 ; CHECK-NEXT: [[T1_NEG:%.*]] = mul i8 [[T0_NEG]], [[Y:%.*]]
1286 ; CHECK-NEXT: ret i8 [[T1_NEG]]
1289 %t1 = mul i8 %t0, %y
1294 define i8 @negate_add_with_single_negatible_operand_extrause(i8 %x, i8 %y) {
1295 ; CHECK-LABEL: @negate_add_with_single_negatible_operand_extrause(
1296 ; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], 42
1297 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1298 ; CHECK-NEXT: [[T1:%.*]] = sub i8 -42, [[X]]
1299 ; CHECK-NEXT: ret i8 [[T1]]
1302 call void @use8(i8 %t0)
1306 ; But don't do this if that means just sinking the negation.
1307 define i8 @negate_add_with_single_negatible_operand_non_negation(i8 %x, i8 %y) {
1308 ; CHECK-LABEL: @negate_add_with_single_negatible_operand_non_negation(
1309 ; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], 42
1310 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[Y:%.*]], [[T0]]
1311 ; CHECK-NEXT: ret i8 [[T1]]
1314 %t1 = sub i8 %y, %t0
1318 ; abs/nabs can be negated
1319 define i8 @negate_abs(i8 %x, i8 %y) {
1320 ; CHECK-LABEL: @negate_abs(
1321 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
1322 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1323 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.abs.i8(i8 [[X]], i1 false)
1324 ; CHECK-NEXT: [[T3:%.*]] = sub i8 [[Y:%.*]], [[TMP1]]
1325 ; CHECK-NEXT: ret i8 [[T3]]
1328 call void @use8(i8 %t0)
1329 %t1 = icmp slt i8 %x, 0
1330 %t2 = select i1 %t1, i8 %t0, i8 %x, !prof !0
1331 %t3 = sub i8 %y, %t2
1334 define i8 @negate_nabs(i8 %x, i8 %y) {
1335 ; CHECK-LABEL: @negate_nabs(
1336 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
1337 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1338 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.abs.i8(i8 [[X]], i1 false)
1339 ; CHECK-NEXT: [[T3:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
1340 ; CHECK-NEXT: ret i8 [[T3]]
1343 call void @use8(i8 %t0)
1344 %t1 = icmp slt i8 %x, 0
1345 %t2 = select i1 %t1, i8 %x, i8 %t0, !prof !0
1346 %t3 = sub i8 %y, %t2
1350 ; And in general, if hands of select are known to be negation of each other,
1351 ; we can negate the select
1352 define i8 @negate_select_of_op_vs_negated_op(i8 %x, i8 %y, i1 %c) {
1353 ; CHECK-LABEL: @negate_select_of_op_vs_negated_op(
1354 ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
1355 ; CHECK-NEXT: call void @use8(i8 [[T0]])
1356 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[T0]], !prof [[PROF0:![0-9]+]]
1357 ; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
1358 ; CHECK-NEXT: ret i8 [[T2]]
1361 call void @use8(i8 %t0)
1362 %t1 = select i1 %c, i8 %t0, i8 %x, !prof !0
1363 %t2 = sub i8 %y, %t1
1366 define i8 @dont_negate_ordinary_select(i8 %x, i8 %y, i8 %z, i1 %c) {
1367 ; CHECK-LABEL: @dont_negate_ordinary_select(
1368 ; CHECK-NEXT: [[T0:%.*]] = select i1 [[C:%.*]], i8 [[X:%.*]], i8 [[Y:%.*]]
1369 ; CHECK-NEXT: [[T1:%.*]] = sub i8 [[Z:%.*]], [[T0]]
1370 ; CHECK-NEXT: ret i8 [[T1]]
1372 %t0 = select i1 %c, i8 %x, i8 %y
1373 %t1 = sub i8 %z, %t0
1377 ; Freeze is transparent as far as negation is concerned
1378 define i4 @negate_freeze(i4 %x, i4 %y, i4 %z) {
1379 ; CHECK-LABEL: @negate_freeze(
1380 ; CHECK-NEXT: [[T0_NEG:%.*]] = sub i4 [[Y:%.*]], [[X:%.*]]
1381 ; CHECK-NEXT: [[T1_NEG:%.*]] = freeze i4 [[T0_NEG]]
1382 ; CHECK-NEXT: [[T2:%.*]] = add i4 [[T1_NEG]], [[Z:%.*]]
1383 ; CHECK-NEXT: ret i4 [[T2]]
1387 %t2 = sub i4 %z, %t1
1390 define i4 @negate_freeze_extrause(i4 %x, i4 %y, i4 %z) {
1391 ; CHECK-LABEL: @negate_freeze_extrause(
1392 ; CHECK-NEXT: [[T0:%.*]] = sub i4 [[X:%.*]], [[Y:%.*]]
1393 ; CHECK-NEXT: [[T1:%.*]] = freeze i4 [[T0]]
1394 ; CHECK-NEXT: call void @use4(i4 [[T1]])
1395 ; CHECK-NEXT: [[T2:%.*]] = sub i4 [[Z:%.*]], [[T1]]
1396 ; CHECK-NEXT: ret i4 [[T2]]
1400 call void @use4(i4 %t1)
1401 %t2 = sub i4 %z, %t1
1405 ; Due to the InstCombine's worklist management, there are no guarantees that
1406 ; each instruction we'll encounter has been visited by InstCombine already.
1407 ; In particular, most importantly for us, that means we have to canonicalize
1408 ; constants to RHS ourselves, since that is helpful sometimes.
1409 ; This used to cause an endless combine loop.
1410 define void @noncanonical_mul_with_constant_as_first_operand() {
1411 ; CHECK-LABEL: @noncanonical_mul_with_constant_as_first_operand(
1412 ; CHECK-NEXT: entry:
1413 ; CHECK-NEXT: br label [[IF_END:%.*]]
1415 ; CHECK-NEXT: br label [[IF_END]]
1421 %e.0 = phi i32 [ undef, %entry ], [ %div, %if.end ]
1422 %conv = trunc i32 %e.0 to i16
1423 %mul.i = mul nsw i16 -1, %conv
1424 %conv1 = sext i16 %mul.i to i32
1425 %div = sub nsw i32 0, %conv1
1429 ; CHECK: !0 = !{!"branch_weights", i32 40, i32 1}
1430 !0 = !{!"branch_weights", i32 40, i32 1}