<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/152900>152900</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AVX-512] Consider using `vpshufb(@splat(0xFF), x)` when selecting on `x < 0x80`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Validark
</td>
</tr>
</table>
<pre>
For code like this: [Zig Godbolt](https://zig.godbo.lt/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXAEx8BBAKoBnTAAUAHpwAMvAFYTStJg1AAvPMFJL6yAngGVG6AMKpaAVxYMQ0hwBk8DTAA5dwAjTGIQADZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3T2li0oFyyoIcoNDwqIsKqpq0%2Bp7W/3b8zsiASgtUV2Jkdg5MVTiqgGoqBjXUVAh1FYABADUSomIISIAWUhXXAA5JlYBPEH2j6xIzy%2Bu78Zfj94urrcfgBSADsACFgRoAIIrOErYiYAgzDZ7BoECC3K6qFbAgDMjn2TAUEEOf1OAK%2B9zRMQMGI0qhuGnGkyhsPhHIepDZHI5e2JpNeJw%2BgLuVxpdIgzNZMI543xkJhYIAImy2YtlgQ1hsQpUds8yW8KZ8gVcnr8jSKqT9DcLKUDcRCefDEcjiBsHrjJJEVgA3GIKBCuKghUkC23/E1i/YKWlMemqABiiZZ2PleMV0JVaphaAYCi1BfQuLxyv2eBYmogwMkkiLNck6czeYLKxCrjoNg2%2BLLewrVYb7c7/gbTZz0JbWqEcgA4jPsEIACrYZUAfQO2Ecq6EAEkAFrYZ74xNoSs2Nir/xansrIsAOgSLHQD9cwGAmALQpIPkYwAICETEhHBiVxMRuK4h1oLs72QECxyVGF1j9AMgxDCAKhCcQVkMB4CAeGJMCufx8DmBRnhwvCC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tWhUxtvOKCO8oJQSRCZL2yIoIuD1ouJcPNHBx2kBYBIDQ86i0lunbOkA87NsttHRwSQE2XucA24uyY5GEh2HOEAA%3D%3D%3D)
```zig
export fn foo(x: @Vector(64, u8), y: @Vector(64, u8)) @Vector(64, u8) {
return @select(u8, x < @as(@Vector(64, u8), @splat(0x80)),
y,
@as(@Vector(64, u8), @splat(0)),
);
}
```
LLVM version: ([Godbolt](https://llvm.godbo.lt/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:llvm,selection:(endColumn:2,endLineNumber:6,positionColumn:2,positionLineNumber:6,selectionStartColumn:2,selectionStartLineNumber:6,startColumn:2,startLineNumber:6),source:'define+dso_local+%3C64+x+i8%3E+@foo(%3C64+x+i8%3E+%250,+%3C64+x+i8%3E+%251)+local_unnamed_addr+%7B%0AEntry:%0A++%25.inv+%3D+icmp+slt+%3C64+x+i8%3E+%250,+zeroinitializer%0A++%252+%3D+select+%3C64+x+i1%3E+%25.inv,+%3C64+x+i8%3E+zeroinitializer,+%3C64+x+i8%3E+%251%0A++ret+%3C64+x+i8%3E+%252%0A%7D'),l:'5',n:'0',o:'LLVM+IR+source+%231',t:'0')),k:50.91543156059285,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:llctrunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:llvm,libs:!(),options:'-O3+--mtriple%3Dx86_64-linux+-mcpu%3Dznver5',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+llc+(trunk)+(Editor+%231)',t:'0')),k:49.08456843940715,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4))
```llvm
define dso_local <64 x i8> @foo(<64 x i8> %0, <64 x i8> %1) local_unnamed_addr {
Entry:
%.inv = icmp slt <64 x i8> %0, zeroinitializer
%2 = select <64 x i1> %.inv, <64 x i8> zeroinitializer, <64 x i8> %1
ret <64 x i8> %2
}
```
We used to get:
```asm
vpmovb2m k0, zmm0
vpmovm2b zmm0, k0
vpandnq zmm0, zmm0, zmm1
```
Now we get:
```asm
vpmovb2m k0, zmm0
knotq k1, k0
vmovdqu8 zmm0 {k1} {z}, zmm1
```
However, I thought it might be a good idea in some situations to use this technique:
```asm
vpternlogd zmm2, zmm2, zmm2, 255
vpshufb zmm0, zmm2, zmm0
vpandq zmm0, zmm0, zmm1
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysOVmTorzav4a5sWYKIghezEXCJsoiIiLevMUSIbLKIuiv_wrtmeme7ZyvztvVJeTZnzxbUgRtS5IS468UhyhO-hT0XVo1Xw9BTuKgyT6FVXz_qlTNLKpiPMtJhmddSlpqDmcUh04kmalVHFZ5R3ESBYS06-oJSQGFAsqDJF-SCf0l756Q-YOaQ8vXkxQq6BpztjhCf9j6yBALtIuRDhXGFiMYbGURGo8BdQbkoT0onWGjO9wly01n-zK8tySk7SOURwEhuKlQuYfQhat6gMYNKv7e6dYJA-FtayhrfXGHZaIe1AU0NnUAD6Nv2HQ6oEyQ9hBy8QAve1WzoXRyIIxd5K5DVERz6b70KcABZDhoO0hJfhG1hdEZwd1JITkRmO7ne5ATulLu90qLKlMarlq7udZX-slX8x4FlHkoPFoCnaRzD2CdxQlnHPbkmqjVFV2O7gGmjOJDBCXXqQpNczTouenxCCG3zn20lVfgshi1x_0psueF0_myZPumic4JcqDUQlFJdhXUtMaSG5_4MHlIJx5VsijON4urEsJEMi4l2ENXlq0zY8HNU9TysTNsQ9Xoa9qrpZrd03abrjRcS6eVxmGwrk28Hp6kid4fxjrWkod5MjSOnY_ZOvU1qBV34Hd5YjNp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