Move registrations on OutputUnitData to the impact transformer

Change-Id: I8060149b217d3222c7e784aafe9724e5ef6a7bde
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103530
Reviewed-by: Sigmund Cherem <sigmund@google.com>
diff --git a/pkg/compiler/lib/src/deferred_load.dart b/pkg/compiler/lib/src/deferred_load.dart
index 5c32df6..44ce231 100644
--- a/pkg/compiler/lib/src/deferred_load.dart
+++ b/pkg/compiler/lib/src/deferred_load.dart
@@ -1456,9 +1456,9 @@
 
   /// Registers that a constant is used in the same deferred output unit as
   /// [field].
-  void registerConstantDeferredUse(
-      DeferredGlobalConstantValue constant, OutputUnit unit) {
+  void registerConstantDeferredUse(DeferredGlobalConstantValue constant) {
     if (!isProgramSplit) return;
+    OutputUnit unit = constant.unit;
     assert(
         _constantToUnit[constant] == null || _constantToUnit[constant] == unit);
     _constantToUnit[constant] = unit;
diff --git a/pkg/compiler/lib/src/js_backend/impact_transformer.dart b/pkg/compiler/lib/src/js_backend/impact_transformer.dart
index 1231590..da83817 100644
--- a/pkg/compiler/lib/src/js_backend/impact_transformer.dart
+++ b/pkg/compiler/lib/src/js_backend/impact_transformer.dart
@@ -13,16 +13,17 @@
 import '../common/resolution.dart' show ResolutionImpact;
 import '../common_elements.dart' show ElementEnvironment;
 import '../constants/expressions.dart';
+import '../constants/values.dart';
 import '../elements/entities.dart';
 import '../elements/types.dart';
 import '../js_emitter/native_emitter.dart';
+import '../js_model/elements.dart';
 import '../native/enqueue.dart';
 import '../native/behavior.dart';
 import '../options.dart';
 import '../universe/feature.dart';
 import '../universe/selector.dart';
-import '../universe/use.dart'
-    show StaticUse, StaticUseKind, TypeUse, TypeUseKind;
+import '../universe/use.dart';
 import '../universe/world_impact.dart' show TransformedWorldImpact, WorldImpact;
 import '../util/util.dart';
 import '../world.dart';
@@ -399,6 +400,17 @@
       }
     }
 
