263-2810: advanced compiler design 3.0 ssa form for ... · 3.1 graphs §control flow graph: central...

26
263-2810: Advanced Compiler Design 3.0 SSA form for arbitrary control flow graphs Rudi Eigenmann Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Upload: others

Post on 23-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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

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)

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

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)

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

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)

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)

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

§ Definition(def)di inblockA

§ Use(use)uj inblockB

di⨠ujiff di isinblockA,uj isinblockB,anddi isthelastdefinitionalonganypathfromENTRYtouj

§ Corrollary:A⨠B

§ Candefinedi ⨠ uj aswell

43