Skip to content

Commit d5b7bc2

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Simplify of asub where the subscript is a range
This handles some obscure code that gets inlined. Ranges are not simplified in the general case because they can be huge and usually are used to iterate over some indexes without the need to construct an array. Subscripts are different in that they are used to construct arrays, so they should be safe to evaluate at compile-time.
1 parent 2091a03 commit d5b7bc2

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

Compiler/FrontEnd/ExpressionSimplify.mo

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,12 @@ protected function simplifyAsubExp
310310
algorithm
311311
outExp := matchcontinue (origExp,inExp,inSubs)
312312
local
313-
Integer sub;
313+
Integer sub, istart, istep, istop;
314314
DAE.Type tp;
315315
DAE.Exp e;
316-
list<DAE.Exp> eLst;
316+
list<DAE.Exp> eLst, subs;
317+
Boolean hasRange;
318+
Option<DAE.Exp> step;
317319
// ASUB(CAST(e)) -> CAST(liftArray(t), ASUB(e))
318320
case (_, DAE.CAST(tp,e), _)
319321
equation
@@ -339,6 +341,22 @@ algorithm
339341
Expression.expInt(exp);
340342
end for;
341343
then List.foldr(inSubs,simplifyAsub,inExp);
344+
345+
// Any range in the subscripts that we can evaluate?
346+
case (_,_,_)
347+
algorithm
348+
hasRange := false;
349+
subs := list(match exp
350+
case DAE.RANGE(DAE.T_INTEGER(),DAE.ICONST(istart),step,DAE.ICONST(istop))
351+
algorithm
352+
e := Expression.makeArray(list(DAE.ICONST(i) for i in simplifyRange(istart,match step case NONE() then 1; case SOME(DAE.ICONST(istep)) then istep; end match,istop)), DAE.T_INTEGER_DEFAULT, true);
353+
hasRange := true;
354+
then e;
355+
else exp; end match
356+
for exp in inSubs);
357+
true := hasRange;
358+
then DAE.ASUB(inExp, subs);
359+
342360
else origExp;
343361
end matchcontinue;
344362
end simplifyAsubExp;
@@ -3481,7 +3499,7 @@ protected
34813499
DAE.Type ty;
34823500
algorithm
34833501
// Expand the subscripts.
3484-
indices := List.map(inSubscripts, Expression.splitArray);
3502+
indices := list(Expression.splitArray(simplify1(e)) for e in inSubscripts);
34853503
// Make asubs from all combinations of the subscript indices.
34863504
asubs := List.combinationMap1(indices, simplifyAsubSlicing2, inExp);
34873505
// Make sure one or more dimensions were sliced, i.e. we got more than one element.

0 commit comments

Comments
 (0)