authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-01 18:10:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-04 15:57:39-07:00
log46f4a97d051e630dfae75e3856c81ffd1c7811e3
treea81d3a96c2c31a6ddd6f3cefbd9f7415d3f051ff
parentbdb6fb57639a4508dda523301c5eaf31e8d89edf

Revert "cbe: write more instructions inline"

This reverts commit f8b779c114a5fcb82f08168912f2300d7027a2fd.

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

src/codegen/c.zig+77-56
......@@ -395,27 +395,6 @@ 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),
419398 else => unreachable,
420399 // zig fmt: on
421400 }
......@@ -443,7 +422,6 @@ pub const Function = struct {
443422 try w.writeByte('.');
444423 return f.writeCValue(w, member, .Other);
445424 },
446 .inline_index => unreachable, // Use resolveInstNoInline
447425 else => return f.object.dg.writeCValueMember(w, c_value, member),
448426 }
449427 }
......@@ -2729,15 +2707,15 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
27292707 .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst),
27302708 .wrap_optional => try airWrapOptional(f, inst),
27312709
2732 .is_err => CValue{ .inline_index = inst },
2733 .is_non_err => CValue{ .inline_index = inst },
2734 .is_err_ptr => CValue{ .inline_index = inst },
2735 .is_non_err_ptr => CValue{ .inline_index = inst },
2710 .is_err => try airIsErr(f, inst, false, "!="),
2711 .is_non_err => try airIsErr(f, inst, false, "=="),
2712 .is_err_ptr => try airIsErr(f, inst, true, "!="),
2713 .is_non_err_ptr => try airIsErr(f, inst, true, "=="),
27362714
2737 .is_null => CValue{ .inline_index = inst },
2738 .is_non_null => CValue{ .inline_index = inst },
2739 .is_null_ptr => CValue{ .inline_index = inst },
2740 .is_non_null_ptr => CValue{ .inline_index = inst },
2715 .is_null => try airIsNull(f, inst, "==", false),
2716 .is_non_null => try airIsNull(f, inst, "!=", false),
2717 .is_null_ptr => try airIsNull(f, inst, "==", true),
2718 .is_non_null_ptr => try airIsNull(f, inst, "!=", true),
27412719
27422720 .alloc => try airAlloc(f, inst),
27432721 .ret_ptr => try airRetPtr(f, inst),
......@@ -2765,14 +2743,14 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
27652743 .memset => try airMemset(f, inst),
27662744 .memcpy => try airMemcpy(f, inst),
27672745 .set_union_tag => try airSetUnionTag(f, inst),
2768 .get_union_tag => CValue{ .inline_index = inst },
2769 .clz => CValue{ .inline_index = inst },
2770 .ctz => CValue{ .inline_index = inst },
2771 .popcount => CValue{ .inline_index = inst },
2772 .byte_swap => CValue{ .inline_index = inst },
2773 .bit_reverse => CValue{ .inline_index = inst },
2774 .tag_name => CValue{ .inline_index = inst },
2775 .error_name => CValue{ .inline_index = inst },
2746 .get_union_tag => try airGetUnionTag(f, inst),
2747 .clz => try airUnBuiltinCall(f, inst, "clz", .Bits),
2748 .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits),
2749 .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits),
2750 .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits),
2751 .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits),
2752 .tag_name => try airTagName(f, inst),
2753 .error_name => try airErrorName(f, inst),
27762754 .splat => try airSplat(f, inst),
27772755 .select => try airSelect(f, inst),
27782756 .shuffle => try airShuffle(f, inst),
......@@ -2808,7 +2786,7 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
28082786 .fpext,
28092787 => try airFloatCast(f, inst),
28102788
2811 .ptrtoint => CValue{ .inline_index = inst },
2789 .ptrtoint => try airPtrToInt(f, inst),
28122790
28132791 .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)),
28142792 .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)),
......@@ -2889,7 +2867,7 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [
28892867
28902868 const inst_ty = f.air.typeOfIndex(inst);
28912869 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
2892 const operand = try f.resolveInstNoInline(ty_op.operand);
2870 const operand = try f.resolveInst(ty_op.operand);
28932871 const writer = f.object.writer();
28942872 const local = try f.allocLocal(inst_ty, .Const);
28952873 try writer.writeAll(" = ");
......@@ -4425,11 +4403,16 @@ fn airIsNull(
44254403 inst: Air.Inst.Index,
44264404 operator: []const u8,
44274405 is_ptr: bool,
4428) !void {
4406) !CValue {
4407 if (f.liveness.isUnused(inst))
4408 return CValue.none;
4409
44294410 const un_op = f.air.instructions.items(.data)[inst].un_op;
44304411 const writer = f.object.writer();
44314412 const operand = try f.resolveInst(un_op);
44324413
4414 const local = try f.allocLocal(Type.initTag(.bool), .Const);
4415 try writer.writeAll(" = ");
44334416 try if (is_ptr) f.writeCValueDeref(writer, operand) else f.writeCValue(writer, operand, .Other);
44344417
44354418 const operand_ty = f.air.typeOf(un_op);
......@@ -4457,6 +4440,8 @@ fn airIsNull(
44574440 try writer.writeAll(operator);
44584441 try writer.writeByte(' ');
44594442 try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other);
4443 try writer.writeAll(";\n");
4444 return local;
44604445}
44614446
44624447fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -5038,15 +5023,21 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
50385023 return local;
50395024}
50405025
5041fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !void {
5026fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue {
5027 if (f.liveness.isUnused(inst))
5028 return CValue.none;
5029
50425030 const un_op = f.air.instructions.items(.data)[inst].un_op;
50435031 const writer = f.object.writer();
50445032 const operand = try f.resolveInst(un_op);
50455033 const operand_ty = f.air.typeOf(un_op);
5034 const local = try f.allocLocal(Type.initTag(.bool), .Const);
50465035 const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty;
50475036 const payload_ty = err_union_ty.errorUnionPayload();
50485037 const error_ty = err_union_ty.errorUnionSet();
50495038
5039 try writer.writeAll(" = ");
5040
50505041 if (!error_ty.errorSetIsEmpty())
50515042 if (payload_ty.hasRuntimeBits())
50525043 if (is_ptr)
......@@ -5061,6 +5052,8 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
50615052 try writer.writeAll(operator);
50625053 try writer.writeByte(' ');
50635054 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
5055 try writer.writeAll(";\n");
5056 return local;
50645057}
50655058
50665059fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -5134,16 +5127,21 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {
51345127 return local;
51355128}
51365129
5137fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !void {
5130fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
5131 if (f.liveness.isUnused(inst)) return CValue.none;
5132
51385133 const inst_ty = f.air.typeOfIndex(inst);
5134 const local = try f.allocLocal(inst_ty, .Const);
51395135 const un_op = f.air.instructions.items(.data)[inst].un_op;
51405136 const writer = f.object.writer();
51415137 const operand = try f.resolveInst(un_op);
51425138
5143 try writer.writeAll("(");
5139 try writer.writeAll(" = (");
51445140 try f.renderTypecast(writer, inst_ty);
51455141 try writer.writeByte(')');
51465142 try f.writeCValue(writer, operand, .Other);
5143 try writer.writeAll(";\n");
5144 return local;
51475145}
51485146
51495147fn airUnBuiltinCall(
......@@ -5151,19 +5149,24 @@ fn airUnBuiltinCall(
51515149 inst: Air.Inst.Index,
51525150 operation: []const u8,
51535151 info: BuiltinInfo,
5154) !void {
5152) !CValue {
5153 if (f.liveness.isUnused(inst)) return CValue.none;
5154
5155 const inst_ty = f.air.typeOfIndex(inst);
51555156 const operand = f.air.instructions.items(.data)[inst].ty_op.operand;
51565157 const operand_ty = f.air.typeOf(operand);
51575158
5159 const local = try f.allocLocal(inst_ty, .Const);
51585160 const writer = f.object.writer();
5159 try writer.writeAll("zig_");
5161 try writer.writeAll(" = zig_");
51605162 try writer.writeAll(operation);
51615163 try writer.writeByte('_');
51625164 try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty);
51635165 try writer.writeByte('(');
51645166 try f.writeCValue(writer, try f.resolveInst(operand), .FunctionArgument);
51655167 try f.object.dg.renderBuiltinInfo(writer, operand_ty, info);
5166 try writer.writeAll(")");
5168 try writer.writeAll(");\n");
5169 return local;
51675170}
51685171
51695172fn airBinBuiltinCall(
......@@ -5436,7 +5439,12 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
54365439 return CValue.none;
54375440}
54385441
5439fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !void {
5442fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
5443 if (f.liveness.isUnused(inst))
5444 return CValue.none;
5445
5446 const inst_ty = f.air.typeOfIndex(inst);
5447 const local = try f.allocLocal(inst_ty, .Const);
54405448 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
54415449 const un_ty = f.air.typeOf(ty_op.operand);
54425450 const writer = f.object.writer();
......@@ -5444,31 +5452,44 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !void {
54445452
54455453 const target = f.object.dg.module.getTarget();
54465454 const layout = un_ty.unionGetLayout(target);
5447 assert(layout.tag_size != 0);
5455 if (layout.tag_size == 0) return CValue.none;
54485456
5457 try writer.writeAll(" = ");
54495458 try f.writeCValue(writer, operand, .Other);
5450 try writer.writeAll(".tag");
5459 try writer.writeAll(".tag;\n");
5460 return local;
54515461}
54525462
5453fn airTagName(f: *Function, inst: Air.Inst.Index) !void {
5463fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
5464 if (f.liveness.isUnused(inst)) return CValue.none;
5465
54545466 const un_op = f.air.instructions.items(.data)[inst].un_op;
5467 const inst_ty = f.air.typeOfIndex(inst);
54555468 const enum_ty = f.air.typeOf(un_op);
54565469 const operand = try f.resolveInst(un_op);
54575470
54585471 const writer = f.object.writer();
5459 try writer.print("{s}(", .{try f.object.dg.getTagNameFn(enum_ty)});
5472 const local = try f.allocLocal(inst_ty, .Const);
5473 try writer.print(" = {s}(", .{try f.object.dg.getTagNameFn(enum_ty)});
54605474 try f.writeCValue(writer, operand, .Other);
5461 try writer.writeAll(")");
5475 try writer.writeAll(");\n");
5476
5477 return local;
54625478}
54635479
5464fn airErrorName(f: *Function, inst: Air.Inst.Index) !void {
5480fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {
5481 if (f.liveness.isUnused(inst)) return CValue.none;
5482
54655483 const un_op = f.air.instructions.items(.data)[inst].un_op;
54665484 const writer = f.object.writer();
5485 const inst_ty = f.air.typeOfIndex(inst);
54675486 const operand = try f.resolveInst(un_op);
5487 const local = try f.allocLocal(inst_ty, .Const);
54685488
5469 try writer.writeAll("zig_errorName[");
5489 try writer.writeAll(" = zig_errorName[");
54705490 try f.writeCValue(writer, operand, .Other);
5471 try writer.writeAll("]");
5491 try writer.writeAll("];\n");
5492 return local;
54725493}
54735494
54745495fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {