170 likes | 300 Views
Program Performance through Compiler Optimization Techniques. Compiler Optimization. It is the process of improving the output of the compiler To minimize execution time of the program By removing repeated computation To minimize amount of memory occupied by the program
E N D
Program Performance through Compiler Optimization Techniques
Compiler Optimization • It is the process of improving the output of the compiler • To minimize execution time of the program • By removing repeated computation • To minimize amount of memory occupied by the program • By removing unnecessary code in the program • To minimize the power consumed by the program • By parallelizing the code among multiple cores • Beneficial for low power devices like cell phones
When and where to optimize • Optimization can be performed on • Intermediate Code • In order to reduce size of Abstract Syntax Tree • Machine independent • Final Code generation • In-order to improve register usage • In order to better utilize pipeline • Machine dependent
Types of Compiler Optimization Techniques • Local Optimization • Optimization performed exclusively on basic blocks. • Simplest type of optimization since no control flow information is considered • Some optimization techniques are • Constant Folding • Constant Propagation • Common Sub-expression Elimination • Dead Code Elimination • Copy Propagation • Loop Optimization
Machine Dependent Optimization • Machine Optimization • This is done in final code generation phase • Specific machine features like specialized instructions, hardware pipeline abilities, register details etc are taken into account to produce code optimization for a particular machine architecture • Some optimization techniques are • Register Allocation • Instruction Selection • Instruction Scheduling
Local Optimization Techniques • Constant Folding • It refers to the evaluation of expression whose operands are constants at compile time. • It evaluates the expression and replaces the expression by value • Example • i = 15 + 5 *10 = 65 • Conditional branch : if (a<b) goto L1 else goto L2 • If a and b are constants entire branch condition is transformed to either goto L1 or goto L2 depending on truth of condition
Local Optimization Techniques (cont..) • Constant Propagation • If a variable is assigned a constant value, then later uses of that variable can be replaced by the constant as long as value of variable is not changed • Example int x = 14; int y = 7 - x / 2; return y * (28 / x + 2); Propagating x yields int x = 14; int y = 7 - 14 / 2; return y * (28 / 14 + 2);
Local Optimization Techniques (cont..) • Copy Propagation • Similar to Constant Propagation • But generalized to non-constant values • Example
Local Optimization Techniques (cont..) • Common Sub-expression Elimination • Two operations are common if they produce the same result. • It is efficient to compute the result once and reference it second time rather than re-computing it. • Expression is alive if operands used to compute it have not changed other it is dead. • Example • a = b * c + g; • d = b * c * d; • tmp = b * c; • a = tmp + g; • d = tmp * d;
Local Optimization Techniques (cont..) • Dead Code Elimination • If the result of a instruction is never used, the instruction is said to be dead and can be removed • Example int foo(void) { int a = 24; int b = 25; /* Assignment to dead variable */ int c; c = a << 2; return c; b = 24; /* Unreachable code */ return 0; }
Local Optimization Techniques (cont..) • Loop Optimization • Loop Interchange • Loop invariant Code Motion • Loop Unrolling
Loop Optimization • Loop Interchange • These optimizations exchange inner loops with outer loops. • When the loop variables index into an array, such a transformation can improve locality of reference, depending on the array's layout. • One major purpose of loop interchange is to improve the cache performance for accessing array elements. • Cache misses occur if the contiguously accessed array elements within the loop come from a different cache line. • Loop interchange can help prevent this.
Loop Interchange Example • For example, in the code fragment: for i from 0 to 10 for j from 0 to 20 a[i,j] = i + j • loop interchange would result in: for j from 0 to 20 for i from 0 to 10 a[i,j] = i + j • Row major to column major format or vice versa
Loop-invariant code motion • Loop-invariant code consists of statements or expressions which can be moved outside the body of a loop without affecting the semantics of the program. • Example for (inti = 0; i < n; i++) { x = y + z; a[i] = 6 * i + x * x; } x = y + z; t1 = x * x; for (inti = 0; i < n; i++) a[i] = 6 * i + t1;
Loop unrolling • It is a loop transformation technique that attempts to optimize a program's execution speed at the expense of its binary size (space-time tradeoff). The transformation can be undertaken manually by the programmer or by an compiler optimization technique. • Advantages • branch penalty is minimized. • If the statements in the loop are independent of each other (i.e. where statements that occur earlier in the loop do not affect statements that follow them), the statements can potentially be executed in parallel. • Disadvantages • Increased program code size, which can be undesirable, particularly for embedded applications.