SlideShare a Scribd company logo
Code Optimization
Principal Sources of Optimization – Peep-hole
optimization – DAG- Optimization of Basic
Blocks–Global Data Flow Analysis – Efficient
Data Flow Algorithm – Recent trends in
Compiler Design
Code optimization
• program transformation technique, which tries to
improve the code by eliminating unnecessary
code lines and arranging the statements in a
sequence to make it consume less resources (i.e.
CPU, Memory) and deliver high speed.
Advantages:
 Executes faster.
 Efficient memory usage.
 yields better performance.
• Compiler front end
–Lexical analysis, Syntax analysis, Semantic
analysis
Tasks :
understanding the source code,
making sure the source code is written
correctly.
Code optimization
• Compiler back end:
–Intermediate code generation/improvement
and machine code generation/improvement.
Tasks:
translating the program to a
semantically the same program(in a different
language).
Code optimization
• A code optimizer sits between the frontend and
the code generated.
– Works with intermediate code
– Can do control flow analysis
– Can do data flow analysis
– Does transformations to improve the intermediate
code.
Code optimization is done on basic blocks.
Code optimization
Types of code optimization
• Machine Independent Optimization
– Improve the intermediate code to get a better target
code as output
– Intermediate code transformed doesn’t involve any
CPU registers or absolute memory locations
• Machine Dependent Optimization
– Improve the target code according to the target
machine architecture
– Involves CPU registers and may have absolute
memory references.
– Put efforts to take maximum advantage of the memory
1. Local Optimization
Apply to a single block
Straight line code
Simplest form
No need to analyse the whole procedure body
2. Global Optimisation
Apply across basic blocks
Applied to program segments that includes functions and loops
Data flow analysis is done to perform optimisation
3. Peephole Optimization
Performed on a small set of compiler generated instructions
Operates on the target code considering few instructions at a time
Do machine dependent improvements
Most compilers do (1), many do (2) and very few do (3)
Types of code optimization
Basic blocks:
Straight line code with no branches
Sequence of intermediate code with a single entry
and a single exit.
CFGs show control flow among basic blocks
Optimization is done on the basic blocks
A basic block begins in one of the following ways
The entry point into the functions
The target of a branch (can be a label)
The instruction immediately following a branch
Basic blocks and flow graph
A basic block ends in any of the following ways
a jump statement
a conditional or unconditional branch
a return statement.
Basic blocks and flow graph Contd…
Properties of basic blocks
Property 1:
Sequence of consecutive statements executed in
a sequential manner.
Property 2:
One entry point and one exit point
Property 3:
Should not contain conditional or unconditional
control statements in the middle of the block. It
may be the first or last statement.
Algorithm for partitioning a three address
code into basic block.
Rules
1. Determine the leaders
2. Determine the basic blocks.
1. Determine the leaders
• 1st statement is a leader
• Target of a conditional or unconditional
statement is a leader
• Statement following the conditional or
unconditional jump is a leader.
Determine the basic block and draw the flow graph for the given 3
address code
1. PROD = 0
2. I = 1
3. T2 = addr(A)
4. T4 = addr(B)
5. T1 = 4*I
6. T3 = T2[T1]
7. PROD = PROD+T3
8. I = I+1
9. If I<=20 goto(5)
10. j = j+1
11. k = k+1
12. If j<=5 goto(7)
13. I = i+j
Three address code
2. Determine the basic blocks
begin from the leader statement and end in the
statement before the next leader.
Leaders as per rules – 1,5,7,10,13
1. PROD = 0
2. I = 1 B1
3. T2 = addr(A)
4. T4 = addr(B)
5. T1 = 4*I
6. T3 = T2[T1] B2
7. PROD = PROD+T3
8. I = I+1 B3
9. If I<= 20 goto(5)(B2)
10. j = j+1
11. k = k+1 B4
12. If j<=5 goto(7)(B3)
13. i = i+j B5
Flow graph
Directed graph in which flow control information
is added to the basic blocks.
Basic blocks are the nodes of the graph.
B2
B1
B3
B4
B5
Directed Acyclic Graph(DAG)
DAG representation of a basic block
represent the structure of a basic block
1. In a DAG, internal node represents operators.
2. Leaf node represents identifiers, constants.
3. Internal node also represents result of
expressions.
t = a+b t
a b
+
Application of DAG
• Determining the common sub expressions.
• Determining which names are used inside the
blocks & computed outside the block.
• Determining which statement of the block
could have their computed value outside the
blocks.
• Simplifying the list of quadruples by
eliminating common sub expressions.
(eg) Construct DAG for the expression
a + a*(b-c)+(b-c)*d
+
+
* *
a - d
b c
a = b + c
b = a - d c
c = b + c
d = a - d - b, d
+ a d
b c
+
+
-
a = b*-c + b*-c
a
b
c
=
-
*
+
a = b+c
b = b-d
c = c+d
e = b+c
a b c
b c d
+
+
+ -
d = b * c
e = a + b
b = b * c
a = e – d a
e d, b
a b c
+
-
*
a = (a*b+c)-(a*b+c)
=
a -
+
* c
a b
Optimization of basic blocks
1. Finding Local Common sub expression
2. Dead code elimination.
3. Renaming temporary variables.
4. Interchange of statements.
5. Algebraic transformations.
Peephole Optimization
• Applied to improve the performance of
program by examining a short sequence of
instructions in a window(peephole) and
replace the instructions by a faster or short
sequence of instructions.
Peephole Optimization Techniques
1. Eliminating Redundant Loads and Stores.
2. Eliminating Unreachable Code.
3. Flow of control optimizations.
4. Algebraic Simplifications
5. Machine idioms.
1. Eliminating redundant loads and
stores
Consider one instruction
mov R0, a
mov a, R0
eliminate 2nd instruction since a is already in R0.
2. Eliminating Unreachable Code
• Eliminate the statements which are unreachable.
(i.e) never executed.
(eg:1) i = 0; (eg:2) int add(int a, b)
if(i==1) {
{ i=0 int c=a+b;
sum = 0; return c;
} printf(“%d”, c); Eliminate
}
3. Flow of Control Optimizations
• Unnecessary jumps can be eliminated
(eg)
Multiple jumps can make the code inefficient.
Above code can replaced
goto L1 goto L3
….. …..
L1: goto L2 L1: goto L3
….. …..
L2: goto L3 L2: goto L3
….. L3: Mov a, R0
L3: Mov a, R0
4. Algebraic Simplifications
(eg) x = x+0 (or) x = x*1
The above statements can be eliminated because
by executing those statements the result x
won’t change.
(eg) a = replaced with a = x*x
b = y/8 replaced with b = y>>3
5. Use of Machine Idioms
• It is the process of using powerful features of
CPU instructions.
(eg) auto inc/dec function can be used to inc/dec
variable.
a = a+1 replaced with inc a.
b = a-1 replaced with dec a.
Principal Sources of Optimization
(or) Semantic-Preserving
Transformation
1. Common sub expression elimination.
2. Compile time evaluation
 Constant folding.
 Constant propagation.
