05.10.2013 Views

1 Code Generation Code generator phase ... - VTU e-Learning

1 Code Generation Code generator phase ... - VTU e-Learning

1 Code Generation Code generator phase ... - VTU e-Learning

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Each instruction takes some machine cycles to perform the operation. The optimization strategies<br />

should be such that it should reduce the number of machine cycles or in other words the strength of<br />

instruction should be reduced to have better optimization.<br />

Example:<br />

x 2 can be replaced by expression x * x.<br />

Expressions like adding 0 or multiplying by 1 can be removed, as these do not change the value of<br />

variable.<br />

Example:<br />

1) x = x + 0<br />

2) a = a * 1<br />

These instructions can be eliminated as they do not change the value of x and a.<br />

This is called algebraic transformation.<br />

Data Structure:<br />

Syntax trees can be used for some of the optimization techniques like constant folding, constant<br />

propagation etc., but for optimization like eliminating loop invariant, or dead code elimination, it is<br />

not very efficient, Specially for global optimization syntax tree is not efficient as it requires the study<br />

for control flow. Hence flow graphs are used. Flow graphs consist of basic blocks as nodes and edges<br />

connecting basic blocks indicate the control flow. The sequence of three address statement is<br />

converted to flow graph using following steps.<br />

1. Construct basic block<br />

2. Generate flow graph<br />

1. Construction of Basic Blocks<br />

a) Determine set of header statements. Header statements are the first statement of each basic<br />

block.<br />

b) First statement is a header statement<br />

c) Any statement which is target of conditional or unconditional jump is a header statement.<br />

d) Any statement following conditional or unconditional jump is a header statement.<br />

2. Construct flow graph<br />

Construct graph with B1 as the stating node where B1 is basic block which has first statement of the<br />

program. Generate edge from Bi to Bj if control flows from block Bi to Bj. Entry for any block Bk<br />

will be from the first statement of Bk and exit from Bk will be from last statement only. No<br />

intermediate jump or return can happen in the basic block.<br />

Example: Consider the following C statement<br />

for i = 1 to n do<br />

13

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!