coins.casttohir
クラス ToHirCOpt

java.lang.Object
  |
  +--coins.casttohir.ToHirVisit
        |
        +--coins.casttohir.ToHirCOpt
直系の既知のサブクラス:
ToHirCOpt2

public class ToHirCOpt
extends ToHirVisit

Do simple optimizations (optimizations of expressions and conditions) for HIR-C. The level of optimizations can be specified by compiler option. ToHirCOpt does the optimizations of kind 1 throgh 3 as default optimizations.

 Expression optimization

 Kind 1. Non-volatile const-variable with initial value
   is changed to constant.
   ( At present, this transformation is bypassed
     because C parser ignores const qualifier.)

 Kind 2. Optimize pointer expressions
   Convert pointer arithmetic expression to array subscript expression.
   This conversion makes easy to find candidate arrays of parallelization
   without doing pointer analysis.
   a. (DECAY(array)+i+j)  -->  array[i+j]
   b. (pointer+i+j)       -->  UNDECAY(pointer)[i+j]

   However, it is limited that b. is done when the following conditions are
   satisfied. The pointer which satisfies these conditions is
   "Array parameter to which the content is not changed."
   . The value is not substituted.
   . The value is not acquired (is not copied).
   . The address is not operated. (ptr+int ptr-int)
   . The address is not taken. (&ptr)
   . The number of elements is not 0. (The pointer type maintains the number
       of elements when declared as an array parameter of C language.
       'Pointer type with range information' )

 Kind 3. Eliminate mutually opposing operations
   *&v    -->  v
   &*v    -->  v
   ~~v    -->  v
   -(-v)  -->  v
   DECAY(UNDECAY(pointer))  -->  pointer
   UNDECAY(DECAY(array))    -->  array

 Kind 4. Do optimization for special constants
   Special constants:
   . 0
   . 1
   . ALL_BITS_ARE_1: constant having 1 on all bit positions
       and having precesion greater or equal than that of
       at least one operand.
   . MORE_THAN_SIZE: constant having bit size greater than
       that of resultant expression.

     v +  0  -->  v
     v -  0  -->  v
     v -  v  -->  0 (v is non volatile integer or pointer)
     v *  0  -->  0
     v /  0  -->  issue warning
     v /  1  -->  v
     v %  0  -->  issue warning
     v %  1  -->  0
     v &  0  -->  0
     v &  ALL_BITS_ARE_1  -->  v
     v |  0               -->  v
     v |  ALL_BITS_ARE_1  -->  ALL_BITS_ARE_1
     v << 0               -->  v
     v << MORE_THAN_SIZE  -->  0
     v >> 0               -->  v
     v >> MORE_THAN_SIZE  -->  0 (v is unsigned integer)
     v >>>MORE_THAN_SIZE  -->  0

     [ 0 -  v  -->  -v (not supported) ]

 Condition optimization

 Kind 5. Do inversion of comparison
     !(a<b)  -->  a>=b
     (>, <=, >=, ==, != are transformed in the similar way)
   Before reaching to here, logical-not has been already converted to equality
   expression and comparison has been converted to conditional expression
   in ToHirC2. So, in actual coding, they are transformed in the following
   way:
    (a<b ? CONST_X : CONST_Y)==CONST_X  -->  a<b
    (a<b ? CONST_X : CONST_Y)==CONST_Y  -->  a>=b (inversion of comparison)
    (a<b ? CONST_X : CONST_Y)!=CONST_Y  -->  a<b
    (a<b ? CONST_X : CONST_Y)!=CONST_X  -->  a>=b (inversion of comparison)
  (>, <=, >=, ==, != are transformed in the similar way)

 6. Convert comparison that leads to always true or always false
   to constant issuing warning
     constant-a comparison-operator constant-b  --> true or false
     variable >  maximal value  --> false
     variable <  minimum value  --> false
     variable >= minimum value  --> true
     variable <= maximum value  --> true
   Following transformations are applied not only ADDR but also DECAY:
     (ADDR object-x) == (ADDR object-x)  --> true
     (ADDR object-x) == (ADDR object-y)  --> false
     (ADDR object-x) == 0                --> false
     (ADDR object-x) != (ADDR object-x)  --> false
     (ADDR object-x) != (ADDR object-y)  --> true
     (ADDR object-x) != 0                --> true

 7. If left operand of && or || is true or false, then
   change it to (side effect expression of left operand)
   followed with comma and (right operand or trure or false)
    e1 && e2
      false && e2  -->  (side effect of e1),false
      true  && e2  -->  (side effect of e1),e2
    e1 || e2
      false || e2  -->  (side effect of e1),e2
      true  || e2  -->  (side effect of e1),true
 8. If conditional expression part c of select expression is always
   true or false, then change it to (side effect of c) followed by
   comma and (2nd or 3rd operand)
      e1 ? e2 : e3  --> (side effect of e1), e2 when e1 is always true
      e1 ? e2 : e3  --> (side effect of e1), e3 when e1 is always false
      r = (a=1)>0 ? 1 : 0;  -->  r = (a=1) , 1;

 Note
   Side effects are placed at the point immediately preceeding the
   side effect completion point and combined with rest of given expression
   by using comma operator. Foe example,
   . if( f()*0 != 0 ) --> if( f(),false )
   . a += f()*0; --> f(),a+=0; --> f();
 Note
   Side effect completion point (sequence point)
   . after evaluation of an actual parameter
   . logical-and expression &&
   . logical-or  expression ||
   . selection   expression ?:
   . comma       expression ,
   . initiation  expression
   . expression statement (ExpStmt)
   . conditional expression part of if/switch/while/do-while
   . three parts s1, e2, s3 of for-statement for(s1; e2; s3)
   . optional return value expression of return statement
 


