Control-Flow Analysis and Loop Detection | CS 553, Study notes of Computer Science

Material Type: Notes; Class: Algorithmic Language Compilers; Subject: Computer Science; University: Colorado State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-ro0
koofers-user-ro0 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS553 Lecture Control-Flow and Loop Detection 2
Control-Flow Analysis and Loop Detection
Last time
Speeding up data-flow analysis
Today
Control-flow analysis
Loops
Identifying loops using dominators
Reducibility
CS553 Lecture Control-Flow and Loop Detection 3
Context
Data-flow
Flow of data values from defs to u ses
Could alternatively be represented as a data dependence
Control-flow
Sequencing of operations
Could alternatively be represented as a control dependence
e.g., Evaluation of then-code and e lse-code depends on if-test,
CS553 Lecture Control-Flow and Loop Detection 4
Why study control flow an alysis?
Finding Loops
most computation time is spent in l oops
to optimize them, we need to find t hem
Loop Optimizations
Loop-invariant code hoisting
Induction variable elimination
Array bounds check removal
Loop unrolling
Parallelization
...
Identifying structured control flo w
can be used to speed up da ta-flow analysis
CS553 Lecture Control-Flow and Loop Detection 5
Representing Control-Flow
High-level representation
Control flow is implicit in an AST
Low-level representation:
Use a Control-flow graph
Nodes represent statements
Edges represent explicit flow of control
Other options
Control dependences in program d ependence graph (PDG) [Ferrante87]
Dependences on explicit state in va lue dependence graph (VDG) [Weise 94]
pf3
pf4
pf5

Partial preview of the text

Download Control-Flow Analysis and Loop Detection | CS 553 and more Study notes Computer Science in PDF only on Docsity!

CS553 Lecture Control-Flow and Loop Detection 2

Control-Flow Analysis and Loop Detection

Last time

  • Speeding up data-flow analysis Today
  • Control-flow analysis
  • Loops
  • Identifying loops using dominators
  • Reducibility CS553 Lecture Control-Flow and Loop Detection 3

Context

Data-flow

  • Flow of data values from defs to uses
  • Could alternatively be represented as a data dependence Control-flow
  • Sequencing of operations
  • Could alternatively be represented as a control dependence
  • e.g., Evaluation of then-code and else-code depends on if-test, CS553 Lecture Control-Flow and Loop Detection 4

Why study control flow analysis?

Finding Loops

  • most computation time is spent in loops
  • to optimize them, we need to find them Loop Optimizations
  • Loop-invariant code hoisting
  • Induction variable elimination
  • Array bounds check removal
  • Loop unrolling
  • Parallelization
  • ... Identifying structured control flow
  • can be used to speed up data-flow analysis CS553 Lecture Control-Flow and Loop Detection 5

Representing Control-Flow

High-level representation

  • Control flow is implicit in an AST Low-level representation:
  • Use a Control-flow graph
  • Nodes represent statements
  • Edges represent explicit flow of control Other options
  • Control dependences in program dependence graph (PDG) [Ferrante87]
  • Dependences on explicit state in value dependence graph (VDG) [Weise 94]

CS553 Lecture Control-Flow and Loop Detection 6

What Is Control-Flow Analysis?

Control-flow analysis discovers the flow of control within a procedure ( e.g., builds a CFG, identifies loops) Example 1 a := 0 2 b := a * b 3 L1: c := b/d 4 if c < x goto L 5 e := b / c 6 f := e + 1 7 L2: g := f 8 h := t - g 9 if e > 0 goto L 10 goto L 11 L3: return No Yes 1 a := 0 b := a * b (^3) c := b/d c < x? (^5) e := b / c f := e + 1 7 g := f h := t - g e > 0? 10 goto 11 return CS553 Lecture Control-Flow and Loop Detection 7

Loop Concepts

Loop : Strongly connected component of CFG with a single entry point (header) Loop entry edge : Source not in loop & target in loop Loop exit edge : Source in loop & target not in loop Loop header node : Target of loop entry edge Natural loop : Nodes with path to backedge without going through header. Back edge : Target is loop header & source is in the loop Loop tail node : Source of back edge Loop preheader node : Single node that’s source of the loop entry edge Nested loop : Loop whose header is inside another loop CS553 Lecture Control-Flow and Loop Detection 8 entry edge

Picturing Loop Terminology

preheader exit edge loop back edge tail head CS553 Lecture Control-Flow and Loop Detection 9 h t p pre-header

The Value of Preheader Nodes

Not all loops have preheaders

  • Sometimes it is useful to create them Without preheader node
  • There can be multiple entry edges With single preheader node
  • There is only one entry edge Useful when moving code outside the loop
  • Don’t have to replicate code for multiple entry edges h t

CS553 Lecture Control-Flow and Loop Detection 14 {n, p, q, r, s} {n, p, q, r, s}{n, p, q,^ r, s}

Computing Dominators (example)

Input : Set of nodes N and an entry node s Output : Dom[i] = set of all nodes that dominate node i Dom[s] = {s} for each n ∈ N – {s} Dom[n] = N repeat change = false for each n ∈ N – {s} D = {n} ∪ (∩p∈pred(n) Dom[p]) if D ≠ Dom[n] change = true Dom[n] = D until !change n p r Initially Dom[s] = {s} Dom[q] = {n, p, q, r, s}... Finally Dom[q] = Dom[r] = Dom[p] = Dom[n] = s {n, p, q, r, s} {n, p, q, r, s} {s} {q, s} {r, s} {p, s} {n, p, s} {n, p, q, r, s} (^) q {n, p, q, r, s} {n, p, q, r, s} CS553 Lecture Control-Flow and Loop Detection 15

Reducibility

Definition

  • A CFG is reducible (well-structured) if we can partition its edges into two disjoint sets, the forward edges and the back edges, such that - The forward edges form an acyclic graph in which every node can be reached from the entry node - The back edges consist only of edges whose targets dominate their sources
  • A CFG is reducible if it can be converted into a single node using T1 and T2 transformations. Structured control-flow constructs give rise to reducible CFGs Value of reducibility
  • Dominance useful in identifying loops
  • Simplifies code transformations (every loop has a single header)
  • Permits interval analysis and it is easy to calculate the CFG depth CS553 Lecture Control-Flow and Loop Detection 16

T1 and T2 transformations

T1 transformation

  • remove self-cycles T2 transformation
  • if node n has a unique predecessor p, then remove n and make all the successors for n be successors for p b c a d (^) c ab d a a CS553 Lecture Control-Flow and Loop Detection 17

Handling Irreducible CFG’s

Node splitting

  • Can turn irreducible CFGs into reducible CFGs b c a d e b c a d d′ e′ e

CS553 Lecture Control-Flow and Loop Detection 18

Why Go To All This Trouble?

Modern languages provide structured control flow

  • Shouldn’t the compiler remember this information rather than throw it away and then re-compute it? Answers?
  • We may want to work on the binary code in which case such information is unavailable
  • Most modern languages still provide a goto statement
  • Languages typically provide multiple types of loops. This analysis lets us treat them all uniformly
  • We may want a compiler with multiple front ends for multiple languages; rather than translate each language to a CFG, translate each language to a canonical LIR, and translate that representation once to a CFG CS553 Lecture Control-Flow and Loop Detection 19

Concepts

Control-flow analysis Control-flow graph (CFG) Loop terminology Identifying loops Dominators Reducibility CS553 Lecture Control-Flow and Loop Detection 20

Next Time

Assignments

  • Project 2 due: the writeup is IMPORTANT Reading
  • Ch 18. Lecture
  • Loop invariant code motion