| ... | ... | @@ -25,6 +25,7 @@ tomb_bits: []usize, |
| 25 | 25 | /// array. The meaning of the data depends on the AIR tag. |
| 26 | 26 | /// * `cond_br` - points to a `CondBr` in `extra` at this index. |
| 27 | 27 | /// * `switch_br` - points to a `SwitchBr` in `extra` at this index. |
| 28 | /// * `loop` - points to a `Loop` in `extra` at this index. |
| 28 | 29 | /// * `asm`, `call`, `aggregate_init` - the value is a set of bits which are the extra tomb |
| 29 | 30 | /// bits of operands. |
| 30 | 31 | /// The main tomb bits are still used and the extra ones are starting with the lsb of the |
| ... | ... | @@ -51,6 +52,11 @@ pub const SwitchBr = struct { |
| 51 | 52 | else_death_count: u32, |
| 52 | 53 | }; |
| 53 | 54 | |
| 55 | /// Trailing is the set of instructions whose lifetimes end at the end of the loop body. |
| 56 | pub const Loop = struct { |
| 57 | death_count: u32, |
| 58 | }; |
| 59 | |
| 54 | 60 | pub fn analyze(gpa: Allocator, air: Air) Allocator.Error!Liveness { |
| 55 | 61 | const tracy = trace(@src()); |
| 56 | 62 | defer tracy.end(); |
| ... | ... | @@ -76,6 +82,11 @@ pub fn analyze(gpa: Allocator, air: Air) Allocator.Error!Liveness { |
| 76 | 82 | const main_body = air.getMainBody(); |
| 77 | 83 | try a.table.ensureTotalCapacity(gpa, @intCast(u32, main_body.len)); |
| 78 | 84 | try analyzeWithContext(&a, null, main_body); |
| 85 | { |
| 86 | var to_remove: std.AutoHashMapUnmanaged(Air.Inst.Index, void) = .{}; |
| 87 | defer to_remove.deinit(gpa); |
| 88 | try removeDeaths(&a, &to_remove, main_body); |
| 89 | } |
| 79 | 90 | return Liveness{ |
| 80 | 91 | .tomb_bits = a.tomb_bits, |
| 81 | 92 | .special = a.special, |
| ... | ... | @@ -650,6 +661,18 @@ pub fn getSwitchBr(l: Liveness, gpa: Allocator, inst: Air.Inst.Index, cases_len: |
| 650 | 661 | }; |
| 651 | 662 | } |
| 652 | 663 | |
| 664 | pub const LoopSlice = struct { |
| 665 | deaths: []const Air.Inst.Index, |
| 666 | }; |
| 667 | |
| 668 | pub fn getLoop(l: Liveness, inst: Air.Inst.Index) LoopSlice { |
| 669 | const index: usize = l.special.get(inst) orelse return .{ |
| 670 | .deaths = &.{}, |
| 671 | }; |
| 672 | const death_count = l.extra[index]; |
| 673 | return .{ .deaths = l.extra[index + 1 ..][0..death_count] }; |
| 674 | } |
| 675 | |
| 653 | 676 | pub fn deinit(l: *Liveness, gpa: Allocator) void { |
| 654 | 677 | gpa.free(l.tomb_bits); |
| 655 | 678 | gpa.free(l.extra); |
| ... | ... | @@ -1138,7 +1161,39 @@ fn analyzeInst( |
| 1138 | 1161 | .loop => { |
| 1139 | 1162 | const extra = a.air.extraData(Air.Block, inst_datas[inst].ty_pl.payload); |
| 1140 | 1163 | const body = a.air.extra[extra.end..][0..extra.data.body_len]; |
| 1141 | | try analyzeWithContext(a, new_set, body); |
| 1164 | |
| 1165 | var body_table: std.AutoHashMapUnmanaged(Air.Inst.Index, void) = .{}; |
| 1166 | defer body_table.deinit(gpa); |
| 1167 | |
| 1168 | // Instructions outside the loop body cannot die within the loop, since further loop |
| 1169 | // iterations may occur. Track deaths from the loop body - we'll remove all of these |
| 1170 | // retroactively, and add them to our extra data. |
| 1171 | |
| 1172 | try analyzeWithContext(a, &body_table, body); |
| 1173 | |
| 1174 | if (new_set) |ns| { |
| 1175 | try ns.ensureUnusedCapacity(gpa, body_table.count()); |
| 1176 | var it = body_table.keyIterator(); |
| 1177 | while (it.next()) |key| { |
| 1178 | _ = ns.putAssumeCapacity(key.*, {}); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | try a.extra.ensureUnusedCapacity(gpa, std.meta.fields(Loop).len + body_table.count()); |
| 1183 | const extra_index = a.addExtraAssumeCapacity(Loop{ |
| 1184 | .death_count = body_table.count(), |
| 1185 | }); |
| 1186 | { |
| 1187 | var it = body_table.keyIterator(); |
| 1188 | while (it.next()) |key| { |
| 1189 | a.extra.appendAssumeCapacity(key.*); |
| 1190 | } |
| 1191 | } |
| 1192 | try a.special.put(gpa, inst, extra_index); |
| 1193 | |
| 1194 | // We'll remove invalid deaths in a separate pass after main liveness analysis. See |
| 1195 | // removeDeaths for more details. |
| 1196 | |
| 1142 | 1197 | return; // Loop has no operands and it is always unreferenced. |
| 1143 | 1198 | }, |
| 1144 | 1199 | .@"try" => { |
| ... | ... | @@ -1412,3 +1467,566 @@ const ExtraTombs = struct { |
| 1412 | 1467 | et.big_tomb_bits_extra.deinit(et.analysis.gpa); |
| 1413 | 1468 | } |
| 1414 | 1469 | }; |
| 1470 | |
| 1471 | /// Remove any deaths invalidated by the deaths from an enclosing `loop`. Reshuffling deaths stored |
| 1472 | /// in `extra` causes it to become non-dense, but that's fine - we won't remove too much data. |
| 1473 | /// Making it dense would be a lot more work - it'd require recomputing every index in `special`. |
| 1474 | fn removeDeaths( |
| 1475 | a: *Analysis, |
| 1476 | to_remove: *std.AutoHashMapUnmanaged(Air.Inst.Index, void), |
| 1477 | body: []const Air.Inst.Index, |
| 1478 | ) error{OutOfMemory}!void { |
| 1479 | for (body) |inst| { |
| 1480 | try removeInstDeaths(a, to_remove, inst); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | fn removeInstDeaths( |
| 1485 | a: *Analysis, |
| 1486 | to_remove: *std.AutoHashMapUnmanaged(Air.Inst.Index, void), |
| 1487 | inst: Air.Inst.Index, |
| 1488 | ) !void { |
| 1489 | const inst_tags = a.air.instructions.items(.tag); |
| 1490 | const inst_datas = a.air.instructions.items(.data); |
| 1491 | |
| 1492 | switch (inst_tags[inst]) { |
| 1493 | .add, |
| 1494 | .add_optimized, |
| 1495 | .addwrap, |
| 1496 | .addwrap_optimized, |
| 1497 | .add_sat, |
| 1498 | .sub, |
| 1499 | .sub_optimized, |
| 1500 | .subwrap, |
| 1501 | .subwrap_optimized, |
| 1502 | .sub_sat, |
| 1503 | .mul, |
| 1504 | .mul_optimized, |
| 1505 | .mulwrap, |
| 1506 | .mulwrap_optimized, |
| 1507 | .mul_sat, |
| 1508 | .div_float, |
| 1509 | .div_float_optimized, |
| 1510 | .div_trunc, |
| 1511 | .div_trunc_optimized, |
| 1512 | .div_floor, |
| 1513 | .div_floor_optimized, |
| 1514 | .div_exact, |
| 1515 | .div_exact_optimized, |
| 1516 | .rem, |
| 1517 | .rem_optimized, |
| 1518 | .mod, |
| 1519 | .mod_optimized, |
| 1520 | .bit_and, |
| 1521 | .bit_or, |
| 1522 | .xor, |
| 1523 | .cmp_lt, |
| 1524 | .cmp_lt_optimized, |
| 1525 | .cmp_lte, |
| 1526 | .cmp_lte_optimized, |
| 1527 | .cmp_eq, |
| 1528 | .cmp_eq_optimized, |
| 1529 | .cmp_gte, |
| 1530 | .cmp_gte_optimized, |
| 1531 | .cmp_gt, |
| 1532 | .cmp_gt_optimized, |
| 1533 | .cmp_neq, |
| 1534 | .cmp_neq_optimized, |
| 1535 | .bool_and, |
| 1536 | .bool_or, |
| 1537 | .store, |
| 1538 | .array_elem_val, |
| 1539 | .slice_elem_val, |
| 1540 | .ptr_elem_val, |
| 1541 | .shl, |
| 1542 | .shl_exact, |
| 1543 | .shl_sat, |
| 1544 | .shr, |
| 1545 | .shr_exact, |
| 1546 | .atomic_store_unordered, |
| 1547 | .atomic_store_monotonic, |
| 1548 | .atomic_store_release, |
| 1549 | .atomic_store_seq_cst, |
| 1550 | .set_union_tag, |
| 1551 | .min, |
| 1552 | .max, |
| 1553 | => { |
| 1554 | const o = inst_datas[inst].bin_op; |
| 1555 | removeOperandDeaths(a, to_remove, inst, .{ o.lhs, o.rhs, .none }); |
| 1556 | }, |
| 1557 | |
| 1558 | .vector_store_elem => { |
| 1559 | const o = inst_datas[inst].vector_store_elem; |
| 1560 | const extra = a.air.extraData(Air.Bin, o.payload).data; |
| 1561 | removeOperandDeaths(a, to_remove, inst, .{ o.vector_ptr, extra.lhs, extra.rhs }); |
| 1562 | }, |
| 1563 | |
| 1564 | .arg, |
| 1565 | .alloc, |
| 1566 | .ret_ptr, |
| 1567 | .constant, |
| 1568 | .const_ty, |
| 1569 | .trap, |
| 1570 | .breakpoint, |
| 1571 | .dbg_stmt, |
| 1572 | .dbg_inline_begin, |
| 1573 | .dbg_inline_end, |
| 1574 | .dbg_block_begin, |
| 1575 | .dbg_block_end, |
| 1576 | .unreach, |
| 1577 | .fence, |
| 1578 | .ret_addr, |
| 1579 | .frame_addr, |
| 1580 | .wasm_memory_size, |
| 1581 | .err_return_trace, |
| 1582 | .save_err_return_trace_index, |
| 1583 | .c_va_start, |
| 1584 | .work_item_id, |
| 1585 | .work_group_size, |
| 1586 | .work_group_id, |
| 1587 | => {}, |
| 1588 | |
| 1589 | .not, |
| 1590 | .bitcast, |
| 1591 | .load, |
| 1592 | .fpext, |
| 1593 | .fptrunc, |
| 1594 | .intcast, |
| 1595 | .trunc, |
| 1596 | .optional_payload, |
| 1597 | .optional_payload_ptr, |
| 1598 | .optional_payload_ptr_set, |
| 1599 | .errunion_payload_ptr_set, |
| 1600 | .wrap_optional, |
| 1601 | .unwrap_errunion_payload, |
| 1602 | .unwrap_errunion_err, |
| 1603 | .unwrap_errunion_payload_ptr, |
| 1604 | .unwrap_errunion_err_ptr, |
| 1605 | .wrap_errunion_payload, |
| 1606 | .wrap_errunion_err, |
| 1607 | .slice_ptr, |
| 1608 | .slice_len, |
| 1609 | .ptr_slice_len_ptr, |
| 1610 | .ptr_slice_ptr_ptr, |
| 1611 | .struct_field_ptr_index_0, |
| 1612 | .struct_field_ptr_index_1, |
| 1613 | .struct_field_ptr_index_2, |
| 1614 | .struct_field_ptr_index_3, |
| 1615 | .array_to_slice, |
| 1616 | .float_to_int, |
| 1617 | .float_to_int_optimized, |
| 1618 | .int_to_float, |
| 1619 | .get_union_tag, |
| 1620 | .clz, |
| 1621 | .ctz, |
| 1622 | .popcount, |
| 1623 | .byte_swap, |
| 1624 | .bit_reverse, |
| 1625 | .splat, |
| 1626 | .error_set_has_value, |
| 1627 | .addrspace_cast, |
| 1628 | .c_va_arg, |
| 1629 | .c_va_copy, |
| 1630 | => { |
| 1631 | const o = inst_datas[inst].ty_op; |
| 1632 | removeOperandDeaths(a, to_remove, inst, .{ o.operand, .none, .none }); |
| 1633 | }, |
| 1634 | |
| 1635 | .is_null, |
| 1636 | .is_non_null, |
| 1637 | .is_null_ptr, |
| 1638 | .is_non_null_ptr, |
| 1639 | .is_err, |
| 1640 | .is_non_err, |
| 1641 | .is_err_ptr, |
| 1642 | .is_non_err_ptr, |
| 1643 | .ptrtoint, |
| 1644 | .bool_to_int, |
| 1645 | .ret, |
| 1646 | .ret_load, |
| 1647 | .is_named_enum_value, |
| 1648 | .tag_name, |
| 1649 | .error_name, |
| 1650 | .sqrt, |
| 1651 | .sin, |
| 1652 | .cos, |
| 1653 | .tan, |
| 1654 | .exp, |
| 1655 | .exp2, |
| 1656 | .log, |
| 1657 | .log2, |
| 1658 | .log10, |
| 1659 | .fabs, |
| 1660 | .floor, |
| 1661 | .ceil, |
| 1662 | .round, |
| 1663 | .trunc_float, |
| 1664 | .neg, |
| 1665 | .neg_optimized, |
| 1666 | .cmp_lt_errors_len, |
| 1667 | .set_err_return_trace, |
| 1668 | .c_va_end, |
| 1669 | => { |
| 1670 | const operand = inst_datas[inst].un_op; |
| 1671 | removeOperandDeaths(a, to_remove, inst, .{ operand, .none, .none }); |
| 1672 | }, |
| 1673 | |
| 1674 | .add_with_overflow, |
| 1675 | .sub_with_overflow, |
| 1676 | .mul_with_overflow, |
| 1677 | .shl_with_overflow, |
| 1678 | .ptr_add, |
| 1679 | .ptr_sub, |
| 1680 | .ptr_elem_ptr, |
| 1681 | .slice_elem_ptr, |
| 1682 | .slice, |
| 1683 | => { |
| 1684 | const ty_pl = inst_datas[inst].ty_pl; |
| 1685 | const extra = a.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1686 | removeOperandDeaths(a, to_remove, inst, .{ extra.lhs, extra.rhs, .none }); |
| 1687 | }, |
| 1688 | |
| 1689 | .dbg_var_ptr, |
| 1690 | .dbg_var_val, |
| 1691 | => { |
| 1692 | const operand = inst_datas[inst].pl_op.operand; |
| 1693 | removeOperandDeaths(a, to_remove, inst, .{ operand, .none, .none }); |
| 1694 | }, |
| 1695 | |
| 1696 | .prefetch => { |
| 1697 | const prefetch = inst_datas[inst].prefetch; |
| 1698 | removeOperandDeaths(a, to_remove, inst, .{ prefetch.ptr, .none, .none }); |
| 1699 | }, |
| 1700 | |
| 1701 | .call, .call_always_tail, .call_never_tail, .call_never_inline => { |
| 1702 | const inst_data = inst_datas[inst].pl_op; |
| 1703 | const callee = inst_data.operand; |
| 1704 | const extra = a.air.extraData(Air.Call, inst_data.payload); |
| 1705 | const args = @ptrCast([]const Air.Inst.Ref, a.air.extra[extra.end..][0..extra.data.args_len]); |
| 1706 | |
| 1707 | var death_remover = BigTombDeathRemover.init(a, to_remove, inst); |
| 1708 | death_remover.feed(callee); |
| 1709 | for (args) |operand| { |
| 1710 | death_remover.feed(operand); |
| 1711 | } |
| 1712 | death_remover.finish(); |
| 1713 | }, |
| 1714 | .select => { |
| 1715 | const pl_op = inst_datas[inst].pl_op; |
| 1716 | const extra = a.air.extraData(Air.Bin, pl_op.payload).data; |
| 1717 | removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, extra.lhs, extra.rhs }); |
| 1718 | }, |
| 1719 | .shuffle => { |
| 1720 | const extra = a.air.extraData(Air.Shuffle, inst_datas[inst].ty_pl.payload).data; |
| 1721 | removeOperandDeaths(a, to_remove, inst, .{ extra.a, extra.b, .none }); |
| 1722 | }, |
| 1723 | .reduce, .reduce_optimized => { |
| 1724 | const reduce = inst_datas[inst].reduce; |
| 1725 | removeOperandDeaths(a, to_remove, inst, .{ reduce.operand, .none, .none }); |
| 1726 | }, |
| 1727 | .cmp_vector, .cmp_vector_optimized => { |
| 1728 | const extra = a.air.extraData(Air.VectorCmp, inst_datas[inst].ty_pl.payload).data; |
| 1729 | removeOperandDeaths(a, to_remove, inst, .{ extra.lhs, extra.rhs, .none }); |
| 1730 | }, |
| 1731 | .aggregate_init => { |
| 1732 | const ty_pl = inst_datas[inst].ty_pl; |
| 1733 | const aggregate_ty = a.air.getRefType(ty_pl.ty); |
| 1734 | const len = @intCast(usize, aggregate_ty.arrayLen()); |
| 1735 | const elements = @ptrCast([]const Air.Inst.Ref, a.air.extra[ty_pl.payload..][0..len]); |
| 1736 | |
| 1737 | var death_remover = BigTombDeathRemover.init(a, to_remove, inst); |
| 1738 | for (elements) |elem| { |
| 1739 | death_remover.feed(elem); |
| 1740 | } |
| 1741 | death_remover.finish(); |
| 1742 | }, |
| 1743 | .union_init => { |
| 1744 | const extra = a.air.extraData(Air.UnionInit, inst_datas[inst].ty_pl.payload).data; |
| 1745 | removeOperandDeaths(a, to_remove, inst, .{ extra.init, .none, .none }); |
| 1746 | }, |
| 1747 | .struct_field_ptr, .struct_field_val => { |
| 1748 | const extra = a.air.extraData(Air.StructField, inst_datas[inst].ty_pl.payload).data; |
| 1749 | removeOperandDeaths(a, to_remove, inst, .{ extra.struct_operand, .none, .none }); |
| 1750 | }, |
| 1751 | .field_parent_ptr => { |
| 1752 | const extra = a.air.extraData(Air.FieldParentPtr, inst_datas[inst].ty_pl.payload).data; |
| 1753 | removeOperandDeaths(a, to_remove, inst, .{ extra.field_ptr, .none, .none }); |
| 1754 | }, |
| 1755 | .cmpxchg_strong, .cmpxchg_weak => { |
| 1756 | const extra = a.air.extraData(Air.Cmpxchg, inst_datas[inst].ty_pl.payload).data; |
| 1757 | removeOperandDeaths(a, to_remove, inst, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 1758 | }, |
| 1759 | .mul_add => { |
| 1760 | const pl_op = inst_datas[inst].pl_op; |
| 1761 | const extra = a.air.extraData(Air.Bin, pl_op.payload).data; |
| 1762 | removeOperandDeaths(a, to_remove, inst, .{ extra.lhs, extra.rhs, pl_op.operand }); |
| 1763 | }, |
| 1764 | .atomic_load => { |
| 1765 | const ptr = inst_datas[inst].atomic_load.ptr; |
| 1766 | removeOperandDeaths(a, to_remove, inst, .{ ptr, .none, .none }); |
| 1767 | }, |
| 1768 | .atomic_rmw => { |
| 1769 | const pl_op = inst_datas[inst].pl_op; |
| 1770 | const extra = a.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 1771 | removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, extra.operand, .none }); |
| 1772 | }, |
| 1773 | .memset, |
| 1774 | .memcpy, |
| 1775 | => { |
| 1776 | const pl_op = inst_datas[inst].pl_op; |
| 1777 | const extra = a.air.extraData(Air.Bin, pl_op.payload).data; |
| 1778 | removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, extra.lhs, extra.rhs }); |
| 1779 | }, |
| 1780 | |
| 1781 | .br => { |
| 1782 | const br = inst_datas[inst].br; |
| 1783 | removeOperandDeaths(a, to_remove, inst, .{ br.operand, .none, .none }); |
| 1784 | }, |
| 1785 | .assembly => { |
| 1786 | const extra = a.air.extraData(Air.Asm, inst_datas[inst].ty_pl.payload); |
| 1787 | var extra_i: usize = extra.end; |
| 1788 | const outputs = @ptrCast([]const Air.Inst.Ref, a.air.extra[extra_i..][0..extra.data.outputs_len]); |
| 1789 | extra_i += outputs.len; |
| 1790 | const inputs = @ptrCast([]const Air.Inst.Ref, a.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 1791 | extra_i += inputs.len; |
| 1792 | |
| 1793 | var death_remover = BigTombDeathRemover.init(a, to_remove, inst); |
| 1794 | for (outputs) |output| { |
| 1795 | if (output != .none) { |
| 1796 | death_remover.feed(output); |
| 1797 | } |
| 1798 | } |
| 1799 | for (inputs) |input| { |
| 1800 | death_remover.feed(input); |
| 1801 | } |
| 1802 | death_remover.finish(); |
| 1803 | }, |
| 1804 | .block => { |
| 1805 | const extra = a.air.extraData(Air.Block, inst_datas[inst].ty_pl.payload); |
| 1806 | const body = a.air.extra[extra.end..][0..extra.data.body_len]; |
| 1807 | try removeDeaths(a, to_remove, body); |
| 1808 | }, |
| 1809 | .loop => { |
| 1810 | const extra = a.air.extraData(Air.Block, inst_datas[inst].ty_pl.payload); |
| 1811 | const body = a.air.extra[extra.end..][0..extra.data.body_len]; |
| 1812 | |
| 1813 | const liveness_extra_idx = a.special.get(inst) orelse { |
| 1814 | try removeDeaths(a, to_remove, body); |
| 1815 | return; |
| 1816 | }; |
| 1817 | |
| 1818 | const death_count = a.extra.items[liveness_extra_idx]; |
| 1819 | var deaths = a.extra.items[liveness_extra_idx + 1 ..][0..death_count]; |
| 1820 | |
| 1821 | // Remove any deaths in `to_remove` from this loop's deaths |
| 1822 | deaths.len = removeExtraDeaths(to_remove, deaths); |
| 1823 | a.extra.items[liveness_extra_idx] = @intCast(u32, deaths.len); |
| 1824 | |
| 1825 | // Temporarily add any deaths of ours to `to_remove` |
| 1826 | try to_remove.ensureUnusedCapacity(a.gpa, @intCast(u32, deaths.len)); |
| 1827 | for (deaths) |d| { |
| 1828 | to_remove.putAssumeCapacity(d, {}); |
| 1829 | } |
| 1830 | try removeDeaths(a, to_remove, body); |
| 1831 | for (deaths) |d| { |
| 1832 | _ = to_remove.remove(d); |
| 1833 | } |
| 1834 | }, |
| 1835 | .@"try" => { |
| 1836 | const pl_op = inst_datas[inst].pl_op; |
| 1837 | const extra = a.air.extraData(Air.Try, pl_op.payload); |
| 1838 | const body = a.air.extra[extra.end..][0..extra.data.body_len]; |
| 1839 | try removeDeaths(a, to_remove, body); |
| 1840 | removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, .none, .none }); |
| 1841 | }, |
| 1842 | .try_ptr => { |
| 1843 | const extra = a.air.extraData(Air.TryPtr, inst_datas[inst].ty_pl.payload); |
| 1844 | const body = a.air.extra[extra.end..][0..extra.data.body_len]; |
| 1845 | try removeDeaths(a, to_remove, body); |
| 1846 | removeOperandDeaths(a, to_remove, inst, .{ extra.data.ptr, .none, .none }); |
| 1847 | }, |
| 1848 | .cond_br => { |
| 1849 | const inst_data = inst_datas[inst].pl_op; |
| 1850 | const condition = inst_data.operand; |
| 1851 | const extra = a.air.extraData(Air.CondBr, inst_data.payload); |
| 1852 | const then_body = a.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 1853 | const else_body = a.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 1854 | |
| 1855 | if (a.special.get(inst)) |liveness_extra_idx| { |
| 1856 | const then_death_count = a.extra.items[liveness_extra_idx + 0]; |
| 1857 | const else_death_count = a.extra.items[liveness_extra_idx + 1]; |
| 1858 | var then_deaths = a.extra.items[liveness_extra_idx + 2 ..][0..then_death_count]; |
| 1859 | var else_deaths = a.extra.items[liveness_extra_idx + 2 + then_death_count ..][0..else_death_count]; |
| 1860 | |
| 1861 | const new_then_death_count = removeExtraDeaths(to_remove, then_deaths); |
| 1862 | const new_else_death_count = removeExtraDeaths(to_remove, else_deaths); |
| 1863 | |
| 1864 | a.extra.items[liveness_extra_idx + 0] = new_then_death_count; |
| 1865 | a.extra.items[liveness_extra_idx + 1] = new_else_death_count; |
| 1866 | |
| 1867 | if (new_then_death_count < then_death_count) { |
| 1868 | // `else` deaths need to be moved earlier in `extra` |
| 1869 | const src = a.extra.items[liveness_extra_idx + 2 + then_death_count ..]; |
| 1870 | const dest = a.extra.items[liveness_extra_idx + 2 + new_then_death_count ..]; |
| 1871 | std.mem.copy(u32, dest, src[0..new_else_death_count]); |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | try removeDeaths(a, to_remove, then_body); |
| 1876 | try removeDeaths(a, to_remove, else_body); |
| 1877 | |
| 1878 | removeOperandDeaths(a, to_remove, inst, .{ condition, .none, .none }); |
| 1879 | }, |
| 1880 | .switch_br => { |
| 1881 | const pl_op = inst_datas[inst].pl_op; |
| 1882 | const condition = pl_op.operand; |
| 1883 | const switch_br = a.air.extraData(Air.SwitchBr, pl_op.payload); |
| 1884 | |
| 1885 | var air_extra_index: usize = switch_br.end; |
| 1886 | for (0..switch_br.data.cases_len) |_| { |
| 1887 | const case = a.air.extraData(Air.SwitchBr.Case, air_extra_index); |
| 1888 | const case_body = a.air.extra[case.end + case.data.items_len ..][0..case.data.body_len]; |
| 1889 | air_extra_index = case.end + case.data.items_len + case_body.len; |
| 1890 | try removeDeaths(a, to_remove, case_body); |
| 1891 | } |
| 1892 | { // else |
| 1893 | const else_body = a.air.extra[air_extra_index..][0..switch_br.data.else_body_len]; |
| 1894 | try removeDeaths(a, to_remove, else_body); |
| 1895 | } |
| 1896 | |
| 1897 | if (a.special.get(inst)) |liveness_extra_idx| { |
| 1898 | const else_death_count = a.extra.items[liveness_extra_idx]; |
| 1899 | var read_idx = liveness_extra_idx + 1; |
| 1900 | var write_idx = read_idx; // write_idx <= read_idx always |
| 1901 | for (0..switch_br.data.cases_len) |_| { |
| 1902 | const case_death_count = a.extra.items[read_idx]; |
| 1903 | const case_deaths = a.extra.items[read_idx + 1 ..][0..case_death_count]; |
| 1904 | const new_death_count = removeExtraDeaths(to_remove, case_deaths); |
| 1905 | a.extra.items[write_idx] = new_death_count; |
| 1906 | if (write_idx < read_idx) { |
| 1907 | std.mem.copy(u32, a.extra.items[write_idx + 1 ..], a.extra.items[read_idx + 1 ..][0..new_death_count]); |
| 1908 | } |
| 1909 | read_idx += 1 + case_death_count; |
| 1910 | write_idx += 1 + new_death_count; |
| 1911 | } |
| 1912 | const else_deaths = a.extra.items[read_idx..][0..else_death_count]; |
| 1913 | const new_else_death_count = removeExtraDeaths(to_remove, else_deaths); |
| 1914 | a.extra.items[liveness_extra_idx] = new_else_death_count; |
| 1915 | if (write_idx < read_idx) { |
| 1916 | std.mem.copy(u32, a.extra.items[write_idx..], a.extra.items[read_idx..][0..new_else_death_count]); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | removeOperandDeaths(a, to_remove, inst, .{ condition, .none, .none }); |
| 1921 | }, |
| 1922 | .wasm_memory_grow => { |
| 1923 | const pl_op = inst_datas[inst].pl_op; |
| 1924 | removeOperandDeaths(a, to_remove, inst, .{ pl_op.operand, .none, .none }); |
| 1925 | }, |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | fn removeOperandDeaths( |
| 1930 | a: *Analysis, |
| 1931 | to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void), |
| 1932 | inst: Air.Inst.Index, |
| 1933 | operands: [bpi - 1]Air.Inst.Ref, |
| 1934 | ) void { |
| 1935 | const usize_index = (inst * bpi) / @bitSizeOf(usize); |
| 1936 | |
| 1937 | const cur_tomb = @truncate(Bpi, a.tomb_bits[usize_index] >> |
| 1938 | @intCast(Log2Int(usize), (inst % (@bitSizeOf(usize) / bpi)) * bpi)); |
| 1939 | |
| 1940 | var toggle_bits: Bpi = 0; |
| 1941 | |
| 1942 | for (operands, 0..) |op_ref, i| { |
| 1943 | const mask = @as(Bpi, 1) << @intCast(OperandInt, i); |
| 1944 | const op_int = @enumToInt(op_ref); |
| 1945 | if (op_int < Air.Inst.Ref.typed_value_map.len) continue; |
| 1946 | const operand: Air.Inst.Index = op_int - @intCast(u32, Air.Inst.Ref.typed_value_map.len); |
| 1947 | if ((cur_tomb & mask) != 0 and to_remove.contains(operand)) { |
| 1948 | log.debug("remove death of %{} in %{}", .{ operand, inst }); |
| 1949 | toggle_bits ^= mask; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | a.tomb_bits[usize_index] ^= @as(usize, toggle_bits) << |
| 1954 | @intCast(Log2Int(usize), (inst % (@bitSizeOf(usize) / bpi)) * bpi); |
| 1955 | } |
| 1956 | |
| 1957 | fn removeExtraDeaths( |
| 1958 | to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void), |
| 1959 | deaths: []Air.Inst.Index, |
| 1960 | ) u32 { |
| 1961 | var new_len = @intCast(u32, deaths.len); |
| 1962 | var i: usize = 0; |
| 1963 | while (i < new_len) { |
| 1964 | if (to_remove.contains(deaths[i])) { |
| 1965 | log.debug("remove extra death of %{}", .{deaths[i]}); |
| 1966 | deaths[i] = deaths[new_len - 1]; |
| 1967 | new_len -= 1; |
| 1968 | } else { |
| 1969 | i += 1; |
| 1970 | } |
| 1971 | } |
| 1972 | return new_len; |
| 1973 | } |
| 1974 | |
| 1975 | const BigTombDeathRemover = struct { |
| 1976 | a: *Analysis, |
| 1977 | to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void), |
| 1978 | inst: Air.Inst.Index, |
| 1979 | |
| 1980 | operands: [bpi - 1]Air.Inst.Ref = .{.none} ** (bpi - 1), |
| 1981 | next_oper: OperandInt = 0, |
| 1982 | |
| 1983 | bit_index: u32 = 0, |
| 1984 | // Initialized once we finish the small tomb operands: see `feed` |
| 1985 | extra_start: u32 = undefined, |
| 1986 | extra_offset: u32 = 0, |
| 1987 | |
| 1988 | fn init(a: *Analysis, to_remove: *const std.AutoHashMapUnmanaged(Air.Inst.Index, void), inst: Air.Inst.Index) BigTombDeathRemover { |
| 1989 | return .{ |
| 1990 | .a = a, |
| 1991 | .to_remove = to_remove, |
| 1992 | .inst = inst, |
| 1993 | }; |
| 1994 | } |
| 1995 | |
| 1996 | fn feed(dr: *BigTombDeathRemover, operand: Air.Inst.Ref) void { |
| 1997 | if (dr.next_oper < bpi - 1) { |
| 1998 | dr.operands[dr.next_oper] = operand; |
| 1999 | dr.next_oper += 1; |
| 2000 | if (dr.next_oper == bpi - 1) { |
| 2001 | removeOperandDeaths(dr.a, dr.to_remove, dr.inst, dr.operands); |
| 2002 | if (dr.a.special.get(dr.inst)) |idx| dr.extra_start = idx; |
| 2003 | } |
| 2004 | return; |
| 2005 | } |
| 2006 | |
| 2007 | defer dr.bit_index += 1; |
| 2008 | |
| 2009 | const op_int = @enumToInt(operand); |
| 2010 | if (op_int < Air.Inst.Ref.typed_value_map.len) return; |
| 2011 | |
| 2012 | const op_inst: Air.Inst.Index = op_int - @intCast(u32, Air.Inst.Ref.typed_value_map.len); |
| 2013 | |
| 2014 | while (dr.bit_index - dr.extra_offset * 31 >= 31) { |
| 2015 | dr.extra_offset += 1; |
| 2016 | } |
| 2017 | const dies = @truncate(u1, dr.a.extra.items[dr.extra_start + dr.extra_offset] >> |
| 2018 | @intCast(u5, dr.bit_index - dr.extra_offset * 31)) != 0; |
| 2019 | |
| 2020 | if (dies and dr.to_remove.contains(op_inst)) { |
| 2021 | log.debug("remove big death of %{}", .{op_inst}); |
| 2022 | dr.a.extra.items[dr.extra_start + dr.extra_offset] ^= |
| 2023 | (@as(u32, 1) << @intCast(u5, dr.bit_index - dr.extra_offset * 31)); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | fn finish(dr: *BigTombDeathRemover) void { |
| 2028 | if (dr.next_oper < bpi) { |
| 2029 | removeOperandDeaths(dr.a, dr.to_remove, dr.inst, dr.operands); |
| 2030 | } |
| 2031 | } |
| 2032 | }; |