| ... | @@ -1072,10 +1072,44 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1072,10 +1072,44 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1072 | | 1072 | |
| 1073 | fn airMin(self: *Self, inst: Air.Inst.Index) !void { | 1073 | fn airMin(self: *Self, inst: Air.Inst.Index) !void { |
| 1074 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 1074 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1075 | const result: MCValue = if (self.liveness.isUnused(inst)) | 1075 | if (self.liveness.isUnused(inst)) { |
| 1076 | .dead | 1076 | return self.finishAir(inst, .dead, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 1077 | else | 1077 | } |
| 1078 | return self.fail("TODO implement min for {}", .{self.target.cpu.arch}); | 1078 | |
| | 1079 | const ty = self.air.typeOfIndex(inst); |
| | 1080 | if (ty.zigTypeTag() != .Int) { |
| | 1081 | return self.fail("TODO implement min for type {}", .{ty}); |
| | 1082 | } |
| | 1083 | const signedness = ty.intInfo(self.target.*).signedness; |
| | 1084 | const result: MCValue = result: { |
| | 1085 | // TODO improve by checking if any operand can be reused. |
| | 1086 | // TODO audit register allocation |
| | 1087 | const lhs = try self.resolveInst(bin_op.lhs); |
| | 1088 | lhs.freezeIfRegister(&self.register_manager); |
| | 1089 | defer lhs.unfreezeIfRegister(&self.register_manager); |
| | 1090 | |
| | 1091 | const lhs_reg = try self.copyToTmpRegister(ty, lhs); |
| | 1092 | self.register_manager.freezeRegs(&.{lhs_reg}); |
| | 1093 | defer self.register_manager.unfreezeRegs(&.{lhs_reg}); |
| | 1094 | |
| | 1095 | const rhs_mcv = try self.limitImmediateType(bin_op.rhs, i32); |
| | 1096 | rhs_mcv.freezeIfRegister(&self.register_manager); |
| | 1097 | defer rhs_mcv.unfreezeIfRegister(&self.register_manager); |
| | 1098 | |
| | 1099 | try self.genBinMathOpMir(.cmp, ty, .{ .register = lhs_reg }, rhs_mcv); |
| | 1100 | |
| | 1101 | const dst_mcv = try self.copyToRegisterWithInstTracking(inst, ty, rhs_mcv); |
| | 1102 | _ = try self.addInst(.{ |
| | 1103 | .tag = if (signedness == .signed) .cond_mov_lt else .cond_mov_below, |
| | 1104 | .ops = (Mir.Ops{ |
| | 1105 | .reg1 = dst_mcv.register, |
| | 1106 | .reg2 = lhs_reg, |
| | 1107 | }).encode(), |
| | 1108 | .data = undefined, |
| | 1109 | }); |
| | 1110 | |
| | 1111 | break :result dst_mcv; |
| | 1112 | }; |
| 1079 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); | 1113 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 1080 | } | 1114 | } |
| 1081 | | 1115 | |
| ... | @@ -3283,6 +3317,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { | ... | @@ -3283,6 +3317,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 3283 | // There are 2 operands, destination and source. | 3317 | // There are 2 operands, destination and source. |
| 3284 | // Either one, but not both, can be a memory operand. | 3318 | // Either one, but not both, can be a memory operand. |
| 3285 | // Source operand can be an immediate, 8 bits or 32 bits. | 3319 | // Source operand can be an immediate, 8 bits or 32 bits. |
| | 3320 | // TODO this looks wrong. Why do simply reuse lhs without checking if it is dead or alive? |
| 3286 | const dst_mcv = if (lhs.isImmediate() or (lhs.isMemory() and rhs.isMemory())) | 3321 | const dst_mcv = if (lhs.isImmediate() or (lhs.isMemory() and rhs.isMemory())) |
| 3287 | MCValue{ .register = try self.copyToTmpRegister(ty, lhs) } | 3322 | MCValue{ .register = try self.copyToTmpRegister(ty, lhs) } |
| 3288 | else | 3323 | else |