From ed84c54ce0d03073db64363b443c41903324b397 Mon Sep 17 00:00:00 2001 From: "srdjan@google.com" Date: Tue, 19 Mar 2013 22:43:03 +0000 Subject: [PATCH] Improve code (less code, slightly faster) for polymorphic calls, by loading the argument descriptor only once instead of once for each call. Review URL: https://codereview.chromium.org//12646012 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@20254 260f80e4-7a28-3924-810f-c04153c831b5 --- runtime/vm/code_patcher_ia32.cc | 9 ++++----- runtime/vm/code_patcher_x64.cc | 19 +++++++++---------- runtime/vm/flow_graph_compiler_ia32.cc | 26 ++++++++++++++++++-------- runtime/vm/flow_graph_compiler_x64.cc | 26 ++++++++++++++++++-------- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/runtime/vm/code_patcher_ia32.cc b/runtime/vm/code_patcher_ia32.cc index cd21ef2d488..2bd3f1bebad 100644 --- a/runtime/vm/code_patcher_ia32.cc +++ b/runtime/vm/code_patcher_ia32.cc @@ -93,7 +93,7 @@ class InstanceCall : public DartCallPattern { // The expected pattern of a dart static call: -// mov EDX, arguments_descriptor_array +// mov EDX, arguments_descriptor_array (optional in polymorphic calls) // call target_address // <- return address class StaticCall : public ValueObject { @@ -108,8 +108,7 @@ class StaticCall : public ValueObject { uint8_t* code_bytes = reinterpret_cast( return_address - (kNumInstructions * kInstructionSize)); - return (code_bytes[0] == 0xBA) && - (code_bytes[1 * kInstructionSize] == 0xE8); + return (code_bytes[0] == 0xE8); } uword target() const { @@ -124,7 +123,7 @@ class StaticCall : public ValueObject { CPU::FlushICache(call_address(), kInstructionSize); } - static const int kNumInstructions = 2; + static const int kNumInstructions = 1; static const int kInstructionSize = 5; // All instructions have same length. private: @@ -133,7 +132,7 @@ class StaticCall : public ValueObject { } uword call_address() const { - return start_ + 1 * kInstructionSize; + return start_; } uword start_; diff --git a/runtime/vm/code_patcher_x64.cc b/runtime/vm/code_patcher_x64.cc index 5be92be8f18..1f2cebe8321 100644 --- a/runtime/vm/code_patcher_x64.cc +++ b/runtime/vm/code_patcher_x64.cc @@ -86,7 +86,7 @@ class InstanceCall : public DartCallPattern { // The expected pattern of a dart static call: -// mov R10, arguments_descriptor_array (10 bytes) +// mov R10, arguments_descriptor_array (10 bytes) (optional in polym. calls) // mov R11, target_address (10 bytes) // call R11 (3 bytes) // <- return address @@ -95,28 +95,27 @@ class StaticCall : public ValueObject { explicit StaticCall(uword return_address) : start_(return_address - kCallPatternSize) { ASSERT(IsValid(return_address)); - ASSERT((kCallPatternSize - 10) == Assembler::kCallExternalLabelSize); + ASSERT(kCallPatternSize == Assembler::kCallExternalLabelSize); } - static const int kCallPatternSize = 23; + static const int kCallPatternSize = 13; static bool IsValid(uword return_address) { uint8_t* code_bytes = reinterpret_cast(return_address - kCallPatternSize); - return (code_bytes[00] == 0x49) && (code_bytes[01] == 0xBA) && - (code_bytes[10] == 0x49) && (code_bytes[11] == 0xBB) && - (code_bytes[20] == 0x41) && (code_bytes[21] == 0xFF) && - (code_bytes[22] == 0xD3); + return (code_bytes[00] == 0x49) && (code_bytes[01] == 0xBB) && + (code_bytes[10] == 0x41) && (code_bytes[11] == 0xFF) && + (code_bytes[12] == 0xD3); } uword target() const { - return *reinterpret_cast(start_ + 10 + 2); + return *reinterpret_cast(start_ + 2); } void set_target(uword target) const { - uword* target_addr = reinterpret_cast(start_ + 10 + 2); + uword* target_addr = reinterpret_cast(start_ + 2); *target_addr = target; - CPU::FlushICache(start_ + 10, 2 + 8); + CPU::FlushICache(start_, 2 + 8); } private: diff --git a/runtime/vm/flow_graph_compiler_ia32.cc b/runtime/vm/flow_graph_compiler_ia32.cc index 6b0a35ef46c..25534e15f23 100644 --- a/runtime/vm/flow_graph_compiler_ia32.cc +++ b/runtime/vm/flow_graph_compiler_ia32.cc @@ -1386,8 +1386,8 @@ void FlowGraphCompiler::RestoreLiveRegisters(LocationSummary* locs) { void FlowGraphCompiler::EmitTestAndCall(const ICData& ic_data, Register class_id_reg, - intptr_t arg_count, - const Array& arg_names, + intptr_t argument_count, + const Array& argument_names, Label* deopt, intptr_t deopt_id, intptr_t token_index, @@ -1397,6 +1397,12 @@ void FlowGraphCompiler::EmitTestAndCall(const ICData& ic_data, const intptr_t len = ic_data.NumberOfChecks(); GrowableArray sorted(len); SortICDataByCount(ic_data, &sorted); + ASSERT(class_id_reg != EDX); + ASSERT(len > 0); // Why bother otherwise. + const Array& arguments_descriptor = + Array::ZoneHandle(ArgumentsDescriptor::New(argument_count, + argument_names)); + __ LoadObject(EDX, arguments_descriptor); for (intptr_t i = 0; i < len; i++) { const bool is_last_check = (i == (len - 1)); Label next_test; @@ -1406,12 +1412,16 @@ void FlowGraphCompiler::EmitTestAndCall(const ICData& ic_data, } else { assembler()->j(NOT_EQUAL, &next_test); } - GenerateStaticCall(deopt_id, - token_index, - *sorted[i].target, - arg_count, - arg_names, - locs); + // Do not use the code from the function, but let the code be patched so + // that we can record the outgoing edges to other code. + GenerateDartCall(deopt_id, + token_index, + &StubCode::CallStaticFunctionLabel(), + PcDescriptors::kFuncCall, + locs); + const Function& function = *sorted[i].target; + AddStaticCallTarget(function); + __ Drop(argument_count); if (!is_last_check) { assembler()->jmp(&match_found); } diff --git a/runtime/vm/flow_graph_compiler_x64.cc b/runtime/vm/flow_graph_compiler_x64.cc index e0533498230..2af94a416f2 100644 --- a/runtime/vm/flow_graph_compiler_x64.cc +++ b/runtime/vm/flow_graph_compiler_x64.cc @@ -1386,8 +1386,8 @@ void FlowGraphCompiler::RestoreLiveRegisters(LocationSummary* locs) { void FlowGraphCompiler::EmitTestAndCall(const ICData& ic_data, Register class_id_reg, - intptr_t arg_count, - const Array& arg_names, + intptr_t argument_count, + const Array& argument_names, Label* deopt, intptr_t deopt_id, intptr_t token_index, @@ -1397,6 +1397,12 @@ void FlowGraphCompiler::EmitTestAndCall(const ICData& ic_data, const intptr_t len = ic_data.NumberOfChecks(); GrowableArray sorted(len); SortICDataByCount(ic_data, &sorted); + ASSERT(class_id_reg != R10); + ASSERT(len > 0); // Why bother otherwise. + const Array& arguments_descriptor = + Array::ZoneHandle(ArgumentsDescriptor::New(argument_count, + argument_names)); + __ LoadObject(R10, arguments_descriptor); for (intptr_t i = 0; i < len; i++) { const bool is_last_check = (i == (len - 1)); Label next_test; @@ -1406,12 +1412,16 @@ void FlowGraphCompiler::EmitTestAndCall(const ICData& ic_data, } else { assembler()->j(NOT_EQUAL, &next_test); } - GenerateStaticCall(deopt_id, - token_index, - *sorted[i].target, - arg_count, - arg_names, - locs); + // Do not use the code from the function, but let the code be patched so + // that we can record the outgoing edges to other code. + GenerateDartCall(deopt_id, + token_index, + &StubCode::CallStaticFunctionLabel(), + PcDescriptors::kFuncCall, + locs); + const Function& function = *sorted[i].target; + AddStaticCallTarget(function); + __ Drop(argument_count); if (!is_last_check) { assembler()->jmp(&match_found); }