+2017-10-18 Thomas Koenig <tkoenig@gcc.gnu.org>
+
+ PR fortran/82567
+ * frontend-passes.c (combine_array_constructor): If an array
+ constructor is all constants and has more elements than a small
+ constant, don't convert a*[b,c] to [a*b,a*c] to reduce compilation
+ times.
+
2017-10-18 Thomas Koenig <tkoenig@gcc.gnu.org>
PR fortran/79795
gfc_constructor *c, *new_c;
gfc_constructor_base oldbase, newbase;
bool scalar_first;
+ int n_elem;
+ bool all_const;
/* Array constructors have rank one. */
if (e->rank != 1)
if (op2->ts.type == BT_CHARACTER)
return false;
- scalar = create_var (gfc_copy_expr (op2), "constr");
+ /* This might be an expanded constructor with very many constant values. If
+ we perform the operation here, we might end up with a long compile time
+ and actually longer execution time, so a length bound is in order here.
+ If the constructor constains something which is not a constant, it did
+ not come from an expansion, so leave it alone. */
+
+#define CONSTR_LEN_MAX 4
oldbase = op1->value.constructor;
+
+ n_elem = 0;
+ all_const = true;
+ for (c = gfc_constructor_first (oldbase); c; c = gfc_constructor_next(c))
+ {
+ if (c->expr->expr_type != EXPR_CONSTANT)
+ {
+ all_const = false;
+ break;
+ }
+ n_elem += 1;
+ }
+
+ if (all_const && n_elem > CONSTR_LEN_MAX)
+ return false;
+
+#undef CONSTR_LEN_MAX
+
newbase = NULL;
e->expr_type = EXPR_ARRAY;
+ scalar = create_var (gfc_copy_expr (op2), "constr");
+
for (c = gfc_constructor_first (oldbase); c;
c = gfc_constructor_next (c))
{
--- /dev/null
+! { dg-do compile }
+! { dg-additional-options "-ffrontend-optimize -fdump-tree-original" }
+! PR 82567 - long compile times caused by large constant constructors
+! multiplied by variables
+
+ SUBROUTINE sub()
+ IMPLICIT NONE
+
+ INTEGER, PARAMETER :: n = 1000
+ REAL, ALLOCATABLE :: x(:)
+ REAL :: xc, h
+ INTEGER :: i
+
+ ALLOCATE( x(n) )
+ xc = 100.
+ h = xc/n
+ x = h*[(i,i=1,n)]
+
+end
+! { dg-final { scan-tree-dump-times "__var" 0 "original" } }