Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Principles Fall 2014-2015 Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.

Similar presentations


Presentation on theme: "Compiler Principles Fall 2014-2015 Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University."— Presentation transcript:

1 Compiler Principles Fall 2014-2015 Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University

2 Tentative syllabus Front End Scanning Top-down Parsing (LL) Bottom-up Parsing (LR) Intermediate Representation Lowering Optimizations Local Optimizations Dataflow Analysis Loop Optimizations Code Generation Register Allocation Instruction Selection 2 mid-termexam

3 Previously The need for Intermediate Representations Three-Address Code Lowering abstract syntax trees (AST) to IR Sethi-Ullman algorithm for efficient lowering 3

4 agenda Introduction to optimizations Formalisms for program analysis – Basic blocks – Control flow graphs Local program analyses and optimizations – Available expressions  common sub-expression elimination + copy propagation – Live variables  dead code elimination 4

5 5 Introduction to optimizations

6 Optimization points 6 source code Front end IR Code generator target code User profile program change algorithm Compiler apply IR optimizations Compiler register allocation instruction selection peephole transformations today and next week

7 Overview of IR optimization Formalisms and Terminology – Control-flow graphs – Basic blocks Local optimizations – Optimizing small pieces of a function Global optimizations – Optimizing functions as a whole The dataflow framework – Defining and implementing a wide class of optimizations 7

8 Semantics-preserving optimizations An optimization is semantics-preserving if it does not alter the semantics of the original program Examples: – Eliminating unnecessary statements – Computing values that are known statically at compile-time instead of runtime – Evaluating constant expressions outside of a loop instead of inside Non-examples: – Reordering side-effecting computations – Replacing bubble sort with quicksort (why?) The optimizations we will consider in this class are all semantics-preserving How can we find opportunities for optimizations? 8

9 Program analysis In order to optimize a program, the compiler has to be able to reason about the properties of that program An analysis is called sound if it never asserts an incorrect fact about a program All the analyses we will discuss in this class are sound – (Why?) 9

10 Soundness 10 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x holds some integer value”

11 Soundness 11 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x is either 137 or 42”

12 Soundness 12 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x is 137”

13 Soundness 13 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x is either 137, 42, or 271”

14 14 Control flow graphs

15 Visualizing IR 15 main: _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;

16 Visualizing IR 16 main: _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 Visualizing IR 17 main: _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

18 Basic blocks A basic block is a sequence of IR instructions where – There is exactly one spot where control enters the sequence, which must be at the start of the sequence – There is exactly one spot where control leaves the sequence, which must be at the end of the sequence Informally, a sequence of instructions that always execute as a group 18

19 Control-flow graphs A control-flow graph (CFG) is a graph of the basic blocks in a function – From here on CFG stands for “control-flow graph” and not “context free grammar” Each edge from one basic block to another indicates that control can flow from the end of the first block to the start of the second block Dedicated nodes for the start and end of a function 19

20 Scope of optimizations An optimization is local if it works on just a single basic block An optimization is global if it works on an entire control-flow graph An optimization is interprocedural if it works across the control-flow graphs of multiple functions – We won't talk about this in this course 20

21 21 Examples of control flow graphs and optimizations

