Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!gatech!bloom-beacon!eru!hagbard!sunic!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c Subject: Re: May too many register variables hurt? (was Re: Novice question.) Message-ID: <972@mwtech.UUCP> Date: 24 Nov 90 01:37:26 GMT References: <965@demott.COM> <11476@j.cc.purdue.edu> <967@mwtech.UUCP> <14538@smoke.brl.mil> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 89 In article <14538@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <967@mwtech.UUCP> martin@mwtech.UUCP (I) wrote: >>In general, my advice is to use no more than two register variables, and >>only in the *outmost* blocklevel in the body of any function. > >Mine is the opposite. > >>The order in which the variables are declared is a-b-c, so c will not >>profit from its storage class. > >Sure it will. Since b and c are declared in separate parallel blocks, >older-technology compilers such as PCC will share the explicit register >that is assigned for these two variables. This is in fact a good way >to exploit "register" in such compilers. Though it might be not wise to argue with one of the network gods, I beg to differ here. (For those who tuned in late: The question is: If you write code for an unknown compiler, may *too many* register declarations hurt overall performance.) My argument was that *without* some internal knowledge of the inner workings of some (non-optimizing) compiler, it is *not* possible to choose the appropriate places for more than two or maybe three register variables declared in the outmost (function) block. K&R-1, pg. 193 states concerning register: ... only the first few of such declarations are effective ... and on page 81 ... on the PDP-11, only the first three register declarations in a function are effective ... Now let's assume the implementor of an unknown% compiler has read his K&R-1, though it might not be alike K&R's PDP-11 compiler, but James McCosh's for the 6809, which has only *two* free register variables and I determine the five most heavily used variables in my function as `a', `b', `c', `d', `e' (in that order, i.e. `e' is less frequently accessed). Further we may have the following block structure: func() { ...d, e, used here .... for (......) { ...b, d, used here { ... c used here } for (......) { ... a, b used here } ...b, d, used here } ... e, used here again } If I follow the simple rule to place all register declarations outside (at function block level) and to depend not on more than two beeing effective, I can easily verify that the "right" ones are given. If I further trust the compiler that it gets K&R-1 right in only obbeying the declarations that come first, I may even declare all the five variables with sorage class register (in decreasing order of their access frequency), not risking that the most important ones are missed. This would work for the 6809 (2 registers) and the PDP-11 (3 registers). On the other hand, if I declare the variables at the inner blocks (as the required scope allows), it may be possible for PCC-like compilers to share registers between blocks, but it is not possible for me to find the set of variables which should receive the register attribut, without knowing the number of available registers: For McCosh's-6809 compiler (only two registers) I should declare `a', `b', and `c' as register (and hope that the compiler is PCC-ish enough to share the register for `a' and `c' between the blocks). On the PDP-11 I could (and propably should) try to use the third register by also declaring `d' as register, but that would on the 6809 force the most important variable (`a') out of the available registers. ---------------------------- %: Hand-optimizing register declarations for a compiler which I know very well is quite another topic ... -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83