CompilationLecture7
IR+OptimizationsNoamRinetzky
1
BasicCompilerPhases
2
Sourceprogram(string)
.EXE
lexicalanalysis
syntaxanalysis
semanticanalysis
Codegeneration
Assembler/Linker
Tokens
Abstractsyntaxtree
Assembly
FramemanagerControlFlowGraph
IROptimization
3
Optimizationpoints
sourcecode
Frontend IR Code
generatortargetcode
Userprofileprogramchangealgorithm
Compilerintraprocedural IRInterprocedural IRIRoptimizations
Compilerregisterallocationinstructionselection
peepholetransformations
now 4
IROptimization
• Makingcodebetter
5
IROptimization
• Makingcode“better”
6
“Optimized”evaluation
b
5 c
*
arrayaccess
+
abase index
w=0
w=0 w=0
w=1w=0
w=1
w=1
_t0= cgen( a+b[5*c])Phase2:- useweightstodecideonorderoftranslation
_t0
_t0
_t0Heaviersub-tree
Heaviersub-tree
_t0 = _t1 * _t0
_t0 = _t1[_t0]
_t0 = _t1 + _t0
_t0_t1
_t1
_t1_t0 = c
_t1 = 5
_t1 = b
_t1 = a7
Butwhatabout…
a:=1+2;y:=a+b;x:=a+b+8;z:=b+a;
a:=a+1;w:=a+b;
8
OverviewofIRoptimization
• FormalismsandTerminology– Control-flowgraphs– Basicblocks
• Localoptimizations– Speedingupsmallpiecesofaprocedure
• Globaloptimizations– Speedingupprocedureasawhole
• Thedataflowframework– Definingandimplementingawideclassofoptimizations
9
ProgramAnalysis
• Inordertooptimizeaprogram,thecompilerhastobeabletoreasonaboutthepropertiesofthatprogram
• Ananalysisiscalledsound ifitneverassertsanincorrectfactaboutaprogram
• Alltheanalyseswewilldiscussinthisclassaresound– (Why?)
10
Soundnessint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x holdssome
integervalue”
11
Soundnessint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x iseither137
or42”
12
(Un)Soundnessint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x is137”
13
Soundness&Precisionint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x iseither137,
42,or271”
14
Semantics-preservingoptimizations
• Anoptimizationissemantics-preserving ifitdoesnotalterthesemanticsoftheoriginalprogram
• Examples:– Eliminatingunnecessarytemporaryvariables– Computingvaluesthatareknownstaticallyatcompile-time
insteadofruntime– Evaluatingconstantexpressionsoutsideofaloopinsteadof
inside• Non-examples:
– Replacingbubblesortwithquicksort (why?)– Theoptimizationswewillconsiderinthisclassareall
semantics-preserving
15
AformalismforIRoptimization
• Everyphaseofthecompilerusessomenewabstraction:– Scanningusesregularexpressions– ParsingusesCFGs– Semanticanalysisusesproofsystemsandsymboltables
– IRgenerationusesASTs• Inoptimization,weneedaformalismthatcapturesthestructureofaprograminawayamenabletooptimization
16
VisualizingIRmain:
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_L0:_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
_L1:Push a;Call _PrintInt;
17
VisualizingIRmain:
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_L0:_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
_L1:Push a;Call _PrintInt;
18
VisualizingIRmain:
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_L0:_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
_L1:Push a;Call _PrintInt;
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;
c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
Push a;Call _PrintInt;
start
end 19
Basicblocks
• Abasicblock isasequenceofIRinstructionswhere– Thereisexactlyonespotwherecontrolentersthesequence,whichmustbeatthestartofthesequence
– Thereisexactlyonespotwherecontrolleavesthesequence,whichmustbeattheendofthesequence
• Informally,asequenceofinstructionsthatalwaysexecuteasagroup
20
Control-FlowGraphs
• Acontrol-flowgraph(CFG)isagraphofthebasicblocksinafunction
• ThetermCFGisoverloaded– fromhereonout,we'llmean“control-flowgraph”andnot“contextfreegrammar”
• Eachedgefromonebasicblocktoanotherindicatesthatcontrolcanflowfromtheendofthefirstblocktothestartofthesecondblock
• Thereisadedicatednodeforthestartandendofafunction
21
Typesofoptimizations
• Anoptimizationislocal ifitworksonjustasinglebasicblock
• Anoptimizationisglobal ifitworksonanentirecontrol-flowgraph
• Anoptimizationisinterprocedural ifitworksacrossthecontrol-flowgraphsofmultiplefunctions– Wewon'ttalkaboutthisinthiscourse
22
Basicblocksexerciseint main()
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
START:_t0 = 137;y = _t0;IfZ x Goto _L0;t1 = y;z = _t1;Goto END:
_L0:_t2 = y;x = _t2;
END:
Dividethecodeintobasicblocks23
Control-flowgraphexerciseSTART:
_t0 = 137;y = _t0;IfZ x Goto _L0;t1 = y;z = _t1;Goto END:
_L0:_t2 = y;x = _t2;
END:
Drawthecontrol-flowgraph
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
24
Control-flowgraphexercise
_t0 = 137;y = _t0;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
25
Localoptimizations
_t0 = 137;y = _t0;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
end
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
26
Localoptimizations
_t0 = 137;y = _t0;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
27
Localoptimizations
y = 137;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
28
Localoptimizations
y = 137;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
29
Localoptimizations
y = 137;IfZ x Goto _L0;
start
z = y;_t2 = y;x = _t2;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
30
Localoptimizations
y = 137;IfZ x Goto _L0;
start
z = y;_t2 = y;x = _t2;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
31
Localoptimizations
y = 137;IfZ x Goto _L0;
start
z = y; x = y;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
32
Globaloptimizations
y = 137;IfZ x Goto _L0;
z = y; x = y;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
33
start
Globaloptimizations
y = 137;IfZ x Goto _L0;
z = y; x = y;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
34
start
Globaloptimizations
y = 137;IfZ x Goto _L0;
z = 137; x = 137;
End
int main() int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;
35
start
LocalOptimizations
36
Optimizationpath
IR Control-FlowGraph
CFGbuilder
ProgramAnalysis
AnnotatedCFG
OptimizingTransformation
TargetCode
CodeGeneration
(+optimizations)
donewithIR
optimizations
IRoptimizations
37
ExampleObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
38
Forbrevity:Simplified IRforprocedurereturns
ExampleObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
39
ClassObjectmethodfn(int);
Explainingtheprogram
ExampleObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
40
Size ofObject
ObjectClass
ClassObjectmethodfn(int);
Forsimplicity,ignorePoppingreturnvalue,
parametersetc.
Explainingtheprogram
ExampleObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
41
ClassObjectmethodfn(int);
Explainingtheprogram
ExampleObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
42
ClassObjectmethodfn(int);
Explainingtheprogram
ExampleObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
43
PointstoObjectC
Startoffn
ClassObjectmethodfn(int);
Explainingtheprogram
CommonSubexpressionElimination
• Ifwehavetwovariableassignmentsv1=aopb…v2=aopb
• andthevaluesofv1,a,andbhavenotchangedbetweentheassignments,rewritethecodeasv1=aopb…v2=v1
• Eliminatesuselessrecalculation• Pavesthewayforlateroptimizations
44
CommonSubexpressionElimination
• Ifwehavetwovariableassignmentsv1=aopb[or:v1=a]…v2=aopb[or:v2=a]
• andthevaluesofv1,a,andbhavenotchangedbetweentheassignments,rewritethecodeasv1=aopb[or:v1=a]…v2=v1
• Eliminatesuselessrecalculation• Pavesthewayforlateroptimizations
45
Commonsubexpressionelimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
46
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
Commonsubexpressionelimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
47
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
Commonsubexpressionelimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
48
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
Commonsubexpressionelimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
49
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
Commonsubexpressionelimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
50
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
Commonsubexpressionelimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
51
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation
• Ifwehaveavariableassignmentv1=v2thenaslongasv1andv2arenotreassigned,wecanrewriteexpressionsoftheforma=…v1…asa=…v2…providedthatsucharewriteislegal
52
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
53
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
54
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
55
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
56
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
57
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
58
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
59
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
60
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
61
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
62
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
IsthistransformationOK?Whatdoweneedtoknow?
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
63
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
64
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
65
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
CopyPropagation_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
66
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
DeadCodeElimination
• Anassignmenttoavariableviscalleddeadifthevalueofthatassignmentisneverreadanywhere
• DeadcodeeliminationremovesdeadassignmentsfromIR
• Determiningwhetheranassignmentisdeaddependsonwhatvariableisbeingassignedtoandwhenit'sbeingassigned
67
DeadCodeElimination
68
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
DeadCodeElimination_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
69
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
DeadCodeEliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;_tmp2 = ObjectC;*(_tmp1) = ObjectC;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = ObjectC;_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
valuesneverread
valuesneverread
70
DeadCodeEliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;
*(_tmp1) = ObjectC;
_tmp4 = _tmp0 + b;c = _tmp4;
_tmp7 = *(ObjectC);Push c;Push _tmp1;Call _tmp7;
71
Applyinglocaloptimizations
• Thedifferentoptimizationswe'veseensofaralltakecareofjustasmallpieceoftheoptimization
• Commonsubexpressioneliminationeliminatesunnecessarystatements
• Copypropagationhelpsidentifydeadcode• Deadcodeeliminationremovesstatementsthatarenolongerneeded
• Togetmaximumeffect,wemayhavetoapplytheseoptimizationsnumeroustimes
72
Applyinglocaloptimizationsexample
b = a * a;c = a * a;d = b + c;e = b + b;
73
Applyinglocaloptimizationsexample
b = a * a;c = a * a;d = b + c;e = b + b;
Whichoptimizationshouldweapplyhere?
74
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + c;e = b + b;
Commonsub-expressionelimination
Whichoptimizationshouldweapplyhere?
75
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + c;e = b + b;
Whichoptimizationshouldweapplyhere?
76
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + b;e = b + b;
Whichoptimizationshouldweapplyhere?
Copypropagation
77
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + b;e = b + b;
Whichoptimizationshouldweapplyhere?
78
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + b;e = d;
Whichoptimizationshouldweapplyhere?
Commonsub-expressionelimination(again)
79
Othertypesoflocaloptimizations
• ArithmeticSimplification– Replace“hard”operationswitheasierones– e.g.rewritex = 4 * a; asx = a << 2;
• ConstantFolding– Evaluateexpressionsatcompile-timeiftheyhaveaconstantvalue.
– e.g.rewritex = 4 * 5; asx = 20;
80
Optimizationsandanalyses
• Mostoptimizationsareonlypossiblegivensomeanalysisoftheprogram'sbehavior
• Inordertoimplementanoptimization,wewilltalkaboutthecorrespondingprogramanalyses
81
Availableexpressions
• Bothcommonsubexpressioneliminationandcopypropagationdependonananalysisoftheavailableexpressionsinaprogram
• Anexpressioniscalledavailable ifsomevariableintheprogramholdsthevalueofthatexpression
• Incommonsubexpressionelimination,wereplaceanavailableexpressionbythevariableholdingitsvalue
• Incopypropagation,wereplacetheuseofavariablebytheavailableexpressionitholds
82
Findingavailableexpressions
• Initially,noexpressionsareavailable• Wheneverweexecuteastatementa=bop c:– Anyexpressionholdinga isinvalidated– Theexpressiona=bop cbecomesavailable
• Idea:Iterateacrossthebasicblock,beginningwiththeemptysetofexpressionsandupdatingavailableexpressionsateachvariable
83
Availableexpressionsexample
84
a = b + 2;
b = x;
d = a + b;
e = a + b;
d = x;
f = a + b; b = x, d = x, e = a + b
b = x, d = a + b, e = a + b
b = x, d = a + b
b = x
a = b + 2
b = x, d = x, e = a + b, f = a + b
Commonsub-expressionelimination
85
a = b + 2;
b = x;
d = a + b;
e = d;
d = b;
f = e; b = x, d = x, e = a + b
b = x, d = a + b, e = a + b
b = x, d = a + b
b = x
a = b + 2
b = x, d = x, e = a + b, f = a + b
Commonsub-expressionelimination
86
a = b + 2;
b = x;
d = a + b;
e = a + b;
d = x;
f = a + b; b = x, d = x, e = a + b
b = x, d = a + b, e = a + b
b = x, d = a + b
b = x
a = b + 2
b = x, d = x, e = a + b, f = a + b
Commonsub-expressionelimination
87
a = b + 2;
b = 1;
d = a + b;
e = a + b;
d = b;
f = a + b; b = 1, d = b, e = a + b
b = 1, d = a + b, e = a + b
b = 1, d = a + b
b = 1
a = b + 2
a = b, c = b, d = b, e = a + b, f = a + b
Commonsub-expressionelimination
88
a = b + 2;
b = 1;
d = a + b;
e = a + b;
d = b;
f = a + b; b = 1, d = b, e = a + b
b = 1, d = a + b, e = a + b
b = 1, d = a + b
b = 1
a = b + 2
a = b, c = b, d = b, e = a + b, f = a + b
Commonsub-expressionelimination
89
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b; a = b, c = b, d = b, e = a + b
a = b, c = b, d = a + b, e = a + b
a = b, c = b, d = a + b
a = b, c = b
a = b
a = b, c = b, d = b, e = a + b, f = a + b
90
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b; a = b, c = b, d = b, e = a + b
a = b, c = b, d = a + b, e = a + b
a = b, c = b, d = a + b
a = b, c = b
a = b
a = b, c = b, d = b, e = a + b, f = a + b
Commonsub-expressionelimination
91
a = b;
b = 1;
d = a + b;
e = d;
d = a;
f = e; a = b, c = b, d = b, e = a + b
a = b, c = b, d = a + b, e = a + b
a = b, c = b, d = a + b
a = b, b = b
a = b
a = b, c = b, d = b, e = a + b, f = a + b
Commonsub-expressionelimination
Livevariables
• Theanalysiscorrespondingtodeadcodeeliminationiscalledlivenessanalysis
• Avariableislive atapointinaprogramiflaterintheprogramitsvaluewillbereadbeforeitiswrittentoagain
• Deadcodeeliminationworksbycomputinglivenessforeachvariable,theneliminatingassignmentstodeadvariables
92
Computinglivevariables• Toknowifavariablewillbeusedatsomepoint,weiterateacrossthestatementsinabasicblockinreverseorder
• Initially,somesmallsetofvaluesareknowntobelive(whichonesdependsontheparticularprogram)
• Whenweseethestatementa=bopc:– Justbeforethestatement,aisnotalive,sinceitsvalueisabouttobeoverwritten
– Justbeforethestatement,bothbandcarealive,sincewe'reabouttoreadtheirvalues
– (whatifwehavea=a+b?) 93
Livenessanalysisa = b;
c = a;
d = a + b;
e = d;
d = a;
f = e; b, d, e
a, b, e
a, b, d
a, b
a, b
b
b, d - given
Whichstatementsaredead?
94
DeadCodeEliminationa = b;
c = a;
d = a + b;
e = d;
d = a;
f = e; b, d, e
a, b, e
a, b, d
a, b
a, b
b
b, d
Whichstatementsaredead?
95
DeadCodeEliminationa = b;
d = a + b;
e = d;
d = a; b, d, e
a, b, e
a, b, d
a, b
a, b
b
b, d 96
LivenessanalysisIIa = b;
d = a + b;
e = d;
d = a; b, d
a, b
a, b, d
a, b
b
Whichstatementsaredead?
97
LivenessanalysisIIa = b;
d = a + b;
e = d;
d = a; b, d
a, b
a, b, d
a, b
b
Which statements are dead?
98
Deadcodeeliminationa = b;
d = a + b;
e = d;
d = a; b, d
a, b
a, b, d
a, b
b
Which statements are dead?
99
Deadcodeeliminationa = b;
d = a + b;
d = a; b, d
a, b
a, b, d
a, b
b
100
LivenessanalysisIIIa = b;
d = a + b;
d = a; b, d
a, b
a, b
b
Whichstatementsaredead?
101
Deadcodeeliminationa = b;
d = a + b;
d = a; b, d
a, b
a, b
b
Whichstatementsaredead?
102
Deadcodeeliminationa = b;
d = a; b, d
a, b
a, b
b
103
Deadcodeeliminationa = b;
d = a;
104
Ifwefurtherapplycopypropagationthisstatementcanbeeliminatedtoo
Acombinedalgorithm
• Startwithinitiallivevariablesatendofblock
• Traversestatementsfromendtobeginning• Foreachstatement
– Ifassignstodeadvariables– eliminateit– Otherwise,computelivevariablesbeforestatementandcontinueinreverse
105
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
106
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
f = e; b, d
107
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
f = e; b, d
108
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
b, d 109
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
b, d
a, b
110
Acombinedalgorithm
111
a = b;
c = a;
d = a + b;
e = d;
d = a;
b, d
a, b
Acombinedalgorithm
112
a = b;
c = a;
d = a + b;
d = a;
b, d
a, b
Acombinedalgorithma = b;
c = a;
d = a + b;
d = a;
b, d
a, b
113
Acombinedalgorithma = b;
c = a;
d = a;
b, d
a, b
114
Acombinedalgorithma = b;
c = a;
d = a;
b, d
a, b
115
Acombinedalgorithma = b;
d = a;
b, d
a, b
116
Acombinedalgorithma = b;
d = a;
b, d
a, b
117
b
Acombinedalgorithma = b;
d = a;
118
High-levelgoals
• Generalizeanalysismechanism– Reusecommoningredientsformanyanalyses– Reuseproofsofcorrectness
• GeneralizefrombasicblockstoentireCFGs– Gofromlocaloptimizationstoglobaloptimizations
119
ProgramAnalysis
• Reasonsaboutthebehavior ofaprogram• Ananalysisissound ifitonlyassertsancorrectfactsaboutaprogram
• Ananalysisisprecise ifitassertsallcorrectfacts(ofinterests)
• Soundanalysisallowsforsemantic-preservingoptimizations– “Moreprecise”analysesare“moreuseful”:mayenablemoreoptimizations 120
Examples
• Availableexpressions,allows:ØCommonsub-expressionseliminationØCopypropagation
• Constantpropagation,allows:ØConstantfolding
• Liveness analysisØDead-codeeliminationØRegisterallocation
Localvs.globaloptimizations
• Anoptimizationislocal ifitworksonjustasinglebasicblock
• Anoptimizationisglobal ifitworksonanentirecontrol-flowgraphofaprocedure
• Anoptimizationisinterprocedural ifitworksacrossthecontrol-flowgraphsofmultipleprocedure– Wewon'ttalkaboutthisinthiscourse
122
Formalizinglocalanalyses
123
a = b + c
OutputValueVout
InputValueVin
Vout = fa=b+c(Vin)
TransferFunction
AvailableExpressions
124
a = b + c
OutputValueVout
InputValueVin
Vout =(Vin \ e|econtainsa)4 a=b+c
Expressionsoftheformsa=…andx=…a…
LiveVariables
125
a = b + c
OutputValueVout
InputValueVin
Vin = (Vout \ a) 4 b,c
Vin
Vout
LiveVariables
126
a = b + c
OutputValueVout
InputValueVin
Vin = (Vout \ a) 4 b,c
Vin
Vout
Informationforalocalanalysis
• Whatdirectionarewegoing?– Sometimesforward(availableexpressions)– Sometimesbackward(livenessanalysis)
• Howdoweupdateinformationafterprocessingastatement?– Whatarethenewsemantics?– Whatinformationdoweknowinitially?
127
Formalizinglocalanalyses
• Defineananalysisofabasicblockasaquadruple(D,V,F,I)where– D isadirection(forwardsorbackwards)– V isasetofvaluestheprogramcanhaveatanypoint
– F isafamilyoftransferfunctionsdefiningthemeaningofanyexpressionasafunctionf:Vt V
– I istheinitialinformationatthetop(orbottom)ofabasicblock
128
AvailableExpressions
• Direction: Forward• Values: Setsofexpressionsassignedtovariables• Transferfunctions: GivenasetofvariableassignmentsVandstatementa=b+c:– RemovefromVanyexpressioncontainingaasasubexpression
– AddtoVtheexpressiona=b+c– Formally:Vout =(Vin \ e|econtainsa)4 a=b+c
• Initialvalue: Emptysetofexpressions
129
LivenessAnalysis
• Direction: Backward• Values: Setsofvariables• Transferfunctions: GivenasetofvariableassignmentsV
andstatementa=b+c:• RemoveafromV(anypreviousvalueofaisnowdead.)• AddbandctoV(anypreviousvalueofborcisnowlive.)• Formally:Vin =(Vout \ a)4 b,c• Initialvalue: Dependsonsemanticsoflanguage
– E.g.,functionargumentsandreturnvalues(pushes)– Resultoflocalanalysisofotherblocksaspartofaglobalanalysis 130
Runninglocalanalyses
• Givenananalysis(D,V,F,I)forabasicblock• AssumethatD is“forward;”analogousforthereversecase
• Initially,setOUT[entry]toI• Foreachstatements,inorder:
– SetIN[s]toOUT[prev],whereprev isthepreviousstatement
– SetOUT[s]tofs(IN[s]),wherefs isthetransferfunctionforstatements
131
Kill/Gen
132
GlobalOptimizations
133
High-levelgoals
• Generalizeanalysismechanism– Reusecommoningredientsformanyanalyses– Reuseproofsofcorrectness
• GeneralizefrombasicblockstoentireCFGs– Gofromlocaloptimizationstoglobaloptimizations
134
Globalanalysis
• Aglobalanalysisisananalysisthatworksonacontrol-flowgraphasawhole
• Substantiallymorepowerfulthanalocalanalysis– (Why?)
• Substantiallymorecomplicatedthanalocalanalysis– (Why?)
135
Localvs.globalanalysis• Manyoftheoptimizationsfromlocalanalysiscanstill
beappliedglobally– Commonsub-expressionelimination– Copypropagation– Deadcodeelimination
• Certainoptimizationsarepossibleinglobalanalysisthataren'tpossiblelocally:– e.g.codemotion:Movingcodefromonebasicblockinto
anothertoavoidcomputingvaluesunnecessarily• Exampleglobaloptimizations:
– Globalconstantpropagation– Partialredundancyelimination
136
Loopinvariantcodemotionexample
137
while (t < 120) z = z + x - y;
w = x – y;while (t < 120) z = z + w;
valueofexpressionx– yisnotchangedbyloopbody
Whyglobalanalysisishard
• Needtobeabletohandlemultiplepredecessors/successorsforabasicblock
• Needtobeabletohandlemultiplepathsthroughthecontrol-flowgraph,andmayneedtoiteratemultipletimestocomputethefinalvalue(buttheanalysisstillneedstoterminate!)
• Needtobeabletoassigneachbasicblockareasonabledefaultvalueforbeforewe'veanalyzedit
138
Globaldeadcodeelimination
• Localdeadcodeeliminationneededtoknowwhatvariableswereliveonexitfromabasicblock
• Thisinformationcanonlybecomputedaspartofaglobalanalysis
• HowdowemodifyourlivenessanalysistohandleaCFG?
139
CFGswithoutloops
140Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
CFGswithoutloops
141Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
x, y
x, y
a, b, c, d
a, b, c, d a, b, c, d
a, b, c, db, c, d
a, b, c, d
a, c, d
?
Whichvariablesmaybeliveonsomeexecutionpath?
CFGswithoutloops
142Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
x, y
x, y
a, b, c, d
a, b, c, d a, b, c, d
a, b, c, db, c, d
a, b, c, d
a, c, d
CFGswithoutloops
143Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
CFGswithoutloops
144Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
Majorchanges– part1
• Inalocalanalysis,eachstatementhasexactlyonepredecessor
• Inaglobalanalysis,eachstatementmayhavemultiplepredecessors
• Aglobalanalysismusthavesomemeansofcombininginformationfromallpredecessorsofabasicblock
145
CFGswithoutloops
146Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
x, y
x, y
a, b, c, d
a, b, c, d a, b, c, d
a, b, c, db, c, d
b, c, d
c, d Needtocombinecurrently-computedvaluewithnewvalue
Needtocombinecurrently-computedvaluewithnewvalue
CFGswithoutloops
147Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
x, y
x, y
a, b, c, d
a, b, c, d a, b, c, d
a, b, c, db, c, d
a, b, c, d
c, d
CFGswithoutloops
148Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
x, y
x, y
a, b, c, d
a, b, c, d a, b, c, d
a, b, c, db, c, d
a, b, c, d
a, c, d
Majorchanges– part2
• Inalocalanalysis,thereisonlyonepossiblepaththroughabasicblock
• Inaglobalanalysis,theremaybemanypathsthroughaCFG
• Mayneedtorecomputevaluesmultipletimesasmoreinformationbecomesavailable
• Needtobecarefulwhendoingthisnottoloopinfinitely!– (Moreonthatlater)
• Canorderofcomputationaffectresult?149
CFGswithloops• Uptothispoint,we'veconsideredloop-freeCFGs,whichhaveonlyfinitelymanypossiblepaths
• Whenweaddloopsintothepicture,thisisnolongertrue
• NotallpossibleloopsinaCFGcanberealizedintheactualprogram
150
IfZ x goto Top
x = 1;
Top:
x = 0;
x = 2;
CFGswithloops• Uptothispoint,we'veconsideredloop-freeCFGs,whichhaveonlyfinitelymanypossiblepaths
• Whenweaddloopsintothepicture,thisisnolongertrue
• NotallpossibleloopsinaCFGcanberealizedintheactualprogram
• Soundapproximation:AssumethateverypossiblepaththroughtheCFGcorrespondstoavalidexecution– Includesallrealizablepaths,butsomeadditionalpathsaswell
– Maymakeouranalysislessprecise(butstillsound)– Makestheanalysisfeasible;we'llseehowlater
151
CFGswithloops
152Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;IfZ ...
Entry
a
?
Majorchanges– part3
• Inalocalanalysis,thereisalwaysawelldefined“first”statementtobeginprocessing
• Inaglobalanalysiswithloops,everybasicblockmightdependoneveryotherbasicblock
• Tofixthis,weneedtoassigninitialvaluestoalloftheblocksintheCFG
153
CFGswithloops- initialization
154Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
CFGswithloops- iteration
155Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a
CFGswithloops- iteration
156Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, b, c
a
CFGswithloops- iteration
157Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, b, c
a
a, b, c
CFGswithloops- iteration
158Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
b, c
a, b, c
a
a, b, c
CFGswithloops- iteration
159Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
b, c
a, b, c
a
a, b, c
b, c
CFGswithloops- iteration
160Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
b, c
c, d
a, b, c
a
a, b, c
b, c
a, b, c
CFGswithloops- iteration
161Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a
a, b, c
b, c
a, b, c
CFGswithloops- iteration
162Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a
a, b, c
b, c
a, b, c
CFGswithloops- iteration
163Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a, c, d
a, b, c
b, c
a, b, c
CFGswithloops- iteration
164Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a, c, d
a, b, c
b, c
a, b, c
CFGswithloops- iteration
165Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a, c, d
a, b, c
b, c
a, b, c
CFGswithloops- iteration
166Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a, c, d
a, b, c
b, c
a, b, c
CFGswithloops- iteration
167Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
c, d
a, b, c
a, c, d
a, b, c
a, b, c
a, b, c
CFGswithloops- iteration
168Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
a, c, d
a, b, c
a, c, d
a, b, c
a, b, c
a, b, c
CFGswithloops- iteration
169Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
a
a, bb, c
a, c, d
a, b, c
a, c, d
a, b, c
a, b, c
a, b, c
Summaryofdifferences
• Needtobeabletohandlemultiplepredecessors/successorsforabasicblock
• Needtobeabletohandlemultiplepathsthroughthecontrol-flowgraph,andmayneedtoiteratemultipletimestocomputethefinalvalue– Buttheanalysisstillneedstoterminate!
• Needtobeabletoassigneachbasicblockareasonabledefaultvalueforbeforewe'veanalyzedit
170
Globallivenessanalysis• Initially,setIN[s]=foreachstatements• SetIN[exit]tothesetofvariablesknowntobeliveonexit(language-specificknowledge)
• Repeatuntilnochangesoccur:– Foreachstatements oftheforma=b+c,inanyorderyou'dlike:• SetOUT[s]tosetunionofIN[p]foreachsuccessorp ofs• SetIN[s]to(OUT[s]– a)4 b,c.
• Yetanotherfixed-pointiteration!
171
Globallivenessanalysis
172
a=b+c
s2 s3
IN[s2] IN[s3]
OUT[s]=IN[s2]4 IN[s3]
IN[s]=(UT[s] – a)4 b,c
Whydoesthiswork?• Toshowcorrectness,weneedtoshowthat
– Thealgorithmeventuallyterminates,and– Whenitterminates,ithasasoundanswer
• Terminationargument:– Onceavariableisdiscoveredtobeliveduringsomepointofthe
analysis,italwaysstayslive– Onlyfinitelymanyvariablesandfinitelymanyplaceswherea
variablecanbecomelive• Soundnessargument(sketch):
– Eachindividualrule,appliedtosomeset,correctlyupdateslivenessinthatset
– Whencomputingtheunionofthesetoflivevariables,avariableisonlyliveifitwasliveonsomepathleavingthestatement
173
AbstractInterpretation
• Theoreticalfoundationsofprogramanalysis
• Cousot andCousot 1977
• Abstractmeaningofprograms– Executedatcompiletime
174
Anotherviewoflocaloptimization
• Inlocaloptimization,wewanttoreasonaboutsomepropertyoftheruntimebehavioroftheprogram
• Couldweruntheprogramandjustwatchwhathappens?
• Idea:Redefinethesemanticsofourprogramminglanguagetogiveusinformationaboutouranalysis
175
Propertiesoflocalanalysis
• Theonlywaytofindoutwhataprogramwillactuallydoistorunit
• Problems:– Theprogrammightnotterminate– Theprogrammighthavesomebehaviorwedidn'tseewhenweranitonaparticularinput
• However,thisisnotaprobleminsideabasicblock– Basicblockscontainnoloops– Thereisonlyonepaththroughthebasicblock
176
Assigningnewsemantics
• Example:AvailableExpressions• Redefinethestatementa=b+ctomean“anowholdsthevalueofb+c,andanyvariableholdingthevalueaisnowinvalid”
• Runtheprogramassumingthesenewsemantics
• Treattheoptimizerasaninterpreterforthesenewsemantics
177
Theorytotherescue
• Buildingupallofthemachinerytodesignthisanalysiswastricky
• Thekeyideas,however,aremostlyindependentoftheanalysis:– Weneedtobeabletocomputefunctionsdescribingthebehaviorofeachstatement
– Weneedtobeabletomergeseveralsubcomputationstogether
– Weneedaninitialvalueforallofthebasicblocks• Thereisabeautifulformalismthatcapturesmanyoftheseproperties
178
Joinsemilattices• Ajoinsemilatticeisaorderingdefinedonasetof
elements• Anytwoelementshavesomejointhatisthesmallest
elementlargerthanbothelements• Thereisauniquebottomelement,whichissmaller
thanallotherelements• Intuitively:
– Thejoinoftwoelementsrepresentscombininginformationfromtwoelementsbyanoverapproximation
• Thebottomelementrepresents“noinformationyet”or“theleastconservativepossibleanswer”
179
Joinsemilatticeforliveness
180
a b c
a, b a, c b, c
a, b, c
Bottomelement
Whatisthejoinofbandc?
181
a b c
a, b a, c b, c
a, b, c
Whatisthejoinofbandc?
182
a b c
a, b a, c b, c
a, b, c
Whatisthejoinofbanda,c?
183
a b c
a, b a, c b, c
a, b, c
Whatisthejoinofbanda,c?
184
a b c
a, b a, c b, c
a, b, c
Whatisthejoinofaanda,b?
185
a b c
a, b a, c b, c
a, b, c
Whatisthejoinofaanda,b?
186
a b c
a, b a, c b, c
a, b, c
Formaldefinitions
• Ajoinsemilatticeisapair(V,7),where• Visadomainofelements• 7 isajoinoperatorthatis
– commutative:x7 y=y7 x– associative:(x7 y)7 z=x7 (y7 z)– idempotent:x7 x=x
• Ifx7 y=z,wesaythatzisthejoinor(leastupperbound)ofxandy
• Everyjoinsemilatticehasabottomelementdenotedz suchthatz 7 x=xforallx
187
Joinsemilatticesandordering
188
a b c
a, b a, c b, c
a, b, cGreater
Lower
Joinsemilatticesandordering
189
a b c
a, b a, c b, c
a, b, cLeastprecise
Mostprecise
Joinsemilatticesandorderings
• Everyjoinsemilattice(V,7)inducesanorderingrelationshipb overitselements
• Definexb yiffx7 y=y• Needtoprove
– Reflexivity:xb x– Antisymmetry:Ifxb yandyb x,thenx=y– Transitivity:Ifxb yandyb z,thenxb z
190
Anexamplejoinsemilattice
• Thesetofnaturalnumbersandthemax function• Idempotent
– maxa,a=a• Commutative
– maxa,b=maxb,a• Associative
– maxa,maxb,c=maxmaxa,b,c• Bottomelementis0:
– max0,a=a• Whatistheorderingovertheseelements?
191
Ajoinsemilatticeforliveness
• Setsoflivevariablesandthesetunionoperation• Idempotent:
– x4 x=x• Commutative:
– x4 y=y4 x• Associative:
– (x4 y)4 z=x4 (y4 z)• Bottomelement:
– Theemptyset:Ø4 x=x• Whatistheorderingovertheseelements?
192
Semilatticesandprogramanalysis
• Semilatticesnaturallysolvemanyoftheproblemsweencounteringlobalanalysis
• Howdowecombineinformationfrommultiplebasicblocks?
• Whatvaluedowegivetobasicblockswehaven'tseenyet?
• Howdoweknowthatthealgorithmalwaysterminates?
193
Semilatticesandprogramanalysis
• Semilatticesnaturallysolvemanyoftheproblemsweencounteringlobalanalysis
• Howdowecombineinformationfrommultiplebasicblocks?– Takethejoinofallinformationfromthoseblocks
• Whatvaluedowegivetobasicblockswehaven'tseenyet?– Usethebottomelement
• Howdoweknowthatthealgorithmalwaysterminates?– Actually,westilldon't!Moreonthatlater
194
Semilatticesandprogramanalysis
• Semilatticesnaturallysolvemanyoftheproblemsweencounteringlobalanalysis
• Howdowecombineinformationfrommultiplebasicblocks?– Takethejoinofallinformationfromthoseblocks
• Whatvaluedowegivetobasicblockswehaven'tseenyet?– Usethebottomelement
• Howdoweknowthatthealgorithmalwaysterminates?– Actually,westilldon't!Moreonthatlater
195
Ageneralframework
• Aglobalanalysisisatuple(D,V,7,F,I),where– D isadirection(forwardorbackward)
• Theordertovisitstatementswithinabasicblock,nottheorderinwhichtovisitthebasicblocks
– V isasetofvalues– 7 isajoinoperatoroverthosevalues– F isasetoftransferfunctionsf:Vt V– I isaninitialvalue
• Theonlydifferencefromlocalanalysisistheintroductionofthejoinoperator
196
Runningglobalanalyses
• Assumethat(D,V,7,F,I)isaforwardanalysis• SetOUT[s]=z forallstatementss• SetOUT[entry]=I• Repeatuntilnovalueschange:
– Foreachstatements withpredecessorsp1,p2,…,pn:• SetIN[s]=OUT[p1]7 OUT[p2]7 …7 OUT[pn]• SetOUT[s]=fs (IN[s])
• Theorderofthisiterationdoesnotmatter– Thisissometimescalledchaoticiteration
197
Forcomparison• SetOUT[s]=z forall
statementss• SetOUT[entry]=I
• Repeatuntilnovalueschange:– Foreachstatements
withpredecessorsp1,p2,…,pn:• SetIN[s]=OUT[p1]7OUT[p2]7 …7 OUT[pn]
• SetOUT[s]=fs (IN[s])
• SetIN[s]= forallstatementss
• SetOUT[exit]=thesetofvariablesknowntobeliveonexit
• Repeatuntilnovalueschange:– Foreachstatements ofthe
forma=b+c:• SetOUT[s]=setunionofIN[x]foreachsuccessorx ofs
• SetIN[s]=(OUT[s]-a) 4 b,c
198
Thedataflowframework
• Thisformofanalysisiscalledthedataflowframework
• Canbeusedtoeasilyproveananalysisissound
• Withcertainrestrictions,canbeusedtoprovethatananalysiseventuallyterminates– Again,moreonthatlater
199
Globalconstantpropagation
• Constantpropagationisanoptimizationthatreplaceseachvariablethatisknowntobeaconstantvaluewiththatconstant
• Anelegantexampleofthedataflowframework
200
Globalconstantpropagation
201
exit x = 4;
z = x;
w = x;
y = x; z = y;
x = 6;entry
Globalconstantpropagation
202
exit x = 4;
z = x;
w = x;
y = x; z = y;
x = 6;entry
Globalconstantpropagation
203
exit x = 4;
z = x;
w = 6;
y = 6; z = y;
x = 6;entry
Constantpropagationanalysis
• Inordertodoaconstantpropagation,weneedtotrackwhatvaluesmightbeassignedtoavariableateachprogrampoint
• Everyvariablewilleither– Neverhaveavalueassignedtoit,– Haveasingleconstantvalueassignedtoit,– Havetwoormoreconstantvaluesassignedtoit,or– Haveaknownnon-constantvalue.– OuranalysiswillpropagatethisinformationthroughoutaCFGtoidentifylocationswhereavalueisconstant
204
Propertiesofconstantpropagation
• Fornow,considerjustsomesinglevariablex• Ateachpointintheprogram,weknowoneofthree
thingsaboutthevalueofx:– x isdefinitelynotaconstant,sinceit'sbeenassignedtwo
valuesorassignedavaluethatweknowisn'taconstant– x isdefinitelyaconstantandhasvaluek– Wehaveneverseenavalueforx
• Notethatthefirstandlastofthesearenot thesame!– Thefirstonemeansthattheremaybeawayforx tohave
multiplevalues– Thelastonemeansthatx neverhadavalueatall
205
Definingajoinoperator• ThejoinofanytwodifferentconstantsisNot-a-Constant
– (Ifthevariablemighthavetwodifferentvaluesonentrytoastatement,itcannotbeaconstant)
• ThejoinofNotaConstantandanyothervalueisNot-a-Constant– (Ifonsomepaththevalueisknownnottobeaconstant,thenon
entrytoastatementitsvaluecan'tpossiblybeaconstant)• ThejoinofUndefined andanyothervalueisthatothervalue
– (Ifx hasnovalueonsomepathanddoeshaveavalueonsomeotherpath,wecanjustpretenditalwayshadtheassignedvalue)
206
Asemilatticeforconstantpropagation• Onepossiblesemilatticeforthisanalysisisshownhere(foreachvariable):
207
Undefined
0-1-2 1 2 ......
Not-a-constant
The lattice is infinitely wide
Asemilatticeforconstantpropagation• Onepossiblesemilatticeforthisanalysisisshownhere(foreachvariable):
208
Undefined
0-1-2 1 2 ......
Not-a-constant
• Note:• ThejoinofanytwodifferentconstantsisNot-a-Constant• ThejoinofNotaConstantandanyothervalueisNot-a-Constant• ThejoinofUndefined andanyothervalueisthatothervalue
Globalconstantpropagation
209
exit x = 4;Undefined
z = x;Undefined
w = x;
y = x; z = y;
x = 6;entry
Globalconstantpropagation
210
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
x=Undefinedy=Undefinedz=Undefinedw=Undefined
Globalconstantpropagation
211
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
Globalconstantpropagation
212
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;Undefined
entryUndefined
Globalconstantpropagation
213
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6, y=z=w=Ω
entryUndefined
Globalconstantpropagation
214
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6, y=z=w=Ω
entryUndefined
Globalconstantpropagation
215
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
216
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
217
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
y=67 y=Undefinedgiveswhat?
Globalconstantpropagation
218
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
219
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;Undefined
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
220
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
221
exit x = 4;Undefined
z = x;Undefined
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
222
exit x = 4;Undefined
x=y=w=6z = x;Undefined
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
223
exit x = 4;Undefined
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
224
exit x = 4;Undefined
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
225
exitx=y=w=z=6x = 4;Undefined
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
226
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
227
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
228
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
229
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
230
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
231
exitx=y=w=z=6x = 4;x=4, y=w=z=6
x=y=w=6z = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
x=67 x=4giveswhat?
Globalconstantpropagation
232
exitx=y=w=z=6x = 4;x=4, y=w=z=6
y=w=6, x=ºz = x;x=y=w=z=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
233
exitx=y=w=z=6x = 4;x=4, y=w=z=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
234
exitx=y=w=z=6x = 4;x=4, y=w=z=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
235
exity=w=6 x = 4;x=4, y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
236
exity=w=6 x = 4;x=4, y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalanalysisreachedfixpoint
Globalconstantpropagation
237
exity=w=6x = 4;y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = x;x=y=w=6
x=6y = x;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Globalconstantpropagation
238
exity=w=6x = 4;y=w=6
y=w=6z = x;y=w=6
x=6,y=6w = 6;x=y=w=6
x=6y = 6;x=6,y=6
x = 6z = y;x = 6
Undefinedx = 6;x = 6
entryUndefined
Dataflowforconstantpropagation
• Direction:Forward• Semilattice:Varst Undefined,0,1,-1,2,-2,…,Not-a-Constant– Joinmappingforvariablespoint-wisexh1,yh1,zh17 xh1,yh2,zhNot-a-Constant=xh1,yhNot-a-Constant,zhNot-a-Constant
• Transferfunctions:– fx=k(V)=V|xhk (updateVbymappingxtok)– fx=a+b(V)=V|xhNot-a-Constant (assignNot-a-Constant)
• Initialvalue:xisUndefined– (Whenmightweusesomeothervalue?)
239
Provingtermination
• Ouralgorithmforrunningtheseanalysescontinuouslyloopsuntilnochangesaredetected
• Giventhis,howdoweknowtheanalyseswilleventuallyterminate?– Ingeneral,wedon‘t
240
Terminates?
241
LivenessAnalysis
• Avariableislive atapointinaprogramiflaterintheprogramitsvaluewillbereadbeforeitiswrittentoagain
242
Joinsemilatticedefinition
• Ajoinsemilatticeisapair(V,7),where• Visadomainofelements• 7 isajoinoperatorthatis
– commutative:x7 y=y7 x– associative:(x7 y)7 z=x7 (y7 z)– idempotent:x7 x=x
• Ifx7 y=z,wesaythatzisthejoinor(LeastUpperBound)ofxandy
• Everyjoinsemilatticehasabottomelementdenotedz suchthatz 7 x=xforallx
243
Partialorderinginducedbyjoin
• Everyjoinsemilattice(V,7)inducesanorderingrelationshipb overitselements
• Definexb yiffx7 y=y• Needtoprove
– Reflexivity:xb x– Antisymmetry:Ifxb yandyb x,thenx=y– Transitivity:Ifxb yandyb z,thenxb z
244
Ajoinsemilatticeforliveness
• Setsoflivevariablesandthesetunionoperation• Idempotent:
– x4 x=x• Commutative:
– x4 y=y4 x• Associative:
– (x4 y)4 z=x4 (y4 z)• Bottomelement:
– Theemptyset:Ø4 x=x• Orderingoverelements=subsetrelation
245
Joinsemilatticeexampleforliveness
246
a b c
a, b a, c b, c
a, b, c
Bottomelement
Dataflowframework
• Aglobalanalysisisatuple(D,V,7,F,I),where– D isadirection(forwardorbackward)
• Theordertovisitstatementswithinabasicblock,NOT theorderinwhichtovisitthebasicblocks
– V isasetofvalues(sometimescalleddomain)– 7 isajoinoperatoroverthosevalues– F isasetoftransferfunctionsfs :Vt V(foreverystatements)– I isaninitialvalue
247
Runningglobalanalyses• Assumethat(D,V,7,F,I)isaforwardanalysis• Foreverystatementsmaintainvaluesbefore- IN[s]- andafter
- OUT[s]• SetOUT[s]=z forallstatementss• SetOUT[entry]=I• Repeatuntilnovalueschange:
– Foreachstatements withpredecessorsPRED[s]=p1,p2,…,pn• SetIN[s]=OUT[p1]7 OUT[p2]7 …7 OUT[pn]• SetOUT[s]=fs(IN[s])
• Theorderofthisiterationdoesnotmatter– Chaoticiteration
248
Provingtermination
• Ouralgorithmforrunningtheseanalysescontinuouslyloopsuntilnochangesaredetected
• Problem: howdoweknowtheanalyseswilleventuallyterminate?
249
Anon-terminatinganalysis
• ThefollowinganalysiswillloopinfinitelyonanyCFGcontainingaloop:
• Direction: Forward• Domain: ℕ• Joinoperator:max• Transferfunction: f(n) = n+1• Initialvalue:0
250
Anon-terminatinganalysis
251
start
end
x = y
Initialization
252
start
end
x = y0
0
Fixed-pointiteration
253
start
end
x = y0
0
Chooseablock
254
start
end
x = y0
0
Iteration1
255
start
end
x = y0
0
0
Iteration1
256
start
end
x = y1
0
0
Chooseablock
257
start
end
x = y1
0
0
Iteration2
258
start
end
x = y1
0
0
Iteration2
259
start
end
x = y1
0
1
Iteration2
260
start
end
x = y2
0
1
Chooseablock
261
start
end
x = y2
0
1
Iteration3
262
start
end
x = y2
0
1
Iteration3
263
start
end
x = y2
0
2
Iteration3
264
start
end
x = y3
0
2
Whydoesn’tthisterminate?• Valuescanincreasewithoutbound• Notethat“increase”referstothelatticeordering,nottheorderingonthenaturalnumbers
• Theheight ofasemilatticeisthelengthofthelongestincreasingsequenceinthatsemilattice
• Thedataflowframeworkisnotguaranteedtoterminateforsemilatticesofinfiniteheight
• Notethatasemilatticecanbeinfinitelylargebuthavefiniteheight– e.g.constantpropagation
265
0
1
2
3
4
...
Heightofalattice
• Anincreasingchainisasequenceofelementsz a a1 a a2 a …a ak– Thelengthofsuchachainisk
• Theheightofalatticeisthelengthofthemaximalincreasingchain
• Forlivenesswithn programvariables:– _ v1_ v1,v2_ …_ v1,…,vn
• Foravailableexpressionsitisthenumberofexpressionsoftheforma=bopc– Forn programvariablesandm operatortypes:m$n3
266
Anothernon-terminatinganalysis
• Thisanalysisworksonafinite-heightsemilattice,butwillnotterminateoncertainCFGs:
• Direction: Forward• Domain: Booleanvaluestrue andfalse• Joinoperator:LogicalOR• Transferfunction:LogicalNOT• Initialvalue:false
267
Anon-terminatinganalysis
268
start
end
x = y
Initialization
269
start
end
x = yfalse
false
Fixed-pointiteration
270
start
end
x = yfalse
false
Chooseablock
271
start
end
x = yfalse
false
Iteration1
272
start
end
x = yfalse
false
false
Iteration1
273
start
end
x = ytrue
false
false
Iteration2
274
start
end
x = ytrue
false
true
Iteration2
275
start
end
x = yfalse
false
true
Iteration3
276
start
end
x = yfalse
false
false
Iteration3
277
start
end
x = ytrue
false
false
Whydoesn’titterminate?• Valuescanloopindefinitely• Intuitively,thejoinoperatorkeepspullingvaluesup
• Ifthetransferfunctioncankeeppushingvaluesbackdownagain,thenthevaluesmightcycleforever
278
false
true
false
true
false
...
Whydoesn’titterminate?• Valuescanloopindefinitely• Intuitively,thejoinoperatorkeepspullingvaluesup
• Ifthetransferfunctioncankeeppushingvaluesbackdownagain,thenthevaluesmightcycleforever
• Howcanwefixthis?
279
false
true
false
true
false
...
Monotonetransferfunctions
• Atransferfunctionf ismonotone iffifxb y,thenf(x)b f(y)
• Intuitively,ifyouknowlessinformationaboutaprogrampoint,youcan't“gainback”moreinformationaboutthatprogrampoint
• Manytransferfunctionsaremonotone,includingthoseforlivenessandconstantpropagation
• Note:Monotonicity doesnotmeanthatxb f(x)– (Thisisadifferentpropertycalledextensivity)
280
Livenessandmonotonicity
• Atransferfunctionf ismonotone iffifxb y,thenf(x)b f(y)
• Recallourtransferfunctionfora=b+cis– fa=b+c(V)=(V– a)4 b,c
• Recallthatourjoinoperatorissetunionandinducesanorderingrelationship
Xb Yiff X`Y• Isthismonotone?
281
Isconstantpropagationmonotone?• Atransferfunctionf ismonotone iff
ifxb y,thenf(x)b f(y)• Recallourtransferfunctions
– fx=k(V)=V|xhk (updateVbymappingxtok)– fx=a+b(V)=V|xhNot-a-Constant (assignNot-a-Constant)
• Isthismonotone?
282Undefined
0-1-2 1 2 ......
Not-a-constant
Thegrandresult
• Theorem: Adataflowanalysiswithafinite-heightsemilattice andfamilyofmonotonetransferfunctions alwaysterminates
• Proofsketch:– Thejoinoperatorcanonlybringvaluesup– Transferfunctionscanneverlowervaluesbackdownbelowwheretheywereinthepast(monotonicity)
– Valuescannotincreaseindefinitely(finiteheight)
283
An“optimality”result
• Atransferfunctionf isdistributiveiff(a 7 b)=f(a)7 f(b)
foreverydomainelementsa andb• Ifalltransferfunctionsaredistributivethenthefixed-pointsolutionisthesolutionthatwouldbecomputedbyjoiningresultsfromall(potentiallyinfinite)control-flowpaths– Joinoverallpaths
• Optimalifweignoreprogramconditions
284
An“optimality”result
• Atransferfunctionf isdistributiveiff(a 7 b)=f(a)7 f(b)
foreverydomainelementsa andb• Ifalltransferfunctionsaredistributivethenthefixed-pointsolutionisequaltothesolutioncomputedbyjoiningresultsfromall(potentiallyinfinite)control-flowpaths– Joinoverallpaths
• Optimalifwepretendallcontrol-flowpathscanbeexecutedbytheprogram
• Whichanalysesusedistributivefunctions?
285
Loopoptimizations• Mostofaprogram’scomputationsaredoneinside
loops– Focusoptimizationseffortonloops
• Theoptimizationswe’veseensofarareindependentofthecontrolstructure
• Someoptimizationsarespecializedtoloops– Loop-invariantcodemotion– (Strengthreductionviainductionvariables)
• Requireanothertypeofanalysistofindoutwhereexpressionsgettheirvaluesfrom– Reachingdefinitions
• (Alsousefulforimprovingregisterallocation)
286
Loopinvariantcomputation
287
y = t * 4x < y + z
endx = x + 1
start
y = …t = …z = …
Loopinvariantcomputation
288
y = t * 4x < y + z
endx = x + 1
start
y = …t = …z = …
t*4andy+zhavesamevalueoneachiteration
Codehoisting
289
x < w
endx = x + 1
start
y = …t = …z = …y = t * 4w = y + z
Whatreasoningdidweuse?
290
y = t * 4x < y + z
endx = x + 1
start
y = …t = …z = …
yisdefinedinsideloopbutitisloopinvariantsincet*4isloop-invariant
Bothtandzaredefinedonlyoutsideofloop
constantsaretriviallyloop-invariant
Whataboutnow?
291
y=t*4x<y+z
endx=x+1t=t+1
start
y=…t=…z=…
Nowtisnotloop-invariantandsoaret*4andy
Loop-invariantcodemotion• d:t=a1 opa2
– d isaprogramlocation• a1 opa2loop-invariant (foraloopL)ifcomputesthe
samevalueineachiteration– Hardtoknowingeneral
• Conservativeapproximation– Eachai isaconstant,or– Alldefinitionsofai thatreachd areoutsideL,or– Onlyonedefinitionofof ai reachesd,andisloop-invariant
itself• Transformation:hoisttheloop-invariantcodeoutside
oftheloop
292
Reachingdefinitionsanalysis• Adefinitiond:t=…reaches aprogramlocationifthereisa
pathfromthedefinitiontotheprogramlocation,alongwhichthedefinedvariableisneverredefined
293
Reachingdefinitionsanalysis• Adefinitiond:t=…reaches aprogramlocationifthereisa
pathfromthedefinitiontotheprogramlocation,alongwhichthedefinedvariableisneverredefined
• Direction: Forward• Domain: setsofprogramlocationsthataredefinitions`• Joinoperator: union• Transferfunction:
fd:a=bopc(RD) =(RD- defs(a))4 dfd:not-a-def(RD) =RD
– Wheredefs(a)isthesetoflocationsdefininga (statementsoftheforma=...)
• Initialvalue:
294
Reachingdefinitionsanalysis
295
d4: y = t * 4
d4:x < y + z
d6: x = x + 1
d1: y = …
d2: t = …
d3: z = …
start
end
Reachingdefinitionsanalysis
296
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
end
Initialization
297
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
end
Iteration1
298
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
end
Iteration1
299
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1
d1, d2
d1, d2, d3
end
Iteration2
300
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1
d1, d2
d1, d2, d3
Iteration2
301
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3
d1
d1, d2
d1, d2, d3
Iteration2
302
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3
d1
d1, d2
d1, d2, d3
d2, d3, d4
Iteration2
303
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3
d1
d1, d2
d1, d2, d3
d2, d3, d4
d2, d3, d4
Iteration3
304
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3
d2, d3, d4
d1
d1, d2
d1, d2, d3
d2, d3, d4
d2, d3, d4
Iteration3
305
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3
d2, d3, d4
d1
d1, d2
d1, d2, d3
d2, d3, d4
d2, d3, d4
d2, d3, d4, d5
Iteration4
306
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3
d2, d3, d4
d1
d1, d2
d1, d2, d3
d2, d3, d4
d2, d3, d4
d2, d3, d4, d5
Iteration4
307
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3, d4, d5
d2, d3, d4
d1
d1, d2
d1, d2, d3
d2, d3, d4
d2, d3, d4
d2, d3, d4, d5
Iteration4
308
d4: y = t * 4
x < y + z end
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
d1, d2, d3, d4, d5
d2, d3, d4
d1
d1, d2
d1, d2, d3
d2, d3, d4, d5
d2, d3, d4, d5
d2, d3, d4, d5
Iteration5
309
end
start
d1: y = …
d2: t = …
d3: z = …
d2, d3, d4, d5
d1
d1, d2
d1, d2, d3
d5: x = x + 1d2, d3, d4
d2, d3, d4, d5
d4: y = t * 4
x < y + z
d1, d2, d3, d4, d5
d2, d3, d4, d5
d2, d3, d4, d5
Iteration6
310
end
start
d1: y = …
d2: t = …
d3: z = …
d2, d3, d4, d5
d1
d1, d2
d1, d2, d3
d5: x = x + 1d2, d3, d4, d5
d2, d3, d4, d5
d4: y = t * 4
x < y + z
d1, d2, d3, d4, d5
d2, d3, d4, d5
d2, d3, d4, d5
Whichexpressionsareloopinvariant?
311
tisdefinedonlyind2– outsideofloop
zisdefinedonlyind3– outsideofloop
yisdefinedonlyind4– insideofloopbutdependsontand4,bothloop-invariant
start
d1: y = …
d2: t = …
d3: z = …
d1
d1, d2
d1, d2, d3
endd2, d3, d4, d5
d5: x = x + 1d2, d3, d4, d5
d2, d3, d4, d5
d4: y = t * 4
x < y + z
d1, d2, d3, d4, d5
d2, d3, d4, d5
d2, d3, d4, d5xisdefinedonlyind5–insideofloopsoisnotaloop-invariant
Inferringloop-invariantexpressions
• Forastatements oftheformt=a1 opa2• Avariableai isimmediatelyloop-invariantifallreachingdefinitionsIN[s]=d1,…,dkforai areoutsideoftheloop
• LOOP-INV=immediatelyloop-invariantvariablesandconstantsLOOP-INV=LOOP-INV4 x|d:x=a1 opa2, disintheloop,andbotha1 anda2areinLOOP-INV– Iterateuntilfixed-point
• Anexpressionisloop-invariantifalloperandsareloop-invariants
312
ComputingLOOP-INV
313
end
start
d1:y=…
d2:t=…
d3:z=…
d2,d3,d4
d1
d1,d2
d1,d2,d3
d4:y=t*4
x<y+z
d5:x=x+1
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
(immediately)LOOP-INV=T
ComputingLOOP-INV
314
end
start
d1:y=…
d2:t=…
d3:z=…
d2,d3,d4
d1
d1,d2
d1,d2,d3
d4:y=t*4
x<y+z
d5:x=x+1
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
(immediately)LOOP-INV=t
ComputingLOOP-INV
315
end
start
d1:y=…
d2:t=…
d3:z=…
d2,d3,d4
d1
d1,d2
d1,d2,d3
d4:y=t*4
x<y+z
d5:x=x+1
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
(immediately)LOOP-INV=t,z
ComputingLOOP-INV
316
end
start
d1:y=…
d2:t=…
d3:z=…
d2,d3,d4
d1
d1,d2
d1,d2,d3
d4:y=t*4
x<y+z
d5:x=x+1
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
(immediately)LOOP-INV=t,z
ComputingLOOP-INV
317
end
start
d1:y=…
d2:t=…
d3:z=…
d2,d3,d4
d1
d1,d2
d1,d2,d3
d4:y=t*4
x<y+z
d5:x=x+1
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
(immediately)LOOP-INV=t,z
318
end
start
d1:y=…
d2:t=…
d3:z=…
d2,d3,d4
d1
d1,d2
d1,d2,d3LOOP-INV=t,z
d4:y=t*4
x<y+z
d5:x=x+1
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
ComputingLOOP-INV
ComputingLOOP-INV
319
d4:y=t*4
x<y+z end
d5:x=x+1
start
d1:y=…
d2:t=…
d3:z=…
d1,d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4
d1
d1,d2
d1,d2,d3
d2,d3,d4,d5
d2,d3,d4,d5
d2,d3,d4,d5
LOOP-INV=t,z,y
Inductionvariables
320
while (i < x) j = a + 4 * ia[j] = ji = i + 1
i isincrementedbyaloop-invariantexpressiononeachiteration– thisiscalledaninductionvariable
jisalinearfunctionoftheinductionvariablewithmultiplier4
Strength-reduction
321
j = a + 4 * iwhile (i < x)
j = j + 4a[j] = ji = i + 1
Prepareinitialvalue
Incrementbymultiplier
TheEnd