263-2810: advanced compiler design 3.0 ssa form for ... · 3.1 graphs §control flow graph: central...
TRANSCRIPT
263-2810:AdvancedCompilerDesign
3.0SSAformforarbitrarycontrolflowgraphs
RudiEigenmannThomasR.Gross
ComputerScienceDepartmentETHZurich,Switzerland
SSAformat
§ f functionsallowsustodealwithabasicblockthathasmultiplepredecessors
§ ToturnaprogramintoSSAformat§ Insertf functions§ Determineversiontobeusedasoperand§ Producenewversionswhenthereisanassignment
§ Oraf functiondeliversthecorrectversion
§ Sofar:syntacticapproach§ Worksforwell-structuredprograms§ Next:anapproachforgeneralcontrolflowgraphs§ Butfirst:reviewofconceptsforgraphs
2
Outline
§ 3.1Graphs
§ 3.2Approachestoinsertionoff functions
§ 3.3Dominancefrontier
§ 3.4Algorithmforinsertionoff functions
§ 3.5Algorithmforvariablerenaming
§ 3.6Example
3
3.1Graphs
§ Controlflowgraph:centraldatastructure§ Nodes:basicblocks(orsequencesofstraight-lineprogramsegments)§ Edges:directed,possiblecontrolflow
§ Specialnodes§ ENTRY:specialwithoutpredecessor§ EXIT:specialwithoutsuccessor
§ EdgefromENTRYtoEXIT
4
Dominance
§ GivenaCFG.AnodeA(orbasicblockA)dominates nodeB(basicblockB)ifAisoneverypathfromENTRYtoB.
Adom B
Adom A
X¬dom Y:XdoesnotdominateY
5
7
9
Immediatedominator
§ Aidom B§ Adom B§ ∄ XsuchthatAdom XandXdom B(X≠A,X≠B)
11
14
Dominatortree
§ Thedominatortree(DT)capturestheimmediatedominatorrelationship§ Xidom Y:edgeX➞ YinDT
§ Thereisexactlyoneimmediatedominator§ That’swhyit’satree…§ LastdominatoronanypathfromENTRY
15
Example2(Dominatortree)
17
ENTRY
A
B
C D
EXIT
Terminology
§ Controlflowgraph§ predecessor§ successor§ direct(predecessor/successor):Thereisanedge--- indicatespossible
controltransfer
§ Example§ BdirectsuccessorofA§ CdirectsuccessorofB§ AdirectpredecessorofB§ DsuccessorofA§ ApredecessorofC§ CpredecessorofB
20
Terminology2
§ Dominatortree§ child§ parent§ ancestor:parent,grandparent,great-grandparent,…§ descendant:child,grandchild,great-grandchild,…
§ Example§ AparentofB§ BchildofA§ CchildofB
§ Nostatementaboutactualcontrolflow§ ENTRY→A→B→D→EXIT 23
Dominatorrelationship
§ dom reflexive§ Xdom X
§ Sometimeswewantanirreflexive (anti-reflexive)relationship§ WanttobesurethatAdom BimpliesA≠B
§ XstrictlydominatesY:Xdom YandX≠Y
X⨠Y
§ Distinguishfrom(weak)domination:Xdom Y
X⨠Y
24
27
3.2Insertionoff functions
§ Wherecouldweinsertf functions?
§ Whereshouldweinsertf functions?
29
Examplegraph
30
ENTRY
p =
EXIT
p =
= p BB6
§ Programnotwell-structured
§ Insertf functions– OptionA:§ Insertaf functionintotheblockthatusesavariable§ Example:p isusedinbasicblockBB6
31
33
§ Insertf functions– OptionB:§ Insertaf functionasearlyaspossible§ Evenifthereisnouseofavariableinbasicblock§ Example:functionsinBB3,BB5,andBB6
34
36
§ OptionB:Numberofargumentstof functiondependsonthenumberofdirectpredecessorsinCFG§ Fixedforagivenprogramminglanguageresp.implementation§ case/switchstatementmayresultinunlimitednumberof
predecessorsunlesscompiledintocascadingsetofif-statements
§ OptionA:Numberofargumentstof functiondependsonnumberofpathsthatreachagivenpoint§ Unbounded
§ OptionBpreferredbycompilerdesigners
37
Setup
§ Letusassumethereisaninitial(dedicated)assignmenttoeachvariable§ Forp,q,r,…wehavep0,q0,r0,…§ “Placed”intotheENTRYnode§ “pseudo-assignment”
§ Benefits§ Thereisatleastoneassignmentoneverypath§ IftheRHSofanassignmentorexpressionreadsv0 thereispossiblya
useofanuninitializedvariable
38
CFGnodeswithf functions
§ ConsidertwonodesA,B
§ Adefinesp,Busesp
40
p =
B
A
§ No functionisneededalongpathfromAtoBifA⨠B
= p
§ ButA=Bispossible.Needtodealwith
§ Extenddefinitionofdominancefrombasicblocks(nodesofCFG)tooperationsinbasicblock§ definitions§ uses
42
= p
p =
p =
= p
§ Definition(def)di inblockA
§ Use(use)uj inblockB
di⨠ujiff di isinblockA,uj isinblockB,anddi isthelastdefinitionalonganypathfromENTRYtouj
§ Corrollary:A⨠B
§ Candefinedi ⨠ uj aswell
43