+    for (ConstantUse constantUse in impact.constantUses) {
+      switch (constantUse.value.kind) {
+        case ConstantValueKind.DEFERRED_GLOBAL:
+          _closedWorld.outputUnitData
+              .registerConstantDeferredUse(constantUse.value);
+          break;
+        default:
+          break;
+      }
+    }
+
     for (Pair<DartType, DartType> check
         in impact.typeVariableBoundsSubtypeChecks) {
       _rtiChecksBuilder.registerTypeVariableBoundsSubtypeCheck(
@@ -406,12 +418,39 @@
     }
 
     for (StaticUse staticUse in impact.staticUses) {
-      if (staticUse.kind == StaticUseKind.CALL_METHOD) {
-        FunctionEntity callMethod = staticUse.element;
-        if (_rtiNeed.methodNeedsSignature(callMethod)) {
-          _impacts.computeSignature
-              .registerImpact(transformed, _elementEnvironment);
-        }
+      switch (staticUse.kind) {
+        case StaticUseKind.GENERATOR_BODY_INVOKE:
+          JGeneratorBody generatorBody = staticUse.element;
+          _closedWorld.outputUnitData
+              .registerColocatedMembers(generatorBody.function, generatorBody);
+          break;
+        case StaticUseKind.CALL_METHOD:
+          FunctionEntity callMethod = staticUse.element;
+          if (_rtiNeed.methodNeedsSignature(callMethod)) {
+            _impacts.computeSignature
+                .registerImpact(transformed, _elementEnvironment);
+          }
+          break;
+        case StaticUseKind.STATIC_TEAR_OFF:
+        case StaticUseKind.INSTANCE_FIELD_GET:
+        case StaticUseKind.INSTANCE_FIELD_SET:
+        case StaticUseKind.SUPER_INVOKE:
+        case StaticUseKind.STATIC_INVOKE:
+        case StaticUseKind.SUPER_FIELD_SET:
+        case StaticUseKind.SUPER_SETTER_SET:
+        case StaticUseKind.STATIC_SET:
+        case StaticUseKind.SUPER_TEAR_OFF:
+        case StaticUseKind.SUPER_GET:
+        case StaticUseKind.STATIC_GET:
+        case StaticUseKind.FIELD_INIT:
+        case StaticUseKind.FIELD_CONSTANT_INIT:
+        case StaticUseKind.CONSTRUCTOR_INVOKE:
+        case StaticUseKind.CONST_CONSTRUCTOR_INVOKE:
+        case StaticUseKind.DIRECT_INVOKE:
+        case StaticUseKind.INLINING:
+        case StaticUseKind.CLOSURE:
+        case StaticUseKind.CLOSURE_CALL:
+          break;
       }
     }
 
diff --git a/pkg/compiler/lib/src/ssa/builder_kernel.dart b/pkg/compiler/lib/src/ssa/builder_kernel.dart
index 0bd6b75..f81349e 100644
--- a/pkg/compiler/lib/src/ssa/builder_kernel.dart
+++ b/pkg/compiler/lib/src/ssa/builder_kernel.dart
@@ -12,6 +12,7 @@
 import '../common_elements.dart';
 import '../constants/constant_system.dart' as constant_system;
 import '../constants/values.dart';
+import '../deferred_load.dart';
 import '../dump_info.dart';
 import '../elements/entities.dart';
 import '../elements/jumps.dart';
@@ -1358,7 +1359,6 @@
     }
 
     JGeneratorBody body = _elementMap.getGeneratorBody(function);
-    closedWorld.outputUnitData.registerColocatedMembers(function, body);
     push(new HInvokeGeneratorBody(
         body,
         inputs,
@@ -1734,11 +1734,13 @@
         _sourceInformationBuilder.buildGet(node);
     if (!closedWorld.outputUnitData
         .hasOnlyNonDeferredImportPathsToConstant(targetElement, value)) {
+      OutputUnit outputUnit =
+          closedWorld.outputUnitData.outputUnitForConstant(value);
+      ConstantValue deferredConstant =
+          new DeferredGlobalConstantValue(value, outputUnit);
+      registry.registerConstantUse(new ConstantUse.deferred(deferredConstant));
       stack.add(graph.addDeferredConstant(
-          value,
-          closedWorld.outputUnitData.outputUnitForConstant(value),
-          sourceInformation,
-          closedWorld));
+          deferredConstant, sourceInformation, closedWorld));
     } else {
       stack.add(graph.addConstant(value, closedWorld,
           sourceInformation: sourceInformation));
@@ -3297,15 +3299,20 @@
         push(new HStatic(field, _typeInferenceMap.getInferredTypeOf(field),
             sourceInformation));
       } else if (fieldData.isEffectivelyConstant) {
-        var unit = closedWorld.outputUnitData.outputUnitForMember(field);
+        OutputUnit outputUnit =
+            closedWorld.outputUnitData.outputUnitForMember(field);
         // TODO(sigmund): this is not equivalent to what the old FE does: if
         // there is no prefix the old FE wouldn't treat this in any special
         // way. Also, if the prefix points to a constant in the main output
         // unit, the old FE would still generate a deferred wrapper here.
         if (!closedWorld.outputUnitData
             .hasOnlyNonDeferredImportPaths(targetElement, field)) {
+          ConstantValue deferredConstant = new DeferredGlobalConstantValue(
+              fieldData.initialValue, outputUnit);
+          registry
+              .registerConstantUse(new ConstantUse.deferred(deferredConstant));
           stack.add(graph.addDeferredConstant(
-              fieldData.initialValue, unit, sourceInformation, closedWorld));
+              deferredConstant, sourceInformation, closedWorld));
         } else {
           stack.add(graph.addConstant(fieldData.initialValue, closedWorld,
               sourceInformation: sourceInformation));
diff --git a/pkg/compiler/lib/src/ssa/nodes.dart b/pkg/compiler/lib/src/ssa/nodes.dart
index e6bb87a..cee6360 100644
--- a/pkg/compiler/lib/src/ssa/nodes.dart
+++ b/pkg/compiler/lib/src/ssa/nodes.dart
@@ -8,7 +8,6 @@
 import '../common.dart';
 import '../constants/constant_system.dart' as constant_system;
 import '../constants/values.dart';
-import '../deferred_load.dart' show OutputUnit;
 import '../elements/entities.dart';
 import '../elements/jumps.dart';
 import '../elements/types.dart';
@@ -283,11 +282,9 @@
     return result;
   }
 
