使用 xor reg, reg 是否优于 mov reg, 0? [英] Does using xor reg, reg give advantage over mov reg, 0?

查看:21
本文介绍了使用 xor reg, reg 是否优于 mov reg, 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种众所周知的方法可以在 x86 上将整数寄存器设置为零值.

There're two well-known ways to set an integer register to zero value on x86.

要么

mov reg, 0

xor reg, reg

有人认为第二种变体更好,因为值 0 没有存储在代码中,并且可以节省生成的机器代码的几个字节.这绝对是好的 - 使用更少的指令缓存,这有时可以允许更快的代码执行.许多编译器生成这样的代码.

There's an opinion that the second variant is better since the value 0 is not stored in the code and that saves several bytes of produced machine code. This is definitely good - less instruction cache is used and this can sometimes allow for faster code execution. Many compilers produce such code.

但是,在 xor 指令和更改同一寄存器的任何较早指令之间,在形式上存在指令间依赖关系.由于存在依赖关系,后一条指令需要等到前一条指令完成,这可能会降低处理器单元的负载并损害性能.

However there's formally an inter-instruction dependency between the xor instruction and whatever earlier instruction that changes the same register. Since there's a depedency the latter instruction needs to wait until the former completes and this could reduce the processor units load and hurt performance.

add reg, 17
;do something else with reg here
xor reg, reg

很明显,无论初始寄存器值如何,xor 的结果都将完全相同.但处理器能够识别这一点吗?

It's obvious that the result of xor will be exactly the same regardless of the initial register value. But it the processor able to recognize this?

我在 VC++7 中尝试了以下测试:

I tried the following test in VC++7:

const int Count = 10 * 1000 * 1000 * 1000;
int _tmain(int argc, _TCHAR* argv[])
{
    int i;
    DWORD start = GetTickCount();
    for( i = 0; i < Count ; i++ ) {
        __asm {
            mov eax, 10
            xor eax, eax
        };
    }
    DWORD diff = GetTickCount() - start;
    start = GetTickCount();
    for( i = 0; i < Count ; i++ ) {
        __asm {
            mov eax, 10
            mov eax, 0
        };
    }
    diff = GetTickCount() - start;
    return 0;
}

优化关闭两个循环所需的时间完全相同.这是否合理地证明处理器认识到 xor reg, reg 指令不依赖于较早的 mov eax, 0 指令?什么是更好的测试来检查这个?

With optimizations off both loops take exactly the same time. Does this reasonably prove that the processor recognizes that there's no dependency of xor reg, reg instruction on the earlier mov eax, 0 instruction? What could be a better test to check this?

推荐答案

给你的实际答案:

Intel 64 和 IA-32 架构优化参考手册

第 3.5.1.8 节是您想要查看的地方.

Section 3.5.1.8 is where you want to look.

简而言之,在某些情况下可能更喜欢 xor 或 mov.问题集中在依赖链和条件代码的保存上.

In short there are situations where an xor or a mov may be preferred. The issues center around dependency chains and preservation of condition codes.

这篇关于使用 xor reg, reg 是否优于 mov reg, 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