blob: 7e75bf8eb01513325624a7a1618c66abf73ee112 [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 Murdoch4a90d5f2016-03-22 12:00:34 +0000442 CheckChange(IrOpcode::kChangeBoolToBit, MachineRepresentation::kTagged,
443 Type::None(), MachineRepresentation::kBit);
444 CheckChange(IrOpcode::kChangeBitToBool, MachineRepresentation::kBit,
445 Type::None(), MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000447 CheckChange(IrOpcode::kChangeInt32ToTagged, MachineRepresentation::kWord32,
448 Type::Signed32(), MachineRepresentation::kTagged);
449 CheckChange(IrOpcode::kChangeUint32ToTagged, MachineRepresentation::kWord32,
450 Type::Unsigned32(), MachineRepresentation::kTagged);
451 CheckChange(IrOpcode::kChangeFloat64ToTagged, MachineRepresentation::kFloat64,
452 Type::None(), MachineRepresentation::kTagged);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 CheckChange(IrOpcode::kChangeTaggedToInt32, MachineRepresentation::kTagged,
455 Type::Signed32(), MachineRepresentation::kWord32);
456 CheckChange(IrOpcode::kChangeTaggedToUint32, MachineRepresentation::kTagged,
457 Type::Unsigned32(), MachineRepresentation::kWord32);
458 CheckChange(IrOpcode::kChangeTaggedToFloat64, MachineRepresentation::kTagged,
459 Type::None(), MachineRepresentation::kFloat64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460
461 // Int32,Uint32 <-> Float64 are actually machine conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000462 CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
463 Type::Signed32(), MachineRepresentation::kFloat64);
464 CheckChange(IrOpcode::kChangeUint32ToFloat64, MachineRepresentation::kWord32,
465 Type::Unsigned32(), MachineRepresentation::kFloat64);
466 CheckChange(IrOpcode::kChangeFloat64ToInt32, MachineRepresentation::kFloat64,
467 Type::Signed32(), MachineRepresentation::kWord32);
468 CheckChange(IrOpcode::kChangeFloat64ToUint32, MachineRepresentation::kFloat64,
469 Type::Unsigned32(), MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400470
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000471 CheckChange(IrOpcode::kTruncateFloat64ToFloat32,
472 MachineRepresentation::kFloat64, Type::None(),
473 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400474
475 // Int32,Uint32 <-> Float32 require two changes.
476 CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 IrOpcode::kTruncateFloat64ToFloat32,
478 MachineRepresentation::kWord32, Type::Signed32(),
479 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400480 CheckTwoChanges(IrOpcode::kChangeUint32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481 IrOpcode::kTruncateFloat64ToFloat32,
482 MachineRepresentation::kWord32, Type::Unsigned32(),
483 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400484 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000485 IrOpcode::kChangeFloat64ToInt32,
486 MachineRepresentation::kFloat32, Type::Signed32(),
487 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400488 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489 IrOpcode::kChangeFloat64ToUint32,
490 MachineRepresentation::kFloat32, Type::Unsigned32(),
491 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400492
493 // Float32 <-> Tagged require two changes.
494 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 IrOpcode::kChangeFloat64ToTagged,
496 MachineRepresentation::kFloat32, Type::None(),
497 MachineRepresentation::kTagged);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400498 CheckTwoChanges(IrOpcode::kChangeTaggedToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000499 IrOpcode::kTruncateFloat64ToFloat32,
500 MachineRepresentation::kTagged, Type::None(),
501 MachineRepresentation::kFloat32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502}
503
504
505TEST(SignednessInWord32) {
506 RepresentationChangerTester r;
507
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000508 CheckChange(IrOpcode::kChangeTaggedToInt32, MachineRepresentation::kTagged,
509 Type::Signed32(), MachineRepresentation::kWord32);
510 CheckChange(IrOpcode::kChangeTaggedToUint32, MachineRepresentation::kTagged,
511 Type::Unsigned32(), MachineRepresentation::kWord32);
512 CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
513 Type::None(), MachineRepresentation::kFloat64);
514 CheckChange(IrOpcode::kChangeFloat64ToInt32, MachineRepresentation::kFloat64,
515 Type::Signed32(), MachineRepresentation::kWord32);
516 CheckChange(IrOpcode::kTruncateFloat64ToInt32,
517 MachineRepresentation::kFloat64, Type::Number(),
518 MachineRepresentation::kWord32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400519
520 CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000521 IrOpcode::kTruncateFloat64ToFloat32,
522 MachineRepresentation::kWord32, Type::None(),
523 MachineRepresentation::kFloat32);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400524 CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525 IrOpcode::kTruncateFloat64ToInt32,
526 MachineRepresentation::kFloat32, Type::Number(),
527 MachineRepresentation::kWord32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528}
529
530
531TEST(Nops) {
532 RepresentationChangerTester r;
533
534 // X -> X is always a nop for any single representation X.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535 for (size_t i = 0; i < arraysize(kMachineTypes); i++) {
536 r.CheckNop(kMachineTypes[i].representation(), Type::None(),
537 kMachineTypes[i].representation());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 }
539
540 // 32-bit floats.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000541 r.CheckNop(MachineRepresentation::kFloat32, Type::None(),
542 MachineRepresentation::kFloat32);
543 r.CheckNop(MachineRepresentation::kFloat32, Type::Number(),
544 MachineRepresentation::kFloat32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545
546 // 32-bit words can be used as smaller word sizes and vice versa, because
547 // loads from memory implicitly sign or zero extend the value to the
548 // full machine word size, and stores implicitly truncate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
550 MachineRepresentation::kWord8);
551 r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
552 MachineRepresentation::kWord16);
553 r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
554 MachineRepresentation::kWord32);
555 r.CheckNop(MachineRepresentation::kWord8, Type::Signed32(),
556 MachineRepresentation::kWord32);
557 r.CheckNop(MachineRepresentation::kWord16, Type::Signed32(),
558 MachineRepresentation::kWord32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559
560 // kRepBit (result of comparison) is implicitly a wordish thing.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000561 r.CheckNop(MachineRepresentation::kBit, Type::None(),
562 MachineRepresentation::kWord8);
563 r.CheckNop(MachineRepresentation::kBit, Type::None(),
564 MachineRepresentation::kWord16);
565 r.CheckNop(MachineRepresentation::kBit, Type::None(),
566 MachineRepresentation::kWord32);
567 r.CheckNop(MachineRepresentation::kBit, Type::None(),
568 MachineRepresentation::kWord64);
569 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
570 MachineRepresentation::kWord8);
571 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
572 MachineRepresentation::kWord16);
573 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
574 MachineRepresentation::kWord32);
575 r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
576 MachineRepresentation::kWord64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577}
578
579
580TEST(TypeErrors) {
581 RepresentationChangerTester r;
582
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000583 // Wordish cannot be implicitly converted to/from comparison conditions.
584 r.CheckTypeError(MachineRepresentation::kWord8, Type::None(),
585 MachineRepresentation::kBit);
586 r.CheckTypeError(MachineRepresentation::kWord16, Type::None(),
587 MachineRepresentation::kBit);
588 r.CheckTypeError(MachineRepresentation::kWord32, Type::None(),
589 MachineRepresentation::kBit);
590 r.CheckTypeError(MachineRepresentation::kWord64, Type::None(),
591 MachineRepresentation::kBit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592
593 // Floats cannot be implicitly converted to/from comparison conditions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000594 r.CheckTypeError(MachineRepresentation::kFloat64, Type::None(),
595 MachineRepresentation::kBit);
596 r.CheckTypeError(MachineRepresentation::kBit, Type::None(),
597 MachineRepresentation::kFloat64);
598 r.CheckTypeError(MachineRepresentation::kBit, Type::Boolean(),
599 MachineRepresentation::kFloat64);
600
601 // Floats cannot be implicitly converted to/from comparison conditions.
602 r.CheckTypeError(MachineRepresentation::kFloat32, Type::None(),
603 MachineRepresentation::kBit);
604 r.CheckTypeError(MachineRepresentation::kBit, Type::None(),
605 MachineRepresentation::kFloat32);
606 r.CheckTypeError(MachineRepresentation::kBit, Type::Boolean(),
607 MachineRepresentation::kFloat32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608
609 // Word64 is internal and shouldn't be implicitly converted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000610 r.CheckTypeError(MachineRepresentation::kWord64, Type::None(),
611 MachineRepresentation::kTagged);
612 r.CheckTypeError(MachineRepresentation::kTagged, Type::None(),
613 MachineRepresentation::kWord64);
614 r.CheckTypeError(MachineRepresentation::kTagged, Type::Boolean(),
615 MachineRepresentation::kWord64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616
617 // Word64 / Word32 shouldn't be implicitly converted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000618 r.CheckTypeError(MachineRepresentation::kWord64, Type::None(),
619 MachineRepresentation::kWord32);
620 r.CheckTypeError(MachineRepresentation::kWord32, Type::None(),
621 MachineRepresentation::kWord64);
622 r.CheckTypeError(MachineRepresentation::kWord32, Type::Signed32(),
623 MachineRepresentation::kWord64);
624 r.CheckTypeError(MachineRepresentation::kWord32, Type::Unsigned32(),
625 MachineRepresentation::kWord64);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000627
628} // namespace compiler
629} // namespace internal
630} // namespace v8