Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I control register allocation in g++?

I have highly optimized piece of C++ and making even small changes in places far from hot spots can hit performance as much as 20%. After deeper investigation it turned out to be (probably) slightly different registers used in hot spots. I can control inlineing with always_inline attribute, but can I control register allocation?

like image 478
Łukasz Lew Avatar asked Feb 11 '09 01:02

Łukasz Lew


2 Answers

If you really want to mess with the register alloation then you can force GCC to allocate local and global variables in certain registers.

You do this with a special variable declaration like this:

 register int test_integer asm ("EBX");

Works for other architectures as well, just replace EBX with a target specific register name.

For more info on this I suggest you take a look at the gcc documentation:

http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Local-Reg-Vars.html

My suggestion however is not to mess with the register allocation unless you have very good reasons for it. If you allocate some registers yourself the allocator has less registers to work with and you may end up with a code that is worse than the code you started with.

If your function is that performance critical that you get 20% performance differences between compiles it may be a good idea to write that thing in inline-assembler.


EDIT: As strager pointed out the compiler is not forced to use the register for the variable. It's only forced to use the register if the variable is used at all. E.g. if the variable it does not survive an optimization pass it won't be used. Also the register can be used for other variables as well.

like image 102
Nils Pipenbrinck Avatar answered Sep 30 '22 02:09

Nils Pipenbrinck


In general the register keyword is simply ignored by all modern compilers. The only exception is the (relatively) recent addition of an error if you attempt to take the address of a variable you've marked with the register keyword.

I've experienced this sort of pain as well, and eventually found the only real way around it was to look at output assembly to try and determine what is causing gcc to go off the deepend. There are other things you can do but it depends on exactly what your code is trying to do. I was working in a very very large function with a large amount of computed goto mayhem in which minor (seemingly innocuous) changes could cause catastrophic performance hits. If you're doing similar there are a few things you can do to try and mitigate the problem, but the details are somewhat icky so i'll forgo discussing them here unless it's actually relevant.

like image 44
olliej Avatar answered Sep 30 '22 02:09

olliej