Top Banner
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination
18

CS 201 Compiler Construction

Jan 25, 2016

Download

Documents

maegan

CS 201 Compiler Construction. Lecture 7 Code Optimizations: Partial Redundancy Elimination. Redundancy Elimination. The goal of redundancy elimination is to eliminate unnecessary reevaluations of the same computation. Global Common Sub-expression Elimination Loop Invariant Code Motion - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 201 Compiler Construction

1

CS 201Compiler Construction

Lecture 7Code Optimizations:

Partial Redundancy Elimination

Page 2: CS 201 Compiler Construction

Redundancy Elimination

The goal of redundancy elimination is to eliminate unnecessary reevaluations of the same computation.

• Global Common Sub-expression Elimination

• Loop Invariant Code Motion• Partial Redundancy Elimination – this is

the most general and thus it subsumes the above two optimizations.

2

Page 3: CS 201 Compiler Construction

3

Global Common Sub-expression Elimination

Analysis Required1. Available Expressions2. Reaching Definitions

Page 4: CS 201 Compiler Construction

Transformation Algorithm

For each statement S: A = B op C st B op C is available at entry of S’s basic block and neither B or C are redefined prior to S do the following:

1.Find definitions that reach S’s block that have B op C on the right hand side.

2.Create a new name T.3.Replace each statement D = B op C

found in step 1 by: T = B op C; D = T;4.Replace statement S by A = T.

4

Page 5: CS 201 Compiler Construction

Loop Invariant Code Motion

Loop Invariant: A statement that performs the same computation over and over again during multiple iterations of a loop’s execution.To detect invariant statements in a loop L:1.Mark invariant statements whose operands are either constants or variables with ALL reaching definitions outside the loop.2.Mark invariant statements whose operands satisfy conditions in step 1 or have exactly one reaching definition that is from inside the loop and has already been marked invariant.3.Repeat step 2 till no more invariants are identified.

5

Page 6: CS 201 Compiler Construction

Transformation

For each statement S (A=B, A=op B, A=B op C) that is marked invariant wrt L, check the following:1.S is in a block that dominates all exits of L;2.A is not defined elsewhere in L; and.3.All uses of A in L can only be reached by the definition of A in S.

Move in the order identified as invariant, each invariant statement S found to satisfy the above conditions to a newly created pre-header.

6

Page 7: CS 201 Compiler Construction

Why Conditions are Needed?

7

Page 8: CS 201 Compiler Construction

Limitations of…

8

X+Y is notAvailable here

Global Common Sub-expression Elim.

Loop Invariant Code Motion

Page 9: CS 201 Compiler Construction

Partial Redundancy Elimination

9Use code motion Move expressions

Page 10: CS 201 Compiler Construction

Algorithm for PRE

Before performing PRE, split critical edges to create suitable placement points.

10

Page 11: CS 201 Compiler Construction

Algorithm for PRE

Key Idea: Move expression evaluations back through the control flow graph and place them at the earliest possible points in the control flow graph. This process eliminates full and partial redundancy.

11

Page 12: CS 201 Compiler Construction

Algorithm for PRE

12

Page 13: CS 201 Compiler Construction

Algorithm Steps

1. Down-Safety Analysis backward analysis that determines to where all expressions can be “safely” moved.

2. Earliestness Analysis forward analysis that determines the earliest points to where an expression can be safely moved.

3. Perform transformation by placing expression evaluations at earliest points and eliminating them from other points.

13

Page 14: CS 201 Compiler Construction

Down-Safety Analysis

• Used(n) is tre if n evaluates the expression.

• Transp(n) is true if n does not modify any operands of the expression (i.e., n is transparent, it does not kill the expression) 14

Page 15: CS 201 Compiler Construction

Earliestness Analysis

• Forward analysis that finds the earliest nodes that are down-safe. A node n is an earliest node if there is a path from start node to n such that along this path n is the first node that is down-safe for the current value of the expression.

15

Page 16: CS 201 Compiler Construction

Transformation Step

1. Introduce a new auxiliary variable h for expression exp.

2. Insert at the entry of every node n for which D-Safe(n) ∧ Earliest(n) is true, the assignment: h = exp.

3. Replace every original computation of exp by h.

16

Page 17: CS 201 Compiler Construction

Example

17

Page 18: CS 201 Compiler Construction

Optional Step

• Suppressing unnecessary code motion using Delayability Analaysis - reduces register usage.

18