* [fortran,committed] Simplify code for memory allocation and freeing
@ 2015-08-29 10:14 FX
0 siblings, 0 replies; only message in thread
From: FX @ 2015-08-29 10:14 UTC (permalink / raw)
To: gcc patches; +Cc: gfortran
[-- Attachment #1: Type: text/plain, Size: 713 bytes --]
The attached patch simplifies code in various gcc/fortran/trans* routines related to malloc and free:
- we don’t need to check if types match before calling fold_convert(): fold_convert() does it itself
- in some cases where the argument is used only once, we don’t need to call gfc_evaluate_now()
- in other cases, we can used save_expr() instead of gfc_evaluate_now(), which is simpler to use and doesn’t create a new decl
- the callers of gfc_call_free() don’t need to convert their argument to pvoid_type_node: gfc_call_free() itself does it
The resulting code is simpler, shorter and more unified.
Committed as revision 227316, after regtesting on x86_64-pc-linux-gnu.
FX
[-- Attachment #2: x.ChangeLog --]
[-- Type: application/octet-stream, Size: 521 bytes --]
2015-08-29 Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
* trans.c (gfc_call_malloc, gfc_allocate_using_malloc,
gfc_allocate_using_lib, gfc_allocate_allocatable,
gfc_call_realloc): Simplify code.
* trans-array.c (gfc_trans_allocate_array_storage,
gfc_trans_auto_array_allocation, gfc_conv_array_parameter): Do not
convert gfc_call_free() argument.
* trans-expr.c (gfc_conv_string_tmp, gfc_conv_procedure_call,
fcncall_realloc_result): Likewise.
* trans-intrinsic.c (gfc_conv_intrinsic_transfer): Likewise.
[-- Attachment #3: x.diff --]
[-- Type: application/octet-stream, Size: 7880 bytes --]
Index: trans-array.c
===================================================================
--- trans-array.c (revision 227296)
+++ trans-array.c (working copy)
@@ -922,7 +922,7 @@ gfc_trans_allocate_array_storage (stmtblock_t * pr
{
/* Free the temporary. */
tmp = gfc_conv_descriptor_data_get (desc);
- tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
+ tmp = gfc_call_free (tmp);
gfc_add_expr_to_block (post, tmp);
}
}
@@ -5885,7 +5885,7 @@ gfc_trans_auto_array_allocation (tree decl, gfc_sy
gfc_add_modify (&init, decl, tmp);
/* Free the temporary. */
- tmp = gfc_call_free (convert (pvoid_type_node, decl));
+ tmp = gfc_call_free (decl);
space = NULL_TREE;
}
@@ -7542,7 +7542,7 @@ gfc_conv_array_parameter (gfc_se * se, gfc_expr *
}
/* Free the temporary. */
- tmp = gfc_call_free (convert (pvoid_type_node, ptr));
+ tmp = gfc_call_free (ptr);
gfc_add_expr_to_block (&block, tmp);
stmt = gfc_finish_block (&block);
Index: trans-expr.c
===================================================================
--- trans-expr.c (revision 227296)
+++ trans-expr.c (working copy)
@@ -3035,7 +3035,7 @@ gfc_conv_string_tmp (gfc_se * se, tree type, tree
gfc_add_modify (&se->pre, var, tmp);
/* Free the temporary afterwards. */
- tmp = gfc_call_free (convert (pvoid_type_node, var));
+ tmp = gfc_call_free (var);
gfc_add_expr_to_block (&se->post, tmp);
}
@@ -5880,7 +5880,7 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol *
gfc_add_modify (&se->pre, var,
fold_convert (TREE_TYPE (var),
null_pointer_node));
- tmp = gfc_call_free (convert (pvoid_type_node, var));
+ tmp = gfc_call_free (var);
gfc_add_expr_to_block (&se->post, tmp);
}
@@ -6140,14 +6140,14 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol *
if (se->ss && se->ss->loop)
{
gfc_add_expr_to_block (&se->ss->loop->post, tmp);
- tmp = gfc_call_free (convert (pvoid_type_node, info->data));
+ tmp = gfc_call_free (info->data);
gfc_add_expr_to_block (&se->ss->loop->post, tmp);
}
else
{
gfc_add_expr_to_block (&se->post, tmp);
tmp = gfc_class_data_get (se->expr);
- tmp = gfc_call_free (convert (pvoid_type_node, tmp));
+ tmp = gfc_call_free (tmp);
gfc_add_expr_to_block (&se->post, tmp);
}
expr->must_finalize = 0;
@@ -8453,7 +8453,7 @@ fcncall_realloc_result (gfc_se *se, int rank)
boolean_type_node, tmp,
build_int_cst (TREE_TYPE (tmp), 0));
zero_cond = gfc_evaluate_now (zero_cond, &se->post);
- tmp = gfc_call_free (fold_convert (pvoid_type_node, tmp));
+ tmp = gfc_call_free (tmp);
gfc_add_expr_to_block (&se->post, tmp);
tmp = gfc_conv_descriptor_data_get (res_desc);
Index: trans-intrinsic.c
===================================================================
--- trans-intrinsic.c (revision 227311)
+++ trans-intrinsic.c (working copy)
@@ -6259,7 +6259,7 @@ gfc_conv_intrinsic_transfer (gfc_se * se, gfc_expr
/* Free the temporary. */
gfc_start_block (&block);
- tmp = gfc_call_free (convert (pvoid_type_node, source));
+ tmp = gfc_call_free (source);
gfc_add_expr_to_block (&block, tmp);
stmt = gfc_finish_block (&block);
Index: trans.c
===================================================================
--- trans.c (revision 227296)
+++ trans.c (working copy)
@@ -567,17 +567,13 @@ gfc_call_malloc (stmtblock_t * block, tree type, t
tree tmp, msg, malloc_result, null_result, res, malloc_tree;
stmtblock_t block2;
- size = gfc_evaluate_now (size, block);
-
- if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
- size = fold_convert (size_type_node, size);
-
/* Create a variable to hold the result. */
res = gfc_create_var (prvoid_type_node, NULL);
/* Call malloc. */
gfc_start_block (&block2);
+ size = fold_convert (size_type_node, size);
size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
build_int_cst (size_type_node, 1));
@@ -604,7 +600,6 @@ gfc_call_malloc (stmtblock_t * block, tree type, t
}
malloc_result = gfc_finish_block (&block2);
-
gfc_add_expr_to_block (block, malloc_result);
if (type != NULL)
@@ -643,11 +638,6 @@ gfc_allocate_using_malloc (stmtblock_t * block, tr
stmtblock_t on_error;
tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
- /* Evaluate size only once, and make sure it has the right type. */
- size = gfc_evaluate_now (size, block);
- if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
- size = fold_convert (size_type_node, size);
-
/* If successful and stat= is given, set status to 0. */
if (status != NULL_TREE)
gfc_add_expr_to_block (block,
@@ -655,6 +645,7 @@ gfc_allocate_using_malloc (stmtblock_t * block, tr
status, build_int_cst (status_type, 0)));
/* The allocation itself. */
+ size = fold_convert (size_type_node, size);
gfc_add_modify (block, pointer,
fold_convert (TREE_TYPE (pointer),
build_call_expr_loc (input_location,
@@ -716,11 +707,6 @@ gfc_allocate_using_lib (stmtblock_t * block, tree
gcc_assert (token != NULL_TREE);
- /* Evaluate size only once, and make sure it has the right type. */
- size = gfc_evaluate_now (size, block);
- if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
- size = fold_convert (size_type_node, size);
-
/* The allocation itself. */
if (status == NULL_TREE)
pstat = null_pointer_node;
@@ -734,6 +720,7 @@ gfc_allocate_using_lib (stmtblock_t * block, tree
errlen = build_int_cst (integer_type_node, 0);
}
+ size = fold_convert (size_type_node, size);
tmp = build_call_expr_loc (input_location,
gfor_fndecl_caf_register, 6,
fold_build2_loc (input_location,
@@ -782,9 +769,7 @@ gfc_allocate_allocatable (stmtblock_t * block, tre
tree tmp, null_mem, alloc, error;
tree type = TREE_TYPE (mem);
- if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
- size = fold_convert (size_type_node, size);
-
+ size = fold_convert (size_type_node, size);
null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
boolean_type_node, mem,
build_int_cst (type, 0)),
@@ -866,27 +851,22 @@ gfc_allocate_allocatable (stmtblock_t * block, tre
/* Free a given variable, if it's not NULL. */
+
tree
gfc_call_free (tree var)
{
- stmtblock_t block;
- tree tmp, cond, call;
+ tree cond, call;
- if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
- var = fold_convert (pvoid_type_node, var);
+ /* Only evaluate the variable once. */
+ var = save_expr (fold_convert (pvoid_type_node, var));
- gfc_start_block (&block);
- var = gfc_evaluate_now (var, &block);
cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
build_int_cst (pvoid_type_node, 0));
call = build_call_expr_loc (input_location,
builtin_decl_explicit (BUILT_IN_FREE),
1, var);
- tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
- build_empty_stmt (input_location));
- gfc_add_expr_to_block (&block, tmp);
-
- return gfc_finish_block (&block);
+ return fold_build3_loc (input_location, COND_EXPR, void_type_node,
+ cond, call, build_empty_stmt (input_location));
}
@@ -1499,11 +1479,9 @@ gfc_call_realloc (stmtblock_t * block, tree mem, t
tree msg, res, nonzero, null_result, tmp;
tree type = TREE_TYPE (mem);
- size = gfc_evaluate_now (size, block);
+ /* Only evaluate the size once. */
+ size = save_expr (fold_convert (size_type_node, size));
- if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
- size = fold_convert (size_type_node, size);
-
/* Create a variable to hold the result. */
res = gfc_create_var (type, NULL);
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2015-08-29 8:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-29 10:14 [fortran,committed] Simplify code for memory allocation and freeing FX
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).