authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-23 18:59:18+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-23 19:40:07+01:00
log2c67a1ecd2d6680631e13c8a7bc56915d633eb29
tree35b70968e7ac345ff8cd2725edcc27fa58133894
parentf644263a6c2df79f907af5fb40b2bb0586ea436f

llvm: optimize i32 constants


2 files changed, 26 insertions(+), 35 deletions(-)

src/codegen/llvm.zig+25-32
...@@ -896,19 +896,22 @@ pub const Object = struct {...@@ -896,19 +896,22 @@ pub const Object = struct {
896 .{ .optimized = comp.root_mod.optimize_mode != .Debug },896 .{ .optimized = comp.root_mod.optimize_mode != .Debug },
897 );897 );
898898
899 const i32_2 = try builder.intConst(.i32, 2);
900 const i32_3 = try builder.intConst(.i32, 3);
899 const debug_info_version = try builder.debugModuleFlag(901 const debug_info_version = try builder.debugModuleFlag(
900 try builder.debugConstant(try builder.intConst(.i32, 2)),902 try builder.debugConstant(i32_2),
901 try builder.metadataString("Debug Info Version"),903 try builder.metadataString("Debug Info Version"),
902 try builder.debugConstant(try builder.intConst(.i32, 3)),904 try builder.debugConstant(i32_3),
903 );905 );
904906
905 switch (comp.config.debug_format) {907 switch (comp.config.debug_format) {
906 .strip => unreachable,908 .strip => unreachable,
907 .dwarf => |f| {909 .dwarf => |f| {
910 const i32_4 = try builder.intConst(.i32, 4);
908 const dwarf_version = try builder.debugModuleFlag(911 const dwarf_version = try builder.debugModuleFlag(
909 try builder.debugConstant(try builder.intConst(.i32, 2)),912 try builder.debugConstant(i32_2),
910 try builder.metadataString("Dwarf Version"),913 try builder.metadataString("Dwarf Version"),
911 try builder.debugConstant(try builder.intConst(.i32, 4)),914 try builder.debugConstant(i32_4),
912 );915 );
913 switch (f) {916 switch (f) {
914 .@"32" => {917 .@"32" => {
...@@ -919,9 +922,9 @@ pub const Object = struct {...@@ -919,9 +922,9 @@ pub const Object = struct {
919 },922 },
920 .@"64" => {923 .@"64" => {
921 const dwarf64 = try builder.debugModuleFlag(924 const dwarf64 = try builder.debugModuleFlag(
922 try builder.debugConstant(try builder.intConst(.i32, 2)),925 try builder.debugConstant(i32_2),
923 try builder.metadataString("DWARF64"),926 try builder.metadataString("DWARF64"),
924 try builder.debugConstant(try builder.intConst(.i32, 1)),927 try builder.debugConstant(.@"1"),
925 );928 );
926 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{929 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{
927 debug_info_version,930 debug_info_version,
...@@ -933,9 +936,9 @@ pub const Object = struct {...@@ -933,9 +936,9 @@ pub const Object = struct {
933 },936 },
934 .code_view => {937 .code_view => {
935 const code_view = try builder.debugModuleFlag(938 const code_view = try builder.debugModuleFlag(
936 try builder.debugConstant(try builder.intConst(.i32, 2)),939 try builder.debugConstant(i32_2),
937 try builder.metadataString("CodeView"),940 try builder.metadataString("CodeView"),
938 try builder.debugConstant(try builder.intConst(.i32, 1)),941 try builder.debugConstant(.@"1"),
939 );942 );
940 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{943 try builder.debugNamed(try builder.metadataString("llvm.module.flags"), &.{
941 debug_info_version,944 debug_info_version,
...@@ -4265,7 +4268,7 @@ pub const Object = struct {...@@ -4265,7 +4268,7 @@ pub const Object = struct {
4265 const err_align = err_int_ty.abiAlignment(mod);4268 const err_align = err_int_ty.abiAlignment(mod);
4266 const index: u32 = if (payload_align.compare(.gt, err_align)) 2 else 1;4269 const index: u32 = if (payload_align.compare(.gt, err_align)) 2 else 1;
4267 return o.builder.gepConst(.inbounds, try o.lowerType(eu_ty), parent_ptr, null, &.{4270 return o.builder.gepConst(.inbounds, try o.lowerType(eu_ty), parent_ptr, null, &.{
4268 try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, index),4271 .@"0", try o.builder.intConst(.i32, index),
4269 });4272 });
4270 },4273 },
4271 .opt_payload => |opt_ptr| {4274 .opt_payload => |opt_ptr| {
...@@ -4281,9 +4284,7 @@ pub const Object = struct {...@@ -4281,9 +4284,7 @@ pub const Object = struct {
4281 return parent_ptr;4284 return parent_ptr;
4282 }4285 }
42834286
4284 return o.builder.gepConst(.inbounds, try o.lowerType(opt_ty), parent_ptr, null, &.{4287 return o.builder.gepConst(.inbounds, try o.lowerType(opt_ty), parent_ptr, null, &.{ .@"0", .@"0" });
4285 try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, 0),
4286 });
4287 },4288 },
4288 .comptime_field => unreachable,4289 .comptime_field => unreachable,
4289 .elem => |elem_ptr| {4290 .elem => |elem_ptr| {
...@@ -4314,7 +4315,7 @@ pub const Object = struct {...@@ -4314,7 +4315,7 @@ pub const Object = struct {
43144315
4315 const parent_llvm_ty = try o.lowerType(parent_ty);4316 const parent_llvm_ty = try o.lowerType(parent_ty);
4316 return o.builder.gepConst(.inbounds, parent_llvm_ty, parent_ptr, null, &.{4317 return o.builder.gepConst(.inbounds, parent_llvm_ty, parent_ptr, null, &.{
4317 try o.builder.intConst(.i32, 0),4318 .@"0",
4318 try o.builder.intConst(.i32, @intFromBool(4319 try o.builder.intConst(.i32, @intFromBool(
4319 layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align),4320 layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align),
4320 )),4321 )),
...@@ -4340,7 +4341,7 @@ pub const Object = struct {...@@ -4340,7 +4341,7 @@ pub const Object = struct {
4340 parent_ptr,4341 parent_ptr,
4341 null,4342 null,
4342 if (o.llvmFieldIndex(parent_ty, field_index)) |llvm_field_index| &.{4343 if (o.llvmFieldIndex(parent_ty, field_index)) |llvm_field_index| &.{
4343 try o.builder.intConst(.i32, 0),4344 .@"0",
4344 try o.builder.intConst(.i32, llvm_field_index),4345 try o.builder.intConst(.i32, llvm_field_index),
4345 } else &.{4346 } else &.{
4346 try o.builder.intConst(.i32, @intFromBool(4347 try o.builder.intConst(.i32, @intFromBool(
...@@ -4353,7 +4354,7 @@ pub const Object = struct {...@@ -4353,7 +4354,7 @@ pub const Object = struct {
4353 assert(parent_ty.isSlice(mod));4354 assert(parent_ty.isSlice(mod));
4354 const parent_llvm_ty = try o.lowerType(parent_ty);4355 const parent_llvm_ty = try o.lowerType(parent_ty);
4355 return o.builder.gepConst(.inbounds, parent_llvm_ty, parent_ptr, null, &.{4356 return o.builder.gepConst(.inbounds, parent_llvm_ty, parent_ptr, null, &.{
4356 try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, field_index),4357 .@"0", try o.builder.intConst(.i32, field_index),
4357 });4358 });
4358 },4359 },
4359 else => unreachable,4360 else => unreachable,
...@@ -7842,7 +7843,7 @@ pub const FuncGen = struct {...@@ -7842,7 +7843,7 @@ pub const FuncGen = struct {
7842 .none,7843 .none,
7843 if (scalar_ty.isSignedInt(mod)) .@"smul.fix.sat" else .@"umul.fix.sat",7844 if (scalar_ty.isSignedInt(mod)) .@"smul.fix.sat" else .@"umul.fix.sat",
7844 &.{try o.lowerType(inst_ty)},7845 &.{try o.lowerType(inst_ty)},
7845 &.{ lhs, rhs, try o.builder.intValue(.i32, 0) },7846 &.{ lhs, rhs, .@"0" },
7846 "",7847 "",
7847 );7848 );
7848 }7849 }
...@@ -8165,7 +8166,6 @@ pub const FuncGen = struct {...@@ -8165,7 +8166,6 @@ pub const FuncGen = struct {
81658166
8166 const libc_fn = try self.getLibcFunction(fn_name, &.{ scalar_llvm_ty, scalar_llvm_ty }, .i32);8167 const libc_fn = try self.getLibcFunction(fn_name, &.{ scalar_llvm_ty, scalar_llvm_ty }, .i32);
81678168
8168 const zero = try o.builder.intConst(.i32, 0);
8169 const int_cond: Builder.IntegerCondition = switch (pred) {8169 const int_cond: Builder.IntegerCondition = switch (pred) {
8170 .eq => .eq,8170 .eq => .eq,
8171 .neq => .ne,8171 .neq => .ne,
...@@ -8182,7 +8182,7 @@ pub const FuncGen = struct {...@@ -8182,7 +8182,7 @@ pub const FuncGen = struct {
8182 const init = try o.builder.poisonValue(vector_result_ty);8182 const init = try o.builder.poisonValue(vector_result_ty);
8183 const result = try self.buildElementwiseCall(libc_fn, &params, init, vec_len);8183 const result = try self.buildElementwiseCall(libc_fn, &params, init, vec_len);
81848184
8185 const zero_vector = try o.builder.splatValue(vector_result_ty, zero);8185 const zero_vector = try o.builder.splatValue(vector_result_ty, .@"0");
8186 return self.wip.icmp(int_cond, result, zero_vector, "");8186 return self.wip.icmp(int_cond, result, zero_vector, "");
8187 }8187 }
81888188
...@@ -8195,7 +8195,7 @@ pub const FuncGen = struct {...@@ -8195,7 +8195,7 @@ pub const FuncGen = struct {
8195 &params,8195 &params,
8196 "",8196 "",
8197 );8197 );
8198 return self.wip.icmp(int_cond, result, zero.toValue(), "");8198 return self.wip.icmp(int_cond, result, .@"0", "");
8199 }8199 }
82008200
8201 const FloatOp = enum {8201 const FloatOp = enum {
...@@ -9021,18 +9021,14 @@ pub const FuncGen = struct {...@@ -9021,18 +9021,14 @@ pub const FuncGen = struct {
9021 // https://github.com/ziglang/zig/issues/119469021 // https://github.com/ziglang/zig/issues/11946
9022 return o.builder.intValue(llvm_usize, 0);9022 return o.builder.intValue(llvm_usize, 0);
9023 }9023 }
9024 const result = try self.wip.callIntrinsic(.normal, .none, .returnaddress, &.{}, &.{9024 const result = try self.wip.callIntrinsic(.normal, .none, .returnaddress, &.{}, &.{.@"0"}, "");
9025 try o.builder.intValue(.i32, 0),
9026 }, "");
9027 return self.wip.cast(.ptrtoint, result, llvm_usize, "");9025 return self.wip.cast(.ptrtoint, result, llvm_usize, "");
9028 }9026 }
90299027
9030 fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {9028 fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
9031 _ = inst;9029 _ = inst;
9032 const o = self.dg.object;9030 const o = self.dg.object;
9033 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{9031 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{.@"0"}, "");
9034 try o.builder.intValue(.i32, 0),
9035 }, "");
9036 return self.wip.cast(.ptrtoint, result, try o.lowerType(Type.usize), "");9032 return self.wip.cast(.ptrtoint, result, try o.lowerType(Type.usize), "");
9037 }9033 }
90389034
...@@ -10116,7 +10112,6 @@ pub const FuncGen = struct {...@@ -10116,7 +10112,6 @@ pub const FuncGen = struct {
10116 const field_align = mod.unionFieldNormalAlignment(union_obj, extra.field_index);10112 const field_align = mod.unionFieldNormalAlignment(union_obj, extra.field_index);
10117 const llvm_usize = try o.lowerType(Type.usize);10113 const llvm_usize = try o.lowerType(Type.usize);
10118 const usize_zero = try o.builder.intValue(llvm_usize, 0);10114 const usize_zero = try o.builder.intValue(llvm_usize, 0);
10119 const i32_zero = try o.builder.intValue(.i32, 0);
1012010115
10121 const llvm_union_ty = t: {10116 const llvm_union_ty = t: {
10122 const payload_ty = p: {10117 const payload_ty = p: {
...@@ -10155,7 +10150,7 @@ pub const FuncGen = struct {...@@ -10155,7 +10150,7 @@ pub const FuncGen = struct {
10155 .flags = .{ .alignment = field_align },10150 .flags = .{ .alignment = field_align },
10156 });10151 });
10157 if (layout.tag_size == 0) {10152 if (layout.tag_size == 0) {
10158 const indices = [3]Builder.Value{ usize_zero, i32_zero, i32_zero };10153 const indices = [3]Builder.Value{ usize_zero, .@"0", .@"0" };
10159 const len: usize = if (field_size == layout.payload_size) 2 else 3;10154 const len: usize = if (field_size == layout.payload_size) 2 else 3;
10160 const field_ptr =10155 const field_ptr =
10161 try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, indices[0..len], "");10156 try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, indices[0..len], "");
...@@ -10165,11 +10160,9 @@ pub const FuncGen = struct {...@@ -10165,11 +10160,9 @@ pub const FuncGen = struct {
1016510160
10166 {10161 {
10167 const payload_index = @intFromBool(layout.tag_align.compare(.gte, layout.payload_align));10162 const payload_index = @intFromBool(layout.tag_align.compare(.gte, layout.payload_align));
10168 const indices: [3]Builder.Value =10163 const indices: [3]Builder.Value = .{ usize_zero, try o.builder.intValue(.i32, payload_index), .@"0" };
10169 .{ usize_zero, try o.builder.intValue(.i32, payload_index), i32_zero };
10170 const len: usize = if (field_size == layout.payload_size) 2 else 3;10164 const len: usize = if (field_size == layout.payload_size) 2 else 3;
10171 const field_ptr =10165 const field_ptr = try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, indices[0..len], "");
10172 try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, indices[0..len], "");
10173 try self.store(field_ptr, field_ptr_ty, llvm_payload, .none);10166 try self.store(field_ptr, field_ptr_ty, llvm_payload, .none);
10174 }10167 }
10175 {10168 {
...@@ -10275,7 +10268,7 @@ pub const FuncGen = struct {...@@ -10275,7 +10268,7 @@ pub const FuncGen = struct {
1027510268
10276 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;10269 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
10277 const dimension = pl_op.payload;10270 const dimension = pl_op.payload;
10278 if (dimension >= 3) return o.builder.intValue(.i32, 1);10271 if (dimension >= 3) return .@"1";
1027910272
10280 // Fetch the dispatch pointer, which points to this structure:10273 // Fetch the dispatch pointer, which points to this structure:
10281 // https://github.com/RadeonOpenCompute/ROCR-Runtime/blob/adae6c61e10d371f7cbc3d0e94ae2c070cab18a4/src/inc/hsa.h#L291310274 // https://github.com/RadeonOpenCompute/ROCR-Runtime/blob/adae6c61e10d371f7cbc3d0e94ae2c070cab18a4/src/inc/hsa.h#L2913
src/codegen/llvm/Builder.zig+1-3
...@@ -5533,9 +5533,7 @@ pub const WipFunction = struct {...@@ -5533,9 +5533,7 @@ pub const WipFunction = struct {
5533 name: []const u8,5533 name: []const u8,
5534 ) Allocator.Error!Value {5534 ) Allocator.Error!Value {
5535 assert(ty.isStruct(self.builder));5535 assert(ty.isStruct(self.builder));
5536 return self.gep(.inbounds, ty, base, &.{5536 return self.gep(.inbounds, ty, base, &.{ .@"0", try self.builder.intValue(.i32, index) }, name);
5537 try self.builder.intValue(.i32, 0), try self.builder.intValue(.i32, index),
5538 }, name);
5539 }5537 }
55405538
5541 pub fn conv(5539 pub fn conv(