SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
C dCode
O i i iOptimization
P K Singh 1M M M Engg. College, Gorakhpur
Code Optimization and Phases
source front code
intermediate
code code
intermediate
code targetsource
program
front
end
code
optimizer
code code
generator
code target
code
symbol
table
P K Singh 2M M M Engg. College, Gorakhpur
Optimization
• Code produced by standard algorithms can often be made to run
faster, take less space or both
• These improvements are achieved through transformations called• These improvements are achieved through transformations called
optimization
• Compilers that apply these transformations are called optimizing
compilerscompilers
• It is especially important to optimize frequently executed parts of a
program
The 90/10 rule– The 90/10 rule
– Profiling can be helpful, but compilers do not typically have sample input
data
– Inner loops tend to be good candidates for optimizationInner loops tend to be good candidates for optimization
P K Singh 3M M M Engg. College, Gorakhpur
Criteria for Transformations
• A transformation must preserve the meaning of
a programa program
– Can not change the output produced for any input
– Can not introduce an errorCan not introduce an error
• Transformations should, on average, speed up
programsprograms
• Transformations should be worth the effort
P K Singh 4M M M Engg. College, Gorakhpur
Beyond Optimizing Compilers
• Really improvements can be made at various phases
• Source code:
Algorithmic transformations can produce spectacular– Algorithmic transformations can produce spectacular
improvements
– Profiling can be helpful to focus a programmer's attention on
important codep
• Intermediate code:
– Compiler can improve loops, procedure calls, and address
calculationscalculations
– Typically only optimizing compilers include this phase
• Target code:
Compilers can use registers efficiently– Compilers can use registers efficiently
– Peephole transformation can be applied
P K Singh 5M M M Engg. College, Gorakhpur
Peephole Optimizations
• A simple technique for locally improving target
code (can also be applied to intermediate code)( )
• The peephole is a small, moving window on the
target program
• Each improvement replaces the instructions of
the peephole with a shorter or faster sequence
• Each improvement may create opportunities for
additional improvements
• Repeated passes may be necessary
P K Singh 6M M M Engg. College, Gorakhpur
Redundant-Instruction Elimination
• Redundant loads and stores:
(1)MOV R0 a(1)MOV R0, a
(2)MOV a, R0
• Unreachable code:
#define debug 0
if (debug) {
/* i t d b i i f ti *//* print debugging information */
}
P K Singh 7M M M Engg. College, Gorakhpur
Flow-of-Control Optimizations
• Jumps to jumps, jumps to conditional jumps, and conditional jumps
to jumps are not necessary
• Jumps to jumps example:p j p p
– The following replacement is valid:
goto L1
…
goto L2
…
– If there are no other jumps to L1 and L1 is preceded by an unconditional
L1: goto L2 L1: goto L2
– If there are no other jumps to L1 and L1 is preceded by an unconditional
jump, the statement at L1 can be eliminated
• Jumps to conditional jumps and conditional jumps to jumps lead to
similar transformations
P K Singh 8M M M Engg. College, Gorakhpur
Other Peephole Optimizationsp p
• A few algebraic identities that occur frequently
(such as x := x + 0 or x := x * 1) can be
eliminated
• Reduction in strength replaces expensive
operations with cheaper onesoperations with cheaper ones
– Calculating x * x is likely much cheaper than x^2 using an
exponentiation routine
– It may be cheaper to implement x * 5 as x * 4 + x
• Some machines may have hardware instructions to
implement certain specific operations efficiently
– For example, auto-increment may be cheaper than a straight-For example, auto increment may be cheaper than a straight
forward x := x + 1
– Auto-increment and auto-decrement are also useful when
pushing into or popping off of a stackp g p pp g
P K Singh 9M M M Engg. College, Gorakhpur
Optimizing Intermediate Code
• This phase is generally only included in
optimizing compilersoptimizing compilers
• Offers the following advantages:
Operations needed to implement high level constructs– Operations needed to implement high-level constructs
are made explicit (i.e. address calculations)
– Intermediate code is independent of target machine;p g ;
code generator can be replaced for different machine
• We are assuming intermediate code uses three-
address instructions
P K Singh 10M M M Engg. College, Gorakhpur
QuickSort in C
void quicksort(int m, int n) {
int i, j, v, x;
if (n <= m) return;if (n < m) return;
/* Start of partition code */
i = m-1; j = n; v =a[n];
while (1) {
do i = i+1; while (a[i] < v);
do j = j-1; while (a[j] > v);
if (i >= j) break;( j) ;
x = a[i]; a[i] = a[j]; a[j] = x;
}
x = a[i]; a[i] = a[n]; a[n] = x;
/* E d f titi d *//* End of partition code */
quicksort(m, j); quicksort(i+1, n);
}
P K Singh 11M M M Engg. College, Gorakhpur
Partition in Three-Address Code
(1) i := m-1
(2) j := n
(3) 1 4*
(16) t7 := 4*i
(17) t8 := 4*j
(18) 9 [ 8](3) t1 := 4*n
(4) v := a[t1]
(5) i := i+1
(6) t2 := 4*i
(18) t9 := a[t8]
(19) a[t7] := t9
(20) t10 := 4*j
(21) a[t10] := x( )
(7) t3 := a[t2]
(8) if t3 < v goto (5)
(9) j := j-1
(10) t4 : 4*j
( ) [ ]
(22) goto (5)
(23) t11 := 4*i
(24) x := a[t11]
(25) t12 : 4*i(10) t4 := 4*j
(11) t5 := a[t4]
(12) if t5 > v goto (9)
(13) if i >= j goto (23)
(25) t12 := 4*i
(26) t13 := 4*n
(27) t14 := a[t13]
(28) a[t12] := t14
(14) t6 := 4*i
(15) x := a[t6]
(29) t15 := 4*n
(30) a[t15] := x
P K Singh 12M M M Engg. College, Gorakhpur
Partition Flow Graph
P K Singh 13M M M Engg. College, Gorakhpur
Local vs. Global Transformations
• Local transformations involve statements within a
single basic block
• All other transformations are called global
transformations
• Local transformations are generally performed first
• Many types of transformations can be performed
either locally or globally
P K Singh 14M M M Engg. College, Gorakhpur
Code Optimizations
• Local/global common subexpression elimination
• Dead code elimination• Dead-code elimination
• Instruction reordering
C t t f ldi• Constant folding
• Algebraic transformations
C i• Copy propagation
• Loop optimizations
15P K Singh M M M Engg. College, Gorakhpur
Loop Optimizations
• Code motion
• Induction variable elimination• Induction variable elimination
• Reduction in strength
l t• … lots more
16P K Singh M M M Engg. College, Gorakhpur
Code Motion
i := 0
t2 := 4*i
B1:
B2:
i := 0
t1 := n-2
B1:
t :
A[t2] := 0
i := i+1
t2 := 4*i
A[t2] := 0
i := i+1
B2:
t1 := n-2
if i < t1 goto B2
B3:
if i < t1 goto B2B3:
Move loop-invariant computations before the loop
17P K Singh M M M Engg. College, Gorakhpur
Strength Reduction
i := 0
t1 := n-2
B1: i := 0
t1 := n-2
t2 := 4*i
B1:
t2 := 4*i
A[t2] := 0
i := i+1
B2: A[t2] := 0
i := i+1
t2 := t2+4
B2:
if i < t1 goto B2B3: if i < t1 goto B2B3:
Replace expensive computations with induction variables
18P K Singh M M M Engg. College, Gorakhpur
Reduction Variable Elimination
i 0B1: t1 4*B1:i := 0
t1 := n-2
t2 := 4*i
B1:
B2
t1 := 4*n
t1 := t1-8
t2 := 4*i
B1:
A[t2] := 0
i := i+1
t2 := t2+4
B2:
A[t2] := 0
t2 := t2+4
B2:
if i<t1 goto B2B3: if t2<t1 goto B2B3:
Replace induction variable in expressions with another
19P K Singh M M M Engg. College, Gorakhpur
Common Subexpressions
• E is a common subexpression if:
– E was previously computed– E was previously computed
– Variables in E have not changed since previous
computation
• Can avoid recomputing E if previously computed
value is still available
• Dags are useful to detect common subexpressions
P K Singh 20M M M Engg. College, Gorakhpur
Local Common Subexpressionsp
t6 := 4*i
x := a[t6]
7 4*i
t6 := 4*i
[ 6]t7 := 4*i
t8 := 4*j
t9 := a[t8]
a[t7] := t9
x := a[t6]
t8 := 4*j
t9 := a[t8]
a[t6] := t9a[t7] := t9
t10 := 4*j
a[t10] := x
goto B2
a[t6] := t9
a[t8] := x
goto B2
goto B2
P K Singh 21M M M Engg. College, Gorakhpur
Global Common Subexpressionsp
P K Singh 22M M M Engg. College, Gorakhpur
Copy Propagation
• Assignments of the form f := g are called copy
statements (or copies)
• The idea behind copy propagation is to use g for f
whenever possible after such a statement
• For example applied to block B5 of the previous• For example, applied to block B5 of the previous
flow graph, we obtain:
x := t3
[t2] t5a[t2] := t5
a[t4] := t3
goto B2
Copy propagation often turns the copy statement• Copy propagation often turns the copy statement
into "dead code"
P K Singh 23M M M Engg. College, Gorakhpur
Dead-Code Elimination
• Dead code includes code that can never be reached and
code that computes a value that never gets used
C id if (d b ) i• Consider: if (debug) print …
– It can sometimes be deduced at compile time that the value of an
expression is constant
– Then the constant can be used in place of the expression (constant– Then the constant can be used in place of the expression (constant
folding)
– Let's assume a previous statement assigns debug := false and
value never changes
– Then the print statement becomes unreachable and can be
eliminated
• Consider the example from the previous slide
The value of computed by the copy statement never gets used after– The value of x computed by the copy statement never gets used after
the copy propagation
– The copy statement is now dead code and can be eliminated
P K Singh 24M M M Engg. College, Gorakhpur
Loop Optimizations (1)
• The running time of a program may be improved if
we do both of the following:we do both of the following:
– Decrease the number of statements in an inner loop
– Increase the number of statements in the outer loopp
• Code motion moves code outside of a loop
– For example, consider:p ,
while (i <= limit-2) …
– The result of code motion would be:
t li it 2t = limit – 2
while (i <= t) …
P K Singh 25M M M Engg. College, Gorakhpur
Loop Optimizations (2)
• Induction variables: variables that remain in "lock-step"
– For example, in block B3 of previous flow graph, j and t4 are
induction variablesinduction variables
– Induction-variable elimination can sometimes eliminate all but one of
a set of induction variables
• Reduction in strength replaces a more expensive operationReduction in strength replaces a more expensive operation
with a less expensive one
– For example, in block B3, t4 decreases by four with every iteration
– If initialized correctly, can replace multiplication with subtractiony
– Often application of reduction in strength leads to induction-variable
elimination
• Methods exist to recognize induction variables and apply
i t t f ti t ti llappropriate transformations automatically
P K Singh 26M M M Engg. College, Gorakhpur
Loop Optimization Examplep p p
P K Singh 27M M M Engg. College, Gorakhpur
Example
• Given the following code segment, obtain:
The 3AC statements for this computation– The 3AC statements for this computation
– The basic blocks and the flow graph
prod = 0; i = 1;prod 0; i 1;
do {
prod = prod + a[i] * b[i];prod = prod + a[i] b[i];
i = i + 1;
} while ( i <= 20 );} while ( i <= 20 );
P K Singh 28M M M Engg. College, Gorakhpur
Example …contd
• 3AC for the given code segment
(1) prod :=0
(2) i := 1
(3) t1 := 4 * i (9) prod := t6
(4) t2 := a [t1] (10) t7 := i + 1
(5) t3 := 4 * i (11) i := t7
(6) t4 := b [t3] (12) if i <= 20 goto (3)
(7) t5 := t2 * t4(7) t5 := t2 * t4
(8) t6 := prod + t5
P K Singh 29M M M Engg. College, Gorakhpur
Example …contd
2 Basic blocks
(1) prod :=0
(2) i := 1
B1
(3) t1 := 4 * i (9) prod := t6
(4) t2 := a [t1] (10) t7 := i + 1
(5) t3 := 4 * i (11) i := t7
(6) t4 := b [t3] (12) if i <= 20 goto (3)
(7) t5 := t2 * t4(7) t5 := t2 * t4
(8) t6 := prod + t5
B2
P K Singh 30M M M Engg. College, Gorakhpur
B2
Example …contd
Flow Graph
t1 := 4 * iB2
prod := 0
t1 := 4 i
t2 := a [t1]
t3 := 4 * i
t4 := b [t3]B1
B2
i := 1 t5 := t2 * t4
t6 := prod + t5
prod := t6
t7 := i + 1t7 : i 1
i := t7
if i <= 20 goto B2B1
B2
P K Singh 31M M M Engg. College, Gorakhpur
Implementing a Code Optimizer
• Organization consists of control-flow analysis, then data-
flow analysis, and then transformations
• The code generator is applied to the transformedThe code generator is applied to the transformed
intermediate code
• Details of code optimization are beyond the scope of this
coursecourse
P K Singh 32M M M Engg. College, Gorakhpur