-  HConstant addDeferredConstant(ConstantValue constant, OutputUnit unit,
+  HConstant addDeferredConstant(DeferredGlobalConstantValue constant,
       SourceInformation sourceInformation, JClosedWorld closedWorld) {
-    ConstantValue wrapper = new DeferredGlobalConstantValue(constant, unit);
-    closedWorld.outputUnitData.registerConstantDeferredUse(wrapper, unit);
-    return addConstant(wrapper, closedWorld,
+    return addConstant(constant, closedWorld,
         sourceInformation: sourceInformation);
   }
 
diff --git a/pkg/compiler/lib/src/universe/codegen_world_builder.dart b/pkg/compiler/lib/src/universe/codegen_world_builder.dart
index afe16e0..a4c0dbf 100644
--- a/pkg/compiler/lib/src/universe/codegen_world_builder.dart
+++ b/pkg/compiler/lib/src/universe/codegen_world_builder.dart
@@ -345,6 +345,7 @@
         useSet.addAll(
             usage.invoke(Accesses.superAccess, staticUse.callStructure));
         break;
+      case StaticUseKind.GENERATOR_BODY_INVOKE:
       case StaticUseKind.STATIC_INVOKE:
         registerStaticInvocation(staticUse);
         useSet.addAll(
diff --git a/pkg/compiler/lib/src/universe/resolution_world_builder.dart b/pkg/compiler/lib/src/universe/resolution_world_builder.dart
index 82cc3a2..19d9030 100644
--- a/pkg/compiler/lib/src/universe/resolution_world_builder.dart
+++ b/pkg/compiler/lib/src/universe/resolution_world_builder.dart
@@ -688,6 +688,7 @@
         break;
       case StaticUseKind.INLINING:
       case StaticUseKind.CALL_METHOD:
+      case StaticUseKind.GENERATOR_BODY_INVOKE:
         failedAt(CURRENT_ELEMENT_SPANNABLE,
             "Static use ${staticUse.kind} is not supported during resolution.");
     }
diff --git a/pkg/compiler/lib/src/universe/use.dart b/pkg/compiler/lib/src/universe/use.dart
index d062fc5..7fa2550 100644
--- a/pkg/compiler/lib/src/universe/use.dart
+++ b/pkg/compiler/lib/src/universe/use.dart
@@ -158,6 +158,7 @@
   CONST_CONSTRUCTOR_INVOKE,
   DIRECT_INVOKE,
   INLINING,
+  GENERATOR_BODY_INVOKE,
   STATIC_INVOKE,
   STATIC_GET,
   STATIC_SET,
@@ -488,7 +489,7 @@
   /// Direct invocation of a generator (body) [element], as a static call or
   /// through a this or super constructor call.
   factory StaticUse.generatorBodyInvoke(FunctionEntity element) {
-    return new StaticUse.internal(element, StaticUseKind.STATIC_INVOKE,
+    return new StaticUse.internal(element, StaticUseKind.GENERATOR_BODY_INVOKE,
         callStructure: CallStructure.NO_ARGS);
   }
 
@@ -901,9 +902,12 @@
   /// Type constant used for registration of custom elements.
   ConstantUse.customElements(TypeConstantValue value) : this._(value);
 
-  /// Constant literal used on code.
+  /// Constant literal used in code.
   ConstantUse.literal(ConstantValue value) : this._(value);
 
+  /// Deferred constant used in code.
+  ConstantUse.deferred(DeferredGlobalConstantValue value) : this._(value);
+
   @override
   bool operator ==(other) {
     if (identical(this, other)) return true;