From d7bd4f339c90b9ce07283600cb7ef129912ceef3 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Tue, 20 Jun 2023 13:44:24 -0400 Subject: Sema: optimize value resolution --- src/InternPool.zig | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/InternPool.zig') diff --git a/src/InternPool.zig b/src/InternPool.zig index bece6dbc4a..f93539daf8 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -622,6 +622,8 @@ pub const Key = union(enum) { len: Index = .none, pub const Addr = union(enum) { + const Tag = @typeInfo(Addr).Union.tag_type.?; + decl: Module.Decl.Index, mut_decl: MutDecl, comptime_field: Index, @@ -5702,10 +5704,18 @@ pub fn isNoReturn(ip: *const InternPool, ty: Index) bool { }; } +pub fn isUndef(ip: *const InternPool, val: Index) bool { + return val == .undef or ip.items.items(.tag)[@intFromEnum(val)] == .undef; +} + pub fn isRuntimeValue(ip: *const InternPool, val: Index) bool { return ip.items.items(.tag)[@intFromEnum(val)] == .runtime_value; } +pub fn isVariable(ip: *const InternPool, val: Index) bool { + return ip.items.items(.tag)[@intFromEnum(val)] == .variable; +} + pub fn getBackingDecl(ip: *const InternPool, val: Index) Module.Decl.OptionalIndex { var base = @intFromEnum(val); while (true) { @@ -5730,6 +5740,29 @@ pub fn getBackingDecl(ip: *const InternPool, val: Index) Module.Decl.OptionalInd } } +pub fn getBackingAddrTag(ip: *const InternPool, val: Index) ?Key.Ptr.Addr.Tag { + var base = @intFromEnum(val); + while (true) { + switch (ip.items.items(.tag)[base]) { + .ptr_decl => return .decl, + .ptr_mut_decl => return .mut_decl, + .ptr_comptime_field => return .comptime_field, + .ptr_int => return .int, + inline .ptr_eu_payload, + .ptr_opt_payload, + .ptr_elem, + .ptr_field, + => |tag| base = ip.extra.items[ + ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "base").? + ], + inline .ptr_slice => |tag| base = ip.extra.items[ + ip.items.items(.data)[base] + std.meta.fieldIndex(tag.Payload(), "ptr").? + ], + else => return null, + } + } +} + /// This is a particularly hot function, so we operate directly on encodings /// rather than the more straightforward implementation of calling `indexToKey`. pub fn zigTypeTagOrPoison(ip: *const InternPool, index: Index) error{GenericPoison}!std.builtin.TypeId { -- cgit v1.2.3