opt
Class Optimizer

java.lang.Object
  extended by opt.Optimizer

public final class Optimizer
extends java.lang.Object

The Optimizer class optimizes the program. It first converts the AST into a control flow graph of basic blocks containing three address code. It then performs data-flow analyses on the CFG and uses these analyses to transform the CFG. Finally, it converts the CFG to assembly code. This class is incomplete and will need to be implemented by the student.


Constructor Summary
Optimizer(util.ClassTreeNode root, boolean debug)
          Optimizer constructor
 
Method Summary
 java.util.Map<java.lang.String,java.lang.Object> applyVisitor(visitor.Visitor visitor, java.lang.Object o)
          Applies a visitor to all ClassTreeNodes.
 void buildCFG()
          Build the control flow graph
protected  int getClassID(java.lang.String name)
          Get class identifier
static java.lang.String getDestination(TACInst inst)
          Gets the destination of an instruction or null if none
static java.util.Set<java.lang.String> getFieldsUsedInCFG(BasicBlock entryBB)
          Returns all field vars written to or read from in the CFG starting at entryBB.
protected  int getMethodIdx(java.lang.String refType, java.lang.String name)
          Get method index
static java.lang.String getNextTmp()
          Get a new temporary
static java.lang.String getOperand1(TACInst inst)
          Gets the first operand of an instruction or null if none
static java.lang.String getOperand2(TACInst inst)
          Gets the second operand of an instruction or null if none
static java.util.Set<java.lang.String> getSources(TACInst inst)
          Returns new a set with all variables used by given instruction.
static java.util.Set<java.lang.String> getSources(TACInst inst, java.util.Set<java.lang.String> in)
          Returns a set with all variables used by given instruction; if in is not null, it will be used to store result (not cleared).
protected  java.lang.String getStaticName(java.lang.String refType, java.lang.String name)
          Get static method name or null if must use dynamic dispatch
static boolean isCallToBuiltIn(CallInst inst)
          Returns true, if given call instruction calls a method in a built-in class.
static boolean isField(java.lang.String str)
          Is this string a field name
static boolean isNotConst(java.lang.String v)
          Returns true, if this a use which should be considered.
 void optimize(Optimization optimization)
          Perform specified optimization pass on current CFG.
 void print()
          Print out program
protected  void printDot(java.lang.String prefix)
          Outputs a .dot file for each non-builtin method for every class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Optimizer

public Optimizer(util.ClassTreeNode root,
                 boolean debug)
Optimizer constructor

Parameters:
root - root of the class hierarchy tree
optLevel - level of optimization
debug - boolean indicating whether debugging is enabled
Method Detail

optimize

public void optimize(Optimization optimization)
Perform specified optimization pass on current CFG.

Parameters:
optimization - Optimization pass

printDot

protected void printDot(java.lang.String prefix)
Outputs a .dot file for each non-builtin method for every class.

Parameters:
prefix - Filename prefix, e.g. "ssa_"

print

public void print()
Print out program


applyVisitor

public java.util.Map<java.lang.String,java.lang.Object> applyVisitor(visitor.Visitor visitor,
                                                                     java.lang.Object o)
Applies a visitor to all ClassTreeNodes.

Parameters:
visitor - Visitor to apply
o - data to pass to the visitor
Returns:
Map: ClassName -> Visitor result

buildCFG

public void buildCFG()
Build the control flow graph


getNextTmp

public static java.lang.String getNextTmp()
Get a new temporary

Returns:
new temporary

getDestination

public static java.lang.String getDestination(TACInst inst)
Gets the destination of an instruction or null if none

Parameters:
inst - instruction to check
Returns:
destination of instruction

getSources

public static java.util.Set<java.lang.String> getSources(TACInst inst)
Returns new a set with all variables used by given instruction.

Parameters:
inst - TACInst
Returns:
Set or null (if no uses)

getSources

public static java.util.Set<java.lang.String> getSources(TACInst inst,
                                                         java.util.Set<java.lang.String> in)
Returns a set with all variables used by given instruction; if in is not null, it will be used to store result (not cleared).

Parameters:
inst - TACInst
in - null or set to store result in
Returns:
Set or null (if no uses)

getOperand1

public static java.lang.String getOperand1(TACInst inst)
Gets the first operand of an instruction or null if none

Parameters:
inst - instruction to check
Returns:
first operand of instruction

getOperand2

public static java.lang.String getOperand2(TACInst inst)
Gets the second operand of an instruction or null if none

Parameters:
inst - instruction to check
Returns:
second operand of instruction

isField

public static boolean isField(java.lang.String str)
Is this string a field name

Parameters:
str - string to check
Returns:
flag indicating whether a field

isNotConst

public static final boolean isNotConst(java.lang.String v)
Returns true, if this a use which should be considered.

Parameters:
v - Use
Returns:
true, if not constant

getFieldsUsedInCFG

public static java.util.Set<java.lang.String> getFieldsUsedInCFG(BasicBlock entryBB)
Returns all field vars written to or read from in the CFG starting at entryBB.

Parameters:
entryBB - Entry block of CFG
Returns:
set of field var names (may be empty, but not null)

isCallToBuiltIn

public static boolean isCallToBuiltIn(CallInst inst)
Returns true, if given call instruction calls a method in a built-in class.

Parameters:
inst - call instruction.
Returns:
true, if call to built-in class method

getClassID

protected int getClassID(java.lang.String name)
Get class identifier

Parameters:
name - name of class
Returns:
class ID

getMethodIdx

protected int getMethodIdx(java.lang.String refType,
                           java.lang.String name)
Get method index

Parameters:
refType - name of reference class type
name - name of method
Returns:
class ID

getStaticName

protected java.lang.String getStaticName(java.lang.String refType,
                                         java.lang.String name)
Get static method name or null if must use dynamic dispatch

Parameters:
refType - name of reference class type
name - name of method
Returns:
static method name (null if need dispatch)