Más contenido relacionado

La actualidad más candente

Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Urvashi Singh
 
Intel x86 and ARM Data types
Intel x86 and ARM Data typesIntel x86 and ARM Data types
Intel x86 and ARM Data typesRowena Cornejo
 
Floating point presentation
Floating point presentationFloating point presentation
Floating point presentationSnehalataAgasti
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Pin configuration of 8085
Pin configuration of 8085Pin configuration of 8085
Pin configuration of 808582338476
 
Timing and control
Timing and controlTiming and control
Timing and controlchauhankapil
 
Control Units : Microprogrammed and Hardwired:control unit
Control Units : Microprogrammed and Hardwired:control unitControl Units : Microprogrammed and Hardwired:control unit
Control Units : Microprogrammed and Hardwired:control unitabdosaidgkv
 
INTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSORINTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSORGurudev joshi
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clrSanSan149
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in CJeya Lakshmi
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IVManoj Patil
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow GraphsJenny Galino
 
Register Organisation of 8086 Microprocessor
Register Organisation of 8086 MicroprocessorRegister Organisation of 8086 Microprocessor
Register Organisation of 8086 MicroprocessorNikhil Kumar
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 

La actualidad más candente (20)

Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Intel x86 and ARM Data types
Intel x86 and ARM Data typesIntel x86 and ARM Data types
Intel x86 and ARM Data types
 
