authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-01 23:00:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-01 23:00:57-07:00
log9e4091200e165db808216ad2a340c6ed4f18c494
tree3ee2ffa80e472edaf163f685e44fd4294842fb98
parentaa79cc10aa38c2c3641d5a430aa33b8bfa6a3d61

stage1: avoid using LLVMGetElementType


1 files changed, 17 insertions(+), 14 deletions(-)

src/stage1/codegen.cpp+17-14
...@@ -916,9 +916,7 @@ static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMTypeRef elem_llvm_ty, LLVMV...@@ -916,9 +916,7 @@ static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMTypeRef elem_llvm_ty, LLVMV
916{916{
917 LLVMValueRef result = LLVMBuildLoad2(g->builder, elem_llvm_ty, ptr, name);917 LLVMValueRef result = LLVMBuildLoad2(g->builder, elem_llvm_ty, ptr, name);
918 if (is_volatile) LLVMSetVolatile(result, true);918 if (is_volatile) LLVMSetVolatile(result, true);
919 if (alignment == 0) {919 if (alignment != 0) {
920 LLVMSetAlignment(result, LLVMABIAlignmentOfType(g->target_data_ref, LLVMGetElementType(LLVMTypeOf(ptr))));
921 } else {
922 LLVMSetAlignment(result, alignment);920 LLVMSetAlignment(result, alignment);
923 }921 }
924 return result;922 return result;
...@@ -5129,7 +5127,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI...@@ -5129,7 +5127,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
51295127
5130 // Store a zero in the awaiter's result ptr to indicate we do not need a copy made.5128 // Store a zero in the awaiter's result ptr to indicate we do not need a copy made.
5131 LLVMValueRef awaiter_ret_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_ret_start + 1, "");5129 LLVMValueRef awaiter_ret_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_ret_start + 1, "");
5132 LLVMValueRef zero_ptr = LLVMConstNull(LLVMGetElementType(LLVMTypeOf(awaiter_ret_ptr)));5130 LLVMValueRef zero_ptr = LLVMConstNull(LLVMGetGEPSourceElementType(awaiter_ret_ptr));
5133 LLVMBuildStore(g->builder, zero_ptr, awaiter_ret_ptr);5131 LLVMBuildStore(g->builder, zero_ptr, awaiter_ret_ptr);
5134 }5132 }
51355133
...@@ -5147,7 +5145,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI...@@ -5147,7 +5145,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
51475145
5148 LLVMValueRef fn_ptr_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_fn_ptr_index, "");5146 LLVMValueRef fn_ptr_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_fn_ptr_index, "");
5149 LLVMValueRef bitcasted_fn_val = LLVMBuildBitCast(g->builder, fn_val,5147 LLVMValueRef bitcasted_fn_val = LLVMBuildBitCast(g->builder, fn_val,
5150 LLVMGetElementType(LLVMTypeOf(fn_ptr_ptr)), "");5148 LLVMGetGEPSourceElementType(fn_ptr_ptr), "");
5151 LLVMBuildStore(g->builder, bitcasted_fn_val, fn_ptr_ptr);5149 LLVMBuildStore(g->builder, bitcasted_fn_val, fn_ptr_ptr);
51525150
5153 LLVMValueRef resume_index_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_resume_index, "");5151 LLVMValueRef resume_index_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_resume_index, "");
...@@ -5266,8 +5264,8 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI...@@ -5266,8 +5264,8 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
5266 size_t field_count = arg_calc.field_index;5264 size_t field_count = arg_calc.field_index;
52675265
5268 LLVMTypeRef *field_types = heap::c_allocator.allocate_nonzero<LLVMTypeRef>(field_count);5266 LLVMTypeRef *field_types = heap::c_allocator.allocate_nonzero<LLVMTypeRef>(field_count);
5269 LLVMGetStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc)), field_types);5267 LLVMGetStructElementTypes(frame_struct_llvm_ty, field_types);
5270 assert(LLVMCountStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc))) == arg_calc_start.field_index);5268 assert(LLVMCountStructElementTypes(frame_struct_llvm_ty) == arg_calc_start.field_index);
52715269
5272 arg_calc = arg_calc_start;5270 arg_calc = arg_calc_start;
5273 for (size_t arg_i = 0; arg_i < gen_param_values.length; arg_i += 1) {5271 for (size_t arg_i = 0; arg_i < gen_param_values.length; arg_i += 1) {
...@@ -5648,7 +5646,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5648,7 +5646,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5648 size_t param_index = 0;5646 size_t param_index = 0;
5649 LLVMTypeRef *param_types = heap::c_allocator.allocate<LLVMTypeRef>(input_and_output_count);5647 LLVMTypeRef *param_types = heap::c_allocator.allocate<LLVMTypeRef>(input_and_output_count);
5650 LLVMValueRef *param_values = heap::c_allocator.allocate<LLVMValueRef>(input_and_output_count);5648 LLVMValueRef *param_values = heap::c_allocator.allocate<LLVMValueRef>(input_and_output_count);
5651 bool *param_needs_attr = heap::c_allocator.allocate<bool>(input_and_output_count);5649 LLVMTypeRef *param_needs_attr = heap::c_allocator.allocate<LLVMTypeRef>(input_and_output_count);
5652 for (size_t i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) {5650 for (size_t i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) {
5653 AsmOutput *asm_output = asm_expr->output_list.at(i);5651 AsmOutput *asm_output = asm_expr->output_list.at(i);
5654 bool is_return = (asm_output->return_type != nullptr);5652 bool is_return = (asm_output->return_type != nullptr);
...@@ -5664,7 +5662,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5664,7 +5662,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5664 buf_appendf(&constraint_buf, "=%s", buf_ptr(asm_output->constraint) + 1);5662 buf_appendf(&constraint_buf, "=%s", buf_ptr(asm_output->constraint) + 1);
5665 } else {5663 } else {
5666 buf_appendf(&constraint_buf, "=*%s", buf_ptr(asm_output->constraint) + 1);5664 buf_appendf(&constraint_buf, "=*%s", buf_ptr(asm_output->constraint) + 1);
5667 param_needs_attr[param_index] = true;5665 ZigVar *variable = instruction->output_vars[i];
5666 param_needs_attr[param_index] = get_llvm_type(g, variable->var_type);
5668 }5667 }
5669 if (total_index + 1 < total_constraint_count) {5668 if (total_index + 1 < total_constraint_count) {
5670 buf_append_char(&constraint_buf, ',');5669 buf_append_char(&constraint_buf, ',');
...@@ -5690,6 +5689,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5690,6 +5689,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5690 ZigType *const type = ir_input->value->type;5689 ZigType *const type = ir_input->value->type;
5691 LLVMTypeRef type_ref = get_llvm_type(g, type);5690 LLVMTypeRef type_ref = get_llvm_type(g, type);
5692 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);5691 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);
5692 LLVMTypeRef elem_type_ref = nullptr;
5693 // Handle integers of non pot bitsize by widening them.5693 // Handle integers of non pot bitsize by widening them.
5694 if (type->id == ZigTypeIdInt) {5694 if (type->id == ZigTypeIdInt) {
5695 const size_t bitsize = type->data.integral.bit_count;5695 const size_t bitsize = type->data.integral.bit_count;
...@@ -5701,6 +5701,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5701,6 +5701,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5701 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);5701 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);
5702 }5702 }
5703 } else if (handle_is_ptr(g, type)) {5703 } else if (handle_is_ptr(g, type)) {
5704 elem_type_ref = type_ref;
5704 ZigType *gen_type = get_pointer_to_type(g, type, true);5705 ZigType *gen_type = get_pointer_to_type(g, type, true);
5705 type_ref = get_llvm_type(g, gen_type);5706 type_ref = get_llvm_type(g, gen_type);
5706 }5707 }
...@@ -5708,7 +5709,9 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5708,7 +5709,9 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5708 param_types[param_index] = type_ref;5709 param_types[param_index] = type_ref;
5709 param_values[param_index] = value_ref;5710 param_values[param_index] = value_ref;
5710 // In the case of indirect inputs, LLVM requires the callsite to have an elementtype(<ty>) attribute.5711 // In the case of indirect inputs, LLVM requires the callsite to have an elementtype(<ty>) attribute.
5711 param_needs_attr[param_index] = buf_ptr(asm_input->constraint)[0] == '*';5712 if (buf_ptr(asm_input->constraint)[0] == '*') {
5713 param_needs_attr[param_index] = elem_type_ref;
5714 }
5712 }5715 }
5713 for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {5716 for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {
5714 Buf *clobber_buf = asm_expr->clobber_list.at(i);5717 Buf *clobber_buf = asm_expr->clobber_list.at(i);
...@@ -5756,8 +5759,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A...@@ -5756,8 +5759,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
5756 LLVMValueRef built_call = LLVMBuildCall2(g->builder, LLVMGlobalGetValueType(asm_fn), asm_fn, param_values, (unsigned)input_and_output_count, "");5759 LLVMValueRef built_call = LLVMBuildCall2(g->builder, LLVMGlobalGetValueType(asm_fn), asm_fn, param_values, (unsigned)input_and_output_count, "");
57575760
5758 for (size_t i = 0; i < input_and_output_count; i += 1) {5761 for (size_t i = 0; i < input_and_output_count; i += 1) {
5759 if (param_needs_attr[i]) {5762 if (param_needs_attr[i] != nullptr) {
5760 LLVMTypeRef elem_ty = LLVMGetElementType(param_types[i]);5763 LLVMTypeRef elem_ty = param_needs_attr[i];
5761 ZigLLVMSetCallElemTypeAttr(built_call, i, elem_ty);5764 ZigLLVMSetCallElemTypeAttr(built_call, i, elem_ty);
5762 }5765 }
5763 }5766 }
...@@ -7665,7 +7668,7 @@ static LLVMValueRef ir_render_await(CodeGen *g, Stage1Air *executable, Stage1Air...@@ -7665,7 +7668,7 @@ static LLVMValueRef ir_render_await(CodeGen *g, Stage1Air *executable, Stage1Air
7665 target_frame_ptr, frame_ret_start + 1, "");7668 target_frame_ptr, frame_ret_start + 1, "");
7666 if (result_loc == nullptr) {7669 if (result_loc == nullptr) {
7667 // no copy needed7670 // no copy needed
7668 LLVMBuildStore(g->builder, LLVMConstNull(LLVMGetElementType(LLVMTypeOf(awaiter_ret_ptr_ptr))),7671 LLVMBuildStore(g->builder, LLVMConstNull(LLVMGetGEPSourceElementType(awaiter_ret_ptr_ptr)),
7669 awaiter_ret_ptr_ptr);7672 awaiter_ret_ptr_ptr);
7670 } else {7673 } else {
7671 LLVMBuildStore(g->builder, result_loc, awaiter_ret_ptr_ptr);7674 LLVMBuildStore(g->builder, result_loc, awaiter_ret_ptr_ptr);
...@@ -9440,7 +9443,7 @@ static void do_code_gen(CodeGen *g) {...@@ -9440,7 +9443,7 @@ static void do_code_gen(CodeGen *g) {
9440 get_llvm_type(g, get_fn_frame_type(g, g->cur_fn)),9443 get_llvm_type(g, get_fn_frame_type(g, g->cur_fn)),
9441 g->cur_frame_ptr,9444 g->cur_frame_ptr,
9442 frame_index_trace_arg(g, fn_type_id->return_type), "");9445 frame_index_trace_arg(g, fn_type_id->return_type), "");
9443 LLVMValueRef zero_ptr = LLVMConstNull(LLVMGetElementType(LLVMTypeOf(trace_ptr_ptr)));9446 LLVMValueRef zero_ptr = LLVMConstNull(LLVMGetGEPSourceElementType(trace_ptr_ptr));
9444 LLVMBuildStore(g->builder, zero_ptr, trace_ptr_ptr);9447 LLVMBuildStore(g->builder, zero_ptr, trace_ptr_ptr);
9445 }9448 }
94469449