Patchwork C++ PATCH for c++/56481 (time hog with repeated &&)

login
register
mail settings
Submitter Jason Merrill
Date Feb. 28, 2013, 8:20 p.m.
Message ID <512FBC23.5030507@redhat.com>
Download mbox | patch
Permalink /patch/224181/
State New
Headers show

Comments

Jason Merrill - Feb. 28, 2013, 8:20 p.m.
The problem with this testcase was that for a repeated &&, each call of 
potential_constant_expression_1 led to two calls for the LHS, giving it 
O(N^2) complexity.  Fixed by avoiding the redundant call in 
maybe_constant_value by calling cxx_eval_outermost_constant_expr directly.

Tested x86_64-pc-linux-gnu, applying to trunk.

Patch

commit 001c03d979ea1aaa9bc9565f2b9c82371cc481f1
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Feb 28 12:30:40 2013 -0500

    	PR c++/56481
    	* semantics.c (potential_constant_expression_1): Use
    	cxx_eval_outermost_constant_expr rather than maybe_constant_value.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 9446f83..8038aa2 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -8683,10 +8683,12 @@  potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
     case ROUND_MOD_EXPR:
       {
 	tree denom = TREE_OPERAND (t, 1);
-	/* We can't call maybe_constant_value on an expression
+	if (!potential_constant_expression_1 (denom, rval, flags))
+	  return false;
+	/* We can't call cxx_eval_outermost_constant_expr on an expression
 	   that hasn't been through fold_non_dependent_expr yet.  */
 	if (!processing_template_decl)
-	  denom = maybe_constant_value (denom);
+	  denom = cxx_eval_outermost_constant_expr (denom, true);
 	if (integer_zerop (denom))
 	  {
 	    if (flags & tf_error)
@@ -8696,7 +8698,8 @@  potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
 	else
 	  {
 	    want_rval = true;
-	    goto binary;
+	    return potential_constant_expression_1 (TREE_OPERAND (t, 0),
+						    want_rval, flags);
 	  }
       }
 
@@ -8731,7 +8734,7 @@  potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
 	if (!potential_constant_expression_1 (op, rval, flags))
 	  return false;
 	if (!processing_template_decl)
-	  op = maybe_constant_value (op);
+	  op = cxx_eval_outermost_constant_expr (op, true);
 	if (tree_int_cst_equal (op, tmp))
 	  return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
 	else
@@ -8793,7 +8796,7 @@  potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
       if (!potential_constant_expression_1 (tmp, rval, flags))
 	return false;
       if (!processing_template_decl)
-	tmp = maybe_constant_value (tmp);
+	tmp = cxx_eval_outermost_constant_expr (tmp, true);
       if (integer_zerop (tmp))
 	return potential_constant_expression_1 (TREE_OPERAND (t, 2),
 						want_rval, flags);