Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 <limits> |
| 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "test/cctest/cctest.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "test/cctest/compiler/codegen-tester.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "test/cctest/compiler/graph-builder-tester.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10 | #include "test/cctest/compiler/value-helper.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | |
| 12 | #include "src/compiler/node-matchers.h" |
| 13 | #include "src/compiler/representation-change.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | namespace v8 { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | namespace internal { |
| 17 | namespace compiler { |
| 18 | |
| 19 | class RepresentationChangerTester : public HandleAndZoneScope, |
| 20 | public GraphAndBuilders { |
| 21 | public: |
| 22 | explicit RepresentationChangerTester(int num_parameters = 0) |
| 23 | : GraphAndBuilders(main_zone()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | javascript_(main_zone()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 25 | jsgraph_(main_isolate(), main_graph_, &main_common_, &javascript_, |
| 26 | &main_simplified_, &main_machine_), |
| 27 | changer_(&jsgraph_, main_isolate()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | Node* s = graph()->NewNode(common()->Start(num_parameters)); |
| 29 | graph()->SetStart(s); |
| 30 | } |
| 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 49 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 58 | CHECK_DOUBLE_EQ(expected, m.Value()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void CheckFloat32Constant(Node* n, float expected) { |
| 62 | CHECK_EQ(IrOpcode::kFloat32Constant, n->opcode()); |
| 63 | float fval = OpParameter<float>(n->op()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 64 | CHECK_FLOAT_EQ(expected, fval); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | void CheckHeapConstant(Node* n, HeapObject* expected) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | HeapObjectMatcher m(n); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | CHECK(m.HasValue()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 70 | CHECK_EQ(expected, *m.Value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void CheckNumberConstant(Node* n, double expected) { |
| 74 | NumberMatcher m(n); |
| 75 | CHECK_EQ(IrOpcode::kNumberConstant, n->opcode()); |
| 76 | CHECK(m.HasValue()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 77 | CHECK_DOUBLE_EQ(expected, m.Value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Node* Parameter(int index = 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | Node* n = graph()->NewNode(common()->Parameter(index), graph()->start()); |
| 82 | NodeProperties::SetType(n, Type::Any()); |
| 83 | return n; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 86 | void CheckTypeError(MachineRepresentation from, Type* from_type, |
| 87 | MachineRepresentation to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | changer()->testing_type_errors_ = true; |
| 89 | changer()->type_error_ = false; |
| 90 | Node* n = Parameter(0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 91 | Node* c = changer()->GetRepresentationFor(n, from, from_type, to); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | CHECK(changer()->type_error_); |
| 93 | CHECK_EQ(n, c); |
| 94 | } |
| 95 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 96 | void CheckNop(MachineRepresentation from, Type* from_type, |
| 97 | MachineRepresentation to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | Node* n = Parameter(0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | Node* c = changer()->GetRepresentationFor(n, from, from_type, to); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | CHECK_EQ(n, c); |
| 101 | } |
| 102 | }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | |
| 104 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | const 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | TEST(BoolToBit_constant) { |
| 113 | RepresentationChangerTester r; |
| 114 | |
| 115 | Node* true_node = r.jsgraph()->TrueConstant(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | Node* true_bit = r.changer()->GetRepresentationFor( |
| 117 | true_node, MachineRepresentation::kTagged, Type::None(), |
| 118 | MachineRepresentation::kBit); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | r.CheckInt32Constant(true_bit, 1); |
| 120 | |
| 121 | Node* false_node = r.jsgraph()->FalseConstant(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | Node* false_bit = r.changer()->GetRepresentationFor( |
| 123 | false_node, MachineRepresentation::kTagged, Type::None(), |
| 124 | MachineRepresentation::kBit); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 125 | r.CheckInt32Constant(false_bit, 0); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | TEST(BitToBool_constant) { |
| 130 | RepresentationChangerTester r; |
| 131 | |
| 132 | for (int i = -5; i < 5; i++) { |
| 133 | Node* node = r.jsgraph()->Int32Constant(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 134 | Node* val = r.changer()->GetRepresentationFor( |
| 135 | node, MachineRepresentation::kBit, Type::Boolean(), |
| 136 | MachineRepresentation::kTagged); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | r.CheckHeapConstant(val, i == 0 ? r.isolate()->heap()->false_value() |
| 138 | : r.isolate()->heap()->true_value()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | TEST(ToTagged_constant) { |
| 144 | RepresentationChangerTester r; |
| 145 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 146 | { |
| 147 | FOR_FLOAT64_INPUTS(i) { |
| 148 | Node* n = r.jsgraph()->Float64Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | Node* c = r.changer()->GetRepresentationFor( |
| 150 | n, MachineRepresentation::kFloat64, Type::None(), |
| 151 | MachineRepresentation::kTagged); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 152 | r.CheckNumberConstant(c, *i); |
| 153 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 156 | { |
| 157 | FOR_FLOAT64_INPUTS(i) { |
| 158 | Node* n = r.jsgraph()->Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 159 | Node* c = r.changer()->GetRepresentationFor( |
| 160 | n, MachineRepresentation::kFloat64, Type::None(), |
| 161 | MachineRepresentation::kTagged); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 162 | r.CheckNumberConstant(c, *i); |
| 163 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 166 | { |
| 167 | FOR_FLOAT32_INPUTS(i) { |
| 168 | Node* n = r.jsgraph()->Float32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 169 | Node* c = r.changer()->GetRepresentationFor( |
| 170 | n, MachineRepresentation::kFloat32, Type::None(), |
| 171 | MachineRepresentation::kTagged); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 172 | r.CheckNumberConstant(c, *i); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | { |
| 177 | FOR_INT32_INPUTS(i) { |
| 178 | Node* n = r.jsgraph()->Int32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 179 | Node* c = r.changer()->GetRepresentationFor( |
| 180 | n, MachineRepresentation::kWord32, Type::Signed32(), |
| 181 | MachineRepresentation::kTagged); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 182 | r.CheckNumberConstant(c, *i); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | { |
| 187 | FOR_UINT32_INPUTS(i) { |
| 188 | Node* n = r.jsgraph()->Int32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | Node* c = r.changer()->GetRepresentationFor( |
| 190 | n, MachineRepresentation::kWord32, Type::Unsigned32(), |
| 191 | MachineRepresentation::kTagged); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 192 | r.CheckNumberConstant(c, *i); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | TEST(ToFloat64_constant) { |
| 199 | RepresentationChangerTester r; |
| 200 | |
| 201 | { |
| 202 | FOR_FLOAT64_INPUTS(i) { |
| 203 | Node* n = r.jsgraph()->Float64Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 204 | Node* c = r.changer()->GetRepresentationFor( |
| 205 | n, MachineRepresentation::kFloat64, Type::None(), |
| 206 | MachineRepresentation::kFloat64); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 207 | CHECK_EQ(n, c); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | { |
| 212 | FOR_FLOAT64_INPUTS(i) { |
| 213 | Node* n = r.jsgraph()->Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 214 | Node* c = r.changer()->GetRepresentationFor( |
| 215 | n, MachineRepresentation::kTagged, Type::None(), |
| 216 | MachineRepresentation::kFloat64); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 217 | r.CheckFloat64Constant(c, *i); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | { |
| 222 | FOR_FLOAT32_INPUTS(i) { |
| 223 | Node* n = r.jsgraph()->Float32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 224 | Node* c = r.changer()->GetRepresentationFor( |
| 225 | n, MachineRepresentation::kFloat32, Type::None(), |
| 226 | MachineRepresentation::kFloat64); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 227 | r.CheckFloat64Constant(c, *i); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | { |
| 232 | FOR_INT32_INPUTS(i) { |
| 233 | Node* n = r.jsgraph()->Int32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 234 | Node* c = r.changer()->GetRepresentationFor( |
| 235 | n, MachineRepresentation::kWord32, Type::Signed32(), |
| 236 | MachineRepresentation::kFloat64); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 237 | r.CheckFloat64Constant(c, *i); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | { |
| 242 | FOR_UINT32_INPUTS(i) { |
| 243 | Node* n = r.jsgraph()->Int32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 244 | Node* c = r.changer()->GetRepresentationFor( |
| 245 | n, MachineRepresentation::kWord32, Type::Unsigned32(), |
| 246 | MachineRepresentation::kFloat64); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 247 | r.CheckFloat64Constant(c, *i); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static bool IsFloat32Int32(int32_t val) { |
| 254 | return val >= -(1 << 23) && val <= (1 << 23); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static bool IsFloat32Uint32(uint32_t val) { return val <= (1 << 23); } |
| 259 | |
| 260 | |
| 261 | TEST(ToFloat32_constant) { |
| 262 | RepresentationChangerTester r; |
| 263 | |
| 264 | { |
| 265 | FOR_FLOAT32_INPUTS(i) { |
| 266 | Node* n = r.jsgraph()->Float32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 267 | Node* c = r.changer()->GetRepresentationFor( |
| 268 | n, MachineRepresentation::kFloat32, Type::None(), |
| 269 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 270 | CHECK_EQ(n, c); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | { |
| 275 | FOR_FLOAT32_INPUTS(i) { |
| 276 | Node* n = r.jsgraph()->Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 277 | Node* c = r.changer()->GetRepresentationFor( |
| 278 | n, MachineRepresentation::kTagged, Type::None(), |
| 279 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 280 | r.CheckFloat32Constant(c, *i); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | { |
| 285 | FOR_FLOAT32_INPUTS(i) { |
| 286 | Node* n = r.jsgraph()->Float64Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 287 | Node* c = r.changer()->GetRepresentationFor( |
| 288 | n, MachineRepresentation::kFloat64, Type::None(), |
| 289 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 290 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 298 | Node* c = r.changer()->GetRepresentationFor( |
| 299 | n, MachineRepresentation::kWord32, Type::Signed32(), |
| 300 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 301 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | Node* c = r.changer()->GetRepresentationFor( |
| 310 | n, MachineRepresentation::kWord32, Type::Unsigned32(), |
| 311 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 312 | r.CheckFloat32Constant(c, static_cast<float>(*i)); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | TEST(ToInt32_constant) { |
| 319 | RepresentationChangerTester r; |
| 320 | |
| 321 | { |
| 322 | FOR_INT32_INPUTS(i) { |
| 323 | Node* n = r.jsgraph()->Int32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 324 | Node* c = r.changer()->GetRepresentationFor( |
| 325 | n, MachineRepresentation::kWord32, Type::Signed32(), |
| 326 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 327 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 335 | Node* c = r.changer()->GetRepresentationFor( |
| 336 | n, MachineRepresentation::kFloat32, Type::Signed32(), |
| 337 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 338 | r.CheckInt32Constant(c, *i); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | { |
| 343 | FOR_INT32_INPUTS(i) { |
| 344 | Node* n = r.jsgraph()->Float64Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 345 | Node* c = r.changer()->GetRepresentationFor( |
| 346 | n, MachineRepresentation::kFloat64, Type::Signed32(), |
| 347 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 348 | r.CheckInt32Constant(c, *i); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | { |
| 353 | FOR_INT32_INPUTS(i) { |
| 354 | Node* n = r.jsgraph()->Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 355 | Node* c = r.changer()->GetRepresentationFor( |
| 356 | n, MachineRepresentation::kTagged, Type::Signed32(), |
| 357 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 358 | r.CheckInt32Constant(c, *i); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | |
| 364 | TEST(ToUint32_constant) { |
| 365 | RepresentationChangerTester r; |
| 366 | |
| 367 | { |
| 368 | FOR_UINT32_INPUTS(i) { |
| 369 | Node* n = r.jsgraph()->Int32Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 370 | Node* c = r.changer()->GetRepresentationFor( |
| 371 | n, MachineRepresentation::kWord32, Type::Unsigned32(), |
| 372 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 373 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 381 | Node* c = r.changer()->GetRepresentationFor( |
| 382 | n, MachineRepresentation::kFloat32, Type::Unsigned32(), |
| 383 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 384 | r.CheckUint32Constant(c, *i); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | { |
| 389 | FOR_UINT32_INPUTS(i) { |
| 390 | Node* n = r.jsgraph()->Float64Constant(*i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 391 | Node* c = r.changer()->GetRepresentationFor( |
| 392 | n, MachineRepresentation::kFloat64, Type::Unsigned32(), |
| 393 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 394 | r.CheckUint32Constant(c, *i); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | { |
| 399 | FOR_UINT32_INPUTS(i) { |
| 400 | Node* n = r.jsgraph()->Constant(static_cast<double>(*i)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 401 | Node* c = r.changer()->GetRepresentationFor( |
| 402 | n, MachineRepresentation::kTagged, Type::Unsigned32(), |
| 403 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 404 | r.CheckUint32Constant(c, *i); |
| 405 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 410 | static void CheckChange(IrOpcode::Value expected, MachineRepresentation from, |
| 411 | Type* from_type, MachineRepresentation to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 412 | RepresentationChangerTester r; |
| 413 | |
| 414 | Node* n = r.Parameter(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 415 | Node* c = r.changer()->GetRepresentationFor(n, from, from_type, to); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 416 | |
| 417 | CHECK_NE(c, n); |
| 418 | CHECK_EQ(expected, c->opcode()); |
| 419 | CHECK_EQ(n, c->InputAt(0)); |
| 420 | } |
| 421 | |
| 422 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 423 | static void CheckTwoChanges(IrOpcode::Value expected2, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 424 | IrOpcode::Value expected1, |
| 425 | MachineRepresentation from, Type* from_type, |
| 426 | MachineRepresentation to) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 427 | RepresentationChangerTester r; |
| 428 | |
| 429 | Node* n = r.Parameter(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 430 | Node* c1 = r.changer()->GetRepresentationFor(n, from, from_type, to); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 431 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 441 | TEST(SingleChanges) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 442 | CheckChange(IrOpcode::kChangeBoolToBit, MachineRepresentation::kTagged, |
| 443 | Type::None(), MachineRepresentation::kBit); |
| 444 | CheckChange(IrOpcode::kChangeBitToBool, MachineRepresentation::kBit, |
| 445 | Type::None(), MachineRepresentation::kTagged); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 446 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 447 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 453 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 454 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 460 | |
| 461 | // Int32,Uint32 <-> Float64 are actually machine conversions. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 462 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 470 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 471 | CheckChange(IrOpcode::kTruncateFloat64ToFloat32, |
| 472 | MachineRepresentation::kFloat64, Type::None(), |
| 473 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 474 | |
| 475 | // Int32,Uint32 <-> Float32 require two changes. |
| 476 | CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 477 | IrOpcode::kTruncateFloat64ToFloat32, |
| 478 | MachineRepresentation::kWord32, Type::Signed32(), |
| 479 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 480 | CheckTwoChanges(IrOpcode::kChangeUint32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 481 | IrOpcode::kTruncateFloat64ToFloat32, |
| 482 | MachineRepresentation::kWord32, Type::Unsigned32(), |
| 483 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 484 | CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 485 | IrOpcode::kChangeFloat64ToInt32, |
| 486 | MachineRepresentation::kFloat32, Type::Signed32(), |
| 487 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 488 | CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 489 | IrOpcode::kChangeFloat64ToUint32, |
| 490 | MachineRepresentation::kFloat32, Type::Unsigned32(), |
| 491 | MachineRepresentation::kWord32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 492 | |
| 493 | // Float32 <-> Tagged require two changes. |
| 494 | CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 495 | IrOpcode::kChangeFloat64ToTagged, |
| 496 | MachineRepresentation::kFloat32, Type::None(), |
| 497 | MachineRepresentation::kTagged); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 498 | CheckTwoChanges(IrOpcode::kChangeTaggedToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 499 | IrOpcode::kTruncateFloat64ToFloat32, |
| 500 | MachineRepresentation::kTagged, Type::None(), |
| 501 | MachineRepresentation::kFloat32); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | |
| 505 | TEST(SignednessInWord32) { |
| 506 | RepresentationChangerTester r; |
| 507 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 508 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 519 | |
| 520 | CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 521 | IrOpcode::kTruncateFloat64ToFloat32, |
| 522 | MachineRepresentation::kWord32, Type::None(), |
| 523 | MachineRepresentation::kFloat32); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 524 | CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 525 | IrOpcode::kTruncateFloat64ToInt32, |
| 526 | MachineRepresentation::kFloat32, Type::Number(), |
| 527 | MachineRepresentation::kWord32); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | |
| 531 | TEST(Nops) { |
| 532 | RepresentationChangerTester r; |
| 533 | |
| 534 | // X -> X is always a nop for any single representation X. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 535 | for (size_t i = 0; i < arraysize(kMachineTypes); i++) { |
| 536 | r.CheckNop(kMachineTypes[i].representation(), Type::None(), |
| 537 | kMachineTypes[i].representation()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // 32-bit floats. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 541 | r.CheckNop(MachineRepresentation::kFloat32, Type::None(), |
| 542 | MachineRepresentation::kFloat32); |
| 543 | r.CheckNop(MachineRepresentation::kFloat32, Type::Number(), |
| 544 | MachineRepresentation::kFloat32); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 545 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 549 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 559 | |
| 560 | // kRepBit (result of comparison) is implicitly a wordish thing. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 561 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | TEST(TypeErrors) { |
| 581 | RepresentationChangerTester r; |
| 582 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 583 | // 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | |
| 593 | // Floats cannot be implicitly converted to/from comparison conditions. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 594 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 608 | |
| 609 | // Word64 is internal and shouldn't be implicitly converted. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 610 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 616 | |
| 617 | // Word64 / Word32 shouldn't be implicitly converted. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 618 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 626 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 627 | |
| 628 | } // namespace compiler |
| 629 | } // namespace internal |
| 630 | } // namespace v8 |