data flow analysis - columbia university
TRANSCRIPT
Data Flow Analysis SumanJana
AdoptedFromUPennCIS570:ModernProgrammingLanguageImplementa=on(Autumn2006)
Data flow analysis
• Derivesinforma=onaboutthedynamicbehaviorofaprogrambyonlyexaminingthesta*ccode• Intraproceduralanalysis• Flow-sensi=ve:sensi=vetothecontrolflowinafunc=on
• Examples– Livevariableanalysis– Constantpropaga=on– Commonsubexpressionelimina=on– Deadcodedetec=on
1 a := 0 2 L1: b := a + 1
3 c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c
• Howmanyregistersdoweneed?• Easybound:#ofusedvariables(3)• NeedbeTeranswer
Data flow analysis
• Sta=cally:finiteprogram• Dynamically:canhaveinfinitelymanypaths• Dataflowanalysisabstrac=on
• Foreachpointintheprogram,combinesinforma=onofallinstancesofthesameprogrampoint
Example 1: Liveness Analysis
Liveness Analysis
Defini*on– Avariableisliveatapar=cularpointintheprogramifitsvalueatthatpointwillbeusedinthefuture(dead,otherwise).
– Tocomputelivenessatagivenpoint,weneedtolookintothefutureMo*va*on:RegisterAlloca*on– Aprogramcontainsanunboundednumberofvariables– Mustexecuteonamachinewithaboundednumberofregisters– Twovariablescanusethesameregisteriftheyareneverinuseatthesame=me(i.e,neversimultaneouslylive).
– Registeralloca=onuseslivenessinforma=on
Control Flow Graph
• Let’sconsiderCFGwherenodescontainprogramstatementinsteadofbasicblock.• Example
1. a:=02. L1:b:=a+13. c:=c+b4. a:=b*25. ifa<9gotoL16. returnc
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Liveness by Example
• Liverangeofb• Variablebisreadinline4,sobisliveon3->4edge• bisalsoreadinline3,sobisliveon(2->3)edge• Line2assignsb,sovalueofbonedges(1->2)and(5->2)arenotneeded.Sobisdeadalongthoseedges.
• b’sliverangeis(2->3->4)
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Liveness by Example
• Liverangeofa• (1->2)and(4->5->2)• aisdeadon(2->3->4)
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Terminology • Flowgraphterms
• ACFGnodehasout-edgesthatleadtosuccessornodesandin-edgesthatcomefrompredecessornodes
• pred[n]isthesetofallpredecessorsofnoden
• succ[n]isthesetofallsuccessorsofnoden
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Examples – Out-edgesofnode5:(5→6)and(5→2)
–succ[5]={2,6}–pred[5]={4}–pred[2]={1,5}
Uses and Defs Def(ordefini*on)– Anassignmentofavaluetoavariable– def[v]=setofCFGnodesthatdefinevariablev– def[n]=setofvariablesthataredefinedatnoden
Use– Areadofavariable’svalue– use[v]=setofCFGnodesthatusevariablev– use[n]=setofvariablesthatareusedatnoden
Moreprecisedefini*onofliveness–AvariablevisliveonaCFGedgeif
(1) ∃ adirectedpathfromthatedgetoauseofv(nodeinuse[v]),and
(2) thatpathdoesnotgothroughanydefofv(nonodesindef[v])
a = 0
a < 9
∉ def[v]
∈ use[v]
v live
The Flow of Liveness • Data-flow• LivenessofvariablesisapropertythatflowsthroughtheedgesoftheCFG
• Direc=onofFlow• LivenessflowsbackwardsthroughtheCFG,becausethebehavioratfuturenodesdetermineslivenessatagivennode
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Liveness at Nodes
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
a = 0
Justbeforecomputa=on
Justamercomputa=on
TwoMoreDefini*ons–Avariableislive-outatanodeifitisliveonanyoutedges
–Avariableislive-inatanodeifitisliveonanyinedges
CompuEng Liveness • Generateliveness:Ifavariableisinuse[n],itislive-inatnoden• Pushlivenessacrossedges:
• Ifavariableislive-inatanoden• thenitislive-outatallnodesinpred[n]
• Pushlivenessacrossnodes:• Ifavariableislive-outatnodenandnotindef[n]• thenthevariableisalsolive-inatn
• DataflowEqua=on: in[n] = use[n] ∪ (out[n] – def[n])
out[n] = ∪ in[s] s ∈ succ[n]
Solving Dataflow EquaEon
foreachnodeninCFGin[n]=∅;out[n]=∅repeatforeachnodeninCFGin’[n]=in[n]out’[n]=out[n]in[n]=use[n]∪(out[n]–def[n])out[n]=∪in[s]s∈succ[n]un*lin’[n]=in[n]andout’[n]=out[n]foralln
Ini=alizesolu=ons
Savecurrentresults
Solvedata-flowequa=on
Testforconvergence
CompuEng Liveness Example
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
IteraEng Backwards: Converges Faster
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Liveness Example: Round1
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
A variable is live at a particular point in the program if its value at that point will be used in the future (dead, otherwise).
Node use def
6 c
5 a
4 b a
3 bc c
2 a b
1 a
Liveness Example: Round1
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No
Yes
Node use def
6 c
5 a
4 b a
3 bc c
2 a b
1 a
in:c
in:ac
out:c
in:bc
out:ac
in:bc
out:bc
in:ac
out:bc
in:c
out:ac
Liveness Example: Round1
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No
Yes
Node use def
6 c
5 a
4 b a
3 bc c
2 a b
1 a
in:c
in:ac
out:ac
in:bc
out:ac
in:bc
out:bc
in:ac
out:bc
in:c
out:ac
ConservaEve ApproximaEon
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No YesSolu*onX:-Fromthepreviousslide
ConservaEve ApproximaEon
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Solu*onY:Carriesvariableduselessly–DoesYleadtoacorrectprogram?
Impreciseconserva*vesolu*ons⇒sub-op*malbutcorrectprograms
ConservaEve ApproximaEon
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Solu*onZ:Doesnotiden=fycasliveinallcases–DoesZleadtoacorrectprogram?
Non-conserva*vesolu*ons⇒incorrectprograms
Need for approximaEon
• Sta=cvs.DynamicLiveness:b*bisalwaysnon-nega=ve,soc>=bisalwaystrueanda’svaluewillneverbeusedamernode
Nocompilercansta*callyiden*fyallinfeasiblepaths
Liveness Analysis Example Summary
• Liverangeofa• (1->2)and(4->5->2)
• Liverangeofb• (2->3->4)
• Liverangeofc• Entry->1->2->3->4->5->2,5->6
Youneed2registersWhy?
4.a=b*2
2.b=a+1
1.a=0
3.c=c+b
5.a<9
6.returnc
No Yes
Example 2: Reaching DefiniEon
CompuEng Reaching DefiniEon
• Assump=on:Atmostonedefini=onpernode
• Gen[n]:Defini=onsthataregeneratedbynoden(atmostone)• Kill[n]:Defini=onsthatarekilledbynoden
{y,i}
Data-flow equaEons for Reaching DefiniEon
Recall Liveness Analysis
• Data-flowEqua=onforliveness
• Livenessequa*onsintermsofGenandKill
Gen:Newinforma=onthat’saddedatanodeKill:Oldinforma=onthat’sremovedatanodeCandefinealmostanydata-flowanalysisintermsofGenandKill
DirecEon of Flow
Data-Flow EquaEon for reaching definiEon
Available Expression
• Anexpression,x+y,isavailableatnodenifeverypathfromtheentrynodetonevaluatesx+y,andtherearenodefini=onsofxoryamerthelastevalua=on.
Available Expression for CSE
• CommonSubexpressioneliminated• Ifanexpressionisavailableatapointwhereitisevaluated,itneednotberecomputed
Must vs. May analysis
• Mayinforma*on:Iden=fiespossibili=es• Mustinforma*on:Impliesaguarantee
May Must
Forward ReachingDefini=on AvailableExpression
Backward LiveVariables VeryBusyExpression