フィールドの概要
protected  SideEffectBuffer buffer
           
 int fDbgLevel
           
protected  HIR hir
           
protected  ConditionInverter inverter
           
protected  BlockStmt nowBlock
          Now processing block (used to create initializer).
protected  Sym sym
           
protected  ToHirCast toCast
           
protected  ToHir toHir
           
 
コンストラクタの概要
ToHirCOpt(ToHir tohir)
          Constructor.
 
メソッドの概要
protected  Exp atAdd(Exp e)
          At add expression node.
protected  Exp atAddAssign(Exp e)
          At add-assign expression node.
protected  Exp atAddr(Exp e)
          At address expression node.
protected  Exp atAnd(Exp e)
          At and expression node.
protected  Exp atAndAssign(Exp e)
          At and-assign expression node.
protected  Exp atArrow(Exp e)
          At arrow expression node.
protected  Exp atARShift(Exp e)
          At arithmetic R-shift expression node.
protected  Exp atAssign(Exp e)
          At assign expression node.
protected  void atAssignStmt(AssignStmt s)
          At assign statement node.
protected  void atBlock(BlockStmt s)
          At block statement node.
protected  Exp atCall(FunctionExp e)
          At function call expression node.
protected  Exp atCmpEq(Exp e)
          At EQ expression node.
protected  Exp atCmpGe(Exp e)
          At GE expression node.
protected  Exp atCmpGt(Exp e)
          At GT expression node.
protected  Exp atCmpLe(Exp e)
          At LE expression node.
protected  Exp atCmpLt(Exp e)
          At LT expression node.
protected  Exp atCmpNe(Exp e)
          At NE expression node.
protected  Exp atComma(Exp e)
          At comma expression node.
protected  Exp atConst(ConstNode e)
          At constant node.
protected  Exp atContents(Exp e)
          At indirection expression node.
protected  Exp atConv(Exp e)
          At cast expression node.
protected  Exp atDecay(Exp e)
          At decay expression node.
protected  Exp atDiv(Exp e)
          At div expression node.
protected  Exp atDivAssign(Exp e)
          At div-assign expression node.
protected  Exp atElem(ElemNode e)
          At element node.
protected  Exp atEqZero(Exp e)
          At logical-not expression node.
protected  Exp atExpList(ExpListExp e)
          At expression list node.
protected  Exp atExpRepeat(Exp e)
          At expression repeatation node.
protected  void atExpStmt(ExpStmt s)
          At expression statement node.
protected  void atFor(LoopStmt s)
          At for statement node.
protected  void atIf(IfStmt s)
          At if statement node.
protected  Exp atIndex(Exp e)
          At index expression node.
protected  InfStmt atInfStmt(InfStmt pInf)
          atInfStmt parses the pragma body in the form of String and change its symbols to instances of Sym (Var, Subp, Label, Const) and items enclosed in parenthesis to IrList changing its elements to Sym, etc.
protected  void atJump(JumpStmt s)
          At goto statement node.
protected  void atLabeledStmt(LabeledStmt s)
          At labeled statement node.
protected  Exp atLgAnd(Exp e)
          At logical-and expression node.
protected  Exp atLgOr(Exp e)
          At logical-or expression node.
protected  Exp atLShift(Exp e)
          At L-shift expression node.
protected  Exp atLShiftAssign(Exp e)
          At L-shift-assign expression node.
protected  Exp atMod(Exp e)
          At mod expression node.
