Dataflow Analysis

download Dataflow Analysis

of 110

  • date post

    25-Jan-2017
  • Category

    Software

  • view

    49
  • download

    0

Embed Size (px)

Transcript of Dataflow Analysis

  • Challenge the future

    Delft University of Technology

    Course IN4303, 2016-2017 Compiler Construction

    Guido Wachsmuth, Eelco Visser

    Dataflow Analysis

  • todays lecture

    Dataflow Analysis

    Overview

    Control flow graphs

    intermediate representation

    2

  • todays lecture

    Dataflow Analysis

    Overview

    Control flow graphs

    intermediate representationNon-local optimizations

    dead code elimination constant & copy propagation common subexpression elimination

    2

  • todays lecture

    Dataflow Analysis

    Overview

    Control flow graphs

    intermediate representationNon-local optimizations

    dead code elimination constant & copy propagation common subexpression elimination

    Data flow analyses

    liveness analysis reaching definitions available expressions

    2

  • todays lecture

    Dataflow Analysis

    Overview

    Control flow graphs

    intermediate representationNon-local optimizations

    dead code elimination constant & copy propagation common subexpression elimination

    Data flow analyses

    liveness analysis reaching definitions available expressions

    Optimizations (again)

    2

  • Term Rewriting

    Optimizations

    I

    3

  • optimization

    Dataflow Analysis

    Optimizing Compilers

    Fully optimizing compiler

    Opt(P) produces smallest program with same I/O behavior smallest program for non-terminating programs without I/O:

    Loop = (L : goto L)

    solves halting problem: Opt(Q) = Loop iff Q halts Optimizing compiler

    produces program with same I/O behavior that is smaller or faster

    Full employment theorem for compiler writers

    there is always a better optimizing compiler

    4

  • optimization

    Dataflow Analysis

    Local vs Non-Local Optimizations

    Local optimizations

    peephole optimization constant folding

    pattern match + rewrite

    Non-local optimizations

    constant propagation dead-code elimination

    require information from different parts of program

    5

    iload 1iload 1mul

    iload 1dupmul

    (3 + 4) * x 7 * x

  • optimization

    Dataflow Analysis

    Program Optimizations

    Register allocation

    keep non-overlapping temporaries in same register Common-subexpression elimination

    if expression is computed more than once, eliminate one computation Dead-code elimination

    delete computation whose result will never be used Constant folding

    if operands of expression are constant, do computation at compile-time

    And many more possible optimizations

    6

  • example

    Dataflow Analysis

    Dead Code Elimination

    7

    delete computation whose result will never be used

    a 0b a + 1c c + ba 2 * breturn c

  • example

    Dataflow Analysis

    Dead Code Elimination

    7

    a 0b a + 1c c + b

    return c

    delete computation whose result will never be used

    a 0b a + 1c c + ba 2 * breturn c

  • example

    Dataflow Analysis

    Constant Propagation

    8

    a 0b a + 1c c + ba 2 * breturn c

    substitute reference to constant valued variable

  • example

    Dataflow Analysis

    Constant Propagation

    8

    a 0b 0 + 1c c + ba 2 * breturn c

    a 0b a + 1c c + ba 2 * breturn c

    substitute reference to constant valued variable

  • example

    Dataflow Analysis

    Constant Propagation + Folding

    9

    a 1b a + 1c c + ba 2 * breturn c

  • example

    Dataflow Analysis

    Constant Propagation + Folding

    9

    a 0b 2c c + 2a 4return c

    a 1b a + 1c c + ba 2 * breturn c

  • example

    Dataflow Analysis

    Copy Propagation

    10

    a eb a + 1c c + ba 2 * breturn c

  • example

    Dataflow Analysis

    Copy Propagation

    10

    a eb e + 1c c + ba 2 * breturn c

    a eb a + 1c c + ba 2 * breturn c

  • example

    Dataflow Analysis

    Common Subexpression Elimination

    11

    c a + bd 1e a + b

    if expression is computed more than once, eliminate one computation

  • example

    Dataflow Analysis

    Common Subexpression Elimination

    11

    c a + bd 1e a + b

    x a + bc xd 1e x

    if expression is computed more than once, eliminate one computation

  • optimization

    Dataflow Analysis

    Intraprocedural Global Optimization

    Terminology

    Intraprocedural: within a single procedure or function Global: spans all statements with procedure Interprocedural: operating on several procedures at once

    Recipe

    Dataflow analysis: traverse flow graph, gather information Transformation: modify program using information from analysis

    12

  • Dataflow Analysis

    Control-flow Graphs

    II

    13

  • quadruples

    Dataflow Analysis

    Intermediate Language

    Store

    a b ca b

    Memory access

    a M[b]M[a] b

    Functions

    f(a1, , an)b f(a1, , an)

    Jumps

    L:goto Lif a b goto L1 else goto L2

    14

  • example

    Dataflow Analysis

    Control-Flow Graphs

    15

    a 0L1: b a + 1 c c + b a 2 * b if a < N goto L1 else goto L2L2: return c

  • example

    Dataflow Analysis

    Control-Flow Graphs

    15

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    a 0L1: b a + 1 c c + b a 2 * b if a < N goto L1 else goto L2L2: return c

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    node

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    node

    in-edge

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    node

    in-edge

    in-edge

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    node

    predecessor

    predecessor

    in-edge

    in-edge

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    node

    predecessor

    predecessor

    in-edge

    out-edge

    in-edge

  • terminology

    Dataflow Analysis

    Control-Flow Graphs

    16

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    node

    predecessor

    predecessor

    successor

    in-edge

    out-edge

    in-edge

  • Dataflow Analysis

    Liveness Analysis

    III

    17

  • definition

    Dataflow Analysis

    Liveness Analysis

    Motivation: register allocation

    intermediate code with unbounded number of temporary variables map to bounded number of registers if two variables are not live at the same time, store in same register

    Liveness

    a variable is live if it holds a value that may be needed in the future

    18

  • example

    Dataflow Analysis

    Register Allocation

    19

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

  • example

    Dataflow Analysis

    Register Allocation

    19

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    {a, b} ! r{c} ! q

  • example

    Dataflow Analysis

    Register Allocation

    19

    r 0

    r r + 1

    q q + r

    r 2 * r

    if r < N

    return q

    a 0

    b a + 1

    c c + b

    a 2 * b

    if a < N

    return c

    {a, b} ! r{c} ! q

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c

    if a < N

    b a + 1

    a 2 * b

    c c + b

    example

    Dataflow Analysis

    Liveness Analysis

    20

  • a 0

    return c