12941 – [tree-ssa] builtin-bitops-1.c miscompilation (original) (raw)

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 Drea 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 ;; :;; 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 ;; } else {

};

i = i + 1;; :;; if (i <= 63) { goto ;; } else { goto ;; }; :;; x = 4;; i = 0;; goto ;; :;; 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 ;; } else {

};

i = i + 1;; :;; if (i <= 63) { goto ;; } else { goto ;; }; :;; if (i != 61) { abort ();; } else {

};

return 0;; }

Comment 4 Drea Pinski 2003-11-18 09:35:17 UTC

Must be another RTL miscompiling problem.

Comment 5 Drea Pinski 2003-12-01 04:38:47 UTC

Does this work now?

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 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.