summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/compiler/test-representation-change.cc
blob: 092a5f7d90cab204aa9fe1a055240f87d382b0f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <limits>

#include "src/v8.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/graph-builder-tester.h"

#include "src/compiler/node-matchers.h"
#include "src/compiler/representation-change.h"
#include "src/compiler/typer.h"

using namespace v8::internal;
using namespace v8::internal::compiler;

namespace v8 {  // for friendiness.
namespace internal {
namespace compiler {

class RepresentationChangerTester : public HandleAndZoneScope,
                                    public GraphAndBuilders {
 public:
  explicit RepresentationChangerTester(int num_parameters = 0)
      : GraphAndBuilders(main_zone()),
        typer_(main_zone()),
        jsgraph_(main_graph_, &main_common_, &typer_),
        changer_(&jsgraph_, &main_simplified_, &main_machine_, main_isolate()) {
    Node* s = graph()->NewNode(common()->Start(num_parameters));
    graph()->SetStart(s);
  }

  Typer typer_;
  JSGraph jsgraph_;
  RepresentationChanger changer_;

  Isolate* isolate() { return main_isolate(); }
  Graph* graph() { return main_graph_; }
  CommonOperatorBuilder* common() { return &main_common_; }
  JSGraph* jsgraph() { return &jsgraph_; }
  RepresentationChanger* changer() { return &changer_; }

  // TODO(titzer): use ValueChecker / ValueUtil
  void CheckInt32Constant(Node* n, int32_t expected) {
    ValueMatcher<int32_t> m(n);
    CHECK(m.HasValue());
    CHECK_EQ(expected, m.Value());
  }

  void CheckHeapConstant(Node* n, Object* expected) {
    ValueMatcher<Handle<Object> > m(n);
    CHECK(m.HasValue());
    CHECK_EQ(expected, *m.Value());
  }

  void CheckNumberConstant(Node* n, double expected) {
    ValueMatcher<double> m(n);
    CHECK_EQ(IrOpcode::kNumberConstant, n->opcode());
    CHECK(m.HasValue());
    CHECK_EQ(expected, m.Value());
  }

  Node* Parameter(int index = 0) {
    return graph()->NewNode(common()->Parameter(index), graph()->start());
  }

  void CheckTypeError(RepTypeUnion from, RepTypeUnion to) {
    changer()->testing_type_errors_ = true;
    changer()->type_error_ = false;
    Node* n = Parameter(0);
    Node* c = changer()->GetRepresentationFor(n, from, to);
    CHECK_EQ(n, c);
    CHECK(changer()->type_error_);
  }

  void CheckNop(RepTypeUnion from, RepTypeUnion to) {
    Node* n = Parameter(0);
    Node* c = changer()->GetRepresentationFor(n, from, to);
    CHECK_EQ(n, c);
  }
};
}
}
}  // namespace v8::internal::compiler


static const RepType all_reps[] = {rBit, rWord32, rWord64, rFloat64, rTagged};


// TODO(titzer): lift this to ValueHelper
static const double double_inputs[] = {
    0.0,   -0.0,    1.0,    -1.0,        0.1,         1.4,    -1.7,
    2,     5,       6,      982983,      888,         -999.8, 3.1e7,
    -2e66, 2.3e124, -12e73, V8_INFINITY, -V8_INFINITY};


static const int32_t int32_inputs[] = {
    0,      1,                                -1,
    2,      5,                                6,
    982983, 888,                              -999,
    65535,  static_cast<int32_t>(0xFFFFFFFF), static_cast<int32_t>(0x80000000)};


static const uint32_t uint32_inputs[] = {
    0,      1,   static_cast<uint32_t>(-1),   2,     5,          6,
    982983, 888, static_cast<uint32_t>(-999), 65535, 0xFFFFFFFF, 0x80000000};


TEST(BoolToBit_constant) {
  RepresentationChangerTester r;

  Node* true_node = r.jsgraph()->TrueConstant();
  Node* true_bit = r.changer()->GetRepresentationFor(true_node, rTagged, rBit);
  r.CheckInt32Constant(true_bit, 1);

  Node* false_node = r.jsgraph()->FalseConstant();
  Node* false_bit =
      r.changer()->GetRepresentationFor(false_node, rTagged, rBit);
  r.CheckInt32Constant(false_bit, 0);
}


TEST(BitToBool_constant) {
  RepresentationChangerTester r;

  for (int i = -5; i < 5; i++) {
    Node* node = r.jsgraph()->Int32Constant(i);
    Node* val = r.changer()->GetRepresentationFor(node, rBit, rTagged);
    r.CheckHeapConstant(val, i == 0 ? r.isolate()->heap()->false_value()
                                    : r.isolate()->heap()->true_value());
  }
}


TEST(ToTagged_constant) {
  RepresentationChangerTester r;

  for (size_t i = 0; i < ARRAY_SIZE(double_inputs); i++) {
    Node* n = r.jsgraph()->Float64Constant(double_inputs[i]);
    Node* c = r.changer()->GetRepresentationFor(n, rFloat64, rTagged);
    r.CheckNumberConstant(c, double_inputs[i]);
  }

  for (size_t i = 0; i < ARRAY_SIZE(int32_inputs); i++) {
    Node* n = r.jsgraph()->Int32Constant(int32_inputs[i]);
    Node* c = r.changer()->GetRepresentationFor(n, rWord32 | tInt32, rTagged);
    r.CheckNumberConstant(c, static_cast<double>(int32_inputs[i]));
  }

  for (size_t i = 0; i < ARRAY_SIZE(uint32_inputs); i++) {
    Node* n = r.jsgraph()->Int32Constant(uint32_inputs[i]);
    Node* c = r.changer()->GetRepresentationFor(n, rWord32 | tUint32, rTagged);
    r.CheckNumberConstant(c, static_cast<double>(uint32_inputs[i]));
  }
}


