authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-23 16:10:16+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-28 16:47:55+00:00
log9c0c65c313776fdc394d8e481ee4eb548a5333ed
tree4edb7f6bb616ec8ba268ca7bba50d38499dac4ec
parentfb224178aaeeb5d8e37392f5c168afca771147ca
signaturelock-open Commit is signed but in an unrecognized format.

llvm: minor refactors, and incremental `@tagName` updates


2 files changed, 201 insertions(+), 176 deletions(-)

src/codegen/llvm.zig+49-37
......@@ -562,7 +562,7 @@ pub const Object = struct {
562562 /// Same deal as `decl_map` but for anonymous declarations, which are always global constants.
563563 uav_map: std.AutoHashMapUnmanaged(InternPool.Index, Builder.Global.Index),
564564 /// Maps enum types to their corresponding LLVM functions for implementing the `tag_name` instruction.
565 enum_tag_name_map: std.AutoHashMapUnmanaged(InternPool.Index, Builder.Global.Index),
565 enum_tag_name_map: std.AutoHashMapUnmanaged(InternPool.Index, Builder.Function.Index),
566566 /// Serves the same purpose as `enum_tag_name_map` but for the `is_named_enum_value` instruction.
567567 named_enum_map: std.AutoHashMapUnmanaged(InternPool.Index, Builder.Function.Index),
568568 /// Maps Zig types to LLVM types. The table memory is backed by the GPA of
......@@ -1138,7 +1138,7 @@ pub const Object = struct {
11381138 func_index: InternPool.Index,
11391139 air: *const Air,
11401140 liveness: *const ?Air.Liveness,
1141 ) !void {
1141 ) Zcu.CodegenFailError!void {
11421142 const zcu = o.zcu;
11431143 const comp = zcu.comp;
11441144 const ip = &zcu.intern_pool;
......@@ -1514,10 +1514,7 @@ pub const Object = struct {
15141514 defer fg.deinit();
15151515 deinit_wip = false;
15161516
1517 fg.genBody(air.getMainBody(), .poi) catch |err| switch (err) {
1518 error.CodegenFail => return, // MLUGG TODO
1519 else => |e| return e,
1520 };
1517 try fg.genBody(air.getMainBody(), .poi);
15211518
15221519 // If we saw any loads or stores involving `allowzero` pointers, we need to mark the whole
15231520 // function as considering null pointers valid so that LLVM's optimizers don't remove these
......@@ -1894,6 +1891,9 @@ pub const Object = struct {
18941891 if (o.named_enum_map.get(ty)) |function_index| {
18951892 try o.updateIsNamedEnumValueFunction(.fromInterned(ty), function_index);
18961893 }
1894 if (o.enum_tag_name_map.get(ty)) |function_index| {
1895 try o.updateEnumTagNameFunction(.fromInterned(ty), function_index);
1896 }
18971897 }
18981898
18991899 /// Should only be called by the `link.ConstPool` implementation.
......@@ -4271,25 +4271,39 @@ pub const Object = struct {
42714271 return o.errors_len_variable;
42724272 }
42734273
4274 /// MLUGG TODO: this also needs incremental updates dumbass
4275 pub fn getEnumTagNameFunction(o: *Object, enum_ty: Type) !Builder.Function.Index {
4274 pub fn getEnumTagNameFunction(o: *Object, enum_ty: Type) Allocator.Error!Builder.Function.Index {
42764275 const zcu = o.zcu;
42774276 const ip = &zcu.intern_pool;
4278 const enum_type = ip.loadEnumType(enum_ty.toIntern());
42794277
42804278 const gop = try o.enum_tag_name_map.getOrPut(o.gpa, enum_ty.toIntern());
4281 if (gop.found_existing) return gop.value_ptr.ptrConst(&o.builder).kind.function;
4279 if (gop.found_existing) return gop.value_ptr.*;
42824280 errdefer assert(o.enum_tag_name_map.remove(enum_ty.toIntern()));
4283
4284 const usize_ty = try o.lowerType(.usize);
4285 const ret_ty = try o.lowerType(.slice_const_u8_sentinel_0);
4286 const llvm_int_ty = try o.lowerType(.fromInterned(enum_type.int_tag_type));
4287 const target = &zcu.root_mod.resolved_target.result;
42884281 const function_index = try o.builder.addFunction(
4289 try o.builder.fnType(ret_ty, &.{llvm_int_ty}, .normal),
4290 try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_type.name.fmt(ip)}),
4291 toLlvmAddressSpace(.generic, target),
4282 // Dummy function type; `updateEnumTagNameFunction` will replace it with the correct type.
4283 // TODO: change the builder API so we don't need to do this.
4284 try o.builder.fnType(.void, &.{}, .normal),
4285 try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_ty.containerTypeName(ip).fmt(ip)}),
4286 toLlvmAddressSpace(.generic, zcu.getTarget()),
42924287 );
4288 gop.value_ptr.* = function_index;
4289 try o.updateEnumTagNameFunction(enum_ty, function_index);
4290 return function_index;
4291 }
4292 fn updateEnumTagNameFunction(
4293 o: *Object,
4294 enum_ty: Type,
4295 function_index: Builder.Function.Index,
4296 ) Allocator.Error!void {
4297 const zcu = o.zcu;
4298 const ip = &zcu.intern_pool;
4299 const loaded_enum = ip.loadEnumType(enum_ty.toIntern());
4300
4301 const llvm_usize_ty = try o.lowerType(.usize);
4302 const llvm_ret_ty = try o.lowerType(.slice_const_u8_sentinel_0);
4303 const llvm_int_ty = try o.lowerType(.fromInterned(loaded_enum.int_tag_type));
4304
4305 function_index.ptrConst(&o.builder).global.ptr(&o.builder).type =
4306 try o.builder.fnType(llvm_ret_ty, &.{llvm_int_ty}, .normal);
42934307
42944308 var attributes: Builder.FunctionAttributes.Wip = .{};
42954309 defer attributes.deinit(&o.builder);
......@@ -4298,7 +4312,6 @@ pub const Object = struct {
42984312 function_index.setLinkage(if (o.builder.strip) .private else .internal, &o.builder);
42994313 function_index.setCallConv(.fastcc, &o.builder);
43004314 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
4301 gop.value_ptr.* = function_index.ptrConst(&o.builder).global;
43024315
43034316 var wip = try Builder.WipFunction.init(&o.builder, .{
43044317 .function = function_index,
......@@ -4312,13 +4325,13 @@ pub const Object = struct {
43124325 var wip_switch = try wip.@"switch"(
43134326 tag_int_value,
43144327 bad_value_block,
4315 @intCast(enum_type.field_names.len),
4328 @intCast(loaded_enum.field_names.len),
43164329 .none,
43174330 );
43184331 defer wip_switch.finish(&wip);
43194332
4320 for (0..enum_type.field_names.len) |field_index| {
4321 const name = try o.builder.stringNull(enum_type.field_names.get(ip)[field_index].toSlice(ip));
4333 for (0..loaded_enum.field_names.len) |field_index| {
4334 const name = try o.builder.stringNull(loaded_enum.field_names.get(ip)[field_index].toSlice(ip));
43224335 const name_init = try o.builder.stringConst(name);
43234336 const name_variable_index =
43244337 try o.builder.addVariable(.empty, name_init.typeOf(&o.builder), .default);
......@@ -4328,13 +4341,13 @@ pub const Object = struct {
43284341 name_variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
43294342 name_variable_index.setAlignment(comptime Builder.Alignment.fromByteUnits(1), &o.builder);
43304343
4331 const name_val = try o.builder.structValue(ret_ty, &.{
4344 const name_val = try o.builder.structValue(llvm_ret_ty, &.{
43324345 name_variable_index.toConst(&o.builder),
4333 try o.builder.intConst(usize_ty, name.slice(&o.builder).?.len - 1),
4346 try o.builder.intConst(llvm_usize_ty, name.slice(&o.builder).?.len - 1),
43344347 });
43354348
43364349 const return_block = try wip.block(1, "Name");
4337 const llvm_tag_val = switch (enum_type.field_values.getOrNone(ip, field_index)) {
4350 const llvm_tag_val = switch (loaded_enum.field_values.getOrNone(ip, field_index)) {
43384351 .none => try o.builder.intConst(llvm_int_ty, field_index), // auto-numbered
43394352 else => |tag_val_ip| try o.lowerValue(tag_val_ip),
43404353 };
......@@ -4348,7 +4361,6 @@ pub const Object = struct {
43484361 _ = try wip.@"unreachable"();
43494362
43504363 try wip.finish();
4351 return function_index;
43524364 }
43534365
43544366 pub fn lazyAbiAlignment(o: *Object, pt: Zcu.PerThread, ty: Type) Allocator.Error!Builder.Alignment.Lazy {
......@@ -4356,7 +4368,7 @@ pub const Object = struct {
43564368 return o.lazy_abi_aligns.items[@intFromEnum(index)];
43574369 }
43584370
4359 pub fn getIsNamedEnumValueFunction(o: *Object, enum_ty: Type) !Builder.Function.Index {
4371 pub fn getIsNamedEnumValueFunction(o: *Object, enum_ty: Type) Allocator.Error!Builder.Function.Index {
43604372 const zcu = o.zcu;
43614373 const ip = &zcu.intern_pool;
43624374
......@@ -4380,22 +4392,22 @@ pub const Object = struct {
43804392 function_index: Builder.Function.Index,
43814393 ) Allocator.Error!void {
43824394 const zcu = o.zcu;
4383 const builder = &o.builder;
4384 const loaded_enum = zcu.intern_pool.loadEnumType(enum_ty.toIntern());
4395 const ip = &zcu.intern_pool;
4396 const loaded_enum = ip.loadEnumType(enum_ty.toIntern());
43854397
43864398 const llvm_int_ty = try o.lowerType(.fromInterned(loaded_enum.int_tag_type));
4387 function_index.ptrConst(builder).global.ptr(builder).type =
4388 try builder.fnType(.i1, &.{llvm_int_ty}, .normal);
4399 function_index.ptrConst(&o.builder).global.ptr(&o.builder).type =
4400 try o.builder.fnType(.i1, &.{llvm_int_ty}, .normal);
43894401
43904402 var attributes: Builder.FunctionAttributes.Wip = .{};
4391 defer attributes.deinit(builder);
4403 defer attributes.deinit(&o.builder);
43924404 try o.addCommonFnAttributes(&attributes, zcu.root_mod, zcu.root_mod.omit_frame_pointer);
43934405
4394 function_index.setLinkage(if (o.builder.strip) .private else .internal, builder);
4395 function_index.setCallConv(.fastcc, builder);
4396 function_index.setAttributes(try attributes.finish(builder), builder);
4406 function_index.setLinkage(if (o.builder.strip) .private else .internal, &o.builder);
4407 function_index.setCallConv(.fastcc, &o.builder);
4408 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
43974409
4398 var wip: Builder.WipFunction = try .init(builder, .{
4410 var wip: Builder.WipFunction = try .init(&o.builder, .{
43994411 .function = function_index,
44004412 .strip = true,
44014413 });
......@@ -4409,7 +4421,7 @@ pub const Object = struct {
44094421 defer wip_switch.finish(&wip);
44104422
44114423 if (loaded_enum.field_values.len > 0) {
4412 for (loaded_enum.field_values.get(&zcu.intern_pool)) |tag_val_ip| {
4424 for (loaded_enum.field_values.get(ip)) |tag_val_ip| {
44134425 const llvm_tag_val = try o.lowerValue(tag_val_ip);
44144426 try wip_switch.addCase(llvm_tag_val, named_block, &wip);
44154427 }
src/codegen/llvm/FuncGen.zig+152-139
......@@ -175,11 +175,6 @@ fn resolveValue(self: *FuncGen, val: Value) Allocator.Error!Builder.Constant {
175175 }
176176}
177177
178/// MLUGG TODO okay yeah prolly delete this again
179fn lowerType(fg: *const FuncGen, ty: Type) Allocator.Error!Builder.Type {
180 return fg.object.lowerType(ty);
181}
182
183178pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air.CoveragePoint) TodoError!void {
184179 const o = self.object;
185180 const zcu = self.object.zcu;
......@@ -608,7 +603,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
608603 }
609604
610605 const ret_ptr = if (!sret) null else blk: {
611 const llvm_ret_ty = try self.lowerType(return_type);
606 const llvm_ret_ty = try o.lowerType(return_type);
612607 try attributes.addParamAttr(0, .{ .sret = llvm_ret_ty }, &o.builder);
613608
614609 const alignment = return_type.abiAlignment(zcu).toLlvm();
......@@ -630,7 +625,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
630625 const arg = args[it.zig_index - 1];
631626 const param_ty = self.typeOf(arg);
632627 const llvm_arg = try self.resolveInst(arg);
633 const llvm_param_ty = try self.lowerType(param_ty);
628 const llvm_param_ty = try o.lowerType(param_ty);
634629 if (isByRef(param_ty, zcu)) {
635630 const alignment = param_ty.abiAlignment(zcu).toLlvm();
636631 const loaded = try self.wip.load(.normal, llvm_param_ty, llvm_arg, alignment, "");
......@@ -659,7 +654,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
659654 const llvm_arg = try self.resolveInst(arg);
660655
661656 const alignment = param_ty.abiAlignment(zcu).toLlvm();
662 const param_llvm_ty = try self.lowerType(param_ty);
657 const param_llvm_ty = try o.lowerType(param_ty);
663658 const arg_ptr = try self.buildAlloca(param_llvm_ty, alignment);
664659 if (isByRef(param_ty, zcu)) {
665660 const loaded = try self.wip.load(.normal, param_llvm_ty, llvm_arg, alignment, "");
......@@ -729,7 +724,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
729724 llvm_arg = ptr;
730725 }
731726
732 const float_ty = try self.lowerType(aarch64_c_abi.getFloatArrayType(arg_ty, zcu).?);
727 const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(arg_ty, zcu).?);
733728 const array_ty = try o.builder.arrayType(count, float_ty);
734729
735730 const loaded = try self.wip.load(.normal, array_ty, llvm_arg, alignment, "");
......@@ -770,7 +765,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
770765 .byref => {
771766 const param_index = it.zig_index - 1;
772767 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[param_index]);
773 const param_llvm_ty = try self.lowerType(param_ty);
768 const param_llvm_ty = try o.lowerType(param_ty);
774769 const alignment = param_ty.abiAlignment(zcu).toLlvm();
775770 try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment, it.byval_attr, param_llvm_ty);
776771 },
......@@ -822,7 +817,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
822817 },
823818 toLlvmCallConvTag(fn_info.cc, target).?,
824819 try attributes.finish(&o.builder),
825 try self.lowerType(zig_fn_ty),
820 try o.lowerType(zig_fn_ty),
826821 llvm_fn,
827822 llvm_args.items,
828823 "",
......@@ -836,7 +831,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
836831 return .none;
837832 }
838833
839 const llvm_ret_ty = try self.lowerType(return_type);
834 const llvm_ret_ty = try o.lowerType(return_type);
840835 if (ret_ptr) |rp| {
841836 if (isByRef(return_type, zcu)) {
842837 return rp;
......@@ -908,7 +903,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
908903 const operand = try self.resolveInst(un_op);
909904 const val_is_undef = if (un_op.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
910905 if (val_is_undef and safety) {
911 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
906 const len = try o.builder.intValue(try o.lowerType(.usize), ret_ty.abiSize(zcu));
912907 _ = try self.wip.callMemSet(
913908 self.ret_ptr,
914909 ret_ty.abiAlignment(zcu).toLlvm(),
......@@ -964,7 +959,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
964959 if (val_is_undef and safety) {
965960 const llvm_ret_ty = operand.typeOfWip(&self.wip);
966961 const rp = try self.buildAlloca(llvm_ret_ty, alignment);
967 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
962 const len = try o.builder.intValue(try o.lowerType(.usize), ret_ty.abiSize(zcu));
968963 _ = try self.wip.callMemSet(
969964 rp,
970965 alignment,
......@@ -1034,17 +1029,18 @@ fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
10341029 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
10351030 const list = try self.resolveInst(ty_op.operand);
10361031 const arg_ty = ty_op.ty.toType();
1037 const llvm_arg_ty = try self.lowerType(arg_ty);
1032 const llvm_arg_ty = try self.object.lowerType(arg_ty);
10381033
10391034 return self.wip.vaArg(list, llvm_arg_ty, "");
10401035}
10411036
10421037fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1043 const zcu = self.object.zcu;
1038 const o = self.object;
1039 const zcu = o.zcu;
10441040 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
10451041 const src_list = try self.resolveInst(ty_op.operand);
10461042 const va_list_ty = ty_op.ty.toType();
1047 const llvm_va_list_ty = try self.lowerType(va_list_ty);
1043 const llvm_va_list_ty = try o.lowerType(va_list_ty);
10481044
10491045 const result_alignment = va_list_ty.abiAlignment(zcu).toLlvm();
10501046 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
......@@ -1065,9 +1061,10 @@ fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
10651061}
10661062
10671063fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1068 const zcu = self.object.zcu;
1064 const o = self.object;
1065 const zcu = o.zcu;
10691066 const va_list_ty = self.typeOfIndex(inst);
1070 const llvm_va_list_ty = try self.lowerType(va_list_ty);
1067 const llvm_va_list_ty = try o.lowerType(va_list_ty);
10711068
10721069 const result_alignment = va_list_ty.abiAlignment(zcu).toLlvm();
10731070 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
......@@ -1227,7 +1224,8 @@ fn lowerBlock(
12271224 maybe_inline_func: ?InternPool.Index,
12281225 body: []const Air.Inst.Index,
12291226) TodoError!Builder.Value {
1230 const zcu = self.object.zcu;
1227 const o = self.object;
1228 const zcu = o.zcu;
12311229 const inst_ty = self.typeOfIndex(inst);
12321230
12331231 if (inst_ty.isNoReturn(zcu)) {
......@@ -1253,7 +1251,7 @@ fn lowerBlock(
12531251
12541252 // Create a phi node only if the block returns a value.
12551253 if (have_block_result) {
1256 const raw_llvm_ty = try self.lowerType(inst_ty);
1254 const raw_llvm_ty = try o.lowerType(inst_ty);
12571255 const llvm_ty: Builder.Type = ty: {
12581256 // If the zig tag type is a function, this represents an actual function body; not
12591257 // a pointer to it. LLVM IR allows the call instruction to use function bodies instead
......@@ -1370,7 +1368,7 @@ fn lowerSwitchDispatch(
13701368 const table_index = try self.wip.conv(
13711369 .unsigned,
13721370 try self.wip.bin(.@"sub nuw", cond, jmp_table.min.toValue(), ""),
1373 try self.lowerType(.usize),
1371 try o.lowerType(.usize),
13741372 "",
13751373 );
13761374 const target_ptr_ptr = try self.ptraddScaled(
......@@ -1395,7 +1393,7 @@ fn lowerSwitchDispatch(
13951393 // The switch prongs will correspond to our scalar cases. Ranges will
13961394 // be handled by conditional branches in the `else` prong.
13971395
1398 const llvm_usize = try self.lowerType(.usize);
1396 const llvm_usize = try o.lowerType(.usize);
13991397 const cond_int = if (cond.typeOfWip(&self.wip).isPointer(&o.builder))
14001398 try self.wip.cast(.ptrtoint, cond, llvm_usize, "")
14011399 else
......@@ -1642,7 +1640,7 @@ fn lowerTry(
16421640 } else if (isByRef(payload_ty, zcu)) {
16431641 return fg.loadByRef(payload_ptr, payload_ty, payload_align.toLlvm(), .normal);
16441642 } else {
1645 return fg.wip.load(.normal, try fg.lowerType(payload_ty), payload_ptr, payload_align.toLlvm(), "");
1643 return fg.wip.load(.normal, try o.lowerType(payload_ty), payload_ptr, payload_align.toLlvm(), "");
16461644 }
16471645}
16481646
......@@ -1912,9 +1910,9 @@ fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
19121910 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
19131911 const operand_ty = self.typeOf(ty_op.operand);
19141912 const array_ty = operand_ty.childType(zcu);
1915 const llvm_usize = try self.lowerType(.usize);
1913 const llvm_usize = try o.lowerType(.usize);
19161914 const len = try o.builder.intValue(llvm_usize, array_ty.arrayLen(zcu));
1917 const slice_llvm_ty = try self.lowerType(self.typeOfIndex(inst));
1915 const slice_llvm_ty = try o.lowerType(self.typeOfIndex(inst));
19181916 const operand = try self.resolveInst(ty_op.operand);
19191917 return self.wip.buildAggregate(slice_llvm_ty, &.{ operand, len }, "");
19201918}
......@@ -1931,7 +1929,7 @@ fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value
19311929
19321930 const dest_ty = self.typeOfIndex(inst);
19331931 const dest_scalar_ty = dest_ty.scalarType(zcu);
1934 const dest_llvm_ty = try self.lowerType(dest_ty);
1932 const dest_llvm_ty = try o.lowerType(dest_ty);
19351933 const target = zcu.getTarget();
19361934
19371935 if (intrinsicsAllowed(dest_scalar_ty, target)) return self.wip.conv(
......@@ -1999,7 +1997,7 @@ fn airIntFromFloat(
19991997
20001998 const dest_ty = self.typeOfIndex(inst);
20011999 const dest_scalar_ty = dest_ty.scalarType(zcu);
2002 const dest_llvm_ty = try self.lowerType(dest_ty);
2000 const dest_llvm_ty = try o.lowerType(dest_ty);
20032001
20042002 if (intrinsicsAllowed(operand_scalar_ty, target)) {
20052003 // TODO set fast math flag
......@@ -2033,7 +2031,7 @@ fn airIntFromFloat(
20332031 compiler_rt_dest_abbrev,
20342032 });
20352033
2036 const operand_llvm_ty = try self.lowerType(operand_ty);
2034 const operand_llvm_ty = try o.lowerType(operand_ty);
20372035 const libc_fn = try o.getLibcFunction(fn_name, &.{operand_llvm_ty}, libc_ret_ty);
20382036 var result = try self.wip.call(
20392037 .normal,
......@@ -2058,7 +2056,7 @@ fn sliceOrArrayPtr(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!B
20582056fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {
20592057 const o = fg.object;
20602058 const zcu = o.zcu;
2061 const llvm_usize = try fg.lowerType(.usize);
2059 const llvm_usize = try o.lowerType(.usize);
20622060 switch (ty.ptrSize(zcu)) {
20632061 .slice => {
20642062 const len = try fg.wip.extractValue(ptr, &.{1}, "");
......@@ -2240,7 +2238,7 @@ fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
22402238 },
22412239 .float => {
22422240 // bitcast int->float
2243 return self.wip.cast(.bitcast, field_int_val, try self.lowerType(field_ty), "");
2241 return self.wip.cast(.bitcast, field_int_val, try o.lowerType(field_ty), "");
22442242 },
22452243 }
22462244 }
......@@ -2277,8 +2275,8 @@ fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
22772275 const field_offset = parent_ty.structFieldOffset(extra.field_index, zcu);
22782276 if (field_offset == 0) return field_ptr;
22792277
2280 const res_ty = try self.lowerType(ty_pl.ty.toType());
2281 const llvm_usize = try self.lowerType(.usize);
2278 const res_ty = try o.lowerType(ty_pl.ty.toType());
2279 const llvm_usize = try o.lowerType(.usize);
22822280
22832281 const field_ptr_int = try self.wip.cast(.ptrtoint, field_ptr, llvm_usize, "");
22842282 const base_ptr_int = try self.wip.bin(
......@@ -2495,7 +2493,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
24952493 const output_inst = try self.resolveInst(output.operand);
24962494 const output_ty = self.typeOf(output.operand);
24972495 assert(output_ty.zigTypeTag(zcu) == .pointer);
2498 const elem_llvm_ty = try self.lowerType(output_ty.childType(zcu));
2496 const elem_llvm_ty = try o.lowerType(output_ty.childType(zcu));
24992497
25002498 switch (constraint[0]) {
25012499 '=' => {},
......@@ -2533,7 +2531,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
25332531 llvm_ret_indirect[output.index] = false;
25342532
25352533 const ret_ty = self.typeOfIndex(inst);
2536 llvm_ret_types[llvm_ret_i] = try self.lowerType(ret_ty);
2534 llvm_ret_types[llvm_ret_i] = try o.lowerType(ret_ty);
25372535 llvm_ret_i += 1;
25382536 }
25392537
......@@ -2572,7 +2570,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
25722570 llvm_param_types[llvm_param_i] = arg_llvm_value.typeOfWip(&self.wip);
25732571 } else {
25742572 const alignment = arg_ty.abiAlignment(zcu).toLlvm();
2575 const arg_llvm_ty = try self.lowerType(arg_ty);
2573 const arg_llvm_ty = try o.lowerType(arg_ty);
25762574 const load_inst =
25772575 try self.wip.load(.normal, arg_llvm_ty, arg_llvm_value, alignment, "");
25782576 llvm_param_values[llvm_param_i] = load_inst;
......@@ -2613,7 +2611,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
26132611 llvm_param_attrs[llvm_param_i] = if (constraint[0] == '*') blk: {
26142612 if (!is_by_ref) self.maybeMarkAllowZeroAccess(arg_ty.ptrInfo(zcu));
26152613
2616 break :blk try self.lowerType(if (is_by_ref) arg_ty else arg_ty.childType(zcu));
2614 break :blk try o.lowerType(if (is_by_ref) arg_ty else arg_ty.childType(zcu));
26172615 } else .none;
26182616
26192617 llvm_param_i += 1;
......@@ -2627,7 +2625,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
26272625 if (constraint[0] != '+') continue;
26282626
26292627 const rw_ty = self.typeOf(output.operand);
2630 const llvm_elem_ty = try self.lowerType(rw_ty.childType(zcu));
2628 const llvm_elem_ty = try o.lowerType(rw_ty.childType(zcu));
26312629 if (llvm_ret_indirect[output.index]) {
26322630 llvm_param_values[llvm_param_i] = llvm_rw_vals[output.index];
26332631 llvm_param_types[llvm_param_i] = llvm_rw_vals[output.index].typeOfWip(&self.wip);
......@@ -2827,7 +2825,7 @@ fn airIsNonNull(
28272825 const operand = try self.resolveInst(un_op);
28282826 const operand_ty = self.typeOf(un_op);
28292827 const optional_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty;
2830 const optional_llvm_ty = try self.lowerType(optional_ty);
2828 const optional_llvm_ty = try o.lowerType(optional_ty);
28312829 const payload_ty = optional_ty.optionalChild(zcu);
28322830
28332831 const access_kind: Builder.MemoryAccessKind =
......@@ -2896,7 +2894,7 @@ fn airIsErr(
28962894
28972895 if (!payload_ty.hasRuntimeBits(zcu)) {
28982896 const loaded = if (operand_is_ptr)
2899 try self.wip.load(access_kind, try self.lowerType(err_union_ty), operand, operand_ty.ptrAlignment(zcu).toLlvm(), "")
2897 try self.wip.load(access_kind, try o.lowerType(err_union_ty), operand, operand_ty.ptrAlignment(zcu).toLlvm(), "")
29002898 else
29012899 operand;
29022900 return self.wip.icmp(cond, loaded, zero, "");
......@@ -2981,7 +2979,8 @@ fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Buil
29812979}
29822980
29832981fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool) Allocator.Error!Builder.Value {
2984 const zcu = self.object.zcu;
2982 const o = self.object;
2983 const zcu = o.zcu;
29852984 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
29862985 const operand = try self.resolveInst(ty_op.operand);
29872986 const operand_ty = self.typeOf(ty_op.operand);
......@@ -3001,7 +3000,7 @@ fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool
30013000 if (isByRef(payload_ty, zcu)) {
30023001 return self.loadByRef(payload_ptr, payload_ty, payload_alignment, .normal);
30033002 } else {
3004 const payload_llvm_ty = try self.lowerType(payload_ty);
3003 const payload_llvm_ty = try o.lowerType(payload_ty);
30053004 return self.wip.load(.normal, payload_llvm_ty, payload_ptr, payload_alignment, "");
30063005 }
30073006}
......@@ -3148,7 +3147,7 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.
31483147 const optional_ty = self.typeOfIndex(inst);
31493148 if (optional_ty.optionalReprIsPayload(zcu)) return operand;
31503149 assert(isByRef(optional_ty, zcu)); // optionals with runtime bits are by-ref unless `optionalReprIsPayload`
3151 const llvm_optional_ty = try self.lowerType(optional_ty);
3150 const llvm_optional_ty = try o.lowerType(optional_ty);
31523151 const optional_ptr = if (self.isNextRet(body_tail))
31533152 self.ret_ptr
31543153 else brk: {
......@@ -3181,7 +3180,7 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) All
31813180 assert(payload_ty.hasRuntimeBits(zcu));
31823181 assert(isByRef(err_un_ty, zcu)); // error unions with runtime bits are always by-ref
31833182 const ok_err_code = try o.builder.intValue(try o.errorIntType(), 0);
3184 const err_un_llvm_ty = try self.lowerType(err_un_ty);
3183 const err_un_llvm_ty = try o.lowerType(err_un_ty);
31853184
31863185 const result_ptr = if (self.isNextRet(body_tail))
31873186 self.ret_ptr
......@@ -3205,7 +3204,8 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) All
32053204}
32063205
32073206fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
3208 const zcu = self.object.zcu;
3207 const o = self.object;
3208 const zcu = o.zcu;
32093209 const inst = body_tail[0];
32103210 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
32113211 const err_un_ty = self.typeOfIndex(inst);
......@@ -3213,7 +3213,7 @@ fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocat
32133213 const operand = try self.resolveInst(ty_op.operand);
32143214 if (!payload_ty.hasRuntimeBits(zcu)) return operand;
32153215 assert(isByRef(err_un_ty, zcu)); // error unions with runtime bits are always by-ref
3216 const err_un_llvm_ty = try self.lowerType(err_un_ty);
3216 const err_un_llvm_ty = try o.lowerType(err_un_ty);
32173217
32183218 const result_ptr = if (self.isNextRet(body_tail))
32193219 self.ret_ptr
......@@ -3236,7 +3236,7 @@ fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
32363236 const o = self.object;
32373237 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
32383238 const index = pl_op.payload;
3239 const llvm_usize = try self.lowerType(.usize);
3239 const llvm_usize = try o.lowerType(.usize);
32403240 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{llvm_usize}, &.{
32413241 try o.builder.intValue(.i32, index),
32423242 }, "");
......@@ -3246,7 +3246,7 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
32463246 const o = self.object;
32473247 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
32483248 const index = pl_op.payload;
3249 const llvm_isize = try self.lowerType(.isize);
3249 const llvm_isize = try o.lowerType(.isize);
32503250 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{llvm_isize}, &.{
32513251 try o.builder.intValue(.i32, index), try self.resolveInst(pl_op.operand),
32523252 }, "");
......@@ -3260,7 +3260,8 @@ fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.
32603260}
32613261
32623262fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3263 const zcu = self.object.zcu;
3263 const o = self.object;
3264 const zcu = o.zcu;
32643265 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
32653266 const lhs = try self.resolveInst(bin_op.lhs);
32663267 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3272,14 +3273,15 @@ fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
32723273 .normal,
32733274 .none,
32743275 if (scalar_ty.isSignedInt(zcu)) .smin else .umin,
3275 &.{try self.lowerType(inst_ty)},
3276 &.{try o.lowerType(inst_ty)},
32763277 &.{ lhs, rhs },
32773278 "",
32783279 );
32793280}
32803281
32813282fn airMax(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3282 const zcu = self.object.zcu;
3283 const o = self.object;
3284 const zcu = o.zcu;
32833285 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
32843286 const lhs = try self.resolveInst(bin_op.lhs);
32853287 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3291,7 +3293,7 @@ fn airMax(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
32913293 .normal,
32923294 .none,
32933295 if (scalar_ty.isSignedInt(zcu)) .smax else .umax,
3294 &.{try self.lowerType(inst_ty)},
3296 &.{try o.lowerType(inst_ty)},
32953297 &.{ lhs, rhs },
32963298 "",
32973299 );
......@@ -3303,7 +3305,7 @@ fn airSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
33033305 const ptr = try self.resolveInst(bin_op.lhs);
33043306 const len = try self.resolveInst(bin_op.rhs);
33053307 const inst_ty = self.typeOfIndex(inst);
3306 return self.wip.buildAggregate(try self.lowerType(inst_ty), &.{ ptr, len }, "");
3308 return self.wip.buildAggregate(try self.object.lowerType(inst_ty), &.{ ptr, len }, "");
33073309}
33083310
33093311fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
......@@ -3334,7 +3336,7 @@ fn airSafeArithmetic(
33343336 const scalar_ty = inst_ty.scalarType(zcu);
33353337
33363338 const intrinsic = if (scalar_ty.isSignedInt(zcu)) signed_intrinsic else unsigned_intrinsic;
3337 const llvm_inst_ty = try fg.lowerType(inst_ty);
3339 const llvm_inst_ty = try o.lowerType(inst_ty);
33383340 const results =
33393341 try fg.wip.callIntrinsic(.normal, .none, intrinsic, &.{llvm_inst_ty}, &.{ lhs, rhs }, "");
33403342
......@@ -3372,7 +3374,8 @@ fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
33723374}
33733375
33743376fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3375 const zcu = self.object.zcu;
3377 const o = self.object;
3378 const zcu = o.zcu;
33763379 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33773380 const lhs = try self.resolveInst(bin_op.lhs);
33783381 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3383,7 +3386,7 @@ fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
33833386 .normal,
33843387 .none,
33853388 if (scalar_ty.isSignedInt(zcu)) .@"sadd.sat" else .@"uadd.sat",
3386 &.{try self.lowerType(inst_ty)},
3389 &.{try o.lowerType(inst_ty)},
33873390 &.{ lhs, rhs },
33883391 "",
33893392 );
......@@ -3410,7 +3413,8 @@ fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
34103413}
34113414
34123415fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3413 const zcu = self.object.zcu;
3416 const o = self.object;
3417 const zcu = o.zcu;
34143418 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34153419 const lhs = try self.resolveInst(bin_op.lhs);
34163420 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3421,7 +3425,7 @@ fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
34213425 .normal,
34223426 .none,
34233427 if (scalar_ty.isSignedInt(zcu)) .@"ssub.sat" else .@"usub.sat",
3424 &.{try self.lowerType(inst_ty)},
3428 &.{try o.lowerType(inst_ty)},
34253429 &.{ lhs, rhs },
34263430 "",
34273431 );
......@@ -3448,7 +3452,8 @@ fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
34483452}
34493453
34503454fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3451 const zcu = self.object.zcu;
3455 const o = self.object;
3456 const zcu = o.zcu;
34523457 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34533458 const lhs = try self.resolveInst(bin_op.lhs);
34543459 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3459,7 +3464,7 @@ fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
34593464 .normal,
34603465 .none,
34613466 if (scalar_ty.isSignedInt(zcu)) .@"smul.fix.sat" else .@"umul.fix.sat",
3462 &.{try self.lowerType(inst_ty)},
3467 &.{try o.lowerType(inst_ty)},
34633468 &.{ lhs, rhs, .@"0" },
34643469 "",
34653470 );
......@@ -3503,7 +3508,7 @@ fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35033508 return self.buildFloatOp(.floor, fast, inst_ty, 1, .{result});
35043509 }
35053510 if (scalar_ty.isSignedInt(zcu)) {
3506 const inst_llvm_ty = try self.lowerType(inst_ty);
3511 const inst_llvm_ty = try o.lowerType(inst_ty);
35073512
35083513 const ExpectedContents = [std.math.big.int.calcTwosCompLimbCount(256)]std.math.big.Limb;
35093514 var stack align(@max(
......@@ -3579,7 +3584,7 @@ fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo
35793584 const lhs = try self.resolveInst(bin_op.lhs);
35803585 const rhs = try self.resolveInst(bin_op.rhs);
35813586 const inst_ty = self.typeOfIndex(inst);
3582 const inst_llvm_ty = try self.lowerType(inst_ty);
3587 const inst_llvm_ty = try o.lowerType(inst_ty);
35833588 const scalar_ty = inst_ty.scalarType(zcu);
35843589
35853590 if (scalar_ty.isRuntimeFloat()) {
......@@ -3646,7 +3651,7 @@ fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
36463651 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
36473652 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
36483653 const ptr_or_slice = try self.resolveInst(bin_op.lhs);
3649 const llvm_usize_ty = try self.lowerType(.usize);
3654 const llvm_usize_ty = try o.lowerType(.usize);
36503655 const ptr_ty = self.typeOf(bin_op.lhs);
36513656 const elem_ty = ptr_ty.indexableElem(zcu);
36523657 const ptr = switch (ptr_ty.ptrSize(zcu)) {
......@@ -3665,7 +3670,8 @@ fn airOverflow(
36653670 signed_intrinsic: Builder.Intrinsic,
36663671 unsigned_intrinsic: Builder.Intrinsic,
36673672) Allocator.Error!Builder.Value {
3668 const zcu = self.object.zcu;
3673 const o = self.object;
3674 const zcu = o.zcu;
36693675 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
36703676 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
36713677
......@@ -3678,8 +3684,8 @@ fn airOverflow(
36783684 assert(isByRef(inst_ty, zcu)); // auto structs are by-ref
36793685
36803686 const intrinsic = if (scalar_ty.isSignedInt(zcu)) signed_intrinsic else unsigned_intrinsic;
3681 const llvm_inst_ty = try self.lowerType(inst_ty);
3682 const llvm_lhs_ty = try self.lowerType(lhs_ty);
3687 const llvm_inst_ty = try o.lowerType(inst_ty);
3688 const llvm_lhs_ty = try o.lowerType(lhs_ty);
36833689 const results =
36843690 try self.wip.callIntrinsic(.normal, .none, intrinsic, &.{llvm_lhs_ty}, &.{ lhs, rhs }, "");
36853691
......@@ -3750,7 +3756,7 @@ fn buildFloatCmp(
37503756 const zcu = o.zcu;
37513757 const target = zcu.getTarget();
37523758 const scalar_ty = ty.scalarType(zcu);
3753 const scalar_llvm_ty = try self.lowerType(scalar_ty);
3759 const scalar_llvm_ty = try o.lowerType(scalar_ty);
37543760
37553761 if (intrinsicsAllowed(scalar_ty, target)) {
37563762 const cond: Builder.FloatCondition = switch (pred) {
......@@ -3856,7 +3862,7 @@ fn buildFloatOp(
38563862 const zcu = o.zcu;
38573863 const target = zcu.getTarget();
38583864 const scalar_ty = ty.scalarType(zcu);
3859 const llvm_ty = try self.lowerType(ty);
3865 const llvm_ty = try o.lowerType(ty);
38603866
38613867 if (op != .tan and intrinsicsAllowed(scalar_ty, target)) switch (op) {
38623868 // Some operations are dedicated LLVM instructions, not available as intrinsics
......@@ -3993,7 +3999,8 @@ fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
39933999}
39944000
39954001fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3996 const zcu = self.object.zcu;
4002 const o = self.object;
4003 const zcu = o.zcu;
39974004 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
39984005 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
39994006
......@@ -4011,9 +4018,9 @@ fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Buil
40114018
40124019 const dest_ty = self.typeOfIndex(inst);
40134020 assert(isByRef(dest_ty, zcu)); // auto structs are by-ref
4014 const llvm_dest_ty = try self.lowerType(dest_ty);
4021 const llvm_dest_ty = try o.lowerType(dest_ty);
40154022
4016 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
4023 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), "");
40174024
40184025 const result = try self.wip.bin(.shl, lhs, casted_rhs, "");
40194026 const reconstructed = try self.wip.bin(if (lhs_scalar_ty.isSignedInt(zcu))
......@@ -4063,7 +4070,8 @@ fn airXor(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
40634070}
40644071
40654072fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4066 const zcu = self.object.zcu;
4073 const o = self.object;
4074 const zcu = o.zcu;
40674075 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
40684076
40694077 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4077,7 +4085,7 @@ fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
40774085 }
40784086 const lhs_scalar_ty = lhs_ty.scalarType(zcu);
40794087
4080 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
4088 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), "");
40814089 return self.wip.bin(if (lhs_scalar_ty.isSignedInt(zcu))
40824090 .@"shl nsw"
40834091 else
......@@ -4085,7 +4093,8 @@ fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
40854093}
40864094
40874095fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4088 const zcu = self.object.zcu;
4096 const o = self.object;
4097 const zcu = o.zcu;
40894098 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
40904099
40914100 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4097,7 +4106,7 @@ fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
40974106 // features which we do not use. Therefore this branch is currently impossible.
40984107 unreachable;
40994108 }
4100 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
4109 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), "");
41014110 return self.wip.bin(.shl, lhs, casted_rhs, "");
41024111}
41034112
......@@ -4111,7 +4120,7 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
41114120
41124121 const lhs_ty = self.typeOf(bin_op.lhs);
41134122 const lhs_info = lhs_ty.intInfo(zcu);
4114 const llvm_lhs_ty = try self.lowerType(lhs_ty);
4123 const llvm_lhs_ty = try o.lowerType(lhs_ty);
41154124 const llvm_lhs_scalar_ty = llvm_lhs_ty.scalarType(&o.builder);
41164125
41174126 const rhs_ty = self.typeOf(bin_op.rhs);
......@@ -4122,7 +4131,7 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
41224131 }
41234132 const rhs_info = rhs_ty.intInfo(zcu);
41244133 assert(rhs_info.signedness == .unsigned);
4125 const llvm_rhs_ty = try self.lowerType(rhs_ty);
4134 const llvm_rhs_ty = try o.lowerType(rhs_ty);
41264135 const llvm_rhs_scalar_ty = llvm_rhs_ty.scalarType(&o.builder);
41274136
41284137 const result = try self.wip.callIntrinsic(
......@@ -4184,7 +4193,8 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
41844193}
41854194
41864195fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!Builder.Value {
4187 const zcu = self.object.zcu;
4196 const o = self.object;
4197 const zcu = o.zcu;
41884198 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41894199
41904200 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4198,7 +4208,7 @@ fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!
41984208 }
41994209 const lhs_scalar_ty = lhs_ty.scalarType(zcu);
42004210
4201 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
4211 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), "");
42024212 const is_signed_int = lhs_scalar_ty.isSignedInt(zcu);
42034213
42044214 return self.wip.bin(if (is_exact)
......@@ -4219,7 +4229,7 @@ fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
42194229 .normal,
42204230 .none,
42214231 .abs,
4222 &.{try self.lowerType(operand_ty)},
4232 &.{try o.lowerType(operand_ty)},
42234233 &.{ operand, try o.builder.intValue(.i1, 0) },
42244234 "",
42254235 ),
......@@ -4233,7 +4243,7 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
42334243 const zcu = o.zcu;
42344244 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
42354245 const dest_ty = fg.typeOfIndex(inst);
4236 const dest_llvm_ty = try fg.lowerType(dest_ty);
4246 const dest_llvm_ty = try o.lowerType(dest_ty);
42374247 const operand = try fg.resolveInst(ty_op.operand);
42384248 const operand_ty = fg.typeOf(ty_op.operand);
42394249 const operand_info = operand_ty.intInfo(zcu);
......@@ -4261,8 +4271,8 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
42614271
42624272 if (!have_min_check and !have_max_check) break :bounds_check;
42634273
4264 const operand_llvm_ty = try fg.lowerType(operand_ty);
4265 const operand_scalar_llvm_ty = try fg.lowerType(operand_scalar);
4274 const operand_llvm_ty = try o.lowerType(operand_ty);
4275 const operand_scalar_llvm_ty = try o.lowerType(operand_scalar);
42664276
42674277 const is_vector = operand_ty.zigTypeTag(zcu) == .vector;
42684278 assert(is_vector == (dest_ty.zigTypeTag(zcu) == .vector));
......@@ -4340,7 +4350,7 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
43404350fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
43414351 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
43424352 const operand = try self.resolveInst(ty_op.operand);
4343 const dest_llvm_ty = try self.lowerType(self.typeOfIndex(inst));
4353 const dest_llvm_ty = try self.object.lowerType(self.typeOfIndex(inst));
43444354 return self.wip.cast(.trunc, operand, dest_llvm_ty, "");
43454355}
43464356
......@@ -4354,10 +4364,10 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
43544364 const target = zcu.getTarget();
43554365
43564366 if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) {
4357 return self.wip.cast(.fptrunc, operand, try self.lowerType(dest_ty), "");
4367 return self.wip.cast(.fptrunc, operand, try o.lowerType(dest_ty), "");
43584368 } else {
4359 const operand_llvm_ty = try self.lowerType(operand_ty);
4360 const dest_llvm_ty = try self.lowerType(dest_ty);
4369 const operand_llvm_ty = try o.lowerType(operand_ty);
4370 const dest_llvm_ty = try o.lowerType(dest_ty);
43614371
43624372 const dest_bits = dest_ty.floatBits(target);
43634373 const src_bits = operand_ty.floatBits(target);
......@@ -4388,10 +4398,10 @@ fn airFpext(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
43884398 const target = zcu.getTarget();
43894399
43904400 if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) {
4391 return self.wip.cast(.fpext, operand, try self.lowerType(dest_ty), "");
4401 return self.wip.cast(.fpext, operand, try o.lowerType(dest_ty), "");
43924402 } else {
4393 const operand_llvm_ty = try self.lowerType(operand_ty);
4394 const dest_llvm_ty = try self.lowerType(dest_ty);
4403 const operand_llvm_ty = try o.lowerType(operand_ty);
4404 const dest_llvm_ty = try o.lowerType(dest_ty);
43954405
43964406 const dest_bits = dest_ty.scalarType(zcu).floatBits(target);
43974407 const src_bits = operand_ty.scalarType(zcu).floatBits(target);
......@@ -4431,7 +4441,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
44314441 const zcu = o.zcu;
44324442 const operand_is_ref = isByRef(operand_ty, zcu);
44334443 const result_is_ref = isByRef(inst_ty, zcu);
4434 const llvm_dest_ty = try self.lowerType(inst_ty);
4444 const llvm_dest_ty = try o.lowerType(inst_ty);
44354445
44364446 if (operand_is_ref and result_is_ref) {
44374447 // They are both pointers, so just return the same opaque pointer :)
......@@ -4477,7 +4487,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
44774487 } else if (operand_ty.zigTypeTag(zcu) == .array and inst_ty.zigTypeTag(zcu) == .vector) {
44784488 const elem_ty = operand_ty.childType(zcu);
44794489 assert(operand_is_ref); // arrays are always by-ref provided they have runtime bits
4480 const llvm_vector_ty = try self.lowerType(inst_ty);
4490 const llvm_vector_ty = try o.lowerType(inst_ty);
44814491
44824492 const bitcast_ok = elem_ty.bitSize(zcu) == elem_ty.abiSize(zcu) * 8;
44834493 if (bitcast_ok) {
......@@ -4488,7 +4498,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
44884498 } else {
44894499 // If the ABI size of the element type is not evenly divisible by size in bits;
44904500 // a simple bitcast will not work, and we fall back to extractelement.
4491 const elem_llvm_ty = try self.lowerType(elem_ty);
4501 const elem_llvm_ty = try o.lowerType(elem_ty);
44924502 const elem_size = elem_ty.abiSize(zcu);
44934503 const vector_len = operand_ty.arrayLen(zcu);
44944504 var vector = try o.builder.poisonValue(llvm_vector_ty);
......@@ -4629,7 +4639,7 @@ fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
46294639 const ptr_info = ptr_ty.ptrInfo(zcu);
46304640 return (try o.lowerPtrToVoid(ptr_info.flags.alignment, ptr_info.flags.address_space)).toValue();
46314641 }
4632 const pointee_llvm_ty = try self.lowerType(pointee_type);
4642 const pointee_llvm_ty = try o.lowerType(pointee_type);
46334643 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
46344644 return self.buildAlloca(pointee_llvm_ty, alignment);
46354645}
......@@ -4644,7 +4654,7 @@ fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
46444654 return (try o.lowerPtrToVoid(ptr_info.flags.alignment, ptr_info.flags.address_space)).toValue();
46454655 }
46464656 if (self.ret_ptr != .none) return self.ret_ptr;
4647 const ret_llvm_ty = try self.lowerType(ret_ty);
4657 const ret_llvm_ty = try o.lowerType(ret_ty);
46484658 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
46494659 return self.buildAlloca(ret_llvm_ty, alignment);
46504660}
......@@ -4696,7 +4706,7 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
46964706
46974707 self.maybeMarkAllowZeroAccess(ptr_info);
46984708
4699 const len = try o.builder.intValue(try self.lowerType(.usize), operand_ty.abiSize(zcu));
4709 const len = try o.builder.intValue(try o.lowerType(.usize), operand_ty.abiSize(zcu));
47004710 _ = try self.wip.callMemSet(
47014711 dest_ptr,
47024712 ptr_ty.ptrAlignment(zcu).toLlvm(),
......@@ -4735,7 +4745,7 @@ fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
47354745
47364746 if (ptr_info.flags.vector_index != .none) {
47374747 const index_u32 = try o.builder.intValue(.i32, ptr_info.flags.vector_index);
4738 const vec_elem_ty = try fg.lowerType(elem_ty);
4748 const vec_elem_ty = try o.lowerType(elem_ty);
47394749 const vec_ty = try o.builder.vectorType(.normal, ptr_info.packed_offset.host_size, vec_elem_ty);
47404750
47414751 const loaded_vector = try fg.wip.load(access_kind, vec_ty, ptr, llvm_ptr_align, "");
......@@ -4753,7 +4763,7 @@ fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
47534763 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
47544764 const shift_amt = try o.builder.intValue(containing_int_ty, ptr_info.packed_offset.bit_offset);
47554765 const shifted_value = try fg.wip.bin(.lshr, containing_int, shift_amt, "");
4756 const elem_llvm_ty = try fg.lowerType(elem_ty);
4766 const elem_llvm_ty = try o.lowerType(elem_ty);
47574767
47584768 if (isByRef(elem_ty, zcu)) {
47594769 const result_align = elem_ty.abiAlignment(zcu).toLlvm();
......@@ -4813,7 +4823,7 @@ fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V
48134823fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48144824 _ = inst;
48154825 const o = self.object;
4816 const llvm_usize = try self.lowerType(.usize);
4826 const llvm_usize = try o.lowerType(.usize);
48174827 if (!target_util.supportsReturnAddress(self.object.zcu.getTarget(), self.ownerModule().optimize_mode)) {
48184828 // https://github.com/ziglang/zig/issues/11946
48194829 return o.builder.intValue(llvm_usize, 0);
......@@ -4825,7 +4835,7 @@ fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
48254835fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48264836 _ = inst;
48274837 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{.@"0"}, "");
4828 return self.wip.cast(.ptrtoint, result, try self.lowerType(.usize), "");
4838 return self.wip.cast(.ptrtoint, result, try self.object.lowerType(.usize), "");
48294839}
48304840
48314841fn airCmpxchg(
......@@ -4842,7 +4852,7 @@ fn airCmpxchg(
48424852 var expected_value = try self.resolveInst(extra.expected_value);
48434853 var new_value = try self.resolveInst(extra.new_value);
48444854 const operand_ty = ptr_ty.childType(zcu);
4845 const llvm_operand_ty = try self.lowerType(operand_ty);
4855 const llvm_operand_ty = try o.lowerType(operand_ty);
48464856 const llvm_abi_ty = try self.getAtomicAbiType(operand_ty, false);
48474857 if (llvm_abi_ty != .none) {
48484858 // operand needs widening and truncating
......@@ -4885,7 +4895,7 @@ fn airCmpxchg(
48854895 const non_null_bit = try self.wip.not(success_bit, "");
48864896
48874897 const payload_align = operand_ty.abiAlignment(zcu).toLlvm();
4888 const alloca_inst = try self.buildAlloca(try self.lowerType(optional_ty), payload_align);
4898 const alloca_inst = try self.buildAlloca(try o.lowerType(optional_ty), payload_align);
48894899
48904900 // Payload is always the first field at offset 0, so address is `alloca_inst`
48914901 _ = try self.wip.store(.normal, payload, alloca_inst, payload_align);
......@@ -4911,7 +4921,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
49114921 const op = toLlvmAtomicRmwBinOp(extra.op(), is_signed_int, is_float);
49124922 const ordering = toLlvmAtomicOrdering(extra.ordering());
49134923 const llvm_abi_ty = try self.getAtomicAbiType(operand_ty, op == .xchg);
4914 const llvm_operand_ty = try self.lowerType(operand_ty);
4924 const llvm_operand_ty = try o.lowerType(operand_ty);
49154925
49164926 const access_kind: Builder.MemoryAccessKind =
49174927 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
......@@ -4954,7 +4964,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
49544964 access_kind,
49554965 op,
49564966 ptr,
4957 try self.wip.cast(.ptrtoint, operand, try self.lowerType(.usize), ""),
4967 try self.wip.cast(.ptrtoint, operand, try o.lowerType(.usize), ""),
49584968 self.sync_scope,
49594969 ordering,
49604970 ptr_alignment,
......@@ -4963,7 +4973,8 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
49634973}
49644974
49654975fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4966 const zcu = self.object.zcu;
4976 const o = self.object;
4977 const zcu = o.zcu;
49674978 const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
49684979 const ptr = try self.resolveInst(atomic_load.ptr);
49694980 const ptr_ty = self.typeOf(atomic_load.ptr);
......@@ -4978,7 +4989,7 @@ fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V
49784989 Type.fromInterned(info.child).abiAlignment(zcu)).toLlvm();
49794990 const access_kind: Builder.MemoryAccessKind =
49804991 if (info.flags.is_volatile) .@"volatile" else .normal;
4981 const elem_llvm_ty = try self.lowerType(elem_ty);
4992 const elem_llvm_ty = try o.lowerType(elem_ty);
49824993
49834994 self.maybeMarkAllowZeroAccess(info);
49844995
......@@ -5135,7 +5146,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
51355146 const body_block = try self.wip.block(1, "InlineMemsetBody");
51365147 const end_block = try self.wip.block(1, "InlineMemsetEnd");
51375148
5138 const llvm_usize_ty = try self.lowerType(.usize);
5149 const llvm_usize_ty = try o.lowerType(.usize);
51395150 const end_ptr = switch (ptr_ty.ptrSize(zcu)) {
51405151 .slice => try self.ptraddScaled(
51415152 dest_ptr,
......@@ -5265,7 +5276,7 @@ fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.
52655276 assert(layout.tag_size != 0);
52665277 const union_ptr = try self.resolveInst(ty_op.operand);
52675278 if (isByRef(un_ty, zcu)) {
5268 const llvm_un_ty = try self.lowerType(un_ty);
5279 const llvm_un_ty = try o.lowerType(un_ty);
52695280 if (layout.payload_size == 0)
52705281 return self.wip.load(.normal, llvm_un_ty, union_ptr, .default, "");
52715282 const tag_index = @intFromBool(layout.tag_align.compare(.lt, layout.payload_align));
......@@ -5296,6 +5307,7 @@ fn airNeg(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo
52965307}
52975308
52985309fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic) Allocator.Error!Builder.Value {
5310 const o = self.object;
52995311 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
53005312 const inst_ty = self.typeOfIndex(inst);
53015313 const operand_ty = self.typeOf(ty_op.operand);
......@@ -5305,14 +5317,15 @@ fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)
53055317 .normal,
53065318 .none,
53075319 intrinsic,
5308 &.{try self.lowerType(operand_ty)},
5320 &.{try o.lowerType(operand_ty)},
53095321 &.{ operand, .false },
53105322 "",
53115323 );
5312 return self.wip.conv(.unsigned, result, try self.lowerType(inst_ty), "");
5324 return self.wip.conv(.unsigned, result, try o.lowerType(inst_ty), "");
53135325}
53145326
53155327fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic) Allocator.Error!Builder.Value {
5328 const o = self.object;
53165329 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
53175330 const inst_ty = self.typeOfIndex(inst);
53185331 const operand_ty = self.typeOf(ty_op.operand);
......@@ -5322,11 +5335,11 @@ fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)
53225335 .normal,
53235336 .none,
53245337 intrinsic,
5325 &.{try self.lowerType(operand_ty)},
5338 &.{try o.lowerType(operand_ty)},
53265339 &.{operand},
53275340 "",
53285341 );
5329 return self.wip.conv(.unsigned, result, try self.lowerType(inst_ty), "");
5342 return self.wip.conv(.unsigned, result, try o.lowerType(inst_ty), "");
53305343}
53315344
53325345fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
......@@ -5339,7 +5352,7 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
53395352
53405353 const inst_ty = self.typeOfIndex(inst);
53415354 var operand = try self.resolveInst(ty_op.operand);
5342 var llvm_operand_ty = try self.lowerType(operand_ty);
5355 var llvm_operand_ty = try o.lowerType(operand_ty);
53435356
53445357 if (bits % 16 == 8) {
53455358 // If not an even byte-multiple, we need zero-extend + shift-left 1 byte
......@@ -5360,7 +5373,7 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
53605373
53615374 const result =
53625375 try self.wip.callIntrinsic(.normal, .none, .bswap, &.{llvm_operand_ty}, &.{operand}, "");
5363 return self.wip.conv(.unsigned, result, try self.lowerType(inst_ty), "");
5376 return self.wip.conv(.unsigned, result, try o.lowerType(inst_ty), "");
53645377}
53655378
53665379fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
......@@ -5437,10 +5450,10 @@ fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
54375450 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
54385451 const operand = try self.resolveInst(un_op);
54395452 const slice_ty = self.typeOfIndex(inst);
5440 const slice_llvm_ty = try self.lowerType(slice_ty);
5453 const slice_llvm_ty = try o.lowerType(slice_ty);
54415454
54425455 // If operand is small (e.g. `u8`), then signedness becomes a problem -- GEP always treats the index as signed.
5443 const operand_usize = try self.wip.conv(.unsigned, operand, try self.lowerType(.usize), "");
5456 const operand_usize = try self.wip.conv(.unsigned, operand, try o.lowerType(.usize), "");
54445457
54455458 const error_name_table_ptr = try o.getErrorNameTable();
54465459 const error_name_ptr = try self.ptraddScaled(error_name_table_ptr.toValue(&o.builder), operand_usize, slice_ty.abiSize(zcu));
......@@ -5451,7 +5464,7 @@ fn airSplat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
54515464 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
54525465 const scalar = try self.resolveInst(ty_op.operand);
54535466 const vector_ty = self.typeOfIndex(inst);
5454 return self.wip.splatVector(try self.lowerType(vector_ty), scalar, "");
5467 return self.wip.splatVector(try self.object.lowerType(vector_ty), scalar, "");
54555468}
54565469
54575470fn airSelect(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
......@@ -5474,9 +5487,9 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
54745487 const operand = try fg.resolveInst(unwrapped.operand);
54755488 const mask = unwrapped.mask;
54765489 const operand_ty = fg.typeOf(unwrapped.operand);
5477 const llvm_operand_ty = try fg.lowerType(operand_ty);
5478 const llvm_result_ty = try fg.lowerType(unwrapped.result_ty);
5479 const llvm_elem_ty = try fg.lowerType(unwrapped.result_ty.childType(zcu));
5490 const llvm_operand_ty = try o.lowerType(operand_ty);
5491 const llvm_result_ty = try o.lowerType(unwrapped.result_ty);
5492 const llvm_elem_ty = try o.lowerType(unwrapped.result_ty.childType(zcu));
54805493 const llvm_poison_elem = try o.builder.poisonConst(llvm_elem_ty);
54815494 const llvm_poison_mask_elem = try o.builder.poisonConst(.i32);
54825495 const llvm_mask_ty = try o.builder.vectorType(.normal, @intCast(mask.len), .i32);
......@@ -5578,7 +5591,7 @@ fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
55785591 const unwrapped = fg.air.unwrapShuffleTwo(zcu, inst);
55795592
55805593 const mask = unwrapped.mask;
5581 const llvm_elem_ty = try fg.lowerType(unwrapped.result_ty.childType(zcu));
5594 const llvm_elem_ty = try o.lowerType(unwrapped.result_ty.childType(zcu));
55825595 const llvm_mask_ty = try o.builder.vectorType(.normal, @intCast(mask.len), .i32);
55835596 const llvm_poison_mask_elem = try o.builder.poisonConst(.i32);
55845597
......@@ -5670,7 +5683,7 @@ fn buildReducedCall(
56705683 accum_init: Builder.Value,
56715684) Allocator.Error!Builder.Value {
56725685 const o = self.object;
5673 const usize_ty = try self.lowerType(.usize);
5686 const usize_ty = try o.lowerType(.usize);
56745687 const llvm_vector_len = try o.builder.intValue(usize_ty, vector_len);
56755688 const llvm_result_ty = accum_init.typeOfWip(&self.wip);
56765689
......@@ -5730,9 +5743,9 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) A
57305743 const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
57315744 const operand = try self.resolveInst(reduce.operand);
57325745 const operand_ty = self.typeOf(reduce.operand);
5733 const llvm_operand_ty = try self.lowerType(operand_ty);
5746 const llvm_operand_ty = try o.lowerType(operand_ty);
57345747 const scalar_ty = self.typeOfIndex(inst);
5735 const llvm_scalar_ty = try self.lowerType(scalar_ty);
5748 const llvm_scalar_ty = try o.lowerType(scalar_ty);
57365749
57375750 switch (reduce.operation) {
57385751 .And, .Or, .Xor => return self.wip.callIntrinsic(.normal, .none, switch (reduce.operation) {
......@@ -5839,7 +5852,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
58395852 const result_ty = self.typeOfIndex(inst);
58405853 const len: usize = @intCast(result_ty.arrayLen(zcu));
58415854 const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra.items[ty_pl.payload..][0..len]);
5842 const llvm_result_ty = try self.lowerType(result_ty);
5855 const llvm_result_ty = try o.lowerType(result_ty);
58435856
58445857 switch (result_ty.zigTypeTag(zcu)) {
58455858 .vector => {
......@@ -5905,7 +5918,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
59055918 field_ptr_align.toLlvm(),
59065919 llvm_field_val,
59075920 field_ty.abiAlignment(zcu).toLlvm(),
5908 try o.builder.intValue(try self.lowerType(.usize), field_ty.abiSize(zcu)),
5921 try o.builder.intValue(try o.lowerType(.usize), field_ty.abiSize(zcu)),
59095922 .normal,
59105923 self.disable_intrinsics,
59115924 );
......@@ -5956,7 +5969,7 @@ fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
59565969 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
59575970 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
59585971 const union_ty = self.typeOfIndex(inst);
5959 const union_llvm_ty = try self.lowerType(union_ty);
5972 const union_llvm_ty = try o.lowerType(union_ty);
59605973 const union_obj = zcu.typeToUnion(union_ty).?;
59615974
59625975 assert(union_obj.layout != .@"packed");
......@@ -6046,8 +6059,7 @@ fn airAddrSpaceCast(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
60466059 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
60476060 const inst_ty = self.typeOfIndex(inst);
60486061 const operand = try self.resolveInst(ty_op.operand);
6049
6050 return self.wip.cast(.addrspacecast, operand, try self.lowerType(inst_ty), "");
6062 return self.wip.cast(.addrspacecast, operand, try self.object.lowerType(inst_ty), "");
60516063}
60526064
60536065fn workIntrinsic(
......@@ -6192,7 +6204,7 @@ fn loadTruncate(
61926204
61936205 const o = fg.object;
61946206 const zcu = o.zcu;
6195 const payload_llvm_ty = try fg.lowerType(payload_ty);
6207 const payload_llvm_ty = try o.lowerType(payload_ty);
61966208 const abi_size = payload_ty.abiSize(zcu);
61976209
61986210 const load_llvm_ty = if (payload_ty.isAbiInt(zcu))
......@@ -6220,7 +6232,7 @@ fn loadByRef(
62206232 access_kind: Builder.MemoryAccessKind,
62216233) Allocator.Error!Builder.Value {
62226234 const o = fg.object;
6223 const pointee_llvm_ty = try fg.lowerType(pointee_type);
6235 const pointee_llvm_ty = try o.lowerType(pointee_type);
62246236 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)
62256237 .max(pointee_type.abiAlignment(o.zcu)).toLlvm();
62266238 const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align);
......@@ -6230,7 +6242,7 @@ fn loadByRef(
62306242 result_align,
62316243 ptr,
62326244 ptr_alignment,
6233 try o.builder.intValue(try fg.lowerType(.usize), size_bytes),
6245 try o.builder.intValue(try o.lowerType(.usize), size_bytes),
62346246 access_kind,
62356247 fg.disable_intrinsics,
62366248 );
......@@ -6274,7 +6286,7 @@ fn storeFull(
62746286
62756287 if (info.flags.vector_index != .none) {
62766288 const index_u32 = try o.builder.intValue(.i32, info.flags.vector_index);
6277 const vec_elem_ty = try self.lowerType(elem_ty);
6289 const vec_elem_ty = try o.lowerType(elem_ty);
62786290 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
62796291
62806292 const loaded_vector = try self.wip.load(.normal, vec_ty, ptr, ptr_alignment, "");
......@@ -6343,7 +6355,7 @@ fn storeFull(
63436355 ptr_alignment,
63446356 elem,
63456357 elem_ty.abiAlignment(zcu).toLlvm(),
6346 try o.builder.intValue(try self.lowerType(.usize), elem_ty.abiSize(zcu)),
6358 try o.builder.intValue(try o.lowerType(.usize), elem_ty.abiSize(zcu)),
63476359 access_kind,
63486360 self.disable_intrinsics,
63496361 );
......@@ -6370,7 +6382,7 @@ fn store(
63706382 elem,
63716383 elem_ty.abiAlignment(zcu).toLlvm(),
63726384 try o.builder.intValue(
6373 try fg.lowerType(.usize),
6385 try o.lowerType(.usize),
63746386 elem_ty.abiSize(zcu),
63756387 ),
63766388 .normal,
......@@ -6391,7 +6403,7 @@ fn store(
63916403fn valgrindMarkUndef(fg: *FuncGen, ptr: Builder.Value, len: Builder.Value) Allocator.Error!void {
63926404 const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
63936405 const o = fg.object;
6394 const usize_ty = try fg.lowerType(.usize);
6406 const usize_ty = try o.lowerType(.usize);
63956407 const zero = try o.builder.intValue(usize_ty, 0);
63966408 const req = try o.builder.intValue(usize_ty, VG_USERREQ__MAKE_MEM_UNDEFINED);
63976409 const ptr_as_usize = try fg.wip.cast(.ptrtoint, ptr, usize_ty, "");
......@@ -6413,7 +6425,7 @@ fn valgrindClientRequest(
64136425 const target = zcu.getTarget();
64146426 if (!target_util.hasValgrindSupport(target, .stage2_llvm)) return default_value;
64156427
6416 const llvm_usize = try fg.lowerType(.usize);
6428 const llvm_usize = try o.lowerType(.usize);
64176429 const usize_alignment = Type.usize.abiAlignment(zcu).toLlvm();
64186430
64196431 const array_llvm_ty = try o.builder.arrayType(6, llvm_usize);
......@@ -7261,8 +7273,9 @@ fn getAtomicAbiType(fg: *const FuncGen, ty: Type, is_rmw_xchg: bool) Allocator.E
72617273
72627274fn ptraddConst(fg: *FuncGen, ptr: Builder.Value, offset: u64) Allocator.Error!Builder.Value {
72637275 if (offset == 0) return ptr;
7264 const llvm_usize_ty = try fg.lowerType(.usize);
7265 const offset_val = try fg.object.builder.intValue(llvm_usize_ty, offset);
7276 const o = fg.object;
7277 const llvm_usize_ty = try o.lowerType(.usize);
7278 const offset_val = try o.builder.intValue(llvm_usize_ty, offset);
72667279 return fg.wip.gep(.inbounds, .i8, ptr, &.{offset_val}, "");
72677280}
72687281fn ptraddScaled(fg: *FuncGen, ptr: Builder.Value, index: Builder.Value, scale: u64) Allocator.Error!Builder.Value {