0% found this document useful (0 votes)
18 views15 pages

Unit V QB

compiler design question bank

Uploaded by

Jeeva R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Unit V QB

compiler design question bank

Uploaded by

Jeeva R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Unit- V

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.

2. What are the global common sub expressions ? (Nov/Dec 2020)


Along every path to P in the flow graph:
• E (Expression) must be evaluated at least once
• no variables in E redefined after the last evaluation
Observations:
E may have different values on different paths

3. What are the control-flow constraints ? (Nov/Dec 2020)


Precedence constraints link executables, containers, and tasks in packages in a control
flow, and specify conditions that determine whether executables run. An executable can
be a For Loop, Foreach Loop, or Sequence container; a task; or an event handler.

4. Define common sub expressions. (Nov/Dec 2018)


The expression or sub-expression that has been appeared and computed before
and appears again during the computation of the code is the common sub-expression.
Elimination of that sub-expression is known as Common sub-expression elimination.

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.

7. What is a basic block? Give an example. (Nov/Dec 2020) (Nov/Dec 2023)


Basic Block is a straight line code sequence that has no branches in and out
branches except to the entry and at the end respectively. Basic Block is a set of
statements that always executes one after other, in a sequence.
(1) a=5
(2) b=6
(3) if a<4 goto (1)
(4) return

B1
(1) a=5
B2
(2) b=6
(3) if a<4 goto (1)
B3
return

8. Define constant folding (April/May 2022)


 Constant folding in compiler design is a compiler optimization technique that
eliminates expressions of the code whose value can be computed before
executing the code.
 It implies that if we can determine the value of an expression at compile time
itself instead of computing it at run time, then this technique will eliminate it
and make the code efficient.

9. What are the principles sources of optimization? (Nov/Dec 2019)


The key areas of code optimization in compiler design are instruction
scheduling, register allocation, loop unrolling, dead code elimination, constant
propagation, and function inlining. These techniques aim to make code faster, more
efficient, and smaller while preserving its functionality.

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

11. Define peephole optimization. (Nov/Dec 2020) (April/May 2023)


Peephole optimization is an optimization method performed on a small set of
compiler generated instructions; the small set is known as peephole optimization in
compiler design or window.
There are three objectives of peephole optimization Improve performance,
Reduce memory footprint, and Reduce code size.

12. Define – code motion. (Nov/Dec 2020)


 The evaluation frequency of expression is reduced.
 The loop invariant statements are brought out of the loop.
Example
a = 200;
while(a>0)
{
b = x + y;
if (a % b == 0)
printf(“%d”, a);
}

//This code can be further optimized as


a = 200;
b = x + y;
while(a>0)
{
if (a % b == 0}
printf(“%d”, a);
}

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

14. Construct a DAG for the following code (Nov/Dec 2019)


a=b+c
b=a-d
c=b+c
d=a-d

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

(i) MOV b, a 1+1+1=3


ADD c,a 1+1+1=3
(ii) MOV *R1, *R0 1+2+1=4
ADD *R2, *R0 1+2+1=4

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

17. What are the properties of optimizing compiler? (Nov/Dec 2021)


 Compile Time Evaluation
 Common Subexpression Elimination
 Variable Propagation
 Dead Code Elimination.

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.+

19. What are the characteristics of peephole optimization? (Nov/Dec 2016)


 Improved performance
 Reduced memory footprint
 Reduced code size

20. What do you mean by copy propagation? (April/May 2017)


 Copy propagation is the process in which the occurrence of targets of direct
assignments(e.g.,b=5, a=b) is replaced directly with their values.
 Copy propagation is related to common subexpressions where copies are
introduced during common expression elimination.

21. What is DAG? Give example. (Nov/Dec 2023)


A DAG has leaves corresponding to atomic operands and interior codes
corresponding to operators. The difference is that a node N in a DAG has more than
one parent if N represents a common subexpression; in a syntax tree, the tree for the
common subexpression would be replicated as many times as the subexpression
appears in the original expression.
Part- B
1. Discuss in detail the role of dead code elimination and loop optimization during code
optimization of a compiler. (Nov/Dec 2020)/ Write in detail about loop optimization.
(Nov/Dec 2018)
Dead Code Elimination
Code that is unreachable or that does not affect the program (e.g. dead stores) can be
eliminated.

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 */
}

Below is the code fragment after dead code elimination.


int global;
void f ()
{
global = 2;
return;
}

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

3. Construct the dag for the following basic block.


