Must useless expressions be evaluated?

Barry Margolin barmar at mit-eddie.UUCP
Tue Oct 18 14:25:55 AEST 1983


Yes, Steve Haflich has a point about special locations which have
side-effects just from being accessed.  However, there are plenty of
ways to fool a compiler into leaving the references around without
giving up a very common optimization.  For instance, you could have a
library routine, do_nothing, which takes one argument and just returns.
You could then say "do_nothing(*special_loc)".  The compiler has no way
of knowing that the parameter will be ignored, and I have never heard of
an optimizing loader (at least not that smart).

It is not too smart to depend too heavily on what you expect the
generated code to be.  For instance, *special_loc+*special_loc will
probably NOT access special_loc twice; more likely, it will be loaded
into a register, and doubled.  A good optimizer can really contort your
code in order to generate efficient code.  Perhaps there should be ways
to tell the compiler and optimizer that certain variables should never
be optimized into registers (a not_register declaration?).  Another
place to be careful is in expressions; I believe that C does not define
the order of evaluation of terms in an expression (I know PL/I doesn't).
-- 
			Barry Margolin
			ARPA: barmar at MIT-Multics
			UUCP: ..!genrad!mit-eddie!barmar



More information about the Comp.lang.c mailing list