SSA Form for the Java HotSpot™ Client Compiler
Transcript of SSA Form for the Java HotSpot™ Client Compiler
Sun Microsystems, Inc.
Institute for System Software
Johannes Kepler University Linz, Austria
Department of Computer Science
University of California, Irvine
Java HotSpot™ Client Compiler
SSA Form for the
Christian Wimmer [email protected]
www.christianwimmer.at
April 2009
2
Java is also an island…
Java Source Code
Java Compiler
Java Bytecodes
Just-in-Time Compiler
Garbage Collector
Java Class Library
Java Language
Specification
Java Virtual Machine
Specification
Java Virtual Machine
Java Specification
Requests
Java Enterprise Edition
Java Servlets
Java Server Faces
Enterprise JavaBeans
Java Persistence API
Class Loader
Interpreter
Java Micro Edition
Today’s Talk
3
Java HotSpot™ VM
Execution Engine
executes compiles generates
Garbage Collector collects
access memory
loads Class Loader
Interpreter JIT Compiler
Stop-and-Copy
Mark-and-Compact
Heap
Bytecodes Machine Code
Eden
Old Space collects
Deoptimization
Young Generation
Old Generation
From To
4
Client Compiler – Design
Compilation Speed Code Quality
Completeness
All Optimizations
Data Flow Analysis
Profile Data Simple Structure
Common Code Patterns
Fast Optimizations
JSR and Locking
Corner Cases
Unstructured Bytecodes
Design Decisions
for a JIT Compiler
Client Compiler Server Compiler
Interpreter
5
Bytecode
Parsing
Client Compiler – Structure
Bytecodes
HIR
Optimized HIR
LIR
Machine Code
Method Inlining
Constant Propagation
Local Value Numbering
Register Allocation
Global
Optimizations
LIR Generation
Code Generation
Back End
Bounds Check Elimination
HIR (SSA Form)
Global Value Numbering
Register Allocation
Escape Analysis
Null Check Elimination
Conditional Expr. Elim.
[Thomas Würthinger]
[Thomas Kotzmann]
Phi Function Placement
6
!""#$%&'(!"
)""#*%+,-()"
.""#/(#*01$2")3
4""#$%&'(!"
5""#$%&'(!"
3""#*%+,-()"6""#,78"
9""#+:%;2"!"#$%
)."#07$"
)<"#,-%=2()"
)>"?%-%")9
)3"#*%+,-()"
)6"#,-%=2()"
)9"#$%&'()"
.!"#=2-7=+"
!"#"$%&$'"&!"#$@$'"&+A"B"
""$'""1C"
""$(&@+"D")A"B"""""1"E"+"F"!"#$@+"G")AC"
""H")*!)"B"
""""1"E")C"
""H"
""+)",+'"1C"H"
#!" G"
#!" G" $-&
#!" G" $-&#!"
#!" G" $.&#!"#!"
#!" G" $/&#!"
#!" G" $0&#!"
#!" G" $1&
#!" $1&#!" #5"
#!" #6"
#!"#)!"
#!" G"
Phi Function Placement
7
!""#$%&'(!"
)""#*%+,-()"
.""#/(#*01$2")3
4""#$%&'(!"
5""#$%&'(!"
3""#*%+,-()"6""#,78"
9""#+:%;2"!"#$%
)."#07$"
)<"#,-%=2()"
)>"?%-%")9
)3"#*%+,-()"
)6"#,-%=2()"
)9"#$%&'()"
.!"#=2-7=+"
#!""&"'"()$)'%*%
#)"")"
".""#!"IE"#)"J"K."L"K)"
#)!"1M#N#5O"#6P"
"))"#=2-7=+"#)!"
#6"")"
"9""?%-%"K<"
#<"")"#>""#!"G"#<"
#4""#+:%;2"!"#$@#>A"
#5""#!"F"#4"
"3""?%-%"K<"
K!"
K)"
K."
K<"
Bytecodes Local Variables
and Operand Stack
HIR Instructions
Phi Function Placement – Loops
8
!""#*%+,-()"
)""#,-%=2()"
.""#*%+,-()"<""#,-%=2(."
>""#$%&'(."
4""#$%&'(!"
5""#/(#*01?-")9
9""#$%&'()"
)!"#$%&'(."
))"#07$")."#,-%=2()"
)<"##+*".")"
)5"?%-%">"
)9"#$%&'()"
.!"#=2-7=+"
!"#"$%&$'"&!"#$@$'""+A"B"
""$'"&1"E")C"
""(2+&@$'"&#"E")C"#"IE"+C"#QQA"B"""""1"E"1"F"#C"
""H"
""+)",+'&1C"
H"
#<" #3" #9"
#!" #)" #)"
#<" #>" #4"
#<" #>" #4"
Phi Function Placement – Loops
9
!""#*%+,-()"
)""#,-%=2()"
.""#*%+,-()"<""#,-%=2(."
>""#$%&'(."
4""#$%&'(!"
5""#/(#*01?-")9
9""#$%&'()"
)!"#$%&'(."
))"#07$")."#,-%=2()"
)<"##+*".")"
)5"?%-%">"
)9"#$%&'()"
.!"#=2-7=+"
#!""&"'"()$)'%*%
#)"")"
".""?%-%"K)"
"))"#=2-7=+"#>"
#3""#>"F"#4"
#6"")"
#9""#4"Q"#6"")!"?%-%"K)"
#<""1M#N#!O"GP"
#>""1M#N#)O"GP"
#4""1M#N#)O"GP""5""#4"D"#<"J"K<"L"K."
K!"
K."
K<"
Bytecodes Local Variables HIR Instructions
#<""1M#N#!O"#<P"
#>""1M#N#)O"#3P"
#4""1M#N#)O"#9P""5""#4"D"#<"J"K<"L"K."
K)"
$-& #3" #9"
#!" #)" #)"
$-& #>" #4"
$-& #>" #4"
Phi Function Placement – Loops
10
!""#*%+,-()"
)""#,-%=2()"
.""#*%+,-()"<""#,-%=2(."
>""#$%&'(."
4""#$%&'(!"
5""#/(#*01?-")9
9""#$%&'()"
)!"#$%&'(."
))"#07$")."#,-%=2()"
)<"##+*".")"
)5"?%-%">"
)9"#$%&'()"
.!"#=2-7=+"
#!""&"'"()$)'%*%
#)"")"
".""?%-%"K)"
"))"#=2-7=+"#>"
#3""#>"F"#4"
#6"")"
#9""#4"Q"#6"")!"?%-%"K)"
K!"
K."
K<"
Bytecodes Local Variables HIR Instructions
Simplify Phi Functions
Eliminate #<"Replace with #!
#>""1M#N#)O"#3P"
#4""1M#N#)O"#9P""5""#4"D"$-"J"K<"L"K."
K)"
#!" #3" #9"
#!" #)" #)"
#!" #>" #4"
#!" #>" #4"
Phi Function Placement – Loops
11
!""#*%+,-()"
)""#,-%=2()"
.""#*%+,-()"<""#,-%=2(."
>""#$%&'(."
4""#$%&'(!"
5""#/(#*01?-")9
9""#$%&'()"
)!"#$%&'(."
))"#07$")."#,-%=2()"
)<"##+*".")"
)5"?%-%">"
)9"#$%&'()"
.!"#=2-7=+"
#!""&"'"()$)'%*%
#)"")"
".""?%-%"K)"
"))"#=2-7=+"#>"
#3""#>"F"#4"
#6"")"
#9""#4"Q"#6"")!"?%-%"K)"
K!"
K."
K<"
Bytecodes Local Variables HIR Instructions
#>""1M#N#)O"#3P"
#4""1M#N#)O"#9P""5""#4"D"#!"J"K<"L"K."
K)"
Create Phi Functions
Only for Variables
Modified in a Loop
Simplify Phi Functions
12
SSA Construction
Detect Block Boundaries
Mark Loop Headers
field not hot
Mark Variables Modified in Loops
Reverse Postorder of Blocks
Fill Blocks with HIR Instructions
Create Phi Function
Variable Equal in
all Predecessors?
yes no
yes no no
Global Optimizations
…
Simplify Phi Functions
Setup Phi Functions
Abstract Interpretation of Block
Save Variable State
Variable Modified
in any Loop?
yes
Phi Function Necessary for a Variable?
Loop Header?
yes
no Variable Alive?
No Phi Function Necessary
Phi Function Statistics
13
One Run of SPECjvm98 (All Benchmarks)
~1000 Methods Compiled
~ 210 KByte Code Compiled
Without Loop Optimization With Loop Optimization
Created Simplified Created Simplified
Loop Phi Functions 2843 2046 72% 1224 429 35%
Other Phi Functions 1930 376 20% 1685 108 6%
Total 4773 2422 50% 2909 537 18%
~ 122,000 HIR Instructions
SSA Deconstruction
14
#!""&"'"()$)'%*%
#)"")"
"))"#=2-7=+"#>"
#3""#>"F"#4"
#9""#4"Q"#)"
K!"
K."
K<"
#>""1M#N#)O"#3P"
#4""1M#N#)O"#9P"
"5""#4"D"#!"J"K<"L"K."
K)"
0%:2"2*RGDS)"
0%:2")GDS."
0%:2")GDS<"
0%:2"S.GD2&R"
=2-7=+"2&R"
0%:2"S.GDS>"
07$""S>OS<GDS>"
0%:2"S<GDS4"&''""S4O)GDS4"
0%:2"S>GDS."
0%:2"S4GDS<"
T701"K)"
*01""S<OS)"
T701"UVOK<"
LIR Operation HIR Instructions
R1
R2
R3
R4
R5
Intervals for Linear Scan Register Allocation
Moves in Predecessors
No Coalescing
Register Hints for
Linear Scan Register Allocator
0%:2")GD2&R"
0%:2")GD28R"
=2-7=+"2&R"
07$""2&RO28RGD2&R"
&''""28RO)GD28R"
T701"K)"
*01""28RO2*R"
T701"UVOK<"
eax
ecx
eax
ebx
ebx
LIR Register for Phi Function
Delete Unnecessary Moves
K!"
K."
K<"
K)"
Exception Handling
15
#!"")"
#)""?2-/#2$'"WWW"
#.""#&$%&'"WWW"#<""."
#>""*&$$"WWW"
#4""'#:"WWW"
WWW"
#)!"1M#N#!O#!O#<O#<P"
WWW"
Normal Control flow
All Critical Edges Split
Save Position to Insert Moves
Exception Edges
Start “In the Middle” of a Block
Many Phi Function Operands
Split Critical Exception Edges?
Would Lead to Many Blocks
Most of them Finally Empty
Overhead to Remove Them
Phi Functions in LIR
Known by Register Allocator
Create Adapter Blocks on Demand
Array Bounds Check Elimination
16
&!""&"'"()$)'%"%
#)""&"'"()$)'%*%
#.""!"
"<""?%-%"K)"
")!"=2-7=+"
"5""&!N#>P"E"#."
#3"")"
#6""#>"Q"#3""9""?%-%"K)"
K."
K<"
HIR Instructions
#>""1M#N#.O"#6P"
"4""#>"I"#)"J"K."L"K<"
K)"
!"#"$%&32$4"#+)"'@$'"NP"&O"$'""+A"B"
""(2+"@$'""#"E"!C"#"I"+C"#QQA"B"
""""&N#P"E"!C"""H"
H"
Java Source Code
Bounds for #> at instruction 5"
Lower Bound: #."
Increasing (because of #6)
Upper Bound: #)"
&!""&"'"()$)'%"%
#)""&"'"()$)'%*%
#.""!""))"*M2*;"#)"IE"&!W$2+?-M"
"<""?%-%"K)"
All Bounds Loop Invariant
K!"
Insert Check Before Loop
Remove Check Inside Loop
Java Exception Semantics
Integer Overflows
Escape Analysis
17
#!""&"'"()$)'%*%
#)"")"
".""#!"IE"#)"J"K."L"K)"
&9""+2X",*$)-)'%
")!"&9W6"E"#)"
"))"?%-%"K<"
&<""+2X",*$)-)'%
#>""#!"Y"#)"
#4""#+:%;2"!"#$@#>A"#5""#!"F"#4"
"3""&<W6"E"#5"
"6""?%-%"K<"
HIR Instructions
&)."1M#N&<O"&9P"
#)>"&).W6"")4"#=2-7=+"#)>"
K!"
K)"
K."
K<"
!"#"$%&$'"&!"#$@$'"&+A"B"
""Z+-2?2="1C"
""$(&@+"D")A"B"""""1"E"')5"Z+-2?2=@+"F"!"#$@+"G")AAC"
""H")*!)"B"
""""1"E"')5"Z+-2?2=@)AC"
""H"
""+)",+'"1W#+-[&$72@AC"H"
Java Source Code
#!" G"
#!" &<"
#!" &9"
Escape Analysis
18
#!""&"'"()$)'%*%
#)"")"
".""#!"IE"#)"J"K."L"K)"
&9""+2X",*$)-)'%
")!"&9W6"E"#)"
"))"?%-%"K<"
&<""+2X",*$)-)'%
#>""#!"Y"#)"
#4""#+:%;2"!"#$@#>A"#5""#!"F"#4"
"3""&<W6"E"#5"
"6""?%-%"K<"
HIR Instructions
#!"&)."
&9W6L#)"
&<W6L#5"
&).W6L#)<"&)."1M#N&<O"&9P"
#)<"1M#N#5O"#)P"
#)>"&).W6"")4"#=2-7=+"#)>"
"))"?%-%"K<"
#>""#!"Y"#)"
#4""#+:%;2"!"#$@#>A"#5""#!"F"#4"
"6""?%-%"K<"
#)<"1M#N#5O"#)P"
")4"#=2-7=+"#)<"
K!"
K)"
K."
K<"
Track Field Values
State Similar to Variables
Create Phi Functions
Track Escape State
Method Local
Thread Local
Escaping
Optimize Non-Escaping Objects
Eliminate Object Allocation
Eliminate Field Stores
Replace Field Loads
Alias Effects
Equi-Escape Sets
19
Performance Comparison
Speedup of JDK 6 Client and Server relative to JDK 5 Client
JDK 5 Client
JDK 6 Client
JDK 6 Server
Intel Pentium D processor 830 with 3.0 GHz, 2 GByte
main memory, Microsoft Windows XP Professional
Client and Server configuration use same garbage collector
First run
Last run
Selected Publications
!! Client Compiler, SSA Form, Linear Scan Register Allocation "! Thomas Kotzmann, Christian Wimmer, Hanspeter Mössenböck, Thomas Rodriguez, Kenneth Russell,
David Cox: Design of the Java HotSpot™ Client Compiler for Java 6. In ACM Transactions on Architecture and Code Optimization, volume 5, issue 1, article 7. ACM Press, 2008. doi:10.1145/1369396.1370017
"! Christian Wimmer, Hanspeter Mössenböck: Optimized Interval Splitting in a Linear Scan Register Allocator. In Proceedings of the ACM/USENIX International Conference on Virtual Execution Environments, pages 132-141. ACM Press, 2005. doi:10.1145/1064979.1064998
!! Array Bounds Check Elimination "! Thomas Würthinger, Christian Wimmer, Hanspeter Mössenböck: Array Bounds Check Elimination
in the Context of Deoptimization. In Science of Computer Programming, volume 74, issues 5-6, pages 279-295. Elsevier, 2009. doi:10.1016/j.scico.2009.01.002
!! Escape Analysis "! Thomas Kotzmann, Hanspeter Mössenböck: Escape Analysis in the Context of Dynamic
Compilation and Deoptimization. In Proceedings of the ACM/USENIX International Conference on Virtual Execution Environments, pages 111-120. ACM Press, 2005. doi:10.1145/1064979.1064996
"! Thomas Kotzmann, Hanspeter Mössenböck: Run-Time Support for Optimizations Based on Escape Analysis. In Proceedings of the International Symposium on Code Generation and Optimization, pages 49-60. IEEE Computer Society, 2007. doi:10.1109/CGO.2007.34
!! Complete List of Publications: "! http://wikis.sun.com/display/HotSpotInternals/Publications+JKU
20