Peephole optimisation
Robert Firth
firth at sei.cmu.edu
Tue Feb 28 00:00:22 AEST 1989
[ NOT (a OR b) => a NOR b ]
csimmons at oracle.UUCP (Charles Simmons) wrote:
Since the 'nor' instruction doesn't directly map into the C language,
I didn't really expect the compiler to handle this minor special case.)
w-colinp at microsoft.UUCP (Colin Plumb) writes:
Really? I do. Since the sequence "or $c, $a, $b; not $c, $c" is
exactly equivalent to "nor $c, $a, $b" and easily recognised by a
peephole optimiser.
cik at l.cc.purdue.edu (Herman Rubin) writes:
There is no question that in some cases a peephole optimizer can catch
things like this. But this requires that someone has anticipated the
problem and built it into the optimizer. And how big is the peephole?
Here's a data point; I rather think it supports everybody's thesis.
When the 'nor' idiom was first posted, I immediately tried it on my
MIPS codegenerator. The 'nor' was not generated; the less efficient
'or' followed by 'not' came out. So there's another compiler that
missed the good code.
However, the reason was very simple - I'd completely overlooked the
equivalence when writing the codegenerator. So the failure was human
error.
Finally, the fix was equally simple: it was necessary to add one
more element to a lookup table in a specific part of the codegenerator,
that basically says [OR;NOT => NOR].
Of course, none of this matters if you have some automated way of
generating the peephole optimisation rules, which seems to me much the
best solution to the problem.
More information about the Comp.lang.c
mailing list