This Definitely Falls into the Wonderful Camp
-
When I saw a
cmovne
instruction in a disassembly through which I am tracing, I asked Bing what it was, and got the details at [Purpose of cmove instruction in x86 assembly? - Stack Overflow](https://stackoverflow.com/a/56312037/3079037), which are borne out by the demonstrated behavior of my code. It appears that the Visual C++ compiler that ships with Visual Studio 2019 counts the cost of twomov
instructions followed by acmovne
instruction as less than that of a branch that causes both to execute anyway, even though the outcome of one of the twomov
instructions would eventually be discarded. Moreover, this is happening in a debug build! This is by no means the first such unexpected optimization that I've seen the MSVC compiler generate in a debug build.David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
When I saw a
cmovne
instruction in a disassembly through which I am tracing, I asked Bing what it was, and got the details at [Purpose of cmove instruction in x86 assembly? - Stack Overflow](https://stackoverflow.com/a/56312037/3079037), which are borne out by the demonstrated behavior of my code. It appears that the Visual C++ compiler that ships with Visual Studio 2019 counts the cost of twomov
instructions followed by acmovne
instruction as less than that of a branch that causes both to execute anyway, even though the outcome of one of the twomov
instructions would eventually be discarded. Moreover, this is happening in a debug build! This is by no means the first such unexpected optimization that I've seen the MSVC compiler generate in a debug build.David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
David A. Gray wrote:
It appears that the Visual C++ compiler that ships with Visual Studio 2019 counts the cost of two
mov
instructions followed by acmovne
instruction as less than that of a branch that causes bothKeep in mind that due to the speculative execution leaks... the emitted instructions may be avoiding data-dependent branching. Optimization is not as simple as it was ten years ago... Do me a favor and right click on your project settings and navigate to the 'Code Generation' tab and check if /Qspectre is enabled.
-
David A. Gray wrote:
It appears that the Visual C++ compiler that ships with Visual Studio 2019 counts the cost of two
mov
instructions followed by acmovne
instruction as less than that of a branch that causes bothKeep in mind that due to the speculative execution leaks... the emitted instructions may be avoiding data-dependent branching. Optimization is not as simple as it was ten years ago... Do me a favor and right click on your project settings and navigate to the 'Code Generation' tab and check if /Qspectre is enabled.
My take on the whole point of the conditional move instruction is to eliminate speculative execution from the mix by keeping the program flow in a straight line. FWIW, I double-checked, and Spectre mitigation is disabled. I suspected it was, because I've never enabled it for any of my projects. What made it stand out was that this was happening in a debug build. Had it been a release build, I would have expected this level of optimization. However, this is the most recent of many such unanticipated, and welcome, optimizations, that I've seen in recent debug builds.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
My take on the whole point of the conditional move instruction is to eliminate speculative execution from the mix by keeping the program flow in a straight line. FWIW, I double-checked, and Spectre mitigation is disabled. I suspected it was, because I've never enabled it for any of my projects. What made it stand out was that this was happening in a debug build. Had it been a release build, I would have expected this level of optimization. However, this is the most recent of many such unanticipated, and welcome, optimizations, that I've seen in recent debug builds.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
David A. Gray wrote:
What made it stand out was that this was happening in a debug build. Had it been a release build, I would have expected this level of optimization. However, this is the most recent of many such unanticipated, and welcome, optimizations, that I've seen in recent debug builds.
Yeah, it's not a coincidence that the first compiler implementing the /Qspectre mitigations[^] is generating slighty different conditional moves. It's an active area of development. Best Wishes, -David Delaune