考虑这个例子:
volatile unsigned int x;
unsigned int y;
void f() {
x /= 2;
}
void g() {
y /= 2;
}
当使用 -Os 编译时,clang-6.0 在 x64 上为 f 和 g 生成相同的 shrl <offset>(%rip)
指令模式(见 https://godbolt.org/g/hUPprL ),而 gcc-7.3 为 f() 生成这个(见 https://godbolt.org/g/vMcKVV ):
mov 0x200b67(%rip),%eax # 601034 <x>
shr %eax
mov %eax,0x200b5f(%rip) # 601034 <x>
这只是一个错过的优化,还是 gcc 拒绝 shrl <offset>(%rip)
的理由?如果访问不稳定?谁错了?
最佳答案
这只是 gcc 遗漏的优化。两种实现都精确地保留了对 x
的读取和写入,因此都是正确的。
对内存操作数的“底层”操作执行与较长实现相同的加载和存储。
关于c++ - clang 与 gcc : different code for volatile access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50039867/