authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-21 19:00:39+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-22 21:56:34+01:00
log2d3462c79a09f7c2abf96b79910663067a7bb91e
treee76c1e9e7c8fc296c3e04fe632ee893934bd7ece
parent5ba66911faf71cc6bab7f9329474ed15804e783d

liveness: add helper for extracting liveness of switch branch


1 files changed, 36 insertions(+), 0 deletions(-)

src/Liveness.zig+36
......@@ -135,6 +135,42 @@ pub fn getCondBr(l: Liveness, inst: Air.Inst.Index) CondBrSlices {
135135 };
136136}
137137
138/// Indexed by case number as they appear in AIR.
139/// Else is the last element.
140pub const SwitchBrTable = struct {
141 deaths: []const []const Air.Inst.Index,
142};
143
144/// Caller owns the memory.
145pub fn getSwitchBr(l: Liveness, gpa: Allocator, inst: Air.Inst.Index, cases_len: u32) Allocator.Error!SwitchBrTable {
146 var index: usize = l.special.get(inst) orelse return SwitchBrTable{
147 .deaths = &.{},
148 };
149 const else_death_count = l.extra[index];
150 index += 1;
151
152 var deaths = std.ArrayList([]const Air.Inst.Index).init(gpa);
153 defer deaths.deinit();
154 try deaths.ensureTotalCapacity(cases_len + 1);
155
156 var case_i: u32 = 0;
157 while (case_i < cases_len - 1) : (case_i += 1) {
158 const case_death_count: u32 = l.extra[index];
159 index += 1;
160 const case_deaths = l.extra[index..][0..case_death_count];
161 index += case_death_count;
162 deaths.appendAssumeCapacity(case_deaths);
163 }
164 {
165 // Else
166 const else_deaths = l.extra[index..][0..else_death_count];
167 deaths.appendAssumeCapacity(else_deaths);
168 }
169 return SwitchBrTable{
170 .deaths = deaths.toOwnedSlice(),
171 };
172}
173
138174pub fn deinit(l: *Liveness, gpa: Allocator) void {
139175 gpa.free(l.tomb_bits);
140176 gpa.free(l.extra);