Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnews!cbnewsm!lfd From: lfd@cbnewsm.att.com (leland.f.derbenwick) Newsgroups: comp.lang.c Subject: Re: May too many register variables hurt? (was Re: Novice question.) Summary: Few registers, limited optimization "ancient"? Message-ID: <1990Nov21.221908.19871@cbnewsm.att.com> Date: 21 Nov 90 22:19:08 GMT References: <1990Nov14.010511.7241@ux1.cso.uiuc.edu> <965@demott.COM> <967@mwtech.UUCP> Organization: AT&T Bell Laboratories Lines: 51 In article <967@mwtech.UUCP>, martin@mwtech.UUCP (Martin Weitzel) writes: > 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. > > Why? Listen: > > If you have a modern, optimizing compiler, it will ignore the register > keyword and make independent decissions about use of CPU-registers. If > you have modern hardware with many available registers, most probably > you also have a modern compiler, so there's no reason to worry about having > defined "not enough" register variables. Modern hardware. Such as the entire VAX line, Intel 80x86, Motorola 680x0, etc.? None of these have "many" available registers, yet all are certainly modern in the sense that they are currently used and sold in large quantities. And while commonly available compilers for these do vary in their optimization quality, none that I've used has come close to the levels of optimization being applied to RISC architectures. So let's turn the proposed advice around. Unless you _know_ that your code is being written _only_ for a processor with lots of registers and a smart compiler, and that it will never be ported to any of today's common processors, use register declarations generously -- typically from 1 up to 4 or 5 in each function will _help_, not hurt. (Alternatively, the performance of your code may be irrelevant to you. This _does_ occur in practice: in some code I worked on several years ago to run on IBM mainframes, the _only_ relevant optimization was reducing the number of data base accesses -- everything else was so fast by comparison that it was lost in the noise.) Martin Weitzel's advice not to declare inner-block variables as register is good -- some un-smart compilers ignore them; others limit their optimizizations near them; some handle them well. It's a gamble. Using register declarations will _never_ interfere with a smart optimizing compiler. A register declaration is a suggestion, not an absolute: the compiler is perfectly free to ignore it in order to do other optimizations. (It is also a promise: you will never take the address of a register variable.) Even as far back as K&R I, "A register declaration is best thought of as an auto declaration, together with a hint to the compiler that the variables declared will be heavily used." It's a hint that a good compiler can use, but that it will ignore if better optimizations are available. (Anyone who writes a compiler capable of doing optimal register allocation on its own had _better_ make it ignore register declarations!) -- Speaking strictly for myself, -- Lee Derbenwick, AT&T Bell Laboratories, Warren, NJ -- lfd@cbnewsm.ATT.COM or !att!cbnewsm!lfd