LLVM backend: optimize memset with comptime-known element

When the element is comptime-known, we can check if it has a repeated
byte representation. In this case, `@memset` can be lowered with the
LLVM intrinsic rather than with a loop.
This commit is contained in:
Andrew Kelley
2023-04-26 13:41:02 -07:00
parent 51adbf472b
commit 9295355985
4 changed files with 83 additions and 19 deletions

View File

@@ -94,7 +94,7 @@ test "memset with 1-byte array element" {
try expect(buf[4][0]);
}
test "memset with large array element" {
test "memset with large array element, runtime known" {
const A = [128]u64;
var buf: [5]A = undefined;
var runtime_known_element = [_]u64{0} ** 128;
@@ -106,6 +106,18 @@ test "memset with large array element" {
for (buf[4]) |elem| try expect(elem == 0);
}
test "memset with large array element, comptime known" {
const A = [128]u64;
var buf: [5]A = undefined;
const comptime_known_element = [_]u64{0} ** 128;
@memset(&buf, comptime_known_element);
for (buf[0]) |elem| try expect(elem == 0);
for (buf[1]) |elem| try expect(elem == 0);
for (buf[2]) |elem| try expect(elem == 0);
for (buf[3]) |elem| try expect(elem == 0);
for (buf[4]) |elem| try expect(elem == 0);
}
test "memcpy and memset intrinsics" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;