


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



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).
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.
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.
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!
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.
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.