Skip to content

Commit

Permalink
- Optimized ExpressionSimplify.simplify
Browse files Browse the repository at this point in the history
- Optimized VarTransform.mo (do only perform union every 7th addition.)
- made DAEDump.dumpInlineTypeStr public
- Improved Expression.isConst (now also returns true for constant CALL expressions)
- Improved Expression.containFunctioncall (now also checks ASUB)
- Improved Values.valString, now stores and resets Print buffer using handle.
- Added VarTransform.addReplacementLst

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16384 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Leonardo Laguna committed Jun 18, 2013
1 parent f9e615b commit a133bca
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/DAEDump.mo
Expand Up @@ -1641,7 +1641,7 @@ algorithm
end match;
end dumpParallelismStr;

protected function dumpInlineTypeStr
public function dumpInlineTypeStr
input DAE.InlineType inlineType;
output String str;
algorithm
Expand Down
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -6362,6 +6362,8 @@ algorithm

case (DAE.RELATION(exp1=e1,exp2=e2),_) then isConstWork(e1,isConstWork(e2,true));

case (DAE.CALL(expLst = ae),_) then isConstWorkList(ae,true);

case (DAE.ARRAY(array = ae),_) then isConstWorkList(ae,true);

case (DAE.MATRIX(matrix = matrix),_)
Expand Down Expand Up @@ -7105,6 +7107,14 @@ algorithm
then
true;

// asub
case(DAE.ASUB(e1,elst))
equation
blst = List.map(e1::elst, containVectorFunctioncall);
res = Util.boolOrList(blst);
then
res;

// anything else
case (_) then false;

Expand Down
214 changes: 122 additions & 92 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -221,85 +221,12 @@ algorithm
DAE.Dimension dim;
Options options;
DAE.CallAttributes attr;

// noEvent propagated to relations and event triggering functions
case ((DAE.CALL(path=Absyn.IDENT("noEvent"),expLst={e}),(b,options)))
equation
(e1,_) = simplify1WithOptions(Expression.stripNoEvent(e),options);
e2 = Expression.addNoEventToRelations(e1);
e3 = Expression.addNoEventToEventTriggeringFunctions(e2);
// TODO: Do this better?
then
((e3,(b,options)));

// der(-v) -> -der(v)
case ((DAE.CALL(path=Absyn.IDENT("der"), expLst={DAE.UNARY(DAE.UMINUS(tp),e1 as DAE.CREF(componentRef=_))},attr=attr), (b, options)))
then
((DAE.UNARY(DAE.UMINUS(tp),DAE.CALL(Absyn.IDENT("der"),{e1},attr)),(true,options)));
case ((DAE.CALL(path=Absyn.IDENT("der"), expLst={DAE.UNARY(DAE.UMINUS_ARR(tp),e1 as DAE.CREF(componentRef=_))},attr=attr), (b, options)))
then
((DAE.UNARY(DAE.UMINUS_ARR(tp),DAE.CALL(Absyn.IDENT("der"),{e1},attr)),(true,options)));

