Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views41 pages

Cdunit 5

Uploaded by

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

Cdunit 5

Uploaded by

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

UNIT - V

Runtime environment:
Symbol table format,
organization of block structure languages, hashing
tree structures representation of scope information
Block structures and non-block structures storage allocation
static, runtime stack and heap storage allocations
storage allocation for arrays, strings and records.

Code Optimization:
Optimization
scope of optimization
common sub expression elimination
frequency reduction
strength reduction
loop optimization
peep-hole optimization
copy propagation.
Code Optimization

• Elimination of unnecessary instructions in object code


• The replacement of one sequence of instructions by a faster sequence of
instructions that does the same thing
• Also called as “code improvement”.

The Principal Sources of Optimization

Causes of Redundancy

• Redundancy may available at the source level


• Redundancy is a Side effect of having written the program in high level
language
• From a software-engineering perspective that programmers only access
data elements by their high-level names
- the programs are easier to write, easier to understand and evolve
A Running Example : Quick Sort
Semantics-Preserving Transformations

• There are a number of ways in which a compiler can improve a program


without changing the function it computes
• Also called as Function Preserving Transformations
• Some of the transformation techniques are
• Common- sub expression elimination
• Copy propagation
• Dead code elimination
• Constant folding
• Many of the duplicate calculations cannot be avoided by the programmer
because they lie below the level of detail accessible within the source
language.
Global Common Subexpressions

• An occurrence of an expression E is called a common subexpression :


- If E was previously computed and the values of the variables in E have not changed
since the previous computation
•Avoid recomputing E and use its previously computed value.
Copy Propagation

• Assignments of the form u = v called copy statements, or copies.

• The idea behind the copy propagation is to use v for u, when ever possible after the copy
statement u = v.
• This change may not be an improvement but gives the opportunity to eliminate some
assignments.
Dead Code Elimination

• A variable is live at a point in a program if its values can be used


subsequently
-otherwise, it is dead at that point
•Dead( or useless) code compute values that never get used.
•May appear as a result of previous transformations.
•Ex : if(debug) print ………..
debug = false

•Copy propagation often turns the copy statements into dead code.
Constant folding

• Evaluation of an expression with constant operands to replace the


expression with single value
• Example:

area := (22.0/7.0) * r ** 2

area := 3.14286 * r ** 2
Code Motion
• Reduce frequency with which computation performed
• If it will always produce same result
• Especially moving code out of loop
• Loop invariant computation

Ex : while( I <= limit – 2) /* statement does not change limit */


Code motion will result in the equivalent code

t = limit – 2
while( I <= t) /* statement does not change limit or t */

for (i = 0; i < n; i++) {


int ni = n*i;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (j = 0; j < n; j++)
a[ni + j] = b[j];
a[n*i + j] = b[j];
}
Induction Variable and Reduction in Strength

• Another important optimization is to find induction variables in loops and


optimize their computation.
• A variable x is said to be an "induction variable“ if there is a positive or
negative constant c such that each time x is assigned, its value increases
by c.
• Induction variables can be computed with a single increment (addition or
subtraction) per loop iteration.
• The transformation of replacing an expensive operation, such as
multiplication, by a cheaper one, such as addition, is known as strength
reduction
Optimization of basic blocks

• The intermediate code is represented in the form of a graph.


• The representation is constructed as follows :
• Partition the intermediate code into basic blocks, with the properties
that
• The flow of control can only enter the basic block through the first
instruction in the block
• Control will leave the block at the last instruction of the block.
• The basic blocks become the nodes of a flow graph, whose edges
indicate which blocks can follow which other blocks
Basic Blocks

• Begin a new basic block with the first instruction and keep adding
instructions until either a jump, a conditional jump or a label on the
following instruction is encountered
• In the absence of jumps and labels, control proceeds sequentially from one
Basic Blocks Construction
Example

Source code Intermediate code

leader
leader
leader

