Go to the previous, next section.

Replicating Trees and Instructions

Many SUIF programs need to duplicate parts of abstract syntax trees or expression trees. For example, loop transformations such as unrolling and peeling require copies of the loop bodies, and entire procedures must be copied to implement procedure inlining. Because code replication can be quite complicated in SUIF, it is handled in the library by the cloning methods.

Several different kinds of SUIF objects can be cloned. The tree_node, tree_node_list, instruction, and operand classes have clone methods. The various derived tree_node classes also have wrapper methods to cast the result of the base class clone method to the appropriate pointer types. When an object is cloned, everything within it is recursively cloned. In addition to the child nodes and instructions, this includes symbol tables and the types and symbols within them. All of the known references to cloned objects (including those in registered annotations) are updated.

The complexity of cloning is primarily due to SUIF's scope rules. The code being copied may reference symbols and types that are not visible in the scope where the copy will be used. Thus the cloning process is divided into three steps:

  1. The code to be copied is scanned for references to symbols and types that are not visible in the specified destination scope. These are referred to as exposed references.

  2. The exposed references are resolved in the symbol table for the destination scope. New variables are created to replace the exposed variables. Types are usually moved up in the symbol table hierarchy so that they are visible in both the source and destination scopes.

  3. The code is actually copied. Symbol tables nested within the code are copied and references to them and their contents are updated to use the copies. Similarly, the exposed references are replaced with references to any new symbols or types that were created in the previous step.

Each of these steps is described in more detail below. The methods that perform the individual steps are available to users who need more control over the cloning process. For example, you may want to insert code to initialize new variables that are created in the destination scope due to exposed references. The cloning methods can also be used to replace references to particular symbols or types without actually copying anything.

The replacements structure is used throughout the cloning process to keep track of exposed references and objects that have been replaced. This structure includes lists of symbols, types, variable definitions, symbol tables, and instructions. For each kind of object, there are two lists. Except in intermediate steps, these lists should be the same length. An entry on the first list refers to an object in the original code and the corresponding entry on the second list is a new object that has been created to replace the original.

Go to the previous, next section.