diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-07-17 12:41:39 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-07-20 12:19:16 -0700 |
| commit | b2733a36f8fa2379fd4e07f936a4ad22a4541c7c (patch) | |
| tree | cdcc97d9d8b7f1094b7dc0630e61e1f55c9c07fe /src/Sema.zig | |
| parent | 761f36ff93b5c551101d7f731a136c2d66093e76 (diff) | |
| download | zig-b2733a36f8fa2379fd4e07f936a4ad22a4541c7c.tar.gz zig-b2733a36f8fa2379fd4e07f936a4ad22a4541c7c.zip | |
Sema: memoize decl_val instructions when result is constant
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 79f1ed0614..d796ae2a5a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -45,6 +45,7 @@ branch_count: u32 = 0, /// contain a mapped source location. src: LazySrcLoc = .{ .token_offset = 0 }, next_arg_index: usize = 0, +decl_val_table: std.AutoHashMapUnmanaged(*Decl, Air.Inst.Ref) = .{}, const std = @import("std"); const mem = std.mem; @@ -77,6 +78,7 @@ pub fn deinit(sema: *Sema) void { sema.air_values.deinit(gpa); sema.air_variables.deinit(gpa); sema.inst_map.deinit(gpa); + sema.decl_val_table.deinit(gpa); sema.* = undefined; } @@ -7159,9 +7161,23 @@ fn coerceArrayPtrToMany( return sema.mod.fail(&block.base, inst_src, "TODO implement coerceArrayPtrToMany runtime instruction", .{}); } -fn analyzeDeclVal(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, decl: *Decl) CompileError!Air.Inst.Ref { +fn analyzeDeclVal( + sema: *Sema, + block: *Scope.Block, + src: LazySrcLoc, + decl: *Decl, +) CompileError!Air.Inst.Ref { + if (sema.decl_val_table.get(decl)) |result| { + return result; + } const decl_ref = try sema.analyzeDeclRef(block, src, decl); - return sema.analyzeLoad(block, src, decl_ref, src); + const result = try sema.analyzeLoad(block, src, decl_ref, src); + if (Air.refToIndex(result)) |index| { + if (sema.air_instructions.items(.tag)[index] == .constant) { + sema.decl_val_table.put(sema.gpa, decl, result) catch {}; + } + } + return result; } fn analyzeDeclRef(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, decl: *Decl) CompileError!Air.Inst.Ref { |
