Docsity

Data Flow Analysis: Understanding Flow- and Context-Sensitivity, Slides of Compilers

Partial preview of the text

Download Data Flow Analysis: Understanding Flow- and Context-Sensitivity and more Slides Compilers in PDF only on Docsity!

Everything Else About Data Flow

Analysis

Flow- and Context-Sensitivity Logical Representation Pointer Analysis Interprocedural Analysis

Three Levels of Sensitivity

  • In DFA so far, we have cared about where in the program we are. - Called flow-sensitivity.
  • But we didn’t care how we got there.
    • Called context-sensitivity.
  • We could even care about neither.
    • Example: where could x ever be defined in this program?

Context Sensitivity

  • Can distinguish paths to a given point.
  • Example: If we remembered paths, we would not have the problem in the constant- propagation framework where x+y = 5 but neither x nor y is constant over all paths.

The Example Again

5

x = 3 y = 2

x = 2 y = 3

z = x+y

Context-Sensitivity --- (2)

  • Loops and recursive calls lead to an infinite number of contexts.
  • Generally used only for interprocedural analysis, so forget about loops.
  • Need to collapse strong components of the calling graph to a single group.
  • “Context” becomes the sequence of groups on the calling stack.

Example: Calling Graph

8

main

p

r s

q

t (^) Contexts: Green Green, pink Green, yellow Green, pink, yellow

Logical Representation

  • We have used a set-theoretic formulation of DFA. - IN = set of definitions, e.g.
  • There has been recent success with a logical formulation, involving predicates.
  • Example: Reach(d,x,i) = “definition d of variable x can reach point i.”

Comparison: Sets Vs. Logic

  • Both have an efficiency enhancement.
    • Sets: bit vectors and boolean ops.
    • Logic: BDD’s, incremental evaluation.
  • Logic allows integration of different aspects of a flow problem. - Think of PRE as an example. We needed 6 stages to compute what we wanted.

Example: Datalog Rules

Reach(d,x,j) :- Reach(d,x,i) &

StatementAt(i,s) & NOT Assign(s,x) & Follows(i,j)

Reach(s,x,j) :- StatementAt(i,s) &

Assign(s,x) & Follows(i,j)

Datalog --- (2)

  • Intuition: subgoals in the body are combined by “and” (strictly speaking: “join”).
  • Intuition: Multiple rules for a predicate (head) are combined by “or.”

EDB Vs. IDB Predicates

  • Some predicates come from the program, and their tuples are computed by inspection. - Called EDB , or extensional database predicates.
  • Others are defined by the rules only.
    • Called IDB , or intensional database predicates.

Iterative Algorithm for Datalog

  • Start with the EDB predicates = “whatever the code dictates,” and with all IDB predicates empty.
  • Repeatedly examine the bodies of the rules, and see what new IDB facts can be discovered from the EDB and existing IDB facts.

Example: Seminaive

Path(x,y) :- Arc(x,y)

Path(x,y) :- Path(x,z) & Path(z,y) NewPath(x,y) = Arc(x,y); Path(x,y) = ∅; while (NewPath != ∅) do { NewPath(x,y) = {(x,y) | NewPath(x,z) && Path(z,y) || Path(x,z) && NewPath(z,y)} – Path(x,y); Path(x,y) = Path(x,y) ∪ NewPath(x,y); }

Stratification

  • A risk occurs if there are negated literals involved in a recursive predicate. - Leads to oscillation in the result.
  • Requirement for stratification :
    • Must be able to order the IDB predicates so that if a rule with P in the head has NOT Q in the body, then Q is either EDB or earlier in the order than P.