Skip to content

Commit 03e1659

Browse files
stereotype441Commit Queue
authored andcommitted
Flow analysis: reduce the extent to which variable is used by initialize().
In a future CL I will need the ability for flow analysis to initialize promotion keys that aren't associated with any particular variable. In anticipation of that, this CL refactors `FlowAnalysis.initialize` so that it immediately looks up the unpromoted type of the variable (which is the only information it needs), and thereafter just uses the variable's promotion key. From the point of view of flow analysis clients, there is no functional change. Change-Id: I54794bde49c7af745b43a09914f70c9c4e6d48da Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279074 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent a973316 commit 03e1659

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,18 +2178,19 @@ class FlowModel<Type extends Object> {
21782178
FlowModel<Type> write<Variable extends Object>(
21792179
FlowModelHelper<Type> helper,
21802180
NonPromotionReason? nonPromotionReason,
2181-
Variable variable,
21822181
int variableKey,
21832182
Type writtenType,
21842183
SsaNode<Type> newSsaNode,
21852184
Operations<Variable, Type> operations,
2186-
{bool promoteToTypeOfInterest = true}) {
2185+
{bool promoteToTypeOfInterest = true,
2186+
required Type unpromotedType}) {
21872187
FlowModel<Type>? newModel;
21882188
VariableModel<Type>? infoForVar = variableInfo[variableKey];
21892189
if (infoForVar != null) {
2190-
VariableModel<Type> newInfoForVar = infoForVar.write(nonPromotionReason,
2191-
variable, variableKey, writtenType, operations, newSsaNode,
2192-
promoteToTypeOfInterest: promoteToTypeOfInterest);
2190+
VariableModel<Type> newInfoForVar = infoForVar.write(
2191+
nonPromotionReason, variableKey, writtenType, operations, newSsaNode,
2192+
promoteToTypeOfInterest: promoteToTypeOfInterest,
2193+
unpromotedType: unpromotedType);
21932194
if (!identical(newInfoForVar, infoForVar)) {
21942195
newModel = _updateVariableInfo(variableKey, newInfoForVar);
21952196
}
@@ -2863,12 +2864,12 @@ class VariableModel<Type extends Object> {
28632864
/// reason for any potential demotion.
28642865
VariableModel<Type> write<Variable extends Object>(
28652866
NonPromotionReason? nonPromotionReason,
2866-
Variable variable,
28672867
int variableKey,
28682868
Type writtenType,
28692869
Operations<Variable, Type> operations,
28702870
SsaNode<Type> newSsaNode,
2871-
{required bool promoteToTypeOfInterest}) {
2871+
{required bool promoteToTypeOfInterest,
2872+
required Type unpromotedType}) {
28722873
if (writeCaptured) {
28732874
return new VariableModel<Type>(
28742875
promotedTypes: promotedTypes,
@@ -2882,10 +2883,9 @@ class VariableModel<Type extends Object> {
28822883
_demoteViaAssignment(writtenType, operations, nonPromotionReason);
28832884
List<Type>? newPromotedTypes = demotionResult.promotedTypes;
28842885

2885-
Type declaredType = operations.variableType(variable);
28862886
if (promoteToTypeOfInterest) {
28872887
newPromotedTypes = _tryPromoteToTypeOfInterest(
2888-
operations, declaredType, newPromotedTypes, writtenType);
2888+
operations, unpromotedType, newPromotedTypes, writtenType);
28892889
}
28902890
// TODO(paulberry): remove demotions from demotionResult.nonPromotionHistory
28912891
// that are no longer in effect due to re-promotion.
@@ -3949,6 +3949,7 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
39493949
{required bool isFinal,
39503950
required bool isLate,
39513951
required bool isImplicitlyTyped}) {
3952+
Type unpromotedType = operations.variableType(variable);
39523953
int variableKey = promotionKeyStore.keyForVariable(variable);
39533954
ExpressionInfo<Type>? expressionInfo;
39543955
if (isLate) {
@@ -3965,12 +3966,13 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
39653966
SsaNode<Type> newSsaNode = new SsaNode<Type>(
39663967
expressionInfo is _TrivialExpressionInfo ? null : expressionInfo);
39673968
_current = _current.write(
3968-
this, null, variable, variableKey, matchedType, newSsaNode, operations,
3969-
promoteToTypeOfInterest: !isImplicitlyTyped && !isFinal);
3969+
this, null, variableKey, matchedType, newSsaNode, operations,
3970+
promoteToTypeOfInterest: !isImplicitlyTyped && !isFinal,
3971+
unpromotedType: unpromotedType);
39703972
if (isImplicitlyTyped && operations.isTypeParameterType(matchedType)) {
39713973
_current = _current
3972-
.tryPromoteForTypeCheck(
3973-
this, _variableReference(variable, variableKey), matchedType)
3974+
.tryPromoteForTypeCheck(this,
3975+
_variableReference(variableKey, unpromotedType), matchedType)
39743976
.ifTrue;
39753977
}
39763978
}
@@ -4436,11 +4438,12 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
44364438

44374439
@override
44384440
Type? variableRead(Expression expression, Variable variable) {
4441+
Type unpromotedType = operations.variableType(variable);
44394442
int variableKey = promotionKeyStore.keyForVariable(variable);
44404443
VariableModel<Type> variableModel = _current._getInfo(variableKey);
44414444
Type? promotedType = variableModel.promotedTypes?.last;
44424445
_storeExpressionReference(
4443-
expression, _variableReference(variable, variableKey));
4446+
expression, _variableReference(variableKey, unpromotedType));
44444447
ExpressionInfo<Type>? expressionInfo = variableModel.ssaNode?.expressionInfo
44454448
?.rebaseForward(operations, _current);
44464449
if (expressionInfo != null) {
@@ -4505,6 +4508,7 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
45054508
@override
45064509
void write(Node node, Variable variable, Type writtenType,
45074510
Expression? writtenExpression) {
4511+
Type unpromotedType = operations.variableType(variable);
45084512
int variableKey = promotionKeyStore.keyForVariable(variable);
45094513
ExpressionInfo<Type>? expressionInfo = writtenExpression == null
45104514
? null
@@ -4514,11 +4518,11 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
45144518
_current = _current.write(
45154519
this,
45164520
new DemoteViaExplicitWrite<Variable>(variable, node),
4517-
variable,
45184521
variableKey,
45194522
writtenType,
45204523
newSsaNode,
4521-
operations);
4524+
operations,
4525+
unpromotedType: unpromotedType);
45224526
}
45234527

45244528
@override
@@ -4775,9 +4779,9 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
47754779
isPromotable: false, isThisOrSuper: true);
47764780

47774781
ReferenceWithType<Type> _variableReference(
4778-
Variable variable, int variableKey) =>
4782+
int variableKey, Type unpromotedType) =>
47794783
new ReferenceWithType<Type>(variableKey,
4780-
promotedType(variable) ?? operations.variableType(variable),
4784+
_current.infoFor(variableKey).promotedTypes?.last ?? unpromotedType,
47814785
isPromotable: true, isThisOrSuper: false);
47824786
}
47834787

pkg/_fe_analyzer_shared/test/flow_analysis/flow_analysis_test.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7309,12 +7309,7 @@ extension on FlowModel<Type> {
73097309
Var variable,
73107310
Type writtenType,
73117311
SsaNode<Type> newSsaNode) =>
7312-
write(
7313-
h,
7314-
nonPromotionReason,
7315-
variable,
7316-
h.promotionKeyStore.keyForVariable(variable),
7317-
writtenType,
7318-
newSsaNode,
7319-
h.typeOperations);
7312+
write(h, nonPromotionReason, h.promotionKeyStore.keyForVariable(variable),
7313+
writtenType, newSsaNode, h.typeOperations,
7314+
unpromotedType: variable.type);
73207315
}

0 commit comments

Comments
 (0)