[dart2js] Move Dart2js const conditional simplifier to phase 0b, the CFE linker.

Change-Id: I9ed7f46c5641c28dc71b7712dbd4e8d2062b1f4d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332820
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
diff --git a/pkg/compiler/lib/src/compiler.dart b/pkg/compiler/lib/src/compiler.dart
index ee7c2af..7eaa16e 100644
--- a/pkg/compiler/lib/src/compiler.dart
+++ b/pkg/compiler/lib/src/compiler.dart
@@ -47,14 +47,12 @@
     show GlobalTypeInferenceResults, GlobalTypeInferenceTask;
 import 'inferrer/wrapped.dart' show WrappedAbstractValueStrategy;
 import 'io/source_information.dart';
-import 'ir/annotations.dart';
 import 'js_backend/codegen_inputs.dart' show CodegenInputs;
 import 'js_backend/enqueuer.dart';
 import 'js_backend/inferred_data.dart';
 import 'js_model/js_strategy.dart';
 import 'js_model/js_world.dart';
 import 'js_model/locals.dart';
-import 'kernel/dart2js_target.dart';
 import 'kernel/front_end_adapter.dart' show CompilerFileSystem;
 import 'kernel/kernel_strategy.dart';
 import 'kernel/kernel_world.dart';
@@ -437,34 +435,6 @@
   bool shouldStopAfterLoadKernel(load_kernel.Output? output) =>
       output == null || compilationFailed || stage.shouldOnlyComputeDill;
 
-  void simplifyConstConditionals(ir.Component component) {
-    void reportMessage(
-        fe.LocatedMessage message, List<fe.LocatedMessage>? context) {
-      reportLocatedMessage(reporter, message, context);
-    }
-
-    bool shouldNotInline(ir.TreeNode node) {
-      if (node is! ir.Annotatable) {
-        return false;
-      }
-      return computePragmaAnnotationDataFromIr(node).any((pragma) =>
-          pragma == const PragmaAnnotationData('noInline') ||
-          pragma == const PragmaAnnotationData('never-inline'));
-    }
-
-    fe.ConstConditionalSimplifier(
-            const Dart2jsDartLibrarySupport(),
-            const Dart2jsConstantsBackend(supportsUnevaluatedConstants: false),
-            component,
-            reportMessage,
-            environmentDefines: environment.definitions,
-            evaluationMode: options.useLegacySubtyping
-                ? fe.EvaluationMode.weak
-                : fe.EvaluationMode.strong,
-            shouldNotInline: shouldNotInline)
-        .run();
-  }
-
   GlobalTypeInferenceResults performGlobalTypeInference(
       JClosedWorld closedWorld) {
     FunctionEntity mainFunction = closedWorld.elementEnvironment.mainFunction!;
@@ -551,11 +521,6 @@
     ir.Component component = output.component;
     JClosedWorld? closedWorld;
     if (!stage.shouldReadClosedWorld) {
-      // If we're deserializing the closed world, the input .dill already
-      // contains the modified AST, so the transformer only needs to run if
-      // the closed world is being computed from scratch.
-      simplifyConstConditionals(component);
-
       Uri rootLibraryUri = output.rootLibraryUri!;
       List<Uri> libraries = output.libraries!;
       closedWorld = computeClosedWorld(component, rootLibraryUri, libraries);
diff --git a/pkg/compiler/lib/src/phase/load_kernel.dart b/pkg/compiler/lib/src/phase/load_kernel.dart
index 135fb6c..6c1027e 100644
--- a/pkg/compiler/lib/src/phase/load_kernel.dart
+++ b/pkg/compiler/lib/src/phase/load_kernel.dart
@@ -22,9 +22,14 @@
 import '../common.dart';
 import '../diagnostics/diagnostic_listener.dart';
 import '../environment.dart';
+import '../ir/annotations.dart';
 import '../ir/constants.dart';
 import '../kernel/dart2js_target.dart'
-    show Dart2jsTarget, implicitlyUsedLibraries;
+    show
+        Dart2jsConstantsBackend,
+        Dart2jsDartLibrarySupport,
+        Dart2jsTarget,
+        implicitlyUsedLibraries;
 import '../kernel/front_end_adapter.dart';
 import '../kernel/transformations/global/transform.dart' as globalTransforms;
 import '../options.dart';
@@ -113,6 +118,36 @@
   _LoadFromKernelResult(this.component, this.entryLibrary);
 }
 
+void _simplifyConstConditionals(ir.Component component, CompilerOptions options,
+    ir.ClassHierarchy classHierarchy, DiagnosticReporter reporter) {
+  void reportMessage(
+      fe.LocatedMessage message, List<fe.LocatedMessage>? context) {
+    reportLocatedMessage(reporter, message, context);
+  }
+
+  bool shouldNotInline(ir.TreeNode node) {
+    if (node is! ir.Annotatable) {
+      return false;
+    }
+    return computePragmaAnnotationDataFromIr(node).any((pragma) =>
+        pragma == const PragmaAnnotationData('noInline') ||
+        pragma == const PragmaAnnotationData('never-inline'));
+  }
+
+  fe.ConstConditionalSimplifier(
+          const Dart2jsDartLibrarySupport(),
+          const Dart2jsConstantsBackend(supportsUnevaluatedConstants: false),
+          component,
+          reportMessage,
+          environmentDefines: options.environment,
+          classHierarchy: classHierarchy,
+          evaluationMode: options.useLegacySubtyping
+              ? fe.EvaluationMode.weak
+              : fe.EvaluationMode.strong,
+          shouldNotInline: shouldNotInline)
+      .run();
+}
+
 // Perform any backend-specific transforms here that can be done on both
 // serialized components and components from source.
 void _doTransformsOnKernelLoad(
@@ -142,6 +177,7 @@
         .visitComponent(component);
     globalTransforms.transformLibraries(
         component.libraries, constantsEvaluator, coreTypes, options);
+    _simplifyConstConditionals(component, options, classHierarchy, reporter);
   }
 }