case ((e as DAE.CALL(path=Absyn.IDENT("pre"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
then
((e,(b,options)));
case ((e as DAE.CALL(path=Absyn.IDENT("change"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
then
((e,(b,options)));
case ((e as DAE.CALL(path=Absyn.IDENT("edge"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
then
((e,(b,options)));

case ((e1 as DAE.CALL(path=Absyn.IDENT("pre"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
equation
b2 = Expression.isConst(exp);
e1 = Util.if_(b2,e,e1);
then
((e1,(b or b2,options)));
case ((e1 as DAE.CALL(path=Absyn.IDENT("change"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
equation
b2 = Expression.isConst(exp);
e1 = Util.if_(b2,DAE.BCONST(false),e1);
then
((e1,(b or b2,options)));
case ((e1 as DAE.CALL(path=Absyn.IDENT("edge"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
equation
b2 = Expression.isConst(exp);
e1 = Util.if_(b2,DAE.BCONST(false),e1);
then
((e1,(b or b2,options)));

// move pre inside
case ((DAE.CALL(path=Absyn.IDENT("pre"), expLst={e}), (b, options)))
equation
((e,b2)) = Expression.traverseExpTopDown(e,preCref,false);
then
((e,(b or b2,options)));
case ((DAE.CALL(path=Absyn.IDENT("change"), expLst={e}), (b, options)))
equation
((e,b2)) = Expression.traverseExpTopDown(e,changeCref,false);
then
((e,(b or b2,options)));
case ((DAE.CALL(path=Absyn.IDENT("edge"), expLst={e}), (b, options)))
equation
((e,b2)) = Expression.traverseExpTopDown(e,edgeCref,false);
then
((e,(b or b2,options)));

// normal (pure) call
case ((e as DAE.CALL(expLst=expl, attr=DAE.CALL_ATTR(isImpure=false)),(_,options)))
equation
true = Expression.isConstWorkList(expl, true);
e2 = simplifyBuiltinConstantCalls(e);
then
((e2,(true,options)));

// simplify some builtin calls, like cross, etc
case ((e as DAE.CALL(attr=DAE.CALL_ATTR(builtin = true)),(_,options)))
// component references
case ((DAE.CREF(componentRef = c_1 as DAE.CREF_IDENT(idn,_,s),ty=t),(_,options)))
equation
e2 = simplifyBuiltinCalls(e);
exp1 = simplifyCref(c_1,t);
then
((e2,(true,options)));
((exp1,(true,options)));

// simplify size operator
case ((e as DAE.SIZE(exp=e1,sz=SOME(e2)),(_,options)))
Expand All @@ -324,14 +251,6 @@ algorithm
e = simplifyCast(e,tp);
then ((e,(true,options)));

// simplify identity
case ((DAE.CALL(path = Absyn.IDENT(name = "identity"), expLst = {DAE.ICONST(n)}),(_,options)))
equation
matrix = simplifyIdentity(1,n);
e = DAE.ARRAY(DAE.T_ARRAY(DAE.T_INTEGER_DEFAULT,{DAE.DIM_INTEGER(n),DAE.DIM_INTEGER(n)},DAE.emptyTypeSource),
false,matrix);
then ((e,(true,options)));

// MetaModelica builtin operators are calls
case ((e,(_,options)))
equation
Expand Down Expand Up @@ -414,19 +333,17 @@ algorithm
e = simplifyIfExp(e1,e2,e3);
then ((e,(true,options)));

// component references
case ((DAE.CREF(componentRef = c_1 as DAE.CREF_IDENT(idn,_,s),ty=t),(_,options)))
equation
exp1 = simplifyCref(c_1,t);
then
((exp1,(true,options)));

case ((DAE.REDUCTION(reductionInfo,e1,riters),(_,options)))
equation
(riters,b2) = simplifyReductionIterators(riters, {}, false);
exp1 = DAE.REDUCTION(reductionInfo,e1,riters);
then ((simplifyReduction(exp1,b2),(true,options)));

case(inTpl as (DAE.CALL(path=_),(b,options)))
equation
tpl = simplifyWorkCall(inTpl);
then tpl;

// Look for things we really *should* have simplified, but only if we did not modify anything!
case ((e,(false,_)))
equation
Expand All @@ -442,6 +359,119 @@ algorithm
end matchcontinue;
end simplifyWork;

public function simplifyWorkCall
"help function to simplifyWork"
input tuple<DAE.Exp,tuple<Boolean,Options>> inTpl;
output tuple<DAE.Exp,tuple<Boolean,Options>> tpl;
algorithm
tpl := matchcontinue (inTpl)
local
Integer n,i;
DAE.Exp e,exp,e1,e_1,e2,e3,exp1;
Type t,tp;
Boolean b,b2;
String idn,str;
list<DAE.Exp> expl,matrix,subs;
list<Subscript> s;
ComponentRef c_1;
Operator op;
Integer index_;
Option<tuple<DAE.Exp,Integer,Integer>> isExpisASUB;
DAE.ReductionInfo reductionInfo;
DAE.ReductionIterators riters;
DAE.Dimensions dims;
DAE.Dimension dim;
Options options;
DAE.CallAttributes attr;
// noEvent propagated to relations and event triggering functions
case ((DAE.CALL(path=Absyn.IDENT("noEvent"),expLst={e}),(b,options)))
equation
(e1,_) = simplify1WithOptions(Expression.stripNoEvent(e),options);
e2 = Expression.addNoEventToRelations(e1);
e3 = Expression.addNoEventToEventTriggeringFunctions(e2);
// TODO: Do this better?
then
((e3,(b,options)));

// der(-v) -> -der(v)
case ((DAE.CALL(path=Absyn.IDENT("der"), expLst={DAE.UNARY(DAE.UMINUS(tp),e1 as DAE.CREF(componentRef=_))},attr=attr), (b, options)))
then
((DAE.UNARY(DAE.UMINUS(tp),DAE.CALL(Absyn.IDENT("der"),{e1},attr)),(true,options)));
case ((DAE.CALL(path=Absyn.IDENT("der"), expLst={DAE.UNARY(DAE.UMINUS_ARR(tp),e1 as DAE.CREF(componentRef=_))},attr=attr), (b, options)))
then
((DAE.UNARY(DAE.UMINUS_ARR(tp),DAE.CALL(Absyn.IDENT("der"),{e1},attr)),(true,options)));

case ((e as DAE.CALL(path=Absyn.IDENT("pre"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
then
((e,(b,options)));
case ((e as DAE.CALL(path=Absyn.IDENT("change"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
then
((e,(b,options)));
case ((e as DAE.CALL(path=Absyn.IDENT("edge"), expLst={DAE.CREF(componentRef=_)}), (b, options)))
then
((e,(b,options)));

case ((e1 as DAE.CALL(path=Absyn.IDENT("pre"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
equation
b2 = Expression.isConst(exp);
e1 = Util.if_(b2,e,e1);
then
((e1,(b or b2,options)));
case ((e1 as DAE.CALL(path=Absyn.IDENT("change"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
equation
b2 = Expression.isConst(exp);
e1 = Util.if_(b2,DAE.BCONST(false),e1);
then
((e1,(b or b2,options)));
case ((e1 as DAE.CALL(path=Absyn.IDENT("edge"), expLst={e as DAE.ASUB(exp = exp)}), (b, options)))
equation
b2 = Expression.isConst(exp);
e1 = Util.if_(b2,DAE.BCONST(false),e1);
then
((e1,(b or b2,options)));

// move pre inside
case ((DAE.CALL(path=Absyn.IDENT("pre"), expLst={e}), (b, options)))
equation
((e,b2)) = Expression.traverseExpTopDown(e,preCref,false);
then
((e,(b or b2,options)));
case ((DAE.CALL(path=Absyn.IDENT("change"), expLst={e}), (b, options)))
equation
((e,b2)) = Expression.traverseExpTopDown(e,changeCref,false);
then
((e,(b or b2,options)));
case ((DAE.CALL(path=Absyn.IDENT("edge"), expLst={e}), (b, options)))
equation
((e,b2)) = Expression.traverseExpTopDown(e,edgeCref,false);
then
((e,(b or b2,options)));

// simplify identity
case ((DAE.CALL(path = Absyn.IDENT(name = "identity"), expLst = {DAE.ICONST(n)}),(_,options)))
equation
matrix = simplifyIdentity(1,n);
e = DAE.ARRAY(DAE.T_ARRAY(DAE.T_INTEGER_DEFAULT,{DAE.DIM_INTEGER(n),DAE.DIM_INTEGER(n)},DAE.emptyTypeSource),
false,matrix);
then ((e,(true,options)));

// normal (pure) call
case ((e as DAE.CALL(expLst=expl, attr=DAE.CALL_ATTR(isImpure=false)),(_,options)))
equation
true = Expression.isConstWorkList(expl, true);
e2 = simplifyBuiltinConstantCalls(e);
then
((e2,(true,options)));

// simplify some builtin calls, like cross, etc
case ((e as DAE.CALL(attr=DAE.CALL_ATTR(builtin = true)),(_,options)))
equation
e2 = simplifyBuiltinCalls(e);
then
((e2,(true,options)));
end matchcontinue;
end simplifyWorkCall;

protected function preCref
input tuple<DAE.Exp,Boolean> iExp;
output tuple<DAE.Exp,Boolean,Boolean> oExp;
Expand Down
8 changes: 3 additions & 5 deletions Compiler/FrontEnd/ValuesUtil.mo
Expand Up @@ -2003,14 +2003,12 @@ public function valString "function: valString
input Value inValue;
output String outString;
protected
String oldBuffer;
Integer handle;
algorithm
oldBuffer := Print.getString();
Print.clearBuf();
handle := Print.saveAndClearBuf();
valString2(inValue);
outString := Print.getString();
Print.clearBuf();
Print.printBuf(oldBuffer);
Print.restoreBuf(handle);
end valString;

public function valString2 "function: valString
Expand Down

0 comments on commit a133bca

Please sign in to comment.