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

           Summary: inline assembly "i" constraint does not accept
                    constexpr function return value
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: inline-asm
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: hvdie...@elis.ugent.be


constexpr functions would be helpful to improve the performance of inline
assembly by passing the constant value as an assembly constant rather than
through a register.

In the following simplified example we want to add a constant to a variable.

constexpr int const_func() { return 12; }

int main(int argc, char * argv[]) {
    int ret;
    __asm__( "add %2,%0" : "=g"(ret) : "0"(argc), "i"(const_func()) : );
    return ret;
}

If we put an "r" constraint instead of an "i", then gcc will load the constant
in a register first:
movl    %edi,%eax
movl    $0x0000000c,%edx
addl    %edx,%eax
ret

but what we want is obviously
movl    %edi,%eax
addl    $0x0000000c,%eax
ret

With -O4, constexpr can generally be passed to an "i" constraint, but not at
-O0 which we use for debugging:
$ gcc-mp-4.6 -std=c++0x -O0 -o asm_constexpr asm_constexpr.cc 
asm_constexpr.cc: In function 'int main(int, char**)':
asm_constexpr.cc:5:72: warning: asm operand 2 probably doesn't match
constraints [enabled by default]
asm_constexpr.cc:5:72: error: impossible constraint in 'asm'

The reason is that the constexpr function is evaluated at runtime at -O0.

In short, the feature request is that inline assembly "i" constraints (and
potentially other types of constant constraints) should accept return values
from constexpr functions, also at -O0.

Reply via email to