Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21
Code Optimization
Shifa Tasmiah Tisha
Slide courtesy: Prof. Dr. M. Moshiul Hoque
Code Optimization • High-level language constructs can introduce substantial run-time overhead if we naively translate each construct independently into machine code. • Elimination of unnecessary instructions in object code, or the replacement of one sequence of instructions by a faster sequence of instructions that does the same thing is usually called "code improvement" or "code optimization." Quicksort Integers = 04 bytes B1 B5 B2
• Copy Propagation • Dead-code elimination • Constant folding Before Local Common Sub- expression Elimination
• A program will include several
calculations of the same value, such as an offset in an array. • Some of these duplicate calculations cannot be avoided by the programmer After because they lie below the level of detail accessible within the source language. B5 recalculates 4*i & 4*j, although none of these calculations were requested explicitly by the programmer. GLOBAL Common Sub- expression Elimination An occurrence of an expression E is called a common sub-expression if E was previously computed & the values of the variables in E have not changed since the previous computation We avoid recomputing E if we can use its previously computed value; variable x to which the previous computation of E was assigned has not changed in the interim. Example 9.2: Fig. shows the result of eliminating both global and local common sub expressions from blocks B5 and B6 in the flow graph. After local common sub expressions are eliminated, B5 still evaluates 4*i & 4*j. Both are common subexpressions; in particular, the three statements B5
B3
using t4 computed in block B3 .
Observe that as control passes from the evaluation of 4*j in B3 to B5, there is no change to j & no change to t4, so t4 can be used if 4*j is needed. Another common sub expression The statements comes to light in B5 after t4 replaces t8 The new expression a[t4] corresponds to the value of a[ j] at the source level. Not only does j retain its value as control leaves B3 & then enters B5 , but a[j] , a value computed into a temporary t5, does too, because there are no assignments to B5 elements of the array a in the interim. B3 B5 B2 Copy Propagation
• Block B5 can be further improved by
eliminating x using two new transformations. • One concerns assignments of the form u = v called copy statements. (a) (b) The idea behind the copy- propagation transformation is to use v for u, wherever possible after the copy statement u=v. B5 assignment x = t3 in block B5 is a copy. Dead Code Elimination • A variable is live at a point in a program if its value can be used subsequently; otherwise, it is dead at that point. • A related idea is dead (or useless) code statements that compute values that never get used. • While the programmer is unlikely to introduce any dead code intentionally, it may appear as the result of previous transformations. • One advantage of copy propagation is that it often turns copy statement into dead code. Copy propagation followed by dead-code elimination removes the assignment to x and transforms the code Problems • Page 51: 2.2.1, 2.2.2 • Page 125: 3.3.2, • Page 151: 3.6.3 • Page 166: 3.7.3 • Page 206: 4.2.1, 4.2.2 • Page 216: 4.3.1 • Page 370: 6.2.1 • Page 440: 7.2.3 • Page 516: 8.2.1, 8.2.2, 8.2.6 • Page 531: 8.4.1