Bug 48090 - [4.5 Regression] gcc 4.5.2 miscompilation when building on arm
Summary: [4.5 Regression] gcc 4.5.2 miscompilation when building on arm
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.5.2
: P2 normal
Target Milestone: 4.5.3
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-03-12 09:01 UTC by arnaud patard
Modified: 2011-04-27 09:44 UTC (History)
4 users (show)

See Also:
Host:
Target: arm
Build:
Known to work: 4.4.5, 4.6.0
Known to fail:
Last reconfirmed: 2011-04-10 10:41:58


Attachments
test file (37.65 KB, application/octet-stream)
2011-03-12 09:02 UTC, arnaud patard
Details
standalone test case (406 bytes, text/plain)
2011-03-12 17:38 UTC, Mikael Pettersson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description arnaud patard 2011-03-12 09:01:36 UTC
With gcc-4.5.2, even with gcc-4.5-20110310, the following line of code gets miscompiled (See attachment) :

size -= nblk * 512;

size and nblk are 64 bits.
gcc tries to compute -nblk. nblk is stored in r0,r1. gcc produces the following asm code :

        rsbs    r1, r0, #0
        rsc     r2, r1, #0

So r1 gets corrupted by the rsbs insn, thus getting wrong value in r2.

I'm building the asm code with :
gcc -O2  -march=armv5t list2.i -o - -S

When I use :
gcc -O2 -fno-cse-follow-jumps  -march=armv5t list2.i -o - -S

I get :

        rsbs    r0, r0, #0
        rsc     r1, r1, #0

which is fine.
Comment 1 arnaud patard 2011-03-12 09:02:30 UTC
Created attachment 23636 [details]
test file
Comment 2 Mikael Pettersson 2011-03-12 12:03:18 UTC
I get correct-looking code on armv5tel-linux with vanilla gcc-4.6-20110305 and gcc-4.5-20110310, with 4.4 the code looks different but not obviously broken.

Can you make the test case standalone and include a runtime check for the possible miscompilation?

