[compiler] Extend the functionality of CodeStubAssembler
After this change, the functionality of the CodeStubAssembler should be sufficient to generate non-trivial stubs (e.g. the KeyedLoadIC) with control flow, variables and probing of internal meta data structures. Specifically this patch: * introduces a Label class, which allows stubs to construct graphs that don't have linear control graphs. * introduces a Variable class. Variables can be bound to Node* values at different points in a non-linear control flow graph. In conjunction with the Label machinery, the CodeStubAssembler ensures that Phi nodes are inserted at the "minimal" set of merge points. * adds Tail calling support to other Stubs and to any arbitrary code whose interface can be described by a CallInterfaceDescriptor. * provides new macros for accessing FixedArray elements that are optimized for use with Smi values. Review URL: https://codereview.chromium.org/1649723002 Cr-Commit-Position: refs/heads/master@{#33664}
This commit is contained in:
parent
df1964a08e
commit
1f28904b24
@ -24,7 +24,6 @@ namespace v8 {
|
||||
namespace internal {
|
||||
namespace compiler {
|
||||
|
||||
|
||||
CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone,
|
||||
const CallInterfaceDescriptor& descriptor,
|
||||
Code::Flags flags, const char* name)
|
||||
@ -34,8 +33,8 @@ CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone,
|
||||
CallDescriptor::kNoFlags))),
|
||||
flags_(flags),
|
||||
name_(name),
|
||||
code_generated_(false) {}
|
||||
|
||||
code_generated_(false),
|
||||
variables_(zone) {}
|
||||
|
||||
CodeStubAssembler::~CodeStubAssembler() {}
|
||||
|
||||
@ -77,6 +76,9 @@ Node* CodeStubAssembler::BooleanConstant(bool value) {
|
||||
return raw_assembler_->BooleanConstant(value);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::ExternalConstant(ExternalReference address) {
|
||||
return raw_assembler_->ExternalConstant(address);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::Parameter(int value) {
|
||||
return raw_assembler_->Parameter(value);
|
||||
@ -87,6 +89,13 @@ void CodeStubAssembler::Return(Node* value) {
|
||||
return raw_assembler_->Return(value);
|
||||
}
|
||||
|
||||
void CodeStubAssembler::Bind(CodeStubAssembler::Label* label) {
|
||||
return label->Bind();
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::LoadFramePointer() {
|
||||
return raw_assembler_->LoadFramePointer();
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::SmiShiftBitsConstant() {
|
||||
return Int32Constant(kSmiShiftSize + kSmiTagSize);
|
||||
@ -102,27 +111,73 @@ Node* CodeStubAssembler::SmiUntag(Node* value) {
|
||||
return raw_assembler_->WordSar(value, SmiShiftBitsConstant());
|
||||
}
|
||||
|
||||
|
||||
Node* CodeStubAssembler::IntPtrAdd(Node* a, Node* b) {
|
||||
return raw_assembler_->IntPtrAdd(a, b);
|
||||
}
|
||||
|
||||
|
||||
Node* CodeStubAssembler::IntPtrSub(Node* a, Node* b) {
|
||||
return raw_assembler_->IntPtrSub(a, b);
|
||||
}
|
||||
|
||||
#define DEFINE_CODE_STUB_ASSEMBER_BINARY_OP(name) \
|
||||
Node* CodeStubAssembler::name(Node* a, Node* b) { \
|
||||
return raw_assembler_->name(a, b); \
|
||||
}
|
||||
CODE_STUB_ASSEMBLER_BINARY_OP_LIST(DEFINE_CODE_STUB_ASSEMBER_BINARY_OP)
|
||||
#undef DEFINE_CODE_STUB_ASSEMBER_BINARY_OP
|
||||
|
||||
Node* CodeStubAssembler::WordShl(Node* value, int shift) {
|
||||
return raw_assembler_->WordShl(value, Int32Constant(shift));
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::WordIsSmi(Node* a) {
|
||||
return WordEqual(raw_assembler_->WordAnd(a, Int32Constant(kSmiTagMask)),
|
||||
Int32Constant(0));
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::LoadBufferObject(Node* buffer, int offset) {
|
||||
return raw_assembler_->Load(MachineType::AnyTagged(), buffer,
|
||||
IntPtrConstant(offset));
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::LoadObjectField(Node* object, int offset) {
|
||||
return raw_assembler_->Load(MachineType::AnyTagged(), object,
|
||||
IntPtrConstant(offset - kHeapObjectTag));
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::LoadFixedArrayElementSmiIndex(Node* object,
|
||||
Node* smi_index,
|
||||
int additional_offset) {
|
||||
Node* header_size = raw_assembler_->Int32Constant(
|
||||
additional_offset + FixedArray::kHeaderSize - kHeapObjectTag);
|
||||
Node* scaled_index =
|
||||
(kSmiShiftSize == 0)
|
||||
? raw_assembler_->Word32Shl(
|
||||
smi_index, Int32Constant(kPointerSizeLog2 - kSmiTagSize))
|
||||
: raw_assembler_->Word32Shl(SmiUntag(smi_index),
|
||||
Int32Constant(kPointerSizeLog2));
|
||||
Node* offset = raw_assembler_->Int32Add(scaled_index, header_size);
|
||||
return raw_assembler_->Load(MachineType::AnyTagged(), object, offset);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::LoadFixedArrayElementConstantIndex(Node* object,
|
||||
int index) {
|
||||
Node* offset = raw_assembler_->Int32Constant(
|
||||
FixedArray::kHeaderSize - kHeapObjectTag + index * kPointerSize);
|
||||
return raw_assembler_->Load(MachineType::AnyTagged(), object, offset);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::LoadRoot(Heap::RootListIndex root_index) {
|
||||
if (isolate()->heap()->RootCanBeTreatedAsConstant(root_index)) {
|
||||
Handle<Object> root = isolate()->heap()->root_handle(root_index);
|
||||
if (root->IsSmi()) {
|
||||
return Int32Constant(Handle<Smi>::cast(root)->value());
|
||||
} else {
|
||||
return HeapConstant(Handle<HeapObject>::cast(root));
|
||||
}
|
||||
}
|
||||
|
||||
compiler::Node* roots_array_start =
|
||||
ExternalConstant(ExternalReference::roots_array_start(isolate()));
|
||||
USE(roots_array_start);
|
||||
|
||||
// TODO(danno): Implement thee root-access case where the root is not constant
|
||||
// and must be loaded from the root array.
|
||||
UNIMPLEMENTED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::CallN(CallDescriptor* descriptor, Node* code_target,
|
||||
Node** args) {
|
||||
@ -147,19 +202,77 @@ Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id,
|
||||
return raw_assembler_->CallRuntime2(function_id, arg1, arg2, context);
|
||||
}
|
||||
|
||||
|
||||
Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
|
||||
Node* context, Node* arg1) {
|
||||
return raw_assembler_->TailCallRuntime1(function_id, arg1, context);
|
||||
}
|
||||
|
||||
|
||||
Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
|
||||
Node* context, Node* arg1,
|
||||
Node* arg2) {
|
||||
return raw_assembler_->TailCallRuntime2(function_id, arg1, arg2, context);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
|
||||
Node* context, Node* arg1, Node* arg2,
|
||||
Node* arg3) {
|
||||
return raw_assembler_->TailCallRuntime3(function_id, arg1, arg2, arg3,
|
||||
context);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id,
|
||||
Node* context, Node* arg1, Node* arg2,
|
||||
Node* arg3, Node* arg4) {
|
||||
return raw_assembler_->TailCallRuntime4(function_id, arg1, arg2, arg3, arg4,
|
||||
context);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::TailCallStub(CodeStub& stub, Node** args) {
|
||||
Node* code_target = HeapConstant(stub.GetCode());
|
||||
CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
|
||||
isolate(), zone(), stub.GetCallInterfaceDescriptor(),
|
||||
stub.GetStackParameterCount(), CallDescriptor::kSupportsTailCalls);
|
||||
return raw_assembler_->TailCallN(descriptor, code_target, args);
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::TailCall(
|
||||
const CallInterfaceDescriptor& interface_descriptor, Node* code_target,
|
||||
Node** args) {
|
||||
CallDescriptor* descriptor = Linkage::GetStubCallDescriptor(
|
||||
isolate(), zone(), interface_descriptor,
|
||||
interface_descriptor.GetStackParameterCount(),
|
||||
CallDescriptor::kSupportsTailCalls);
|
||||
return raw_assembler_->TailCallN(descriptor, code_target, args);
|
||||
}
|
||||
|
||||
void CodeStubAssembler::Goto(CodeStubAssembler::Label* label) {
|
||||
label->MergeVariables();
|
||||
raw_assembler_->Goto(label->label_);
|
||||
}
|
||||
|
||||
void CodeStubAssembler::Branch(Node* condition,
|
||||
CodeStubAssembler::Label* true_label,
|
||||
CodeStubAssembler::Label* false_label) {
|
||||
true_label->MergeVariables();
|
||||
false_label->MergeVariables();
|
||||
return raw_assembler_->Branch(condition, true_label->label_,
|
||||
false_label->label_);
|
||||
}
|
||||
|
||||
void CodeStubAssembler::Switch(Node* index, Label* default_label,
|
||||
int32_t* case_values, Label** case_labels,
|
||||
size_t case_count) {
|
||||
RawMachineLabel** labels =
|
||||
new (zone()->New(sizeof(RawMachineLabel*) * case_count))
|
||||
RawMachineLabel*[case_count];
|
||||
for (size_t i = 0; i < case_count; ++i) {
|
||||
labels[i] = case_labels[i]->label_;
|
||||
case_labels[i]->MergeVariables();
|
||||
default_label->MergeVariables();
|
||||
}
|
||||
return raw_assembler_->Switch(index, default_label->label_, case_values,
|
||||
labels, case_count);
|
||||
}
|
||||
|
||||
// RawMachineAssembler delegate helpers:
|
||||
Isolate* CodeStubAssembler::isolate() { return raw_assembler_->isolate(); }
|
||||
@ -170,6 +283,159 @@ Graph* CodeStubAssembler::graph() { return raw_assembler_->graph(); }
|
||||
|
||||
Zone* CodeStubAssembler::zone() { return raw_assembler_->zone(); }
|
||||
|
||||
// The core implementation of Variable is stored through an indirection so
|
||||
// that it can outlive the often block-scoped Variable declarations. This is
|
||||
// needed to ensure that variable binding and merging through phis can
|
||||
// properly be verified.
|
||||
class CodeStubAssembler::Variable::Impl : public ZoneObject {
|
||||
public:
|
||||
explicit Impl(MachineRepresentation rep) : value_(nullptr), rep_(rep) {}
|
||||
Node* value_;
|
||||
MachineRepresentation rep_;
|
||||
};
|
||||
|
||||
CodeStubAssembler::Variable::Variable(CodeStubAssembler* assembler,
|
||||
MachineRepresentation rep)
|
||||
: impl_(new (assembler->zone()) Impl(rep)) {
|
||||
assembler->variables_.push_back(impl_);
|
||||
}
|
||||
|
||||
void CodeStubAssembler::Variable::Bind(Node* value) { impl_->value_ = value; }
|
||||
|
||||
Node* CodeStubAssembler::Variable::value() const {
|
||||
DCHECK_NOT_NULL(impl_->value_);
|
||||
return impl_->value_;
|
||||
}
|
||||
|
||||
MachineRepresentation CodeStubAssembler::Variable::rep() const {
|
||||
return impl_->rep_;
|
||||
}
|
||||
|
||||
bool CodeStubAssembler::Variable::IsBound() const {
|
||||
return impl_->value_ != nullptr;
|
||||
}
|
||||
|
||||
CodeStubAssembler::Label::Label(CodeStubAssembler* assembler)
|
||||
: bound_(false), merge_count_(0), assembler_(assembler), label_(nullptr) {
|
||||
void* buffer = assembler->zone()->New(sizeof(RawMachineLabel));
|
||||
label_ = new (buffer) RawMachineLabel();
|
||||
}
|
||||
|
||||
CodeStubAssembler::Label::Label(CodeStubAssembler* assembler,
|
||||
int merged_value_count,
|
||||
CodeStubAssembler::Variable** merged_variables)
|
||||
: bound_(false), merge_count_(0), assembler_(assembler), label_(nullptr) {
|
||||
void* buffer = assembler->zone()->New(sizeof(RawMachineLabel));
|
||||
label_ = new (buffer) RawMachineLabel();
|
||||
for (int i = 0; i < merged_value_count; ++i) {
|
||||
variable_phis_[merged_variables[i]->impl_] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
CodeStubAssembler::Label::Label(CodeStubAssembler* assembler,
|
||||
CodeStubAssembler::Variable* merged_variable)
|
||||
: CodeStubAssembler::Label(assembler, 1, &merged_variable) {}
|
||||
|
||||
void CodeStubAssembler::Label::MergeVariables() {
|
||||
++merge_count_;
|
||||
for (auto var : assembler_->variables_) {
|
||||
size_t count = 0;
|
||||
Node* node = var->value_;
|
||||
if (node != nullptr) {
|
||||
auto i = variable_merges_.find(var);
|
||||
if (i != variable_merges_.end()) {
|
||||
i->second.push_back(node);
|
||||
count = i->second.size();
|
||||
} else {
|
||||
count = 1;
|
||||
variable_merges_[var] = std::vector<Node*>(1, node);
|
||||
}
|
||||
}
|
||||
// If the following asserts, then you've jumped to a label without a bound
|
||||
// variable along that path that expects to merge its value into a phi.
|
||||
DCHECK(variable_phis_.find(var) == variable_phis_.end() ||
|
||||
count == merge_count_);
|
||||
USE(count);
|
||||
|
||||
// If the label is already bound, we already know the set of variables to
|
||||
// merge and phi nodes have already been created.
|
||||
if (bound_) {
|
||||
auto phi = variable_phis_.find(var);
|
||||
if (phi != variable_phis_.end()) {
|
||||
DCHECK_NOT_NULL(phi->second);
|
||||
assembler_->raw_assembler_->AppendPhiInput(phi->second, node);
|
||||
} else {
|
||||
auto i = variable_merges_.find(var);
|
||||
USE(i);
|
||||
// If the following assert fires, then you've declared a variable that
|
||||
// has the same bound value along all paths up until the point you bound
|
||||
// this label, but then later merged a path with a new value for the
|
||||
// variable after the label bind (it's not possible to add phis to the
|
||||
// bound label after the fact, just make sure to list the variable in
|
||||
// the label's constructor's list of merged variables).
|
||||
DCHECK(find_if(i->second.begin(), i->second.end(),
|
||||
[node](Node* e) -> bool { return node != e; }) ==
|
||||
i->second.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CodeStubAssembler::Label::Bind() {
|
||||
DCHECK(!bound_);
|
||||
assembler_->raw_assembler_->Bind(label_);
|
||||
|
||||
// Make sure that all variables that have changed along any path up to this
|
||||
// point are marked as merge variables.
|
||||
for (auto var : assembler_->variables_) {
|
||||
Node* shared_value = nullptr;
|
||||
auto i = variable_merges_.find(var);
|
||||
if (i != variable_merges_.end()) {
|
||||
for (auto value : i->second) {
|
||||
DCHECK(value != nullptr);
|
||||
if (value != shared_value) {
|
||||
if (shared_value == nullptr) {
|
||||
shared_value = value;
|
||||
} else {
|
||||
variable_phis_[var] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto var : variable_phis_) {
|
||||
CodeStubAssembler::Variable::Impl* var_impl = var.first;
|
||||
auto i = variable_merges_.find(var_impl);
|
||||
// If the following assert fires, then a variable that has been marked as
|
||||
// being merged at the label--either by explicitly marking it so in the
|
||||
// label constructor or by having seen different bound values at branches
|
||||
// into the label--doesn't have a bound value along all of the paths that
|
||||
// have been merged into the label up to this point.
|
||||
DCHECK(i != variable_merges_.end() && i->second.size() == merge_count_);
|
||||
Node* phi = assembler_->raw_assembler_->Phi(
|
||||
var.first->rep_, static_cast<int>(merge_count_), &(i->second[0]));
|
||||
variable_phis_[var_impl] = phi;
|
||||
}
|
||||
|
||||
// Bind all variables to a merge phi, the common value along all paths or
|
||||
// null.
|
||||
for (auto var : assembler_->variables_) {
|
||||
auto i = variable_phis_.find(var);
|
||||
if (i != variable_phis_.end()) {
|
||||
var->value_ = i->second;
|
||||
} else {
|
||||
auto j = variable_merges_.find(var);
|
||||
if (j != variable_merges_.end() && j->second.size() == merge_count_) {
|
||||
var->value_ = j->second.back();
|
||||
} else {
|
||||
var->value_ = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bound_ = true;
|
||||
}
|
||||
|
||||
} // namespace compiler
|
||||
} // namespace internal
|
||||
|
@ -5,11 +5,16 @@
|
||||
#ifndef V8_COMPILER_CODE_STUB_ASSEMBLER_H_
|
||||
#define V8_COMPILER_CODE_STUB_ASSEMBLER_H_
|
||||
|
||||
#include <map>
|
||||
|
||||
// Clients of this interface shouldn't depend on lots of compiler internals.
|
||||
// Do not include anything from src/compiler here!
|
||||
#include "src/allocation.h"
|
||||
#include "src/builtins.h"
|
||||
#include "src/heap/heap.h"
|
||||
#include "src/machine-type.h"
|
||||
#include "src/runtime/runtime.h"
|
||||
#include "src/zone-containers.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
@ -25,8 +30,41 @@ class Graph;
|
||||
class Node;
|
||||
class Operator;
|
||||
class RawMachineAssembler;
|
||||
class RawMachineLabel;
|
||||
class Schedule;
|
||||
|
||||
#define CODE_STUB_ASSEMBLER_BINARY_OP_LIST(V) \
|
||||
V(IntPtrAdd) \
|
||||
V(IntPtrSub) \
|
||||
V(Int32Add) \
|
||||
V(Int32Sub) \
|
||||
V(Int32Mul) \
|
||||
V(WordEqual) \
|
||||
V(WordNotEqual) \
|
||||
V(WordOr) \
|
||||
V(WordAnd) \
|
||||
V(WordXor) \
|
||||
V(WordShl) \
|
||||
V(WordShr) \
|
||||
V(WordSar) \
|
||||
V(WordRor) \
|
||||
V(Word32Equal) \
|
||||
V(Word32NotEqual) \
|
||||
V(Word32Or) \
|
||||
V(Word32And) \
|
||||
V(Word32Xor) \
|
||||
V(Word32Shr) \
|
||||
V(Word32Sar) \
|
||||
V(Word32Ror) \
|
||||
V(Word64Equal) \
|
||||
V(Word64NotEqual) \
|
||||
V(Word64Or) \
|
||||
V(Word64And) \
|
||||
V(Word64Xor) \
|
||||
V(Word64Shr) \
|
||||
V(Word64Sar) \
|
||||
V(Word64Ror)
|
||||
|
||||
class CodeStubAssembler {
|
||||
public:
|
||||
CodeStubAssembler(Isolate* isolate, Zone* zone,
|
||||
@ -36,29 +74,55 @@ class CodeStubAssembler {
|
||||
|
||||
Handle<Code> GenerateCode();
|
||||
|
||||
class Label;
|
||||
class Variable {
|
||||
public:
|
||||
explicit Variable(CodeStubAssembler* assembler, MachineRepresentation rep);
|
||||
void Bind(Node* value);
|
||||
Node* value() const;
|
||||
MachineRepresentation rep() const;
|
||||
bool IsBound() const;
|
||||
|
||||
private:
|
||||
friend class CodeStubAssembler;
|
||||
class Impl;
|
||||
Impl* impl_;
|
||||
};
|
||||
|
||||
// ===========================================================================
|
||||
// Base Assembler
|
||||
// ===========================================================================
|
||||
|
||||
// Constants.
|
||||
Node* Int32Constant(int value);
|
||||
Node* IntPtrConstant(intptr_t value);
|
||||
Node* NumberConstant(double value);
|
||||
Node* HeapConstant(Handle<HeapObject> object);
|
||||
Node* BooleanConstant(bool value);
|
||||
Node* ExternalConstant(ExternalReference address);
|
||||
|
||||
Node* Parameter(int value);
|
||||
void Return(Node* value);
|
||||
|
||||
// Tag and untag Smi values.
|
||||
Node* SmiTag(Node* value);
|
||||
Node* SmiUntag(Node* value);
|
||||
void Bind(Label* label);
|
||||
void Goto(Label* label);
|
||||
void Branch(Node* condition, Label* true_label, Label* false_label);
|
||||
|
||||
void Switch(Node* index, Label* default_label, int32_t* case_values,
|
||||
Label** case_labels, size_t case_count);
|
||||
|
||||
// Access to the frame pointer
|
||||
Node* LoadFramePointer();
|
||||
Node* LoadParentFramePointer();
|
||||
|
||||
// Basic arithmetic operations.
|
||||
#define DECLARE_CODE_STUB_ASSEMBER_BINARY_OP(name) Node* name(Node* a, Node* b);
|
||||
CODE_STUB_ASSEMBLER_BINARY_OP_LIST(DECLARE_CODE_STUB_ASSEMBER_BINARY_OP)
|
||||
#undef DECLARE_CODE_STUB_ASSEMBER_BINARY_OP
|
||||
|
||||
// Basic arithmetic operations.
|
||||
Node* IntPtrAdd(Node* a, Node* b);
|
||||
Node* IntPtrSub(Node* a, Node* b);
|
||||
Node* WordShl(Node* value, int shift);
|
||||
|
||||
// Load a field from an object on the heap.
|
||||
Node* LoadObjectField(Node* object, int offset);
|
||||
|
||||
// Call runtime function.
|
||||
// Calls
|
||||
Node* CallRuntime(Runtime::FunctionId function_id, Node* context, Node* arg1);
|
||||
Node* CallRuntime(Runtime::FunctionId function_id, Node* context, Node* arg1,
|
||||
Node* arg2);
|
||||
@ -67,6 +131,38 @@ class CodeStubAssembler {
|
||||
Node* arg1);
|
||||
Node* TailCallRuntime(Runtime::FunctionId function_id, Node* context,
|
||||
Node* arg1, Node* arg2);
|
||||
Node* TailCallRuntime(Runtime::FunctionId function_id, Node* context,
|
||||
Node* arg1, Node* arg2, Node* arg3);
|
||||
Node* TailCallRuntime(Runtime::FunctionId function_id, Node* context,
|
||||
Node* arg1, Node* arg2, Node* arg3, Node* arg4);
|
||||
|
||||
Node* TailCallStub(CodeStub& stub, Node** args);
|
||||
Node* TailCall(const CallInterfaceDescriptor& descriptor, Node* target,
|
||||
Node** args);
|
||||
|
||||
// ===========================================================================
|
||||
// Macros
|
||||
// ===========================================================================
|
||||
|
||||
// Tag and untag Smi values.
|
||||
Node* SmiTag(Node* value);
|
||||
Node* SmiUntag(Node* value);
|
||||
|
||||
// Load a value from the root array.
|
||||
Node* LoadRoot(Heap::RootListIndex root_index);
|
||||
|
||||
// Check a value for smi-ness
|
||||
Node* WordIsSmi(Node* a);
|
||||
|
||||
// Load an object pointer from a buffer that isn't in the heap.
|
||||
Node* LoadBufferObject(Node* buffer, int offset);
|
||||
// Load a field from an object on the heap.
|
||||
Node* LoadObjectField(Node* object, int offset);
|
||||
|
||||
// Load an array element from a FixedArray.
|
||||
Node* LoadFixedArrayElementSmiIndex(Node* object, Node* smi_index,
|
||||
int additional_offset = 0);
|
||||
Node* LoadFixedArrayElementConstantIndex(Node* object, int index);
|
||||
|
||||
private:
|
||||
friend class CodeStubAssemblerTester;
|
||||
@ -85,10 +181,38 @@ class CodeStubAssembler {
|
||||
Code::Flags flags_;
|
||||
const char* name_;
|
||||
bool code_generated_;
|
||||
ZoneVector<Variable::Impl*> variables_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CodeStubAssembler);
|
||||
};
|
||||
|
||||
class CodeStubAssembler::Label {
|
||||
public:
|
||||
explicit Label(CodeStubAssembler* assembler);
|
||||
Label(CodeStubAssembler* assembler, int merged_variable_count,
|
||||
CodeStubAssembler::Variable** merged_variables);
|
||||
Label(CodeStubAssembler* assembler,
|
||||
CodeStubAssembler::Variable* merged_variable);
|
||||
~Label() {}
|
||||
|
||||
private:
|
||||
friend class CodeStubAssembler;
|
||||
|
||||
void Bind();
|
||||
void MergeVariables();
|
||||
|
||||
bool bound_;
|
||||
size_t merge_count_;
|
||||
CodeStubAssembler* assembler_;
|
||||
RawMachineLabel* label_;
|
||||
// Map of variables that need to be merged to their phi nodes (or placeholders
|
||||
// for those phis).
|
||||
std::map<Variable::Impl*, Node*> variable_phis_;
|
||||
// Map of variables to the list of value nodes that have been added from each
|
||||
// merge path in their order of merging.
|
||||
std::map<Variable::Impl*, std::vector<Node*>> variable_merges_;
|
||||
};
|
||||
|
||||
} // namespace compiler
|
||||
} // namespace internal
|
||||
} // namespace v8
|
||||
|
@ -42,17 +42,15 @@ void Graph::RemoveDecorator(GraphDecorator* decorator) {
|
||||
decorators_.erase(it);
|
||||
}
|
||||
|
||||
|
||||
Node* Graph::NewNode(const Operator* op, int input_count, Node** inputs,
|
||||
Node* Graph::NewNode(const Operator* op, int input_count, Node* const* inputs,
|
||||
bool incomplete) {
|
||||
Node* node = NewNodeUnchecked(op, input_count, inputs, incomplete);
|
||||
Verifier::VerifyNode(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
Node* Graph::NewNodeUnchecked(const Operator* op, int input_count,
|
||||
Node** inputs, bool incomplete) {
|
||||
Node* const* inputs, bool incomplete) {
|
||||
Node* const node =
|
||||
Node::New(zone(), NextNodeId(), op, input_count, inputs, incomplete);
|
||||
Decorate(node);
|
||||
|
@ -34,16 +34,16 @@ class Graph : public ZoneObject {
|
||||
explicit Graph(Zone* zone);
|
||||
|
||||
// Base implementation used by all factory methods.
|
||||
Node* NewNodeUnchecked(const Operator* op, int input_count, Node** inputs,
|
||||
bool incomplete = false);
|
||||
Node* NewNodeUnchecked(const Operator* op, int input_count,
|
||||
Node* const* inputs, bool incomplete = false);
|
||||
|
||||
// Factory that checks the input count.
|
||||
Node* NewNode(const Operator* op, int input_count, Node** inputs,
|
||||
Node* NewNode(const Operator* op, int input_count, Node* const* inputs,
|
||||
bool incomplete = false);
|
||||
|
||||
// Factories for nodes with static input counts.
|
||||
Node* NewNode(const Operator* op) {
|
||||
return NewNode(op, 0, static_cast<Node**>(nullptr));
|
||||
return NewNode(op, 0, static_cast<Node* const*>(nullptr));
|
||||
}
|
||||
Node* NewNode(const Operator* op, Node* n1) { return NewNode(op, 1, &n1); }
|
||||
Node* NewNode(const Operator* op, Node* n1, Node* n2) {
|
||||
|
@ -63,7 +63,7 @@ void RawMachineAssembler::Goto(RawMachineLabel* label) {
|
||||
void RawMachineAssembler::Branch(Node* condition, RawMachineLabel* true_val,
|
||||
RawMachineLabel* false_val) {
|
||||
DCHECK(current_block_ != schedule()->end());
|
||||
Node* branch = AddNode(common()->Branch(), condition);
|
||||
Node* branch = MakeNode(common()->Branch(), 1, &condition);
|
||||
schedule()->AddBranch(CurrentBlock(), branch, Use(true_val), Use(false_val));
|
||||
current_block_ = nullptr;
|
||||
}
|
||||
@ -281,6 +281,51 @@ Node* RawMachineAssembler::TailCallRuntime2(Runtime::FunctionId function,
|
||||
return tail_call;
|
||||
}
|
||||
|
||||
Node* RawMachineAssembler::TailCallRuntime3(Runtime::FunctionId function,
|
||||
Node* arg1, Node* arg2, Node* arg3,
|
||||
Node* context) {
|
||||
const int kArity = 3;
|
||||
CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor(
|
||||
zone(), function, kArity, Operator::kNoProperties,
|
||||
CallDescriptor::kSupportsTailCalls);
|
||||
int return_count = static_cast<int>(desc->ReturnCount());
|
||||
|
||||
Node* centry = HeapConstant(CEntryStub(isolate(), return_count).GetCode());
|
||||
Node* ref = AddNode(
|
||||
common()->ExternalConstant(ExternalReference(function, isolate())));
|
||||
Node* arity = Int32Constant(kArity);
|
||||
|
||||
Node* nodes[] = {centry, arg1, arg2, arg3, ref, arity, context};
|
||||
Node* tail_call = MakeNode(common()->TailCall(desc), arraysize(nodes), nodes);
|
||||
|
||||
NodeProperties::MergeControlToEnd(graph(), common(), tail_call);
|
||||
schedule()->AddTailCall(CurrentBlock(), tail_call);
|
||||
current_block_ = nullptr;
|
||||
return tail_call;
|
||||
}
|
||||
|
||||
Node* RawMachineAssembler::TailCallRuntime4(Runtime::FunctionId function,
|
||||
Node* arg1, Node* arg2, Node* arg3,
|
||||
Node* arg4, Node* context) {
|
||||
const int kArity = 4;
|
||||
CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor(
|
||||
zone(), function, kArity, Operator::kNoProperties,
|
||||
CallDescriptor::kSupportsTailCalls);
|
||||
int return_count = static_cast<int>(desc->ReturnCount());
|
||||
|
||||
Node* centry = HeapConstant(CEntryStub(isolate(), return_count).GetCode());
|
||||
Node* ref = AddNode(
|
||||
common()->ExternalConstant(ExternalReference(function, isolate())));
|
||||
Node* arity = Int32Constant(kArity);
|
||||
|
||||
Node* nodes[] = {centry, arg1, arg2, arg3, arg4, ref, arity, context};
|
||||
Node* tail_call = MakeNode(common()->TailCall(desc), arraysize(nodes), nodes);
|
||||
|
||||
NodeProperties::MergeControlToEnd(graph(), common(), tail_call);
|
||||
schedule()->AddTailCall(CurrentBlock(), tail_call);
|
||||
current_block_ = nullptr;
|
||||
return tail_call;
|
||||
}
|
||||
|
||||
Node* RawMachineAssembler::CallCFunction0(MachineType return_type,
|
||||
Node* function) {
|
||||
@ -369,9 +414,24 @@ BasicBlock* RawMachineAssembler::CurrentBlock() {
|
||||
return current_block_;
|
||||
}
|
||||
|
||||
Node* RawMachineAssembler::Phi(MachineRepresentation rep, int input_count,
|
||||
Node* const* inputs) {
|
||||
Node** buffer = new (zone()->New(sizeof(Node*) * (input_count + 1)))
|
||||
Node*[input_count + 1];
|
||||
std::copy(inputs, inputs + input_count, buffer);
|
||||
buffer[input_count] = graph()->start();
|
||||
return AddNode(common()->Phi(rep, input_count), input_count + 1, buffer);
|
||||
}
|
||||
|
||||
void RawMachineAssembler::AppendPhiInput(Node* phi, Node* new_input) {
|
||||
const Operator* op = phi->op();
|
||||
const Operator* new_op = common()->ResizeMergeOrPhi(op, phi->InputCount());
|
||||
phi->InsertInput(zone(), phi->InputCount() - 1, new_input);
|
||||
NodeProperties::ChangeOp(phi, new_op);
|
||||
}
|
||||
|
||||
Node* RawMachineAssembler::AddNode(const Operator* op, int input_count,
|
||||
Node** inputs) {
|
||||
Node* const* inputs) {
|
||||
DCHECK_NOT_NULL(schedule_);
|
||||
DCHECK_NOT_NULL(current_block_);
|
||||
Node* node = MakeNode(op, input_count, inputs);
|
||||
@ -379,9 +439,8 @@ Node* RawMachineAssembler::AddNode(const Operator* op, int input_count,
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
Node* RawMachineAssembler::MakeNode(const Operator* op, int input_count,
|
||||
Node** inputs) {
|
||||
Node* const* inputs) {
|
||||
// The raw machine assembler nodes do not have effect and control inputs,
|
||||
// so we disable checking input counts here.
|
||||
return graph()->NewNodeUnchecked(op, input_count, inputs);
|
||||
|
@ -604,7 +604,12 @@ class RawMachineAssembler {
|
||||
// Tail call to a runtime function with two arguments.
|
||||
Node* TailCallRuntime2(Runtime::FunctionId function, Node* arg1, Node* arg2,
|
||||
Node* context);
|
||||
|
||||
// Tail call to a runtime function with three arguments.
|
||||
Node* TailCallRuntime3(Runtime::FunctionId function, Node* arg1, Node* arg2,
|
||||
Node* arg3, Node* context);
|
||||
// Tail call to a runtime function with four arguments.
|
||||
Node* TailCallRuntime4(Runtime::FunctionId function, Node* arg1, Node* arg2,
|
||||
Node* arg3, Node* arg4, Node* context);
|
||||
|
||||
// ===========================================================================
|
||||
// The following utility methods deal with control flow, hence might switch
|
||||
@ -624,24 +629,26 @@ class RawMachineAssembler {
|
||||
|
||||
// Variables.
|
||||
Node* Phi(MachineRepresentation rep, Node* n1, Node* n2) {
|
||||
return AddNode(common()->Phi(rep, 2), n1, n2);
|
||||
return AddNode(common()->Phi(rep, 2), n1, n2, graph()->start());
|
||||
}
|
||||
Node* Phi(MachineRepresentation rep, Node* n1, Node* n2, Node* n3) {
|
||||
return AddNode(common()->Phi(rep, 3), n1, n2, n3);
|
||||
return AddNode(common()->Phi(rep, 3), n1, n2, n3, graph()->start());
|
||||
}
|
||||
Node* Phi(MachineRepresentation rep, Node* n1, Node* n2, Node* n3, Node* n4) {
|
||||
return AddNode(common()->Phi(rep, 4), n1, n2, n3, n4);
|
||||
return AddNode(common()->Phi(rep, 4), n1, n2, n3, n4, graph()->start());
|
||||
}
|
||||
Node* Phi(MachineRepresentation rep, int input_count, Node* const* inputs);
|
||||
void AppendPhiInput(Node* phi, Node* new_input);
|
||||
|
||||
// ===========================================================================
|
||||
// The following generic node creation methods can be used for operators that
|
||||
// are not covered by the above utility methods. There should rarely be a need
|
||||
// to do that outside of testing though.
|
||||
|
||||
Node* AddNode(const Operator* op, int input_count, Node** inputs);
|
||||
Node* AddNode(const Operator* op, int input_count, Node* const* inputs);
|
||||
|
||||
Node* AddNode(const Operator* op) {
|
||||
return AddNode(op, 0, static_cast<Node**>(nullptr));
|
||||
return AddNode(op, 0, static_cast<Node* const*>(nullptr));
|
||||
}
|
||||
|
||||
template <class... TArgs>
|
||||
@ -651,7 +658,7 @@ class RawMachineAssembler {
|
||||
}
|
||||
|
||||
private:
|
||||
Node* MakeNode(const Operator* op, int input_count, Node** inputs);
|
||||
Node* MakeNode(const Operator* op, int input_count, Node* const* inputs);
|
||||
BasicBlock* Use(RawMachineLabel* label);
|
||||
BasicBlock* EnsureBlock(RawMachineLabel* label);
|
||||
BasicBlock* CurrentBlock();
|
||||
|
@ -120,6 +120,133 @@ TEST(SimpleTailCallRuntime2Arg) {
|
||||
CHECK_EQ(16, Handle<Smi>::cast(result.ToHandleChecked())->value());
|
||||
}
|
||||
|
||||
TEST(VariableMerge1) {
|
||||
Isolate* isolate(CcTest::InitIsolateOnce());
|
||||
VoidDescriptor descriptor(isolate);
|
||||
CodeStubAssemblerTester m(isolate, descriptor);
|
||||
CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged);
|
||||
CodeStubAssembler::Label l1(&m), l2(&m), merge(&m);
|
||||
Node* temp = m.Int32Constant(0);
|
||||
var1.Bind(temp);
|
||||
m.Branch(m.Int32Constant(1), &l1, &l2);
|
||||
m.Bind(&l1);
|
||||
CHECK_EQ(var1.value(), temp);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&l2);
|
||||
CHECK_EQ(var1.value(), temp);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&merge);
|
||||
CHECK_EQ(var1.value(), temp);
|
||||
}
|
||||
|
||||
TEST(VariableMerge2) {
|
||||
Isolate* isolate(CcTest::InitIsolateOnce());
|
||||
VoidDescriptor descriptor(isolate);
|
||||
CodeStubAssemblerTester m(isolate, descriptor);
|
||||
CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged);
|
||||
CodeStubAssembler::Label l1(&m), l2(&m), merge(&m);
|
||||
Node* temp = m.Int32Constant(0);
|
||||
var1.Bind(temp);
|
||||
m.Branch(m.Int32Constant(1), &l1, &l2);
|
||||
m.Bind(&l1);
|
||||
CHECK_EQ(var1.value(), temp);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&l2);
|
||||
Node* temp2 = m.Int32Constant(2);
|
||||
var1.Bind(temp2);
|
||||
CHECK_EQ(var1.value(), temp2);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&merge);
|
||||
CHECK_NE(var1.value(), temp);
|
||||
}
|
||||
|
||||
TEST(VariableMerge3) {
|
||||
Isolate* isolate(CcTest::InitIsolateOnce());
|
||||
VoidDescriptor descriptor(isolate);
|
||||
CodeStubAssemblerTester m(isolate, descriptor);
|
||||
CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged);
|
||||
CodeStubAssembler::Variable var2(&m, MachineRepresentation::kTagged);
|
||||
CodeStubAssembler::Label l1(&m), l2(&m), merge(&m);
|
||||
Node* temp = m.Int32Constant(0);
|
||||
var1.Bind(temp);
|
||||
var2.Bind(temp);
|
||||
m.Branch(m.Int32Constant(1), &l1, &l2);
|
||||
m.Bind(&l1);
|
||||
CHECK_EQ(var1.value(), temp);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&l2);
|
||||
Node* temp2 = m.Int32Constant(2);
|
||||
var1.Bind(temp2);
|
||||
CHECK_EQ(var1.value(), temp2);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&merge);
|
||||
CHECK_NE(var1.value(), temp);
|
||||
CHECK_NE(var1.value(), temp2);
|
||||
CHECK_EQ(var2.value(), temp);
|
||||
}
|
||||
|
||||
TEST(VariableMergeBindFirst) {
|
||||
Isolate* isolate(CcTest::InitIsolateOnce());
|
||||
VoidDescriptor descriptor(isolate);
|
||||
CodeStubAssemblerTester m(isolate, descriptor);
|
||||
CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged);
|
||||
CodeStubAssembler::Label l1(&m), l2(&m), merge(&m, &var1), end(&m);
|
||||
Node* temp = m.Int32Constant(0);
|
||||
var1.Bind(temp);
|
||||
m.Branch(m.Int32Constant(1), &l1, &l2);
|
||||
m.Bind(&l1);
|
||||
CHECK_EQ(var1.value(), temp);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&merge);
|
||||
CHECK(var1.value() != temp);
|
||||
CHECK(var1.value() != nullptr);
|
||||
m.Goto(&end);
|
||||
m.Bind(&l2);
|
||||
Node* temp2 = m.Int32Constant(2);
|
||||
var1.Bind(temp2);
|
||||
CHECK_EQ(var1.value(), temp2);
|
||||
m.Goto(&merge);
|
||||
m.Bind(&end);
|
||||
CHECK(var1.value() != temp);
|
||||
CHECK(var1.value() != nullptr);
|
||||
}
|
||||
|
||||
TEST(VariableMergeSwitch) {
|
||||
Isolate* isolate(CcTest::InitIsolateOnce());
|
||||
VoidDescriptor descriptor(isolate);
|
||||
CodeStubAssemblerTester m(isolate, descriptor);
|
||||
CodeStubAssembler::Variable var1(&m, MachineRepresentation::kTagged);
|
||||
CodeStubAssembler::Label l1(&m), l2(&m), default_label(&m);
|
||||
CodeStubAssembler::Label* labels[] = {&l1, &l2};
|
||||
int32_t values[] = {1, 2};
|
||||
Node* temp = m.Int32Constant(0);
|
||||
var1.Bind(temp);
|
||||
m.Switch(m.Int32Constant(2), &default_label, values, labels, 2);
|
||||
m.Bind(&l1);
|
||||
DCHECK_EQ(temp, var1.value());
|
||||
m.Return(temp);
|
||||
m.Bind(&l2);
|
||||
DCHECK_EQ(temp, var1.value());
|
||||
m.Return(temp);
|
||||
m.Bind(&default_label);
|
||||
DCHECK_EQ(temp, var1.value());
|
||||
m.Return(temp);
|
||||
}
|
||||
|
||||
TEST(FixedArrayAccessSmiIndex) {
|
||||
Isolate* isolate(CcTest::InitIsolateOnce());
|
||||
VoidDescriptor descriptor(isolate);
|
||||
CodeStubAssemblerTester m(isolate, descriptor);
|
||||
Handle<FixedArray> array = isolate->factory()->NewFixedArray(5);
|
||||
array->set(4, Smi::FromInt(733));
|
||||
m.Return(m.LoadFixedArrayElementSmiIndex(m.HeapConstant(array),
|
||||
m.SmiTag(m.Int32Constant(4))));
|
||||
Handle<Code> code = m.GenerateCode();
|
||||
FunctionTester ft(descriptor, code);
|
||||
MaybeHandle<Object> result = ft.Call();
|
||||
CHECK_EQ(733, Handle<Smi>::cast(result.ToHandleChecked())->value());
|
||||
}
|
||||
|
||||
} // namespace compiler
|
||||
} // namespace internal
|
||||
} // namespace v8
|
||||
|
Loading…
Reference in New Issue
Block a user