Skip to content

Commit

Permalink
Traverse match-expressions, keeping the old expression if nothing cha…
Browse files Browse the repository at this point in the history
…nged

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17931 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 29, 2013
1 parent ea5d8d9 commit 2a9e82d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 37 deletions.
13 changes: 7 additions & 6 deletions Compiler/FrontEnd/Algorithm.mo
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,20 @@ public function optimizeIf
input DAE.Else iels;
input DAE.ElementSource isource;
output list<DAE.Statement> ostmts "can be empty or selected branch";
output Boolean changed;
algorithm
ostmts := match (icond, istmts, iels, isource)
(ostmts,changed) := match (icond, istmts, iels, isource)
local
list<DAE.Statement> stmts;
DAE.Else els;
DAE.ElementSource source;
DAE.Exp cond;

case (DAE.BCONST(true), stmts, _, source) then stmts;
case (DAE.BCONST(false), stmts, DAE.NOELSE(), source) then {};
case (DAE.BCONST(false), _, DAE.ELSE(stmts), source) then stmts;
case (DAE.BCONST(false), _, DAE.ELSEIF(cond, stmts, els), source) then optimizeIf(cond, stmts, els, source);
else DAE.STMT_IF(icond, istmts, iels, isource)::{};
case (DAE.BCONST(true), stmts, _, source) then (stmts,true);
case (DAE.BCONST(false), stmts, DAE.NOELSE(), source) then ({},true);
case (DAE.BCONST(false), _, DAE.ELSE(stmts), source) then (stmts,true);
case (DAE.BCONST(false), _, DAE.ELSEIF(cond, stmts, els), source) equation (ostmts,_) = optimizeIf(cond, stmts, els, source); then (ostmts,true);
else then (DAE.STMT_IF(icond, istmts, iels, isource)::{},false);
end match;
end optimizeIf;

Expand Down
68 changes: 45 additions & 23 deletions Compiler/FrontEnd/DAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3694,9 +3694,12 @@ protected function traverseDAEEquationsStmtsList "Traversing of DAE.Statement."
replaceable type Type_a subtypeof Any;
protected
list<list<DAE.Statement>> outStmtsLst;
Boolean b;
algorithm
(outStmtsLst,oextraArg) := List.map2Fold(inStmts,traverseDAEEquationsStmtsWork,func,opt,iextraArg);
outStmts := List.flatten(outStmtsLst);
b := List.allReferenceEq(inStmts,outStmts);
outStmts := Util.if_(b,inStmts,outStmts);
end traverseDAEEquationsStmtsList;

protected function traverseStatementsOptionsEvalLhs
Expand All @@ -3717,7 +3720,7 @@ algorithm
end traverseStatementsOptionsEvalLhs;

protected function traverseDAEEquationsStmtsWork "Handles the traversing of DAE.Statement."
input DAE.Statement inStmts;
input DAE.Statement inStmt;
input FuncExpType func;
input TraverseStatementsOptions opt;
input Type_a iextraArg;
Expand All @@ -3726,7 +3729,7 @@ protected function traverseDAEEquationsStmtsWork "Handles the traversing of DAE.
partial function FuncExpType input tuple<DAE.Exp,Type_a> arg; output tuple<DAE.Exp,Type_a> oarg; end FuncExpType;
replaceable type Type_a subtypeof Any;
algorithm
(outStmts,oextraArg) := matchcontinue(inStmts,func,opt,iextraArg)
(outStmts,oextraArg) := matchcontinue (inStmt,func,opt,iextraArg)
local
DAE.Exp e_1,e_2,e,e2,e3,e_3;
list<DAE.Exp> expl1,expl2;
Expand All @@ -3738,102 +3741,117 @@ algorithm
String id1,str;
Integer ix;
DAE.ElementSource source;
DAE.Else algElse;
DAE.Else algElse,algElse1;
Type_a extraArg;
list<tuple<DAE.ComponentRef,Absyn.Info>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";
list<DAE.ComponentRef> conditions;
Boolean initialCall;
Boolean initialCall,b;

