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

Commit

Permalink
[NF] Simplification improvements.
Browse files Browse the repository at this point in the history
- Remove for-loops with empty ranges.
- Simplify all statements in the flat model,
  not only those inside functions.
- Also simplify when-statements.
- Implement evaluation of and/or/not of arrays.

Belonging to [master]:
  - #2557
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jul 3, 2018
1 parent fd178e1 commit 25d5df2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
43 changes: 29 additions & 14 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -1013,8 +1013,8 @@ function evalLogicBinaryOp
output Expression exp;
algorithm
exp := match op.op
case Op.AND then evalLogicBinaryAnd(exp1, exp2, target);
case Op.OR then evalLogicBinaryOr(exp1, exp2, target);
case Op.AND then evalLogicBinaryAnd(evalExp(exp1, target), exp2, target);
case Op.OR then evalLogicBinaryOr(evalExp(exp1, target), exp2, target);
else
algorithm
Error.addInternalError(getInstanceName() + ": unimplemented case for " +
Expand All @@ -1029,37 +1029,51 @@ function evalLogicBinaryAnd
input Expression exp2;
input EvalTarget target;
output Expression exp;
protected
Expression e;
algorithm
e := evalExp(exp1, target);
exp := matchcontinue exp1
local
list<Expression> expl;

exp := match e
case Expression.BOOLEAN()
then if e.value then evalExp(exp2, target) else e;
then if exp1.value then evalExp(exp2, target) else exp1;

case Expression.ARRAY()
algorithm
Expression.ARRAY(elements = expl) := evalExp(exp2, target);
expl := list(evalLogicBinaryAnd(e1, e2, target)
threaded for e1 in exp1.elements, e2 in expl);
then
Expression.ARRAY(Type.setArrayElementType(exp1.ty, Type.BOOLEAN()), expl);

else
algorithm
exp := Expression.BINARY(exp1, Operator.makeAnd(Type.UNKNOWN()), exp2);
printFailedEvalError(getInstanceName(), exp, sourceInfo());
then
fail();
end match;
end matchcontinue;
end evalLogicBinaryAnd;

function evalLogicBinaryOr
input Expression exp1;
input Expression exp2;
input EvalTarget target;
output Expression exp;
protected
Expression e;
algorithm
e := evalExp(exp1, target);
exp := match exp1
local
list<Expression> expl;

exp := match e
case Expression.BOOLEAN()
then if e.value then e else evalExp(exp2, target);
then if exp1.value then exp1 else evalExp(exp2, target);

case Expression.ARRAY()
algorithm
Expression.ARRAY(elements = expl) := evalExp(exp2, target);
expl := list(evalLogicBinaryOr(e1, e2, target)
threaded for e1 in exp1.elements, e2 in expl);
then
Expression.ARRAY(Type.setArrayElementType(exp1.ty, Type.BOOLEAN()), expl);

else
algorithm
Expand All @@ -1076,7 +1090,7 @@ function evalLogicUnaryOp
output Expression exp;
algorithm
exp := match op.op
case Op.NOT then evalLogicUnaryNot(exp1);
case Op.NOT then Expression.mapArrayElements(exp1, evalLogicUnaryNot);
else
algorithm
Error.addInternalError(getInstanceName() + ": unimplemented case for " +
Expand All @@ -1092,6 +1106,7 @@ function evalLogicUnaryNot
algorithm
exp := match exp1
case Expression.BOOLEAN() then Expression.BOOLEAN(not exp1.value);

else
algorithm
exp := Expression.UNARY(Operator.makeNot(Type.UNKNOWN()), exp1);
Expand Down
31 changes: 25 additions & 6 deletions Compiler/NFFrontEnd/NFSimplifyModel.mo
Expand Up @@ -61,6 +61,8 @@ algorithm
flatModel.variables := list(simplifyVariable(v) for v in flatModel.variables);
flatModel.equations := simplifyEquations(flatModel.equations);
flatModel.initialEquations := simplifyEquations(flatModel.initialEquations);
flatModel.algorithms := list(simplifyAlgorithm(a) for a in flatModel.algorithms);
flatModel.initialAlgorithms := list(simplifyAlgorithm(a) for a in flatModel.initialAlgorithms);

functions := FunctionTree.map(functions, simplifyFunction);

Expand Down Expand Up @@ -183,6 +185,12 @@ algorithm
end match;
end simplifyEquation;

function simplifyAlgorithm
input output Algorithm alg;
algorithm
alg.statements := simplifyStatements(alg.statements);
end simplifyAlgorithm;

function simplifyStatements
input list<Statement> stmts;
output list<Statement> outStmts = {};
Expand All @@ -209,10 +217,24 @@ algorithm
then
stmt :: statements;

case Statement.FOR()
case Statement.FOR(range = SOME(e))
algorithm
if not Type.isEmptyArray(Expression.typeOf(e)) then
stmt.range := SimplifyExp.simplifyOpt(stmt.range);
stmt.body := simplifyStatements(stmt.body);
statements := stmt :: statements;
end if;
then
statements;

case Statement.IF()
then simplifyIfStmtBranches(stmt.branches, stmt.source, Statement.makeIf, simplifyStatements, statements);

case Statement.WHEN()
algorithm
stmt.range := SimplifyExp.simplifyOpt(stmt.range);
stmt.body := simplifyStatements(stmt.body);
stmt.branches := list(
(SimplifyExp.simplify(Util.tuple21(b)), simplifyStatements(Util.tuple22(b)))
for b in stmt.branches);
then
stmt :: statements;

Expand All @@ -227,9 +249,6 @@ algorithm
then
statements;

case Statement.IF()
then simplifyIfStmtBranches(stmt.branches, stmt.source, Statement.makeIf, simplifyStatements, statements);

else stmt :: statements;
end match;
end simplifyStatement;
Expand Down

0 comments on commit 25d5df2

Please sign in to comment.