Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin...
-
date post
21-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin...
![Page 1: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/1.jpg)
Copy Propagation and Common Subexpression Elimination in Titanium
Johnathon Jamison
David Marin
CS265
S. Graham
![Page 2: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/2.jpg)
Outline
• Titanium
• Def/Use analysis (used by CSE)
• Copy Propagation
• Common Subexpression Elimination
• Implementation
• Examples
![Page 3: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/3.jpg)
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: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/4.jpg)
Def/Use
• Given:
a = …
…
…a…
• We want to link the use of a to the definition of a above.
![Page 5: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/5.jpg)
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: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/6.jpg)
Def/Use
• The Titanium compile has (may) def/use information available
• It seems this could be leveraged for Copy Propagation or CSE (rather than writing a whole new dataflow analysis)
![Page 7: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/7.jpg)
Global Copy Propagation
• Given
a = b;
…
x = a + 1;
• We want to replace a with b on the last line, but we need to know that a and b are unchanged
• Def/use analysis isn’t quite enough (why?)
![Page 8: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/8.jpg)
Inserting Fake Defs and Uses
• Add fake defs and uses so that def/use analysis gives us the info we need
b = b;
a = b;
…
newfaketemp = b;
x = a + 1;• We can use a similar technique to enable CSE.
![Page 9: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/9.jpg)
CSE
• Given:
a = f * i
…
b = f * i
• We want to compute f * i only once
![Page 10: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/10.jpg)
CSE
• We could do:
a = f * i
temp = a
…
b = temp
• But only if the value of f * i has not changed
![Page 11: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/11.jpg)
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 12: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/12.jpg)
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 13: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/13.jpg)
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 14: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/14.jpg)
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 15: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/15.jpg)
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 16: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/16.jpg)
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 17: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/17.jpg)
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 18: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/18.jpg)
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 19: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/19.jpg)
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 20: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/20.jpg)
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 21: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/21.jpg)
Examples
![Page 22: Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.](https://reader030.fdocuments.us/reader030/viewer/2022032522/56649d635503460f94a45d1e/html5/thumbnails/22.jpg)
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