1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

CSC 4181 Compiler Construction Code Generation & Optimization.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Course Outline Traditional Static Program Analysis Software Testing
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
1 CS 201 Compiler Construction Machine Code Generation.
Computer Architecture Lecture 7 Compiler Considerations and Optimizations.
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Introduction to Program Optimizations Chapter 11 Mooly Sagiv.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Compiler Code Optimizations. Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
What’s in an optimizing compiler?
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
High-Level Transformations for Embedded Computing
Chapter 10 Code Optimization Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
Basic Block Optimizations
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Code Generation Part III
Optimizing Transformations Hal Perkins Autumn 2011
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Fall Compiler Principles Lecture 10: Loop Optimizations
9. Optimization Marcus Denker.
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Lecture 19: Code Optimisation
Basic Block Optimizations
Code Optimization.
Presentation transcript:

1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code” It involves a complex analysis of the intermediate code and the performance of various transformations but every optimizing transformation must also preserve the semantics of the program. That is a compiler should not attempt any optimization that would lead to a change in the programs semantics.There are generally two types of optmization.Machine independent and machine dependent.

2 Machine Independent Optimization  Machine independent optimization or Source level optimization. can be performed independently of the target machine for which the compiler is generating code. that is the optimizations are not tied to the target machines specific platform or language. Examples of machine independent optimizations are: elimination of loop invariant computation, induction variable elimination, and elimination of common sub expressions.

3 Machine Dependent Optmization  On the other hand, machine dependent optimization or Target level optimization requires knowledge of the target machine, an attempt to generate object code that will utilize the target machines registers more efficiently is an example of machine dependent code optimization. Actually, code optimization is a misnomer even after performing various optimizing transformations there is no guarantee that the generated object code will be optimal. Hence we are actually performing code improvement.

4 CRITERIA When attempting any optimizing transformation the following criteria should be applied:  The optimizing should capture most of the potential improvements with out an unreasonable amount of effort.  The optimization should be such that the meaning of the source program is preserved.  The optimization should on average reduce the time and space expended by the object code.

5 Local vs Global Optimization  A transformation of a program is called local if it can be performed by looking only at the statements in a basic blocks ; other wise,it is called global.  Many transformations can be performed at both the local and global levels  Local transformations are usually performed first.

6 Local Optimizations The simplest form of optimizations are local to a small piece of code. No need to analyze the whole procedure body, just the basic block (typically just a small portion of one statement) in question Example: algebraic simplification

7 Algebraic Simplification  Some statements can be deleted  Some statements can be simplified x = x + 0 x = x * 1

8 Algebraic Simplification x = x * 0  x = 0 y = y ^ 2  y = y * y x3  x*x*x

9 Algebraic Simplification  X + 0 = 0 + x x  X – 0 x  X * 1 = 1 * x x  X / 1 = x

10 Strength Reductions  Strength reduction means replacing expensive operations with cheaper ones.  Replacing integer multiplication or division by constants with shift operations.  Replacing 32-bit integer division by 64-bit floating point division.  Replacing floating point multiplications by small constants with floating point additions.  Replacing power function by floating point multiplications.

11 Reduction in strength  Another class of algebraic optimization includes reduction in strength,that is,replacing a more expensive operator by a cheaper one as in  X=y * * 2 x=y * y  Z=2.0 * x z= x + x (if x=2,2*2=4,2+2=4)  Z=X / 2 z= x * 0.5

12 Structure-Preserving Transformations There are a number of ways in which a compiler can improve a program without changing the function it computes.writen below are the examples of such function preserving transformation.  Common subexpression elimination  Dead code elimination  Copy propagation  Constant folding

13 Common Subexpression elimination  An occurrence of an expression “E” is called a common subexpression if E was previously computed, and the values of variables in E have not changed since the previous computation. We can avoid recomputing the expression if we can use the previous computed value. For example;

14 Common subexpression elimination For example; a := b + c b := a - d c := b + c d := a - d a := b + c b := a - d c := b + c d := b

15 Another example X = b * c + 4 z = b * c - 1 t = b * c x = t + 4 z = t - 1

16 Copy propagation  Given an assignment of the form x = y, replace later uses of x with uses of y (as long as there are no instructions between the assignment and the uses that redefine x or y)

17 example x = b + c z = y * x x = b + c z = y * ( b + c)

18 Dead code elimination A variable is dead at some point if its value is not used on any path from that point to the exit. An instruction is dead if it computes only values that are not used on any path to the exit. Dead code elimination removes dead instructions. Dead code often arises from other optimizations.

19 Example_1 Dead code Elimination is merely the removal of code that is never used. i=0 If (i!=0) deadcode(i);

