0% found this document useful (0 votes)
83 views

Code Optimization Questions For Discussion

The document discusses code generation and optimization techniques for the C code segment shown. It contains: 1) Two nested for loops with loop invariant computations that could be hoisted out of the loops. 2) Common subexpressions that offer opportunities for common subexpression elimination. 3) Strength reduction possibilities from reordering computations. 4) No dead code that could be eliminated.

Uploaded by

Sohail Akram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Code Optimization Questions For Discussion

The document discusses code generation and optimization techniques for the C code segment shown. It contains: 1) Two nested for loops with loop invariant computations that could be hoisted out of the loops. 2) Common subexpressions that offer opportunities for common subexpression elimination. 3) Strength reduction possibilities from reordering computations. 4) No dead code that could be eliminated.

Uploaded by

Sohail Akram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Code Generation and

Optimization
the following C code segmer
Conside

i ++)
0:1<n;
for (i=

for= 0:j<n:j ++)


2
i f(i 2 )

x t (4 *j+5 * i);
y+ (7 +4 *j); (a)
4
3

which one of the following is false? 3

(a)The code contains loop invariant computation


b)There is scope of common
elimination in this code
sub-expression
cThere is scope strength reduction in this code
dThere is scope of dead code elimination in this
COde

[2006:2 Marks]
42 Some code optimizations are
carried out on the (
intermediate code because
a) They enhance the portability of the
to other complier
target processors
(6) Program analysis is more
accurate on
intermediate code than on machine code
) The information from data
cannot otherwise be used for
flow analysis
4.5
2011:2 Marks]
d) The information from the optimization
Consider the program
given below, in a
otherwise be used for
front end cannot structured pseudo-language with lexical block-
optimization and nesting of
procedures permitted. scoping
2008:1Mark] Program main;
A3 Which languages necessarily need Var..
allocation heap Procedure Al;
in the runtime
(a) Those that environment! Var
support
6) Those that use recursion Call A2;
) Those that allow
dynamic scoping End A1
a) Those that use dynamic data structure Procedure A2;
global variables Var.
Procedure A21;
44 Consider [2010:1Mark]
Var...
two binary operators T and with Call A1,
ne precedence of operator being lower than End A21
hat of the
operator 1. Operator T is right Call A21;
SsOCiative while operator is left associative. End A2
Which one of the Call A1;
ree for
following represents the parse
End main,
expression (731413'2)
524
GATE Previous Years
Solved Papers:
d=d* d;
SMADE
Consider the calling chain:
e =e* e
Main A1->A2-> A21 > A1
activation records along
with
The correet set of
their access links is given by 4.6 Suppose the instruction set
(a)
Mnin processor has only two registers architecture of
complier optimization is codeThemo only th
Al
moves statements from one allowod
A2
place to anoth hich
preserving correctness. What is # hil
A21 number of spills to memory in the
a) 0
cnmniled code?
1 1
FRAME
ACCESS
LINKS
(2 3
POINTER

(b) Main

What is the minimum number


2013:2 Marks
Al
4.7 of registon
needed in the instruction set
A2 architecture oft.
of the
processor to compile this code segment with
any spill to memory? Do not thout
A21
optimization other than
apply ay
A1 optimizin register
FRAME ACCESS allocation?
POINTER LINKS
a) 3 (b)4
Main 5
(C) d) 6

FRAME
Al
[2013:2 Marks
POINTER A2 4.8 Which one of the following is FALSE?
(a) A basic block is a sequence of instructions
A21 ACCESS where control enters the sequence at the
LINKS
beginning and exits at the end.
Main (b) Available expression analysis can be usedfor
common subexpression elimination.
(c) Live variable analysis can be used for dead
code elimination.
A21
(d)x = 4x5 » 20 is an example of common
Al subexpression elimination.
FRAME ACCE SS
POINTER LINKS [2014 (Set-1):1Mark
[2012:2Marks]
4.9 Which one of the following is NOT performed
4.6 and 4.7
Common Data for Questions during compilation?
code segment is executed on a processor allocation
The following (a) Dynamic memory
in its instructions.
which allows only register operands b) Type checking
almost two source operands
Each instruction can have (c) Symbol table management
one destinations operand.
Assume that all
and (d) Inline expansion
variables are dead after this code segment. [2014 (Set-2):1Mark
c=a+b; ne
d=e * a 410 For a C accessing X[]5||k),
program
by a
code is generated
ecta folowing intermediate integer 13
x=c * c;
compiler. Assume that
the size of an
of a character
is 8 bits.
if(x> a){ 32 bits and the size

ya*a t 0 =i* 1024


t1=j*32
else t2 k *4
Compiler Design
EASY 525

1,3=11 + to

14=13 + t12
The variables which are live both at the statement
in basic block 2 and at the statement in basic
h=X}t4)
of the following statements about the block 3 of the above control flow graph are
whede for the C program is CORRECT (a) p, s,u (b) r, s,u
declared as "int X[32]|32][8". (c)r,u (d) q, v
"nt X|4][1024]|[32.
Nis declared as
[2015 (Set-1): 2 Marks]
Xis declared as "char X[4][32][8r.
"char 4.14 The least number of
(d) Xis
declared a s X[32][16][2 temporary variables required
[2014 (Set-2): 2 Marks]
tO Create a three-address code in static single
aL.ioh of the following statements are CORRECT? assignment form for the expressionqtr/3+s-t
*
l l Whiet

(1) Static allocation of all data areas by a 5tu *v/w is.


compiler makes it ossible to
implement [2015 (Set-1): 2 Marks]
recursion 4.15 Inthe context of
Atomatic garbage collection is essential to abstract-syntax-tree (AST) and
implement recursion. control-flow-graph (CFG), which one of the
allocation of following is True?
(3)
Dynamic
activation records is a) In both AST and
essential to implement recursion. CFG, let node
successor N, be the
dBoth heap and stack are
essential of node N,. In the input program,
to the code
implement recursion.
the code
corresponding to N2 is present after
(a) 1 and 2 only b) 2 and 3
only (6) For any corresponding to NN
(c) 3 and 4 only
) 1 and 3
only input program, neither AST nor
CFG will contain a
[2014 (Set-3):1 Mark] (c) The maximum cycle
412 Consider the basic block given below. number of successors of a
node in an AST and
a = b+ c, c=a+ d, d = b+ c a CFG
input program depends on the
e=d - b, a = e +b d) Each node in AST and
The minimum number CFG
of nodes and
edges
at most one
statement in the
corresponds to
present in the DAG input programa
basic block representation of the above
respectively are [2015 (Set-2) : 1 Mark]
(a) 6 and 6 4.16 Consider the
(b) 8 and 10 intermediate code given below:
c)9 and 12 d) 4 and 4 1. i=1
[2014 (Set-3) : 2 Marks] 2. j= 1
413 A variable x is 3. t1= 5 * i
said to be live at
a
program if the
statement S, in
a
4. t2
following three conditions hold t1 +j
simultaneously
. There
5. t3= 4 * t2

