Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/compiler/simplified-lowering.h"
6 :
7 : #include "src/compiler/compiler-source-position-table.h"
8 : #include "src/compiler/machine-operator.h"
9 : #include "src/compiler/simplified-operator.h"
10 :
11 : #include "test/unittests/compiler/graph-unittest.h"
12 : #include "test/unittests/compiler/node-test-utils.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 : namespace compiler {
17 :
18 : class SimplifiedLoweringTest : public GraphTest {
19 : public:
20 1 : explicit SimplifiedLoweringTest(int num_parameters = 1)
21 : : GraphTest(num_parameters),
22 : num_parameters_(num_parameters),
23 : machine_(zone()),
24 : javascript_(zone()),
25 : simplified_(zone()),
26 : jsgraph_(isolate(), graph(), common(), &javascript_, &simplified_,
27 3 : &machine_) {}
28 1 : ~SimplifiedLoweringTest() override = default;
29 :
30 13 : void LowerGraph(Node* node) {
31 : // Make sure we always start with an empty graph.
32 13 : graph()->SetStart(graph()->NewNode(common()->Start(num_parameters())));
33 13 : graph()->SetEnd(graph()->NewNode(common()->End(1), graph()->start()));
34 :
35 : // Return {node} directly, so that we can match it with
36 : // "IsReturn(expected)".
37 13 : Node* zero = graph()->NewNode(common()->NumberConstant(0));
38 13 : Node* ret = graph()->NewNode(common()->Return(), zero, node,
39 : graph()->start(), graph()->start());
40 13 : NodeProperties::MergeControlToEnd(graph(), common(), ret);
41 :
42 : {
43 : // Simplified lowering needs to run w/o the typer decorator so make sure
44 : // the object is not live at the same time.
45 26 : Typer typer(broker(), Typer::kNoFlags, graph());
46 13 : typer.Run();
47 : }
48 :
49 : SimplifiedLowering lowering(jsgraph(), broker(), zone(), source_positions(),
50 : node_origins(),
51 13 : PoisoningMitigationLevel::kDontPoison);
52 13 : lowering.LowerAllNodes();
53 13 : }
54 :
55 : int num_parameters() const { return num_parameters_; }
56 26 : JSGraph* jsgraph() { return &jsgraph_; }
57 :
58 : private:
59 : const int num_parameters_;
60 : MachineOperatorBuilder machine_;
61 : JSOperatorBuilder javascript_;
62 : SimplifiedOperatorBuilder simplified_;
63 : JSGraph jsgraph_;
64 : };
65 :
66 : const int kSmiValues[] = {Smi::kMinValue,
67 : Smi::kMinValue + 1,
68 : Smi::kMinValue + 2,
69 : 3,
70 : 2,
71 : 1,
72 : 0,
73 : -1,
74 : -2,
75 : -3,
76 : Smi::kMaxValue - 2,
77 : Smi::kMaxValue - 1,
78 : Smi::kMaxValue};
79 :
80 15419 : TEST_F(SimplifiedLoweringTest, SmiConstantToIntPtrConstant) {
81 53 : TRACED_FOREACH(int, x, kSmiValues) {
82 26 : LowerGraph(jsgraph()->Constant(x));
83 : intptr_t smi = bit_cast<intptr_t>(Smi::FromInt(x));
84 91 : EXPECT_THAT(graph()->end()->InputAt(1),
85 0 : IsReturn(IsIntPtrConstant(smi), start(), start()));
86 : }
87 1 : }
88 :
89 : } // namespace compiler
90 : } // namespace internal
91 9249 : } // namespace v8
|