Optimization Techniques Code Optimizations
Optimization Techniques Code Optimizations
3 4
1
Constant Propagation Strength Reduction
z Identify expressions that can be evaluated at •Replace expensive operations with equivalent
compile time, and replace them with their cheaper (more efficient) ones.
values. y = 2; => y = 2;
z x = 5; => x = 5; => x = 5; z = x^y; z = x*x;
y = 2; y = 2; y = 2; ... ...
v = u + y; v = u + y; v = u + 2; •The underlying architecture may determine
z = x * y; z = x * y; z = 10; which operations are cheaper and which
w = v + z + 2; w = v + z + 2; w = v + 12; ones are more expensive.
... ... ...
5 6
7 8
2
Peephole Optimization Profile-based Optimization
• Optimizations that examine small code sections at a time, • A compiler has difficulty in predicting:
and transform them • likely outcome of branches
• Peephole: a small, moving window in the target program • functions and/or loops that are most frequently
• Much simpler to implement than global optimizations executed
• Typically applied at machine code, and some times at • sizes of arrays
intermediate code level as well • or more generally, any thing that depends on
• Any optimization can be a peephole optimization, dynamic rogram behavior.
provided it operates on the code within the peephole. • Runtime profiles can provide this missing information,
• redundant instruction elimination making it easier for compilers to decide when certain
• flow-of control optimizations
• algebraic simplifications
• ...
9 10
11 12
3
Organization of Optimizer Flow Graph for Quicksort
z B1,…,B6 are basic blocks
• sequence of statements where
control enters at beginning,
with no branches in the middle
z Possible optimizations
• Common subexpression
elimination (CSE)
• Copy propagation
• Generalization of constant
folding to handle assignments
of the form x = y
• Dead code elimination
• Loop optimizations
• Code motion
• Strength reduction
• Induction variable elimination
13 14
4
Induction Vars, Strength Reduction
Dead Code Elimination and IV Elimination
z Dead variable: a z Induction Var: a variable whose value
variable whose value is changes in lock-step with a loop index
no longer used
z If expensive operations are used for
z Live variable: opposite
of dead variable computing IV values, they can be replaced
z Dead code: a statement
by less expensive operations
that assigns to a dead z When there are multiple IVs, some can be
variable eliminated
z Copy propagation turns
copy statement into
dead code.
17 18
19 20
5
Program Analysis Applications of Program Analysis
Compiler optimization
z Optimization is usually expressed as a z
z Debugging/Bug-finding
program transformation • “Enhanced” type checking
C1 ⇔ C2 when property P holds • Use before assign
• Null pointer dereference
z Whether property P holds is determined by a • Returning pointer to stack-allocated data
z Vulnerability analysis/mitigation
program analysis • Information flow analysis
z Most program properties are undecidable in • Detect propagation of sensitive data, e.g., passwords
• Detect use of untrustworthy data in security-critical context
general • Find potential buffer overflows
6
Points and Paths Reaching Definitions
z A definition of a variable x is a statement that
assigns to x
• Ambiguous definition: In the presence of aliasing, a
statement may define a variable, but it may be impossible to
determine this for sure.
z A definition d reaches a point p provided:
• There is a path from d to p, and this definition is not “killed”
along p
• “Kill” means an unambiguous redefinition
z Ambiguity Î approximation
• Need to ensure that approximation is in the right direction,
so that the analysis will be sound
25 26
27 28
7
DF Equations for Reaching Defns Direction of Approximation
z Actual kill is a superset of the set computed by
the dataflow equations
z Actual gen is a subset of the set computed by
these equations
z Are other choices possible?
• Subset approximation of kill, superset approximation of gen
• Subset approximation of both
• Superset approximation of both
z Which approximation is suitable depends on the
intended use of analysis results
29 30
8
Use-Definition Chains Available Expressions
z Convenient way to represent reaching z An expression e is available at point p if
definition information • every path to p evaluates e
• none of the variables in e are assigned after last
z ud-chain for a variable links each use of the computation of e
variable to its reaching definitions z A block kills e if it assigns to some variable in e
and does not recompute e.
• One list for each use of a variable
z A block generates e if it computes e and doesn’t
subsequently assign to variables in e
z Exercise: Set up data-flow equations for
available expressions. Give an example use for
which your equations are sound, and another
example for which they aren’t
33 34
9
Def-Use Chains Optimizations and Related Analyses
z du-chain links the definition of a variable with z Common subexpression elimination
all its uses • Available expressions
Copy propagation
• Use of a definition of a variable x at a point p z
• In every path that reaches a program point p, the variables
implies that there is a path from this definition to p x and y have identical values
in which there are no assignments to x z Detection of loop-invariant computation
z du-chains can be computed using a dataflow • Any assignment x := e where the definition of every variable
in e occurs outside the loop.
analysis similar to that for live variables
z Code reordering: A statement x := e can be moved
• earlier before statements that (a) do not use x, (b) do not
assign to variables in e
• later after statements that (a) do not use , (b) do not assign
to variables in e
37 38
10