Professional Documents
Culture Documents
Unit-5-1
Unit-5-1
UNIT-5
2
SRMIST, Vadapalani Campus
BASIC BLOCKS AND FLOW GRAPHS
BASIC BLOCKS
● A basic block is a sequence of consecutive statements in which flow of
control enters at the beginning and leaves at the end without any halt or
possibility of branching except at the end.
5
SRMIST, Vadapalani Campus
Transformations on Basic Blocks:
7
SRMIST, Vadapalani Campus
2. Dead-code elimination:
● Suppose x is dead, that is, never subsequently used, at the point where the
statement x : = y + z appears in a basic block.
● Then this statement may be safely removed without changing the value of the basic
block.
8
SRMIST, Vadapalani Campus
4. Interchange of statements:
● Suppose a block has two adjacent statements
● We can interchange the two statements without affecting the
value of the block if and only if neither x nor y is t1 and neither b nor c is t2.
Algebraic transformations
● It can be used to change the set of expressions computed by a basic block into an
algebraically equivalent set.
i) Simplify expression:
Statement such as x : = x + 0 or x : = x * 1 can be eliminated from a basic block without
changing the set of expressions it computes.
ii) Replace expensive operations by cheaper ones:
The exponential operator in the statement x : = y * * 2 can be replaced by x : = y * y.
9
SRMIST, Vadapalani Campus
FLOW GRAPHS:
10
SRMIST, Vadapalani Campus
● There is a directed edge from block B1 to block B2 if B2 can immediately
follow B1 in some execution sequence; i.e., if
○ there is a conditional or unconditional jump from the last statement of B1 to
the first statement of B2,
○ B2 immediately follows B1 in the order of the program, and B1 does not end
in an unconditional jump.
● Then B1 is predecessor of B2, and B2 is a successor of B1
11
SRMIST, Vadapalani Campus
Basic block 1: Statement (1) to (2)
Basic block 2: Statement (3) to (12)
12
SRMIST, Vadapalani Campus
Representation of Basic Blocks:
● After partitioning the three-address statements, each basic block can be represented
by a variety of data structures
○ record consisting of a count of the number of quadruples in the block
○ pointer to the leader (first quadruple) of the block
○ lists of predecessors and successors of the block
● An alternative is to make a linked list of the quadruples in each block
Loops
Question: In a flow graph, what is a loop, and how does one find all loops?
● A loop is a collection of nodes in a flow graph such that
1. All nodes in the collection are strongly connectedfrom any node in the loop to
any other, there is a path of length one or more, wholly within the loop
2. The collection of nodes has a unique entrya node in the loop has only way to
reach a node of the loop from a node outside the loop to first go through the entry
● A loop that contains no other loops is called an inner loop.
13
SRMIST, Vadapalani Campus
NEXT-USE INFORMATION
● Collect next-use information about names in basic blocks.
● If the name in a register is no longer needed, then we remove the name
from the register and the register can be used to store some other names
● Idea: keeping a name in storage only if it will be used subsequently
Computing Next Uses
● Suppose three-address statement i assigns a value to x.
● If statement j has x as an operand, and control can flow from statement i
to j along a path that has no intervening assignments to x, then we say
statement j uses the value of x computed at i.
● To determine for each three-address statement x : = y op z and what is the
next uses of x, y and z. Need to use backward scan
14
SRMIST, Vadapalani Campus
Storage for Temporary Names
● Pack two temporaries into the same location if they are not live simultaneously
● Since almost all temporaries are defined and used within basic blocks, next-use
information can be applied to pack temporaries.
15
SRMIST, Vadapalani Campus
● We can allocate storage locations for temporaries by examining and assigning a
temporary to the first location in the field for temporaries that does not contain a live
temporary.
● If a temporary cannot be assigned to any previously created location, add a new
location to the data area for the current procedure.
● Temporaries can be packed into registers rather than memory locations
● Example: six temporaries can be packed into two locations. These locations
correspond to t1 and t2
16
SRMIST, Vadapalani Campus
INTRODUCTION - CODE OPTIMIZATION
● The code produced by the straight forward compiling algorithms can often be
made to run faster or take less space, or both.
● This improvement is achieved by program transformations that are traditionally
called optimizations.
● Compilers that apply code-improving transformations are called optimizing
compilers.
● It increases the execution speed of the program
● It reduces the storage space
● Optimization can be categorized into two types:
1) Machine dependent – are program transformations that improve the target
code without taking into consideration any properties of the target machine.
2) Machine independent - are based on register allocation and utilization of
special machine-instruction sequences.
17
SRMIST, Vadapalani Campus
The criteria for code improvement transformations:
● A transformation must preserve the meaning of programs.
i.e., the optimization must not change the output produced by a program
for a given input, or cause an error.
● A transformation must, on the average, speed up programs by a
measurable amount
● The transformation must be worth the effort
Getting Better Performance
18
SRMIST, Vadapalani Campus
An Organization for an Optimizing Compiler
● This concentrates on the transformation of intermediate code using the
organization
Example:
● Assume that the intermediate code consists of three-address statements
● A sorting C program for quicksort will be used for code improving
transformation
19
SRMIST, Vadapalani Campus
Assuming that each array element takes four bytes:
20
SRMIST, Vadapalani Campus
Flow graph for the program
In code optimizer, programs are represented by
flow graphs in which edges indicate the flow of
control and nodes represents basic blocks
21
SRMIST, Vadapalani Campus
PRINCIPAL SOURCES OF OPTIMIZATION
● Techniques for implementing these transformations are presented here
● A transformation of a program is called local if it can be performed by looking only at
the statements in a basic block; otherwise, it is called global
● Many transformation can be performed both in local and global levels
● Local transformation are usually performed first
Function-Preserving Transformations
● There are a number of ways in which a compiler can improve a program without
changing the function it computes
Examples are
● Common sub expression elimination
● Copy propagation
● Dead-code elimination and
● Constant folding 22
SRMIST, Vadapalani Campus
1. Common Sub expressions elimination
● An occurrence of an expression E is called a common sub-expression if E
was previously computed, and the values of variables in E have not
changed since the previous computation. i.e., avoid re-computing the expression
Use the previously computed value
23
SRMIST, Vadapalani Campus
Example: Consider the three-address code of quicksort
In B5 can be replaced by
t9=a[t4], a[t4]=x
24
SRMIST, Vadapalani Campus
2. Copy Propagation
● Assignments of the form f : = g called copy statements, or copies for short.
● The idea behind the copy-propagation transformation is to use g for f,
whenever possible
This block can be
further improved by
eliminating x using
new transformation
25
SRMIST, Vadapalani Campus
3 Dead Code Elimination or useless code
● A variable is live at a point in a program if its value can be used
subsequently; otherwise, it is dead at that point.
● Statement that computes values that never get used
Dead-code elimination
● Programmer may not introduce any dead code removes the assignment
● It may appear as a result of previous transformations to x and transforms into:
4 Constant Folding
● deducing at compile time that the value of an expression is a constant
and using the constant instead is known as constant folding.
26
SRMIST, Vadapalani Campus
Loop Optimization
● The inner loops where programs tend to spend the bulk of their time.
● The running time of a program may be improved if we decrease the
number of instructions in an inner loop, even if we increase the amount of
code outside that loop.
Three techniques are important for loop optimization:
● code motion which moves code outside a loop;
● Induction-variable elimination which eliminate variables from inner
loop
● Reduction in strength which replaces expensive operation by a cheaper
one, such as a multiplication by an addition
27
SRMIST, Vadapalani Campus
Code motion which moves code outside a loop;
● It decreases the amount of code in a loop is code motion.
● Transformation takes an expression that yields the same result independent of
the number of times a loop is executed (loop-invariant computation) and places
the expression before the loop.
Example:
28
SRMIST, Vadapalani Campus
Induction Variables and Reduction in Strength
29
SRMIST, Vadapalani Campus
Example:
30
SRMIST, Vadapalani Campus
After reduction in strength is applied to the inner
loops around B2 and B3
31
SRMIST, Vadapalani Campus
OPTIMIZATION OF BASIC BLOCKS
● A number of code-improving transformations for basic blocks includes
structure-preserving transformations, such as common subexpression
elimination and dead-code elimination, and algebraic transformations such
as reduction in strength.
● Many of the structure-preserving transformations can be implemented by
constructing a dag for a basic block.
● For common subexpressions: Say a new node m is about to be added;
there is an existing node n with the same children, same order, and with
same operator. Then n computes the same value as m and may be used in
its place.
32
SRMIST, Vadapalani Campus
However, the node with fourth statement d:=a-d has the
operator - and the nodes labeled a and do as children.
Consider the third statement c : =b+c if both b and d are live on exit, then a fourth statement
must be used to copy the value from one to the other
the use of b in b+c refers to the node labeled -,
because that is the most recent definition of b.
33
SRMIST, Vadapalani Campus
Example: Consider the expressions that are guaranteed to
compute the same value, no matter how that value is computed.
The dag method will miss the fact that the expression
computed by the first and fourth statements in the
sequence is the same, i.e., b+c
34
SRMIST, Vadapalani Campus
The Use of Algebraic Identities
● Algebraic identities represent another important class of optimizations on
basic blocks.
Example:
● If t is not needed outside this block, we can change this sequence to using
both the associativity and commutativity of +
Example:
36
SRMIST, Vadapalani Campus
PEEPHOLE OPTIMIZATION
● A statement-by-statement code-generations strategy often produce target
code that contains redundant instructions and suboptimal constructs .
● The quality of such target code can be improved by applying “optimizing”
transformations to the target program.
● A simple but effective technique for improving the target code is peephole
optimization, a method for trying to improving the performance of the
target program by examining a short sequence of target instructions (called
the peephole) and replacing these instructions by a shorter or faster
sequence, whenever possible.
● Peephole optimization is a technique for improving the quality of the target
code, it can also be applied directly after intermediate code generation to
improve the intermediate representation
37
SRMIST, Vadapalani Campus
PEEPHOLE OPTIMIZATION
● The peephole is a small, moving window on the target program. The code in the
peephole need not contiguous, although some implementations do require as
improvements.
● The following examples of program transformations that are characteristic
of peephole optimizations:
Redundant-instructions elimination
Flow-of-control optimizations
Algebraic simplifications
Use of machine idioms
Redundant Loads And Stores:
we can delete instructions (2) because whenever (2) is
If we see the instructions sequence executed. (1) will ensure that the value of
(1) MOV R0,a a is already in register R0.If (2) had a label we could not
be sure that (1) was always executed
(2) MOV a,R0 immediately before (2) and so we could not remove (2).
38
SRMIST, Vadapalani Campus
Unreachable Code:
41
SRMIST, Vadapalani Campus
● If there are now no jumps to L1, then it may be possible to eliminate the statement
L1:goto L2 provided it is preceded by an unconditional jump
● Similarly, the sequence
42
SRMIST, Vadapalani Campus
Algebraic Simplification:
● There is no end to the amount of algebraic simplification that can be attempted
through peephole optimization.
● Only a few algebraic identities occur frequently enough that it is worth considering
implementing them
● For example, statements such as x := x+0 Or x := x * 1
● Are often produced by straightforward intermediate code-generation algorithms,
and they can be eliminated easily through peephole optimization.
Reduction in Strength:
● Reduction in strength replaces expensive operations by equivalent cheaper ones
on the target machine.
● Certain machine instructions are considerably cheaper than others and can often
be used as special cases of more expensive operators.
● For example, x² is invariably cheaper to implement as x*x than as a call to an
exponentiation routine.
43
SRMIST, Vadapalani Campus
● Fixed-point multiplication or division by a power of two is cheaper to implement as
a shift.
● Floating-point division by a constant can be implemented as multiplication by a
constant, which may be cheaper.
Use of Machine Idioms:
● The target machine may have hardware instructions to implement certain specific
operations efficiently.
● For example, some machines have auto-increment and auto-decrement addressing
modes. These add or subtract one from an operand before or after using its value.
● The use of these modes greatly improves the quality of code when pushing or
popping a stack, as in parameter passing.
● These modes can also be used in code for statements like i : =i+1.
44
SRMIST, Vadapalani Campus
BUILDING EXPRESSION OF DAG
● Directed acyclic graphs (dags) are useful data structures for implementing
transformations on basic blocks.
● A dag gives a picture of how the value computed by each statement in a
basic block is used in subsequent statements of the block.
● Constructing a dag from three-address statements is a good way of
determining common sub expressions (expressions computed more than
once) within a block.
45
SRMIST, Vadapalani Campus
Dag Construction
46
SRMIST, Vadapalani Campus
Two details should be mentioned
● First, if x (not x0) had previously labeled some other node, we remove that
label, since the "current" value of x is the node just created.
● Second, for an assignment such as x : = y we do not create a new node.
we append label x to the list of names on the node for the "current " value
of y.
47
SRMIST, Vadapalani Campus
Example
48
SRMIST, Vadapalani Campus
49
SRMIST, Vadapalani Campus
50
SRMIST, Vadapalani Campus
Application of DAG
51
SRMIST, Vadapalani Campus
Stages in DAG Construction Process
52
SRMIST, Vadapalani Campus
53
SRMIST, Vadapalani Campus
Generating Code from DAGS
● The advantage of generating code for a basic block from its dag
representation is that, from a dag we can easily see how to rearrange the
order of the final computation sequence than we can starting from a linear
sequence of three-address statements or quadruples
54
SRMIST, Vadapalani Campus
55
SRMIST, Vadapalani Campus
Rearranging the order
The order in which computations are done can affect the cost of resulting object code.
56
SRMIST, Vadapalani Campus
Possible questions from code generation
and code optimization
1. Convert the source code (program/expression/statement) to three-address code
2. Find the syntax tree for three-address code
3. Construct the dag for three-address code
4. Find the optimized dag for three-address code
5. Find the number of basic blocks present in the given three address code
6. Draw the flow graph for three-address code
7. Find the optimization of basic block (apply any transformation method and find the
optimized code)
8. Generate the code for three address code
9. Generate the optimized code for three-address code
10. Construct code for the following sentences
57
SRMIST, Vadapalani Campus