How was your gcc configured?
Comment 3 arnaud patard 2011-03-12 12:16:12 UTC
(In reply to comment #2)
> I get correct-looking code on armv5tel-linux with vanilla gcc-4.6-20110305 and
> gcc-4.5-20110310, with 4.4 the code looks different but not obviously broken.

I said -march=armv5t (or -march=armv4t I guess). It's important as -march=armv5te won't trigger the bug.

>
> Can you make the test case standalone and include a runtime check for the
> possible miscompilation?

My test case was to build tar and run the delete01.at check but it's not really a standalone test.
So far, I've not managed to have a test case small than compiling the preprocessed file.

> 
> How was your gcc configured?

I reproduce it with my gcc and debian gcc 4.5.2 so I believe this rules out misconfiguration.
Comment 4 Mikael Pettersson 2011-03-12 15:42:13 UTC
I have a runtime test case (given test file + separate file with main and missing support functions) that works with gcc-4.5-20110310 -march=armv5te but fails with -march=armv5t.  gcc-4.4.5 and 4.6-20110305 work with both armv5te and armv5t.
Comment 5 Mikael Pettersson 2011-03-12 17:38:12 UTC
Created attachment 23637 [details]
standalone test case

Reduced standalone test case.  Fails with gcc-4.5-20110310 -march=armv5t, works with -march=armv5te.  Also works with gcc-4.4 and gcc-4.6.
Comment 6 Mikael Pettersson 2011-03-12 21:19:40 UTC
The test case was fixed for 4.6 by r159644.  However, that patch
<http://gcc.gnu.org/ml/gcc-patches/2010-05/msg01528.html> was described as a minor improvement to double-word register allocation and not a correctness fix, so the underlying bug may be latent on trunk.

I'll investigate some more tomorrow.
Comment 7 Mikael Pettersson 2011-03-13 15:09:36 UTC
The miscompilation on 4.5 branch started with r154181, the PR42031 fix; see
<http://gcc.gnu.org/ml/gcc-patches/2009-11/msg00725.html>.

On the standalone test case, r154181 changed the -march=armv5t -O2 code as follows:

--- pr48090.s-r154180   2011-03-13 15:39:16.000000000 +0100
+++ pr48090.s-r154181   2011-03-13 15:43:21.000000000 +0100
@@ -81,11 +81,9 @@
        mov     r6, r1
        bl      seek_archive
        cmp     r1, #0
-       mov     r3, r0
-       mov     r4, r1
        blt     .L14
-       rsbs    r1, r3, #0
-       rsc     r2, r4, #0
+       rsbs    r1, r0, #0
+       rsc     r2, r1, #0
        mov     r4, r2, asl #9
        mov     r3, r1, asl #9
        orr     r4, r4, r1, lsr #23

which breaks the second 'rsc' insn since it now reads the updated r1 value instead of the original one.
Comment 8 Mikael Pettersson 2011-03-13 19:32:46 UTC
The test case broke due to the combination of r154181 and r147087.  r147087 is Kazu's "expmed.c: Improve multiplication by more constants" patch, see <http://gcc.gnu.org/ml/gcc-patches/2009-04/msg00915.html>.

Backporting either of these to 4.4.5 doesn't break the test case, but when both are backported at the same time it breaks.  And reverting r147087 from current 4.5 branch unbreaks the test case.
Comment 9 arnaud patard 2011-03-13 20:40:27 UTC
I confirm that backporting r159644 and r159683 make things work. From comment 8, I guess that the bug is still there and that one can still hit it sooner or later, right  ? (btw, amazing job)
Comment 10 Richard Biener 2011-04-10 10:41:58 UTC
(In reply to comment #9)
> I confirm that backporting r159644 and r159683 make things work. From comment
> 8, I guess that the bug is still there and that one can still hit it sooner or
> later, right  ? (btw, amazing job)

It probably papers over it as you guessed.

This bug lacks proper analysis.
Comment 11 Ramana Radhakrishnan 2011-04-11 10:24:09 UTC
(In reply to comment #10)
> (In reply to comment #9)
> > I confirm that backporting r159644 and r159683 make things work. From comment
> > 8, I guess that the bug is still there and that one can still hit it sooner or
> > later, right  ? (btw, amazing job)
> 
> It probably papers over it as you guessed.
> 
> This bug lacks proper analysis.

The problem is latent in all versions of the compiler and is to do with the pattern *arm_negdi2 and has nothing to do with the afore mentioned revisions. Will submit this after a round of testing.



Index: gcc/config/arm/arm.md
===================================================================
--- gcc/config/arm/arm.md	(revision 172252)
+++ gcc/config/arm/arm.md	(working copy)
@@ -3554,7 +3554,7 @@
 ;; The constraints here are to prevent a *partial* overlap (where %Q0 == %R1).
 ;; The first alternative allows the common case of a *full* overlap.
 (define_insn "*arm_negdi2"
-  [(set (match_operand:DI         0 "s_register_operand" "=&r,r")
+  [(set (match_operand:DI         0 "s_register_operand" "=r,&r")
 	(neg:DI (match_operand:DI 1 "s_register_operand"  "0,r")))
    (clobber (reg:CC CC_REGNUM))]
   "TARGET_ARM"
Comment 12 Ramana Radhakrishnan 2011-04-12 13:42:52 UTC
Author: ramana
Date: Tue Apr 12 13:42:48 2011
New Revision: 172318

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172318
Log:

Fix PR target/48090

2011-04-12  Ramana Radhakrishnan  <ramana.radhakrishnan@linaro.org>

       PR target/48090
       * config/arm/arm.md (*arm_negdi2): Fix early clobber constraints.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/arm/arm.md
Comment 13 Ramana Radhakrishnan 2011-04-12 13:52:49 UTC
Author: ramana
Date: Tue Apr 12 13:52:46 2011
New Revision: 172320

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172320
Log:

Fix PR target/48090


Modified:
    branches/gcc-4_5-branch/gcc/ChangeLog
    branches/gcc-4_5-branch/gcc/config/arm/arm.md
Comment 14 Ramana Radhakrishnan 2011-04-12 13:53:39 UTC
Still need to backport and test on the 4.6 branch. That is next.

Ramana
Comment 15 froydnj@codesourcery.com 2011-04-12 13:55:57 UTC
On Tue, Apr 12, 2011 at 01:53:48PM +0000, ramana at gcc dot gnu.org wrote:
> Still need to backport and test on the 4.6 branch. That is next.

Small procedural note: it is preferred to go trunk -> 4.6 -> 4.5, rather
than trunk -> 4.5 -> 4.6, even if the bug is reported against 4.5.
Comment 16 Ramana Radhakrishnan 2011-04-13 07:41:13 UTC
Author: ramana
Date: Wed Apr 13 07:41:08 2011
New Revision: 172363

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172363
Log:

Fix PR target/48090


Modified:
    branches/gcc-4_6-branch/gcc/ChangeLog
    branches/gcc-4_6-branch/gcc/config/arm/arm.md
Comment 17 Ramana Radhakrishnan 2011-04-13 07:56:00 UTC
(In reply to comment #15)
> On Tue, Apr 12, 2011 at 01:53:48PM +0000, ramana at gcc dot gnu.org wrote:
> > Still need to backport and test on the 4.6 branch. That is next.
> 
> Small procedural note: it is preferred to go trunk -> 4.6 -> 4.5, rather
> than trunk -> 4.5 -> 4.6, even if the bug is reported against 4.5.

Yes I know but it was just a function of what tree I had ready to test and finish at that point of time. Will do it in the right order from next time.

I don't have a 4.4 tree handy and this bug should be latent there as well - so if someone wants to backport it please do so but I don't have the time to do it in the next couple of weeks. 

cheers
Ramana
Comment 18 Ramana Radhakrishnan 2011-04-27 09:44:05 UTC
Fixed.