Commit 1f28904b authored by danno's avatar danno Committed by Commit bot

[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}
parent df1964a0
This diff is collapsed.
......@@ -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);
// Basic arithmetic operations.
Node* IntPtrAdd(Node* a, Node* b);
Node* IntPtrSub(Node* a, Node* b);
Node* WordShl(Node* value, int shift);
void Switch(Node* index, Label* default_label, int32_t* case_values,
Label** case_labels, size_t case_count);
// Load a field from an object on the heap.
Node* LoadObjectField(Node* object, int offset);
// 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
Node* WordShl(Node* value, int shift);
// 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
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