<div dir="ltr"><div><div>If we're loading the first and last elements of a vector using a masked load [1], can we replace
the masked load with a full vector load?<br><br>"The result of this operation is equivalent to a regular vector load
instruction followed by a ‘select’ between the loaded and the passthru
values, predicated on the same mask. However, using this intrinsic
prevents exceptions on memory access to masked-off lanes."<br><br>I think the fact that we're
loading the endpoints of the vector guarantees that a full vector load can't have any
different faulting/exception behavior on x86 and most (?) other targets. We would, however, be reading memory that the program has not explicitly requested.<br><br></div>IR example:<br><br>define <4 x i32> @maskedload_endpoints(<4 x i32>* %addr, <4 x i32> %v) {<br></div> ; load the first and last elements pointed to by %addr and shuffle those into %v<br><div> %res = call <4 x i32> @llvm.masked.load.v4i32(<4 x i32>* %addr, i32 4, <4 x i1> <i1 1, i1 0, i1 0, i1 1>, <4 x i32> %v)<br> ret <4 x i32> %res<br>}<br><br></div><div>would become something like:<br></div><div><br>define <4 x i32> @maskedload_endpoints(<4 x i32>* %addr, <4 x i32> %v) {<br></div><div> %vecload = load <4 x i32>, <4 x i32>* %addr, align 4<br></div><div> %sel = select <4 x i1> <i1 1, i1 0, i1 0, i1 1>, <4 x i32> %vecload, <4 x i32> %v<br></div><div> ret <4 x i32> %sel<br>}<br><br></div><div>If this isn't valid as an IR optimization, would it be acceptable as a DAG combine with target hook to opt in?<br></div><div><div><div><br>[1] <a href="http://llvm.org/docs/LangRef.html#llvm-masked-load-intrinsics">http://llvm.org/docs/LangRef.html#llvm-masked-load-intrinsics</a><br></div></div></div></div>