spslr_pinpoint - got rid of unnecessary temporaries

This commit is contained in:
York Jasper Niebuhr 2025-10-23 12:40:48 +02:00
parent ad73897a6c
commit c5b4a31970

View File

@ -336,7 +336,6 @@ static tree instrument_offsetof_maybe(tree ref) {
tree cast_back = build1_loc(loc, NOP_EXPR, field_ptr_type, plus); tree cast_back = build1_loc(loc, NOP_EXPR, field_ptr_type, plus);
tree new_ref = build1_loc(loc, INDIRECT_REF, result_type, cast_back); tree new_ref = build1_loc(loc, INDIRECT_REF, result_type, cast_back);
return new_ref; return new_ref;
} }
@ -478,9 +477,8 @@ static tree gimple_instrument_offsetof_maybe(tree ref, gimple_stmt_iterator* gsi
if (!gcrc.relevant) if (!gcrc.relevant)
return NULL_TREE; return NULL_TREE;
// Store base pointer in a temporary variable (as char*) // Get base pointer
tree char_ptr_type = build_pointer_type(char_type_node);
tree base_tmp = NULL_TREE; tree base_tmp = NULL_TREE;
{ {
tree base_ptr; tree base_ptr;
@ -492,11 +490,7 @@ static tree gimple_instrument_offsetof_maybe(tree ref, gimple_stmt_iterator* gsi
base_ptr = build_fold_addr_expr(gcrc.base); base_ptr = build_fold_addr_expr(gcrc.base);
} }
tree base_char_ptr = fold_convert(char_ptr_type, base_ptr); base_tmp = base_ptr;
base_tmp = create_tmp_var(char_ptr_type, NULL);
gimple* base_tmp_assignment = gimple_build_assign(base_tmp, base_char_ptr);
gsi_insert_before(gsi, base_tmp_assignment, GSI_SAME_STMT);
} }
// For each component ref in chain, add the member offset to the pointer // For each component ref in chain, add the member offset to the pointer
@ -520,10 +514,12 @@ static tree gimple_instrument_offsetof_maybe(tree ref, gimple_stmt_iterator* gsi
gimple_call_set_lhs(call_stmt, offset_tmp); gimple_call_set_lhs(call_stmt, offset_tmp);
gsi_insert_before(gsi, call_stmt, GSI_SAME_STMT); gsi_insert_before(gsi, call_stmt, GSI_SAME_STMT);
// Add call return value to current base pointer // Add call return value to current base pointer (result is field pointer)
tree addition = build2(POINTER_PLUS_EXPR, char_ptr_type, base_tmp, offset_tmp); tree field_ptr_type = build_pointer_type(TREE_TYPE(cr.t));
base_tmp = create_tmp_var(char_ptr_type, NULL); tree field_ptr = build2(POINTER_PLUS_EXPR, field_ptr_type, base_tmp, offset_tmp);
gimple* addition_assignment = gimple_build_assign(base_tmp, addition);
base_tmp = create_tmp_var(field_ptr_type, NULL);
gimple* addition_assignment = gimple_build_assign(base_tmp, field_ptr);
gsi_insert_before(gsi, addition_assignment, GSI_SAME_STMT); gsi_insert_before(gsi, addition_assignment, GSI_SAME_STMT);
} else { } else {
// Add offsetof contant // Add offsetof contant
@ -536,25 +532,20 @@ static tree gimple_instrument_offsetof_maybe(tree ref, gimple_stmt_iterator* gsi
return NULL_TREE; return NULL_TREE;
} }
// Add constant offset to current base pointer // Add constant offset to current base pointer (result is field pointer)
tree addition = build2(POINTER_PLUS_EXPR, char_ptr_type, base_tmp, build_int_cst(sizetype, offset)); tree field_ptr_type = build_pointer_type(TREE_TYPE(cr.t));
base_tmp = create_tmp_var(char_ptr_type, NULL); tree field_ptr = build2(POINTER_PLUS_EXPR, field_ptr_type, base_tmp, build_int_cst(sizetype, offset));
gimple* addition_assignment = gimple_build_assign(base_tmp, addition);
base_tmp = create_tmp_var(field_ptr_type, NULL);
gimple* addition_assignment = gimple_build_assign(base_tmp, field_ptr);
gsi_insert_before(gsi, addition_assignment, GSI_SAME_STMT); gsi_insert_before(gsi, addition_assignment, GSI_SAME_STMT);
} }
} }
// Cast char pointer back to field pointer and dereference // Current pointer is a field pointer -> dereference
tree field_ptr_type = build_pointer_type(TREE_TYPE(ref)); tree offset0 = fold_convert(TREE_TYPE(base_tmp), build_int_cst(sizetype, 0));
tree result_ref = build2(MEM_REF, TREE_TYPE(ref), base_tmp, offset0);
tree field_ptr = fold_convert(field_ptr_type, base_tmp);
tree field_ptr_tmp = create_tmp_var(field_ptr_type, NULL);
gimple* field_ptr_tmp_assignment = gimple_build_assign(field_ptr_tmp, field_ptr);
gsi_insert_before(gsi, field_ptr_tmp_assignment, GSI_SAME_STMT);
tree offset0 = fold_convert(field_ptr_type, build_int_cst(sizetype, 0));
tree result_ref = build2(MEM_REF, TREE_TYPE(ref), field_ptr_tmp, offset0);
return result_ref; return result_ref;
} }