public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/32820]  New: optimizer malfunction when mixed with asm statements
@ 2007-07-19 13:16 jbuehler at spirentcom dot com
  2007-07-25 13:09 ` [Bug middle-end/32820] " jbuehler at spirentcom dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: jbuehler at spirentcom dot com @ 2007-07-19 13:16 UTC (permalink / raw)
  To: gcc-bugs

The below C code is a stripped down version of some code generated
by the GHC Haskell compiler.  When it is compiled with -O, incorrect
code is generated.  The Hp variable should live in %r7 but the compiled
routine does not set %r7.  It does set it when -O is omitted.

I checked a few versions of gcc and the problem is present in at least
the following versions:

3.0.2
3.1.1
3.2.3
3.3.6
3.4.6
4.0.1
4.0.4
4.1.2

compile line:

gcc -O -o temp.s -x c temp.i -fno-strict-aliasing -S

input file:

typedef signed char StgInt8;
typedef unsigned char StgWord8;
typedef signed short StgInt16;
typedef unsigned short StgWord16;
typedef signed int StgInt32;
typedef unsigned int StgWord32;
typedef signed long long int StgInt64;
typedef unsigned long long int StgWord64;
typedef StgInt32 StgInt;
typedef StgWord32 StgWord;
typedef StgInt16 StgHalfInt;
typedef StgWord16 StgHalfWord;
typedef void* StgAddr;
typedef StgWord32 StgChar;
typedef int StgBool;
typedef float StgFloat;
typedef double StgDouble;
typedef void StgVoid;
typedef struct StgClosure_ StgClosure;
typedef StgClosure* StgClosurePtr;
typedef StgWord* StgPtr;
typedef StgWord StgOffset;
typedef struct StgTSO_* StgTSOPtr;
typedef void* StgForeignPtr;
typedef StgInt StgStackOffset;
typedef StgWord* StgStackPtr;
typedef StgWord8 StgCode;
typedef StgPtr* StgArray;
typedef char* StgByteArray;
typedef void* StgStablePtr;
typedef void *(*(*StgFunPtr)(void))(void);
typedef StgFunPtr StgFun(void);
typedef StgChar C_;
typedef StgWord W_;
typedef StgWord* P_;
typedef P_* PP_;
typedef StgInt I_;
typedef StgAddr A_;
typedef const StgWord* D_;
typedef StgFunPtr F_;
typedef StgByteArray B_;
typedef StgClosurePtr L_;
typedef StgInt64 LI_;
typedef StgWord64 LW_;
typedef struct {
  StgFunPtr stgGCEnter1;
  StgFunPtr stgGCFun;
} StgFunTable;
typedef union {
    StgWord w;
    StgAddr a;
    StgChar c;
    StgInt8 i8;
    StgInt i;
    StgPtr p;
    StgClosurePtr cl;
    StgStackOffset offset;
    StgByteArray b;
    StgTSOPtr t;
} StgUnion;
typedef struct StgRegTable_ {
  StgUnion rR1;
  StgUnion rR2;
  StgUnion rR3;
  StgUnion rR4;
  StgUnion rR5;
  StgUnion rR6;
  StgUnion rR7;
  StgUnion rR8;
  StgUnion rR9;
  StgUnion rR10;
  StgFloat rF1;
  StgFloat rF2;
  StgFloat rF3;
  StgFloat rF4;
  StgDouble rD1;
  StgDouble rD2;
  StgWord64 rL1;
  StgPtr rSp;
  StgPtr rSpLim;
  StgPtr rHp;
  StgPtr rHpLim;
  struct StgTSO_ *rCurrentTSO;
  struct bdescr_ *rNursery;
  struct bdescr_ *rCurrentNursery;
  StgWord rHpAlloc;
} StgRegTable;
typedef struct Capability_ {
    StgFunTable f;
    StgRegTable r;
} Capability;
extern W_ MainCapability[];
register P_ Sp __asm__("%" "r4");
register P_ SpLim __asm__("%" "r6");
register P_ Hp __asm__("%" "r7");
register P_ HpLim __asm__("%" "r8");

F_ stg_returnToStackTop(void) {
W_ _c2;
_c2 = (W_)((&((Capability *)MainCapability)[0].r)->rCurrentTSO);
Sp = (P_)(*((P_)(_c2+52)));
SpLim = (P_)(_c2+140);
Hp = (P_)((*((P_)((W_)((&((Capability
*)MainCapability)[0].r)->rCurrentNursery)+4))) + (-0x4U));
HpLim = (P_)((*((P_)((W_)((&((Capability
*)MainCapability)[0].r)->rCurrentNursery)))) +
(((((I_)(*((P_)((W_)((&((Capability
*)MainCapability)[0].r)->rCurrentNursery)+24))))) * 0x1000U) + (-0x1U)));
do { void *_procedure = (void *)(*((P_)((W_)Sp + (0x0 * 0x4U)))); if (((int)
_procedure) & 2) _procedure = (void *)(*((int *) (_procedure - 2))); goto
*_procedure; } while(0);
}


-- 
           Summary: optimizer malfunction when mixed with asm statements
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jbuehler at spirentcom dot com
 GCC build triplet: hppa1.0-hp-hpux11.00
  GCC host triplet: hppa1.0-hp-hpux11.00
GCC target triplet: hppa1.0-hp-hpux11.00


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
  2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
@ 2007-07-25 13:09 ` jbuehler at spirentcom dot com
  2007-07-25 13:22 ` jbuehler at spirentcom dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: jbuehler at spirentcom dot com @ 2007-07-25 13:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from jbuehler at spirentcom dot com  2007-07-25 13:09 -------
This bug is also present in 4.0.1 for powerpc-ibm-aix5.2.0.0.  Change the
registers in the problematic code to r14, r15, r16, r17 -- one of the registers
is not set by the optimized routine, but is by the non-optimized routine.  I
presume that this is not a target-specific bug.


-- 

jbuehler at spirentcom dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jbuehler at spirentcom dot
                   |                            |com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
  2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
  2007-07-25 13:09 ` [Bug middle-end/32820] " jbuehler at spirentcom dot com
