为什么这 8 个字节写入没有优化为 MOV

Why are these 8 byte-writes not optimized into a MOV?

本文关键字:优化 MOV 字节 为什么      更新时间:2023-10-16

我和我的同事没有成功地解释为什么GCC,ICC和Clang没有优化这个功能

void f(std::uint64_t a, void * p) {
    std::uint8_t *x = reinterpret_cast<std::uint8_t *>(p);
    x[7] = a >> 56;
    x[6] = a >> 48;
    x[5] = a >> 40;
    x[4] = a >> 32;
    x[3] = a >> 24;
    x[2] = a >> 16;
    x[1] = a >> 8;
    x[0] = a;
}

进入这个

mov     QWORD PTR [rsi], rdi

如果我们用memcpy来表述f,它只发出那个mov。如果我们执行看似微不足道的字节写入序列,为什么不会发生这种情况?

我不是专家,但 gcc 只获得了在 gcc 7 中合并相邻存储以获得即时常量的能力:

  • 立即常量的已关闭错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=23684
  • 打开用于分配小结构的错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78821
  • 商店合并密码:https://github.com/gcc-mirror/gcc/blob/master/gcc/gimple-ssa-store-merging.c

如果我不得不猜测,到第二个错误时,等待可能不会太久。