Unit V QB
Unit V QB
Part- A
1. Why are quadruples preferred over triples in an optimizing compiler? (Nov/Dec 2020)
Quadruples are preferred over triples in an optimizing compiler as instructions
are often found to move around in it.
In case of triples, the result of any given operation is referred to by its position
and therefore if one instruction is moved, then it is required to make changes in all the
references that lead to that result.
5. Construct the DAG and identify the value numbers for the subexpressions of the
following expressions, assuming + associates from the left. (Nov/Dec 2020)
i) a*b + (a*b)
ii) a*b*a*b
6. Brief about the methodology used to locally improve the target code. (Nov/Dec 2020)
Peephole optimization is a technique for locally improving the target code which is
done by examining a sliding window of target instructions and replacing the instruction
sequences within the peephole by shorter or faster sequences wherever possible.
B1
(1) a=5
B2
(2) b=6
(3) if a<4 goto (1)
B3
return
10. Identify and write down the optimizations that could be performed on a peephole.
(April/May 2022)
Redundant load and store elimination
Constant folding
Strength Reduction
Null sequences/ Simplify Algebraic Expressions
Deadcode Elimination
Combine operations
13. What is flow graph? State its compilation process. (Nov/Dec 2020) (April/May 2023)
A flow graph is a directed graph containing control flow information for the set
of fundamental blocks.
A control flow graph shows how program control is parsed among the blocks
15. What is the cost of the following sequence of the instructions? (Nov/Dec 2019)
(i) MOV b, a
ADD c,a
(ii) MOV *R1, *R0
ADD *R2, *R0
16. Write the object code sequence for t=a+b produced by a typical code generator.
(April/May 2019)
MOV a, R0
ADD b, R0
MOV R0, t
18. Identify the constructs for optimization in basic block. (Nov/Dec 2016)
Dead Code Elimination.
Common Subexpression Elimination.
Renaming of Temporary variables.
Interchange of two independent adjacent statements.+
Example:
In the example below, the value assigned to i is never used, and the dead store can be
eliminated. The first assignment to global is dead, and the third assignment to global
is unreachable; both can be eliminated.
int global;
void f ()
{
int i;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
global = 3; /* unreachable */
}
Loop optimization
Loop optimization is most valuable machine-independent optimization because
program's inner loop takes bulk to time of a programmer.
If we decrease the number of instructions in an inner loop then the running time of a
program may be improved even if we increase the amount of code outside that loop.
For loop optimization the following three techniques are important:
1. Code motion
2. Induction-variable elimination
3. Strength reduction
1.Code Motion:
Code motion is used to decrease the amount of code in loop. This transformation
takes a statement or expression which can be moved outside the loop body without
affecting the semantics of the program.
For example
In the while statement, the limit-2 equation is a loop invariant equation.
1. while (i<=limit-2) /*statement does not change limit*/
2. After code motion the result is as follows:
3. a= limit-2;
4. while(i<=a) /*statement does not change limit or a*/
2. Induction-Variable Elimination
Induction variable elimination is used to replace variable from inner loop.
It can reduce the number of additions in a loop. It improves both code space and run
time performance.
In this figure, we can replace the assignment t4:=4*j by t4:=t4-4. The only problem
which will be arose that t4 does not have a value when we enter block B2 for the first
time. So we place a relation t4=4*j on entry to the block B2.
3. Reduction in Strength
o Strength reduction is used to replace the expensive operation by the cheaper
once on the target machine.
o Addition of a constant is cheaper than a multiplication. So we can replace
multiplication with an addition within the loop.
o Multiplication is cheaper than exponentiation. So we can replace exponentiation
with multiplication within the loop.
Example:
1. while (i<10)
2. {
3. j= 3 * i+1;
4. a[j]=a[j]-2;
5. i=i+2;
6. }
After strength reduction the code will be:
1. s= 3*i+1;
2. while (i<10)
3. {
4. j=s;
5. a[j]= a[j]-2;
6. i=i+2;
7. s=s+6;
8. }
2. Use the following code to identify the leader instructions and their corresponding
basic blocks and draw the control flow graph below. (Nov/Dec 2020)
1) P : = 0
2) I : = 1
3) P : = P + I
4) IF P < = 60 GO TO (7)
5) P : = 0
6) I : = 5
7) T1 : = I ∗ 2
8) I : = T1 + 1
9) IF I <= 20 GO TO (3)
10) K : = P ∗ 3
B1
1) P : = 0
2) I : = 1
B2
3) P : = P + I
4) IF P < = 60 GO TO B4
False
B3
5) P : = 0
6) I : = 5
True True
B4
7) T1 : = I ∗ 2
8) I : = T1 + 1
9) IF I <= 20 GO TO B2
B5 False
10) K : = P ∗ 3
MOV x, R0
MOV R0, R1
We can delete the first instruction and re-write the sentence as:
MOV x, R1
Unreachable code
Unreachable code is a part of the program code that is never accessed because of
programming constructs. Programmers may have accidently written a piece of code
that can never be reached.
Example:
void add_ten(int x)
{
return x + 10;
printf(“value of x is %d”, x);
}
In this code segment, the printf statement will never be executed as the program
control returns back before it can execute, hence printf can be removed.
MOV R1, R2
GOTO L1
...
L1 : GOTO L2
L2 : INC R1
In this code,label L1 can be removed as it passes the control to L2. So instead of
jumping to L1 and then to L2, the control can directly reach L2, as shown below:
...
MOV R1, R2
GOTO L2
...
L2 : INC R1
Strength reduction
There are operations that consume more time and space. Their ‘strength’ can
be reduced by replacing them with other operations that consume less time and space,
but produce the same result. For example, x * 2 can be replaced by x << 1, which
involves only one left shift. Though the output of a * a and a2 is same, a2 is much
more efficient to implement.
6. Explain the DAG representation and construction algorithm in detail. (Nov/Dec 2020)
(Nov/Dec 2023)
A DAG for basic block is a directed acyclic graph with the following labels on nodes:
1. The leaves of graph are labeled by unique identifier and that identifier can be
variable names or constants.
2. Interior nodes of the graph is labeled by an operator symbol.
3. Nodes are also given a sequence of identifiers for labels to store the computed
value.
o DAGs are a type of data structure. It is used to implement transformations on
basic blocks.
o DAG provides a good way to determine the common sub-expression.
o It gives a picture representation of how the value computed by the statement is
used in subsequent statements.
Example
a=b+c
b=a-d
c=b+c
d=a-d
a0=b0+c0
b1=a0-d0
c1=b1+c0
d1=a0-d0
The above code can be optimized using the common sub-expression elimination as
t1: = 4*i
t2: = a [t1]
t3: = 4*j
t5: = n
t6: = b [t1] +t5
The common sub expression t4: =4*i is eliminated as its computation is already in t1
and the value of i is not been changed from definition to use.
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
after the copy statement f: = g. Copy propagation means use of one variable instead of
another. This may not appear to be an improvement, but as we shall see it gives us an
opportunity to eliminate x.
For example:
x=Pi;
A=x*r*r;
The optimization using copy propagation can be done as follows: A=Pi*r*r;
Here the variable x is eliminated
Dead-Code Eliminations:
A variable is live at a point in a program if its value can be used subsequently;
otherwise, it is dead at that point. A related idea is dead or useless code, statements that
compute values that never get used. While the programmer is unlikely to introduce any
dead code intentionally, it may appear as the result of previous transformations.
Example:
i=0;
if(i=1)
{
a=b+5;
}
Here, ‘if’ statement is dead code because this condition will never get satisfied.
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. One advantage of copy propagation
is that it often turns the copy statement into dead code.
For example,
a=3.14157/2 can be replaced by
a=1.570 there by eliminating a division operation.
Loop Optimizations:
In loops, especially in the inner loops, programs tend to spend the bulk of their
time. The running time of a program may be improved if the number of instructions in
an inner loop is decreased, even if we increase the amount of code outside that loop.
Code Motion:
An important modification that decreases the amount of code in a loop is code
motion. This transformation takes an expression that yields the same result independent
of the number of times a loop is executed (a loop-invariant computation) and places the
expression before the loop. Note that the notion “before the loop” assumes the existence
of an entry for the loop. For example, evaluation of limit-2 is a loop-invariant
computation in the following while-statement:
Induction Variables:
Loops are usually processed inside out. For example consider the loop around
B3. Note that the values of j and t4 remain in lock-step; every time the value of j
decreases by 1, that of t4 decreases by 4 because 4*j is assigned to t4. Such identifiers
are called induction variables.
When there are two or more induction variables in a loop, it may be possible to
get rid of all but one, by the process of induction-variable elimination. For the inner
loop around B3 in above figure we cannot get rid of either j or t4 completely; t4 is used
in B3 and j in B4.