@ 2007-07-25 13:22 ` jbuehler at spirentcom dot com
  2007-07-25 13:28 ` jbuehler at spirentcom dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: jbuehler at spirentcom dot com @ 2007-07-25 13:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from jbuehler at spirentcom dot com  2007-07-25 13:22 -------
The same bug is present in the following versions of gcc for
sparc-sun-solaris2.9:

2.95.3
2.95
3.0.2
3.0.4
3.1.1
3.2.3
3.3.6
3.4.6
4.0.1
4.0.4
4.1.2
4.2.0

Use registers l1, l2, l3, l4 in the sample code to demonstrate the problem.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
  2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
  2007-07-25 13:09 ` [Bug middle-end/32820] " jbuehler at spirentcom dot com
  2007-07-25 13:22 ` jbuehler at spirentcom dot com
@ 2007-07-25 13:28 ` jbuehler at spirentcom dot com
  2007-08-09 11:38 ` jbuehler at spirentcom dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: jbuehler at spirentcom dot com @ 2007-07-25 13:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jbuehler at spirentcom dot com  2007-07-25 13:28 -------
I would appreciate a fix for this because the code being compiled is the GHC
compiler itself.  I have to compile GHC for hppa without optimization because I
have been unable to find a compiler options workaround for this problem.  The
unoptimized GHC has a 40 megabyte text section and I am hoping for significant
speed improvements once the optimizer is usable.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
  2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
                   ` (2 preceding siblings ...)
  2007-07-25 13:28 ` jbuehler at spirentcom dot com
