Control Flow Ana 2

Embed Size (px)

Citation preview

  • 7/28/2019 Control Flow Ana 2

    1/34

    Control Flow Analysis - Part 2

    Y.N. Srikant

    Department of Computer ScienceIndian Institute of Science

    Bangalore 560 012

    NPTEL Course on Compiler Design

    Y.N. Srikant Control Flow Analysis

    http://goforward/http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    2/34

    Outline of the Lecture

    Why control flow analysis?

    Dominators and natural loops

    Intervals and reducibilityT1 T2 transformations and graph reduction

    Regions

    Topic 1 and part of topic 2 were covered in part 1 of the lecture

    Y.N. Srikant Control Flow Analysis

    http://find/
  • 7/28/2019 Control Flow Ana 2

    3/34

    Why Control-Flow Analysis?

    Control-flow analysis (CFA) helps us to understand thestructure of control-flow graphs (CFG)

    To determine the loop structure of CFGs

    Formulation of conditions for code motion use dominator

    information, which is obtained by CFAConstruction of the static single assignment form (SSA)

    requires dominance frontier information from CFA

    It is possible to use interval structure obtained from CFA to

    carry out data-flow analysis

    Finding Control dependence, which is needed in

    parallelization, requires CFA

    Y.N. Srikant Control Flow Analysis

    http://find/
  • 7/28/2019 Control Flow Ana 2

    4/34

    Dominators

    We say that a node d in a flow graph dominatesnode n,

    written d dom n, if every path from the initial node of theflow graph to n goes through d

    Initial node is the root, and each node dominates only its

    descendents in the tree (including itself)

    The node x strictly dominates y, if x dominates y and

    x= y

    x is the immediate dominatorof y (denoted idom(y)), if xis the closest strict dominator of y

    A dominator treeshows all the immediate dominator

    relationshipsPrinciple of the dominator algorithm

    If p1, p2, ..., pk, are all the predecessors of n, and d= n,then d dom n, iff d dom pi for each i

    Y.N. Srikant Control Flow Analysis

    http://find/
  • 7/28/2019 Control Flow Ana 2

    5/34

    Dominator Example

    Y.N. Srikant Control Flow Analysis

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    6/34

    Dominators and Natural Loops

    Edges whose heads dominate their tails are called back

    edges(a b : b = head, a= tail)

    Given a back edge n d

    The natural loopof the edge is d plus the set of nodes thatcan reach n without going through dd is the header of the loop

    A single entry point to the loop that dominates all nodes in

    the loop

    Atleast one path back to the header exists (so that the loop

    can be iterated)

    Y.N. Srikant Control Flow Analysis

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    7/34

    Algorithm for finding the Natural Loop of a Back Edge

    /* The back edge under consideration is n d /*{ stack = empty; loop = {d};

    /* This ensures that we do not look at predecessors of d */insert(n);

    while (stack is not empty) do {

    pop(m, stack);

    for each predecessor p of m do insert(p);

    }

    }

    procedure insert(m) {

    if m / loop then {loop = loop {m};push(m, stack);

    }

    }

    Y.N. Srikant Control Flow Analysis

    D i B k Ed d N l L

    http://find/
  • 7/28/2019 Control Flow Ana 2

    8/34

    Dominators, Back Edges, and Natural Loops

    Y.N. Srikant Control Flow Analysis

    D i t B k Ed d N t l L

    http://find/
  • 7/28/2019 Control Flow Ana 2

    9/34

    Dominators, Back Edges, and Natural Loops

    Y.N. Srikant Control Flow Analysis

    D th Fi t N b i f N d i CFG

    http://find/
  • 7/28/2019 Control Flow Ana 2

    10/34

    Depth-First Numbering of Nodes in a CFG

    Y.N. Srikant Control Flow Analysis

    D th Fi t N b i E l 1

    http://find/
  • 7/28/2019 Control Flow Ana 2

    11/34

    Depth-First Numbering Example 1

    Y.N. Srikant Control Flow Analysis

    Depth First Numbering Example 2

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    12/34

    Depth-First Numbering Example 2

    Y.N. Srikant Control Flow Analysis

    Reducibility

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    13/34

    Reducibility

    A flow graph G is reducible iffits edges can be partitioned into two disjoint groups,

    forwardedges and backedges (back edge: heads

    dominate tails)

    forward edges form a DAG in which every node can be

    reached from the initial node of G

    In a reducible flow graph, all retreating edges in a DFS will

    be back edges

    In an irreducible flow graph, some retreating edges will

    NOT be back edges and hence the graph of forwardedges will be cyclic

    Y.N. Srikant Control Flow Analysis

    Reducibility Example 1

    http://find/
  • 7/28/2019 Control Flow Ana 2

    14/34

    Reducibility - Example 1

    Y.N. Srikant Control Flow Analysis

    Reducibility Example 2

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    15/34

    Reducibility - Example 2

    Y.N. Srikant Control Flow Analysis

    Inner Loops

    http://find/
  • 7/28/2019 Control Flow Ana 2

    16/34

    Inner Loops

    Unless two loops have the same header, they are eitherdisjoint or one is nested within the other

    Nesting is checked by testing whether the set of nodes of a

    loop A is a subset of the set of nodes of another loop B

    Similarly, two loops are disjoint if their sets of nodes aredisjoint

    When two loops share a header, neither of these may hold

    (see next slide)

    In such a case the two loops are combined andtransformed as in the next slide

    Y.N. Srikant Control Flow Analysis

    Inner Loops and Loops with the same header

    http://find/
  • 7/28/2019 Control Flow Ana 2

    17/34

    Inner Loops and Loops with the same header

    Y.N. Srikant Control Flow Analysis

    Preheader

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    18/34

    Preheader

    Y.N. Srikant Control Flow Analysis

    Depth of a Flow Graph and Convergence of DFA

    http://find/
  • 7/28/2019 Control Flow Ana 2

    19/34

    Depth of a Flow Graph and Convergence of DFA

    Given a depth-first spanning tree of a CFG, the largest

    number of retreating edges on any cycle-free path is thedepthof the CFG

    The number of passes needed for convergence of the

    solution to a forward DFA problem is (1 + depth of CFG)

    One more pass is needed to determine no change, andhence the bound is actually (2 + depth of CFG)

    This bound can be actually met if we traverse the CFG

    using the depth-first numberingof the nodes

    For a backward DFA, the same bound holds, but we must

    consider the reverse of the depth-first numbering of nodes

    Any other order will still produce the correct solution, but

    the number of passes may be more

    Y.N. Srikant Control Flow Analysis

    Depth of a CFG - Example 1

    http://find/
  • 7/28/2019 Control Flow Ana 2

    20/34

    Depth of a CFG Example 1

    Y.N. Srikant Control Flow Analysis

    Depth of a CFG - Example 2

    http://find/
  • 7/28/2019 Control Flow Ana 2

    21/34

    Depth of a CFG Example 2

    Y.N. Srikant Control Flow Analysis

    Intervals

    http://find/
  • 7/28/2019 Control Flow Ana 2

    22/34

    Intervals

    Intervals have a header node that dominates all nodes in

    the intervalGiven a flow graph G with initial node n0, and a node nofG, the interval with header n, denoted I(n) is defined asfollows

    1 n is in I(n)

    2 If all the predecessors of some node m= n0 are in I(n),then m is in I(n)

    3 Nothing else is in I(n)

    Constructing I(n)

    I(n) := {n};while (there exists a node m= n0, all of whosepredecessors are in I(n)) do I(n) := I(n) {m};

    Y.N. Srikant Control Flow Analysis

    Partitioning a Flow Graph into Disjoint Intervals

    http://find/
  • 7/28/2019 Control Flow Ana 2

    23/34

    g p j

    Mark all nodes as unselected;Construct I(n0); /* n0 is the header of I(n0) */Mark all the nodes in I(n0) as selected;while (there is a node m, not yet marked selected,

    but with a selected predecessor) do {

    Construct I(m);/* m is the header of I(m) */Mark all nodes in I(m) as selected;

    }

    Note: The order in which interval headers are picked does not

    alter the final partition

    Y.N. Srikant Control Flow Analysis

    Intervals and Reducibility - 1

    http://find/http://goback/
  • 7/28/2019 Control Flow Ana 2

    24/34

    y

    Y.N. Srikant Control Flow Analysis

    Intervals and Reducibility - 2

    http://find/
  • 7/28/2019 Control Flow Ana 2

    25/34

    y

    Y.N. Srikant Control Flow Analysis

    Interval Graphs

    http://find/
  • 7/28/2019 Control Flow Ana 2

    26/34

    p

    Intervals correspond to nodesInterval containing n0 is the initial node of I(G)

    If there is an edge from a node in interval I(m) to theheader of the interval I(n), in G, then there is an edge fromI(m) to I(n) in I(G)

    We make intervals in interval graphs and reduce them

    further

    Finally, we reach a limit flow graph, which cannot be

    reduced further

    A flow graph is reducible iff its limit flow graph is a single

    node

    Y.N. Srikant Control Flow Analysis

    Node Splitting

    http://find/
  • 7/28/2019 Control Flow Ana 2

    27/34

    p g

    If we reach a limit flow graph that is other than a single

    node, we can proceed further only if we split one or morenodes

    If a node has k predecessors, we may replace nby k

    nodes, n1, n2, ..., nk

    The ith predecessor of nbecomes the predecessor of nionly, while all successors of nbecome successors of the

    nis

    After splitting, we continue reduction and splitting again (if

    necessary), to obtain a single node as the limit flow graph

    The node to be split is picked up arbitrarily, say, the nodewith largest number of predecessors

    However, success is not guaranteed

    Y.N. Srikant Control Flow Analysis

    Node Splitting Example

    http://find/
  • 7/28/2019 Control Flow Ana 2

    28/34

    p g p

    Y.N. Srikant Control Flow Analysis

    T1 T2 Transformations and Graph Reduction

    http://find/
  • 7/28/2019 Control Flow Ana 2

    29/34

    Transformation T1: If n is a node with a loop, i.e., an edgen nexists, then delete that edge

    Transformation T2: If there is a node n, not the initial

    node, that has a unique predecessor m, then m may

    consume nby deleting nand making all successors of n(including n, possibly) be successors of m

    By applying the transformations T1 and T2 repeatedly in

    any order, we reach the limit flow graph

    Node splitting may be necessary as in the case of interval

    graph reduction

    Y.N. Srikant Control Flow Analysis

    Example of T1 T2 Reduction

    http://find/
  • 7/28/2019 Control Flow Ana 2

    30/34

    Y.N. Srikant Control Flow Analysis

    Example of T1 T2 Reduction

    http://find/
  • 7/28/2019 Control Flow Ana 2

    31/34

    Y.N. Srikant Control Flow Analysis

    Example of T1 T2 Reduction

    http://find/
  • 7/28/2019 Control Flow Ana 2

    32/34

    Y.N. Srikant Control Flow Analysis

    Regions

    http://find/
  • 7/28/2019 Control Flow Ana 2

    33/34

    A set of nodes N that includes a header, which dominates

    all other nodes in the region

    All edges between nodes in N are in the region, except(possibly) for some of those that enter the headerAll intervals are regions but there are regions that are notintervals

    A region may omit some nodes that an interval wouldinclude or they may omit some edges back to the headerFor example, I(7) = {7, 8, 9, 10, 11}, but {8, 9, 10} could bea region

    A region may have multiple exitsAs we reduce a flow graph Gby T1 and T2 transformations,at all times, the following conditions are true

    1 A node represents a region of G2 An edge from a to b in a reduced graph represents a set of

    edges3 Each node and edge of G is represented by exactly one

    node or edge of the current graph

    Y.N. Srikant Control Flow Analysis

    Region Example

    http://find/
  • 7/28/2019 Control Flow Ana 2

    34/34

    Y.N. Srikant Control Flow Analysis

    http://find/