22 Basic blocks example 22 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { START: _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; _t1 := y; z := _t1; Goto END: _L0: _t2 := y; x := _t2; END: Divide the code into basic blocks

23 Control-flow graph example 23 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { START: _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; _t1 := y; z := _t1; Goto END: _L0: _t2 := y; x := _t2; END: Draw the control-flow graph

24 Control-flow graph example 24 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end

25 Local optimizations example 25 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end

26 Local optimizations example 26 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end

27 Local optimizations example 27 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 137 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end Copy propagation

28 Local optimizations example 28 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end Constant folding

29 Local optimizations example 29 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start _t1 := y; z := y; _t2 := y; x := _t2; end Copy propagation

30 Local optimizations example 30 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y; _t2 := y; x := _t2; end Dead code elimination

31 Local optimizations example 31 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y; _t2 := y; x := _t2; end

32 Local optimizations example 32 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y;x := y; end Copy propagation followed by dead code elimination

33 Global optimizations example 33 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y;x := y; end

34 Global optimizations example 34 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y;x := y; end

35 Global optimizations example 35 int main() { int x; int y; int z; y := 137 + 3; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := 140;x := 140; end Copy propagation

36 36 Common Subexpression elimination

37 CSE Example 37 b := a * a; c := a * a; d := b + c; e := b + b;

38 CSE Example 38 b := a * a; c := a * a; d := b + c; e := b + b;

39 CSE Example 39 b := a * a; c := b; d := b + c; e := b + b; Common sub-expression elimination

40 Common Subexpression Elimination If we have two variable assignments v1 := a op b … v2 := a op b and the values of v1, a, and b have not changed between the assignments, rewrite the code as v1 := a op b … v2 := v1 Eliminates useless recalculation Paves the way for later optimizations 40

41 41 copy propagation

42 CP Example 42 b := a * a; c := b; d := b + c; e := b + b;

43 CP Example 43 b := a * a; c := b; d := b + b; e := b + b; Copy propagation

44 Copy Propagation If we have a variable assignment v1 := v2 then as long as v1 and v2 are not reassigned, we can rewrite expressions of the form a := … v1 … as a := … v2 … 44

45 Local optimizations example 45 b := a * a; c := b; d := b + b; e := b + b; Which optimization should we apply here?

46 Local optimizations example 46 b := a * a; c := b; d := b + b; e := d; Which optimization should we apply here? Common sub-expression elimination (again)

47 Optimizations and analyses Most optimizations are only possible given some analysis of the program's behavior In order to implement an optimization, we will talk about the corresponding program analyses Program analysis = algorithm that processes program and infers facts – Sound facts = facts that hold for all program executions – Sound analysis = program analysis that infers only sound facts 47

48 48 Available expressions

49 Both common subexpression elimination and copy propagation depend on an analysis of the available expressions in a program An expression a = b op c is called available at program location L if variable a holds the value of b op c at that location – Similarly for a = b In common subexpression elimination, we replace an available expression (b op c) by the variable holding its value (a) In copy propagation, we replace the use of a variable (a) by the available expression it holds (b) 49

50 Finding available expressions Compute for each program location L a set of expressions AE L of the forms a = b op c and a = b that are definitely available there Idea: Iterate across the basic block, beginning with the empty set of expressions and updating available expressions at each statement Whenever we execute a statement a := b op c: – Any expression holding a is invalidated – The expression a = b op c becomes available 50

51 Available expressions step 51 a := b + c Output Value AE out Input Value AE in AE out = ( AE in \ {e | e contains a })  { a=b+c } Expressions of the forms a =… and x=… a … Note that this is an equation (a fact) – not a statement Provided that a and b and a and c are different pairs of variables

52 Available expressions example 52 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 }

53 Optimizing via available expressions Common sub-expression elimination – If {… t = y op z … } x := y op z – Can transform statement into x := t Copy propagation – If {… y = t … } x := y op z – Can transform statement into x := t op z Note: same for x=y 53

54 Applying CSE + CP 54 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 }

55 Applying CSE + CP 55 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 }

56 Applying CSE + CP 56 a := b; c := a; 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, c = b } { a = b } { } { a = b, c = b, d = b, e = a + b, f = a + b }

57 57 dead code elimination

58 Dead code elimination a := b; c := a; d := a + b; e := d; d := a; f := e; 58 Print(d); Can we remove this statement?

59 Dead code elimination An assignment to a variable v is called dead if the value of that assignment is never read anywhere Dead code elimination removes dead assignments from IR Determining whether an assignment is dead depends on assignments preceding it 59

60 Live variables The analysis corresponding to dead code elimination is called liveness analysis A variable is live at a point in a program if later in the program its value will be read before it is written to again Dead code elimination works by computing liveness for each variable, then eliminating assignments to dead variables 60

