hoist-negation-out-of-bias-calculation.ll
4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S < %s -instcombine | FileCheck %s
; Fold
; (X & (- Y)) - X
; to
; - (X & (Y - 1))
;
; This allows us to possibly hoist said negation further out.
; https://bugs.llvm.org/show_bug.cgi?id=44448
; Base tests
define i8 @t0(i8 %x, i8 %y) {
; CHECK-LABEL: @t0(
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 0, [[TMP2]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
%unbiasedx = and i8 %negy, %x
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}
declare i8 @gen8()
define i8 @t1_commutative(i8 %y) {
; CHECK-LABEL: @t1_commutative(
; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X]], [[TMP1]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 0, [[TMP2]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%x = call i8 @gen8()
%negy = sub i8 0, %y
%unbiasedx = and i8 %x, %negy ; commutative, swapped
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}
define <2 x i8> @t2_vec(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @t2_vec(
; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[Y:%.*]], <i8 -1, i8 -1>
; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i8> [[TMP1]], [[X:%.*]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP2]]
; CHECK-NEXT: ret <2 x i8> [[NEGBIAS]]
;
%negy = sub <2 x i8> <i8 0, i8 0>, %y
%unbiasedx = and <2 x i8> %negy, %x
%negbias = sub <2 x i8> %unbiasedx, %x
ret <2 x i8> %negbias
}
define <2 x i8> @t3_vec_undef(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @t3_vec_undef(
; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[Y:%.*]], <i8 -1, i8 -1>
; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i8> [[TMP1]], [[X:%.*]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub <2 x i8> zeroinitializer, [[TMP2]]
; CHECK-NEXT: ret <2 x i8> [[NEGBIAS]]
;
%negy = sub <2 x i8> <i8 0, i8 undef>, %y
%unbiasedx = and <2 x i8> %negy, %x
%negbias = sub <2 x i8> %unbiasedx, %x
ret <2 x i8> %negbias
}
; Extra uses always prevent fold
declare void @use8(i8)
define i8 @n4_extrause0(i8 %x, i8 %y) {
; CHECK-LABEL: @n4_extrause0(
; CHECK-NEXT: [[NEGY:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[NEGY]])
; CHECK-NEXT: [[UNBIASEDX:%.*]] = and i8 [[NEGY]], [[X:%.*]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
call void @use8(i8 %negy)
%unbiasedx = and i8 %negy, %x
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}
define i8 @n5_extrause1(i8 %x, i8 %y) {
; CHECK-LABEL: @n5_extrause1(
; CHECK-NEXT: [[NEGY:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: [[UNBIASEDX:%.*]] = and i8 [[NEGY]], [[X:%.*]]
; CHECK-NEXT: call void @use8(i8 [[UNBIASEDX]])
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
%unbiasedx = and i8 %negy, %x
call void @use8(i8 %unbiasedx)
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}
define i8 @n6_extrause2(i8 %x, i8 %y) {
; CHECK-LABEL: @n6_extrause2(
; CHECK-NEXT: [[NEGY:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[NEGY]])
; CHECK-NEXT: [[UNBIASEDX:%.*]] = and i8 [[NEGY]], [[X:%.*]]
; CHECK-NEXT: call void @use8(i8 [[UNBIASEDX]])
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
call void @use8(i8 %negy)
%unbiasedx = and i8 %negy, %x
call void @use8(i8 %unbiasedx)
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}
; Negative tests
define i8 @n7(i8 %x, i8 %y) {
; CHECK-LABEL: @n7(
; CHECK-NEXT: [[NEGY_NOT:%.*]] = add i8 [[Y:%.*]], -1
; CHECK-NEXT: [[NEGBIAS:%.*]] = and i8 [[NEGY_NOT]], [[X:%.*]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
%unbiasedx = and i8 %negy, %x
%negbias = sub i8 %x, %unbiasedx ; wrong order
ret i8 %negbias
}
define i8 @n8(i8 %x, i8 %y) {
; CHECK-LABEL: @n8(
; CHECK-NEXT: [[NEGY:%.*]] = sub i8 1, [[Y:%.*]]
; CHECK-NEXT: [[UNBIASEDX:%.*]] = and i8 [[NEGY]], [[X:%.*]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 1, %y ; not negation
%unbiasedx = and i8 %negy, %x
%negbias = sub i8 %unbiasedx, %x
ret i8 %negbias
}
define i8 @n9(i8 %x0, i8 %x1, i8 %y) {
; CHECK-LABEL: @n9(
; CHECK-NEXT: [[NEGY:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: [[UNBIASEDX:%.*]] = and i8 [[NEGY]], [[X1:%.*]]
; CHECK-NEXT: [[NEGBIAS:%.*]] = sub i8 [[UNBIASEDX]], [[X0:%.*]]
; CHECK-NEXT: ret i8 [[NEGBIAS]]
;
%negy = sub i8 0, %y
%unbiasedx = and i8 %x1, %negy ; not %x0
%negbias = sub i8 %unbiasedx, %x0 ; not %x1
ret i8 %negbias
}