+ All Categories
Home > Documents > Optimizing Compilers CISC 673 Spring 2009 Control Flow

Optimizing Compilers CISC 673 Spring 2009 Control Flow

Date post: 03-Jan-2016
Category:
Upload: faith-rutledge
View: 24 times
Download: 7 times
Share this document with a friend
Description:
Optimizing Compilers CISC 673 Spring 2009 Control Flow. John Cavazos University of Delaware. Control-Flow Analysis. Motivating example: identifying loops majority of runtime focus optimization on loop bodies! remove redundant code, replace expensive operations ) speed up program - PowerPoint PPT Presentation
23
UNIVERSITY NIVERSITY OF OF D DELAWARE ELAWARE C COMPUTER & OMPUTER & INFORMATION NFORMATION SCIENCES CIENCES DEPARTMENT EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Control Flow John Cavazos University of Delaware
Transcript
Page 1: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT

Optimizing CompilersCISC 673

Spring 2009Control Flow

John CavazosUniversity of Delaware

Page 2: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 2

Control-Flow Analysis Motivating example: identifying loops

majority of runtime focus optimization on loop bodies!

remove redundant code, replace expensive operations ) speed up program

Finding loops: easy… for i = 1 to 1000

for j = 1 to 1000 for k = 1 to 1000 do something

1 i = 1; j = 1; k = 1;2 A1: if i > 1000 goto L1;3 A2: if j > 1000 goto L2;4 A3: if k > 1000 goto L3;5 do something6 k = k + 1; goto A3;7 L3: j = j + 1; goto A2;8 L2: i = i + 1; goto A1;9 L1: halt

or harder(GOTOs)

Page 3: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 3

Steps to Finding Loops

1. Identify basic blocks2. Build control-flow graph3. Analyze CFG to find loops

Page 4: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 4

Control-Flow Graphs

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

Page 5: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 5

Identifying Basic Blocks

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

Page 6: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 6

Basic Block Partition Algorithm

leaders = instr(1) // first instruction

for i = 1 to |n| // iterate thru all instrsif instr(i) is a branch

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

worklist = leadersWhile worklist not empty

x = first instruction in worklistworklist = worklist – {x}block(x) = {x}for (i = x + 1; i <= |n| && i not in leaders; i++)

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

Page 7: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 7

Class Exampleleaders = instr(1)for i = 1 to |n|

if instr(i) is a branchleaders = leaders

∪ targets of instr(i)leaders = leaders ∪ instr(i+1)

worklist = leadersWhile worklist not empty

x = first instruction in worklistworklist = worklist – {x}block(x) = {x}for (i = x + 1; i <= |n| && i not in

leaders; i++)block(x) = block(x) ∪ {i}

1 A = 42 t1 = A * B3 L1: t2 = t1/C4 if t2 < W goto L25 M = t1 * k6 t3 = M + I7 L2: H = I8 M = t3 – H9 if t3 >= 0 goto L410 L3: goto L111 L4: goto L3

Page 8: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 8

Basic Block Example

Leaders

Basic blocks

1 A = 42 t1 = A * B3 L1: t2 = t1/C4 if t2 < W goto L25 M = t1 * k6 t3 = M + I7 L2: H = I8 M = t3 – H9 if t3 >= 0 goto L410 L3: goto L111 L4: goto L3

Page 9: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 9

Control-Flow Edges

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)

Page 10: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 10

Control-Flow Edge Algorithm

Input: block(i), sequence of basic blocksOutput: CFG where nodes are basic blocks

for i = 1 to the number of blocksx = 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

Page 11: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 11

CFG Edge Example

Leaders

Basic blocks

1 A = 42 t1 = A * B3 L1: t2 = t1/C4 if t2 < W goto L25 M = t1 * k6 t3 = M + I7 L2: H = I8 M = t3 – H9 if t3 >= 0 goto L410 L3: goto L111 L4: goto L3

Page 12: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 12

Steps to Finding Loops

1. Identify basic blocks2. Build control-flow graph3. Analyze CFG to find loops

Spanning trees, depth-first spanning trees

Reducibility Dominators Dominator tree Strongly-connected components

Page 13: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 13

Spanning Tree

Build a tree containing every node and some edges from CFG

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 = falseInTree(root) = trueSpan(root)

A

B

C

D

E F

Page 14: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 14

CFG Edge Classification

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

D

E F

loop

Page 15: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 15

Depth-first spanning treeprocedure 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 = 0InTree(root)DFST(root)

A

B

C

D

E F

1

2

3

4

5 6

Page 16: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 16

Class Problem: Identify Edgesprocedure 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 = 0InTree(root)DFST(root)

Page 17: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 17

Compiler Optimizations I

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

Page 18: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 18

Compiler Optimizations II

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?

Page 19: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 19

Compiler Optimizations III

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.

Page 20: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 20

Compiler Optimizations IV

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.

Page 21: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 21

Compiler Optimizations V

Class analysis for safe object inlining

Policies for object inlining

IDEA 1: Implement object inlining optimization.

Page 22: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 22

Compiler Optimizations VI

Field Reordering Graph Coloring RA

Porting of old code Instruction Scheduling for X86

Porting PowerPC version to X86 Autotuning Java applications

Interesting!

Page 23: Optimizing Compilers CISC 673 Spring 2009 Control Flow

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 23

Next Time

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.


Recommended