322
Compilation Lecture 7 IR + Optimizations Noam Rinetzky 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

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

“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

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

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

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

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

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

(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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

TheEnd