Bug 49189 - [4.7 regression] infinite recursion in constant folder
Summary: [4.7 regression] infinite recursion in constant folder
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.0
Assignee: Richard Biener
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-27 10:54 UTC by Eric Botcazou
Modified: 2011-05-27 13:30 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-05-27 11:05:24


Attachments
Concatenated testcase (345 bytes, text/plain)
2011-05-27 10:56 UTC, Eric Botcazou
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Botcazou 2011-05-27 10:54:08 UTC
The patch

2011-05-26  Richard Guenther  <rguenther@suse.de>

	* fold-const.c (fold_unary_loc): Remove bogus code.

has introduced an infinite recursion in the constant folder for the Ada testcase about to be attached.  No change after the fix for PR middle-end/49177.
Comment 1 Eric Botcazou 2011-05-27 10:56:33 UTC
Created attachment 24367 [details]
Concatenated testcase

Run gnatchop on the file.  Suitable for inclusion in the gnat.dg testsuite.
Comment 2 Richard Biener 2011-05-27 11:05:24 UTC
Confirmed.  Endlessly visiting


Breakpoint 6, fold_unary_loc (loc=0, code=TRUTH_NOT_EXPR, type=0x7ffff5b7e5e8, 
    op0=0x7ffff5b820a0) at /space/rguenther/src/svn/trunk/gcc/fold-const.c:7555
7555      enum tree_code_class kind = TREE_CODE_CLASS (code);
(gdb) call debug_generic_expr (op0)
(const boolean) ((boolean) ((system__unsigned_types__short_unsigned) v->OBJECT >> (integer) i) & 1)

Breakpoint 6, fold_unary_loc (loc=0, code=NOP_EXPR, type=0x7ffff5b7e5e8, 
    op0=0x7ffff5b83000) at /space/rguenther/src/svn/trunk/gcc/fold-const.c:7555
7555      enum tree_code_class kind = TREE_CODE_CLASS (code);
(gdb) call debug_generic_expr (op0)
((boolean) ((system__unsigned_types__short_unsigned) v->OBJECT >> (integer) i) & 1) == 0
(gdb) call debug_generic_expr (type)
const boolean

Breakpoint 7, fold_binary_loc (loc=0, code=EQ_EXPR, type=0x7ffff5b7e5e8, 
    op0=0x7ffff5b72e70, op1=0x7ffff7ee85e0)
    at /space/rguenther/src/svn/trunk/gcc/fold-const.c:9280
9280      enum tree_code_class kind = TREE_CODE_CLASS (code);
(gdb) call debug_generic_expr (op0)
(boolean) ((system__unsigned_types__short_unsigned) v->OBJECT >> (integer) i) & 1
(gdb) call debug_generic_expr (op1)
0

Breakpoint 6, fold_unary_loc (loc=0, code=NOP_EXPR, type=0x7ffff5b7e5e8, 
    op0=0x7ffff5b72e70) at /space/rguenther/src/svn/trunk/gcc/fold-const.c:7555
7555      enum tree_code_class kind = TREE_CODE_CLASS (code);
(gdb) call debug_generic_expr (op0)
(boolean) ((system__unsigned_types__short_unsigned) v->OBJECT >> (integer) i) & 1
(gdb) call debug_generic_expr (type)
const boolean

Breakpoint 6, fold_unary_loc (loc=0, code=TRUTH_NOT_EXPR, type=0x7ffff5b7e5e8, 
    op0=0x7ffff5b820c8) at /space/rguenther/src/svn/trunk/gcc/fold-const.c:7555
7555      enum tree_code_class kind = TREE_CODE_CLASS (code);
...
Comment 3 Richard Biener 2011-05-27 11:13:11 UTC
fold_truth_not_expr canonicalizes !(A & 1) to (A & 1) == 0 but fold_binary
canonicalizes bool == 0 to !bool.  A recipie for recursion.  fold_truth_not_expr
doesn't re-fold its result but its result is fold-converted to bool through
which we then end up in that latent cycle.
Comment 4 Richard Biener 2011-05-27 11:21:09 UTC
The old code did not re-fold in

      /* If we have (type) (a CMP b) and type is an integral type, return
         new expression involving the new type.  */
      if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
        return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
                            TREE_OPERAND (op0, 1));

for BOOLEAN_TYPE.

It could be argued that re-folding is never necessary here because the
comparison does not depend on the type of the boolean result it produces.

So I'll disable re-folding here.
Comment 5 Richard Biener 2011-05-27 13:13:34 UTC
Author: rguenth
Date: Fri May 27 13:13:28 2011
New Revision: 174330

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174330
Log:
2011-05-27  Richard Guenther  <rguenther@suse.de>

	PR middle-end/49189
	* fold-const.c (fold_unary_loc): Do not re-fold folding conversions
	of comparisons.

	* gnat.dg/bit_packed_array5.adb: New testcase.
	* gnat.dg/bit_packed_array5.ads: Likewise.

Added:
    trunk/gcc/testsuite/gnat.dg/bit_packed_array5.adb
    trunk/gcc/testsuite/gnat.dg/bit_packed_array5.ads
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Richard Biener 2011-05-27 13:13:50 UTC
Fixed.
Comment 7 Bill Schmidt 2011-05-27 13:30:03 UTC
Author: wschmidt
Date: Fri May 27 13:29:57 2011
New Revision: 174331

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174331
Log:
Index: gcc/ChangeLog
===================================================================
--- gcc/ChangeLog	(revision 174330)
+++ gcc/ChangeLog	(working copy)
@@ -1,3 +1,9 @@
+2011-05-27  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
+
+	PR tree-optimization/49170
+	* tree-ssa-math-opts.c (execute_cse_sincos):  Add checks for
+	sincos or cexp.
+	
 2011-05-27  Richard Guenther  <rguenther@suse.de>
 
 	PR middle-end/49189
Index: gcc/tree-ssa-math-opts.c
===================================================================
--- gcc/tree-ssa-math-opts.c	(revision 174330)
+++ gcc/tree-ssa-math-opts.c	(working copy)
@@ -1093,6 +1093,10 @@ execute_cse_sincos (void)
 		CASE_FLT_FN (BUILT_IN_COS):
 		CASE_FLT_FN (BUILT_IN_SIN):
 		CASE_FLT_FN (BUILT_IN_CEXPI):
+		  /* Make sure we have either sincos or cexp.  */
+		  if (!TARGET_HAS_SINCOS && !TARGET_C99_FUNCTIONS)
+		    break;
+
 		  arg = gimple_call_arg (stmt, 0);
 		  if (TREE_CODE (arg) == SSA_NAME)
 		    cfg_changed |= execute_cse_sincos_1 (arg);

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/tree-ssa-math-opts.c