3b414a277c
Relands165c583d57[VM] Introduction of type testing stubs - Part 1 This CL: * Adds a field to [RawAbstractType] which will always hold a pointer to the entrypoint of a type testing stub * Makes this new field be initialized to a default stub whenever a instances are created (e.g. via Type::New(), snapshot reader, ...) * Makes the clustered snapshotter write a reference to the corresponding [RawInstructions] object when writing the field and do the reverse when reading it. * Makes us call the type testing stub for performing assert-assignable checks. To reduce unnecessary loads on callsites, we store the entrypoint of the type testing stubs directly in the type objects. This means that the caller of type testing stubs can simply branch there without populating a code object first. This also means that the type testing stubs themselves have no access to a pool and we therefore also don't hold on to the [Code] object, only the [Instruction] object is necessary. The type testing stubs do not setup a frame themselves and also have no safepoint. In the case when the type testing stubs could not determine a positive answer they will tail-call a general-purpose stub. The general-purpose stub sets up a stub frame, tries to consult a [SubtypeTestCache] and bails out to runtime if this was unsuccessful. This CL is just the the first, for ease of reviewing. The actual type-specialized type testing stubs will be generated in later CLs. Reviewed-on: https://dart-review.googlesource.com/44787 Relandsf226c22424[VM] Introduction of type testing stubs - Part 2 This CL starts building type testing stubs specialzed for [Type] objects we test against. More specifically, it adds support for: * Handling obvious fast cases on the call sites (while still having a call to stub for negative case) * Handling type tests against type parameters, by loading the value of the type parameter on the call sites and invoking it's type testing stub. * Specialzed type testing stubs for instantiated types where we can do [CidRange]-based subtype-checks. ==> e.g. String/List<dynamic> * Specialzed type testing stubs for instantiated types where we can do [CidRange]-based subclass-checks for the class and [CidRange]-based subtype-checks for the type arguments. ==> e.g. Widget<State>, where we know [Widget] is only extended and not implemented. * Specialzed type testing stubs for certain non-instantiated types where we can do [CidRange]-based subclass-checks for the class and [CidRange]-based subtype-checks for the instantiated type arguments and cid based comparisons for type parameters. (Note that this fast-case migth result in some false-negatives!) ==> e.g. _HashMapEntry<K, V>, where we know [_HashMapEntry] is only extended and not implemented. This optimizes cases where the caller uses `new HashMap<A, B>()` and only uses `A` and `B` as key/values (and not subclasses of it). The false-negative can occur when subtypes of A or B are used. In such cases we fall back to the [SubtypeTestCache]-based imlementation. Reviewed-on: https://dart-review.googlesource.com/44788 Relands25f98bcc75[VM] Introduction of type testing stubs - Part 3 The changes include: * Make AssertAssignableInstr no longer have a call-summary, which helps methods with several parameter checks by not having to re-load/re-initialize type arguments registers * Lazily create SubtypeTestCaches: We already go to runtime to warm up the caches, so we now also create the caches on the first runtime call and patch the pool entries. * No longer load the destination name into a register: We only need the name when we throw an exception, so it is not on the hot path. Instead we let the runtime look at the call site, decoding a pool index from the instructions stream. The destination name will be available in the pool, at a consecutive index to the subtype cache. * Remove the fall-through to N=1 case for probing subtypeing tests, since those will always be handled by the optimized stubs. * Do not generate optimized stubs for FutureOr<T> (so far it just falled-through to TTS). We can make optimzed version of that later, but it requires special subtyping rules. * Local code quality improvement in the type-testing-stubs: Avoid extra jump at last case of cid-class-range checks. There are still a number of optimization opportunities we can do in future changes. Reviewed-on: https://dart-review.googlesource.com/46984 Relands2c52480ec8[VM] Introduction of type testing stubs - Part 4 In order to avoid generating type testing stubs for too many types in the system - and thereby potentially cause an increase in code size - this change introduces a smarter way to decide for which types we should generate optimized type testing stubs. The precompiler creates a [TypeUsageInfo] which we use to collect information. More specifically: a) We collect the destination types for all type checks we emit (we do this inside AssertAssignableInstr::EmitNativeCode). -> These are types we might want to generate optimized type testing stubs for. b) We collect type argument vectors used in instance creations (we do this inside AllocateObjectInstr::EmitNativeCode) and keep a set of of used type argument vectors for each class. After the precompiler has finished compiling normal code we scan the set of destination types collected in a) for uninstantiated types (or more specifically, type parameter types). We then propagate the type argument vectors used on object allocation sites, which were collected in b), in order to find out what kind of types are flowing into those type parameters. This allows us to extend the set of types which we test against, by adding the types that flow into type parameters. We use this final augmented set of destination types as a "filter" when making the decision whether to generate an optimized type testing stub for a given type. Reviewed-on: https://dart-review.googlesource.com/48640 Issue https://github.com/dart-lang/sdk/issues/32603 Closes https://github.com/dart-lang/sdk/issues/32852 Change-Id: Ib79fbe7f043aa88f32bddad62d7656c638914b44 Reviewed-on: https://dart-review.googlesource.com/50944 Commit-Queue: Martin Kustermann <kustermann@google.com> Reviewed-by: Régis Crelier <regis@google.com>
318 lines
10 KiB
C++
318 lines
10 KiB
C++
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "vm/globals.h" // Needed here to get TARGET_ARCH_X64.
|
|
#if defined(TARGET_ARCH_X64)
|
|
|
|
#include "vm/code_patcher.h"
|
|
#include "vm/compiler/assembler/assembler.h"
|
|
#include "vm/compiler/backend/flow_graph_compiler.h"
|
|
#include "vm/cpu.h"
|
|
#include "vm/dart_entry.h"
|
|
#include "vm/instructions.h"
|
|
#include "vm/object.h"
|
|
#include "vm/raw_object.h"
|
|
|
|
namespace dart {
|
|
|
|
intptr_t IndexFromPPLoad(uword start) {
|
|
int32_t offset = *reinterpret_cast<int32_t*>(start);
|
|
return ObjectPool::IndexFromOffset(offset);
|
|
}
|
|
|
|
intptr_t IndexFromPPLoadDisp8(uword start) {
|
|
int8_t offset = *reinterpret_cast<int8_t*>(start);
|
|
return ObjectPool::IndexFromOffset(offset);
|
|
}
|
|
|
|
class UnoptimizedCall : public ValueObject {
|
|
public:
|
|
UnoptimizedCall(uword return_address, const Code& code)
|
|
: object_pool_(ObjectPool::Handle(code.GetObjectPool())),
|
|
start_(return_address - kCallPatternSize) {
|
|
ASSERT((kCallPatternSize - 7) == Assembler::kCallExternalLabelSize);
|
|
ASSERT(IsValid());
|
|
}
|
|
|
|
static const int kCallPatternSize = 22;
|
|
|
|
bool IsValid() const {
|
|
static int16_t pattern[kCallPatternSize] = {
|
|
0x49, 0x8b, 0x9f, -1, -1, -1, -1, // movq RBX, [PP + offs]
|
|
0x4d, 0x8b, 0xa7, -1, -1, -1, -1, // movq CR, [PP + offs]
|
|
0x4d, 0x8b, 0x5c, 0x24, 0x07, // movq TMP, [CR + entry_point_offs]
|
|
0x41, 0xff, 0xd3 // callq TMP
|
|
};
|
|
return MatchesPattern(start_, pattern, kCallPatternSize);
|
|
}
|
|
|
|
intptr_t argument_index() const { return IndexFromPPLoad(start_ + 3); }
|
|
|
|
RawObject* ic_data() const { return object_pool_.ObjectAt(argument_index()); }
|
|
|
|
RawCode* target() const {
|
|
intptr_t index = IndexFromPPLoad(start_ + 10);
|
|
Code& code = Code::Handle();
|
|
code ^= object_pool_.ObjectAt(index);
|
|
return code.raw();
|
|
}
|
|
|
|
void set_target(const Code& target) const {
|
|
intptr_t index = IndexFromPPLoad(start_ + 10);
|
|
object_pool_.SetObjectAt(index, target);
|
|
// No need to flush the instruction cache, since the code is not modified.
|
|
}
|
|
|
|
protected:
|
|
const ObjectPool& object_pool_;
|
|
|
|
private:
|
|
uword start_;
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(UnoptimizedCall);
|
|
};
|
|
|
|
class NativeCall : public UnoptimizedCall {
|
|
public:
|
|
NativeCall(uword return_address, const Code& code)
|
|
: UnoptimizedCall(return_address, code) {}
|
|
|
|
NativeFunction native_function() const {
|
|
return reinterpret_cast<NativeFunction>(
|
|
object_pool_.RawValueAt(argument_index()));
|
|
}
|
|
|
|
void set_native_function(NativeFunction func) const {
|
|
object_pool_.SetRawValueAt(argument_index(), reinterpret_cast<uword>(func));
|
|
}
|
|
|
|
private:
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(NativeCall);
|
|
};
|
|
|
|
class InstanceCall : public UnoptimizedCall {
|
|
public:
|
|
InstanceCall(uword return_address, const Code& code)
|
|
: UnoptimizedCall(return_address, code) {
|
|
#if defined(DEBUG)
|
|
ICData& test_ic_data = ICData::Handle();
|
|
test_ic_data ^= ic_data();
|
|
ASSERT(test_ic_data.NumArgsTested() > 0);
|
|
#endif // DEBUG
|
|
}
|
|
|
|
private:
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(InstanceCall);
|
|
};
|
|
|
|
class UnoptimizedStaticCall : public UnoptimizedCall {
|
|
public:
|
|
UnoptimizedStaticCall(uword return_address, const Code& code)
|
|
: UnoptimizedCall(return_address, code) {
|
|
#if defined(DEBUG)
|
|
ICData& test_ic_data = ICData::Handle();
|
|
test_ic_data ^= ic_data();
|
|
ASSERT(test_ic_data.NumArgsTested() >= 0);
|
|
#endif // DEBUG
|
|
}
|
|
|
|
private:
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(UnoptimizedStaticCall);
|
|
};
|
|
|
|
// The expected pattern of a call where the target is loaded from
|
|
// the object pool.
|
|
class PoolPointerCall : public ValueObject {
|
|
public:
|
|
explicit PoolPointerCall(uword return_address, const Code& code)
|
|
: start_(return_address - kCallPatternSize),
|
|
object_pool_(ObjectPool::Handle(code.GetObjectPool())) {
|
|
ASSERT(IsValid());
|
|
}
|
|
|
|
static const int kCallPatternSize = 15;
|
|
|
|
bool IsValid() const {
|
|
static int16_t pattern[kCallPatternSize] = {
|
|
0x4d, 0x8b, 0xa7, -1, -1, -1, -1, // movq CR, [PP + offs]
|
|
0x4d, 0x8b, 0x5c, 0x24, 0x07, // movq TMP, [CR + entry_point_off]
|
|
0x41, 0xff, 0xd3 // callq TMP
|
|
};
|
|
return MatchesPattern(start_, pattern, kCallPatternSize);
|
|
}
|
|
|
|
intptr_t pp_index() const { return IndexFromPPLoad(start_ + 3); }
|
|
|
|
RawCode* Target() const {
|
|
Code& code = Code::Handle();
|
|
code ^= object_pool_.ObjectAt(pp_index());
|
|
return code.raw();
|
|
}
|
|
|
|
void SetTarget(const Code& target) const {
|
|
object_pool_.SetObjectAt(pp_index(), target);
|
|
// No need to flush the instruction cache, since the code is not modified.
|
|
}
|
|
|
|
protected:
|
|
uword start_;
|
|
const ObjectPool& object_pool_;
|
|
|
|
private:
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(PoolPointerCall);
|
|
};
|
|
|
|
// Instance call that can switch between a direct monomorphic call, an IC call,
|
|
// and a megamorphic call.
|
|
// load guarded cid load ICData load MegamorphicCache
|
|
// load monomorphic target <-> load ICLookup stub -> load MMLookup stub
|
|
// call target.entry call stub.entry call stub.entry
|
|
class SwitchableCall : public ValueObject {
|
|
public:
|
|
SwitchableCall(uword return_address, const Code& code)
|
|
: start_(return_address - kCallPatternSize),
|
|
object_pool_(ObjectPool::Handle(code.GetObjectPool())) {
|
|
ASSERT(IsValid());
|
|
}
|
|
|
|
static const int kCallPatternSize = 21;
|
|
|
|
bool IsValid() const {
|
|
static int16_t pattern[kCallPatternSize] = {
|
|
0x4d, 0x8b, 0xa7, -1, -1, -1, -1, // movq r12, [PP + code_offs]
|
|
0x49, 0x8b, 0x4c, 0x24, 0x0f, // movq rcx, [r12 + entrypoint_off]
|
|
0x49, 0x8b, 0x9f, -1, -1, -1, -1, // movq rbx, [PP + cache_offs]
|
|
0xff, 0xd1, // call rcx
|
|
};
|
|
ASSERT(ARRAY_SIZE(pattern) == kCallPatternSize);
|
|
return MatchesPattern(start_, pattern, kCallPatternSize);
|
|
}
|
|
|
|
intptr_t data_index() const { return IndexFromPPLoad(start_ + 15); }
|
|
intptr_t target_index() const { return IndexFromPPLoad(start_ + 3); }
|
|
|
|
RawObject* data() const { return object_pool_.ObjectAt(data_index()); }
|
|
RawCode* target() const {
|
|
return reinterpret_cast<RawCode*>(object_pool_.ObjectAt(target_index()));
|
|
}
|
|
|
|
void SetData(const Object& data) const {
|
|
ASSERT(!Object::Handle(object_pool_.ObjectAt(data_index())).IsCode());
|
|
object_pool_.SetObjectAt(data_index(), data);
|
|
// No need to flush the instruction cache, since the code is not modified.
|
|
}
|
|
|
|
void SetTarget(const Code& target) const {
|
|
ASSERT(Object::Handle(object_pool_.ObjectAt(target_index())).IsCode());
|
|
object_pool_.SetObjectAt(target_index(), target);
|
|
// No need to flush the instruction cache, since the code is not modified.
|
|
}
|
|
|
|
protected:
|
|
uword start_;
|
|
const ObjectPool& object_pool_;
|
|
|
|
private:
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(SwitchableCall);
|
|
};
|
|
|
|
RawCode* CodePatcher::GetStaticCallTargetAt(uword return_address,
|
|
const Code& code) {
|
|
ASSERT(code.ContainsInstructionAt(return_address));
|
|
PoolPointerCall call(return_address, code);
|
|
return call.Target();
|
|
}
|
|
|
|
void CodePatcher::PatchStaticCallAt(uword return_address,
|
|
const Code& code,
|
|
const Code& new_target) {
|
|
PatchPoolPointerCallAt(return_address, code, new_target);
|
|
}
|
|
|
|
void CodePatcher::PatchPoolPointerCallAt(uword return_address,
|
|
const Code& code,
|
|
const Code& new_target) {
|
|
ASSERT(code.ContainsInstructionAt(return_address));
|
|
PoolPointerCall call(return_address, code);
|
|
call.SetTarget(new_target);
|
|
}
|
|
|
|
RawCode* CodePatcher::GetInstanceCallAt(uword return_address,
|
|
const Code& code,
|
|
ICData* ic_data) {
|
|
ASSERT(code.ContainsInstructionAt(return_address));
|
|
InstanceCall call(return_address, code);
|
|
if (ic_data != NULL) {
|
|
*ic_data ^= call.ic_data();
|
|
}
|
|
return call.target();
|
|
}
|
|
|
|
intptr_t CodePatcher::InstanceCallSizeInBytes() {
|
|
return InstanceCall::kCallPatternSize;
|
|
}
|
|
|
|
void CodePatcher::InsertDeoptimizationCallAt(uword start) {
|
|
UNREACHABLE();
|
|
}
|
|
|
|
RawFunction* CodePatcher::GetUnoptimizedStaticCallAt(uword return_address,
|
|
const Code& code,
|
|
ICData* ic_data_result) {
|
|
ASSERT(code.ContainsInstructionAt(return_address));
|
|
UnoptimizedStaticCall static_call(return_address, code);
|
|
ICData& ic_data = ICData::Handle();
|
|
ic_data ^= static_call.ic_data();
|
|
if (ic_data_result != NULL) {
|
|
*ic_data_result = ic_data.raw();
|
|
}
|
|
return ic_data.GetTargetAt(0);
|
|
}
|
|
|
|
void CodePatcher::PatchSwitchableCallAt(uword return_address,
|
|
const Code& caller_code,
|
|
const Object& data,
|
|
const Code& target) {
|
|
ASSERT(caller_code.ContainsInstructionAt(return_address));
|
|
SwitchableCall call(return_address, caller_code);
|
|
call.SetData(data);
|
|
call.SetTarget(target);
|
|
}
|
|
|
|
RawCode* CodePatcher::GetSwitchableCallTargetAt(uword return_address,
|
|
const Code& caller_code) {
|
|
ASSERT(caller_code.ContainsInstructionAt(return_address));
|
|
SwitchableCall call(return_address, caller_code);
|
|
return call.target();
|
|
}
|
|
|
|
RawObject* CodePatcher::GetSwitchableCallDataAt(uword return_address,
|
|
const Code& caller_code) {
|
|
ASSERT(caller_code.ContainsInstructionAt(return_address));
|
|
SwitchableCall call(return_address, caller_code);
|
|
return call.data();
|
|
}
|
|
|
|
void CodePatcher::PatchNativeCallAt(uword return_address,
|
|
const Code& code,
|
|
NativeFunction target,
|
|
const Code& trampoline) {
|
|
ASSERT(code.ContainsInstructionAt(return_address));
|
|
NativeCall call(return_address, code);
|
|
call.set_target(trampoline);
|
|
call.set_native_function(target);
|
|
}
|
|
|
|
RawCode* CodePatcher::GetNativeCallAt(uword return_address,
|
|
const Code& code,
|
|
NativeFunction* target) {
|
|
ASSERT(code.ContainsInstructionAt(return_address));
|
|
NativeCall call(return_address, code);
|
|
*target = call.native_function();
|
|
return call.target();
|
|
}
|
|
|
|
} // namespace dart
|
|
|
|
#endif // defined TARGET_ARCH_X64
|