Bug 8896 - '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
Bug#: 8896 Product:  gcc Version: 3.2.1
Host: i386-redhat-linux Target: arm-linux Build: i386-redhat-linux
Status: RESOLVED Severity: normal Priority: P3
Resolution: FIXED Assigned To: rearnsha@gcc.gnu.org Reported By: enrico.scholz@informatik.tu-chemnitz.de
Component: rtl-optimization Target Milestone: 3.4.0
Summary: '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
Keywords:  wrong-code
Opened: 2002-12-10 15:46
Description:   Last confirmed: Opened: 2002-12-10 15:46
The arm-linux-gcc miscompiles XScale linux-kernels (the attached testcase is
derived from fs/binfmt_elf.c of linux-2.5.49 + various ARM patches).

Because of other environment-problems, I do not have a complete test-program,
but the miscompilation is visible in the disassembled .o-file. The interesting
code is:

 ---- objdump -d binfmt_elf.o
  70:   e3a04002        mov     r4, #2  ; 0x2
  7c:   e0844004        add     r4, r4, r4
  84:   e1a0e004        mov     lr, r4
  e0:   e50be0b0        str     lr, [fp, -#176]


These statements are the result of:

|  60          elf_info[ei_index++] =  3; ...

which is wrong. Other ELF-tags are incorrect too, but this is the most obvious
one.

Release:
gcc-3.2.1

Environment:
Reading specs from /usr/lib/gcc-lib/arm-linux/3.2.1/specs
Configured with: ../configure --host=i386-redhat-linux --target=arm-linux
--prefix=/usr --mandir=/usr/share/man --disable-checking
--with-cpu=strongarm1100 --enable-languages=c
Thread model: posix
gcc version 3.2.1

How-To-Repeat:
Compile the testprogram with

| arm-linux-gcc -O2 -fno-strict-aliasing -mcpu=xscale -c binfmt_elf.c

Watch the output with 'objdump -d binfmt_elf.o'

------- Comment #1 From enrico.scholz@informatik.tu-chemnitz.de 2002-12-10 15:46 -------
Fix:
The following change should fix the problem, but I'd be grateful if you
could test it for me, since I cannot execute your testcase.


--- reload1.c.orig	Thu Oct 10 16:40:20 2002
+++ reload1.c	Thu Dec 12 14:43:56 2002
@@ -8674,7 +8674,9 @@
 	 ... (MEM (PLUS (REGZ) (REGY)))... .
 
 	 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
-	 and that we know all uses of REGX before it dies.  */
+	 and that we know all uses of REGX before it dies.  
+	 Also, explicitly check that REGX != REGY; our life information 
+	 does not yet show whether REGY changes in this insn.  */
       set = single_set (insn);
       if (set != NULL_RTX
 	  && GET_CODE (SET_DEST (set)) == REG
@@ -8684,6 +8686,7 @@
 	  && GET_CODE (SET_SRC (set)) == PLUS
 	  && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
 	  && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
+	  && !rtx_equal_p (XEXP (SET_SRC (set), 1), SET_DEST (set))
 	  && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
 	{
 	  rtx reg = SET_DEST (set);

------- Comment #2 From Richard Earnshaw 2002-12-11 09:57 -------
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed.  The same bug does not occur with the latest compiler, but I think that's because an earlier pass of the compiler has optimized the problem away before it trips the bug.  
    
    Initial investigation suggests this is a problem with reload_combine().

------- Comment #3 From Richard Earnshaw 2002-12-12 07:03 -------
Responsible-Changed-From-To: unassigned->rearnsha
Responsible-Changed-Why: .

------- Comment #4 From Richard Earnshaw 2002-12-12 15:14 -------
From: Richard Earnshaw <rearnsha@arm.com>
To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org,
        enrico.scholz@informatik.tu-chemnitz.de, gcc-bugs@gcc.gnu.org
Cc:  
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2' generates
 wrong code
Date: Thu, 12 Dec 2002 15:14:52 +0000

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8896
 
 I've attached what I believe to be the fix to the above PR.  It would be 
 helpful if you could give it a whirl.
 
 R.
 


------- Comment #5 From enrico.scholz@sigma-chemnitz.de 2002-12-16 18:59 -------
From: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org,
   enrico.scholz@informatik.tu-chemnitz.de, gcc-bugs@gcc.gnu.org,
   rearnsha@gcc.gnu.org
Cc:  
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
Date: 16 Dec 2002 18:59:14 +0100

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8896
 
 The patch fixes the testcase, the kernel is running fine with it
 and I have not seen other/new errors since using it.
 
 
 Thanks for the fast fix
 Enrico

------- Comment #6 From Steven Bosscher 2003-03-22 10:05 -------
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed according to reporter.  Richard Earnshaw posted a patch for this months ago -- apparently without mentioning the PR in the ChangeLog.

------- Comment #7 From Richard Earnshaw 2003-03-22 13:28 -------
From: Richard Earnshaw <rearnsha@arm.com>
To: steven@gcc.gnu.org, enrico.scholz@informatik.tu-chemnitz.de,
        gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, rearnsha@gcc.gnu.org,
        gcc-gnats@gcc.gnu.org
Cc: Richard.Earnshaw@arm.com
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2' 
 generates wrong code
Date: Sat, 22 Mar 2003 13:28:25 +0000

 > Synopsis: '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
 > 
 > State-Changed-From-To: analyzed->closed
 > State-Changed-By: steven
 > State-Changed-When: Sat Mar 22 10:05:37 2003
 > State-Changed-Why:
 >     Fixed according to reporter.  Richard Earnshaw posted a patch for this months ago -- apparently without mentioning the PR in the ChangeLog.
 > 
 > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8896
 
 That's 'cos I never got around to committing the patch...
 
 R.
 


------- Comment #8 From Steven Bosscher 2003-03-22 13:54 -------
State-Changed-From-To: closed->analyzed
State-Changed-Why: The patch was never commited.

------- Comment #9 From s.bosscher@student.tudelft.nl 2003-03-22 14:33 -------
From: Steven Bosscher <s.bosscher@student.tudelft.nl>
To: Richard.Earnshaw@arm.com
Cc: steven@gcc.gnu.org, enrico.scholz@informatik.tu-chemnitz.de,
	gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, rearnsha@gcc.gnu.org,
	gcc-gnats@gcc.gnu.org
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2'
	generates wrong code
Date: 22 Mar 2003 14:33:14 +0100

 Op za 22-03-2003, om 14:28 schreef Richard Earnshaw:
 > > Synopsis: '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
 > > 
 > > State-Changed-From-To: analyzed->closed
 > > State-Changed-By: steven
 > > State-Changed-When: Sat Mar 22 10:05:37 2003
 > > State-Changed-Why:
 > >     Fixed according to reporter.  Richard Earnshaw posted a patch for this months ago -- apparently without mentioning the PR in the ChangeLog.
 > > 
 > > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8896
 > 
 > That's 'cos I never got around to committing the patch...
 > 
 > R.
 
 Ah, that explains why I couldn't find the PR in the ChangeLog.
 Should I re-open the PR?
 
 Greetz
 Steven
 
 

------- Comment #10 From mentre@tcl.ite.mee.com 2003-05-06 15:03 -------
From: David Mentre <Mentre@tcl.ite.mee.com>
To: "'gcc-gnats@gcc.gnu.org'" <gcc-gnats@gcc.gnu.org>,
   "'enrico.scholz@informatik.tu-chemnitz.de'"
	 <enrico.scholz@informatik.tu-chemnitz.de>,
   "'gcc-bugs@gcc.gnu.org'"
	 <gcc-bugs@gcc.gnu.org>,
   "'gcc-prs@gcc.gnu.org'" <gcc-prs@gcc.gnu.org>,
   "'rearnsha@gcc.gnu.org'" <rearnsha@gcc.gnu.org>
Cc:  
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2' ge
	nerates wrong code
Date: Tue, 6 May 2003 15:03:23 +0200 

 Hello,
 
 The patch describe in the below gnats entry has never been applied to
 official gcc tree. However, this patch is systematically applied to =
 each gcc
 generated for arm-* target.=20
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=3Dview%20audit-trail&database=
 =3Dgcc&p
 r=3D8896
 
 Is this patch really needed? If true, why isn't it applied to official =
 gcc
 tree? If not, then people using this patch should know that this patch =
 is
 not needed.
 
 Best regards,
 david
 --=20
 David Mentr=E9 <mentre@tcl.ite.mee.com> - Research engineer
 Mitsubishi Electric ITE-TCL / European Telecommunication Research =
 Laboratory
 
 Phone: +33 2 23 45 58 29 / Fax: +33 2 23 45 58 59=20
 http://www.mitsubishi-electric-itce.fr
 


------- Comment #11 From Dara Hazeghi 2003-05-14 10:44 -------
From: Dara Hazeghi <dhazeghi@yahoo.com>
To: gcc-gnats@gcc.gnu.org, rearnsha@gcc.gnu.org
Cc: enrico.scholz@informatik.tu-chemnitz.de
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
Date: Wed, 14 May 2003 10:44:01 -0700

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit- 
 trail&database=gcc&pr=8896
 
 Hello,
 
 this PR has a patch that seems not to have been applied yet. Is this  
 the case? Richard, would it be possible try to get this patch reviewed  
 and checked in soon? Thanks,
 
 Dara
 


------- Comment #12 From Daniel Jacobowitz 2003-05-15 14:36 -------
From: Daniel Jacobowitz <drow@mvista.com>
To: Richard Earnshaw <rearnsha@arm.com>
Cc: gcc-gnats@gcc.gnu.org, enrico.scholz@informatik.tu-chemnitz.de,
	gcc-bugs@gcc.gnu.org
Subject: Re: optimization/8896: '-mcpu=xscale -fno-strict-aliasing -O2' generates wrong code
Date: Thu, 15 May 2003 14:36:28 -0400

 On Thu, Dec 12, 2002 at 03:14:52PM +0000, Richard Earnshaw wrote:
 > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8896
 > 
 > I've attached what I believe to be the fix to the above PR.  It would be 
 > helpful if you could give it a whirl.
 
 I've added an updated testcase (produced after much labor by Russell
 King) which shows the problem in 3.2.3.  The testcase is extremely
 delicate.  It doesn't show the problem in 3.3 or HEAD, but I wouldn't
 be at all surprised if the problem was still there.  Making a
 testcase to show it is beyond me though.
 
 For the curious, here's a diff of the first differing dump with and
 without Richard's patch, using the 3.2 CVS branch.  It fails using
 only "-march=armv4 -O2".
 
 --- 32.s.22.postreload	2003-05-15 14:29:58.000000000 -0400
 +++ 32p.s.22.postreload	2003-05-15 14:29:59.000000000 -0400
 @@ -155,18 +155,25 @@
  (note 79 843 95 NOTE_INSN_DELETED)
  
  (insn 95 79 107 (set (reg/v:SI 11 fp [47])
 -        (const_int 2 [0x2])) 175 {*arm_movsi_insn} (insn_list:REG_DEP_ANTI 79 (nil))
 -    (nil))
 +        (const_int 1 [0x1])) 175 {*arm_movsi_insn} (insn_list:REG_DEP_ANTI 79 (nil))
 +    (expr_list:REG_EQUAL (const_int 1 [0x1])
 +        (nil)))
 +
 +(note 107 95 117 NOTE_INSN_DELETED)
  
 -(note 107 95 129 NOTE_INSN_DELETED)
 +(insn 117 107 129 (set (reg/v:SI 11 fp [47])
 +        (plus:SI (reg/v:SI 11 fp [47])
 +            (reg/f:SI 11 fp))) 4 {*arm_addsi3} (insn_list:REG_DEP_ANTI 107 (insn_list 95 (nil)))
 +    (expr_list:REG_EQUAL (const_int 2 [0x2])
 +        (nil)))
  
 -(note 129 107 135 NOTE_INSN_DELETED)
 +(note 129 117 135 NOTE_INSN_DELETED)
  
  (note 135 129 137 NOTE_INSN_DELETED)
  
  (insn 137 135 141 (set (reg/v:SI 11 fp [47])
          (plus:SI (reg/v:SI 11 fp [47])
 -            (reg/f:SI 11 fp))) 4 {*arm_addsi3} (insn_list:REG_DEP_ANTI 129 (insn_list 117 (nil)))
 +            (const_int 1 [0x1]))) 4 {*arm_addsi3} (insn_list:REG_DEP_ANTI 129 (insn_list 117 (nil)))
      (expr_list:REG_EQUAL (const_int 3 [0x3])
          (nil)))
  
 @@ -177,7 +184,7 @@
  
  (insn 197 141 221 (set (reg/v:SI 11 fp [47])
          (plus:SI (reg/v:SI 11 fp [47])
 -            (const_int 2 [0x2]))) 4 {*arm_addsi3} (insn_list:REG_DEP_ANTI 141 (insn_list:REG_DEP_OUTPUT 137 (insn_list 135 (nil))))
 +            (const_int 3 [0x3]))) 4 {*arm_addsi3} (insn_list:REG_DEP_ANTI 141 (insn_list:REG_DEP_OUTPUT 137 (insn_list 135 (nil))))
      (expr_list:REG_EQUAL (const_int 6 [0x6])
          (nil)))
  
 
 
 -- 
 Daniel Jacobowitz
 MontaVista Software                         Debian GNU/Linux Developer

------- Comment #13 From CVS Commits 2003-10-31 15:14 -------
Subject: Bug 8896

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	rearnsha@gcc.gnu.org	2003-10-31 15:14:22

Modified files:
	gcc            : ChangeLog 

Log message:
	Add PR to:
	PR optimization/8896
	* postreload.c (reload_combine): Check that REGY doesn't die in an
	insn of the form (set (regx) (plus (regx) (regy))), ie REGX != REGY.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.1621&r2=2.1622


------- Comment #14 From Richard Earnshaw 2003-10-31 15:15 -------
Patch applied to trunk. The 3.2 branch is dead, and this problem hasn't been
reproduced on the 3.3 branch.