3 ms·
Generally speaking it will get compiled to something like SSA form and the outcome depends on the overall dataflow and what optimizations happen.
by Ferrotin 4y ago
Generally speaking it will get compiled to something like SSA form and the outcome depends on the overall dataflow and what optimizations happen.
- valleyer 4y agoAw, that's an unsatisfying answer! I'm a total .NET noob, but this attempt in Compiler Explorer shows it not using the xor trick. https://godbolt.org/z/jhzqxEKzT https://godbolt.org/z/jhzqxEKzT Maybe I need some extra compiler flags or something.
- moonchild 4y agoThe xor trick has no practical value. Sorry.
- sicp-enjoyer 4y agoI agree it isn't normally helpful on a modern computer, but that's a strong statement. What if you need to swap large storage spaces with no extra memory? Isn't there still old hardware out there especially in aerospace that could benefit?
- lscharen 4y agoIt was useful in single accumulator processor architectures since it could avoid one memory store.
- Ferrotin 4y agoThat would just load each global variable into registers and write them back to memory, swapping them. If the compiler had decided to put two locals located already in registers, and had the swap in a branch, like so: if (foo()) { bar(); (a, b) = (b, a); } Then the registers might get swapped with an xchg instruction or something, I don’t know. The compiler’s goal is to have pipelined execution be as fast as possible, so there’s no way it’s going to use bit-mangling operations that would get in the way of that.
- Someone 4y ago> I’m a total .NET noob, but this attempt in Compiler Explorer shows it not using the xor trick. It better not. The CLR trick introduces a data dependency that slows down the code on modern CPUs. Compilers sometimes also can compile this down to zero instructions. Nothing says the variable-to-register mapping has to be constant in a single function.