Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: src/compiler/x64/instruction-selector-x64.cc

Issue 1019803005: [turbofan] Factor out common switch-related code in instruction selectors. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/compiler/ppc/instruction-selector-ppc.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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> 5 #include <algorithm>
6 6
7 #include "src/compiler/instruction-selector-impl.h" 7 #include "src/compiler/instruction-selector-impl.h"
8 #include "src/compiler/node-matchers.h" 8 #include "src/compiler/node-matchers.h"
9 #include "src/compiler/node-properties.h" 9 #include "src/compiler/node-properties.h"
10 10
(...skipping 1202 matching lines...) Expand 10 before | Expand all | Expand 10 after
1213 default: 1213 default:
1214 break; 1214 break;
1215 } 1215 }
1216 } 1216 }
1217 1217
1218 // Branch could not be combined with a compare, emit compare against 0. 1218 // Branch could not be combined with a compare, emit compare against 0.
1219 VisitCompareZero(this, value, kX64Cmp32, &cont); 1219 VisitCompareZero(this, value, kX64Cmp32, &cont);
1220 } 1220 }
1221 1221
1222 1222
1223 void InstructionSelector::VisitSwitch(Node* node, BasicBlock* default_branch, 1223 void InstructionSelector::VisitSwitch(Node* node, const SwitchInfo& sw) {
1224 BasicBlock** case_branches,
1225 int32_t* case_values, size_t case_count,
1226 int32_t min_value, int32_t max_value) {
1227 X64OperandGenerator g(this); 1224 X64OperandGenerator g(this);
1228 InstructionOperand value_operand = g.UseRegister(node->InputAt(0)); 1225 InstructionOperand value_operand = g.UseRegister(node->InputAt(0));
1229 InstructionOperand default_operand = g.Label(default_branch);
1230 1226
1231 // Note that {value_range} can be 0 if {min_value} is -2^31 and {max_value} 1227 // Emit either ArchTableSwitch or ArchLookupSwitch.
1232 // is 2^31-1, so don't assume that it's non-zero below. 1228 size_t table_space_cost = 4 + sw.value_range;
1233 size_t value_range =
1234 1u + bit_cast<uint32_t>(max_value) - bit_cast<uint32_t>(min_value);
1235
1236 // Determine whether to issue an ArchTableSwitch or an ArchLookupSwitch
1237 // instruction.
1238 size_t table_space_cost = 4 + value_range;
1239 size_t table_time_cost = 3; 1229 size_t table_time_cost = 3;
1240 size_t lookup_space_cost = 3 + 2 * case_count; 1230 size_t lookup_space_cost = 3 + 2 * sw.case_count;
1241 size_t lookup_time_cost = case_count; 1231 size_t lookup_time_cost = sw.case_count;
1242 if (case_count > 4 && 1232 if (sw.case_count > 4 &&
1243 table_space_cost + 3 * table_time_cost <= 1233 table_space_cost + 3 * table_time_cost <=
1244 lookup_space_cost + 3 * lookup_time_cost && 1234 lookup_space_cost + 3 * lookup_time_cost &&
1245 min_value > std::numeric_limits<int32_t>::min()) { 1235 sw.min_value > std::numeric_limits<int32_t>::min()) {
1246 InstructionOperand index_operand = g.TempRegister(); 1236 InstructionOperand index_operand = g.TempRegister();
1247 if (min_value) { 1237 if (sw.min_value) {
1248 // The leal automatically zero extends, so result is a valid 64-bit index. 1238 // The leal automatically zero extends, so result is a valid 64-bit index.
1249 Emit(kX64Lea32 | AddressingModeField::encode(kMode_MRI), index_operand, 1239 Emit(kX64Lea32 | AddressingModeField::encode(kMode_MRI), index_operand,
1250 value_operand, g.TempImmediate(-min_value)); 1240 value_operand, g.TempImmediate(-sw.min_value));
1251 } else { 1241 } else {
1252 // Zero extend, because we use it as 64-bit index into the jump table. 1242 // Zero extend, because we use it as 64-bit index into the jump table.
1253 Emit(kX64Movl, index_operand, value_operand); 1243 Emit(kX64Movl, index_operand, value_operand);
1254 } 1244 }
1255 size_t input_count = 2 + value_range; 1245 // Generate a table lookup.
1256 auto* inputs = zone()->NewArray<InstructionOperand>(input_count); 1246 return EmitTableSwitch(sw, index_operand);
1257 inputs[0] = index_operand;
1258 std::fill(&inputs[1], &inputs[input_count], default_operand);
1259 for (size_t index = 0; index < case_count; ++index) {
1260 size_t value = case_values[index] - min_value;
1261 BasicBlock* branch = case_branches[index];
1262 DCHECK_LE(0u, value);
1263 DCHECK_LT(value + 2, input_count);
1264 inputs[value + 2] = g.Label(branch);
1265 }
1266 Emit(kArchTableSwitch, 0, nullptr, input_count, inputs, 0, nullptr);
1267 return;
1268 } 1247 }
1269 1248
1270 // Generate a sequence of conditional jumps. 1249 // Generate a sequence of conditional jumps.
1271 size_t input_count = 2 + case_count * 2; 1250 return EmitLookupSwitch(sw, value_operand);
1272 auto* inputs = zone()->NewArray<InstructionOperand>(input_count);
1273 inputs[0] = value_operand;
1274 inputs[1] = default_operand;
1275 for (size_t index = 0; index < case_count; ++index) {
1276 int32_t value = case_values[index];
1277 BasicBlock* branch = case_branches[index];
1278 inputs[index * 2 + 2 + 0] = g.TempImmediate(value);
1279 inputs[index * 2 + 2 + 1] = g.Label(branch);
1280 }
1281 Emit(kArchLookupSwitch, 0, nullptr, input_count, inputs, 0, nullptr);
1282 } 1251 }
1283 1252
1284 1253
1285 void InstructionSelector::VisitWord32Equal(Node* const node) { 1254 void InstructionSelector::VisitWord32Equal(Node* const node) {
1286 Node* user = node; 1255 Node* user = node;
1287 FlagsContinuation cont(kEqual, node); 1256 FlagsContinuation cont(kEqual, node);
1288 Int32BinopMatcher m(user); 1257 Int32BinopMatcher m(user);
1289 if (m.right().Is(0)) { 1258 if (m.right().Is(0)) {
1290 Node* value = m.left().node(); 1259 Node* value = m.left().node();
1291 1260
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
1481 if (CpuFeatures::IsSupported(SSE4_1)) { 1450 if (CpuFeatures::IsSupported(SSE4_1)) {
1482 flags |= MachineOperatorBuilder::kFloat64RoundDown | 1451 flags |= MachineOperatorBuilder::kFloat64RoundDown |
1483 MachineOperatorBuilder::kFloat64RoundTruncate; 1452 MachineOperatorBuilder::kFloat64RoundTruncate;
1484 } 1453 }
1485 return flags; 1454 return flags;
1486 } 1455 }
1487 1456
1488 } // namespace compiler 1457 } // namespace compiler
1489 } // namespace internal 1458 } // namespace internal
1490 } // namespace v8 1459 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/ppc/instruction-selector-ppc.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698