Skip to content

Commit

Permalink
cute test working
Browse files Browse the repository at this point in the history
define i64 @main() {entry:  %a3 = alloca i64  %a4 = load i64, i64* %a3  %c8 = alloca i64  %c9 = load i64, i64* %c8  %b14 = alloca i64  %b15 = load i64, i64* %b14  br label %body0body0:  br label %if.cond1if.cond1:  %imm_store5 = add i64 1, 0  %_6 = icmp eq i64 %a4, %imm_store5  br i1 %_6, label %then.body2, label %if.exit4then.body2:  br label %then.exit3then.exit3:  br label %if.exit4if.exit4:  %b0 = phi i64 [ %b15, %if.cond1 ], [ %c9, %then.exit3 ]  br label %exitexit:  %return_reg12 = phi i64 [ %a4, %if.exit4 ]  ret i64 %return_reg12}
  • Loading branch information
dleiferives committed May 22, 2024
1 parent 6c9555e commit 96571ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions .auto_run_cmd.file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zig test --main-pkg-path ./ ./src/ir/phi.zig
17 changes: 16 additions & 1 deletion src/ir/ir_phi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub const Function = struct {
regs: LookupTable(Register.ID, Register, Register.getID),
cfg: CfgFunction,
exitBBID: BasicBlock.ID,
retRegUsed: bool = false,

/// a list of the instructions that are within the fuction
/// the basic blocks have a list of instructions that they use,
Expand Down Expand Up @@ -415,7 +416,16 @@ pub const Function = struct {

pub const NotFoundError = error{ OutOfMemory, UnboundIdentifier, AllocFailed };

pub fn getNamedRef(self: *Function, ir: *const IR, name: StrID, bb: IR.BasicBlock.ID) NotFoundError!Ref {
pub fn getNamedRef(self: *Function, ir: *const IR, name: StrID, bb: BasicBlock.ID) NotFoundError!Ref {
const namedRef = try self.getNamedRefInner(ir, name, bb);
if (self.returnReg == null) return namedRef;
if (namedRef.i == self.returnReg.?) {
self.retRegUsed = true;
}
return namedRef;
}

pub fn getNamedRefInner(self: *Function, ir: *const IR, name: StrID, bb: IR.BasicBlock.ID) NotFoundError!Ref {
if (name != IR.InternPool.NULL) {
std.debug.print("getting ref for {s}\n", .{ir.getIdent(name)});
} else {
Expand Down Expand Up @@ -2112,6 +2122,11 @@ pub fn OrderedList(comptime T: type) type {
pub fn append(self: *Self, item: T) !void {
_ = try self.add(item);
}

pub fn orderedRemove(self: *Self, idx: u32) T {
self.len -= 1;
return self.list.orderedRemove(idx);
}
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/ir/phi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ pub fn gen_function(
_ = try fun.addInst(fun.exitBBID, Inst.retVoid(), .void);
}

if (fun.retRegUsed == false and fun.returnType != .void) {
_ = fun.bbs.get(IR.Function.entryBBID).insts.orderedRemove(0);
}

try fun.addCtrlFlowInst(entryBB, Inst.jmp(IR.Ref.label(fun.cfgToBBs.get(0).?)));
return fun;
}
Expand Down

0 comments on commit 96571ee

Please sign in to comment.