| ... | ... | @@ -135,6 +135,42 @@ pub fn getCondBr(l: Liveness, inst: Air.Inst.Index) CondBrSlices { |
| 135 | 135 | }; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | /// Indexed by case number as they appear in AIR. |
| 139 | /// Else is the last element. |
| 140 | pub const SwitchBrTable = struct { |
| 141 | deaths: []const []const Air.Inst.Index, |
| 142 | }; |
| 143 | |
| 144 | /// Caller owns the memory. |
| 145 | pub 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 | |
| 138 | 174 | pub fn deinit(l: *Liveness, gpa: Allocator) void { |
| 139 | 175 | gpa.free(l.tomb_bits); |
| 140 | 176 | gpa.free(l.extra); |