[turbofan] Lower NumberConstant nodes to IntPtrConstant.

If a NumberConstant can be represented as a Smi, then lower it to a
IntPtrConstant node during simplified lowering. Thanks to this, all backends can
match Smi values that can also be encoded as immediates in the instruction
selector. Additionally, we can apply the same lowering to the CodeAssembler for
the snapshot.

As a result, we can remove `mov` instructions generated because Int32Matcher and
Int64Matcher didn't not recognize Smis:

For 32-bit target, it's common for Smis also be immediates: "if (a < 100) {}"
~~~
mov r1, #200 -> cmp r0, #200
cmp r0, r1   -> blt <>
blt <>       ->
~~~

On Arm64 particularly, we lose opportunites to use `cbz`: "if (a == 0) {}"
~~~
movz x0, #0x0 -> cbz x1 <>
cmp x1, x0    ->
b.eq <>       ->
~~~

Overall, we do not see an impact on benchmarks such as webtooling. However, we
do see noteworthy code size reduction, from 0.5% to 1.5%.

Bug: 
Change-Id: I7fbb718ad51b9036c3514fa31c1326bdd6f2b0e6
Reviewed-on: https://chromium-review.googlesource.com/848814
Reviewed-by: Jaroslav Sevcik <jarin@chromium.org>
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Pierre Langlois <pierre.langlois@arm.com>
Cr-Commit-Position: refs/heads/master@{#50569}
diff --git a/src/compiler/code-assembler.cc b/src/compiler/code-assembler.cc
index 6f4dd2b..071f895 100644
--- a/src/compiler/code-assembler.cc
+++ b/src/compiler/code-assembler.cc
@@ -245,7 +245,12 @@
 }
 
 TNode<Number> CodeAssembler::NumberConstant(double value) {
-  return UncheckedCast<Number>(raw_assembler()->NumberConstant(value));
+  int smi_value;
+  if (DoubleToSmiInteger(value, &smi_value)) {
+    return UncheckedCast<Number>(SmiConstant(smi_value));
+  } else {
+    return UncheckedCast<Number>(raw_assembler()->NumberConstant(value));
+  }
 }
 
 TNode<Smi> CodeAssembler::SmiConstant(Smi* value) {
diff --git a/src/compiler/simplified-lowering.cc b/src/compiler/simplified-lowering.cc
index 8789453..46e2446 100644
--- a/src/compiler/simplified-lowering.cc
+++ b/src/compiler/simplified-lowering.cc
@@ -1497,8 +1497,20 @@
         return VisitLeaf(node, MachineRepresentation::kWord64);
       case IrOpcode::kExternalConstant:
         return VisitLeaf(node, MachineType::PointerRepresentation());
-      case IrOpcode::kNumberConstant:
-        return VisitLeaf(node, MachineRepresentation::kTagged);
+      case IrOpcode::kNumberConstant: {
+        double const value = OpParameter<double>(node);
+        int value_as_int;
+        if (DoubleToSmiInteger(value, &value_as_int)) {
+          VisitLeaf(node, MachineRepresentation::kTaggedSigned);
+          if (lower()) {
+            intptr_t smi = bit_cast<intptr_t>(Smi::FromInt(value_as_int));
+            DeferReplacement(node, lowering->jsgraph()->IntPtrConstant(smi));
+          }
+          return;
+        }
+        VisitLeaf(node, MachineRepresentation::kTagged);
+        return;
+      }
       case IrOpcode::kHeapConstant:
         return VisitLeaf(node, MachineRepresentation::kTaggedPointer);
       case IrOpcode::kPointerConstant: {
diff --git a/src/compiler/simplified-lowering.h b/src/compiler/simplified-lowering.h
index eaa148e..a78d885 100644
--- a/src/compiler/simplified-lowering.h
+++ b/src/compiler/simplified-lowering.h
@@ -20,7 +20,7 @@
 class SourcePositionTable;
 class TypeCache;
 
-class SimplifiedLowering final {
+class V8_EXPORT_PRIVATE SimplifiedLowering final {
  public:
   SimplifiedLowering(JSGraph* jsgraph, Zone* zone,
                      SourcePositionTable* source_positions);
diff --git a/test/unittests/BUILD.gn b/test/unittests/BUILD.gn
index d145fcf..7f70a5c 100644
--- a/test/unittests/BUILD.gn
+++ b/test/unittests/BUILD.gn
@@ -122,6 +122,7 @@
     "compiler/schedule-unittest.cc",
     "compiler/scheduler-rpo-unittest.cc",
     "compiler/scheduler-unittest.cc",
+    "compiler/simplified-lowering-unittest.cc",
     "compiler/simplified-operator-reducer-unittest.cc",
     "compiler/simplified-operator-unittest.cc",
     "compiler/state-values-utils-unittest.cc",
diff --git a/test/unittests/compiler/simplified-lowering-unittest.cc b/test/unittests/compiler/simplified-lowering-unittest.cc
new file mode 100644
index 0000000..5f5afe5
--- /dev/null
+++ b/test/unittests/compiler/simplified-lowering-unittest.cc
@@ -0,0 +1,89 @@
+// Copyright 2017 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/compiler/simplified-lowering.h"
+
+#include "src/compiler/compiler-source-position-table.h"
+#include "src/compiler/machine-operator.h"
+#include "src/compiler/simplified-operator.h"
+
+#include "test/unittests/compiler/graph-unittest.h"
+#include "test/unittests/compiler/node-test-utils.h"
+
+namespace v8 {
+namespace internal {
+namespace compiler {
+
+class SimplifiedLoweringTest : public GraphTest {
+ public:
+  explicit SimplifiedLoweringTest(int num_parameters = 1)
+      : GraphTest(num_parameters),
+        num_parameters_(num_parameters),
+        machine_(zone()),
+        javascript_(zone()),
+        simplified_(zone()),
+        jsgraph_(isolate(), graph(), common(), &javascript_, &simplified_,
+                 &machine_) {}
+  ~SimplifiedLoweringTest() override {}
+
+  void LowerGraph(Node* node) {
+    // Make sure we always start with an empty graph.
+    graph()->SetStart(graph()->NewNode(common()->Start(num_parameters())));
+    graph()->SetEnd(graph()->NewNode(common()->End(1), graph()->start()));
+
+    // Return {node} directly, so that we can match it with
+    // "IsReturn(expected)".
+    Node* zero = graph()->NewNode(common()->NumberConstant(0));
+    Node* ret = graph()->NewNode(common()->Return(), zero, node,
+                                 graph()->start(), graph()->start());
+    NodeProperties::MergeControlToEnd(graph(), common(), ret);
+
+    {
+      // Simplified lowering needs to run w/o the typer decorator so make sure
+      // the object is not live at the same time.
+      Typer typer(isolate(), Typer::kNoFlags, graph());
+      typer.Run();
+    }
+
+    SimplifiedLowering lowering(jsgraph(), zone(), source_positions());
+    lowering.LowerAllNodes();
+  }
+
+  int num_parameters() const { return num_parameters_; }
+  JSGraph* jsgraph() { return &jsgraph_; }
+
+ private:
+  const int num_parameters_;
+  MachineOperatorBuilder machine_;
+  JSOperatorBuilder javascript_;
+  SimplifiedOperatorBuilder simplified_;
+  JSGraph jsgraph_;
+};
+
+const int kSmiValues[] = {Smi::kMinValue,
+                          Smi::kMinValue + 1,
+                          Smi::kMinValue + 2,
+                          3,
+                          2,
+                          1,
+                          0,
+                          -1,
+                          -2,
+                          -3,
+                          Smi::kMaxValue - 2,
+                          Smi::kMaxValue - 1,
+                          Smi::kMaxValue};
+
+TEST_F(SimplifiedLoweringTest, SmiConstantToIntPtrConstant) {
+  TRACED_FOREACH(int, x, kSmiValues) {
+    LowerGraph(jsgraph()->Constant(x));
+    intptr_t smi = bit_cast<intptr_t>(Smi::FromInt(x));
+    EXPECT_THAT(graph()->end()->InputAt(1),
+                IsReturn(IsIntPtrConstant(smi), start(), start()));
+  }
+}
+
+}  // namespace compiler
+}  // namespace internal
+}  // namespace v8
diff --git a/test/unittests/unittests.gyp b/test/unittests/unittests.gyp
index 90d6c1c..50e820e 100644
--- a/test/unittests/unittests.gyp
+++ b/test/unittests/unittests.gyp
@@ -91,6 +91,7 @@
       'compiler/schedule-unittest.cc',
       'compiler/scheduler-unittest.cc',
       'compiler/scheduler-rpo-unittest.cc',
+      'compiler/simplified-lowering-unittest.cc',
       'compiler/simplified-operator-reducer-unittest.cc',
       'compiler/simplified-operator-unittest.cc',
       'compiler/state-values-utils-unittest.cc',