Function parameter passing in Registers (GCC)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
CopperMan

Function parameter passing in Registers (GCC)

Post by CopperMan »

Hi All.

Does anyone used OpenWatcom C/C++ compiler. It has beautiffull Register-Based calling convention. With this it produces very fast code.

I have switched from OpenWatcom to MinGW/GCC some days ago. I've found that code produced with OpenWatcom is little better then GCC one.

Q : Does GCC have something like Register-Based calling convention ?

P.S. If we have a function which receives one int parameter, GCC pushes it on stack i.e. we have memory op, we have one hope : this memory line already in cache, otherwise we have speed penalty.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Function parameter passing in Registers (GCC)

Post by Solar »

There is the "register" keyword, which is part of the C/C++ standard. The compiler is allowed to ignore it, however: It is a suggestion by the developer.

For GCC specifics, check out http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html. fastcall and regparm look like interesting candidates.
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Function parameter passing in Registers (GCC)

Post by Pype.Clicker »

fortunately enough, the IA-32 are very good at caching the stack content (yeah, really). And using "foo(register int bar)" do not tell the compiler "i want bar to be passed through a register" but rather "once you retrieved bar, keep it in a register as i'm using it veryveryveryoften".

What i usually do is to tell the compiler the function is "static" so that it can feel free to do whatever it wants with it (inline it, break calling convention or whatever), since it now knows nothing from outside could possibly call the function.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Function parameter passing in Registers (GCC)

Post by Solar »

Actually "register" means that the address of the variable cannot be taken - nothing more and nothing less. This enables the compiler to keep (and possibly pass) it in a register.

It also enables several other optimizations, as it avoids several aliasing problems inherent in the language. If you're doing C99 and are interested in giving the compiler an easy time optimizing your code, you should also check the "restrict" keyword.
Every good solution is obvious once you've found it.
CopperMan

Re:Function parameter passing in Registers (GCC)

Post by CopperMan »

Thanks to All for Help. :)
Post Reply