authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-26 17:09:04-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-26 17:09:04-07:00
logafbcad9939169f0b9b9b8ecb287718023c58b428
treef26a4b4e953c5db2de39839e05b6d9bbbf401368
parentf618398b24acdc3317e6fd81f486d49176ffcef9
parent3df2f356eba9b0882ee3fa09704aae7dc173f3d4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15452 from mlugg/zig-cbe-opt

CBE: minor optimizations to output source

2 files changed, 38 insertions(+), 4 deletions(-)

src/Sema.zig+13
......@@ -5888,6 +5888,19 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
58885888 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
58895889
58905890 const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;
5891
5892 if (block.instructions.items.len != 0) {
5893 const idx = block.instructions.items[block.instructions.items.len - 1];
5894 if (sema.air_instructions.items(.tag)[idx] == .dbg_stmt) {
5895 // The previous dbg_stmt didn't correspond to any actual code, so replace it.
5896 sema.air_instructions.items(.data)[idx].dbg_stmt = .{
5897 .line = inst_data.line,
5898 .column = inst_data.column,
5899 };
5900 return;
5901 }
5902 }
5903
58915904 _ = try block.addInst(.{
58925905 .tag = .dbg_stmt,
58935906 .data = .{ .dbg_stmt = .{
src/codegen/c.zig+25-4
......@@ -4296,9 +4296,13 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue {
42964296 }
42974297
42984298 try f.object.indent_writer.insertNewline();
4299 // label might be unused, add a dummy goto
4300 // label must be followed by an expression, add an empty one.
4301 try writer.print("goto zig_block_{d};\nzig_block_{d}: (void)0;\n", .{ block_id, block_id });
4299
4300 // noreturn blocks have no `br` instructions reaching them, so we don't want a label
4301 if (!f.air.typeOfIndex(inst).isNoReturn()) {
4302 // label must be followed by an expression, include an empty one.
4303 try writer.print("zig_block_{d}:;\n", .{block_id});
4304 }
4305
43024306 return result;
43034307}
43044308
......@@ -4350,7 +4354,7 @@ fn lowerTry(
43504354 else
43514355 try f.writeCValueMember(writer, err_union, .{ .identifier = "error" });
43524356 }
4353 try writer.writeByte(')');
4357 try writer.writeAll(") ");
43544358
43554359 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
43564360 try f.object.indent_writer.insertNewline();
......@@ -4422,7 +4426,11 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
44224426
44234427 const local = try f.allocLocal(inst, dest_ty);
44244428
4429 // If the assignment looks like 'x = x', we don't need it
4430 const can_elide = operand == .local and operand.local == local.new_local;
4431
44254432 if (operand_ty.isAbiInt() and dest_ty.isAbiInt()) {
4433 if (can_elide) return local;
44264434 const src_info = dest_ty.intInfo(target);
44274435 const dest_info = operand_ty.intInfo(target);
44284436 if (src_info.signedness == dest_info.signedness and
......@@ -4437,6 +4445,7 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
44374445 }
44384446
44394447 if (dest_ty.isPtrAtRuntime() and operand_ty.isPtrAtRuntime()) {
4448 if (can_elide) return local;
44404449 try f.writeCValue(writer, local, .Other);
44414450 try writer.writeAll(" = (");
44424451 try f.renderType(writer, dest_ty);
......@@ -5468,6 +5477,12 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
54685477 const error_ty = error_union_ty.errorUnionSet();
54695478 const payload_ty = error_union_ty.errorUnionPayload();
54705479 const local = try f.allocLocal(inst, inst_ty);
5480
5481 if (!payload_ty.hasRuntimeBits() and operand == .local and operand.local == local.new_local) {
5482 // The store will be 'x = x'; elide it.
5483 return local;
5484 }
5485
54715486 const writer = f.object.writer();
54725487 try f.writeCValue(writer, local, .Other);
54735488 try writer.writeAll(" = ");
......@@ -5565,6 +5580,12 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
55655580
55665581 const writer = f.object.writer();
55675582 const local = try f.allocLocal(inst, inst_ty);
5583
5584 if (repr_is_err and err == .local and err.local == local.new_local) {
5585 // The store will be 'x = x'; elide it.
5586 return local;
5587 }
5588
55685589 if (!repr_is_err) {
55695590 const a = try Assignment.start(f, writer, payload_ty);
55705591 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });