Related articles |
---|
looking for control structure optimization johnson@p.cs.uiuc.edu (1987-12-09) |
Date: | Wed, 9 Dec 87 16:41:02 CST |
From: | johnson@p.cs.uiuc.edu (Ralph Johnson) |
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]
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.