public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug inline-asm/57299] New: Inline assembly memory dependencies produce spurious loads, register pressure, compilation failures
@ 2013-05-15 18:08 umbricola at gmail dot com
  2013-05-15 18:14 ` [Bug inline-asm/57299] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: umbricola at gmail dot com @ 2013-05-15 18:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 57299
           Summary: Inline assembly memory dependencies produce spurious
                    loads, register pressure, compilation failures
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: inline-asm
          Assignee: unassigned at gcc dot gnu.org
          Reporter: umbricola at gmail dot com

Consider this function with an inline assembly statement that takes two pointer
inputs in named registers and declares that it reads and writes the memory they
point to.

void f(char *x, char *y) {
  register char *p asm("edi") = x;
  register char *q asm("esi") = y;
  asm(""
      : "=m" (*x), "=m" (*y)
      : "r" (p), "r" (q), "m" (*x), "m" (*y));
}

This function should compile cleanly, and the disassembly should have only the
two instructions to load edi and esi from the stacked function arguments,
preceded by the prologue of f() and followed by the epilogue of f().

In fact this function is rejected by gcc -m32 -fPIC -c -o f.o f.c:

f.c: In function ‘f’:
f.c:4:3: error: ‘asm’ operand has impossible constraints
   asm(""
   ^

This error is wrong.  I am using only two named registers, neither of which is
ebx (which is reserved for the implementation of PIC).  There are three
general-purpose registers left, not to mention that loading edi and esi from
the stack shouldn't need any scratch registers anyway.

More strangely, removing any one or more of the four memory inputs and outputs
causes the function to compile successfully.  Memory dependencies should not
have any effect on register bindings entering and leaving the inline assembly
statement, only on optimizations in surrounding code.  Removing the -fPIC
option (which frees ebx) also makes the function compile.

If I remove one of the memory dependencies and disassemble the generated object
code, I see the two expected loads to edi and esi from the stack followed by
three unexpected loads to eax, ecx, and edx from the stack.  If I leave all the
memory dependencies and remove -fPIC instead, then all four of eax, ebx, ecx,
and edx get loaded after the asm statement.

It appears that gcc can convert memory dependencies of inline assembly
statements into useless loads into unrelated general-purpose registers.  This
action is always inefficient and sometimes spuriously exhausts the
general-purpose registers, causing valid code to be rejected.

I am running a 64-bit gcc 4.8.0 that I compiled from sources.  gcc -v says:

Using built-in specs.
COLLECT_GCC=/home/cmihelic/gcc-4.8.0/install/bin/gcc
COLLECT_LTO_WRAPPER=/home/cmihelic/gcc-4.8.0/install/libexec/gcc/x86_64-unknown-linux-gnu/4.8.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --prefix=/home/cmihelic/gcc-4.8.0/install
Thread model: posix
gcc version 4.8.0 (GCC) 

On a different Linux machine I have also tried several 64-bit gcc installations
I did not compile myself: versions 4.7.2, 4.5.0, 4.4.5, 4.4.3, 4.1.2, and
3.3.1.  They fail similarly, although the error message used to be

f.c:4: error: can't find a register in class `GENERAL_REGS' while reloading
`asm'

Thus this behavior is at least ten years old.
>From gcc-bugs-return-422385-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Wed May 15 18:11:06 2013
Return-Path: <gcc-bugs-return-422385-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 8204 invoked by alias); 15 May 2013 18:11:06 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 8149 invoked by uid 48); 15 May 2013 18:11:02 -0000
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/57287] GCC 4.9.0 fails to build GDB on Ubuntu 12.04
Date: Wed, 15 May 2013 18:11:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: middle-end
X-Bugzilla-Version: 4.9.0
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: pinskia at gcc dot gnu.org
X-Bugzilla-Status: WAITING
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields: component bug_severity
Message-ID: <bug-57287-4-fOWAIyssf6@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-57287-4@http.gcc.gnu.org/bugzilla/>
References: <bug-57287-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2013-05/txt/msg01058.txt.bz2
Content-length: 353

http://gcc.gnu.org/bugzilla/show_bug.cgi?idW287

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |middle-end
           Severity|blocker                     |normal


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

end of thread, other threads:[~2013-05-15 21:59 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-15 18:08 [Bug inline-asm/57299] New: Inline assembly memory dependencies produce spurious loads, register pressure, compilation failures umbricola at gmail dot com
2013-05-15 18:14 ` [Bug inline-asm/57299] " pinskia at gcc dot gnu.org
2013-05-15 18:22 ` umbricola at gmail dot com
2013-05-15 18:24 ` pinskia at gcc dot gnu.org
2013-05-15 18:44 ` umbricola at gmail dot com
2013-05-15 18:53 ` pinskia at gcc dot gnu.org
2013-05-15 19:20 ` umbricola at gmail dot com
2013-05-15 20:16 ` pinskia at gcc dot gnu.org
2013-05-15 21:57 ` umbricola at gmail dot com
2013-05-15 21:59 ` pinskia at gcc dot gnu.org

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