252-210: compiler design - eth zürich · pdf file252-210: compiler design 11 intermediate...
TRANSCRIPT
252-210:CompilerDesign
11IntermediateRepresenta/on(s)
ThomasR.Gross
ComputerScienceDepartmentETHZurich,Switzerland
Outline
! DesignconcernsforIR! SSA:StaGcSingleAssignment
2
Simplecompiler:Tree-basedIR
! Oneassignmentstatement:onebinarytreeint A, B, C, D;
A = B + C * D;
! Atreehasaroot! Topnode! Rightandle8subtrees
! Sequencesofstatements:forestoftrees
3
5
IRconcerns
! Makedatadependencesexplicit! Captureproducer–consumerrela/onship
// 1 // x = a + b ;
// other basic blocks //
// 2 // d = x + 1;
// 3 // b = a + c;
! xproducedby1,consumedby2
! bin1mustbereadbeforebiswriNenin39
! SimplesoluGon:execute1,2,and3inorder
! WhatmaNersis1readstheoldvalueand3producesanewvalue
! Idea:renamevariableand/ormakecopies
// 1 // x = a + foo ;
// 2 // d = x + 1;
// 3 // bar = a + c;
11
! Compilerintroducesanewnamewheneveranewvalueisproduced.
! Coulduseanynamebutusuallyusesubscripts:b1,b2,…
// 1 // x = a + b34 ;
// 2 // d = x + 1;
// 3 // b56 = a + c;
! IRusedinopGmizingcompilers:staGcsingleassignment(SSA)
12
11.2StaGcSingleAssignmentIR
! Thereisoneassignmentstatementthatwritesavariable/field/memorylocaGon! Assignmentforshort:statement,expression,….
! Sta/c:inthesource/IR! Single:eachstatementwritesadifferentvariable
! SSAmakesdatadependencesexplicit
13
15
Example
! Statement1producesavalueforstatement2
! Onlytruedependencesrecorded! Noconstraintsduetovariablenames
17
x1 = a0 + b0 d1 = x1 + 1
b1 = a0 + c0
SSAasIR
! Disclaimer:! Onlymethod-localscalarvariablesconsidered
! Arrays&fieldsignoredfornow! Mustbeconserva/ve:nopointer-basedassignmentsfornow
! SSAformforstraightlinecode! Howtoturna(JavaLi/C/Java/…)programintoSSAform
! CondiGonalstatements
18
SSAforabasicblock
! AssumpGon:Program(method)translatedintobasicblocks,forestofIRtreesforeachbasicblock! E.g.,ASTorsimilarIR
! Allsourcesanddes/na/onsofopera/onsvisible! Programwri[enwithoutconsidera/onofSSAformat
! Goal:transformonebasicblockintoSSAformat
! Approach:considerallstatements(IRtrees)insequence
19
! ForeachvariableX:CounterCX! CXini/alizedto0atstartofmethod
! CXindicatesthe“current”version! Givenastatementorexpression
D=S⊗TorS⊗T
1. LookupCSandCT1. Yieldscurrentversion,saySnandTm
2. IncrementCD1. Yieldsnewversion(Dk)
3. ReplaceS,T,D:Dk=Sn⊗TmorSn⊗Tm
20
Example
x = a + b
y = x + 2
z = a + 1
x = c * 2
w = x + 1
21
Example
x1 = a0 + b0 y1 = x1 + 2
z1 = a0 + 1
x2 = c0 * 2
w1 = x2 + 1
22
Example
TurnthefollowingexampleintoSSAform(3min)
a = a + x
x = a + b
c = a + b
x = c + 2
a = x + b
23
Example
TurnthefollowingexampleintoSSAform(3min)
a1 = a0 + x0 x1 = a1 + b0 c1 = a1 + b0 x2 = c1 + 2
a2 = x2 + b0
24
! Eachbasicblockcanbehandledthatway….! Onlythevariablenamesarechanged
! Otherwiseusetreesasbefore! CoulduseanyotherIR
! SomeGmesvariableaj iscalledaversionofa
! NeedrightvalueforcountersCXatthestartofabasicblock
25
CondiGonalstatements
! Simpleexample
a = 1;
if (b ≠ 0) {
a = 0;
}
x = a ;
Counters
! Ca = ! Cb = ! Cx = 26
2.2CondiGonalstatements
! Simpleexample
a = 1;
if (b ≠ 0) {
a = 0;
}
x = a ;
Counters
! Ca = ! Cb = ! Cx = 27
BB0
BB1
BB2
Findingthecurrentversion
! Simpleexample
a = 1;
if (b ≠ 0) {
a = 0;
}
x = a ;
28
a1 = 1;
if (b0 ≠ 0) {
a2 = 0;
}
x1 = a??? ;
Can’tuse a1 Can’tuse a2
SoluGon:φfuncGon
! Introducea“magic”funcGonφ
! φ funcGondeliversthecorrectversion! (intheexample)
! If(b0=0):returnsa1! If(b0≠0):returnsa2
! ThefuncGonpicksthecorrectversiondependingonthepathtakentoreachBB2! Moreprecisely:thepointwhereweneedtouseeithera1ora2
30
φfuncGon
! Result(returnvalue)dependsonpathtaken! Resultvalueassignedtoanewversionofvariable
! Argumentsarepossiblereturnvalues! Differentversionsof(conceptually)thesamevariable
32
a1 = 1;
if (b0 ≠ 0) {
a2 = 0;
}
a3 = φ (a2, a1)
x1 = a3 ;
φfuncGon--Notes
! φfuncGonappearsonlyontherighthandsideofanassignment
! φfuncGonplacedatbeginningofbasicblock! Notmandatorybutsimplifiesreadingexamples
! Simplifiesop/miza/ons/transforma/ons
35
38
ConverGngtoSSA
! GivenaCFGwithENTRYnode,forestofIRtreesorAST! StartwithENTRYnode
! Converttheopera/onsinthisbasicblock
! Insertφ funcGonsasneeded! MoreonthisinAdvancedCompilerDesign
! ProcessnextbasicblockunGlallblockshavebeenprocessed
39
ImplemenGngφ funcGons
40
BB0
BB1 BB2
BB2
if (b0 ≠ 0)
a2 = 0 a1 = 1
a3 = φ (a2 , a1)
! Assigna1anda2tothesameregister! Botha1anda2areallocatedtoaregisterandtheygetthesame
register
! Usefulifa3isreadbeforeitisspilled
! Createatemporaryt(storedinmemory)andinsertcopystatements
41
BB1 BB2
BB2
a2 = 0 t = a2
a1 = 1 t = a1
a3 = t
SSAinpracGce
! Usedinmanycompilers! InGCC3.0(2001)
! InteresGngconsequencesforregisterallocaGon! Interferencegraphchordalifvariableversionsdefinenodes
! Edgesindicatethatthereisaconflict
42
SSAinpracGce
! Usedinmanycompilers
! InteresGngconsequencesforregisterallocaGon! Interferencegraphchordalifvariableversionsdefinenodes
! Edgesindicatethatthereisaconflict! Canbecoloredinpolynomial/me
! Butthereisnofreelunch! Interferencegraphbasedonvariableversionshasalargernumberof
nodesthaninterferencegraphbasedonliveranges
44