| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 "src/compiler/representation-change.h" |
| 6 | |
| 7 | #include <sstream> |
| 8 | |
| 9 | #include "src/base/bits.h" |
| 10 | #include "src/code-factory.h" |
| 11 | #include "src/compiler/machine-operator.h" |
| 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | namespace compiler { |
| 16 | |
| 17 | const char* Truncation::description() const { |
| 18 | switch (kind()) { |
| 19 | case TruncationKind::kNone: |
| 20 | return "no-value-use"; |
| 21 | case TruncationKind::kBool: |
| 22 | return "truncate-to-bool"; |
| 23 | case TruncationKind::kWord32: |
| 24 | return "truncate-to-word32"; |
| 25 | case TruncationKind::kWord64: |
| 26 | return "truncate-to-word64"; |
| 27 | case TruncationKind::kFloat32: |
| 28 | return "truncate-to-float32"; |
| 29 | case TruncationKind::kFloat64: |
| 30 | return "truncate-to-float64"; |
| 31 | case TruncationKind::kAny: |
| 32 | return "no-truncation"; |
| 33 | } |
| 34 | UNREACHABLE(); |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | // Partial order for truncations: |
| 40 | // |
| 41 | // kWord64 kAny |
| 42 | // ^ ^ |
| 43 | // \ | |
| 44 | // \ kFloat64 <--+ |
| 45 | // \ ^ ^ | |
| 46 | // \ / | | |
| 47 | // kWord32 kFloat32 kBool |
| 48 | // ^ ^ ^ |
| 49 | // \ | / |
| 50 | // \ | / |
| 51 | // \ | / |
| 52 | // \ | / |
| 53 | // \ | / |
| 54 | // kNone |
| 55 | |
| 56 | // static |
| 57 | Truncation::TruncationKind Truncation::Generalize(TruncationKind rep1, |
| 58 | TruncationKind rep2) { |
| 59 | if (LessGeneral(rep1, rep2)) return rep2; |
| 60 | if (LessGeneral(rep2, rep1)) return rep1; |
| 61 | // Handle the generalization of float64-representable values. |
| 62 | if (LessGeneral(rep1, TruncationKind::kFloat64) && |
| 63 | LessGeneral(rep2, TruncationKind::kFloat64)) { |
| 64 | return TruncationKind::kFloat64; |
| 65 | } |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 66 | // Handle the generalization of any-representable values. |
| 67 | if (LessGeneral(rep1, TruncationKind::kAny) && |
| 68 | LessGeneral(rep2, TruncationKind::kAny)) { |
| 69 | return TruncationKind::kAny; |
| 70 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 71 | // All other combinations are illegal. |
| 72 | FATAL("Tried to combine incompatible truncations"); |
| 73 | return TruncationKind::kNone; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // static |
| 78 | bool Truncation::LessGeneral(TruncationKind rep1, TruncationKind rep2) { |
| 79 | switch (rep1) { |
| 80 | case TruncationKind::kNone: |
| 81 | return true; |
| 82 | case TruncationKind::kBool: |
| 83 | return rep2 == TruncationKind::kBool || rep2 == TruncationKind::kAny; |
| 84 | case TruncationKind::kWord32: |
| 85 | return rep2 == TruncationKind::kWord32 || |
| 86 | rep2 == TruncationKind::kWord64 || |
| 87 | rep2 == TruncationKind::kFloat64 || rep2 == TruncationKind::kAny; |
| 88 | case TruncationKind::kWord64: |
| 89 | return rep2 == TruncationKind::kWord64; |
| 90 | case TruncationKind::kFloat32: |
| 91 | return rep2 == TruncationKind::kFloat32 || |
| 92 | rep2 == TruncationKind::kFloat64 || rep2 == TruncationKind::kAny; |
| 93 | case TruncationKind::kFloat64: |
| 94 | return rep2 == TruncationKind::kFloat64 || rep2 == TruncationKind::kAny; |
| 95 | case TruncationKind::kAny: |
| 96 | return rep2 == TruncationKind::kAny; |
| 97 | } |
| 98 | UNREACHABLE(); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | namespace { |
| 104 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | bool IsWord(MachineRepresentation rep) { |
| 106 | return rep == MachineRepresentation::kWord8 || |
| 107 | rep == MachineRepresentation::kWord16 || |
| 108 | rep == MachineRepresentation::kWord32; |
| 109 | } |
| 110 | |
| 111 | } // namespace |
| 112 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | // Changes representation from {output_rep} to {use_rep}. The {truncation} |
| 114 | // parameter is only used for sanity checking - if the changer cannot figure |
| 115 | // out signedness for the word32->float64 conversion, then we check that the |
| 116 | // uses truncate to word32 (so they do not care about signedness). |
| 117 | Node* RepresentationChanger::GetRepresentationFor( |
| 118 | Node* node, MachineRepresentation output_rep, Type* output_type, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 119 | Node* use_node, UseInfo use_info) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 120 | if (output_rep == MachineRepresentation::kNone && |
| 121 | output_type->IsInhabited()) { |
| 122 | // The output representation should be set if the type is inhabited (i.e., |
| 123 | // if the value is possible). |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 124 | return TypeError(node, output_rep, output_type, use_info.representation()); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 126 | |
| 127 | // Handle the no-op shortcuts when no checking is necessary. |
| 128 | if (use_info.type_check() == TypeCheckKind::kNone || |
| 129 | output_rep != MachineRepresentation::kWord32) { |
| 130 | if (use_info.representation() == output_rep) { |
| 131 | // Representations are the same. That's a no-op. |
| 132 | return node; |
| 133 | } |
| 134 | if (IsWord(use_info.representation()) && IsWord(output_rep)) { |
| 135 | // Both are words less than or equal to 32-bits. |
| 136 | // Since loads of integers from memory implicitly sign or zero extend the |
| 137 | // value to the full machine word size and stores implicitly truncate, |
| 138 | // no representation change is necessary. |
| 139 | return node; |
| 140 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 142 | |
| 143 | switch (use_info.representation()) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 144 | case MachineRepresentation::kTaggedSigned: |
| 145 | case MachineRepresentation::kTaggedPointer: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 146 | case MachineRepresentation::kTagged: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 147 | DCHECK(use_info.type_check() == TypeCheckKind::kNone); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 148 | return GetTaggedRepresentationFor(node, output_rep, output_type); |
| 149 | case MachineRepresentation::kFloat32: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 150 | DCHECK(use_info.type_check() == TypeCheckKind::kNone); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 151 | return GetFloat32RepresentationFor(node, output_rep, output_type, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 152 | use_info.truncation()); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 153 | case MachineRepresentation::kFloat64: |
| 154 | return GetFloat64RepresentationFor(node, output_rep, output_type, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 155 | use_node, use_info); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | case MachineRepresentation::kBit: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 157 | DCHECK(use_info.type_check() == TypeCheckKind::kNone); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 158 | return GetBitRepresentationFor(node, output_rep, output_type); |
| 159 | case MachineRepresentation::kWord8: |
| 160 | case MachineRepresentation::kWord16: |
| 161 | case MachineRepresentation::kWord32: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 162 | return GetWord32RepresentationFor(node, output_rep, output_type, use_node, |
| 163 | use_info); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 164 | case MachineRepresentation::kWord64: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 165 | DCHECK(use_info.type_check() == TypeCheckKind::kNone); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 166 | return GetWord64RepresentationFor(node, output_rep, output_type); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 167 | case MachineRepresentation::kSimd128: // Fall through. |
| 168 | // TODO(bbudge) Handle conversions between tagged and untagged. |
| 169 | break; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | case MachineRepresentation::kNone: |
| 171 | return node; |
| 172 | } |
| 173 | UNREACHABLE(); |
| 174 | return nullptr; |
| 175 | } |
| 176 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | Node* RepresentationChanger::GetTaggedRepresentationFor( |
| 178 | Node* node, MachineRepresentation output_rep, Type* output_type) { |
| 179 | // Eagerly fold representation changes for constants. |
| 180 | switch (node->opcode()) { |
| 181 | case IrOpcode::kNumberConstant: |
| 182 | case IrOpcode::kHeapConstant: |
| 183 | return node; // No change necessary. |
| 184 | case IrOpcode::kInt32Constant: |
| 185 | if (output_type->Is(Type::Signed32())) { |
| 186 | int32_t value = OpParameter<int32_t>(node); |
| 187 | return jsgraph()->Constant(value); |
| 188 | } else if (output_type->Is(Type::Unsigned32())) { |
| 189 | uint32_t value = static_cast<uint32_t>(OpParameter<int32_t>(node)); |
| 190 | return jsgraph()->Constant(static_cast<double>(value)); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 191 | } else if (output_type->Is(Type::Boolean())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 192 | return OpParameter<int32_t>(node) == 0 ? jsgraph()->FalseConstant() |
| 193 | : jsgraph()->TrueConstant(); |
| 194 | } else { |
| 195 | return TypeError(node, output_rep, output_type, |
| 196 | MachineRepresentation::kTagged); |
| 197 | } |
| 198 | case IrOpcode::kFloat64Constant: |
| 199 | return jsgraph()->Constant(OpParameter<double>(node)); |
| 200 | case IrOpcode::kFloat32Constant: |
| 201 | return jsgraph()->Constant(OpParameter<float>(node)); |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | // Select the correct X -> Tagged operator. |
| 206 | const Operator* op; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 207 | if (output_rep == MachineRepresentation::kNone) { |
| 208 | // We should only asisgn this representation if the type is empty. |
| 209 | CHECK(!output_type->IsInhabited()); |
| 210 | op = machine()->ImpossibleToTagged(); |
| 211 | } else if (output_rep == MachineRepresentation::kBit) { |
| 212 | if (output_type->Is(Type::Boolean())) { |
| 213 | op = simplified()->ChangeBitToTagged(); |
| 214 | } else { |
| 215 | return TypeError(node, output_rep, output_type, |
| 216 | MachineRepresentation::kTagged); |
| 217 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 218 | } else if (IsWord(output_rep)) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 219 | if (output_type->Is(Type::Signed31())) { |
| 220 | op = simplified()->ChangeInt31ToTaggedSigned(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 221 | } else if (output_type->Is(Type::Signed32())) { |
| 222 | op = simplified()->ChangeInt32ToTagged(); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 223 | } else if (output_type->Is(Type::Unsigned32())) { |
| 224 | op = simplified()->ChangeUint32ToTagged(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 225 | } else { |
| 226 | return TypeError(node, output_rep, output_type, |
| 227 | MachineRepresentation::kTagged); |
| 228 | } |
| 229 | } else if (output_rep == |
| 230 | MachineRepresentation::kFloat32) { // float32 -> float64 -> tagged |
| 231 | node = InsertChangeFloat32ToFloat64(node); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 232 | op = simplified()->ChangeFloat64ToTagged( |
| 233 | output_type->Maybe(Type::MinusZero()) |
| 234 | ? CheckForMinusZeroMode::kCheckForMinusZero |
| 235 | : CheckForMinusZeroMode::kDontCheckForMinusZero); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 236 | } else if (output_rep == MachineRepresentation::kFloat64) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 237 | if (output_type->Is(Type::Signed31())) { // float64 -> int32 -> tagged |
| 238 | node = InsertChangeFloat64ToInt32(node); |
| 239 | op = simplified()->ChangeInt31ToTaggedSigned(); |
| 240 | } else if (output_type->Is( |
| 241 | Type::Signed32())) { // float64 -> int32 -> tagged |
| 242 | node = InsertChangeFloat64ToInt32(node); |
| 243 | op = simplified()->ChangeInt32ToTagged(); |
| 244 | } else if (output_type->Is( |
| 245 | Type::Unsigned32())) { // float64 -> uint32 -> tagged |
| 246 | node = InsertChangeFloat64ToUint32(node); |
| 247 | op = simplified()->ChangeUint32ToTagged(); |
| 248 | } else { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 249 | op = simplified()->ChangeFloat64ToTagged( |
| 250 | output_type->Maybe(Type::MinusZero()) |
| 251 | ? CheckForMinusZeroMode::kCheckForMinusZero |
| 252 | : CheckForMinusZeroMode::kDontCheckForMinusZero); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 253 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 254 | } else { |
| 255 | return TypeError(node, output_rep, output_type, |
| 256 | MachineRepresentation::kTagged); |
| 257 | } |
| 258 | return jsgraph()->graph()->NewNode(op, node); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | Node* RepresentationChanger::GetFloat32RepresentationFor( |
| 263 | Node* node, MachineRepresentation output_rep, Type* output_type, |
| 264 | Truncation truncation) { |
| 265 | // Eagerly fold representation changes for constants. |
| 266 | switch (node->opcode()) { |
| 267 | case IrOpcode::kFloat64Constant: |
| 268 | case IrOpcode::kNumberConstant: |
| 269 | return jsgraph()->Float32Constant( |
| 270 | DoubleToFloat32(OpParameter<double>(node))); |
| 271 | case IrOpcode::kInt32Constant: |
| 272 | if (output_type->Is(Type::Unsigned32())) { |
| 273 | uint32_t value = static_cast<uint32_t>(OpParameter<int32_t>(node)); |
| 274 | return jsgraph()->Float32Constant(static_cast<float>(value)); |
| 275 | } else { |
| 276 | int32_t value = OpParameter<int32_t>(node); |
| 277 | return jsgraph()->Float32Constant(static_cast<float>(value)); |
| 278 | } |
| 279 | case IrOpcode::kFloat32Constant: |
| 280 | return node; // No change necessary. |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | // Select the correct X -> Float32 operator. |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 285 | const Operator* op = nullptr; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 286 | if (output_rep == MachineRepresentation::kNone) { |
| 287 | // We should only use kNone representation if the type is empty. |
| 288 | CHECK(!output_type->IsInhabited()); |
| 289 | op = machine()->ImpossibleToFloat32(); |
| 290 | } else if (IsWord(output_rep)) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | if (output_type->Is(Type::Signed32())) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 292 | // int32 -> float64 -> float32 |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 293 | op = machine()->ChangeInt32ToFloat64(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 294 | node = jsgraph()->graph()->NewNode(op, node); |
| 295 | op = machine()->TruncateFloat64ToFloat32(); |
| 296 | } else if (output_type->Is(Type::Unsigned32()) || |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 297 | truncation.IsUsedAsWord32()) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 298 | // Either the output is uint32 or the uses only care about the |
| 299 | // low 32 bits (so we can pick uint32 safely). |
| 300 | |
| 301 | // uint32 -> float64 -> float32 |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 302 | op = machine()->ChangeUint32ToFloat64(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 303 | node = jsgraph()->graph()->NewNode(op, node); |
| 304 | op = machine()->TruncateFloat64ToFloat32(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 305 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 306 | } else if (output_rep == MachineRepresentation::kTagged) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 307 | if (output_type->Is(Type::NumberOrOddball())) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 308 | // tagged -> float64 -> float32 |
| 309 | if (output_type->Is(Type::Number())) { |
| 310 | op = simplified()->ChangeTaggedToFloat64(); |
| 311 | } else { |
| 312 | op = simplified()->TruncateTaggedToFloat64(); |
| 313 | } |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 314 | node = jsgraph()->graph()->NewNode(op, node); |
| 315 | op = machine()->TruncateFloat64ToFloat32(); |
| 316 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 317 | } else if (output_rep == MachineRepresentation::kFloat64) { |
| 318 | op = machine()->TruncateFloat64ToFloat32(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 319 | } |
| 320 | if (op == nullptr) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | return TypeError(node, output_rep, output_type, |
| 322 | MachineRepresentation::kFloat32); |
| 323 | } |
| 324 | return jsgraph()->graph()->NewNode(op, node); |
| 325 | } |
| 326 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 327 | Node* RepresentationChanger::GetFloat64RepresentationFor( |
| 328 | Node* node, MachineRepresentation output_rep, Type* output_type, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 329 | Node* use_node, UseInfo use_info) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 330 | // Eagerly fold representation changes for constants. |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 331 | if ((use_info.type_check() == TypeCheckKind::kNone)) { |
| 332 | // TODO(jarin) Handle checked constant conversions. |
| 333 | switch (node->opcode()) { |
| 334 | case IrOpcode::kNumberConstant: |
| 335 | return jsgraph()->Float64Constant(OpParameter<double>(node)); |
| 336 | case IrOpcode::kInt32Constant: |
| 337 | if (output_type->Is(Type::Signed32())) { |
| 338 | int32_t value = OpParameter<int32_t>(node); |
| 339 | return jsgraph()->Float64Constant(value); |
| 340 | } else { |
| 341 | DCHECK(output_type->Is(Type::Unsigned32())); |
| 342 | uint32_t value = static_cast<uint32_t>(OpParameter<int32_t>(node)); |
| 343 | return jsgraph()->Float64Constant(static_cast<double>(value)); |
| 344 | } |
| 345 | case IrOpcode::kFloat64Constant: |
| 346 | return node; // No change necessary. |
| 347 | case IrOpcode::kFloat32Constant: |
| 348 | return jsgraph()->Float64Constant(OpParameter<float>(node)); |
| 349 | default: |
| 350 | break; |
| 351 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 352 | } |
| 353 | // Select the correct X -> Float64 operator. |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 354 | const Operator* op = nullptr; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 355 | if (output_rep == MachineRepresentation::kNone) { |
| 356 | // We should only use kNone representation if the type is empty. |
| 357 | CHECK(!output_type->IsInhabited()); |
| 358 | op = machine()->ImpossibleToFloat64(); |
| 359 | } else if (IsWord(output_rep)) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 360 | if (output_type->Is(Type::Signed32())) { |
| 361 | op = machine()->ChangeInt32ToFloat64(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 362 | } else if (output_type->Is(Type::Unsigned32()) || |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 363 | use_info.truncation().IsUsedAsWord32()) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 364 | // Either the output is uint32 or the uses only care about the |
| 365 | // low 32 bits (so we can pick uint32 safely). |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 366 | op = machine()->ChangeUint32ToFloat64(); |
| 367 | } |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 368 | } else if (output_rep == MachineRepresentation::kBit) { |
| 369 | op = machine()->ChangeUint32ToFloat64(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 370 | } else if (output_rep == MachineRepresentation::kTagged) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 371 | if (output_type->Is(Type::Undefined())) { |
| 372 | return jsgraph()->Float64Constant( |
| 373 | std::numeric_limits<double>::quiet_NaN()); |
| 374 | } else if (output_type->Is(Type::TaggedSigned())) { |
| 375 | node = InsertChangeTaggedSignedToInt32(node); |
| 376 | op = machine()->ChangeInt32ToFloat64(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 377 | } else if (output_type->Is(Type::Number())) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 378 | op = simplified()->ChangeTaggedToFloat64(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 379 | } else if (output_type->Is(Type::NumberOrOddball())) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 380 | // TODO(jarin) Here we should check that truncation is Number. |
| 381 | op = simplified()->TruncateTaggedToFloat64(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 382 | } else if (use_info.type_check() == TypeCheckKind::kNumber || |
| 383 | (use_info.type_check() == TypeCheckKind::kNumberOrOddball && |
| 384 | !output_type->Maybe(Type::BooleanOrNullOrNumber()))) { |
| 385 | op = simplified()->CheckedTaggedToFloat64(CheckTaggedInputMode::kNumber); |
| 386 | } else if (use_info.type_check() == TypeCheckKind::kNumberOrOddball) { |
| 387 | op = simplified()->CheckedTaggedToFloat64( |
| 388 | CheckTaggedInputMode::kNumberOrOddball); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 389 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 390 | } else if (output_rep == MachineRepresentation::kFloat32) { |
| 391 | op = machine()->ChangeFloat32ToFloat64(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 392 | } |
| 393 | if (op == nullptr) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 394 | return TypeError(node, output_rep, output_type, |
| 395 | MachineRepresentation::kFloat64); |
| 396 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 397 | return InsertConversion(node, op, use_node); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 400 | Node* RepresentationChanger::MakeTruncatedInt32Constant(double value) { |
| 401 | return jsgraph()->Int32Constant(DoubleToInt32(value)); |
| 402 | } |
| 403 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 404 | Node* RepresentationChanger::GetWord32RepresentationFor( |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 405 | Node* node, MachineRepresentation output_rep, Type* output_type, |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 406 | Node* use_node, UseInfo use_info) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 407 | // Eagerly fold representation changes for constants. |
| 408 | switch (node->opcode()) { |
| 409 | case IrOpcode::kInt32Constant: |
| 410 | return node; // No change necessary. |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 411 | case IrOpcode::kFloat32Constant: { |
| 412 | float const fv = OpParameter<float>(node); |
| 413 | if (use_info.type_check() == TypeCheckKind::kNone || |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 414 | ((use_info.type_check() == TypeCheckKind::kSignedSmall || |
| 415 | use_info.type_check() == TypeCheckKind::kSigned32) && |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 416 | IsInt32Double(fv))) { |
| 417 | return MakeTruncatedInt32Constant(fv); |
| 418 | } |
| 419 | break; |
| 420 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 421 | case IrOpcode::kNumberConstant: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 422 | case IrOpcode::kFloat64Constant: { |
| 423 | double const fv = OpParameter<double>(node); |
| 424 | if (use_info.type_check() == TypeCheckKind::kNone || |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 425 | ((use_info.type_check() == TypeCheckKind::kSignedSmall || |
| 426 | use_info.type_check() == TypeCheckKind::kSigned32) && |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 427 | IsInt32Double(fv))) { |
| 428 | return MakeTruncatedInt32Constant(fv); |
| 429 | } |
| 430 | break; |
| 431 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 432 | default: |
| 433 | break; |
| 434 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 435 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 436 | // Select the correct X -> Word32 operator. |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 437 | const Operator* op = nullptr; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 438 | if (output_rep == MachineRepresentation::kNone) { |
| 439 | // We should only use kNone representation if the type is empty. |
| 440 | CHECK(!output_type->IsInhabited()); |
| 441 | op = machine()->ImpossibleToWord32(); |
| 442 | } else if (output_rep == MachineRepresentation::kBit) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 443 | return node; // Sloppy comparison -> word32 |
| 444 | } else if (output_rep == MachineRepresentation::kFloat64) { |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 445 | if (output_type->Is(Type::Unsigned32())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 446 | op = machine()->ChangeFloat64ToUint32(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 447 | } else if (output_type->Is(Type::Signed32())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 448 | op = machine()->ChangeFloat64ToInt32(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 449 | } else if (use_info.truncation().IsUsedAsWord32()) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 450 | op = machine()->TruncateFloat64ToWord32(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 451 | } else if (use_info.type_check() == TypeCheckKind::kSignedSmall || |
| 452 | use_info.type_check() == TypeCheckKind::kSigned32) { |
| 453 | op = simplified()->CheckedFloat64ToInt32( |
| 454 | output_type->Maybe(Type::MinusZero()) |
| 455 | ? CheckForMinusZeroMode::kCheckForMinusZero |
| 456 | : CheckForMinusZeroMode::kDontCheckForMinusZero); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 457 | } |
| 458 | } else if (output_rep == MachineRepresentation::kFloat32) { |
| 459 | node = InsertChangeFloat32ToFloat64(node); // float32 -> float64 -> int32 |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 460 | if (output_type->Is(Type::Unsigned32())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | op = machine()->ChangeFloat64ToUint32(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 462 | } else if (output_type->Is(Type::Signed32())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 463 | op = machine()->ChangeFloat64ToInt32(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 464 | } else if (use_info.truncation().IsUsedAsWord32()) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 465 | op = machine()->TruncateFloat64ToWord32(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 466 | } else if (use_info.type_check() == TypeCheckKind::kSignedSmall || |
| 467 | use_info.type_check() == TypeCheckKind::kSigned32) { |
| 468 | op = simplified()->CheckedFloat64ToInt32( |
| 469 | output_type->Maybe(Type::MinusZero()) |
| 470 | ? CheckForMinusZeroMode::kCheckForMinusZero |
| 471 | : CheckForMinusZeroMode::kDontCheckForMinusZero); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 472 | } |
| 473 | } else if (output_rep == MachineRepresentation::kTagged) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 474 | if (output_type->Is(Type::TaggedSigned())) { |
| 475 | op = simplified()->ChangeTaggedSignedToInt32(); |
| 476 | } else if (output_type->Is(Type::Unsigned32())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 477 | op = simplified()->ChangeTaggedToUint32(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 478 | } else if (output_type->Is(Type::Signed32())) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 479 | op = simplified()->ChangeTaggedToInt32(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 480 | } else if (use_info.truncation().IsUsedAsWord32()) { |
| 481 | if (use_info.type_check() != TypeCheckKind::kNone) { |
| 482 | op = simplified()->CheckedTruncateTaggedToWord32(); |
| 483 | } else { |
| 484 | op = simplified()->TruncateTaggedToWord32(); |
| 485 | } |
| 486 | } else if (use_info.type_check() == TypeCheckKind::kSignedSmall) { |
| 487 | op = simplified()->CheckedTaggedSignedToInt32(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 488 | } else if (use_info.type_check() == TypeCheckKind::kSigned32) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 489 | op = simplified()->CheckedTaggedToInt32( |
| 490 | output_type->Maybe(Type::MinusZero()) |
| 491 | ? CheckForMinusZeroMode::kCheckForMinusZero |
| 492 | : CheckForMinusZeroMode::kDontCheckForMinusZero); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 493 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 494 | } else if (output_rep == MachineRepresentation::kWord32) { |
| 495 | // Only the checked case should get here, the non-checked case is |
| 496 | // handled in GetRepresentationFor. |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 497 | if (use_info.type_check() == TypeCheckKind::kSignedSmall || |
| 498 | use_info.type_check() == TypeCheckKind::kSigned32) { |
| 499 | if (output_type->Is(Type::Signed32())) { |
| 500 | return node; |
| 501 | } else if (output_type->Is(Type::Unsigned32())) { |
| 502 | op = simplified()->CheckedUint32ToInt32(); |
| 503 | } |
| 504 | } else { |
| 505 | DCHECK_EQ(TypeCheckKind::kNumberOrOddball, use_info.type_check()); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 506 | return node; |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 507 | } |
| 508 | } else if (output_rep == MachineRepresentation::kWord8 || |
| 509 | output_rep == MachineRepresentation::kWord16) { |
| 510 | DCHECK(use_info.representation() == MachineRepresentation::kWord32); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 511 | DCHECK(use_info.type_check() == TypeCheckKind::kSignedSmall || |
| 512 | use_info.type_check() == TypeCheckKind::kSigned32); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 513 | return node; |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 514 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 515 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 516 | if (op == nullptr) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 517 | return TypeError(node, output_rep, output_type, |
| 518 | MachineRepresentation::kWord32); |
| 519 | } |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 520 | return InsertConversion(node, op, use_node); |
| 521 | } |
| 522 | |
| 523 | Node* RepresentationChanger::InsertConversion(Node* node, const Operator* op, |
| 524 | Node* use_node) { |
| 525 | if (op->ControlInputCount() > 0) { |
| 526 | // If the operator can deoptimize (which means it has control |
| 527 | // input), we need to connect it to the effect and control chains. |
| 528 | Node* effect = NodeProperties::GetEffectInput(use_node); |
| 529 | Node* control = NodeProperties::GetControlInput(use_node); |
| 530 | Node* conversion = jsgraph()->graph()->NewNode(op, node, effect, control); |
| 531 | NodeProperties::ReplaceEffectInput(use_node, conversion); |
| 532 | return conversion; |
| 533 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 534 | return jsgraph()->graph()->NewNode(op, node); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | Node* RepresentationChanger::GetBitRepresentationFor( |
| 539 | Node* node, MachineRepresentation output_rep, Type* output_type) { |
| 540 | // Eagerly fold representation changes for constants. |
| 541 | switch (node->opcode()) { |
| 542 | case IrOpcode::kHeapConstant: { |
| 543 | Handle<HeapObject> value = OpParameter<Handle<HeapObject>>(node); |
| 544 | DCHECK(value.is_identical_to(factory()->true_value()) || |
| 545 | value.is_identical_to(factory()->false_value())); |
| 546 | return jsgraph()->Int32Constant( |
| 547 | value.is_identical_to(factory()->true_value()) ? 1 : 0); |
| 548 | } |
| 549 | default: |
| 550 | break; |
| 551 | } |
| 552 | // Select the correct X -> Bit operator. |
| 553 | const Operator* op; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 554 | if (output_rep == MachineRepresentation::kNone) { |
| 555 | // We should only use kNone representation if the type is empty. |
| 556 | CHECK(!output_type->IsInhabited()); |
| 557 | op = machine()->ImpossibleToBit(); |
| 558 | } else if (output_rep == MachineRepresentation::kTagged) { |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 559 | op = simplified()->ChangeTaggedToBit(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 560 | } else { |
| 561 | return TypeError(node, output_rep, output_type, |
| 562 | MachineRepresentation::kBit); |
| 563 | } |
| 564 | return jsgraph()->graph()->NewNode(op, node); |
| 565 | } |
| 566 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 567 | Node* RepresentationChanger::GetWord64RepresentationFor( |
| 568 | Node* node, MachineRepresentation output_rep, Type* output_type) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 569 | if (output_rep == MachineRepresentation::kNone) { |
| 570 | // We should only use kNone representation if the type is empty. |
| 571 | CHECK(!output_type->IsInhabited()); |
| 572 | return jsgraph()->graph()->NewNode(machine()->ImpossibleToFloat64(), node); |
| 573 | } else if (output_rep == MachineRepresentation::kBit) { |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 574 | return node; // Sloppy comparison -> word64 |
| 575 | } |
| 576 | // Can't really convert Word64 to anything else. Purported to be internal. |
| 577 | return TypeError(node, output_rep, output_type, |
| 578 | MachineRepresentation::kWord64); |
| 579 | } |
| 580 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 581 | const Operator* RepresentationChanger::Int32OperatorFor( |
| 582 | IrOpcode::Value opcode) { |
| 583 | switch (opcode) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 584 | case IrOpcode::kSpeculativeNumberAdd: // Fall through. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 585 | case IrOpcode::kNumberAdd: |
| 586 | return machine()->Int32Add(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 587 | case IrOpcode::kSpeculativeNumberSubtract: // Fall through. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 588 | case IrOpcode::kNumberSubtract: |
| 589 | return machine()->Int32Sub(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 590 | case IrOpcode::kSpeculativeNumberMultiply: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 591 | case IrOpcode::kNumberMultiply: |
| 592 | return machine()->Int32Mul(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 593 | case IrOpcode::kSpeculativeNumberDivide: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 594 | case IrOpcode::kNumberDivide: |
| 595 | return machine()->Int32Div(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 596 | case IrOpcode::kSpeculativeNumberModulus: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 597 | case IrOpcode::kNumberModulus: |
| 598 | return machine()->Int32Mod(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 599 | case IrOpcode::kSpeculativeNumberBitwiseOr: // Fall through. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 600 | case IrOpcode::kNumberBitwiseOr: |
| 601 | return machine()->Word32Or(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 602 | case IrOpcode::kSpeculativeNumberBitwiseXor: // Fall through. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 603 | case IrOpcode::kNumberBitwiseXor: |
| 604 | return machine()->Word32Xor(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 605 | case IrOpcode::kSpeculativeNumberBitwiseAnd: // Fall through. |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 606 | case IrOpcode::kNumberBitwiseAnd: |
| 607 | return machine()->Word32And(); |
| 608 | case IrOpcode::kNumberEqual: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 609 | case IrOpcode::kSpeculativeNumberEqual: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 610 | return machine()->Word32Equal(); |
| 611 | case IrOpcode::kNumberLessThan: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 612 | case IrOpcode::kSpeculativeNumberLessThan: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 613 | return machine()->Int32LessThan(); |
| 614 | case IrOpcode::kNumberLessThanOrEqual: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 615 | case IrOpcode::kSpeculativeNumberLessThanOrEqual: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 616 | return machine()->Int32LessThanOrEqual(); |
| 617 | default: |
| 618 | UNREACHABLE(); |
| 619 | return nullptr; |
| 620 | } |
| 621 | } |
| 622 | |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 623 | const Operator* RepresentationChanger::Int32OverflowOperatorFor( |
| 624 | IrOpcode::Value opcode) { |
| 625 | switch (opcode) { |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 626 | case IrOpcode::kSpeculativeNumberAdd: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 627 | return simplified()->CheckedInt32Add(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 628 | case IrOpcode::kSpeculativeNumberSubtract: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 629 | return simplified()->CheckedInt32Sub(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 630 | case IrOpcode::kSpeculativeNumberDivide: |
| 631 | return simplified()->CheckedInt32Div(); |
| 632 | case IrOpcode::kSpeculativeNumberModulus: |
| 633 | return simplified()->CheckedInt32Mod(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 634 | default: |
| 635 | UNREACHABLE(); |
| 636 | return nullptr; |
| 637 | } |
| 638 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 639 | |
| 640 | const Operator* RepresentationChanger::Uint32OperatorFor( |
| 641 | IrOpcode::Value opcode) { |
| 642 | switch (opcode) { |
| 643 | case IrOpcode::kNumberAdd: |
| 644 | return machine()->Int32Add(); |
| 645 | case IrOpcode::kNumberSubtract: |
| 646 | return machine()->Int32Sub(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 647 | case IrOpcode::kSpeculativeNumberMultiply: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 648 | case IrOpcode::kNumberMultiply: |
| 649 | return machine()->Int32Mul(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 650 | case IrOpcode::kSpeculativeNumberDivide: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 651 | case IrOpcode::kNumberDivide: |
| 652 | return machine()->Uint32Div(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 653 | case IrOpcode::kSpeculativeNumberModulus: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 654 | case IrOpcode::kNumberModulus: |
| 655 | return machine()->Uint32Mod(); |
| 656 | case IrOpcode::kNumberEqual: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 657 | case IrOpcode::kSpeculativeNumberEqual: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 658 | return machine()->Word32Equal(); |
| 659 | case IrOpcode::kNumberLessThan: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 660 | case IrOpcode::kSpeculativeNumberLessThan: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 661 | return machine()->Uint32LessThan(); |
| 662 | case IrOpcode::kNumberLessThanOrEqual: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 663 | case IrOpcode::kSpeculativeNumberLessThanOrEqual: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 664 | return machine()->Uint32LessThanOrEqual(); |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 665 | case IrOpcode::kNumberClz32: |
| 666 | return machine()->Word32Clz(); |
| 667 | case IrOpcode::kNumberImul: |
| 668 | return machine()->Int32Mul(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 669 | default: |
| 670 | UNREACHABLE(); |
| 671 | return nullptr; |
| 672 | } |
| 673 | } |
| 674 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 675 | const Operator* RepresentationChanger::Uint32OverflowOperatorFor( |
| 676 | IrOpcode::Value opcode) { |
| 677 | switch (opcode) { |
| 678 | case IrOpcode::kSpeculativeNumberDivide: |
| 679 | return simplified()->CheckedUint32Div(); |
| 680 | case IrOpcode::kSpeculativeNumberModulus: |
| 681 | return simplified()->CheckedUint32Mod(); |
| 682 | default: |
| 683 | UNREACHABLE(); |
| 684 | return nullptr; |
| 685 | } |
| 686 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 687 | |
| 688 | const Operator* RepresentationChanger::Float64OperatorFor( |
| 689 | IrOpcode::Value opcode) { |
| 690 | switch (opcode) { |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 691 | case IrOpcode::kSpeculativeNumberAdd: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 692 | case IrOpcode::kNumberAdd: |
| 693 | return machine()->Float64Add(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 694 | case IrOpcode::kSpeculativeNumberSubtract: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 695 | case IrOpcode::kNumberSubtract: |
| 696 | return machine()->Float64Sub(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 697 | case IrOpcode::kSpeculativeNumberMultiply: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 698 | case IrOpcode::kNumberMultiply: |
| 699 | return machine()->Float64Mul(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 700 | case IrOpcode::kSpeculativeNumberDivide: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 701 | case IrOpcode::kNumberDivide: |
| 702 | return machine()->Float64Div(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 703 | case IrOpcode::kSpeculativeNumberModulus: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 704 | case IrOpcode::kNumberModulus: |
| 705 | return machine()->Float64Mod(); |
| 706 | case IrOpcode::kNumberEqual: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 707 | case IrOpcode::kSpeculativeNumberEqual: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 708 | return machine()->Float64Equal(); |
| 709 | case IrOpcode::kNumberLessThan: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 710 | case IrOpcode::kSpeculativeNumberLessThan: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 711 | return machine()->Float64LessThan(); |
| 712 | case IrOpcode::kNumberLessThanOrEqual: |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 713 | case IrOpcode::kSpeculativeNumberLessThanOrEqual: |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 714 | return machine()->Float64LessThanOrEqual(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 715 | case IrOpcode::kNumberAbs: |
| 716 | return machine()->Float64Abs(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 717 | case IrOpcode::kNumberAcos: |
| 718 | return machine()->Float64Acos(); |
| 719 | case IrOpcode::kNumberAcosh: |
| 720 | return machine()->Float64Acosh(); |
| 721 | case IrOpcode::kNumberAsin: |
| 722 | return machine()->Float64Asin(); |
| 723 | case IrOpcode::kNumberAsinh: |
| 724 | return machine()->Float64Asinh(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 725 | case IrOpcode::kNumberAtan: |
| 726 | return machine()->Float64Atan(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 727 | case IrOpcode::kNumberAtanh: |
| 728 | return machine()->Float64Atanh(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 729 | case IrOpcode::kNumberAtan2: |
| 730 | return machine()->Float64Atan2(); |
| 731 | case IrOpcode::kNumberCbrt: |
| 732 | return machine()->Float64Cbrt(); |
| 733 | case IrOpcode::kNumberCeil: |
| 734 | return machine()->Float64RoundUp().placeholder(); |
| 735 | case IrOpcode::kNumberCos: |
| 736 | return machine()->Float64Cos(); |
| 737 | case IrOpcode::kNumberCosh: |
| 738 | return machine()->Float64Cosh(); |
| 739 | case IrOpcode::kNumberExp: |
| 740 | return machine()->Float64Exp(); |
| 741 | case IrOpcode::kNumberExpm1: |
| 742 | return machine()->Float64Expm1(); |
| 743 | case IrOpcode::kNumberFloor: |
| 744 | return machine()->Float64RoundDown().placeholder(); |
| 745 | case IrOpcode::kNumberFround: |
| 746 | return machine()->TruncateFloat64ToFloat32(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 747 | case IrOpcode::kNumberLog: |
| 748 | return machine()->Float64Log(); |
| 749 | case IrOpcode::kNumberLog1p: |
| 750 | return machine()->Float64Log1p(); |
| 751 | case IrOpcode::kNumberLog2: |
| 752 | return machine()->Float64Log2(); |
| 753 | case IrOpcode::kNumberLog10: |
| 754 | return machine()->Float64Log10(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 755 | case IrOpcode::kNumberMax: |
| 756 | return machine()->Float64Max(); |
| 757 | case IrOpcode::kNumberMin: |
| 758 | return machine()->Float64Min(); |
| 759 | case IrOpcode::kNumberPow: |
| 760 | return machine()->Float64Pow(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 761 | case IrOpcode::kNumberSin: |
| 762 | return machine()->Float64Sin(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 763 | case IrOpcode::kNumberSinh: |
| 764 | return machine()->Float64Sinh(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 765 | case IrOpcode::kNumberSqrt: |
| 766 | return machine()->Float64Sqrt(); |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 767 | case IrOpcode::kNumberTan: |
| 768 | return machine()->Float64Tan(); |
| 769 | case IrOpcode::kNumberTanh: |
| 770 | return machine()->Float64Tanh(); |
| 771 | case IrOpcode::kNumberTrunc: |
| 772 | return machine()->Float64RoundTruncate().placeholder(); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 773 | case IrOpcode::kNumberSilenceNaN: |
| 774 | return machine()->Float64SilenceNaN(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 775 | default: |
| 776 | UNREACHABLE(); |
| 777 | return nullptr; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | |
| 782 | Node* RepresentationChanger::TypeError(Node* node, |
| 783 | MachineRepresentation output_rep, |
| 784 | Type* output_type, |
| 785 | MachineRepresentation use) { |
| 786 | type_error_ = true; |
| 787 | if (!testing_type_errors_) { |
| 788 | std::ostringstream out_str; |
| 789 | out_str << output_rep << " ("; |
| 790 | output_type->PrintTo(out_str, Type::SEMANTIC_DIM); |
| 791 | out_str << ")"; |
| 792 | |
| 793 | std::ostringstream use_str; |
| 794 | use_str << use; |
| 795 | |
| 796 | V8_Fatal(__FILE__, __LINE__, |
| 797 | "RepresentationChangerError: node #%d:%s of " |
| 798 | "%s cannot be changed to %s", |
| 799 | node->id(), node->op()->mnemonic(), out_str.str().c_str(), |
| 800 | use_str.str().c_str()); |
| 801 | } |
| 802 | return node; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | Node* RepresentationChanger::InsertChangeFloat32ToFloat64(Node* node) { |
| 807 | return jsgraph()->graph()->NewNode(machine()->ChangeFloat32ToFloat64(), node); |
| 808 | } |
| 809 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 810 | Node* RepresentationChanger::InsertChangeFloat64ToUint32(Node* node) { |
| 811 | return jsgraph()->graph()->NewNode(machine()->ChangeFloat64ToUint32(), node); |
| 812 | } |
| 813 | |
| 814 | Node* RepresentationChanger::InsertChangeFloat64ToInt32(Node* node) { |
| 815 | return jsgraph()->graph()->NewNode(machine()->ChangeFloat64ToInt32(), node); |
| 816 | } |
| 817 | |
| 818 | Node* RepresentationChanger::InsertChangeTaggedSignedToInt32(Node* node) { |
| 819 | return jsgraph()->graph()->NewNode(simplified()->ChangeTaggedSignedToInt32(), |
| 820 | node); |
| 821 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 822 | |
| 823 | Node* RepresentationChanger::InsertChangeTaggedToFloat64(Node* node) { |
| 824 | return jsgraph()->graph()->NewNode(simplified()->ChangeTaggedToFloat64(), |
| 825 | node); |
| 826 | } |
| 827 | |
| 828 | } // namespace compiler |
| 829 | } // namespace internal |
| 830 | } // namespace v8 |