On Jul 15, 3:20 am, mingze zhang <(EMail Removed)> wrote:
> Sorry that this post may not be a correct place for C++ forum. I hope
> someone can give me some idea on this.
> We all know that logic expressions can be manipulated or simplified,
> for example,
> 1. (x && y)  (x && z) is equivalent to x && (y  z)
> 2. x && (x  y) is equivalent to x
> 3. (x  y) && (x  z) is equivalent to x  (y && z)
> 4. (y && x)  (z && x) is equivalent to (y  z) && x
> 5. (x  y) && x is equivalent to x
> 6. (y  x) && (z  x) is equivalent to (y && z)  x
If this is C++: the && and  are not (Boolean) logic operators,
but something very computer specific, which does NOT obey the
rules of Boolean algebra. Your "equivalent" aren't, at least
not in general.
> But this simplification is apparently not all valid if shortcircuit
> evaluation is considered. In the above examples, Cases 13 are valid,
> but 46 are not (assume x, y, z may have side effects).
None are valid, unless the compiler can prove that they make no
difference in the observable behavior. (If, for example, x,
y and z are expressions without any function calls, using only
doubles and ints, the compiler can use any of the equivalences
above. If x, y and z contain a function call which outputs to
cout, none of the above rewrites can be done. Note too that
because && and  introduce sequence points, they determine the
order the side effects become visible.)
> So here comes my question, is there a general guideline to
> simplify a very complicated logic expressions (under
> shortcircuit equivalence) when some of the logic expressions
> have side effect and some do not.
General guidelines, I don't know. In general, I suspect that
compilers avoid any reordering when there is any possibility of
side effects.
> I know some compilers may do this kind of simplification. I just want
> to know whether there are some tutorial or general procedures so that
> I can quickly catch up. I tried to search on Google but with no good
> luck.
One of the major objections to shortcircuiting is that it
inhibits optimization. The practical benefits for the
programmer are such, however, that most modern languages support
it.

James Kanze
