Page 1
4/8/17
1
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSEP590–ProgrammingSystemsUniversityofWashington
Lecture2:OverviewPartII–BackEnd
MichaelRingenburgSpring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CourseNews• SubmitpresentaPontopicproposalsbyApril14
– Ifyouwouldliketoworkwithapartner,bothofyouwillhavetopresent,andIwillexpectamoreindepth/longerpresentaPon
– We’reupto19students–trickytofit>18intofinal3weeks.Letmeknowifyou’dbewillingtopresentMay9.
• Otherwisemayhavetocomeearlyorstaylateoneclass(we’llvote)• Todayandnextweek:
– Finishcompileroverview– Cover1or2advancedtopicsincompilers:
• RegisterallocaPonviagraphcoloring• PossiblySSAform
• A]erthat,broadenourhorizonsabitandlookatothertypesofprogrammingsystems
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 1
Page 2
4/8/17
2
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Reminder:CompilerStructure
Source Target
Scanner
Parser
OpPmizer
CodeGen
characters
tokens
IR
IR (often different)
Machine code
SemanPcAnalysis
IR (may be different)
“FrontEnd”
“BackEnd”
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 2
IntermediateRepresenta.ons
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 3
Page 3
4/8/17
3
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
IntermediateRepresentaPons
• TheparserbuildsanintermediaterepresentaPonoftheprogram– TypicallyanAST
• RestofthecompilerchecksandtransformstheIRtoimprove(“opPmize”)it,andeventuallytranslatesittofinalcode– TypicallywilltransforminiPalIRtooneormorelowerlevelIRsalongtheway
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 4
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
IRDesignTaxonomy
• Structure– Graphical(trees,graphs,etc.)– Linear(codeforsomeabstractmachine)– Hybridsarecommon(e.g.,control-flowgraphswithlinearcodeinbasicblocks)
• AbstracPonLevel– High-level,neartosourcelanguage– Low-level,closertomachine
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 5
Page 4
4/8/17
4
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:ArrayReference
Source:A[i,j]AST:
High-levellinear:t1 ← A[i,j]
Low-levellinear(3address):loadI 1 => r1sub rj,r1 => r2loadI 10 => r3mult r2,r3 => r4sub ri,r1 => r5add r4,r5 => r6loadI @A => r7add r7,r6 => r8load r8 => r9
subscript
A i j
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 6
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
GraphicalIRs
• IRsrepresentedasagraph(ortree)• Nodesandedgestypicallyreflectsomestructureoftheprogram– E.g.,source,controlflow,datadependence
• Maybelarge(especiallysyntaxtrees)• High-levelexamples:
– Syntaxtrees– Controlflowgraphs– Datadependencegraphs– O]enusedinopPmizaPonandcodegeneraPon
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 7
Page 5
4/8/17
5
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
GraphicalIR:ConcreteSyntaxTrees
• Thefullgrammarisneededtoguidetheparser,butcontainsmanyextraneousdetails– E.g.,syntacPctokens,rulesthatcontrolprecedence
• Typicallythefullsyntaxtreedoesnotneedtobeusedexplicitly
E
T
T * F
F Id(y)
Id(x)
Subscript
A [ i ]
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 8
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
GraphicalIR:AbstractSyntaxTrees
• WantonlyessenPalstructuralinformaPon• Canberepresentedexplicitlyasatreeorinalinearform,e.g.,
intheorderofadepth-firsttraversal.Fora[i+j],thismightbe:Subscript Id(A) Plus Id(i) Id(j)
• Commonoutputfromparser;usedforstaPcsemanPcs(typechecking,etc.)andsomePmeshigh-levelopPmizaPons
Mult
Id(x) Id(y)
Subscript
A Plus
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 9
Id(i) Id(j)
Page 6
4/8/17
6
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ControlFlowGraph(CFG)• NodesareBasicBlocks
– Codethatalwaysexecutestogether(i.e.,nobranchesintooroutofthemiddleoftheblock).
– I.e.,“straightlinecode”• Edgesrepresentpathsthatcontrolflowcouldtake.– I.e.,possibleexecuPonorderings.– EdgefromBasicBlockAtoBasicBlockBmeansBlockBcouldexecuteimmediatelya]erBlockAcompletes.
• RequiredformuchoftheanalysisdoneintheopPmizer.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 10
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 11
Page 7
4/8/17
7
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y)
print(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 12
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y)
print(“equal”);b = 9;
print(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 13
Page 8
4/8/17
8
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);
print(“hello”);a = 7;if (x == y)
b = 10;print(“equal”);b = 9;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 14
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);
print(“hello”);a = 7;if (x == y)
b = 10;print(“equal”);b = 9;
while (a < b)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 15
Page 9
4/8/17
9
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);
print(“hello”);a = 7;if (x == y)
b = 10;print(“equal”);b = 9;
while (a < b)
a++;print(“increase”);
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 16
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CFGExampleprint(“hello”);a = 7;if (x == y) { print(“equal”); b = 9;} else { b = 10;}while (a < b) { a++; print(“increase”);}print(“done”);
print(“hello”);a = 7;if (x == y)
b = 10;print(“equal”);b = 9;
while (a < b)
a++;print(“increase”);
print(“done”);Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 17
Page 10
4/8/17
10
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
(Program/Data)DependenceGraph
• O]enusedinconjuncPonwithanotherIR.• Inadatadependencegraph,edgesbetweennodesrepresent“dependencies”betweenthecoderepresentedbythosenodes.– IfAandBaccessthesamedata,andAmustoccurbeforeBtoachievecorrectbehavior,thenthereisadependenceedgefromAtoB.
– AàBmeanscompilercan’tmoveBbeforeA.– Granularityofnodesvaries.DependsonabstracPonlevelofrestofIR.E.g.,nodescouldbeloads/stores,orwholestatements.
– E.g.,a=2;b=2;c=a+7;• Where’sthedependence?
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 18
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Typesofdependencies• Read-a]er-write(RAW)/“flowdependence”
– E.g.,a=7;b=a+1;– Thereadof‘a’mustfollowthewriteto‘a’,otherwiseitwon’tseethecorrectvalue.
• Write-a]er-read(WAR)/“anPdependence”– E.g.,b=a*2;a=5;– Thewriteto‘a’mustfollowthereadof‘a’,otherwisethereadwon’tseethecorrectvalue.
• Write-a]er-write(WAW)/“outputdependence”– E.g.,a=1;…a=2;…– Thewritesto‘a’musthappeninthecorrectorder,otherwise‘a’willhavethewrongfinalvalue.
• WhataboutRAR/”inputdependence”??
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 19
Page 11
4/8/17
11
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Loop-CarriedDependence
• Loopcarrieddependence:AdependenceacrossiteraPonsofaloop
for(i=0;i<size;i++) x=foo(x);
• RAWloopcarrieddependence:thereadof‘x’dependsonthewriteof‘x’inthepreviousiteraPon
• IdenPfyingandunderstandingtheseiscriPcalforloopparallelizaPon/vectorizaPon– Ifthecompiler“understands”thenatureofthedependence,itcansomePmesberemovedordealtwith
– O]enusesophisPcatedarraysubscriptanalysisforthis
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 20
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DependenceGraphExample
a = 7;print(“hello”);while (a < b) { print(“increase”); a++;}print(“done”);
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 21
Page 12
4/8/17
12
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DependenceGraphExample
a = 7;print(“hello”);while (a < b) { print(“increase”); a++;}print(“done”);
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 22
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DependenceGraphExample
a = 7;print(“hello”);while (a < b) { print(“increase”); a++;}print(“done”);
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 23
Page 13
4/8/17
13
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DependenceGraphExample
a = 7;print(“hello”);while (a < b) { print(“increase”); a++;}print(“done”);
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 24
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DependenceGraphExample
a = 7;print(“hello”);while (a < b) { print(“increase”); a++;}print(“done”);
LCD
LCD:Loop-CarriedDependence
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 25
Page 14
4/8/17
14
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DependenceGraphExample
a = 7;print(“hello”);while (a < b) { print(“increase”); a++;}print(“done”);
LCD
LCD
LCD:Loop-CarriedDependence
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 26
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LinearIRs
• Pseudo-codeforsomeabstractmachine• LevelofabstracPonvaries• Simple,compactdatastructures
– Commonlyused:arrays,linkedstructures
• Examples:3-addresscode,stackmachinecode T1ß2
T2ßbT3ßT1*T2T4ßaT5ßT4–T3
push2pushbmulPplypushasubtract
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 27
Page 15
4/8/17
15
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
WhatIRtoUse?
• Commonchoice:all(!)– ASTorotherstructuralrepresentaPonbuiltbyparserandusedinearlystagesofthecompiler
• Closertosourcecode• GoodforsemanPcanalysis• Facilitatessomehigher-levelopPmizaPons
– Lowertolow-levellinearIRforlaterstagesofcompiler• Closertomachinecode• Exposesmachine-relatedopPmizaPons• GoodforresourceallocaPonandscheduling
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 28
Seman.cAnalysis
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 29
Page 16
4/8/17
16
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Whatdoweneedtochecktocompilethis?
classC{inta;C(intiniPal){ a=iniPal;}voidsetA(intval){ a=val;}
}
classMain{publicstaPcvoid
main(String[]args){ Cc=newC(17); c.setA(42);
}}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 30
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
BeyondSyntax
• Thereisalevelofcorrectnessthatisnotcapturedbyacontext-freegrammar– Hasavariablebeendeclared?– Aretypesconsistentinanexpression?– Intheassignmentx=y,isyassignabletox?– Doesamethodcallhavetherightnumberandtypesofparameters?
– Inaselectorp.q,isqamethodorfieldofclassinstancep?– IsvariablexguaranteedtobeiniPalizedbeforeitisused?– Inp.q,couldpbenull?– Etc.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 31
Page 17
4/8/17
17
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CheckedProperPes
• SomeenforcedatcompilePme,othersatrunPme(typicallydependsonlanguagespec).
• Differentlanguageshavedifferentrequirements– E.g.,Cvs.Javatypingrules,iniPalizaPonrequirements– SomeoftheseproperPesareo]endesirableinprograms,evenifthelanguagedoesn’trequirethem.
– Compilersshouldn’tenforceapropertythatisnotrequiredbythelanguage(butcanwarn).
– However,therearestaPccheckersforsomeoftheseproperPesthatusecompiler-stylealgorithms.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 32
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Whatelsedoweneedtoknowtogeneratecode?
• Wherearefieldsallocatedinanobject?• Howbigareobjects?(i.e.,howmuchstorageneedstobeallocated)
• Wherearelocalvariablesstoredwhenamethodiscalled?– Stack?Whatoffset?Orexclusivelyinregister?Whichregister?
• Whichmethodsareassociatedwithanobject/class?– InparPcular,howdowefigureoutwhichmethodtocallbasedontherun-Pmetypeofanobject?
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 33
Page 18
4/8/17
18
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
SemanPcAnalysis• Maintasks:
– ExtracttypesandotherinformaPonfromtheprogram– Checklanguagerulesthatgobeyondthecontext-freegrammar– Resolvenames
• RelatedeclaraPonsandusesofeachvariable– “Understand”theprogramwellenoughforsynthesis
• E.g.,sizes,layoutsofclasses/structs• Keydatastructure:Symboltables
– MapeachidenPfierintheprogramtoinformaPonaboutit(kind,type,etc.)
• Thisistypicallyconsideredthefinalpartofthe“frontend”ofthecompiler(oncecomplete,knowwhetherornotprogramislegal).
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 34
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
SomeKindsofSemanPcInformaPon
Information Generated From Used to process
Symbol names (variables, methods)
Declarations Expressions, statements
Type information Declarations, expressions
Operations
Memory layout information
Assigned by compiler Target code generation
Values Constants Expressions (constant folding)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 35
Page 19
4/8/17
19
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ASamplingofSemanPcChecksandComputaPons
• Appearanceofanameinanexpression:id– Check:Symbolhasbeendeclaredandisinscope– Compute:Inferredtypeisthedeclaredtypeofsymbol
• Constant:v– Compute:Inferredtypeandvalueareexplicit– Example:42.0hastypedoubleandvalue42.0
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 36
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ASamplingofSemanPcChecksandComputaPons
• Binaryoperator:exp1opexp2– Check:exp1andexp2havecompaPbletypes
• EitheridenPcal,orwell-definedconversiontoappropriatetypes
• TypesarecompaPblewithop• Example:42 + truefails,20 + 21.9999passes
– Compute:InferredtypeofexpressionisafuncPonoftheoperatorandoperandtypes
• Example:20 + 21.999hastypedouble,42 + “, the answer”hastypeString(inJava).
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 37
Page 20
4/8/17
20
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributeGrammars
• AsystemaPcwaytothinkaboutsemanPcanalysis
• FormalizeproperPeschecked/computedduringsemanPcanalysisandrelatethemtogrammarproducPonsintheCFG.
• SomePmesuseddirectly,butevenwhennot,AGsareausefulwaytoorganizetheanalysisandthinkaboutit
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 38
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributeGrammars
• Idea:associateayributeswitheachnodeinthesyntaxtree
• Examplesofayributes– TypeinformaPon– StorageinformaPon– Assignable(e.g.,expressionvsvariable–lvaluevsrvalueforC/C++programmers)
– Value(forconstantexpressions)– etc.…
• NotaPon:X.aifaisanayributeofnodeXSpring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 39
Page 21
4/8/17
21
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
InheritedandSynthesizedAyributes
• GivenaproducPonX::=Y1Y2…Yn• AsynthesizedayributeX.aisafuncPonofsomecombinaPonofayributesofYi’s(boyomup)– E.g.,avalueayribute
• AninheritedayributeYi.bisafuncPonofsomecombinaPonofayributesX.aandotherYj.c(topdown)– O]enrestrictedabit:onlyY’stothele]canbeused.– E.g.,a“typeenvironment”ora“valueenvironment”–mappingsofsymbolstotypesorvalues(iftheyareknownconstants).
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 40
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributeEquaPons
• ForeachkindofnodewegiveasetofequaPonsrelaPngayributevaluesofthenodeanditschildren– Example:
plus.val=exp1.val+exp2.val
• AyribuPon(aka,evaluaPon)meansimplicitlyfindingasoluPonthatsaPsfiesalloftheequaPonsinthetree
Plus
exp1 exp2
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 41
Page 22
4/8/17
22
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
InformalExampleofAyributeRules
• Supposewehavethefollowinggrammarforatriviallanguageprogram::=declListstmtdeclList::=declListdecl|decltwostmts::=stmtstmtdecl::=intid;stmt::=exp=exp;exp::=id|exp+exp|INTEGER_LITERAL
• Wewanttogivesuitableayributesforbasictypeandlvalue/rvaluechecking,andconstantfolding
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 42
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
InformalExampleofAyributeRules
• Ayributesofnodes– env(typeenvironment)storesthetypesofalldeclaredvariables;synthesizedbydeclaraPons,inheritedbythestatement
• Eachentrymapsanametoitstype– envPre(fordeclaraPons)–Usedtobuilduptheenvironment
• RepresentstheenvironmentpriortothedeclaraPon.• E.g.,“intx;inty;”.TheenvPreof“inty”willmapxtoanint.Theenvof“inty”willmapxtointandytoint.
– type(forexpressions);synthesizedfromchildren(andpossibleenvlookup)
– kind:var(assignable)orval(notassignable);synthesized– value(forexpressions):UNK(unknown)oranInteger,representscomputedconstantvalue;synthesized
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 43
Page 23
4/8/17
23
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforDeclaraPons
• decl::=intid;– decl.env=decl.preEnvU{(id,int)}– IntuiPon:add(id,int)mappingtoanenvironmentcontainingmappingsforpreviousdeclaraPons
• Example:AyribuPonforint y,giventhatwepreviouslysawint x– Sawint xearlier,soassumedecl.preEnv={(x,int)}– decl::=int y;– decl.env=decl.preEnvU{(y,int)}=
{(x,int)}U{(y,int)}= {(x,int),(y,int)}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 44
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforDeclaraPons
• declList1::=declList2decl– decl.preEnv=declList2.env– declList1.env=decl.env– IntuiPon:declList2.envcontainsallofthepreviouslyseenmappings,souseitasthepre-environmentforournewdeclaraPon.Theenvironmentforthecombinedlist(list1)willbetheresultofaddingthemappingfordecltothemappingsofthesublist(list2).
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 45
Page 24
4/8/17
24
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforDeclaraPons
• declList::=decl– decl.preEnv={}– declList.env=decl.env– IntuiPon:ForthefirstelementinourdeclaraPonlist,wecanstartwithanemptyenvironment,becausewewon’thaveseenanydeclaraPonsyet.(Truehere,butprobablynotinareallanguage.)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 46
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
int a; int b; int c;
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}Spring2017
Page 25
4/8/17
25
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}
int a; int b; int c;
declList
declList
declList
decl
decl
decl
inta
intb
intc
Spring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}
int a; int b; int c;
declList
declList
declList
decl
decl
decl
inta
intb
intc
pre:{}
Spring2017
Page 26
4/8/17
26
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}
int a; int b; int c;
declList
declList
declList
decl
decl
decl
inta
intb
intc
pre:{},env:{a,int}
env:{a,int}
Spring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}
int a; int b; int c;
declList
declList
declList
decl
decl
decl
inta
intb
intc
pre:{},env:{a,int}
env:{a,int} pre:{a,int}
Spring2017
Page 27
4/8/17
27
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}
int a; int b; int c;
declList
declList
declList
decl
decl
decl
inta
intb
intc
pre:{},env:{a,int}
env:{a,int} pre:{a,int},env:{(a,int)(b,int)}
Spring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleDeclaraPonList
• declList::=decl• decl.preEnv={}• declList.env=decl.env
• declList1::=declList2decl• decl.preEnv=declList2.env• declList1.env=decl.env
• decl::=intid;• decl.env=decl.preEnvU{(id,int)}
int a; int b; int c;
declList
declList
declList
decl
decl
decl
inta
intb
intc
pre:{},env:{a,int}
env:{a,int} pre:{a,int},env:{(a,int)(b,int)}
env:{(a,int)(b,int)} pre:{(a,int)(b,int)},env:{(a,int)(b,int)(c,int)}
env:{(a,int)(b,int)(c,int)}
Spring2017
Page 28
4/8/17
28
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforProgram
• program::=declListstmt– stmt.env=declList.env– IntuiPon:WewanttotypecheckourstatementgiventhetypeenvironmentsynthesizedbyourdeclaraPonlist.
• Example:Ifprogramwas
int a; int b; b = a + 1;
Wewouldtypechecktheassignmentstatementwiththeenvironment{(a,int),(b,int)}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 54
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforConstants
• exp::=INTEGER_LITERAL– exp.kind=val– exp.type=int– exp.value=INTEGER_LITERAL– IntuiPon:Anintegerconstant(literal)clearlyhastypeint,andexplicitvalue.Youcan’tassigntoit(5=xisnotlegal),soitisavalue(val)notavariable(var).
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 55
Page 29
4/8/17
29
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforIdenPfierExpressions
• exp::=id– id.type=exp.env.lookup(id)
• Ifthislookupfails,issueanundeclaredvariableerror.– exp.type=id.type– exp.kind=var– exp.value=UNK– IntuiPon:WelookuptheidenPfier’stypeintheenvironment,andusethatastheexpression’stype.Ifitdoesn’texistintheenvironment,itmustnothavebeendeclared,soit’sanerror.Sinceitisavariable,itisassignableandhasunknownvalue.
– Example:Typecheckingawithenvironment{(a,int)}givestypeint.Typecheckingbwiththesameenvironmentgivesanerror.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 56
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributesforAddiPon
• exp::=exp1+exp2– exp1.env=exp2.env=exp.env– errorifexp1.type!=exp2.type(orifnotcompaPbleifusingmore
complexrules)
– exp.type=exp1.type(orconvertedtypeifmorecomplexrules)
– exp.kind=val– exp.value=(exp1.value==UNK||exp2.value==UNK)?
UNK:exp1.value+exp2.value– IntuiPon:TypecheckoperandswithsameenvironmentasoperaPon.VerifythattypesarecompaPble,andsetresulttypeappropriately.Notassignable,sosetkindtoval.Computevalueifbothoperandshaveconstantvalue.
Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 57
Page 30
4/8/17
30
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AyributeRulesforAssignment
• stmt::=exp1=exp2;– exp1.env=stmt.env– exp2.env=stmt.env– Errorifexp2.typeisnotassignmentcompaPblewithexp1.type
– Errorifexp1.kindisnotvar(can’tbeval)– IntuiPon:Verifythatle]handsideisassignable,andthattypesofle]andrighthandsidesarecompaPble.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 58
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• program::=declListstmt• stmt.env=declList.env
• exp::=INTEGER_LITERAL• exp.kind=val• exp.type=int• exp.value=INTEGER_LITERAL
• exp::=id• type=exp.env.lookup(id)
• (erroriffails)• exp.type=id.type• exp.kind=var• exp.value=UNKSpring2017
Page 31
4/8/17
31
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• program::=declListstmt• stmt.env=declList.env
• exp::=INTEGER_LITERAL• exp.kind=val• exp.type=int• exp.value=INTEGER_LITERAL
• exp::=id• type=exp.env.lookup(id)
• (erroriffails)• exp.type=id.type• exp.kind=var• exp.value=UNK
TypecheckywithdeclListenvKind:var,Value:UNK
Spring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• program::=declListstmt• stmt.env=declList.env
• exp::=INTEGER_LITERAL• exp.kind=val• exp.type=int• exp.value=INTEGER_LITERAL
• exp::=id• type=exp.env.lookup(id)
• (erroriffails)• exp.type=id.type• exp.kind=var• exp.value=UNK
Type:int,Value:1,Kind:val
Type:int,Value:2,Kind:val
Spring2017
Page 32
4/8/17
32
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• exp::=exp1+exp2• exp1.env=exp2.env=exp.env
• errorifexp1.type!=exp2.type(orifnotcompaPble)
• exp.type=exp1.type(orconvertedtype)
• exp.kind=val• exp.value=(exp1.value==
UNK||exp2.value==UNK)?UNK:exp1.value+exp2.value
Type:int,Value:3,Kind:val
Spring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• exp::=exp1+exp2• exp1.env=exp2.env=exp.env
• errorifexp1.type!=exp2.type(orifnotcompaPble)
• exp.type=exp1.type(orconvertedtype)
• exp.kind=val• exp.value=(exp1.value==
UNK||exp2.value==UNK)?UNK:exp1.value+exp2.value
Type:int,Value:UNK,Kind:val
Spring2017
Page 33
4/8/17
33
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• stmt::=exp1=exp2;• exp1.env=stmt.env• exp2.env=stmt.env• Errorifexp2.typeisnot
assignmentcompaPblewithexp1.type
• Errorifexp1.kindisnotvar(can’tbeval)
Type:int,Value:UNK,Kind:val
TypecheckxwithdeclListenvType:int,Kind:var
Spring2017
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Exampleintx;inty;intz;x=y+(1+2);
program
declList stmt(=)
exp
id(x)
exp
+
exp exp
id(y)+
…
INT(1) INT(2)
• stmt::=exp1=exp2;• exp1.env=stmt.env• exp2.env=stmt.env• Errorifexp2.typeisnot
assignmentcompaPblewithexp1.type
• Errorifexp1.kindisnotvar(can’tbeval)
Type:int,Value:UNK,Kind:val
TypecheckxwithdeclListenvType:int,Kind:var
Passesbothchecks!
Spring2017
Page 34
4/8/17
34
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ObservaPons• TheseareequaPonal(funcPonal)computaPons• Thiscanbeautomated(ifequaPonsarenon-circular)• ButimplementaPonproblems
– Non-localcomputaPon:AyributeequaPonscanonlyrefertovaluesassociatedwithsymbolsthatappearinasingleproducPonrule.
• Ifyouneednon-localvalues,youneedtoaddspecialrulestothegrammartocopythemaround.Canmakegrammarverylarge.
– Can’taffordtoliterallypassaroundcopiesoflarge,aggregatestructureslikeenvironments.
– UseofproducPonrulesbindsayributestotheparsetreeratherthanthe(typicallysmaller,andmoreuseful)AST.Canworkaroundthis(use“ASTgrammar”),butresultsinmorecomplexayributerules.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 66
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
InPracPce
• AyributegrammarsgiveusagoodwayofthinkingabouthowtostructuresemanPcchecks
• SymboltableswillholdenvironmentinformaPon• AddfieldstoASTnodestorefertoappropriateayributes(symboltableentriesforidenPfiers,typesforexpressions,etc.)– PutinappropriateplacesinASTclassinheritancetree–moststatementsdon’tneedtypes,forexample
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 67
Page 35
4/8/17
35
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
SymbolTables• MapidenPfiersto<type,kind,locaPon,otherproperPes>
• OperaPons– Lookup(id)=>informaPon– Enter(id,informaPon)– Open/closescopes
• Build&useduringsemanPcspass– BuildfirstfromdeclaraPons– ThenusetochecksemanPcrules
• Use(andaddto)duringlaterphasesaswell
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 68
CodeGenera.on
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 69
Page 36
4/8/17
36
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
BasicCodeGeneraPonStrategy
• WalktheASTorotherIR,outpu�ngcodeforeachconstructencountered
• Handlingofnode’schildrenisdependentontypeofnode– E.g.,forbinaryoperaPonlike+:
• Generatecodetocomputeoperand1(andstoreresult)• Generatecodetocomputeoperand2(andstoreresult)• Generatecodetoloadoperandresultsandaddthemtogether
• Todayisjustasamplingofbasicconstructs,togivebasicidea
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 70
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ConvenPonsforExamples
• Thefollowingslideswillwalkthroughhowthisisdoneformanycommonlanguageconstructs
• ExamplesshowcodesnippetsinisolaPon• Registereaxusedbelowasagenericexample
– RenameasneededformorecomplexcodeusingmulPpleregisters
• Afewpeepholeop7miza7onsincludedbelowforaflavorofwhat’spossible– LocalizedopPmizaPonsperformedonsmallASMinstrucPonsequences.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 71
Page 37
4/8/17
37
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Variables
• Forourpurposes,assumealldatawillbeineither:– Astackframe(methodlocalvariables)– Anobject(instancevariables)
• Localvariablesaccessedviaebp(stackbasepointer) moveax,[ebp-12]
• Objectinstancevariablesaccessedviaanobjectaddressinaregister
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 72
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CodeGeneraPonforConstants
• Source17
• x86moveax,17
– Idea:realizeconstantvalueinaregister
• OpPmizaPon:ifconstantis0 xoreax,eax
– Maybesmallerandfaster
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 73
Page 38
4/8/17
38
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AssignmentStatement
• Sourcevar=exp;
• x86<codetoevaluateexpinto,say,eax>mov[ebp+offsetvar],eax
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 74
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
UnaryMinus
• Source-exp
• x86<codeevaluaPngexpintoeax>negeax
• OpPmizaPon– Collapse-(-exp)toexp
• Unaryplusisano-op
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 75
Page 39
4/8/17
39
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Binary+
• Sourceexp1+exp2
• x86<codeevaluaPngexp1intoeax><codeevaluaPngexp2intoedx>addeax,edx
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 76
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Binary+
• OpPmizaPons– Ifexp2isasimplevariableorconstant,don’tneedtoloaditintoanotherregisterfirst.Instead:
addeax,immConst;immisconstantaddeax,[ebp+offsetvar];offsetisvariable’sstackoffset
– Changeexp1+(-exp2)intoexp1-exp2– Ifexp2is1
inceax• Somewhatsurprising:whetherthisisbeyerthanaddeax,1dependsonprocessorimplementaPonandhaschangedoverPme
Spring2017 UWCSEP590(PMPProgrammingSystems):
Ringenburg 77
Page 40
4/8/17
40
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ControlFlow
• Basicidea:decomposehigherleveloperaPonintocondiPonalanduncondiPonalgotos
• Inthefollowing,jfalseisusedtomeanjumpwhenacondiPonisfalse– NosuchinstrucPononx86– CanrealizewithappropriatesequenceofinstrucPonstosetcondiPoncodesfollowedbycondiPonaljumps
– Normallydon’tactuallygeneratethevalue“true”or“false”inaregister
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 78
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
While
• Sourcewhile(cond)stmt
• X86test: <codeevaluaPngcond> jfalsedone <codeforstmt> jmptest
done:
– Note:Ingeneratedasmcodewe’llneedtogenerateuniquelabelsforeachloop
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 79
Page 41
4/8/17
41
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
If
• Sourceif(cond)stmt
• x86 <codeevaluaPngcond> jfalseskip <codeforstmt>
skip:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 80
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
BooleanExpressions
• Whatdowedowiththis?x>y
• Itisanexpressionthatevaluatestotrueorfalse– Couldgeneratethevalue(0/1orwhateverthelocalconvenPonis)
– Butnormallywedon’twant/needthevalue;we’reonlytryingtodecidewhethertojump
• OneexcepPon:assignmentexpressions,e.g.,while(my_bool=(x<y)){…}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 81
Page 42
4/8/17
42
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Codeforexp1>exp2
• Generatedcodedependsoncontext– Whatisthejumptarget?– JumpifthecondiPonistrueoriffalse?
• Example:evaluateexp1>exp2,jumponfalse,targetifjumptakenisL123
<evaluateexp1toeax><evaluateexp2toedx>cmpeax,edxjngL123 ;greater-thantest,jumponfalse,sojng ;(jumpnotgreater)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 82
Op.miza.onOverview
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 83
Page 43
4/8/17
43
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
OpPmizaPons• UseaddedpassestoidenPfyinefficienciesinintermediate
ortargetcode• Replacewithequivalent(“hasthesameexternallyvisible
behavior”)butbeyersequences– Beyercanmeanmanythings:faster,smaller,lessmemory,moreenergy-efficient,etc.
• Target-independentopPmizaPonsbestdoneonIRcode– RemovingredundantcomputaPons,deadcode,etc.
• Target-dependentopPmizaPonsbestdoneontargetcode– GeneraPngsequencethataremoreefficientonaparPcularmachine
• “OpPmize”overlyopPmisPc:“usuallyimprove”isgenerallymoreaccurate– And“clever”programmerscanoutwityou!
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 84
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplex = a[i] + b[2]; c[i] = x - 5;
t1 = *(fp + ioffset); // i t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t5 = 2; t6 = t5 * 4; t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t11 = 5; t12 = t10 - t11; t13 = *(fp + ioffset); // i t14 = t13 * 4; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 85
Page 44
4/8/17
44
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t5 = 2; t6 = t5 << 2; t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t11 = 5; t12 = t10 - t11; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
StrengthReducPon:shi]o]encheaperthanmulPply
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 86
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t5 = 2; t6 = 2 << 2; // was t5 << 2 t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t11 = 5; t12 = t10 – 5; // was t10 – t11 t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
ConstantpropagaPon:Replacevariableswithknownconstantvalue.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 87
Page 45
4/8/17
45
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t5 = 2; t6 = 2 << 2; t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t11 = 5; t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
DeadStore(orDeadAssignment)EliminaPon:Removeassignementstoprovablyunusedvariables.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 88
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t6 = 2 << 2; t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
DeadStore(orDeadAssignment)EliminaPon:Removestorestoprovablyunusedvariables.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 89
Page 46
4/8/17
46
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t6 = 8; // was 2 << 2 t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
ConstantFolding:StaPcallycomputeoperaPonswithonlyconstantoperands.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 90
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t6 = 8; t7 = fp + 8; // was fp + t6 t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
ConstantPropagaPon,thenDeadStoreEliminaPon
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 91
Page 47
4/8/17
47
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t7 = fp + 8; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
ConstantPropagaPon,thenDeadStoreEliminaPon
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 92
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t7 = boffset + 8; t8 = *(t7 + fp); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
ApplyingarithmePcidenPPes:Weknow+iscommutaPve&associaPve.boffsetistypicallyaknowncompile-Pmeconstant(say,-30),sothisenables…
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 93
Page 48
4/8/17
48
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t7 = -22; // was boffset(-30) + 8 t8 = *(t7 + fp); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
…moreconstantfolding.Whichinturnenables…
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 94
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t7 = -22; t8 = *(fp - 22); // b[2] (was t7+fp) t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
MoreconstantpropagaPonanddeadstoreeliminaPon.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 95
Page 49
4/8/17
49
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = *(fp + ioffset); // i t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
MoreconstantpropagaPonanddeadstoreeliminaPon.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 96
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = *(fp + xoffset); // x t12 = t10 – 5; t13 = t1; // i (was *(fp+ioffset)) t14 = t13 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
CommonsubexpressioneliminaPon:Noneedtocompute*(fp+ioffset)twiceifweknowitwon’tchange.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 97
Page 50
4/8/17
50
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = t9; // x (was *(fp+xoffset)) t12 = t10 – 5; t13 = t1; // i t14 = t1 << 2; // was t13 << 2 t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
CopypropagaPon:Replaceassignmenttargetswiththeirvalues.E.g.,replacet13witht1.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 98
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = t9; // x t12 = t9 – 5; // Was t10 - 5 t13 = t1; // i t14 = t1 << 2; t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
MorecopypropagaPon
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 99
Page 51
4/8/17
51
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = t9; // x t12 = t9 – 5; t13 = t1; // i t14 = t2; // was t1 << 2 t15 = fp + t14; *(t15 + coffset) = t12; // c[i] := …
CommonsubexpressioneliminaPon.
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 100
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = t9; // x t12 = t9 – 5; t13 = t1; // i t14 = t2; t15 = fp + t2; // was fp + t14 *(t15 + coffset) = t12; // c[i] := …
CopyPropagaPon
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 101
Page 52
4/8/17
52
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t10 = t9; // x t12 = t9 – 5; t13 = t1; // i t14 = t2; t15 = fp + t2; *(t15 + coffset) = t12; // c[i] := …
DeadAssignmentEliminaPon
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 102
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t12 = t9 – 5; t15 = fp + t2; *(t15 + coffset) = t12; // c[i] := …
DeadAssignmentEliminaPon
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 103
Page 53
4/8/17
53
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Anexamplet1 = *(fp + ioffset); // i t2 = t1 << 2; t3 = fp + t2; t4 = *(t3 + aoffset); // a[i] t8 = *(fp - 22); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = … t12 = t9 – 5; t15 = fp + t2; *(t15 + coffset) = t12; // c[i] := …
• Final:3loads(i,a[i],b[2]),2stores(x,c[i]),5register-onlymoves,9+/-,1shi]• Original:5loads,2stores,10register-onlymoves,12+/-,3*
• (OpPmizertypicallydealsin“pseudo-registers”–canhaveasmanyasyouwant–andletsregisterallocatorfigureoutopPmalassignmentsofpseudo-registerstorealregisters.)
x = a[i] + b[2]; c[i] = x - 5;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 104
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
KindsofOpPmizaPons• peephole:lookatadjacentinstrucPons• local:lookatindividualbasicblocks
– Straight-linesequenceofstatements• intraprocedural:lookatwholeprocedure
– Commonlycalled“global”• interprocedural:lookacrossprocedures
– “wholeprogram”analysis– gcc’s“linkPmeopPmizaPon”isaversionofthis
• Largerscope=>usuallybeyeropPmizaPonbutmorecostandcomplexity– Analysisiso]enlessprecisebecauseofmorepossibiliPes
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 105
Page 54
4/8/17
54
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
PeepholeOpPmizaPon
• A]ertargetcodegeneraPon,lookatadjacentinstrucPons(a“peephole”onthecodestream)– trytoreplaceadjacentinstrucPonswithsomethingfaster,e.g.,storeandloadwithstoreandregistermove:
– JumpchainingcanalsobeconsideredaformofpeepholeopPmizaPon(removingjump-to-jump)
movq %r9,12(%rsp) movq 12(%rsp),%r12
movq %r9,12(%rsp) movq %r9,%r12
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 106
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AlgebraicSimplificaPon• “constantfolding”:pre-calculateoperaPononconstant• “strengthreducPon”:replaceoperaPonwithacheaperoperaPon• “simplificaPon”:applyingalgebraicidenPPes
– z = 3 + 4; à z = 7; – z = x + 0; à z = x; – z = x * 1; à z = x; – z = x * 2; à z = x << 1; or z = x + x; – z = x * 8; à z = x << 3; – z = x / 8; à z = x >> 3; – z = (x + y) - y; à z = x;
• Canbedoneatmanylevels,frompeepholeonup.• Whydotheseexampleshappen?
– O]encreated:Conversiontolower-levelIR,OtheropPmizaPons,CodegeneraPon
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 107
Page 55
4/8/17
55
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Higher-levelExample:Loop-basedStrengthReducPon
• SomePmesmulPplicaPonbytheloopvariableinaloopcanbereplacedbyaddiPonsintoatemporaryaccumulator
• Similarly,exponenPaPoncanbereplacedbymulPplicaPon.
for(inti=0;i<size;i++){foo[i]=i;}
for(inti=0;i<size;i++){*(foo+i*elementSize)=i;}
t1=0;for(inti=0;i<size;i++){*(foo+t1)=i;t1=t1+8;}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 108
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalOpPmizaPons
• AnalysisandopPmizaPonswithinabasicblock• Basicblock:straight-linesequenceofstatements– nocontrolflowintooroutofmiddleofsequence
• NottoohardtoimplementwithareasonableIR
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 109
Page 56
4/8/17
56
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalConstantPropagaPon
• Ifvariableassignedaconstant,replacedownstreamusesofthevariablewithconstant(unPlvariableisnextassigned)
• Canenablemoreconstantfolding– Code;unopPmizedintermediatecode:
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = count; t2 = 5; t3 = t1 * t2; x = t3; t4 = x; t5 = 3; t6 = exp(t4, t5); y = t6;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 110
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalConstantPropagaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; // CP count t2 = 5; t3 = 10 * 5; // CP t1 x = t3; t4 = x; t5 = 3; t6 = exp(t4, 3); // CP t5 y = t6;
• Ifvariableassignedaconstant,replacedownstreamusesofthevariablewithconstant(unPlvariableisnextassigned)
• Canenablemoreconstantfolding– Code;propagatedintermediatecode:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 111
Page 57
4/8/17
57
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalConstantPropagaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; t2 = 5; t3 = 50; // CF 5 * 10 x = t3; t4 = x; t5 = 3; t6 = exp(t4, 3); y = t6;
• Ifvariableassignedaconstant,replacedownstreamusesofthevariablewithconstant(unPlvariableisnextassigned)
• Canenablemoreconstantfolding– Code;foldedintermediatecode:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 112
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalConstantPropagaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; t2 = 5; t3 = 50; x = 50; // CP t3 t4 = 50; // CP x t5 = 3; t6 = exp(50, 3); // CP t4 y = t6;
• Ifvariableassignedaconstant,replacedownstreamusesofthevariablewithconstant(unPlvariableisnextassigned)
• Canenablemoreconstantfolding– Code;repropagatedintermediatecode:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 113
Page 58
4/8/17
58
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalConstantPropagaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; t2 = 5; t3 = 50; x = 50; t4 = 50; t5 = 3; t6 = 125000; // CF 50^3 y = t6;
• Ifvariableassignedaconstant,replacedownstreamusesofthevariablewithconstant(unPlvariableisnextassigned)
• Canenablemoreconstantfolding– Code;refoldedintermediatecode:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 114
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalConstantPropagaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; t2 = 5; t3 = 50; x = 50; t4 = 50; t5 = 3; t6 = 125000; y = 125000; // CP t6
• Ifvariableassignedaconstant,replacedownstreamusesofthevariablewithconstant(unPlvariableisnextassigned)
• Canenablemoreconstantfolding– Code;repropagatedintermediatecode:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 115
Page 59
4/8/17
59
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalDeadAssignmentEliminaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; t2 = 5; t3 = 50; x = 50; t4 = 50; t5 = 3; t6 = 125000; y = 125000; // CP t6
• Ifle]sideofassignmentneverreferencedagainbeforebeingoverwriyen,thencandeleteassignment– Whywouldthishappen?– Clean-upa]erpreviousopPmizaPons,o]en
• Intermediatecodea]erconstantpropagaPon:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 116
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalDeadAssignmentEliminaPon
count = 10; ... // No count assigns x = count * 5; y = x ^ 3;
count = 10 t1 = 10; t2 = 5; t3 = 50; x = 50; t4 = 50; t5 = 3; t6 = 125000; y = 125000; // CP t6
• Ifle]sideofassignmentneverreferencedagainbeforebeingoverwriyen,thencandeleteassignment– Whywouldthishappen?– Clean-upa]erpreviousopPmizaPons,o]en
• Intermediatecodea]erconstantpropagaPon:
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 117
Page 60
4/8/17
60
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalCommonSubexpressionEliminaPon
• LooksforrepePPonsofthesamecomputaPon,andeliminatethemiftheresultwon’thavechanged(andnosideeffects)– AvoidsrepeaPngthesamecalculaPon– Eliminatesredundantloads
• Idea:walkbasicblock,keepingtrackofavailableexpressions ... a[i] + b[i] ...
t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = *(fp + ioffset); t6 = t5 * 4; t7 = fp + t6; t8 = *(t7 + boffset); t9 = t4 + t8;
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 118
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalCommonSubexpressionEliminaPon
... a[i] + b[i] ...
t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = t1; // CSE t6 = t5 * 4; t7 = fp + t6; t8 = *(t7 + boffset); t9 = t4 + t8;
• LooksforrepiPPonsofthesamecomputaPon,andeliminatethemiftheresultwon’thavechanged(andnosideeffects)– AvoidsrepeaPngthesamecalculaPon– Eliminatesredundantloads
• Idea:walkbasicblock,keepingtrackofavailableexpressions
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 119
Page 61
4/8/17
61
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalCommonSubexpressionEliminaPon
... a[i] + b[i] ...
t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = t1; t6 = t1 * 4; // CP t7 = fp + t6; t8 = *(t7 + boffset); t9 = t4 + t8;
• LooksforrepiPPonsofthesamecomputaPon,andeliminatethemiftheresultwon’thavechanged(andnosideeffects)– AvoidsrepeaPngthesamecalculaPon– Eliminatesredundantloads
• Idea:walkbasicblock,keepingtrackofavailableexpressions
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 120
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalCommonSubexpressionEliminaPon
... a[i] + b[i] ...
t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = t1; t6 = t2; // CSE t7 = fp + t2; // CP t8 = *(t7 + boffset); t9 = t4 + t8;
• LooksforrepiPPonsofthesamecomputaPon,andeliminatethemiftheresultwon’thavechanged(andnosideeffects)– AvoidsrepeaPngthesamecalculaPon– Eliminatesredundantloads
• Idea:walkbasicblock,keepingtrackofavailableexpressions
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 121
Page 62
4/8/17
62
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalCommonSubexpressionEliminaPon
... a[i] + b[i] ...
t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = t1; t6 = t2; t7 = t3; // CSE t8 = *(t3 + boffset);//CP t9 = t4 + t8;
• LooksforrepiPPonsofthesamecomputaPon,andeliminatethemiftheresultwon’thavechanged(andnosideeffects)– AvoidsrepeaPngthesamecalculaPon– Eliminatesredundantloads
• Idea:walkbasicblock,keepingtrackofavailableexpressions
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 122
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LocalCommonSubexpressionEliminaPon
... a[i] + b[i] ...
t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = t1; // DAE t6 = t2; // DAE t7 = t3; // DAE t8 = *(t3 + boffset); t9 = t4 + t8;
• LooksforrepiPPonsofthesamecomputaPon,andeliminatethemiftheresultwon’thavechanged(andnosideeffects)– AvoidsrepeaPngthesamecalculaPon– Eliminatesredundantloads
• Idea:walkbasicblock,keepingtrackofavailableexpressions
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 123
Page 63
4/8/17
63
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
IntraproceduralopPmizaPons
• Enlargescopeofanalysistowholeprocedure– moreopportuniPesforopPmizaPon– havetodealwithbranches,merges,andloops
• CandoconstantpropagaPon,commonsubexpressioneliminaPon,etc.atfuncPon-widelevel
• Candonewthings,e.g.loopopPmizaPons• OpPmizingcompilersusuallyworkatthislevel(-O2)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 124
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CodeMoPon• Goal:moveloop-invariantcalculaPonsoutofloops• Candoatsourceleveloratintermediatecodelevel
for (i = 0; i < 10; i = i+1) { a[i] = a[i] + b[j]; z = z + (foo*bar)^2; }
t1 = b[j]; t2 = (foo*bar)^2; for (i = 0; i < 10; i = i+1) { a[i] = a[i] + t1; z = z + t2; }
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 125
Page 64
4/8/17
64
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
InterproceduralOpPmizaPon
• Expandscopeofanalysistoprocedurescallingeachother
• Candolocal&intraproceduralopPmizaPonsatlargerscope
• CandonewopPmizaPons,e.g.inlining
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 126
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Inlining:replacecallwithbody
• Replaceprocedurecallwithbodyofcalledprocedure,andsubsPtuPngactualargumentsforformalparameters
• Source:final double pi = 3.1415927; double circle_area(double radius) { return pi * (radius * radius); } ... double r = 5.0; ... double a = circle_area(r);
• A]erinlining: double r = 5.0; ... double a = pi * r * r;
• (Thenwhat?ConstantpropagaPon/folding.)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 127
Page 65
4/8/17
65
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
DataStructuresforOpPmizaPons
• Needtorepresentcontrolanddataflow• Controlflowgraph(CFG)capturesflowofcontrol
– nodesarebasicblocks– edgesrepresent(allpossible)controlflow– nodewithmulPplesuccessors=branch/switch– nodewithmulPplepredecessors=mergeorjoinpoint– loopingraph=loop
• Dataflowgraph(DFG)captureflowofdata,e.g.def/usechains:– nodesaredef(iniPon)sandusesofdata/variables– edgesfromdefstousesof(potenPally)thesamedata– adefcanreachmulPpleuses– ausecanhavemulPplereachingdefs(differentcontrolflow,possiblealiasing,etc.)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 128
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AnalysisandTransformaPon
• EachopPmizaPonismadeupof– somenumberofanalyses– followedbyatransformaPon
• AnalyzeCFGand/orDFGbypropagaPnginfoforwardorbackwardalongCFGand/orDFGedges– mergesingraphrequirecombininginfo– loopsingraphrequire(conservaPve)itera7veapproxima7on
• Perform(improving)transformaPonsbasedoninfocomputed• AnalysismustbeconservaPve/safe/soundsothat
transformaPonspreserveprogrambehavior
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 129
Page 66
4/8/17
66
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:ConstantPropagaPon,Folding
• CanuseeithertheCFGortheDFG• CFGanalysisinfo:tablemappingeachvariableinscopetooneof:
– aparPcularconstant– NonConstant– Undefined
• TransformaPonateachinstrucPon:– Ifencounteranassignmentofaconstanttoavariable,setvariableas
constant– ifreferenceavariablethatthetablemapstoaconstant,thenreplace
withthatconstant(constantpropagaPon)– ifr.h.s.expressioninvolvesonlyconstants,andhasnoside-effects,then
performoperaPonatcompile-Pmeandreplacer.h.s.withconstantresult(constantfolding)
• Forbestanalysis,doconstantfoldingaspartofanalysis,tolearnallconstantsinonepass
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 130
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Mergingdataflowanalysisinfo
• Constraint:mergeresultsmustbesound– ifsomethingisbelievedtruea]erthemerge,thenitmustbetruenomayerwhichpathwetookintothemerge
– onlythingstruealongallpredecessorsaretruea]erthemerge
• TomergetwomapsofconstantinformaPon,buildmapbymergingcorrespondingvariableinformaPon
• TomergeinformaPonabouttwovariable– ifoneisUndefined,keeptheother(uniniPalizedvariablesinmanylanguagesallowedtohaveanyvalue)
– ifbotharethesameconstant,keepthatconstant– otherwise,degeneratetoNonConstant
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 131
Page 67
4/8/17
67
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleMerges
A {x:5}
D {x:6}
// Block Aint x;x = 5;if (foo) { // Block B x++;} else { // Block C x=6;}// Block D…
C {x:6} B {x:6}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 132
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleMerges
A {x:Undefined}
D {x:5}
// Block Aint x;if (foo) { // Block B z++; x = 5;} else { // Block C z--; x = 5;}// Block D…
C {x:5} B {x:5}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 133
Page 68
4/8/17
68
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleMerges
A {x:Undefined}
D {x:NonConstant}
// Block Aint x;if (foo) { // Block B z++; x = 5;} else { // Block C z--; x = 4;}// Block D…
C {x:4} B {x:5}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 134
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
ExampleMerges
A {x:Undefined}
D {x:4}
// Block Aint x;if (foo) { // Block B z++;} else { // Block C z--; x = 4;}// Block D…
C {x:4} B {x:Undefined}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 135
Page 69
4/8/17
69
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Howtoanalyzeloopsi = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
• Safebutimprecise:forgeteverythingwhenweenterorexitaloop
• Precisebutunsafe:keepeverythingwhenweenterorexitaloop
• Canwedobeyer?
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 136
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LoopTerminology
preheader
entry edge
head
back edge
tail
loop
exit edge
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 137
Page 70
4/8/17
70
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
OpPmisPcIteraPveAnalysis
• AssuminginformaPonatloopheadissameasinformaPonatloopentry
• Thenanalyzeloopbody,compuPnginformaPonatbackedge
• MergeinformaPonatloopbackedgeandloopentry• TestifmergedinformaPonissameasoriginalassumpPon– Ifso,thenwe’redone– Ifnot,thenreplacepreviousassumpPonwithmergedinformaPon,
– andgobacktoanalysisofloopbody
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 138
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examplei = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 139
Page 71
4/8/17
71
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examplei = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
i=0,x=10,y=20
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 140
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examplei = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
i=0,x=10,y=20
i=1,x=10,y=30
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 141
Page 72
4/8/17
72
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examplei = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
i=NC,x=10,y=NC
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 142
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examplei = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
i=NC,x=10,y=NC
i=NC,x=10,y=NC
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 143
Page 73
4/8/17
73
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Examplei = 0; x = 10; y = 20; while (...) { // what’s true here? ... i = i + 1; y = 30; } // what’s true here? ... x ... i ... y ...
i=NC,x=10,y=NC
i=NC,x=10,y=NC
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 144
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Whydoesthiswork?
• WhyaretheresultsalwaysconservaPve?• Becauseifthealgorithmstops,then
– theloopheadinfoisatleastasconservaPveasboththeloopentryinfoandtheloopbackedgeinfo
– theanalysiswithintheloopbodyisconservaPve,giventheassumpPonthattheloopheadinfoisconservaPve
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 145
Page 74
4/8/17
74
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
OpPmizaPonSummary
• OpPmizaPonsorganizedascollecPonsofpasses,eachrewriPngILinplaceinto(hopefully)beyerversion
• Eachpassdoesanalysistodeterminewhatispossible,followedby(orconcurrentwith)transformaPonsthat(hopefully)improvetheprogram– SomePmeshave“analysis-only”passes–produceinfousedbylaterpasses
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 146
DataflowAnalysis(ifwehaveextra.meandenergy!)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 147
Page 75
4/8/17
75
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Nexttopic:DataflowAnalysis
• Aframeworkandalgorithmformanycommoncompileranalyses
• IniPalexample:dataflowanalysisforcommonsubexpressioneliminaPon
• Otheranalysisproblemsthatworkinthesameframework
• We’llbediscussingsomeofthesameopPmizaPonswesawintheopPmizaPonoverview,butwithmoreformalismanddetails.
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 148
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
MoPvaPngExample:CommonSubexpressionEliminaPon(CSE)
m = a + b n = a + b
A
p = c + d r = c + d
B q = a + b r = c + d
C
e = b + 18 s = a + b u = e + f
D e = a + 17 t = c + d u = e + f
E
v = a + b w = c + d x = e + f
F
y = a + b z = c + d
G
• Goal:Findcommonsubexpressions,replacewithtemporaries
• Idea:calculateavailableexpressionsatbeginningofeachbasicblock
• Avoidre-evaluaPonofanavailableexpression–copyatempinstead– Simpleinsideasingleblock;
morecomplexdataflowanalysisusedacrossbocks
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 149
Page 76
4/8/17
76
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
“Available”andOtherTerms
• AnexpressioneisdefinedatpointpintheCFG(controlflowgraph)ifitsvalueiscomputedatp– SomePmescalleddefini7onsite
• Anexpressioneiskilledatpointpifoneofitsoperands(components)isredefinedatp– SomePmescalledkillsite
• AnexpressioneisavailableatpointpifeverypathleadingtopcontainsapriordefiniPonofeandeisnotkilledbetweenthatdefiniPonandp
t1=a+b…
t10=a+b…
b=7…
a+bdefined
a+bavailable
a+bkilled
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 150
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
AvailableExpressionSets
• Tocomputeavailableexpressions,foreachblockb,define– AVAIL(b)–thesetofexpressionsavailableonentrytob
– NKILL(b)–thesetofexpressionsnotkilledinb– DEF(b)–thesetofexpressionsdefinedinbandnotsubsequentlykilledinb
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 151
Page 77
4/8/17
77
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompuPngAvailableExpressions
• AVAIL(b)(expressionsavailableonentrytob)istheset
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
– preds(b)isthesetofb’sdirectpredecessorsintheCFG– In“english”,theexpressionsavailableonentrytobaretheexpressionsthatwereavailableattheendofeverydirectlyprecedingbasicblockx.(Thisisthe∩x∈preds(b))
– Theexpressionsavailableattheendofblockxareexactlythosethatweredefinedinx(andnotkilled),andthosethatwereavailableatthebeginningofxandnotkilledinx.
• ApplyingtoeveryblockgivesasystemofsimultaneousequaPons–adataflowproblem
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 152
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompuPngAvailableExpressions
• BigPicture– Buildcontrol-flowgraph– CalculateiniPallocaldata–DEF(b)andNKILL(b)foreveryblockb
• Thisonlyneedstobedoneonce– IteraPvelycalculateAVAIL(b)byrepeatedlyevaluaPngequaPonsunPlnothingchanges
• Afixed-pointalgorithm
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 153
Page 78
4/8/17
78
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompuPngDEFandNKILL(1)
• ForeachblockbwithoperaPonso1,o2,…,ok
KILLED=∅//Killedvariables(notexpressions)DEF(b)=∅fori=kto1//Noteweareworkingbackwards-importantassumeoiis“x=y+z” if(y∉KILLEDandz∉KILLED) //ExpressioninDEFonlyif add“y+z”toDEF(b) //theyaren’tlaterkilledaddxtoKILLED
…
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 154
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandKILL
x=a+b;b=c+d;m=5*n;
DEF={}KILL={}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 155
Page 79
4/8/17
79
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n}KILL={m}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 156
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n,c+d}KILL={m,b}
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 157
Page 80
4/8/17
80
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n,c+d}KILL={m,b,x}(biskilled,sodon’tadda+btoDEF)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 158
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompuPngDEFandNKILL(2)
• A]ercompuPngDEFandKILLforablockb,conceptuallywedothefollowing:
//NKILLisexpressionsnotkilled.NKILL(b)={allexpressionsinprogram}foreachexpressione//Removeanykilledforeachvariablev∈eifv∈KILLthen NKILL(b)=NKILL(b)-e
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 159
Page 81
4/8/17
81
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n,c+d}KILL={m,b,x}NKILL=allexpressionsthatdon’tusem,b,orx
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 160
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CompuPngAvailableExpressions
• OnceDEF(b)andNKILL(b)arecomputedforallblocksb,computeAVAILforallblocksbyrepeatedlyapplyingthepreviousformulainafixed-pointalgorithm:
Worklist={allblocksbi}while(Worklist≠∅)removeablockbfromWorklist//IfbinWorklist,atleast1predecessorchangedletAVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))ifAVAIL(b)changedWorklist=Worklist∪successors(b)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 161
Page 82
4/8/17
82
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a DEF={2*a}NKILL=exprsw/oh=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 162
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
AVAIL={}DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a DEF={2*a}NKILL=exprsw/oh=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 163
Page 83
4/8/17
83
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
AVAIL={}DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a AVAIL={5*n}DEF={2*a}NKILL=exprsw/oh
=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 164
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
AVAIL={2*a,2*b}DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
AVAIL={}DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a AVAIL={5*n}DEF={2*a}NKILL=exprsw/oh
=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 165
Page 84
4/8/17
84
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
AVAIL={2*a,2*b}DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n AVAIL={2*a,2*b}DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
AVAIL={}DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a AVAIL={5*n}DEF={2*a}NKILL=exprsw/oh
=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 166
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
AVAIL={2*a,2*b}DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n AVAIL={2*a,2*b}DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
AVAIL={}DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a AVAIL={5*n,2*a}DEF={2*a}NKILL=exprsw/oh
=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 167
Page 85
4/8/17
85
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:CompuPngDEFandNKILL
x=a+b;b=c+d;m=5*n;
AVAIL={2*a,2*b}DEF={5*n,c+d}NKILL=exprsw/om,b,orx
c=5*n AVAIL={2*a,2*b}DEF={5*n}NKILL=exprsw/oc
j=2*ak=2*b
AVAIL={}DEF={2*a,2*b}NKILL=exprsw/ojork
h=2*a AVAIL={5*n,2*a}DEF={2*a}NKILL=exprsw/oh
=inWorklist
=Processing
AVAIL(b)=∩x∈preds(b)(DEF(x)∪(AVAIL(x)∩NKILL(x)))
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 168
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Dataflowanalysis
• Availableexpressionsareanexampleofadataflowanalysisproblem
• Manyothercompileranalysescanbeexpressedinasimilarframework
• Onlythefirstpartofthestory–oncewe’vediscoveredfacts,wethenneedtousethemtoimprovecode
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 169
Page 86
4/8/17
86
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CharacterizingDataflowAnalysis
• Allofthesealgorithmsinvolvesetsoffactsabouteachbasicblockb– IN(b)–factstrueonentrytob– OUT(b)–factstrueonexitfromb– GEN(b)–factscreatedandnotkilledinb– KILL(b)–factskilledinb
• ThesearerelatedbytheequaPon OUT(b)=GEN(b)∪(IN(b)–KILL(b))
– (SubtracPngKILL(b)isequivalenttointersecPngNKILL(b))– SolvethisiteraPvelyforallblocks– SomePmesinformaPonpropagatesforward;somePmesbackward(reverseinandout)
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 170
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
Example:LiveVariableAnalysis
• Avariablevisliveatpointpifandonlyifthereisanypathfromptoauseofvalongwhichvisnotredefined(i.e.,vmightbeusedbeforeitisredefined)
• Someuses:– RegisterallocaPon–registersallocatedtoliveranges– EliminaPnguselessstores–ifvariableisnotliveatstore,thestoredvaluewillneverbeused
– DetecPngusesofuniniPalizedvariables–ifliveatdeclaraPon(beforeiniPalizaPon),maybeuseduniniPalized.
– ImproveSSAconstrucPon–onlycreatephifuncPons(variablemerges)forlivevariables-cominglater…
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 171
Page 87
4/8/17
87
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
LivenessAnalysisSets
• Foreachblockb,define– use[b]=variableusedinbbeforeanydef– def[b]=variabledefinedinbbeforeanyuse– in[b]=variablesliveonentrytob– out[b]=variablesliveonexitfromb
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 172
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
CSE341: Programming Languages
Lecture 1 Course Mechanics
ML Variable Bindings
Dan Grossman Fall 2011
EquaPonsforLiveVariables
• GiventheprecedingdefiniPons,wehavein[b]=use[b]∪(out[b]–def[b])out[b]=∪s∈succ[b]in[s]
• I.e.,liveatentryiffthisblocksgeneratesliveness(use[b])oritwasliveattheexitandthisblockdoesnotkillliveness(out[b]–def[b]).
• Andliveatexitiffliveatentrytoanysuccessor.• Algorithm
– Setin[b]=out[b]=∅– Computeuse[b]anddef[b]foreveryblock(onePme)– Updatein,outunPlnochange
Spring2017 UWCSEP590(PMPProgrammingSystems):Ringenburg 173