Floating point presentation
Floating point presentationFloating point presentation
Floating point presentation
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Pin configuration of 8085
Pin configuration of 8085Pin configuration of 8085
Pin configuration of 8085
 
Timing and control
Timing and controlTiming and control
Timing and control
 
Control Units : Microprogrammed and Hardwired:control unit
Control Units : Microprogrammed and Hardwired:control unitControl Units : Microprogrammed and Hardwired:control unit
Control Units : Microprogrammed and Hardwired:control unit
 
INTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSORINTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSOR
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
 
Arm instruction set
Arm instruction setArm instruction set
Arm instruction set
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Register Organisation of 8086 Microprocessor
Register Organisation of 8086 MicroprocessorRegister Organisation of 8086 Microprocessor
Register Organisation of 8086 Microprocessor
 
Pipelining In computer
Pipelining In computer Pipelining In computer
Pipelining In computer
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 

Similar a Optimization

CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptssuser0be977
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design LogsAk
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankarDipankar Nalui
 
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsCSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsDhiviya Rose
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsPraveen Penumathsa
 
lect23_optimization.ppt
lect23_optimization.pptlect23_optimization.ppt
lect23_optimization.pptssuser0be977
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
loopoptimization-180418113642.pdf
loopoptimization-180418113642.pdfloopoptimization-180418113642.pdf
loopoptimization-180418113642.pdf16115yogendraSingh
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 