3. Code movement (or) Code motion.
4. Dead code elimination.
5. Strength reduction.
1. Common Sub expression
Elimination
• Common sub expression
– an expression(E) which appears repeatedly in the
program, which is computed previously but the
values of variables in expression haven’t changed.
– replaces the redundant expression each time it is
encountered.
Unoptimized code Optimized code
a=b+c a=b+c
b=a-d b=a-d
c=b+c c=b+c
d=a-d d=b
c=a-d
2. Compile Time Evaluation
• done at the compile time instead of run time.
a. Constant Folding:
Evaluate the expression and submit the
result.
(eg) area=(22/7)*r*r
22/7 is calculated and result 3.14 is
replaced.
Contd…
b. Constant Propagation:
propagate the constant through out the
expression.
Constant replace a variable.
(eg) pi = 3.14, r = 5
area = pi*r*r
area = 3.14*5*5
3. Code Movement (or) Code Motion
• moves the code outside the loop if it won’t
have any difference if it executes inside or
outside the loop.
Unoptimized Code Optimized Code
for(i=0; i<n; i++) x = y+3
{ for(i=0; i<n; i++)
x = y+3; {
a[i] = 6*i; a[i] = 6*i;
} }
4. Dead Code Elimination
• Includes elimination of those statements which
are never executed or if executes output is
never used.
(eg)
Unoptimized Code Optimized Code
i=0 i=0
if(i==i)
{
a= x+i;
}
Contd…
Unoptimized Code Optimized Code
int add(int x, int y) int add(int x, int y)
{ {
int z; int z;
z=x+y; z=x+y;
return z; return z;
printf(“%d”,z); }
}
5. Reduction in Strength
• Replacing expensive operator with cheapest
operator
b = a*2 is replaced with
b = a+a
Data Flow Analysis (DFA)
• All the optimization techniques depend on data flow analysis.
• DFA know about how the data is flowing in any control-flow
graph.
• DFA – collects all information about entire program and
distribute this information to each block in the flow graph.
• It helps in identifying the variables that hold values at different
points in the program and how these values change over time.
• This information is used to optimize the program by
eliminating dead code, identifying constant expressions, and
reducing unnecessary computations.
Data Flow Analysis
• Helps in improving the efficiency of the code generated by the
compiler
• Program debugging
• Program optimization
Global Data Flow Analysis
• Collect the data flow information about entire program and
distribute the information to each block
• Data flow is collected using the data flow equation
out [s] = gen [s] U(in[s] – kill[s])
out [s] – Info at the end of S
gen [s] - Info generated by S
in[s] - Info enter at the beginning of S
kill[s] – Info killed by S
i.e information that comes out of S is info generated by S +
information coming to the block S and the information killed
in the block S
Global Data Flow Analysis
• Structured Statement
S->id :=E/S ; S
S -> if E then S1 else S2/ do S while E
E- > id+id/id
Global Data Flow Analysis
Efficient Data Flow Algorithm
• Redundant common sub expression elimination
• Copy Propagation
a :=b
y = x z = 3 + y
Copy propagation would yield:
z = 3 + x
• Induction Variable
– The value of the variable changes value every time
– With each iteration, its value either gets incremented
or decremented by some constant value.
Principal Sources of Optimization in compiler design

