c++ - 错误 : invalid operand for instruction using indexed addressing and Clang

标签 c++ x86 clang inline-assembly

我正在使用 Clang 捕捉编译问题。 GCC 可以很好地编译程序。该程序使用索引寻址。

错误是:

$ clang++ -g2 -O1 test.cxx -c
test.cxx:19:10: error: invalid operand for instruction
        "movq     (%[idx],%[in]), %[x]   ;\n"
         ^
<inline asm>:5:23: note: instantiated into assembly here
movq     (%rbx,%rsi), -8(%rsp)   ;
                      ^~~~~~~~
test.cxx:20:10: error: invalid operand for instruction
        "movq     (%[idx],%[out]), %[y]  ;\n"
         ^
<inline asm>:6:23: note: instantiated into assembly here
movq     (%rbx,%rdi), -16(%rsp)  ;
                      ^~~~~~~~~
test.cxx:21:10: error: invalid operand for instruction
        "cmovnzq  %[x], %[y]             ;\n"  // copy in to out if NZ
         ^
<inline asm>:7:20: note: instantiated into assembly here
cmovnzq  -8(%rsp), -16(%rsp)             ;
                   ^~~~~~~~~
test.cxx:22:10: error: invalid operand for instruction
        "movq     %[y], (%[idx],%[out])  ;\n"
         ^
<inline asm>:8:21: note: instantiated into assembly here
movq     -16(%rsp), (%rbx,%rdi)  ;
                    ^~~~~~~~~~~
4 errors generated.

我该如何解决这个问题? (或者我如何告诉 Clang stop defining __GNUC__ 使其远离 GCC 代码路径)。


$ cat test.cxx
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdint>

void test_cmov(uint8_t in[96], uint8_t out[96], uint64_t flag)
{
#if defined(__GNUC__)
    const uint32_t iter = 96/sizeof(uint64_t);
    uint64_t* optr = reinterpret_cast<uint64_t*>(out);
    uint64_t* iptr = reinterpret_cast<uint64_t*>(in);
    uint64_t idx=0, x, y;

    __asm__ __volatile__ (
        ".att_syntax                     ;\n"
        "cmpq     $0, %[flag]            ;\n"  // compare, set ZERO flag
        "movq     %[iter], %%rcx         ;\n"  // load iteration count
        "1:                              ;\n"
        "movq     (%[idx],%[in]), %[x]   ;\n"
        "movq     (%[idx],%[out]), %[y]  ;\n"
        "cmovnzq  %[x], %[y]             ;\n"  // copy in to out if NZ
        "movq     %[y], (%[idx],%[out])  ;\n"
        "leaq     8(%[idx]), %[idx]      ;\n"  // increment index
        "loopnz   1b                     ;\n"  // does not affect flags
        : [out] "+D" (optr), [in] "+S" (iptr), [idx] "+b" (idx),
          [x] "=g" (x), [y] "=g" (y)
        : [flag] "g" (flag), [iter] "I" (iter)
        : "rcx", "cc"
    );
#else
    if (flag)
        std::memcpy(out, in, 96);
#endif
}

int main(int argc, char*argv[])
{
    uint8_t in[96], out[96];
    uint64_t flag = (argc >=2 && argv[1][0] == 'y');

    std::memset(in, 0x00, 96);
    std::memset(out, 0x00, 96);

    std::memcpy(in, argv[0], std::min(96ul, std::strlen(argv[0])));

    test_cmov(in, out, flag);
    std::cout << (const char*)out << std::endl;

    return 0;
}

$ gcc --version
gcc (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
...

$ clang --version
clang version 7.0.1 (Fedora 7.0.1-6.fc29)
Target: x86_64-unknown-linux-gnu
...

$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: Fedora
Description:    Fedora release 29 (Twenty Nine)
Release:        29
Codename:       TwentyNine

最佳答案

您使用了 "=g"让编译器为 %[x] 选择内存操作数的约束和 %[y] .

使用 "=r"相反。

您的模板使用 movq (%[idx],%[in]), %[x] ,如果 %[x] 显然无法组装是内存,因为 x86 不支持任何指令的 2 个显式内存操作数。

这里 clang 与 gcc 的区别在于,如果可以选择,它喜欢选择内存操作数。 (这是一个优化器错误,IMO,但不是正确性问题。您的内联 asm 有错误,并且只适用于 GCC,因为它为 [x] "=g" (x) 选择了一个寄存器)

如果您阅读错误消息,这是显而易见的:

<inline asm>:5:23: note: instantiated into assembly here
movq     (%rbx,%rsi), -8(%rsp)

...

cmovnzq  -8(%rsp), -16(%rsp)

显然这些不是有效的指令。


如果您关心 clang,通常避免给它选择内联 asm 的内存操作数,除非它在正常情况下肯定有帮助。

当您在内联 asm 中编写整个循环时,您肯定希望编译器溢出其他内容以释放寄存器(如果有必要)用于某些临时循环。或者实际上任何时候你在同一个内联 asm block 中多次使用和操作数。 GCC不看这个,不会知道选择内存的成本。 (而且 clang 是愚蠢的,即使有很多空闲寄存器也会选择内存。)

关于c++ - 错误 : invalid operand for instruction using indexed addressing and Clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55925692/

相关文章:

c++ - 如何禁用 LLVM 中的优化

c++ - 为什么我的任何 pthread 函数都不能被 g++ 或 clang 识别?

c++ - 导入错误:没有名为 _tkinter 的模块

c++:没有全局变量或单例或将其传递给每个方法的记录器类

c++ - g++ 是否满足 std::string C++11 要求

C++ 构造函数设置值

assembly - 在 NASM 汇编中对数字进行平方而不进行乘法

assembly - 如何编译汇编例程以用于 C 程序(GNU 汇编器)?

debugging - 多伦多证券交易所 : Get the address that caused the abort

c++ - clang:从互斥锁初始化锁引用