authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2024-04-05 13:20:14+02:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-04-06 14:57:46-04:00
log637b1d606d52ed2984013bfd1e196934b08de4a9
tree5986a9306ea88b47383367d7d91946f5e2a04a19
parent39420838061a9049fbc889212836a9d4d2ab9af4

LLVM Builder: Emit binary op optional flags for exact and no wrap


2 files changed, 86 insertions(+), 16 deletions(-)

src/codegen/llvm/Builder.zig+50-16
...@@ -14734,39 +14734,23 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14734,39 +14734,23 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14734 }, adapter);14734 }, adapter);
14735 },14735 },
14736 .add,14736 .add,
14737 .@"add nsw",
14738 .@"add nuw",
14739 .@"add nuw nsw",
14740 .@"and",14737 .@"and",
14741 .fadd,14738 .fadd,
14742 .fdiv,14739 .fdiv,
14743 .fmul,14740 .fmul,
14744 .mul,14741 .mul,
14745 .@"mul nsw",
14746 .@"mul nuw",
14747 .@"mul nuw nsw",
14748 .frem,14742 .frem,
14749 .fsub,14743 .fsub,
14750 .sdiv,14744 .sdiv,
14751 .@"sdiv exact",
14752 .sub,14745 .sub,
14753 .@"sub nsw",
14754 .@"sub nuw",
14755 .@"sub nuw nsw",
14756 .udiv,14746 .udiv,
14757 .@"udiv exact",
14758 .xor,14747 .xor,
14759 .shl,14748 .shl,
14760 .@"shl nsw",
14761 .@"shl nuw",
14762 .@"shl nuw nsw",
14763 .lshr,14749 .lshr,
14764 .@"lshr exact",
14765 .@"or",14750 .@"or",
14766 .urem,14751 .urem,
14767 .srem,14752 .srem,
14768 .ashr,14753 .ashr,
14769 .@"ashr exact",
14770 => |kind| {14754 => |kind| {
14771 const extra = func.extraData(Function.Instruction.Binary, datas[instr_index]);14755 const extra = func.extraData(Function.Instruction.Binary, datas[instr_index]);
14772 try function_block.writeAbbrev(FunctionBlock.Binary{14756 try function_block.writeAbbrev(FunctionBlock.Binary{
...@@ -14775,6 +14759,56 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14775,6 +14759,56 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14775 .rhs = adapter.getOffsetValueIndex(extra.rhs),14759 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14776 });14760 });
14777 },14761 },
14762 .@"sdiv exact",
14763 .@"udiv exact",
14764 .@"lshr exact",
14765 .@"ashr exact",
14766 => |kind| {
14767 const extra = func.extraData(Function.Instruction.Binary, datas[instr_index]);
14768 try function_block.writeAbbrev(FunctionBlock.BinaryExact{
14769 .opcode = kind.toBinaryOpcode(),
14770 .lhs = adapter.getOffsetValueIndex(extra.lhs),
14771 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14772 });
14773 },
14774 .@"add nsw",
14775 .@"add nuw",
14776 .@"add nuw nsw",
14777 .@"mul nsw",
14778 .@"mul nuw",
14779 .@"mul nuw nsw",
14780 .@"sub nsw",
14781 .@"sub nuw",
14782 .@"sub nuw nsw",
14783 .@"shl nsw",
14784 .@"shl nuw",
14785 .@"shl nuw nsw",
14786 => |kind| {
14787 const extra = func.extraData(Function.Instruction.Binary, datas[instr_index]);
14788 try function_block.writeAbbrev(FunctionBlock.BinaryNoWrap{
14789 .opcode = kind.toBinaryOpcode(),
14790 .lhs = adapter.getOffsetValueIndex(extra.lhs),
14791 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14792 .flags = switch (kind) {
14793 .@"add nsw",
14794 .@"mul nsw",
14795 .@"sub nsw",
14796 .@"shl nsw",
14797 => .{ .no_unsigned_wrap = false, .no_signed_wrap = true },
14798 .@"add nuw",
14799 .@"mul nuw",
14800 .@"sub nuw",
14801 .@"shl nuw",
14802 => .{ .no_unsigned_wrap = true, .no_signed_wrap = false },
14803 .@"add nuw nsw",
14804 .@"mul nuw nsw",
14805 .@"sub nuw nsw",
14806 .@"shl nuw nsw",
14807 => .{ .no_unsigned_wrap = true, .no_signed_wrap = true },
14808 else => unreachable,
14809 },
14810 });
14811 },
14778 .@"fadd fast",14812 .@"fadd fast",
14779 .@"fdiv fast",14813 .@"fdiv fast",
14780 .@"fmul fast",14814 .@"fmul fast",
src/codegen/llvm/ir.zig+36
...@@ -1102,6 +1102,8 @@ pub const FunctionBlock = struct {...@@ -1102,6 +1102,8 @@ pub const FunctionBlock = struct {
1102 FNeg,1102 FNeg,
1103 FNegFast,1103 FNegFast,
1104 Binary,1104 Binary,
1105 BinaryNoWrap,
1106 BinaryExact,
1105 BinaryFast,1107 BinaryFast,
1106 Cmp,1108 Cmp,
1107 CmpFast,1109 CmpFast,
...@@ -1232,6 +1234,40 @@ pub const FunctionBlock = struct {...@@ -1232,6 +1234,40 @@ pub const FunctionBlock = struct {
1232 opcode: BinaryOpcode,1234 opcode: BinaryOpcode,
1233 };1235 };
12341236
1237 pub const BinaryNoWrap = struct {
1238 const BinaryOpcode = Builder.BinaryOpcode;
1239 pub const ops = [_]AbbrevOp{
1240 .{ .literal = 2 },
1241 ValueAbbrev,
1242 ValueAbbrev,
1243 .{ .fixed = @bitSizeOf(BinaryOpcode) },
1244 .{ .fixed = 2 },
1245 };
1246
1247 lhs: u32,
1248 rhs: u32,
1249 opcode: BinaryOpcode,
1250 flags: packed struct(u2) {
1251 no_unsigned_wrap: bool,
1252 no_signed_wrap: bool,
1253 },
1254 };
1255
1256 pub const BinaryExact = struct {
1257 const BinaryOpcode = Builder.BinaryOpcode;
1258 pub const ops = [_]AbbrevOp{
1259 .{ .literal = 2 },
1260 ValueAbbrev,
1261 ValueAbbrev,
1262 .{ .fixed = @bitSizeOf(BinaryOpcode) },
1263 .{ .literal = 1 },
1264 };
1265
1266 lhs: u32,
1267 rhs: u32,
1268 opcode: BinaryOpcode,
1269 };
1270
1235 pub const BinaryFast = struct {1271 pub const BinaryFast = struct {
1236 const BinaryOpcode = Builder.BinaryOpcode;1272 const BinaryOpcode = Builder.BinaryOpcode;
1237 pub const ops = [_]AbbrevOp{1273 pub const ops = [_]AbbrevOp{