61 Computing live variables To know if a variable will be used at some point, we iterate across the statements in a basic block in reverse order Initially, some small set of values are known to be live (which ones depends on the particular program) – Usually arguments of a function call or a returned variable (temporaries that are pushed on the stack) When we see the statement a := b op c: – Just before the statement, a is not alive, since its value is about to be overwritten – Just before the statement, both b and c are alive, since we're about to read their values – (what if we have a := a + b?) 61

62 Live variables step 62 a := b + c Output Value LV out Input Value LV in LV in = ( LV out \ { a })  { b,c }

63 Liveness analysis a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d, e } { a, b, e } { a, b, d } { a, b } { b } { b, d } Which statements are dead? 63 Push b; { d } Push d; { }

64 Optimizing via liveness analysis Dead code elimination – If x := y op z {v 1,…,v k } – And x  {v 1,…,v k } – We can eliminate x := y op z Note: same for x:=y 64

65 Dead code elimination a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d, e } { a, b, e } { a, b, d } { a, b } { b } { b, d } - given Which statements are dead? 65

66 Dead code elimination a := b; d := a + b; e := d; d := a; { b, d, e } { a, b, e } { a, b, d } { a, b } { b } { b, d } 66

67 Liveness analysis II a := b; d := a + b; e := d; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } Which statements are dead? 67

68 Liveness analysis II a := b; d := a + b; e := d; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } Which statements are dead? 68

69 Dead code elimination a := b; d := a + b; e := d; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } Which statements are dead? 69

70 Dead code elimination a := b; d := a + b; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } 70

71 Liveness analysis III a := b; d := a + b; d := a; { b, d } { a, b } { b } Which statements are dead? 71

72 Dead code elimination a := b; d := a + b; d := a; { b, d } { a, b } { b } Which statements are dead? 72

73 Dead code elimination a := b; d := a; { b, d } { a, b } { b } 73

74 Dead code elimination a := b; d := a; 74 If we further apply copy propagation this statement can be eliminated too

75 A combined algorithm Start with initial live variables at end of block Traverse statements from end to beginning For each statement – If assigns to dead variables – eliminate it – Otherwise, compute live variables before statement and continue in reverse 75

76 A combined algorithm a := b; c := a; d := a + b; e := d; d := a; f := e; 76

77 A combined algorithm a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d } 77

78 A combined algorithm a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d } 78

79 A combined algorithm a := b; c := a; d := a + b; e := d; d := a; { b, d } 79

80 A combined algorithm a := b; c := a; d := a + b; e := d; d := a; { b, d } { a, b } 80

81 A combined algorithm 81 a := b; c := a; d := a + b; e := d; d := a; { b, d } { a, b }

82 A combined algorithm 82 a := b; c := a; d := a + b; d := a; { b, d } { a, b }

83 A combined algorithm a := b; c := a; d := a + b; d := a; { b, d } { a, b } 83

84 A combined algorithm a := b; c := a; d := a; { b, d } { a, b } 84

85 A combined algorithm a := b; c := a; d := a; { b, d } { a, b } 85

86 A combined algorithm a := b; d := a; { b, d } { a, b } 86

87 A combined algorithm a := b; d := a; { b, d } { a, b } 87 { b }

88 A combined algorithm a := b; d := a; 88

89 89 Notes about optimizations

90 Applying local optimizations The different optimizations we've seen so far all take care of just a small piece of the optimization – Common subexpression elimination eliminates unnecessary statements – Copy propagation helps identify dead code – Dead code elimination removes statements that are no longer needed To get maximum effect, we may have to apply these optimizations numerous times 90

91 Optimization path 91 IR Control-Flow Graph CFG builder Program Analysis Annotated CFG Optimizing Transformation Target Code Code Generation (+optimizations) done with IR optimizations IR optimizations

92 Other types of local optimizations Arithmetic Simplification – Replace “hard” operations with easier ones – e.g. rewrite x := 4 * a; as x := a << 2; Constant Folding – Evaluate expressions at compile-time if they have a constant value. – e.g. rewrite x := 4 * 5; as x := 20; 92

93 Next lecture: Global Optimizations via Dataflow Analysis


Download ppt "Compiler Principles Fall 2014-2015 Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University."

Similar presentations


Ads by Google