Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_COMPILER_REPRESENTATION_CHANGE_H_ | 5 #ifndef V8_COMPILER_REPRESENTATION_CHANGE_H_ |
| 6 #define V8_COMPILER_REPRESENTATION_CHANGE_H_ | 6 #define V8_COMPILER_REPRESENTATION_CHANGE_H_ |
| 7 | 7 |
| 8 #include "src/base/bits.h" | 8 #include "src/base/bits.h" |
| 9 #include "src/compiler/js-graph.h" | 9 #include "src/compiler/js-graph.h" |
| 10 #include "src/compiler/machine-operator.h" | 10 #include "src/compiler/machine-operator.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 int32_t value = OpParameter<int32_t>(node); | 81 int32_t value = OpParameter<int32_t>(node); |
| 82 return jsgraph()->Constant(value); | 82 return jsgraph()->Constant(value); |
| 83 } else if (output_type & kRepBit) { | 83 } else if (output_type & kRepBit) { |
| 84 return OpParameter<int32_t>(node) == 0 ? jsgraph()->FalseConstant() | 84 return OpParameter<int32_t>(node) == 0 ? jsgraph()->FalseConstant() |
| 85 : jsgraph()->TrueConstant(); | 85 : jsgraph()->TrueConstant(); |
| 86 } else { | 86 } else { |
| 87 return TypeError(node, output_type, kRepTagged); | 87 return TypeError(node, output_type, kRepTagged); |
| 88 } | 88 } |
| 89 case IrOpcode::kFloat64Constant: | 89 case IrOpcode::kFloat64Constant: |
| 90 return jsgraph()->Constant(OpParameter<double>(node)); | 90 return jsgraph()->Constant(OpParameter<double>(node)); |
| 91 case IrOpcode::kFloat32Constant: | |
| 92 return jsgraph()->Constant(OpParameter<float>(node)); | |
| 91 default: | 93 default: |
| 92 break; | 94 break; |
| 93 } | 95 } |
| 94 // Select the correct X -> Tagged operator. | 96 // Select the correct X -> Tagged operator. |
| 95 const Operator* op; | 97 const Operator* op; |
| 96 if (output_type & kRepBit) { | 98 if (output_type & kRepBit) { |
| 97 op = simplified()->ChangeBitToBool(); | 99 op = simplified()->ChangeBitToBool(); |
| 98 } else if (output_type & rWord) { | 100 } else if (output_type & rWord) { |
| 99 if (output_type & kTypeUint32) { | 101 if (output_type & kTypeUint32) { |
| 100 op = simplified()->ChangeUint32ToTagged(); | 102 op = simplified()->ChangeUint32ToTagged(); |
| 101 } else if (output_type & kTypeInt32) { | 103 } else if (output_type & kTypeInt32) { |
| 102 op = simplified()->ChangeInt32ToTagged(); | 104 op = simplified()->ChangeInt32ToTagged(); |
| 103 } else { | 105 } else { |
| 104 return TypeError(node, output_type, kRepTagged); | 106 return TypeError(node, output_type, kRepTagged); |
| 105 } | 107 } |
| 106 } else if (output_type & kRepFloat32) { | 108 } else if (output_type & kRepFloat32) { // float32 -> float64 -> tagged |
| 107 node = jsgraph()->graph()->NewNode(machine()->ChangeFloat32ToFloat64(), | 109 node = InsertChangeFloat32ToFloat64(node); |
| 108 node); | |
| 109 op = simplified()->ChangeFloat64ToTagged(); | 110 op = simplified()->ChangeFloat64ToTagged(); |
| 110 } else if (output_type & kRepFloat64) { | 111 } else if (output_type & kRepFloat64) { |
| 111 op = simplified()->ChangeFloat64ToTagged(); | 112 op = simplified()->ChangeFloat64ToTagged(); |
| 112 } else { | 113 } else { |
| 113 return TypeError(node, output_type, kRepTagged); | 114 return TypeError(node, output_type, kRepTagged); |
| 114 } | 115 } |
| 115 return jsgraph()->graph()->NewNode(op, node); | 116 return jsgraph()->graph()->NewNode(op, node); |
| 116 } | 117 } |
| 117 | 118 |
| 118 Node* GetFloat32RepresentationFor(Node* node, MachineTypeUnion output_type) { | 119 Node* GetFloat32RepresentationFor(Node* node, MachineTypeUnion output_type) { |
| 119 // Eagerly fold representation changes for constants. | 120 // Eagerly fold representation changes for constants. |
| 120 switch (node->opcode()) { | 121 switch (node->opcode()) { |
| 121 // TODO(turbofan): NumberConstant, Int32Constant, and Float64Constant? | 122 case IrOpcode::kFloat64Constant: |
| 123 case IrOpcode::kNumberConstant: | |
| 124 return jsgraph()->Float32Constant(OpParameter<float>(node)); | |
|
Benedikt Meurer
2014/09/24 11:43:31
DoubleToFloat32(OpParameter<double>(node))
titzer
2014/09/24 11:52:40
Done.
| |
| 125 case IrOpcode::kInt32Constant: | |
| 126 if (output_type & kTypeUint32) { | |
| 127 uint32_t value = OpParameter<uint32_t>(node); | |
| 128 return jsgraph()->Float32Constant(static_cast<float>(value)); | |
|
Benedikt Meurer
2014/09/24 11:43:31
is that static_cast<float> necessary?
titzer
2014/09/24 11:52:40
Done.
| |
| 129 } else { | |
| 130 int32_t value = OpParameter<int32_t>(node); | |
| 131 return jsgraph()->Float32Constant(value); | |
| 132 } | |
| 122 case IrOpcode::kFloat32Constant: | 133 case IrOpcode::kFloat32Constant: |
| 123 return node; // No change necessary. | 134 return node; // No change necessary. |
| 124 default: | 135 default: |
| 125 break; | 136 break; |
| 126 } | 137 } |
| 127 // TODO(turbofan): Select the correct X -> Float32 operator. | 138 // Select the correct X -> Float32 operator. |
| 128 return TypeError(node, output_type, kRepFloat32); | 139 const Operator* op; |
| 140 if (output_type & kRepBit) { | |
| 141 return TypeError(node, output_type, kRepFloat32); | |
| 142 } else if (output_type & rWord) { | |
| 143 if (output_type & kTypeUint32) { | |
| 144 op = machine()->ChangeUint32ToFloat64(); | |
| 145 } else { | |
| 146 op = machine()->ChangeInt32ToFloat64(); | |
| 147 } | |
| 148 // int32 -> float64 -> float32 | |
| 149 node = jsgraph()->graph()->NewNode(op, node); | |
| 150 op = machine()->TruncateFloat64ToFloat32(); | |
| 151 } else if (output_type & kRepTagged) { | |
| 152 op = simplified() | |
| 153 ->ChangeTaggedToFloat64(); // tagged -> float64 -> float32 | |
| 154 node = jsgraph()->graph()->NewNode(op, node); | |
| 155 op = machine()->TruncateFloat64ToFloat32(); | |
| 156 } else if (output_type & kRepFloat64) { | |
| 157 op = machine()->ChangeFloat32ToFloat64(); | |
| 158 } else { | |
| 159 return TypeError(node, output_type, kRepFloat32); | |
| 160 } | |
| 161 return jsgraph()->graph()->NewNode(op, node); | |
| 129 } | 162 } |
| 130 | 163 |
| 131 Node* GetFloat64RepresentationFor(Node* node, MachineTypeUnion output_type) { | 164 Node* GetFloat64RepresentationFor(Node* node, MachineTypeUnion output_type) { |
| 132 // Eagerly fold representation changes for constants. | 165 // Eagerly fold representation changes for constants. |
| 133 switch (node->opcode()) { | 166 switch (node->opcode()) { |
| 134 case IrOpcode::kNumberConstant: | 167 case IrOpcode::kNumberConstant: |
| 135 return jsgraph()->Float64Constant(OpParameter<double>(node)); | 168 return jsgraph()->Float64Constant(OpParameter<double>(node)); |
| 136 case IrOpcode::kInt32Constant: | 169 case IrOpcode::kInt32Constant: |
| 137 if (output_type & kTypeUint32) { | 170 if (output_type & kTypeUint32) { |
| 138 uint32_t value = OpParameter<uint32_t>(node); | 171 uint32_t value = OpParameter<uint32_t>(node); |
| 139 return jsgraph()->Float64Constant(static_cast<double>(value)); | 172 return jsgraph()->Float64Constant(static_cast<double>(value)); |
| 140 } else { | 173 } else { |
| 141 int32_t value = OpParameter<int32_t>(node); | 174 int32_t value = OpParameter<int32_t>(node); |
| 142 return jsgraph()->Float64Constant(value); | 175 return jsgraph()->Float64Constant(value); |
| 143 } | 176 } |
| 144 case IrOpcode::kFloat64Constant: | 177 case IrOpcode::kFloat64Constant: |
| 145 return node; // No change necessary. | 178 return node; // No change necessary. |
| 179 case IrOpcode::kFloat32Constant: | |
| 180 return jsgraph()->Float64Constant(OpParameter<float>(node)); | |
| 146 default: | 181 default: |
| 147 break; | 182 break; |
| 148 } | 183 } |
| 149 // Select the correct X -> Float64 operator. | 184 // Select the correct X -> Float64 operator. |
| 150 const Operator* op; | 185 const Operator* op; |
| 151 if (output_type & kRepBit) { | 186 if (output_type & kRepBit) { |
| 152 return TypeError(node, output_type, kRepFloat64); | 187 return TypeError(node, output_type, kRepFloat64); |
| 153 } else if (output_type & rWord) { | 188 } else if (output_type & rWord) { |
| 154 if (output_type & kTypeUint32) { | 189 if (output_type & kTypeUint32) { |
| 155 op = machine()->ChangeUint32ToFloat64(); | 190 op = machine()->ChangeUint32ToFloat64(); |
| 156 } else { | 191 } else { |
| 157 op = machine()->ChangeInt32ToFloat64(); | 192 op = machine()->ChangeInt32ToFloat64(); |
| 158 } | 193 } |
| 159 } else if (output_type & kRepTagged) { | 194 } else if (output_type & kRepTagged) { |
| 160 op = simplified()->ChangeTaggedToFloat64(); | 195 op = simplified()->ChangeTaggedToFloat64(); |
| 161 } else if (output_type & kRepFloat32) { | 196 } else if (output_type & kRepFloat32) { |
| 162 op = machine()->ChangeFloat32ToFloat64(); | 197 op = machine()->ChangeFloat32ToFloat64(); |
| 163 } else { | 198 } else { |
| 164 return TypeError(node, output_type, kRepFloat64); | 199 return TypeError(node, output_type, kRepFloat64); |
| 165 } | 200 } |
| 166 return jsgraph()->graph()->NewNode(op, node); | 201 return jsgraph()->graph()->NewNode(op, node); |
| 167 } | 202 } |
| 168 | 203 |
| 204 Node* MakeInt32Constant(double value) { | |
| 205 if (value < 0) { | |
| 206 DCHECK(IsInt32Double(value)); | |
| 207 int32_t iv = static_cast<int32_t>(value); | |
| 208 return jsgraph()->Int32Constant(iv); | |
| 209 } else { | |
| 210 DCHECK(IsUint32Double(value)); | |
| 211 int32_t iv = static_cast<int32_t>(static_cast<uint32_t>(value)); | |
| 212 return jsgraph()->Int32Constant(iv); | |
| 213 } | |
| 214 } | |
| 215 | |
| 169 Node* GetWord32RepresentationFor(Node* node, MachineTypeUnion output_type, | 216 Node* GetWord32RepresentationFor(Node* node, MachineTypeUnion output_type, |
| 170 bool use_unsigned) { | 217 bool use_unsigned) { |
| 171 // Eagerly fold representation changes for constants. | 218 // Eagerly fold representation changes for constants. |
| 172 switch (node->opcode()) { | 219 switch (node->opcode()) { |
| 173 case IrOpcode::kInt32Constant: | 220 case IrOpcode::kInt32Constant: |
| 174 return node; // No change necessary. | 221 return node; // No change necessary. |
| 175 case IrOpcode::kNumberConstant: | 222 case IrOpcode::kNumberConstant: |
| 176 case IrOpcode::kFloat64Constant: { | 223 case IrOpcode::kFloat32Constant: |
| 177 double value = OpParameter<double>(node); | 224 return MakeInt32Constant(OpParameter<float>(node)); |
| 178 if (value < 0) { | 225 case IrOpcode::kFloat64Constant: |
| 179 DCHECK(IsInt32Double(value)); | 226 return MakeInt32Constant(OpParameter<double>(node)); |
| 180 int32_t iv = static_cast<int32_t>(value); | |
| 181 return jsgraph()->Int32Constant(iv); | |
| 182 } else { | |
| 183 DCHECK(IsUint32Double(value)); | |
| 184 int32_t iv = static_cast<int32_t>(static_cast<uint32_t>(value)); | |
| 185 return jsgraph()->Int32Constant(iv); | |
| 186 } | |
| 187 } | |
| 188 default: | 227 default: |
| 189 break; | 228 break; |
| 190 } | 229 } |
| 191 // Select the correct X -> Word32 operator. | 230 // Select the correct X -> Word32 operator. |
| 192 const Operator* op = NULL; | 231 const Operator* op = NULL; |
| 193 if (output_type & kRepFloat64) { | 232 if (output_type & kRepFloat64) { |
| 194 if (output_type & kTypeUint32 || use_unsigned) { | 233 if (output_type & kTypeUint32 || use_unsigned) { |
| 195 op = machine()->ChangeFloat64ToUint32(); | 234 op = machine()->ChangeFloat64ToUint32(); |
| 196 } else { | 235 } else { |
| 197 op = machine()->ChangeFloat64ToInt32(); | 236 op = machine()->ChangeFloat64ToInt32(); |
| 198 } | 237 } |
| 238 } else if (output_type & kRepFloat32) { | |
| 239 node = InsertChangeFloat32ToFloat64(node); // float32 -> float64 -> int32 | |
| 240 if (output_type & kTypeUint32 || use_unsigned) { | |
| 241 op = machine()->ChangeFloat64ToUint32(); | |
| 242 } else { | |
| 243 op = machine()->ChangeFloat64ToInt32(); | |
| 244 } | |
| 199 } else if (output_type & kRepTagged) { | 245 } else if (output_type & kRepTagged) { |
| 200 if (output_type & kTypeUint32 || use_unsigned) { | 246 if (output_type & kTypeUint32 || use_unsigned) { |
| 201 op = simplified()->ChangeTaggedToUint32(); | 247 op = simplified()->ChangeTaggedToUint32(); |
| 202 } else { | 248 } else { |
| 203 op = simplified()->ChangeTaggedToInt32(); | 249 op = simplified()->ChangeTaggedToInt32(); |
| 204 } | 250 } |
| 205 } else { | 251 } else { |
| 206 return TypeError(node, output_type, kRepWord32); | 252 return TypeError(node, output_type, kRepWord32); |
| 207 } | 253 } |
| 208 return jsgraph()->graph()->NewNode(op, node); | 254 return jsgraph()->graph()->NewNode(op, node); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 | 406 |
| 361 V8_Fatal(__FILE__, __LINE__, | 407 V8_Fatal(__FILE__, __LINE__, |
| 362 "RepresentationChangerError: node #%d:%s of " | 408 "RepresentationChangerError: node #%d:%s of " |
| 363 "%s cannot be changed to %s", | 409 "%s cannot be changed to %s", |
| 364 node->id(), node->op()->mnemonic(), out_str.c_str(), | 410 node->id(), node->op()->mnemonic(), out_str.c_str(), |
| 365 use_str.c_str()); | 411 use_str.c_str()); |
| 366 } | 412 } |
| 367 return node; | 413 return node; |
| 368 } | 414 } |
| 369 | 415 |
| 416 Node* InsertChangeFloat32ToFloat64(Node* node) { | |
| 417 return jsgraph()->graph()->NewNode(machine()->ChangeFloat32ToFloat64(), | |
| 418 node); | |
| 419 } | |
| 420 | |
| 370 JSGraph* jsgraph() { return jsgraph_; } | 421 JSGraph* jsgraph() { return jsgraph_; } |
| 371 Isolate* isolate() { return isolate_; } | 422 Isolate* isolate() { return isolate_; } |
| 372 SimplifiedOperatorBuilder* simplified() { return simplified_; } | 423 SimplifiedOperatorBuilder* simplified() { return simplified_; } |
| 373 MachineOperatorBuilder* machine() { return jsgraph()->machine(); } | 424 MachineOperatorBuilder* machine() { return jsgraph()->machine(); } |
| 374 }; | 425 }; |
| 375 | 426 |
| 376 } // namespace compiler | 427 } // namespace compiler |
| 377 } // namespace internal | 428 } // namespace internal |
| 378 } // namespace v8 | 429 } // namespace v8 |
| 379 | 430 |
| 380 #endif // V8_COMPILER_REPRESENTATION_CHANGE_H_ | 431 #endif // V8_COMPILER_REPRESENTATION_CHANGE_H_ |
| OLD | NEW |