More Related Content

PPTX
Code optimization
PPTX
Peephole optimization techniques in compiler design
PPTX
Principal source of optimization in compiler design
PPTX
Issues in design_of_code_generator
PDF
Issues in the design of Code Generator
PPT
Compiler Construction introduction
PPTX
Single pass assembler
PPT
Introduction to compiler
Code optimization
Peephole optimization techniques in compiler design
Principal source of optimization in compiler design
Issues in design_of_code_generator
Issues in the design of Code Generator
Compiler Construction introduction
Single pass assembler
Introduction to compiler

What's hot (20)

PPTX
Input-Buffering
PDF
I. AO* SEARCH ALGORITHM
PDF
Target language in compiler design
PPTX
Link state routing protocol
PDF
Artificial Intelligence - Hill climbing.
PPTX
Performance analysis(Time & Space Complexity)
PPTX
Type checking in compiler design
PPTX
Role-of-lexical-analysis
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
PPTX
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
PPTX
Structure of agents
PPT
Chapter 4 data link layer
PPTX
Finite automata-for-lexical-analysis
PPTX
Syntax Analysis in Compiler Design
PPTX
And or graph
PPT
Chapter 5 Syntax Directed Translation
PPTX
Framing in data link layer
PDF
Syntax Directed Definition and its applications
PDF
Address in the target code in Compiler Construction
PDF
Token, Pattern and Lexeme
Input-Buffering
I. AO* SEARCH ALGORITHM
Target language in compiler design
Link state routing protocol
Artificial Intelligence - Hill climbing.
Performance analysis(Time & Space Complexity)
Type checking in compiler design
Role-of-lexical-analysis
Type Checking(Compiler Design) #ShareThisIfYouLike
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Structure of agents
Chapter 4 data link layer
Finite automata-for-lexical-analysis
Syntax Analysis in Compiler Design
And or graph
Chapter 5 Syntax Directed Translation
Framing in data link layer
Syntax Directed Definition and its applications
Address in the target code in Compiler Construction
Token, Pattern and Lexeme
Ad

Similar to Principal Sources of Optimization in compiler design (20)

