this pointer mapped to a register?

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?
 
V

Victor Bazarov

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
 
W

W Karas

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

It's a performance issue (related to another recent topic I started) that would rarely matter.

In the embedded C++ world we often have to rely on de facto portability. The C++11 <atomic> lib additional seems to be a good replacement for a lot the "rules" for de facto portability (with maintained performance) of lower-level code.
 
J

Jorgen Grahn

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.

His "And why does it matter?" is still relevant though.

"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.

The best hint is probably "whatever popular compilers do is probably
on average efficient", but then we've come full circle ...

/Jorgen
 
M

Melzzzzz

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?

If calling convention is to pass arguments via stack, that would
be meaningful. On 32 bit x86 abi stack was used so I guess
some compilers would do like that(passing `this` via register).
64 bit x86 abi requires passing arguments via registers...
 
W

W Karas

On Wednesday, December 11, 2013 4:31:57 PM UTC-5, Jorgen Grahn wrote:

....
"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.
....

Do you have a reference/link that elaborates on this? I'm far from a machine arch expert. But what your saying seems very implausible. Do you mean explicit caching in regs is replaced by implicit caching? I would think that would necessarily imply a much less reg-centric instruction set. And thus extremely bulky machine code, with all operands being memory addresses (?).
 
W

W Karas

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.)

On second thought, I guess I'm being silly. To be used, the "this" pointerhas to be in a register (in a non-bizarre arch.). It would be a bizarre arch where getting it from an absolute mem location would be faster than getting it with base-pointer-relative addressing from the stack. So, even if it were in the stack, it's moot. So my "inline this" Standard change suggestion is of no use.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top