unsigned-mul-lack-of-overflow-check-via-mul-udiv.ll
5.28 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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
; Fold
; ((%x * %y) u/ %x) == %y
; to
; @llvm.umul.with.overflow(%x, %y) + extractvalue + not
define i1 @t0_basic(i8 %x, i8 %y) {
; CHECK-LABEL: @t0_basic(
; CHECK-NEXT: [[UMUL:%.*]] = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
; CHECK-NEXT: [[UMUL_OV:%.*]] = extractvalue { i8, i1 } [[UMUL]], 1
; CHECK-NEXT: [[UMUL_NOT_OV:%.*]] = xor i1 [[UMUL_OV]], true
; CHECK-NEXT: ret i1 [[UMUL_NOT_OV]]
;
%t0 = mul i8 %x, %y
%t1 = udiv i8 %t0, %x
%r = icmp eq i8 %t1, %y
ret i1 %r
}
define <2 x i1> @t1_vec(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @t1_vec(
; CHECK-NEXT: [[UMUL:%.*]] = call { <2 x i8>, <2 x i1> } @llvm.umul.with.overflow.v2i8(<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]])
; CHECK-NEXT: [[UMUL_OV:%.*]] = extractvalue { <2 x i8>, <2 x i1> } [[UMUL]], 1
; CHECK-NEXT: [[UMUL_NOT_OV:%.*]] = xor <2 x i1> [[UMUL_OV]], <i1 true, i1 true>
; CHECK-NEXT: ret <2 x i1> [[UMUL_NOT_OV]]
;
%t0 = mul <2 x i8> %x, %y
%t1 = udiv <2 x i8> %t0, %x
%r = icmp eq <2 x i8> %t1, %y
ret <2 x i1> %r
}
declare i8 @gen8()
define i1 @t2_commutative(i8 %x) {
; CHECK-LABEL: @t2_commutative(
; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8()
; CHECK-NEXT: [[UMUL:%.*]] = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 [[Y]])
; CHECK-NEXT: [[UMUL_OV:%.*]] = extractvalue { i8, i1 } [[UMUL]], 1
; CHECK-NEXT: [[UMUL_NOT_OV:%.*]] = xor i1 [[UMUL_OV]], true
; CHECK-NEXT: ret i1 [[UMUL_NOT_OV]]
;
%y = call i8 @gen8()
%t0 = mul i8 %y, %x ; swapped
%t1 = udiv i8 %t0, %x
%r = icmp eq i8 %t1, %y
ret i1 %r
}
define i1 @t3_commutative(i8 %x) {
; CHECK-LABEL: @t3_commutative(
; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8()
; CHECK-NEXT: [[UMUL:%.*]] = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 [[Y]])
; CHECK-NEXT: [[UMUL_OV:%.*]] = extractvalue { i8, i1 } [[UMUL]], 1
; CHECK-NEXT: [[UMUL_NOT_OV:%.*]] = xor i1 [[UMUL_OV]], true
; CHECK-NEXT: ret i1 [[UMUL_NOT_OV]]
;
%y = call i8 @gen8()
%t0 = mul i8 %y, %x ; swapped
%t1 = udiv i8 %t0, %x
%r = icmp eq i8 %t1, %y
ret i1 %r
}
define i1 @t4_commutative(i8 %x) {
; CHECK-LABEL: @t4_commutative(
; CHECK-NEXT: [[Y:%.*]] = call i8 @gen8()
; CHECK-NEXT: [[UMUL:%.*]] = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 [[Y]])
; CHECK-NEXT: [[UMUL_OV:%.*]] = extractvalue { i8, i1 } [[UMUL]], 1
; CHECK-NEXT: [[UMUL_NOT_OV:%.*]] = xor i1 [[UMUL_OV]], true
; CHECK-NEXT: ret i1 [[UMUL_NOT_OV]]
;
%y = call i8 @gen8()
%t0 = mul i8 %y, %x ; swapped
%t1 = udiv i8 %t0, %x
%r = icmp eq i8 %y, %t1 ; swapped
ret i1 %r
}
; Extra-use tests
declare void @use8(i8)
define i1 @t5_extrause0(i8 %x, i8 %y) {
; CHECK-LABEL: @t5_extrause0(
; CHECK-NEXT: [[UMUL:%.*]] = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
; CHECK-NEXT: [[UMUL_VAL:%.*]] = extractvalue { i8, i1 } [[UMUL]], 0
; CHECK-NEXT: [[UMUL_OV:%.*]] = extractvalue { i8, i1 } [[UMUL]], 1
; CHECK-NEXT: [[UMUL_NOT_OV:%.*]] = xor i1 [[UMUL_OV]], true
; CHECK-NEXT: call void @use8(i8 [[UMUL_VAL]])
; CHECK-NEXT: ret i1 [[UMUL_NOT_OV]]
;
%t0 = mul i8 %x, %y
call void @use8(i8 %t0)
%t1 = udiv i8 %t0, %x
%r = icmp eq i8 %t1, %y
ret i1 %r
}
define i1 @t6_extrause1(i8 %x, i8 %y) {
; CHECK-LABEL: @t6_extrause1(
; CHECK-NEXT: [[T0:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[T1:%.*]] = udiv i8 [[T0]], [[X]]
; CHECK-NEXT: call void @use8(i8 [[T1]])
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[T1]], [[Y]]
; CHECK-NEXT: ret i1 [[R]]
;
%t0 = mul i8 %x, %y
%t1 = udiv i8 %t0, %x
call void @use8(i8 %t1)
%r = icmp eq i8 %t1, %y
ret i1 %r
}
define i1 @t7_extrause2(i8 %x, i8 %y) {
; CHECK-LABEL: @t7_extrause2(
; CHECK-NEXT: [[T0:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: call void @use8(i8 [[T0]])
; CHECK-NEXT: [[T1:%.*]] = udiv i8 [[T0]], [[X]]
; CHECK-NEXT: call void @use8(i8 [[T1]])
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[T1]], [[Y]]
; CHECK-NEXT: ret i1 [[R]]
;
%t0 = mul i8 %x, %y
call void @use8(i8 %t0)
%t1 = udiv i8 %t0, %x
call void @use8(i8 %t1)
%r = icmp eq i8 %t1, %y
ret i1 %r
}
; Negative tests
define i1 @n8_different_x(i8 %x0, i8 %x1, i8 %y) {
; CHECK-LABEL: @n8_different_x(
; CHECK-NEXT: [[T0:%.*]] = mul i8 [[X0:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[T1:%.*]] = udiv i8 [[T0]], [[X1:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[T1]], [[Y]]
; CHECK-NEXT: ret i1 [[R]]
;
%t0 = mul i8 %x0, %y
%t1 = udiv i8 %t0, %x1
%r = icmp eq i8 %t1, %y
ret i1 %r
}
define i1 @n9_different_y(i8 %x, i8 %y0, i8 %y1) {
; CHECK-LABEL: @n9_different_y(
; CHECK-NEXT: [[T0:%.*]] = mul i8 [[X:%.*]], [[Y0:%.*]]
; CHECK-NEXT: [[T1:%.*]] = udiv i8 [[T0]], [[X]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[T1]], [[Y1:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%t0 = mul i8 %x, %y0
%t1 = udiv i8 %t0, %x
%r = icmp eq i8 %t1, %y1
ret i1 %r
}
define i1 @n10_wrong_pred(i8 %x, i8 %y) {
; CHECK-LABEL: @n10_wrong_pred(
; CHECK-NEXT: [[T0:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[T1:%.*]] = udiv i8 [[T0]], [[X]]
; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[T1]], [[Y]]
; CHECK-NEXT: ret i1 [[R]]
;
%t0 = mul i8 %x, %y
%t1 = udiv i8 %t0, %x
%r = icmp ult i8 %t1, %y
ret i1 %r
}