<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/134513>134513</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Auto-vectorization via `masked.load` blocks constprop
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
scottmcm
</td>
</tr>
</table>
<pre>
I was writing some code in Rust and ended up with the following IR, where even though everything's a constant -- it should just be `ret i64 165` -- the masked loads from autovectorization on `-Ctarget-cpu=x86-64-v3` kept that from happening:
```llvm
define noundef i64 @test() unnamed_addr #0 {
bb3.preheader:
%iter = alloca [64 x i8], align 8
call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %iter)
%_3.sroa.5.0.iter.sroa_idx = getelementptr inbounds nuw i8, ptr %iter, i64 16
store <4 x i64> <i64 23, i64 16, i64 54, i64 3>, ptr %_3.sroa.5.0.iter.sroa_idx, align 8
%_3.sroa.9.0.iter.sroa_idx = getelementptr inbounds nuw i8, ptr %iter, i64 48
store i64 60, ptr %_3.sroa.9.0.iter.sroa_idx, align 8
%_3.sroa.10.0.iter.sroa_idx = getelementptr inbounds nuw i8, ptr %iter, i64 56
store i64 9, ptr %_3.sroa.10.0.iter.sroa_idx, align 8
%unmaskedload = load <4 x i64>, ptr %_3.sroa.5.0.iter.sroa_idx, align 8, !alias.scope !2
%0 = getelementptr inbounds nuw i8, ptr %iter, i64 48
%wide.masked.load.1 = call <4 x i64> @llvm.masked.load.v4i64.p0(ptr nonnull %0, i32 8, <4 x i1> <i1 true, i1 true, i1 false, i1 false>, <4 x i64> poison), !alias.scope !2
%1 = add <4 x i64> %wide.masked.load.1, %unmaskedload
%2 = shufflevector <4 x i64> %1, <4 x i64> %unmaskedload, <4 x i32> <i32 0, i32 1, i32 6, i32 7>
%3 = tail call i64 @llvm.vector.reduce.add.v4i64(<4 x i64> %2)
call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %iter)
ret i64 %3
}
```
It looks like trunk can't optimize that to a constant either: <https://llvm.godbolt.org/z/z6MKz6cz1>
(Trunk at least doesn't need the store-load of the vector constant, but it still doesn't const-prop the stores and the `masked.load`.)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVlGPmzgQ_jXOyygIbCDkgYftbleqTvdS3Xtl8ADuGhvhIWn3159sSJtk96o7XSVWOw7jb775xjNGeq97i1iz4gMrnnZyocHNtW8d0diOu8ap7_UnOEsP51mTtj14NyK0TiFoC58XTyCtArQKFSwTnDUNQANC54xx57Dj02fGH-E84IyAJ7RAg1v6Idjzdxq07Rk_eJDQOutJWoL9HjSBH9xiFHwNMRoEVqYzEugyh6wsWJkGtxBplP4FFRgnlYdudiPIhdwJW3KzfpWknQVnw_79I8m5R9q308LE07eq3Jf5_iQC2AtOBDRIWiEGOU1oAzfxwNIHVqbrY8xpZOmDwk5bBOsWq7CLpFieEnpivGL8CIu1ckT1RSo1A-MiBXb4wNKHphHJNOOAUuG8YgMwXmjCGZh4AmmMayWw4kOZwzfQFSuegn7S6N5CFf1baQycnFYhaGCUGN0h6RETT3KmZEoZrwKpMg97J5rBOmsXYy6xGD8GKMaLLyLxs5NJkaRJeBNXX7T6Fun0SGhwREsBRNsmZOzBLudAbcP-gfm4lSey9ORmBCYeYx5lzsTHsAoeXFz5blaRXyzBxMcr6H8keC_Lle_xtyWTV1fJREnTd7i9ifcLbln628gV5R254zvc3sZ7h9xi1zYKXRTpbMZV8f5rSfgjMJ5Jo6VPfOsmDEt-CZj-34owXpy1wmTlnQS-SRZBY3vcnbutT66dT7ku87VV7hokVlgLviWxIWWXA5wBzQtGnxuzk8bf2qtot1Qmp72zof9-LdCai1TqPpX38l6xbqp4weERxw9L1xlcp-JbxOwtz3u4KwfBL1IIDj_Eyi5GeTEOQYCNhog0SGqz1mcbmbEqK6tkRrW0mEi11SaM0ntKfBtcv5qBaNW_n4BwuVQCxzDpD0_X8z7Y6cMn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