d : = b* c
e:=a+b
b:=b*c
a:=e–d
4. Discuss in detail about global data flow analysis. (Nov/Dec 2018) (Nov/Dec
2020)(Nov/Dec 23)
o To efficiently optimize the code compiler collects all the information about the
program and distribute this information to each block of the flow graph. This
process is known as data-flow graph analysis.
o Certain optimization can only be achieved by examining the entire program. It
can't be achieve by examining just a portion of the program.
o For this kind of optimization user defined chaining is one particular problem.
o Here using the value of the variable, we try to find out that which definition of a
variable is applicable in a statement.
Based on the local information a compiler can perform some optimizations. For
example, consider the following code:
1. x = a + b;
2. x = 6 * 3
o In this code, the first assignment of x is useless. The value computer for x is never
used in the program.
o At compile time the expression 6*3 will be computed, simplifying the second
assignment statement to x = 18;
Some optimization needs more global information. For example, consider the
following code:
1. a = 1;
2. b = 2;
3. c = 3;
4. if (....) x = a + 5;
5. else x = b + 4;
6. c = x + 1;
In this code, at line 3 the initial assignment is useless and x +1 expression can be
simplified as 7.
But it is less obvious that how a compiler can discover these facts by looking only at
one or two consecutive statements. A more global analysis is required so that the
compiler knows the following things at each point in the program:
o Which variables are guaranteed to have constant values
o Which variables will be used before being redefined
Data flow analysis is used to discover this kind of property. The data flow analysis
can be performed on the program's control flow graph (CFG).
The control flow graph of a program is used to determine those parts of a program to
which a particular value assigned to a variable might propagate.
5. Discuss the characteristics of peephole optimization. (Nov/Dec 2018)
This optimization technique works locally on the source code to transform it
into an optimized code. By locally, we mean a small portion of the code block at
hand. These methods can be applied on intermediate codes as well as on target codes.
A bunch of statements is analyzed and are checked for the following possible
optimization:

Redundant instruction elimination


At source code level, the following can be done by the user:
int add_ten(int x)
{
int y, z;
y = 10;
z = x + y;
return z;
}
int add_ten(int x)
{
int y;
y = 10;
y = x + y;
return y;
}
int add_ten(int x)
{
int y = 10;
return x + y;
}
int add_ten(int x)
{
return x + 10;
}
At compilation level, the compiler searches for instructions redundant in nature.
Multiple loading and storing of instructions may carry the same meaning even if some
of them are removed. For example:

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.

Flow of control optimization


There are instances in a code where the program control jumps back and forth without
performing any significant task. These jumps can be removed. Consider the following
chunk of code:

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

Algebraic expression simplification


There are occasions where algebraic expressions can be made simple. For
example, the expression a = a + 0 can be replaced by a itself and the expression a = a
+ 1 can simply be replaced by INC a.

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.

Accessing machine instructions


The target machine can deploy more sophisticated instructions, which can
have the capability to perform specific operations much efficiently. If the target code
can accommodate those instructions directly, that will not only improve the quality of
code, but also yield more efficient results.

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.

Algorithm for construction of DAG


Input:It contains a basic block
Output: It contains the following information:
o Each node contains a label. For leaves, the label is an identifier.
o Each node contains a list of attached identifiers to hold the computed values.
1. Case (i) x:= y OP z
2. Case (ii) x:= OP y
3. Case (iii) x:= y
Method:
Step 1:
 If y operand is undefined then create node(y).
 If z operand is undefined then for case(i) create node(z).
Step 2:
 For case(i), create node(OP) whose right child is node(z) and left child is
node(y).
 For case(ii), check whether there is node(OP) with one child node(y).
 For case(iii), node n will be node(y).
Output:
For node(x) delete x from the list of identifiers. Append x to attached identifiers list for
the node n found in step 2. Finally set node(x) to n.

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

7. Construct DAG for basic block (April/May 2023)


t1=4*i
t2=a[t1]
t3=4*i
t4=b[t3]
t5=t2*t4
t6=prod+t5
prod=t6
t7=i+1
i=t7
if i<=20 goto 1

8. Explain Principal sources of optimization with examples. (Nov/Dec 2021)/ What is


code optimization? State its advantages. Discuss various code optimization schemes in
detail. (Nov/Dec 2020) (April/May 2023)/ Discuss about the following with example:
(Nov/Dec 2020) (Nov/Dec 2023)
i) copy Propagation
ii) Dead-code Elimination and
iii) code motion.

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
transformations can be performed at both the local and global levels. Local
transformations are usually performed first.
There are a number of ways in which a compiler can improve a program without
changing the function it computes.
Function preserving transformations examples:
 Common sub expression elimination
 Copy propagation,
 Dead-code elimination
 Constant folding

The other transformations come up primarily when global optimizations are


performed. Frequently, a program will include several calculations of the offset in an
array. Some of the duplicate calculations cannot be avoided by the programmer because
they lie below the level of detail accessible within the source language.

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. We can avoid recomputing the expression if we can use the
previously computed value.
For example
t1: = 4*i
t2: = a [t1]
t3: = 4*j
t4: = 4*i
t5: = n
t6: = b [t4] +t5

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.

Three techniques are important for loop optimization:


 Code motion, which moves code outside a loop;
 Induction-variable elimination, which we apply to replace variables from inner
loop.
 Reduction in strength, which replaces and expensive operation by a cheaper one,
such as a multiplication by an addition.

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:

while (i <= limit-2) /* statement does not change limit*/

Code motion will result in the equivalent of


t= limit-2;
while (i<=t) /* statement does not change limit or t */

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy