Skip to content

Commit

Permalink
- Added simplify all expressions before/after optimization phases
Browse files Browse the repository at this point in the history
- Removed odd hacks that assume Expression.isConst is actually Expression.isConstValue
  + The difference is that you need to simplify a const non-value for it to become a value


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11582 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Mar 28, 2012
1 parent b3bc4aa commit 38855c8
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 43 deletions.
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -6204,6 +6204,7 @@ algorithm
Debug.fcall(Flags.DUMP_DAE_LOW, BackendDump.dump, inDAE);
System.realtimeTick(BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
// pre optimisation phase
_ := traverseBackendDAEExps(inDAE,ExpressionSimplify.simplifyTraverseHelper,0) "simplify all expressions";
(optdae,Util.SUCCESS()) := preoptimiseDAE(inDAE,functionTree,preOptModules);

// transformation phase (matching and sorting using a index reduction method
Expand All @@ -6218,6 +6219,7 @@ algorithm
Debug.execStat("findZeroCrossings",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
indexed_dlow := translateDae(sode1,NONE());
Debug.execStat("translateDAE",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
_ := traverseBackendDAEExps(indexed_dlow,ExpressionSimplify.simplifyTraverseHelper,0) "simplify all expressions";
sode2 := calculateValues(inCache, inEnv, indexed_dlow);
Debug.execStat("calculateValue",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
outSODE := expandAlgorithmsbyInitStmts(sode2);
Expand Down
66 changes: 34 additions & 32 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -10540,24 +10540,26 @@ algorithm
Option<DAE.VariableAttributes> dae_var_attr;
DAE.Exp minValue, maxValue;

case(BackendDAE.VAR(varType=DAE.T_REAL(source=_), values=dae_var_attr)) equation
(SOME(minValue), SOME(maxValue)) = DAEUtil.getMinMaxValues(dae_var_attr);
true = Expression.isConst(minValue);
true = Expression.isConst(maxValue);
then (SOME(minValue), SOME(maxValue));

case(BackendDAE.VAR(varType=DAE.T_REAL(source=_), values=dae_var_attr)) equation
(SOME(minValue), NONE()) = DAEUtil.getMinMaxValues(dae_var_attr);
true = Expression.isConst(minValue);
then (SOME(minValue), NONE());

case(BackendDAE.VAR(varType=DAE.T_REAL(source=_), values=dae_var_attr)) equation
(NONE(), SOME(maxValue)) = DAEUtil.getMinMaxValues(dae_var_attr);
true = Expression.isConst(maxValue);
then (NONE(), SOME(maxValue));
case(BackendDAE.VAR(varType=DAE.T_REAL(source=_), values=dae_var_attr))
equation
(SOME(minValue), SOME(maxValue)) = DAEUtil.getMinMaxValues(dae_var_attr);
true = Expression.isConstValue(minValue);
true = Expression.isConstValue(maxValue);
then (SOME(minValue), SOME(maxValue));

case (_) equation
then (NONE(), NONE());
case(BackendDAE.VAR(varType=DAE.T_REAL(source=_), values=dae_var_attr))
equation
(SOME(minValue), NONE()) = DAEUtil.getMinMaxValues(dae_var_attr);
true = Expression.isConstValue(minValue);
then (SOME(minValue), NONE());

case(BackendDAE.VAR(varType=DAE.T_REAL(source=_), values=dae_var_attr))
equation
(NONE(), SOME(maxValue)) = DAEUtil.getMinMaxValues(dae_var_attr);
true = Expression.isConstValue(maxValue);
then (NONE(), SOME(maxValue));

else (NONE(), NONE());
end matchcontinue;
end getMinMaxValues;

Expand All @@ -10576,87 +10578,87 @@ algorithm
case (BackendDAE.VAR(varKind = BackendDAE.VARIABLE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.VARIABLE(), varType = DAE.T_STRING(source = _), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.VARIABLE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DISCRETE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DISCRETE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.STATE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_DER(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.DUMMY_STATE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.PARAM(), varType = DAE.T_STRING(source = _), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.PARAM(), bindValue = SOME(value)))
equation
e = ValuesUtil.valueExp(value);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
/* String - Parameters without value binding. Investigate if it has start value */
case (BackendDAE.VAR(varKind = BackendDAE.PARAM(), varType = DAE.T_STRING(source = _), bindValue = NONE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttr(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
/* Parameters without value binding. Investigate if it has start value */
case (BackendDAE.VAR(varKind = BackendDAE.PARAM(), bindValue = NONE(), values = dae_var_attr))
equation
e = DAEUtil.getStartAttr(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then
SOME(e);
case (BackendDAE.VAR(varKind = BackendDAE.EXTOBJ(_), bindExp = SOME(e)))
Expand All @@ -10678,7 +10680,7 @@ algorithm

case (BackendDAE.VAR(varType = DAE.T_REAL(source = _), values = dae_var_attr)) equation
e = DAEUtil.getNominalAttrFail(dae_var_attr);
true = Expression.isConst(e);
true = Expression.isConstValue(e);
then SOME(e);

case (_) equation
Expand Down
51 changes: 50 additions & 1 deletion Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -104,6 +104,19 @@ algorithm
end matchcontinue;
end simplify;

public function simplifyTraverseHelper
input tuple<DAE.Exp,A> tpl;
output tuple<DAE.Exp,A> otpl;
replaceable type A subtypeof Any;
protected
A a;
DAE.Exp exp;
algorithm
(exp,a) := tpl;
(exp,_) := simplify(exp);
otpl := (exp,a);
end simplifyTraverseHelper;

public function simplify1time "simplify1 with timing"
input DAE.Exp e;
output DAE.Exp outE;
Expand Down Expand Up @@ -3097,7 +3110,7 @@ protected function simplifyBinaryConst
algorithm
outExp := match (inOperator1,inExp2,inExp3)
local
Integer ie1,ie2;
Integer ie1,ie2,i1,i2;
Real e2_1,e1_1,v1,v2;
Boolean b,b1,b2;
DAE.Exp exp1,exp2,val;
Expand Down Expand Up @@ -3235,6 +3248,12 @@ algorithm
then
DAE.BCONST(b);

case(DAE.LESS(ty=_),DAE.ENUM_LITERAL(index=i1),DAE.ENUM_LITERAL(index=i2))
equation
b = i1 < i2;
then
DAE.BCONST(b);

case(DAE.LESSEQ(ty=_),DAE.BCONST(true),DAE.BCONST(false))
then DAE.BCONST(false);

Expand All @@ -3248,6 +3267,12 @@ algorithm
b = v1 <=. v2;
then
DAE.BCONST(b);

case(DAE.LESSEQ(ty=_),DAE.ENUM_LITERAL(index=i1),DAE.ENUM_LITERAL(index=i2))
equation
b = i1 <= i2;
then
DAE.BCONST(b);

case(DAE.GREATER(ty=_),DAE.BCONST(true),DAE.BCONST(false))
then DAE.BCONST(true);
Expand All @@ -3263,6 +3288,12 @@ algorithm
then
DAE.BCONST(b);

case(DAE.GREATER(ty=_),DAE.ENUM_LITERAL(index=i1),DAE.ENUM_LITERAL(index=i2))
equation
b = i1 > i2;
then
DAE.BCONST(b);

case(DAE.GREATEREQ(ty=_),DAE.BCONST(false),DAE.BCONST(true))
then DAE.BCONST(false);

Expand All @@ -3277,6 +3308,12 @@ algorithm
then
DAE.BCONST(b);

case(DAE.GREATEREQ(ty=_),DAE.ENUM_LITERAL(index=i1),DAE.ENUM_LITERAL(index=i2))
equation
b = i1 >= i2;
then
DAE.BCONST(b);

case(DAE.EQUAL(ty=_),DAE.BCONST(b1),DAE.BCONST(b2))
equation
b = boolEq(b1,b2);
Expand All @@ -3297,6 +3334,12 @@ algorithm
then
DAE.BCONST(b);

case(DAE.EQUAL(ty=_),DAE.ENUM_LITERAL(index=i1),DAE.ENUM_LITERAL(index=i2))
equation
b = i1 == i2;
then
DAE.BCONST(b);

case(DAE.NEQUAL(ty=_),DAE.BCONST(b1),DAE.BCONST(b2))
equation
b = not boolEq(b1,b2);
Expand All @@ -3317,6 +3360,12 @@ algorithm
then
DAE.BCONST(b);

case(DAE.NEQUAL(ty=_),DAE.ENUM_LITERAL(index=i1),DAE.ENUM_LITERAL(index=i2))
equation
b = i1 <> i2;
then
DAE.BCONST(b);

case(DAE.AND(DAE.T_BOOL(varLst = _)),exp1,exp2)
equation
b1 = Expression.getBoolConst(exp1);
Expand Down
8 changes: 0 additions & 8 deletions Compiler/susan_codegen/SimCode/CodegenC.tpl
Expand Up @@ -2242,13 +2242,6 @@ template initVal(Exp initialValue)
else error(sourceInfo(), 'initial value of unknown type: <%printExpStr(initialValue)%>')
end initVal;

template evalConstExpCond(Exp expCond)
::=
match expCond
case BCONST(__) then if bool then true else false
else error(sourceInfo(), 'condition of unknown type: <%printExpStr(expCond)%>')
end evalConstExpCond;

template initValXml(Exp initialValue)
::=
match initialValue
Expand All @@ -2257,7 +2250,6 @@ template initValXml(Exp initialValue)
case SCONST(__) then '<%Util.escapeModelicaStringToXmlString(string)%>'
case BCONST(__) then if bool then "true" else "false"
case ENUM_LITERAL(__) then '<%index%> /*ENUM:<%dotPath(name)%>*/'
case IFEXP(__) then if '<%evalConstExpCond(expCond)%>' then '<%initValXml(expThen)%>' else '<%initValXml(expThen)%>'
else error(sourceInfo(), 'initial value of unknown type: <%printExpStr(initialValue)%>')
end initValXml;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/susan_codegen/SimCode/CodegenFMU.tpl
Expand Up @@ -883,7 +883,7 @@ match platform
<%\t%> rm -f <%fileNamePrefix%>.def <%fileNamePrefix%>.o <%fileNamePrefix%>_FMU.libs <%fileNamePrefix%>_FMU.makefile <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>_records.o

<%fileNamePrefix%>.dll: clean <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>.o <%fileNamePrefix%>_records.o
<%\t%> $(CXX) -shared -I. -o <%fileNamePrefix%>.dll <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>.o <%fileNamePrefix%>_records.o $(CPPFLAGS) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(CFLAGS) $(LDFLAGS) $(SENDDATALIBS) <%match System.os() case "OSX" then "-lf2c" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic"%> -Wl,--kill-at
<%\t%> $(CXX) -shared -I. -o <%fileNamePrefix%>.dll <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>.o <%fileNamePrefix%>_records.o $(CPPFLAGS) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(CFLAGS) $(LDFLAGS) <%match System.os() case "OSX" then "-lf2c" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic"%> -Wl,--kill-at

<%\t%> "mkdir.exe" -p <%fileNamePrefix%>
<%\t%> "mkdir.exe" -p <%fileNamePrefix%>/binaries
Expand All @@ -893,7 +893,7 @@ match platform
else
<<
<%fileNamePrefix%>_FMU: <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>.o <%fileNamePrefix%>_records.o
<%\t%> $(CXX) -shared -I. -o <%fileNamePrefix%>$(DLLEXT) <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>.o <%fileNamePrefix%>_records.o $(CPPFLAGS) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(CFLAGS) $(LDFLAGS) $(SENDDATALIBS) <%match System.os() case "OSX" then "-lf2c" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic"%>
<%\t%> $(CXX) -shared -I. -o <%fileNamePrefix%>$(DLLEXT) <%fileNamePrefix%>_FMU.o <%fileNamePrefix%>.o <%fileNamePrefix%>_records.o $(CPPFLAGS) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(CFLAGS) $(LDFLAGS) <%match System.os() case "OSX" then "-lf2c" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic"%>

<%\t%> mkdir -p <%fileNamePrefix%>
<%\t%> mkdir -p <%fileNamePrefix%>/binaries
Expand Down

0 comments on commit 38855c8

Please sign in to comment.