20 Example_2  A typical example of unreachable code is the use of a constant flag to turn debugging information on and off.  # define debug 0  If (debug)  {………}  If debug is set to 0 then the bracketed code inside the if statement is unreachable and target code need not be generated for either it or the enclosing if statement.

21 Example_3 x = 1 x = b + c If x is not referred to at all

22 Example_4  An other example of unreachable code is a procedure that is never called (or is only called from code that is itself unreachable)  The elimination of unreachable code does not usually affect execution speed significantly,but it can substantially reduce the size of the target code.

23 Constant Folding  The evaluation at compile time of expressions whose values are known to be constant. Many operations on constants can be computed at compile time In general, if there is a statement x = y op z …and y and z are constants (or set to constants) …then y op z can be computed at compile time

24 example Example: x =  x = 4 Example: if 2 < 0 jump L can be deleted

25 Another example Constant folding is when expressions with multiple constants are folded together and evaluated at compile time. a = 1+ 2 Will be replaced by a = 3. ( 100 > 0) true

26 Procedure inlining  One operation that can sometimes be relatively expensive is procedure call, where many calling sequence operations must be performed. Modern processors have reduced this cost substantially by offering hardware support (by increasing no of register of cpu) for standard calling sequences but the removal of frequent calls to small procedures can still produce measurable speedups.

27 There is a way to remove procedure calls by replacing procedure call with the code for the procedure body. This is called procedure inlining.

28 An Example Initial code: … a = x ^ 2 b = 3 c = x d = c * c e = b * 2 f = a + d g = e * f …

29 An Example Algebraic Optimization: … a = x ^ 2 b = 3 c = x d = c * c e = b * 2 f = a + d g = e * f …

30 An Example Algebraic Optimization: … a = x ^ 2 b = 3 c = x d = c * c e = b * 2 f = a + d g = e * f …

31 An Example Algebraic Optimization: … a = x * x b = 3 c = x d = c * c e = b << 1 f = a + d g = e * f …

32 An Example Copy propagation: … a = x * x b = 3 c = x d = c * c e = b << 1 f = a + d g = e * f …

33 An Example Copy propagation: … a = x * x b = 3 c = x d = c * c e = b << 1 f = a + d g = e * f …

34 An Example Copy propagation: … a = x * x b = 3 c = x d = c * c e = 3 << 1 f = a + d g = e * f …

35 An Example Copy propagation: … a = x * x b = 3 c = x d = c * c e = 3 << 1 f = a + d g = e * f …

36 An Example Copy propagation: … a = x * x b = 3 c = x d = x * x e = 3 << 1 f = a + d g = e * f …

37 An Example Constant folding: … a = x * x b = 3 c = x d = x * x e = 3 << 1 f = a + d g = e * f …

38 An Example Constant folding: … a = x * x b = 3 c = x d = x * x e = 3 << 1 f = a + d g = e * f …

39 An Example Constant folding: … a = x * x b = 3 c = x d = x * x e = 6 f = a + d g = e * f …

40 An Example Common subexpression elimination: … a = x * x b = 3 c = x d = x * x e = 6 f = a + d g = e * f …

41 An Example Common subexpression elimination: … a = x * x b = 3 c = x d = x * x e = 6 f = a + d g = e * f …

42 An Example Common subexpression elimination: … a = x * x b = 3 c = x d = a e = 6 f = a + d g = e * f …

43 An Example Copy propagation (round 2): … a = x * x b = 3 c = x d = a e = 6 f = a + d g = e * f …

44 An Example Copy propagation (round 2): … a = x * x b = 3 c = x d = a e = 6 f = a + d g = e * f …

45 An Example Copy propagation (round 2): … a = x * x b = 3 c = x d = a e = 6 f = a + a g = 6 * f …

46 An Example Dead code elimination: … a = x * x b = 3 c = x d = a e = 6 f = a + a g = 6 * f …

47 An Example Dead code elimination: … a = x * x b = 3 c = x d = a e = 6 f = a + a g = 6 * f …

48 An Example Dead code elimination: … a = x * x f = a + a g = 6 * f …

49 An Example The final program: … a = x * x f = a + a g = 6 * f …

50 Loop optmization Loop optimization is the most valuable machine independent optimization especially the inner loops where programs tend to spend the bulk of their time. The running time of a program may be improved if we decrease the number of instructions in an inner loop, even if we increase the amount of code outside that loop.

51 Three techniques are important for loop optimization. Code motion Induction variable elimination. Reduction in strength

52 Code motion(Loop invariant computation) In code motion we move code outside a loop. A loop invariant computation is one that computes the same value every time a loop is executed. Therefore,moving such a computation outside the loop leads to a reduction in the execution time.

53 To eliminate loop invariant computations,we identify the invariant computations and then move them outside loop if the move does not lead to a change in the program’s meaning. Identification of loop invariant computation requires the detection of loops in the program. Whether a loop exists in the program or not depends on the program’s control flow,therefore, requiring a control flow analysis.

