1 / 20

Code Optimization

SC4312. Lecture 13. 2. Code Optimization. Code Optimization is the phase of compilation that focuses on generating a good code.Most of the time a good code means a code that runs fast.However, there are some cases where a good code is a code that does not require a lot of memory.. SC4312. Lecture

farren
Download Presentation

Code Optimization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. SC4312 Lecture 14 1 Code Optimization SC4312 – Compiler Construction Kwankamol Nongpong

    2. SC4312 Lecture 13 2 Code Optimization Code Optimization is the phase of compilation that focuses on generating a good code. Most of the time a good code means a code that runs fast. However, there are some cases where a good code is a code that does not require a lot of memory.

    3. SC4312 Lecture 13 3 Local Optimization Local Optimization focuses on: Elimination of redundant operations Effective instruction scheduling Effective register allocation. Optimization is considered “local” if it is done at a basic block level (a sequence of instruction where there is no branch in and out through its entirety).

    4. SC4312 Lecture 13 4 Global Optimization Global Optimization focuses on: Same techniques performed by local optimization but at multi-basic-block level Code modifications to improve the performance of loops. Both local and global optimization use a control flow graph to represent the program, and a data flow analysis algorithm to trace the flow of information.

    5. SC4312 Lecture 13 5 Phases of Code Improvement

    6. SC4312 Lecture 13 6 Phases of Code Improvement

    7. SC4312 Lecture 13 7 Peephole Optimization (1) Peephole Optimization works by sliding a several-instruction window (a peephole) over the target code, and looking for suboptimal patterns of instructions. The patterns to look for are heuristic, and typically based on special instructions available on a given machine.

    8. Peephole Optimization (2) Elimination of redundant loads and stores Constant folding Common subexpression elimination Copy propagation Strength reduction Elimination of useless instruction Loop improvement SC4312 Lecture 13 8

    9. SC4312 Lecture 13 9 Elimination of Redundant Loads and Stores The peephole optimizer can recognize that the value produced by a load instruction is already available in a register. Example: r2 := r1 + 5 i := r2 r3 := i r4 := r3 x 3

    10. SC4312 Lecture 13 10 Elimination of Redundant Loads and Stores Example: r2 := r1 + 5 r2 := r1 + 5 i := r2 becomes i := r2 r3 := i r4 := r2 x 3 r4 := r3 x 3 Similarly, if there are two stores to the same location within a peephole, then we can eliminate the first.

    11. SC4312 Lecture 13 11 Constant Folding A naďve code generator may produce code that performs calculations at run-time that could actually be performed at compile-time. Example: r2 := 3 x 2 becomes r2 := 6

    12. SC4312 Lecture 13 12 Constant Propagation Sometimes we can tell that a variable will have a constant value at a particular point in a program. Example: r2 := 4 r3 := r1 + r2 r2 := …

    13. SC4312 Lecture 13 13 Constant Propagation The final assignment to r2 indicates that its previous value is dead. Loads of dead values can then be cut out. Example: r2 := 4 r2 := 4 r3 := r1 + r2 becomes r3 := r1 + 4 r2 := … r2 := … then r3 := r1 + 4 r2 := …

    14. SC4312 Lecture 13 14 Common Sub-expression Elimination When the same calculation occurs twice within the peephole, we can often eliminate the second calculation. Example: r2 := r1 x 5 r2 := r2 + r3 r3 := r1 x 5

    15. SC4312 Lecture 13 15 Common Sub-expression Elimination An extra register is often needed to hold the common value. Example: r2 := r1 x 5 r4 := r1 x 5 r2 := r2 + r3 becomes r2 := r4 + 3 r3 := r1 x 5 r3 := r4

    16. SC4312 Lecture 13 16 Copy Propagation Even if we cannot tell that the content of register b will be constant, we may be able to tell that register b will contain the same value as register a. Example: r2 := r1 r3 := r1 + r2 r2 := 5

    17. SC4312 Lecture 13 17 Copy Propagation If copy propagation is performed early, it can help decrease register pressure. Example: r2 := r1 r2 := r1 r3 := r1 + r2 becomes r3 := r1 + r1 r2 := 5 r2 := 5 then r3 := r1 + r1 r2 := 5

    18. SC4312 Lecture 13 18 Strength Reduction Multiplication or division by powers of two can be replaced by adds or shifts. Example: r1 := r2 x 2 r1 := r2 / 2 r1 := r2 x 0

    19. SC4312 Lecture 13 19 Strength Reduction Numeric identities can sometimes be used to replace a more expensive instruction with a cheaper one. Algebraic identities allow us to simplify instructions. Example: r1 := r2 x 2 r1 := r2 + r2 r1 := r2 / 2 becomes r1 := r2 >> 1 r1 := r2 x 0 r1 := 0

    20. SC4312 Lecture 13 20 Elimination of Useless Instruction Some instructions that do not modify any memory storage can be dropped. Example: r1 := r1 + 0 r1 := r1 x 1

    21. SC4312 Lecture 13 21 Loop Improvement Programs tend to spend most of their time in loops therefore code optimization that can increase the speed of loops are very important. These are several techniques to achieve this. Removal of loop invariants is one that can be used to extract a part of loop’s body that does not change to the loop’s header.

More Related