Similar a Optimization (20)

Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.ppt
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Code optimization
Code optimizationCode optimization
Code optimization
 
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming FundamentalsCSEG1001Unit 2 C Programming Fundamentals
CSEG1001Unit 2 C Programming Fundamentals
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
 
lect23_optimization.ppt
lect23_optimization.pptlect23_optimization.ppt
lect23_optimization.ppt
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
loopoptimization-180418113642.pdf
loopoptimization-180418113642.pdfloopoptimization-180418113642.pdf
loopoptimization-180418113642.pdf
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Cpmprt
CpmprtCpmprt
Cpmprt
 

Más de Royalzig Luxury Furniture

A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...Royalzig Luxury Furniture
 
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production UnitIndia's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production UnitRoyalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chairHand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chairRoyalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table Royalzig Luxury Furniture
 
Hand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa setHand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa setRoyalzig Luxury Furniture
 
Royalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood BedRoyalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood BedRoyalzig Luxury Furniture
 

Más de Royalzig Luxury Furniture (20)

A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
A Glimpse of Beautiful Hand-Carved Luxury Italian Furniture Photos from Royal...
 
French Style Slim Carving Bedroom Sets
French Style Slim Carving Bedroom SetsFrench Style Slim Carving Bedroom Sets
French Style Slim Carving Bedroom Sets
 
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production UnitIndia's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
India's Luxury Furniture Brand Royalzig setting-Up a New Production Unit
 
