![Page 1: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/1.jpg)
263-2810:AdvancedCompilerDesign
3.0SSAformforarbitrarycontrolflowgraphs
RudiEigenmannThomasR.Gross
ComputerScienceDepartmentETHZurich,Switzerland
![Page 2: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/2.jpg)
SSAformat
§ f functionsallowsustodealwithabasicblockthathasmultiplepredecessors
§ ToturnaprogramintoSSAformat§ Insertf functions§ Determineversiontobeusedasoperand§ Producenewversionswhenthereisanassignment
§ Oraf functiondeliversthecorrectversion
§ Sofar:syntacticapproach§ Worksforwell-structuredprograms§ Next:anapproachforgeneralcontrolflowgraphs§ Butfirst:reviewofconceptsforgraphs
2
![Page 3: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/3.jpg)
Outline
§ 3.1Graphs
§ 3.2Approachestoinsertionoff functions
§ 3.3Dominancefrontier
§ 3.4Algorithmforinsertionoff functions
§ 3.5Algorithmforvariablerenaming
§ 3.6Example
3
![Page 4: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/4.jpg)
3.1Graphs
§ Controlflowgraph:centraldatastructure§ Nodes:basicblocks(orsequencesofstraight-lineprogramsegments)§ Edges:directed,possiblecontrolflow
§ Specialnodes§ ENTRY:specialwithoutpredecessor§ EXIT:specialwithoutsuccessor
§ EdgefromENTRYtoEXIT
4
![Page 5: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/5.jpg)
Dominance
§ GivenaCFG.AnodeA(orbasicblockA)dominates nodeB(basicblockB)ifAisoneverypathfromENTRYtoB.
Adom B
Adom A
X¬dom Y:XdoesnotdominateY
5
![Page 6: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/6.jpg)
7
![Page 7: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/7.jpg)
9
![Page 8: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/8.jpg)
Immediatedominator
§ Aidom B§ Adom B§ ∄ XsuchthatAdom XandXdom B(X≠A,X≠B)
11
![Page 9: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/9.jpg)
14
![Page 10: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/10.jpg)
Dominatortree
§ Thedominatortree(DT)capturestheimmediatedominatorrelationship§ Xidom Y:edgeX➞ YinDT
§ Thereisexactlyoneimmediatedominator§ That’swhyit’satree…§ LastdominatoronanypathfromENTRY
15
![Page 11: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/11.jpg)
Example2(Dominatortree)
17
ENTRY
A
B
C D
EXIT
![Page 12: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/12.jpg)
Terminology
§ Controlflowgraph§ predecessor§ successor§ direct(predecessor/successor):Thereisanedge--- indicatespossible
controltransfer
§ Example§ BdirectsuccessorofA§ CdirectsuccessorofB§ AdirectpredecessorofB§ DsuccessorofA§ ApredecessorofC§ CpredecessorofB
20
![Page 13: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/13.jpg)
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
![Page 14: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/14.jpg)
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
![Page 15: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/15.jpg)
27
![Page 16: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/16.jpg)
3.2Insertionoff functions
§ Wherecouldweinsertf functions?
§ Whereshouldweinsertf functions?
29
![Page 17: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/17.jpg)
Examplegraph
30
ENTRY
p =
EXIT
p =
= p BB6
![Page 18: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/18.jpg)
§ Programnotwell-structured
§ Insertf functions– OptionA:§ Insertaf functionintotheblockthatusesavariable§ Example:p isusedinbasicblockBB6
31
![Page 19: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/19.jpg)
33
![Page 20: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/20.jpg)
§ Insertf functions– OptionB:§ Insertaf functionasearlyaspossible§ Evenifthereisnouseofavariableinbasicblock§ Example:functionsinBB3,BB5,andBB6
34
![Page 21: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/21.jpg)
36
![Page 22: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/22.jpg)
§ OptionB:Numberofargumentstof functiondependsonthenumberofdirectpredecessorsinCFG§ Fixedforagivenprogramminglanguageresp.implementation§ case/switchstatementmayresultinunlimitednumberof
predecessorsunlesscompiledintocascadingsetofif-statements
§ OptionA:Numberofargumentstof functiondependsonnumberofpathsthatreachagivenpoint§ Unbounded
§ OptionBpreferredbycompilerdesigners
37
![Page 23: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/23.jpg)
Setup
§ Letusassumethereisaninitial(dedicated)assignmenttoeachvariable§ Forp,q,r,…wehavep0,q0,r0,…§ “Placed”intotheENTRYnode§ “pseudo-assignment”
§ Benefits§ Thereisatleastoneassignmentoneverypath§ IftheRHSofanassignmentorexpressionreadsv0 thereispossiblya
useofanuninitializedvariable
38
![Page 24: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/24.jpg)
CFGnodeswithf functions
§ ConsidertwonodesA,B
§ Adefinesp,Busesp
40
p =
B
A
§ No functionisneededalongpathfromAtoBifA⨠B
= p
![Page 25: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/25.jpg)
§ ButA=Bispossible.Needtodealwith
§ Extenddefinitionofdominancefrombasicblocks(nodesofCFG)tooperationsinbasicblock§ definitions§ uses
42
= p
p =
p =
= p
![Page 26: 263-2810: Advanced Compiler Design 3.0 SSA form for ... · 3.1 Graphs §Control flow graph: central data structure §Nodes: basic blocks (or sequences of straight-line program segments)](https://reader034.vdocuments.site/reader034/viewer/2022050222/5f67b1358d4a6b3ce13c35f6/html5/thumbnails/26.jpg)
§ Definition(def)di inblockA
§ Use(use)uj inblockB
di⨠ujiff di isinblockA,uj isinblockB,anddi isthelastdefinitionalonganypathfromENTRYtouj
§ Corrollary:A⨠B
§ Candefinedi ⨠ uj aswell
43