johnson@p.cs.uiuc.edu (Ralph Johnson) (12/10/87)
My compiler often produces code that looks like this: x <- if y<z then 1 else 2 if x=1 then A else B Naturally, I would like it to produce code that looks like this: if y<z then A else B It would not be difficult to look for this pattern and treat it as a special case, but that seems kludgy. I am looking for a general purpose optimization that would handle this along the way. If we performed symbolic execution of each statement then we would know that the value of x after the first statement was (if y<z then 1 else 2), and the boolean expression would reduce to (if y<z then 1=1 else 2=1), which would reduce to (if y<z then true else false), which would reduce to (y<z). However, symbolic evaluation seems overkill. Does anybody have any ideas how this could be handled? [If x is an actual variable in the source program, then you need some data flow analysis to detect the contexts in which values are used. If the compiler is making up x all by itself, then a simpler strategy like the one in PCC should do the trick. In PCC, whenever an expression or subexpression is compiled, it compiles for the value, for the side effect, or for the condition codes (or local equivalent.) PCC would evaluate y<z for condition codes, since it can see by looking at the parse tree that it's in a conditional context, and generates decent code. -John] -- Send compilers articles to ima!compilers or, in a pinch, to Levine@YALE.ARPA Plausible paths are { ihnp4 | decvax | cbosgd | harvard | yale | bbn}!ima Please send responses to the originator of the message -- I cannot forward mail accidentally sent back to compilers. Meta-mail to ima!compilers-request