Chapter 7 and 8
Chapter 7 and 8
Chapter 7 and 8
Compiler Design • Code optimization is a technique which tries to improve the code by eliminating
unnecessary code lines and arrange the statement such in a sequence speed up the
program execution without wasting the resources.
Advantage:
• faster execution speed
• utilizes the memory efficiently
• gives better performance
27-Jun-22 Fikru Tafesse(MSc. in Computer Science) 1 2
Cont'd ...
Code Optimization Techniques 1. Compile Time Evaluation
• Two techniques that falls under compile time evaluation are:
• Important code optimization techniques are:
i. Constant Folding
• it involves folding of the constants.
• The expressions that contain the operands having constant values at compile time are evaluated.
• Those expressions are then replaced with their respective results.
Example : Circumference of Circle = (22/7) x Diameter
• Here,
3 4
Cont'd ... Cont'd ...
2. Common Sub-Expression Elimination
ii. Constant Propagation
• The expression that has been already computed before and appears again in the code for computation is
• If some variable has been assigned some constant value, then it replaces that variable
called as Common Sub-Expression.
with its constant value in the further program during compilation. • In this technique,
• The condition is that the value of variable must not get alter in between. • As the name suggests, it involves eliminating the common sub expressions.
Example : pi = 3.14 • The redundant expressions are eliminated to avoid their re-computation.
• The already computed result is used in the further program when required.
radius = 10
Example : Code Before Optimization Code After Optimization
Area of circle = pi x radius x radius
S1 = 4*i S1 = 4*i
• Here, S2 = a[S1] S2 = a[S1]
• This technique substitutes the value of variables ‘pi’ and ‘radius’ at compile time. S3 = 4*j S3 = 4*j
S4 = 4*i // Redundant Expression S5 = n
• It then evaluates the expression 3.14 x 10 x 10.
S5 = n S6 = b[S44] + S5
• The expression is then replaced with its result 314. S6 = b[S44] + S5
7 8
5. Strength Reduction
Cont'd ... Code Generation
• Strength reduction is used to replace the expensive operation by the cheaper once on the • Code generation can be considered as the final phase of compilation.
target machine. • The code generated by the compiler is an object code of some lower-level
• Addition of a constant is cheaper than a multiplication. programming language, for example, assembly language.
• So, we can replace multiplication with an addition within the loop. • We have seen that the source code written in a higher-level language is transformed
• Multiplication is cheaper than exponentiation. into a lower-level language that results in a lower-level object code, which should have
• So, we can replace exponentiation with multiplication within the loop. the following minimum properties:
Example :
Code Before Optimization Code After Optimization It should carry the exact meaning of the source code.
It should be efficient in terms of CPU usage and memory management.
B=A*2 B=A+A • Code generator is used to produce the target code for three-address statements.
i= j3 i = j *j * j
• It uses registers to store the operands of the three-address statement.
9 10
Cont'd ...
• Example: Consider the three-address statement x: = y + z. Issues in the design of a Code Generator
• It can have the following sequence of codes:
• The following issues arise during the code generation phase:
MOV z, R0
• Input to the code generator
ADD y, R0
• Target program
• Code generator converts the intermediate representation of source code into a form that
• Memory management
can be readily executed by the machine.
• Instruction selection
• A code generator is expected to generate the correct code.
• Register allocation
• Evaluation order
after the other. • Output: List of basic blocks with each three-address statement in exactly one block
• Method: First identify the leader in the code.
• The characteristics of basic blocks are:
• Rule-01: Determining Leaders:
o They do not contain any kind of jump statements in them.
1. The first instruction in three address code statement is a leader.
o There is no possibility of branching or getting halt in the middle.
2. Any instructions that is the target of conditional or unconditional jump is
o All the statements execute in the same order they appear. a leader
o They do not lose the flow control of the program. 3. Any instruction that immediately follows a conditional or unconditional
jump is a leader.
13 14
1. All the statements that follow the leader (including the leader) till the next leader
appears form one basic block. • Block B1 is the initial node.
• Block B2 immediately follows B1, so from
2. The first statement of the code is called as the first leader.
B2 to B1 there is an edge.
3. The block containing the first leader is called as Initial block.
• The target of jump from last statement of
2. Flow Graphs: is a directed graph with flow control information added to the basic blocks. B1 is the first statement B2.
1. The nodes of the flow graph are basic blocks. • So, from B1 to B2 there is an edge.
15 16
Cont'd ... • Example 3: Consider the following source code: Cont'd ...
• Example 2: Consider the following source code
17 18 End