if vs ?: - (nf)

Morris Keesan keesan at bbncca.ARPA
Fri Feb 17 02:36:27 AEST 1984

It is unclear that there will be any differences between the compiled code for

if (i) j++; else k++;           and         i ? j++ : k++ ;

Certainly, the compiler's discarding of statements with no side effects doesn't
imply anything of the sort.  I recently made a similar enhancement to BBN's C
compiler (based on the Dennis Ritchie PDP-11 C compiler), making it not generate
code for operations with no side effects (such as the statement "i+j;"), and
when I ran the two statements above through it, it produced identical code.  In
my case, this means that I didn't include ?: in my list of "operators with no
side effects".  I see no reason to expect that the implementers of Lattice C
were any less careful.

    An interesting effect of the way I implemented the enhancement is that I
get different code for

if (i) j+1 ; else k+1;          and         i ? j+1 : k+1 ;

because in the first case j+1 and k+1 are statements, and thus get discarded,
leaving an empty "if" framework which gets dropped by the optimizer.  In the
second case, j+1 and k+1 are subexpressions of ?:, and the additions get
generated.  I'm not too concerned about this, since the main reason for my
enhancement was to avoid generating unnecessary conversion code for some casts
being generated internally.  It would be interesting to see what the Lattice C
compiler does with this, though.
					Morris M. Keesan
					keesan @ BBN-UNIX.ARPA

More information about the Comp.lang.c mailing list