Skip to content

Commit

Permalink
- Simplify ew array division/multiplication ({1,2}./{2,2}={0.5,1})
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11190 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 23, 2012
1 parent 2ea4635 commit 5656a89
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 28 deletions.
79 changes: 62 additions & 17 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -4990,6 +4990,8 @@ algorithm

case (DAE.ASUB(exp=e,sub=ae),_) then isConstWorkList(ae,isConstWork(e,true));

case (DAE.TSUB(exp=e),_) then isConstWork(e,true);

case (DAE.SIZE(exp=e,sz=NONE()),_) then isConstWork(e,true);

case (DAE.SIZE(exp=e1,sz=SOME(e2)),_) then isConstWork(e1,isConstWork(e2,true));
Expand All @@ -5003,6 +5005,54 @@ algorithm
end match;
end isConstWork;

protected function isConstValueWork
"Returns true if an expression is a constant value"
input DAE.Exp inExp;
input Boolean res;
output Boolean outBoolean;
algorithm
outBoolean := match (inExp,res)
local
Boolean res;
Operator op;
DAE.Exp e,e1,e2;
Type t;
list<DAE.Exp> ae;
list<list<DAE.Exp>> matrix;

case (_,false) then false;
case (DAE.ICONST(integer = _),_) then true;
case (DAE.RCONST(real = _),_) then true;
case (DAE.BCONST(bool = _),_) then true;
case (DAE.SCONST(string = _),_) then true;
case (DAE.ENUM_LITERAL(name = _),_) then true;

/* A bit torn if we should simplify ranges or not */
case (DAE.RANGE(exp=e1,expOption=NONE(),range=e2),_) then isConstWork(e1,isConstWork(e2,true));
case (DAE.RANGE(exp=e,expOption=SOME(e1),range=e2),_) then isConstWork(e,isConstWork(e1,isConstWork(e2,true)));

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

case (DAE.MATRIX(matrix = matrix),_)
equation
res = List.fold(matrix,isConstValueWorkList,true);
then res;

case (DAE.TUPLE(PR = ae),_) then isConstWorkList(ae,true);

else false;

end match;
end isConstValueWork;

public function isConstValue
"Returns true if an expression is a constant value (not a composite operation)"
input DAE.Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean := isConstValueWork(inExp,true);
end isConstValue;

public function isConstWorkList
"Returns true if a list of expressions is constant"
input list<DAE.Exp> inExps;
Expand All @@ -5018,25 +5068,20 @@ algorithm
end match;
end isConstWorkList;

protected function isConstMatrixExpList
"Helper to isConst. The input is the same as a DAE.MATRIX stores."
input list<tuple<DAE.Exp,Boolean>> inLst;
output Boolean outBoolean;
algorithm
outBoolean := List.mapAllValueBool(inLst,isConstMatrixExp,true);
end isConstMatrixExpList;

protected function isConstMatrixExp
"Helper to isConst. The input is the same as a DAE.MATRIX stores."
input tuple<DAE.Exp,Boolean> inTpl;
public function isConstValueWorkList
"Returns true if a list of expressions is a constant value"
input list<DAE.Exp> inExps;
input Boolean inBoolean;
output Boolean outBoolean;
replaceable type TypeA subtypeof Any;
protected
DAE.Exp exp;
algorithm
exp := Util.tuple21(inTpl);
outBoolean := isConst(exp);
end isConstMatrixExp;
outBoolean := match (inExps,inBoolean)
local
DAE.Exp e; list<DAE.Exp> exps;
case (_,false) then false;
case ({},_) then true;
case (e::exps,_) then isConstValueWorkList(exps,isConstValueWork(e,true));
end match;
end isConstValueWorkList;

public function isNotConst
"function isNotConst
Expand Down
7 changes: 6 additions & 1 deletion Compiler/FrontEnd/ExpressionDump.mo
Expand Up @@ -296,7 +296,12 @@ algorithm
then
s;
case (DAE.MUL_ARR(ty = _)) then " *<MUL_ARRAY> ";
case (DAE.DIV_ARR(ty = _)) then " / ";
case (DAE.DIV_ARR(ty = t))
equation
ts = typeString(t);
s = stringAppendList({" /<DIV_ARR><", ts, "> "});
then
s;
case (DAE.POW_ARR(ty = _)) then " ^ ";
case (DAE.POW_ARR2(ty = _)) then " ^ ";
case (DAE.MUL_ARRAY_SCALAR(ty = _)) then " *<MUL_ARRAY_SCALAR> ";
Expand Down
37 changes: 30 additions & 7 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -127,7 +127,7 @@ algorithm
DAE.Exp e,res,exp,e1_1,exp_1,e1,e_1,e2,e2_1,e3_1,e3,sub,exp1;
Type t,tp;
Boolean b,b1,remove_if,tpl,builtin,b2;
Ident idn;
String idn,str;
list<DAE.Exp> exps,exps_1,expl,matrix;
list<Subscript> s;
ComponentRef c_1;
Expand Down Expand Up @@ -276,6 +276,17 @@ algorithm
exp1 = DAE.REDUCTION(reductionInfo,e1,riters);
then ((simplifyReduction(exp1,b2),true));

