<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/73454>73454</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
WASM: v128.bitselect When Argument is Zeroed Can Be Simplified
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Geotale
</td>
</tr>
</table>
<pre>
Example code, compiled with Rustc nightly (arguments: `-C opt-level=3 --target=wasm32-unknown-unknown -C target-feature=+simd128 -C target-feature=+multivalue`)
```rust
#![feature(portable_simd)]
use std::simd::*;
#[inline(never)]
pub fn flush_subnormals(input: f32x4) -> f32x4 {
let bits = input.to_bits();
let mask = bits & u32x4::splat(0x7f800000);
let ne_mask = mask.simd_ne(u32x4::splat(0x00000000));
let result = bits & ne_mask.to_int().cast::<u32>();
f32x4::from_bits(result)
}
```
This results in the following WebAssembly:
```wasm
flush_subnormals:
local.get 0
v128.const 0, 0, 0, 0
local.tee 2
local.get 1
v128.load 0
local.tee 3
local.get 2
local.get 3
v128.const 2139095040, 2139095040, 2139095040, 2139095040 ; 0x7f800000 0x7f800000 0x7f800000 0x7f800000
v128.and
i32x4.eq
v128.bitselect
v128.store 0
end_function
```
Due to the first argument of `bitselect` being `0 0 0 0` and the condition being reversible, the WebAssembly is simplifiable into:
```wasm
flush_subnormals:
local.get 0
local.get 1
v128.load 0
local.tee 2 ; One fewer local used
v128.const 0, 0, 0, 0
local.get 2
v128.const 2139095040, 2139095040, 2139095040, 2139095040 ; 0x7f800000 0x7f800000 0x7f800000 0x7f800000
v128.and
i32x4.ne ; Condition reversed
v128.and ; bitselect replaced with and
v128.store 0
end_function
```
If the `select` expression had the second argument as a zeroinitializer instead, the condition would not need to be reversed, and the `bitselect` would simply be able to be converted into an `and`!
Godbolt link: https://godbolt.org/z/TqK5f588f
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJz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