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

CBE: minor optimizations to output source
This commit is contained in:
Andrew Kelley 2023-04-26 17:09:04 -07:00 committed by GitHub
commit afbcad9939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View File

@ -5888,6 +5888,19 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;
if (block.instructions.items.len != 0) {
const idx = block.instructions.items[block.instructions.items.len - 1];
if (sema.air_instructions.items(.tag)[idx] == .dbg_stmt) {
// The previous dbg_stmt didn't correspond to any actual code, so replace it.
sema.air_instructions.items(.data)[idx].dbg_stmt = .{
.line = inst_data.line,
.column = inst_data.column,
};
return;
}
}
_ = try block.addInst(.{
.tag = .dbg_stmt,
.data = .{ .dbg_stmt = .{

View File

@ -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" });