Upload
shonali-katariya
View
222
Download
0
Embed Size (px)
Citation preview
8/12/2019 Code Optimization FINAL
1/29
8/12/2019 Code Optimization FINAL
2/29
Code Optimizations
Introduction Optimized code
Executes faster
efficient memory usage yielding better performance.
Compilers can be designed to provide codeoptimization.
Users should only focus on optimizations notprovided by the compiler such as choosing afaster andor less memory intensive algorithm.
2
8/12/2019 Code Optimization FINAL
3/29
Introduction
Concerns !ith machine"independent codeoptimization #$"%$ rule& execution spends #$' time in %$'
of the code. It is moderately easy to achieve #$'
optimization. The rest %$' is very difficult.
Identification of the %$' of the code is not
possible for a compiler ( it is the )ob of a profiler. In general* loops are the hot"spots
3
8/12/2019 Code Optimization FINAL
4/29
Compiler Code
Optimizations + Code optimizer sits bet!een the front
end and the code generator.
,or-s !ith intermediate code. Can do control flo! analysis.
Can do data flo! analysis.
oes transformations to improve the
intermediate code.
4
8/12/2019 Code Optimization FINAL
5/29
Compiler Code
Optimizations Optimizations provided by a compiler
includes& Inlining small functions
Code hoisting
ead store elimination
Eliminating common sub"expressions
/oop unrolling /oop optimizations& Code motion* Induction
variable elimination* and 0eduction in strength.
5
8/12/2019 Code Optimization FINAL
6/29
Introduction
Optimization can be done in almost all
phases of compilation.
6
Front
end
Code
generator
Source
code
Inter.
code
target
code
Profile and
optimize
(user)
Loop, proc
calls, addr
calculation
improvement
(compiler)
Reg usage,
instruction
choice,
peephole opt
(compiler)
8/12/2019 Code Optimization FINAL
7/29
Themes behind
Optimization Techniques +void redundancy& something already computed
need not be computed again
1maller code& less !or- for C2U* cache* and
memory3 /ess )umps&)umps interfere !ith code pre"fetch
Code locality& codes executed close together intime is generated close together in memory (
increase locality of reference Extract more information about code& 4ore
info ( better code generation
7
R d d li i i
8/12/2019 Code Optimization FINAL
8/29
Redundancy elimination
0edundancy elimination5 etermining that t!ocomputations are equivalent and eliminating one.
There are several types of redundancy elimination& 6alue numbering
+ssociates symbolic values to computations and identifies expressions thathave the same value
Common subexpression elimination Identifies expressions that have operands !ith the same name
ConstantCopy propagation Identifies variables that have constantcopy values and uses the
constantscopies in place of the variables.
2artial redundancy elimination Inserts computations in paths to convert partial redundancy to full
redundancy.
8
8/12/2019 Code Optimization FINAL
9/29
Levels of optimization
Design level Implementation of this design !ill benefit
from a good choice of efficient algorithms
and the implementation of these algorithms!ill benefit from !riting good quality code.The architectural design of a systemover!helmingly affects its performance. Thechoice of algorithmaffects efficiency more
than any other item of the design and* sincethe choice of algorithm usually is the firstthing that must be decided* argumentsagainst early or 7premature optimization7may be hard to )ustify.
9
http://en.wikipedia.org/wiki/Algorithmic_efficiencyhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Algorithmic_efficiency8/12/2019 Code Optimization FINAL
10/29
Sou ce code level +voiding poor quality coding can also
improve performance* by avoiding obvious
7slo!do!ns7. +fter that* ho!ever* some
optimizations are possible that actually
decrease maintainability. 1ome* but not
all* optimizations can no!adays be
performed by optimizing compilers.
10
http://en.wikipedia.org/wiki/Optimizing_compilerhttp://en.wikipedia.org/wiki/Optimizing_compiler8/12/2019 Code Optimization FINAL
11/29
Build level 8et!een the source and compile level*
directivesand build flagscan be used to
tune performance options in the source
code and compiler respectively* such as
using preprocessordefines to disable
unneeded soft!are features* or optimizing
for specific processor models or hard!arecapabilities.
11
http://en.wikipedia.org/wiki/Directive_(programming)http://en.wikipedia.org/wiki/Build_automationhttp://en.wikipedia.org/wiki/Build_automationhttp://en.wikipedia.org/wiki/Directive_(programming)8/12/2019 Code Optimization FINAL
12/29
Compile level Use of an optimizing compiler tends to
ensure that the executable program is
optimized at least as much as the
compiler can predict.
12
8/12/2019 Code Optimization FINAL
13/29
ssembly level +t the lo!est level* !riting code using
an assembly language* designed for a
particular hard!are platform can produce
the most efficient and compact code if the
programmer ta-es advantage of the full
repertoire of machine instructions.
4any operating systems usedon embedded systems have been
traditionally !ritten in assembler code for
this reason.13
8/12/2019 Code Optimization FINAL
14/29
Optimizing
Transformations Compile time evaluation Constant folding
Constant propagation
Common sub"expression elimination Code motion
1trength 0eduction
ead code elimination
Copy propagation
14
8/12/2019 Code Optimization FINAL
15/29
Compile!Time "valuation
Expressions !hose values can be pre"
computed at the compilation time
T!o !ays& Constant folding
Constant propagation
15
8/12/2019 Code Optimization FINAL
16/29
Constant #olding
Evaluation of an expression !ith constant
operands to replace the expression !ith
single value
Example&
area := (22.0/7.0) * r ** 2
area := 3.14286 * r ** 2
16
8/12/2019 Code Optimization FINAL
17/29
Constant $ropagation
,hat does it mean9 :iven an assignment x 5 c* !here c is a constant
value. 1imilar to copy propagation Extra feature& It can analyze constant"value conditionals to
determine !hether a branch should be executed or not.
,hen is it performed9 Early in the optimization process.
,hat is the result9 1maller code ;e!er registers
17
8/12/2019 Code Optimization FINAL
18/29
Constant $ropagation
0eplace a variable !ith constant !hich
has been assigned to it earlier.
Example&pi := 3.14286
area = pi * r ** 2
area = 3.14286 * r ** 2
18
8/12/2019 Code Optimization FINAL
19/29
Common Sub!e%pression
"valuation Identify common sub"expression present in differentexpression* compute once* and use the result in allthe places. The definitionof the variables involved should not change
Example&
a := b * c temp := b * c
a := temp
x := b * c + 5 x := temp + 5
19
8/12/2019 Code Optimization FINAL
20/29
Code &otion
4oving code from one part of the program
to other !ithout modifying the algorithm 0educe size of the program
0educe execution frequency of the code
sub)ected to movement
20
8/12/2019 Code Optimization FINAL
21/29
Code &otion
1. Code Space reduction& 1imilar to commonsub"expression elimination but !ith theob)ective to reduce code size.
Example& Code hoisting
temp & 5 x a? b@ then if >a? b@ then
z &5 x
8/12/2019 Code Optimization FINAL
22/29
Code &otion
2 Execution frequency reduction& reduce executionfrequency of partially available expressions>expressions available atleast in one path@
Example&if >a?b@ then if >a?b@ then
z 5 x < = temp 5 x < =
z 5 temp
else else
y 5 %$ y 5 %$
temp 5 x < =g 5 x < = g 5 tempB
22
8/12/2019 Code Optimization FINAL
23/29
Code &otion
4ove expression out of a loop if the
evaluation does not change inside the
loop.
Example&
whie ( i ! (max"2) )
Equivalent to&
t := max " 2
whie ( i ! t )
23
8/12/2019 Code Optimization FINAL
24/29
Code &otion
1afety of Code movement4ovement of an expression efrom a basic bloc- bito another bloc- bj* is safe if it does not introduceany ne! occurrence of ealong any path.
Example& Unsafe code movementtemp 5 x < =
if >a?b@ then if >a?b@ then
z 5 x < = z 5 temp
else else
y 5 %$ y 5 %$
24
8/12/2019 Code Optimization FINAL
25/29
Strength Reduction
0eplacement of an operator !ith a less costly one.
Example&temp 5 DB
for i5% to %$ do for i5% to %$ do
x 5 i < D x 5 temp
temp 5 temp A D
end end
Typical cases of strength reduction occurs in addresscalculation of array references.
+pplies to integer expressions involving inductionvariables >loop optimization@
25
8/12/2019 Code Optimization FINAL
26/29
'ead Code "limination
ead Code are portion of the program !hich!ill not be executed in any path of theprogram.
Can be removed Examples&
Fo control flo!s into a basic bloc-
+ variable is dead at a point "G its value is not
used any!here in the program +n assignment is dead "G assignment assigns a
value to a dead variable
26
8/12/2019 Code Optimization FINAL
27/29
'ead Code "limination
27
!"amples#
#$%&':=0
i (#$%&') prit Can $e
eliminated
8/12/2019 Code Optimization FINAL
28/29
Copy $ropagation
,hat does it mean9 :iven an assignment x 5 y* replace later uses of x
!ith uses of y* provided there are no intervening
assignments to x or y.
,hen is it performed9 +t any level* but usually early in the optimization
process.
,hat is the result9 1maller code
28
8/12/2019 Code Optimization FINAL
29/29
Copy $ropagation
:= are called copy statements or copies Use of for * !henever possible after copy
statement
Example&
xHi 5 aB xHi 5 aB
sum 5 xHi A aB sum 5 a A aB
4ay not appear to be code improvement* butopens up scope for other optimizations.
29