Improve SameValue folding in TypedOptimization to ignore renames

R=jarin@google.com, tebbi@google.com

Change-Id: I23b92df275ce294d62c906a0b94dcb9b15f6be39
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1609803
Commit-Queue: Georg Schmid <gsps@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61472}
diff --git a/src/compiler/backend/instruction-selector.cc b/src/compiler/backend/instruction-selector.cc
index 97f9127..459f9a2 100644
--- a/src/compiler/backend/instruction-selector.cc
+++ b/src/compiler/backend/instruction-selector.cc
@@ -2842,7 +2842,7 @@
 
 void InstructionSelector::VisitStaticAssert(Node* node) {
   node->InputAt(0)->Print();
-  FATAL("Expected static assert to hold, but got non-true input!\n");
+  FATAL("Expected turbofan static assert to hold, but got non-true input!\n");
 }
 
 void InstructionSelector::VisitDeadValue(Node* node) {
diff --git a/src/compiler/js-intrinsic-lowering.cc b/src/compiler/js-intrinsic-lowering.cc
index f3b2b31..a143eda 100644
--- a/src/compiler/js-intrinsic-lowering.cc
+++ b/src/compiler/js-intrinsic-lowering.cc
@@ -28,7 +28,8 @@
   if (node->opcode() != IrOpcode::kJSCallRuntime) return NoChange();
   const Runtime::Function* const f =
       Runtime::FunctionForId(CallRuntimeParametersOf(node->op()).id());
-  if (f->function_id == Runtime::kStaticAssert) return ReduceStaticAssert(node);
+  if (f->function_id == Runtime::kTurbofanStaticAssert)
+    return ReduceTurbofanStaticAssert(node);
   if (f->intrinsic_type != Runtime::IntrinsicType::INLINE) return NoChange();
   switch (f->function_id) {
     case Runtime::kInlineCopyDataProperties:
@@ -269,7 +270,7 @@
   return Change(node, simplified()->ObjectIsSmi());
 }
 
-Reduction JSIntrinsicLowering::ReduceStaticAssert(Node* node) {
+Reduction JSIntrinsicLowering::ReduceTurbofanStaticAssert(Node* node) {
   if (FLAG_always_opt) {
     // Ignore static asserts, as we most likely won't have enough information
     RelaxEffectsAndControls(node);
diff --git a/src/compiler/js-intrinsic-lowering.h b/src/compiler/js-intrinsic-lowering.h
index 36cc121..2162a09 100644
--- a/src/compiler/js-intrinsic-lowering.h
+++ b/src/compiler/js-intrinsic-lowering.h
@@ -58,7 +58,7 @@
   Reduction ReduceIsInstanceType(Node* node, InstanceType instance_type);
   Reduction ReduceIsJSReceiver(Node* node);
   Reduction ReduceIsSmi(Node* node);
-  Reduction ReduceStaticAssert(Node* node);
+  Reduction ReduceTurbofanStaticAssert(Node* node);
   Reduction ReduceToLength(Node* node);
   Reduction ReduceToObject(Node* node);
   Reduction ReduceToString(Node* node);
diff --git a/src/compiler/typed-optimization.cc b/src/compiler/typed-optimization.cc
index a555d7f..556fec5 100644
--- a/src/compiler/typed-optimization.cc
+++ b/src/compiler/typed-optimization.cc
@@ -113,6 +113,26 @@
   return {};
 }
 
+Node* ResolveRenames(Node* node) {
+  while (true) {
+    switch (node->opcode()) {
+      case IrOpcode::kCheckHeapObject:
+      case IrOpcode::kCheckNumber:
+      case IrOpcode::kCheckSmi:
+      case IrOpcode::kFinishRegion:
+      case IrOpcode::kTypeGuard:
+        if (node->IsDead()) {
+          return node;
+        } else {
+          node = node->InputAt(0);
+          continue;
+        }
+      default:
+        return node;
+    }
+  }
+}
+
 }  // namespace
 
 Reduction TypedOptimization::ReduceConvertReceiver(Node* node) {
@@ -507,7 +527,10 @@
   Node* const rhs = NodeProperties::GetValueInput(node, 1);
   Type const lhs_type = NodeProperties::GetType(lhs);
   Type const rhs_type = NodeProperties::GetType(rhs);
-  if (lhs == rhs) {
+  if (ResolveRenames(lhs) == ResolveRenames(rhs)) {
+    if (NodeProperties::GetType(node).IsNone()) {
+      return NoChange();
+    }
     // SameValue(x,x) => #true
     return Replace(jsgraph()->TrueConstant());
   } else if (lhs_type.Is(Type::Unique()) && rhs_type.Is(Type::Unique())) {
diff --git a/src/runtime/runtime-test.cc b/src/runtime/runtime-test.cc
index b709f0c..faff3d7 100644
--- a/src/runtime/runtime-test.cc
+++ b/src/runtime/runtime-test.cc
@@ -1277,7 +1277,7 @@
   return ReadOnlyRoots(isolate).undefined_value();
 }
 
-RUNTIME_FUNCTION(Runtime_StaticAssert) {
+RUNTIME_FUNCTION(Runtime_TurbofanStaticAssert) {
   SealHandleScope shs(isolate);
   // Always lowered to StaticAssert node in Turbofan, so we should never get
   // here in compiled code.
diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h
index aa21ff0..f7c4982 100644
--- a/src/runtime/runtime.h
+++ b/src/runtime/runtime.h
@@ -519,7 +519,7 @@
   F(WasmNumInterpretedCalls, 1, 1)            \
   F(WasmTraceMemory, 1, 1)                    \
   F(SetWasmThreadsEnabled, 1, 1)              \
-  F(StaticAssert, 1, 1)                       \
+  F(TurbofanStaticAssert, 1, 1)               \
   F(EnableCodeLoggingForTesting, 0, 1)
 
 #define FOR_EACH_INTRINSIC_TYPEDARRAY(F, I) \
diff --git a/test/mjsunit/compiler/constant-fold-add-static.js b/test/mjsunit/compiler/constant-fold-add-static.js
index 5b148da..cdeb7f2f 100644
--- a/test/mjsunit/compiler/constant-fold-add-static.js
+++ b/test/mjsunit/compiler/constant-fold-add-static.js
@@ -7,7 +7,7 @@
 // Check that constant-folding of arithmetic results in identical nodes.
 (function() {
   function foo(x) {
-    %StaticAssert(1 * x == x + 0);
+    %TurbofanStaticAssert(1 * x == x + 0);
   }
   foo(121);
   foo(122);