protected  Exp atModAssign(Exp e)
          At mod-assign expression node.
protected  Exp atMul(Exp e)
          At mul expression node.
protected  Exp atMulAssign(Exp e)
          At mul-assign expression node.
protected  Exp atNeg(Exp e)
          At negative expression node.
protected  Exp atNot(Exp e)
          At not expression node.
protected  Exp atOffset(Exp e)
          At offset(difference of address) expression node.
protected  Exp atOr(Exp e)
          At or expression node.
protected  Exp atOrAssign(Exp e)
          At or-assign expression node.
protected  Exp atPost(int op, Exp e)
          At post-operator expression node.
protected  Exp atPre(int op, Exp e)
          At pre-operator expression node.
protected  Exp atQual(Exp e)
          At member-access expression node.
protected  void atReturn(ReturnStmt s)
          At return statement node.
protected  Exp atRShift(Exp e)
          At logical R-shift expression node.
protected  Exp atRShiftAssign(Exp e)
          At R-shift-assign expression node.
protected  Exp atSelect(Exp e)
          At selection expression node.
protected  void atSetDataStmt(SetDataStmt s)
          At datacode statement node.
protected  Exp atSub(Exp e)
          At sub expression node.
protected  Exp atSubAssign(Exp e)
          At sub-assign expression node.
protected  Exp atSubp(SubpNode e)
          At function node.
protected  SubpDefinition atSubpDefinition(SubpDefinition s)
          At block statement node.
protected  Exp atSubs(Exp e)
          At subscript expression node.
protected  void atSwitch(SwitchStmt s)
          At switch statement node.
protected  Exp atUndecay(Exp e)
          At undecay expression node.
protected  void atUntil(LoopStmt s)
          At do-while statement node.
protected  Exp atVar(VarNode e)
          At variable node.
protected  void atWhile(LoopStmt s)
          At while statement node.
protected  Exp atXor(Exp e)
          At xor expression node.
protected  Exp atXorAssign(Exp e)
          At xor-assign expression node.
protected  boolean inInitBlock()
          Return true if now processing in the initialization block.
protected  void message(int level, java.lang.String mes)
          Output debug message.
(パッケージプライベート)  IrList processPragmaItem(ParseString pParseString, java.lang.String pNextItem, IrList pList)
           
(パッケージプライベート)  Exp visitExp(Exp e)
           
 void visitProgram()
          Visit HIR program tree.
(パッケージプライベート)  void visitProgram(Program program)
          Visit HIR program tree.
(パッケージプライベート)  void visitStmt(Stmt s)
          Call appropriate method by operator of statement node.
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

toHir

protected final ToHir toHir

hir

protected final HIR hir

sym

protected final Sym sym

toCast

protected final ToHirCast toCast

buffer

protected final SideEffectBuffer buffer

inverter

protected final ConditionInverter inverter

nowBlock

protected BlockStmt nowBlock
Now processing block (used to create initializer).


fDbgLevel

public final int fDbgLevel
コンストラクタの詳細

ToHirCOpt

public ToHirCOpt(ToHir tohir)
Constructor.

パラメータ:
tohir - Offers cooperation with the object of other packages.
メソッドの詳細

message

protected void message(int level,
                       java.lang.String mes)
Output debug message.

オーバーライド:
クラス ToHirVisit 内の message
パラメータ:
level - Debug level.
mes - Debug message.

atIf

protected void atIf(IfStmt s)
At if statement node.

オーバーライド:
クラス ToHirVisit 内の atIf
パラメータ:
s - IfStmt

atWhile

protected void atWhile(LoopStmt s)
At while statement node.

オーバーライド:
クラス ToHirVisit 内の atWhile
パラメータ:
s - LoopStmt

atFor

protected void atFor(LoopStmt s)
At for statement node.

オーバーライド:
クラス ToHirVisit 内の atFor
パラメータ:
s - LoopStmt

atUntil

protected void atUntil(LoopStmt s)
At do-while statement node.

オーバーライド:
クラス ToHirVisit 内の atUntil
パラメータ:
s - LoopStmt

atSwitch

protected void atSwitch(SwitchStmt s)
At switch statement node.

オーバーライド:
クラス ToHirVisit 内の atSwitch
パラメータ:
s - SwitchStmt

atReturn

protected void atReturn(ReturnStmt s)
At return statement node.

オーバーライド:
クラス ToHirVisit 内の atReturn
パラメータ:
s - ReturnStmt

atExpStmt

protected void atExpStmt(ExpStmt s)
At expression statement node.

