extern void abort (void); struct S { unsigned char a, b, c, d[16]; }; void __attribute__ ((noinline)) foo (struct S *x, struct S *y) { int a, b; unsigned char c, *d, *e; b = x->b; d = x->d; e = y->d; a = 0; while (b) { if (b >= 8) { c = 0xff; b -= 8; } else { c = 0xff << (8 - b); b = 0; } e[a] = d[a] & c; a++; } } int main (void) { struct S x = { 0, 25, 0, { 0xaa, 0xbb, 0xcc, 0xdd }}; struct S y = { 0, 0, 0, { 0 }}; foo (&x, &y); if (x.d[0] != y.d[0] || x.d[1] != y.d[1] || x.d[2] != y.d[2] || (x.d[3] & 0x80) != y.d[3]) abort (); return 0; } On i686-linux with -O2 -m32 the loop is miscompiled, it copies over just first 24 bits and not the 25th bit. From quick skimming of the tree dumps, this looks messed up by ivopts pass. Seems to be a recent regression, at least 20060304 GCC 4.1.x worked fine, while 20060420 does not.
Caused by http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=112675
Does the mainline work if so something else that was needed was not backported.
Can you try the patch for PR25985 and see if it fixes your bug?
Actually it looks 100% the same testcase so that patch just needs backporting (I don't have time to test it at all).
Yes, it fixes it. Will regression test it on a bunch of arches and post to gcc-patches. Thanks.
Subject: Bug 27285 Author: jakub Date: Thu May 4 06:40:15 2006 New Revision: 113515 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113515 Log: PR tree-optimization/27285 Backport from mainline: 2006-03-28 Zdenek Dvorak <dvorakz@suse.cz> PR tree-optimization/25985 * tree-ssa-loop-niter.c (number_of_iterations_le, number_of_iterations_ne): Make comments more precise. (number_of_iterations_cond): Add only_exit argument. Use the fact that signed variables do not overflow only when only_exit is true. (loop_only_exit_p): New. (number_of_iterations_exit): Pass result of loop_only_exit_p to number_of_iterations_cond. * gcc.c-torture/execute/pr27285.c: New test. Added: branches/gcc-4_1-branch/gcc/testsuite/gcc.c-torture/execute/pr27285.c Modified: branches/gcc-4_1-branch/gcc/ChangeLog branches/gcc-4_1-branch/gcc/testsuite/ChangeLog branches/gcc-4_1-branch/gcc/tree-ssa-loop-niter.c
Subject: Bug 27285 Author: jakub Date: Thu May 4 06:43:50 2006 New Revision: 113516 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113516 Log: PR tree-optimization/27285 * gcc.c-torture/execute/pr27285.c: New test. Added: trunk/gcc/testsuite/gcc.c-torture/execute/pr27285.c Modified: trunk/gcc/testsuite/ChangeLog
I've now reconfirmed that this has been fixed on the gcc-4_1-branch by Jakub's backport of Zdenek's patch. Thanks to you both.