Upload
buikhuong
View
222
Download
2
Embed Size (px)
Citation preview
CompilerDesignSpring2017
10Intermediaterepresentation(s)
Dr.Zoltán Majó
CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation
Outline
§ DesignconcernsforIR
§ SSA:StaticSingleAssignment
2
Simplecompiler:Tree-basedIR
§ Oneassignmentstatement:onebinary treeint A, B, C, D;
A = B + C * D;
§ Atreehasaroot§ Topnode§ Rightandleftsubtrees
§ Sequencesofstatements:forest oftrees
3
x = a + b;
d = x + 1;
b = a + c;
4
5
IRconcerns
§ Makedatadependencesexplicit§ Captureproducer-consumerrelationship
// 1 // x = a + b ;
// 2 // d = x + 1;
// 3 // b = a + c;
§ x producedby1,consumedby2
§ b in1mustbereadbeforeb iswrittenin3
9
10
IRconcerns
§ Makedatadependencesexplicit§ Captureproducer-consumerrelationship
// 1 // x = a + b ;
// other basic blocks //
// 2 // d = x + 1;
// 3 // b = a + c;
§ x producedby1,consumedby2
§ b in1mustbereadbeforeb iswrittenin3
11
§ Simplesolution:execute1,2,and3inorder
§ Whatmattersis1readstheold value(ofb)and3producesanew value(ofb)
§ Idea:renamevariableand/ormakecopies// 1 // x = a + b;
// 2 // d = x + 1;
// 3 // b = a + c;
12
// 1 // x = a + foo;
// 2 // d = x + 1;
// 3 // bar = a + c;
§ Compilerintroducesanewnamewheneveranewvalueisproduced.
§ Coulduseanynamebutusuallyusesubscripts:b1,b2,…// 1 // x = a + b34;
// 2 // d = x + 1;
// 3 // b56 = a + c;
§ IRusedinoptimizingcompilers:staticsingleassignment(SSA)
13
10.2StaticSingleAssignmentIR
§ Thereisoneassignmentstatementthatwritesavariable/field/memorylocation§ Assignmentforshort:statement,expression,…§ Static:inthesource/IR§ Single:eachstatementwritesadifferentvariable
§ SSAmakesdatadependencesexplicit
14
Example
15
x1 = a0 + b0d1 = x1 + 1
b1 = a0 + c0
16
Example
§ Statement1producesavalueforstatement2
§ Onlytruedependencesrecorded§ Noconstraintsduetovariablenames
19
x1 = a0 + b0d1 = x1 + 1
b1 = a0 + c0
SSAasIR
§ Disclaimer:§ Onlymethod-localscalarvariablesconsidered§ Arrays&fieldsignoredfornow§ Mustbeconservative:nopointer-basedassignmentsfornow
§ SSAformforstraightlinecode§ Howtoturna(JavaLi/C/Java/…)programintoSSAform
§ Conditionalstatements
20
SSAforabasicblock
§ Assumption:Program(method)translatedintobasicblocks,forestofIRtreesforeachbasicblock§ E.g.,ASTorsimilarIR§ Allsourcesanddestinationsofoperationsvisible§ ProgramwrittenwithoutconsiderationofSSAformat
§ Goal:transformonebasicblockintoSSAformat
§ Approach:considerallstatements(IRtrees)insequence
21
SSAconstructionforabasicblock
§ ForeachvariableX:CounterCX§ CX initializedto0atstartofmethod
§ CX indicatesthe“current”version
§ Givenastatementorexpression
D=S⊗ Tor S⊗ T
1. LookupCS andCT§ Yieldscurrentversion,saySn andTm
2. IncrementCD§ Yieldsnewversion(Dk)
3. ReplaceS,T,D:Dk =Sn ⊗ Tm or Sn ⊗ Tm22
Example
NoSSAx = a + b
y = x + 2
z = a + 1
x = c * 2
w = x + 1
SSAx1 = a0 + b0
y1 = x1 + 2
z1 = a0 + 1
x2 = c0 * 2
w1 = x2 + 1
23
Example
PleaseturnthefollowingexampleintoSSAform(3min)
a = a + x
x = a + b
c = a + b
x = c + 2
a = x + b
24
25
SSAconstructionforabasicblock
§ Eachbasicblockcanbehandledthatway…
§ Onlythevariablenamesarechanged§ Otherwiseusetreesasbefore§ CoulduseanyotherIR
§ Sometimesvariableaj iscalledaversionofa
§ NeedrightvalueforcountersCX atthestartofabasicblock
27
Conditionalstatements
§ Simpleexamplea = 1;
if (b ≠ 0) {
a = 0;
}
x = a ;
§ CountersCa =
Cb =
Cx =
28
BB0
BB1
BB2
Findingthecurrentversion
§ Simpleexamplea = 1;
if (b ≠ 0) {
a = 0;
}
x = a ;
§ CountersCa =
Cb =
Cx =
a1 = 1;
if (b0 ≠ 0) {
a2 = 0;
}
x1 = a???;
§ Can’tusea1§ Can’tusea2
29
BB0
BB1
BB2
Solution:f function
§ Introducea“magic”functionf § Insertanodewiththef function(f node)
§ f functiondeliversthecorrectversion§ (intheexample)§ If(b0 =0):returnsa1§ If(b0 ≠0):returnsa2
§ ThefunctionpicksthecorrectversiondependingonthepathtakentoreachBB2§ Moreprecisely:thepointwhereweneedtouseeithera1 ora2
30
a1 = 1;if (b0 ≠ 0) {
a2 = 0;}x1 = a???;
Paths
31
a1 = 1;
if (b0 ≠ 0) {
a2 = 0;
}
a3 = f (a1, a2)
x1 = a3 ;
32
f function
§ Result(returnvalue)dependsonpathtaken§ Resultvalueassignedtoanewversionofvariable
§ Argumentsarepossiblereturnvalues§ Differentversionsofthesame(conceptually)variable
34
a1 = 1;
if (b0 ≠ 0) {
a2 = 0;
}
a3 = f (a1, a2)
x1 = a3 ;
f function– Notes
§ f functionappearsonlyontherighthandsideofanassignment
§ f functionplacedatbeginningofbasicblock§ Notmandatorybutsimplifiesreadingexamples§ Simplifiesoptimizations/transformations
35
f function– Notes
§ f functionreadsargumentsinpredecessorbasicblock§ (Ifpathwastaken)
if (b0 ≠ 0) {
a2 = 0;
}
else {
a1 = 1;
}
a3 = f (a2 , a1)
36
BB0BB1
BB2
BB2
ConvertingtoSSA
§ GivenaCFGwithENTRYnode,forestofIRtreesorAST
§ StartwithENTRYnode§ Converttheoperationsinthisbasicblock
§ Insertf functionsasneeded§ MoreonthisinAdvancedCompilerDesign
§ Processnextbasicblockuntilallblockshavebeenprocessed
39
Implementingf functions
40
BB0
BB1 BB2
BB2
if (b0 ≠ 0)
a2 = 0 a1 = 1
a3 = f (a2 , a1)
Implementingf functions
§ Option1:Assigna1 anda2 tothesameregister§ Botha1 anda2 areallocatedtoaregisterand theygetthesame
register§ Usefulifa3 isreadbeforeitisspilled
§ Option2:Createatemporaryt (storedinmemory)andinsertcopystatements
41
BB1 BB2
BB2
a2 = 0t = a2
a1 = 1t = a1
a3 = t
SSAinpractice
§ Usedinmanycompilers§ InGCC3.0(2001)§ HotSpot JVMC1andC2
§ Consequence:Optimizations(transformations)simplified§ Def-usechainsexplicitintheIR
§ Interestingconsequencesforregisterallocation§ Interferencegraphchordalifvariableversionsdefinenodes
§ Edgesindicatethatthereisaconflict
§ Butthereisnofreelunch§ Interferencegraphbasedonvariableversionshasalargernumberof
nodesthaninterferencegraphbasedonliveranges
42
43
CompilerDesignSpring2017
10Intermediaterepresentation(s)
Dr.Zoltán Majó
CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation
IRssofar
§ JavaLi:Tree-based§ Globalprogramanalysis:Three-addresscode§ SSA
§ OtherIRs§ C1:CFGwith“instructions”
§ High-levelIR:usesvirtualregisters§ Low-levelIR:moremachinedetails,physicalregisters
§ C2:“Seaofnodes”§ NoCFG,nobasicblocks§ Dataandcontroldependencesexplicit
48