blob: e850da7735d725160bbf6cf61b021e53229bf057 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 <limits>
6
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/ast/scopes.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/compiler/change-lowering.h"
9#include "src/compiler/control-builders.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/compiler/js-graph.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/compiler/node-properties.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/compiler/pipeline.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include "src/compiler/select-lowering.h"
14#include "src/compiler/simplified-lowering.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/compiler/typer.h"
16#include "src/compiler/verifier.h"
17#include "src/execution.h"
18#include "src/globals.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/parsing/parser.h"
20#include "src/parsing/rewriter.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021#include "test/cctest/cctest.h"
22#include "test/cctest/compiler/codegen-tester.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023#include "test/cctest/compiler/function-tester.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024#include "test/cctest/compiler/graph-builder-tester.h"
25#include "test/cctest/compiler/value-helper.h"
26
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027namespace v8 {
28namespace internal {
29namespace compiler {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030
31template <typename ReturnType>
32class ChangesLoweringTester : public GraphBuilderTester<ReturnType> {
33 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 explicit ChangesLoweringTester(MachineType p0 = MachineType::None())
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 : GraphBuilderTester<ReturnType>(p0),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 javascript(this->zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 jsgraph(this->isolate(), this->graph(), this->common(), &javascript,
38 nullptr, this->machine()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 function(Handle<JSFunction>::null()) {}
40
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 JSOperatorBuilder javascript;
42 JSGraph jsgraph;
43 Handle<JSFunction> function;
44
45 Node* start() { return this->graph()->start(); }
46
47 template <typename T>
48 T* CallWithPotentialGC() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 // TODO(titzer): we wrap the code in a JSFunction here to reuse the
50 // JSEntryStub; that could be done with a special prologue or other stub.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 if (function.is_null()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052 function = FunctionTester::ForMachineGraph(this->graph());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 }
54 Handle<Object>* args = NULL;
55 MaybeHandle<Object> result =
56 Execution::Call(this->isolate(), function, factory()->undefined_value(),
57 0, args, false);
58 return T::cast(*result.ToHandleChecked());
59 }
60
61 void StoreFloat64(Node* node, double* ptr) {
62 Node* ptr_node = this->PointerConstant(ptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 this->Store(MachineType::Float64(), ptr_node, node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 }
65
66 Node* LoadInt32(int32_t* ptr) {
67 Node* ptr_node = this->PointerConstant(ptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 return this->Load(MachineType::Int32(), ptr_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 }
70
71 Node* LoadUint32(uint32_t* ptr) {
72 Node* ptr_node = this->PointerConstant(ptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073 return this->Load(MachineType::Uint32(), ptr_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 }
75
76 Node* LoadFloat64(double* ptr) {
77 Node* ptr_node = this->PointerConstant(ptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 return this->Load(MachineType::Float64(), ptr_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 }
80
81 void CheckNumber(double expected, Object* number) {
82 CHECK(this->isolate()->factory()->NewNumber(expected)->SameValue(number));
83 }
84
85 void BuildAndLower(const Operator* op) {
86 // We build a graph by hand here, because the raw machine assembler
87 // does not add the correct control and effect nodes.
88 Node* p0 = this->Parameter(0);
89 Node* change = this->graph()->NewNode(op, p0);
90 Node* ret = this->graph()->NewNode(this->common()->Return(), change,
91 this->start(), this->start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 Node* end = this->graph()->NewNode(this->common()->End(1), ret);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 this->graph()->SetEnd(end);
94 LowerChange(change);
95 }
96
97 void BuildStoreAndLower(const Operator* op, const Operator* store_op,
98 void* location) {
99 // We build a graph by hand here, because the raw machine assembler
100 // does not add the correct control and effect nodes.
101 Node* p0 = this->Parameter(0);
102 Node* change = this->graph()->NewNode(op, p0);
103 Node* store = this->graph()->NewNode(
104 store_op, this->PointerConstant(location), this->Int32Constant(0),
105 change, this->start(), this->start());
106 Node* ret = this->graph()->NewNode(
107 this->common()->Return(), this->Int32Constant(0), store, this->start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 Node* end = this->graph()->NewNode(this->common()->End(1), ret);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 this->graph()->SetEnd(end);
110 LowerChange(change);
111 }
112
113 void BuildLoadAndLower(const Operator* op, const Operator* load_op,
114 void* location) {
115 // We build a graph by hand here, because the raw machine assembler
116 // does not add the correct control and effect nodes.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400117 Node* load = this->graph()->NewNode(
118 load_op, this->PointerConstant(location), this->Int32Constant(0),
119 this->start(), this->start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 Node* change = this->graph()->NewNode(op, load);
121 Node* ret = this->graph()->NewNode(this->common()->Return(), change,
122 this->start(), this->start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 Node* end = this->graph()->NewNode(this->common()->End(1), ret);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 this->graph()->SetEnd(end);
125 LowerChange(change);
126 }
127
128 void LowerChange(Node* change) {
129 // Run the graph reducer with changes lowering on a single node.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 Typer typer(this->isolate(), this->graph());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400131 typer.Run();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 ChangeLowering change_lowering(&jsgraph);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400133 SelectLowering select_lowering(this->graph(), this->common());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 GraphReducer reducer(this->zone(), this->graph());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400135 reducer.AddReducer(&change_lowering);
136 reducer.AddReducer(&select_lowering);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 reducer.ReduceNode(change);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400138 Verifier::Run(this->graph(), Verifier::UNTYPED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 }
140
141 Factory* factory() { return this->isolate()->factory(); }
142 Heap* heap() { return this->isolate()->heap(); }
143};
144
145
146TEST(RunChangeTaggedToInt32) {
147 // Build and lower a graph by hand.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148 ChangesLoweringTester<int32_t> t(MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 t.BuildAndLower(t.simplified()->ChangeTaggedToInt32());
150
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 FOR_INT32_INPUTS(i) {
152 int32_t input = *i;
153
154 if (Smi::IsValid(input)) {
155 int32_t result = t.Call(Smi::FromInt(input));
156 CHECK_EQ(input, result);
157 }
158
159 {
160 Handle<Object> number = t.factory()->NewNumber(input);
161 int32_t result = t.Call(*number);
162 CHECK_EQ(input, result);
163 }
164
165 {
166 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
167 int32_t result = t.Call(*number);
168 CHECK_EQ(input, result);
169 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 }
171}
172
173
174TEST(RunChangeTaggedToUint32) {
175 // Build and lower a graph by hand.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 ChangesLoweringTester<uint32_t> t(MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 t.BuildAndLower(t.simplified()->ChangeTaggedToUint32());
178
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 FOR_UINT32_INPUTS(i) {
180 uint32_t input = *i;
181
182 if (Smi::IsValid(input)) {
183 uint32_t result = t.Call(Smi::FromInt(input));
184 CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result));
185 }
186
187 {
188 Handle<Object> number = t.factory()->NewNumber(input);
189 uint32_t result = t.Call(*number);
190 CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result));
191 }
192
193 {
194 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
195 uint32_t result = t.Call(*number);
196 CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result));
197 }
198 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199}
200
201
202TEST(RunChangeTaggedToFloat64) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000203 ChangesLoweringTester<int32_t> t(MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 double result;
205
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206 t.BuildStoreAndLower(t.simplified()->ChangeTaggedToFloat64(),
207 t.machine()->Store(StoreRepresentation(
208 MachineRepresentation::kFloat64, kNoWriteBarrier)),
209 &result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000211 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 FOR_INT32_INPUTS(i) {
213 int32_t input = *i;
214
215 if (Smi::IsValid(input)) {
216 t.Call(Smi::FromInt(input));
217 CHECK_EQ(input, static_cast<int32_t>(result));
218 }
219
220 {
221 Handle<Object> number = t.factory()->NewNumber(input);
222 t.Call(*number);
223 CHECK_EQ(input, static_cast<int32_t>(result));
224 }
225
226 {
227 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
228 t.Call(*number);
229 CHECK_EQ(input, static_cast<int32_t>(result));
230 }
231 }
232 }
233
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 FOR_FLOAT64_INPUTS(i) {
236 double input = *i;
237 {
238 Handle<Object> number = t.factory()->NewNumber(input);
239 t.Call(*number);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000240 CheckDoubleEq(input, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 }
242
243 {
244 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
245 t.Call(*number);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000246 CheckDoubleEq(input, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 }
248 }
249 }
250}
251
252
253TEST(RunChangeBoolToBit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 ChangesLoweringTester<int32_t> t(MachineType::AnyTagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 t.BuildAndLower(t.simplified()->ChangeBoolToBit());
256
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 Object* true_obj = t.heap()->true_value();
259 int32_t result = t.Call(true_obj);
260 CHECK_EQ(1, result);
261 }
262
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 Object* false_obj = t.heap()->false_value();
265 int32_t result = t.Call(false_obj);
266 CHECK_EQ(0, result);
267 }
268}
269
270
271TEST(RunChangeBitToBool) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000272 ChangesLoweringTester<Object*> t(MachineType::Int32());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000273 t.BuildAndLower(t.simplified()->ChangeBitToBool());
274
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000275 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 Object* result = t.Call(1);
277 Object* true_obj = t.heap()->true_value();
278 CHECK_EQ(true_obj, result);
279 }
280
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000281 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 Object* result = t.Call(0);
283 Object* false_obj = t.heap()->false_value();
284 CHECK_EQ(false_obj, result);
285 }
286}
287
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000288} // namespace compiler
289} // namespace internal
290} // namespace v8