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

Optimization of Basic Block

The document discusses how representing a basic block as a directed acyclic graph (DAG) allows for several code optimizations, including eliminating common subexpressions, dead code, and reordering statements. It provides examples of detecting and removing common subexpressions, eliminating dead variables, using algebraic identities for reduction, and interchanging statements. The DAG representation also enables automatic detection of these optimizations. Peephole optimization is mentioned as another local technique that examines a window of instructions to replace sequences with improved ones.

Uploaded by

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

Optimization of Basic Block

The document discusses how representing a basic block as a directed acyclic graph (DAG) allows for several code optimizations, including eliminating common subexpressions, dead code, and reordering statements. It provides examples of detecting and removing common subexpressions, eliminating dead variables, using algebraic identities for reduction, and interchanging statements. The DAG representation also enables automatic detection of these optimizations. Peephole optimization is mentioned as another local technique that examines a window of instructions to replace sequences with improved ones.

Uploaded by

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

Optimization of Basic Block

DAG Representation of Basic Block


• We can obtain a substantial improvement in
the running time of code merely by
performing local optimization within each
basic block by itself.
• Many important techniques for local
optimization begin by transforming a basic
block into a DAG (directed acyclic graph).
DAG Representation of Basic Block
• 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 sub expressions, 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.
Finding local common sub expression
• 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. If so, N computes
the same value as M and may be used in its place.
Dead code elimination
• Eliminate variables which are not in use
• Suppose x is dead, i.e. never subsequently
used, at the point where the statement x = y+z
appears in a basic block. Then this statement
may be safely removed.
Use of Algebraic identities
• Algebraic Identities

• Local reduction in strength

• Constant folding
– 2*pi can be replaced with 6.28 (2*3.14)
Interchange of statements
• Suppose we have a block with two adjacent
statements
– a = b+ c + d
– e = b+c
• We can interchange them as
– e = b+c
–a=e+d
• Hence less registers will be required.
Applications of DAG
• Common sub expression can be detected
automatically.
• Identifiers whose values are used in block can
be determined
• Determine which statements compute values
that could be used outside block .
Peephole Optimization
• A simple but effective technique for locally improving
the target code is peephole optimization, which is
done by examining a sliding window of target
instructions (called the peephole) and replacing
instruction sequences within the peephole by a
shorter or faster sequence, whenever possible.
• Peephole optimization can also be applied directly
after intermediate code generation to improve the
intermediate representation.
• The peephole is a small, sliding window on a program.

You might also like