Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Add support for inline of some if-statements #2076

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions Compiler/BackEnd/Differentiate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -685,21 +685,30 @@ algorithm
then (res, functionTree);

// differentiate tsub
case DAE.TSUB(exp=e1, ix=i, ty=tp) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-TSUB\nDifferentiate exp: " + se1);
case DAE.TSUB(exp=e1, ix=i, ty=tp)
algorithm
(res1, functionTree) := differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);

(res1, functionTree) = differentiateExp(e1, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
if not referenceEq(e1, res1) then
res := DAE.TSUB(res1, i, tp);
(res,_) := ExpressionSimplify.simplify1(res);
else
res := inExp;
end if;
then (res, functionTree);

res = DAE.TSUB(res1, i, tp);
(res,_) = ExpressionSimplify.simplify1(res);
//(res,_) = ExpressionSimplify.simplify(res);

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);
// differentiate tsub
case e1 as DAE.RSUB()
algorithm
(res1, functionTree) := differentiateExp(e1.exp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1);
if not referenceEq(e1.exp, res1) then
e1.exp := res1;
(e1,_) := ExpressionSimplify.simplify1(e1);
end if;
then (e1, functionTree);

// differentiate tuple
// differentiate tuple
case DAE.TUPLE(PR=expl) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-TUPLE\nDifferentiate exp: " + se1);
Expand Down
6 changes: 2 additions & 4 deletions Compiler/BackEnd/ExpressionSolve.mo
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ algorithm
varexp := Expression.expDer(varexp);
cr := ComponentReference.crefPrefixDer(cr);
end if;

if (Types.isIntegerOrRealOrSubTypeOfEither(Expression.typeof(e1)) and Types.isIntegerOrRealOrSubTypeOfEither(Expression.typeof(e2))) then
(e1, e2) := preprocessingSolve(e1, e2, varexp, SOME(shared.functionTree), NONE(), 0, false);
end if;
Expand Down Expand Up @@ -915,7 +914,6 @@ algorithm
(factorWithX1, factorWithoutX1) := List.split1OnTrue(f1, expHasCref, inExp3);
pWithX1 := makeProductLstSort(factorWithX1);
pWithoutX1 := makeProductLstSort(factorWithoutX1);

f2 := Expression.expandFactors(inExp2);
(factorWithX2, factorWithoutX2) := List.split1OnTrue(f2, expHasCref, inExp3);
(pWithX2,_) := ExpressionSimplify.simplify1(makeProductLstSort(factorWithX2));
Expand Down Expand Up @@ -989,7 +987,7 @@ algorithm
if expand then
(cr, b) := Expression.expOrDerCref(inExp3);
if b then
(lhs, rhs) := Expression.allTermsForCref(inExp1, cr, Expression.Expression.expHasDerCref);
(lhs, rhs) := Expression.allTermsForCref(inExp1, cr, Expression.expHasDerCref);
else
(lhs, rhs) := Expression.allTermsForCref(inExp1, cr, Expression.expHasCrefNoPreOrStart);
end if;
Expand Down Expand Up @@ -1748,7 +1746,7 @@ algorithm
false = hasOnlyFactors(inExp1,inExp2);
e = Expression.makeDiff(inExp1,inExp2);
(e,_) = ExpressionSimplify.simplify1(e);
//print("\n\ne: ");print(ExpressionDump.printExpStr(e));
//print("\ne: ");print(ExpressionDump.printExpStr(e));
dere = Differentiate.differentiateExpSolve(e, cr, functions);
//print("\nder(e): ");print(ExpressionDump.printExpStr(dere));
(dere,_) = ExpressionSimplify.simplify(dere);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/DAEDump.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ public function ppStmtListStr "
Helper function to pp_stmt_str
"
input list<DAE.Statement> inAlgorithmStatementLst;
input Integer inInteger;
input Integer inInteger=0;
output String outString;
algorithm
outString:=
Expand Down
174 changes: 45 additions & 129 deletions Compiler/FrontEnd/Expression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2936,123 +2936,35 @@ public function factors
algorithm
// TODO: Remove this listReverse as it is pointless.
// It transforms a*b to b*a, but the testsuite expects this :(
outExpLst := listReverse(factorsWork(inExp,{},false,false));
outExpLst := listReverse(factorsWork(inExp,{},false));
end factors;

protected function factorsWork
"Returns the factors of the expression if any as a list of expressions"
input DAE.Exp inExp;
input list<DAE.Exp> inAcc;
input Boolean noFactors "Decides if the default is the empty list or not";
input output list<DAE.Exp> acc;
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
output list<DAE.Exp> outExpLst;
algorithm
outExpLst := match (inExp,inAcc,noFactors,doInverseFactors)
acc := match inExp
local
DAE.Exp e1,e2,e;
ComponentRef cr;
Real r;
Boolean b;
list<DAE.Exp> acc;

