summaryrefslogtreecommitdiff
path: root/gcc/fortran/trans.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/fortran/trans.c')
-rw-r--r--gcc/fortran/trans.c46
1 files changed, 12 insertions, 34 deletions
diff --git a/gcc/fortran/trans.c b/gcc/fortran/trans.c
index aece77ab5cd..457c0e25210 100644
--- a/gcc/fortran/trans.c
+++ b/gcc/fortran/trans.c
@@ -567,17 +567,13 @@ gfc_call_malloc (stmtblock_t * block, tree type, tree size)
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, tree size)
}
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, tree pointer,
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, tree pointer,
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 pointer, tree size,
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 pointer, tree size,
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, tree mem, tree size, tree token,
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, tree mem, tree size, tree token,
/* 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,10 +1479,8 @@ gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
tree msg, res, nonzero, null_result, tmp;
tree type = TREE_TYPE (mem);
- size = gfc_evaluate_now (size, block);
-
- if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
- size = fold_convert (size_type_node, size);
+ /* Only evaluate the size once. */
+ size = save_expr (fold_convert (size_type_node, size));
/* Create a variable to hold the result. */
res = gfc_create_var (type, NULL);