aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-04-26 17:09:04 -0700
committerGitHub <noreply@github.com>2023-04-26 17:09:04 -0700
commitafbcad9939169f0b9b9b8ecb287718023c58b428 (patch)
treef26a4b4e953c5db2de39839e05b6d9bbbf401368 /src/codegen/c.zig
parentf618398b24acdc3317e6fd81f486d49176ffcef9 (diff)
parent3df2f356eba9b0882ee3fa09704aae7dc173f3d4 (diff)
downloadzig-afbcad9939169f0b9b9b8ecb287718023c58b428.tar.gz
zig-afbcad9939169f0b9b9b8ecb287718023c58b428.zip
Merge pull request #15452 from mlugg/zig-cbe-opt
CBE: minor optimizations to output source
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 9dfba241a1..385094e495 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -4296,9 +4296,13 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue {
}
try f.object.indent_writer.insertNewline();
- // label might be unused, add a dummy goto
- // label must be followed by an expression, add an empty one.
- try writer.print("goto zig_block_{d};\nzig_block_{d}: (void)0;\n", .{ block_id, block_id });
+
+ // noreturn blocks have no `br` instructions reaching them, so we don't want a label
+ if (!f.air.typeOfIndex(inst).isNoReturn()) {
+ // label must be followed by an expression, include an empty one.
+ try writer.print("zig_block_{d}:;\n", .{block_id});
+ }
+
return result;
}
@@ -4350,7 +4354,7 @@ fn lowerTry(
else
try f.writeCValueMember(writer, err_union, .{ .identifier = "error" });
}
- try writer.writeByte(')');
+ try writer.writeAll(") ");
try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
try f.object.indent_writer.insertNewline();
@@ -4422,7 +4426,11 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
const local = try f.allocLocal(inst, dest_ty);
+ // If the assignment looks like 'x = x', we don't need it
+ const can_elide = operand == .local and operand.local == local.new_local;
+
if (operand_ty.isAbiInt() and dest_ty.isAbiInt()) {
+ if (can_elide) return local;
const src_info = dest_ty.intInfo(target);
const dest_info = operand_ty.intInfo(target);
if (src_info.signedness == dest_info.signedness and
@@ -4437,6 +4445,7 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
}
if (dest_ty.isPtrAtRuntime() and operand_ty.isPtrAtRuntime()) {
+ if (can_elide) return local;
try f.writeCValue(writer, local, .Other);
try writer.writeAll(" = (");
try f.renderType(writer, dest_ty);
@@ -5468,6 +5477,12 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
const error_ty = error_union_ty.errorUnionSet();
const payload_ty = error_union_ty.errorUnionPayload();
const local = try f.allocLocal(inst, inst_ty);
+
+ if (!payload_ty.hasRuntimeBits() and operand == .local and operand.local == local.new_local) {
+ // The store will be 'x = x'; elide it.
+ return local;
+ }
+
const writer = f.object.writer();
try f.writeCValue(writer, local, .Other);
try writer.writeAll(" = ");
@@ -5565,6 +5580,12 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
const writer = f.object.writer();
const local = try f.allocLocal(inst, inst_ty);
+
+ if (repr_is_err and err == .local and err.local == local.new_local) {
+ // The store will be 'x = x'; elide it.
+ return local;
+ }
+
if (!repr_is_err) {
const a = try Assignment.start(f, writer, payload_ty);
try f.writeCValueMember(writer, local, .{ .identifier = "payload" });