Commit a8065a05a5

Andrew Kelley <andrew@ziglang.org>
2020-07-18 02:03:24
stage2: fix implementation of liveness operandDies()
1 parent 896472c
Changed files (3)
src-self-hosted
test
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;
                 }
             }
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 {
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;
+            \\}
+        ,
+            "",
+        );
     }
 }