From 83744b92a1d1a923fb3faf3aca9d0f57b09cb97d Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 17 Feb 2022 22:49:01 +0100 Subject: [PATCH] x64: fix wrong regalloc with inst tracking in airCmp We return compare flags rather than a register which than wrongly cheats the regalloc into thinking we carry the instruction in the register which we do not. --- src/arch/x86_64/CodeGen.zig | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 6b27627825..2163cfd0bd 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -1724,7 +1724,13 @@ fn airUnaryMath(self: *Self, inst: Air.Inst.Index) !void { return self.finishAir(inst, result, .{ un_op, .none, .none }); } -fn reuseOperand(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, op_index: Liveness.OperandInt, mcv: MCValue) bool { +fn reuseOperand( + self: *Self, + inst: Air.Inst.Index, + operand: Air.Inst.Ref, + op_index: Liveness.OperandInt, + mcv: MCValue, +) bool { if (!self.liveness.operandDies(inst, op_index)) return false; @@ -2267,13 +2273,14 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs: // A potential opportunity for future optimization here would be keeping track // of the fact that the instruction is available both as an immediate // and as a register. + // TODO consolidate with limitImmediateType() function switch (src_mcv) { .immediate => |imm| { if (imm > math.maxInt(u31)) { dst_mcv.freezeIfRegister(&self.register_manager); defer dst_mcv.unfreezeIfRegister(&self.register_manager); - src_mcv = try self.copyToNewRegister(inst, Type.u64, src_mcv); + src_mcv = MCValue{ .register = try self.copyToTmpRegister(Type.usize, src_mcv) }; } }, else => {}, @@ -2907,7 +2914,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { // Either one, but not both, can be a memory operand. // Source operand can be an immediate, 8 bits or 32 bits. const dst_mcv = if (lhs.isImmediate() or (lhs.isMemory() and rhs.isMemory())) - try self.copyToNewRegister(inst, ty, lhs) + MCValue{ .register = try self.copyToTmpRegister(ty, lhs) } else lhs; // This instruction supports only signed 32-bit immediates at most.