Bug 52517 - Bug in PPC pointer arithmetic
Summary: Bug in PPC pointer arithmetic
Status: RESOLVED DUPLICATE of bug 49330
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.6.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-03-06 23:19 UTC by Jeroen Demeyer
Modified: 2024-11-15 05:34 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Testcase, file 1 (193 bytes, text/x-csrc)
2012-03-06 23:19 UTC, Jeroen Demeyer
Details
Testcase, file 2 (55 bytes, text/x-csrc)
2012-03-06 23:20 UTC, Jeroen Demeyer
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jeroen Demeyer 2012-03-06 23:19:51 UTC
Created attachment 26846 [details]
Testcase, file 1

This happens on an OS X 10.4 PPC machine (powerpc-apple-darwin8.11.0) using GCC 4.6.3.  Compile the test program with

gcc bug.c magic.c -O2 -o bug

I would expect the output to be 42, but instead I get 123456789.  Looking at the pointer arithmetic, M should be equal to L.  So it seems that GCC assumes that the assignment of L cannot affect M below.

The value of x matters a lot (but it clearly should not).  So far, I only managed to reproduce the problem when x is a pointer to something, casted to an unsigned long.

Replacing
    M = *(unsigned long*)(B + x);
by the equivalent
    M = ((unsigned long*)(B))[x/4];
solves the problem.

Might be related to PR49330?

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/Users/jdemeyer/sage-5.0.beta7-gcc/local/libexec/gcc/powerpc-apple-darwin8.11.0/4.6.3/lto-wrapper
Target: powerpc-apple-darwin8.11.0
Configured with: ../src/configure --prefix=/Users/jdemeyer/sage-5.0.beta7-gcc/local --with-local-prefix=/Users/jdemeyer/sage-5.0.beta7-gcc/local --with-gmp=/Users/jdemeyer/sage-5.0.beta7-gcc/local --with-mpfr=/Users/jdemeyer/sage-5.0.beta7-gcc/local --with-mpc=/Users/jdemeyer/sage-5.0.beta7-gcc/local --with-system-zlib --disable-multilib
Thread model: posix
gcc version 4.6.3 (GCC)
Comment 1 Jeroen Demeyer 2012-03-06 23:20:23 UTC
Created attachment 26847 [details]
Testcase, file 2
Comment 2 Andrew Pinski 2012-03-06 23:49:08 UTC
Not just related but it is the same issue as PR 49330.

*** This bug has been marked as a duplicate of bug 49330 ***
Comment 3 Andrew Pinski 2024-11-15 04:42:03 UTC
```
by the equivalent
    M = ((unsigned long*)(B))[x/4];
```

Note that is not equivalent and would cause this to be undefined. 

Only `    M = *(unsigned long*)(B + x);` is well defined.