54 For loop detection, a graphical representation, called a “ program flow graph” shows how the control is flowing in the program and how the control is being used.

55 Example Move loop invariant computation out side the loops While ( i < 100) t = x / y { while ( i < 100) *p = x / y + I { *p = t + 1 i = i + 1 i = i + 1 } }

56 Another example while ( i <= limit – 2) /* statement does not change limit */ t= limit – 2 while( i < = t) /* statement does not change limit */

57 Reduction in strength  It means which replaces an expensive operation by a cheaper one, such as multiplication by addition subtractions.Because multiplication takes more times as compare to addition or subtraction process in many machines.

58 example a = b * 17 a = ( b << 4) + b

59 Induction variable Typical loop optimization strategies focus on identifying variables that are incremented by fixed amounts with each iteration called induction variable. These include loop control variables and other variables that depend on the loop control variables in a fixed ways OR

60 We define basic induction variables of a loop as those names whose only assignments within the loop are of the form i = i ± c, where c is a constant or a name whose value does not change within the loop,

61 example From kakede book p – 284(no need at this time)

62 Loop unrolling  Loop unrolling involves replicating the body of the loop to reduce the required number of tests if the number of iterations are constant. For example consider the following loop;

63 I = 1 while( I < = 100) { x [ I ] = 0; I ++; }

64  In this case, the test I < = 100 will be performed 100 times. But if the body of the loop is replicated, then the number of times this test will need to be performed 50. After replication of the body, the loop will be:

65 I = 1 while( I < = 100) { x [ I ] = 0; I ++; x [ I ] = 0; I ++; }

66  It is possible to choose any divisor for the number of times the loop is executed, and the body will be replicated that many times. Unrolling once that is replicating the body to form two copies of the body saves 50% of the maximum possible executions

67 Loop jamming  Loop jamming is a technique that merges the bodies of two loops if the two loops have the same number of iterations and they use the same indices. This eliminates the test of one loop. For example, consider the following loop

68 { for ( I = 0 ; I < 10 ; I ++ ) for ( j = 0 ; j < 10 ; j++ ) x [ I, j ] = 0; for ( I = 0 ; I < 10 ; I ++ ) x [ I, I ] = 1; }

69  Here, the bodies of the loops on I can be concatenated. the result of loop jamming will be:

70 { for ( I = 0 ; I < 10 ; I ++ ) { for ( j = 0 ; j < 10 ; j++ ) x [ I, j ] = 0; } x [ I, I ] = 1; }

71 Conditions for loop Jamming The following conditions are sufficient for making loop jamming legal: No quantity is computed by the second loop at the iteration I if it is computed by the first loop at iteration j > I. If a value is computed by the first loop at iteration j> I, then this value should not be used by second loop at iteration i.

72 Lets Optimize... int sumcalc(int a, int b, int N) { int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y; } return x; }

73 Constant Propagation int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y; } return x;

74 Constant Propagation int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y; } return x;

75 Constant Propagation int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*0; } return x;

76 Algebraic Simplification int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*0; } return x;

77 Algebraic Simplification int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*0; } return x;

78 Algebraic Simplification int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x; } return x;

79 Copy Propagation int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x; } return x;

80 Copy Propagation int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); x = x; } return x;

81 Copy Propagation int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); } return x;

82 Common Subexpression Elimination int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); } return x;

83 Common Subexpression Elimination int i, x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x + (4*a/b)*i + (i+1)*(i+1); } return x;

84 Common Subexpression Elimination int i, x, y, t; x = 0; y = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + (4*a/b)*i + t*t; } return x;

85 Dead Code Elimination int i, x, y, t; x = 0; y = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + (4*a/b)*i + t*t; } return x;

86 Dead Code Elimination int i, x, y, t; x = 0; y = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + (4*a/b)*i + t*t; } return x;

87 Dead Code Elimination int i, x, t; x = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + (4*a/b)*i + t*t; } return x;

88 Loop Invariant Removal int i, x, t; x = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + (4*a/b)*i + t*t; } return x;

89 Loop Invariant Removal int i, x, t; x = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + (4*a/b)*i + t*t; } return x;

90 Loop Invariant Removal int i, x, t, u; x = 0; u = (4*a/b); for(i = 0; i <= N; i++) { t = i+1; x = x + u*i + t*t; } return x;

91 Strength Reduction int i, x, t, u; x = 0; u = (4*a/b); for(i = 0; i <= N; i++) { t = i+1; x = x + u*i + t*t; } return x;

92 Strength Reduction int i, x, t, u; x = 0; u = (4*a/b); for(i = 0; i <= N; i++) { t = i+1; x = x + u*i + t*t; } return x;

93 Strength Reduction int i, x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x;