leader

leader
leader
Flow Graphs

• The nodes in the flow graph are the basic blocks


• There is an edge from block B to block C iff:
• There is a conditional or unconditional jump from the end of B to the
beginning of C.
• C immediately follows B in the original order of the three address
instructions, and B does not end in an unconditional jump.
• B is a predecessor of C, and C is a successor of B.
•Two nodes entry and exit are added that do not correspond to the code
• There is an edge from entry to the executable node of the flow graph
• There is an edge to the exit from any basic block that contains an
instruction that could be the last executed instruction of the program.
Loops

• Many code transformations depend upon the identification of “loops” in a


flow graph.
• A set of nodes L in a flow graph is a loop if L contains a node e called the

loop entry, such that


1. e is not ENTRY, the entry of the entire flow graph
2. No node in L besides e has a predecessor outside L. That is, every
path from ENTRY to any node in L goes through e.
3. Every node in L has a nonempty path, completely with in L, to e.
Exercise :

Translate the following matrix multiplication algorithm into three


address code, construct the flow graph of the code and identify the
loops in the flow graph.
DAG Representation of Basic Blocks

• Construct a DAG for a basic block

1. There is a node in the DAG for each of the initial values of the
variables appearing in the basic block.

2. There is a node N associated with each statement s within the block.


The children of N are those nodes corresponding to statements that
are the last definitions, prior to s, of the operands used by s.

3. Node N is labeled by the operator applied at s, and also attached to N


is the list of variables for which it is the last definition within the block.

4. Certain nodes are designated output nodes. These are the nodes
whose variables are live on exit from the block; that is, their values
may be used later, in another block of the flow graph.

22
Finding Local Common Sub expressions

23
DAG Representation of Basic Blocks

• The DAG representation of a basic block lets us perform

– eliminating local common sub-expressions

– eliminating dead code

– reordering statements that do not depend on one another

– applying algebraic laws to reorder operands of three-address


instructions

24
Dead Code Elimination

• Delete from a DAG any root (node with no ancestors) that has no live
variables attached.
• Repeated application of this transformation will remove all nodes from
the DAG that correspond to dead code.

• Example: assume a and b are live but c and e are not.

– e , and then c can be deleted.

25
Representation of Array References

• x = a[i]
• a[j]=y
• Z = a[i]
• killed node

26
Representation of Array References

a is an array. b is a position in the array a.

x is killed by b[j]=y.

CS308 Compiler Theory 27


The Use of Algebraic Identities

• Eliminate computations

• Reduction in strength

• Constant folding
• 2*3.14 = 6.28 evaluated at compile time
• Other algebraic transformations
– x*y=y*x

– x>y and x-y>0


– a= b+c; e=c+d+b; e=a+d;
28
Pointer Assignments and Procedure Calls
• Problem of the following assignments

x = *P *q = y
– we do not know what p or q point to.

– x = *p is a use of every variable

– *q = y is a possible assignment to every variable.

– the operator =* must take all nodes that are currently associated with
identifiers as arguments, which is relevant for dead-code elimination.
– the *= operator kills all other nodes so far constructed in the DAG.

– Global pointer analysis can be used to limit the set of variables

• Procedure calls behave much like assignments through pointers.


– Assume that a procedure uses and changes any data to which it has
access.
– If variable x is in the scope of a procedure P, a call to P both uses the
29
node with attached variable x and kills that node.
Reassembling Basic Blocks From DAG 's

b is not live on
exit

b is live on exit

30
Reassembling Basic Blocks From DAG 's

• The rules of reassembling


– The order of instructions must respect the order of nodes in the DAG

– Assignments to an array must follow all previous assignments to, or


evaluations from, the same array
– Evaluations of array elements must follow any previous assignments to
the same array
– Any use of a variable must follow all previous procedure calls or
indirect assignments through a pointer.
– Any procedure call or indirect assignment through a pointer must
follow all previous evaluations of any variable.

