W
W Karas
Is it typically the case that, in object code generated from C++ source, the "this" pointer is mapped to a CPU register? And passed in member function calls in a register?
Is it typically the case that, in object code generated from C++
source, the "this" pointer is mapped to a CPU register? And passed
in member function calls in a register?
C++ doesn't really have any facilities to either verify or enforce that,
so I'd refrain from using "typically". And why does it matter?
V
Ignore Victor. Given that that is the case for Intel (and AMD) and ARM
one can confidently say that that is typically the case yes.
Is it typically the case that, in object code generated from C++
source, the "this" pointer is mapped to a CPU register? And passed
in member function calls in a register?
...."Passed in a register" is usually a slightly old-fashioned code phrase
for "efficient" and I'm not sure anyone here can judge that, the way
modern compilers and CPUs work.
I am not sure that this is what Jorgen was saying, but it is certainly
true that on cpus like modern x86 chips - with very optimised
implementations of register-poor architectures - access to cached memory
is very fast. In particular, memory near the top of the stack is often
within buffers in the cpu itself (rather than in the main caches), and
there are write buffers for outgoing memory that can be read and written
while the data is in the queue for writing to RAM. So for a modern x86,
accessing data in memory is not much slower than accessing registers if
it is "nearby" - and stack+offset addressing modes are compact. For any
given instruction, registers will always be faster - but for a set of
instructions, it may be that these registers are more valuable used for
other purposes.
On more modern ISA's, you usually have lots of registers and they are
much faster than memory.
But the general point is that you should not normally care if "this" is
passed in a register. It's just a parameter to a function call - you
want it passed in the most efficient way. That's usually a register,
but it does not have to be. (In fact, if the call is inlined then the
parameter-passing may be skipped entirely. And if the function is
static to the module and is never called externally, the compiler is
free to break all ABI rules for parameter passing and pick whatever is
best - that might mean putting "this" on the stack and passing other
parameters in registers.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.