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 #include "src/base/adapters.h" | 5 #include "src/base/adapters.h" |
6 #include "src/compiler/instruction-selector-impl.h" | 6 #include "src/compiler/instruction-selector-impl.h" |
7 #include "src/compiler/node-matchers.h" | 7 #include "src/compiler/node-matchers.h" |
8 #include "src/compiler/node-properties.h" | 8 #include "src/compiler/node-properties.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 namespace compiler { | 12 namespace compiler { |
13 | 13 |
14 // Adds IA32-specific methods for generating operands. | 14 // Adds IA32-specific methods for generating operands. |
15 class IA32OperandGenerator final : public OperandGenerator { | 15 class IA32OperandGenerator final : public OperandGenerator { |
16 public: | 16 public: |
17 explicit IA32OperandGenerator(InstructionSelector* selector) | 17 explicit IA32OperandGenerator(InstructionSelector* selector) |
18 : OperandGenerator(selector) {} | 18 : OperandGenerator(selector) {} |
19 | 19 |
20 InstructionOperand UseByteRegister(Node* node) { | 20 InstructionOperand UseByteRegister(Node* node) { |
21 // TODO(titzer): encode byte register use constraints. | 21 // TODO(titzer): encode byte register use constraints. |
22 return UseFixed(node, edx); | 22 return UseFixed(node, edx); |
23 } | 23 } |
24 | 24 |
25 InstructionOperand DefineAsByteRegister(Node* node) { | 25 InstructionOperand DefineAsByteRegister(Node* node) { |
26 // TODO(titzer): encode byte register def constraints. | 26 // TODO(titzer): encode byte register def constraints. |
27 return DefineAsRegister(node); | 27 return DefineAsRegister(node); |
28 } | 28 } |
29 | 29 |
| 30 bool CanBeMemoryOperand(InstructionCode opcode, Node* node, Node* input) { |
| 31 if (input->opcode() != IrOpcode::kLoad || |
| 32 !selector()->CanCover(node, input)) { |
| 33 return false; |
| 34 } |
| 35 MachineRepresentation rep = |
| 36 LoadRepresentationOf(input->op()).representation(); |
| 37 switch (opcode) { |
| 38 case kIA32Cmp: |
| 39 case kIA32Test: |
| 40 return rep == MachineRepresentation::kWord32 || |
| 41 rep == MachineRepresentation::kTagged; |
| 42 case kIA32Cmp16: |
| 43 case kIA32Test16: |
| 44 return rep == MachineRepresentation::kWord16; |
| 45 case kIA32Cmp8: |
| 46 case kIA32Test8: |
| 47 return rep == MachineRepresentation::kWord8; |
| 48 default: |
| 49 break; |
| 50 } |
| 51 return false; |
| 52 } |
| 53 |
30 bool CanBeImmediate(Node* node) { | 54 bool CanBeImmediate(Node* node) { |
31 switch (node->opcode()) { | 55 switch (node->opcode()) { |
32 case IrOpcode::kInt32Constant: | 56 case IrOpcode::kInt32Constant: |
33 case IrOpcode::kNumberConstant: | 57 case IrOpcode::kNumberConstant: |
34 case IrOpcode::kExternalConstant: | 58 case IrOpcode::kExternalConstant: |
35 return true; | 59 return true; |
36 case IrOpcode::kHeapConstant: { | 60 case IrOpcode::kHeapConstant: { |
37 // Constants in new space cannot be used as immediates in V8 because | 61 // Constants in new space cannot be used as immediates in V8 because |
38 // the GC does not scan code objects when collecting the new generation. | 62 // the GC does not scan code objects when collecting the new generation. |
39 Handle<HeapObject> value = OpParameter<Handle<HeapObject>>(node); | 63 Handle<HeapObject> value = OpParameter<Handle<HeapObject>>(node); |
(...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1104 } else if (cont->IsDeoptimize()) { | 1128 } else if (cont->IsDeoptimize()) { |
1105 selector->EmitDeoptimize(opcode, 0, nullptr, input_count, inputs, | 1129 selector->EmitDeoptimize(opcode, 0, nullptr, input_count, inputs, |
1106 cont->frame_state()); | 1130 cont->frame_state()); |
1107 } else { | 1131 } else { |
1108 DCHECK(cont->IsSet()); | 1132 DCHECK(cont->IsSet()); |
1109 InstructionOperand output = g.DefineAsRegister(cont->result()); | 1133 InstructionOperand output = g.DefineAsRegister(cont->result()); |
1110 selector->Emit(opcode, 1, &output, input_count, inputs); | 1134 selector->Emit(opcode, 1, &output, input_count, inputs); |
1111 } | 1135 } |
1112 } | 1136 } |
1113 | 1137 |
1114 // Determines if {input} of {node} can be replaced by a memory operand. | |
1115 bool CanUseMemoryOperand(InstructionSelector* selector, InstructionCode opcode, | |
1116 Node* node, Node* input) { | |
1117 if (input->opcode() != IrOpcode::kLoad || !selector->CanCover(node, input)) { | |
1118 return false; | |
1119 } | |
1120 MachineRepresentation load_representation = | |
1121 LoadRepresentationOf(input->op()).representation(); | |
1122 if (load_representation == MachineRepresentation::kWord32 || | |
1123 load_representation == MachineRepresentation::kTagged) { | |
1124 return opcode == kIA32Cmp || opcode == kIA32Test; | |
1125 } | |
1126 return false; | |
1127 } | |
1128 | |
1129 // Shared routine for multiple compare operations. | 1138 // Shared routine for multiple compare operations. |
1130 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, | 1139 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, |
1131 InstructionOperand left, InstructionOperand right, | 1140 InstructionOperand left, InstructionOperand right, |
1132 FlagsContinuation* cont) { | 1141 FlagsContinuation* cont) { |
1133 IA32OperandGenerator g(selector); | 1142 IA32OperandGenerator g(selector); |
1134 opcode = cont->Encode(opcode); | 1143 opcode = cont->Encode(opcode); |
1135 if (cont->IsBranch()) { | 1144 if (cont->IsBranch()) { |
1136 selector->Emit(opcode, g.NoOutput(), left, right, | 1145 selector->Emit(opcode, g.NoOutput(), left, right, |
1137 g.Label(cont->true_block()), g.Label(cont->false_block())); | 1146 g.Label(cont->true_block()), g.Label(cont->false_block())); |
1138 } else if (cont->IsDeoptimize()) { | 1147 } else if (cont->IsDeoptimize()) { |
(...skipping 10 matching lines...) Expand all Loading... |
1149 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, | 1158 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, |
1150 Node* left, Node* right, FlagsContinuation* cont, | 1159 Node* left, Node* right, FlagsContinuation* cont, |
1151 bool commutative) { | 1160 bool commutative) { |
1152 IA32OperandGenerator g(selector); | 1161 IA32OperandGenerator g(selector); |
1153 if (commutative && g.CanBeBetterLeftOperand(right)) { | 1162 if (commutative && g.CanBeBetterLeftOperand(right)) { |
1154 std::swap(left, right); | 1163 std::swap(left, right); |
1155 } | 1164 } |
1156 VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); | 1165 VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); |
1157 } | 1166 } |
1158 | 1167 |
| 1168 // Tries to match the size of the given opcode to that of the operands, if |
| 1169 // possible. |
| 1170 InstructionCode TryNarrowOpcodeSize(InstructionCode opcode, Node* left, |
| 1171 Node* right) { |
| 1172 if (opcode != kIA32Cmp && opcode != kIA32Test) { |
| 1173 return opcode; |
| 1174 } |
| 1175 // Currently, if one of the two operands is not a Load, we don't know what its |
| 1176 // machine representation is, so we bail out. |
| 1177 // TODO(epertoso): we can probably get some size information out of immediates |
| 1178 // and phi nodes. |
| 1179 if (left->opcode() != IrOpcode::kLoad || right->opcode() != IrOpcode::kLoad) { |
| 1180 return opcode; |
| 1181 } |
| 1182 // If the load representations don't match, both operands will be |
| 1183 // zero/sign-extended to 32bit. |
| 1184 LoadRepresentation left_representation = LoadRepresentationOf(left->op()); |
| 1185 if (left_representation != LoadRepresentationOf(right->op())) { |
| 1186 return opcode; |
| 1187 } |
| 1188 switch (left_representation.representation()) { |
| 1189 case MachineRepresentation::kBit: |
| 1190 case MachineRepresentation::kWord8: |
| 1191 return opcode == kIA32Cmp ? kIA32Cmp8 : kIA32Test8; |
| 1192 case MachineRepresentation::kWord16: |
| 1193 return opcode == kIA32Cmp ? kIA32Cmp16 : kIA32Test16; |
| 1194 default: |
| 1195 return opcode; |
| 1196 } |
| 1197 } |
1159 | 1198 |
1160 // Shared routine for multiple float32 compare operations (inputs commuted). | 1199 // Shared routine for multiple float32 compare operations (inputs commuted). |
1161 void VisitFloat32Compare(InstructionSelector* selector, Node* node, | 1200 void VisitFloat32Compare(InstructionSelector* selector, Node* node, |
1162 FlagsContinuation* cont) { | 1201 FlagsContinuation* cont) { |
1163 Node* const left = node->InputAt(0); | 1202 Node* const left = node->InputAt(0); |
1164 Node* const right = node->InputAt(1); | 1203 Node* const right = node->InputAt(1); |
1165 VisitCompare(selector, kSSEFloat32Cmp, right, left, cont, false); | 1204 VisitCompare(selector, kSSEFloat32Cmp, right, left, cont, false); |
1166 } | 1205 } |
1167 | 1206 |
1168 | 1207 |
1169 // Shared routine for multiple float64 compare operations (inputs commuted). | 1208 // Shared routine for multiple float64 compare operations (inputs commuted). |
1170 void VisitFloat64Compare(InstructionSelector* selector, Node* node, | 1209 void VisitFloat64Compare(InstructionSelector* selector, Node* node, |
1171 FlagsContinuation* cont) { | 1210 FlagsContinuation* cont) { |
1172 Node* const left = node->InputAt(0); | 1211 Node* const left = node->InputAt(0); |
1173 Node* const right = node->InputAt(1); | 1212 Node* const right = node->InputAt(1); |
1174 VisitCompare(selector, kSSEFloat64Cmp, right, left, cont, false); | 1213 VisitCompare(selector, kSSEFloat64Cmp, right, left, cont, false); |
1175 } | 1214 } |
1176 | 1215 |
1177 // Shared routine for multiple word compare operations. | 1216 // Shared routine for multiple word compare operations. |
1178 void VisitWordCompare(InstructionSelector* selector, Node* node, | 1217 void VisitWordCompare(InstructionSelector* selector, Node* node, |
1179 InstructionCode opcode, FlagsContinuation* cont) { | 1218 InstructionCode opcode, FlagsContinuation* cont) { |
1180 IA32OperandGenerator g(selector); | 1219 IA32OperandGenerator g(selector); |
1181 Node* left = node->InputAt(0); | 1220 Node* left = node->InputAt(0); |
1182 Node* right = node->InputAt(1); | 1221 Node* right = node->InputAt(1); |
1183 | 1222 |
1184 // If one of the two inputs is an immediate, make sure it's on the right. | 1223 InstructionCode narrowed_opcode = TryNarrowOpcodeSize(opcode, left, right); |
1185 if (!g.CanBeImmediate(right) && g.CanBeImmediate(left)) { | 1224 |
| 1225 // If one of the two inputs is an immediate, make sure it's on the right, or |
| 1226 // if one of the two inputs is a memory operand, make sure it's on the left. |
| 1227 if ((!g.CanBeImmediate(right) && g.CanBeImmediate(left)) || |
| 1228 (g.CanBeMemoryOperand(narrowed_opcode, node, right) && |
| 1229 !g.CanBeMemoryOperand(narrowed_opcode, node, left))) { |
1186 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 1230 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); |
1187 std::swap(left, right); | 1231 std::swap(left, right); |
1188 } | 1232 } |
1189 | 1233 |
1190 // Match immediates on right side of comparison. | 1234 // Match immediates on right side of comparison. |
1191 if (g.CanBeImmediate(right)) { | 1235 if (g.CanBeImmediate(right)) { |
1192 if (CanUseMemoryOperand(selector, opcode, node, left)) { | 1236 if (g.CanBeMemoryOperand(opcode, node, left)) { |
| 1237 // TODO(epertoso): we should use `narrowed_opcode' here once we match |
| 1238 // immediates too. |
1193 return VisitCompareWithMemoryOperand(selector, opcode, left, | 1239 return VisitCompareWithMemoryOperand(selector, opcode, left, |
1194 g.UseImmediate(right), cont); | 1240 g.UseImmediate(right), cont); |
1195 } | 1241 } |
1196 return VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), | 1242 return VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), |
1197 cont); | 1243 cont); |
1198 } | 1244 } |
1199 | 1245 |
| 1246 // Match memory operands on left side of comparison. |
| 1247 if (g.CanBeMemoryOperand(narrowed_opcode, node, left)) { |
| 1248 bool needs_byte_register = |
| 1249 narrowed_opcode == kIA32Test8 || narrowed_opcode == kIA32Cmp8; |
| 1250 return VisitCompareWithMemoryOperand( |
| 1251 selector, narrowed_opcode, left, |
| 1252 needs_byte_register ? g.UseByteRegister(right) : g.UseRegister(right), |
| 1253 cont); |
| 1254 } |
| 1255 |
1200 if (g.CanBeBetterLeftOperand(right)) { | 1256 if (g.CanBeBetterLeftOperand(right)) { |
1201 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 1257 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); |
1202 std::swap(left, right); | 1258 std::swap(left, right); |
1203 } | 1259 } |
1204 | 1260 |
1205 if (CanUseMemoryOperand(selector, opcode, node, left)) { | |
1206 return VisitCompareWithMemoryOperand(selector, opcode, left, | |
1207 g.UseRegister(right), cont); | |
1208 } | |
1209 return VisitCompare(selector, opcode, left, right, cont, | 1261 return VisitCompare(selector, opcode, left, right, cont, |
1210 node->op()->HasProperty(Operator::kCommutative)); | 1262 node->op()->HasProperty(Operator::kCommutative)); |
1211 } | 1263 } |
1212 | 1264 |
1213 void VisitWordCompare(InstructionSelector* selector, Node* node, | 1265 void VisitWordCompare(InstructionSelector* selector, Node* node, |
1214 FlagsContinuation* cont) { | 1266 FlagsContinuation* cont) { |
1215 IA32OperandGenerator g(selector); | 1267 IA32OperandGenerator g(selector); |
1216 Int32BinopMatcher m(node); | 1268 Int32BinopMatcher m(node); |
1217 if (m.left().IsLoad() && m.right().IsLoadStackPointer()) { | 1269 if (m.left().IsLoad() && m.right().IsLoadStackPointer()) { |
1218 LoadMatcher<ExternalReferenceMatcher> mleft(m.left().node()); | 1270 LoadMatcher<ExternalReferenceMatcher> mleft(m.left().node()); |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 MachineOperatorBuilder::kFloat64RoundTruncate | | 1583 MachineOperatorBuilder::kFloat64RoundTruncate | |
1532 MachineOperatorBuilder::kFloat32RoundTiesEven | | 1584 MachineOperatorBuilder::kFloat32RoundTiesEven | |
1533 MachineOperatorBuilder::kFloat64RoundTiesEven; | 1585 MachineOperatorBuilder::kFloat64RoundTiesEven; |
1534 } | 1586 } |
1535 return flags; | 1587 return flags; |
1536 } | 1588 } |
1537 | 1589 |
1538 } // namespace compiler | 1590 } // namespace compiler |
1539 } // namespace internal | 1591 } // namespace internal |
1540 } // namespace v8 | 1592 } // namespace v8 |
OLD | NEW |