31
Exercise: construct the DAG for the following basic block and simplify by assuming:
(a)Only a is live on exit from the block
(b)a,b,c are live on exit form the block
Peephole Optimization block_3:
block_3:
mov
mov[esp-4],ebp
[esp-4],ebp
mov
movebp,esp
ebp,esp
mov [ebp-8],esp
mov [ebp-8],esp
sub
subesp,28
esp,28
mov
moveax,[ebp+8]
eax,[ebp+8]
• Simple Idea cmp
cmpeax,0
eax,0
mov
moveax,0
eax,0
sete
seteahah
– Slide a window over the code cmp
cmpeax,0
eax,0
jzjz block_5
block_5
block_4:
– Optimize code in the window only. block_4:
mov
moveax,1
eax,1
jmp block_6
jmp block_6
• Optimizations are block_5:
block_5:
mov
moveax,[ebp+8]
eax,[ebp+8]
sub
subeax,1
eax,1
– Machine dependent push
pusheaxeax
mov
moveax,[ebp+4]
eax,[ebp+4]
push
pusheaxeax
– Semantic preserving mov
moveax,[eax]
eax,[eax]
mov
moveax,[eax]
eax,[eax]
call
calleax
– Cheap to implement add
eax
addesp,8
esp,8
mov
movebx,[ebp+8]
ebx,[ebp+8]
imul
imulebx,eax
• Usually mov
ebx,eax
moveax,ebx
eax,ebx
block_6:
block_6:
– One can repeat the peephole several times! mov
movesp,[ebp-8]
mov
esp,[ebp-8]
movebp,[ebp-4]
ebp,[ebp-4]
retret
– Each pass can create new opportunities for more
Peephole Optimizations
• A Few Simple techniques
– Load/Store elimination

• Get rid of redundant operations

– Unreachable code

• Get rid of code guaranteed to never execute

– Flow of Control Optimization

• Simply jump sequences.

– Algebraic simplification

• Use rules of algebra to rewrite some basic operation

– Strength Reduction

• Replace expensive instructions by equivalent ones

– Machine Idioms

• Replace expensive instructions by equivalent ones (for a given


machine)
Eliminating redundant loads and stores

• Consider the code sequence

LD R0, a

ST a, R0

• Instruction 2 can always be removed if it does not have a label.


Eliminating Unreachable Code this may be translated as

if debug = 1 goto L1
• Consider following code sequence
goto L2
#define debug 0
L1: print debugging info
if (debug) { L2:
print debugging info Eliminate jump over jumps
if debug != 1 goto L2
} print debugging information
L2:
• Flow of control optimizations:

replace jump sequences


goto L1 goto L2
…… ……
L1 : goto L2
L1: goto L2

if a < b goto L1
if a < b goto L2
………….
………………………
L1: goto L2 L1: goto L2

goto L1 if a < b goto L2


……. goto L3
L1: if a < b goto L2 . . . . .. . .
goto L3 L3:
L3:
• Simplify algebraic expressions and reduction in strength

remove x = x+0 or x=x*1


Use of Machine Idioms

• Replace expensive instructions by machine supported instructions


• Equivalent instruction that are optimized for the platform

replace Add #1,R by Inc R


Assignment - 5

1. Explain in detail with examples about principal sources of optimization.


2. Explain the methods of optimizing basic blocks.
3. What is DAG? Explain with example how a basic block is represented
using DAG.
4. (a) What is an activation record? Explain its possible strucute.
(b) Explain the design goals for garbage collector.
(c) Explain the desirable properties of memory manager.
5. Generate the intermediate code for the statement sum=A[i,j]+B[i,j].
construct DAG and simplify the code.
6. How a basic block is represented using DAG? Explain with example.
7. Explain in detail with example how a flow graph is constructed for the
given three address code.
8. Explain about peep hole optimization with example.

You might also like