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
916916{
917917 LLVMValueRef result = LLVMBuildLoad2(g->builder, elem_llvm_ty, ptr, name);
918918 if (is_volatile) LLVMSetVolatile(result, true);
919 if (alignment == 0) {
920 LLVMSetAlignment(result, LLVMABIAlignmentOfType(g->target_data_ref, LLVMGetElementType(LLVMTypeOf(ptr))));
921 } else {
919 if (alignment != 0) {
922920 LLVMSetAlignment(result, alignment);
923921 }
924922 return result;
......@@ -5129,7 +5127,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
51295127
51305128 // Store a zero in the awaiter's result ptr to indicate we do not need a copy made.
51315129 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));
51335131 LLVMBuildStore(g->builder, zero_ptr, awaiter_ret_ptr);
51345132 }
51355133
......@@ -5147,7 +5145,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, Stage1Air *executable, Stage1AirI
51475145
51485146 LLVMValueRef fn_ptr_ptr = LLVMBuildStructGEP2(g->builder, frame_struct_llvm_ty, frame_result_loc, frame_fn_ptr_index, "");
51495147 LLVMValueRef bitcasted_fn_val = LLVMBuildBitCast(g->builder, fn_val,
5150 LLVMGetElementType(LLVMTypeOf(fn_ptr_ptr)), "");
5148 LLVMGetGEPSourceElementType(fn_ptr_ptr), "");
51515149 LLVMBuildStore(g->builder, bitcasted_fn_val, fn_ptr_ptr);
51525150
51535151 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
52665264 size_t field_count = arg_calc.field_index;
52675265
52685266 LLVMTypeRef *field_types = heap::c_allocator.allocate_nonzero<LLVMTypeRef>(field_count);
5269 LLVMGetStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc)), field_types);
5270 assert(LLVMCountStructElementTypes(LLVMGetElementType(LLVMTypeOf(frame_result_loc))) == arg_calc_start.field_index);
5267 LLVMGetStructElementTypes(frame_struct_llvm_ty, field_types);
5268 assert(LLVMCountStructElementTypes(frame_struct_llvm_ty) == arg_calc_start.field_index);
52715269
52725270 arg_calc = arg_calc_start;
52735271 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
56485646 size_t param_index = 0;
56495647 LLVMTypeRef *param_types = heap::c_allocator.allocate<LLVMTypeRef>(input_and_output_count);
56505648 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);
56525650 for (size_t i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) {
56535651 AsmOutput *asm_output = asm_expr->output_list.at(i);
56545652 bool is_return = (asm_output->return_type != nullptr);
......@@ -5664,7 +5662,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
56645662 buf_appendf(&constraint_buf, "=%s", buf_ptr(asm_output->constraint) + 1);
56655663 } else {
56665664 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);
56685667 }
56695668 if (total_index + 1 < total_constraint_count) {
56705669 buf_append_char(&constraint_buf, ',');
......@@ -5690,6 +5689,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
56905689 ZigType *const type = ir_input->value->type;
56915690 LLVMTypeRef type_ref = get_llvm_type(g, type);
56925691 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);
5692 LLVMTypeRef elem_type_ref = nullptr;
56935693 // Handle integers of non pot bitsize by widening them.
56945694 if (type->id == ZigTypeIdInt) {
56955695 const size_t bitsize = type->data.integral.bit_count;
......@@ -5701,6 +5701,7 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
57015701 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);
57025702 }
57035703 } else if (handle_is_ptr(g, type)) {
5704 elem_type_ref = type_ref;
57045705 ZigType *gen_type = get_pointer_to_type(g, type, true);
57055706 type_ref = get_llvm_type(g, gen_type);
57065707 }
......@@ -5708,7 +5709,9 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
57085709 param_types[param_index] = type_ref;
57095710 param_values[param_index] = value_ref;
57105711 // 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 }
57125715 }
57135716 for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1, total_index += 1) {
57145717 Buf *clobber_buf = asm_expr->clobber_list.at(i);
......@@ -5756,8 +5759,8 @@ static LLVMValueRef ir_render_asm_gen(CodeGen *g, Stage1Air *executable, Stage1A
57565759 LLVMValueRef built_call = LLVMBuildCall2(g->builder, LLVMGlobalGetValueType(asm_fn), asm_fn, param_values, (unsigned)input_and_output_count, "");
57575760
57585761 for (size_t i = 0; i < input_and_output_count; i += 1) {
5759 if (param_needs_attr[i]) {
5760 LLVMTypeRef elem_ty = LLVMGetElementType(param_types[i]);
5762 if (param_needs_attr[i] != nullptr) {
5763 LLVMTypeRef elem_ty = param_needs_attr[i];
57615764 ZigLLVMSetCallElemTypeAttr(built_call, i, elem_ty);
57625765 }
57635766 }
......@@ -7665,7 +7668,7 @@ static LLVMValueRef ir_render_await(CodeGen *g, Stage1Air *executable, Stage1Air
76657668 target_frame_ptr, frame_ret_start + 1, "");
76667669 if (result_loc == nullptr) {
76677670 // no copy needed
7668 LLVMBuildStore(g->builder, LLVMConstNull(LLVMGetElementType(LLVMTypeOf(awaiter_ret_ptr_ptr))),
7671 LLVMBuildStore(g->builder, LLVMConstNull(LLVMGetGEPSourceElementType(awaiter_ret_ptr_ptr)),
76697672 awaiter_ret_ptr_ptr);
76707673 } else {
76717674 LLVMBuildStore(g->builder, result_loc, awaiter_ret_ptr_ptr);
......@@ -9440,7 +9443,7 @@ static void do_code_gen(CodeGen *g) {
94409443 get_llvm_type(g, get_fn_frame_type(g, g->cur_fn)),
94419444 g->cur_frame_ptr,
94429445 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));
94449447 LLVMBuildStore(g->builder, zero_ptr, trace_ptr_ptr);
94459448 }
94469449