blob: 76aa390491a700bb152596c5eb23ea753676efdd [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 Murdochb8a8cc12014-11-26 15:28:44 +00007#include "test/cctest/cctest.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "test/cctest/compiler/codegen-tester.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "test/cctest/compiler/graph-builder-tester.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#include "test/cctest/compiler/value-helper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011
12#include "src/compiler/node-matchers.h"
13#include "src/compiler/representation-change.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016namespace internal {
17namespace compiler {
18
19class RepresentationChangerTester : public HandleAndZoneScope,
20 public GraphAndBuilders {
21 public:
22 explicit RepresentationChangerTester(int num_parameters = 0)
23 : GraphAndBuilders(main_zone()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024 javascript_(main_zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000025 jsgraph_(main_isolate(), main_graph_, &main_common_, &javascript_,
26 &main_simplified_, &main_machine_),
27 changer_(&jsgraph_, main_isolate()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 Node* s = graph()->NewNode(common()->Start(num_parameters));
29 graph()->SetStart(s);
30 }
31
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032 JSOperatorBuilder javascript_;
33 JSGraph jsgraph_;
34 RepresentationChanger changer_;
35
36 Isolate* isolate() { return main_isolate(); }
37 Graph* graph() { return main_graph_; }
38 CommonOperatorBuilder* common() { return &main_common_; }
39 JSGraph* jsgraph() { return &jsgraph_; }
40 RepresentationChanger* changer() { return &changer_; }
41
42 // TODO(titzer): use ValueChecker / ValueUtil
43 void CheckInt32Constant(Node* n, int32_t expected) {
44 Int32Matcher m(n);
45 CHECK(m.HasValue());
46 CHECK_EQ(expected, m.Value());
47 }
48
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 void CheckUint32Constant(Node* n, uint32_t expected) {
50 Uint32Matcher m(n);
51 CHECK(m.HasValue());
52 CHECK_EQ(static_cast<int>(expected), static_cast<int>(m.Value()));
53 }
54
55 void CheckFloat64Constant(Node* n, double expected) {
56 Float64Matcher m(n);
57 CHECK(m.HasValue());
Ben Murdochda12d292016-06-02 14:46:10 +010058 CHECK_DOUBLE_EQ(expected, m.Value());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 }
60
61 void CheckFloat32Constant(Node* n, float expected) {
62 CHECK_EQ(IrOpcode::kFloat32Constant, n->opcode());
63 float fval = OpParameter<float>(n->op());
Ben Murdochda12d292016-06-02 14:46:10 +010064 CHECK_FLOAT_EQ(expected, fval);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065 }
66
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 void CheckHeapConstant(Node* n, HeapObject* expected) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 HeapObjectMatcher m(n);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 CHECK(m.HasValue());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 CHECK_EQ(expected, *m.Value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 }
72
73 void CheckNumberConstant(Node* n, double expected) {
74 NumberMatcher m(n);
75 CHECK_EQ(IrOpcode::kNumberConstant, n->opcode());
76 CHECK(m.HasValue());
Ben Murdochda12d292016-06-02 14:46:10 +010077 CHECK_DOUBLE_EQ(expected, m.Value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 }
79
80 Node* Parameter(int index = 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 Node* n = graph()->NewNode(common()->Parameter(index), graph()->start());
82 NodeProperties::SetType(n, Type::Any());
83 return n;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 }
85
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086 void CheckTypeError(MachineRepresentation from, Type* from_type,
87 MachineRepresentation to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 changer()->testing_type_errors_ = true;
89 changer()->type_error_ = false;
90 Node* n = Parameter(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 Node* c = changer()->GetRepresentationFor(n, from, from_type, to);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 CHECK(changer()->type_error_);
93 CHECK_EQ(n, c);
94 }
95
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 void CheckNop(MachineRepresentation from, Type* from_type,
97 MachineRepresentation to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 Node* n = Parameter(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 Node* c = changer()->GetRepresentationFor(n, from, from_type, to);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 CHECK_EQ(n, c);
101 }
102};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103
104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105const MachineType kMachineTypes[] = {
106 MachineType::Float32(), MachineType::Float64(), MachineType::Int8(),
107 MachineType::Uint8(), MachineType::Int16(), MachineType::Uint16(),
108 MachineType::Int32(), MachineType::Uint32(), MachineType::Int64(),
109 MachineType::Uint64(), MachineType::AnyTagged()};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110
111
112TEST(BoolToBit_constant) {
113 RepresentationChangerTester r;
114
115 Node* true_node = r.jsgraph()->TrueConstant();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 Node* true_bit = r.changer()->GetRepresentationFor(
117 true_node, MachineRepresentation::kTagged, Type::None(),
118 MachineRepresentation::kBit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 r.CheckInt32Constant(true_bit, 1);
120
121 Node* false_node = r.jsgraph()->FalseConstant();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 Node* false_bit = r.changer()->GetRepresentationFor(
123 false_node, MachineRepresentation::kTagged, Type::None(),
124 MachineRepresentation::kBit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125 r.CheckInt32Constant(false_bit, 0);
126}
127
128
129TEST(BitToBool_constant) {
130 RepresentationChangerTester r;
131
132 for (int i = -5; i < 5; i++) {
133 Node* node = r.jsgraph()->Int32Constant(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 Node* val = r.changer()->GetRepresentationFor(
135 node, MachineRepresentation::kBit, Type::Boolean(),
136 MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 r.CheckHeapConstant(val, i == 0 ? r.isolate()->heap()->false_value()
138 : r.isolate()->heap()->true_value());
139 }
140}
141
142
143TEST(ToTagged_constant) {
144 RepresentationChangerTester r;
145
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400146 {
147 FOR_FLOAT64_INPUTS(i) {
148 Node* n = r.jsgraph()->Float64Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 Node* c = r.changer()->GetRepresentationFor(
150 n, MachineRepresentation::kFloat64, Type::None(),
151 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400152 r.CheckNumberConstant(c, *i);
153 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 }
155
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400156 {
157 FOR_FLOAT64_INPUTS(i) {
158 Node* n = r.jsgraph()->Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 Node* c = r.changer()->GetRepresentationFor(
160 n, MachineRepresentation::kFloat64, Type::None(),
161 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400162 r.CheckNumberConstant(c, *i);
163 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 }
165
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400166 {
167 FOR_FLOAT32_INPUTS(i) {
168 Node* n = r.jsgraph()->Float32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169 Node* c = r.changer()->GetRepresentationFor(
170 n, MachineRepresentation::kFloat32, Type::None(),
171 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400172 r.CheckNumberConstant(c, *i);
173 }
174 }
175
176 {
177 FOR_INT32_INPUTS(i) {
178 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179 Node* c = r.changer()->GetRepresentationFor(
180 n, MachineRepresentation::kWord32, Type::Signed32(),
181 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400182 r.CheckNumberConstant(c, *i);
183 }
184 }
185
186 {
187 FOR_UINT32_INPUTS(i) {
188 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 Node* c = r.changer()->GetRepresentationFor(
190 n, MachineRepresentation::kWord32, Type::Unsigned32(),
191 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400192 r.CheckNumberConstant(c, *i);
193 }
194 }
195}
196
197
198TEST(ToFloat64_constant) {
199 RepresentationChangerTester r;
200
201 {
202 FOR_FLOAT64_INPUTS(i) {
203 Node* n = r.jsgraph()->Float64Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 Node* c = r.changer()->GetRepresentationFor(
205 n, MachineRepresentation::kFloat64, Type::None(),
206 MachineRepresentation::kFloat64);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400207 CHECK_EQ(n, c);
208 }
209 }
210
211 {
212 FOR_FLOAT64_INPUTS(i) {
213 Node* n = r.jsgraph()->Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000214 Node* c = r.changer()->GetRepresentationFor(
215 n, MachineRepresentation::kTagged, Type::None(),
216 MachineRepresentation::kFloat64);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400217 r.CheckFloat64Constant(c, *i);
218 }
219 }
220
221 {
222 FOR_FLOAT32_INPUTS(i) {
223 Node* n = r.jsgraph()->Float32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224 Node* c = r.changer()->GetRepresentationFor(
225 n, MachineRepresentation::kFloat32, Type::None(),
226 MachineRepresentation::kFloat64);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400227 r.CheckFloat64Constant(c, *i);
228 }
229 }
230
231 {
232 FOR_INT32_INPUTS(i) {
233 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234 Node* c = r.changer()->GetRepresentationFor(
235 n, MachineRepresentation::kWord32, Type::Signed32(),
236 MachineRepresentation::kFloat64);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400237 r.CheckFloat64Constant(c, *i);
238 }
239 }
240
241 {
242 FOR_UINT32_INPUTS(i) {
243 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 Node* c = r.changer()->GetRepresentationFor(
245 n, MachineRepresentation::kWord32, Type::Unsigned32(),
246 MachineRepresentation::kFloat64);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400247 r.CheckFloat64Constant(c, *i);
248 }
249 }
250}
251
252
253static bool IsFloat32Int32(int32_t val) {
254 return val >= -(1 << 23) && val <= (1 << 23);
255}
256
257
258static bool IsFloat32Uint32(uint32_t val) { return val <= (1 << 23); }
259
260
261TEST(ToFloat32_constant) {
262 RepresentationChangerTester r;
263
264 {
265 FOR_FLOAT32_INPUTS(i) {
266 Node* n = r.jsgraph()->Float32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000267 Node* c = r.changer()->GetRepresentationFor(
268 n, MachineRepresentation::kFloat32, Type::None(),
269 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400270 CHECK_EQ(n, c);
271 }
272 }
273
274 {
275 FOR_FLOAT32_INPUTS(i) {
276 Node* n = r.jsgraph()->Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 Node* c = r.changer()->GetRepresentationFor(
278 n, MachineRepresentation::kTagged, Type::None(),
279 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400280 r.CheckFloat32Constant(c, *i);
281 }
282 }
283
284 {
285 FOR_FLOAT32_INPUTS(i) {
286 Node* n = r.jsgraph()->Float64Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287 Node* c = r.changer()->GetRepresentationFor(
288 n, MachineRepresentation::kFloat64, Type::None(),
289 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400290 r.CheckFloat32Constant(c, *i);
291 }
292 }
293
294 {
295 FOR_INT32_INPUTS(i) {
296 if (!IsFloat32Int32(*i)) continue;
297 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 Node* c = r.changer()->GetRepresentationFor(
299 n, MachineRepresentation::kWord32, Type::Signed32(),
300 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400301 r.CheckFloat32Constant(c, static_cast<float>(*i));
302 }
303 }
304
305 {
306 FOR_UINT32_INPUTS(i) {
307 if (!IsFloat32Uint32(*i)) continue;
308 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 Node* c = r.changer()->GetRepresentationFor(
310 n, MachineRepresentation::kWord32, Type::Unsigned32(),
311 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400312 r.CheckFloat32Constant(c, static_cast<float>(*i));
313 }
314 }
315}
316
317
318TEST(ToInt32_constant) {
319 RepresentationChangerTester r;
320
321 {
322 FOR_INT32_INPUTS(i) {
323 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 Node* c = r.changer()->GetRepresentationFor(
325 n, MachineRepresentation::kWord32, Type::Signed32(),
326 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400327 r.CheckInt32Constant(c, *i);
328 }
329 }
330
331 {
332 FOR_INT32_INPUTS(i) {
333 if (!IsFloat32Int32(*i)) continue;
334 Node* n = r.jsgraph()->Float32Constant(static_cast<float>(*i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335 Node* c = r.changer()->GetRepresentationFor(
336 n, MachineRepresentation::kFloat32, Type::Signed32(),
337 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400338 r.CheckInt32Constant(c, *i);
339 }
340 }
341
342 {
343 FOR_INT32_INPUTS(i) {
344 Node* n = r.jsgraph()->Float64Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345 Node* c = r.changer()->GetRepresentationFor(
346 n, MachineRepresentation::kFloat64, Type::Signed32(),
347 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400348 r.CheckInt32Constant(c, *i);
349 }
350 }
351
352 {
353 FOR_INT32_INPUTS(i) {
354 Node* n = r.jsgraph()->Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000355 Node* c = r.changer()->GetRepresentationFor(
356 n, MachineRepresentation::kTagged, Type::Signed32(),
357 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400358 r.CheckInt32Constant(c, *i);
359 }
360 }
361}
362
363
364TEST(ToUint32_constant) {
365 RepresentationChangerTester r;
366
367 {
368 FOR_UINT32_INPUTS(i) {
369 Node* n = r.jsgraph()->Int32Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000370 Node* c = r.changer()->GetRepresentationFor(
371 n, MachineRepresentation::kWord32, Type::Unsigned32(),
372 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400373 r.CheckUint32Constant(c, *i);
374 }
375 }
376
377 {
378 FOR_UINT32_INPUTS(i) {
379 if (!IsFloat32Uint32(*i)) continue;
380 Node* n = r.jsgraph()->Float32Constant(static_cast<float>(*i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381 Node* c = r.changer()->GetRepresentationFor(
382 n, MachineRepresentation::kFloat32, Type::Unsigned32(),
383 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400384 r.CheckUint32Constant(c, *i);
385 }
386 }
387
388 {
389 FOR_UINT32_INPUTS(i) {
390 Node* n = r.jsgraph()->Float64Constant(*i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000391 Node* c = r.changer()->GetRepresentationFor(
392 n, MachineRepresentation::kFloat64, Type::Unsigned32(),
393 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400394 r.CheckUint32Constant(c, *i);
395 }
396 }
397
398 {
399 FOR_UINT32_INPUTS(i) {
400 Node* n = r.jsgraph()->Constant(static_cast<double>(*i));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 Node* c = r.changer()->GetRepresentationFor(
402 n, MachineRepresentation::kTagged, Type::Unsigned32(),
403 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400404 r.CheckUint32Constant(c, *i);
405 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 }
407}
408
409
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000410static void CheckChange(IrOpcode::Value expected, MachineRepresentation from,
411 Type* from_type, MachineRepresentation to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 RepresentationChangerTester r;
413
414 Node* n = r.Parameter();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415 Node* c = r.changer()->GetRepresentationFor(n, from, from_type, to);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416
417 CHECK_NE(c, n);
418 CHECK_EQ(expected, c->opcode());
419 CHECK_EQ(n, c->InputAt(0));
420}
421
422
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400423static void CheckTwoChanges(IrOpcode::Value expected2,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424 IrOpcode::Value expected1,
425 MachineRepresentation from, Type* from_type,
426 MachineRepresentation to) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400427 RepresentationChangerTester r;
428
429 Node* n = r.Parameter();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430 Node* c1 = r.changer()->GetRepresentationFor(n, from, from_type, to);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400431
432 CHECK_NE(c1, n);
433 CHECK_EQ(expected1, c1->opcode());
434 Node* c2 = c1->InputAt(0);
435 CHECK_NE(c2, n);
436 CHECK_EQ(expected2, c2->opcode());
437 CHECK_EQ(n, c2->InputAt(0));
438}
439
440
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441TEST(SingleChanges) {
Ben Murdochc5610432016-08-08 18:44:38 +0100442 CheckChange(IrOpcode::kChangeTaggedToBit, MachineRepresentation::kTagged,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000443 Type::None(), MachineRepresentation::kBit);
Ben Murdochc5610432016-08-08 18:44:38 +0100444 CheckChange(IrOpcode::kChangeBitToTagged, MachineRepresentation::kBit,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 Type::None(), MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446
Ben Murdochc5610432016-08-08 18:44:38 +0100447 CheckChange(IrOpcode::kChangeInt31ToTaggedSigned,
448 MachineRepresentation::kWord32, Type::Signed31(),
449 MachineRepresentation::kTagged);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450 CheckChange(IrOpcode::kChangeInt32ToTagged, MachineRepresentation::kWord32,
451 Type::Signed32(), MachineRepresentation::kTagged);
452 CheckChange(IrOpcode::kChangeUint32ToTagged, MachineRepresentation::kWord32,
453 Type::Unsigned32(), MachineRepresentation::kTagged);
454 CheckChange(IrOpcode::kChangeFloat64ToTagged, MachineRepresentation::kFloat64,
Ben Murdochc5610432016-08-08 18:44:38 +0100455 Type::Number(), MachineRepresentation::kTagged);
456 CheckTwoChanges(IrOpcode::kChangeFloat64ToInt32,
457 IrOpcode::kChangeInt31ToTaggedSigned,
458 MachineRepresentation::kFloat64, Type::Signed31(),
459 MachineRepresentation::kTagged);
460 CheckTwoChanges(IrOpcode::kChangeFloat64ToInt32,
461 IrOpcode::kChangeInt32ToTagged,
462 MachineRepresentation::kFloat64, Type::Signed32(),
463 MachineRepresentation::kTagged);
464 CheckTwoChanges(IrOpcode::kChangeFloat64ToUint32,
465 IrOpcode::kChangeUint32ToTagged,
466 MachineRepresentation::kFloat64, Type::Unsigned32(),
467 MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 CheckChange(IrOpcode::kChangeTaggedToInt32, MachineRepresentation::kTagged,
470 Type::Signed32(), MachineRepresentation::kWord32);
471 CheckChange(IrOpcode::kChangeTaggedToUint32, MachineRepresentation::kTagged,
472 Type::Unsigned32(), MachineRepresentation::kWord32);
473 CheckChange(IrOpcode::kChangeTaggedToFloat64, MachineRepresentation::kTagged,
Ben Murdochc5610432016-08-08 18:44:38 +0100474 Type::Number(), MachineRepresentation::kFloat64);
475 CheckChange(IrOpcode::kChangeTaggedToFloat64, MachineRepresentation::kTagged,
476 Type::NumberOrUndefined(), MachineRepresentation::kFloat64);
477 CheckTwoChanges(IrOpcode::kChangeTaggedSignedToInt32,
478 IrOpcode::kChangeInt32ToFloat64,
479 MachineRepresentation::kTagged, Type::TaggedSigned(),
480 MachineRepresentation::kFloat64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481
482 // Int32,Uint32 <-> Float64 are actually machine conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000483 CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
484 Type::Signed32(), MachineRepresentation::kFloat64);
485 CheckChange(IrOpcode::kChangeUint32ToFloat64, MachineRepresentation::kWord32,
486 Type::Unsigned32(), MachineRepresentation::kFloat64);
487 CheckChange(IrOpcode::kChangeFloat64ToInt32, MachineRepresentation::kFloat64,
488 Type::Signed32(), MachineRepresentation::kWord32);
489 CheckChange(IrOpcode::kChangeFloat64ToUint32, MachineRepresentation::kFloat64,
490 Type::Unsigned32(), MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400491
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 CheckChange(IrOpcode::kTruncateFloat64ToFloat32,
493 MachineRepresentation::kFloat64, Type::None(),
494 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400495
496 // Int32,Uint32 <-> Float32 require two changes.
497 CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000498 IrOpcode::kTruncateFloat64ToFloat32,
499 MachineRepresentation::kWord32, Type::Signed32(),
500 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400501 CheckTwoChanges(IrOpcode::kChangeUint32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 IrOpcode::kTruncateFloat64ToFloat32,
503 MachineRepresentation::kWord32, Type::Unsigned32(),
504 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400505 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000506 IrOpcode::kChangeFloat64ToInt32,
507 MachineRepresentation::kFloat32, Type::Signed32(),
508 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400509 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000510 IrOpcode::kChangeFloat64ToUint32,
511 MachineRepresentation::kFloat32, Type::Unsigned32(),
512 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400513
514 // Float32 <-> Tagged require two changes.
515 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000516 IrOpcode::kChangeFloat64ToTagged,
517 MachineRepresentation::kFloat32, Type::None(),
518 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400519 CheckTwoChanges(IrOpcode::kChangeTaggedToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000520 IrOpcode::kTruncateFloat64ToFloat32,
521 MachineRepresentation::kTagged, Type::None(),
522 MachineRepresentation::kFloat32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523}
524
525
526TEST(SignednessInWord32) {
527 RepresentationChangerTester r;
528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000529 CheckChange(IrOpcode::kChangeTaggedToInt32, MachineRepresentation::kTagged,
530 Type::Signed32(), MachineRepresentation::kWord32);
531 CheckChange(IrOpcode::kChangeTaggedToUint32, MachineRepresentation::kTagged,
532 Type::Unsigned32(), MachineRepresentation::kWord32);
533 CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
534 Type::None(), MachineRepresentation::kFloat64);
535 CheckChange(IrOpcode::kChangeFloat64ToInt32, MachineRepresentation::kFloat64,
536 Type::Signed32(), MachineRepresentation::kWord32);
Ben Murdochc5610432016-08-08 18:44:38 +0100537 CheckChange(IrOpcode::kTruncateFloat64ToWord32,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000538 MachineRepresentation::kFloat64, Type::Number(),
539 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400540
541 CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000542 IrOpcode::kTruncateFloat64ToFloat32,
543 MachineRepresentation::kWord32, Type::None(),
544 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400545 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdochc5610432016-08-08 18:44:38 +0100546 IrOpcode::kTruncateFloat64ToWord32,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 MachineRepresentation::kFloat32, Type::Number(),
548 MachineRepresentation::kWord32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549}
550
551
552TEST(Nops) {
553 RepresentationChangerTester r;
554
555 // X -> X is always a nop for any single representation X.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000556 for (size_t i = 0; i < arraysize(kMachineTypes); i++) {
557 r.CheckNop(kMachineTypes[i].representation(), Type::None(),
558 kMachineTypes[i].representation());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559 }
560
561 // 32-bit floats.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000562 r.CheckNop(MachineRepresentation::kFloat32, Type::None(),
563 MachineRepresentation::kFloat32);
564 r.CheckNop(MachineRepresentation::kFloat32, Type::Number(),
565 MachineRepresentation::kFloat32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566
567 // 32-bit words can be used as smaller word sizes and vice versa, because
568 // loads from memory implicitly sign or zero extend the value to the
569 // full machine word size, and stores implicitly truncate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000570 r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
571 MachineRepresentation::kWord8);
572 r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
573 MachineRepresentation::kWord16);
574 r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
575 MachineRepresentation::kWord32);
576 r.CheckNop(MachineRepresentation::kWord8, Type::Signed32(),
577 MachineRepresentation::kWord32);
578 r.CheckNop(MachineRepresentation::kWord16, Type::Signed32(),
579 MachineRepresentation::kWord32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580
581 // kRepBit (result of comparison) is implicitly a wordish thing.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582 r.CheckNop(MachineRepresentation::kBit, Type::None(),
583 MachineRepresentation::kWord8);
584 r.CheckNop(MachineRepresentation::kBit, Type::None(),
585 MachineRepresentation::kWord16);
586 r.CheckNop(MachineRepresentation::kBit, Type::None(),
587 MachineRepresentation::kWord32);
588 r.CheckNop(MachineRepresentation::kBit, Type::None(),
589 MachineRepresentation::kWord64);
590 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
591 MachineRepresentation::kWord8);
592 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
593 MachineRepresentation::kWord16);
594 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
595 MachineRepresentation::kWord32);
596 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
597 MachineRepresentation::kWord64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598}
599
600
601TEST(TypeErrors) {
602 RepresentationChangerTester r;
603
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000604 // Wordish cannot be implicitly converted to/from comparison conditions.
605 r.CheckTypeError(MachineRepresentation::kWord8, Type::None(),
606 MachineRepresentation::kBit);
607 r.CheckTypeError(MachineRepresentation::kWord16, Type::None(),
608 MachineRepresentation::kBit);
609 r.CheckTypeError(MachineRepresentation::kWord32, Type::None(),
610 MachineRepresentation::kBit);
611 r.CheckTypeError(MachineRepresentation::kWord64, Type::None(),
612 MachineRepresentation::kBit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613
614 // Floats cannot be implicitly converted to/from comparison conditions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000615 r.CheckTypeError(MachineRepresentation::kFloat64, Type::None(),
616 MachineRepresentation::kBit);
617 r.CheckTypeError(MachineRepresentation::kBit, Type::None(),
618 MachineRepresentation::kFloat64);
619 r.CheckTypeError(MachineRepresentation::kBit, Type::Boolean(),
620 MachineRepresentation::kFloat64);
621
622 // Floats cannot be implicitly converted to/from comparison conditions.
623 r.CheckTypeError(MachineRepresentation::kFloat32, Type::None(),
624 MachineRepresentation::kBit);
625 r.CheckTypeError(MachineRepresentation::kBit, Type::None(),
626 MachineRepresentation::kFloat32);
627 r.CheckTypeError(MachineRepresentation::kBit, Type::Boolean(),
628 MachineRepresentation::kFloat32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629
630 // Word64 is internal and shouldn't be implicitly converted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000631 r.CheckTypeError(MachineRepresentation::kWord64, Type::None(),
632 MachineRepresentation::kTagged);
633 r.CheckTypeError(MachineRepresentation::kTagged, Type::None(),
634 MachineRepresentation::kWord64);
635 r.CheckTypeError(MachineRepresentation::kTagged, Type::Boolean(),
636 MachineRepresentation::kWord64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637
638 // Word64 / Word32 shouldn't be implicitly converted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000639 r.CheckTypeError(MachineRepresentation::kWord64, Type::None(),
640 MachineRepresentation::kWord32);
641 r.CheckTypeError(MachineRepresentation::kWord32, Type::None(),
642 MachineRepresentation::kWord64);
643 r.CheckTypeError(MachineRepresentation::kWord32, Type::Signed32(),
644 MachineRepresentation::kWord64);
645 r.CheckTypeError(MachineRepresentation::kWord32, Type::Unsigned32(),
646 MachineRepresentation::kWord64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000648
649} // namespace compiler
650} // namespace internal
651} // namespace v8