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( |
| 125 DoubleToFloat32(OpParameter<double>(node)); |
| 126 case IrOpcode::kInt32Constant: |
| 127 if (output_type & kTypeUint32) { |
| 128 uint32_t value = OpParameter<uint32_t>(node); |
| 129 return jsgraph()->Float32Constant(value); |
| 130 } else { |
| 131 int32_t value = OpParameter<int32_t>(node); |
| 132 return jsgraph()->Float32Constant(value); |
| 133 } |
122 case IrOpcode::kFloat32Constant: | 134 case IrOpcode::kFloat32Constant: |
123 return node; // No change necessary. | 135 return node; // No change necessary. |
124 default: | 136 default: |
125 break; | 137 break; |
126 } | 138 } |
127 // TODO(turbofan): Select the correct X -> Float32 operator. | 139 // Select the correct X -> Float32 operator. |
128 return TypeError(node, output_type, kRepFloat32); | 140 const Operator* op; |
| 141 if (output_type & kRepBit) { |
| 142 return TypeError(node, output_type, kRepFloat32); |
| 143 } else if (output_type & rWord) { |
| 144 if (output_type & kTypeUint32) { |
| 145 op = machine()->ChangeUint32ToFloat64(); |
| 146 } else { |
| 147 op = machine()->ChangeInt32ToFloat64(); |
| 148 } |
| 149 // int32 -> float64 -> float32 |
| 150 node = jsgraph()->graph()->NewNode(op, node); |
| 151 op = machine()->TruncateFloat64ToFloat32(); |
| 152 } else if (output_type & kRepTagged) { |
| 153 op = simplified() |
| 154 ->ChangeTaggedToFloat64(); // tagged -> float64 -> float32 |
| 155 node = jsgraph()->graph()->NewNode(op, node); |
| 156 op = machine()->TruncateFloat64ToFloat32(); |
| 157 } else if (output_type & kRepFloat64) { |
| 158 op = machine()->ChangeFloat32ToFloat64(); |
| 159 } else { |
| 160 return TypeError(node, output_type, kRepFloat32); |
| 161 } |
| 162 return jsgraph()->graph()->NewNode(op, node); |
129 } | 163 } |
130 | 164 |
131 Node* GetFloat64RepresentationFor(Node* node, MachineTypeUnion output_type) { | 165 Node* GetFloat64RepresentationFor(Node* node, MachineTypeUnion output_type) { |
132 // Eagerly fold representation changes for constants. | 166 // Eagerly fold representation changes for constants. |
133 switch (node->opcode()) { | 167 switch (node->opcode()) { |
134 case IrOpcode::kNumberConstant: | 168 case IrOpcode::kNumberConstant: |
135 return jsgraph()->Float64Constant(OpParameter<double>(node)); | 169 return jsgraph()->Float64Constant(OpParameter<double>(node)); |
136 case IrOpcode::kInt32Constant: | 170 case IrOpcode::kInt32Constant: |
137 if (output_type & kTypeUint32) { | 171 if (output_type & kTypeUint32) { |
138 uint32_t value = OpParameter<uint32_t>(node); | 172 uint32_t value = OpParameter<uint32_t>(node); |
139 return jsgraph()->Float64Constant(static_cast<double>(value)); | 173 return jsgraph()->Float64Constant(static_cast<double>(value)); |
140 } else { | 174 } else { |
141 int32_t value = OpParameter<int32_t>(node); | 175 int32_t value = OpParameter<int32_t>(node); |
142 return jsgraph()->Float64Constant(value); | 176 return jsgraph()->Float64Constant(value); |
143 } | 177 } |
144 case IrOpcode::kFloat64Constant: | 178 case IrOpcode::kFloat64Constant: |
145 return node; // No change necessary. | 179 return node; // No change necessary. |
| 180 case IrOpcode::kFloat32Constant: |
| 181 return jsgraph()->Float64Constant(OpParameter<float>(node)); |
146 default: | 182 default: |
147 break; | 183 break; |
148 } | 184 } |
149 // Select the correct X -> Float64 operator. | 185 // Select the correct X -> Float64 operator. |
150 const Operator* op; | 186 const Operator* op; |
151 if (output_type & kRepBit) { | 187 if (output_type & kRepBit) { |
152 return TypeError(node, output_type, kRepFloat64); | 188 return TypeError(node, output_type, kRepFloat64); |
153 } else if (output_type & rWord) { | 189 } else if (output_type & rWord) { |
154 if (output_type & kTypeUint32) { | 190 if (output_type & kTypeUint32) { |
155 op = machine()->ChangeUint32ToFloat64(); | 191 op = machine()->ChangeUint32ToFloat64(); |
156 } else { | 192 } else { |
157 op = machine()->ChangeInt32ToFloat64(); | 193 op = machine()->ChangeInt32ToFloat64(); |
158 } | 194 } |
159 } else if (output_type & kRepTagged) { | 195 } else if (output_type & kRepTagged) { |
160 op = simplified()->ChangeTaggedToFloat64(); | 196 op = simplified()->ChangeTaggedToFloat64(); |
161 } else if (output_type & kRepFloat32) { | 197 } else if (output_type & kRepFloat32) { |
162 op = machine()->ChangeFloat32ToFloat64(); | 198 op = machine()->ChangeFloat32ToFloat64(); |
163 } else { | 199 } else { |
164 return TypeError(node, output_type, kRepFloat64); | 200 return TypeError(node, output_type, kRepFloat64); |
165 } | 201 } |
166 return jsgraph()->graph()->NewNode(op, node); | 202 return jsgraph()->graph()->NewNode(op, node); |
167 } | 203 } |
168 | 204 |
| 205 Node* MakeInt32Constant(double value) { |
| 206 if (value < 0) { |
| 207 DCHECK(IsInt32Double(value)); |
| 208 int32_t iv = static_cast<int32_t>(value); |
| 209 return jsgraph()->Int32Constant(iv); |
| 210 } else { |
| 211 DCHECK(IsUint32Double(value)); |
| 212 int32_t iv = static_cast<int32_t>(static_cast<uint32_t>(value)); |
| 213 return jsgraph()->Int32Constant(iv); |
| 214 } |
| 215 } |
| 216 |
169 Node* GetWord32RepresentationFor(Node* node, MachineTypeUnion output_type, | 217 Node* GetWord32RepresentationFor(Node* node, MachineTypeUnion output_type, |
170 bool use_unsigned) { | 218 bool use_unsigned) { |
171 // Eagerly fold representation changes for constants. | 219 // Eagerly fold representation changes for constants. |
172 switch (node->opcode()) { | 220 switch (node->opcode()) { |
173 case IrOpcode::kInt32Constant: | 221 case IrOpcode::kInt32Constant: |
174 return node; // No change necessary. | 222 return node; // No change necessary. |
175 case IrOpcode::kNumberConstant: | 223 case IrOpcode::kNumberConstant: |
176 case IrOpcode::kFloat64Constant: { | 224 case IrOpcode::kFloat32Constant: |
177 double value = OpParameter<double>(node); | 225 return MakeInt32Constant(OpParameter<float>(node)); |
178 if (value < 0) { | 226 case IrOpcode::kFloat64Constant: |
179 DCHECK(IsInt32Double(value)); | 227 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: | 228 default: |
189 break; | 229 break; |
190 } | 230 } |
191 // Select the correct X -> Word32 operator. | 231 // Select the correct X -> Word32 operator. |
192 const Operator* op = NULL; | 232 const Operator* op = NULL; |
193 if (output_type & kRepFloat64) { | 233 if (output_type & kRepFloat64) { |
194 if (output_type & kTypeUint32 || use_unsigned) { | 234 if (output_type & kTypeUint32 || use_unsigned) { |
195 op = machine()->ChangeFloat64ToUint32(); | 235 op = machine()->ChangeFloat64ToUint32(); |
196 } else { | 236 } else { |
197 op = machine()->ChangeFloat64ToInt32(); | 237 op = machine()->ChangeFloat64ToInt32(); |
198 } | 238 } |
| 239 } else if (output_type & kRepFloat32) { |
| 240 node = InsertChangeFloat32ToFloat64(node); // float32 -> float64 -> int32 |
| 241 if (output_type & kTypeUint32 || use_unsigned) { |
| 242 op = machine()->ChangeFloat64ToUint32(); |
| 243 } else { |
| 244 op = machine()->ChangeFloat64ToInt32(); |
| 245 } |
199 } else if (output_type & kRepTagged) { | 246 } else if (output_type & kRepTagged) { |
200 if (output_type & kTypeUint32 || use_unsigned) { | 247 if (output_type & kTypeUint32 || use_unsigned) { |
201 op = simplified()->ChangeTaggedToUint32(); | 248 op = simplified()->ChangeTaggedToUint32(); |
202 } else { | 249 } else { |
203 op = simplified()->ChangeTaggedToInt32(); | 250 op = simplified()->ChangeTaggedToInt32(); |
204 } | 251 } |
205 } else { | 252 } else { |
206 return TypeError(node, output_type, kRepWord32); | 253 return TypeError(node, output_type, kRepWord32); |
207 } | 254 } |
208 return jsgraph()->graph()->NewNode(op, node); | 255 return jsgraph()->graph()->NewNode(op, node); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 407 |
361 V8_Fatal(__FILE__, __LINE__, | 408 V8_Fatal(__FILE__, __LINE__, |
362 "RepresentationChangerError: node #%d:%s of " | 409 "RepresentationChangerError: node #%d:%s of " |
363 "%s cannot be changed to %s", | 410 "%s cannot be changed to %s", |
364 node->id(), node->op()->mnemonic(), out_str.c_str(), | 411 node->id(), node->op()->mnemonic(), out_str.c_str(), |
365 use_str.c_str()); | 412 use_str.c_str()); |
366 } | 413 } |
367 return node; | 414 return node; |
368 } | 415 } |
369 | 416 |
| 417 Node* InsertChangeFloat32ToFloat64(Node* node) { |
| 418 return jsgraph()->graph()->NewNode(machine()->ChangeFloat32ToFloat64(), |
| 419 node); |
| 420 } |
| 421 |
370 JSGraph* jsgraph() { return jsgraph_; } | 422 JSGraph* jsgraph() { return jsgraph_; } |
371 Isolate* isolate() { return isolate_; } | 423 Isolate* isolate() { return isolate_; } |
372 SimplifiedOperatorBuilder* simplified() { return simplified_; } | 424 SimplifiedOperatorBuilder* simplified() { return simplified_; } |
373 MachineOperatorBuilder* machine() { return jsgraph()->machine(); } | 425 MachineOperatorBuilder* machine() { return jsgraph()->machine(); } |
374 }; | 426 }; |
375 | 427 |
376 } // namespace compiler | 428 } // namespace compiler |
377 } // namespace internal | 429 } // namespace internal |
378 } // namespace v8 | 430 } // namespace v8 |
379 | 431 |
380 #endif // V8_COMPILER_REPRESENTATION_CHANGE_H_ | 432 #endif // V8_COMPILER_REPRESENTATION_CHANGE_H_ |
OLD | NEW |