Download Source Code

Description

In this assignment, super-local register allocation is performed. The program takes SPIM code as input, and is expected to output code with less memory store/load instructions.

The program first identifies all basic blocks in the program, as done in assignment #1. It then propagates constants and removes dead code, as done in assignment #2. Then, it pairs variables in memory with registers as follows:

  1. Perform register/variable associations for each basic block:
    1. Scan each instruction in the block
      • If a LW into a temporary is seen, the compiler sets it as the "source" variable for that register.
      • If a SW to a temporary is seen, the compiler sets it as the "destination" variable for that register.
      • If a definition statement is seen:
        • The compiler first looks at the temporaries appearing on the right-hand side. The current source variable information, if any, is propagated to these operands.
        • If the left-hand operand is a temporary, and the statement is NOT a comparison operation, the compiler scans down the CFG for the first SW or LW instruction where the operand appears. The scanning stops once a join node, or the end of the CFG, is reached. If such an instruction is found, the compiler sets the variable in that instruction as the "destination" variable for that register.
  2. Compute the live sets for all instructions.
  3. Do register allocation for each extended basic block:
    1. Scan each instruction in the block
      • If a LW instruction using a temporary is seen, the compiler checks if the variable loaded has already been allocated to a register. If so, the instruction is deleted. Otherwise, the next available and non-interferring register is allocated for that variable. (Interference between registers may occur if a temporary is never loaded from memory, and never stored to memory. For example, the temporaries used in comparisons/conditional branches are rarely associated with a variable. In that case, the compiler should leave those registers available.)
      • The original SW instructions are removed. Only live variables are saved at the end of an extended basic block (or just before the beginning of a different EBB).
      • For each definition statement, the compiler scans the temporary operands, looking at the right-hand side first. For each temporary:
        • If a "destination" or "source" variable has been set, the register is changed to the one currently allocated to that variable. If the variable has not been allocated to a register yet, the next available register is allocated to that variable, and the register number is updated.

Input & Output

IC = Instruction Count

Description Purpose Input Output Intermediate Original Assignment #1 Assignment #2 Assignment #3
Reads five numbers, performs basic arithmetic operations, then writes the result. Tests register allocation with a program containing one EBB. It also tests a situation where one variable is a copy of another. The two variables are allocated to the same register. 3
7
9
1
4
353-19 test1.i test1.s test1_1.s
IC = 63
test1_2.s
IC = 59
test1_3.s
IC = 37
Sums the four numbers read in, and prints 0 if the sum is less than 10, otherwise prints 1. Tests register allocation with a program containing multiple EBBs. It also demonstrates that register allocation definitely pays off when summing variables. 5
1
-1
0
0 test2.i test2.s test2_1.s
IC = 46
test2_2.s
IC = 42
test2_3.s
IC = 28
Reads a number. If any value other than 0 or 1 is entered, -1 is printed. If 0 is entered, the program computes the sum of two numbers. If 1 is entered, the program computes the difference of two numbers. The two operands, the result and a 0 (for "success") are printed. Tests with even more EBBs and copy statements; in particular, this tests the program's phi-functions. 0
5
3
5320 test3.i test3.s test3_1.s
IC = 90
test3_2.s
IC = 77
test3_3.s
IC = 64
Reads a number. If the number is 0, the program divides two numbers; if the number is 1, the program multiplies two numbers; else -1 is printed. If the result of the operation is greater than 10, only "-2" (for "overflow") is printed; otherwise, the result and a 0 are printed. More testing of multiple EBBs and phi-functions. 1
6
2
-2 test4.i test4.s test4_1.s
IC = 77
test4_2.s
IC = 60
test4_3.s
IC = 47
Notes