icmp-div-constant.ll
5.26 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
target datalayout = "n32"
define i1 @is_rem2_neg_i8(i8 %x) {
; CHECK-LABEL: @is_rem2_neg_i8(
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -127
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP1]], -127
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i8 %x, 2
%r = icmp slt i8 %s, 0
ret i1 %r
}
define <2 x i1> @is_rem2_pos_v2i8(<2 x i8> %x) {
; CHECK-LABEL: @is_rem2_pos_v2i8(
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -127, i8 -127>
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[TMP1]], <i8 1, i8 1>
; CHECK-NEXT: ret <2 x i1> [[R]]
;
%s = srem <2 x i8> %x, <i8 2, i8 2>
%r = icmp sgt <2 x i8> %s, zeroinitializer
ret <2 x i1> %r
}
; i8 -97 == 159 == 0b10011111
define i1 @is_rem32_pos_i8(i8 %x) {
; CHECK-LABEL: @is_rem32_pos_i8(
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -97
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[TMP1]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i8 %x, 32
%r = icmp sgt i8 %s, 0
ret i1 %r
}
; i16 -32765 == 32771 == 0b1000000000000011
define i1 @is_rem4_neg_i16(i16 %x) {
; CHECK-LABEL: @is_rem4_neg_i16(
; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[X:%.*]], -32765
; CHECK-NEXT: [[R:%.*]] = icmp ugt i16 [[TMP1]], -32768
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i16 %x, 4
%r = icmp slt i16 %s, 0
ret i1 %r
}
declare void @use(i32)
; TODO: This is still worth folding because srem is difficult?
define i1 @is_rem32_neg_i32_extra_use(i32 %x) {
; CHECK-LABEL: @is_rem32_neg_i32_extra_use(
; CHECK-NEXT: [[S:%.*]] = srem i32 [[X:%.*]], 32
; CHECK-NEXT: call void @use(i32 [[S]])
; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[S]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i32 %x, 32
call void @use(i32 %s)
%r = icmp slt i32 %s, 0
ret i1 %r
}
; Negative test - wrong compare constant
define i1 @is_rem8_nonneg_i16(i16 %x) {
; CHECK-LABEL: @is_rem8_nonneg_i16(
; CHECK-NEXT: [[S:%.*]] = srem i16 [[X:%.*]], 8
; CHECK-NEXT: [[R:%.*]] = icmp sgt i16 [[S]], -1
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i16 %x, 8
%r = icmp sgt i16 %s, -1
ret i1 %r
}
; Negative test - wrong remainder constant
define i1 @is_rem3_neg_i8(i8 %x) {
; CHECK-LABEL: @is_rem3_neg_i8(
; CHECK-NEXT: [[S:%.*]] = srem i8 [[X:%.*]], 3
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[S]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i8 %x, 3
%r = icmp slt i8 %s, 0
ret i1 %r
}
; Negative test - wrong compare constant
define i1 @is_rem16_something_i8(i8 %x) {
; CHECK-LABEL: @is_rem16_something_i8(
; CHECK-NEXT: [[S:%.*]] = srem i8 [[X:%.*]], 16
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[S]], 7
; CHECK-NEXT: ret i1 [[R]]
;
%s = srem i8 %x, 16
%r = icmp slt i8 %s, 7
ret i1 %r
}
; PR30281 - https://llvm.org/bugs/show_bug.cgi?id=30281
; All of these tests contain foldable division-by-constant instructions, but we
; can't assert that those folds have occurred before we process the later icmp.
define i32 @icmp_div(i16 %a, i16 %c) {
; CHECK-LABEL: @icmp_div(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i16 [[A:%.*]], 0
; CHECK-NEXT: br i1 [[TOBOOL]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK: then:
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[C:%.*]], 0
; CHECK-NEXT: [[PHITMP1:%.*]] = sext i1 [[CMP]] to i32
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ -1, [[ENTRY:%.*]] ], [ [[PHITMP1]], [[THEN]] ]
; CHECK-NEXT: ret i32 [[PHI]]
;
entry:
%tobool = icmp eq i16 %a, 0
br i1 %tobool, label %then, label %exit
then:
%div = sdiv i16 %c, -1
%cmp = icmp ne i16 %div, 0
br label %exit
exit:
%phi = phi i1 [ false, %entry ], [ %cmp, %then ]
%zext = zext i1 %phi to i32
%add = add nsw i32 %zext, -1
ret i32 %add
}
define i32 @icmp_div2(i16 %a, i16 %c) {
; CHECK-LABEL: @icmp_div2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i16 [[A:%.*]], 0
; CHECK-NEXT: br i1 [[TOBOOL]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK: then:
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ -1, [[ENTRY:%.*]] ], [ 0, [[THEN]] ]
; CHECK-NEXT: ret i32 [[PHI]]
;
entry:
%tobool = icmp eq i16 %a, 0
br i1 %tobool, label %then, label %exit
then:
%div = sdiv i16 %c, 0
%cmp = icmp ne i16 %div, 0
br label %exit
exit:
%phi = phi i1 [ false, %entry ], [ %cmp, %then ]
%zext = zext i1 %phi to i32
%add = add nsw i32 %zext, -1
ret i32 %add
}
define i32 @icmp_div3(i16 %a, i16 %c) {
; CHECK-LABEL: @icmp_div3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i16 [[A:%.*]], 0
; CHECK-NEXT: br i1 [[TOBOOL]], label [[THEN:%.*]], label [[EXIT:%.*]]
; CHECK: then:
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[C:%.*]], 0
; CHECK-NEXT: [[PHITMP1:%.*]] = sext i1 [[CMP]] to i32
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ -1, [[ENTRY:%.*]] ], [ [[PHITMP1]], [[THEN]] ]
; CHECK-NEXT: ret i32 [[PHI]]
;
entry:
%tobool = icmp eq i16 %a, 0
br i1 %tobool, label %then, label %exit
then:
%div = sdiv i16 %c, 1
%cmp = icmp ne i16 %div, 0
br label %exit
exit:
%phi = phi i1 [ false, %entry ], [ %cmp, %then ]
%zext = zext i1 %phi to i32
%add = add nsw i32 %zext, -1
ret i32 %add
}