Skip to content

Commit 7185aa6

Browse files
tlivelytstellar
authored andcommitted
[WebAssembly] Fix miscompile of select with and
Summary: Rolls back the remaining bad optimizations introduced in eb15d00. Some of them were already rolled back in e661f94 and this finishes the job. Fixes https://bugs.llvm.org/show_bug.cgi?id=44012. Reviewers: dschuff, aheejin Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70347 (cherry picked from commit 194d7ec)
1 parent 5bb85ae commit 7185aa6

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,3 @@ def : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
121121
(SELECT_F32 F32:$rhs, F32:$lhs, I32:$cond)>;
122122
def : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
123123
(SELECT_F64 F64:$rhs, F64:$lhs, I32:$cond)>;
124-
125-
// The legalizer inserts an unnecessary `and 1` to make input conform
126-
// to getBooleanContents, which we can lower away.
127-
def : Pat<(select (i32 (and I32:$cond, 1)), F32:$lhs, F32:$rhs),
128-
(SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>;
129-
def : Pat<(select (i32 (and I32:$cond, 1)), F64:$lhs, F64:$rhs),
130-
(SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>;

llvm/test/CodeGen/WebAssembly/select.ll

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ define i32 @select_i32_bool(i1 zeroext %a, i32 %b, i32 %c) {
1717

1818
; CHECK-LABEL: select_i32_bool_nozext:
1919
; CHECK-NEXT: .functype select_i32_bool_nozext (i32, i32, i32) -> (i32){{$}}
20-
; SLOW-NEXT: i32.const $push0=, 1{{$}}
21-
; SLOW-NEXT: i32.and $push1=, $0, $pop0{{$}}
22-
; SLOW-NEXT: i32.select $push2=, $1, $2, $pop1{{$}}
23-
; SLOW-NEXT: return $pop2{{$}}
20+
; CHECK-NEXT: i32.const $push0=, 1{{$}}
21+
; CHECK-NEXT: i32.and $push1=, $0, $pop0{{$}}
22+
; CHECK-NEXT: i32.select $push2=, $1, $2, $pop1{{$}}
23+
; CHECK-NEXT: return $pop2{{$}}
2424
define i32 @select_i32_bool_nozext(i1 %a, i32 %b, i32 %c) {
2525
%cond = select i1 %a, i32 %b, i32 %c
2626
ret i32 %cond
@@ -57,10 +57,10 @@ define i64 @select_i64_bool(i1 zeroext %a, i64 %b, i64 %c) {
5757

5858
; CHECK-LABEL: select_i64_bool_nozext:
5959
; CHECK-NEXT: .functype select_i64_bool_nozext (i32, i64, i64) -> (i64){{$}}
60-
; SLOW-NEXT: i32.const $push0=, 1{{$}}
61-
; SLOW-NEXT: i32.and $push1=, $0, $pop0{{$}}
62-
; SLOW-NEXT: i64.select $push2=, $1, $2, $pop1{{$}}
63-
; SLOW-NEXT: return $pop2{{$}}
60+
; CHECK-NEXT: i32.const $push0=, 1{{$}}
61+
; CHECK-NEXT: i32.and $push1=, $0, $pop0{{$}}
62+
; CHECK-NEXT: i64.select $push2=, $1, $2, $pop1{{$}}
63+
; CHECK-NEXT: return $pop2{{$}}
6464
define i64 @select_i64_bool_nozext(i1 %a, i64 %b, i64 %c) {
6565
%cond = select i1 %a, i64 %b, i64 %c
6666
ret i64 %cond
@@ -97,8 +97,10 @@ define float @select_f32_bool(i1 zeroext %a, float %b, float %c) {
9797

9898
; CHECK-LABEL: select_f32_bool_nozext:
9999
; CHECK-NEXT: .functype select_f32_bool_nozext (i32, f32, f32) -> (f32){{$}}
100-
; SLOW-NEXT: f32.select $push0=, $1, $2, $0{{$}}
101-
; SLOW-NEXT: return $pop0{{$}}
100+
; CHECK-NEXT: i32.const $push0=, 1{{$}}
101+
; CHECK-NEXT: i32.and $push1=, $0, $pop0{{$}}
102+
; CHECK-NEXT: f32.select $push2=, $1, $2, $pop1{{$}}
103+
; CHECK-NEXT: return $pop2{{$}}
102104
define float @select_f32_bool_nozext(i1 %a, float %b, float %c) {
103105
%cond = select i1 %a, float %b, float %c
104106
ret float %cond
@@ -135,8 +137,10 @@ define double @select_f64_bool(i1 zeroext %a, double %b, double %c) {
135137

136138
; CHECK-LABEL: select_f64_bool_nozext:
137139
; CHECK-NEXT: .functype select_f64_bool_nozext (i32, f64, f64) -> (f64){{$}}
138-
; SLOW-NEXT: f64.select $push0=, $1, $2, $0{{$}}
139-
; SLOW-NEXT: return $pop0{{$}}
140+
; CHECK-NEXT: i32.const $push0=, 1{{$}}
141+
; CHECK-NEXT: i32.and $push1=, $0, $pop0{{$}}
142+
; CHECK-NEXT: f64.select $push2=, $1, $2, $pop1{{$}}
143+
; CHECK-NEXT: return $pop2{{$}}
140144
define double @select_f64_bool_nozext(i1 %a, double %b, double %c) {
141145
%cond = select i1 %a, double %b, double %c
142146
ret double %cond
@@ -162,15 +166,56 @@ define double @select_f64_ne(i32 %a, double %b, double %c) {
162166
ret double %cond
163167
}
164168

165-
; CHECK-LABEL: pr40805:
166-
; CHECK-NEXT: .functype pr40805 (i32, i32, i32) -> (i32){{$}}
169+
; CHECK-LABEL: pr40805_i32:
170+
; CHECK-NEXT: .functype pr40805_i32 (i32, i32, i32) -> (i32){{$}}
167171
; SLOW-NEXT: i32.const $push0=, 1{{$}}
168172
; SLOW-NEXT: i32.and $push1=, $0, $pop0{{$}}
169173
; SLOW-NEXT: i32.select $push2=, $1, $2, $pop1{{$}}
170174
; SLOW-NEXT: return $pop2{{$}}
171-
define i32 @pr40805(i32 %x, i32 %y, i32 %z) {
175+
define i32 @pr40805_i32(i32 %x, i32 %y, i32 %z) {
172176
%a = and i32 %x, 1
173177
%b = icmp ne i32 %a, 0
174178
%c = select i1 %b, i32 %y, i32 %z
175179
ret i32 %c
176180
}
181+
182+
; CHECK-LABEL: pr40805_i64:
183+
; CHECK-NEXT: .functype pr40805_i64 (i64, i64, i64) -> (i64){{$}}
184+
; SLOW-NEXT: i32.wrap_i64 $push0=, $0{{$}}
185+
; SLOW-NEXT: i32.const $push1=, 1{{$}}
186+
; SLOW-NEXT: i32.and $push2=, $pop0, $pop1{{$}}
187+
; SLOW-NEXT: i64.select $push3=, $1, $2, $pop2{{$}}
188+
; SLOW-NEXT: return $pop3{{$}}
189+
define i64 @pr40805_i64(i64 %x, i64 %y, i64 %z) {
190+
%a = and i64 %x, 1
191+
%b = icmp ne i64 %a, 0
192+
%c = select i1 %b, i64 %y, i64 %z
193+
ret i64 %c
194+
}
195+
196+
; CHECK-LABEL: pr44012_i32:
197+
; CHECK-NEXT: .functype pr44012_i32 (i32, f32, f32) -> (f32){{$}}
198+
; SLOW-NEXT: i32.const $push0=, 1{{$}}
199+
; SLOW-NEXT: i32.and $push1=, $0, $pop0{{$}}
200+
; SLOW-NEXT: f32.select $push2=, $1, $2, $pop1{{$}}
201+
; SLOW-NEXT: return $pop2{{$}}
202+
define float @pr44012_i32(i32 %x, float %y, float %z) {
203+
%a = and i32 %x, 1
204+
%b = icmp ne i32 %a, 0
205+
%c = select i1 %b, float %y, float %z
206+
ret float %c
207+
}
208+
209+
; CHECK-LABEL: pr44012_i64:
210+
; CHECK-NEXT: .functype pr44012_i64 (i64, f32, f32) -> (f32){{$}}
211+
; SLOW-NEXT: i32.wrap_i64 $push0=, $0{{$}}
212+
; SLOW-NEXT: i32.const $push1=, 1{{$}}
213+
; SLOW-NEXT: i32.and $push2=, $pop0, $pop1{{$}}
214+
; SLOW-NEXT: f32.select $push3=, $1, $2, $pop2{{$}}
215+
; SLOW-NEXT: return $pop3{{$}}
216+
define float @pr44012_i64(i64 %x, float %y, float %z) {
217+
%a = and i64 %x, 1
218+
%b = icmp ne i64 %a, 0
219+
%c = select i1 %b, float %y, float %z
220+
ret float %c
221+
}

0 commit comments

Comments
 (0)