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

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

Issue 1114163005: [turbofan] Fix tail call optimization. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 7 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
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/base/adapters.h" 7 #include "src/base/adapters.h"
8 #include "src/compiler/instruction-selector-impl.h" 8 #include "src/compiler/instruction-selector-impl.h"
9 #include "src/compiler/node-matchers.h" 9 #include "src/compiler/node-matchers.h"
10 #include "src/compiler/node-properties.h" 10 #include "src/compiler/node-properties.h"
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 VisitRRFloat64(this, kSSEFloat64Round | MiscField::encode(kRoundToZero), 1012 VisitRRFloat64(this, kSSEFloat64Round | MiscField::encode(kRoundToZero),
1013 node); 1013 node);
1014 } 1014 }
1015 1015
1016 1016
1017 void InstructionSelector::VisitFloat64RoundTiesAway(Node* node) { 1017 void InstructionSelector::VisitFloat64RoundTiesAway(Node* node) {
1018 UNREACHABLE(); 1018 UNREACHABLE();
1019 } 1019 }
1020 1020
1021 1021
1022 void InstructionSelector::VisitCall(Node* node, BasicBlock* handler, 1022 void InstructionSelector::VisitCall(Node* node, BasicBlock* handler) {
1023 CallMode call_mode) {
1024 X64OperandGenerator g(this); 1023 X64OperandGenerator g(this);
1025 const CallDescriptor* descriptor = OpParameter<const CallDescriptor*>(node); 1024 const CallDescriptor* descriptor = OpParameter<const CallDescriptor*>(node);
1026 1025
1027 FrameStateDescriptor* frame_state_descriptor = NULL; 1026 FrameStateDescriptor* frame_state_descriptor = nullptr;
1028 if (descriptor->NeedsFrameState()) { 1027 if (descriptor->NeedsFrameState()) {
1029 frame_state_descriptor = GetFrameStateDescriptor( 1028 frame_state_descriptor = GetFrameStateDescriptor(
1030 node->InputAt(static_cast<int>(descriptor->InputCount()))); 1029 node->InputAt(static_cast<int>(descriptor->InputCount())));
1031 } 1030 }
1032 1031
1033 CallBuffer buffer(zone(), descriptor, frame_state_descriptor); 1032 CallBuffer buffer(zone(), descriptor, frame_state_descriptor);
1034 1033
1035 // Compute InstructionOperands for inputs and outputs. 1034 // Compute InstructionOperands for inputs and outputs.
1036 InitializeCallBuffer(node, &buffer, true, true); 1035 InitializeCallBuffer(node, &buffer, true, true);
1037 1036
1038 // Push any stack arguments. 1037 // Push any stack arguments.
1039 for (Node* node : base::Reversed(buffer.pushed_nodes)) { 1038 for (Node* node : base::Reversed(buffer.pushed_nodes)) {
1040 // TODO(titzer): handle pushing double parameters. 1039 // TODO(titzer): handle pushing double parameters.
1041 InstructionOperand value = 1040 InstructionOperand value =
1042 g.CanBeImmediate(node) 1041 g.CanBeImmediate(node)
1043 ? g.UseImmediate(node) 1042 ? g.UseImmediate(node)
1044 : IsSupported(ATOM) ? g.UseRegister(node) : g.Use(node); 1043 : IsSupported(ATOM) ? g.UseRegister(node) : g.Use(node);
1045 Emit(kX64Push, g.NoOutput(), value); 1044 Emit(kX64Push, g.NoOutput(), value);
1046 } 1045 }
1047 1046
1048 // Pass label of exception handler block. 1047 // Pass label of exception handler block.
1049 CallDescriptor::Flags flags = descriptor->flags(); 1048 CallDescriptor::Flags flags = descriptor->flags();
1050 if (handler != nullptr) { 1049 if (handler) {
1051 flags |= CallDescriptor::kHasExceptionHandler; 1050 flags |= CallDescriptor::kHasExceptionHandler;
1052 buffer.instruction_args.push_back(g.Label(handler)); 1051 buffer.instruction_args.push_back(g.Label(handler));
1053 } 1052 }
1054 1053
1055 // Select the appropriate opcode based on the call type. 1054 // Select the appropriate opcode based on the call type.
1056 bool is_tail_call = call_mode == TAIL_CALL;
1057 InstructionCode opcode; 1055 InstructionCode opcode;
1058 switch (descriptor->kind()) { 1056 switch (descriptor->kind()) {
1059 case CallDescriptor::kCallCodeObject: { 1057 case CallDescriptor::kCallCodeObject:
1060 opcode = is_tail_call ? kArchTailCallCodeObject : kArchCallCodeObject; 1058 opcode = kArchCallCodeObject;
1061 break; 1059 break;
1062 }
1063 case CallDescriptor::kCallJSFunction: 1060 case CallDescriptor::kCallJSFunction:
1064 opcode = is_tail_call ? kArchTailCallJSFunction : kArchCallJSFunction; 1061 opcode = kArchCallJSFunction;
1065 break; 1062 break;
1066 default: 1063 default:
1067 UNREACHABLE(); 1064 UNREACHABLE();
1068 return; 1065 return;
1069 } 1066 }
1070 opcode |= MiscField::encode(flags); 1067 opcode |= MiscField::encode(flags);
1071 1068
1072 // Emit the call instruction. 1069 // Emit the call instruction.
1073 size_t size = is_tail_call ? 0 : buffer.outputs.size(); 1070 size_t const output_count = buffer.outputs.size();
1074 InstructionOperand* first_output = 1071 auto* outputs = output_count ? &buffer.outputs.front() : nullptr;
1075 size > 0 ? &buffer.outputs.front() : nullptr; 1072 Emit(opcode, output_count, outputs, buffer.instruction_args.size(),
1076 Instruction* call_instr = 1073 &buffer.instruction_args.front())->MarkAsCall();
1077 Emit(opcode, size, first_output, buffer.instruction_args.size(),
1078 &buffer.instruction_args.front());
1079 call_instr->MarkAsCall();
1080 } 1074 }
1081 1075
1082 1076
1077 void InstructionSelector::VisitTailCall(Node* node) {
1078 X64OperandGenerator g(this);
1079 CallDescriptor const* descriptor = OpParameter<CallDescriptor const*>(node);
1080 DCHECK_NE(0, descriptor->flags() & CallDescriptor::kSupportsTailCalls);
1081 DCHECK_EQ(0, descriptor->flags() & CallDescriptor::kPatchableCallSite);
1082 DCHECK_EQ(0, descriptor->flags() & CallDescriptor::kNeedsNopAfterCall);
1083
1084 // TODO(turbofan): Relax restriction for stack parameters.
1085 if (descriptor->UsesOnlyRegisters() &&
1086 descriptor->HasSameReturnLocationsAs(
1087 linkage()->GetIncomingDescriptor())) {
1088 CallBuffer buffer(zone(), descriptor, nullptr);
1089
1090 // Compute InstructionOperands for inputs and outputs.
1091 InitializeCallBuffer(node, &buffer, true, true);
1092
1093 DCHECK_EQ(0u, buffer.pushed_nodes.size());
1094
1095 // Select the appropriate opcode based on the call type.
1096 InstructionCode opcode;
1097 switch (descriptor->kind()) {
1098 case CallDescriptor::kCallCodeObject:
1099 opcode = kArchTailCallCodeObject;
1100 break;
1101 case CallDescriptor::kCallJSFunction:
1102 opcode = kArchTailCallJSFunction;
1103 break;
1104 default:
1105 UNREACHABLE();
1106 return;
1107 }
1108 opcode |= MiscField::encode(descriptor->flags());
1109
1110 // Emit the tailcall instruction.
1111 Emit(opcode, 0, nullptr, buffer.instruction_args.size(),
1112 &buffer.instruction_args.front());
1113 } else {
1114 FrameStateDescriptor* frame_state_descriptor =
1115 descriptor->NeedsFrameState()
1116 ? GetFrameStateDescriptor(
1117 node->InputAt(static_cast<int>(descriptor->InputCount())))
1118 : nullptr;
1119
1120 CallBuffer buffer(zone(), descriptor, frame_state_descriptor);
1121
1122 // Compute InstructionOperands for inputs and outputs.
1123 InitializeCallBuffer(node, &buffer, true, true);
1124
1125 // Push any stack arguments.
1126 for (Node* node : base::Reversed(buffer.pushed_nodes)) {
1127 // TODO(titzer): Handle pushing double parameters.
1128 InstructionOperand value =
1129 g.CanBeImmediate(node)
1130 ? g.UseImmediate(node)
1131 : IsSupported(ATOM) ? g.UseRegister(node) : g.Use(node);
1132 Emit(kX64Push, g.NoOutput(), value);
1133 }
1134
1135 // Select the appropriate opcode based on the call type.
1136 InstructionCode opcode;
1137 switch (descriptor->kind()) {
1138 case CallDescriptor::kCallCodeObject:
1139 opcode = kArchCallCodeObject;
1140 break;
1141 case CallDescriptor::kCallJSFunction:
1142 opcode = kArchCallJSFunction;
1143 break;
1144 default:
1145 UNREACHABLE();
1146 return;
1147 }
1148 opcode |= MiscField::encode(descriptor->flags());
1149
1150 // Emit the call instruction.
1151 size_t output_count = buffer.outputs.size();
1152 auto* outputs = &buffer.outputs.front();
1153 Emit(opcode, output_count, outputs, buffer.instruction_args.size(),
1154 &buffer.instruction_args.front())->MarkAsCall();
1155 Emit(kArchRet, 0, nullptr, output_count, outputs);
1156 }
1157 }
1158
1159
1083 namespace { 1160 namespace {
1084 1161
1085 // Shared routine for multiple compare operations. 1162 // Shared routine for multiple compare operations.
1086 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, 1163 void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
1087 InstructionOperand left, InstructionOperand right, 1164 InstructionOperand left, InstructionOperand right,
1088 FlagsContinuation* cont) { 1165 FlagsContinuation* cont) {
1089 X64OperandGenerator g(selector); 1166 X64OperandGenerator g(selector);
1090 opcode = cont->Encode(opcode); 1167 opcode = cont->Encode(opcode);
1091 if (cont->IsBranch()) { 1168 if (cont->IsBranch()) {
1092 selector->Emit(opcode, g.NoOutput(), left, right, 1169 selector->Emit(opcode, g.NoOutput(), left, right,
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
1552 if (CpuFeatures::IsSupported(SSE4_1)) { 1629 if (CpuFeatures::IsSupported(SSE4_1)) {
1553 flags |= MachineOperatorBuilder::kFloat64RoundDown | 1630 flags |= MachineOperatorBuilder::kFloat64RoundDown |
1554 MachineOperatorBuilder::kFloat64RoundTruncate; 1631 MachineOperatorBuilder::kFloat64RoundTruncate;
1555 } 1632 }
1556 return flags; 1633 return flags;
1557 } 1634 }
1558 1635
1559 } // namespace compiler 1636 } // namespace compiler
1560 } // namespace internal 1637 } // namespace internal
1561 } // namespace v8 1638 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698