Commit c140a90c authored by zhengxing.li's avatar zhengxing.li Committed by Commit bot

X87: [turbofan]: Support using push instructions for setting up tail call parameters.

  port bd0d9e7d (r37477)

  original commit message:
  This optimizes the passing of stack parameters in function calls.

  For some architectures (ia32/x64), using pushes when possible instead
  of bumping the stack and then storing parameters generates much
  smaller code, and in some cases is faster (e.g. when a push of a memory
  location can implement a memory-to-memory copy and thus elide an
  intermediate load. On others (e.g. ARM), the benefit is smaller, where
  it's only possible to elide direct stack pointer adjustment in certain cases
  or combine multiple register stores into a single instruction in other limited
  situations. On yet other platforms (ARM64, MIPS), there are no push instructions,
  and this optimization isn't used at all.

  Ideally, this mechanism would be used for both tail calls and normal calls,
  but "normal" calls are currently pretty efficient, and tail calls are very
  inefficient, so this CL sets the bar low for building a new mechanism to
  handle parameter pushing that only needs to raise the bar on tail calls for now.

  The key aspect of this change is that adjustment to the stack pointer
  for tail calls (and perhaps later real calls) is an explicit step separate from
  instruction selection and gap resolution, but aware of both, making it possible
  to safely recognize gap moves that are actually pushes.

BUG=

Review-Url: https://codereview.chromium.org/2120413002
Cr-Commit-Position: refs/heads/master@{#37508}
parent 462d57ae
...@@ -420,21 +420,7 @@ void CodeGenerator::AssembleDeconstructFrame() { ...@@ -420,21 +420,7 @@ void CodeGenerator::AssembleDeconstructFrame() {
__ pop(ebp); __ pop(ebp);
} }
void CodeGenerator::AssembleDeconstructActivationRecord(int stack_param_delta) { void CodeGenerator::AssemblePrepareTailCall() {
int sp_slot_delta = TailCallFrameStackSlotDelta(stack_param_delta);
if (sp_slot_delta > 0) {
__ add(esp, Immediate(sp_slot_delta * kPointerSize));
}
frame_access_state()->SetFrameAccessToDefault();
}
void CodeGenerator::AssemblePrepareTailCall(int stack_param_delta) {
int sp_slot_delta = TailCallFrameStackSlotDelta(stack_param_delta);
if (sp_slot_delta < 0) {
__ sub(esp, Immediate(-sp_slot_delta * kPointerSize));
frame_access_state()->IncreaseSPDelta(-sp_slot_delta);
}
if (frame_access_state()->has_frame()) { if (frame_access_state()->has_frame()) {
__ mov(ebp, MemOperand(ebp, 0)); __ mov(ebp, MemOperand(ebp, 0));
} }
...@@ -479,6 +465,68 @@ void CodeGenerator::AssemblePopArgumentsAdaptorFrame(Register args_reg, ...@@ -479,6 +465,68 @@ void CodeGenerator::AssemblePopArgumentsAdaptorFrame(Register args_reg,
__ bind(&done); __ bind(&done);
} }
namespace {
void AdjustStackPointerForTailCall(MacroAssembler* masm,
FrameAccessState* state,
int new_slot_above_sp,
bool allow_shrinkage = true) {
int current_sp_offset = state->GetSPToFPSlotCount() +
StandardFrameConstants::kFixedSlotCountAboveFp;
int stack_slot_delta = new_slot_above_sp - current_sp_offset;
if (stack_slot_delta > 0) {
masm->sub(esp, Immediate(stack_slot_delta * kPointerSize));
state->IncreaseSPDelta(stack_slot_delta);
} else if (allow_shrinkage && stack_slot_delta < 0) {
masm->add(esp, Immediate(-stack_slot_delta * kPointerSize));
state->IncreaseSPDelta(stack_slot_delta);
}
}
} // namespace
void CodeGenerator::AssembleTailCallBeforeGap(Instruction* instr,
int first_unused_stack_slot) {
CodeGenerator::PushTypeFlags flags(kImmediatePush | kScalarPush);
ZoneVector<MoveOperands*> pushes(zone());
GetPushCompatibleMoves(instr, flags, &pushes);
if (!pushes.empty() &&
(LocationOperand::cast(pushes.back()->destination()).index() + 1 ==
first_unused_stack_slot)) {
X87OperandConverter g(this, instr);
for (auto move : pushes) {
LocationOperand destination_location(
LocationOperand::cast(move->destination()));
InstructionOperand source(move->source());
AdjustStackPointerForTailCall(masm(), frame_access_state(),
destination_location.index());
if (source.IsStackSlot()) {
LocationOperand source_location(LocationOperand::cast(source));
__ push(g.SlotToOperand(source_location.index()));
} else if (source.IsRegister()) {
LocationOperand source_location(LocationOperand::cast(source));
__ push(source_location.GetRegister());
} else if (source.IsImmediate()) {
__ push(Immediate(ImmediateOperand::cast(source).inline_value()));
} else {
// Pushes of non-scalar data types is not supported.
UNIMPLEMENTED();
}
frame_access_state()->IncreaseSPDelta(1);
move->Eliminate();
}
}
AdjustStackPointerForTailCall(masm(), frame_access_state(),
first_unused_stack_slot, false);
}
void CodeGenerator::AssembleTailCallAfterGap(Instruction* instr,
int first_unused_stack_slot) {
AdjustStackPointerForTailCall(masm(), frame_access_state(),
first_unused_stack_slot);
}
// Assembles an instruction after register allocation, producing machine code. // Assembles an instruction after register allocation, producing machine code.
CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
Instruction* instr) { Instruction* instr) {
...@@ -524,8 +572,6 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( ...@@ -524,8 +572,6 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
__ VerifyX87StackDepth(1); __ VerifyX87StackDepth(1);
} }
__ fstp(0); __ fstp(0);
int stack_param_delta = i.InputInt32(instr->InputCount() - 1);
AssembleDeconstructActivationRecord(stack_param_delta);
if (arch_opcode == kArchTailCallCodeObjectFromJSFunction) { if (arch_opcode == kArchTailCallCodeObjectFromJSFunction) {
AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister,
no_reg, no_reg, no_reg); no_reg, no_reg, no_reg);
...@@ -539,15 +585,15 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( ...@@ -539,15 +585,15 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
__ jmp(reg); __ jmp(reg);
} }
frame_access_state()->ClearSPDelta(); frame_access_state()->ClearSPDelta();
frame_access_state()->SetFrameAccessToDefault();
break; break;
} }
case kArchTailCallAddress: { case kArchTailCallAddress: {
int stack_param_delta = i.InputInt32(instr->InputCount() - 1);
AssembleDeconstructActivationRecord(stack_param_delta);
CHECK(!HasImmediateInput(instr, 0)); CHECK(!HasImmediateInput(instr, 0));
Register reg = i.InputRegister(0); Register reg = i.InputRegister(0);
__ jmp(reg); __ jmp(reg);
frame_access_state()->ClearSPDelta(); frame_access_state()->ClearSPDelta();
frame_access_state()->SetFrameAccessToDefault();
break; break;
} }
case kArchCallJSFunction: { case kArchCallJSFunction: {
...@@ -592,14 +638,13 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( ...@@ -592,14 +638,13 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
__ VerifyX87StackDepth(1); __ VerifyX87StackDepth(1);
} }
__ fstp(0); __ fstp(0);
int stack_param_delta = i.InputInt32(instr->InputCount() - 1);
AssembleDeconstructActivationRecord(stack_param_delta);
if (arch_opcode == kArchTailCallJSFunctionFromJSFunction) { if (arch_opcode == kArchTailCallJSFunctionFromJSFunction) {
AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister, AssemblePopArgumentsAdaptorFrame(kJavaScriptCallArgCountRegister,
no_reg, no_reg, no_reg); no_reg, no_reg, no_reg);
} }
__ jmp(FieldOperand(func, JSFunction::kCodeEntryOffset)); __ jmp(FieldOperand(func, JSFunction::kCodeEntryOffset));
frame_access_state()->ClearSPDelta(); frame_access_state()->ClearSPDelta();
frame_access_state()->SetFrameAccessToDefault();
break; break;
} }
case kArchPrepareCallCFunction: { case kArchPrepareCallCFunction: {
...@@ -610,7 +655,7 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction( ...@@ -610,7 +655,7 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
break; break;
} }
case kArchPrepareTailCall: case kArchPrepareTailCall:
AssemblePrepareTailCall(i.InputInt32(instr->InputCount() - 1)); AssemblePrepareTailCall();
break; break;
case kArchCallCFunction: { case kArchCallCFunction: {
if (FLAG_debug_code && FLAG_enable_slow_asserts) { if (FLAG_debug_code && FLAG_enable_slow_asserts) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment