Docsity

CS 553 Compiler Construction Assignment 3: Common Subexpression Elimination - Prof. Michel, Assignments of Computer Science

Toggle sidebar
bg1
CS 553 Compiler Construction — Fall 2005 Programming Assignment #3
Common Sub-expression Elimination Due Nov 7, Nov 18, and Dec 9, 2005
Your goal in this assignment is to implement common subexpression elimination and available expressions
augmented with reaching expressions. This project will make our discussions of data-flow analysis more
concrete.
The basic idea is to perform available expression analysis to identify expressions that are available at
the various points in the program. At any computation of an expression that is already available, you can
replace the computation with a temporary variable. You will also have to assign the value of the available
expression at the last point it was computed along all incoming control-flow paths. (Thus the reaching
expressions information).
1 Available and Reaching Expressions
You will perform global dataflow analysis, i.e., within an entire procedure. Assume that the procedure
is represented as a control flow graph with statements in a middle-level intermediate representation. In
particular, each statement can have up to one assignment. Let Ube defined as the set of all expressions
in the procedure. Each expression also has a set of statements associated with it to indicate where it was
last computed along all incoming control-flow paths. Flow values for this problem are subsets of U. Each
statement Sin the procedure has associated with it two sets, gen[S] and kill[S]. g en[S] is the subset of U
that is generated by S,i.e., all expressions that Scomputes. kill[S] is the set of expressions that are killed
by S.
Your data-flow analysis will be done on the basic-block level, so you will need a notion of gen[S] and
kill[S] that applies to basic blocks.
1.1 Creating a control flow graph
Before you can do an iterative data-flow analysis, you will need a CFG whose nodes are basic blocks containing
sequences of instructions in a middle-level intermediate representation. (C-Breeze has a data-flow analysis
engine that operates at a higher level, but you should not use that for this assignment.)
C-Breeze has a built-in phase (-dismantle) that dismantles high-level C code into a MIR form, using
if’s and goto’s instead of arbitrary control structures, and having at most one assignment per statement.
Another phase (-cfg) divides this dismantled code into basic blocks contained in basicblockNode’s (a
subclass of blockNode) and threads the blocks together in a control flow graph. In the resulting CFG, each
procedure will have the following form:
procNode
declNode
blockNode (proc->body())
decl_list
stmt_list
basicblockNode
stmt_list
basicblockNode
stmt_list
basicblockNode
stmt_list
...
1
pf3
pf4

Partial preview of the text

Download CS 553 Compiler Construction Assignment 3: Common Subexpression Elimination - Prof. Michel and more Assignments Computer Science in PDF only on Docsity!

CS 553 Compiler Construction — Fall 2005 Programming Assignment # Common Sub-expression Elimination Due Nov 7, Nov 18, and Dec 9, 2005

Your goal in this assignment is to implement common subexpression elimination and available expressions augmented with reaching expressions. This project will make our discussions of data-flow analysis more concrete. The basic idea is to perform available expression analysis to identify expressions that are available at the various points in the program. At any computation of an expression that is already available, you can replace the computation with a temporary variable. You will also have to assign the value of the available expression at the last point it was computed along all incoming control-flow paths. (Thus the reaching expressions information).

1 Available and Reaching Expressions

You will perform global dataflow analysis, i.e., within an entire procedure. Assume that the procedure is represented as a control flow graph with statements in a middle-level intermediate representation. In particular, each statement can have up to one assignment. Let U be defined as the set of all expressions in the procedure. Each expression also has a set of statements associated with it to indicate where it was last computed along all incoming control-flow paths. Flow values for this problem are subsets of U. Each statement S in the procedure has associated with it two sets, gen[S] and kill[S]. gen[S] is the subset of U that is generated by S, i.e., all expressions that S computes. kill[S] is the set of expressions that are killed by S. Your data-flow analysis will be done on the basic-block level, so you will need a notion of gen[S] and kill[S] that applies to basic blocks.

1.1 Creating a control flow graph

Before you can do an iterative data-flow analysis, you will need a CFG whose nodes are basic blocks containing sequences of instructions in a middle-level intermediate representation. (C-Breeze has a data-flow analysis engine that operates at a higher level, but you should not use that for this assignment.) C-Breeze has a built-in phase (-dismantle) that dismantles high-level C code into a MIR form, using if’s and goto’s instead of arbitrary control structures, and having at most one assignment per statement. Another phase (-cfg) divides this dismantled code into basic blocks contained in basicblockNode’s (a subclass of blockNode) and threads the blocks together in a control flow graph. In the resulting CFG, each procedure will have the following form:

