scale.score.expr
Class CompareExpr

java.lang.Object
  extended by scale.common.Root
      extended by scale.score.Note
          extended by scale.score.expr.Expr
              extended by scale.score.expr.BinaryExpr
                  extended by scale.score.expr.CompareExpr
All Implemented Interfaces:
AnnotationInterface, DisplayNode

public class CompareExpr
extends BinaryExpr

This class represents three-valued comparison functions.

$Id: CompareExpr.java,v 1.26 2007-04-27 18:04:35 burrill Exp $

Copyright 2008 by the Scale Compiler Group,
Department of Computer Science
University of Massachusetts,
Amherst MA. 01003, USA
All Rights Reserved.

If the left argument is less than the right argument, a -1 is returned. If the left argument is greater than the right argument, a 1 is returned. Otherwise, a 0 is returned. If the arguments are floating point and either is Nan, then either a -1 or a 1 is returned depending on the mode setting.

We use instances of this class instead of generating comparisons and IfThenElseChords directly in the CFG. There is no benefit to generating the decision points in the CFG and doing so makes the CFG unnecessarily complex. Also, there is no easy way to test for a NaN in C code so a function call would have to be made to do that test anyway.

By using this class, Scribble2C can generate a function call for floating point arguments and use the C conditional operator inline for integer arguments.

When going direct to machine code, an instance of this class can be converted directly into machine instructions that test for NaN.


Field Summary
static int FloatG
          If either argument is Nan, return 1.
static int FloatL
          If either argument is Nan, return -1.
static java.lang.String[] modes
          String representation of the compare mode.
static int Normal
          Use normal compare.
 
Fields inherited from class scale.score.expr.Expr
fpReorder, SE_DOMAIN, SE_NONE, SE_OVERFLOW, SE_STATE
 
Constructor Summary
CompareExpr(Expr la, Expr ra, int mode)
          The expression type is integer two's complement.
CompareExpr(Type type, Expr la, Expr ra, int mode)
           
 
Method Summary
 Expr copy()
          Perform a deep copy of the expression tree.
 Literal getConstantValue()
          Return the constant value of the expression.
 Literal getConstantValue(HashMap<Expr,Literal> cvMap)
          Return the constant value of the expression.
 java.lang.String getDisplayLabel()
          Return a String suitable for labeling this node in a graphical display.
 int getMode()
          Return the mode of the comparison operation.
 void visit(Predicate p)
          Process a node by calling its associated routine.
 
Methods inherited from class scale.score.expr.BinaryExpr
associativeSwapOperands, containsDeclaration, dependsOnDeclaration, equivalent, executionCostEstimate, getAliasAnnote, getDeclList, getExprList, getLeftArg, getLoadExprList, getOperand, getOperandArray, getRightArg, isAssociative, isCommutative, isLeftDistributive, isRightDistributive, loopClean, numOperands, optimizationCandidate, pushOperands, recordRefs, removeRefs, removeUseDef, replaceDecl, setLeftArg, setOperand, setRightArg, sideEffects, swapOperands, unlinkExpression, validate
 
Methods inherited from class scale.score.expr.Expr
addCast, addCast, canonical, changeInDataEdge, conditionalCopy, conditionalUnlinkExpression, deleteOutDataEdge, executionOrder, executionOrdinal, findCriticalChord, findLinearCoefficient, findSubscriptExpr, getAffineExpr, getAffineRepresentation, getCall, getCoreType, getCriticalChord, getDefExpr, getDisplayColorHint, getDisplayShapeHint, getDualExpr, getInDataEdge, getInDataEdgeArray, getLoopHeader, getLow, getLValue, getOutDataEdge, getPointedToCore, getReference, getReuseLevel, getRValue, getType, getUseDef, hasTrueFalseResult, isCast, isDefined, isDefined, isLiteralExpr, isLoopInvariant, isMatchExpr, isMemoryDef, isMemRefExpr, isScalar, isSimpleExpr, mayGenerateCall, numInDataEdges, reduce, removeDualExprs, setCrossloopReuse, setOutDataEdge, setSpatialReuse, setStep, setTemporalReuse, setType, setUseDef, toStringSpecial, validLValue
 
Methods inherited from class scale.score.Note
getChord, getEssentialUse, setAnnotationLevel, setReportLevel, toString
 
Methods inherited from class scale.common.Root
addAnnotation, allAnnotations, allMatchingAnnotations, getAnnotation, getDisplayName, getDisplayString, getNodeCount, getNodeID, hasAnnotation, hasEqualAnnotation, hashCode, removeAnnotation, removeAnnotations, toStringAnnotations, toStringClass, trace, trace, trace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

Normal

public static final int Normal
Use normal compare.

See Also:
Constant Field Values

FloatL

public static final int FloatL
If either argument is Nan, return -1.

See Also:
Constant Field Values

FloatG

public static final int FloatG
If either argument is Nan, return 1.

See Also:
Constant Field Values

modes

public static final java.lang.String[] modes
String representation of the compare mode.

Constructor Detail

CompareExpr

public CompareExpr(Type type,
                   Expr la,
                   Expr ra,
                   int mode)
Parameters:
type - is the expression type
la - is the left operand
ra - is the right operand
mode - determines the type of comparison

CompareExpr

public CompareExpr(Expr la,
                   Expr ra,
                   int mode)
The expression type is integer two's complement.

Parameters:
la - is the left operand
ra - is the right operand
mode - determines the type of comparison
Method Detail

copy

public Expr copy()
Description copied from class: Expr
Perform a deep copy of the expression tree. By deep copy, we mean that copies are made of the expression tree.

Specified by:
copy in class Expr
Returns:
a copy of this expression

getMode

public int getMode()
Return the mode of the comparison operation.


visit

public void visit(Predicate p)
Description copied from class: Note
Process a node by calling its associated routine. See the "visitor" design pattern in Design Patterns: Elements of Reusable Object-Oriented Software by E. Gamma, et al, Addison Wesley, ISBN 0-201-63361-2.

Each class has a visit(Predicate p) method. For example, in class ABC:

   public void visit(Predicate p)
   {
     p.visitABC(this);
   }
 
and the class that implements Predicate has a method
   public void visitABC(Note n)
   {
     ABC a = (ABC) n;
     ...
   }
 
Thus, the class that implements Predicate can call
   n.visit(this);
 
where n is a Note sub-class without determining which specific sub-class n is. The visit pattern basically avoids implementing a large switch statement or defining different methods in each class for some purpose.

Specified by:
visit in class Note
See Also:
Predicate

getDisplayLabel

public java.lang.String getDisplayLabel()
Description copied from class: Expr
Return a String suitable for labeling this node in a graphical display. This method should be over-ridden as it simplay returns the class name.

Specified by:
getDisplayLabel in interface DisplayNode
Overrides:
getDisplayLabel in class Expr

getConstantValue

public Literal getConstantValue(HashMap<Expr,Literal> cvMap)
Return the constant value of the expression. Follow use-def links.

Overrides:
getConstantValue in class Expr
See Also:
Lattice

getConstantValue

public Literal getConstantValue()
Return the constant value of the expression. Do not follow use-def links.

Overrides:
getConstantValue in class Expr
See Also:
Lattice