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

Commit 7d0153e

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Simplification improvements.
- Move simplification of sum/product from the typing phase to the model simplification phase, so that it's done after loop unrolling. - Reevaluate the type of simplified crefs and ranges, since their types can depend on the simplified expressions. Belonging to [master]: - #2550
1 parent 582a038 commit 7d0153e

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

Compiler/NFFrontEnd/NFBuiltinCall.mo

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -710,29 +710,8 @@ protected
710710
(arg, ty, variability) := Typing.typeExp(listHead(args), origin, info);
711711
ty := Type.arrayElementType(ty);
712712

713-
if intBitAnd(origin, intBitOr(ExpOrigin.FUNCTION, ExpOrigin.ALGORITHM)) == 0 then
714-
(arg, expanded) := ExpandExp.expand(arg);
715-
else
716-
expanded := false;
717-
end if;
718-
719-
if expanded then
720-
args := Expression.arrayScalarElements(arg);
721-
722-
if listEmpty(args) then
723-
callExp := Expression.makeZero(ty);
724-
else
725-
callExp :: args := args;
726-
op := Operator.makeAdd(ty);
727-
728-
for e in args loop
729-
callExp := Expression.BINARY(callExp, op, e);
730-
end for;
731-
end if;
732-
else
733-
{fn} := Function.typeRefCache(fn_ref);
734-
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
735-
end if;
713+
{fn} := Function.typeRefCache(fn_ref);
714+
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
736715
end typeSumCall;
737716

738717
function typeProductCall
@@ -762,29 +741,8 @@ protected
762741
(arg, ty, variability) := Typing.typeExp(listHead(args), origin, info);
763742
ty := Type.arrayElementType(ty);
764743

765-
if intBitAnd(origin, intBitOr(ExpOrigin.FUNCTION, ExpOrigin.ALGORITHM)) == 0 then
766-
(arg, expanded) := ExpandExp.expand(arg);
767-
else
768-
expanded := false;
769-
end if;
770-
771-
if expanded then
772-
args := Expression.arrayScalarElements(arg);
773-
774-
if listEmpty(args) then
775-
callExp := Expression.makeOne(ty);
776-
else
777-
callExp :: args := args;
778-
op := Operator.makeMul(ty);
779-
780-
for e in args loop
781-
callExp := Expression.BINARY(callExp, op, e);
782-
end for;
783-
end if;
784-
else
785-
{fn} := Function.typeRefCache(fn_ref);
786-
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
787-
end if;
744+
{fn} := Function.typeRefCache(fn_ref);
745+
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
788746
end typeProductCall;
789747

790748
function typeSmoothCall

Compiler/NFFrontEnd/NFSimplifyExp.mo

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import NFCeval.EvalTarget;
4545
import NFFunction.Function;
4646
import ComponentRef = NFComponentRef;
4747
import ExpandExp = NFExpandExp;
48+
import TypeCheck = NFTypeCheck;
49+
import Absyn;
4850

4951
public
5052

@@ -55,6 +57,7 @@ algorithm
5557
case Expression.CREF()
5658
algorithm
5759
exp.cref := ComponentRef.simplifySubscripts(exp.cref);
60+
exp.ty := ComponentRef.getSubscriptedType(exp.cref);
5861
then
5962
exp;
6063

@@ -65,7 +68,7 @@ algorithm
6568
exp;
6669

6770
case Expression.RANGE()
68-
then Expression.RANGE(exp.ty, simplify(exp.start), simplifyOpt(exp.step), simplify(exp.stop));
71+
then simplifyRange(exp);
6972

7073
case Expression.RECORD()
7174
algorithm
@@ -102,6 +105,31 @@ algorithm
102105
end match;
103106
end simplifyOpt;
104107

108+
function simplifyRange
109+
input Expression range;
110+
output Expression exp;
111+
protected
112+
Expression start_exp1, stop_exp1, start_exp2, stop_exp2;
113+
Option<Expression> step_exp1, step_exp2;
114+
Type ty;
115+
algorithm
116+
Expression.RANGE(ty = ty, start = start_exp1, step = step_exp1, stop = stop_exp1) := range;
117+
118+
start_exp2 := simplify(start_exp1);
119+
step_exp2 := simplifyOpt(step_exp1);
120+
stop_exp2 := simplify(stop_exp1);
121+
122+
if referenceEq(start_exp1, start_exp2) and
123+
referenceEq(step_exp1, step_exp2) and
124+
referenceEq(stop_exp1, stop_exp2) then
125+
exp := range;
126+
else
127+
ty := TypeCheck.getRangeType(start_exp2, step_exp2, stop_exp2,
128+
Type.arrayElementType(ty), Absyn.dummyInfo);
129+
exp := Expression.RANGE(ty, start_exp2, step_exp2, stop_exp2);
130+
end if;
131+
end simplifyRange;
132+
105133
function simplifyCall
106134
input output Expression callExp;
107135
protected
@@ -151,10 +179,46 @@ algorithm
151179
then
152180
exp;
153181

182+
case "sum" then simplifySumProduct(listHead(args), call, isSum = true);
183+
case "product" then simplifySumProduct(listHead(args), call, isSum = false);
184+
154185
else Expression.CALL(call);
155186
end match;
156187
end simplifyBuiltinCall;
157188

189+
function simplifySumProduct
190+
input Expression arg;
191+
input Call call;
192+
input Boolean isSum;
193+
output Expression exp;
194+
protected
195+
Boolean expanded;
196+
list<Expression> args;
197+
Type ty;
198+
Operator op;
199+
algorithm
200+
(exp, expanded) := ExpandExp.expand(arg);
201+
202+
if expanded then
203+
args := Expression.arrayScalarElements(exp);
204+
ty := Expression.typeOf(arg);
205+
206+
if listEmpty(args) then
207+
exp := if isSum then Expression.makeZero(ty) else Expression.makeOne(ty);
208+
else
209+
exp :: args := args;
210+
op := if isSum then Operator.makeAdd(Type.arrayElementType(ty)) else
211+
Operator.makeMul(Type.arrayElementType(ty));
212+
213+
for e in args loop
214+
exp := Expression.BINARY(exp, op, e);
215+
end for;
216+
end if;
217+
else
218+
exp := Expression.CALL(call);
219+
end if;
220+
end simplifySumProduct;
221+
158222
function simplifySize
159223
input output Expression sizeExp;
160224
algorithm

0 commit comments

Comments
 (0)