Material Type: Notes;
Professor: Weimer;
Class: Programming Languages;
Subject: Computer Science;
University: University of Virginia;
Term: Spring 2008;
Download Local Optimizations - Programming Languages - Slides | CS 4610 and more Study notes Programming Languages in PDF only on Docsity!
Local Local
Optimizations Optimizations
One-Slide Summary
(^) An optimization changes a program so that it computes the same answer in less time (or using less of some other resource).
(^) We represent the program using a special intermediate form.
(^) Each method is viewed as a control flowgraph where the nodes as basic blocks of instructions with known entry and exit points. The instructions have been changed so that a single assignment defines each variable.
Why Optimize?
What's the point?
Do we care about this in real life?
When To Optimize?
(^) When to perform optimizations
(^) On AST (just like type checking)
(^) Pro: Machine independent
(^) Cons: Too high level
(^) On assembly language (compilers only)
(^) Pro: Exposes optimization opportunities
(^) Cons: Machine dependent
(^) Cons: Must reimplement optimizations when retargetting
(^) On an intermediate language
(^) Pro: Machine independent
(^) Pro: Exposes optimization opportunities
(^) Cons: One more language to worry about You do not have to know assembly language.
Three-Address Intermediate Code
(^) Each instruction is of the form x := y op z
(^) y and z can be only registers, variables or constants
(^) Common form of intermediate code
(^) The AST expression x + y * z is translated as t 1 := y * z t 2 := x + t 1
(^) Each subexpression lives in a temporary
Generating Intermediate Code
(^) igen(e, t) function generates code to compute the value of e in register t
(^) Example: igen(e 1 + e 2 , t) = igen(e 1 , t 1 ) (t 1 is a fresh register) igen(e 2 , t 2 ) (t 2 is a fresh register) t := t 1
t 2
(^) Unlimited number of registers ⇒ simple code generation
Basic Blocks
(^) A basic block is a maximal sequence of instructions with: - (^) no labels (except at the first instruction), and - (^) no jumps (except in the last instruction)
(^) Idea:
(^) Cannot jump into a basic block ( except at beginning)
(^) Cannot jump out of a basic block ( except at end)
(^) Each instruction in a basic block is executed after all the preceding instructions have been executed
Basic Block Example
(^) Consider the basic block
L1:
t := 2 * x
w := t + x
if w > 0 goto L
(^) No way for (3) to be executed without (2) having been executed right before
Basic Block Example
(^) Consider the basic block
L1:
t := 2 * x
w := t + x
if w > 0 goto L
(^) No way for (3) to be executed without (2) having been executed right before
(^) We can change (3) to w := 3 * x
(^) Can we eliminate (2) as well?
Control-Flow Graphs
(^) A control-flow graph is a directed graph:
(^) Basic blocks as nodes
(^) An edge from block A to block B if the execution can flow from the last instruction in A to the first instruction in B
e.g., the last instruction in A is^ jump L B
(^) e.g., the execution can fall-through from block A to block B
(^) Frequently abbreviated as CFG
CFG
Flow
Chart
Optimization Overview
(^) Optimization seeks to improve a program’s utilization of some resource - (^) Execution time (most often) - (^) Code size - (^) Network messages sent - (^) Battery power used, etc.
(^) Optimization should not alter what the program computes - (^) The answer must still be the same
Cost of Optimizations
(^) In practice, a conscious decision is made not to implement the fanciest optimization known
(^) Why?
Cost of Optimizations
(^) In practice, a conscious decision is made not to implement the fanciest optimization known
(^) Why?
(^) Some optimizations are hard to implement
(^) Some optimizations are costly in terms of compilation/interpretation time
(^) The fancy optimizations are both hard and costly
(^) The goal: maximum improvement with minimum of cost