Allocating Local Variables to CPU Registers

Actually, I am familiar with the .NET framework memory model.

I'm wondering if the JIT compiler can put local variables in CPU registers to improve application performance instead of allocating variables on the stack? If possible, what are the requirements for such an allocation and how does he decide whether to comply or not?

+3


source to share


1 answer


This is done all the time by JIT. This is a standard optimization for almost any JIT and native compiler.

Don't confuse the IL logical stack with the x86 physical stack, which uses jitted code. They are very loosely coupled. The contents of the IL stack and local IL locators are stored preferably in registers and pushed onto the x86 stack only when needed.



The only exception is structs, which are usually stacked on the glass in both .NET 4.5 JIT and vNext RyuJIT (as in VS2015 Preview). This is not documented, but testing clearly shows that even in the simplest cases, structures are not registered. I may have missed some case where this happens, but this is definitely a rare case.

Primitive types and object references are stored sequentially in registers (if available) as per my testing.

+3


source







All Articles