Bug 12941 - [tree-ssa] builtin-bitops-1.c miscompilation
Summary: [tree-ssa] builtin-bitops-1.c miscompilation
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: tree-ssa
: P1 critical
Target Milestone: 3.3.3
Assignee: Richard Henderson
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-11-07 14:47 UTC by Falk Hueffner
Modified: 2004-01-30 23:25 UTC (History)
2 users (show)

See Also:
Host: alphaev68-unknown-linux-gnu
Target: alphaev68-unknown-linux-gnu
Build: alphaev68-unknown-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2003-12-01 04:38:47


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Falk Hueffner 2003-11-07 14:47:45 UTC
This makes builtin-bitops-1.c fail at -O3. Test case:

int main (void)
{
  unsigned long long x;
  int i;

  x = 2;
  for (i = 0; i < 64; i++)
    if (x & (1ULL << (64 - i - 1)))
      break;
  x = 4;
  for (i = 0; i < 64; i++)
    if (x & (1ULL << (64 - i - 1)))
      break;
  if (i != 61)
    abort ();
  return 0;
}

This aborts at -O and higher.

version: 3.5-tree-ssa 20031106
Comment 1 Falk Hueffner 2003-11-07 17:54:52 UTC
In combine, the shift in the second loop gets lost somehow. However, combine
on tree-ssa does the same as on mainline AFAIK, so most likely something
went wrong earlier already.
Comment 2 Andrew Pinski 2003-11-18 05:39:38 UTC
How does the tree dumps look?
Comment 3 falk.hueffner 2003-11-18 09:23:09 UTC
Subject: Re:  [tree-ssa] loop miscompilation

"pinskia at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:

> How does the tree dumps look?

;; Function main (main)

main ()
{
  int T.1;
  long long unsigned int T.2;
  unsigned int T.3;
  int T.4;
  int T.5;
  _Bool T.6;
  long long unsigned int x;
  int i;
  extern  abort;

  x = 2;;
  i = 0;;
  goto <D42f>;;
  <D427>:;;
  T.1 = 63 - i;;
  T.2 = x >> T.1;;
  T.3 = (unsigned int)T.2;;
  T.4 = (int)T.3;;
  T.5 = T.4 & 1;;
  T.6 = (_Bool)T.5;;
  if (T.6)
    {
      goto <D426>;;
    }
  else
    {
      
    };
  i = i + 1;;
  <D42f>:;;
  if (i <= 63)
    {
      goto <D427>;;
    }
  else
    {
      goto <D426>;;
    };
  <D426>:;;
  x = 4;;
  i = 0;;
  goto <D433>;;
  <D431>:;;
  T.1 = 63 - i;;
  T.2 = x >> T.1;;
  T.3 = (unsigned int)T.2;;
  T.4 = (int)T.3;;
  T.5 = T.4 & 1;;
  T.6 = (_Bool)T.5;;
  if (T.6)
    {
      goto <D430>;;
    }
  else
    {
      
    };
  i = i + 1;;
  <D433>:;;
  if (i <= 63)
    {
      goto <D431>;;
    }
  else
    {
      goto <D430>;;
    };
  <D430>:;;
  if (i != 61)
    {
      abort ();;
    }
  else
    {
      
    };
  return 0;;
}

Comment 4 Andrew Pinski 2003-11-18 09:35:17 UTC
Must be another RTL miscompiling problem.
Comment 5 Andrew Pinski 2003-12-01 04:38:47 UTC
Does this work now?
Comment 6 Falk Hueffner 2003-12-01 14:22:15 UTC
(In reply to comment #5)
> Does this work now?

No, still present with 20031201.

Interestingly, Diego's latest test result
(http://gcc.gnu.org/ml/gcc-testresults/2003-11/msg01294.html)
doesn't show builtin-bitops-1.c as failing, which is weird, because it
does fail for me...
Comment 7 Richard Henderson 2004-01-23 02:19:38 UTC
This is not loop, but combine.  Its line of reasoning goes

1)   (2 & (1 << (63 - i))) != 0)
2) = (((2 >> (63 - i)) & 1) != 0)
3) = (((2 >> ~i) & 1) != 0)         # From SHIFT_COUNT_TRUNCATED
4) = ~i == 1                        # Since only 2 >> 1 & 1 != 0
5) = i == -2

Of course, I is never negative; the correct result is I == 62.

Clearly the reasoning in step 4 is flawed if SHIFT_COUNT_TRUNCATED is set.
I've not yet located the exact place in combine that makes this leap...

Comment 8 GCC Commits 2004-01-23 20:27:41 UTC
Subject: Bug 12941

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	tree-ssa-20020619-branch
Changes by:	rth@gcc.gnu.org	2004-01-23 20:27:39

Modified files:
	gcc            : ChangeLog.tree-ssa combine.c 

Log message:
	PR opt/12941
	* combine.c (SHIFT_COUNT_TRUNCATED): Provide default value.
	(simplify_comparison): Don't simplify (eq (zero_extract c 1 r) 0)
	if SHIFT_COUNT_TRUNCATED is set.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.tree-ssa.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.2.1125&r2=1.1.2.1126
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/combine.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.299.2.32&r2=1.299.2.33

Comment 9 Richard Henderson 2004-01-23 22:38:41 UTC
http://gcc.gnu.org/ml/gcc-patches/2004-01/msg02512.html
Comment 10 Richard Henderson 2004-01-30 20:02:54 UTC
Subject: Re:  [tree-ssa] builtin-bitops-1.c miscompilation

On Fri, Jan 30, 2004 at 07:57:10PM -0000, jbuck at gcc dot gnu dot org wrote:
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>    Target Milestone|3.3.3                       |tree-ssa

No, the bug was latent.  I backported it.


r~
Comment 11 Joe Buck 2004-01-30 23:25:53 UTC
RTH writes:
> No, the bug was latent.  I backported it.

OK; since there was no record of a backport or a reference to anything other
than the tree-ssa branch, I assumed that the target milestone was an error.