オーバーライド:
クラス ToHirVisit 内の atExpStmt
パラメータ:
s - ExpStmt

atVar

protected Exp atVar(VarNode e)
At variable node.

オーバーライド:
クラス ToHirVisit 内の atVar
パラメータ:
e - Exp
戻り値:
Exp

atCall

protected Exp atCall(FunctionExp e)
At function call expression node.

オーバーライド:
クラス ToHirVisit 内の atCall
パラメータ:
e - Exp
戻り値:
Exp

atNot

protected Exp atNot(Exp e)
At not expression node.

オーバーライド:
クラス ToHirVisit 内の atNot
パラメータ:
e - Exp
戻り値:
Exp

atNeg

protected Exp atNeg(Exp e)
At negative expression node.

オーバーライド:
クラス ToHirVisit 内の atNeg
パラメータ:
e - Exp
戻り値:
Exp

atAddr

protected Exp atAddr(Exp e)
At address expression node.

オーバーライド:
クラス ToHirVisit 内の atAddr
パラメータ:
e - Exp
戻り値:
Exp

atDecay

protected Exp atDecay(Exp e)
At decay expression node.

オーバーライド:
クラス ToHirVisit 内の atDecay
パラメータ:
e - Exp
戻り値:
Exp

atUndecay

protected Exp atUndecay(Exp e)
At undecay expression node.

オーバーライド:
クラス ToHirVisit 内の atUndecay
パラメータ:
e - Exp
戻り値:
Exp

atContents

protected Exp atContents(Exp e)
At indirection expression node.

オーバーライド:
クラス ToHirVisit 内の atContents
パラメータ:
e - Exp
戻り値:
Exp

atEqZero

protected Exp atEqZero(Exp e)
At logical-not expression node.

オーバーライド:
クラス ToHirVisit 内の atEqZero
パラメータ:
e - Exp
戻り値:
Exp

inInitBlock

protected boolean inInitBlock()
Return true if now processing in the initialization block.

戻り値:
boolean

visitProgram

public final void visitProgram()
Visit HIR program tree.


visitProgram

final void visitProgram(Program program)
Visit HIR program tree.

パラメータ:
program - Program

visitStmt

final void visitStmt(Stmt s)
Call appropriate method by operator of statement node.

パラメータ:
s - Visited statement.

atBlock

protected void atBlock(BlockStmt s)
At block statement node.

パラメータ:
s - BlockStmt

atLabeledStmt

protected void atLabeledStmt(LabeledStmt s)
At labeled statement node.

パラメータ:
s - LabeledStmt

atAssignStmt

protected void atAssignStmt(AssignStmt s)
At assign statement node.

パラメータ:
s - AssignStmt

atJump

protected void atJump(JumpStmt s)
At goto statement node.

パラメータ:
s - JumpStmt

atSetDataStmt

protected void atSetDataStmt(SetDataStmt s)
At datacode statement node.

パラメータ:
s - SetDataStmt

visitExp

final Exp visitExp(Exp e)

atConst

protected Exp atConst(ConstNode e)
At constant node.

パラメータ:
e - Exp
戻り値:
Exp

atSubp

protected Exp atSubp(SubpNode e)
At function node.

パラメータ:
e - Exp
戻り値:
Exp

atElem

protected Exp atElem(ElemNode e)
At element node.

パラメータ:
e - Exp
戻り値:
Exp

atSubs

protected Exp atSubs(Exp e)
At subscript expression node.

パラメータ:
e - Exp
戻り値:
Exp

atIndex

protected Exp atIndex(Exp e)
At index expression node.

パラメータ:
e - Exp
戻り値:
Exp

atQual

protected Exp atQual(Exp e)
At member-access expression node.

パラメータ:
e - Exp
戻り値:
Exp

atArrow

protected Exp atArrow(Exp e)
At arrow expression node.

パラメータ:
e - Exp
戻り値:
Exp

atAdd

protected Exp atAdd(Exp e)
At add expression node.

パラメータ:
e - Exp
戻り値:
Exp

atSub

protected Exp atSub(Exp e)
At sub expression node.

パラメータ:
e - Exp
戻り値:
Exp

atMul

protected Exp atMul(Exp e)
At mul expression node.

パラメータ:
e - Exp
戻り値:
Exp

atDiv

protected Exp atDiv(Exp e)
At div expression node.

パラメータ:
e - Exp
戻り値:
Exp

atMod

protected Exp atMod(Exp e)
At mod expression node.

パラメータ:
e - Exp
戻り値:
Exp

atAnd

protected Exp atAnd(Exp e)
At and expression node.