@ 2007-08-09 11:38 ` jbuehler at spirentcom dot com
  2007-11-26 14:10 ` steven at gcc dot gnu dot org
  2009-02-04  4:24 ` danglin at gcc dot gnu dot org
  5 siblings, 0 replies; 9+ messages in thread
From: jbuehler at spirentcom dot com @ 2007-08-09 11:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jbuehler at spirentcom dot com  2007-08-09 11:38 -------
Created an attachment (id=14045)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=14045&action=view)
gcc 4.0.4 global register variable optimizer patch

The attached patch fixes the optimizer bug for gcc 4.0.4 and allows GHC 6.6.1
to be compiled with optimization turned on.  The problem is that lifetime
analysis is marking global registers as REG_UNUSED and then the combiner pass
is eliminating them.  The fix is to not mark global registers as unused.

The fix is based on a review of the flow.c code for gcc 4.2.0.  Code was added
to flow.c somewhere after 4.0.4 to handle "stack registers" and no mark them as
REG_UNUSED.  This patch merely mimics "stack register" handling in flow.c for
global registers.

I am not a gcc expert and make no claims for the correctness of this patch --
it does fix my ghc compile though.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
  2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
                   ` (3 preceding siblings ...)
  2007-08-09 11:38 ` jbuehler at spirentcom dot com
@ 2007-11-26 14:10 ` steven at gcc dot gnu dot org
  2009-02-04  4:24 ` danglin at gcc dot gnu dot org
  5 siblings, 0 replies; 9+ messages in thread
From: steven at gcc dot gnu dot org @ 2007-11-26 14:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from steven at gcc dot gnu dot org  2007-11-26 14:10 -------
Thanks for the effort you have put into this.

Your bug is probably fixed in GCC 4.3 (for which the entire dataflow module has
been rewritten from scratch) but it probably still exists in GCC 4.2.

As you have shown, the bug exists since GCC 3.0, so the bug is not a
regression.  GCC 4.2 and earlier are in "regression fixes only" mode.  This
means, I'm sorry to say, that your bug will not be fixed for these already
released GCC versions.

Could you please test this bug with a recent GCC 4.3 snapshot?  If the bug
persists, we can still fix the problem before GCC 4.3 is released.


-- 

steven at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
  2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
                   ` (4 preceding siblings ...)
  2007-11-26 14:10 ` steven at gcc dot gnu dot org
@ 2009-02-04  4:24 ` danglin at gcc dot gnu dot org
  5 siblings, 0 replies; 9+ messages in thread
From: danglin at gcc dot gnu dot org @ 2009-02-04  4:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from danglin at gcc dot gnu dot org  2009-02-04 04:24 -------
I tried the testcase with 4.4.0.  The problem is not fixed.  All the explicit
register variables are optimized away at -O.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
       [not found] <bug-32820-4@http.gcc.gnu.org/bugzilla/>
  2010-10-11 19:18 ` siarhei.siamashka at gmail dot com
@ 2010-10-25 10:18 ` siarhei.siamashka at gmail dot com
  1 sibling, 0 replies; 9+ messages in thread
From: siarhei.siamashka at gmail dot com @ 2010-10-25 10:18 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820

--- Comment #8 from Siarhei Siamashka <siarhei.siamashka at gmail dot com> 2010-10-25 10:17:47 UTC ---
On the second thought, this bug was about global variables. But my problem is
related to the use of local variables. So I have submitted a separate PR46164
about it.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/32820] optimizer malfunction when mixed with asm statements
       [not found] <bug-32820-4@http.gcc.gnu.org/bugzilla/>
@ 2010-10-11 19:18 ` siarhei.siamashka at gmail dot com
  2010-10-25 10:18 ` siarhei.siamashka at gmail dot com
  1 sibling, 0 replies; 9+ messages in thread
From: siarhei.siamashka at gmail dot com @ 2010-10-11 19:18 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32820

Siarhei Siamashka <siarhei.siamashka at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |siarhei.siamashka at gmail
                   |                            |dot com

--- Comment #7 from Siarhei Siamashka <siarhei.siamashka at gmail dot com> 2010-10-11 19:18:46 UTC ---
Looks like this or similar "Variables in Specified Registers" bug is also
reproducible on ARM with gcc 4.5.1

$ cat test.c

int f(int a)
{
  register int result asm("r0");
  asm (
    "add    r0, %[a], #123\n"
    : [result] "=&r" (result)
    : [a]      "r"   (a)
  );
  return result;
}

$ gcc -O2 -c test.c
$ objdump -d test.o

00000000 <f>:
   0:   e280007b        add     r0, r0, #123    ; 0x7b
   4:   e1a00003        mov     r0, r3
   8:   e12fff1e        bx      lr

Here the local variable 'result' gets assigned to register r3 instead of r0
causing all kind of problems.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-10-25 10:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-19 13:16 [Bug c/32820] New: optimizer malfunction when mixed with asm statements jbuehler at spirentcom dot com
2007-07-25 13:09 ` [Bug middle-end/32820] " jbuehler at spirentcom dot com
2007-07-25 13:22 ` jbuehler at spirentcom dot com
2007-07-25 13:28 ` jbuehler at spirentcom dot com
2007-08-09 11:38 ` jbuehler at spirentcom dot com
2007-11-26 14:10 ` steven at gcc dot gnu dot org
2009-02-04  4:24 ` danglin at gcc dot gnu dot org
     [not found] <bug-32820-4@http.gcc.gnu.org/bugzilla/>
2010-10-11 19:18 ` siarhei.siamashka at gmail dot com
2010-10-25 10:18 ` siarhei.siamashka at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).