optimizing compilers cisc 673 spring 2011 more control flow

25
UNIVERSITY NIVERSITY OF OF D DELAWARE ELAWARE C COMPUTER & OMPUTER & INFORMATION NFORMATION SCIENCES CIENCES DEPARTMENT EPARTMENT Optimizing Compilers CISC 673 Spring 2011 More Control Flow John Cavazos University of Delaware

Upload: apu

Post on 21-Jan-2016

49 views

Category:

Documents


0 download

DESCRIPTION

Optimizing Compilers CISC 673 Spring 2011 More Control Flow. John Cavazos University of Delaware. Overview. Introduced control-flow analysis Basic blocks Control-flow graphs Discuss application of graph algorithms: loops Spanning trees, depth-first spanning trees Dominators Reducibility - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Optimizing CompilersCISC 673

Spring 2011More Control Flow

John CavazosUniversity of Delaware

Page 2: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Overview

Introduced control-flow analysis Basic blocks Control-flow graphs

Discuss application of graph algorithms: loops Spanning trees, depth-first spanning trees Dominators Reducibility Dominator tree Strongly-connected components

Page 3: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Dominance

Node d dominates node i (“d dom i” )if every path from Entry to i includes d

Properties of Dominators Reflexive: a dom a Transitive: if a dom b and b dom c

then a dom c Antisymmetric: if a dom b and b

dom a then b=a

Page 4: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Immediate Dominance

a idom b iff a dom b there is no c such that a dom c, c dom b (c a, c b)

Idom’s: each node has unique idom relation forms a dominator tree

Page 5: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Natural loops

Single entry node (d) no jumps into middle of loop d dominates all nodes in loop

Requires back edge into loop header (n→

d) n is tail, d is head single entry point

Natural loop of back edge (n→ d) d + {all nodes that can reach n with

touching d}

Page 6: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Loops

Reducible: hierarchical, “well-structured” flowgraph reducible iff

all loops in it natural

A

B

C

D

E

F G

Entry

A

B

C

D

E

F G

Entry

reducible graph irreducible graph

Page 7: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Graph Test

Graph is reducible iff …all back edges are ones whose

head dominates its tail

Page 8: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Loops (more examples)

Is this a Natural Loop? Why or why not?

Back edge

Loop Header

Page 9: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Loops (more examples)

Yes, Natural Loop with Multiple Branches

Back edge

Loop Header

Page 10: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Loops (more examples)

Is this a Natural Loop? Why or why not?

Page 11: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Loops (more examples)

Not a Natural Loop: 4→ 1 is back edge, but 1 (head) does not dominate 4 (tail)

Page 12: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Why is this not a Reducible Graph?

Page 13: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Nonreducible Graph

B (head) does not dominate C (tail)

Page 14: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducibility Example

Some languages only permit procedures with reducible flowgraphs (e.g., Java)

“GOTO Considered Harmful”: can introduce irreducibility FORTRAN C C++

Page 15: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Dominance Tree

A

B

C

D

E

F G

Entry

Immediate and other dominators:(excluding Entry) a idom b; a dom a, b, c,

d, e, f, g b idom c; b dom b, c,

d, e, f, g c idom d; c dom c, d, e,

f, g d idom e; d dom d, e, f,

g e idom f, e idom g; e

dom e, f, g

A

B

C

D

E

F G

Entry

control-flow graph dominator tree

Page 16: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Dominator Tree?

Page 17: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Dominator Tree

Page 18: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Reducible Graph?

Construct Spanning Tree to identify back edges. Now check natural back edges property, i.e., head must dominate tail

Page 19: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Natural Loops

Now we can find natural loops Given back edge m → n, natural

loop is n (loop header) and nodes that can reach m without passing through n

Page 20: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Find Natural Loops?

Page 21: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Natural Loops

Back Edge Natural Loop J → G {G,H,J} G → D {D,E,F,G,H,J} D → C {C,D,E,F,G,H,J} H → C {C,D,E,F,G,H,J} I → A {A,B,C,D,E,F,G,H,I,J}

Page 22: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Strongly-Connected Components

What about irreducible flowgraphs? Most general loop form = strongly-

connected component (SCC): subgraph S such that every node in S

reachable from every other node by path including only edges in S

Maximal SCC: S is maximal SCC if it is the largest SCC

that contains S.

Page 23: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

SCC Example

Entry

B1

B2

B3

Strongly-connected components (SCC)(B1, B2), (B2, B3),

(B1, B2, B3)

Maximal SCC(B1, B2, B3)

Page 24: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Computing Maximal SCCs

Tarjan’s algorithm: Computes all maximal SCCs Linear-time (in number of nodes and

edges) CLR algorithm:

Also linear-time Simpler:

Two depth-first searches and one “transpose”:reverse all graph edge

Page 25: Optimizing Compilers CISC 673 Spring 2011 More  Control Flow

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

Next Time

Dataflow analysis Read Marlowe and Ryder paper