[tree-ssa] Problem with constant folding

law@redhat.com law@redhat.com
Wed Dec 17 23:25:00 GMT 2003


In message <20031217111421.GA19438@atrey.karlin.mff.cuni.cz>, Zdenek Dvorak wri
tes:
 >Hello,
 >
 >>  >>  >[snip] (http://gcc.gnu.org/ml/gcc-patches/2003-12/msg00786.html)
 >>  >>  >
 >>  >>  >> The patch below fixes the problem by preventing the overflowed cons
 >tant
 >>  >s
 >>  >>  >> from being propagated.  It is probably way too conservative, but I 
 >was
 >>  >>  >> not able to determine the place where to fix the problem in other w
 >ay.
 >>  >>  >
 >>  >>  >... and unfortunately it is also wrong, since it fails to bootstrap. 
 >Does
 >>  >>  >someone has an idea what is the right way to fix this?
 >>  >> Your approach seems pretty reasonable.  And it seems extremely odd that
 >>  >> it wouldn't bootstrap since it ought to just force such constants into
 >>  >> new variables and prevent them from being propagated by the optimizers.
 >>  >> 
 >>  >> I'd suggest investigating why the patch didn't bootstrap.
 >>  >
 >>  >because gimplification fails when constants are not considered to be
 >>  >invariant. 
 >> And why is that?  It would seem to me that gimplification in this case
 >> should put the overflowed constant into a temporary then move on to
 >> something more interesting.
 >
 >with the change to is_gimple_rhs the things seem to work; I have not
 >however run a full regtesting yet.
Here's the final version of the patch.  Bootstrapped and regression tested
on i686-pc-linux-gnu.

There's even a nonzero chance this fixes some bugs in my queue :-)



	* tree-cfg.c (tree_node_shared_p): Explicitly allow sharing of
	CST nodes.
	* tree-simple.c (is_gimple_rhs): Allow CST nodes.
	(is_gimple_min_invariant): Reject constants with TREE_OVERFLOW set.
	* tree-ssa-ccp (visit_assignment): Test is_gimple_min_invariant
	after munging bitfields.
	* tree-ssa-dom.c (record_equivalences_from_stmt): Similarly.
	
Index: tree-cfg.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-cfg.c,v
retrieving revision 1.1.4.237
diff -c -3 -p -r1.1.4.237 tree-cfg.c
*** tree-cfg.c	17 Dec 2003 05:39:01 -0000	1.1.4.237
--- tree-cfg.c	17 Dec 2003 23:12:36 -0000
*************** static bool
*** 2815,2825 ****
  tree_node_shared_p (tree t)
  {
    if (TYPE_P (t) || DECL_P (t)
        || is_gimple_min_invariant (t)
        || TREE_CODE (t) == SSA_NAME)
      return true;
    while ((TREE_CODE (t) == ARRAY_REF
!           && is_gimple_min_invariant (TREE_OPERAND (t, 1)))
  	 || (TREE_CODE (t) == COMPONENT_REF
  	     || TREE_CODE (t) == REALPART_EXPR
  	     || TREE_CODE (t) == IMAGPART_EXPR))
--- 2815,2831 ----
  tree_node_shared_p (tree t)
  {
    if (TYPE_P (t) || DECL_P (t)
+       /* We check for constants explicitly since they are not considered
+ 	 gimple invariants if they overflowed.  */
+       || TREE_CODE_CLASS (TREE_CODE (t)) == 'c'
        || is_gimple_min_invariant (t)
        || TREE_CODE (t) == SSA_NAME)
      return true;
    while ((TREE_CODE (t) == ARRAY_REF
! 	  /* We check for constants explicitly since they are not considered
! 	     gimple invariants if they overflowed.  */
!           && (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 1))) == 'c'
! 	      || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
  	 || (TREE_CODE (t) == COMPONENT_REF
  	     || TREE_CODE (t) == REALPART_EXPR
  	     || TREE_CODE (t) == IMAGPART_EXPR))
Index: tree-simple.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-simple.c,v
retrieving revision 1.1.4.66
diff -c -3 -p -r1.1.4.66 tree-simple.c
*** tree-simple.c	15 Dec 2003 15:55:34 -0000	1.1.4.66
--- tree-simple.c	17 Dec 2003 23:12:39 -0000
*************** is_gimple_rhs (tree t)
*** 206,211 ****
--- 206,216 ----
      case CONSTRUCTOR:
        /* FIXME lower VA_ARG_EXPR.  */
      case VA_ARG_EXPR:
+     case INTEGER_CST:
+     case REAL_CST:
+     case STRING_CST:
+     case COMPLEX_CST:
+     case VECTOR_CST:
        return 1;
  
      default:
*************** is_gimple_min_invariant (tree t)
*** 316,322 ****
      case STRING_CST:
      case COMPLEX_CST:
      case VECTOR_CST:
!       return true;
  
      default:
        return false;
--- 321,327 ----
      case STRING_CST:
      case COMPLEX_CST:
      case VECTOR_CST:
!       return !TREE_OVERFLOW (t);
  
      default:
        return false;
Index: tree-ssa-ccp.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-ssa-ccp.c,v
retrieving revision 1.1.2.125
diff -c -3 -p -r1.1.2.125 tree-ssa-ccp.c
*** tree-ssa-ccp.c	15 Dec 2003 22:58:34 -0000	1.1.2.125
--- tree-ssa-ccp.c	17 Dec 2003 23:12:41 -0000
*************** visit_assignment (tree stmt)
*** 661,667 ****
        {
  	tree w = widen_bitfield (val.const_val, TREE_OPERAND (lhs, 1), lhs);
  
! 	if (w)
  	  val.const_val = w;
  	else
  	  {
--- 661,667 ----
        {
  	tree w = widen_bitfield (val.const_val, TREE_OPERAND (lhs, 1), lhs);
  
! 	if (w && is_gimple_min_invariant (w))
  	  val.const_val = w;
  	else
  	  {
Index: tree-ssa-dom.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-ssa-dom.c,v
retrieving revision 1.1.2.98
diff -c -3 -p -r1.1.2.98 tree-ssa-dom.c
*** tree-ssa-dom.c	16 Dec 2003 21:42:31 -0000	1.1.2.98
--- tree-ssa-dom.c	17 Dec 2003 23:12:45 -0000
*************** record_equivalences_from_stmt (tree stmt
*** 2098,2103 ****
--- 2098,2107 ----
  	    rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
  	  else
  	    rhs = NULL;
+ 
+ 	  /* If the value overflowed, then we can not use this equivalence.  */
+ 	  if (rhs && ! is_gimple_min_invariant (rhs))
+ 	    rhs = NULL;
  	}
  
        if (rhs)




More information about the Gcc-patches mailing list