This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: PR c/8068
On Fri, Feb 14, 2003 at 11:36:04PM +0100, Arend Bayer wrote:
> Two general questions on const-fold.c:
> 1. I was very much surprised to see this happen at -O0. Shouldn't
> extract_muldiv(), or at least it's less obvious simplications such as
> (op + const1) * const2 -> op*const2 + const1*const2 etc., only be executed at
> -O1 and higher?
*shrug* We've always done it this way.
> 2. I find it very hard to verify that similar exponential behaviour
> will not get triggered with other testcases. Maybe extract_muldiv() should
> have a bound how many recursive calls it tries while simplifying one
> expression?
Indeed, based on the fact that this can happen with / as well,
this does seem the best solution. I'm testing this.
r~
Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.234
diff -c -p -d -u -r1.234 fold-const.c
--- fold-const.c 5 Feb 2003 22:37:51 -0000 1.234
+++ fold-const.c 16 Feb 2003 05:17:12 -0000
@@ -104,6 +104,7 @@ static tree unextend PARAMS ((tree, int
static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
static tree optimize_minmax_comparison PARAMS ((tree));
static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
+static tree extract_muldiv_1 PARAMS ((tree, tree, enum tree_code, tree));
static tree strip_compound_expr PARAMS ((tree, tree));
static int multiple_of_p PARAMS ((tree, tree, tree));
static tree constant_boolean_node PARAMS ((int, tree));
@@ -4046,6 +4047,31 @@ extract_muldiv (t, c, code, wide_type)
enum tree_code code;
tree wide_type;
{
+ /* To avoid exponential search depth, refuse to allow recursion past
+ three levels. Beyond that (1) it's highly unlikely that we'll find
+ something interesting and (2) we've probably processed it before
+ when we built the inner expression. */
+
+ static int depth;
+ tree ret;
+
+ if (depth > 3)
+ return NULL;
+
+ depth++;
+ ret = extract_muldiv_1 (t, c, code, wide_type);
+ depth--;
+
+ return ret;
+}
+
+static tree
+extract_muldiv_1 (t, c, code, wide_type)
+ tree t;
+ tree c;
+ enum tree_code code;
+ tree wide_type;
+{
tree type = TREE_TYPE (t);
enum tree_code tcode = TREE_CODE (t);
tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
@@ -4253,6 +4279,14 @@ extract_muldiv (t, c, code, wide_type)
&& TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
&& integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
return omit_one_operand (type, integer_zero_node, op0);
+
+ /* Arrange for the code below to simplify two constants first. */
+ if (TREE_CODE (op1) == INTEGER_CST && TREE_CODE (op0) != INTEGER_CST)
+ {
+ tree tmp = op0;
+ op0 = op1;
+ op1 = tmp;
+ }
/* ... fall through ... */