common subexpression elimination

21
Common Subexpression Elimination Johnathon Jamison CS265 S. Graham

Upload: floria

Post on 21-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Common Subexpression Elimination. Johnathon Jamison CS265 S. Graham. Outline. Titanium Def/Use analysis (used by CSE) Common Subexpression Elimination Implementation Examples. Titanium. Titanium is an extension of Java The Titanium compiler compiles Titanium code to C - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Common Subexpression Elimination

Common Subexpression Elimination

Johnathon Jamison

CS265

S. Graham

Page 2: Common Subexpression Elimination

Outline

• Titanium

• Def/Use analysis (used by CSE)

• Common Subexpression Elimination

• Implementation

• Examples

Page 3: Common Subexpression Elimination

Titanium

• Titanium is an extension of Java

• The Titanium compiler compiles Titanium code to C

• The C code is then compiled by the system compiler, e.g. gcc

Page 4: Common Subexpression Elimination

Def/Use

• Given:

a = …

…a…

• We want to link the use of a to the definition of a above.

Page 5: Common Subexpression Elimination

Def/Use

• Every use of a variable has a list of all possible definitions associated with it

• Every definition of a variable has a list of all possible uses associated with it

• Method calls and pointer indirection are included in this analysis

Page 6: Common Subexpression Elimination

Def/Use

• The Titanium compile has def/use information available

• It seems this could be leveraged for CSE

Page 7: Common Subexpression Elimination

CSE

• Given:

a = f * i

b = f * i

• We want to compute f * i only once

Page 8: Common Subexpression Elimination

CSE

• We could do:

a = f * i

temp = a

b = temp

• But only if the value of f * i has not changed

Page 9: Common Subexpression Elimination

Finding CSEs

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– Leverage our def/use analysis!

• But checking for that could be onerous

Page 10: Common Subexpression Elimination

Finding CSEs

• 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

Page 11: Common Subexpression Elimination

Finding CSEs

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

Page 12: Common Subexpression Elimination

Handing Global CSEs

• This is fine and dandy for straight line code, but what if you have:

a = f * i b = f * i

… …

c = f * i

Page 13: Common Subexpression Elimination

Handing Global CSEs

• 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 definition introduced by this analysis?

• If not, then an elimination can occur

Page 14: Common Subexpression Elimination

Simultaneous CSEs

• The def/use analysis is expensive– You can not run the def use analysis for every

potential CSE

• Thus all CSEs should be analyzed simultaneously

• So, extra assignments are placed everywhere in the code a CSE could be

Page 15: Common Subexpression Elimination

Simultaneous CSEs

• When tracing definitions, those introduced definitions must be explicitly ignored

• Trace back from a use

• If it is a definition associated with a CSE we are cool

• If it is an introduced one, pass through

• If it is neither, we can not use this

Page 16: Common Subexpression Elimination

Altogether Now…

• Insert the extra assignments

• For every similar expression– At every site, try to eliminate this expression

• Delete the assignments, so as not to interfere with anything else

Page 17: Common Subexpression Elimination

Interaction with Copy Propagation

• Any temps introduced are placed after the calculation, so that copy propagation can remove thema = f * i a = f * i

temp_1 = a… …b = f * i b = temp_1

temp_2 = b… …c = f * i c = temp_2

Page 18: Common Subexpression Elimination

CSE Tidbits

• Compiler temps are placed at top level, as the live range of CSEs are unknown

• Associativity is accounted for

• Only binary and unary operations are done– Can be extended

Page 19: Common Subexpression Elimination

Examples

Page 20: Common Subexpression Elimination

Timings – Preliminary Results

• CSE alone seems to have negligable effect

• Global copy propagation gives a few percent increase

• CSE on top of global copy propagation gives a couple percent more

Page 21: Common Subexpression Elimination

Local CSE

• Used Muchnick’s algorithm (described in class)

• Used defs to find what was killed

• Fully implemented– Except method calls

• Since we are using defs already, why not so something more substantial?