From: Roger Sayle Date: Fri, 22 Dec 2006 03:56:43 +0000 (+0000) Subject: trans-array.c (gfc_trans_create_temp_array): When the size is known at compile-time... X-Git-Tag: releases/gcc-4.3.0~7859 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fcac92297367e6274fad4392fc8a183076920943;p=thirdparty%2Fgcc.git trans-array.c (gfc_trans_create_temp_array): When the size is known at compile-time, avoid an unnecessary conditional assignment. * trans-array.c (gfc_trans_create_temp_array): When the size is known at compile-time, avoid an unnecessary conditional assignment. (gfc_array_init_size): Likewise. From-SVN: r120141 --- diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index a7ae44b91699..d25f5bf15234 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,9 @@ +2006-12-21 Roger Sayle + + * trans-array.c (gfc_trans_create_temp_array): When the size is known + at compile-time, avoid an unnecessary conditional assignment. + (gfc_array_init_size): Likewise. + 2006-12-22 Kazu Hirata * interface.c: Fix a comment typo. diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c index 6201c4c05618..56e69a3d4350 100644 --- a/gcc/fortran/trans-array.c +++ b/gcc/fortran/trans-array.c @@ -701,24 +701,33 @@ gfc_trans_create_temp_array (stmtblock_t * pre, stmtblock_t * post, { if (function) { - var = gfc_create_var (TREE_TYPE (size), "size"); - gfc_start_block (&thenblock); - gfc_add_modify_expr (&thenblock, var, gfc_index_zero_node); - thencase = gfc_finish_block (&thenblock); - - gfc_start_block (&elseblock); - gfc_add_modify_expr (&elseblock, var, size); - elsecase = gfc_finish_block (&elseblock); + /* If we know at compile-time whether any dimension size is + negative, we can avoid a conditional and pass the true size + to gfc_trans_allocate_array_storage, which can then decide + whether to allocate this on the heap or on the stack. */ + if (integer_zerop (or_expr)) + ; + else if (integer_onep (or_expr)) + size = gfc_index_zero_node; + else + { + var = gfc_create_var (TREE_TYPE (size), "size"); + gfc_start_block (&thenblock); + gfc_add_modify_expr (&thenblock, var, gfc_index_zero_node); + thencase = gfc_finish_block (&thenblock); + + gfc_start_block (&elseblock); + gfc_add_modify_expr (&elseblock, var, size); + elsecase = gfc_finish_block (&elseblock); - tmp = gfc_evaluate_now (or_expr, pre); - tmp = build3_v (COND_EXPR, tmp, thencase, elsecase); - gfc_add_expr_to_block (pre, tmp); - nelem = var; - size = var; + tmp = gfc_evaluate_now (or_expr, pre); + tmp = build3_v (COND_EXPR, tmp, thencase, elsecase); + gfc_add_expr_to_block (pre, tmp); + size = var; + } } - else - nelem = size; + nelem = size; size = fold_build2 (MULT_EXPR, gfc_array_index_type, size, TYPE_SIZE_UNIT (gfc_get_element_type (type))); } @@ -3275,6 +3284,11 @@ gfc_array_init_size (tree descriptor, int rank, tree * poffset, *poffset = offset; } + if (integer_zerop (or_expr)) + return size; + if (integer_onep (or_expr)) + return gfc_index_zero_node; + var = gfc_create_var (TREE_TYPE (size), "size"); gfc_start_block (&thenblock); gfc_add_modify_expr (&thenblock, var, gfc_index_zero_node);