exists a statement that 6. t4 t3


2. There is a path from to S; uses x

S, S, in the flow graph 7. a[t4=-1


3,
corresponding to the
program 8. j=j+1
The path has no intervening
assignment to
9. ifj<=5 goto (3)
x
including at S, and S
10. i = i +1
Pq+r 11. if i < 5 goto (2)
P*9
The number of nodes and edges in the
control.
flow-graph constructed or the above
respectively, are code,
D=r+u2 (a) 5 and 7 b) 6 and 7
(c) 5 and 5 (d) 7 and 8
2015 (Set-2) : 2
Marks|
526 GATE Previous Years Solved Papers: o

MADE EAS
4.17 Consider the following code segment: 4.20 Consider the expression (a-1
Let X be the
equired by an nimum
optimal codenumber ot
any register spill) generat on r(vithot
egistora
architecture, in which
dh
algorithm for: a
EI- z;
)only load loadls
instructions have memory
can
and
The minimum number of total variables required arithmetic instructions haveoperands
can
atore
and fi
to convert the above code segment to static single
or immediate operands. The ont
val"sta
assignment form is Xs
2016 (Set-1):1 Mark] 2017 (Set-1):2 Markal
4.21 Consider the following
4.18 A student wrote two context-free grammars G1 grammar
and G2 for generating a single C-like array stmt if expr then expr
else expr,
declaration. The dimension of the array is at least expr term relop
term |term stmt o
term> id|number
one. For example.
id > alb|le
int a [10] [3];
number [0-9]
The grammars use D as the start symbol, and
where relop is a relational
use six terminal symbols int; id [] num. operator (e.g s.
..., 0 refers to the empty
Grammar G1 Grammar G2
then, else are terminals.
statement, and
And
D int L; D int L
L idE
Consider a
program P following the aboe
L i d [E
grammar containing ten if terminals.
E num] E E [num] number of control flow paths in Pis he
E> num] [E E [num For example, the program
Which of the grammars correctly generate the if e, then e, else e
declaration mentioned above?
has 2 control flow paths, e -e and
(a) Both G1 and G2 () Only G1 e e
() Only G2 d) Neither G1 nor G2 [2017 (Set-1):2 Marks
[2016 (Set-2):2 Marks]l 4.22 Consider the following C code segment:

4.19 Consider the following intermediate program in a =btc,


three address code e atl1;
P a- b d b+c;
9 p *e f=d+1;
p =u *v 8e+f
In a compiler, this code segment is represented
The
Which one of the following corresponds to a internally as a directed acyclic graph (DAG).
static single assignment form of the above code? number of nodes in the DAG is
(a) p, = a - b b 2021 (Set-1) :2Marks
(b) P a -

4 P *c 44 P3 *c the followng
4.23 Inthe context of compilers, which of
P u *v P u is/are NOTan intermediate representationoue
91 Pt9 95Pt 94 source program?
( P a -b (d)P, a - b (a) Symbol table
41 Pa *c 9p *c (b) Three address code
P3u * v 9 2 u *U ()Control Flow Graph (CFG)
92P4t 93 d) Abstract Syntax Tree (AST) Mark)
:1
[2017 (Set-1):1 Mark] [2021 (Set-2)
puoE
EASY /
| Compile Design 527

tatement in
program, in the context
a

tiveness analysis, thefollowing sets are defined: 4.25 Consider the following ANSI C code segment:

UISES): The set ofvariables used in Ss 2=x+3+y->f1 +y f2;


for (i = 0; i< 200; i= i +2){
The set of variables that are live at the
entryofS if(a>9{
UTYS): The set of variables that are live at the p p +x +3;
exito f ' S qqtyf1;
Consider a basic block that consists of two else{
statements, S, followed by S p=p+y>f2;
following statements is correct? qtx +3;
Which o n e of
the
(a OUTS,) = USE(S,) U IN(S,)

b) OUTYS)=IN(S,) u OUT(S,)
OUTS)=IN(S,) Assume that the variable ypoints to a struct
OUTS) =IN(S)UUSE(S,) (allocated on the heap) containing two fields f1
and f2, and the local variables x,
[2021 (Set-2) :2 y, z, p, q, andi
Marks] are allotted registers. Common sub-expression
elimination (CSE) optimization is applied on the
code. The number of addition and
dereference
operations (of the form y >f1 or y > f2) in the
optimized code, respectively, are
a) 303 and 2 (6 403 and 102
203 and2 (d) 308 and 102
[2021 (Set-2) :2 Marks]

You might also like