Luxury furniture manufacturer in india
Luxury furniture manufacturer in indiaLuxury furniture manufacturer in india
Luxury furniture manufacturer in india
 
bone inlay hand carved luxury table
bone inlay hand carved luxury table bone inlay hand carved luxury table
bone inlay hand carved luxury table
 
Hand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chairHand Carved Luxury & Designer Wooden arm chair
Hand Carved Luxury & Designer Wooden arm chair
 
beautiful hand carved dressing table
beautiful hand carved dressing table  beautiful hand carved dressing table
beautiful hand carved dressing table
 
Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table Hand Carved Luxury & Designer Wooden Dining Table
Hand Carved Luxury & Designer Wooden Dining Table
 
Hand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa setHand Carved Luxury & Designer Wooden sofa set
Hand Carved Luxury & Designer Wooden sofa set
 
Royalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood BedRoyalzig Hand Carved Luxury & Designer Wood Bed
Royalzig Hand Carved Luxury & Designer Wood Bed
 
hand carved luxury wedding throne
hand carved luxury wedding thronehand carved luxury wedding throne
hand carved luxury wedding throne
 
Hand carved Luxury wood divan
Hand carved Luxury wood divanHand carved Luxury wood divan
Hand carved Luxury wood divan
 
Royalzig luxury furniture
Royalzig luxury furnitureRoyalzig luxury furniture
Royalzig luxury furniture
 
Royalzigppt 004
Royalzigppt 004Royalzigppt 004
Royalzigppt 004
 
Royalzig high end furniture
Royalzig high end furnitureRoyalzig high end furniture
Royalzig high end furniture
 
Royalzigppt 002
Royalzigppt 002Royalzigppt 002
Royalzigppt 002
 
Transcript
TranscriptTranscript
Transcript
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Run time
Run timeRun time
Run time
 

Último

Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 

Último (20)

Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