PDF
Code optimization in compiler design
PPTX
UNIT V - Compiler Design notes power point presentation
PPTX
Code optimization
PPTX
Code optmize.pptx which is related to coding
PPT
basics of optimizations presentation s
PDF
Code optimization lecture
PPTX
Compiler Design theory and various phases of compiler.pptx
PPT
457418.-Compiler-Design-Code-optimization.ppt
PPTX
Code Optimization
PPTX
complier design unit 5 for helping students
PPT
lect23_optimization.ppt
PPT
Code Optimization Lec#7.ppt Code Optimizer
PDF
SPCC_Sem6_Chapter 6_Code Optimization part
PPTX
compiler design-Intermediate code generation.pptx
PPT
u5 code optimization and code generation.ppt
PPTX
Machine_Learning_JNTUH_R18_UNIT5_CONCEPTS.pptx
PDF
PDF
PDF
Optimization in Programming languages
PDF
Intermediate code generation
Code optimization in compiler design
UNIT V - Compiler Design notes power point presentation
Code optimization
Code optmize.pptx which is related to coding
basics of optimizations presentation s
Code optimization lecture
Compiler Design theory and various phases of compiler.pptx
457418.-Compiler-Design-Code-optimization.ppt
Code Optimization
complier design unit 5 for helping students
lect23_optimization.ppt
Code Optimization Lec#7.ppt Code Optimizer
SPCC_Sem6_Chapter 6_Code Optimization part
compiler design-Intermediate code generation.pptx
u5 code optimization and code generation.ppt
Machine_Learning_JNTUH_R18_UNIT5_CONCEPTS.pptx
Optimization in Programming languages
Intermediate code generation
Ad

Recently uploaded (20)

PDF
English Language Teaching from Post-.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
Landforms and landscapes data surprise preview
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
PDF
Types of Literary Text: Poetry and Prose
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
English Language Teaching from Post-.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
How to Manage Loyalty Points in Odoo 18 Sales
Cardiovascular Pharmacology for pharmacy students.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
UPPER GASTRO INTESTINAL DISORDER.docx
Introduction and Scope of Bichemistry.pptx
Landforms and landscapes data surprise preview
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Week 4 Term 3 Study Techniques revisited.pptx
Onica Farming 24rsclub profitable farm business
Renaissance Architecture: A Journey from Faith to Humanism
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
Types of Literary Text: Poetry and Prose
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...

