deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / unittests / compiler / js-builtin-reducer-unittest.cc
1 // Copyright 2014 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/js-builtin-reducer.h"
6 #include "src/compiler/js-graph.h"
7 #include "src/compiler/node-properties.h"
8 #include "src/compiler/typer.h"
9 #include "test/unittests/compiler/graph-unittest.h"
10 #include "test/unittests/compiler/node-test-utils.h"
11 #include "testing/gmock-support.h"
12
13 using testing::BitEq;
14 using testing::Capture;
15
16 namespace v8 {
17 namespace internal {
18 namespace compiler {
19
20 class JSBuiltinReducerTest : public TypedGraphTest {
21  public:
22   JSBuiltinReducerTest() : javascript_(zone()) {}
23
24  protected:
25   Reduction Reduce(Node* node, MachineOperatorBuilder::Flags flags =
26                                    MachineOperatorBuilder::Flag::kNoFlags) {
27     MachineOperatorBuilder machine(zone(), kMachPtr, flags);
28     JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine);
29     JSBuiltinReducer reducer(&jsgraph);
30     return reducer.Reduce(node);
31   }
32
33   Handle<JSFunction> MathFunction(const char* name) {
34     Handle<Object> m =
35         JSObject::GetProperty(isolate()->global_object(),
36                               isolate()->factory()->NewStringFromAsciiChecked(
37                                   "Math")).ToHandleChecked();
38     Handle<JSFunction> f = Handle<JSFunction>::cast(
39         JSObject::GetProperty(
40             m, isolate()->factory()->NewStringFromAsciiChecked(name))
41             .ToHandleChecked());
42     return f;
43   }
44
45   JSOperatorBuilder* javascript() { return &javascript_; }
46
47  private:
48   JSOperatorBuilder javascript_;
49 };
50
51
52 namespace {
53
54 // TODO(mstarzinger): Find a common place and unify with test-js-typed-lowering.
55 Type* const kNumberTypes[] = {
56     Type::UnsignedSmall(), Type::Negative32(),  Type::Unsigned31(),
57     Type::SignedSmall(),   Type::Signed32(),    Type::Unsigned32(),
58     Type::Integral32(),    Type::MinusZero(),   Type::NaN(),
59     Type::OrderedNumber(), Type::PlainNumber(), Type::Number()};
60
61 }  // namespace
62
63
64 // -----------------------------------------------------------------------------
65 // Math.max
66
67
68 TEST_F(JSBuiltinReducerTest, MathMax0) {
69   Handle<JSFunction> f = MathFunction("max");
70
71   Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
72   Node* call =
73       graph()->NewNode(javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS),
74                        fun, UndefinedConstant());
75   Reduction r = Reduce(call);
76
77   ASSERT_TRUE(r.Changed());
78   EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY));
79 }
80
81
82 TEST_F(JSBuiltinReducerTest, MathMax1) {
83   Handle<JSFunction> f = MathFunction("max");
84
85   TRACED_FOREACH(Type*, t0, kNumberTypes) {
86     Node* p0 = Parameter(t0, 0);
87     Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
88     Node* call =
89         graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
90                          fun, UndefinedConstant(), p0);
91     Reduction r = Reduce(call);
92
93     ASSERT_TRUE(r.Changed());
94     EXPECT_THAT(r.replacement(), p0);
95   }
96 }
97
98
99 TEST_F(JSBuiltinReducerTest, MathMax2) {
100   Handle<JSFunction> f = MathFunction("max");
101
102   TRACED_FOREACH(Type*, t0, kNumberTypes) {
103     TRACED_FOREACH(Type*, t1, kNumberTypes) {
104       Node* p0 = Parameter(t0, 0);
105       Node* p1 = Parameter(t1, 1);
106       Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
107       Node* call = graph()->NewNode(
108           javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun,
109           UndefinedConstant(), p0, p1);
110       Reduction r = Reduce(call);
111
112       if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) {
113         ASSERT_TRUE(r.Changed());
114         EXPECT_THAT(r.replacement(),
115                     IsSelect(kMachNone, IsNumberLessThan(p1, p0), p0, p1));
116       } else {
117         ASSERT_FALSE(r.Changed());
118         EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode());
119       }
120     }
121   }
122 }
123
124
125 // -----------------------------------------------------------------------------
126 // Math.imul
127
128
129 TEST_F(JSBuiltinReducerTest, MathImul) {
130   Handle<JSFunction> f = MathFunction("imul");
131
132   TRACED_FOREACH(Type*, t0, kNumberTypes) {
133     TRACED_FOREACH(Type*, t1, kNumberTypes) {
134       Node* p0 = Parameter(t0, 0);
135       Node* p1 = Parameter(t1, 1);
136       Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
137       Node* call = graph()->NewNode(
138           javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun,
139           UndefinedConstant(), p0, p1);
140       Reduction r = Reduce(call);
141
142       if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) {
143         ASSERT_TRUE(r.Changed());
144         EXPECT_THAT(r.replacement(), IsInt32Mul(p0, p1));
145       } else {
146         ASSERT_FALSE(r.Changed());
147         EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode());
148       }
149     }
150   }
151 }
152
153
154 // -----------------------------------------------------------------------------
155 // Math.fround
156
157
158 TEST_F(JSBuiltinReducerTest, MathFround) {
159   Handle<JSFunction> f = MathFunction("fround");
160
161   TRACED_FOREACH(Type*, t0, kNumberTypes) {
162     Node* p0 = Parameter(t0, 0);
163     Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
164     Node* call =
165         graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
166                          fun, UndefinedConstant(), p0);
167     Reduction r = Reduce(call);
168
169     ASSERT_TRUE(r.Changed());
170     EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0));
171   }
172 }
173
174 }  // namespace compiler
175 }  // namespace internal
176 }  // namespace v8