Optimization

  • 1. C dCode O i i iOptimization P K Singh 1M M M Engg. College, Gorakhpur
  • 2. Code Optimization and Phases source front code intermediate code code intermediate code targetsource program front end code optimizer code code generator code target code symbol table P K Singh 2M M M Engg. College, Gorakhpur
  • 3. Optimization • Code produced by standard algorithms can often be made to run faster, take less space or both • These improvements are achieved through transformations called• These improvements are achieved through transformations called optimization • Compilers that apply these transformations are called optimizing compilerscompilers • It is especially important to optimize frequently executed parts of a program The 90/10 rule– The 90/10 rule – Profiling can be helpful, but compilers do not typically have sample input data – Inner loops tend to be good candidates for optimizationInner loops tend to be good candidates for optimization P K Singh 3M M M Engg. College, Gorakhpur
  • 4. Criteria for Transformations • A transformation must preserve the meaning of a programa program – Can not change the output produced for any input – Can not introduce an errorCan not introduce an error • Transformations should, on average, speed up programsprograms • Transformations should be worth the effort P K Singh 4M M M Engg. College, Gorakhpur
  • 5. Beyond Optimizing Compilers • Really improvements can be made at various phases • Source code: Algorithmic transformations can produce spectacular– Algorithmic transformations can produce spectacular improvements – Profiling can be helpful to focus a programmer's attention on important codep • Intermediate code: – Compiler can improve loops, procedure calls, and address calculationscalculations – Typically only optimizing compilers include this phase • Target code: Compilers can use registers efficiently– Compilers can use registers efficiently – Peephole transformation can be applied P K Singh 5M M M Engg. College, Gorakhpur
  • 6. Peephole Optimizations • A simple technique for locally improving target code (can also be applied to intermediate code)( ) • The peephole is a small, moving window on the target program • Each improvement replaces the instructions of the peephole with a shorter or faster sequence • Each improvement may create opportunities for additional improvements • Repeated passes may be necessary P K Singh 6M M M Engg. College, Gorakhpur
  • 7. Redundant-Instruction Elimination • Redundant loads and stores: (1)MOV R0 a(1)MOV R0, a (2)MOV a, R0 • Unreachable code: #define debug 0 if (debug) { /* i t d b i i f ti *//* print debugging information */ } P K Singh 7M M M Engg. College, Gorakhpur
  • 8. Flow-of-Control Optimizations • Jumps to jumps, jumps to conditional jumps, and conditional jumps to jumps are not necessary • Jumps to jumps example:p j p p – The following replacement is valid: goto L1 … goto L2 … – If there are no other jumps to L1 and L1 is preceded by an unconditional L1: goto L2 L1: goto L2 – If there are no other jumps to L1 and L1 is preceded by an unconditional jump, the statement at L1 can be eliminated • Jumps to conditional jumps and conditional jumps to jumps lead to similar transformations P K Singh 8M M M Engg. College, Gorakhpur
  • 9. Other Peephole Optimizationsp p • A few algebraic identities that occur frequently (such as x := x + 0 or x := x * 1) can be eliminated • Reduction in strength replaces expensive operations with cheaper onesoperations with cheaper ones – Calculating x * x is likely much cheaper than x^2 using an exponentiation routine – It may be cheaper to implement x * 5 as x * 4 + x • Some machines may have hardware instructions to implement certain specific operations efficiently – For example, auto-increment may be cheaper than a straight-For example, auto increment may be cheaper than a straight forward x := x + 1 – Auto-increment and auto-decrement are also useful when pushing into or popping off of a stackp g p pp g P K Singh 9M M M Engg. College, Gorakhpur
  • 10. Optimizing Intermediate Code • This phase is generally only included in optimizing compilersoptimizing compilers • Offers the following advantages: Operations needed to implement high level constructs– Operations needed to implement high-level constructs are made explicit (i.e. address calculations) – Intermediate code is independent of target machine;p g ; code generator can be replaced for different machine • We are assuming intermediate code uses three- address instructions P K Singh 10M M M Engg. College, Gorakhpur
  • 11. QuickSort in C void quicksort(int m, int n) { int i, j, v, x; if (n <= m) return;if (n < m) return; /* Start of partition code */ i = m-1; j = n; v =a[n]; while (1) { do i = i+1; while (a[i] < v); do j = j-1; while (a[j] > v); if (i >= j) break;( j) ; x = a[i]; a[i] = a[j]; a[j] = x; } x = a[i]; a[i] = a[n]; a[n] = x; /* E d f titi d *//* End of partition code */ quicksort(m, j); quicksort(i+1, n); } P K Singh 11M M M Engg. College, Gorakhpur
  • 12. Partition in Three-Address Code (1) i := m-1 (2) j := n (3) 1 4* (16) t7 := 4*i (17) t8 := 4*j (18) 9 [ 8](3) t1 := 4*n (4) v := a[t1] (5) i := i+1 (6) t2 := 4*i (18) t9 := a[t8] (19) a[t7] := t9 (20) t10 := 4*j (21) a[t10] := x( ) (7) t3 := a[t2] (8) if t3 < v goto (5) (9) j := j-1 (10) t4 : 4*j ( ) [ ] (22) goto (5) (23) t11 := 4*i (24) x := a[t11] (25) t12 : 4*i(10) t4 := 4*j (11) t5 := a[t4] (12) if t5 > v goto (9) (13) if i >= j goto (23) (25) t12 := 4*i (26) t13 := 4*n (27) t14 := a[t13] (28) a[t12] := t14 (14) t6 := 4*i (15) x := a[t6] (29) t15 := 4*n (30) a[t15] := x P K Singh 12M M M Engg. College, Gorakhpur
  • 13. Partition Flow Graph P K Singh 13M M M Engg. College, Gorakhpur
  • 14. Local vs. Global Transformations • Local transformations involve statements within a single basic block • All other transformations are called global transformations • Local transformations are generally performed first • Many types of transformations can be performed either locally or globally P K Singh 14M M M Engg. College, Gorakhpur
  • 15. Code Optimizations • Local/global common subexpression elimination • Dead code elimination• Dead-code elimination • Instruction reordering C t t f ldi• Constant folding • Algebraic transformations C i• Copy propagation • Loop optimizations 15P K Singh M M M Engg. College, Gorakhpur
  • 16. Loop Optimizations • Code motion • Induction variable elimination• Induction variable elimination • Reduction in strength l t• … lots more 16P K Singh M M M Engg. College, Gorakhpur
  • 17. Code Motion i := 0 t2 := 4*i B1: B2: i := 0 t1 := n-2 B1: t : A[t2] := 0 i := i+1 t2 := 4*i A[t2] := 0 i := i+1 B2: t1 := n-2 if i < t1 goto B2 B3: if i < t1 goto B2B3: Move loop-invariant computations before the loop 17P K Singh M M M Engg. College, Gorakhpur
  • 18. Strength Reduction i := 0 t1 := n-2 B1: i := 0 t1 := n-2 t2 := 4*i B1: t2 := 4*i A[t2] := 0 i := i+1 B2: A[t2] := 0 i := i+1 t2 := t2+4 B2: if i < t1 goto B2B3: if i < t1 goto B2B3: Replace expensive computations with induction variables 18P K Singh M M M Engg. College, Gorakhpur
  • 19. Reduction Variable Elimination i 0B1: t1 4*B1:i := 0 t1 := n-2 t2 := 4*i B1: B2 t1 := 4*n t1 := t1-8 t2 := 4*i B1: A[t2] := 0 i := i+1 t2 := t2+4 B2: A[t2] := 0 t2 := t2+4 B2: if i<t1 goto B2B3: if t2<t1 goto B2B3: Replace induction variable in expressions with another 19P K Singh M M M Engg. College, Gorakhpur
  • 20. Common Subexpressions • E is a common subexpression if: – E was previously computed– E was previously computed – Variables in E have not changed since previous computation • Can avoid recomputing E if previously computed value is still available • Dags are useful to detect common subexpressions P K Singh 20M M M Engg. College, Gorakhpur
  • 21. Local Common Subexpressionsp t6 := 4*i x := a[t6] 7 4*i t6 := 4*i [ 6]t7 := 4*i t8 := 4*j t9 := a[t8] a[t7] := t9 x := a[t6] t8 := 4*j t9 := a[t8] a[t6] := t9a[t7] := t9 t10 := 4*j a[t10] := x goto B2 a[t6] := t9 a[t8] := x goto B2 goto B2 P K Singh 21M M M Engg. College, Gorakhpur
  • 22. Global Common Subexpressionsp P K Singh 22M M M Engg. College, Gorakhpur
  • 23. Copy Propagation • Assignments of the form f := g are called copy statements (or copies) • The idea behind copy propagation is to use g for f whenever possible after such a statement • For example applied to block B5 of the previous• For example, applied to block B5 of the previous flow graph, we obtain: x := t3 [t2] t5a[t2] := t5 a[t4] := t3 goto B2 Copy propagation often turns the copy statement• Copy propagation often turns the copy statement into "dead code" P K Singh 23M M M Engg. College, Gorakhpur
  • 24. Dead-Code Elimination • Dead code includes code that can never be reached and code that computes a value that never gets used C id if (d b ) i• Consider: if (debug) print … – It can sometimes be deduced at compile time that the value of an expression is constant – Then the constant can be used in place of the expression (constant– Then the constant can be used in place of the expression (constant folding) – Let's assume a previous statement assigns debug := false and value never changes – Then the print statement becomes unreachable and can be eliminated • Consider the example from the previous slide The value of computed by the copy statement never gets used after– The value of x computed by the copy statement never gets used after the copy propagation – The copy statement is now dead code and can be eliminated P K Singh 24M M M Engg. College, Gorakhpur
  • 25. Loop Optimizations (1) • The running time of a program may be improved if we do both of the following:we do both of the following: – Decrease the number of statements in an inner loop – Increase the number of statements in the outer loopp • Code motion moves code outside of a loop – For example, consider:p , while (i <= limit-2) … – The result of code motion would be: t li it 2t = limit – 2 while (i <= t) … P K Singh 25M M M Engg. College, Gorakhpur
  • 26. Loop Optimizations (2) • Induction variables: variables that remain in "lock-step" – For example, in block B3 of previous flow graph, j and t4 are induction variablesinduction variables – Induction-variable elimination can sometimes eliminate all but one of a set of induction variables • Reduction in strength replaces a more expensive operationReduction in strength replaces a more expensive operation with a less expensive one – For example, in block B3, t4 decreases by four with every iteration – If initialized correctly, can replace multiplication with subtractiony – Often application of reduction in strength leads to induction-variable elimination • Methods exist to recognize induction variables and apply i t t f ti t ti llappropriate transformations automatically P K Singh 26M M M Engg. College, Gorakhpur
  • 27. Loop Optimization Examplep p p P K Singh 27M M M Engg. College, Gorakhpur
  • 28. Example • Given the following code segment, obtain: The 3AC statements for this computation– The 3AC statements for this computation – The basic blocks and the flow graph prod = 0; i = 1;prod 0; i 1; do { prod = prod + a[i] * b[i];prod = prod + a[i] b[i]; i = i + 1; } while ( i <= 20 );} while ( i <= 20 ); P K Singh 28M M M Engg. College, Gorakhpur
  • 29. Example …contd • 3AC for the given code segment (1) prod :=0 (2) i := 1 (3) t1 := 4 * i (9) prod := t6 (4) t2 := a [t1] (10) t7 := i + 1 (5) t3 := 4 * i (11) i := t7 (6) t4 := b [t3] (12) if i <= 20 goto (3) (7) t5 := t2 * t4(7) t5 := t2 * t4 (8) t6 := prod + t5 P K Singh 29M M M Engg. College, Gorakhpur
  • 30. Example …contd 2 Basic blocks (1) prod :=0 (2) i := 1 B1 (3) t1 := 4 * i (9) prod := t6 (4) t2 := a [t1] (10) t7 := i + 1 (5) t3 := 4 * i (11) i := t7 (6) t4 := b [t3] (12) if i <= 20 goto (3) (7) t5 := t2 * t4(7) t5 := t2 * t4 (8) t6 := prod + t5 B2 P K Singh 30M M M Engg. College, Gorakhpur B2
  • 31. Example …contd Flow Graph t1 := 4 * iB2 prod := 0 t1 := 4 i t2 := a [t1] t3 := 4 * i t4 := b [t3]B1 B2 i := 1 t5 := t2 * t4 t6 := prod + t5 prod := t6 t7 := i + 1t7 : i 1 i := t7 if i <= 20 goto B2B1 B2 P K Singh 31M M M Engg. College, Gorakhpur
  • 32. Implementing a Code Optimizer • Organization consists of control-flow analysis, then data- flow analysis, and then transformations • The code generator is applied to the transformedThe code generator is applied to the transformed intermediate code • Details of code optimization are beyond the scope of this coursecourse P K Singh 32M M M Engg. College, Gorakhpur