36
Compiler Design Spring 2017 10 Intermediate representation(s) Dr. Zoltán Majó Compiler Group – Java HotSpot Virtual Machine Oracle Corporation

Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Embed Size (px)

Citation preview

Page 1: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

CompilerDesignSpring2017

10Intermediaterepresentation(s)

Dr.Zoltán Majó

CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation

Page 2: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Outline

§ DesignconcernsforIR

§ SSA:StaticSingleAssignment

2

Page 3: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Simplecompiler:Tree-basedIR

§ Oneassignmentstatement:onebinary treeint A, B, C, D;

A = B + C * D;

§ Atreehasaroot§ Topnode§ Rightandleftsubtrees

§ Sequencesofstatements:forest oftrees

3

Page 4: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

x = a + b;

d = x + 1;

b = a + c;

4

Page 5: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

5

Page 6: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

IRconcerns

§ Makedatadependencesexplicit§ Captureproducer-consumerrelationship

// 1 // x = a + b ;

// 2 // d = x + 1;

// 3 // b = a + c;

§ x producedby1,consumedby2

§ b in1mustbereadbeforeb iswrittenin3

9

Page 7: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

10

Page 8: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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

Page 9: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

§ 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;

Page 10: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

§ Compilerintroducesanewnamewheneveranewvalueisproduced.

§ Coulduseanynamebutusuallyusesubscripts:b1,b2,…// 1 // x = a + b34;

// 2 // d = x + 1;

// 3 // b56 = a + c;

§ IRusedinoptimizingcompilers:staticsingleassignment(SSA)

13

Page 11: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

10.2StaticSingleAssignmentIR

§ Thereisoneassignmentstatementthatwritesavariable/field/memorylocation§ Assignmentforshort:statement,expression,…§ Static:inthesource/IR§ Single:eachstatementwritesadifferentvariable

§ SSAmakesdatadependencesexplicit

14

Page 12: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Example

15

x1 = a0 + b0d1 = x1 + 1

b1 = a0 + c0

Page 13: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

16

Page 14: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Example

§ Statement1producesavalueforstatement2

§ Onlytruedependencesrecorded§ Noconstraintsduetovariablenames

19

x1 = a0 + b0d1 = x1 + 1

b1 = a0 + c0

Page 15: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

SSAasIR

§ Disclaimer:§ Onlymethod-localscalarvariablesconsidered§ Arrays&fieldsignoredfornow§ Mustbeconservative:nopointer-basedassignmentsfornow

§ SSAformforstraightlinecode§ Howtoturna(JavaLi/C/Java/…)programintoSSAform

§ Conditionalstatements

20

Page 16: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

SSAforabasicblock

§ Assumption:Program(method)translatedintobasicblocks,forestofIRtreesforeachbasicblock§ E.g.,ASTorsimilarIR§ Allsourcesanddestinationsofoperationsvisible§ ProgramwrittenwithoutconsiderationofSSAformat

§ Goal:transformonebasicblockintoSSAformat

§ Approach:considerallstatements(IRtrees)insequence

21

Page 17: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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

Page 18: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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

Page 19: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Example

PleaseturnthefollowingexampleintoSSAform(3min)

a = a + x

x = a + b

c = a + b

x = c + 2

a = x + b

24

Page 20: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

25

Page 21: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

SSAconstructionforabasicblock

§ Eachbasicblockcanbehandledthatway…

§ Onlythevariablenamesarechanged§ Otherwiseusetreesasbefore§ CoulduseanyotherIR

§ Sometimesvariableaj iscalledaversionofa

§ NeedrightvalueforcountersCX atthestartofabasicblock

27

Page 22: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Conditionalstatements

§ Simpleexamplea = 1;

if (b ≠ 0) {

a = 0;

}

x = a ;

§ CountersCa =

Cb =

Cx =

28

BB0

BB1

BB2

Page 23: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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

Page 24: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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???;

Page 25: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Paths

31

a1 = 1;

if (b0 ≠ 0) {

a2 = 0;

}

a3 = f (a1, a2)

x1 = a3 ;

Page 26: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

32

Page 27: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

f function

§ Result(returnvalue)dependsonpathtaken§ Resultvalueassignedtoanewversionofvariable

§ Argumentsarepossiblereturnvalues§ Differentversionsofthesame(conceptually)variable

34

a1 = 1;

if (b0 ≠ 0) {

a2 = 0;

}

a3 = f (a1, a2)

x1 = a3 ;

Page 28: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

f function– Notes

§ f functionappearsonlyontherighthandsideofanassignment

§ f functionplacedatbeginningofbasicblock§ Notmandatorybutsimplifiesreadingexamples§ Simplifiesoptimizations/transformations

35

Page 29: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

f function– Notes

§ f functionreadsargumentsinpredecessorbasicblock§ (Ifpathwastaken)

if (b0 ≠ 0) {

a2 = 0;

}

else {

a1 = 1;

}

a3 = f (a2 , a1)

36

BB0BB1

BB2

BB2

Page 30: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

ConvertingtoSSA

§ GivenaCFGwithENTRYnode,forestofIRtreesorAST

§ StartwithENTRYnode§ Converttheoperationsinthisbasicblock

§ Insertf functionsasneeded§ MoreonthisinAdvancedCompilerDesign

§ Processnextbasicblockuntilallblockshavebeenprocessed

39

Page 31: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

Implementingf functions

40

BB0

BB1 BB2

BB2

if (b0 ≠ 0)

a2 = 0 a1 = 1

a3 = f (a2 , a1)

Page 32: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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

Page 33: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

SSAinpractice

§ Usedinmanycompilers§ InGCC3.0(2001)§ HotSpot JVMC1andC2

§ Consequence:Optimizations(transformations)simplified§ Def-usechainsexplicitintheIR

§ Interestingconsequencesforregisterallocation§ Interferencegraphchordalifvariableversionsdefinenodes

§ Edgesindicatethatthereisaconflict

§ Butthereisnofreelunch§ Interferencegraphbasedonvariableversionshasalargernumberof

nodesthaninterferencegraphbasedonliveranges

42

Page 34: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

43

Page 35: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

CompilerDesignSpring2017

10Intermediaterepresentation(s)

Dr.Zoltán Majó

CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation

Page 36: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w15_02... · Simple compiler: Tree-based IR §One assignment statement: ... §Simple solution: ... §More on

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