52
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Recap from last time

• We were trying to do Common Subexpression Elimination

• Compute expressions that are available at each program point

Page 2: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Recap from last time

X := Y op Z

in

out

FX := Y op Z(in) = in – { X ! * } – { * ! ... X ... } [{ X ! Y op Z | X Y Æ X Z}

X := Y

in

out

FX := Y(in) = in – { X ! * } – { * ! ... X ... } [{ X ! E | Y ! E 2 in }

Page 3: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 4: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Problems

• z := j * 4 is not optimized to z := x, even though x contains the value j * 4

• m := b + a is not optimized to m := i, even i contains a + b

• w := 4 * m it not optimized to w := x, even though x contains the value 4 *m

Page 5: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Problems: more abstractly

• Available expressions overly sensitive to name choices, operand orderings, renamings, assignments

• Use SSA: distinct values have distinct names

• Do copy prop before running available exprs

• Adopt canonical form for commutative ops

Page 6: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example in SSA

X := Y op Z

in

out

FX := Y op Z(in) =

X := (Y,Z)

in0

out

FX := Y(in0, in1) =in1

Page 7: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example in SSA

X := Y op Z

in

out

FX := Y op Z(in) = in [ { X ! Y op Z }

X := (Y,Z)

in0

out

FX := Y(in0, in1) = (in0 Å in1 ) [ { X ! E | Y ! E 2 in0 Æ Z ! E 2 in1 }

in1

Page 8: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example in SSA

Page 9: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example in SSA

Page 10: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

What about pointers?

Page 11: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

What about pointers?

• Option 1: don’t use SSA for point-to memory

• Option 2: insert copies between SSA vars and real vars

Page 12: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

SSA helps us with CSE

• Let’s see what else SSA can help us with

• Loop-invariant code motion

Page 13: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Loop-invariant code motion

• Two steps: analysis and transformations

• Step1: find invariant computations in loop– invariant: computes same result each time evaluated

• Step 2: move them outside loop– to top if used within loop: code hoisting– to bottom if used after loop: code sinking

Page 14: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 15: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 16: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Detecting loop invariants

• An expression is invariant in a loop L iff:

(base cases)– it’s a constant– it’s a variable use, all of whose defs are outside of L

(inductive cases)– it’s a pure computation all of whose args are loop-

invariant– it’s a variable use with only one reaching def, and the

rhs of that def is loop-invariant

Page 17: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Computing loop invariants

• Option 1: iterative dataflow analysis– optimistically assume all expressions loop-invariant,

and propagate

• Option 2: build def/use chains– follow chains to identify and propagate invariant

expressions

• Option 3: SSA– like option 2, but using SSA instead of ef/use chains

Page 18: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example using def/use chains

• An expression is invariant in a loop L iff:

(base cases)– it’s a constant– it’s a variable use, all of

whose defs are outside of L

(inductive cases)– it’s a pure computation all of

whose args are loop-invariant– it’s a variable use with only

one reaching def, and the rhs of that def is loop-invariant

Page 19: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example using def/use chains

• An expression is invariant in a loop L iff:

(base cases)– it’s a constant– it’s a variable use, all of

whose defs are outside of L

(inductive cases)– it’s a pure computation all of

whose args are loop-invariant– it’s a variable use with only

one reaching def, and the rhs of that def is loop-invariant

Page 20: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Loop invariant detection using SSA

• An expression is invariant in a loop L iff:

(base cases)– it’s a constant– it’s a variable use, all of whose single defs are outside

of L

(inductive cases)– it’s a pure computation all of whose args are loop-

invariant– it’s a variable use whose single reaching def, and the

rhs of that def is loop-invariant

• functions are not pure

Page 21: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example using SSA

• An expression is invariant in a loop L iff:

(base cases)– it’s a constant– it’s a variable use, all of

whose single defs are outside of L

(inductive cases)– it’s a pure computation all of

whose args are loop-invariant– it’s a variable use whose

single reaching def, and the rhs of that def is loop-invariant

• functions are not pure

Page 22: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example using SSA and preheader

• An expression is invariant in a loop L iff:

(base cases)– it’s a constant– it’s a variable use, all of

whose single defs are outside of L

(inductive cases)– it’s a pure computation all of

whose args are loop-invariant– it’s a variable use whose

single reaching def, and the rhs of that def is loop-invariant

• functions are not pure

Page 23: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Code motion

Page 24: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 25: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Lesson from example: domination restriction

Page 26: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Domination restriction in for loops

Page 27: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Domination restriction in for loops

Page 28: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Avoiding domination restriction

Page 29: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Another example

Page 30: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Data dependence restriction

Page 31: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Avoiding data restriction

Page 32: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Representing control dependencies

• We’ve seen SSA, a way to encode data dependencies better than just def/use chains– makes CSE easier– makes loop invariant detection easier– makes code motion easier

• Now we move on to looking at how to encode control dependencies

Page 33: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Control Dependences

• A node (basic block) Y is control-dependent on another X iff X determines whether Y executes– there exists a path from X to Y s.t. every node in the

path other than X and Y is post-dominated by Y– X is not post-dominated by Y

Page 34: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Control Dependences

• A node (basic block) Y is control-dependent on another X iff X determines whether Y executes– there exists a path from X to Y s.t. every node in the

path other than X and Y is post-dominated by Y– X is not post-dominated by Y

Page 35: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 36: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 37: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Control Dependence Graph

• Control dependence graph: Y descendent of X iff Y is control dependent on X– label each child edge with required condition– group all children with same condition under region

node

• Program dependence graph: super-impose dataflow graph (in SSA form or not) on top of the control dependence graph

Page 38: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 39: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Example

Page 40: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Another example

Page 41: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Another example

Page 42: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Another example

Page 43: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Summary so far

• Dataflow analysis– flow functions, lattice theoretic framework, optimistic

iterative analysis, MOP

• Advanced Program Representations– SSA, CDG, PDG, code motion

• Next: dealing with procedures

Page 44: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Interprocedural analyses and optimizations

Page 45: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Costs of procedure calls

• Up until now, we treated calls conservatively:– make the flow function for call nodes return top– start iterative analysis with incoming edge of the CFG

set to top– This leads to less precise results: “lost-precision” cost

• Calls also incur a direct runtime cost– cost of call, return, argument & result passing, stack

frame maintainance– “direct runtime” cost

Page 46: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Addressing costs of procedure calls

• Technique 1: try to get rid of calls, using inlining and other techniques

• Technique 2: interprocedural analysis, for calls that are left

Page 47: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Inlining

• Replace call with body of callee

• Turn parameter- and result-passing into assignments– do copy prop to eliminate copies

• Manage variable scoping correctly– rename variables where appropriate

Page 48: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Program representation for inlining

• Call graph– nodes are procedures

– edges are calls, labelled by invocation counts/frequency

• Hard cases for builing call graph– calls to/from external routines

– calls through pointers, function values, messages

• Where in the compiler should inlining be performed?

Page 49: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Inlining pros and cons (discussion)

Page 50: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Inlining pros and cons

• Pros– eliminate overhead of call/return sequence– eliminate overhead of passing args & returning results– can optimize callee in context of caller and vice versa

• Cons– can increase compiled code space requirements– can slow down compilation– recursion?

• Virtual inlining: simulate inlining during analysis of caller, but don’t actually perform the inlining

Page 51: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Which calls to inline (discussion)

• What affects the decision as to which calls to inline?

Page 52: Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point

Which calls to inline

• What affects the decision as to which calls to inline?– size of caller and callee (easy to compute size before

inlining, but what about size after inlining?)– frequency of call (static estimates or dynamic profiles)– call sites where callee benefits most from optimization

(not clear how to quantify)– programmer annotations (if so, annotate procedure or

call site? Also, should the compiler really listen to the programmer?)