case (DAE.STMT_ASSIGN(type_ = tp,exp1 = e,exp = e2, source = source),_,_,extraArg)
equation
((e_1,extraArg)) = traverseStatementsOptionsEvalLhs(((e, extraArg)),func,opt);
((e_2,extraArg)) = func((e2, extraArg));
then (DAE.STMT_ASSIGN(tp,e_1,e_2,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(e2,e_2),inStmt,DAE.STMT_ASSIGN(tp,e_1,e_2,source));
then (x::{},extraArg);

case (DAE.STMT_TUPLE_ASSIGN(type_ = tp,expExpLst = expl1, exp = e, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
((DAE.TUPLE(expl2), extraArg)) = traverseStatementsOptionsEvalLhs(((DAE.TUPLE(expl1), extraArg)),func,opt);
then (DAE.STMT_TUPLE_ASSIGN(tp,expl2,e_1,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(expl1,expl2),inStmt,DAE.STMT_TUPLE_ASSIGN(tp,expl2,e_1,source));
then (x::{},extraArg);

case (DAE.STMT_ASSIGN_ARR(type_ = tp,componentRef = cr, exp = e, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
((e_2 as DAE.CREF(cr_1,_), extraArg)) = traverseStatementsOptionsEvalLhs(((Expression.crefExp(cr), extraArg)),func,opt);
then (DAE.STMT_ASSIGN_ARR(tp,cr_1,e_1,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(cr,cr_1),inStmt,DAE.STMT_ASSIGN_ARR(tp,cr_1,e_1,source));
then (x::{},extraArg);

// NOTE: This is making the function use matchcontinue!
case (DAE.STMT_ASSIGN_ARR(type_ = tp,componentRef = cr, exp = e, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
failure(((DAE.CREF(_,_), _)) = func((Expression.crefExp(cr), extraArg)));
x = Util.if_(referenceEq(e,e_1),inStmt,DAE.STMT_ASSIGN_ARR(tp,cr,e_1,source));
/* We need to pass this through because simplify/etc may scalarize the cref...
true = Flags.isSet(Flags.FAILTRACE);
print(DAEDump.ppStatementStr(x));
print("Warning, not allowed to set the componentRef to a expression in DAEUtil.traverseDAEEquationsStmts\n");
*/
then (DAE.STMT_ASSIGN_ARR(tp,cr,e_1,source)::{},extraArg);
then (x::{},extraArg);

case (DAE.STMT_IF(exp=e,statementLst=stmts,else_ = algElse, source = source),_,_,extraArg)
equation
(algElse,extraArg) = traverseDAEEquationsStmtsElse(algElse,func,opt,extraArg);
(algElse1,extraArg) = traverseDAEEquationsStmtsElse(algElse,func,opt,extraArg);
(stmts2,extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
((e_1,extraArg)) = func((e, extraArg));
stmts1 = Algorithm.optimizeIf(e_1,stmts2,algElse,source);
(stmts1,b) = Algorithm.optimizeIf(e_1,stmts2,algElse1,source);
stmts1 = Util.if_(not b and referenceEq(e,e_1) and referenceEq(stmts,stmts2) and referenceEq(algElse,algElse1),inStmt::{},stmts1);
then (stmts1,extraArg);

case (DAE.STMT_FOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, source = source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_FOR(tp,b1,id1,ix,e_1,stmts2,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(stmts,stmts2),inStmt,DAE.STMT_FOR(tp,b1,id1,ix,e_1,stmts2,source));
then (x::{},extraArg);

case (DAE.STMT_PARFOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, loopPrlVars=loopPrlVars, source = source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_PARFOR(tp,b1,id1,ix,e_1,stmts2,loopPrlVars,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(stmts,stmts2),inStmt,DAE.STMT_PARFOR(tp,b1,id1,ix,e_1,stmts2,loopPrlVars,source));
then (x::{},extraArg);

case (DAE.STMT_WHILE(exp = e,statementLst=stmts, source = source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_WHILE(e_1,stmts2,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(stmts,stmts2),inStmt,DAE.STMT_WHILE(e_1,stmts2,source));
then (x::{},extraArg);

case (DAE.STMT_WHEN(exp=e,conditions=conditions,initialCall=initialCall,statementLst=stmts,elseWhen=NONE(),source=source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_WHEN(e_1,conditions,initialCall,stmts2,NONE(),source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(stmts,stmts2),inStmt,DAE.STMT_WHEN(e_1,conditions,initialCall,stmts2,NONE(),source));
then (x::{},extraArg);

case (DAE.STMT_WHEN(exp=e,conditions=conditions,initialCall=initialCall,statementLst=stmts,elseWhen=SOME(ew),source=source),_,_,extraArg)
equation
({ew_1}, extraArg) = traverseDAEEquationsStmtsList({ew},func,opt,extraArg);
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_WHEN(e_1,conditions,initialCall,stmts2,SOME(ew),source)::{},extraArg);
x = Util.if_(referenceEq(ew,ew_1) and referenceEq(e,e_1) and referenceEq(stmts,stmts2),inStmt,DAE.STMT_WHEN(e_1,conditions,initialCall,stmts2,SOME(ew),source));
then (x::{},extraArg);

case (DAE.STMT_ASSERT(cond = e, msg=e2, level=e3, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
((e_2, extraArg)) = func((e2, extraArg));
((e_3, extraArg)) = func((e3, extraArg));
then (DAE.STMT_ASSERT(e_1,e_2,e_3,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(e2,e_2) and referenceEq(e3,e_3),inStmt,DAE.STMT_ASSERT(e_1,e_2,e_3,source));
then (x::{},extraArg);

case (DAE.STMT_TERMINATE(msg = e, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_TERMINATE(e_1,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1),inStmt,DAE.STMT_TERMINATE(e_1,source));
then (x::{},extraArg);

case (DAE.STMT_REINIT(var = e,value=e2, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
((e_2, extraArg)) = func((e2, extraArg));
then (DAE.STMT_REINIT(e_1,e_2,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1) and referenceEq(e2,e_2),inStmt,DAE.STMT_REINIT(e_1,e_2,source));
then (x::{},extraArg);

case (DAE.STMT_NORETCALL(exp = e, source = source),_,_,extraArg)
equation
((e_1, extraArg)) = func((e, extraArg));
then (DAE.STMT_NORETCALL(e_1,source)::{},extraArg);
x = Util.if_(referenceEq(e,e_1),inStmt,DAE.STMT_NORETCALL(e_1,source));
then (x::{},extraArg);

case (x as DAE.STMT_RETURN(source = source),_,_,extraArg)
then (x::{},extraArg);
Expand All @@ -3845,17 +3863,20 @@ algorithm
case (DAE.STMT_FAILURE(body=stmts, source = source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
then (DAE.STMT_FAILURE(stmts2,source)::{},extraArg);
x = Util.if_(referenceEq(stmts,stmts2),inStmt,DAE.STMT_FAILURE(stmts2,source));
then (x::{},extraArg);

case (DAE.STMT_TRY(tryBody=stmts, source = source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
then (DAE.STMT_TRY(stmts2,source)::{},extraArg);
x = Util.if_(referenceEq(stmts,stmts2),inStmt,DAE.STMT_TRY(stmts2,source));
then (x::{},extraArg);

case (DAE.STMT_CATCH(catchBody=stmts, source = source),_,_,extraArg)
equation
(stmts2, extraArg) = traverseDAEEquationsStmtsList(stmts,func,opt,extraArg);
then (DAE.STMT_CATCH(stmts2,source)::{},extraArg);
x = Util.if_(referenceEq(stmts,stmts2),inStmt,DAE.STMT_CATCH(stmts2,source));
then (x::{},extraArg);

case (x as DAE.STMT_THROW(source = source),_,_,extraArg)
then (x::{},extraArg);
Expand All @@ -3864,6 +3885,7 @@ algorithm
equation
str = DAEDump.ppStatementStr(x);
str = "DAEUtil.traverseDAEEquationsStmts not implemented correctly: " +& str;
print(str);
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end matchcontinue;
Expand Down Expand Up @@ -3973,7 +3995,7 @@ algorithm
(stmts2,extraArg) = traverseDAEStmts(stmts,func,extraArg);
((e_1,extraArg)) = func((e, x, extraArg));
(xs_1,extraArg) = traverseDAEStmts(xs, func, extraArg);
stmts1 = Algorithm.optimizeIf(e_1,stmts2,algElse,source);
(stmts1,_) = Algorithm.optimizeIf(e_1,stmts2,algElse,source);
then (listAppend(stmts1, xs_1),extraArg);

case (((x as DAE.STMT_FOR(type_=tp,iterIsArray=b1,iter=id1,index=ix,range=e,statementLst=stmts, source = source))::xs),_,extraArg)
Expand Down
17 changes: 9 additions & 8 deletions Compiler/FrontEnd/Patternm.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1896,21 +1896,22 @@ algorithm
local
list<DAE.Pattern> patterns;
list<DAE.Element> decls;
list<DAE.Statement> body;
Option<DAE.Exp> result,patternGuard;
list<DAE.Statement> body,body1;
Option<DAE.Exp> result,result1,patternGuard,patternGuard1;
Integer jump;
Absyn.Info resultInfo,info;
list<DAE.MatchCase> cases;
list<DAE.MatchCase> cases,cases1;
A a;

case ({},_,a) then ({},a);
case (DAE.CASE(patterns,patternGuard,decls,body,result,resultInfo,jump,info)::cases,_,a)
equation
(body,(_,a)) = DAEUtil.traverseDAEEquationsStmts(body,Expression.traverseSubexpressionsHelper,(func,a));
((patternGuard,a)) = Expression.traverseExpOpt(patternGuard,func,a);
((result,a)) = Expression.traverseExpOpt(result,func,a);
(cases,a) = traverseCases(cases,func,a);
then (DAE.CASE(patterns,patternGuard,decls,body,result,resultInfo,jump,info)::cases,a);
(body1,(_,a)) = DAEUtil.traverseDAEEquationsStmts(body,Expression.traverseSubexpressionsHelper,(func,a));
((patternGuard1,a)) = Expression.traverseExpOpt(patternGuard,func,a);
((result1,a)) = Expression.traverseExpOpt(result,func,a);
(cases1,a) = traverseCases(cases,func,a);
cases = Util.if_(referenceEq(cases,cases1) and referenceEq(patternGuard,patternGuard1) and referenceEq(result,result1) and referenceEq(body,body1), inCases, DAE.CASE(patterns,patternGuard1,decls,body1,result1,resultInfo,jump,info)::cases1);
then (cases,a);
end match;
end traverseCases;

Expand Down
15 changes: 15 additions & 0 deletions Compiler/Util/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9200,4 +9200,19 @@ algorithm
end match;
end combinationMap1_tail2;

public function allReferenceEq "Checks if all elements in the lists have equal references"
input list<ElementInType> lst1;
input list<ElementInType> lst2;
output Boolean b;
algorithm
b := match (lst1,lst2)
local
ElementInType el1,el2;
list<ElementInType> rest1,rest2;
case ({},{}) then true;
case (el1::rest1,el2::rest2) then referenceEq(el1,el2) and allReferenceEq(rest1,rest2);
else false;
end match;
end allReferenceEq;

end List;

0 comments on commit 2a9e82d

Please sign in to comment.