Compilers (CS362 2002F)

Code Improvement


Summary: In recent classes, we've examined some possible ways in which a compiler can optimize intermediate code. In today's lab, you will explore the ways in which our Pascal compiler optimizes or fails to optimize code.

Collaboration: Feel free to work on this lab in pairs or trios.

Turning It In: This lab is primarily intended to help you reflect on optimization. You need not turn in anything.

Grading: Since you're not going to turn this in, I"m not going to grade it.

Compiling Pascal Programs: In order to compile Pascal programs, you will need to use the pc or gpc compiler on erdos. You should use the -S flag to generate assembly code.


1. You should refer to lab 10 for guidance in reading GNU x86 assembly.

2. You should read the info pages for gpc to determine how to tell gpc to optimize. You may also want to read the man page for gcc.


Exercise 1: A Simple Program

Here's a simple Pascal program that reads a value, applies a function to that value, and prints out the result.

program example(input,output);
    inval: integer;
    result1,result2: integer;
  function increment(val: integer): integer;
    val := val + 5;
    val := val * 2;
    increment := val;
  function decrement(val: integer; delta: integer): integer;
    decrement := val - delta;
  result1 := increment(inval);
  result2 := decrement(inval,10);

a. Compile this program with no optimization, with basic optimization (-O1), with intermediate optimization (-O2), and with more advanced optimization (-O3).

b. What differences, if any, do you notice in the resulting code?

c. Do you have any hypotheses as to which optizzations are performed at which optimization level?

Exercise 2: Loop Optimization

Here is a variant of the loop code we explored in class.

program loop(input,output);
  DIM = 10;
  matrix = array[1..DIM,1..DIM] of integer;
  a: matrix;
  b: matrix;
  i: integer;
  j: integer;
  for i := 1 to DIM do
    for j := 1 to DIM do
      a[i,j] := b[j,i];

a. Generate assembly code with no optimization. Read through the code to make sure that you understand what it does.

b. Note any potential optimizations you see in that code.

Exercise 3: Loop Optimization, Continued

a. What changes do the various levels of optimization make to the assembly code in the previous problem?

b. Make a table of the compile and run times for the program under the four levels of optimization (none, O1, O2, O3).

Exercise 4: Varying Boudns

Try different dimension sizes in the code from the previous exercises (e.g., 1, 2, 1000) to see whether the optimization differs. You may also want to tell the compiler to try to unroll loops using -funroll-loops.



Tuesday, 10 December 2002

  • Created.


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Dec 10 08:53:58 2002.
The source to the document was last modified on Tue Dec 10 08:53:54 2002.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Glimmer Labs: The Grinnell Laboratory for Interactive Multimedia Experimentation & Research