Skip to content

Commit

Permalink
- Added match->switch conversion for integers.
Browse files Browse the repository at this point in the history
  - There are very few cases using this, but they are used for lookup and the performance increase seems good


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7749 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 23, 2011
1 parent de97145 commit 6d93004
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions Compiler/FrontEnd/Patternm.mo
Expand Up @@ -595,13 +595,16 @@ algorithm
tuple<Integer,DAE.ExpType,Integer> tpl;
list<list<DAE.Pattern>> patternMatrix;
String str;
DAE.ExpType ty;
case (Absyn.MATCHCONTINUE(),_,_) then DAE.MATCHCONTINUE();
case (_,cases,_)
equation
true = listLength(cases) > 2;
patternMatrix = Util.transposeList(Util.listMap(cases,getCasePatterns));
tpl = findPatternToConvertToSwitch(patternMatrix,0,info);
Error.assertionOrAddSourceMessage(not RTOpts.debugFlag("patternmAllInfo"),Error.MATCH_TO_SWITCH_OPTIMIZATION, {}, info);
(_,ty,_) = tpl;
str = ExpressionDump.typeString(ty);
Error.assertionOrAddSourceMessage(not RTOpts.debugFlag("patternmAllInfo"),Error.MATCH_TO_SWITCH_OPTIMIZATION, {str}, info);
then DAE.MATCH(SOME(tpl));
else DAE.MATCH(NONE());
end matchcontinue;
Expand Down Expand Up @@ -646,18 +649,23 @@ algorithm
true = listLength(ixs)>7; // hashing has a considerable overhead, only convert to switch if it is worth it
ix = findMinMod(ixs,1);
then (DAE.ET_STRING(),ix);
case ({},_,_) then (ty,0);
case (DAE.PAT_CONSTANT(exp=DAE.SCONST(str))::pats,ixs,_)
equation
ix = System.stringHashDjb2Mod(str,65536);
false = listMember(ix,ixs);
(ty,extraarg) = findPatternToConvertToSwitch2(pats,ix::ixs,DAE.ET_STRING());
then (ty,extraarg);
case ({},_,DAE.ET_METATYPE()) then (ty,0);
case (DAE.PAT_CALL(index=ix)::pats,ixs,_)
equation
false = listMember(ix,ixs);
(ty,extraarg) = findPatternToConvertToSwitch2(pats,ix::ixs,DAE.ET_METATYPE());
then (ty,extraarg);
case (DAE.PAT_CONSTANT(exp=DAE.ICONST(ix))::pats,ixs,_)
equation
false = listMember(ix,ixs);
(ty,extraarg) = findPatternToConvertToSwitch2(pats,ix::ixs,DAE.ET_INT());
then (ty,extraarg);
end match;
end findPatternToConvertToSwitch2;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/Error.mo
Expand Up @@ -691,7 +691,7 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
(META_UNUSED_DECL,TRANSLATION(),NOTIFICATION(),"Unused local variable: %s."),
(META_UNUSED_AS_BINDING,TRANSLATION(),NOTIFICATION(),"Removing unused as-binding: %s."),
(FAILED_TO_EVALUATE_EXPRESSION,TRANSLATION(),ERROR(),"Could not evaluate expression: %s"),
(MATCH_TO_SWITCH_OPTIMIZATION,TRANSLATION(),NOTIFICATION(),"Converted match expression to switch."),
(MATCH_TO_SWITCH_OPTIMIZATION,TRANSLATION(),NOTIFICATION(),"Converted match expression to switch of type %s."),

(COMPILER_NOTIFICATION,TRANSLATION(),NOTIFICATION(),"%s"),
(COMPILER_WARNING,TRANSLATION(),WARNING(),"%s")
Expand Down
5 changes: 5 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -5675,6 +5675,10 @@ case exp as MATCHEXPRESSION(__) then
'stringHashDjb2Mod(<%prefix%>_in<%switchIndex%>,<%div%>)'
case MATCH(switch=SOME((switchIndex,ET_METATYPE(__),_))) then
'valueConstructor(<%prefix%>_in<%switchIndex%>)'
case MATCH(switch=SOME((switchIndex,ty as ET_INT(__),_))) then
'<%prefix%>_in<%switchIndex%>'
case MATCH(switch=SOME(_)) then
'<%\n%>#error "unknown switch"<%\n%>'
else tempDecl('int', &varDeclsInner)
let done = tempDecl('int', &varDeclsInner)
let onPatternFail = match exp.matchType case MATCHCONTINUE(__) then "MMC_THROW()" case MATCH(__) then "break"
Expand Down Expand Up @@ -5744,6 +5748,7 @@ template switchIndex(Pattern pattern, Integer extraArg)
match pattern
case PAT_CALL(__) then 'case <%getValueCtor(index)%>'
case PAT_CONSTANT(exp=e as SCONST(__)) then 'case <%stringHashDjb2Mod(e.string,extraArg)%> /* <%e.string%> */'
case PAT_CONSTANT(exp=e as ICONST(__)) then 'case <%e.integer%>'
else 'default'
end switchIndex;

Expand Down

0 comments on commit 6d93004

Please sign in to comment.