From 2d3462c79a09f7c2abf96b79910663067a7bb91e Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 21 Feb 2022 19:00:39 +0100 Subject: [PATCH] liveness: add helper for extracting liveness of switch branch --- src/Liveness.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Liveness.zig b/src/Liveness.zig index 5a329d2a7a..e2671702c4 100644 --- a/src/Liveness.zig +++ b/src/Liveness.zig @@ -135,6 +135,42 @@ pub fn getCondBr(l: Liveness, inst: Air.Inst.Index) CondBrSlices { }; } +/// Indexed by case number as they appear in AIR. +/// Else is the last element. +pub const SwitchBrTable = struct { + deaths: []const []const Air.Inst.Index, +}; + +/// Caller owns the memory. +pub fn getSwitchBr(l: Liveness, gpa: Allocator, inst: Air.Inst.Index, cases_len: u32) Allocator.Error!SwitchBrTable { + var index: usize = l.special.get(inst) orelse return SwitchBrTable{ + .deaths = &.{}, + }; + const else_death_count = l.extra[index]; + index += 1; + + var deaths = std.ArrayList([]const Air.Inst.Index).init(gpa); + defer deaths.deinit(); + try deaths.ensureTotalCapacity(cases_len + 1); + + var case_i: u32 = 0; + while (case_i < cases_len - 1) : (case_i += 1) { + const case_death_count: u32 = l.extra[index]; + index += 1; + const case_deaths = l.extra[index..][0..case_death_count]; + index += case_death_count; + deaths.appendAssumeCapacity(case_deaths); + } + { + // Else + const else_deaths = l.extra[index..][0..else_death_count]; + deaths.appendAssumeCapacity(else_deaths); + } + return SwitchBrTable{ + .deaths = deaths.toOwnedSlice(), + }; +} + pub fn deinit(l: *Liveness, gpa: Allocator) void { gpa.free(l.tomb_bits); gpa.free(l.extra);