java coins.driver.Driver -coins:hirOpt=hiroptspec/hiroptspec/...
where each of hiroptspec specifies some optimizing option such as
cf for constant folding, cpf for constant propagation, cse for local common
subexpression elimination, etc.
ex. java coins.Driver -S -coins:hirOpt=cf/cpf
The order of the optimizations is approximately the same to the order
of the optimization specifications by the command line, but in some
cases, some optimization may be inserted to make ready to apply another
specified optimization, or neglected when the effect of the optimization
is covered by another optimization specified. For example, by the command
java coins.Driver -S -coins:hirOpt=cf/cpf
constant folding is done at first and then constant propagation and folding.
java coins.Driver -S -coins:hirOpt=pre
common subexpression elimination within basic blocks is automatically
inserted. As another example, by a command
java coins.Driver -S -coins:hirOpt=pre/cse
cse is negrected because pre will cover the effect of cse.
cf // constant folding
cpf // constant propagation and folding triggered by the propagation
cse // common subexpression elimination within basic blocks
dce // dead code elimination
gt // global variable temporalization within basic block
The optimizers cf and gt do not require data flow analysis, however,
cpf, cse, dce require some result of data flow analysis.
java cooins.driver.Driver -S -coins:hirOpt=cf/cpf
int main ()
{
int a, b, c, d, x;
a = 1;
b = 2;
c = a + b;
d = a + b;
if (a+1 < c-1)
x = a - b;
else
x = a + b;
printf("%d€n",x);
}
is compiled by
java coins.driver.Driver -S -coins:hirOpt=cf/cpf/dce,hir2c=opt
then following C program will be generated by converting resulting
HIR to C by hir2c.
int main( )
{
int a;
int b;
int c;
int d;
int x;
a = (int )1;
b = (int )2;
if (((int )2) < ((int )2))
{
x = ((a) - (b));
}
else
{
x = ((a) + (b));
}
(&(printf))( (const char * )((("%d€n"))),x);
}
The statement
d = a + b;
is eliminated as a dead code and condition expression is changed to
an expression that can be evaluated as false at compile time.
In the backend part of COINS, code sequence for only else-part
is generated for such if-statement as follows:
.global main
main:
save %sp,-96,%sp
.L18:
mov 1,%i1
mov 2,%i0
.L20:
add %i1,%i0,%o1
.L21:
sethi %hi(string.17),%o0
or %o0,%lo(string.17),%o0
call printf
nop
.L22:
ret
restore
java -coins:regpromote
is specified, it is not required to specify hirOpt=gt because
register promotion for global variables is done in the COINS backend.
In many cases, regpromote option will produce more efficient code than
hirOpt=gt option.
ex. new ConstFolding(lResults).doSubp(lSubpFlow);The method will return a boolean value indicating whether the program has been changed (optimized) as a result of the call to this optimizer method. For more examples, see the basic optimizer driver class coins.opt.Opt.
loopexp // loop expansion
loopif // loop-if expansion
inline // inline expansion
pre // partial redundancy elimination
All of them may change the control flow structure of subprograms.
The loopexp, loopif, and inline optimizer does not require data flow
analysis. The pre optimizer requires data flow analysis.
java coins.driver.Driver -S -coins:hirOpt=loopexp
java coins.driver.Driver -S -coins:hirOpt=loopif
java coins.driver.Driver -S -coins:hirOpt=inline
java coins.driver.Driver -S -coins:hirOpt=pre
4 if number of registers N <= 16
8 if N > 16
as default, but it can be changed by specifying expansion number as a
sub-option in such way as
hirOpt=loopexp.6
Following loops are not expanded:
(1) Outer loop (not an inner-most loop)
(2) Loop including subprogram call
(3) Loop including volatile variable
(4) Non-simple for-loop, that is, a loop having some of following
characteristics
not a for-loop
start condition is null
start condition is not a simple arithmetic comparison expression
for loop control variable
loop control variable is changed in the loop body (not in loop step part)
complexity level of the loop body is large, that is,
if (((R <= 8)&&(E * N > 200))||
((R <= 32)&&(E * N > 300))||
((R > 32)&&(E * N > 600)))
is true, where,
R: number of general registers of the target machine.
E: expansion number
N: the number of executable operators and assign-operators
Statements in the loop body may be changed if reordering does not affect
execution results. For example, following loop
for (i = 0; i < 100; i++) {
sum1 = sum1 + i;
sum2 = sum2 + a[i];
}
will be expanded as follows:
_var5 = 1*7;
_var7 = 1*8;
for (i = 0; i < 100 - _var5; i = i + _var7) {
sum1 = sum1 + i + i + 1 + i + 2 + i + 3 + i + 4 + i + 5
+ i + 6 + i + 7;
sum2 = sum2 + a[i] + a[i+1] + a[i+2] + a[i+3]
+ a[i+4] + a[i+5] + a[i+6] + a[i+7];
}
for (; i < 100; i = i + 1) {
sum1 = sum1 + i;
sum2 = sum2 + a[i];
}
for (i = 0; i < pn; i++) {
lSum = lSum + pa[i];
if (pMode > 0)
lSum = lSum + i;
else
lSum = lSum + i * i;
}
will be transformed as follows:
if (pMode > 0) {
for (i = 0; i < pn; i++) {
lSum = lSum + pa[i];
lSum = lSum + i;
}
}else {
for (i = 0; i < pn; i++) {
lSum = lSum + pa[i];
lSum = lSum + i * i;
}
}
If hirOpt=loopif/loopexp is specified, then the resultant loops in
then-part and else-part will be expanded by the loop expansion optimizer.
(1) Complexity of subprogram body is large
Subprogram having more than 100 HIR nodes are not expanded.
This complexity threshold can be changed by sub-option; for example,
hirOpt=inline.200
will expand subprograms up to 200 HIR nodes.
(2) Call is included in conditional expression of if-statement,
loop-statement, or included in case-selection expression of switch-statement.
(3) Subprogram whose definition is not given in the same compile unit.
Subprograms called before giving its definition can be expanded. Recursive
subprograms are also expanded up to 2 times. For example,
int fact(int p) {
if (p > 0)
return p * fact(p - 1);
else
return 1;
}
will be expanded as follows:
int fact( int p ) {
int _var1, _var3, _var5, _var7, _var9, _var11;
if (p > 0) {
_var1 = p - 1;
if (_var1 > 0) {
_var5 = _var1 - 1;
if (_var5 > 0) {
_var7 = _var5 - 1;
if (_var7 > 0) {
_var9 = _var7 * fact(_var7 - 1);
goto _lab19;
}else {
_var9 = 1;
goto _lab19;
}
_lab19:;
_var11 = _var5 * _var9;
goto _lab22;
}else {
_var11 = 1;
goto _lab22;
}
_lab22:;
_var3 = _var1 * _var11;
goto _lab12;
}else {
_var3 = 1;
goto _lab12;
}
_lab12:;
return p * _var3;
}
else {
return 1;
}
}
locally available: EGen (downward exposed)
After computation, operands are not changed.
available: AvailIn
locally anticipable: AntLoc (upward exposed)
Operands are not set in preceding operations
(before use) in a basic block.
safe: Either anticipable or available.
e-path([b_i ... b_k]) = set of eliminatable computation e included
in b_k, i.e.
{e | e is locally available in b_i and locally anticipable in b_k } &
empty((b_i ... b_k)) & // not computed in intermediate point
e is safe at exit of each node on the path [b_i ... b_k),
where, b_i, ..., b_k are basic block i, ..., basic block k, respectively.
e-path suffix is the maximal suffix of an E-path such that
AntIn * (not AvIn) = true for each node in it.Data flow properties are as follows:
Comp_i : e is locally available in b_i Antloc_i : e is locally anticipable in b_i Transp_i : b_i does not contain definitions of e's operands AvIn_i : e is available at entry of b_i AvOut_i : e is available at exit of b_i AntIn_i : e is anticipable at entry of b_i AntOut_i : e is anticipable at exit of b_i EpsIn_i : entry of b_i is in an e-path suffix EpsOut_i : exit of b_i is in an e-path suffix Redund_i : Occurrence of e in b_i is redundant Insert_i : Insert t_e := e in node b_i Insert_i_j : Insert t_e := e along edge (b_i, b_j) SaIn_i : A Save must be inserted above the entry of b_i SaOut_i : A Save must be inserted above the exit of b_i Save_i : e should be saved in t_e in node b_iwhere, t_e is a temporal variable to hold the value of e.
AvIn_i = PAI_p (AvOut_p) AvOut_i = AvIn_i * Transp_i + Comp_i AntIn_i = AntOut_i * Transp_i + Antloc_i AntOut_i = PAI_s (AntIn_s) EpsIn_i = SIGMA_p (AvOut_p + EpsOut_p) * AntIn_i * (not AvIn_i) EpsOut_i = EpsIn_i * (not Antloc_i) Redund_i = (EpxIn_i + AvIn_i) * Antloc_i Insert_i = (not AvOut_i) * (not EpsOut_i) * PAI_s(EpsIn_s) Insert_i_j = (not AvOut_i) * (not EpsOut_i) * (not Insert_i) * EpsIn_j SaOut_i = SIGMA_s (EpsIn_s + Redund_s + SaIn_s) * AvOut_i SaIn_i = SaOut_i * (not Comp_i) Save_i = SaOut_i * Comp_i * (not Redund_i * Transp_i)where, _s means successor and _p means predecessor.
Save the value of e: computation t_e is inserted before an occurrence of e and the occurrence of e is replaced by t_e (as indicated by Save_i). Insert an evaluation of e: A computation t_e <- e is inserted (as indicated by Insert_i and Insert_i_j). Eliminate a redundant evaluation of e: An occurrence of e is replaced by t_e (as indicated by Redund_i).Before doing partial redundancy elimination, critical edges[3] in control flow graph are removed by preparatory transformation phase (NormalizeHir). A critical edge is an edge that goes from a basic block having multiple successors to a basic block having multiple predecessors. For example,
switch (i) {
case 0:
s = 0;
case 1:
s = s + i;
....
}
will be changed by the preparatory transformation phase to
switch (i) {
case 0:
s = 0;
goto _lab11;
case 1:
{ _lab11:;
s = s + i;
}
......
}
java coins.driver.Driver -S -coins:hirOpt=fromc xxx.c
Most optimizations done by fromc specification are covered
by other HIR and LIR optimizations. The effect of the optimization
in C front is to make slim the HIR representation of source program
so that succeeding processing will be simplified.
coins.flow: Flow analysis used currently in all HIR optimizations
such as loopif/loopexp/inline/cf/cpf/cse/gt/pre.
coins.aflow: Old version flow analysis which is used currently in
loop parallelizer, coarse grain parallelizing module (-coins:mdf).
In building new modules, it is recommended to use coins.flow version
because coins.aflow version may take long compile time and huge
storage space for large subprograms.
flowRoot: instance of FlowRoot (usually passed from Driver).
subpDefinition: instance for SubpDefinition representing
the HIR subtree of a subprogram.
subpFlow: instance of SubpFlow to represent control/data flow information
of specified subprogram.
The instance of HirSubpFlowImpl is required to be made only once
for each subprogram. All control flow information and data flow
information are linked from this instance and if you renew the
instance, then all flow information previously computed will be reset.
coins.flow.SubpFlow lSubpFlow = new HirSubpFlowImpl(flowRoot, subpDefinition);
and after executing it, the instance can be refered by
flowRoot.fSubpFlow
To do control flow analysis, it is necessary to prepare for it by
flowRoot.flow.controlFlowAnal(lSubpFlow);
After executing this statement, methods related to basic blocks such as
cfgIterator(), getEntryBBlock(), getBBlockOfIR(ir.getIndex()), bblockSubtreeIterator(bblock), ...are made available. There are many other methods for control/data flow analysis as they are shown in the interface coins.flow.SubpFlow. After executing controlFlowAnal(lSubpFlow), methods of coins.flow.BBlock interface such as
getPredList(), getSuccList(), getImmediateDominator(), getPostDominatedChildren(), ...are made available. To see the result of control flow analysis, the coding sequence
coins.flow.ShowControlFlow lShow = flowRoot.controlFlow.getShowControlFlow();
lShow.showAll();
will print the result of control flow analysis.
flowRoot.flow.controlFlowAnal(lSubpFlow);
resets previous control flow analysis information and begins to re-compute.
If there is no change in HIR subtree of SubpDefinition instance, then it is
not necessary to re-compute it. It is recommended to avoid it by
following coding sequence:
if (flowRoot.flow.getFlowAnalStateLevel() <
coins.flow.Flow.STATE_CFG_AVAILABLE)
flowRoot.flow.controlFlowAnal(lSubpFlow);
The method finishHir() and setIndexNumbetToAllNodes() of HIR0 interface
will make
getGlowAnalStateLevel() < coins.flow.Flow.STATE_CFG_AVAILABLE)
true so as to inform re-computation is required.
x, y, t, u : variable or register representing an operand.
(variable may be a compound variable such as
array element or structure element.)
op : operator.
def(x) : shows that value of x is defined (value is set).
def(x, y, ...) : shows that values of x, y, ... are defined.
use(x) : shows that x is used.
p(use(x)) : x is used at program point p.
or_all(z) : construct a set by applying or-operation
on all components indicated by z.
and_all(z) : construct a set by applying and-operation
on all components indicated by z.
The data flow analyzer will compute following information according to requests:
Def(B) =
{ p | for some x, p(def(x)) is included in B and after that point
there is no p'(def(x)) in B. }
Kill(B) =
{ p | for some x, p(def(x)) is included in B' (where, B' != B)
and there exists some defining point of x p'(def(x)) in B. }
Reach(B)=
{ p | there is some path from program point p defining x
(that is p(def(x))) to the entry of B such that there is
no p'(def(x)) on that path. }
Reach(B) = or_all( (Def(B') | (Reach(B') - Kill(B')))
for all predecessors B' of B)
Defined(B) =
{ x | x is defined in B. }
Exposed(B) =
{ x | x is used in B and x is not defined in B
before x is used. }
Used(B) =
{x|x is used in B}
EGen(B) =
{ op(x,y) | expression op(x,y) is computed in B and after
that point, neither x nor y are defined in B. }
Thus, the result of op(x,y) is available after B.
EKill(B) =
{ op(x,y) | operand x or y is defined in B and the
expression op(x,y) is not re-evaluated after
that definition in B. }
If t = op(x,y) is killed in B,
then op(t,u) should also be killed in B.
AvailIn(B) =
{ op(x,y) | op(x,y) is computed in every paths to B and
x, y are not defined after the computations
on the paths. }
Thus, the result of op(x,y) can be used without
re-evaluation in B.
AvailOut(B) =
{ op(x,y) | op(x,y) is computed in every paths to the exit of B and
x, y are not defined after the computations
on the paths. }
Thus, op(x,y) can be used without re-evaluation after B.
Following relations hold.
AvailIn(B) = and_all(AvailOut(B') for all predecessors B' of B)
if B is not an entry block;
AvailIn(B) = { } if B is an entry block.
AvailOut(B) = EGen(B) | (AvailIn(B) - EKill(B))
LiveIn(B) =
{ x | x is alive at entry to B, that is, on some path from
entrance point of B to use point of x, x is not defined. }
Thus, x in LiveIn(B) should not be changed until it is used.
LiveOut(B) =
{ x | x is live at exit from B, that is, there is some
path from B to B' where x is in Exposed(B'). }
Following relations hold.
LiveOut(B) = or_all(LiveIn(B') for all successors B' of B
LiveIn(B) = Exposed(B) | (LiveOut(B) - Defined(B))
DefIn(B) =
{ x | x is always defined at entry to B whichever path
may be taken. }
DefIn(B) = and_all(DefOut(B') for all predecessors B' of B)
DefOut(B) =
{ x | x is always defined at exit from B whichever path
may be taken.}
DefOut(B) = Defined(B) | DefIn(B)
Reach(p(use(x))) =
{ p'(def(x)) | there are some paths from p to p' on which
x is not re-defined. }
DefUseList(p(def(x))) =
{ p'(use(x)) | p(def(x)) is included in p'(use(x)). }
UseDefList(p(use(x))) =
{ p'(def(x)) | p'(def(x)) is included in p(use(x)). }
flowRoot.flow.dataFlwoAnal(subpDefinition);
at the first time. This makes coins.flow.SubpFlow methods such as
getDefinedSyms(), getUsedSyms(), ...
available. It also makes coins.flow.BBlock methods such as
getDefIn(), getDefOut(), getRech(), getLiveIn(), getLiveOut(),
getAvailIn(), getAvailOut(), ....
available. available. There are many other methods for accessing data flow
information as shown in the interface SubpFlow. Such methods can be
called via the SubpFlow instance
flowRoot.fSubpFlow
which is prepared by calling dataFlwoAnal(subpDefinition);
if (flowRoot.flow.getFlowAnalStateLevel() <
coins.flow.Flow.STATE_DATA_FLOW_AVAILABLE)
flowRoot.dataFlow = flowRoot.flow.dataFlowAnal(subpDefinition);
The methods finishHir() and setIndexNumbetToAllNodes() of HIR0 interface
will make getFlowAnalStateLevel() as STATE_DATA_UNAVAILABLE, that is, it will
make
getFlowAnalStateLevel() < coins.flow.Flow.STATE_DATA_FLOW_AVAILABLE
true so as to inform re-computation is required.
AssignStmt
Conditional expressions in LoopStmt
Subprogram call
ReturnStmt
which are treated as a statement in the data flow analysis.
Following methods are available for SetRefRepr.
defSym() returns the set of symbols definitely defined.
modSyms() returns the set of symbols that are possibly defined.
useSyms() returns the set of symbols definitely used (referred).
As for expressions, ExpId interface provides following methods:
getOperandSet() returns the set of variables used as leaf operand.
getExpInf().hasCall() returns true if the expression has call.
SubpFlow interface provides following methods for corresponding subprogram:
cfgIterator() traverses all reachable basic blocks of the subprogram.
bblockSubtreeItrator(BBlock pBBlock) returns iterator that traverse top
subtrees of the basic block pBBlock.
Traversed top-subtrees are
LabeledStmt, AssignStmt, ExpStmt, ReturnStmt,
IfStmt, LoopStmt, SwitchStmt
Conditional expression in IfStmt and LoopStmt
Case-selection expression in SwitchStmt
Call subtree (irrespective of contained in ExpStmt or Exp)
bblockStmtIterator(BBlock pBBlock) returns iterator to traverse all
HIR statements in the basic block pBBlock.
bblockNodeIterator(BBlock pBBlock) returns iterator to traverse all
HIR nodes in the basic block pBBlock.
getSetRefReprOfIR(IR pIr) returns SetRefRepr corresponding to pIr
or returns null if pIr has no SetRefRepr instance.
getExpId(IR pIr) returns ExpId corresponding to pIr.
getExpOfTemp(Var pTemp) returns the expression represented by
the temporal variable pTemp.
setOfGlobalVariables() returns the set of global variables appeared.
setOfAddressTakenVariables() returns the set of address taken variables.
getRecordAlias() returns the instance of RecordAlias that is used to
access alias information of the subprogram.
DefUseList and UseDefList are computed by the information of definitely
defined and definitely used relations because if all possibilities are
unconditionally included, define/use lists and use/define lists will become
very large. Possibly defined symbols and possibly used symbols can be get
from defSyms() and useSyms() by using the set of global variables, the
set of address-taken variables, and the set of variables aliased to a variable.
int printf(char*, ...);
int func(int pa[10], int pn);
int ga1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int main()
{
int a = 1, b = 2, c, d;
int i = 0;
int *ptrc, *ptry;
int sum;
int x[10];
int y[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int z[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int zz[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ptrc = &c;
ptry = y;
x[i] = a;
*ptrc = x[i] + 1;
sum = c + func(z, 10);
d = zz[2] + zz[3];
printf(" sum=%d ", sum);
for (i = 0; i < 10; i++) {
d = d + (zz[2] + zz[3]);
d = d + z[i] + z[i];
d = d + zz[i] + zz[i];
sum = ga1[i] + ga1[i];
sum = sum + *ptry;
printf(" *ptry=%d d=%d ", *ptry, d);
ptry = ptry + 1;
sum = sum + z[i] + z[i];
sum = sum + zz[i] + zz[i];
d = d + ga1[i] + ga1[i];
}
d = d + ga1[2] + ga1[2];
printf("\n");
d = d + (zz[2] + zz[3]);
d = d + ga1[2] + ga1[2];
printf("%d %d %d \n", sum, c, d);
return 0;
}
basic blocks are
BBlock 1: statements from the beginning up to "i=0;" of for-statement
BBlock 2: conditional expression "i < 10"
BBlock 3: from "d=d+(zz[2]+zz[3]);" to "d=d+ga1[i]+ga1[i];"
BBlock 4: "i++"
BBlock 5: rest of statements (from "d=d+ga1[2]+ga1[2];" to "return 0;")
and
setOfAddressTakenVariables() = { c, z, y }
Available expressions of basic blocks are
BBlock 1
AvailOut= { zz[2], zz[3], &c, zz[2]+zz[3] }
BBlock 2
AvailIn = { zz[2], zz[3], &c, zz[2]+zz[3] }
AvailOut= { zz[2], zz[3], &c, zz[2]+zz[3], i<10 }
BBlock 3
AvailIn = { zz[2], zz[3], &c, zz[2]+zz[3] }
AvailOut= { zz[2], zz[3], &c, zz[2]+zz[3], i<10, z[i], zz[i], ga1[i] }
BBlock 4
AvailIn = { zz[2], zz[3], &c, zz[2]+zz[3], i<10, z[i], zz[i], ga1[i] }
AvailOut= { zz[2], zz[3], &c, zz[2]+zz[3] }
BBlock 5
AvailIn = { zz[2], zz[3], &c, zz[2]+zz[3], i<10 }
AvailOut= { zz[2], zz[3], &c, zz[2]+zz[3], i<10 }
At the statement "*ptrc = x[i] + 1;", address taken variables are assumed
to be modified.
defSym = { *ptrc }
modSyms= { c, *ptrc, z, ptrc, y }
At the statement "sum = c + func(z, 10);", global variables are assumed
to be modified.
defSym = { sum }
modSyms= { z, ga1, sum }
Thus, after partial redundancy elimination, z[i]+z[i] is re-computed
after subprogram call but z[i]+z[i] is not re-computed (eliminated
as common subexpression), and zz[2]+zz[3] is recorded in a temporal
variable before entering the for-loop and all later occurrences of
zz[2]+zz[3] are replaced by the temporal variable.
SubpFlow lSubpFlow = new HirSubpFlowImpl(flowRoot, subpDefinition);
ControlFlow lControlFlow = flowRoot.flow.controlFlowAnal(lSubpFlow);
DataFlow lDataFlow = flowRoot.flow.dataFlowAnal(lSubpFlow);
RecordAlias lRecordAlias = lSubpFlow.getRecordAlias();
for (Iterator lBBlockIterator = lSubpFlow.cfgIterator();
lBBlockIterator.hasNext(); ) {
BBlock lBBlock = (BBlock)lBBlockIterator.next();
ExpVector lAvailableExp = lBBlock.getAvailIn();
for (BBlockSubtreeIterator lSubtreeIterator
= lSubpFlow.bblockSubtreeIterator(lBBlock);
lSubtreeIterator.hasNext(); ) {
HIR lSubtree = (HIR)lSubtreeIterator.next();
SetRefRepr lSetRefRepr = lSubpFlow.getSetRefReprOfIR(lSubtree);
Set lModSyms = lSetRefRepr.modSyms();
Set lModSymsAlias = fRecordAlias.aliasSymGroup(lModSyms); // Set of
// symbold aliased to some of modified variables.
for (ExpVectorIterator lExpIterator = lAvailableExp.expVectorIterator();
lExpIterator.hasNext(); ) {
ExpId lExpId = nextExpId();
Set lOperands = lExpId.getOperandSet();
if (! lOperands.retailAll(lModSymsAlias).isEmpty()) {
// Treat the expression corresponding to lExpId as unavailable
// because some operand may be changed by the subtree lSubtree.
}
......
}
....
}
....
}
To see the result of data flow analysis, execute following statement:
flowRoot.dataFlow.showSummary();
The amount of printed result may be large for subprograms with hundreds
of statements.
package coins.flow;
import coins.FlowRoot;
import coins.ir.hir.SubpDefinition;
import java.util.Iterator;
public class
MySubpFlow extends HirSubpFlowImpl implements HirSubpFlow
{
ExpVector fTransparent[];
public MySubpFlow(FlowRoot pFlowRoot, SubpDefinition pSubpDefinition)
{
super(pFlowRoot, pSubpDefinition);
} // MySubpFlow
public void
computeTransparent()
{
ExpVector lEKillAll;
ExpVector lTemp1 = expVector();
ExpVector lTemp2 = expVector();
FlowAnalSymVector lDefined;
int lBBlockNum;
fTransparent = new ExpVector[fBBlockCount + 1]; // Get space
// to record transparent vectors for all basic blocks.
for (Iterator lIterator = cfgIterator();
lIterator.hasNext(); ) { // Repeat for each basic block.
BBlock lBBlock = (BBlock)lIterator.next();
if (lBBlock == null)
continue;
lBBlockNum = lBBlock.getBBlockNumber(); // Get basic block number.
fTransparent[lBBlockNum] = expVector(); // Initiate by zero vector.
lEKillAll = lBBlock.getEKillAll(); // Get the cumulative set of
//expressions killed by some statements in this BBlock.
lEKillAll.vectorNot(lTemp1); // lTemp1 is negation of lEKillAll..
// Get the set of defined variables.
lDefined = (FlowAnalSymVector)lBBlock.getDefined();
lTemp2 = lDefined.flowAnalSymToExpVector(); // Change the set to vector.
lTemp1.vectorSub(lTemp2, fTransparent[lBBlockNum]);
// fTransparent[lBBlockNum] = lTemp1 - lTemp2
if (fDbgLevel > 1) // If trace=Flow.2 or more, print the result.
ioRoot.dbgFlow.print(2, "Transparent B"+lBBlockNum,
fTransparent[lBBlockNum].toStringShort());
}
setComputedFlag(DF_TRSNSPARENT); // Set already-computed flag.
} // computeTransparent
/**
* Get the transparent expression for the basic block pBBlock.
* Expressions are represented by ExpId corresponding to the expression.
* @param pBBlock basic block.
* @return expression vector showing transparent expressions.
*/
public ExpVector
getTransparent( BBlock pBBlock )
{
if (! isComputed(DF_TRSNSPARENT)) // If already computed,
computeTransparent(); // do not re-compute but reuse.
return fTransparent[pBBlock.getBBlockNumber()];
} // getTransparent
} // MySubpFlow
In this example, it is necessary to add
public static final int DF_TRANSPARENT = 26;
as a flag number to SubpFlow.java. To use the subclass for extending
the flow analysis capability, write such coding as
SubpFlow lSubpFlow = new MySubpFlow(flowRoot, subpDefinition);
instead of
SubpFlow lSubpFlow = new HirSubpFlowImpl(flowRoot, subpDefinition);
which is shown in the previous example.
x, y, t, u : variable or register representing an operand.
op : operator.
def(x) : shows that value of x is definitely defined.
mod(x) : shows that value of x is possibly defined.
use(x) : shows that x is used.
p(def(x)) : value of x is (definitely) modified (i.e. via assign)
at program point p.
p(mod(x, y, ...)) : value of x, y, ... are modified at program point p
(modified means possibly changed).
p(use(x)) : x is used at program point p.
or_all(z) : construct a set by applying or-operation
on all components indicated by z.
and_all(z) : construct a set by applying and-operation
on all components indicated by z.
The data flow analyzer will compute following information according to requests:
PDef(B) =
{ p | p(mod(x, y, ...)) is included in B and after that point there is
no p' s.t. p'(def(x)) nor p" s.t. p"(def(y)), ... in B. }
DKill(B) =
{ p | p(def(x)) is not included in B and
p'(def(x)) is included in B. }
PReach(B)=
{ p | there is some path from program point p
that modifies some variables x, y, ... (that is, p(mod(x, y, ...)))
to the entry of B such that there is no p'(def(x)) or no p''(def(y))
or ... on that path. }
PReach(B) = or_all( (PDef(B') | (PReach(B') - DKill(B')))
for all predecessors B' of B)
DDefined(B) =
{ x | x is definitely modified in B. }
PDefined(B) =
{ x | x is posibly modified in B. }
PExposed(B) =
{ x | x is possibly used in B and x is not definitely set in B
before x is used. }
PUsed(B) = {x|x is possibly used in B}
DEGen(B) =
{ op(x,y) | expression op(x,y) is computed in B and after
that point, neither x nor y are possibly set in B. }
Thus, the result of op(x,y) is available after B.
PEKill(B) =
{ op(x,y) | operand x or y is possibly modified in B and the
expression op(x,y) is not re-evaluated after
that definition in B. }
If t = op(x,y) is killed in B,
then op(t,u) should also be killed in B.
DAvailIn(B) =
{ op(x,y) | op(x,y) is computed in every paths to B and
x, y are not modified after the computations
on the paths. }
Thus, the result of op(x,y) can be used without
re-evaluation in B.
DAvailOut(B) =
{ op(x,y) | op(x,y) is computed in every paths to the exit of B and
x, y are not modified after the computations
on the paths. }
Thus, op(x,y) can be used without re-evaluation after B.
Following relations hold.
DAvailIn(B) = and_all(DAvailOut(B') for all predecessors B'
of B) if B is not an entry block;
DAvailIn(B) = { } if B is an entry block.
DAvailOut(B) = DEGen(B) | (DAvailIn(B) - PEKill(B))
PLiveIn(B) =
{ x | x is alive at entry to B, that is, on some path from
entrance point of B to use point of x, x is not definitely set. }
Thus, x in PLiveIn(B) should not be changed until it is used.
PLiveOut(B) =
{ x | x is live at exit from B, that is, there is some
path from B to B' where x is in PExposed(B'). }
Following relations hold.
PLiveOut(B) = or_all(PLiveIn(B') for all successors B' of B
PLiveIn(B) = PExposed(B) | (PLiveOut(B) - DDefined(B))
DDefIn(B) =
{ x | x is always defined at entry to B whichever path
may be taken. }
DDefIn(B) = and_all(DDefOut(B') for all predecessors B' of B)
DDefOut(B) =
{ x | x is always defined at exit from B whichever path
may be taken.}
DDefOut(B) = DDefined(B) | DefIn(B)
ex. find("Def", lBBlock);
(2) get
ex. get("Def", lBBlock);
(3) put
ex. put("Def", lBBlock, lDefVector);
(4) getRaw
ex. getRaw("Def", lBBlock)
In the following code snippet, the Reach vector for the exit BBlock of the
SubpDefinition variable subpDef is going to be stored in the local variable
lReach.
// Establishes the map between the analysis names and the analyzer methods
// that actually do the analysis.
// A key of this map together with the arguments of the associated
// analyzer class methods forms a piece of information that supports the
// automatic analysis mechanism.
// This method will be called only twice during the program life; once
// for HIR and once for LIR.
FlowResults.putRegClasses(new RegisterFlowAnalClasses());
// Instantiate a FlowResults map.
FlowResults lResults = flow.results();
// Instantiate a SubpFlow object, with FlowResults object passed as an
// argument to the factory method.
SubpFlow lSubpFlow = flow.subpFlow(subpDef, lResults);
// Performs control flow analysis.
// Control flow analysis does not support the automatic flow analysis
// mechanism and must be called explicitly.
lSubpFlow.controlFlowAnal();
// Collects some basic information that does not require a complex
// algorithm.
// Some pieces of information obtained here ARE part of the automatic
// analysis picture, but some are not, so I call it explicitly.
lSubpFlow.initiateDataFlow();
// Finds the Reach vector for each of the BBlocks that belong to lSubpFlow.
// There is no need to call lSubpFlow.getExitBBlock().findDef() or
// lSubpFlow.getExitBBlock().findKill() (or lSubpFlow.findReach()) since
// they are called automatically (automatic analysis).
lReach = lResults.get("Reach", lSubpFlow.getExitBBlock());
// OR lReach = lSubpFlow.getExitBBlock().getReach();
...
The following command invokes HIR flow analyzer.
java coins.driver.Driver -S -coins:hirAnal,trace=Flow.2
The trace option is attached to see the result of the flow analysis.
There are 2 levels of analysis:
Also, there are 2 modes of analysis for both AliasAnalLevel1 and
AliasAnalLevel2:
-coins:alias=opt
There are fine computation mode and coarse computation mode in the alias
analysis. The fine computation mode will consume much time and memory.
For large subprograms (more than 1000 HIR nodes), the coarse computation mode
is automatically adopted. In the fine computation mode, size of set
showing aliased symbols will be small compared to the coarse computation mode.
5.6.2. How to Use
The alias analyzer is automatically called in process of data flow analysis.
The result of alyas analysis can be used by calling methods of RecordAlias
such as mayAlias, aliasSyms, and aliasSymGroup. Following is an example of
using alias information.
RecordAlias lRecordAlias = flowRoot.subpFlow.getRecordAlias();
....
if(lRecordAlias.mayAlias(x, y)) {
// Assume y may be changed when x is changed.
....
}
Set lSetOfVariablesAliased = lRecordAlias.aliasSyms(x);
where, x, y are variables.
5.7. References
[1] Morel, Etienne and Renvoise, Claude: Global optimization by suppression
of partial redundancies, CACM, Vol. 22, No. 2, pp.96-103 (Feb. 1979).
[2] Muchnick, Steven S.: Advanced Compiler Design and Implementation,
Morgan Kaufmann Publishers (1997).
[3] Nakata, Ikuo: Compiler Construction and Optimization, Asakura Shoten, (1999).
[4] Dhamdhere, Dhananjay M.: E-path_PRE - Partial redundancy elimination made
easy, ACM SIGPLAN Notices, Vol. 37, No. 8, pp. 53-65 (Aug. 2002).
Contacts: c_o_n_t_a_c_t_@_c_o_i_n_s_-_p_r_o_j_e_c_t_._o_r_g (remove "_")
Coins Internal Information
Copyright 2002 by COINS-project
Last modified:
Fri Mar 31 18:48:11 JST 2006