+ All Categories
Home > Documents > Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in...

Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in...

Date post: 18-Jan-2018
Category:
Upload: britton-jacobs
View: 218 times
Download: 0 times
Share this document with a friend
Description:
This Lecture Considerations for optimization Safety Profitability Opportunity Optimization beyond a local scope Extended basic blocks Dominator trees COMP 512, Rice University3
22
Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Comp 512 Spring 2011
Transcript
Page 1: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

Terminology, Principles, and Concerns, II

With examples from superlocal value numbering (Ch 8 in EaC2e)

Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved.Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved.

Comp 512Spring 2011

Page 2: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

Last Lecture

Scopes of optimization• Local, regional, global (single procedure), whole program

Redundancy elimination as an example• Local value numbering algorithm • Linear time in size of block• Platform for folding constants, simplifying algebraic identities• Good naming scheme can simplify algorithm

Unique name for each definition of a variable

COMP 512, Rice University

2

Page 3: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

This Lecture

Considerations for optimization• Safety• Profitability• Opportunity

Optimization beyond a local scope• Extended basic blocks• Dominator trees

COMP 512, Rice University

3

Page 4: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

4

Optimization

In discussing any optimization, look for three issues

Safety – Does it change the results of the computation?

• Safety is proven with results of analysis• Data-flow analysis or other special case analysis

Profitability – Is it expected to speed up execution?

• Many authors assume transformations are always profitable• Use either heuristics or a careful algebra of costs

Opportunity – Can we efficiently locate places to apply it?

• Can we find find all the places where the transformation works?• Do we need to update safety information afterward?

Should think about these issues with every transformation

*

Page 5: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

5

Safety

The first principle of optimizationThe compiler must preserve the code’s “meaning”

When can the compiler transform the code?• Original & transformed code must have the same final state• Variables that are visible at exit• Equality of result, not equality of method (ignore

temporaries)

Formal notion

Different translations with identical results are fine

For two expressions, M and N, we say that M and N are observationally equivalent if and only if, in any context C where both M and N are closed (that is, have no free variables), evaluating C[M] and C[N] either produces identical results or neither terminates.

Plotkin, 1975

compilation

Page 6: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

6

Safety

In practice, compilers use a simpler notion of equivalence

• This restatement ignores divergence• If e’ is faster than e, the transformation is profitable

Equivalence and context• Compiled code always executes in some context• Optimization is the art of capitalizing on context• Lack of context fully general (i.e., slow) code

Some compilers employ a worse standard (FORTRAN)

• Correct behavior for “standard conforming” code• Undefined behavior for other code

If, in their actual program context, the result of evaluating e’ cannot be distinguished from the result of evaluating e, the compiler can substitute e’ for e.

Page 7: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

7

Safety

My favorite bad quote on safety

The point• You must not violate the first principle• Without the first principle, just compile a return and be done

You, as a compiler writer, must decide if it’s worth the risk of doing this kind of optimization. It’s difficult for the compiler to distinguish between the safe and dangerous cases, here. For example, many C compilers perform risky optimizations because the compiler writer has assumed that a C programmer can understand the problems and take steps to remedy them at the source code level. It’s better to provide the maximum optimization, even if it’s dangerous, than to be conservative at the cost of less efficient code. A Pascal programmer may not have the same level of sophistication as a C programmer, however, so the better choice in this situation might be to avoid the risky optimization entirely or to require a special command-line switch to enable the optimization.

Allen Holub, Compiler Design in C, Prentice Hall, 1990, p. 677

Like N. Wirth?

Gone in second edition

*

Page 8: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

8

Safety & Value Numbering

Why is local value numbering safe?• Hash table starts empty• Expressions placed in table as processed• If <operator,VN(o1),VN(o2)> is in the table, then

It has already occurred at least once in the block Neither o1 nor o2 have been subsequently redefined

The mapping uses VN(o1) and VN(o2), not o1 and o2 If either was redefined, it would have a new VN

If <operator,VN(o1),VN(o2)> has a VN, the compiler can safely use it

• Algorithm incrementally constructs a proof that <operator,VN(o1),VN(o2)> is either redundant or not

• Algorithm modifies the code, but does not invalidate the table

Critical property of a basic block:If any statement executes, they all execute, in a predetermined order

Page 9: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

9

Profitability

The compiler should only transform the code when it helps!• Eliminating one or more operations• Replacing an operation with a cheaper one• Moving an operation to a place where it will execute fewer times

Sometimes, we can prove profitability Fold a constant expression into an immediate operation

Sometimes, we must guess Eliminating a redundant operation in a loop

Sometimes, we cannot tell … Inlining in a Fortran compiler

We need to consider the issue explicitly ...

Compiler writers should know when they cannot tell if some transformation is profitable !

*

Page 10: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

10

When is local value numbering profitable?

• If reuse is cheaper than re-computation Does not cause a spill or a copy (hard to determine) In practice, assumed to be true

• Local constant folding is always profitable Re-computing uses a register, as does load immediate Immediate form of operation avoids even that cost

• Algebraic identities If it eliminates an operation, it is profitable (x +

0) Profitability of simplification depends on target (2x x+x) Easy to factor target machine costs into the implementation

Do not perform the unprofitable rewrites

Profitability & Value Numbering

Page 11: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

