Emailing Optimization
Emailing Optimization
Optimization
Optimized
Intermediate
code
Optimized
Target code
Basic Blocks & Flow graphs
• Introduce a graph representation of intermediate code that is
helpful for discussing code generation
• Even if the graph is not constructed explicitly by a code-
generation algorithm.
• Task: Identify leaders, that is, the first instructions in some basic
block.
Basic Blocks - Leaders
Basic Blocks
• In this array, we refer to nodes by giving the integer index of the record for
that node within the array.
• Each row of the array represents one record, and therefore one node.
• In each record, the first field is an operation code, indicating the label of the
node.
Optimization of Basic Blocks
DAG Representation of Basic Blocks
Finding Local Common Subexpressions
Since there are only three nonleaf nodes in the DAG, the basic block in
can be replaced by a block with only three statements.
If a and b are live but c and e are not, we can immediately remove the
root labeled e.
The roots labeled a and b remain, since they each have live variables
attached
The Use of Algebraic Identities
The Use of Algebraic Identities
???
v
Representation of Array References
Reassembling Basic Blocks From DAG 's
• After we perform whatever optimizations are possible while constructing the
DAG or by manipulating the DAG once constructed,
• we may reconstitute the three-address code for the basic block from
which we built the DAG
• We prefer to compute the result into a variable that is live on exit from the block.
Since there are only three nonleaf nodes in the DAG, the basic block in
can be replaced by a block with only three statements.
• Redundant loads and stores of this nature would not be generated by the
simple code generation algorithm
However, a naive code generation algorithm would generate redundant
sequences
• Note that if the store instruction had a label, we could not be sure that
the first instruction is always executed before the second, so we
could not remove the store instruction.
Instruction Selection – Example
Redundant
Eliminating Unreachable Code
An unlabeled instruction immediately following an unconditional jump
may be removed.
One obvious peephole optimization is to eliminate jumps over jumps
After optimization
Flow-of-Control Optimizations
• Simple intermediate code-generation algorithms frequently
produce
• jumps to jumps, jumps to conditional jumps, or
conditional jumps to jumps.
Copy propagation