authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-25 04:49:47+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-26 01:46:09+01:00
log2e23ddbe7ba48857fbdcf014da3251f1decca00d
treef2a22d0c2b063fd73a98f1a794092f4c3cf97d5a
parent295b8ca467da36cd1066395e7f50b6245f456573
signaturelock-open Commit is signed but in an unrecognized format.

CBE: minor optimizations to output source


1 files changed, 25 insertions(+), 4 deletions(-)

src/codegen/c.zig+25-4
......@@ -4291,9 +4291,13 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue {
42914291 }
42924292
42934293 try f.object.indent_writer.insertNewline();
4294 // label might be unused, add a dummy goto
4295 // label must be followed by an expression, add an empty one.
4296 try writer.print("goto zig_block_{d};\nzig_block_{d}: (void)0;\n", .{ block_id, block_id });
4294
4295 // noreturn blocks have no `br` instructions reaching them, so we don't want a label
4296 if (!f.air.typeOfIndex(inst).isNoReturn()) {
4297 // label must be followed by an expression, include an empty one.
4298 try writer.print("zig_block_{d}:;\n", .{block_id});
4299 }
4300
42974301 return result;
42984302}
42994303
......@@ -4345,7 +4349,7 @@ fn lowerTry(
43454349 else
43464350 try f.writeCValueMember(writer, err_union, .{ .identifier = "error" });
43474351 }
4348 try writer.writeByte(')');
4352 try writer.writeAll(") ");
43494353
43504354 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
43514355 try f.object.indent_writer.insertNewline();
......@@ -4417,7 +4421,11 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
44174421
44184422 const local = try f.allocLocal(inst, dest_ty);
44194423
4424 // If the assignment looks like 'x = x', we don't need it
4425 const can_elide = operand == .local and operand.local == local.new_local;
4426
44204427 if (operand_ty.isAbiInt() and dest_ty.isAbiInt()) {
4428 if (can_elide) return local;
44214429 const src_info = dest_ty.intInfo(target);
44224430 const dest_info = operand_ty.intInfo(target);
44234431 if (src_info.signedness == dest_info.signedness and
......@@ -4432,6 +4440,7 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
44324440 }
44334441
44344442 if (dest_ty.isPtrAtRuntime() and operand_ty.isPtrAtRuntime()) {
4443 if (can_elide) return local;
44354444 try f.writeCValue(writer, local, .Other);
44364445 try writer.writeAll(" = (");
44374446 try f.renderType(writer, dest_ty);
......@@ -5463,6 +5472,12 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
54635472 const error_ty = error_union_ty.errorUnionSet();
54645473 const payload_ty = error_union_ty.errorUnionPayload();
54655474 const local = try f.allocLocal(inst, inst_ty);
5475
5476 if (!payload_ty.hasRuntimeBits() and operand == .local and operand.local == local.new_local) {
5477 // The store will be 'x = x'; elide it.
5478 return local;
5479 }
5480
54665481 const writer = f.object.writer();
54675482 try f.writeCValue(writer, local, .Other);
54685483 try writer.writeAll(" = ");
......@@ -5560,6 +5575,12 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
55605575
55615576 const writer = f.object.writer();
55625577 const local = try f.allocLocal(inst, inst_ty);
5578
5579 if (repr_is_err and err == .local and err.local == local.new_local) {
5580 // The store will be 'x = x'; elide it.
5581 return local;
5582 }
5583
55635584 if (!repr_is_err) {
55645585 const a = try Assignment.start(f, writer, payload_ty);
55655586 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });