short circuit evaluation

m5d at bobkat.UUCP m5d at bobkat.UUCP
Thu Mar 5 08:08:03 AEST 1987


In article <609 at viper.UUCP> john at viper.UUCP (John Stanley) writes:
>
>The example Greg provides here is misleading.  
>
>If you have three expressions:
>	1:  a+b+c
>	2: (a+b)+c
>	3:  a+(b+c)
>all three of them should not be evaluated the same way.  Greg implys that
>they should be.  This is not so.  When an equation contains '(' and ')'
>it intentionaly (and explicitly) defines the parse tree structure that will
>result.  The statement "redundant ()'s grow in C like mushrooms" may be true,
>but it doesn't give anyone the right to arbitrarily ignore explicit cues
>to the compiler.  When I don't care, I don't use them.  When I do, I do
>so for a reason..........
>
>  Since there is additional, undesireable, and unnecessary overhead in the
>detection of this SUM(a1,a2,a3...,aN) special case, and since there appears
>to be little or no advantage to doing so (you have to add them up in -some-
>order, you might as well let the programmer decide as anyone), why bother?
>
>--- 
>John Stanley (john at viper.UUCP)
>Software Consultant - DynaSoft Systems
>UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john

Sorry, but the compiler writer DOES have the right to use algebraic
manipulation to improve code.  Why?  Because the specification of the
language says so.

There is some difference between "a + (b + c)" and "(a + b) + c":

        +               +
       / \             / \
      a  +            +   c
        / \          / \
       b   c        a   b

A typical code generation scheme will prefer one to the other (one
saves a register).

What happens in other languages (like FORTRAN) if a programmer doesn't
want code re-arranged?  At least in C, the compiler is not supposed to
mess with stuff across statement boundaries.  In a language like
FORTRAN or Pascal, the compiler is liable to do common subexpression
elimination across an entire basic block (or maybe even globally), not
to mention loop unravelling, code hoisting, induction variable
elimination, and removal of dead code (a real bad one for you device
driver fans).  Since lots of people seem outraged at the liscense given
to C compilers, perhaps they should give examples of mechanisms which
allow optimization to be prevented in other languages.  I would hope
that such mechanisms are portable.



-- 
Mike McNally, mercifully employed at Digital Lynx ---
    Where Plano Road the Mighty Flood of Forest Lane doth meet,
    And Garland fair, whose perfumed air flows soft about my feet...
uucp: {texsun,killer,infotel}!pollux!bobkat!m5d (214) 238-7474



More information about the Comp.lang.c mailing list