11

Opportunity

To perform an optimization, the compiler must locate all the places in the code where it can be applied• Allows compiler to evaluate each possible application• Leads to efficient application of the transformation• Avoids additional search

Approaches• Perform analysis to find opportunities

VERYBUSY expressions & code hoisting• Look at every operation

Value numbering, loop invariant code motion• Iterate over subset of the IR

Operator strength reduction on SSA

…x+y…

…x+y…

Page 12: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

12

Opportunity & Value Numbering

How does local value numbering find opportunity?• Linear scan over block, in execution order• Incrementally constructs a model of program state• At each operation, checks for opportunities

Summary• It performs an exhaustive search of the opportunities• This answer is not satisfying, but it is true

Search is linear in |operations| Must keep cost of checking each operation in check

For example, use a tree of algebraic identities by operator• Hashing keeps cost down to O(1) per operand + per operation

Page 13: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

13

Missed opportunities(need stronger methods)

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

Value Numbering

Local Value Numbering• 1 block at a time• Strong local results• No cross-block effects

*

Page 14: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

14

An Aside on Terminology

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

Control-flow graph (CFG)•Nodes for basic blocks•Edges for branches•Basis for much of program

analysis & transformation

This CFG, G = (N,E)•N = {A,B,C,D,E,F,G}•E = {(A,B),(A,C),(B,G),

(C,D), (C,E),(D,F),(E,F),(F,E)}

• |N| = 7, |E| = 8

Page 15: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

15

Regional Optimization: Extended Basic Blocks

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F An Extended Basic Block (EBB)• Set of blocks b1, b2, …, bn

• b1 has > 1 predecessor• All other bi have 1 predecessor• EBBs provide more context for optimization

*

Page 16: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

16

Regional Optimization: Extended Basic Blocks

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

An EBB contains 1 or more pathIf b1, b2, …, bn is a path• b1 has > 1 predecessor• bi has 1 predecessor, bi-1

*

{A, B, C, D, E} is an EBB• {A,B}, {A,C,D}, and {A,C,E} are paths in {A, B, C, D, E}{F} and {G} are also EBBs• They have only trivial paths

Page 17: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

17

Superlocal Value Numbering

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

FThe Concept• Apply local method to each path in the EBB• Do {A,B}, {A,C,D}, & {A,C,E}• Obtain reuse from ancestors• Does not help with F or G• Key: avoid re-analyzing A & C

*

Superlocal ≈ path in the EBB

Page 18: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

18

Superlocal Value Numbering

Efficiency• Use A’s table to initialize tables for B & C• To avoid duplication, use a scoped hash table

A, AB, A, AC, ACD, AC, ACE, F, G• Need a VN name mapping to handle kills

Must restore map with scope Adds complication, not cost

To simplify matters• Unique name for each definition• Makes name VN• Use the SSA name space

EaC: § 5.7.3 & App. B

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

Subscripted names from example in last lecture

Page 19: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

19

Aside: SSA Name Space (locally)

Example (from lecture 3)

With VNs a0

3 x01 + y0

2

b03 x0

1 + y02

a14 17

c03 x0

1 + y02

Notation:• While complex, the meaning is clear

Original Code a0 x0 + y0

b0 x0 + y0

a1 17 c0 x0 + y0

Renaming:• Give each value a unique name• Makes it clear

Rewritten a0

3 x01 + y0

2

b03 a0

3

a14 17

c03 a0

3

Result:• a0

3 is available• rewriting works

These are SSA names …

Page 20: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

20

Aside: SSA Name Space (in general)

Two principles• Each name is defined by exactly one operation• Each operand refers to exactly one definition

To reconcile these principles with real code• Insert -functions at merge points to reconcile name space• Add subscripts to variable names for uniqueness

We’ll look at how to construct SSA form in a week or two

x ... x ...

... x + ...

x0 ... x1 ...

x2 (x0,x1) x2 + ...

becomes

A -function selects one of its operands, based on the control-flow path used to reach the block.

Page 21: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

21

This is in SSA Form

Superlocal Value Numbering

m0 a + bn0 a + b

A

p0 c + dr0 c + d

B

r2 (r0,r1)y0 a + bz0 c + d

G

q0 a + br1 c + d

C

e0 b + 18s0 a + bu0 e + f

D e1 a + 17t0 c + du1 e + f

E

e3 (e0,e1)u2 (u0,u1)v0 a + bw0 c + dx0 e + f

F

With all the bells & whistles• Find more redundancy• Pay little additional cost• Still does nothing for F & G

Superlocal techniques • Some local methods extend

cleanly to superlocal scopes• VN does not back up• If C adds to A, it’s a

problem

Page 22: Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,

COMP 512, Rice University

22

What About Larger Scopes?

We have not helped with F or G• Multiple predecessors

• Must decide what facts hold in F and in G For G, combine B & F? Merging state is expensive Fall back on what’s known

G

m0 a + bn0 a + b

A

p0 c + dr0 c + d

B

r2 (r0,r1)y0 a + bz0 c + d

q0 a + br1 c + d

C

e0 b + 18s0 a + bu0 e + f

D e1 a + 17t0 c + du1 e + f

E

e3 (e0,e1)u2 (u0,u1)v0 a + bw0 c + dx0 e + f

F


Recommended