case (DAE.BINARY(exp1 = e1,operator = DAE.MUL(),exp2 = e2),acc,_,_)
equation
acc = factorsWork(e1,acc,true,doInverseFactors);
acc = factorsWork(e2,acc,true,doInverseFactors);
then acc;
case (DAE.BINARY(exp1 = e1,operator = DAE.DIV(ty = DAE.T_REAL()),exp2 = e2),acc,_,_)
equation
acc = factorsWork(e1,acc,true,doInverseFactors);
acc = factorsWork(e2,acc,true,not doInverseFactors);
case DAE.BINARY(exp1 = e1,operator = DAE.MUL(),exp2 = e2)
algorithm
acc := factorsWork(e1,acc,doInverseFactors);
acc := factorsWork(e2,acc,doInverseFactors);
then acc;
case (DAE.CREF(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.BINARY(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.ICONST(integer = 1),acc,_,_)
case DAE.BINARY(exp1 = e1,operator = DAE.DIV(ty = DAE.T_REAL()),exp2 = e2)
algorithm
acc := factorsWork(e1,acc,doInverseFactors);
acc := factorsWork(e2,acc,not doInverseFactors);
then acc;
case (DAE.ICONST(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.RCONST(real = r),acc,_,_)
equation
b = not realEq(r,1.0);
e = if b and doInverseFactors then inverseFactors(inExp) else inExp;
acc = List.consOnTrue(b, e, acc);
case DAE.ICONST(integer = 1)
then acc;
case (DAE.SCONST(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.UNARY(),acc,_,_) // factor(-(x*y)) is -(x*y) ??
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.LUNARY(),acc,_,_)
then inExp::acc;
case (DAE.IFEXP(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.CALL(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.RECORD(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.RECORD(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.PARTEVALFUNCTION(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.ARRAY(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.MATRIX(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.RANGE(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.CAST(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.ASUB(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.TSUB(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.SIZE(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (DAE.REDUCTION(),acc,_,_)
equation
e = if doInverseFactors then inverseFactors(inExp) else inExp;
then e::acc;
case (e,acc,true,_)
equation
e = if doInverseFactors then inverseFactors(e) else e;
then e::acc;
case (_,acc,false,_)
case DAE.RCONST(real = 1.0)
then acc;
// case DAE.UNARY() // factor(-(x*y)) is -(x*y) ??
else (if doInverseFactors then inverseFactors(inExp) else inExp) :: acc;
end match;
end factorsWork;

Expand Down Expand Up @@ -3107,53 +3019,51 @@ algorithm
// TODO: Remove this listReverse as it is pointless.
// It transforms a*b to b*a, but the testsuite expects this :(
// issue with expEqual(a*b,b*a) return false
outExpLst := listReverse(expandFactorsWork(inExp,{},false,false));
outExpLst := listReverse(expandFactorsWork(inExp,{},false));
end expandFactors;

protected function expandFactorsWork
"Returns the factors of the expression if any as a list of expressions"
input DAE.Exp inExp;
input list<DAE.Exp> inAcc;
input Boolean noFactors "Decides if the default is the empty list or not";
input output list<DAE.Exp> acc;
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
output list<DAE.Exp> outExpLst;
algorithm

outExpLst := match (inExp,inAcc,noFactors,doInverseFactors)
acc := match inExp
local
DAE.Exp e1,e2,e3,e;
Type tp;
list<DAE.Exp> acc, pow_acc, pow_acc2;
list<DAE.Exp> pow_acc, pow_acc2;

// (x*y)^n = x^n*y^n
case (DAE.BINARY(DAE.BINARY(e1,DAE.MUL(),e2), DAE.POW(), e3),acc,_,_)
case DAE.BINARY(DAE.BINARY(e1,DAE.MUL(),e2), DAE.POW(), e3)
equation
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
pow_acc = expPowLst(pow_acc, e3);

pow_acc2 = expandFactorsWork(e2,{},noFactors,doInverseFactors);
pow_acc2 = expandFactorsWork(e2,{},doInverseFactors);
pow_acc2 = expPowLst(pow_acc2, e3);

acc = listAppend(pow_acc, acc);
acc = listAppend(pow_acc2, acc);
then acc;
// (x/y)^n = x^n*y^(-n)
case (DAE.BINARY(DAE.BINARY(e1,DAE.DIV(),e2), DAE.POW(), e3),acc,_,_)
case DAE.BINARY(DAE.BINARY(e1,DAE.DIV(),e2), DAE.POW(), e3)
equation
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
pow_acc = expPowLst(pow_acc, e3);

pow_acc2 = expandFactorsWork(e2,{},noFactors,doInverseFactors);
pow_acc2 = expandFactorsWork(e2,{},doInverseFactors);
pow_acc2 = expPowLst(pow_acc2, negate(e3));

acc = listAppend(pow_acc, acc);
acc = listAppend(pow_acc2, acc);
then acc;
// (x^n)^m = x^(n*m)
case (DAE.BINARY(DAE.BINARY(e1,DAE.POW(),e2), DAE.POW(), e3),acc,_,_)
case DAE.BINARY(DAE.BINARY(e1,DAE.POW(),e2), DAE.POW(), e3)
equation
e = expMul(e2,e3);
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
pow_acc = expPowLst(pow_acc, e);

acc = listAppend(pow_acc, acc);
Expand All @@ -3165,31 +3075,30 @@ algorithm
// abs(x/y) = abs(x)/abs(y);

// -(x) = -1*x
case(DAE.UNARY(DAE.UMINUS(tp),e1),acc,_,_)
case DAE.UNARY(DAE.UMINUS(tp),e1)
equation
e = makeConstOne(tp);
acc = expandFactorsWork(e1,acc,true,doInverseFactors);
acc = expandFactorsWork(e1,acc,doInverseFactors);
e = negate(e);
then e::acc;
case(DAE.UNARY(DAE.UMINUS_ARR(tp),e1),acc,_,_)
case DAE.UNARY(DAE.UMINUS_ARR(tp),e1)
equation
e = makeConstOne(tp);
acc = expandFactorsWork(e1,acc,true,doInverseFactors);
acc = expandFactorsWork(e1,acc,doInverseFactors);
e = negate(e);
then e::acc;

else
equation
acc = factorsWork(inExp,inAcc,noFactors,doInverseFactors);
then expandFactorsWork2(acc, noFactors, doInverseFactors);
acc = factorsWork(inExp,acc,doInverseFactors);
then expandFactorsWork2(acc,doInverseFactors);

end match;

end expandFactorsWork;

protected function expandFactorsWork2
input list<DAE.Exp> inAcc;
input Boolean noFactors "Decides if the default is the empty list or not";
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
output list<DAE.Exp> outExpLst = {};
protected
Expand All @@ -3198,11 +3107,11 @@ algorithm

for elem in inAcc loop
tmpExpLst := match(elem)
case(DAE.BINARY(DAE.BINARY(_,DAE.DIV(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
case(DAE.BINARY(DAE.BINARY(_,DAE.MUL(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
case(DAE.BINARY(DAE.BINARY(_,DAE.POW(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
case(DAE.UNARY(DAE.UMINUS(),_)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
case(DAE.UNARY(DAE.UMINUS_ARR(),_)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
case(DAE.BINARY(DAE.BINARY(_,DAE.DIV(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
case(DAE.BINARY(DAE.BINARY(_,DAE.MUL(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
case(DAE.BINARY(DAE.BINARY(_,DAE.POW(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
case(DAE.UNARY(DAE.UMINUS(),_)) then expandFactorsWork(elem,{},doInverseFactors);
case(DAE.UNARY(DAE.UMINUS_ARR(),_)) then expandFactorsWork(elem,{},doInverseFactors);
else {elem};
end match;
outExpLst := listAppend(tmpExpLst, outExpLst);
Expand Down Expand Up @@ -7112,6 +7021,13 @@ algorithm
then
(DAE.ASUB(e1, expl), arg);

case e1 as DAE.RSUB()
equation
(e2, arg) = traverseExpBidir(e1.exp, inEnterFunc, inExitFunc, inArg);
e1.exp = e2;
then
(e1, arg);

case DAE.TSUB(e1, i, ty)
equation
(e1, arg) = traverseExpBidir(e1, inEnterFunc, inExitFunc, inArg);
Expand Down
14 changes: 14 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ algorithm

case (DAE.CALL(),_) then (simplifyCall(inExp),options);

case (DAE.RSUB(),_) then (simplifyRSub(inExp),options);

case (DAE.MATCHEXPRESSION(),_) then (simplifyMatch(inExp),options);
case (DAE.UNBOX(),_) then (simplifyUnbox(inExp),options);
case (DAE.BOX(),_) then (simplifyUnbox(inExp),options);
Expand All @@ -302,6 +304,18 @@ algorithm
end match;
end simplifyWork;

protected function simplifyRSub
input output DAE.Exp e;
algorithm
e := match e
local
DAE.ComponentRef cr;
case (DAE.RSUB(exp=DAE.CREF(componentRef=cr), ix=-1))
then DAE.CREF(ComponentReference.joinCrefs(cr, ComponentReference.makeCrefIdent(e.fieldName, e.ty, {})), e.ty);
else e;
end match;
end simplifyRSub;

protected function simplifyAsubExp
input DAE.Exp origExp;
input DAE.Exp inExp;
Expand Down
Loading