パラメータ:
e - Exp
戻り値:
Exp

atOr

protected Exp atOr(Exp e)
At or expression node.

パラメータ:
e - Exp
戻り値:
Exp

atXor

protected Exp atXor(Exp e)
At xor expression node.

パラメータ:
e - Exp
戻り値:
Exp

atCmpEq

protected Exp atCmpEq(Exp e)
At EQ expression node.

パラメータ:
e - Exp
戻り値:
Exp

atCmpNe

protected Exp atCmpNe(Exp e)
At NE expression node.

パラメータ:
e - Exp
戻り値:
Exp

atCmpGt

protected Exp atCmpGt(Exp e)
At GT expression node.

パラメータ:
e - Exp
戻り値:
Exp

atCmpGe

protected Exp atCmpGe(Exp e)
At GE expression node.

パラメータ:
e - Exp
戻り値:
Exp

atCmpLt

protected Exp atCmpLt(Exp e)
At LT expression node.

パラメータ:
e - Exp
戻り値:
Exp

atCmpLe

protected Exp atCmpLe(Exp e)
At LE expression node.

パラメータ:
e - Exp
戻り値:
Exp

atLShift

protected Exp atLShift(Exp e)
At L-shift expression node.

パラメータ:
e - Exp
戻り値:
Exp

atARShift

protected Exp atARShift(Exp e)
At arithmetic R-shift expression node.

パラメータ:
e - Exp
戻り値:
Exp

atRShift

protected Exp atRShift(Exp e)
At logical R-shift expression node.

パラメータ:
e - Exp
戻り値:
Exp

atConv

protected Exp atConv(Exp e)
At cast expression node.

パラメータ:
e - Exp
戻り値:
Exp

atAssign

protected Exp atAssign(Exp e)
At assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atOffset

protected Exp atOffset(Exp e)
At offset(difference of address) expression node.

パラメータ:
e - Exp
戻り値:
Exp

atLgAnd

protected Exp atLgAnd(Exp e)
At logical-and expression node.

パラメータ:
e - Exp
戻り値:
Exp

atLgOr

protected Exp atLgOr(Exp e)
At logical-or expression node.

パラメータ:
e - Exp
戻り値:
Exp

atSelect

protected Exp atSelect(Exp e)
At selection expression node.

パラメータ:
e - Exp
戻り値:
Exp

atComma

protected Exp atComma(Exp e)
At comma expression node.

パラメータ:
e - Exp
戻り値:
Exp

atPre

protected Exp atPre(int op,
                    Exp e)
At pre-operator expression node.

パラメータ:
e - Exp
戻り値:
Exp

atPost

protected Exp atPost(int op,
                     Exp e)
At post-operator expression node.

パラメータ:
e - Exp
戻り値:
Exp

atAddAssign

protected Exp atAddAssign(Exp e)
At add-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atSubAssign

protected Exp atSubAssign(Exp e)
At sub-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atMulAssign

protected Exp atMulAssign(Exp e)
At mul-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atDivAssign

protected Exp atDivAssign(Exp e)
At div-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atModAssign

protected Exp atModAssign(Exp e)
At mod-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atLShiftAssign

protected Exp atLShiftAssign(Exp e)
At L-shift-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atRShiftAssign

protected Exp atRShiftAssign(Exp e)
At R-shift-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atAndAssign

protected Exp atAndAssign(Exp e)
At and-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atOrAssign

protected Exp atOrAssign(Exp e)
At or-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atXorAssign

protected Exp atXorAssign(Exp e)
At xor-assign expression node.

パラメータ:
e - Exp
戻り値:
Exp

atExpList

protected Exp atExpList(ExpListExp e)
At expression list node.

パラメータ:
e - Exp
戻り値:
Exp

atExpRepeat

protected Exp atExpRepeat(Exp e)
At expression repeatation node.

パラメータ:
e - Exp
戻り値:
Exp

atSubpDefinition

protected SubpDefinition atSubpDefinition(SubpDefinition s)
At block statement node.

パラメータ:
s - BlockStmt

atInfStmt

protected InfStmt atInfStmt(InfStmt pInf)
atInfStmt parses the pragma body in the form of String and change its symbols to instances of Sym (Var, Subp, Label, Const) and items enclosed in parenthesis to IrList changing its elements to Sym, etc. This may be called twice for the same pragma. In the second call, return pInf unchanged because it is already in the final form.

戻り値:
InfStmt having Sym elements and (nested) IrList.

processPragmaItem

IrList processPragmaItem(ParseString pParseString,
                         java.lang.String pNextItem,
                         IrList pList)