Principal Sources of Optimization in compiler design

  • 1. Code Optimization Principal Sources of Optimization – Peep-hole optimization – DAG- Optimization of Basic Blocks–Global Data Flow Analysis – Efficient Data Flow Algorithm – Recent trends in Compiler Design
  • 2. Code optimization • program transformation technique, which tries to improve the code by eliminating unnecessary code lines and arranging the statements in a sequence to make it consume less resources (i.e. CPU, Memory) and deliver high speed. Advantages:  Executes faster.  Efficient memory usage.  yields better performance.
  • 3. • Compiler front end –Lexical analysis, Syntax analysis, Semantic analysis Tasks : understanding the source code, making sure the source code is written correctly. Code optimization
  • 4. • Compiler back end: –Intermediate code generation/improvement and machine code generation/improvement. Tasks: translating the program to a semantically the same program(in a different language). Code optimization
  • 5. • A code optimizer sits between the frontend and the code generated. – Works with intermediate code – Can do control flow analysis – Can do data flow analysis – Does transformations to improve the intermediate code. Code optimization is done on basic blocks. Code optimization
  • 6. Types of code optimization • Machine Independent Optimization – Improve the intermediate code to get a better target code as output – Intermediate code transformed doesn’t involve any CPU registers or absolute memory locations • Machine Dependent Optimization – Improve the target code according to the target machine architecture – Involves CPU registers and may have absolute memory references. – Put efforts to take maximum advantage of the memory
  • 7. 1. Local Optimization Apply to a single block Straight line code Simplest form No need to analyse the whole procedure body 2. Global Optimisation Apply across basic blocks Applied to program segments that includes functions and loops Data flow analysis is done to perform optimisation 3. Peephole Optimization Performed on a small set of compiler generated instructions Operates on the target code considering few instructions at a time Do machine dependent improvements Most compilers do (1), many do (2) and very few do (3) Types of code optimization
  • 8. Basic blocks: Straight line code with no branches Sequence of intermediate code with a single entry and a single exit. CFGs show control flow among basic blocks Optimization is done on the basic blocks A basic block begins in one of the following ways The entry point into the functions The target of a branch (can be a label) The instruction immediately following a branch Basic blocks and flow graph
  • 9. A basic block ends in any of the following ways a jump statement a conditional or unconditional branch a return statement. Basic blocks and flow graph Contd…
  • 10. Properties of basic blocks Property 1: Sequence of consecutive statements executed in a sequential manner. Property 2: One entry point and one exit point Property 3: Should not contain conditional or unconditional control statements in the middle of the block. It may be the first or last statement.
  • 11. Algorithm for partitioning a three address code into basic block. Rules 1. Determine the leaders 2. Determine the basic blocks. 1. Determine the leaders • 1st statement is a leader • Target of a conditional or unconditional statement is a leader • Statement following the conditional or unconditional jump is a leader.
  • 12. Determine the basic block and draw the flow graph for the given 3 address code 1. PROD = 0 2. I = 1 3. T2 = addr(A) 4. T4 = addr(B) 5. T1 = 4*I 6. T3 = T2[T1] 7. PROD = PROD+T3 8. I = I+1 9. If I<=20 goto(5) 10. j = j+1 11. k = k+1 12. If j<=5 goto(7) 13. I = i+j Three address code
  • 13. 2. Determine the basic blocks begin from the leader statement and end in the statement before the next leader. Leaders as per rules – 1,5,7,10,13 1. PROD = 0 2. I = 1 B1 3. T2 = addr(A) 4. T4 = addr(B) 5. T1 = 4*I 6. T3 = T2[T1] B2
  • 14. 7. PROD = PROD+T3 8. I = I+1 B3 9. If I<= 20 goto(5)(B2) 10. j = j+1 11. k = k+1 B4 12. If j<=5 goto(7)(B3) 13. i = i+j B5
  • 15. Flow graph Directed graph in which flow control information is added to the basic blocks. Basic blocks are the nodes of the graph. B2 B1 B3 B4 B5
  • 16. Directed Acyclic Graph(DAG) DAG representation of a basic block represent the structure of a basic block 1. In a DAG, internal node represents operators. 2. Leaf node represents identifiers, constants. 3. Internal node also represents result of expressions. t = a+b t a b +
  • 17. Application of DAG • Determining the common sub expressions. • Determining which names are used inside the blocks & computed outside the block. • Determining which statement of the block could have their computed value outside the blocks. • Simplifying the list of quadruples by eliminating common sub expressions.
  • 18. (eg) Construct DAG for the expression a + a*(b-c)+(b-c)*d + + * * a - d b c
  • 19. a = b + c b = a - d c c = b + c d = a - d - b, d + a d b c + + -
  • 20. a = b*-c + b*-c a b c = - * +
  • 21. a = b+c b = b-d c = c+d e = b+c a b c b c d + + + -
  • 22. d = b * c e = a + b b = b * c a = e – d a e d, b a b c + - *
  • 23. a = (a*b+c)-(a*b+c) = a - + * c a b
  • 24. Optimization of basic blocks 1. Finding Local Common sub expression 2. Dead code elimination. 3. Renaming temporary variables. 4. Interchange of statements. 5. Algebraic transformations.
  • 25. Peephole Optimization • Applied to improve the performance of program by examining a short sequence of instructions in a window(peephole) and replace the instructions by a faster or short sequence of instructions.
  • 26. Peephole Optimization Techniques 1. Eliminating Redundant Loads and Stores. 2. Eliminating Unreachable Code. 3. Flow of control optimizations. 4. Algebraic Simplifications 5. Machine idioms.
  • 27. 1. Eliminating redundant loads and stores Consider one instruction mov R0, a mov a, R0 eliminate 2nd instruction since a is already in R0.
  • 28. 2. Eliminating Unreachable Code • Eliminate the statements which are unreachable. (i.e) never executed. (eg:1) i = 0; (eg:2) int add(int a, b) if(i==1) { { i=0 int c=a+b; sum = 0; return c; } printf(“%d”, c); Eliminate }
  • 29. 3. Flow of Control Optimizations • Unnecessary jumps can be eliminated (eg) Multiple jumps can make the code inefficient. Above code can replaced goto L1 goto L3 ….. ….. L1: goto L2 L1: goto L3 ….. ….. L2: goto L3 L2: goto L3 ….. L3: Mov a, R0 L3: Mov a, R0
  • 30. 4. Algebraic Simplifications (eg) x = x+0 (or) x = x*1 The above statements can be eliminated because by executing those statements the result x won’t change. (eg) a = replaced with a = x*x b = y/8 replaced with b = y>>3
  • 31. 5. Use of Machine Idioms • It is the process of using powerful features of CPU instructions. (eg) auto inc/dec function can be used to inc/dec variable. a = a+1 replaced with inc a. b = a-1 replaced with dec a.
  • 32. Principal Sources of Optimization (or) Semantic-Preserving Transformation 1. Common sub expression elimination. 2. Compile time evaluation  Constant folding.  Constant propagation. 3. Code movement (or) Code motion. 4. Dead code elimination. 5. Strength reduction.
  • 33. 1. Common Sub expression Elimination • Common sub expression – an expression(E) which appears repeatedly in the program, which is computed previously but the values of variables in expression haven’t changed. – replaces the redundant expression each time it is encountered.
  • 34. Unoptimized code Optimized code a=b+c a=b+c b=a-d b=a-d c=b+c c=b+c d=a-d d=b c=a-d
  • 35. 2. Compile Time Evaluation • done at the compile time instead of run time. a. Constant Folding: Evaluate the expression and submit the result. (eg) area=(22/7)*r*r 22/7 is calculated and result 3.14 is replaced.
  • 36. Contd… b. Constant Propagation: propagate the constant through out the expression. Constant replace a variable. (eg) pi = 3.14, r = 5 area = pi*r*r area = 3.14*5*5
  • 37. 3. Code Movement (or) Code Motion • moves the code outside the loop if it won’t have any difference if it executes inside or outside the loop. Unoptimized Code Optimized Code for(i=0; i<n; i++) x = y+3 { for(i=0; i<n; i++) x = y+3; { a[i] = 6*i; a[i] = 6*i; } }
  • 38. 4. Dead Code Elimination • Includes elimination of those statements which are never executed or if executes output is never used. (eg) Unoptimized Code Optimized Code i=0 i=0 if(i==i) { a= x+i; }
  • 39. Contd… Unoptimized Code Optimized Code int add(int x, int y) int add(int x, int y) { { int z; int z; z=x+y; z=x+y; return z; return z; printf(“%d”,z); } }
  • 40. 5. Reduction in Strength • Replacing expensive operator with cheapest operator b = a*2 is replaced with b = a+a
  • 41. Data Flow Analysis (DFA) • All the optimization techniques depend on data flow analysis. • DFA know about how the data is flowing in any control-flow graph. • DFA – collects all information about entire program and distribute this information to each block in the flow graph. • It helps in identifying the variables that hold values at different points in the program and how these values change over time. • This information is used to optimize the program by eliminating dead code, identifying constant expressions, and reducing unnecessary computations.
  • 42. Data Flow Analysis • Helps in improving the efficiency of the code generated by the compiler • Program debugging • Program optimization
  • 43. Global Data Flow Analysis • Collect the data flow information about entire program and distribute the information to each block • Data flow is collected using the data flow equation out [s] = gen [s] U(in[s] – kill[s]) out [s] – Info at the end of S gen [s] - Info generated by S in[s] - Info enter at the beginning of S kill[s] – Info killed by S i.e information that comes out of S is info generated by S + information coming to the block S and the information killed in the block S
  • 44. Global Data Flow Analysis • Structured Statement S->id :=E/S ; S S -> if E then S1 else S2/ do S while E E- > id+id/id
  • 45. Global Data Flow Analysis
  • 46. Efficient Data Flow Algorithm • Redundant common sub expression elimination • Copy Propagation a :=b y = x z = 3 + y Copy propagation would yield: z = 3 + x • Induction Variable – The value of the variable changes value every time – With each iteration, its value either gets incremented or decremented by some constant value.