This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/40078] New: passing label to inline asm "i" constraint generates bad code


Somewhat to my surprise, the gcc accepts the following inline asm syntax:

asm("jmp %0" : : "i"(&&some_label));

The output is what you'd expect: assuming some_label (in C/C++) is associated
with the assembler label .LLBF4 gives:

jmp .LLBF4

Unfortunately, the optimizer plays havoc with things by happily eliminating the
code associated with that label if it is otherwise unused. Consider the
following code: 

static inline int foo() { return 10; }
int could_be_anything;
long test_label(int volatile* ptr) {
    int rval = 0;
    int dummy = 5;
    static void* const gotos[2] = {&&DONE, &&ERROR};
    asm volatile("jmp %0\n\t nop": :"i"(&&ERROR),"i"(&foo),"r"(dummy));
    //goto *gotos[could_be_anything];
 DONE:
    return rval;
 ERROR:
    rval = 1;
    goto DONE;
}

This function should return 1 after jumping from ERROR to DONE. Instead, the
code for ERROR is eliminated by the optimizer; you either get a return value of
zero or an infinite loop depending on whether the label started below or above
the asm block (I get the latter):

foo:
        jmp     %o7+8
         mov    10, %o0
        .size   foo, .-foo
test_label:
.LL4:
.LL5:
        mov     5, %g1
! 8 "test_label.c" 1
        jmp .LL4
         nop
! 0 "" 2
        jmp     %o7+8
         mov    0, %o0
        .size   test_label, .-test_label


The compiler correctly recognizes that both dummy and foo are in use and does
not eliminate them, but ERROR gets the axe unless the computed goto is enabled. 

In theory the inconsistency should be easy to fix -- just mark the label as
in-use if it gets passed to a live inline asm block (exactly how functions and
variables are currently treated). If for some reason that's impossible or
undesirable, it should at least generate a diagnostic.


-- 
           Summary: passing label to inline asm "i" constraint  generates
                    bad code
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: scovich at gmail dot com
GCC target triplet: sparc-sun-solaris2.10


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]