Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Context-Sensitive Interprocedural Points-to Analysis in the Presence of Function Pointers Presentation by Patrick Kaleem Justin.
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
SSA.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
Code Generation Mooly Sagiv html:// Chapter 4.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Course project presentations No midterm project presentation Instead of classes, next week I’ll meet with each group individually, 30 mins each Two time.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Feedback: Keep, Quit, Start
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Interprocedural analyses and optimizations. Costs of procedure calls Up until now, we treated calls conservatively: –make the flow function for call nodes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Administrative info Subscribe to the class mailing list –instructions are on the class web page, click on “Course Syllabus” –if you don’t subscribe by.
Administrative info Subscribe to the class mailing list –instructions are on the class web page, which is accessible from my home page, which is accessible.
Inlining pros and cons (discussion). Inlining pros and cons Pros –eliminate overhead of call/return sequence –eliminate overhead of passing args & returning.
Previous finals up on the web page use them as practice problems look at them early.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Run time vs. Compile time
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.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
A simple approach Given call graph and CFGs of procedures, create a single CFG (control flow super-graph) by: –connecting call sites to entry nodes of.
From last time: Inlining pros and cons Pros –eliminate overhead of call/return sequence –eliminate overhead of passing args & returning results –can optimize.
Comparison Caller precisionCallee precisionCode bloat Inlining context-insensitive interproc Context sensitive interproc Specialization.
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Course project presentations Midterm project presentation Originally scheduled for Tuesday Nov 4 th Can move to Th Nov 6 th or Th Nov 13 th.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
Pointer analysis. Pointer Analysis Outline: –What is pointer analysis –Intraprocedural pointer analysis –Interprocedural pointer analysis Andersen and.
From last class. The above is Click’s solution (PLDI 95)
Eliminating Memory References Joshua Dunfield Alina Oprea.
Precision Going back to constant prop, in what cases would we lose precision?
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
COMP3190: Principle of Programming Languages
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Program Representations. Representing programs Goals.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Interprocedural analyses and optimizations. Costs of procedure calls Up until now, we treated calls conservatively: –make the flow function for call nodes.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Interprocedural analyses and optimizations. Costs of procedure calls Up until now, we treated calls conservatively: –make the flow function for call nodes.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Inter-procedural analysis
Code Optimization Overview and Examples
Program Representations
For Example: User level quicksort program Three address code.
Program Slicing Baishakhi Ray University of Virginia
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
Binding Times Binding is an association between two things Examples:
Pointer analysis.
Interprocedural analyses and optimizations
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week

Control Dependencies 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

Example

Another example

Summary of Control Depence Graph More flexible way of representing control- depencies than CFG (less constraining) Makes code motion a local transformation However, much harder to convert back to an executable form

Course summary so far Dataflow analysis –flow functions, lattice theoretic framework, optimistic iterative analysis, precision, MOP Advanced Program Representations –SSA, CDG, PDG Along the way, several analyses and opts –reaching defns, const prop & folding, available exprs & CSE, liveness & DAE, loop invariant code motion Next: dealing with procedures

Interprocedural analyses and optimizations

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

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

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

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?

Inlining pros and cons (discussion)

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

Which calls to inline (discussion) What affects the decision as to which calls to inline?

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?)

Inlining heuristics Strategy 1: superficial analysis –examine source code of callee to estimate space costs, use this to determine when to inline –doesn’t account for post-inlining optimizations How can we do better?

Inlining heuristics Strategy 2: deep analysis –perform inlining –perform post-inlining analysis/optimizations –estimate benefits from opts, and measure code space after opts –undo inlining if costs exceed benefits –better accounts for post-inlining effects –much more expensive in compile-time How can we do better?

Inlining heuristics Strategy 3: amortized version of 2 –perform strategy 2: an inlining “trial” –record cost/benefit trade-offs in persistent database –reuse previous cost/benefit results for “similar” call sites

Inlining heuristics Strategy 4: use machine learning techniques For example, use genetic algorithms to evolve heuristics for inlining –fitness is evaluated on how well the heuristics do on a set of benchmarks –cross-populate and mutate heuristics Can work surprisingly well to derive various heuristics for compilers

Another way to remove procedure calls int f(...) { if (...) return g(...);... return h(i(....), j(...)); }

Tail call eliminiation Tail call: last thing before return is a call –callee returns, then caller immediately returns Can splice out one stack frame creation and destruction by jumping to callee rather than calling –callee reuses caller’s stack frame & return address –callee will return directly to caller’s caller –effect on debugging?

Tail recursion elimination If last operation is self-recursive call, what does tail call elimination do?

Tail recursion elimination If last operation is self-recursive call, what does tail call elimination do? Transforms recursion into loop: tail recursion elimination –common optimization in compilers for functional languages –required by some language specifications, eg Scheme –turns stack space usage from O(n) to O(1)

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

Interprocedural analysis Extend intraprocedural analyses to work across calls Doesn’t increase code size But, doesn’t eliminate direct runtime costs of call And it may not be as effective as inlining at cutting the “precision cost” of procedure calls

A simple approach (discussion)

A simple approach Given call graph and CFGs of procedures, create a single CFG (control flow super-graph) by: –connecting call sites to entry nodes of callees (entries become merges) –connecting return nodes of callees back to calls (returns become splits) Cons: –speed? –separate compilation? –imprecision due to “unrealizable paths”

Another approach: summaries (discussion)

Code examples for discussion global a; a := 5; f(...); b := a + 10; global a; global b; f(p) { *p := 0; } g() { a := 5; f(&a); b := a + 10; } h() { a := 5; f(&b); b := a + 10; }