From a8065a05a5bc3df4036f1d7abe0928901cf7f5df Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 17 Jul 2020 17:03:24 -0700 Subject: [PATCH] stage2: fix implementation of liveness operandDies() --- src-self-hosted/codegen.zig | 2 ++ src-self-hosted/ir.zig | 2 +- test/stage2/compare_output.zig | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/codegen.zig b/src-self-hosted/codegen.zig index 6e1686fd3e..2cc471a07d 100644 --- a/src-self-hosted/codegen.zig +++ b/src-self-hosted/codegen.zig @@ -407,6 +407,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { for (body.instructions) |inst| { const new_inst = try self.genFuncInst(inst); try inst_table.putNoClobber(self.gpa, inst, new_inst); + // TODO process operand deaths } } @@ -1194,6 +1195,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { while (true) { i -= 1; if (self.branch_stack.items[i].inst_table.get(inst)) |mcv| { + assert(mcv != .dead); return mcv; } } diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index a150957de0..9902bd70aa 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -38,7 +38,7 @@ pub const Inst = struct { pub fn operandDies(self: Inst, index: DeathsBitIndex) bool { assert(index < deaths_bits); - return @truncate(u1, self.deaths << index) != 0; + return @truncate(u1, self.deaths >> index) != 0; } pub fn specialOperandDeaths(self: Inst) bool { diff --git a/test/stage2/compare_output.zig b/test/stage2/compare_output.zig index 4c8d23f3c6..6a6772f935 100644 --- a/test/stage2/compare_output.zig +++ b/test/stage2/compare_output.zig @@ -231,5 +231,41 @@ pub fn addCases(ctx: *TestContext) !void { , "", ); + + // More stress on the liveness detection. + case.addCompareOutput( + \\export fn _start() noreturn { + \\ add(3, 4); + \\ + \\ exit(); + \\} + \\ + \\fn add(a: u32, b: u32) void { + \\ const c = a + b; // 7 + \\ const d = a + c; // 10 + \\ const e = d + b; // 14 + \\ const f = d + e; // 24 + \\ const g = e + f; // 38 + \\ const h = f + g; // 62 + \\ const i = g + h; // 100 + \\ assert(i == 100); + \\} + \\ + \\pub fn assert(ok: bool) void { + \\ if (!ok) unreachable; // assertion failure + \\} + \\ + \\fn exit() noreturn { + \\ asm volatile ("syscall" + \\ : + \\ : [number] "{rax}" (231), + \\ [arg1] "{rdi}" (0) + \\ : "rcx", "r11", "memory" + \\ ); + \\ unreachable; + \\} + , + "", + ); } }