1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S < %s -instcombine | FileCheck %s
3
4; Fold
5;   (X & C) - X
6; to
7;   - (X & ~C)
8;
9; This allows us to possibly hoist said negation further out,
10; and decreases use count of X.
11
12; https://bugs.llvm.org/show_bug.cgi?id=44427
13
14; Base tests
15
16define i8 @t0(i8 %x) {
17; CHECK-LABEL: @t0(
18; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X:%.*]], -43
19; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 0, [[TMP1]]
20; CHECK-NEXT:    ret i8 [[NEGBIAS]]
21;
22  %unbiasedx = and i8 %x, 42
23  %negbias = sub i8 %unbiasedx, %x
24  ret i8 %negbias
25}
26
27define <2 x i8> @t1_vec(<2 x i8> %x) {
28; CHECK-LABEL: @t1_vec(
29; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -43, i8 -43>
30; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP1]]
31; CHECK-NEXT:    ret <2 x i8> [[NEGBIAS]]
32;
33  %unbiasedx = and <2 x i8> %x, <i8 42, i8 42>
34  %negbias = sub <2 x i8> %unbiasedx, %x
35  ret <2 x i8> %negbias
36}
37
38define <2 x i8> @t2_vec_undef(<2 x i8> %x) {
39; CHECK-LABEL: @t2_vec_undef(
40; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -43, i8 undef>
41; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP1]]
42; CHECK-NEXT:    ret <2 x i8> [[NEGBIAS]]
43;
44  %unbiasedx = and <2 x i8> %x, <i8 42, i8 undef>
45  %negbias = sub <2 x i8> %unbiasedx, %x
46  ret <2 x i8> %negbias
47}
48
49define <2 x i8> @t3_vec_nonsplat(<2 x i8> %x) {
50; CHECK-LABEL: @t3_vec_nonsplat(
51; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -43, i8 -45>
52; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP1]]
53; CHECK-NEXT:    ret <2 x i8> [[NEGBIAS]]
54;
55  %unbiasedx = and <2 x i8> %x, <i8 42, i8 44>
56  %negbias = sub <2 x i8> %unbiasedx, %x
57  ret <2 x i8> %negbias
58}
59
60; Extra uses always prevent fold
61
62declare void @use8(i8)
63
64define i8 @n4_extrause(i8 %x) {
65; CHECK-LABEL: @n4_extrause(
66; CHECK-NEXT:    [[UNBIASEDX:%.*]] = and i8 [[X:%.*]], 42
67; CHECK-NEXT:    call void @use8(i8 [[UNBIASEDX]])
68; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
69; CHECK-NEXT:    ret i8 [[NEGBIAS]]
70;
71  %unbiasedx = and i8 %x, 42
72  call void @use8(i8 %unbiasedx)
73  %negbias = sub i8 %unbiasedx, %x
74  ret i8 %negbias
75}
76
77; Negative tests
78
79define i8 @n5(i8 %x) {
80; CHECK-LABEL: @n5(
81; CHECK-NEXT:    [[NEGBIAS:%.*]] = and i8 [[X:%.*]], -43
82; CHECK-NEXT:    ret i8 [[NEGBIAS]]
83;
84  %unbiasedx = and i8 %x, 42
85  %negbias = sub i8 %x, %unbiasedx ; wrong order
86  ret i8 %negbias
87}
88
89define i8 @n6(i8 %x0, i8 %x1) {
90; CHECK-LABEL: @n6(
91; CHECK-NEXT:    [[UNBIASEDX:%.*]] = and i8 [[X1:%.*]], 42
92; CHECK-NEXT:    [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X0:%.*]]
93; CHECK-NEXT:    ret i8 [[NEGBIAS]]
94;
95  %unbiasedx = and i8 %x1, 42 ; not %x0
96  %negbias = sub i8 %unbiasedx, %x0 ; not %x1
97  ret i8 %negbias
98}
99