This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[tree-ssa] Problem with constant folding


Hello,

the following testcase is misscompiled:

int
my_ffs (void)
{
  unsigned long i = 0;

  if (0x80000000UL & (1UL << i))
    return 50;

  return 0;
}

the bug works this way: the code is gimplified into

my_ffs ()
{
  int i.1;
  long unsigned int T.2;
  int T.3;
  int T.4;
  _Bool T.5;
  long unsigned int i;

  i = 0;
  i.1 = (int)i;
  T.2 = 080000000 >> i.1;      (XXX)
  T.3 = (int)T.2;
  T.4 = T.3 & 1;
  T.5 = (_Bool)T.4;
  if (T.5)
    return 50;
  return 0;
}

0 is propagated into (XXX) and (080000000 >> 0) is then folded into
-080000000 (overflow). This is now propagated further into condition,
which becomes a 0 with TREE_OVERFLOW set.  During rtl expansion this
is not recognized as zero and thus the return 50 branch is eliminated.

The patch below fixes the problem by preventing the overflowed constants
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 way.

Zdenek

	* tree-simple.c (is_gimple_min_invariant): Do not consider
	overflowed constants invariant.

Index: tree-simple.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-simple.c,v
retrieving revision 1.1.4.64
diff -c -3 -p -r1.1.4.64 tree-simple.c
*** tree-simple.c	8 Dec 2003 10:03:39 -0000	1.1.4.64
--- tree-simple.c	8 Dec 2003 20:11:29 -0000
*************** is_gimple_min_invariant (tree t)
*** 316,322 ****
      case STRING_CST:
      case COMPLEX_CST:
      case VECTOR_CST:
!       return true;
  
      default:
        return false;
--- 316,322 ----
      case STRING_CST:
      case COMPLEX_CST:
      case VECTOR_CST:
!       return !TREE_OVERFLOW (t);
  
      default:
        return false;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]