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 <algorithm> |
| 6 |
5 #include "src/compiler/instruction-selector-impl.h" | 7 #include "src/compiler/instruction-selector-impl.h" |
6 #include "src/compiler/node-matchers.h" | 8 #include "src/compiler/node-matchers.h" |
7 #include "src/compiler/node-properties.h" | 9 #include "src/compiler/node-properties.h" |
8 | 10 |
9 namespace v8 { | 11 namespace v8 { |
10 namespace internal { | 12 namespace internal { |
11 namespace compiler { | 13 namespace compiler { |
12 | 14 |
13 // Adds X64-specific methods for generating operands. | 15 // Adds X64-specific methods for generating operands. |
14 class X64OperandGenerator FINAL : public OperandGenerator { | 16 class X64OperandGenerator FINAL : public OperandGenerator { |
(...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1144 default: | 1146 default: |
1145 break; | 1147 break; |
1146 } | 1148 } |
1147 } | 1149 } |
1148 | 1150 |
1149 // Branch could not be combined with a compare, emit compare against 0. | 1151 // Branch could not be combined with a compare, emit compare against 0. |
1150 VisitCompareZero(this, value, kX64Cmp32, &cont); | 1152 VisitCompareZero(this, value, kX64Cmp32, &cont); |
1151 } | 1153 } |
1152 | 1154 |
1153 | 1155 |
| 1156 void InstructionSelector::VisitSwitch(Node* node, BasicBlock* default_branch, |
| 1157 BasicBlock** case_branches, |
| 1158 int32_t* case_values, size_t case_count, |
| 1159 int32_t min_value, int32_t max_value) { |
| 1160 X64OperandGenerator g(this); |
| 1161 InstructionOperand value_operand = g.UseRegister(node->InputAt(0)); |
| 1162 InstructionOperand default_operand = g.Label(default_branch); |
| 1163 |
| 1164 // Note that {value_range} can be 0 if {min_value} is -2^31 and {max_value} |
| 1165 // is 2^31-1, so don't assume that it's non-zero below. |
| 1166 size_t value_range = |
| 1167 1u + bit_cast<uint32_t>(max_value) - bit_cast<uint32_t>(min_value); |
| 1168 |
| 1169 // Determine whether to issue an ArchTableSwitch or an ArchLookupSwitch |
| 1170 // instruction. |
| 1171 size_t table_space_cost = 4 + value_range; |
| 1172 size_t table_time_cost = 3; |
| 1173 size_t lookup_space_cost = 3 + 2 * case_count; |
| 1174 size_t lookup_time_cost = case_count; |
| 1175 if (case_count > 4 && |
| 1176 table_space_cost + 3 * table_time_cost <= |
| 1177 lookup_space_cost + 3 * lookup_time_cost && |
| 1178 min_value > std::numeric_limits<int32_t>::min()) { |
| 1179 InstructionOperand index_operand = g.TempRegister(); |
| 1180 if (min_value) { |
| 1181 // The leal automatically zero extends, so result is a valid 64-bit index. |
| 1182 Emit(kX64Lea32 | AddressingModeField::encode(kMode_MRI), index_operand, |
| 1183 value_operand, g.TempImmediate(-min_value)); |
| 1184 } else { |
| 1185 // Zero extend, because we use it as 64-bit index into the jump table. |
| 1186 Emit(kX64Movl, index_operand, value_operand); |
| 1187 } |
| 1188 size_t input_count = 2 + value_range; |
| 1189 auto* inputs = zone()->NewArray<InstructionOperand>(input_count); |
| 1190 inputs[0] = index_operand; |
| 1191 std::fill(&inputs[1], &inputs[input_count], default_operand); |
| 1192 for (size_t index = 0; index < case_count; ++index) { |
| 1193 size_t value = case_values[index] - min_value; |
| 1194 BasicBlock* branch = case_branches[index]; |
| 1195 DCHECK_LE(0u, value); |
| 1196 DCHECK_LT(value + 2, input_count); |
| 1197 inputs[value + 2] = g.Label(branch); |
| 1198 } |
| 1199 Emit(kArchTableSwitch, 0, nullptr, input_count, inputs, 0, nullptr) |
| 1200 ->MarkAsControl(); |
| 1201 return; |
| 1202 } |
| 1203 |
| 1204 // Generate a sequence of conditional jumps. |
| 1205 size_t input_count = 2 + case_count * 2; |
| 1206 auto* inputs = zone()->NewArray<InstructionOperand>(input_count); |
| 1207 inputs[0] = value_operand; |
| 1208 inputs[1] = default_operand; |
| 1209 for (size_t index = 0; index < case_count; ++index) { |
| 1210 int32_t value = case_values[index]; |
| 1211 BasicBlock* branch = case_branches[index]; |
| 1212 inputs[index * 2 + 2 + 0] = g.TempImmediate(value); |
| 1213 inputs[index * 2 + 2 + 1] = g.Label(branch); |
| 1214 } |
| 1215 Emit(kArchLookupSwitch, 0, nullptr, input_count, inputs, 0, nullptr) |
| 1216 ->MarkAsControl(); |
| 1217 } |
| 1218 |
| 1219 |
1154 void InstructionSelector::VisitWord32Equal(Node* const node) { | 1220 void InstructionSelector::VisitWord32Equal(Node* const node) { |
1155 Node* user = node; | 1221 Node* user = node; |
1156 FlagsContinuation cont(kEqual, node); | 1222 FlagsContinuation cont(kEqual, node); |
1157 Int32BinopMatcher m(user); | 1223 Int32BinopMatcher m(user); |
1158 if (m.right().Is(0)) { | 1224 if (m.right().Is(0)) { |
1159 Node* value = m.left().node(); | 1225 Node* value = m.left().node(); |
1160 | 1226 |
1161 // Try to combine with comparisons against 0 by simply inverting the branch. | 1227 // Try to combine with comparisons against 0 by simply inverting the branch. |
1162 while (CanCover(user, value) && value->opcode() == IrOpcode::kWord32Equal) { | 1228 while (CanCover(user, value) && value->opcode() == IrOpcode::kWord32Equal) { |
1163 Int32BinopMatcher m(value); | 1229 Int32BinopMatcher m(value); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1311 MachineOperatorBuilder::kFloat64Ceil | | 1377 MachineOperatorBuilder::kFloat64Ceil | |
1312 MachineOperatorBuilder::kFloat64RoundTruncate | | 1378 MachineOperatorBuilder::kFloat64RoundTruncate | |
1313 MachineOperatorBuilder::kWord32ShiftIsSafe; | 1379 MachineOperatorBuilder::kWord32ShiftIsSafe; |
1314 } | 1380 } |
1315 return MachineOperatorBuilder::kNoFlags; | 1381 return MachineOperatorBuilder::kNoFlags; |
1316 } | 1382 } |
1317 | 1383 |
1318 } // namespace compiler | 1384 } // namespace compiler |
1319 } // namespace internal | 1385 } // namespace internal |
1320 } // namespace v8 | 1386 } // namespace v8 |
OLD | NEW |