[comp.lang.c] Ye Olde Register Variable Hacke

john@amc-gw.amc.com (John Sambrook) (11/28/90)

In article <9733:Nov2722:02:3090@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
>
>Sure. And it *could* also result in much better code. What most of us
>are saying is that in practice extra register declarations help much
>more than they hurt. In typical programs, some variables are used quite
>a lot, and they should be declared register. Some variables are rarely
>used, and they shouldn't be declared register. It's better to err on the
>side of extra register declarations than to pessimize your code in the
>common case. Past that, who cares? The language doesn't provide better
>mechanisms for asserting variable use, so you won't be able to outguess
>the compiler in very many cases.
>
>---Dan

Maybe not everyone knows about the following hack.  I didn't invent
it, but learned about it on a past project and thought I'd share it.

But before we get started, let's all remember that what follows is a
hack, and no one is recommending that you use it.  I'd only use it
under the most dire of circumstances, and only then after sending the
FSF a check for $ 50.00 to support work on good free compilers like
GCC.

Let's say you have a large software system and you are forced to use
inferior C compilers to compile your system.  Inferior compiler #1
honors the first two register declarations that it encounters while
inferior compiler #2 honors the first five.  You could do something
like this:

    1.  Put the following code in the "global" include file that your system
    	no doubt already includes:

    	#if C_COMPILER == INFERIOR_COMPILER_1
    	#define register_1 register
    	#define register_2 register
        #define register_3 
    	#define register_4 
    	...
    	#define register_9  
    	#endif
    	#if C_COMPILER == INFERIOR_COMPILER_2
    	#define register_1 register
    	#define register_2 register
        #define register_3 register
    	#define register_4 register
    	#define register_5 register
    	#define register_6
    	#define register_7
    	...
    	#define register_9 
    	#endif

    2.  Next spend hours tweaking your code so that in each function
    	register declarations are prioritized according to your guesses
    	about variable usage.  The most important variable in the function 
    	is declared as 'register_1 ...' while the least important register
    	variable is assigned 'register_9 ...'.

I actually worked on a project (a UNIX-hosted interpreter for Wang
BASIC-2) where doing this made some difference in performance.  But a
better compiler would have probably made a bigger difference, and we
didn't wind up doing this thoughout the code.  Better algorithms no
doubt would have made the biggest difference.
-- 
John Sambrook                             DNS: john@amc.com
Applied Microsystems Corporation	 UUCP: amc-gw!john
Redmond, Washington  98073               Dial: (206) 882-2000 ext. 630