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.

Similar presentations

Presentation on theme: "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."— Presentation transcript:

1
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 they consume Compilers that apply code-improving transformations are called Optimizing Compilers Machine Independent improvements address the logic of the program Machine Dependent improvements utilize special features of the target instruction set, including registers and special addressing modes

4
4 Flow Analysis Loops represent the most computationally intensive part of a program. Improvements to loops will produce the most significant effect Local Optimizations are performed on basic blocks of code Global Optimizations are performed on the whole code A Basic Block is a sequence of instructions that is only entered at the start and exited at the end, with no jumps into or out of the middle. That is, a basic block begins at a procedure or the target of a jump A basic block ends at the start of the next basic block or the end of the program

5
5 Criteria for code-improvement Transformations 1. Transformations must preserve the meaning of programs 2. A transformation must, on the average, speed up programs by a measurable amount 3. A transformation must be worth the effort

8
8 Augmented 3AC An augmented 3 address code language to simplify the code... Let a be an array of integers starting at byte address a 0 a[add] on the left-hand-side of an assignment is the address a 0 +add a[add] on the right-hand-side of an assignment is the value of the element of the array at address a 0 +add Since integers are stored in 4 bytes the offset address of an element a[i] is 4*i

15
15 Code Motion Code Motion decreases the amount of code in a loop Before while ( i <= limit + 2) /*statement does not change limit */ After t = limit + 2 while ( i <= t) /*statement does not change limit or t*/

17
17 Induction Variables elimination In Block2 whenever i increases by 1, t2 increases by 4, i and t2 are called induction variables. In Block3 whenever j decreases by 1, t4 decreases by 4, j and t4 are induction variables, too. If there are two or more induction variables in a loop, it may be possible to get rid of all but one BeforeAfter Block4: if i >= j goto B6 if t2 >= t4 goto B6

20
20Others Algebraic Simplification x := x + 0 x := x * 1 Use of Registers to store the most used variables because access time is much quicker than memory Use of specialized instructions mov a add registera 1 mov a registera could be justinc a Using shift to left instead of multiplication by powers of 2 Using shift to right instead of division into powers of 2

About project

Feedback

To ensure the functioning of the site, we use cookies. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy &amp Terms.
Your consent to our cookies if you continue to use this website.