authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-12 19:44:41+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-13 11:32:08+01:00
log384e4ddb061634ae6c62e8c22c8876a818c1e2a3
tree71d5c331b90d0d8227eeaa82fa26b2640ae88238
parent12207bbbd620903c81b0134816e71e373751b2d6
signaturelock-open Commit is signed but in an unrecognized format.

stage2 AArch64: spill compare flags when necessary


1 files changed, 52 insertions(+), 3 deletions(-)

src/arch/aarch64/CodeGen.zig+52-3
......@@ -85,6 +85,8 @@ blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .{},
8585register_manager: RegisterManager = .{},
8686/// Maps offset to what is stored there.
8787stack: std.AutoHashMapUnmanaged(u32, StackAllocation) = .{},
88/// Tracks the current instruction allocated to the compare flags
89compare_flags_inst: ?Air.Inst.Index = null,
8890
8991/// Offset from the stack base, representing the end of the stack frame.
9092max_end_stack: u32 = 0,
......@@ -722,6 +724,9 @@ fn processDeath(self: *Self, inst: Air.Inst.Index) void {
722724 const canon_reg = toCanonicalReg(reg);
723725 self.register_manager.freeReg(canon_reg);
724726 },
727 .compare_flags_signed, .compare_flags_unsigned => {
728 self.compare_flags_inst = null;
729 },
725730 else => {}, // TODO process stack allocation death
726731 }
727732}
......@@ -857,6 +862,24 @@ pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void
857862 try self.genSetStack(self.air.typeOfIndex(inst), stack_mcv.stack_offset, reg_mcv);
858863}
859864
865/// Save the current instruction stored in the compare flags if
866/// occupied
867fn spillCompareFlagsIfOccupied(self: *Self) !void {
868 if (self.compare_flags_inst) |inst_to_save| {
869 const mcv = self.getResolvedInstValue(inst_to_save);
870 assert(mcv == .compare_flags_signed or mcv == .compare_flags_unsigned);
871
872 const new_mcv = try self.allocRegOrMem(inst_to_save, true);
873 try self.setRegOrMem(self.air.typeOfIndex(inst_to_save), new_mcv, mcv);
874 log.debug("spilling {d} to mcv {any}", .{ inst_to_save, new_mcv });
875
876 const branch = &self.branch_stack.items[self.branch_stack.items.len - 1];
877 try branch.inst_table.put(self.gpa, inst_to_save, new_mcv);
878
879 self.compare_flags_inst = null;
880 }
881}
882
860883/// Copies a value to a register without tracking the register. The register is not considered
861884/// allocated. A second call to `copyToTmpRegister` may return the same register.
862885/// This can have a side effect of spilling instructions to the stack to free up a register.
......@@ -2402,6 +2425,16 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
24022425 var info = try self.resolveCallingConventionValues(fn_ty);
24032426 defer info.deinit(self);
24042427
2428 // According to the Procedure Call Standard for the ARM
2429 // Architecture, compare flags are not preserved across
2430 // calls. Therefore, if some value is currently stored there, we
2431 // need to save it.
2432 //
2433 // TODO once caller-saved registers are implemented, save them
2434 // here too, but crucially *after* we save the compare flags as
2435 // saving compare flags may require a new caller-saved register
2436 try self.spillCompareFlagsIfOccupied();
2437
24052438 for (info.args) |mc_arg, arg_i| {
24062439 const arg = args[arg_i];
24072440 const arg_ty = self.air.typeOf(arg);
......@@ -2593,6 +2626,9 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void {
25932626 return self.fail("TODO cmp for types with size > 8", .{});
25942627 }
25952628
2629 try self.spillCompareFlagsIfOccupied();
2630 self.compare_flags_inst = inst;
2631
25962632 const signedness: std.builtin.Signedness = blk: {
25972633 // by default we tell the operand type is unsigned (i.e. bools and enum values)
25982634 if (ty.zigTypeTag() != .Int) break :blk .unsigned;
......@@ -2755,12 +2791,24 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
27552791 },
27562792 };
27572793
2794 // If the condition dies here in this condbr instruction, process
2795 // that death now instead of later as this has an effect on
2796 // whether it needs to be spilled in the branches
2797 if (self.liveness.operandDies(inst, 0)) {
2798 const op_int = @enumToInt(pl_op.operand);
2799 if (op_int >= Air.Inst.Ref.typed_value_map.len) {
2800 const op_index = @intCast(Air.Inst.Index, op_int - Air.Inst.Ref.typed_value_map.len);
2801 self.processDeath(op_index);
2802 }
2803 }
2804
27582805 // Capture the state of register and stack allocation state so that we can revert to it.
27592806 const parent_next_stack_offset = self.next_stack_offset;
27602807 const parent_free_registers = self.register_manager.free_registers;
27612808 var parent_stack = try self.stack.clone(self.gpa);
27622809 defer parent_stack.deinit(self.gpa);
27632810 const parent_registers = self.register_manager.registers;
2811 const parent_compare_flags_inst = self.compare_flags_inst;
27642812
27652813 try self.branch_stack.append(.{});
27662814
......@@ -2776,6 +2824,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
27762824 defer saved_then_branch.deinit(self.gpa);
27772825
27782826 self.register_manager.registers = parent_registers;
2827 self.compare_flags_inst = parent_compare_flags_inst;
27792828
27802829 self.stack.deinit(self.gpa);
27812830 self.stack = parent_stack;
......@@ -2867,7 +2916,9 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
28672916
28682917 self.branch_stack.pop().deinit(self.gpa);
28692918
2870 return self.finishAir(inst, .unreach, .{ pl_op.operand, .none, .none });
2919 // We already took care of pl_op.operand earlier, so we're going
2920 // to pass .none here
2921 return self.finishAir(inst, .unreach, .{ .none, .none, .none });
28712922}
28722923
28732924fn isNull(self: *Self, operand: MCValue) !MCValue {
......@@ -2885,8 +2936,6 @@ fn isNonNull(self: *Self, operand: MCValue) !MCValue {
28852936}
28862937
28872938fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
2888 _ = operand;
2889
28902939 const error_type = ty.errorUnionSet();
28912940 const payload_type = ty.errorUnionPayload();
28922941