static void CheckChange(IrOpcode::Value expected, RepTypeUnion from,
                        RepTypeUnion to) {
  RepresentationChangerTester r;

  Node* n = r.Parameter();
  Node* c = r.changer()->GetRepresentationFor(n, from, to);

  CHECK_NE(c, n);
  CHECK_EQ(expected, c->opcode());
  CHECK_EQ(n, c->InputAt(0));
}


TEST(SingleChanges) {
  CheckChange(IrOpcode::kChangeBoolToBit, rTagged, rBit);
  CheckChange(IrOpcode::kChangeBitToBool, rBit, rTagged);

  CheckChange(IrOpcode::kChangeInt32ToTagged, rWord32 | tInt32, rTagged);
  CheckChange(IrOpcode::kChangeUint32ToTagged, rWord32 | tUint32, rTagged);
  CheckChange(IrOpcode::kChangeFloat64ToTagged, rFloat64, rTagged);

  CheckChange(IrOpcode::kChangeTaggedToInt32, rTagged | tInt32, rWord32);
  CheckChange(IrOpcode::kChangeTaggedToUint32, rTagged | tUint32, rWord32);
  CheckChange(IrOpcode::kChangeTaggedToFloat64, rTagged, rFloat64);

  // Int32,Uint32 <-> Float64 are actually machine conversions.
  CheckChange(IrOpcode::kChangeInt32ToFloat64, rWord32 | tInt32, rFloat64);
  CheckChange(IrOpcode::kChangeUint32ToFloat64, rWord32 | tUint32, rFloat64);
  CheckChange(IrOpcode::kChangeFloat64ToInt32, rFloat64 | tInt32, rWord32);
  CheckChange(IrOpcode::kChangeFloat64ToUint32, rFloat64 | tUint32, rWord32);
}


TEST(SignednessInWord32) {
  RepresentationChangerTester r;

  // TODO(titzer): assume that uses of a word32 without a sign mean tInt32.
  CheckChange(IrOpcode::kChangeTaggedToInt32, rTagged, rWord32 | tInt32);
  CheckChange(IrOpcode::kChangeTaggedToUint32, rTagged, rWord32 | tUint32);
  CheckChange(IrOpcode::kChangeInt32ToFloat64, rWord32, rFloat64);
  CheckChange(IrOpcode::kChangeFloat64ToInt32, rFloat64, rWord32);
}


TEST(Nops) {
  RepresentationChangerTester r;

  // X -> X is always a nop for any single representation X.
  for (size_t i = 0; i < ARRAY_SIZE(all_reps); i++) {
    r.CheckNop(all_reps[i], all_reps[i]);
  }

  // 32-bit or 64-bit words can be used as branch conditions (rBit).
  r.CheckNop(rWord32, rBit);
  r.CheckNop(rWord32, rBit | tBool);
  r.CheckNop(rWord64, rBit);
  r.CheckNop(rWord64, rBit | tBool);

  // rBit (result of comparison) is implicitly a wordish thing.
  r.CheckNop(rBit, rWord32);
  r.CheckNop(rBit | tBool, rWord32);
  r.CheckNop(rBit, rWord64);
  r.CheckNop(rBit | tBool, rWord64);
}


TEST(TypeErrors) {
  RepresentationChangerTester r;

  // Floats cannot be implicitly converted to/from comparison conditions.
  r.CheckTypeError(rFloat64, rBit);
  r.CheckTypeError(rFloat64, rBit | tBool);
  r.CheckTypeError(rBit, rFloat64);
  r.CheckTypeError(rBit | tBool, rFloat64);

  // Word64 is internal and shouldn't be implicitly converted.
  r.CheckTypeError(rWord64, rTagged | tBool);
  r.CheckTypeError(rWord64, rTagged);
  r.CheckTypeError(rWord64, rTagged | tBool);
  r.CheckTypeError(rTagged, rWord64);
  r.CheckTypeError(rTagged | tBool, rWord64);

  // Word64 / Word32 shouldn't be implicitly converted.
  r.CheckTypeError(rWord64, rWord32);
  r.CheckTypeError(rWord32, rWord64);
  r.CheckTypeError(rWord64, rWord32 | tInt32);
  r.CheckTypeError(rWord32 | tInt32, rWord64);
  r.CheckTypeError(rWord64, rWord32 | tUint32);
  r.CheckTypeError(rWord32 | tUint32, rWord64);

  for (size_t i = 0; i < ARRAY_SIZE(all_reps); i++) {
    for (size_t j = 0; j < ARRAY_SIZE(all_reps); j++) {
      if (i == j) continue;
      // Only a single from representation is allowed.
      r.CheckTypeError(all_reps[i] | all_reps[j], rTagged);
    }
  }
}


TEST(CompleteMatrix) {
  // TODO(titzer): test all variants in the matrix.
  // rB
  // tBrB
  // tBrT
  // rW32
  // tIrW32
  // tUrW32
  // rW64
  // tIrW64
  // tUrW64
  // rF64
  // tIrF64
  // tUrF64
  // tArF64
  // rT
  // tArT
}