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

Lecture 40

The document discusses code optimization through basic block analysis and control flow graphs. It defines basic blocks as sections of code without jumps in or out except at the entry/exit. A control flow graph represents the flow between basic blocks. Local optimizations like common subexpression elimination can be done by representing a basic block as a directed acyclic graph of operations and variables. This allows reordering and simplifying instructions while preserving the block's behavior.
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)
43 views15 pages

Lecture 40

The document discusses code optimization through basic block analysis and control flow graphs. It defines basic blocks as sections of code without jumps in or out except at the entry/exit. A control flow graph represents the flow between basic blocks. Local optimizations like common subexpression elimination can be done by representing a basic block as a directed acyclic graph of operations and variables. This allows reordering and simplifying instructions while preserving the block's behavior.
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

CODE OPTIMIZATION(II)

Basic Blocks
Consider the code sequence with element size as 8 bytes

for i from 1 to 10 do
for j from 1 to 10 do
a[i,j] = 0.0;
for i from 1 to 10 do
a[i,i] = 1.0;
Corresponding 3AC
1) i = 1 13) t5 = i - 1
2) j = 1 14) t6 = 88 * t5
3) t1 = 10 * i 15) a[t6] = 1.0
4) t2 = t1 + j 16) i = i + 1
5) t3 = 8 * t2 17) if i <= 10 goto (13)
6) t4 = t3 - 88
7) a[t4] = 0.0
8) j = j + 1
9) if j <= 10 goto (3)
10) i = i + 1
11) if i <= 10 goto (2)
12) i = 1
Control Flow Graph (CFG)
• The nodes of the CFG are basic blocks

• One node is distinguished as the initial node

• There is a directed edge B1→B2,

• There is a conditional or unconditional jump from the last


statement of B1 to the first statement of B2, or

• B2 immediately follows B1 in the order of the program, and B1


does not end in an unconditional jump
Corresponding Flow Graph
Exercise: Find Basic Blocks and Flow Graph,
for (i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=0.0;
for (i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
DAG for Basic Blocks
• Local optimization begin by transforming a basic block into a DAG
• We construct a DAG for a basic block as follows,

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

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

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

• Certain nodes are designated output nodes. These are the nodes whose
variables are live on exit from the block.
For Example,
• For the basic block,
Another Example,
Local Optimizations Using DAG
• The DAG representation of a basic block lets us perform several code
improving transformations on the code represented by the block.
• We can eliminate local common subexpressions, that is, instructions that
compute a value that has already been computed.

• We can eliminate dead code, that is, instructions that compute a value
that is never used.

• We can reorder statements that do not depend on one another; such


reordering may reduce the time a temporary value needs to be preserved
in a register.

• We can apply algebraic laws to reorder operands of three-address


instructions, and sometimes thereby simplify the computation.
Local Common Sub-expression Elimination
• Common subexpressions can be detected by noticing, as a new node M
is about to be added, whether there is an existing node N with the same
children, in the same order, and with the same operator.

• For the basic block,


Optimized code can be,
Assuming b is not live at exit of
block, optimized code can be
Another Example,
Another Example,
The corresponding DAG,

You might also like