Homework 4: Static Single Assignment Course: CS 380C: Advanced Compiler Techniques (Fall 2007) Instructor: Keshav Pingali Assigned: Thursday, October 11, 2007 Due: Wednesday, October 24, 2007, 11:59:59 PM changed to: Wednesday, October 31, 2007, 11:59:59 PM You can do this assignment in groups of two. Turn your assignment in time. Turnin will be disabled soon after the deadline. 1. Objective ------------ The goal of this assignment is to 1. Build Static Single Assignment (SSA) form 2. Perform constant propagation 2. Build SSA form ----------------- Implement the algorithm discussed in class to build SSA form. Use the opcode "phi" for phi functions. Name subscripted variables as variable$subscript. For example, variable i would become i$0, i$1, etc. Take a look at a simple example of a loop, and its representation in 3-address and SSA formats. Simple loop i = 0; while (i < 10) { i = i + 1; } 3-address format: instr 4: move 0 i#-8 instr 5: cmplt i#-8 10 instr 6: blbc (5) [10] instr 7: add i#-8 1 instr 8: move (7) i#-8 instr 9: br [5] instr 10: ret 0 3-address format with SSA: (This example is incorrect. Look at the updated version below). instr 4: move 0 i$0#-8 instr 23: phi i$0#-8 i$2#-8 instr 24: move (23) i$1#-8 instr 5: cmplt i$1#-8 10 instr 6: blbc (5) [10] instr 7: add i$1#-8 1 instr 8: move (7) i$2#-8 instr 9: br [23] instr 10: ret 0 Updated version (does not have offsets. You have to keep track of offsets in your symbol table for memory allocation). instr 4: move 0 i$0 instr 23: phi i$0 i$2 instr 24: move (23) i$1 instr 5: cmplt i$1 10 instr 6: blbc (5) [10] instr 7: add i$1 1 instr 8: move (7) i$2 instr 9: br [23] instr 10: ret 0 Notice the new instructions instr 23 and 24. A Phi instruction has been added only for variable i. Since virtual registers generated by our frontend csc are all block-local (written to, and read from, within the same basic-block), any phi functions inserted for these virtual registers would be dead. Therefore, it is not necessary it insert phi functions for such virtual registers (as long as you maintain the block-local property). For this assignment, you do not have to add support to your frontend to read in 3-address code in SSA form. 3. Perform Constant Propagation ------------------------------- Perform constant propagation on the CFG in SSA form. 4. Output --------- As usual, your compiler should accept 3-address code as input from stdin, and write output to stdout. Your compiler invoked by the script 'run.sh' should accept the following command line arguments. 1. -opt, a comma separated list of transformations. The transformations to support are dce, scp, pre and ssa. -opt=ssa,scp means convert to SSA form and then perform constant propagation. 2. -backend, the backend to be used to write output to. The optimizations to support are c, cfg and 3addr. You do not have to support the C backend when SSA is performed. 5. Turning in your assignment ----------------------------- Download this tarball. http://www.cs.utexas.edu/users/pingali/CS380C/2007fa/assignments/assignment4/assignment4.tar.gz This is organized similar to pervious homeworks. Your assignment should contain the following: 1. A single tar.gz file named hw4.tar.gz, which, when extracted, creates directory hw4. 2. The hw4 directory can contain sub-directories. 3. The hw4 directory should contain the following files: a. README - Please include your name(s) and UTEID(s) here. b. compile.sh - a script to compile your source code. c. run.sh - a script that runs your compiler. This script should read 3-address code as input from stdin and write output to stdout. The output is specified by the command line arguments described in section 4. The hw4 directory already exists with these files in the tarball you downloaded. Turn in your assignment by running the following commands on a UTCS Linux machine. $ # Go the parent directory of the hw4 directory. $ tar -zcvf hw4.tar.gz hw4 $ turnin --submit suriya cs380c-hw4 hw4.tar.gz $ turnin --list suriya cs380c-hw4 Please use turnin to submit your assignment. Only homeworks that are turned in using the procedure described above will be accepted. 6. Hints -------- 0. Start early :) 1. Watch the clarifications page http://www.cs.utexas.edu/users/pingali/CS380C/2007fa/clarifications.html