Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A...
Transcript of Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A...
![Page 1: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/1.jpg)
CompilationLecture7
IR+OptimizationsNoamRinetzky
1
![Page 2: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/2.jpg)
BasicCompilerPhases
2
Sourceprogram(string)
.EXE
lexicalanalysis
syntaxanalysis
semanticanalysis
Codegeneration
Assembler/Linker
Tokens
Abstractsyntaxtree
Assembly
FramemanagerControlFlowGraph
![Page 3: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/3.jpg)
IROptimization
3
![Page 4: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/4.jpg)
Optimizationpoints
sourcecode
Frontend IR Code
generatortargetcode
Userprofileprogramchangealgorithm
Compilerintraprocedural IRInterprocedural IRIRoptimizations
Compilerregisterallocationinstructionselection
peepholetransformations
now 4
![Page 5: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/5.jpg)
IROptimization
• Makingcodebetter
5
![Page 6: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/6.jpg)
IROptimization
• Makingcode“better”
6
![Page 7: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/7.jpg)
“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
![Page 8: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/8.jpg)
Butwhatabout…
a:=1+2;y:=a+b;x:=a+b+8;z:=b+a;
a:=a+1;w:=a+b;
8
![Page 9: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/9.jpg)
OverviewofIRoptimization
• FormalismsandTerminology– Control-flowgraphs– Basicblocks
• Localoptimizations– Speedingupsmallpiecesofaprocedure
• Globaloptimizations– Speedingupprocedureasawhole
• Thedataflowframework– Definingandimplementingawideclassofoptimizations
9
![Page 10: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/10.jpg)
ProgramAnalysis
• Inordertooptimizeaprogram,thecompilerhastobeabletoreasonaboutthepropertiesofthatprogram
• Ananalysisiscalledsound ifitneverassertsanincorrectfactaboutaprogram
• Alltheanalyseswewilldiscussinthisclassaresound– (Why?)
10
![Page 11: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/11.jpg)
Soundnessint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x holdssome
integervalue”
11
![Page 12: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/12.jpg)
Soundnessint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x iseither137
or42”
12
![Page 13: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/13.jpg)
(Un)Soundnessint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x is137”
13
![Page 14: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/14.jpg)
Soundness&Precisionint x;int y;
if (y < 5)x = 137;
elsex = 42;
Print(x);
“Atthispointintheprogram,x iseither137,
42,or271”
14
![Page 15: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/15.jpg)
Semantics-preservingoptimizations
• Anoptimizationissemantics-preserving ifitdoesnotalterthesemanticsoftheoriginalprogram
• Examples:– Eliminatingunnecessarytemporaryvariables– Computingvaluesthatareknownstaticallyatcompile-time
insteadofruntime– Evaluatingconstantexpressionsoutsideofaloopinsteadof
inside• Non-examples:
– Replacingbubblesortwithquicksort (why?)– Theoptimizationswewillconsiderinthisclassareall
semantics-preserving
15
![Page 16: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/16.jpg)
AformalismforIRoptimization
• Everyphaseofthecompilerusessomenewabstraction:– Scanningusesregularexpressions– ParsingusesCFGs– Semanticanalysisusesproofsystemsandsymboltables
– IRgenerationusesASTs• Inoptimization,weneedaformalismthatcapturesthestructureofaprograminawayamenabletooptimization
16
![Page 17: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/17.jpg)
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
![Page 18: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/18.jpg)
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
![Page 19: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/19.jpg)
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
![Page 20: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/20.jpg)
Basicblocks
• Abasicblock isasequenceofIRinstructionswhere– Thereisexactlyonespotwherecontrolentersthesequence,whichmustbeatthestartofthesequence
– Thereisexactlyonespotwherecontrolleavesthesequence,whichmustbeattheendofthesequence
• Informally,asequenceofinstructionsthatalwaysexecuteasagroup
20
![Page 21: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/21.jpg)
Control-FlowGraphs
• Acontrol-flowgraph(CFG)isagraphofthebasicblocksinafunction
• ThetermCFGisoverloaded– fromhereonout,we'llmean“control-flowgraph”andnot“contextfreegrammar”
• Eachedgefromonebasicblocktoanotherindicatesthatcontrolcanflowfromtheendofthefirstblocktothestartofthesecondblock
• Thereisadedicatednodeforthestartandendofafunction
21
![Page 22: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/22.jpg)
Typesofoptimizations
• Anoptimizationislocal ifitworksonjustasinglebasicblock
• Anoptimizationisglobal ifitworksonanentirecontrol-flowgraph
• Anoptimizationisinterprocedural ifitworksacrossthecontrol-flowgraphsofmultiplefunctions– Wewon'ttalkaboutthisinthiscourse
22
![Page 23: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/23.jpg)
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
![Page 24: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/24.jpg)
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
![Page 25: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/25.jpg)
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
![Page 26: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/26.jpg)
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
![Page 27: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/27.jpg)
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
![Page 28: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/28.jpg)
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
![Page 29: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/29.jpg)
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
![Page 30: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/30.jpg)
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
![Page 31: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/31.jpg)
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
![Page 32: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/32.jpg)
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
![Page 33: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/33.jpg)
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
![Page 34: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/34.jpg)
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
![Page 35: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/35.jpg)
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
![Page 36: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/36.jpg)
LocalOptimizations
36
![Page 37: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/37.jpg)
Optimizationpath
IR Control-FlowGraph
CFGbuilder
ProgramAnalysis
AnnotatedCFG
OptimizingTransformation
TargetCode
CodeGeneration
(+optimizations)
donewithIR
optimizations
IRoptimizations
37
![Page 38: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/38.jpg)
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
![Page 39: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/39.jpg)
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
![Page 40: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/40.jpg)
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
![Page 41: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/41.jpg)
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
![Page 42: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/42.jpg)
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
![Page 43: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/43.jpg)
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
![Page 44: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/44.jpg)
CommonSubexpressionElimination
• Ifwehavetwovariableassignmentsv1=aopb…v2=aopb
• andthevaluesofv1,a,andbhavenotchangedbetweentheassignments,rewritethecodeasv1=aopb…v2=v1
• Eliminatesuselessrecalculation• Pavesthewayforlateroptimizations
44
![Page 45: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/45.jpg)
CommonSubexpressionElimination
• Ifwehavetwovariableassignmentsv1=aopb[or:v1=a]…v2=aopb[or:v2=a]
• andthevaluesofv1,a,andbhavenotchangedbetweentheassignments,rewritethecodeasv1=aopb[or:v1=a]…v2=v1
• Eliminatesuselessrecalculation• Pavesthewayforlateroptimizations
45
![Page 46: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/46.jpg)
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);
![Page 47: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/47.jpg)
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);
![Page 48: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/48.jpg)
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);
![Page 49: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/49.jpg)
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);
![Page 50: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/50.jpg)
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);
![Page 51: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/51.jpg)
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);
![Page 52: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/52.jpg)
CopyPropagation
• Ifwehaveavariableassignmentv1=v2thenaslongasv1andv2arenotreassigned,wecanrewriteexpressionsoftheforma=…v1…asa=…v2…providedthatsucharewriteislegal
52
![Page 53: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/53.jpg)
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);
![Page 54: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/54.jpg)
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);
![Page 55: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/55.jpg)
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);
![Page 56: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/56.jpg)
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);
![Page 57: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/57.jpg)
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);
![Page 58: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/58.jpg)
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);
![Page 59: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/59.jpg)
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);
![Page 60: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/60.jpg)
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);
![Page 61: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/61.jpg)
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);
![Page 62: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/62.jpg)
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?
![Page 63: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/63.jpg)
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);
![Page 64: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/64.jpg)
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);
![Page 65: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/65.jpg)
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);
![Page 66: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/66.jpg)
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);
![Page 67: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/67.jpg)
DeadCodeElimination
• Anassignmenttoavariableviscalleddeadifthevalueofthatassignmentisneverreadanywhere
• DeadcodeeliminationremovesdeadassignmentsfromIR
• Determiningwhetheranassignmentisdeaddependsonwhatvariableisbeingassignedtoandwhenit'sbeingassigned
67
![Page 68: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/68.jpg)
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;
![Page 69: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/69.jpg)
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);
![Page 70: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/70.jpg)
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
![Page 71: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/71.jpg)
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
![Page 72: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/72.jpg)
Applyinglocaloptimizations
• Thedifferentoptimizationswe'veseensofaralltakecareofjustasmallpieceoftheoptimization
• Commonsubexpressioneliminationeliminatesunnecessarystatements
• Copypropagationhelpsidentifydeadcode• Deadcodeeliminationremovesstatementsthatarenolongerneeded
• Togetmaximumeffect,wemayhavetoapplytheseoptimizationsnumeroustimes
72
![Page 73: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/73.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = a * a;d = b + c;e = b + b;
73
![Page 74: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/74.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = a * a;d = b + c;e = b + b;
Whichoptimizationshouldweapplyhere?
74
![Page 75: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/75.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + c;e = b + b;
Commonsub-expressionelimination
Whichoptimizationshouldweapplyhere?
75
![Page 76: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/76.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + c;e = b + b;
Whichoptimizationshouldweapplyhere?
76
![Page 77: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/77.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + b;e = b + b;
Whichoptimizationshouldweapplyhere?
Copypropagation
77
![Page 78: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/78.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + b;e = b + b;
Whichoptimizationshouldweapplyhere?
78
![Page 79: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/79.jpg)
Applyinglocaloptimizationsexample
b = a * a;c = b;d = b + b;e = d;
Whichoptimizationshouldweapplyhere?
Commonsub-expressionelimination(again)
79
![Page 80: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/80.jpg)
Othertypesoflocaloptimizations
• ArithmeticSimplification– Replace“hard”operationswitheasierones– e.g.rewritex = 4 * a; asx = a << 2;
• ConstantFolding– Evaluateexpressionsatcompile-timeiftheyhaveaconstantvalue.
– e.g.rewritex = 4 * 5; asx = 20;
80
![Page 81: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/81.jpg)
Optimizationsandanalyses
• Mostoptimizationsareonlypossiblegivensomeanalysisoftheprogram'sbehavior
• Inordertoimplementanoptimization,wewilltalkaboutthecorrespondingprogramanalyses
81
![Page 82: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/82.jpg)
Availableexpressions
• Bothcommonsubexpressioneliminationandcopypropagationdependonananalysisoftheavailableexpressionsinaprogram
• Anexpressioniscalledavailable ifsomevariableintheprogramholdsthevalueofthatexpression
• Incommonsubexpressionelimination,wereplaceanavailableexpressionbythevariableholdingitsvalue
• Incopypropagation,wereplacetheuseofavariablebytheavailableexpressionitholds
82
![Page 83: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/83.jpg)
Findingavailableexpressions
• Initially,noexpressionsareavailable• Wheneverweexecuteastatementa=bop c:– Anyexpressionholdinga isinvalidated– Theexpressiona=bop cbecomesavailable
• Idea:Iterateacrossthebasicblock,beginningwiththeemptysetofexpressionsandupdatingavailableexpressionsateachvariable
83
![Page 84: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/84.jpg)
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
![Page 85: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/85.jpg)
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
![Page 86: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/86.jpg)
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
![Page 87: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/87.jpg)
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
![Page 88: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/88.jpg)
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
![Page 89: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/89.jpg)
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
![Page 90: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/90.jpg)
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
![Page 91: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/91.jpg)
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
![Page 92: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/92.jpg)
Livevariables
• Theanalysiscorrespondingtodeadcodeeliminationiscalledlivenessanalysis
• Avariableislive atapointinaprogramiflaterintheprogramitsvaluewillbereadbeforeitiswrittentoagain
• Deadcodeeliminationworksbycomputinglivenessforeachvariable,theneliminatingassignmentstodeadvariables
92
![Page 93: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/93.jpg)
Computinglivevariables• Toknowifavariablewillbeusedatsomepoint,weiterateacrossthestatementsinabasicblockinreverseorder
• Initially,somesmallsetofvaluesareknowntobelive(whichonesdependsontheparticularprogram)
• Whenweseethestatementa=bopc:– Justbeforethestatement,aisnotalive,sinceitsvalueisabouttobeoverwritten
– Justbeforethestatement,bothbandcarealive,sincewe'reabouttoreadtheirvalues
– (whatifwehavea=a+b?) 93
![Page 94: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/94.jpg)
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
![Page 95: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/95.jpg)
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
![Page 96: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/96.jpg)
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
![Page 97: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/97.jpg)
LivenessanalysisIIa = b;
d = a + b;
e = d;
d = a; b, d
a, b
a, b, d
a, b
b
Whichstatementsaredead?
97
![Page 98: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/98.jpg)
LivenessanalysisIIa = b;
d = a + b;
e = d;
d = a; b, d
a, b
a, b, d
a, b
b
Which statements are dead?
98
![Page 99: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/99.jpg)
Deadcodeeliminationa = b;
d = a + b;
e = d;
d = a; b, d
a, b
a, b, d
a, b
b
Which statements are dead?
99
![Page 100: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/100.jpg)
Deadcodeeliminationa = b;
d = a + b;
d = a; b, d
a, b
a, b, d
a, b
b
100
![Page 101: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/101.jpg)
LivenessanalysisIIIa = b;
d = a + b;
d = a; b, d
a, b
a, b
b
Whichstatementsaredead?
101
![Page 102: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/102.jpg)
Deadcodeeliminationa = b;
d = a + b;
d = a; b, d
a, b
a, b
b
Whichstatementsaredead?
102
![Page 103: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/103.jpg)
Deadcodeeliminationa = b;
d = a; b, d
a, b
a, b
b
103
![Page 104: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/104.jpg)
Deadcodeeliminationa = b;
d = a;
104
Ifwefurtherapplycopypropagationthisstatementcanbeeliminatedtoo
![Page 105: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/105.jpg)
Acombinedalgorithm
• Startwithinitiallivevariablesatendofblock
• Traversestatementsfromendtobeginning• Foreachstatement
– Ifassignstodeadvariables– eliminateit– Otherwise,computelivevariablesbeforestatementandcontinueinreverse
105
![Page 106: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/106.jpg)
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
106
![Page 107: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/107.jpg)
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
f = e; b, d
107
![Page 108: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/108.jpg)
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
f = e; b, d
108
![Page 109: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/109.jpg)
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
b, d 109
![Page 110: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/110.jpg)
Acombinedalgorithma = b;
c = a;
d = a + b;
e = d;
d = a;
b, d
a, b
110
![Page 111: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/111.jpg)
Acombinedalgorithm
111
a = b;
c = a;
d = a + b;
e = d;
d = a;
b, d
a, b
![Page 112: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/112.jpg)
Acombinedalgorithm
112
a = b;
c = a;
d = a + b;
d = a;
b, d
a, b
![Page 113: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/113.jpg)
Acombinedalgorithma = b;
c = a;
d = a + b;
d = a;
b, d
a, b
113
![Page 114: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/114.jpg)
Acombinedalgorithma = b;
c = a;
d = a;
b, d
a, b
114
![Page 115: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/115.jpg)
Acombinedalgorithma = b;
c = a;
d = a;
b, d
a, b
115
![Page 116: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/116.jpg)
Acombinedalgorithma = b;
d = a;
b, d
a, b
116
![Page 117: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/117.jpg)
Acombinedalgorithma = b;
d = a;
b, d
a, b
117
b
![Page 118: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/118.jpg)
Acombinedalgorithma = b;
d = a;
118
![Page 119: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/119.jpg)
High-levelgoals
• Generalizeanalysismechanism– Reusecommoningredientsformanyanalyses– Reuseproofsofcorrectness
• GeneralizefrombasicblockstoentireCFGs– Gofromlocaloptimizationstoglobaloptimizations
119
![Page 120: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/120.jpg)
ProgramAnalysis
• Reasonsaboutthebehavior ofaprogram• Ananalysisissound ifitonlyassertsancorrectfactsaboutaprogram
• Ananalysisisprecise ifitassertsallcorrectfacts(ofinterests)
• Soundanalysisallowsforsemantic-preservingoptimizations– “Moreprecise”analysesare“moreuseful”:mayenablemoreoptimizations 120
![Page 121: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/121.jpg)
Examples
• Availableexpressions,allows:ØCommonsub-expressionseliminationØCopypropagation
• Constantpropagation,allows:ØConstantfolding
• Liveness analysisØDead-codeeliminationØRegisterallocation
![Page 122: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/122.jpg)
Localvs.globaloptimizations
• Anoptimizationislocal ifitworksonjustasinglebasicblock
• Anoptimizationisglobal ifitworksonanentirecontrol-flowgraphofaprocedure
• Anoptimizationisinterprocedural ifitworksacrossthecontrol-flowgraphsofmultipleprocedure– Wewon'ttalkaboutthisinthiscourse
122
![Page 123: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/123.jpg)
Formalizinglocalanalyses
123
a = b + c
OutputValueVout
InputValueVin
Vout = fa=b+c(Vin)
TransferFunction
![Page 124: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/124.jpg)
AvailableExpressions
124
a = b + c
OutputValueVout
InputValueVin
Vout =(Vin \ e|econtainsa)4 a=b+c
Expressionsoftheformsa=…andx=…a…
![Page 125: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/125.jpg)
LiveVariables
125
a = b + c
OutputValueVout
InputValueVin
Vin = (Vout \ a) 4 b,c
Vin
Vout
![Page 126: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/126.jpg)
LiveVariables
126
a = b + c
OutputValueVout
InputValueVin
Vin = (Vout \ a) 4 b,c
Vin
Vout
![Page 127: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/127.jpg)
Informationforalocalanalysis
• Whatdirectionarewegoing?– Sometimesforward(availableexpressions)– Sometimesbackward(livenessanalysis)
• Howdoweupdateinformationafterprocessingastatement?– Whatarethenewsemantics?– Whatinformationdoweknowinitially?
127
![Page 128: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/128.jpg)
Formalizinglocalanalyses
• Defineananalysisofabasicblockasaquadruple(D,V,F,I)where– D isadirection(forwardsorbackwards)– V isasetofvaluestheprogramcanhaveatanypoint
– F isafamilyoftransferfunctionsdefiningthemeaningofanyexpressionasafunctionf:Vt V
– I istheinitialinformationatthetop(orbottom)ofabasicblock
128
![Page 129: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/129.jpg)
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
![Page 130: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/130.jpg)
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
![Page 131: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/131.jpg)
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
![Page 132: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/132.jpg)
Kill/Gen
132
![Page 133: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/133.jpg)
GlobalOptimizations
133
![Page 134: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/134.jpg)
High-levelgoals
• Generalizeanalysismechanism– Reusecommoningredientsformanyanalyses– Reuseproofsofcorrectness
• GeneralizefrombasicblockstoentireCFGs– Gofromlocaloptimizationstoglobaloptimizations
134
![Page 135: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/135.jpg)
Globalanalysis
• Aglobalanalysisisananalysisthatworksonacontrol-flowgraphasawhole
• Substantiallymorepowerfulthanalocalanalysis– (Why?)
• Substantiallymorecomplicatedthanalocalanalysis– (Why?)
135
![Page 136: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/136.jpg)
Localvs.globalanalysis• Manyoftheoptimizationsfromlocalanalysiscanstill
beappliedglobally– Commonsub-expressionelimination– Copypropagation– Deadcodeelimination
• Certainoptimizationsarepossibleinglobalanalysisthataren'tpossiblelocally:– e.g.codemotion:Movingcodefromonebasicblockinto
anothertoavoidcomputingvaluesunnecessarily• Exampleglobaloptimizations:
– Globalconstantpropagation– Partialredundancyelimination
136
![Page 137: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/137.jpg)
Loopinvariantcodemotionexample
137
while (t < 120) z = z + x - y;
w = x – y;while (t < 120) z = z + w;
valueofexpressionx– yisnotchangedbyloopbody
![Page 138: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/138.jpg)
Whyglobalanalysisishard
• Needtobeabletohandlemultiplepredecessors/successorsforabasicblock
• Needtobeabletohandlemultiplepathsthroughthecontrol-flowgraph,andmayneedtoiteratemultipletimestocomputethefinalvalue(buttheanalysisstillneedstoterminate!)
• Needtobeabletoassigneachbasicblockareasonabledefaultvalueforbeforewe'veanalyzedit
138
![Page 139: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/139.jpg)
Globaldeadcodeelimination
• Localdeadcodeeliminationneededtoknowwhatvariableswereliveonexitfromabasicblock
• Thisinformationcanonlybecomputedaspartofaglobalanalysis
• HowdowemodifyourlivenessanalysistohandleaCFG?
139
![Page 140: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/140.jpg)
CFGswithoutloops
140Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
![Page 141: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/141.jpg)
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?
![Page 142: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/142.jpg)
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
![Page 143: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/143.jpg)
CFGswithoutloops
143Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
![Page 144: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/144.jpg)
CFGswithoutloops
144Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
![Page 145: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/145.jpg)
Majorchanges– part1
• Inalocalanalysis,eachstatementhasexactlyonepredecessor
• Inaglobalanalysis,eachstatementmayhavemultiplepredecessors
• Aglobalanalysismusthavesomemeansofcombininginformationfromallpredecessorsofabasicblock
145
![Page 146: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/146.jpg)
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
![Page 147: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/147.jpg)
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
![Page 148: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/148.jpg)
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
![Page 149: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/149.jpg)
Majorchanges– part2
• Inalocalanalysis,thereisonlyonepossiblepaththroughabasicblock
• Inaglobalanalysis,theremaybemanypathsthroughaCFG
• Mayneedtorecomputevaluesmultipletimesasmoreinformationbecomesavailable
• Needtobecarefulwhendoingthisnottoloopinfinitely!– (Moreonthatlater)
• Canorderofcomputationaffectresult?149
![Page 150: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/150.jpg)
CFGswithloops• Uptothispoint,we'veconsideredloop-freeCFGs,whichhaveonlyfinitelymanypossiblepaths
• Whenweaddloopsintothepicture,thisisnolongertrue
• NotallpossibleloopsinaCFGcanberealizedintheactualprogram
150
IfZ x goto Top
x = 1;
Top:
x = 0;
x = 2;
![Page 151: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/151.jpg)
CFGswithloops• Uptothispoint,we'veconsideredloop-freeCFGs,whichhaveonlyfinitelymanypossiblepaths
• Whenweaddloopsintothepicture,thisisnolongertrue
• NotallpossibleloopsinaCFGcanberealizedintheactualprogram
• Soundapproximation:AssumethateverypossiblepaththroughtheCFGcorrespondstoavalidexecution– Includesallrealizablepaths,butsomeadditionalpathsaswell
– Maymakeouranalysislessprecise(butstillsound)– Makestheanalysisfeasible;we'llseehowlater
151
![Page 152: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/152.jpg)
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
?
![Page 153: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/153.jpg)
Majorchanges– part3
• Inalocalanalysis,thereisalwaysawelldefined“first”statementtobeginprocessing
• Inaglobalanalysiswithloops,everybasicblockmightdependoneveryotherbasicblock
• Tofixthis,weneedtoassigninitialvaluestoalloftheblocksintheCFG
153
![Page 154: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/154.jpg)
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
![Page 155: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/155.jpg)
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
![Page 156: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/156.jpg)
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
![Page 157: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/157.jpg)
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
![Page 158: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/158.jpg)
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
![Page 159: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/159.jpg)
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
![Page 160: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/160.jpg)
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
![Page 161: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/161.jpg)
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
![Page 162: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/162.jpg)
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
![Page 163: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/163.jpg)
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
![Page 164: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/164.jpg)
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
![Page 165: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/165.jpg)
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
![Page 166: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/166.jpg)
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
![Page 167: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/167.jpg)
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
![Page 168: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/168.jpg)
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
![Page 169: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/169.jpg)
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
![Page 170: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/170.jpg)
Summaryofdifferences
• Needtobeabletohandlemultiplepredecessors/successorsforabasicblock
• Needtobeabletohandlemultiplepathsthroughthecontrol-flowgraph,andmayneedtoiteratemultipletimestocomputethefinalvalue– Buttheanalysisstillneedstoterminate!
• Needtobeabletoassigneachbasicblockareasonabledefaultvalueforbeforewe'veanalyzedit
170
![Page 171: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/171.jpg)
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
![Page 172: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/172.jpg)
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
![Page 173: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/173.jpg)
Whydoesthiswork?• Toshowcorrectness,weneedtoshowthat
– Thealgorithmeventuallyterminates,and– Whenitterminates,ithasasoundanswer
• Terminationargument:– Onceavariableisdiscoveredtobeliveduringsomepointofthe
analysis,italwaysstayslive– Onlyfinitelymanyvariablesandfinitelymanyplaceswherea
variablecanbecomelive• Soundnessargument(sketch):
– Eachindividualrule,appliedtosomeset,correctlyupdateslivenessinthatset
– Whencomputingtheunionofthesetoflivevariables,avariableisonlyliveifitwasliveonsomepathleavingthestatement
173
![Page 174: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/174.jpg)
AbstractInterpretation
• Theoreticalfoundationsofprogramanalysis
• Cousot andCousot 1977
• Abstractmeaningofprograms– Executedatcompiletime
174
![Page 175: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/175.jpg)
Anotherviewoflocaloptimization
• Inlocaloptimization,wewanttoreasonaboutsomepropertyoftheruntimebehavioroftheprogram
• Couldweruntheprogramandjustwatchwhathappens?
• Idea:Redefinethesemanticsofourprogramminglanguagetogiveusinformationaboutouranalysis
175
![Page 176: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/176.jpg)
Propertiesoflocalanalysis
• Theonlywaytofindoutwhataprogramwillactuallydoistorunit
• Problems:– Theprogrammightnotterminate– Theprogrammighthavesomebehaviorwedidn'tseewhenweranitonaparticularinput
• However,thisisnotaprobleminsideabasicblock– Basicblockscontainnoloops– Thereisonlyonepaththroughthebasicblock
176
![Page 177: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/177.jpg)
Assigningnewsemantics
• Example:AvailableExpressions• Redefinethestatementa=b+ctomean“anowholdsthevalueofb+c,andanyvariableholdingthevalueaisnowinvalid”
• Runtheprogramassumingthesenewsemantics
• Treattheoptimizerasaninterpreterforthesenewsemantics
177
![Page 178: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/178.jpg)
Theorytotherescue
• Buildingupallofthemachinerytodesignthisanalysiswastricky
• Thekeyideas,however,aremostlyindependentoftheanalysis:– Weneedtobeabletocomputefunctionsdescribingthebehaviorofeachstatement
– Weneedtobeabletomergeseveralsubcomputationstogether
– Weneedaninitialvalueforallofthebasicblocks• Thereisabeautifulformalismthatcapturesmanyoftheseproperties
178
![Page 179: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/179.jpg)
Joinsemilattices• Ajoinsemilatticeisaorderingdefinedonasetof
elements• Anytwoelementshavesomejointhatisthesmallest
elementlargerthanbothelements• Thereisauniquebottomelement,whichissmaller
thanallotherelements• Intuitively:
– Thejoinoftwoelementsrepresentscombininginformationfromtwoelementsbyanoverapproximation
• Thebottomelementrepresents“noinformationyet”or“theleastconservativepossibleanswer”
179
![Page 180: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/180.jpg)
Joinsemilatticeforliveness
180
a b c
a, b a, c b, c
a, b, c
Bottomelement
![Page 181: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/181.jpg)
Whatisthejoinofbandc?
181
a b c
a, b a, c b, c
a, b, c
![Page 182: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/182.jpg)
Whatisthejoinofbandc?
182
a b c
a, b a, c b, c
a, b, c
![Page 183: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/183.jpg)
Whatisthejoinofbanda,c?
183
a b c
a, b a, c b, c
a, b, c
![Page 184: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/184.jpg)
Whatisthejoinofbanda,c?
184
a b c
a, b a, c b, c
a, b, c
![Page 185: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/185.jpg)
Whatisthejoinofaanda,b?
185
a b c
a, b a, c b, c
a, b, c
![Page 186: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/186.jpg)
Whatisthejoinofaanda,b?
186
a b c
a, b a, c b, c
a, b, c
![Page 187: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/187.jpg)
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
![Page 188: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/188.jpg)
Joinsemilatticesandordering
188
a b c
a, b a, c b, c
a, b, cGreater
Lower
![Page 189: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/189.jpg)
Joinsemilatticesandordering
189
a b c
a, b a, c b, c
a, b, cLeastprecise
Mostprecise
![Page 190: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/190.jpg)
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
![Page 191: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/191.jpg)
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
![Page 192: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/192.jpg)
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
![Page 193: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/193.jpg)
Semilatticesandprogramanalysis
• Semilatticesnaturallysolvemanyoftheproblemsweencounteringlobalanalysis
• Howdowecombineinformationfrommultiplebasicblocks?
• Whatvaluedowegivetobasicblockswehaven'tseenyet?
• Howdoweknowthatthealgorithmalwaysterminates?
193
![Page 194: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/194.jpg)
Semilatticesandprogramanalysis
• Semilatticesnaturallysolvemanyoftheproblemsweencounteringlobalanalysis
• Howdowecombineinformationfrommultiplebasicblocks?– Takethejoinofallinformationfromthoseblocks
• Whatvaluedowegivetobasicblockswehaven'tseenyet?– Usethebottomelement
• Howdoweknowthatthealgorithmalwaysterminates?– Actually,westilldon't!Moreonthatlater
194
![Page 195: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/195.jpg)
Semilatticesandprogramanalysis
• Semilatticesnaturallysolvemanyoftheproblemsweencounteringlobalanalysis
• Howdowecombineinformationfrommultiplebasicblocks?– Takethejoinofallinformationfromthoseblocks
• Whatvaluedowegivetobasicblockswehaven'tseenyet?– Usethebottomelement
• Howdoweknowthatthealgorithmalwaysterminates?– Actually,westilldon't!Moreonthatlater
195
![Page 196: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/196.jpg)
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
![Page 197: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/197.jpg)
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
![Page 198: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/198.jpg)
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
![Page 199: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/199.jpg)
Thedataflowframework
• Thisformofanalysisiscalledthedataflowframework
• Canbeusedtoeasilyproveananalysisissound
• Withcertainrestrictions,canbeusedtoprovethatananalysiseventuallyterminates– Again,moreonthatlater
199
![Page 200: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/200.jpg)
Globalconstantpropagation
• Constantpropagationisanoptimizationthatreplaceseachvariablethatisknowntobeaconstantvaluewiththatconstant
• Anelegantexampleofthedataflowframework
200
![Page 201: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/201.jpg)
Globalconstantpropagation
201
exit x = 4;
z = x;
w = x;
y = x; z = y;
x = 6;entry
![Page 202: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/202.jpg)
Globalconstantpropagation
202
exit x = 4;
z = x;
w = x;
y = x; z = y;
x = 6;entry
![Page 203: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/203.jpg)
Globalconstantpropagation
203
exit x = 4;
z = x;
w = 6;
y = 6; z = y;
x = 6;entry
![Page 204: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/204.jpg)
Constantpropagationanalysis
• Inordertodoaconstantpropagation,weneedtotrackwhatvaluesmightbeassignedtoavariableateachprogrampoint
• Everyvariablewilleither– Neverhaveavalueassignedtoit,– Haveasingleconstantvalueassignedtoit,– Havetwoormoreconstantvaluesassignedtoit,or– Haveaknownnon-constantvalue.– OuranalysiswillpropagatethisinformationthroughoutaCFGtoidentifylocationswhereavalueisconstant
204
![Page 205: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/205.jpg)
Propertiesofconstantpropagation
• Fornow,considerjustsomesinglevariablex• Ateachpointintheprogram,weknowoneofthree
thingsaboutthevalueofx:– x isdefinitelynotaconstant,sinceit'sbeenassignedtwo
valuesorassignedavaluethatweknowisn'taconstant– x isdefinitelyaconstantandhasvaluek– Wehaveneverseenavalueforx
• Notethatthefirstandlastofthesearenot thesame!– Thefirstonemeansthattheremaybeawayforx tohave
multiplevalues– Thelastonemeansthatx neverhadavalueatall
205
![Page 206: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/206.jpg)
Definingajoinoperator• ThejoinofanytwodifferentconstantsisNot-a-Constant
– (Ifthevariablemighthavetwodifferentvaluesonentrytoastatement,itcannotbeaconstant)
• ThejoinofNotaConstantandanyothervalueisNot-a-Constant– (Ifonsomepaththevalueisknownnottobeaconstant,thenon
entrytoastatementitsvaluecan'tpossiblybeaconstant)• ThejoinofUndefined andanyothervalueisthatothervalue
– (Ifx hasnovalueonsomepathanddoeshaveavalueonsomeotherpath,wecanjustpretenditalwayshadtheassignedvalue)
206
![Page 207: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/207.jpg)
Asemilatticeforconstantpropagation• Onepossiblesemilatticeforthisanalysisisshownhere(foreachvariable):
207
Undefined
0-1-2 1 2 ......
Not-a-constant
The lattice is infinitely wide
![Page 208: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/208.jpg)
Asemilatticeforconstantpropagation• Onepossiblesemilatticeforthisanalysisisshownhere(foreachvariable):
208
Undefined
0-1-2 1 2 ......
Not-a-constant
• Note:• ThejoinofanytwodifferentconstantsisNot-a-Constant• ThejoinofNotaConstantandanyothervalueisNot-a-Constant• ThejoinofUndefined andanyothervalueisthatothervalue
![Page 209: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/209.jpg)
Globalconstantpropagation
209
exit x = 4;Undefined
z = x;Undefined
w = x;
y = x; z = y;
x = 6;entry
![Page 210: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/210.jpg)
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
![Page 211: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/211.jpg)
Globalconstantpropagation
211
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
![Page 212: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/212.jpg)
Globalconstantpropagation
212
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;Undefined
entryUndefined
![Page 213: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/213.jpg)
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
![Page 214: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/214.jpg)
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
![Page 215: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/215.jpg)
Globalconstantpropagation
215
exit x = 4;Undefined
z = x;Undefined
w = x;Undefined
x=6y = x;Undefined
z = y;Undefined
Undefinedx = 6;x = 6
entryUndefined
![Page 216: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/216.jpg)
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
![Page 217: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/217.jpg)
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?
![Page 218: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/218.jpg)
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
![Page 219: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/219.jpg)
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
![Page 220: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/220.jpg)
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
![Page 221: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/221.jpg)
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
![Page 222: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/222.jpg)
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
![Page 223: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/223.jpg)
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
![Page 224: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/224.jpg)
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
![Page 225: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/225.jpg)
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
![Page 226: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/226.jpg)
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
![Page 227: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/227.jpg)
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
![Page 228: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/228.jpg)
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
![Page 229: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/229.jpg)
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
![Page 230: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/230.jpg)
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
![Page 231: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/231.jpg)
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?
![Page 232: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/232.jpg)
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
![Page 233: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/233.jpg)
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
![Page 234: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/234.jpg)
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
![Page 235: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/235.jpg)
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
![Page 236: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/236.jpg)
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
![Page 237: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/237.jpg)
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
![Page 238: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/238.jpg)
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
![Page 239: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/239.jpg)
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
![Page 240: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/240.jpg)
Provingtermination
• Ouralgorithmforrunningtheseanalysescontinuouslyloopsuntilnochangesaredetected
• Giventhis,howdoweknowtheanalyseswilleventuallyterminate?– Ingeneral,wedon‘t
240
![Page 241: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/241.jpg)
Terminates?
241
![Page 242: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/242.jpg)
LivenessAnalysis
• Avariableislive atapointinaprogramiflaterintheprogramitsvaluewillbereadbeforeitiswrittentoagain
242
![Page 243: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/243.jpg)
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
![Page 244: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/244.jpg)
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
![Page 245: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/245.jpg)
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
![Page 246: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/246.jpg)
Joinsemilatticeexampleforliveness
246
a b c
a, b a, c b, c
a, b, c
Bottomelement
![Page 247: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/247.jpg)
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
![Page 248: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/248.jpg)
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
![Page 249: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/249.jpg)
Provingtermination
• Ouralgorithmforrunningtheseanalysescontinuouslyloopsuntilnochangesaredetected
• Problem: howdoweknowtheanalyseswilleventuallyterminate?
249
![Page 250: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/250.jpg)
Anon-terminatinganalysis
• ThefollowinganalysiswillloopinfinitelyonanyCFGcontainingaloop:
• Direction: Forward• Domain: ℕ• Joinoperator:max• Transferfunction: f(n) = n+1• Initialvalue:0
250
![Page 251: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/251.jpg)
Anon-terminatinganalysis
251
start
end
x = y
![Page 252: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/252.jpg)
Initialization
252
start
end
x = y0
0
![Page 253: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/253.jpg)
Fixed-pointiteration
253
start
end
x = y0
0
![Page 254: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/254.jpg)
Chooseablock
254
start
end
x = y0
0
![Page 255: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/255.jpg)
Iteration1
255
start
end
x = y0
0
0
![Page 256: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/256.jpg)
Iteration1
256
start
end
x = y1
0
0
![Page 257: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/257.jpg)
Chooseablock
257
start
end
x = y1
0
0
![Page 258: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/258.jpg)
Iteration2
258
start
end
x = y1
0
0
![Page 259: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/259.jpg)
Iteration2
259
start
end
x = y1
0
1
![Page 260: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/260.jpg)
Iteration2
260
start
end
x = y2
0
1
![Page 261: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/261.jpg)
Chooseablock
261
start
end
x = y2
0
1
![Page 262: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/262.jpg)
Iteration3
262
start
end
x = y2
0
1
![Page 263: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/263.jpg)
Iteration3
263
start
end
x = y2
0
2
![Page 264: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/264.jpg)
Iteration3
264
start
end
x = y3
0
2
![Page 265: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/265.jpg)
Whydoesn’tthisterminate?• Valuescanincreasewithoutbound• Notethat“increase”referstothelatticeordering,nottheorderingonthenaturalnumbers
• Theheight ofasemilatticeisthelengthofthelongestincreasingsequenceinthatsemilattice
• Thedataflowframeworkisnotguaranteedtoterminateforsemilatticesofinfiniteheight
• Notethatasemilatticecanbeinfinitelylargebuthavefiniteheight– e.g.constantpropagation
265
0
1
2
3
4
...
![Page 266: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/266.jpg)
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
![Page 267: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/267.jpg)
Anothernon-terminatinganalysis
• Thisanalysisworksonafinite-heightsemilattice,butwillnotterminateoncertainCFGs:
• Direction: Forward• Domain: Booleanvaluestrue andfalse• Joinoperator:LogicalOR• Transferfunction:LogicalNOT• Initialvalue:false
267
![Page 268: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/268.jpg)
Anon-terminatinganalysis
268
start
end
x = y
![Page 269: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/269.jpg)
Initialization
269
start
end
x = yfalse
false
![Page 270: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/270.jpg)
Fixed-pointiteration
270
start
end
x = yfalse
false
![Page 271: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/271.jpg)
Chooseablock
271
start
end
x = yfalse
false
![Page 272: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/272.jpg)
Iteration1
272
start
end
x = yfalse
false
false
![Page 273: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/273.jpg)
Iteration1
273
start
end
x = ytrue
false
false
![Page 274: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/274.jpg)
Iteration2
274
start
end
x = ytrue
false
true
![Page 275: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/275.jpg)
Iteration2
275
start
end
x = yfalse
false
true
![Page 276: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/276.jpg)
Iteration3
276
start
end
x = yfalse
false
false
![Page 277: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/277.jpg)
Iteration3
277
start
end
x = ytrue
false
false
![Page 278: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/278.jpg)
Whydoesn’titterminate?• Valuescanloopindefinitely• Intuitively,thejoinoperatorkeepspullingvaluesup
• Ifthetransferfunctioncankeeppushingvaluesbackdownagain,thenthevaluesmightcycleforever
278
false
true
false
true
false
...
![Page 279: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/279.jpg)
Whydoesn’titterminate?• Valuescanloopindefinitely• Intuitively,thejoinoperatorkeepspullingvaluesup
• Ifthetransferfunctioncankeeppushingvaluesbackdownagain,thenthevaluesmightcycleforever
• Howcanwefixthis?
279
false
true
false
true
false
...
![Page 280: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/280.jpg)
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
![Page 281: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/281.jpg)
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
![Page 282: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/282.jpg)
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
![Page 283: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/283.jpg)
Thegrandresult
• Theorem: Adataflowanalysiswithafinite-heightsemilattice andfamilyofmonotonetransferfunctions alwaysterminates
• Proofsketch:– Thejoinoperatorcanonlybringvaluesup– Transferfunctionscanneverlowervaluesbackdownbelowwheretheywereinthepast(monotonicity)
– Valuescannotincreaseindefinitely(finiteheight)
283
![Page 284: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/284.jpg)
An“optimality”result
• Atransferfunctionf isdistributiveiff(a 7 b)=f(a)7 f(b)
foreverydomainelementsa andb• Ifalltransferfunctionsaredistributivethenthefixed-pointsolutionisthesolutionthatwouldbecomputedbyjoiningresultsfromall(potentiallyinfinite)control-flowpaths– Joinoverallpaths
• Optimalifweignoreprogramconditions
284
![Page 285: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/285.jpg)
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
![Page 286: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/286.jpg)
Loopoptimizations• Mostofaprogram’scomputationsaredoneinside
loops– Focusoptimizationseffortonloops
• Theoptimizationswe’veseensofarareindependentofthecontrolstructure
• Someoptimizationsarespecializedtoloops– Loop-invariantcodemotion– (Strengthreductionviainductionvariables)
• Requireanothertypeofanalysistofindoutwhereexpressionsgettheirvaluesfrom– Reachingdefinitions
• (Alsousefulforimprovingregisterallocation)
286
![Page 287: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/287.jpg)
Loopinvariantcomputation
287
y = t * 4x < y + z
endx = x + 1
start
y = …t = …z = …
![Page 288: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/288.jpg)
Loopinvariantcomputation
288
y = t * 4x < y + z
endx = x + 1
start
y = …t = …z = …
t*4andy+zhavesamevalueoneachiteration
![Page 289: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/289.jpg)
Codehoisting
289
x < w
endx = x + 1
start
y = …t = …z = …y = t * 4w = y + z
![Page 290: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/290.jpg)
Whatreasoningdidweuse?
290
y = t * 4x < y + z
endx = x + 1
start
y = …t = …z = …
yisdefinedinsideloopbutitisloopinvariantsincet*4isloop-invariant
Bothtandzaredefinedonlyoutsideofloop
constantsaretriviallyloop-invariant
![Page 291: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/291.jpg)
Whataboutnow?
291
y=t*4x<y+z
endx=x+1t=t+1
start
y=…t=…z=…
Nowtisnotloop-invariantandsoaret*4andy
![Page 292: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/292.jpg)
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
![Page 293: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/293.jpg)
Reachingdefinitionsanalysis• Adefinitiond:t=…reaches aprogramlocationifthereisa
pathfromthedefinitiontotheprogramlocation,alongwhichthedefinedvariableisneverredefined
293
![Page 294: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/294.jpg)
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
![Page 295: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/295.jpg)
Reachingdefinitionsanalysis
295
d4: y = t * 4
d4:x < y + z
d6: x = x + 1
d1: y = …
d2: t = …
d3: z = …
start
end
![Page 296: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/296.jpg)
Reachingdefinitionsanalysis
296
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
end
![Page 297: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/297.jpg)
Initialization
297
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
end
![Page 298: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/298.jpg)
Iteration1
298
d4: y = t * 4
d4:x < y + z
d5: x = x + 1
start
d1: y = …
d2: t = …
d3: z = …
end
![Page 299: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/299.jpg)
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
![Page 300: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/300.jpg)
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
![Page 301: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/301.jpg)
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
![Page 302: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/302.jpg)
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
![Page 303: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/303.jpg)
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
![Page 304: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/304.jpg)
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
![Page 305: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/305.jpg)
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
![Page 306: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/306.jpg)
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
![Page 307: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/307.jpg)
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
![Page 308: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/308.jpg)
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
![Page 309: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/309.jpg)
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
![Page 310: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/310.jpg)
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
![Page 311: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/311.jpg)
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
![Page 312: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/312.jpg)
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
![Page 313: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/313.jpg)
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
![Page 314: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/314.jpg)
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
![Page 315: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/315.jpg)
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
![Page 316: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/316.jpg)
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
![Page 317: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/317.jpg)
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
![Page 318: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/318.jpg)
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
![Page 319: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/319.jpg)
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
![Page 320: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/320.jpg)
Inductionvariables
320
while (i < x) j = a + 4 * ia[j] = ji = i + 1
i isincrementedbyaloop-invariantexpressiononeachiteration– thisiscalledaninductionvariable
jisalinearfunctionoftheinductionvariablewithmultiplier4
![Page 321: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/321.jpg)
Strength-reduction
321
j = a + 4 * iwhile (i < x)
j = j + 4a[j] = ji = i + 1
Prepareinitialvalue
Incrementbymultiplier
![Page 322: Compilation - TAUmaon/teaching/2016-2017/compilation/compilatio… · Control-Flow Graphs • A control-flow graph (CFG) is a graph of the basic blocks in a function • The term](https://reader034.fdocuments.us/reader034/viewer/2022042920/5f67b490832e9364130d9a8c/html5/thumbnails/322.jpg)
TheEnd