authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-30 17:09:40+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-30 17:13:56+02:00
logf8b779c114a5fcb82f08168912f2300d7027a2fd
tree703dcdf6d2e1dd8c40f2f044264142f48f362065
parent11ec7109c3d882615c2d220a813100c7f193cc0a

cbe: write more instructions inline


1 files changed, 56 insertions(+), 77 deletions(-)

src/codegen/c.zig+56-77
......@@ -395,6 +395,27 @@ pub const Function = struct {
395395 .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits),
396396 .shl_exact => try airBinOp(f, inst, "<<", "shl", .None),
397397 .not => try airNot (f, inst),
398
399 .is_err => try airIsErr(f, inst, false, "!="),
400 .is_non_err => try airIsErr(f, inst, false, "=="),
401 .is_err_ptr => try airIsErr(f, inst, true, "!="),
402 .is_non_err_ptr => try airIsErr(f, inst, true, "=="),
403
404 .is_null => try airIsNull(f, inst, "==", false),
405 .is_non_null => try airIsNull(f, inst, "!=", false),
406 .is_null_ptr => try airIsNull(f, inst, "==", true),
407 .is_non_null_ptr => try airIsNull(f, inst, "!=", true),
408
409 .get_union_tag => try airGetUnionTag(f, inst),
410 .clz => try airUnBuiltinCall(f, inst, "clz", .Bits),
411 .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits),
412 .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits),
413 .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits),
414 .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits),
415 .tag_name => try airTagName(f, inst),
416 .error_name => try airErrorName(f, inst),
417
418 .ptrtoint => try airPtrToInt(f, inst),
398419 else => unreachable,
399420 // zig fmt: on
400421 }
......@@ -422,6 +443,7 @@ pub const Function = struct {
422443 try w.writeByte('.');
423444 return f.writeCValue(w, member, .Other);
424445 },
446 .inline_index => unreachable, // Use resolveInstNoInline
425447 else => return f.object.dg.writeCValueMember(w, c_value, member),
426448 }
427449 }
......@@ -2591,15 +2613,15 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
25912613 .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst),
25922614 .wrap_optional => try airWrapOptional(f, inst),
25932615
2594 .is_err => try airIsErr(f, inst, false, "!="),
2595 .is_non_err => try airIsErr(f, inst, false, "=="),
2596 .is_err_ptr => try airIsErr(f, inst, true, "!="),
2597 .is_non_err_ptr => try airIsErr(f, inst, true, "=="),
2616 .is_err => CValue{ .inline_index = inst },
2617 .is_non_err => CValue{ .inline_index = inst },
2618 .is_err_ptr => CValue{ .inline_index = inst },
2619 .is_non_err_ptr => CValue{ .inline_index = inst },
25982620
2599 .is_null => try airIsNull(f, inst, "==", false),
2600 .is_non_null => try airIsNull(f, inst, "!=", false),
2601 .is_null_ptr => try airIsNull(f, inst, "==", true),
2602 .is_non_null_ptr => try airIsNull(f, inst, "!=", true),
2621 .is_null => CValue{ .inline_index = inst },
2622 .is_non_null => CValue{ .inline_index = inst },
2623 .is_null_ptr => CValue{ .inline_index = inst },
2624 .is_non_null_ptr => CValue{ .inline_index = inst },
26032625
26042626 .alloc => try airAlloc(f, inst),
26052627 .ret_ptr => try airRetPtr(f, inst),
......@@ -2627,14 +2649,14 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
26272649 .memset => try airMemset(f, inst),
26282650 .memcpy => try airMemcpy(f, inst),
26292651 .set_union_tag => try airSetUnionTag(f, inst),
2630 .get_union_tag => try airGetUnionTag(f, inst),
2631 .clz => try airUnBuiltinCall(f, inst, "clz", .Bits),
2632 .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits),
2633 .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits),
2634 .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits),
2635 .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits),
2636 .tag_name => try airTagName(f, inst),
2637 .error_name => try airErrorName(f, inst),
2652 .get_union_tag => CValue{ .inline_index = inst },
2653 .clz => CValue{ .inline_index = inst },
2654 .ctz => CValue{ .inline_index = inst },
2655 .popcount => CValue{ .inline_index = inst },
2656 .byte_swap => CValue{ .inline_index = inst },
2657 .bit_reverse => CValue{ .inline_index = inst },
2658 .tag_name => CValue{ .inline_index = inst },
2659 .error_name => CValue{ .inline_index = inst },
26382660 .splat => try airSplat(f, inst),
26392661 .select => try airSelect(f, inst),
26402662 .shuffle => try airShuffle(f, inst),
......@@ -2670,7 +2692,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
26702692 .fpext,
26712693 => try airFloatCast(f, inst),
26722694
2673 .ptrtoint => try airPtrToInt(f, inst),
2695 .ptrtoint => CValue{ .inline_index = inst },
26742696
26752697 .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)),
26762698 .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)),
......@@ -2754,7 +2776,7 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [
27542776
27552777 const inst_ty = f.air.typeOfIndex(inst);
27562778 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
2757 const operand = try f.resolveInst(ty_op.operand);
2779 const operand = try f.resolveInstNoInline(ty_op.operand);
27582780 const writer = f.object.writer();
27592781 const local = try f.allocLocal(inst_ty, .Const);
27602782 try writer.writeAll(" = ");
......@@ -4234,16 +4256,11 @@ fn airIsNull(
42344256 inst: Air.Inst.Index,
42354257 operator: []const u8,
42364258 is_ptr: bool,
4237) !CValue {
4238 if (f.liveness.isUnused(inst))
4239 return CValue.none;
4240
4259) !void {
42414260 const un_op = f.air.instructions.items(.data)[inst].un_op;
42424261 const writer = f.object.writer();
42434262 const operand = try f.resolveInst(un_op);
42444263
4245 const local = try f.allocLocal(Type.initTag(.bool), .Const);
4246 try writer.writeAll(" = ");
42474264 try if (is_ptr) f.writeCValueDeref(writer, operand) else f.writeCValue(writer, operand, .Other);
42484265
42494266 const operand_ty = f.air.typeOf(un_op);
......@@ -4271,8 +4288,6 @@ fn airIsNull(
42714288 try writer.writeAll(operator);
42724289 try writer.writeByte(' ');
42734290 try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other);
4274 try writer.writeAll(";\n");
4275 return local;
42764291}
42774292
42784293fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -4842,21 +4857,15 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
48424857 return local;
48434858}
48444859
4845fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue {
4846 if (f.liveness.isUnused(inst))
4847 return CValue.none;
4848
4860fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !void {
48494861 const un_op = f.air.instructions.items(.data)[inst].un_op;
48504862 const writer = f.object.writer();
48514863 const operand = try f.resolveInst(un_op);
48524864 const operand_ty = f.air.typeOf(un_op);
4853 const local = try f.allocLocal(Type.initTag(.bool), .Const);
48544865 const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty;
48554866 const payload_ty = err_union_ty.errorUnionPayload();
48564867 const error_ty = err_union_ty.errorUnionSet();
48574868
4858 try writer.writeAll(" = ");
4859
48604869 if (!error_ty.errorSetIsEmpty())
48614870 if (payload_ty.hasRuntimeBits())
48624871 if (is_ptr)
......@@ -4871,8 +4880,6 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
48714880 try writer.writeAll(operator);
48724881 try writer.writeByte(' ');
48734882 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
4874 try writer.writeAll(";\n");
4875 return local;
48764883}
48774884
48784885fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -4946,21 +4953,16 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {
49464953 return local;
49474954}
49484955
4949fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
4950 if (f.liveness.isUnused(inst)) return CValue.none;
4951
4956fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !void {
49524957 const inst_ty = f.air.typeOfIndex(inst);
4953 const local = try f.allocLocal(inst_ty, .Const);
49544958 const un_op = f.air.instructions.items(.data)[inst].un_op;
49554959 const writer = f.object.writer();
49564960 const operand = try f.resolveInst(un_op);
49574961
4958 try writer.writeAll(" = (");
4962 try writer.writeAll("(");
49594963 try f.renderTypecast(writer, inst_ty);
49604964 try writer.writeByte(')');
49614965 try f.writeCValue(writer, operand, .Other);
4962 try writer.writeAll(";\n");
4963 return local;
49644966}
49654967
49664968fn airUnBuiltinCall(
......@@ -4968,24 +4970,19 @@ fn airUnBuiltinCall(
49684970 inst: Air.Inst.Index,
49694971 operation: []const u8,
49704972 info: BuiltinInfo,
4971) !CValue {
4972 if (f.liveness.isUnused(inst)) return CValue.none;
4973
4974 const inst_ty = f.air.typeOfIndex(inst);
4973) !void {
49754974 const operand = f.air.instructions.items(.data)[inst].ty_op.operand;
49764975 const operand_ty = f.air.typeOf(operand);
49774976
4978 const local = try f.allocLocal(inst_ty, .Const);
49794977 const writer = f.object.writer();
4980 try writer.writeAll(" = zig_");
4978 try writer.writeAll("zig_");
49814979 try writer.writeAll(operation);
49824980 try writer.writeByte('_');
49834981 try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty);
49844982 try writer.writeByte('(');
49854983 try f.writeCValue(writer, try f.resolveInst(operand), .FunctionArgument);
49864984 try f.object.dg.renderBuiltinInfo(writer, operand_ty, info);
4987 try writer.writeAll(");\n");
4988 return local;
4985 try writer.writeAll(")");
49894986}
49904987
49914988fn airBinBuiltinCall(
......@@ -5258,12 +5255,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
52585255 return CValue.none;
52595256}
52605257
5261fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
5262 if (f.liveness.isUnused(inst))
5263 return CValue.none;
5264
5265 const inst_ty = f.air.typeOfIndex(inst);
5266 const local = try f.allocLocal(inst_ty, .Const);
5258fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !void {
52675259 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
52685260 const un_ty = f.air.typeOf(ty_op.operand);
52695261 const writer = f.object.writer();
......@@ -5271,44 +5263,31 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
52715263
52725264 const target = f.object.dg.module.getTarget();
52735265 const layout = un_ty.unionGetLayout(target);
5274 if (layout.tag_size == 0) return CValue.none;
5266 assert(layout.tag_size != 0);
52755267
5276 try writer.writeAll(" = ");
52775268 try f.writeCValue(writer, operand, .Other);
5278 try writer.writeAll(".tag;\n");
5279 return local;
5269 try writer.writeAll(".tag");
52805270}
52815271
5282fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
5283 if (f.liveness.isUnused(inst)) return CValue.none;
5284
5272fn airTagName(f: *Function, inst: Air.Inst.Index) !void {
52855273 const un_op = f.air.instructions.items(.data)[inst].un_op;
5286 const inst_ty = f.air.typeOfIndex(inst);
52875274 const enum_ty = f.air.typeOf(un_op);
52885275 const operand = try f.resolveInst(un_op);
52895276
52905277 const writer = f.object.writer();
5291 const local = try f.allocLocal(inst_ty, .Const);
5292 try writer.print(" = {s}(", .{try f.object.dg.getTagNameFn(enum_ty)});
5278 try writer.print("{s}(", .{try f.object.dg.getTagNameFn(enum_ty)});
52935279 try f.writeCValue(writer, operand, .Other);
5294 try writer.writeAll(");\n");
5295
5296 return local;
5280 try writer.writeAll(")");
52975281}
52985282
5299fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {
5300 if (f.liveness.isUnused(inst)) return CValue.none;
5301
5283fn airErrorName(f: *Function, inst: Air.Inst.Index) !void {
53025284 const un_op = f.air.instructions.items(.data)[inst].un_op;
53035285 const writer = f.object.writer();
5304 const inst_ty = f.air.typeOfIndex(inst);
53055286 const operand = try f.resolveInst(un_op);
5306 const local = try f.allocLocal(inst_ty, .Const);
53075287
5308 try writer.writeAll(" = zig_errorName[");
5288 try writer.writeAll("zig_errorName[");
53095289 try f.writeCValue(writer, operand, .Other);
5310 try writer.writeAll("];\n");
5311 return local;
5290 try writer.writeAll("]");
53125291}
53135292
53145293fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {