180 likes | 276 Vues
Common Subexpression Elimination . Johnathon Jamison CS265 S. Graham. Titanium. Titanium is a compiler. CSE. Given: a = f * i … b = f * i We want to compute f * i only once. CSE. We could do: a = f * i temp = a … b = temp But only if the value of f * i has not changed. Def/Use.
E N D
Common Subexpression Elimination Johnathon Jamison CS265 S. Graham
Titanium • Titanium is a compiler
CSE • Given: • a = f * i • … • b = f * i • We want to compute f * i only once
CSE • We could do: • a = f * i • temp = a • … • b = temp • But only if the value of f * i has not changed
Def/Use • Given: • a = … • … • …a… • We want to link the use of a to the definition of a above.
Def/Use • The link should be of every possible definition of a • Thus method calls or pointer indirection could define a
Def/Use • Titanium has def/use information available • Every use of a variable has a list of all possible definitions associated with it • It seems this could be leveraged for CSE
CSE Revisited • a = f * i • … • b = f * i • The second f * i can be eliminated if the definitions of f and i that are used are exactly the same as the first • But checking for that could be onerous
CSE Revisited • So, lets create some fake definitions of f and i immediately before the first f * i • Then, there is one explicit definition that can be traced to for checking the previously mentioned condition
CSE Revisited • f = f • i = i • a = f * i • … • b = f * i • Thus, if f and i have the same definitions in both places, then the second f * i can be eliminated
CSE Revisited • This is fine and dandy for straight line code, but what if you have: • a = f * i b = f * i • … … • --------------------------- • V • c = f * i
CSE Revisited • So, you need to see if f and i have the same definitions in all pairs of places where the common subexpression exists. • I.E., does f or i have any definition that is not associated with a fake def introduced by this analysis? • If not, then an elimination can occur
CSE Revisited • This is complicated by the fact that all CSEs are done simultaneously • So, extra assignments are placed everywhere in the code a CSE could be • Therefore, when tracing defs, those introduced defs must be explicitly ignored
CSE Tidbits • After completion, these assignments are deleted, so as not to interfere with anything else • Any temps introduced are placed after the calculation, so that copy propagation can remove them • Compiler temps are placed at top level, as the live range of CSEs are unknown • Associatively is accounted for • Only binary and unary operations are done
Local CSE • Used the standard Muchnik (sp?) algorithm • Used defs to find what was killed • Fully implemented • Except method calls