ch9 peephole

Upload: madhurimapatra1987

Post on 04-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Ch9 PeepHole

    1/12

    CMPUT 680 - Compiler Designand Optimization 1

    CH 9: CODE GENERATION

    PEEP HOLE Optimization

  • 7/31/2019 Ch9 PeepHole

    2/12

    CMPUT 680 - Compiler Designand Optimization 2

    Peephole Optimization

    Goals:- improve performance- reduce memory footprint

    - reduce code sizeMethod:

    1. Exam short sequences of target instructions2. Replacing the sequence by a more efficient one.

    redundant-instruction elimination flow-of-control optimizations algebraic simplifications use of machine idioms

  • 7/31/2019 Ch9 PeepHole

    3/12

    CMPUT 680 - Compiler Designand Optimization 3

    Redundant Load and Stores

    (1) LOAD R0, a(2) STORE a, R0

    (1) LOAD R0, a

    If there are no changes to a between (1) and (2),

    then the store (2) is redundant.

  • 7/31/2019 Ch9 PeepHole

    4/12

    CMPUT 680 - Compiler Design

    and Optimization 4

    Example

    debug = 0. . .if(debug) {

    print debugging information}

    debug = 0

    . . .if debug = 1 goto L1goto L2

    L1: print debugging informationL2:

    Source Code:

    Intermediate

    Code:

  • 7/31/2019 Ch9 PeepHole

    5/12

    CMPUT 680 - Compiler Design

    and Optimization 5

    Eliminate Jump after Jump

    debug = 0. . .if debug = 1 goto L1

    goto L2L1: print debugging informationL2:

    Before:

    debug = 0

    . . .if debug 1 goto L2print debugging information

    L2:

    After:

  • 7/31/2019 Ch9 PeepHole

    6/12

    CMPUT 680 - Compiler Design

    and Optimization 6

    Constant Propagation

    Before:

    After:

    debug = 0. . .

    if debug

    1 goto L2print debugging informationL2:

    debug = 0. . .if 0 1 goto L2print debugging information

    L2:

  • 7/31/2019 Ch9 PeepHole

    7/12

    CMPUT 680 - Compiler Design

    and Optimization 7

    Unreachable Code

    (dead code elimination)

    Before:

    After:

    debug = 0. . .

    if debug

    1 goto L2print debugging informationL2:

    debug = 0. . .

  • 7/31/2019 Ch9 PeepHole

    8/12

    CMPUT 680 - Compiler Design

    and Optimization 8

    Flow-of-control optimizations

    goto L1. . .

    L1: goto L2

    goto L2. . .

    L1: goto L2

    if a < b goto L1. . .

    L1: goto L2

    if a < b goto L2. . .

    L1: goto L2

    goto L1. . .

    L1: if a < b goto L2

    L3:

    if a < b goto L2goto L3. . .

    L3:

  • 7/31/2019 Ch9 PeepHole

    9/12

    CMPUT 680 - Compiler Design

    and Optimization 9

    Transformations on Basic

    Blocks

    Structure-Preserving Transformations:

    common subexpression elimination

    dead code eliminationrenaming of temporary variables

    interchange of two independent adjacent

    statements

  • 7/31/2019 Ch9 PeepHole

    10/12

    CMPUT 680 - Compiler Design

    and Optimization 10

    Examples of Transformations

    Common subexpression elimination:

    a := b + cb := a - d

    c := b + cd := a - d

    a := b + cb := a - d

    c := b + cd := b

    Dead code elimination:

    if x is never referenced after the statement x = y+z, the

    statement can be safely eliminated.

  • 7/31/2019 Ch9 PeepHole

    11/12

    CMPUT 680 - Compiler Design

    and Optimization 11

    Examples of Transformations

    Interchange of statements:

    t1 := b + ct2 := x + y

    t2 := x + yt1 := b + c

    Renaming temporary variables:

    if there is a statement t := b + c, we can change it to u := b + c

    and change all uses of t to u.

  • 7/31/2019 Ch9 PeepHole

    12/12

    CMPUT 680 - Compiler Design

    and Optimization 12

    Examples of Transformations

    Algebraic transformations:

    x := x + 0x := x * 1

    x := y**2z := 2*x

    x := y*y

    z := x + x

    Changes such as y**2 into y*y and 2*x into x+x

    are also known as strength reduction.