/*
// Look for things we really *should* have simplified
case ((e,_))
equation
true = Expression.isConst(e);
false = Expression.isConstValue(e);
str = ExpressionDump.printExpStr(e);
Error.addSourceMessage(Error.INTERNAL_ERROR, {str}, Absyn.dummyInfo);
then fail();
*/

// anything else
else inTpl;
end matchcontinue;
Expand Down Expand Up @@ -1297,16 +1308,16 @@ algorithm
then
a1;

case (e1,DAE.MUL_ARR(ty = _),e2)
case (e1,op as DAE.MUL_ARR(ty = _),e2)
equation
tp = Expression.typeof(e1);
a1 = simplifyVectorBinary(e1, DAE.MUL(tp), e2);
a1 = simplifyVectorBinary(e1, op, e2);
then a1;

case (e1,DAE.DIV_ARR(ty = _),e2)
case (e1,op as DAE.DIV_ARR(ty = _),e2)
equation
tp = Expression.typeof(e1);
a1 = simplifyVectorBinary(e1, DAE.DIV(tp), e2);
a1 = simplifyVectorBinary(e1, op, e2);
then a1;

case (e1,DAE.POW_ARR(ty = _),e2)
Expand Down Expand Up @@ -1727,7 +1738,7 @@ protected function simplifyVectorBinary
input DAE.Exp inExp3;
output DAE.Exp outExp;
algorithm
outExp := matchcontinue (inExp1,inOperator2,inExp3)
outExp := match (inExp1,inOperator2,inExp3)
local
Type tp1,tp2;
Boolean scalar1,scalar2;
Expand All @@ -1751,7 +1762,7 @@ algorithm
op2 = removeOperatorDimension(op);
then
DAE.ARRAY(tp1,scalar1,(DAE.BINARY(e1,op2,e2) :: es_1));
end matchcontinue;
end match;
end simplifyVectorBinary;

protected function simplifyMatrixBinary
Expand Down Expand Up @@ -4044,6 +4055,18 @@ algorithm outop := match(inop)
b = DAEUtil.expTypeArray(ty2);
op = Util.if_(b, DAE.SUB_ARR(ty2), DAE.SUB(ty2));
then op;
case DAE.DIV_ARR(ty=ty1)
equation
ty2 = Expression.unliftArray(ty1);
b = DAEUtil.expTypeArray(ty2);
op = Util.if_(b, DAE.DIV_ARR(ty2), DAE.DIV(ty2));
then op;
case DAE.MUL_ARR(ty=ty1)
equation
ty2 = Expression.unliftArray(ty1);
b = DAEUtil.expTypeArray(ty2);
op = Util.if_(b, DAE.MUL_ARR(ty2), DAE.MUL(ty2));
then op;
end match;
end removeOperatorDimension;

Expand Down
6 changes: 3 additions & 3 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -13061,10 +13061,10 @@ algorithm
case (_, _, aboper, DAE.PROP(type1,const1), exp1, DAE.PROP(type2,const2), exp2, _, _, _, _, _, _, _)
equation
(opList, type1,exp1,type2,exp2) = operatorsBinary(aboper, type1, exp1, type2, exp2);
(oper, {exp1,exp2}, otype) = deoverload(opList, {(exp1,type1), (exp2,type2)}, inPre, inInfo);
(oper, {exp1,exp2}, otype) = deoverload(opList, {(exp1,type1), (exp2,type2)}, inPre, inInfo);
const = Types.constAnd(const1, const2);
exp = replaceOperatorWithFcall(AbExp, exp1,oper,SOME(exp2), const);
(exp,_) = ExpressionSimplify.simplify(exp);
exp = replaceOperatorWithFcall(AbExp, exp1,oper,SOME(exp2), const);
(exp,_) = ExpressionSimplify.simplify(exp);
prop = DAE.PROP(otype,const);
warnUnsafeRelations(inEnv,AbExp,const, type1,type2,exp1,exp2,oper,inPre);
then
Expand Down

0 comments on commit 5656a89

Please sign in to comment.