Optimizing Compilers CISC 673 Spring 2009 Control Flow

Download Presentation

Optimizing Compilers CISC 673 Spring 2009 Control Flow

Loading in 2 Seconds...

- 95 Views
- Uploaded on
- Presentation posted in: General

Optimizing Compilers CISC 673 Spring 2009 Control Flow

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Optimizing CompilersCISC 673Spring 2009Control Flow

John Cavazos

University of Delaware

- Motivating example: identifying loops
- majority of runtime
- focus optimization on loop bodies!
- remove redundant code, replace expensive operations ) speed up program

- Finding loops:
- easy…

i = 1; j = 1; k = 1;

A1: if i > 1000 goto L1;

A2: if j > 1000 goto L2;

A3: if k > 1000 goto L3;

do something

k = k + 1; goto A3;

L3: j = j + 1; goto A2;

L2: i = i + 1; goto A1;

L1: halt

for i = 1 to 1000

for j = 1 to 1000

for k = 1 to 1000

do something

- or harder(GOTOs)

- Identify basic blocks
- Build control-flow graph
- Analyze CFG to find loops

- Control-flow graph:
- Node: an instruction or sequence of instructions (a basic block)
- Two instructions i, j in same basic blockiff execution of i guarantees execution of j

- Directed edge: potential flow of control
- Distinguished start node Entry
- First instruction in program

- Node: an instruction or sequence of instructions (a basic block)

- Input: sequence of instructions instr(i)
- Identify leaders:first instruction of basic block
- Iterate: add subsequent instructions to basic block until we reach another leader

leaders = instr(1)// first instruction

for i = 1 to |n|// iterate thru all instrs

if instr(i) is a branch

leaders = leaders ∪ targets of instr(i)

leaders = leaders ∪ instr(i+1) // instr after branch

worklist = leaders

While worklist notempty

x = first instruction in worklist

worklist = worklist – {x}

block(x) = {x}

for (i = x + 1; i <= |n| && i notin leaders; i++)

block(x) = block(x) ∪ {i}

leaders = instr(1)

for i = 1 to |n|

if instr(i) is a branch

leaders = leaders

∪ targets of instr(i)

leaders = leaders ∪ instr(i+1)

worklist = leaders

While worklist notempty

x = first instruction in worklist

worklist = worklist – {x}

block(x) = {x}

for (i = x + 1; i <= |n| && i notin

leaders; i++)

block(x) = block(x) ∪ {i}

A = 4

t1 = A * B

L1: t2 = t1/C

if t2 < W goto L2

M = t1 * k

t3 = M + I

L2: H = I

M = t3 – H

if t3 >= 0 goto L4

L3: goto L1

L4: goto L3

- A = 4
- t1 = A * B
- L1: t2 = t1/C
- if t2 < W goto L2
- M = t1 * k
- t3 = M + I
- L2: H = I
- M = t3 – H
- if t3 >= 0 goto L4
- L3: goto L1
- L4: goto L3

Leaders

Basic blocks

- Basic blocks = nodes
- Edges:
- Add directed edge between B1 and B2 if:
- BRANCH from last statement of B1 to first statement of B2 (B2 is a leader), or
- B2 immediately follows B1 in program order and B1 does NOT end with unconditional branch (goto)

- Add directed edge between B1 and B2 if:

Input: block(i), sequence of basic blocks

Output: CFG where nodes are basic blocks

for i = 1 to the number of blocks

x = last instruction of block(i)

if instr(x) is a branch

for each target y of instr(x),

create edge block i to block y

if instr(x) is not unconditional branch,

create edge block i to block i+1

A

- A = 4
- t1 = A * B
- L1: t2 = t1/C
- if t2 < W goto L2
- M = t1 * k
- t3 = M + I
- L2: H = I
- M = t3 – H
- if t3 >= 0 goto L4
- L3: goto L1
- L4: goto L3

Leaders

B

Basic blocks

C

D

E

F

- Identify basic blocks
- Build control-flow graph
- Analyze CFG to find loops

- Spanning trees, depth-first spanning trees
- Reducibility
- Dominators
- Dominator tree
- Strongly-connected components

- Build a tree containing every node and some edges from CFG

A

procedure Span (v)

for w in Succ(v)

if not InTree(w)

add w, v→ w to ST

InTree(w) = true

Span(w)

for v in V do inTree = false

InTree(root) = true

Span(root)

B

C

D

E

F

Tree edge:

in CFG & ST

Advancing edge:

(v,w) not tree edge but w is descendant of v in ST

Back edge:

(v,w): v=w or w is proper ancestor of v in ST

Cross edge:

(v,w): w neither ancestor nor descendant of v in ST

A

B

C

loop

D

E

F

procedure DFST (v)

pre(v) = vnum++

InStack(v) = true

for w in Succ(v)

if not InTree(w)

add v→w to TreeEdges

InTree(w) = true

DFST(w)

else if pre(v) < pre(w)

add v→w to AdvancingEdges

else if InStack(w)

add v→w to BackEdges

else

add v→w to CrossEdges

InStack(v) = false

for v in V do inTree(v) = false

vnum = 0

InTree(root)

DFST(root)

A

1

B

2

C

3

D

4

E

F

5

6

procedure DFST (v)

pre(v) = vnum++

InStack(v) = true

for w in Succ(v)

if not InTree(w)

add v→w to TreeEdges

InTree(w) = true

DFST(w)

else if pre(v) < pre(w)

add v→w to AdvancingEdges

else if InStack(w)

add v→w to BackEdges

else

add v→w to CrossEdges

InStack(v) = false

for v in V do inTree(v) = false

vnum = 0

InTree(root)

DFST(root)

- Unrolling and other loop optimizations for improving instruction level parallelism (ILP)
IDEA 1: Predict Best Unrolling Factor

IDEA 2: Implement Different Loop Opt

Reversal, Interchange, Fusion, Fission

- Policies for method inlining
Question 1: What Method Characteristics are most important for Inlining?

Question 2: What is the impact of Inlining to Register Allocation?

- Escape analysis
IDEA 1 : Move object access that do not “escape” method into registers.

IDEA 2: Allocate objects that do not “escape” on the stack.

- Improving virtual memory behavior
IDEA 1: Performance study of optimizations on virtual memory and/or parts of memory hierarchy (e.g., L3 cache). Use tool to analyze memory behavior.

- Class analysis for safe object inlining
- Policies for object inlining
IDEA 1: Implement object inlining optimization.

- Field Reordering
- Graph Coloring RA
- Porting of old code

- Instruction Scheduling for X86
- Porting PowerPC version to X86

- Autotuning Java applications
- Interesting!

- Reducibility
- Dominance
- Wikipedia: Dominator (graph_theory)

- Some Dataflow
- T.J. Marlowe and B.G. Ryder Properties of Data Flow Frameworks, pp. 121-163, ACTA Informatica, 28, 1990.