mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Fix value elision on declaration
This commit is contained in:
@@ -337,6 +337,8 @@ struct lbProcedure {
|
|||||||
|
|
||||||
LLVMMetadataRef debug_info;
|
LLVMMetadataRef debug_info;
|
||||||
|
|
||||||
|
lbAddr current_elision_hint;
|
||||||
|
|
||||||
PtrMap<Ast *, lbValue> selector_values;
|
PtrMap<Ast *, lbValue> selector_values;
|
||||||
PtrMap<Ast *, lbAddr> selector_addr;
|
PtrMap<Ast *, lbAddr> selector_addr;
|
||||||
PtrMap<LLVMValueRef, lbTupleFix> tuple_fix_map;
|
PtrMap<LLVMValueRef, lbTupleFix> tuple_fix_map;
|
||||||
|
|||||||
@@ -484,7 +484,14 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo
|
|||||||
LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
|
LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
|
||||||
LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
|
LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, "");
|
||||||
LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
|
LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true);
|
||||||
lbAddr slice = lb_add_local_generated(p, type, false);
|
|
||||||
|
lbAddr slice = {};
|
||||||
|
if (p->current_elision_hint.addr.value && are_types_identical(lb_addr_type(p->current_elision_hint), type)) {
|
||||||
|
slice = p->current_elision_hint;
|
||||||
|
p->current_elision_hint = {};
|
||||||
|
} else {
|
||||||
|
slice = lb_add_local_generated(p, type, false);
|
||||||
|
}
|
||||||
map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
|
map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice);
|
||||||
|
|
||||||
lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
|
lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int});
|
||||||
|
|||||||
@@ -989,7 +989,6 @@ gb_internal void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, T
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
AstFile *file = p->body->file();
|
AstFile *file = p->body->file();
|
||||||
|
|
||||||
LLVMMetadataRef llvm_scope = lb_get_current_debug_scope(p);
|
LLVMMetadataRef llvm_scope = lb_get_current_debug_scope(p);
|
||||||
|
|||||||
@@ -275,6 +275,9 @@ gb_internal void lb_populate_module_pass_manager(LLVMTargetMachineRef target_mac
|
|||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) {
|
gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) {
|
||||||
|
unsigned debug_declare_id = LLVMLookupIntrinsicID("llvm.dbg.declare", 16);
|
||||||
|
GB_ASSERT(debug_declare_id != 0);
|
||||||
|
|
||||||
isize removal_count = 0;
|
isize removal_count = 0;
|
||||||
isize pass_count = 0;
|
isize pass_count = 0;
|
||||||
isize const max_pass_count = 10;
|
isize const max_pass_count = 10;
|
||||||
@@ -310,6 +313,8 @@ gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) {
|
|||||||
|
|
||||||
// NOTE(bill): Explicit instructions are set here because some instructions could have side effects
|
// NOTE(bill): Explicit instructions are set here because some instructions could have side effects
|
||||||
switch (LLVMGetInstructionOpcode(curr_instr)) {
|
switch (LLVMGetInstructionOpcode(curr_instr)) {
|
||||||
|
// case LLVMAlloca:
|
||||||
|
|
||||||
case LLVMFNeg:
|
case LLVMFNeg:
|
||||||
case LLVMAdd:
|
case LLVMAdd:
|
||||||
case LLVMFAdd:
|
case LLVMFAdd:
|
||||||
@@ -329,7 +334,6 @@ gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) {
|
|||||||
case LLVMAnd:
|
case LLVMAnd:
|
||||||
case LLVMOr:
|
case LLVMOr:
|
||||||
case LLVMXor:
|
case LLVMXor:
|
||||||
case LLVMAlloca:
|
|
||||||
case LLVMLoad:
|
case LLVMLoad:
|
||||||
case LLVMGetElementPtr:
|
case LLVMGetElementPtr:
|
||||||
case LLVMTrunc:
|
case LLVMTrunc:
|
||||||
|
|||||||
@@ -2287,18 +2287,25 @@ gb_internal void lb_build_stmt(lbProcedure *p, Ast *node) {
|
|||||||
|
|
||||||
isize lval_index = 0;
|
isize lval_index = 0;
|
||||||
for (Ast *rhs : values) {
|
for (Ast *rhs : values) {
|
||||||
|
p->current_elision_hint = lvals[lval_index];
|
||||||
|
|
||||||
rhs = unparen_expr(rhs);
|
rhs = unparen_expr(rhs);
|
||||||
lbValue init = lb_build_expr(p, rhs);
|
lbValue init = lb_build_expr(p, rhs);
|
||||||
#if 1
|
#if 1
|
||||||
// NOTE(bill, 2023-02-17): lb_const_value might produce a stack local variable for the
|
if (p->current_elision_hint.addr.value != lvals[lval_index].addr.value) {
|
||||||
// compound literal, so reusing that variable should minimize the stack wastage
|
lvals[lval_index] = {}; // do nothing so that nothing will assign to it
|
||||||
if (rhs->kind == Ast_CompoundLit) {
|
} else {
|
||||||
lbAddr *comp_lit_addr = map_get(&p->module->exact_value_compound_literal_addr_map, rhs);
|
// NOTE(bill, 2023-02-17): lb_const_value might produce a stack local variable for the
|
||||||
if (comp_lit_addr) {
|
// compound literal, so reusing that variable should minimize the stack wastage
|
||||||
Entity *e = entity_of_node(vd->names[lval_index]);
|
if (rhs->kind == Ast_CompoundLit) {
|
||||||
if (e) {
|
lbAddr *comp_lit_addr = map_get(&p->module->exact_value_compound_literal_addr_map, rhs);
|
||||||
lb_add_entity(p->module, e, comp_lit_addr->addr);
|
if (comp_lit_addr) {
|
||||||
lvals[lval_index] = {}; // do nothing so that nothing will assign to it
|
Entity *e = entity_of_node(vd->names[lval_index]);
|
||||||
|
if (e) {
|
||||||
|
GB_ASSERT(p->current_elision_hint.addr.value == nullptr);
|
||||||
|
GB_ASSERT(p->current_elision_hint.addr.value != lvals[lval_index].addr.value);
|
||||||
|
lvals[lval_index] = {}; // do nothing so that nothing will assign to it
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2308,6 +2315,8 @@ gb_internal void lb_build_stmt(lbProcedure *p, Ast *node) {
|
|||||||
}
|
}
|
||||||
GB_ASSERT(lval_index == lvals.count);
|
GB_ASSERT(lval_index == lvals.count);
|
||||||
|
|
||||||
|
p->current_elision_hint = {};
|
||||||
|
|
||||||
GB_ASSERT(lvals.count == inits.count);
|
GB_ASSERT(lvals.count == inits.count);
|
||||||
for_array(i, inits) {
|
for_array(i, inits) {
|
||||||
lbAddr lval = lvals[i];
|
lbAddr lval = lvals[i];
|
||||||
|
|||||||
Reference in New Issue
Block a user