0% found this document useful (0 votes)
5 views50 pages

Emailing Optimization

The document discusses code optimization techniques, focusing on the representation of intermediate code through basic blocks and flow graphs. It highlights the importance of next-use information, dead code elimination, and common subexpression detection for improving code efficiency. Additionally, it covers advanced optimization strategies like copy propagation, code motion, and the significance of loops in performance enhancement.

Uploaded by

Joydip
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)
5 views50 pages

Emailing Optimization

The document discusses code optimization techniques, focusing on the representation of intermediate code through basic blocks and flow graphs. It highlights the importance of next-use information, dead code elimination, and common subexpression detection for improving code efficiency. Additionally, it covers advanced optimization strategies like copy propagation, code motion, and the significance of loops in performance enhancement.

Uploaded by

Joydip
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/ 50

Code

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.

• Code generation benefits from context.

• We can do a better job of register allocation if we know how


variables are defined and used.
Basic Blocks & Flow graphs
Basic Blocks
• We begin a new basic block with the first instruction

• Keep adding instructions


• until we meet either a jump, a conditional jump,
• or a label on the following instruction.

• In the absence of jumps and labels, control proceeds sequentially


from one instruction to the next.

• Task: Identify leaders, that is, the first instructions in some basic
block.
Basic Blocks - Leaders
Basic Blocks

leaders are instructions


1, 2, 3, 10, 12, and 13
Basic Blocks
Flow Graphs
• We represent the flow of control by a flow graph.
• The nodes of the flow graph are the basic blocks.
• There is an edge from block B to block C if and only if
• it is possible for the first instruction in block C to
immediately follow the last instruction in block B.
There are two ways that such an edge could be justified:
• There is a conditional or unconditional jump from the end of B
to the beginning of C.
• Block C immediately follows Block B in the original order of the
three-address instructions
• B does not end in an unconditional jump
• Maybe due to labels

We say that B is a predecessor of C, and C is a successor of B.


Flow Graphs

• Often we add two nodes, called the entry and exit,

• There is an edge from the entry to the first executable node


of the flow graph,
• that is, to the basic block that comes from the first
instruction of the intermediate code.

• 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.
Flow Graphs
Loops
Many code transformations depend upon the
identification of "loops" in a flow graph. We say that a set
of nodes L in a flow graph is a loop if
Loops
Flow Graphs
Next-Use Information
• Knowing when the value of a variable will be used next is essential for
generating good code.
• If the value of a variable that is currently in a register will never be
referenced subsequently, then that register can be re-assigned to
another variable.

• Suppose three-address statement i assigns a value to x.


• If statement j has x as an operand, and control can flow from statement i to j
along a path that has no intervening assignments to x, then we say statement
j uses the value of x computed at statement i.
• We further say that x is live at statement i.

• We wish to determine for each three-address statement x = y + z what the


next uses of x, y, and z are.
• We store the information in the symbol table.

• Focus on the basic block containing this three-address statement.

• The algorithm to determine liveness and next-use information makes a


backward pass over each basic block.
Next-Use Information
DAG Representation of Expression
DAG Representation of Expression
Application of SDD – Syntax tree
construction

If the rules are evaluated during a


postorder traversal of the parse tree,
or
with reductions during a bottom-up
parse, then the sequence of steps
DAG Representation of Expression
DAG Representation of Basic Blocks: Value Number Method

• The nodes of a syntax tree or DAG are stored in an array of records

• 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

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.
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 b is not live on exit from the block, then we do not need to


compute b variable, and can use d to receive the value
• If both b and d are live on exit, then a fourth statement must be
used to copy the value from one to the other
Dead Code Elimination

If a and b are live but c and e are not, we can immediately remove the
root labeled e.

Then, the node labeled c becomes a root and can be removed.

The roots labeled a and b remain, since they each have live variables
attached
The Use of Algebraic Identities
The Use of Algebraic Identities

Associative laws might also be applicable to expose common


subexpressions
Representation of Array References

???

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

• For each node that has one or more attached variables,


• we construct a three-address statement that computes the value of one
of those variables.

• We prefer to compute the result into a variable that is live on exit from the block.

• However, if we do not have global live-variable information to work from, we


need to assume that every variable of the program (but not temporaries that are
generated by the compiler to process expressions) is live on exit from the block.
Reassembling Basic Blocks From DAG 's

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 b is not live on exit from the block, then we do not need to


compute b variable, and can use d to receive the value
• If both b and d are live on exit, then a fourth statement must be
used to copy the value from one to the other
Reassembling Basic Blocks From DAG 's
Peephole Optimization
• Improve the quality of the target code by applying "optimizing"
transformations to the target program

• Peephole optimization 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,
Eliminating Redundant Loads and Stores

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

Remove this, if no other jump at L1


• While the number of instructions in
the two sequences is the same,
• we sometimes skip the
unconditional jump in the
second sequence,
• but never in the first
sequence .
Optimization – Beyond basic blocks
• Notice that every array access in the original program translates into a pair of
steps, consisting of a multiplication and an array subscription operation.
• Short program fragment translates into a rather long sequence of three-address
operations.
Entry
node
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.
• We avoid recomputing E if we can use its previously
computed value

Compute the common subexpressions 4 * i and 4 * j, respectively


Global Common Subexpressions

• Control passes from the evaluation of 4 * j


in B3 to B5,
• No change to j and no change to t4, so t4
can be used if 4 * j is needed.
Global Common Subexpressions

a[t4], a value computed into a temporary


t5, retains its value as control leaves B3
and then enters B5
Optimize:
Homework
Copy Propagation

We must use a new variable t to hold the value of d + e


After the copy statement u = v, use v for u
Dead-Code Elimination

Copy propagation

• Drop this code segment


• Constant folding
Code Motion
• Loops are a very important place for optimizations, especially the inner
loops where programs tend to spend the bulk of their time.
• The running time of a program may be improved if we decrease the
number of instructions in an inner loop,
• even if we increase the amount of code outside that loop.
• Code Motion takes an expression
• That yields the same result independent of the number of
times a loop is executed (a loop-invariant computation) and
• Evaluates the expression before the loop
Induction variable and reduction in
strength
Induction variable and reduction in
strength

You might also like