authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-17 10:42:44-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-17 10:42:44-04:00
log1a7cf4cbce1e157067f27c289c8365c8612de395
tree8e3ae8c97a85835222cf9a410fd4cb7655541345
parent3cbf59b4c1a431ebd1ccb0cf46fa37e367b8106e

port 69e3b4e to self-hosted compiler

See #1249

1 files changed, 15 insertions(+), 2 deletions(-)

src-self-hosted/ir.zig+15-2
......@@ -53,6 +53,7 @@ pub const Instruction = struct {
5353 val: IrVal,
5454 ref_count: usize,
5555 span: Span,
56 owner_bb: *BasicBlock,
5657
5758 /// true if this instruction was generated by zig and not from user code
5859 is_generated: bool,
......@@ -132,6 +133,13 @@ pub const Instruction = struct {
132133 }
133134 }
134135
136 fn ref(base: *Instruction, builder: *Builder) void {
137 base.ref_count += 1;
138 if (base.owner_bb != builder.current_basic_block and !base.isCompTime()) {
139 base.owner_bb.ref();
140 }
141 }
142
135143 fn getAsParam(param: *Instruction) !*Instruction {
136144 const child = param.child orelse return error.SemanticAnalysisFailed;
137145 switch (child.val) {
......@@ -161,6 +169,10 @@ pub const Instruction = struct {
161169 }
162170 }
163171
172 pub fn isCompTime(base: *const Instruction) bool {
173 return base.val == IrVal.KnownValue;
174 }
175
164176 pub fn linkToParent(self: *Instruction, parent: *Instruction) void {
165177 assert(self.parent == null);
166178 assert(parent.child == null);
......@@ -816,6 +828,7 @@ pub const Builder = struct {
816828 .child = null,
817829 .parent = null,
818830 .llvm_value = undefined,
831 .owner_bb = self.current_basic_block,
819832 },
820833 .params = params,
821834 });
......@@ -825,8 +838,8 @@ pub const Builder = struct {
825838 inline while (i < @memberCount(I.Params)) : (i += 1) {
826839 const FieldType = comptime @typeOf(@field(I.Params(undefined), @memberName(I.Params, i)));
827840 switch (FieldType) {
828 *Instruction => @field(inst.params, @memberName(I.Params, i)).ref_count += 1,
829 ?*Instruction => if (@field(inst.params, @memberName(I.Params, i))) |other| other.ref_count += 1,
841 *Instruction => @field(inst.params, @memberName(I.Params, i)).ref(self),
842 ?*Instruction => if (@field(inst.params, @memberName(I.Params, i))) |other| other.ref(self),
830843 else => {},
831844 }
832845 }