procNode declNode blockNode (proc->body()) decl_list stmt_list basicblockNode stmt_list basicblockNode stmt_list basicblockNode stmt_list ...

The body of the procedure consists of a blockNode in which each statement is basicblockNode. All of the local variables in the procedure (excluding formal parameters) will be declared in the top-level decl list. All of the statements that perform computation are contained in the stmt lists of the basicblockNodes. So, to traverse all the statements in procedure proc you could use the following code:

procNode * proc; blockNode * body = proc->body();

// -- For all basic blocks... for (stmt_list_p p = body->stmts().begin(); p != body->stmt().end(); ++p) { basicblockNode * bb = (basicblockNode *) *p; // -- Visit all statements in the basic block... for (stmt_list_p q = bb->stmts().begin(); q != bb->stmts().end(); ++q) { stmtNode * s = *q;

do_something_with(s); } }

In the MIR form, the stmtNode s will have one of only five types: threeAddrNode, labelNode, gotoNode, ConditiongotoNode, or returnNode. See the C-Breeze User Guide for more details. In addition, each basicblockNode has preds() and succs() fields are lists of basicblockNode’s, pointing to the predecessors and successors of the block. See the C-Breeze Users’ Guide for more information on the -dismantle and -cfg phases.

1.2 Iterative data-flow analysis for available expressions analysis

You need to implement an iterative algorithm for solving the data-flow equations for available expressions analysis. You can use the naive algorithm or you might consider a more efficient work-list algorithm. Or, you could implement both and compare performance. If you are really ambitious, consider what it would take to implement a data-flow analysis engine into which you can plug any data-flow problem!

2 Your Assignment

  • Turn in a well-written plan by November 7th. The plan should be a two-page document specifying the responsibilities of each team member and outlining the project report.
  • Write a C-Breeze phase that computes, for each statement in each procNode in each unit, the set of available expressions with reaching expression information for that statement.
  • Turn in a well-written description of the available expressions with reaching expressions data-flow analysis by November 18. How did you organize the data-flow analysis? What problems did you run into? What is the average number of available expressions at each statement, and for each one how many reaching expressions do they typically have?
  • Write another C-Breeze phase, invoked by the name "cse" that invokes the available expressions phase and performs common sub-expression elimination.
  • Each statement node has associate with it a string called comment(). You can assign to or append to this string any string you like. When the code is unparsed back with the -c-code phase, the comment() field will appear as a comment in the code. The -cfg phase puts in information about predecessors and successors, but you can get rid of this if you like. When debugging your code, it can be hard to tell where in the code something is happening; you can use the comment() field to display debug information. For instance, you might want to know the gen[S] and kill[S] sets for each statement; you can set the comment field of each statement pointer to its hexadecimal representation, then append to the comment field the hexadecimal representation of all the expressions paired with statement sets in the gen[S] and kill[S] sets. You can also do this for the in[S] and out[S] sets of each basic block to make sure your iterative data-flow analysis is working.
  • You should delete and set to NULL the gen() and kill() sets of each statement node after doing the data-flow analysis, but beyond that you don’t need to worry too much about cleaning up after yourself. In particular, don’t delete nodes; a garbage collection system for nodes is being developed for C-Breeze, so that in the future it is not necessary delete nodes.
  • Run the -cfg phase on several sample C program and examine the output to get an idea of the level of the code you’ll be optimizing. You can use the dot walker you wrote in project 2 to help you visualize and debug.

4 What to turn in

You will turn in your work using WebCT. Put your paper in your copy of the Project2 directory, tar up a clean version of Project2 (ie. do a make clean), and submit the tar ball. The Makefile should produce one executable called cbz that contains all the phases mentioned above, and you should provide enough code so that the C-Breeze executable containing your phase can be reproduced by typing make. Your writeup should be in one of the following formats: ASCII text, LATEX, or PDF.

5 Due dates

The project plan is due Monday November 7 at 2:00pm. The writeup of available expressions extended with reaching expressions is due Friday November 18th at 2:00pm. This final assignment is due Friday December 9 at 2:00pm.