#include "src/compiler/js-call-reducer.h"
#include <functional>
#include <optional>
#include "src/base/container-utils.h"
#include "src/base/small-vector.h"
#include "src/builtins/builtins-promise.h"
#include "src/builtins/builtins-utils.h"
#include "src/codegen/code-factory.h"
#include "src/codegen/tnode.h"
#include "src/compiler/access-builder.h"
#include "src/compiler/access-info.h"
#include "src/compiler/allocation-builder-inl.h"
#include "src/compiler/allocation-builder.h"
#include "src/compiler/common-operator.h"
#include "src/compiler/compilation-dependencies.h"
#include "src/compiler/fast-api-calls.h"
#include "src/compiler/feedback-source.h"
#include "src/compiler/graph-assembler.h"
#include "src/compiler/heap-refs.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/linkage.h"
#include "src/compiler/map-inference.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/opcodes.h"
#include "src/compiler/simplified-operator.h"
#include "src/compiler/state-values-utils.h"
#include "src/compiler/type-cache.h"
#include "src/compiler/use-info.h"
#include "src/flags/flags.h"
#include "src/ic/call-optimization.h"
#include "src/objects/elements-kind.h"
#include "src/objects/instance-type.h"
#include "src/objects/js-function.h"
#include "src/objects/objects-inl.h"
#include "src/objects/ordered-hash-table.h"
#include "src/utils/utils.h"
#ifdef V8_INTL_SUPPORT
#include "src/objects/intl-objects.h"
#endif
namespace v8 {
namespace internal {
namespace compiler {
#define _ [&]()
class JSCallReducerAssembler : public JSGraphAssembler {
static constexpr bool kMarkLoopExits = true;
public:
JSCallReducerAssembler(JSCallReducer* reducer, Node* node,
Node* effect = nullptr, Node* control = nullptr)
: JSGraphAssembler(
reducer->broker(), reducer->JSGraphForGraphAssembler(),
reducer->ZoneForGraphAssembler(), BranchSemantics::kJS,
[reducer](Node* n) { reducer->RevisitForGraphAssembler(n); },
kMarkLoopExits),
dependencies_(reducer->dependencies()),
node_(node) {
InitializeEffectControl(
effect ? effect : NodeProperties::GetEffectInput(node),
control ? control : NodeProperties::GetControlInput(node));
bool has_handler =
NodeProperties::IsExceptionalCall(node, &outermost_handler_);
outermost_catch_scope_.set_has_handler(has_handler);
}
TNode<Object> ReduceJSCallWithArrayLikeOrSpreadOfEmpty(
std::unordered_set<Node*>* generated_calls_with_array_like_or_spread);
TNode<Object> ReduceMathUnary(const Operator* op);
TNode<Object> ReduceMathBinary(const Operator* op);
TNode<String> ReduceStringPrototypeSubstring();
TNode<Boolean> ReduceStringPrototypeStartsWith();
TNode<Boolean> ReduceStringPrototypeStartsWith(
StringRef search_element_string);
TNode<Boolean> ReduceStringPrototypeEndsWith();
TNode<Boolean> ReduceStringPrototypeEndsWith(StringRef search_element_string);
TNode<String> ReduceStringPrototypeCharAt(SpeculationMode speculation_mode);
TNode<String> ReduceStringPrototypeCharAt(StringRef s, uint32_t index);
TNode<Number> ReduceStringPrototypeCharCodeAt(
SpeculationMode speculation_mode);
TNode<String> ReduceStringPrototypeSlice();
TNode<Object> ReduceJSCallMathMinMaxWithArrayLike(Builtin builtin);
TNode<Object> TargetInput() const { return JSCallNode{node_ptr()}.target(); }
template <typename T>
TNode<T> ReceiverInputAs() const {
return TNode<T>::UncheckedCast(JSCallNode{node_ptr()}.receiver());
}
TNode<Object> ReceiverInput() const { return ReceiverInputAs<Object>(); }
Node* node_ptr() const { return node_; }
TNode<Number> SpeculativeToNumber(
TNode<Object> value,
NumberOperationHint hint = NumberOperationHint::kNumberOrOddball);
TNode<Smi> CheckSmi(TNode<Object> value);
TNode<Number> CheckNumber(TNode<Object> value);
TNode<String> CheckString(TNode<Object> value);
TNode<Number> CheckBounds(TNode<Object> value, TNode<Number> limit,
CheckBoundsFlags flags = {});
TNode<Smi> TypeGuardUnsignedSmall(TNode<Object> value);
TNode<Object> TypeGuardNonInternal(TNode<Object> value);
TNode<Number> TypeGuardFixedArrayLength(TNode<Object> value);
TNode<Object> Call4(const Callable& callable, TNode<Context> context,
TNode<Object> arg0, TNode<Object> arg1,
TNode<Object> arg2, TNode<Object> arg3);
TNode<Object> JSCall3(TNode<Object> function, TNode<Object> this_arg,
TNode<Object> arg0, TNode<Object> arg1,
TNode<Object> arg2, FrameState frame_state);
TNode<Object> JSCall4(TNode<Object> function, TNode<Object> this_arg,
TNode<Object> arg0, TNode<Object> arg1,
TNode<Object> arg2, TNode<Object> arg3,
FrameState frame_state);
TNode<Object> CopyNode();
TNode<JSArray> CreateArrayNoThrow(TNode<Object> ctor, TNode<Number> size,
FrameState frame_state);
TNode<JSArray> AllocateEmptyJSArray(ElementsKind kind,
NativeContextRef native_context);
TNode<Number> NumberInc(TNode<Number> value) {
return NumberAdd(value, OneConstant());
}
TNode<Number> LoadMapElementsKind(TNode<Map> map);
template <typename T, typename U>
TNode<T> EnterMachineGraph(TNode<U> input, UseInfo use_info) {
return AddNode<T>(
graph()->NewNode(common()->EnterMachineGraph(use_info), input));
}
template <typename T, typename U>
TNode<T> ExitMachineGraph(TNode<U> input,
MachineRepresentation output_representation,
Type output_type) {
return AddNode<T>(graph()->NewNode(
common()->ExitMachineGraph(output_representation, output_type), input));
}
void MaybeInsertMapChecks(MapInference* inference,
bool has_stability_dependency) {
if (!has_stability_dependency) {
Effect e = effect();
inference->InsertMapChecks(jsgraph(), &e, Control{control()}, feedback());
InitializeEffectControl(e, control());
}
}
TNode<Object> ConvertHoleToUndefined(TNode<Object> value, ElementsKind kind) {
DCHECK(IsHoleyElementsKind(kind));
if (kind == HOLEY_DOUBLE_ELEMENTS) {
#ifdef V8_ENABLE_UNDEFINED_DOUBLE
return AddNode<Number>(graph()->NewNode(
simplified()->ChangeFloat64OrUndefinedOrHoleToTagged(), value));
#else
return AddNode<Number>(
graph()->NewNode(simplified()->ChangeFloat64HoleToTagged(), value));
#endif
}
return ConvertTaggedHoleToUndefined(value);
}
class TryCatchBuilder0 {
public:
using TryFunction = VoidGenerator0;
using CatchFunction = std::function<void(TNode<Object>)>;
TryCatchBuilder0(JSCallReducerAssembler* gasm, const TryFunction& try_body)
: gasm_(gasm), try_body_(try_body) {}
void Catch(const CatchFunction& catch_body) {
TNode<Object> handler_exception;
Effect handler_effect{nullptr};
Control handler_control{nullptr};
auto continuation = gasm_->MakeLabel();
{
CatchScope catch_scope = CatchScope::Inner(gasm_->temp_zone(), gasm_);
try_body_();
gasm_->Goto(&continuation);
catch_scope.MergeExceptionalPaths(&handler_exception, &handler_effect,
&handler_control);
}
{
gasm_->InitializeEffectControl(handler_effect, handler_control);
catch_body(handler_exception);
gasm_->Goto(&continuation);
}
gasm_->Bind(&continuation);
}
private:
JSCallReducerAssembler* const gasm_;
const VoidGenerator0 try_body_;
};
TryCatchBuilder0 Try(const VoidGenerator0& try_body) {
return {this, try_body};
}
using ConditionFunction1 = std::function<TNode<Boolean>(TNode<Number>)>;
using StepFunction1 = std::function<TNode<Number>(TNode<Number>)>;
class ForBuilder0 {
using For0BodyFunction = std::function<void(TNode<Number>)>;
public:
ForBuilder0(JSGraphAssembler* gasm, TNode<Number> initial_value,
const ConditionFunction1& cond, const StepFunction1& step)
: gasm_(gasm),
initial_value_(initial_value),
cond_(cond),
step_(step) {}
void Do(const For0BodyFunction& body) {
auto loop_exit = gasm_->MakeLabel();
{
GraphAssembler::LoopScope<kPhiRepresentation> loop_scope(gasm_);
auto loop_header = loop_scope.loop_header_label();
auto loop_body = gasm_->MakeLabel();
gasm_->Goto(loop_header, initial_value_);
gasm_->Bind(loop_header);
TNode<Number> i = loop_header->PhiAt<Number>(0);
gasm_->BranchWithHint(cond_(i), &loop_body, &loop_exit,
BranchHint::kTrue);
gasm_->Bind(&loop_body);
body(i);
gasm_->Goto(loop_header, step_(i));
}
gasm_->Bind(&loop_exit);
}
private:
static constexpr MachineRepresentation kPhiRepresentation =
MachineRepresentation::kTagged;
JSGraphAssembler* const gasm_;
const TNode<Number> initial_value_;
const ConditionFunction1 cond_;
const StepFunction1 step_;
};
ForBuilder0 ForZeroUntil(TNode<Number> excluded_limit) {
TNode<Number> initial_value = ZeroConstant();
auto cond = [=, this](TNode<Number> i) {
return NumberLessThan(i, excluded_limit);
};
auto step = [=, this](TNode<Number> i) {
return NumberAdd(i, OneConstant());
};
return {this, initial_value, cond, step};
}
ForBuilder0 Forever(TNode<Number> initial_value, const StepFunction1& step) {
return {this, initial_value,
[=, this](TNode<Number>) { return TrueConstant(); }, step};
}
using For1BodyFunction = std::function<void(TNode<Number>, TNode<Object>*)>;
class ForBuilder1 {
public:
ForBuilder1(JSGraphAssembler* gasm, TNode<Number> initial_value,
const ConditionFunction1& cond, const StepFunction1& step,
TNode<Object> initial_arg0)
: gasm_(gasm),
initial_value_(initial_value),
cond_(cond),
step_(step),
initial_arg0_(initial_arg0) {}
V8_WARN_UNUSED_RESULT ForBuilder1& Do(const For1BodyFunction& body) {
body_ = body;
return *this;
}
V8_WARN_UNUSED_RESULT TNode<Object> Value() {
DCHECK(body_);
TNode<Object> arg0 = initial_arg0_;
auto loop_exit = gasm_->MakeDeferredLabel(kPhiRepresentation);
{
GraphAssembler::LoopScope<kPhiRepresentation, kPhiRepresentation>
loop_scope(gasm_);
auto loop_header = loop_scope.loop_header_label();
auto loop_body = gasm_->MakeDeferredLabel(kPhiRepresentation);
gasm_->Goto(loop_header, initial_value_, initial_arg0_);
gasm_->Bind(loop_header);
TNode<Number> i = loop_header->PhiAt<Number>(0);
arg0 = loop_header->PhiAt<Object>(1);
gasm_->BranchWithHint(cond_(i), &loop_body, &loop_exit,
BranchHint::kTrue, arg0);
gasm_->Bind(&loop_body);
body_(i, &arg0);
gasm_->Goto(loop_header, step_(i), arg0);
}
gasm_->Bind(&loop_exit);
return TNode<Object>::UncheckedCast(loop_exit.PhiAt<Object>(0));
}
void ValueIsUnused() { USE(Value()); }
private:
static constexpr MachineRepresentation kPhiRepresentation =
MachineRepresentation::kTagged;
JSGraphAssembler* const gasm_;
const TNode<Number> initial_value_;
const ConditionFunction1 cond_;
const StepFunction1 step_;
For1BodyFunction body_;
const TNode<Object> initial_arg0_;
};
ForBuilder1 For1(TNode<Number> initial_value, const ConditionFunction1& cond,
const StepFunction1& step, TNode<Object> initial_arg0) {
return {this, initial_value, cond, step, initial_arg0};
}
ForBuilder1 For1ZeroUntil(TNode<Number> excluded_limit,
TNode<Object> initial_arg0) {
TNode<Number> initial_value = ZeroConstant();
auto cond = [=, this](TNode<Number> i) {
return NumberLessThan(i, excluded_limit);
};
auto step = [=, this](TNode<Number> i) {
return NumberAdd(i, OneConstant());
};
return {this, initial_value, cond, step, initial_arg0};
}
void ThrowIfNotCallable(TNode<Object> maybe_callable,
FrameState frame_state) {
IfNot(ObjectIsCallable(maybe_callable))
.Then(_ {
JSCallRuntime1(Runtime::kThrowCalledNonCallable, maybe_callable,
ContextInput(), frame_state);
Unreachable();
})
.ExpectTrue();
}
const FeedbackSource& feedback() const {
CallParameters const& p = CallParametersOf(node_ptr()->op());
return p.feedback();
}
int ArgumentCount() const { return JSCallNode{node_ptr()}.ArgumentCount(); }
TNode<Object> Argument(int index) const {
return TNode<Object>::UncheckedCast(JSCallNode{node_ptr()}.Argument(index));
}
template <typename T>
TNode<T> ArgumentAs(int index) const {
return TNode<T>::UncheckedCast(Argument(index));
}
TNode<Object> ArgumentOrNaN(int index) {
return TNode<Object>::UncheckedCast(
ArgumentCount() > index ? Argument(index) : NaNConstant());
}
TNode<Object> ArgumentOrUndefined(int index) {
return TNode<Object>::UncheckedCast(
ArgumentCount() > index ? Argument(index) : UndefinedConstant());
}
TNode<Number> ArgumentOrZero(int index) {
return TNode<Number>::UncheckedCast(
ArgumentCount() > index ? Argument(index) : ZeroConstant());
}
TNode<Context> ContextInput() const {
return TNode<Context>::UncheckedCast(
NodeProperties::GetContextInput(node_));
}
FrameState FrameStateInput() const {
return FrameState(NodeProperties::GetFrameStateInput(node_));
}
CompilationDependencies* dependencies() const { return dependencies_; }
private:
CompilationDependencies* const dependencies_;
Node* const node_;
};
enum class ArrayReduceDirection { kLeft, kRight };
enum class ArrayFindVariant { kFind, kFindIndex };
enum class ArrayEverySomeVariant { kEvery, kSome };
enum class ArrayIndexOfIncludesVariant { kIncludes, kIndexOf };
class IteratingArrayBuiltinReducerAssembler : public JSCallReducerAssembler {
public:
IteratingArrayBuiltinReducerAssembler(JSCallReducer* reducer, Node* node)
: JSCallReducerAssembler(reducer, node) {
DCHECK(v8_flags.turbo_inline_array_builtins);
}
TNode<Object> ReduceArrayPrototypeForEach(MapInference* inference,
const bool has_stability_dependency,
ElementsKind kind,
SharedFunctionInfoRef shared);
TNode<Object> ReduceArrayPrototypeReduce(MapInference* inference,
const bool has_stability_dependency,
ElementsKind kind,
ArrayReduceDirection direction,
SharedFunctionInfoRef shared);
TNode<JSArray> ReduceArrayPrototypeMap(MapInference* inference,
const bool has_stability_dependency,
ElementsKind kind,
SharedFunctionInfoRef shared,
NativeContextRef native_context);
TNode<JSArray> ReduceArrayPrototypeFilter(MapInference* inference,
const bool has_stability_dependency,
ElementsKind kind,
SharedFunctionInfoRef shared,
NativeContextRef native_context);
TNode<Object> ReduceArrayPrototypeFind(MapInference* inference,
const bool has_stability_dependency,
ElementsKind kind,
SharedFunctionInfoRef shared,
NativeContextRef native_context,
ArrayFindVariant variant);
TNode<Boolean> ReduceArrayPrototypeEverySome(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, SharedFunctionInfoRef shared,
NativeContextRef native_context, ArrayEverySomeVariant variant);
TNode<Object> ReduceArrayPrototypeAt(ZoneVector<MapRef> kinds,
bool needs_fallback_builtin_call);
TNode<Object> ReduceArrayPrototypeIndexOfIncludes(
ElementsKind kind, ArrayIndexOfIncludesVariant variant);
TNode<Number> ReduceArrayPrototypePush(MapInference* inference);
private:
std::pair<TNode<Number>, TNode<Object>> SafeLoadElement(ElementsKind kind,
TNode<JSArray> o,
TNode<Number> index) {
TNode<Number> length = LoadJSArrayLength(o, kind);
index = CheckBounds(index, length);
TNode<HeapObject> elements =
LoadField<HeapObject>(AccessBuilder::ForJSObjectElements(), o);
TNode<Object> value = LoadElement<Object>(
AccessBuilder::ForFixedArrayElement(kind), elements, index);
return std::make_pair(index, value);
}
template <typename... Vars>
TNode<Object> MaybeSkipHole(
TNode<Object> o, ElementsKind kind,
GraphAssemblerLabel<sizeof...(Vars)>* continue_label,
TNode<Vars>... vars) {
if (!IsHoleyElementsKind(kind)) return o;
auto if_not_hole = MakeLabel(MachineRepresentationOf<Vars>::value...);
BranchWithHint(HoleCheck(kind, o), continue_label, &if_not_hole,
BranchHint::kFalse, vars...);
Bind(&if_not_hole);
return TypeGuardNonInternal(o);
}
TNode<Smi> LoadJSArrayLength(TNode<JSArray> array, ElementsKind kind) {
return LoadField<Smi>(AccessBuilder::ForJSArrayLength(kind), array);
}
void StoreJSArrayLength(TNode<JSArray> array, TNode<Number> value,
ElementsKind kind) {
StoreField(AccessBuilder::ForJSArrayLength(kind), array, value);
}
void StoreFixedArrayBaseElement(TNode<FixedArrayBase> o, TNode<Number> index,
TNode<Object> v, ElementsKind kind) {
StoreElement(AccessBuilder::ForFixedArrayElement(kind), o, index, v);
}
TNode<FixedArrayBase> LoadElements(TNode<JSObject> o) {
return LoadField<FixedArrayBase>(AccessBuilder::ForJSObjectElements(), o);
}
TNode<Smi> LoadFixedArrayBaseLength(TNode<FixedArrayBase> o) {
return LoadField<Smi>(AccessBuilder::ForFixedArrayLength(), o);
}
TNode<Boolean> HoleCheck(ElementsKind kind, TNode<Object> v) {
return IsDoubleElementsKind(kind)
? NumberIsFloat64Hole(TNode<Number>::UncheckedCast(v))
: IsTheHole(v);
}
};
class PromiseBuiltinReducerAssembler : public JSCallReducerAssembler {
public:
PromiseBuiltinReducerAssembler(JSCallReducer* reducer, Node* node)
: JSCallReducerAssembler(reducer, node) {
DCHECK_EQ(IrOpcode::kJSConstruct, node->opcode());
}
TNode<Object> ReducePromiseConstructor(NativeContextRef native_context);
int ConstructArity() const {
return JSConstructNode{node_ptr()}.ArgumentCount();
}
TNode<Object> TargetInput() const {
return JSConstructNode{node_ptr()}.target();
}
TNode<Object> NewTargetInput() const {
return JSConstructNode{node_ptr()}.new_target();
}
private:
TNode<JSPromise> CreatePromise(TNode<Context> context) {
return AddNode<JSPromise>(
graph()->NewNode(javascript()->CreatePromise(), context, effect()));
}
TNode<Context> CreateFunctionContext(NativeContextRef native_context,
TNode<Context> outer_context,
int slot_count) {
return AddNode<Context>(graph()->NewNode(
javascript()->CreateFunctionContext(
native_context.scope_info(broker()),
slot_count - Context::MIN_CONTEXT_SLOTS, FUNCTION_SCOPE),
outer_context, effect(), control()));
}
void StoreContextNoCellSlot(TNode<Context> context, size_t slot_index,
TNode<Object> value) {
StoreField(AccessBuilder::ForContextSlot(slot_index), context, value);
}
TNode<JSFunction> CreateClosureFromBuiltinSharedFunctionInfo(
SharedFunctionInfoRef shared, TNode<Context> context) {
DCHECK(shared.HasBuiltinId());
Handle<FeedbackCell> feedback_cell =
isolate()->factory()->many_closures_cell();
Callable const callable =
Builtins::CallableFor(isolate(), shared.builtin_id());
CodeRef code = MakeRef(broker(), *callable.code());
return AddNode<JSFunction>(graph()->NewNode(
javascript()->CreateClosure(shared, code), HeapConstant(feedback_cell),
context, effect(), control()));
}
void CallPromiseExecutor(TNode<Object> executor, TNode<JSFunction> resolve,
TNode<JSFunction> reject, FrameState frame_state) {
JSConstructNode n(node_ptr());
const ConstructParameters& p = n.Parameters();
FeedbackSource no_feedback_source{};
Node* no_feedback = UndefinedConstant();
MayThrow(_ {
return AddNode<Object>(graph()->NewNode(
javascript()->Call(JSCallNode::ArityForArgc(2), p.frequency(),
no_feedback_source,
ConvertReceiverMode::kNullOrUndefined),
executor, UndefinedConstant(), resolve, reject, no_feedback,
n.context(), frame_state, effect(), control()));
});
}
void CallPromiseReject(TNode<JSFunction> reject, TNode<Object> exception,
FrameState frame_state) {
JSConstructNode n(node_ptr());
const ConstructParameters& p = n.Parameters();
FeedbackSource no_feedback_source{};
Node* no_feedback = UndefinedConstant();
MayThrow(_ {
return AddNode<Object>(graph()->NewNode(
javascript()->Call(JSCallNode::ArityForArgc(1), p.frequency(),
no_feedback_source,
ConvertReceiverMode::kNullOrUndefined),
reject, UndefinedConstant(), exception, no_feedback, n.context(),
frame_state, effect(), control()));
});
}
};
class FastApiCallReducerAssembler : public JSCallReducerAssembler {
public:
FastApiCallReducerAssembler(
JSCallReducer* reducer, Node* node,
const FunctionTemplateInfoRef function_template_info,
FastApiCallFunction c_function, Node* receiver,
const SharedFunctionInfoRef shared, Node* target, const int arity,
Node* effect)
: JSCallReducerAssembler(reducer, node),
c_function_(c_function),
function_template_info_(function_template_info),
receiver_(receiver),
shared_(shared),
target_(target),
arity_(arity) {
DCHECK_EQ(IrOpcode::kJSCall, node->opcode());
InitializeEffectControl(effect, NodeProperties::GetControlInput(node));
}
TNode<Object> ReduceFastApiCall() {
JSCallNode n(node_ptr());
const int c_argument_count =
static_cast<int>(c_function_.signature->ArgumentCount());
CHECK_GE(c_argument_count, kReceiver);
const int slow_arg_count =
kSlowBuiltinParams +
kReceiver + arity_;
const int value_input_count =
FastApiCallNode::ArityForArgc(c_argument_count, slow_arg_count);
base::SmallVector<Node*, kInlineSize> inputs(value_input_count +
kEffectAndControl);
int cursor = 0;
inputs[cursor++] = n.receiver();
int js_args_count = c_argument_count - kReceiver;
for (int i = 0; i < js_args_count; ++i) {
if (i < n.ArgumentCount()) {
inputs[cursor++] = n.Argument(i);
} else {
inputs[cursor++] = UndefinedConstant();
}
}
bool no_profiling =
broker()->dependencies()->DependOnNoProfilingProtector();
Callable call_api_callback = Builtins::CallableFor(
isolate(), no_profiling ? Builtin::kCallApiCallbackOptimizedNoProfiling
: Builtin::kCallApiCallbackOptimized);
CallInterfaceDescriptor cid = call_api_callback.descriptor();
DCHECK_EQ(cid.GetParameterCount() + (cid.HasContextParameter() ? 1 : 0),
kSlowBuiltinParams);
CallDescriptor* call_descriptor =
Linkage::GetStubCallDescriptor(graph()->zone(), cid, arity_ + kReceiver,
CallDescriptor::kNeedsFrameState);
const ZoneVector<CFunctionInfoWithDetails> overloads =
function_template_info_.c_functions_with_signatures(broker());
const size_t overloads_count = overloads.size();
ZoneVector<Address> c_functions(overloads_count, graph()->zone());
ZoneVector<const CFunctionInfo*> c_signatures(overloads_count,
graph()->zone());
for (size_t i = 0; i < overloads_count; ++i) {
c_functions[i] = overloads[i].address;
c_signatures[i] = overloads[i].signature;
}
ApiFunction api_function(function_template_info_.callback(broker()));
ExternalReference function_reference = ExternalReference::Create(
isolate(), &api_function, ExternalReference::DIRECT_API_CALL,
c_functions.data(), c_signatures.data(),
static_cast<unsigned>(overloads_count));
Node* error_message = jsgraph()->SmiConstant(
static_cast<int>(AbortReason::kUnsupportedDeopt));
Node* continuation_frame_state =
c_function_.signature->ReturnInfo().GetType() == CTypeInfo::Type::kVoid
? CreateInlinedApiFunctionFrameState(jsgraph(), shared_, target_,
ContextInput(), receiver_,
FrameStateInput())
: CreateStubBuiltinContinuationFrameState(
jsgraph(), Builtin::kAbort, ContextInput(), &error_message, 1,
FrameStateInput(), ContinuationFrameStateMode::LAZY);
inputs[cursor++] =
Constant(function_template_info_.callback_data(broker()).value());
inputs[cursor++] = HeapConstant(call_api_callback.code());
inputs[cursor++] = ExternalConstant(function_reference);
inputs[cursor++] = NumberConstant(arity_);
inputs[cursor++] = HeapConstant(function_template_info_.object());
inputs[cursor++] = receiver_;
for (int i = 0; i < arity_; ++i) {
inputs[cursor++] = Argument(i);
}
inputs[cursor++] = ContextInput();
inputs[cursor++] = continuation_frame_state;
inputs[cursor++] = effect();
inputs[cursor++] = control();
DCHECK_EQ(cursor, value_input_count + kEffectAndControl);
return FastApiCall(call_descriptor, inputs.begin(), inputs.size());
}
private:
static constexpr int kEffectAndControl = 2;
static constexpr int kSlowBuiltinParams = 4;
static constexpr int kReceiver = 1;
static constexpr int kInlineSize = 16;
TNode<Object> FastApiCall(CallDescriptor* descriptor, Node** inputs,
size_t inputs_size) {
return AddNode<Object>(graph()->NewNode(
simplified()->FastApiCall(c_function_, feedback(), descriptor),
static_cast<int>(inputs_size), inputs));
}
FastApiCallFunction c_function_;
const FunctionTemplateInfoRef function_template_info_;
Node* const receiver_;
const SharedFunctionInfoRef shared_;
Node* const target_;
const int arity_;
};
TNode<Number> JSCallReducerAssembler::SpeculativeToNumber(
TNode<Object> value, NumberOperationHint hint) {
return AddNode<Number>(
graph()->NewNode(simplified()->SpeculativeToNumber(hint, feedback()),
value, effect(), control()));
}
TNode<Smi> JSCallReducerAssembler::CheckSmi(TNode<Object> value) {
return AddNode<Smi>(graph()->NewNode(simplified()->CheckSmi(feedback()),
value, effect(), control()));
}
TNode<Number> JSCallReducerAssembler::CheckNumber(TNode<Object> value) {
return AddNode<Number>(graph()->NewNode(simplified()->CheckNumber(feedback()),
value, effect(), control()));
}
TNode<String> JSCallReducerAssembler::CheckString(TNode<Object> value) {
return AddNode<String>(graph()->NewNode(simplified()->CheckString(feedback()),
value, effect(), control()));
}
TNode<Number> JSCallReducerAssembler::CheckBounds(TNode<Object> value,
TNode<Number> limit,
CheckBoundsFlags flags) {
return AddNode<Number>(
graph()->NewNode(simplified()->CheckBounds(feedback(), flags), value,
limit, effect(), control()));
}
TNode<Smi> JSCallReducerAssembler::TypeGuardUnsignedSmall(TNode<Object> value) {
return TNode<Smi>::UncheckedCast(TypeGuard(Type::UnsignedSmall(), value));
}
TNode<Object> JSCallReducerAssembler::TypeGuardNonInternal(
TNode<Object> value) {
return TNode<Object>::UncheckedCast(TypeGuard(Type::NonInternal(), value));
}
TNode<Number> JSCallReducerAssembler::TypeGuardFixedArrayLength(
TNode<Object> value) {
DCHECK(TypeCache::Get()->kFixedDoubleArrayLengthType.Is(
TypeCache::Get()->kFixedArrayLengthType));
return TNode<Number>::UncheckedCast(
TypeGuard(TypeCache::Get()->kFixedArrayLengthType, value));
}
TNode<Object> JSCallReducerAssembler::Call4(
const Callable& callable, TNode<Context> context, TNode<Object> arg0,
TNode<Object> arg1, TNode<Object> arg2, TNode<Object> arg3) {
CallDescriptor* desc = Linkage::GetStubCallDescriptor(
graph()->zone(), callable.descriptor(),
callable.descriptor().GetStackParameterCount(), CallDescriptor::kNoFlags,
Operator::kEliminatable);
return TNode<Object>::UncheckedCast(Call(desc, HeapConstant(callable.code()),
arg0, arg1, arg2, arg3, context));
}
TNode<Object> JSCallReducerAssembler::JSCall3(
TNode<Object> function, TNode<Object> this_arg, TNode<Object> arg0,
TNode<Object> arg1, TNode<Object> arg2, FrameState frame_state) {
JSCallNode n(node_ptr());
CallParameters const& p = n.Parameters();
return MayThrow(_ {
return AddNode<Object>(graph()->NewNode(
javascript()->Call(JSCallNode::ArityForArgc(3), p.frequency(),
p.feedback(), ConvertReceiverMode::kAny,
p.speculation_mode(),
CallFeedbackRelation::kUnrelated),
function, this_arg, arg0, arg1, arg2, n.feedback_vector(),
ContextInput(), frame_state, effect(), control()));
});
}
TNode<Object> JSCallReducerAssembler::JSCall4(
TNode<Object> function, TNode<Object> this_arg, TNode<Object> arg0,
TNode<Object> arg1, TNode<Object> arg2, TNode<Object> arg3,
FrameState frame_state) {
JSCallNode n(node_ptr());
CallParameters const& p = n.Parameters();
return MayThrow(_ {
return AddNode<Object>(graph()->NewNode(
javascript()->Call(JSCallNode::ArityForArgc(4), p.frequency(),
p.feedback(), ConvertReceiverMode::kAny,
p.speculation_mode(),
CallFeedbackRelation::kUnrelated),
function, this_arg, arg0, arg1, arg2, arg3, n.feedback_vector(),
ContextInput(), frame_state, effect(), control()));
});
}
TNode<Object> JSCallReducerAssembler::CopyNode() {
return MayThrow(_ {
Node* copy = graph()->CloneNode(node_ptr());
NodeProperties::ReplaceEffectInput(copy, effect());
NodeProperties::ReplaceControlInput(copy, control());
return AddNode<Object>(copy);
});
}
TNode<JSArray> JSCallReducerAssembler::CreateArrayNoThrow(
TNode<Object> ctor, TNode<Number> size, FrameState frame_state) {
return AddNode<JSArray>(graph()->NewNode(
javascript()->CreateArray(1, std::nullopt, feedback()), ctor, ctor, size,
ContextInput(), frame_state, effect(), control()));
}
TNode<JSArray> JSCallReducerAssembler::AllocateEmptyJSArray(
ElementsKind kind, NativeContextRef native_context) {
MapRef map = native_context.GetInitialJSArrayMap(broker(), kind);
AllocationBuilder ab(jsgraph(), broker(), effect(), control());
ab.Allocate(map.instance_size(), AllocationType::kYoung, Type::Array());
ab.Store(AccessBuilder::ForMap(), map);
Node* empty_fixed_array = jsgraph()->EmptyFixedArrayConstant();
ab.Store(AccessBuilder::ForJSObjectPropertiesOrHashKnownPointer(),
empty_fixed_array);
ab.Store(AccessBuilder::ForJSObjectElements(), empty_fixed_array);
ab.Store(AccessBuilder::ForJSArrayLength(kind), jsgraph()->ZeroConstant());
for (int i = 0; i < map.GetInObjectProperties(); ++i) {
ab.Store(AccessBuilder::ForJSObjectInObjectProperty(map, i),
jsgraph()->UndefinedConstant());
}
Node* result = ab.Finish();
InitializeEffectControl(result, control());
return TNode<JSArray>::UncheckedCast(result);
}
TNode<Number> JSCallReducerAssembler::LoadMapElementsKind(TNode<Map> map) {
TNode<Number> bit_field2 =
LoadField<Number>(AccessBuilder::ForMapBitField2(), map);
return NumberShiftRightLogical(
NumberBitwiseAnd(bit_field2,
NumberConstant(Map::Bits2::ElementsKindBits::kMask)),
NumberConstant(Map::Bits2::ElementsKindBits::kShift));
}
TNode<Object> JSCallReducerAssembler::ReduceMathUnary(const Operator* op) {
TNode<Object> input = Argument(0);
TNode<Number> input_as_number = SpeculativeToNumber(input);
return TNode<Object>::UncheckedCast(graph()->NewNode(op, input_as_number));
}
TNode<Object> JSCallReducerAssembler::ReduceMathBinary(const Operator* op) {
TNode<Object> left = Argument(0);
TNode<Object> right = ArgumentOrNaN(1);
TNode<Number> left_number = SpeculativeToNumber(left);
TNode<Number> right_number = SpeculativeToNumber(right);
return TNode<Object>::UncheckedCast(
graph()->NewNode(op, left_number, right_number));
}
TNode<String> JSCallReducerAssembler::ReduceStringPrototypeSubstring() {
TNode<Object> receiver = ReceiverInput();
TNode<Object> start = Argument(0);
TNode<Object> end = ArgumentOrUndefined(1);
TNode<String> receiver_string = CheckString(receiver);
TNode<Number> start_smi = CheckSmi(start);
TNode<Number> length = StringLength(receiver_string);
TNode<Number> end_smi = SelectIf<Number>(IsUndefined(end))
.Then(_ { return length; })
.Else(_ { return CheckSmi(end); })
.ExpectFalse()
.Value();
TNode<Number> zero = TNode<Number>::UncheckedCast(ZeroConstant());
TNode<Number> finalStart = NumberMin(NumberMax(start_smi, zero), length);
TNode<Number> finalEnd = NumberMin(NumberMax(end_smi, zero), length);
TNode<Number> from = NumberMin(finalStart, finalEnd);
TNode<Number> to = NumberMax(finalStart, finalEnd);
return StringSubstring(receiver_string, from, to);
}
TNode<Boolean> JSCallReducerAssembler::ReduceStringPrototypeStartsWith(
StringRef search_element_string) {
DCHECK(search_element_string.IsContentAccessible());
TNode<Object> receiver = ReceiverInput();
TNode<Object> start = ArgumentOrZero(1);
TNode<String> receiver_string = CheckString(receiver);
TNode<Smi> start_smi = CheckSmi(start);
TNode<Number> length = StringLength(receiver_string);
TNode<Number> zero = ZeroConstant();
TNode<Number> clamped_start = NumberMin(NumberMax(start_smi, zero), length);
int search_string_length = search_element_string.length();
DCHECK(search_string_length <= JSCallReducer::kMaxInlineMatchSequence);
auto out = MakeLabel(MachineRepresentation::kTagged);
auto search_string_too_long =
NumberLessThan(NumberSubtract(length, clamped_start),
NumberConstant(search_string_length));
GotoIf(search_string_too_long, &out, BranchHint::kFalse, FalseConstant());
static_assert(String::kMaxLength <= kSmiMaxValue);
for (int i = 0; i < search_string_length; i++) {
TNode<Number> k = NumberConstant(i);
TNode<Number> receiver_string_position = TNode<Number>::UncheckedCast(
TypeGuard(Type::UnsignedSmall(), NumberAdd(k, clamped_start)));
Node* receiver_string_char =
StringCharCodeAt(receiver_string, receiver_string_position);
Node* search_string_char = jsgraph()->ConstantNoHole(
search_element_string.GetChar(broker(), i).value());
auto is_equal = graph()->NewNode(simplified()->NumberEqual(),
search_string_char, receiver_string_char);
GotoIfNot(is_equal, &out, FalseConstant());
}
Goto(&out, TrueConstant());
Bind(&out);
return out.PhiAt<Boolean>(0);
}
TNode<Boolean> JSCallReducerAssembler::ReduceStringPrototypeStartsWith() {
TNode<Object> receiver = ReceiverInput();
TNode<Object> search_element = ArgumentOrUndefined(0);
TNode<Object> start = ArgumentOrZero(1);
TNode<String> receiver_string = CheckString(receiver);
TNode<String> search_string = CheckString(search_element);
TNode<Smi> start_smi = CheckSmi(start);
TNode<Number> length = StringLength(receiver_string);
TNode<Number> zero = ZeroConstant();
TNode<Number> clamped_start = NumberMin(NumberMax(start_smi, zero), length);
TNode<Number> search_string_length = StringLength(search_string);
auto out = MakeLabel(MachineRepresentation::kTagged);
auto search_string_too_long = NumberLessThan(
NumberSubtract(length, clamped_start), search_string_length);
GotoIf(search_string_too_long, &out, BranchHint::kFalse, FalseConstant());
static_assert(String::kMaxLength <= kSmiMaxValue);
ForZeroUntil(search_string_length).Do([&](TNode<Number> k) {
TNode<Number> receiver_string_position = TNode<Number>::UncheckedCast(
TypeGuard(Type::UnsignedSmall(), NumberAdd(k, clamped_start)));
Node* receiver_string_char =
StringCharCodeAt(receiver_string, receiver_string_position);
if (!v8_flags.turbo_loop_variable) {
k = TypeGuard(Type::Unsigned32(), k);
}
Node* search_string_char = StringCharCodeAt(search_string, k);
auto is_equal = graph()->NewNode(simplified()->NumberEqual(),
receiver_string_char, search_string_char);
GotoIfNot(is_equal, &out, FalseConstant());
});
Goto(&out, TrueConstant());
Bind(&out);
return out.PhiAt<Boolean>(0);
}
TNode<Boolean> JSCallReducerAssembler::ReduceStringPrototypeEndsWith(
StringRef search_element_string) {
DCHECK(search_element_string.IsContentAccessible());
TNode<Object> receiver = ReceiverInput();
TNode<Object> end_position = ArgumentOrUndefined(1);
TNode<Number> zero = ZeroConstant();
TNode<String> receiver_string = CheckString(receiver);
TNode<Number> length = StringLength(receiver_string);
int search_string_length = search_element_string.length();
DCHECK_LE(search_string_length, JSCallReducer::kMaxInlineMatchSequence);
TNode<Number> clamped_end =
SelectIf<Number>(IsUndefined(end_position))
.Then(_ { return length; })
.Else(_ {
return NumberMin(NumberMax(CheckSmi(end_position), zero), length);
})
.ExpectTrue()
.Value();
TNode<Number> start =
NumberSubtract(clamped_end, NumberConstant(search_string_length));
auto out = MakeLabel(MachineRepresentation::kTagged);
TNode<Boolean> search_string_too_long = NumberLessThan(start, zero);
GotoIf(search_string_too_long, &out, BranchHint::kFalse, FalseConstant());
for (int i = 0; i < search_string_length; i++) {
TNode<Number> k = NumberConstant(i);
TNode<Number> receiver_string_position = TNode<Number>::UncheckedCast(
TypeGuard(Type::UnsignedSmall(), NumberAdd(k, start)));
Node* receiver_string_char =
StringCharCodeAt(receiver_string, receiver_string_position);
Node* search_string_char = jsgraph()->ConstantNoHole(
search_element_string.GetChar(broker(), i).value());
auto is_equal = graph()->NewNode(simplified()->NumberEqual(),
receiver_string_char, search_string_char);
GotoIfNot(is_equal, &out, FalseConstant());
}
Goto(&out, TrueConstant());
Bind(&out);
return out.PhiAt<Boolean>(0);
}
TNode<Boolean> JSCallReducerAssembler::ReduceStringPrototypeEndsWith() {
TNode<Object> receiver = ReceiverInput();
TNode<Object> search_string = ArgumentOrUndefined(0);
TNode<Object> end_position = ArgumentOrUndefined(1);
TNode<Number> zero = ZeroConstant();
TNode<String> receiver_string = CheckString(receiver);
TNode<Number> length = StringLength(receiver_string);
TNode<String> search_element_string = CheckString(search_string);
TNode<Number> search_string_length = StringLength(search_element_string);
TNode<Number> clamped_end =
SelectIf<Number>(IsUndefined(end_position))
.Then(_ { return length; })
.Else(_ {
return NumberMin(NumberMax(CheckSmi(end_position), zero), length);
})
.ExpectTrue()
.Value();
TNode<Number> start = NumberSubtract(clamped_end, search_string_length);
auto out = MakeLabel(MachineRepresentation::kTagged);
TNode<Boolean> search_string_too_long = NumberLessThan(start, zero);
GotoIf(search_string_too_long, &out, BranchHint::kFalse, FalseConstant());
ForZeroUntil(search_string_length).Do([&](TNode<Number> k) {
TNode<Number> receiver_string_position = TNode<Number>::UncheckedCast(
TypeGuard(Type::UnsignedSmall(), NumberAdd(k, start)));
Node* receiver_string_char =
StringCharCodeAt(receiver_string, receiver_string_position);
if (!v8_flags.turbo_loop_variable) {
k = TypeGuard(Type::Unsigned32(), k);
}
Node* search_string_char = StringCharCodeAt(search_element_string, k);
auto is_equal = graph()->NewNode(simplified()->NumberEqual(),
receiver_string_char, search_string_char);
GotoIfNot(is_equal, &out, FalseConstant());
});
Goto(&out, TrueConstant());
Bind(&out);
return out.PhiAt<Boolean>(0);
}
TNode<String> JSCallReducerAssembler::ReduceStringPrototypeCharAt(
StringRef s, uint32_t index) {
DCHECK(s.IsContentAccessible());
if (s.IsOneByteRepresentation()) {
OptionalObjectRef elem = s.GetCharAsStringOrUndefined(broker(), index);
TNode<String> elem_string =
elem.has_value()
? TNode<String>::UncheckedCast(
jsgraph()->ConstantNoHole(elem.value(), broker()))
: EmptyStringConstant();
return elem_string;
} else {
const uint32_t length = static_cast<uint32_t>(s.length());
if (index >= length) return EmptyStringConstant();
Handle<SeqTwoByteString> flat = broker()->CanonicalPersistentHandle(
broker()
->local_isolate_or_isolate()
->factory()
->NewRawTwoByteString(1, AllocationType::kOld)
.ToHandleChecked());
flat->SeqTwoByteStringSet(0, s.GetChar(broker(), index).value());
TNode<String> two_byte_elem =
TNode<String>::UncheckedCast(jsgraph()->HeapConstantNoHole(flat));
return two_byte_elem;
}
}
TNode<String> JSCallReducerAssembler::ReduceStringPrototypeCharAt(
SpeculationMode speculation_mode) {
TNode<Object> receiver = ReceiverInput();
TNode<Object> index = ArgumentOrZero(0);
TNode<String> receiver_string = CheckString(receiver);
TNode<Number> length = StringLength(receiver_string);
if (speculation_mode == SpeculationMode::kDisallowBoundsCheckSpeculation) {
TNode<Number> index_smi = CheckSmi(index);
return SelectIf<String>(NumberLessThan(index_smi, ZeroConstant()))
.Then(_ { return EmptyStringConstant(); })
.Else(_ {
return SelectIf<String>(NumberLessThan(index_smi, length))
.Then(_ {
return StringFromSingleCharCode(
TNode<Number>::UncheckedCast(StringCharCodeAt(
receiver_string,
TypeGuard(Type::Unsigned32(), index_smi))));
})
.Else(_ { return EmptyStringConstant(); })
.ExpectTrue()
.Value();
})
.ExpectFalse()
.Value();
}
DCHECK_EQ(speculation_mode, SpeculationMode::kAllowSpeculation);
TNode<Number> bounded_index = CheckBounds(index, length);
return StringFromSingleCharCode(TNode<Number>::UncheckedCast(
StringCharCodeAt(receiver_string, bounded_index)));
}
TNode<Number> JSCallReducerAssembler::ReduceStringPrototypeCharCodeAt(
SpeculationMode speculation_mode) {
TNode<Object> receiver = ReceiverInput();
TNode<Object> index = ArgumentOrZero(0);
TNode<String> receiver_string = CheckString(receiver);
TNode<Number> length = StringLength(receiver_string);
if (speculation_mode == SpeculationMode::kDisallowBoundsCheckSpeculation) {
TNode<Number> index_smi = CheckSmi(index);
return SelectIf<Number>(NumberLessThan(index_smi, ZeroConstant()))
.Then(_ { return NaNConstant(); })
.Else(_ {
return SelectIf<Number>(NumberLessThan(index_smi, length))
.Then(_ {
return TNode<Number>::UncheckedCast(StringCharCodeAt(
receiver_string, TypeGuard(Type::Unsigned32(), index_smi)));
})
.Else(_ { return NaNConstant(); })
.ExpectTrue()
.Value();
})
.ExpectFalse()
.Value();
}
DCHECK_EQ(speculation_mode, SpeculationMode::kAllowSpeculation);
TNode<Number> bounded_index = CheckBounds(index, length);
return TNode<Number>::UncheckedCast(
(StringCharCodeAt(receiver_string, bounded_index)));
}
TNode<String> JSCallReducerAssembler::ReduceStringPrototypeSlice() {
TNode<Object> receiver = ReceiverInput();
TNode<String> receiver_string = CheckString(receiver);
TNode<Number> length = StringLength(receiver_string);
TNode<Object> start = Argument(0);
TNode<Object> end = ArgumentOrUndefined(1);
if (ArgumentCount() == 1) {
NumberMatcher m(start);
if (m.Is(-1)) {
return SelectIf<String>(
ReferenceEqual(receiver_string, EmptyStringConstant()))
.Then(_ { return EmptyStringConstant(); })
.Else(_ {
return StringFromSingleCharCode(
TNode<Number>::UncheckedCast(StringCharCodeAt(
receiver_string,
TypeGuard(Type::Unsigned32(),
NumberAdd(length, TNode<Number>::UncheckedCast(
m.node()))))));
})
.Value();
}
}
TNode<Number> start_smi = CheckSmi(start);
TNode<Number> end_smi = SelectIf<Number>(IsUndefined(end))
.Then(_ { return length; })
.Else(_ { return CheckSmi(end); })
.ExpectFalse()
.Value();
TNode<Number> zero = ZeroConstant();
TNode<Number> from_untyped =
SelectIf<Number>(NumberLessThan(start_smi, zero))
.Then(_ { return NumberMax(NumberAdd(length, start_smi), zero); })
.Else(_ { return NumberMin(start_smi, length); })
.ExpectFalse()
.Value();
TNode<Smi> from = TypeGuardUnsignedSmall(from_untyped);
TNode<Number> to_untyped =
SelectIf<Number>(NumberLessThan(end_smi, zero))
.Then(_ { return NumberMax(NumberAdd(length, end_smi), zero); })
.Else(_ { return NumberMin(end_smi, length); })
.ExpectFalse()
.Value();
TNode<Smi> to = TypeGuardUnsignedSmall(to_untyped);
return SelectIf<String>(NumberLessThan(from, to))
.Then(_ { return StringSubstring(receiver_string, from, to); })
.Else(_ { return EmptyStringConstant(); })
.ExpectTrue()
.Value();
}
TNode<Object> JSCallReducerAssembler::ReduceJSCallMathMinMaxWithArrayLike(
Builtin builtin) {
JSCallWithArrayLikeNode n(node_ptr());
TNode<Object> arguments_list = n.Argument(0);
auto call_builtin = MakeLabel();
auto done = MakeLabel(MachineRepresentation::kTagged);
GotoIf(ObjectIsSmi(arguments_list), &call_builtin);
TNode<Map> arguments_list_map =
LoadField<Map>(AccessBuilder::ForMap(),
TNode<HeapObject>::UncheckedCast(arguments_list));
TNode<Number> arguments_list_instance_type = LoadField<Number>(
AccessBuilder::ForMapInstanceType(), arguments_list_map);
auto check_instance_type =
NumberEqual(arguments_list_instance_type, NumberConstant(JS_ARRAY_TYPE));
GotoIfNot(check_instance_type, &call_builtin);
TNode<Number> arguments_list_elements_kind =
LoadMapElementsKind(arguments_list_map);
auto check_element_kind = NumberEqual(arguments_list_elements_kind,
NumberConstant(PACKED_DOUBLE_ELEMENTS));
GotoIfNot(check_element_kind, &call_builtin);
TNode<JSArray> array_arguments_list =
TNode<JSArray>::UncheckedCast(arguments_list);
Goto(&done, builtin == Builtin::kMathMax
? DoubleArrayMax(array_arguments_list)
: DoubleArrayMin(array_arguments_list));
Bind(&call_builtin);
TNode<Object> call = CopyNode();
CallParameters const& p = n.Parameters();
NodeProperties::ChangeOp(
call, javascript()->CallWithArrayLike(
p.frequency(), p.feedback(),
SpeculationMode::kDisallowSpeculation, p.feedback_relation()));
Goto(&done, call);
Bind(&done);
return done.PhiAt<Object>(0);
}
TNode<Object> IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeAt(
ZoneVector<MapRef> maps, bool needs_fallback_builtin_call) {
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> index = ArgumentOrZero(0);
TNode<Number> index_num = CheckSmi(index);
TNode<FixedArrayBase> elements = LoadElements(receiver);
TNode<Map> receiver_map =
TNode<Map>::UncheckedCast(LoadField(AccessBuilder::ForMap(), receiver));
auto out = MakeLabel(MachineRepresentation::kTagged);
for (MapRef map : maps) {
DCHECK(map.supports_fast_array_iteration(broker()));
auto correct_map_label = MakeLabel(), wrong_map_label = MakeLabel();
TNode<Boolean> is_map_equal = ReferenceEqual(receiver_map, Constant(map));
Branch(is_map_equal, &correct_map_label, &wrong_map_label);
Bind(&correct_map_label);
TNode<Number> length = LoadJSArrayLength(receiver, map.elements_kind());
TNode<Boolean> cond = NumberLessThan(index_num, ZeroConstant());
TNode<Number> real_index_num =
SelectIf<Number>(cond)
.Then(_ { return NumberAdd(length, index_num); })
.Else(_ { return index_num; })
.ExpectTrue()
.Value();
GotoIf(NumberLessThan(real_index_num, ZeroConstant()), &out,
UndefinedConstant());
GotoIfNot(NumberLessThan(real_index_num, length), &out,
UndefinedConstant());
if (v8_flags.turbo_typer_hardening) {
real_index_num = CheckBounds(real_index_num, length,
CheckBoundsFlag::kAbortOnOutOfBounds);
}
TNode<Object> element = LoadElement<Object>(
AccessBuilder::ForFixedArrayElement(map.elements_kind()), elements,
real_index_num);
if (IsHoleyElementsKind(map.elements_kind())) {
element = ConvertHoleToUndefined(element, map.elements_kind());
}
Goto(&out, element);
Bind(&wrong_map_label);
}
if (needs_fallback_builtin_call) {
JSCallNode n(node_ptr());
CallParameters const& p = n.Parameters();
const Operator* op = javascript()->Call(
JSCallNode::ArityForArgc(1), p.frequency(), p.feedback(),
ConvertReceiverMode::kNotNullOrUndefined,
SpeculationMode::kDisallowSpeculation, CallFeedbackRelation::kTarget);
Node* fallback_builtin = node_ptr()->InputAt(0);
TNode<Object> res = MayThrow(_ {
return AddNode<Object>(graph()->NewNode(
op, fallback_builtin, receiver, index, n.feedback_vector(),
ContextInput(), n.frame_state(), effect(), control()));
});
Goto(&out, res);
} else {
Goto(&out, UndefinedConstant());
}
Bind(&out);
return out.PhiAt<Object>(0);
}
TNode<Number> IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypePush(
MapInference* inference) {
int const num_push_arguments = ArgumentCount();
ZoneRefSet<Map> const& receiver_maps = inference->GetMaps();
base::SmallVector<MachineRepresentation, 4> argument_reps;
base::SmallVector<Node*, 4> argument_nodes;
for (int i = 0; i < num_push_arguments; ++i) {
argument_reps.push_back(MachineRepresentation::kTagged);
argument_nodes.push_back(Argument(i));
}
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Map> receiver_map = LoadMap(receiver);
auto double_label = MakeLabel(argument_reps);
auto smi_label = MakeLabel(argument_reps);
auto object_label = MakeLabel(argument_reps);
for (size_t i = 0; i < receiver_maps.size(); i++) {
MapRef map = receiver_maps[i];
ElementsKind kind = map.elements_kind();
if (i < receiver_maps.size() - 1) {
TNode<Boolean> is_map_equal = ReferenceEqual(receiver_map, Constant(map));
if (IsDoubleElementsKind(kind)) {
GotoIf(is_map_equal, &double_label, argument_nodes);
} else if (IsSmiElementsKind(kind)) {
GotoIf(is_map_equal, &smi_label, argument_nodes);
} else {
GotoIf(is_map_equal, &object_label, argument_nodes);
}
} else {
if (IsDoubleElementsKind(kind)) {
Goto(&double_label, argument_nodes);
} else if (IsSmiElementsKind(kind)) {
Goto(&smi_label, argument_nodes);
} else {
Goto(&object_label, argument_nodes);
}
}
}
auto return_label = MakeLabel(MachineRepresentation::kTagged);
auto build_array_push = [&](ElementsKind kind,
base::SmallVector<Node*, 1>& push_arguments) {
DCHECK(kind == PACKED_ELEMENTS || kind == PACKED_DOUBLE_ELEMENTS);
TNode<Smi> length = LoadJSArrayLength(receiver, kind);
TNode<Number> return_value = length;
if (num_push_arguments > 0) {
TNode<Number> new_length = return_value =
NumberAdd(length, NumberConstant(num_push_arguments));
TNode<FixedArrayBase> elements = LoadElements(receiver);
TNode<Smi> elements_length = LoadFixedArrayBaseLength(elements);
elements = MaybeGrowFastElements(
kind, feedback(), receiver, elements,
NumberAdd(length, NumberConstant(num_push_arguments - 1)),
elements_length);
StoreJSArrayLength(receiver, new_length, kind);
for (int i = 0; i < num_push_arguments; ++i) {
StoreFixedArrayBaseElement(
elements, NumberAdd(length, NumberConstant(i)),
TNode<Object>::UncheckedCast(push_arguments[i]), kind);
}
}
Goto(&return_label, return_value);
};
if (double_label.IsUsed()) {
Bind(&double_label);
base::SmallVector<Node*, 1> push_arguments(num_push_arguments);
for (int i = 0; i < num_push_arguments; ++i) {
Node* value =
CheckNumber(TNode<Object>::UncheckedCast(double_label.PhiAt(i)));
value = AddNode<Number>(
graph()->NewNode(simplified()->NumberSilenceNaN(), value));
push_arguments[i] = value;
}
build_array_push(PACKED_DOUBLE_ELEMENTS, push_arguments);
}
if (smi_label.IsUsed()) {
Bind(&smi_label);
base::SmallVector<Node*, 4> push_arguments(num_push_arguments);
for (int i = 0; i < num_push_arguments; ++i) {
Node* value = CheckSmi(TNode<Object>::UncheckedCast(smi_label.PhiAt(i)));
push_arguments[i] = value;
}
Goto(&object_label, push_arguments);
}
if (object_label.IsUsed()) {
Bind(&object_label);
base::SmallVector<Node*, 1> push_arguments(num_push_arguments);
for (int i = 0; i < num_push_arguments; ++i) {
push_arguments[i] = object_label.PhiAt(i);
}
build_array_push(PACKED_ELEMENTS, push_arguments);
}
Bind(&return_label);
return TNode<Number>::UncheckedCast(return_label.PhiAt(0));
}
namespace {
struct ForEachFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
TNode<Object> receiver;
TNode<Object> callback;
TNode<Object> this_arg;
TNode<Object> original_length;
};
FrameState ForEachLoopLazyFrameState(const ForEachFrameStateParams& params,
TNode<Object> k) {
Builtin builtin = Builtin::kArrayForEachLoopLazyDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, k, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
FrameState ForEachLoopEagerFrameState(const ForEachFrameStateParams& params,
TNode<Object> k) {
Builtin builtin = Builtin::kArrayForEachLoopEagerDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, k, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::EAGER);
}
}
TNode<Object>
IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeForEach(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, SharedFunctionInfoRef shared) {
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> fncallback = ArgumentOrUndefined(0);
TNode<Object> this_arg = ArgumentOrUndefined(1);
TNode<Number> original_length = LoadJSArrayLength(receiver, kind);
ForEachFrameStateParams frame_state_params{
jsgraph(), shared, context, target, outer_frame_state,
receiver, fncallback, this_arg, original_length};
ThrowIfNotCallable(fncallback, ForEachLoopLazyFrameState(frame_state_params,
ZeroConstant()));
ForZeroUntil(original_length).Do([&](TNode<Number> k) {
Checkpoint(ForEachLoopEagerFrameState(frame_state_params, k));
MaybeInsertMapChecks(inference, has_stability_dependency);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
auto continue_label = MakeLabel();
element = MaybeSkipHole(element, kind, &continue_label);
TNode<Number> next_k = NumberAdd(k, OneConstant());
JSCall3(fncallback, this_arg, element, k, receiver,
ForEachLoopLazyFrameState(frame_state_params, next_k));
Goto(&continue_label);
Bind(&continue_label);
});
return UndefinedConstant();
}
namespace {
struct ReduceFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
ArrayReduceDirection direction;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
};
FrameState ReducePreLoopLazyFrameState(const ReduceFrameStateParams& params,
TNode<Object> receiver,
TNode<Object> callback, TNode<Object> k,
TNode<Number> original_length) {
Builtin builtin = (params.direction == ArrayReduceDirection::kLeft)
? Builtin::kArrayReduceLoopLazyDeoptContinuation
: Builtin::kArrayReduceRightLoopLazyDeoptContinuation;
Node* checkpoint_params[] = {receiver, callback, k, original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
FrameState ReducePreLoopEagerFrameState(const ReduceFrameStateParams& params,
TNode<Object> receiver,
TNode<Object> callback,
TNode<Number> original_length) {
Builtin builtin =
(params.direction == ArrayReduceDirection::kLeft)
? Builtin::kArrayReducePreLoopEagerDeoptContinuation
: Builtin::kArrayReduceRightPreLoopEagerDeoptContinuation;
Node* checkpoint_params[] = {receiver, callback, original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::EAGER);
}
FrameState ReduceLoopLazyFrameState(const ReduceFrameStateParams& params,
TNode<Object> receiver,
TNode<Object> callback, TNode<Object> k,
TNode<Number> original_length) {
Builtin builtin = (params.direction == ArrayReduceDirection::kLeft)
? Builtin::kArrayReduceLoopLazyDeoptContinuation
: Builtin::kArrayReduceRightLoopLazyDeoptContinuation;
Node* checkpoint_params[] = {receiver, callback, k, original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
FrameState ReduceLoopEagerFrameState(const ReduceFrameStateParams& params,
TNode<Object> receiver,
TNode<Object> callback, TNode<Object> k,
TNode<Number> original_length,
TNode<Object> accumulator) {
Builtin builtin = (params.direction == ArrayReduceDirection::kLeft)
? Builtin::kArrayReduceLoopEagerDeoptContinuation
: Builtin::kArrayReduceRightLoopEagerDeoptContinuation;
Node* checkpoint_params[] = {receiver, callback, k, original_length,
accumulator};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::EAGER);
}
}
TNode<Object> IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeReduce(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, ArrayReduceDirection direction,
SharedFunctionInfoRef shared) {
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> fncallback = ArgumentOrUndefined(0);
ReduceFrameStateParams frame_state_params{
jsgraph(), shared, direction, context, target, outer_frame_state};
TNode<Number> original_length = LoadJSArrayLength(receiver, kind);
TNode<Number> k;
StepFunction1 step;
ConditionFunction1 cond;
TNode<Number> zero = ZeroConstant();
TNode<Number> one = OneConstant();
if (direction == ArrayReduceDirection::kLeft) {
k = zero;
step = [&](TNode<Number> i) { return NumberAdd(i, one); };
cond = [&](TNode<Number> i) { return NumberLessThan(i, original_length); };
} else {
k = NumberSubtract(original_length, one);
step = [&](TNode<Number> i) { return NumberSubtract(i, one); };
cond = [&](TNode<Number> i) { return NumberLessThanOrEqual(zero, i); };
}
ThrowIfNotCallable(
fncallback, ReducePreLoopLazyFrameState(frame_state_params, receiver,
fncallback, k, original_length));
TNode<Object> accumulator;
if (ArgumentCount() > 1) {
accumulator = Argument(1);
} else {
auto found_initial_element = MakeLabel(MachineRepresentation::kTagged,
MachineRepresentation::kTagged);
Forever(k, step).Do([&](TNode<Number> k) {
Checkpoint(ReducePreLoopEagerFrameState(frame_state_params, receiver,
fncallback, original_length));
CheckIf(cond(k), DeoptimizeReason::kNoInitialElement);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
auto continue_label = MakeLabel();
GotoIf(HoleCheck(kind, element), &continue_label);
Goto(&found_initial_element, k, TypeGuardNonInternal(element));
Bind(&continue_label);
});
Unreachable();
Bind(&found_initial_element);
k = step(found_initial_element.PhiAt<Number>(0));
accumulator = found_initial_element.PhiAt<Object>(1);
}
TNode<Object> result =
For1(k, cond, step, accumulator)
.Do([&](TNode<Number> k, TNode<Object>* accumulator) {
Checkpoint(ReduceLoopEagerFrameState(frame_state_params, receiver,
fncallback, k, original_length,
*accumulator));
MaybeInsertMapChecks(inference, has_stability_dependency);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
auto continue_label = MakeLabel(MachineRepresentation::kTagged);
element =
MaybeSkipHole(element, kind, &continue_label, *accumulator);
TNode<Number> next_k = step(k);
TNode<Object> next_accumulator = JSCall4(
fncallback, UndefinedConstant(), *accumulator, element, k,
receiver,
ReduceLoopLazyFrameState(frame_state_params, receiver,
fncallback, next_k, original_length));
Goto(&continue_label, next_accumulator);
Bind(&continue_label);
*accumulator = continue_label.PhiAt<Object>(0);
})
.Value();
return result;
}
namespace {
struct MapFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
TNode<Object> receiver;
TNode<Object> callback;
TNode<Object> this_arg;
std::optional<TNode<JSArray>> a;
TNode<Object> original_length;
};
FrameState MapPreLoopLazyFrameState(const MapFrameStateParams& params) {
DCHECK(!params.a);
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kArrayMapPreLoopLazyDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
params.outer_frame_state, ContinuationFrameStateMode::LAZY);
}
FrameState MapLoopLazyFrameState(const MapFrameStateParams& params,
TNode<Number> k) {
Node* checkpoint_params[] = {
params.receiver, params.callback, params.this_arg, *params.a, k,
params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kArrayMapLoopLazyDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
params.outer_frame_state, ContinuationFrameStateMode::LAZY);
}
FrameState MapLoopEagerFrameState(const MapFrameStateParams& params,
TNode<Number> k) {
Node* checkpoint_params[] = {
params.receiver, params.callback, params.this_arg, *params.a, k,
params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kArrayMapLoopEagerDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
params.outer_frame_state, ContinuationFrameStateMode::EAGER);
}
}
TNode<JSArray> IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeMap(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, SharedFunctionInfoRef shared,
NativeContextRef native_context) {
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> fncallback = ArgumentOrUndefined(0);
TNode<Object> this_arg = ArgumentOrUndefined(1);
TNode<Number> original_length = LoadJSArrayLength(receiver, kind);
original_length = CheckBounds(original_length,
NumberConstant(JSArray::kMaxFastArrayLength));
TNode<Object> array_ctor =
Constant(native_context.GetInitialJSArrayMap(broker(), kind)
.GetConstructor(broker()));
MapFrameStateParams frame_state_params{
jsgraph(), shared, context, target, outer_frame_state,
receiver, fncallback, this_arg, {} , original_length};
TNode<JSArray> a =
CreateArrayNoThrow(array_ctor, original_length,
MapPreLoopLazyFrameState(frame_state_params));
frame_state_params.a = a;
ThrowIfNotCallable(fncallback,
MapLoopLazyFrameState(frame_state_params, ZeroConstant()));
ForZeroUntil(original_length).Do([&](TNode<Number> k) {
Checkpoint(MapLoopEagerFrameState(frame_state_params, k));
MaybeInsertMapChecks(inference, has_stability_dependency);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
auto continue_label = MakeLabel();
element = MaybeSkipHole(element, kind, &continue_label);
TNode<Object> v = JSCall3(fncallback, this_arg, element, k, receiver,
MapLoopLazyFrameState(frame_state_params, k));
MapRef holey_double_map =
native_context.GetInitialJSArrayMap(broker(), HOLEY_DOUBLE_ELEMENTS);
MapRef holey_map =
native_context.GetInitialJSArrayMap(broker(), HOLEY_ELEMENTS);
TransitionAndStoreElement(holey_double_map, holey_map, a, k, v);
Goto(&continue_label);
Bind(&continue_label);
});
return a;
}
namespace {
struct FilterFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
TNode<Object> receiver;
TNode<Object> callback;
TNode<Object> this_arg;
TNode<JSArray> a;
TNode<Object> original_length;
};
FrameState FilterLoopLazyFrameState(const FilterFrameStateParams& params,
TNode<Number> k, TNode<Number> to,
TNode<Object> element) {
Node* checkpoint_params[] = {params.receiver,
params.callback,
params.this_arg,
params.a,
k,
params.original_length,
element,
to};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kArrayFilterLoopLazyDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
params.outer_frame_state, ContinuationFrameStateMode::LAZY);
}
FrameState FilterLoopEagerPostCallbackFrameState(
const FilterFrameStateParams& params, TNode<Number> k, TNode<Number> to,
TNode<Object> element, TNode<Object> callback_value) {
Node* checkpoint_params[] = {params.receiver,
params.callback,
params.this_arg,
params.a,
k,
params.original_length,
element,
to,
callback_value};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kArrayFilterLoopLazyDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
params.outer_frame_state, ContinuationFrameStateMode::EAGER);
}
FrameState FilterLoopEagerFrameState(const FilterFrameStateParams& params,
TNode<Number> k, TNode<Number> to) {
Node* checkpoint_params[] = {params.receiver,
params.callback,
params.this_arg,
params.a,
k,
params.original_length,
to};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kArrayFilterLoopEagerDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
params.outer_frame_state, ContinuationFrameStateMode::EAGER);
}
}
TNode<JSArray>
IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeFilter(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, SharedFunctionInfoRef shared,
NativeContextRef native_context) {
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> fncallback = ArgumentOrUndefined(0);
TNode<Object> this_arg = ArgumentOrUndefined(1);
ElementsKind filtered_kind = GetPackedElementsKind(kind);
#ifdef V8_ENABLE_UNDEFINED_DOUBLE
if (kind == ElementsKind::HOLEY_DOUBLE_ELEMENTS) {
filtered_kind = ElementsKind::HOLEY_DOUBLE_ELEMENTS;
}
#endif
TNode<JSArray> a = AllocateEmptyJSArray(filtered_kind, native_context);
TNode<Number> original_length = LoadJSArrayLength(receiver, kind);
FilterFrameStateParams frame_state_params{
jsgraph(), shared, context, target, outer_frame_state,
receiver, fncallback, this_arg, a, original_length};
TNode<Number> zero = ZeroConstant();
ThrowIfNotCallable(fncallback, FilterLoopLazyFrameState(frame_state_params,
zero, zero, zero));
TNode<Number> initial_a_length = zero;
For1ZeroUntil(original_length, initial_a_length)
.Do([&](TNode<Number> k, TNode<Object>* a_length_object) {
TNode<Number> a_length = TNode<Number>::UncheckedCast(*a_length_object);
Checkpoint(FilterLoopEagerFrameState(frame_state_params, k, a_length));
MaybeInsertMapChecks(inference, has_stability_dependency);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
auto continue_label = MakeLabel(MachineRepresentation::kTaggedSigned);
element = MaybeSkipHole(element, kind, &continue_label, a_length);
TNode<Object> v = JSCall3(
fncallback, this_arg, element, k, receiver,
FilterLoopLazyFrameState(frame_state_params, k, a_length, element));
Checkpoint(FilterLoopEagerPostCallbackFrameState(frame_state_params, k,
a_length, element, v));
GotoIfNot(ToBoolean(v), &continue_label, a_length);
{
TNode<Number> a_length1 = TypeGuardFixedArrayLength(a_length);
TNode<FixedArrayBase> elements = LoadElements(a);
elements = MaybeGrowFastElements(kind, FeedbackSource{}, a, elements,
a_length1,
LoadFixedArrayBaseLength(elements));
TNode<Number> new_a_length = NumberInc(a_length1);
StoreJSArrayLength(a, new_a_length, kind);
StoreFixedArrayBaseElement(elements, a_length1, element, kind);
Goto(&continue_label, new_a_length);
}
Bind(&continue_label);
*a_length_object =
TNode<Object>::UncheckedCast(continue_label.PhiAt(0));
})
.ValueIsUnused();
return a;
}
namespace {
struct FindFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
TNode<Object> receiver;
TNode<Object> callback;
TNode<Object> this_arg;
TNode<Object> original_length;
};
FrameState FindLoopLazyFrameState(const FindFrameStateParams& params,
TNode<Number> k, ArrayFindVariant variant) {
Builtin builtin = (variant == ArrayFindVariant::kFind)
? Builtin::kArrayFindLoopLazyDeoptContinuation
: Builtin::kArrayFindIndexLoopLazyDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, k, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
FrameState FindLoopEagerFrameState(const FindFrameStateParams& params,
TNode<Number> k, ArrayFindVariant variant) {
Builtin builtin = (variant == ArrayFindVariant::kFind)
? Builtin::kArrayFindLoopEagerDeoptContinuation
: Builtin::kArrayFindIndexLoopEagerDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, k, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::EAGER);
}
FrameState FindLoopAfterCallbackLazyFrameState(
const FindFrameStateParams& params, TNode<Number> next_k,
TNode<Object> if_found_value, ArrayFindVariant variant) {
Builtin builtin =
(variant == ArrayFindVariant::kFind)
? Builtin::kArrayFindLoopAfterCallbackLazyDeoptContinuation
: Builtin::kArrayFindIndexLoopAfterCallbackLazyDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, next_k,
params.original_length, if_found_value};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
}
TNode<Object> IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeFind(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, SharedFunctionInfoRef shared,
NativeContextRef native_context, ArrayFindVariant variant) {
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> fncallback = ArgumentOrUndefined(0);
TNode<Object> this_arg = ArgumentOrUndefined(1);
TNode<Number> original_length = LoadJSArrayLength(receiver, kind);
FindFrameStateParams frame_state_params{
jsgraph(), shared, context, target, outer_frame_state,
receiver, fncallback, this_arg, original_length};
ThrowIfNotCallable(
fncallback,
FindLoopLazyFrameState(frame_state_params, ZeroConstant(), variant));
const bool is_find_variant = (variant == ArrayFindVariant::kFind);
auto out = MakeLabel(MachineRepresentation::kTagged);
ForZeroUntil(original_length).Do([&](TNode<Number> k) {
Checkpoint(FindLoopEagerFrameState(frame_state_params, k, variant));
MaybeInsertMapChecks(inference, has_stability_dependency);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
if (IsHoleyElementsKind(kind)) {
element = ConvertHoleToUndefined(element, kind);
}
TNode<Object> if_found_value = is_find_variant ? element : k;
TNode<Number> next_k = NumberInc(k);
TNode<Object> v =
JSCall3(fncallback, this_arg, element, k, receiver,
FindLoopAfterCallbackLazyFrameState(frame_state_params, next_k,
if_found_value, variant));
GotoIf(ToBoolean(v), &out, if_found_value);
});
TNode<Object> if_not_found_value =
is_find_variant ? TNode<Object>::UncheckedCast(UndefinedConstant())
: TNode<Object>::UncheckedCast(MinusOneConstant());
Goto(&out, if_not_found_value);
Bind(&out);
return out.PhiAt<Object>(0);
}
namespace {
struct EverySomeFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
TNode<Object> receiver;
TNode<Object> callback;
TNode<Object> this_arg;
TNode<Object> original_length;
};
FrameState EverySomeLoopLazyFrameState(const EverySomeFrameStateParams& params,
TNode<Number> k,
ArrayEverySomeVariant variant) {
Builtin builtin = (variant == ArrayEverySomeVariant::kEvery)
? Builtin::kArrayEveryLoopLazyDeoptContinuation
: Builtin::kArraySomeLoopLazyDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, k, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
FrameState EverySomeLoopEagerFrameState(const EverySomeFrameStateParams& params,
TNode<Number> k,
ArrayEverySomeVariant variant) {
Builtin builtin = (variant == ArrayEverySomeVariant::kEvery)
? Builtin::kArrayEveryLoopEagerDeoptContinuation
: Builtin::kArraySomeLoopEagerDeoptContinuation;
Node* checkpoint_params[] = {params.receiver, params.callback,
params.this_arg, k, params.original_length};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared, builtin, params.target, params.context,
checkpoint_params, arraysize(checkpoint_params), params.outer_frame_state,
ContinuationFrameStateMode::EAGER);
}
}
TNode<Boolean>
IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeEverySome(
MapInference* inference, const bool has_stability_dependency,
ElementsKind kind, SharedFunctionInfoRef shared,
NativeContextRef native_context, ArrayEverySomeVariant variant) {
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> fncallback = ArgumentOrUndefined(0);
TNode<Object> this_arg = ArgumentOrUndefined(1);
TNode<Number> original_length = LoadJSArrayLength(receiver, kind);
EverySomeFrameStateParams frame_state_params{
jsgraph(), shared, context, target, outer_frame_state,
receiver, fncallback, this_arg, original_length};
ThrowIfNotCallable(
fncallback,
EverySomeLoopLazyFrameState(frame_state_params, ZeroConstant(), variant));
auto out = MakeLabel(MachineRepresentation::kTagged);
ForZeroUntil(original_length).Do([&](TNode<Number> k) {
Checkpoint(EverySomeLoopEagerFrameState(frame_state_params, k, variant));
MaybeInsertMapChecks(inference, has_stability_dependency);
TNode<Object> element;
std::tie(k, element) = SafeLoadElement(kind, receiver, k);
auto continue_label = MakeLabel();
element = MaybeSkipHole(element, kind, &continue_label);
TNode<Object> v =
JSCall3(fncallback, this_arg, element, k, receiver,
EverySomeLoopLazyFrameState(frame_state_params, k, variant));
if (variant == ArrayEverySomeVariant::kEvery) {
GotoIfNot(ToBoolean(v), &out, FalseConstant());
} else {
DCHECK_EQ(variant, ArrayEverySomeVariant::kSome);
GotoIf(ToBoolean(v), &out, TrueConstant());
}
Goto(&continue_label);
Bind(&continue_label);
});
Goto(&out, (variant == ArrayEverySomeVariant::kEvery) ? TrueConstant()
: FalseConstant());
Bind(&out);
return out.PhiAt<Boolean>(0);
}
namespace {
Callable GetCallableForArrayIndexOfIncludes(ArrayIndexOfIncludesVariant variant,
ElementsKind elements_kind,
Isolate* isolate) {
if (variant == ArrayIndexOfIncludesVariant::kIndexOf) {
switch (elements_kind) {
case PACKED_SMI_ELEMENTS:
case HOLEY_SMI_ELEMENTS:
return Builtins::CallableFor(isolate, Builtin::kArrayIndexOfSmi);
case PACKED_ELEMENTS:
case HOLEY_ELEMENTS:
return Builtins::CallableFor(isolate,
Builtin::kArrayIndexOfSmiOrObject);
case PACKED_DOUBLE_ELEMENTS:
return Builtins::CallableFor(isolate,
Builtin::kArrayIndexOfPackedDoubles);
default:
DCHECK_EQ(HOLEY_DOUBLE_ELEMENTS, elements_kind);
return Builtins::CallableFor(isolate,
Builtin::kArrayIndexOfHoleyDoubles);
}
} else {
DCHECK_EQ(variant, ArrayIndexOfIncludesVariant::kIncludes);
switch (elements_kind) {
case PACKED_SMI_ELEMENTS:
case HOLEY_SMI_ELEMENTS:
return Builtins::CallableFor(isolate, Builtin::kArrayIncludesSmi);
case PACKED_ELEMENTS:
case HOLEY_ELEMENTS:
return Builtins::CallableFor(isolate,
Builtin::kArrayIncludesSmiOrObject);
case PACKED_DOUBLE_ELEMENTS:
return Builtins::CallableFor(isolate,
Builtin::kArrayIncludesPackedDoubles);
default:
DCHECK_EQ(HOLEY_DOUBLE_ELEMENTS, elements_kind);
return Builtins::CallableFor(isolate,
Builtin::kArrayIncludesHoleyDoubles);
}
}
UNREACHABLE();
}
}
TNode<Object>
IteratingArrayBuiltinReducerAssembler::ReduceArrayPrototypeIndexOfIncludes(
ElementsKind kind, ArrayIndexOfIncludesVariant variant) {
TNode<Context> context = ContextInput();
TNode<JSArray> receiver = ReceiverInputAs<JSArray>();
TNode<Object> search_element = ArgumentOrUndefined(0);
TNode<Object> from_index = ArgumentOrZero(1);
TNode<Number> length = LoadJSArrayLength(receiver, kind);
TNode<FixedArrayBase> elements = LoadElements(receiver);
const bool have_from_index = ArgumentCount() > 1;
if (have_from_index) {
TNode<Smi> from_index_smi = CheckSmi(from_index);
TNode<Boolean> cond = NumberLessThan(from_index_smi, ZeroConstant());
from_index = SelectIf<Number>(cond)
.Then(_ {
return NumberMax(NumberAdd(length, from_index_smi),
ZeroConstant());
})
.Else(_ { return from_index_smi; })
.ExpectFalse()
.Value();
}
return Call4(GetCallableForArrayIndexOfIncludes(variant, kind, isolate()),
context, elements, search_element, length, from_index);
}
namespace {
struct PromiseCtorFrameStateParams {
JSGraph* jsgraph;
SharedFunctionInfoRef shared;
Node* node_ptr;
TNode<Context> context;
TNode<Object> target;
FrameState outer_frame_state;
};
FrameState CreateConstructInvokeStubFrameState(
Node* node, Node* outer_frame_state, SharedFunctionInfoRef shared,
Node* context, CommonOperatorBuilder* common, TFGraph* graph) {
const FrameStateFunctionInfo* state_info =
common->CreateFrameStateFunctionInfo(FrameStateType::kConstructInvokeStub,
1, 0, 0, shared.object(), {});
const Operator* op = common->FrameState(
BytecodeOffset::None(), OutputFrameStateCombine::Ignore(), state_info);
const Operator* op0 = common->StateValues(0, SparseInputMask::Dense());
Node* node0 = graph->NewNode(op0);
static constexpr int kTargetInputIndex = 0;
static constexpr int kReceiverInputIndex = 1;
std::vector<Node*> params;
params.push_back(node->InputAt(kReceiverInputIndex));
const Operator* op_param = common->StateValues(
static_cast<int>(params.size()), SparseInputMask::Dense());
Node* params_node = graph->NewNode(op_param, static_cast<int>(params.size()),
¶ms.front());
DCHECK(context);
return FrameState(graph->NewNode(op, params_node, node0, node0, context,
node->InputAt(kTargetInputIndex),
outer_frame_state));
}
FrameState PromiseConstructorFrameState(
const PromiseCtorFrameStateParams& params, CommonOperatorBuilder* common,
TFGraph* graph) {
DCHECK_EQ(1,
params.shared
.internal_formal_parameter_count_without_receiver_deprecated());
return CreateConstructInvokeStubFrameState(
params.node_ptr, params.outer_frame_state, params.shared, params.context,
common, graph);
}
FrameState PromiseConstructorLazyFrameState(
const PromiseCtorFrameStateParams& params,
FrameState constructor_frame_state) {
JSGraph* jsgraph = params.jsgraph;
Node* checkpoint_params[] = {
jsgraph->UndefinedConstant(),
jsgraph->UndefinedConstant(),
jsgraph->UndefinedConstant(),
jsgraph->TheHoleConstant()
};
return CreateJavaScriptBuiltinContinuationFrameState(
jsgraph, params.shared, Builtin::kPromiseConstructorLazyDeoptContinuation,
params.target, params.context, checkpoint_params,
arraysize(checkpoint_params), constructor_frame_state,
ContinuationFrameStateMode::LAZY);
}
FrameState PromiseConstructorLazyWithCatchFrameState(
const PromiseCtorFrameStateParams& params,
FrameState constructor_frame_state, TNode<JSPromise> promise,
TNode<JSFunction> reject) {
Node* checkpoint_params[] = {
params.jsgraph->UndefinedConstant(),
promise, reject};
return CreateJavaScriptBuiltinContinuationFrameState(
params.jsgraph, params.shared,
Builtin::kPromiseConstructorLazyDeoptContinuation, params.target,
params.context, checkpoint_params, arraysize(checkpoint_params),
constructor_frame_state, ContinuationFrameStateMode::LAZY_WITH_CATCH);
}
}
TNode<Object> PromiseBuiltinReducerAssembler::ReducePromiseConstructor(
NativeContextRef native_context) {
DCHECK_GE(ConstructArity(), 1);
JSConstructNode n(node_ptr());
FrameState outer_frame_state = FrameStateInput();
TNode<Context> context = ContextInput();
TNode<Object> target = TargetInput();
TNode<Object> executor = n.Argument(0);
DCHECK_EQ(target, NewTargetInput());
SharedFunctionInfoRef promise_shared =
native_context.promise_function(broker()).shared(broker());
PromiseCtorFrameStateParams frame_state_params{jsgraph(), promise_shared,
node_ptr(), context,
target, outer_frame_state};
FrameState constructor_frame_state =
PromiseConstructorFrameState(frame_state_params, common(), graph());
IfNot(ObjectIsCallable(executor))
.Then((_ {
JSCallRuntime2(
Runtime::kThrowTypeError,
SmiConstant(
Smi::FromEnum(MessageTemplate::kResolverNotAFunction).value()),
executor, context,
PromiseConstructorLazyFrameState(frame_state_params,
constructor_frame_state));
}))
.ExpectTrue();
TNode<JSPromise> promise = CreatePromise(context);
TNode<Context> promise_context = CreateFunctionContext(
native_context, context, PromiseBuiltins::kPromiseContextLength);
StoreContextNoCellSlot(promise_context, PromiseBuiltins::kPromiseSlot,
promise);
StoreContextNoCellSlot(promise_context, PromiseBuiltins::kAlreadyResolvedSlot,
FalseConstant());
StoreContextNoCellSlot(promise_context, PromiseBuiltins::kDebugEventSlot,
TrueConstant());
SharedFunctionInfoRef resolve_sfi =
MakeRef(broker(), broker()
->isolate()
->factory()
->promise_capability_default_resolve_shared_fun());
TNode<JSFunction> resolve =
CreateClosureFromBuiltinSharedFunctionInfo(resolve_sfi, promise_context);
SharedFunctionInfoRef reject_sfi =
MakeRef(broker(), broker()
->isolate()
->factory()
->promise_capability_default_reject_shared_fun());
TNode<JSFunction> reject =
CreateClosureFromBuiltinSharedFunctionInfo(reject_sfi, promise_context);
FrameState lazy_with_catch_frame_state =
PromiseConstructorLazyWithCatchFrameState(
frame_state_params, constructor_frame_state, promise, reject);
Try(_ {
CallPromiseExecutor(executor, resolve, reject, lazy_with_catch_frame_state);
}).Catch([&](TNode<Object> exception) {
ClearPendingMessage();
CallPromiseReject(reject, exception, lazy_with_catch_frame_state);
});
return promise;
}
#undef _
std::pair<Node*, Node*> JSCallReducer::ReleaseEffectAndControlFromAssembler(
JSCallReducerAssembler* gasm) {
auto catch_scope = gasm->catch_scope();
DCHECK(catch_scope->is_outermost());
if (catch_scope->has_handler() &&
catch_scope->has_exceptional_control_flow()) {
TNode<Object> handler_exception;
Effect handler_effect{nullptr};
Control handler_control{nullptr};
gasm->catch_scope()->MergeExceptionalPaths(
&handler_exception, &handler_effect, &handler_control);
ReplaceWithValue(gasm->outermost_handler(), handler_exception,
handler_effect, handler_control);
}
return {gasm->effect(), gasm->control()};
}
Reduction JSCallReducer::ReplaceWithSubgraph(JSCallReducerAssembler* gasm,
Node* subgraph) {
ReplaceWithValue(gasm->node_ptr(), subgraph, gasm->effect(), gasm->control());
auto catch_scope = gasm->catch_scope();
DCHECK(catch_scope->is_outermost());
if (catch_scope->has_handler() &&
catch_scope->has_exceptional_control_flow()) {
TNode<Object> handler_exception;
Effect handler_effect{nullptr};
Control handler_control{nullptr};
gasm->catch_scope()->MergeExceptionalPaths(
&handler_exception, &handler_effect, &handler_control);
ReplaceWithValue(gasm->outermost_handler(), handler_exception,
handler_effect, handler_control);
}
return Replace(subgraph);
}
Reduction JSCallReducer::ReduceMathUnary(Node* node, const Operator* op) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->NaNConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceMathUnary(op);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceMathBinary(Node* node, const Operator* op) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->NaNConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceMathBinary(op);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceMathImul(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->ZeroConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Node* left = n.Argument(0);
Node* right = n.ArgumentOr(1, jsgraph()->ZeroConstant());
Effect effect = n.effect();
Control control = n.control();
left = effect =
graph()->NewNode(simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
left, effect, control);
right = effect =
graph()->NewNode(simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
right, effect, control);
left = graph()->NewNode(simplified()->NumberToUint32(), left);
right = graph()->NewNode(simplified()->NumberToUint32(), right);
Node* value = graph()->NewNode(simplified()->NumberImul(), left, right);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
Reduction JSCallReducer::ReduceMathClz32(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->ConstantNoHole(32);
ReplaceWithValue(node, value);
return Replace(value);
}
Node* input = n.Argument(0);
Effect effect = n.effect();
Control control = n.control();
input = effect =
graph()->NewNode(simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
input, effect, control);
input = graph()->NewNode(simplified()->NumberToUint32(), input);
Node* value = graph()->NewNode(simplified()->NumberClz32(), input);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
Reduction JSCallReducer::ReduceMathMinMax(Node* node, const Operator* op,
Node* empty_value) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
ReplaceWithValue(node, empty_value);
return Replace(empty_value);
}
Node* effect = NodeProperties::GetEffectInput(node);
Node* control = NodeProperties::GetControlInput(node);
Node* value = effect =
graph()->NewNode(simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
n.Argument(0), effect, control);
for (int i = 1; i < n.ArgumentCount(); i++) {
Node* input = effect = graph()->NewNode(
simplified()->SpeculativeToNumber(NumberOperationHint::kNumberOrOddball,
p.feedback()),
n.Argument(i), effect, control);
value = graph()->NewNode(op, value, input);
}
ReplaceWithValue(node, value, effect);
return Replace(value);
}
Reduction JSCallReducer::Reduce(Node* node) {
switch (node->opcode()) {
case IrOpcode::kJSConstruct:
return ReduceJSConstruct(node);
case IrOpcode::kJSConstructWithArrayLike:
return ReduceJSConstructWithArrayLike(node);
case IrOpcode::kJSConstructWithSpread:
return ReduceJSConstructWithSpread(node);
case IrOpcode::kJSConstructForwardAllArgs:
return ReduceJSConstructForwardAllArgs(node);
case IrOpcode::kJSCall:
return ReduceJSCall(node);
case IrOpcode::kJSCallWithArrayLike:
return ReduceJSCallWithArrayLike(node);
case IrOpcode::kJSCallWithSpread:
return ReduceJSCallWithSpread(node);
default:
break;
}
return NoChange();
}
void JSCallReducer::Finalize() {
std::set<Node*> const waitlist = std::move(waitlist_);
for (Node* node : waitlist) {
if (!node->IsDead()) {
NodeId const max_id = static_cast<NodeId>(graph()->NodeCount() - 1);
Reduction const reduction = Reduce(node);
if (reduction.Changed()) {
Node* replacement = reduction.replacement();
if (replacement != node) {
Replace(node, replacement, max_id);
}
}
}
}
}
Reduction JSCallReducer::ReduceArrayConstructor(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() == SpeculationMode::kDisallowSpeculation) {
return NoChange();
}
Node* target = n.target();
size_t const arity = p.arity_without_implicit_args();
node->RemoveInput(n.FeedbackVectorIndex());
NodeProperties::ReplaceValueInput(node, target, 0);
NodeProperties::ReplaceValueInput(node, target, 1);
NodeProperties::ChangeOp(
node, javascript()->CreateArray(arity, std::nullopt,
n.Parameters().feedback()));
return Changed(node);
}
Reduction JSCallReducer::ReduceBooleanConstructor(Node* node) {
JSCallNode n(node);
Node* value = n.ArgumentOrUndefined(0, jsgraph());
value = graph()->NewNode(simplified()->ToBoolean(), value);
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceObjectConstructor(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) return NoChange();
Node* value = n.Argument(0);
Effect effect = n.effect();
if (NodeProperties::CanBePrimitive(broker(), value, effect)) {
if (!NodeProperties::CanBeNullOrUndefined(broker(), value, effect)) {
NodeProperties::ReplaceValueInputs(node, value);
NodeProperties::ChangeOp(node, javascript()->ToObject());
return Changed(node);
}
} else {
ReplaceWithValue(node, value);
return Replace(value);
}
return NoChange();
}
Reduction JSCallReducer::ReduceFunctionPrototypeApply(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
CallFeedbackRelation new_feedback_relation =
p.feedback_relation() == CallFeedbackRelation::kReceiver
? CallFeedbackRelation::kTarget
: CallFeedbackRelation::kUnrelated;
int arity = p.arity_without_implicit_args();
if (arity < 2) {
ConvertReceiverMode convert_mode;
if (arity == 0) {
convert_mode = ConvertReceiverMode::kNullOrUndefined;
node->ReplaceInput(n.TargetIndex(), n.receiver());
node->ReplaceInput(n.ReceiverIndex(), jsgraph()->UndefinedConstant());
} else {
DCHECK_EQ(arity, 1);
convert_mode = ConvertReceiverMode::kAny;
node->RemoveInput(n.TargetIndex());
--arity;
}
NodeProperties::ChangeOp(
node, javascript()->Call(JSCallNode::ArityForArgc(arity), p.frequency(),
p.feedback(), convert_mode,
p.speculation_mode(), new_feedback_relation));
return Changed(node).FollowedBy(ReduceJSCall(node));
}
Node* target = n.receiver();
Node* this_argument = n.Argument(0);
Node* arguments_list = n.Argument(1);
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
if (!NodeProperties::CanBeNullOrUndefined(broker(), arguments_list, effect)) {
node->ReplaceInput(n.TargetIndex(), target);
node->ReplaceInput(n.ReceiverIndex(), this_argument);
node->ReplaceInput(n.ArgumentIndex(0), arguments_list);
while (arity-- > 1) node->RemoveInput(n.ArgumentIndex(1));
NodeProperties::ChangeOp(
node, javascript()->CallWithArrayLike(p.frequency(), p.feedback(),
p.speculation_mode(),
new_feedback_relation));
return Changed(node).FollowedBy(ReduceJSCallWithArrayLike(node));
}
Node* check_null =
graph()->NewNode(simplified()->ReferenceEqual(), arguments_list,
jsgraph()->NullConstant());
control = graph()->NewNode(common()->Branch(BranchHint::kFalse), check_null,
control);
Node* if_null = graph()->NewNode(common()->IfTrue(), control);
control = graph()->NewNode(common()->IfFalse(), control);
Node* check_undefined =
graph()->NewNode(simplified()->ReferenceEqual(), arguments_list,
jsgraph()->UndefinedConstant());
control = graph()->NewNode(common()->Branch(BranchHint::kFalse),
check_undefined, control);
Node* if_undefined = graph()->NewNode(common()->IfTrue(), control);
control = graph()->NewNode(common()->IfFalse(), control);
Node* effect0 = effect;
Node* control0 = control;
Node* value0 = effect0 = control0 = graph()->NewNode(
javascript()->CallWithArrayLike(p.frequency(), p.feedback(),
p.speculation_mode(),
new_feedback_relation),
target, this_argument, arguments_list, n.feedback_vector(), context,
frame_state, effect0, control0);
Node* effect1 = effect;
Node* control1 = graph()->NewNode(common()->Merge(2), if_null, if_undefined);
Node* value1 = effect1 = control1 = graph()->NewNode(
javascript()->Call(JSCallNode::ArityForArgc(0)), target, this_argument,
n.feedback_vector(), context, frame_state, effect1, control1);
Node* if_exception = nullptr;
if (NodeProperties::IsExceptionalCall(node, &if_exception)) {
Node* if_exception0 =
graph()->NewNode(common()->IfException(), control0, effect0);
control0 = graph()->NewNode(common()->IfSuccess(), control0);
Node* if_exception1 =
graph()->NewNode(common()->IfException(), control1, effect1);
control1 = graph()->NewNode(common()->IfSuccess(), control1);
Node* merge =
graph()->NewNode(common()->Merge(2), if_exception0, if_exception1);
Node* ephi = graph()->NewNode(common()->EffectPhi(2), if_exception0,
if_exception1, merge);
Node* phi =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
if_exception0, if_exception1, merge);
ReplaceWithValue(if_exception, phi, ephi, merge);
}
control = graph()->NewNode(common()->Merge(2), control0, control1);
effect = graph()->NewNode(common()->EffectPhi(2), effect0, effect1, control);
Node* value =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2), value0,
value1, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceFunctionPrototypeBind(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
MapRef first_receiver_map = receiver_maps[0];
bool const is_constructor = first_receiver_map.is_constructor();
HeapObjectRef prototype = first_receiver_map.prototype(broker());
for (MapRef receiver_map : receiver_maps) {
HeapObjectRef map_prototype = receiver_map.prototype(broker());
if (!map_prototype.equals(prototype) ||
receiver_map.is_constructor() != is_constructor ||
!InstanceTypeChecker::IsJSFunctionOrBoundFunctionOrWrappedFunction(
receiver_map.instance_type())) {
return inference.NoChange();
}
if (receiver_map.is_dictionary_map()) return inference.NoChange();
int minimum_nof_descriptors =
std::max(
{JSFunctionOrBoundFunctionOrWrappedFunction::kLengthDescriptorIndex,
JSFunctionOrBoundFunctionOrWrappedFunction::
kNameDescriptorIndex}) +
1;
if (receiver_map.NumberOfOwnDescriptors() < minimum_nof_descriptors) {
return inference.NoChange();
}
const InternalIndex kLengthIndex(
JSFunctionOrBoundFunctionOrWrappedFunction::kLengthDescriptorIndex);
const InternalIndex kNameIndex(
JSFunctionOrBoundFunctionOrWrappedFunction::kNameDescriptorIndex);
StringRef length_string = broker()->length_string();
StringRef name_string = broker()->name_string();
OptionalObjectRef length_value(
receiver_map.GetStrongValue(broker(), kLengthIndex));
OptionalObjectRef name_value(
receiver_map.GetStrongValue(broker(), kNameIndex));
if (!length_value || !name_value) {
TRACE_BROKER_MISSING(
broker(), "name or length descriptors on map " << receiver_map);
return inference.NoChange();
}
if (!receiver_map.GetPropertyKey(broker(), kLengthIndex)
.equals(length_string) ||
!length_value->IsAccessorInfo() ||
!receiver_map.GetPropertyKey(broker(), kNameIndex)
.equals(name_string) ||
!name_value->IsAccessorInfo()) {
return inference.NoChange();
}
}
MapRef map =
is_constructor
? native_context().bound_function_with_constructor_map(broker())
: native_context().bound_function_without_constructor_map(broker());
if (!map.prototype(broker()).equals(prototype)) return inference.NoChange();
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
static constexpr int kBoundThis = 1;
static constexpr int kReceiverContextEffectAndControl = 4;
int const arity = n.ArgumentCount();
if (arity > 0) {
MapRef fixed_array_map = broker()->fixed_array_map();
AllocationBuilder ab(jsgraph(), broker(), effect, control);
if (!ab.CanAllocateArray(arity, fixed_array_map)) {
return NoChange();
}
}
int const arity_with_bound_this = std::max(arity, kBoundThis);
int const input_count =
arity_with_bound_this + kReceiverContextEffectAndControl;
Node** inputs = graph()->zone()->AllocateArray<Node*>(input_count);
int cursor = 0;
inputs[cursor++] = receiver;
inputs[cursor++] = n.ArgumentOrUndefined(0, jsgraph());
for (int i = 1; i < arity; ++i) {
inputs[cursor++] = n.Argument(i);
}
inputs[cursor++] = context;
inputs[cursor++] = effect;
inputs[cursor++] = control;
DCHECK_EQ(cursor, input_count);
Node* value = effect =
graph()->NewNode(javascript()->CreateBoundFunction(
arity_with_bound_this - kBoundThis, map),
input_count, inputs);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceFunctionPrototypeCall(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
Node* target = n.target();
Effect effect = n.effect();
Control control = n.control();
Node* context;
HeapObjectMatcher m(target);
if (m.HasResolvedValue() && m.Ref(broker()).IsJSFunction()) {
JSFunctionRef function = m.Ref(broker()).AsJSFunction();
context = jsgraph()->ConstantNoHole(function.context(broker()), broker());
} else {
context = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSFunctionContext()), target,
effect, control);
}
NodeProperties::ReplaceContextInput(node, context);
NodeProperties::ReplaceEffectInput(node, effect);
int arity = p.arity_without_implicit_args();
ConvertReceiverMode convert_mode;
if (arity == 0) {
convert_mode = ConvertReceiverMode::kNullOrUndefined;
node->ReplaceInput(n.TargetIndex(), n.receiver());
node->ReplaceInput(n.ReceiverIndex(), jsgraph()->UndefinedConstant());
} else {
convert_mode = ConvertReceiverMode::kAny;
node->RemoveInput(n.TargetIndex());
--arity;
}
NodeProperties::ChangeOp(
node, javascript()->Call(JSCallNode::ArityForArgc(arity), p.frequency(),
p.feedback(), convert_mode, p.speculation_mode(),
CallFeedbackRelation::kUnrelated));
return Changed(node).FollowedBy(ReduceJSCall(node));
}
Reduction JSCallReducer::ReduceFunctionPrototypeHasInstance(Node* node) {
JSCallNode n(node);
Node* receiver = n.receiver();
Node* object = n.ArgumentOrUndefined(0, jsgraph());
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
node->ReplaceInput(0, receiver);
node->ReplaceInput(1, object);
node->ReplaceInput(2, context);
node->ReplaceInput(3, frame_state);
node->ReplaceInput(4, effect);
node->ReplaceInput(5, control);
node->TrimInputCount(6);
NodeProperties::ChangeOp(node, javascript()->OrdinaryHasInstance());
return Changed(node);
}
Reduction JSCallReducer::ReduceObjectGetPrototype(Node* node, Node* object) {
Effect effect{NodeProperties::GetEffectInput(node)};
MapInference inference(broker(), object, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& object_maps = inference.GetMaps();
MapRef candidate_map = object_maps[0];
HeapObjectRef candidate_prototype = candidate_map.prototype(broker());
for (size_t i = 0; i < object_maps.size(); ++i) {
MapRef object_map = object_maps[i];
HeapObjectRef map_prototype = object_map.prototype(broker());
if (IsSpecialReceiverInstanceType(object_map.instance_type()) ||
!map_prototype.equals(candidate_prototype)) {
return inference.NoChange();
}
DCHECK(!object_map.IsPrimitiveMap() && object_map.IsJSReceiverMap());
}
if (!inference.RelyOnMapsViaStability(dependencies())) {
return inference.NoChange();
}
Node* value = jsgraph()->ConstantNoHole(candidate_prototype, broker());
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceObjectGetPrototypeOf(Node* node) {
JSCallNode n(node);
Node* object = n.ArgumentOrUndefined(0, jsgraph());
return ReduceObjectGetPrototype(node, object);
}
Reduction JSCallReducer::ReduceObjectIs(Node* node) {
JSCallNode n(node);
Node* lhs = n.ArgumentOrUndefined(0, jsgraph());
Node* rhs = n.ArgumentOrUndefined(1, jsgraph());
Node* value = graph()->NewNode(simplified()->SameValue(), lhs, rhs);
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceObjectPrototypeGetProto(Node* node) {
JSCallNode n(node);
return ReduceObjectGetPrototype(node, n.receiver());
}
Reduction JSCallReducer::ReduceObjectPrototypeHasOwnProperty(Node* node) {
JSCallNode call_node(node);
Node* receiver = call_node.receiver();
Node* name = call_node.ArgumentOrUndefined(0, jsgraph());
Effect effect = call_node.effect();
Control control = call_node.control();
if (name->opcode() == IrOpcode::kJSForInNext) {
JSForInNextNode n(name);
if (n.Parameters().mode() != ForInMode::kGeneric) {
Node* object = n.receiver();
Node* cache_type = n.cache_type();
if (object->opcode() == IrOpcode::kJSToObject) {
object = NodeProperties::GetValueInput(object, 0);
}
if (object == receiver) {
if (!NodeProperties::NoObservableSideEffectBetween(effect, name)) {
Node* receiver_map = effect =
graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()),
receiver, effect, control);
Node* check = graph()->NewNode(simplified()->ReferenceEqual(),
receiver_map, cache_type);
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kWrongMap), check, effect,
control);
}
Node* value = jsgraph()->TrueConstant();
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
if (receiver->opcode() == IrOpcode::kHeapConstant) {
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) {
return inference.NoChange();
}
const ZoneRefSet<Map>& receiver_maps = inference.GetMaps();
if (receiver_maps.size() == 1) {
const MapRef receiver_map = *receiver_maps.begin();
InstanceType instance_type = receiver_map.instance_type();
int const nof = receiver_map.NumberOfOwnDescriptors();
if (nof > 4 || instance_type <= LAST_SPECIAL_RECEIVER_TYPE ||
receiver_map.is_dictionary_map()) {
return inference.NoChange();
}
CallParameters const& p = call_node.Parameters();
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(),
&effect, control, p.feedback());
#define __ gasm.
JSGraphAssembler gasm(broker(), jsgraph(), jsgraph()->zone(),
BranchSemantics::kJS);
gasm.InitializeEffectControl(effect, control);
auto done = __ MakeLabel(MachineRepresentation::kTagged);
const DescriptorArrayRef descriptor_array =
receiver_map.instance_descriptors(broker());
for (InternalIndex key_index : InternalIndex::Range(nof)) {
NameRef receiver_key =
descriptor_array.GetPropertyKey(broker(), key_index);
Node* lhs = jsgraph()->HeapConstantNoHole(receiver_key.object());
__ GotoIf(__ ReferenceEqual(TNode<Object>::UncheckedCast(lhs),
TNode<Object>::UncheckedCast(name)),
&done, __ TrueConstant());
}
__ Goto(&done, __ FalseConstant());
__ Bind(&done);
Node* value = done.PhiAt(0);
ReplaceWithValue(node, value, gasm.effect(), gasm.control());
return Replace(value);
#undef __
}
return inference.NoChange();
}
}
}
return NoChange();
}
Reduction JSCallReducer::ReduceObjectPrototypeIsPrototypeOf(Node* node) {
JSCallNode n(node);
Node* receiver = n.receiver();
Node* value = n.ArgumentOrUndefined(0, jsgraph());
Effect effect = n.effect();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAreJSReceiver()) {
return NoChange();
}
NodeProperties::ReplaceValueInput(node, value, n.TargetIndex());
for (int i = node->op()->ValueInputCount(); i > 2; i--) {
node->RemoveInput(2);
}
NodeProperties::ChangeOp(node, javascript()->HasInPrototypeChain());
return Changed(node);
}
Reduction JSCallReducer::ReduceReflectApply(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
int arity = p.arity_without_implicit_args();
static_assert(n.ReceiverIndex() > n.TargetIndex());
node->RemoveInput(n.ReceiverIndex());
node->RemoveInput(n.TargetIndex());
while (arity < 3) {
node->InsertInput(graph()->zone(), arity++, jsgraph()->UndefinedConstant());
}
while (arity-- > 3) {
node->RemoveInput(arity);
}
NodeProperties::ChangeOp(
node, javascript()->CallWithArrayLike(p.frequency(), p.feedback(),
p.speculation_mode(),
CallFeedbackRelation::kUnrelated));
return Changed(node).FollowedBy(ReduceJSCallWithArrayLike(node));
}
Reduction JSCallReducer::ReduceReflectConstruct(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
int arity = p.arity_without_implicit_args();
TNode<Object> arg_target = n.ArgumentOrUndefined(0, jsgraph());
TNode<Object> arg_argument_list = n.ArgumentOrUndefined(1, jsgraph());
TNode<Object> arg_new_target = n.ArgumentOr(2, arg_target);
static_assert(n.ReceiverIndex() > n.TargetIndex());
node->RemoveInput(n.ReceiverIndex());
node->RemoveInput(n.TargetIndex());
static_assert(JSConstructNode::FirstArgumentIndex() == 2);
while (arity < 3) {
node->InsertInput(graph()->zone(), arity++, jsgraph()->UndefinedConstant());
}
while (arity-- > 3) {
node->RemoveInput(arity);
}
static_assert(JSConstructNode::TargetIndex() == 0);
static_assert(JSConstructNode::NewTargetIndex() == 1);
static_assert(JSConstructNode::kFeedbackVectorIsLastInput);
node->ReplaceInput(JSConstructNode::TargetIndex(), arg_target);
node->ReplaceInput(JSConstructNode::NewTargetIndex(), arg_new_target);
node->ReplaceInput(JSConstructNode::ArgumentIndex(0), arg_argument_list);
NodeProperties::ChangeOp(
node, javascript()->ConstructWithArrayLike(p.frequency(), p.feedback()));
return Changed(node).FollowedBy(ReduceJSConstructWithArrayLike(node));
}
Reduction JSCallReducer::ReduceReflectGetPrototypeOf(Node* node) {
JSCallNode n(node);
Node* target = n.ArgumentOrUndefined(0, jsgraph());
return ReduceObjectGetPrototype(node, target);
}
Reduction JSCallReducer::ReduceObjectCreate(Node* node) {
JSCallNode n(node);
Node* properties = n.ArgumentOrUndefined(1, jsgraph());
if (properties != jsgraph()->UndefinedConstant()) return NoChange();
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
Node* prototype = n.ArgumentOrUndefined(0, jsgraph());
node->ReplaceInput(0, prototype);
node->ReplaceInput(1, context);
node->ReplaceInput(2, frame_state);
node->ReplaceInput(3, effect);
node->ReplaceInput(4, control);
node->TrimInputCount(5);
NodeProperties::ChangeOp(node, javascript()->CreateObject());
return Changed(node);
}
Reduction JSCallReducer::ReduceReflectGet(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
int arity = p.arity_without_implicit_args();
if (arity != 2) return NoChange();
Node* target = n.Argument(0);
Node* key = n.Argument(1);
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), target);
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
{
if_false = efalse = graph()->NewNode(
javascript()->CallRuntime(Runtime::kThrowTypeError, 2),
jsgraph()->ConstantNoHole(
static_cast<int>(MessageTemplate::kCalledOnNonObject)),
jsgraph()->HeapConstantNoHole(factory()->ReflectGet_string()), context,
frame_state, efalse, if_false);
}
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* vtrue;
{
Callable callable = Builtins::CallableFor(isolate(), Builtin::kGetProperty);
auto call_descriptor = Linkage::GetStubCallDescriptor(
graph()->zone(), callable.descriptor(),
callable.descriptor().GetStackParameterCount(),
CallDescriptor::kNeedsFrameState, Operator::kNoProperties);
Node* stub_code = jsgraph()->HeapConstantNoHole(callable.code());
vtrue = etrue = if_true =
graph()->NewNode(common()->Call(call_descriptor), stub_code, target,
key, context, frame_state, etrue, if_true);
}
Node* on_exception = nullptr;
if (NodeProperties::IsExceptionalCall(node, &on_exception)) {
Node* extrue = graph()->NewNode(common()->IfException(), etrue, if_true);
if_true = graph()->NewNode(common()->IfSuccess(), if_true);
Node* exfalse = graph()->NewNode(common()->IfException(), efalse, if_false);
if_false = graph()->NewNode(common()->IfSuccess(), if_false);
Node* merge = graph()->NewNode(common()->Merge(2), extrue, exfalse);
Node* ephi =
graph()->NewNode(common()->EffectPhi(2), extrue, exfalse, merge);
Node* phi =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
extrue, exfalse, merge);
ReplaceWithValue(on_exception, phi, ephi, merge);
}
if_false = graph()->NewNode(common()->Throw(), efalse, if_false);
MergeControlToEnd(graph(), common(), if_false);
ReplaceWithValue(node, vtrue, etrue, if_true);
return Changed(vtrue);
}
Reduction JSCallReducer::ReduceReflectHas(Node* node) {
JSCallNode n(node);
Node* target = n.ArgumentOrUndefined(0, jsgraph());
Node* key = n.ArgumentOrUndefined(1, jsgraph());
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
FrameState frame_state = n.frame_state();
Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), target);
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
{
if_false = efalse = graph()->NewNode(
javascript()->CallRuntime(Runtime::kThrowTypeError, 2),
jsgraph()->ConstantNoHole(
static_cast<int>(MessageTemplate::kCalledOnNonObject)),
jsgraph()->HeapConstantNoHole(factory()->ReflectHas_string()), context,
frame_state, efalse, if_false);
}
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* vtrue;
{
vtrue = etrue = if_true = graph()->NewNode(
javascript()->HasProperty(FeedbackSource()), target, key,
jsgraph()->UndefinedConstant(), context, frame_state, etrue, if_true);
}
Node* on_exception = nullptr;
if (NodeProperties::IsExceptionalCall(node, &on_exception)) {
Node* extrue = graph()->NewNode(common()->IfException(), etrue, if_true);
if_true = graph()->NewNode(common()->IfSuccess(), if_true);
Node* exfalse = graph()->NewNode(common()->IfException(), efalse, if_false);
if_false = graph()->NewNode(common()->IfSuccess(), if_false);
Node* merge = graph()->NewNode(common()->Merge(2), extrue, exfalse);
Node* ephi =
graph()->NewNode(common()->EffectPhi(2), extrue, exfalse, merge);
Node* phi =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
extrue, exfalse, merge);
ReplaceWithValue(on_exception, phi, ephi, merge);
}
if_false = graph()->NewNode(common()->Throw(), efalse, if_false);
MergeControlToEnd(graph(), common(), if_false);
ReplaceWithValue(node, vtrue, etrue, if_true);
return Changed(vtrue);
}
namespace {
bool CanInlineArrayIteratingBuiltin(JSHeapBroker* broker,
ZoneRefSet<Map> const& receiver_maps,
ElementsKind* kind_return) {
DCHECK_NE(0, receiver_maps.size());
*kind_return = receiver_maps[0].elements_kind();
for (MapRef map : receiver_maps) {
if (!map.supports_fast_array_iteration(broker) ||
!UnionElementsKindUptoSize(kind_return, map.elements_kind())) {
return false;
}
}
return true;
}
bool CanInlineArrayResizingBuiltin(JSHeapBroker* broker,
ZoneRefSet<Map> const& receiver_maps,
std::vector<ElementsKind>* kinds,
bool builtin_is_push = false) {
DCHECK_NE(0, receiver_maps.size());
for (MapRef map : receiver_maps) {
if (!map.supports_fast_array_resize(broker)) return false;
if (map.elements_kind() == HOLEY_DOUBLE_ELEMENTS && !builtin_is_push) {
return false;
}
ElementsKind current_kind = map.elements_kind();
auto kind_ptr = kinds->data();
size_t i;
for (i = 0; i < kinds->size(); i++, kind_ptr++) {
if (UnionElementsKindUptoPackedness(kind_ptr, current_kind)) {
break;
}
}
if (i == kinds->size()) kinds->push_back(current_kind);
}
return true;
}
class IteratingArrayBuiltinHelper {
public:
IteratingArrayBuiltinHelper(Node* node, JSHeapBroker* broker,
JSGraph* jsgraph,
CompilationDependencies* dependencies)
: receiver_(NodeProperties::GetValueInput(node, 1)),
effect_(NodeProperties::GetEffectInput(node)),
control_(NodeProperties::GetControlInput(node)),
inference_(broker, receiver_, effect_) {
if (!v8_flags.turbo_inline_array_builtins) return;
DCHECK_EQ(IrOpcode::kJSCall, node->opcode());
const CallParameters& p = CallParametersOf(node->op());
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return;
}
if (!inference_.HaveMaps()) return;
ZoneRefSet<Map> const& receiver_maps = inference_.GetMaps();
if (!CanInlineArrayIteratingBuiltin(broker, receiver_maps,
&elements_kind_)) {
return;
}
if (!dependencies->DependOnNoElementsProtector()) return;
has_stability_dependency_ = inference_.RelyOnMapsPreferStability(
dependencies, jsgraph, &effect_, control_, p.feedback());
can_reduce_ = true;
}
bool can_reduce() const { return can_reduce_; }
bool has_stability_dependency() const { return has_stability_dependency_; }
Effect effect() const { return effect_; }
Control control() const { return control_; }
MapInference* inference() { return &inference_; }
ElementsKind elements_kind() const { return elements_kind_; }
private:
bool can_reduce_ = false;
bool has_stability_dependency_ = false;
Node* receiver_;
Effect effect_;
Control control_;
MapInference inference_;
ElementsKind elements_kind_;
};
}
Reduction JSCallReducer::ReduceArrayForEach(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeForEach(
h.inference(), h.has_stability_dependency(), h.elements_kind(), shared);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayReduce(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeReduce(
h.inference(), h.has_stability_dependency(), h.elements_kind(),
ArrayReduceDirection::kLeft, shared);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayReduceRight(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeReduce(
h.inference(), h.has_stability_dependency(), h.elements_kind(),
ArrayReduceDirection::kRight, shared);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayMap(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
if (!dependencies()->DependOnArraySpeciesProtector()) {
return h.inference()->NoChange();
}
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph =
a.ReduceArrayPrototypeMap(h.inference(), h.has_stability_dependency(),
h.elements_kind(), shared, native_context());
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayFilter(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
if (!dependencies()->DependOnArraySpeciesProtector()) {
return h.inference()->NoChange();
}
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph =
a.ReduceArrayPrototypeFilter(h.inference(), h.has_stability_dependency(),
h.elements_kind(), shared, native_context());
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayFind(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeFind(
h.inference(), h.has_stability_dependency(), h.elements_kind(), shared,
native_context(), ArrayFindVariant::kFind);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayFindIndex(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeFind(
h.inference(), h.has_stability_dependency(), h.elements_kind(), shared,
native_context(), ArrayFindVariant::kFindIndex);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayEvery(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeEverySome(
h.inference(), h.has_stability_dependency(), h.elements_kind(), shared,
native_context(), ArrayEverySomeVariant::kEvery);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayIncludes(Node* node) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeIndexOfIncludes(
h.elements_kind(), ArrayIndexOfIncludesVariant::kIncludes);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayIndexOf(Node* node) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeIndexOfIncludes(
h.elements_kind(), ArrayIndexOfIncludesVariant::kIndexOf);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArraySome(Node* node,
SharedFunctionInfoRef shared) {
IteratingArrayBuiltinHelper h(node, broker(), jsgraph(), dependencies());
if (!h.can_reduce()) return h.inference()->NoChange();
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(h.effect(), h.control());
TNode<Object> subgraph = a.ReduceArrayPrototypeEverySome(
h.inference(), h.has_stability_dependency(), h.elements_kind(), shared,
native_context(), ArrayEverySomeVariant::kSome);
return ReplaceWithSubgraph(&a, subgraph);
}
#if V8_ENABLE_WEBASSEMBLY
Reduction JSCallReducer::ReduceWasmMethodWrapper(Node* node,
JSFunctionRef function,
SharedFunctionInfoRef shared) {
if (!shared.HasBuiltinId() ||
shared.builtin_id() != Builtin::kWasmMethodWrapper) {
return NoChange();
}
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.feedback().IsValid() &&
p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
node->InsertInput(graph()->zone(), n.FirstArgumentIndex(), n.receiver());
NodeProperties::ChangeOp(
node, javascript()->Call(
JSCallNode::ArityForArgc(n.ArgumentCount() + 1), p.frequency(),
p.feedback(), ConvertReceiverMode::kAny, p.speculation_mode()));
ContextRef context = function.context(broker());
OptionalObjectRef target =
context.get(broker(), wasm::kMethodWrapperContextSlot);
if (!target.has_value()) {
TRACE_BROKER_MISSING(broker(), "target value for context " << context);
return NoChange();
}
node->ReplaceInput(n.TargetIndex(),
jsgraph()->ConstantNoHole(target.value(), broker()));
return Changed(node).FollowedBy(ReduceJSCall(node));
}
bool CanInlineJSToWasmCall(const wasm::CanonicalSig* wasm_signature) {
if (wasm_signature->return_count() > 1) {
return false;
}
for (auto type : wasm_signature->all()) {
#if defined(V8_TARGET_ARCH_32_BIT)
if (type == wasm::kWasmI64) return false;
#endif
if (type != wasm::kWasmI32 && type != wasm::kWasmI64 &&
type != wasm::kWasmF32 && type != wasm::kWasmF64 &&
type != wasm::kWasmExternRef) {
return false;
}
}
return true;
}
Reduction JSCallReducer::ReduceCallWasmFunction(Node* node,
SharedFunctionInfoRef shared) {
DCHECK(flags() & kInlineJSToWasmCalls);
JSCallNode n(node);
const CallParameters& p = n.Parameters();
if (p.feedback().IsValid() &&
p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Tagged<Object> trusted_data = shared.object()->GetTrustedData(isolate());
Tagged<WasmExportedFunctionData> function_data;
if (!TryCast(trusted_data, &function_data)) return NoChange();
if (function_data->is_promising()) return NoChange();
Tagged<WasmTrustedInstanceData> instance_data =
function_data->instance_data();
const wasm::CanonicalSig* wasm_signature = function_data->internal()->sig();
if (!CanInlineJSToWasmCall(wasm_signature)) {
return NoChange();
}
wasm::NativeModule* native_module = instance_data->native_module();
int wasm_function_index = function_data->function_index();
bool receiver_is_first_param = function_data->receiver_is_first_param() != 0;
if (wasm_native_module_for_inlining_ == nullptr) {
wasm_native_module_for_inlining_ = native_module;
}
const Operator* op = javascript()->CallWasm(
native_module, wasm_function_index, shared, p.feedback());
size_t actual_arity = n.ArgumentCount();
DCHECK(JSCallNode::kFeedbackVectorIsLastInput);
DCHECK_EQ(actual_arity + JSWasmCallNode::kExtraInputCount - 1,
n.FeedbackVectorIndex());
size_t expected_arity = wasm_signature->parameter_count();
if (receiver_is_first_param) {
node->InsertInput(graph()->zone(), n.FirstArgumentIndex(),
node->InputAt(n.ReceiverIndex()));
actual_arity++;
}
while (actual_arity > expected_arity) {
int removal_index =
static_cast<int>(n.FirstArgumentIndex() + expected_arity);
DCHECK_LT(removal_index, static_cast<int>(node->InputCount()));
node->RemoveInput(removal_index);
actual_arity--;
}
while (actual_arity < expected_arity) {
int insertion_index = n.ArgumentIndex(n.ArgumentCount());
node->InsertInput(graph()->zone(), insertion_index,
jsgraph()->UndefinedConstant());
actual_arity++;
}
NodeProperties::ChangeOp(node, op);
return Changed(node);
}
#endif
Reduction JSCallReducer::ReduceCallApiFunction(Node* node,
SharedFunctionInfoRef shared) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
int const argc = p.arity_without_implicit_args();
Node* target = n.target();
Node* global_proxy = jsgraph()->ConstantNoHole(
native_context().global_proxy_object(broker()), broker());
Node* receiver = (p.convert_mode() == ConvertReceiverMode::kNullOrUndefined)
? global_proxy
: n.receiver();
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
FrameState frame_state = n.frame_state();
if (!shared.function_template_info(broker()).has_value()) {
TRACE_BROKER_MISSING(
broker(), "FunctionTemplateInfo for function with SFI " << shared);
return NoChange();
}
FunctionTemplateInfoRef function_template_info(
shared.function_template_info(broker()).value());
if (function_template_info.accept_any_receiver() &&
function_template_info.is_signature_undefined(broker())) {
receiver = effect = graph()->NewNode(
simplified()->ConvertReceiver(p.convert_mode()), receiver,
jsgraph()->ConstantNoHole(native_context(), broker()), global_proxy,
effect, control);
} else {
MapInference inference(broker(), receiver, effect);
if (inference.HaveMaps()) {
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
MapRef first_receiver_map = receiver_maps[0];
HolderLookupResult api_holder =
function_template_info.LookupHolderOfExpectedType(broker(),
first_receiver_map);
if (api_holder.lookup == CallOptimization::kHolderNotFound) {
return inference.NoChange();
}
CHECK(first_receiver_map.IsJSReceiverMap());
CHECK(!first_receiver_map.is_access_check_needed() ||
function_template_info.accept_any_receiver());
for (size_t i = 1; i < receiver_maps.size(); ++i) {
MapRef receiver_map = receiver_maps[i];
HolderLookupResult holder_i =
function_template_info.LookupHolderOfExpectedType(broker(),
receiver_map);
if (api_holder.lookup != holder_i.lookup) return inference.NoChange();
DCHECK(holder_i.lookup == CallOptimization::kHolderFound ||
holder_i.lookup == CallOptimization::kHolderIsReceiver);
if (holder_i.lookup == CallOptimization::kHolderFound) {
DCHECK(api_holder.holder.has_value() && holder_i.holder.has_value());
if (!api_holder.holder->equals(*holder_i.holder)) {
return inference.NoChange();
}
}
CHECK(receiver_map.IsJSReceiverMap());
CHECK(!receiver_map.is_access_check_needed() ||
function_template_info.accept_any_receiver());
}
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation &&
!inference.RelyOnMapsViaStability(dependencies())) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
} else {
Builtin builtin_name;
if (function_template_info.accept_any_receiver()) {
DCHECK(!function_template_info.is_signature_undefined(broker()));
builtin_name = Builtin::kCallFunctionTemplate_CheckCompatibleReceiver;
} else if (function_template_info.is_signature_undefined(broker())) {
builtin_name = Builtin::kCallFunctionTemplate_CheckAccess;
} else {
builtin_name =
Builtin::kCallFunctionTemplate_CheckAccessAndCompatibleReceiver;
}
receiver = effect = graph()->NewNode(
simplified()->ConvertReceiver(p.convert_mode()), receiver,
jsgraph()->ConstantNoHole(native_context(), broker()), global_proxy,
effect, control);
Callable callable = Builtins::CallableFor(isolate(), builtin_name);
auto call_descriptor = Linkage::GetStubCallDescriptor(
graph()->zone(), callable.descriptor(),
argc + 1 , CallDescriptor::kNeedsFrameState);
node->RemoveInput(n.FeedbackVectorIndex());
node->InsertInput(graph()->zone(), 0,
jsgraph()->HeapConstantNoHole(callable.code()));
node->ReplaceInput(
1, jsgraph()->ConstantNoHole(function_template_info, broker()));
node->InsertInput(graph()->zone(), 2,
jsgraph()->Int32Constant(JSParameterCount(argc)));
node->ReplaceInput(3, receiver);
node->ReplaceInput(6 + argc, effect);
NodeProperties::ChangeOp(node, common()->Call(call_descriptor));
return Changed(node);
}
}
compiler::OptionalObjectRef maybe_callback_data =
function_template_info.callback_data(broker());
if (!maybe_callback_data.has_value()) {
TRACE_BROKER_MISSING(broker(), "call code for function template info "
<< function_template_info);
return NoChange();
}
FastApiCallFunction c_function = fast_api_call::GetFastApiCallTarget(
broker(), function_template_info, argc);
if (c_function.address) {
FastApiCallReducerAssembler a(this, node, function_template_info,
c_function, receiver, shared, target, argc,
effect);
Node* fast_call_subgraph = a.ReduceFastApiCall();
return Replace(fast_call_subgraph);
}
bool no_profiling = broker()->dependencies()->DependOnNoProfilingProtector();
Callable call_api_callback = Builtins::CallableFor(
isolate(), no_profiling ? Builtin::kCallApiCallbackOptimizedNoProfiling
: Builtin::kCallApiCallbackOptimized);
CallInterfaceDescriptor cid = call_api_callback.descriptor();
auto call_descriptor =
Linkage::GetStubCallDescriptor(graph()->zone(), cid, argc + 1
implicit receiver */, CallDescriptor::kNeedsFrameState);
ApiFunction api_function(function_template_info.callback(broker()));
ExternalReference function_reference = ExternalReference::Create(
&api_function, ExternalReference::DIRECT_API_CALL);
Node* continuation_frame_state = CreateInlinedApiFunctionFrameState(
jsgraph(), shared, target, context, receiver, frame_state);
node->RemoveInput(n.FeedbackVectorIndex());
node->InsertInput(graph()->zone(), 0,
jsgraph()->HeapConstantNoHole(call_api_callback.code()));
node->ReplaceInput(1, jsgraph()->ExternalConstant(function_reference));
node->InsertInput(graph()->zone(), 2, jsgraph()->ConstantNoHole(argc));
node->InsertInput(
graph()->zone(), 3,
jsgraph()->HeapConstantNoHole(function_template_info.object()));
node->ReplaceInput(4, receiver);
node->ReplaceInput(5 + argc + 1, continuation_frame_state);
node->ReplaceInput(5 + argc + 2, effect);
NodeProperties::ChangeOp(node, common()->Call(call_descriptor));
return Changed(node);
}
namespace {
bool IsSafeArgumentsElements(Node* node) {
for (Edge const edge : node->use_edges()) {
if (!NodeProperties::IsValueEdge(edge)) continue;
if (edge.from()->opcode() != IrOpcode::kLoadField &&
edge.from()->opcode() != IrOpcode::kLoadElement) {
return false;
}
}
return true;
}
#ifdef DEBUG
bool IsCallOrConstructWithArrayLike(Node* node) {
return node->opcode() == IrOpcode::kJSCallWithArrayLike ||
node->opcode() == IrOpcode::kJSConstructWithArrayLike;
}
#endif
bool IsCallOrConstructWithSpread(Node* node) {
return node->opcode() == IrOpcode::kJSCallWithSpread ||
node->opcode() == IrOpcode::kJSConstructWithSpread;
}
bool IsCallWithArrayLikeOrSpread(Node* node) {
return node->opcode() == IrOpcode::kJSCallWithArrayLike ||
node->opcode() == IrOpcode::kJSCallWithSpread;
}
}
Node* JSCallReducer::ConvertHoleToUndefined(Node* value, ElementsKind kind) {
DCHECK(IsHoleyElementsKind(kind));
if (kind == HOLEY_DOUBLE_ELEMENTS) {
#ifdef V8_ENABLE_UNDEFINED_DOUBLE
return graph()->NewNode(
simplified()->ChangeFloat64OrUndefinedOrHoleToTagged(), value);
#else
return graph()->NewNode(simplified()->ChangeFloat64HoleToTagged(), value);
#endif
}
return graph()->NewNode(simplified()->ConvertTaggedHoleToUndefined(), value);
}
void JSCallReducer::CheckIfConstructor(Node* construct) {
JSConstructNode n(construct);
Node* new_target = n.new_target();
Control control = n.control();
Node* check =
graph()->NewNode(simplified()->ObjectIsConstructor(), new_target);
Node* check_branch =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control);
Node* check_fail = graph()->NewNode(common()->IfFalse(), check_branch);
Node* check_throw = check_fail = graph()->NewNode(
javascript()->CallRuntime(Runtime::kThrowTypeError, 2),
jsgraph()->ConstantNoHole(
static_cast<int>(MessageTemplate::kNotConstructor)),
new_target, n.context(), n.frame_state(), n.effect(), check_fail);
control = graph()->NewNode(common()->IfTrue(), check_branch);
NodeProperties::ReplaceControlInput(construct, control);
Node* on_exception = nullptr;
if (NodeProperties::IsExceptionalCall(construct, &on_exception)) {
Node* if_exception =
graph()->NewNode(common()->IfException(), check_throw, check_fail);
check_fail = graph()->NewNode(common()->IfSuccess(), check_fail);
Node* merge =
graph()->NewNode(common()->Merge(2), if_exception, on_exception);
Node* ephi = graph()->NewNode(common()->EffectPhi(2), if_exception,
on_exception, merge);
Node* phi =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
if_exception, on_exception, merge);
ReplaceWithValue(on_exception, phi, ephi, merge);
merge->ReplaceInput(1, on_exception);
ephi->ReplaceInput(1, on_exception);
phi->ReplaceInput(1, on_exception);
}
Node* throw_node =
graph()->NewNode(common()->Throw(), check_throw, check_fail);
MergeControlToEnd(graph(), common(), throw_node);
}
namespace {
bool ShouldUseCallICFeedback(Node* node) {
HeapObjectMatcher m(node);
if (m.HasResolvedValue() || m.IsCheckClosure() || m.IsJSCreateClosure()) {
return false;
} else if (m.IsPhi()) {
Node* control = NodeProperties::GetControlInput(node);
if (control->opcode() == IrOpcode::kLoop ||
control->opcode() == IrOpcode::kDead) {
return true;
}
int const value_input_count = m.node()->op()->ValueInputCount();
for (int n = 0; n < value_input_count; ++n) {
if (ShouldUseCallICFeedback(node->InputAt(n))) return true;
}
return false;
}
return true;
}
}
Node* JSCallReducer::CheckArrayLength(Node* array, ElementsKind elements_kind,
uint32_t array_length,
const FeedbackSource& feedback_source,
Effect effect, Control control) {
Node* length = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayLength(elements_kind)),
array, effect, control);
Node* check = graph()->NewNode(simplified()->NumberEqual(), length,
jsgraph()->ConstantNoHole(array_length));
return graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kArrayLengthChanged,
feedback_source),
check, effect, control);
}
Reduction
JSCallReducer::ReduceCallOrConstructWithArrayLikeOrSpreadOfCreateArguments(
Node* node, Node* arguments_list, int arraylike_or_spread_index,
CallFrequency const& frequency, FeedbackSource const& feedback,
SpeculationMode speculation_mode, CallFeedbackRelation feedback_relation) {
DCHECK_EQ(arguments_list->opcode(), IrOpcode::kJSCreateArguments);
for (Edge edge : arguments_list->use_edges()) {
if (!NodeProperties::IsValueEdge(edge)) continue;
Node* const user = edge.from();
switch (user->opcode()) {
case IrOpcode::kCheckMaps:
case IrOpcode::kFrameState:
case IrOpcode::kStateValues:
case IrOpcode::kReferenceEqual:
case IrOpcode::kReturn:
continue;
case IrOpcode::kLoadField: {
DCHECK_EQ(arguments_list, user->InputAt(0));
FieldAccess const& access = FieldAccessOf(user->op());
if (access.offset == JSArray::kLengthOffset) {
static_assert(
static_cast<int>(JSArray::kLengthOffset) ==
static_cast<int>(JSStrictArgumentsObject::kLengthOffset));
static_assert(
static_cast<int>(JSArray::kLengthOffset) ==
static_cast<int>(JSSloppyArgumentsObject::kLengthOffset));
continue;
} else if (access.offset == JSObject::kElementsOffset) {
if (IsSafeArgumentsElements(user)) continue;
}
break;
}
case IrOpcode::kJSCallWithArrayLike: {
JSCallWithArrayLikeNode n(user);
if (edge.index() == n.ArgumentIndex(0)) continue;
break;
}
case IrOpcode::kJSConstructWithArrayLike: {
JSConstructWithArrayLikeNode n(user);
if (edge.index() == n.ArgumentIndex(0)) continue;
break;
}
case IrOpcode::kJSCallWithSpread: {
JSCallWithSpreadNode n(user);
if (edge.index() == n.LastArgumentIndex()) continue;
break;
}
case IrOpcode::kJSConstructWithSpread: {
JSConstructWithSpreadNode n(user);
if (edge.index() == n.LastArgumentIndex()) continue;
break;
}
default:
break;
}
waitlist_.insert(node);
return NoChange();
}
CreateArgumentsType const type = CreateArgumentsTypeOf(arguments_list->op());
FrameState frame_state =
FrameState{NodeProperties::GetFrameStateInput(arguments_list)};
int formal_parameter_count;
{
Handle<SharedFunctionInfo> shared;
if (!frame_state.frame_state_info().shared_info().ToHandle(&shared)) {
return NoChange();
}
formal_parameter_count =
MakeRef(broker(), shared)
.internal_formal_parameter_count_without_receiver_deprecated();
}
if (type == CreateArgumentsType::kMappedArguments) {
if (formal_parameter_count != 0) {
Node* effect = NodeProperties::GetEffectInput(node);
if (!NodeProperties::NoObservableSideEffectBetween(effect,
arguments_list)) {
return NoChange();
}
}
}
if (IsCallOrConstructWithSpread(node)) {
if (!dependencies()->DependOnArrayIteratorProtector()) return NoChange();
}
node->RemoveInput(arraylike_or_spread_index);
const int start_index = (type == CreateArgumentsType::kRestParameter)
? formal_parameter_count
: 0;
int argc =
arraylike_or_spread_index - JSCallOrConstructNode::FirstArgumentIndex();
if (frame_state.outer_frame_state()->opcode() != IrOpcode::kFrameState) {
Operator const* op;
if (IsCallWithArrayLikeOrSpread(node)) {
static constexpr int kTargetAndReceiver = 2;
op = javascript()->CallForwardVarargs(argc + kTargetAndReceiver,
start_index);
} else {
static constexpr int kTargetAndNewTarget = 2;
op = javascript()->ConstructForwardVarargs(argc + kTargetAndNewTarget,
start_index);
}
node->RemoveInput(JSCallOrConstructNode::FeedbackVectorIndexForArgc(argc));
NodeProperties::ChangeOp(node, op);
return Changed(node);
}
FrameState outer_state{frame_state.outer_frame_state()};
FrameStateInfo outer_info = outer_state.frame_state_info();
if (outer_info.type() == FrameStateType::kInlinedExtraArguments) {
frame_state = outer_state;
}
StateValuesAccess parameters_access(frame_state.parameters());
for (auto it = parameters_access.begin_without_receiver_and_skip(start_index);
!it.done(); ++it) {
DCHECK_NOT_NULL(it.node());
node->InsertInput(graph()->zone(),
JSCallOrConstructNode::ArgumentIndex(argc++), it.node());
}
if (IsCallWithArrayLikeOrSpread(node)) {
NodeProperties::ChangeOp(
node, javascript()->Call(JSCallNode::ArityForArgc(argc), frequency,
feedback, ConvertReceiverMode::kAny,
speculation_mode, feedback_relation));
return Changed(node).FollowedBy(ReduceJSCall(node));
} else {
NodeProperties::ChangeOp(
node, javascript()->Construct(JSConstructNode::ArityForArgc(argc),
frequency, feedback));
CheckIfConstructor(node);
return Changed(node).FollowedBy(ReduceJSConstruct(node));
}
}
Reduction JSCallReducer::ReduceCallOrConstructWithArrayLikeOrSpread(
Node* node, int argument_count, int arraylike_or_spread_index,
CallFrequency const& frequency, FeedbackSource const& feedback_source,
SpeculationMode speculation_mode, CallFeedbackRelation feedback_relation,
Node* target, Effect effect, Control control) {
DCHECK(IsCallOrConstructWithArrayLike(node) ||
IsCallOrConstructWithSpread(node));
DCHECK_IMPLIES(speculation_mode == SpeculationMode::kAllowSpeculation,
feedback_source.IsValid());
if (feedback_source.IsValid()) {
ProcessedFeedback const& call_feedback =
broker()->GetFeedbackForCall(feedback_source);
if (call_feedback.IsInsufficient()) {
return ReduceForInsufficientFeedback(
node, DeoptimizeReason::kInsufficientTypeFeedbackForCall);
}
}
Node* arguments_list =
NodeProperties::GetValueInput(node, arraylike_or_spread_index);
if (arguments_list->opcode() == IrOpcode::kJSCreateArguments) {
return ReduceCallOrConstructWithArrayLikeOrSpreadOfCreateArguments(
node, arguments_list, arraylike_or_spread_index, frequency,
feedback_source, speculation_mode, feedback_relation);
}
if (!v8_flags.turbo_optimize_apply) return NoChange();
if (!IsCallWithArrayLikeOrSpread(node)) return NoChange();
if (speculation_mode != SpeculationMode::kAllowSpeculation) return NoChange();
if (arguments_list->opcode() != IrOpcode::kJSCreateLiteralArray &&
arguments_list->opcode() != IrOpcode::kJSCreateEmptyLiteralArray) {
return NoChange();
}
if (IsCallOrConstructWithSpread(node)) {
if (!dependencies()->DependOnArrayIteratorProtector()) return NoChange();
}
if (arguments_list->opcode() == IrOpcode::kJSCreateEmptyLiteralArray) {
if (generated_calls_with_array_like_or_spread_.count(node)) {
return NoChange();
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceJSCallWithArrayLikeOrSpreadOfEmpty(
&generated_calls_with_array_like_or_spread_);
return ReplaceWithSubgraph(&a, subgraph);
}
DCHECK_EQ(arguments_list->opcode(), IrOpcode::kJSCreateLiteralArray);
JSCreateLiteralOpNode args_node(arguments_list);
CreateLiteralParameters const& args_params = args_node.Parameters();
const FeedbackSource& array_feedback = args_params.feedback();
const ProcessedFeedback& feedback =
broker()->GetFeedbackForArrayOrObjectLiteral(array_feedback);
if (feedback.IsInsufficient()) return NoChange();
AllocationSiteRef site = feedback.AsLiteral().value();
if (!site.boilerplate(broker()).has_value()) return NoChange();
JSArrayRef boilerplate_array = site.boilerplate(broker())->AsJSArray();
int const array_length =
boilerplate_array.GetBoilerplateLength(broker()).AsSmi();
SBXCHECK_GE(array_length, 0);
uint32_t new_argument_count =
static_cast<uint32_t>(array_length) + argument_count - 1;
const uint32_t kMaxArityForOptimizedFunctionApply = 32;
if (new_argument_count > kMaxArityForOptimizedFunctionApply) {
return NoChange();
}
MapRef array_map = boilerplate_array.map(broker());
if (!array_map.supports_fast_array_iteration(broker())) {
return NoChange();
}
if (!dependencies()->DependOnNoElementsProtector()) {
return NoChange();
}
node->RemoveInput(arraylike_or_spread_index);
effect = graph()->NewNode(
simplified()->CheckMaps(CheckMapsFlag::kNone, ZoneRefSet<Map>(array_map),
feedback_source),
arguments_list, effect, control);
ElementsKind elements_kind = array_map.elements_kind();
effect = CheckArrayLength(arguments_list, elements_kind, array_length,
feedback_source, effect, control);
Node* elements = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSObjectElements()),
arguments_list, effect, control);
for (int i = 0; i < array_length; i++) {
Node* index = jsgraph()->ConstantNoHole(i);
Node* load = effect = graph()->NewNode(
simplified()->LoadElement(
AccessBuilder::ForFixedArrayElement(elements_kind)),
elements, index, effect, control);
if (IsHoleyElementsKind(elements_kind)) {
load = ConvertHoleToUndefined(load, elements_kind);
}
node->InsertInput(graph()->zone(), arraylike_or_spread_index + i, load);
}
NodeProperties::ChangeOp(
node, javascript()->Call(
JSCallNode::ArityForArgc(static_cast<int>(new_argument_count)),
frequency, feedback_source, ConvertReceiverMode::kAny,
speculation_mode, CallFeedbackRelation::kUnrelated));
NodeProperties::ReplaceEffectInput(node, effect);
return Changed(node).FollowedBy(ReduceJSCall(node));
}
bool JSCallReducer::IsBuiltinOrApiFunction(JSFunctionRef function) const {
return function.shared(broker()).HasBuiltinId() ||
function.shared(broker()).function_template_info(broker()).has_value();
}
Reduction JSCallReducer::ReduceJSCall(Node* node) {
if (broker()->StackHasOverflowed()) return NoChange();
JSCallNode n(node);
CallParameters const& p = n.Parameters();
Node* target = n.target();
Effect effect = n.effect();
Control control = n.control();
int arity = p.arity_without_implicit_args();
auto NoChangeOrSoftDeopt = [&]() {
if (p.feedback().IsValid()) {
ProcessedFeedback const& feedback =
broker()->GetFeedbackForCall(p.feedback());
if (feedback.IsInsufficient()) {
return ReduceForInsufficientFeedback(
node, DeoptimizeReason::kInsufficientTypeFeedbackForCall);
}
}
return NoChange();
};
HeapObjectMatcher m(target);
if (m.HasResolvedValue()) {
ObjectRef target_ref = m.Ref(broker());
if (target_ref.IsJSFunction()) {
JSFunctionRef function = target_ref.AsJSFunction();
if (!function.native_context(broker()).equals(native_context())) {
return NoChange();
}
SharedFunctionInfoRef shared = function.shared(broker());
Reduction res = ReduceJSCall(node, shared);
#if V8_ENABLE_WEBASSEMBLY
if (!res.Changed()) {
res = ReduceWasmMethodWrapper(node, function, shared);
}
#endif
if (!res.Changed()) return NoChangeOrSoftDeopt();
return res;
} else if (target_ref.IsJSBoundFunction()) {
JSBoundFunctionRef function = target_ref.AsJSBoundFunction();
ObjectRef bound_this = function.bound_this(broker());
ConvertReceiverMode const convert_mode =
bound_this.IsNullOrUndefined()
? ConvertReceiverMode::kNullOrUndefined
: ConvertReceiverMode::kNotNullOrUndefined;
FixedArrayRef bound_arguments = function.bound_arguments(broker());
const uint32_t bound_arguments_length = bound_arguments.length();
if (arity + bound_arguments_length + 1 >
Code::kMaxArguments) {
return NoChange();
}
static constexpr int kInlineSize = 16;
base::SmallVector<Node*, kInlineSize> args;
for (uint32_t i = 0; i < bound_arguments_length; ++i) {
OptionalObjectRef maybe_arg = bound_arguments.TryGet(broker(), i);
if (!maybe_arg.has_value()) {
TRACE_BROKER_MISSING(broker(), "bound argument");
return NoChangeOrSoftDeopt();
}
args.emplace_back(
jsgraph()->ConstantNoHole(maybe_arg.value(), broker()));
}
NodeProperties::ReplaceValueInput(
node,
jsgraph()->ConstantNoHole(function.bound_target_function(broker()),
broker()),
JSCallNode::TargetIndex());
NodeProperties::ReplaceValueInput(
node, jsgraph()->ConstantNoHole(bound_this, broker()),
JSCallNode::ReceiverIndex());
for (uint32_t i = 0; i < bound_arguments_length; ++i) {
node->InsertInput(graph()->zone(), i + 2, args[i]);
arity++;
}
NodeProperties::ChangeOp(
node,
javascript()->Call(JSCallNode::ArityForArgc(arity), p.frequency(),
p.feedback(), convert_mode, p.speculation_mode(),
CallFeedbackRelation::kUnrelated));
return Changed(node).FollowedBy(ReduceJSCall(node));
}
return NoChangeOrSoftDeopt();
}
if (target->opcode() == IrOpcode::kJSCreateClosure) {
CreateClosureParameters const& params =
JSCreateClosureNode{target}.Parameters();
Reduction res = ReduceJSCall(node, params.shared_info());
if (!res.Changed()) return NoChangeOrSoftDeopt();
return res;
} else if (target->opcode() == IrOpcode::kCheckClosure) {
FeedbackCellRef cell = MakeRef(broker(), FeedbackCellOf(target->op()));
OptionalSharedFunctionInfoRef shared = cell.shared_function_info(broker());
if (!shared.has_value()) {
TRACE_BROKER_MISSING(broker(), "Unable to reduce JSCall. FeedbackCell "
<< cell << " has no FeedbackVector");
return NoChangeOrSoftDeopt();
}
Reduction res = ReduceJSCall(node, *shared);
if (!res.Changed()) return NoChangeOrSoftDeopt();
return res;
}
if (target->opcode() == IrOpcode::kJSCreateBoundFunction) {
Node* bound_target_function = NodeProperties::GetValueInput(target, 0);
Node* bound_this = NodeProperties::GetValueInput(target, 1);
uint32_t const bound_arguments_length =
static_cast<int>(CreateBoundFunctionParametersOf(target->op()).arity());
NodeProperties::ReplaceValueInput(node, bound_target_function,
n.TargetIndex());
NodeProperties::ReplaceValueInput(node, bound_this, n.ReceiverIndex());
for (uint32_t i = 0; i < bound_arguments_length; ++i) {
Node* value = NodeProperties::GetValueInput(target, 2 + i);
node->InsertInput(graph()->zone(), n.ArgumentIndex(i), value);
arity++;
}
ConvertReceiverMode const convert_mode =
NodeProperties::CanBeNullOrUndefined(broker(), bound_this, effect)
? ConvertReceiverMode::kAny
: ConvertReceiverMode::kNotNullOrUndefined;
NodeProperties::ChangeOp(
node,
javascript()->Call(JSCallNode::ArityForArgc(arity), p.frequency(),
p.feedback(), convert_mode, p.speculation_mode(),
CallFeedbackRelation::kUnrelated));
return Changed(node).FollowedBy(ReduceJSCall(node));
}
if (!ShouldUseCallICFeedback(target) ||
p.feedback_relation() == CallFeedbackRelation::kUnrelated ||
!p.feedback().IsValid()) {
return NoChange();
}
ProcessedFeedback const& feedback =
broker()->GetFeedbackForCall(p.feedback());
if (feedback.IsInsufficient()) {
return ReduceForInsufficientFeedback(
node, DeoptimizeReason::kInsufficientTypeFeedbackForCall);
}
OptionalHeapObjectRef feedback_target;
if (p.feedback_relation() == CallFeedbackRelation::kTarget) {
feedback_target = feedback.AsCall().target();
} else {
DCHECK_EQ(p.feedback_relation(), CallFeedbackRelation::kReceiver);
feedback_target = native_context().function_prototype_apply(broker());
}
if (feedback_target.has_value() &&
feedback_target->map(broker()).is_callable()) {
Node* target_function =
jsgraph()->ConstantNoHole(*feedback_target, broker());
Node* check = graph()->NewNode(simplified()->ReferenceEqual(), target,
target_function);
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kWrongCallTarget), check,
effect, control);
NodeProperties::ReplaceValueInput(node, target_function, n.TargetIndex());
NodeProperties::ReplaceEffectInput(node, effect);
return Changed(node).FollowedBy(ReduceJSCall(node));
} else if (feedback_target.has_value() && feedback_target->IsFeedbackCell()) {
FeedbackCellRef feedback_cell = feedback_target.value().AsFeedbackCell();
if (feedback_cell.feedback_vector(broker()).has_value()) {
Node* target_closure = effect =
graph()->NewNode(simplified()->CheckClosure(feedback_cell.object()),
target, effect, control);
NodeProperties::ReplaceValueInput(node, target_closure, n.TargetIndex());
NodeProperties::ReplaceEffectInput(node, effect);
return Changed(node).FollowedBy(ReduceJSCall(node));
}
}
return NoChange();
}
Reduction JSCallReducer::ReduceJSCall(Node* node,
SharedFunctionInfoRef shared) {
JSCallNode n(node);
Node* target = n.target();
if (shared.HasBreakInfo(broker())) return NoChange();
if (IsClassConstructor(shared.kind())) {
NodeProperties::ReplaceValueInputs(node, target);
NodeProperties::ChangeOp(
node, javascript()->CallRuntime(
Runtime::kThrowConstructorNonCallableError, 1));
return Changed(node);
}
Builtin builtin =
shared.HasBuiltinId() ? shared.builtin_id() : Builtin::kNoBuiltinId;
switch (builtin) {
case Builtin::kArrayConstructor:
return ReduceArrayConstructor(node);
case Builtin::kBooleanConstructor:
return ReduceBooleanConstructor(node);
case Builtin::kFunctionPrototypeApply:
return ReduceFunctionPrototypeApply(node);
case Builtin::kFastFunctionPrototypeBind:
return ReduceFunctionPrototypeBind(node);
case Builtin::kFunctionPrototypeCall:
return ReduceFunctionPrototypeCall(node);
case Builtin::kFunctionPrototypeHasInstance:
return ReduceFunctionPrototypeHasInstance(node);
case Builtin::kObjectConstructor:
return ReduceObjectConstructor(node);
case Builtin::kObjectCreate:
return ReduceObjectCreate(node);
case Builtin::kObjectGetPrototypeOf:
return ReduceObjectGetPrototypeOf(node);
case Builtin::kObjectIs:
return ReduceObjectIs(node);
case Builtin::kObjectPrototypeGetProto:
return ReduceObjectPrototypeGetProto(node);
case Builtin::kObjectPrototypeHasOwnProperty:
return ReduceObjectPrototypeHasOwnProperty(node);
case Builtin::kObjectPrototypeIsPrototypeOf:
return ReduceObjectPrototypeIsPrototypeOf(node);
case Builtin::kReflectApply:
return ReduceReflectApply(node);
case Builtin::kReflectConstruct:
return ReduceReflectConstruct(node);
case Builtin::kReflectGet:
return ReduceReflectGet(node);
case Builtin::kReflectGetPrototypeOf:
return ReduceReflectGetPrototypeOf(node);
case Builtin::kReflectHas:
return ReduceReflectHas(node);
case Builtin::kArrayForEach:
return ReduceArrayForEach(node, shared);
case Builtin::kArrayMap:
return ReduceArrayMap(node, shared);
case Builtin::kArrayFilter:
return ReduceArrayFilter(node, shared);
case Builtin::kArrayReduce:
return ReduceArrayReduce(node, shared);
case Builtin::kArrayReduceRight:
return ReduceArrayReduceRight(node, shared);
case Builtin::kArrayPrototypeFind:
return ReduceArrayFind(node, shared);
case Builtin::kArrayPrototypeFindIndex:
return ReduceArrayFindIndex(node, shared);
case Builtin::kArrayEvery:
return ReduceArrayEvery(node, shared);
case Builtin::kArrayIndexOf:
return ReduceArrayIndexOf(node);
case Builtin::kArrayIncludes:
return ReduceArrayIncludes(node);
case Builtin::kArraySome:
return ReduceArraySome(node, shared);
case Builtin::kArrayPrototypeAt:
return ReduceArrayPrototypeAt(node);
case Builtin::kArrayPrototypePush:
return ReduceArrayPrototypePush(node);
case Builtin::kArrayPrototypePop:
return ReduceArrayPrototypePop(node);
case Builtin::kArrayPrototypeSlice:
return ReduceArrayPrototypeSlice(node);
case Builtin::kArrayPrototypeEntries:
return ReduceArrayIterator(node, ArrayIteratorKind::kArrayLike,
IterationKind::kEntries);
case Builtin::kArrayPrototypeKeys:
return ReduceArrayIterator(node, ArrayIteratorKind::kArrayLike,
IterationKind::kKeys);
case Builtin::kArrayPrototypeValues:
return ReduceArrayIterator(node, ArrayIteratorKind::kArrayLike,
IterationKind::kValues);
case Builtin::kArrayIteratorPrototypeNext:
return ReduceArrayIteratorPrototypeNext(node);
case Builtin::kArrayIsArray:
return ReduceArrayIsArray(node);
case Builtin::kArrayBufferIsView:
return ReduceArrayBufferIsView(node);
case Builtin::kDataViewPrototypeGetByteLength:
return ReduceArrayBufferViewByteLengthAccessor(node, JS_DATA_VIEW_TYPE,
builtin);
case Builtin::kDataViewPrototypeGetByteOffset:
return ReduceArrayBufferViewByteOffsetAccessor(node, JS_DATA_VIEW_TYPE,
builtin);
case Builtin::kDataViewPrototypeGetUint8:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalUint8Array);
case Builtin::kDataViewPrototypeGetInt8:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalInt8Array);
case Builtin::kDataViewPrototypeGetUint16:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalUint16Array);
case Builtin::kDataViewPrototypeGetInt16:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalInt16Array);
case Builtin::kDataViewPrototypeGetUint32:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalUint32Array);
case Builtin::kDataViewPrototypeGetInt32:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalInt32Array);
case Builtin::kDataViewPrototypeGetFloat16:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalFloat16Array);
case Builtin::kDataViewPrototypeGetFloat32:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalFloat32Array);
case Builtin::kDataViewPrototypeGetFloat64:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalFloat64Array);
case Builtin::kDataViewPrototypeGetBigInt64:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalBigInt64Array);
case Builtin::kDataViewPrototypeGetBigUint64:
return ReduceDataViewAccess(node, DataViewAccess::kGet,
ExternalArrayType::kExternalBigUint64Array);
case Builtin::kDataViewPrototypeSetUint8:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalUint8Array);
case Builtin::kDataViewPrototypeSetInt8:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalInt8Array);
case Builtin::kDataViewPrototypeSetUint16:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalUint16Array);
case Builtin::kDataViewPrototypeSetInt16:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalInt16Array);
case Builtin::kDataViewPrototypeSetUint32:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalUint32Array);
case Builtin::kDataViewPrototypeSetInt32:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalInt32Array);
case Builtin::kDataViewPrototypeSetFloat16:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalFloat16Array);
case Builtin::kDataViewPrototypeSetFloat32:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalFloat32Array);
case Builtin::kDataViewPrototypeSetFloat64:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalFloat64Array);
case Builtin::kDataViewPrototypeSetBigInt64:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalBigInt64Array);
case Builtin::kDataViewPrototypeSetBigUint64:
return ReduceDataViewAccess(node, DataViewAccess::kSet,
ExternalArrayType::kExternalBigUint64Array);
case Builtin::kDatePrototypeGetFullYear:
return ReduceDatePrototypeGetField(node, JSDate::kYear);
case Builtin::kDatePrototypeGetMonth:
return ReduceDatePrototypeGetField(node, JSDate::kMonth);
case Builtin::kDatePrototypeGetDate:
return ReduceDatePrototypeGetField(node, JSDate::kDay);
case Builtin::kDatePrototypeGetDay:
return ReduceDatePrototypeGetField(node, JSDate::kWeekday);
case Builtin::kDatePrototypeGetHours:
return ReduceDatePrototypeGetField(node, JSDate::kHour);
case Builtin::kDatePrototypeGetMinutes:
return ReduceDatePrototypeGetField(node, JSDate::kMinute);
case Builtin::kDatePrototypeGetSeconds:
return ReduceDatePrototypeGetField(node, JSDate::kSecond);
case Builtin::kTypedArrayPrototypeByteLength:
return ReduceArrayBufferViewByteLengthAccessor(node, JS_TYPED_ARRAY_TYPE,
builtin);
case Builtin::kTypedArrayPrototypeByteOffset:
return ReduceArrayBufferViewByteOffsetAccessor(node, JS_TYPED_ARRAY_TYPE,
builtin);
case Builtin::kTypedArrayPrototypeLength:
return ReduceTypedArrayPrototypeLength(node);
case Builtin::kTypedArrayPrototypeToStringTag:
return ReduceTypedArrayPrototypeToStringTag(node);
case Builtin::kMathAbs:
return ReduceMathUnary(node, simplified()->NumberAbs());
case Builtin::kMathAcos:
return ReduceMathUnary(node, simplified()->NumberAcos());
case Builtin::kMathAcosh:
return ReduceMathUnary(node, simplified()->NumberAcosh());
case Builtin::kMathAsin:
return ReduceMathUnary(node, simplified()->NumberAsin());
case Builtin::kMathAsinh:
return ReduceMathUnary(node, simplified()->NumberAsinh());
case Builtin::kMathAtan:
return ReduceMathUnary(node, simplified()->NumberAtan());
case Builtin::kMathAtanh:
return ReduceMathUnary(node, simplified()->NumberAtanh());
case Builtin::kMathCbrt:
return ReduceMathUnary(node, simplified()->NumberCbrt());
case Builtin::kMathCeil:
return ReduceMathUnary(node, simplified()->NumberCeil());
case Builtin::kMathCos:
return ReduceMathUnary(node, simplified()->NumberCos());
case Builtin::kMathCosh:
return ReduceMathUnary(node, simplified()->NumberCosh());
case Builtin::kMathExp:
return ReduceMathUnary(node, simplified()->NumberExp());
case Builtin::kMathExpm1:
return ReduceMathUnary(node, simplified()->NumberExpm1());
case Builtin::kMathFloor:
return ReduceMathUnary(node, simplified()->NumberFloor());
case Builtin::kMathFround:
return ReduceMathUnary(node, simplified()->NumberFround());
case Builtin::kMathLog:
return ReduceMathUnary(node, simplified()->NumberLog());
case Builtin::kMathLog1p:
return ReduceMathUnary(node, simplified()->NumberLog1p());
case Builtin::kMathLog10:
return ReduceMathUnary(node, simplified()->NumberLog10());
case Builtin::kMathLog2:
return ReduceMathUnary(node, simplified()->NumberLog2());
case Builtin::kMathRound:
return ReduceMathUnary(node, simplified()->NumberRound());
case Builtin::kMathSign:
return ReduceMathUnary(node, simplified()->NumberSign());
case Builtin::kMathSin:
return ReduceMathUnary(node, simplified()->NumberSin());
case Builtin::kMathSinh:
return ReduceMathUnary(node, simplified()->NumberSinh());
case Builtin::kMathSqrt:
return ReduceMathUnary(node, simplified()->NumberSqrt());
case Builtin::kMathTan:
return ReduceMathUnary(node, simplified()->NumberTan());
case Builtin::kMathTanh:
return ReduceMathUnary(node, simplified()->NumberTanh());
case Builtin::kMathTrunc:
return ReduceMathUnary(node, simplified()->NumberTrunc());
case Builtin::kMathAtan2:
return ReduceMathBinary(node, simplified()->NumberAtan2());
case Builtin::kMathPow:
return ReduceMathBinary(node, simplified()->NumberPow());
case Builtin::kMathClz32:
return ReduceMathClz32(node);
case Builtin::kMathImul:
return ReduceMathImul(node);
case Builtin::kMathMax:
return ReduceMathMinMax(node, simplified()->NumberMax(),
jsgraph()->ConstantNoHole(-V8_INFINITY));
case Builtin::kMathMin:
return ReduceMathMinMax(node, simplified()->NumberMin(),
jsgraph()->ConstantNoHole(V8_INFINITY));
case Builtin::kNumberIsFinite:
return ReduceNumberIsFinite(node);
case Builtin::kNumberIsInteger:
return ReduceNumberIsInteger(node);
case Builtin::kNumberIsSafeInteger:
return ReduceNumberIsSafeInteger(node);
case Builtin::kNumberIsNaN:
return ReduceNumberIsNaN(node);
case Builtin::kNumberParseInt:
return ReduceNumberParseInt(node);
case Builtin::kGlobalIsFinite:
return ReduceGlobalIsFinite(node);
case Builtin::kGlobalIsNaN:
return ReduceGlobalIsNaN(node);
case Builtin::kMapPrototypeGet:
return ReduceMapPrototypeGet(node);
case Builtin::kMapPrototypeHas:
return ReduceMapPrototypeHas(node);
case Builtin::kSetPrototypeHas:
return ReduceSetPrototypeHas(node);
case Builtin::kRegExpPrototypeTest:
return ReduceRegExpPrototypeTest(node);
case Builtin::kReturnReceiver:
return ReduceReturnReceiver(node);
case Builtin::kStringPrototypeIndexOf:
return ReduceStringPrototypeIndexOfIncludes(
node, StringIndexOfIncludesVariant::kIndexOf);
case Builtin::kStringPrototypeIncludes:
return ReduceStringPrototypeIndexOfIncludes(
node, StringIndexOfIncludesVariant::kIncludes);
case Builtin::kStringPrototypeCharAt:
return ReduceStringPrototypeCharAt(node);
case Builtin::kStringPrototypeCharCodeAt:
return ReduceStringPrototypeStringCharCodeAt(node);
case Builtin::kStringPrototypeCodePointAt:
return ReduceStringPrototypeStringCodePointAt(node);
case Builtin::kStringPrototypeSubstring:
return ReduceStringPrototypeSubstring(node);
case Builtin::kStringPrototypeSlice:
return ReduceStringPrototypeSlice(node);
case Builtin::kStringPrototypeSubstr:
return ReduceStringPrototypeSubstr(node);
case Builtin::kStringPrototypeStartsWith:
return ReduceStringPrototypeStartsWith(node);
case Builtin::kStringPrototypeEndsWith:
return ReduceStringPrototypeEndsWith(node);
#ifdef V8_INTL_SUPPORT
case Builtin::kStringPrototypeLocaleCompareIntl:
return ReduceStringPrototypeLocaleCompareIntl(node);
case Builtin::kStringPrototypeToLowerCaseIntl:
return ReduceStringPrototypeToLowerCaseIntl(node);
case Builtin::kStringPrototypeToUpperCaseIntl:
return ReduceStringPrototypeToUpperCaseIntl(node);
#endif
case Builtin::kStringFromCharCode:
return ReduceStringFromCharCode(node);
case Builtin::kStringFromCodePoint:
return ReduceStringFromCodePoint(node);
case Builtin::kStringPrototypeIterator:
return ReduceStringPrototypeIterator(node);
case Builtin::kStringIteratorPrototypeNext:
return ReduceStringIteratorPrototypeNext(node);
case Builtin::kStringPrototypeConcat:
return ReduceStringPrototypeConcat(node);
case Builtin::kTypedArrayPrototypeEntries:
return ReduceArrayIterator(node, ArrayIteratorKind::kTypedArray,
IterationKind::kEntries);
case Builtin::kTypedArrayPrototypeKeys:
return ReduceArrayIterator(node, ArrayIteratorKind::kTypedArray,
IterationKind::kKeys);
case Builtin::kTypedArrayPrototypeValues:
return ReduceArrayIterator(node, ArrayIteratorKind::kTypedArray,
IterationKind::kValues);
case Builtin::kPromisePrototypeCatch:
return ReducePromisePrototypeCatch(node);
case Builtin::kPromisePrototypeFinally:
return ReducePromisePrototypeFinally(node);
case Builtin::kPromisePrototypeThen:
return ReducePromisePrototypeThen(node);
case Builtin::kPromiseResolveTrampoline:
return ReducePromiseResolveTrampoline(node);
case Builtin::kMapPrototypeEntries:
return ReduceCollectionIteration(node, CollectionKind::kMap,
IterationKind::kEntries);
case Builtin::kMapPrototypeKeys:
return ReduceCollectionIteration(node, CollectionKind::kMap,
IterationKind::kKeys);
case Builtin::kMapPrototypeGetSize:
return ReduceCollectionPrototypeSize(node, CollectionKind::kMap);
case Builtin::kMapPrototypeValues:
return ReduceCollectionIteration(node, CollectionKind::kMap,
IterationKind::kValues);
case Builtin::kMapIteratorPrototypeNext:
return ReduceCollectionIteratorPrototypeNext(
node, OrderedHashMap::kEntrySize, factory()->empty_ordered_hash_map(),
FIRST_JS_MAP_ITERATOR_TYPE, LAST_JS_MAP_ITERATOR_TYPE);
case Builtin::kSetPrototypeEntries:
return ReduceCollectionIteration(node, CollectionKind::kSet,
IterationKind::kEntries);
case Builtin::kSetPrototypeGetSize:
return ReduceCollectionPrototypeSize(node, CollectionKind::kSet);
case Builtin::kSetPrototypeValues:
return ReduceCollectionIteration(node, CollectionKind::kSet,
IterationKind::kValues);
case Builtin::kSetIteratorPrototypeNext:
return ReduceCollectionIteratorPrototypeNext(
node, OrderedHashSet::kEntrySize, factory()->empty_ordered_hash_set(),
FIRST_JS_SET_ITERATOR_TYPE, LAST_JS_SET_ITERATOR_TYPE);
case Builtin::kDatePrototypeGetTime:
return ReduceDatePrototypeGetTime(node);
case Builtin::kDateNow:
return ReduceDateNow(node);
case Builtin::kNumberConstructor:
return ReduceNumberConstructor(node);
case Builtin::kBigIntConstructor:
return ReduceBigIntConstructor(node);
case Builtin::kBigIntAsIntN:
case Builtin::kBigIntAsUintN:
return ReduceBigIntAsN(node, builtin);
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
case Builtin::kGetContinuationPreservedEmbedderData:
return ReduceGetContinuationPreservedEmbedderData(node);
case Builtin::kSetContinuationPreservedEmbedderData:
return ReduceSetContinuationPreservedEmbedderData(node);
#endif
default:
break;
}
if (shared.function_template_info(broker()).has_value()) {
return ReduceCallApiFunction(node, shared);
}
#if V8_ENABLE_WEBASSEMBLY
if ((flags() & kInlineJSToWasmCalls) &&
IsWasmExportedFunctionData(shared.object()->GetTrustedData(isolate()))) {
return ReduceCallWasmFunction(node, shared);
}
#endif
return NoChange();
}
TNode<Object> JSCallReducerAssembler::ReduceJSCallWithArrayLikeOrSpreadOfEmpty(
std::unordered_set<Node*>* generated_calls_with_array_like_or_spread) {
DCHECK_EQ(generated_calls_with_array_like_or_spread->count(node_ptr()), 0);
JSCallWithArrayLikeOrSpreadNode n(node_ptr());
CallParameters const& p = n.Parameters();
TNode<Object> arguments_list = n.LastArgument();
DCHECK_EQ(static_cast<Node*>(arguments_list)->opcode(),
IrOpcode::kJSCreateEmptyLiteralArray);
TNode<Map> map = LoadMap(TNode<HeapObject>::UncheckedCast(arguments_list));
TNode<HeapObject> proto = TNode<HeapObject>::UncheckedCast(
LoadField(AccessBuilder::ForMapPrototype(), map));
TNode<HeapObject> initial_array_prototype =
HeapConstant(broker()
->target_native_context()
.initial_array_prototype(broker())
.object());
TNode<Boolean> check = ReferenceEqual(proto, initial_array_prototype);
CheckIf(check, DeoptimizeReason::kWrongMap, p.feedback());
// / \
// IfTrue IfFalse
TNode<Number> length = TNode<Number>::UncheckedCast(
LoadField(AccessBuilder::ForJSArrayLength(NO_ELEMENTS), arguments_list));
return SelectIf<Object>(NumberEqual(length, ZeroConstant()))
.Then([&]() {
TNode<Object> call = CopyNode();
static_cast<Node*>(call)->RemoveInput(n.LastArgumentIndex());
NodeProperties::ChangeOp(
call, javascript()->Call(p.arity() - 1, p.frequency(), p.feedback(),
p.convert_mode(), p.speculation_mode(),
p.feedback_relation()));
return call;
})
.Else([&]() {
TNode<Object> call = CopyNode();
generated_calls_with_array_like_or_spread->insert(call);
return call;
})
.ExpectFalse()
.Value();
}
namespace {
bool TargetIsClassConstructor(Node* node, JSHeapBroker* broker) {
Node* target = NodeProperties::GetValueInput(node, 0);
OptionalSharedFunctionInfoRef shared;
HeapObjectMatcher m(target);
if (m.HasResolvedValue()) {
ObjectRef target_ref = m.Ref(broker);
if (target_ref.IsJSFunction()) {
JSFunctionRef function = target_ref.AsJSFunction();
shared = function.shared(broker);
}
} else if (target->opcode() == IrOpcode::kJSCreateClosure) {
CreateClosureParameters const& ccp =
JSCreateClosureNode{target}.Parameters();
shared = ccp.shared_info();
} else if (target->opcode() == IrOpcode::kCheckClosure) {
FeedbackCellRef cell = MakeRef(broker, FeedbackCellOf(target->op()));
shared = cell.shared_function_info(broker);
}
if (shared.has_value() && IsClassConstructor(shared->kind())) return true;
return false;
}
}
Reduction JSCallReducer::ReduceJSCallWithArrayLike(Node* node) {
JSCallWithArrayLikeNode n(node);
CallParameters const& p = n.Parameters();
DCHECK_EQ(p.arity_without_implicit_args(), 1);
if (TargetIsClassConstructor(node, broker())) {
return NoChange();
}
std::optional<Reduction> maybe_result =
TryReduceJSCallMathMinMaxWithArrayLike(node);
if (maybe_result.has_value()) {
return maybe_result.value();
}
return ReduceCallOrConstructWithArrayLikeOrSpread(
node, n.ArgumentCount(), n.LastArgumentIndex(), p.frequency(),
p.feedback(), p.speculation_mode(), p.feedback_relation(), n.target(),
n.effect(), n.control());
}
Reduction JSCallReducer::ReduceJSCallWithSpread(Node* node) {
JSCallWithSpreadNode n(node);
CallParameters const& p = n.Parameters();
DCHECK_GE(p.arity_without_implicit_args(), 1);
if (TargetIsClassConstructor(node, broker())) {
return NoChange();
}
return ReduceCallOrConstructWithArrayLikeOrSpread(
node, n.ArgumentCount(), n.LastArgumentIndex(), p.frequency(),
p.feedback(), p.speculation_mode(), p.feedback_relation(), n.target(),
n.effect(), n.control());
}
Reduction JSCallReducer::ReduceJSConstruct(Node* node) {
if (broker()->StackHasOverflowed()) return NoChange();
JSConstructNode n(node);
ConstructParameters const& p = n.Parameters();
int arity = p.arity_without_implicit_args();
Node* target = n.target();
Node* new_target = n.new_target();
Effect effect = n.effect();
Control control = n.control();
if (p.feedback().IsValid()) {
ProcessedFeedback const& feedback =
broker()->GetFeedbackForCall(p.feedback());
if (feedback.IsInsufficient()) {
return ReduceForInsufficientFeedback(
node, DeoptimizeReason::kInsufficientTypeFeedbackForConstruct);
}
OptionalHeapObjectRef feedback_target = feedback.AsCall().target();
if (feedback_target.has_value() && feedback_target->IsAllocationSite()) {
Node* array_function = jsgraph()->ConstantNoHole(
native_context().array_function(broker()), broker());
Node* check = graph()->NewNode(simplified()->ReferenceEqual(), target,
array_function);
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kWrongCallTarget), check,
effect, control);
NodeProperties::ReplaceEffectInput(node, effect);
static_assert(JSConstructNode::NewTargetIndex() == 1);
node->ReplaceInput(n.NewTargetIndex(), array_function);
node->RemoveInput(n.FeedbackVectorIndex());
NodeProperties::ChangeOp(
node,
javascript()->CreateArray(arity, feedback_target->AsAllocationSite(),
FeedbackSource()));
return Changed(node);
} else if (feedback_target.has_value() &&
!HeapObjectMatcher(new_target).HasResolvedValue() &&
feedback_target->map(broker()).is_constructor()) {
Node* new_target_feedback =
jsgraph()->ConstantNoHole(*feedback_target, broker());
Node* check = graph()->NewNode(simplified()->ReferenceEqual(), new_target,
new_target_feedback);
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kWrongCallTarget), check,
effect, control);
node->ReplaceInput(n.NewTargetIndex(), new_target_feedback);
NodeProperties::ReplaceEffectInput(node, effect);
if (target == new_target) {
node->ReplaceInput(n.TargetIndex(), new_target_feedback);
}
return Changed(node).FollowedBy(ReduceJSConstruct(node));
}
}
HeapObjectMatcher m(target);
if (m.HasResolvedValue()) {
HeapObjectRef target_ref = m.Ref(broker());
if (!target_ref.map(broker()).is_constructor()) {
NodeProperties::ReplaceValueInputs(node, target);
NodeProperties::ChangeOp(node,
javascript()->CallRuntime(
Runtime::kThrowConstructedNonConstructable));
return Changed(node);
}
if (target_ref.IsJSFunction()) {
JSFunctionRef function = target_ref.AsJSFunction();
SharedFunctionInfoRef sfi = function.shared(broker());
if (sfi.HasBreakInfo(broker())) return NoChange();
if (!function.native_context(broker()).equals(native_context())) {
return NoChange();
}
Builtin builtin =
sfi.HasBuiltinId() ? sfi.builtin_id() : Builtin::kNoBuiltinId;
switch (builtin) {
case Builtin::kArrayConstructor: {
static_assert(JSConstructNode::NewTargetIndex() == 1);
node->ReplaceInput(n.NewTargetIndex(), new_target);
node->RemoveInput(n.FeedbackVectorIndex());
NodeProperties::ChangeOp(
node,
javascript()->CreateArray(arity, std::nullopt, p.feedback()));
return Changed(node);
}
case Builtin::kObjectConstructor: {
if (arity == 0) {
node->RemoveInput(n.FeedbackVectorIndex());
NodeProperties::ChangeOp(node, javascript()->Create());
return Changed(node);
}
HeapObjectMatcher mnew_target(new_target);
if (mnew_target.HasResolvedValue() &&
!mnew_target.Ref(broker()).equals(function)) {
node->RemoveInput(n.FeedbackVectorIndex());
for (int i = n.ArgumentCount() - 1; i >= 0; i--) {
node->RemoveInput(n.ArgumentIndex(i));
}
NodeProperties::ChangeOp(node, javascript()->Create());
return Changed(node);
}
break;
}
case Builtin::kPromiseConstructor:
return ReducePromiseConstructor(node);
case Builtin::kStringConstructor:
return ReduceStringConstructor(node, function);
case Builtin::kTypedArrayConstructor:
return ReduceTypedArrayConstructor(node, function.shared(broker()));
default:
break;
}
} else if (target_ref.IsJSBoundFunction()) {
JSBoundFunctionRef function = target_ref.AsJSBoundFunction();
JSReceiverRef bound_target_function =
function.bound_target_function(broker());
FixedArrayRef bound_arguments = function.bound_arguments(broker());
const uint32_t bound_arguments_length = bound_arguments.length();
static constexpr int kInlineSize = 16;
base::SmallVector<Node*, kInlineSize> args;
for (uint32_t i = 0; i < bound_arguments_length; ++i) {
OptionalObjectRef maybe_arg = bound_arguments.TryGet(broker(), i);
if (!maybe_arg.has_value()) {
TRACE_BROKER_MISSING(broker(), "bound argument");
return NoChange();
}
args.emplace_back(
jsgraph()->ConstantNoHole(maybe_arg.value(), broker()));
}
node->ReplaceInput(n.TargetIndex(), jsgraph()->ConstantNoHole(
bound_target_function, broker()));
if (target == new_target) {
node->ReplaceInput(
n.NewTargetIndex(),
jsgraph()->ConstantNoHole(bound_target_function, broker()));
} else {
node->ReplaceInput(
n.NewTargetIndex(),
graph()->NewNode(
common()->Select(MachineRepresentation::kTagged),
graph()->NewNode(simplified()->ReferenceEqual(), target,
new_target),
jsgraph()->ConstantNoHole(bound_target_function, broker()),
new_target));
}
for (uint32_t i = 0; i < bound_arguments_length; ++i) {
node->InsertInput(graph()->zone(), n.ArgumentIndex(i), args[i]);
arity++;
}
NodeProperties::ChangeOp(
node, javascript()->Construct(JSConstructNode::ArityForArgc(arity),
p.frequency(), FeedbackSource()));
return Changed(node).FollowedBy(ReduceJSConstruct(node));
}
}
if (target->opcode() == IrOpcode::kJSCreateBoundFunction) {
Node* bound_target_function = NodeProperties::GetValueInput(target, 0);
uint32_t const bound_arguments_length =
static_cast<int>(CreateBoundFunctionParametersOf(target->op()).arity());
node->ReplaceInput(n.TargetIndex(), bound_target_function);
if (target == new_target) {
node->ReplaceInput(n.NewTargetIndex(), bound_target_function);
} else {
node->ReplaceInput(
n.NewTargetIndex(),
graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
graph()->NewNode(simplified()->ReferenceEqual(),
target, new_target),
bound_target_function, new_target));
}
for (uint32_t i = 0; i < bound_arguments_length; ++i) {
Node* value = NodeProperties::GetValueInput(target, 2 + i);
node->InsertInput(graph()->zone(), n.ArgumentIndex(i), value);
arity++;
}
NodeProperties::ChangeOp(
node, javascript()->Construct(JSConstructNode::ArityForArgc(arity),
p.frequency(), FeedbackSource()));
return Changed(node).FollowedBy(ReduceJSConstruct(node));
}
return NoChange();
}
Reduction JSCallReducer::ReduceStringPrototypeIndexOfIncludes(
Node* node, StringIndexOfIncludesVariant variant) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
if (n.ArgumentCount() > 0) {
Node* receiver = n.receiver();
Node* new_receiver = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), receiver, effect, control);
Node* search_string = n.Argument(0);
Node* new_search_string = effect =
graph()->NewNode(simplified()->CheckString(p.feedback()), search_string,
effect, control);
Node* new_position = jsgraph()->ZeroConstant();
if (n.ArgumentCount() > 1) {
Node* position = n.Argument(1);
new_position = effect = graph()->NewNode(
simplified()->CheckSmi(p.feedback()), position, effect, control);
Node* receiver_length =
graph()->NewNode(simplified()->StringLength(), new_receiver);
new_position = graph()->NewNode(
simplified()->NumberMin(),
graph()->NewNode(simplified()->NumberMax(), new_position,
jsgraph()->ZeroConstant()),
receiver_length);
}
NodeProperties::ReplaceEffectInput(node, effect);
RelaxEffectsAndControls(node);
node->ReplaceInput(0, new_receiver);
node->ReplaceInput(1, new_search_string);
node->ReplaceInput(2, new_position);
node->TrimInputCount(3);
NodeProperties::ChangeOp(node, simplified()->StringIndexOf());
if (variant == StringIndexOfIncludesVariant::kIndexOf) {
return Changed(node);
} else {
DCHECK(variant == StringIndexOfIncludesVariant::kIncludes);
Node* result =
graph()->NewNode(simplified()->BooleanNot(),
graph()->NewNode(simplified()->NumberEqual(), node,
jsgraph()->SmiConstant(-1)));
return Replace(result);
}
}
return NoChange();
}
Reduction JSCallReducer::ReduceStringPrototypeSubstring(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (n.ArgumentCount() < 1) return NoChange();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeSubstring();
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceStringPrototypeSlice(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (n.ArgumentCount() < 1) return NoChange();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeSlice();
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceStringPrototypeSubstr(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (n.ArgumentCount() < 1) return NoChange();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
Node* receiver = n.receiver();
Node* start = n.Argument(0);
Node* end = n.ArgumentOrUndefined(1, jsgraph());
receiver = effect = graph()->NewNode(simplified()->CheckString(p.feedback()),
receiver, effect, control);
start = effect = graph()->NewNode(simplified()->CheckSmi(p.feedback()), start,
effect, control);
Node* length = graph()->NewNode(simplified()->StringLength(), receiver);
{
Node* check = graph()->NewNode(simplified()->ReferenceEqual(), end,
jsgraph()->UndefinedConstant());
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kFalse), check, control);
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* vtrue = length;
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
Node* vfalse = efalse = graph()->NewNode(
simplified()->CheckSmi(p.feedback()), end, efalse, if_false);
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
end = graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue, vfalse, control);
}
Node* initStart = graph()->NewNode(
common()->Select(MachineRepresentation::kTagged, BranchHint::kFalse),
graph()->NewNode(simplified()->NumberLessThan(), start,
jsgraph()->ZeroConstant()),
graph()->NewNode(
simplified()->NumberMax(),
graph()->NewNode(simplified()->NumberAdd(), length, start),
jsgraph()->ZeroConstant()),
start);
initStart = effect = graph()->NewNode(
common()->TypeGuard(Type::UnsignedSmall()), initStart, effect, control);
Node* resultLength = graph()->NewNode(
simplified()->NumberMin(),
graph()->NewNode(simplified()->NumberMax(), end,
jsgraph()->ZeroConstant()),
graph()->NewNode(simplified()->NumberSubtract(), length, initStart));
Node* to = effect = graph()->NewNode(
common()->TypeGuard(Type::UnsignedSmall()),
graph()->NewNode(simplified()->NumberAdd(), initStart, resultLength),
effect, control);
Node* result_string = nullptr;
{
Node* check = graph()->NewNode(simplified()->NumberLessThan(),
jsgraph()->ZeroConstant(), resultLength);
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control);
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* vtrue = etrue =
graph()->NewNode(simplified()->StringSubstring(), receiver, initStart,
to, etrue, if_true);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
Node* vfalse = jsgraph()->EmptyStringConstant();
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
result_string =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue, vfalse, control);
}
ReplaceWithValue(node, result_string, effect, control);
return Replace(result_string);
}
Reduction JSCallReducer::ReduceJSConstructWithArrayLike(Node* node) {
JSConstructWithArrayLikeNode n(node);
ConstructParameters const& p = n.Parameters();
const int arraylike_index = n.LastArgumentIndex();
DCHECK_EQ(n.ArgumentCount(), 1);
return ReduceCallOrConstructWithArrayLikeOrSpread(
node, n.ArgumentCount(), arraylike_index, p.frequency(), p.feedback(),
SpeculationMode::kDisallowSpeculation, CallFeedbackRelation::kTarget,
n.target(), n.effect(), n.control());
}
Reduction JSCallReducer::ReduceJSConstructWithSpread(Node* node) {
JSConstructWithSpreadNode n(node);
ConstructParameters const& p = n.Parameters();
const int spread_index = n.LastArgumentIndex();
DCHECK_GE(n.ArgumentCount(), 1);
return ReduceCallOrConstructWithArrayLikeOrSpread(
node, n.ArgumentCount(), spread_index, p.frequency(), p.feedback(),
SpeculationMode::kDisallowSpeculation, CallFeedbackRelation::kTarget,
n.target(), n.effect(), n.control());
}
Reduction JSCallReducer::ReduceJSConstructForwardAllArgs(Node* node) {
JSConstructForwardAllArgsNode n(node);
DCHECK_EQ(n.ArgumentCount(), 0);
FrameState frame_state = n.frame_state();
if (frame_state.outer_frame_state()->opcode() != IrOpcode::kFrameState) {
return NoChange();
}
FrameState outer_state{frame_state.outer_frame_state()};
FrameStateInfo outer_info = outer_state.frame_state_info();
if (outer_info.type() == FrameStateType::kInlinedExtraArguments) {
frame_state = outer_state;
}
int argc = 0;
StateValuesAccess parameters_access(frame_state.parameters());
for (auto it = parameters_access.begin_without_receiver(); !it.done(); ++it) {
DCHECK_NOT_NULL(it.node());
node->InsertInput(graph()->zone(),
JSCallOrConstructNode::ArgumentIndex(argc++), it.node());
}
ConstructParameters const& p = n.Parameters();
NodeProperties::ChangeOp(
node, javascript()->Construct(JSConstructNode::ArityForArgc(argc),
p.frequency(), p.feedback()));
CheckIfConstructor(node);
return Changed(node).FollowedBy(ReduceJSConstruct(node));
}
Reduction JSCallReducer::ReduceReturnReceiver(Node* node) {
JSCallNode n(node);
Node* receiver = n.receiver();
ReplaceWithValue(node, receiver);
return Replace(receiver);
}
Reduction JSCallReducer::ReduceForInsufficientFeedback(
Node* node, DeoptimizeReason reason) {
DCHECK(node->opcode() == IrOpcode::kJSCall ||
node->opcode() == IrOpcode::kJSCallWithSpread ||
node->opcode() == IrOpcode::kJSCallWithArrayLike ||
node->opcode() == IrOpcode::kJSConstruct ||
node->opcode() == IrOpcode::kJSConstructWithSpread ||
node->opcode() == IrOpcode::kJSConstructWithArrayLike);
if (!(flags() & kBailoutOnUninitialized)) return NoChange();
Node* effect = NodeProperties::GetEffectInput(node);
Node* control = NodeProperties::GetControlInput(node);
Node* frame_state =
NodeProperties::FindFrameStateBefore(node, jsgraph()->Dead());
Node* deoptimize =
graph()->NewNode(common()->Deoptimize(reason, FeedbackSource()),
frame_state, effect, control);
MergeControlToEnd(graph(), common(), deoptimize);
node->TrimInputCount(0);
NodeProperties::ChangeOp(node, common()->Dead());
return Changed(node);
}
Node* JSCallReducer::LoadReceiverElementsKind(Node* receiver, Effect* effect,
Control control) {
Node* effect_node = *effect;
Node* receiver_map = effect_node =
graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()),
receiver, effect_node, control);
Node* receiver_bit_field2 = effect_node = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForMapBitField2()), receiver_map,
effect_node, control);
Node* receiver_elements_kind = graph()->NewNode(
simplified()->NumberShiftRightLogical(),
graph()->NewNode(
simplified()->NumberBitwiseAnd(), receiver_bit_field2,
jsgraph()->ConstantNoHole(Map::Bits2::ElementsKindBits::kMask)),
jsgraph()->ConstantNoHole(Map::Bits2::ElementsKindBits::kShift));
*effect = effect_node;
return receiver_elements_kind;
}
void JSCallReducer::CheckIfElementsKind(Node* receiver_elements_kind,
ElementsKind kind, Node* control,
Node** if_true, Node** if_false) {
Node* is_packed_kind =
graph()->NewNode(simplified()->NumberEqual(), receiver_elements_kind,
jsgraph()->ConstantNoHole(GetPackedElementsKind(kind)));
Node* packed_branch =
graph()->NewNode(common()->Branch(), is_packed_kind, control);
Node* if_packed = graph()->NewNode(common()->IfTrue(), packed_branch);
if (IsHoleyElementsKind(kind)) {
Node* if_not_packed = graph()->NewNode(common()->IfFalse(), packed_branch);
Node* is_holey_kind =
graph()->NewNode(simplified()->NumberEqual(), receiver_elements_kind,
jsgraph()->ConstantNoHole(GetHoleyElementsKind(kind)));
Node* holey_branch =
graph()->NewNode(common()->Branch(), is_holey_kind, if_not_packed);
Node* if_holey = graph()->NewNode(common()->IfTrue(), holey_branch);
Node* if_not_packed_not_holey =
graph()->NewNode(common()->IfFalse(), holey_branch);
*if_true = graph()->NewNode(common()->Merge(2), if_packed, if_holey);
*if_false = if_not_packed_not_holey;
} else {
*if_true = if_packed;
*if_false = graph()->NewNode(common()->IfFalse(), packed_branch);
}
}
Reduction JSCallReducer::ReduceArrayPrototypeAt(Node* node) {
if (!v8_flags.turbo_inline_array_builtins) return NoChange();
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Effect effect = n.effect();
Control control = n.control();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneVector<MapRef> maps(broker()->zone());
bool needs_fallback_builtin_call = false;
for (MapRef map : inference.GetMaps()) {
if (map.supports_fast_array_iteration(broker())) {
maps.push_back(map);
} else {
needs_fallback_builtin_call = true;
}
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
if (maps.empty()) {
return NoChange();
}
if (!dependencies()->DependOnNoElementsProtector()) {
return NoChange();
}
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(effect, control);
TNode<Object> subgraph =
a.ReduceArrayPrototypeAt(maps, needs_fallback_builtin_call);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayPrototypePush(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Effect effect = n.effect();
Control control = n.control();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
std::vector<ElementsKind> kinds;
if (!CanInlineArrayResizingBuiltin(broker(), receiver_maps, &kinds, true)) {
return inference.NoChange();
}
if (!dependencies()->DependOnNoElementsProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
IteratingArrayBuiltinReducerAssembler a(this, node);
a.InitializeEffectControl(effect, control);
TNode<Object> subgraph = a.ReduceArrayPrototypePush(&inference);
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceArrayPrototypePop(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
Node* receiver = n.receiver();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
std::vector<ElementsKind> kinds;
if (!CanInlineArrayResizingBuiltin(broker(), receiver_maps, &kinds)) {
return inference.NoChange();
}
if (!dependencies()->DependOnNoElementsProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
std::vector<Node*> controls_to_merge;
std::vector<Node*> effects_to_merge;
std::vector<Node*> values_to_merge;
Node* value = jsgraph()->UndefinedConstant();
Node* receiver_elements_kind =
LoadReceiverElementsKind(receiver, &effect, control);
Node* next_control = control;
Node* next_effect = effect;
for (size_t i = 0; i < kinds.size(); i++) {
ElementsKind kind = kinds[i];
control = next_control;
effect = next_effect;
if (i != kinds.size() - 1) {
Node* control_node = control;
CheckIfElementsKind(receiver_elements_kind, kind, control_node,
&control_node, &next_control);
control = control_node;
}
Node* length = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayLength(kind)),
receiver, effect, control);
Node* check = graph()->NewNode(simplified()->NumberEqual(), length,
jsgraph()->ZeroConstant());
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kFalse), check, control);
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* vtrue = jsgraph()->UndefinedConstant();
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
Node* vfalse;
{
Node* elements = efalse = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSObjectElements()),
receiver, efalse, if_false);
if (IsSmiOrObjectElementsKind(kind)) {
elements = efalse =
graph()->NewNode(simplified()->EnsureWritableFastElements(),
receiver, elements, efalse, if_false);
}
Node* new_length = graph()->NewNode(simplified()->NumberSubtract(),
length, jsgraph()->OneConstant());
if (v8_flags.turbo_typer_hardening) {
new_length = efalse = graph()->NewNode(
simplified()->CheckBounds(p.feedback(),
CheckBoundsFlag::kAbortOnOutOfBounds),
new_length, length, efalse, if_false);
}
efalse = graph()->NewNode(
simplified()->StoreField(AccessBuilder::ForJSArrayLength(kind)),
receiver, new_length, efalse, if_false);
vfalse = efalse = graph()->NewNode(
simplified()->LoadElement(AccessBuilder::ForFixedArrayElement(kind)),
elements, new_length, efalse, if_false);
efalse = graph()->NewNode(
simplified()->StoreElement(
AccessBuilder::ForFixedArrayElement(GetHoleyElementsKind(kind))),
elements, new_length, jsgraph()->TheHoleConstant(), efalse, if_false);
}
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
value = graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue, vfalse, control);
if (IsHoleyElementsKind(kind)) {
value =
graph()->NewNode(simplified()->ConvertTaggedHoleToUndefined(), value);
}
controls_to_merge.push_back(control);
effects_to_merge.push_back(effect);
values_to_merge.push_back(value);
}
if (controls_to_merge.size() > 1) {
int const count = static_cast<int>(controls_to_merge.size());
control = graph()->NewNode(common()->Merge(count), count,
&controls_to_merge.front());
effects_to_merge.push_back(control);
effect = graph()->NewNode(common()->EffectPhi(count), count + 1,
&effects_to_merge.front());
values_to_merge.push_back(control);
value =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, count),
count + 1, &values_to_merge.front());
}
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceArrayPrototypeShift(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* target = n.target();
Node* receiver = n.receiver();
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
std::vector<ElementsKind> kinds;
if (!CanInlineArrayResizingBuiltin(broker(), receiver_maps, &kinds)) {
return inference.NoChange();
}
if (!dependencies()->DependOnNoElementsProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
std::vector<Node*> controls_to_merge;
std::vector<Node*> effects_to_merge;
std::vector<Node*> values_to_merge;
Node* value = jsgraph()->UndefinedConstant();
Node* receiver_elements_kind =
LoadReceiverElementsKind(receiver, &effect, control);
Node* next_control = control;
Node* next_effect = effect;
for (size_t i = 0; i < kinds.size(); i++) {
ElementsKind kind = kinds[i];
control = next_control;
effect = next_effect;
if (i != kinds.size() - 1) {
Node* control_node = control;
CheckIfElementsKind(receiver_elements_kind, kind, control_node,
&control_node, &next_control);
control = control_node;
}
Node* length = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayLength(kind)),
receiver, effect, control);
Node* check0 = graph()->NewNode(simplified()->NumberEqual(), length,
jsgraph()->ZeroConstant());
Node* branch0 =
graph()->NewNode(common()->Branch(BranchHint::kFalse), check0, control);
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
Node* etrue0 = effect;
Node* vtrue0 = jsgraph()->UndefinedConstant();
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
Node* efalse0 = effect;
Node* vfalse0;
{
Node* check1 = graph()->NewNode(
simplified()->NumberLessThanOrEqual(), length,
jsgraph()->ConstantNoHole(JSArray::kMaxCopyElements));
Node* branch1 = graph()->NewNode(common()->Branch(BranchHint::kTrue),
check1, if_false0);
Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1);
Node* etrue1 = efalse0;
Node* vtrue1;
{
Node* elements = etrue1 = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSObjectElements()),
receiver, etrue1, if_true1);
vtrue1 = etrue1 = graph()->NewNode(
simplified()->LoadElement(
AccessBuilder::ForFixedArrayElement(kind)),
elements, jsgraph()->ZeroConstant(), etrue1, if_true1);
if (IsSmiOrObjectElementsKind(kind)) {
elements = etrue1 =
graph()->NewNode(simplified()->EnsureWritableFastElements(),
receiver, elements, etrue1, if_true1);
}
Node* loop = graph()->NewNode(common()->Loop(2), if_true1, if_true1);
Node* eloop =
graph()->NewNode(common()->EffectPhi(2), etrue1, etrue1, loop);
Node* terminate = graph()->NewNode(common()->Terminate(), eloop, loop);
MergeControlToEnd(graph(), common(), terminate);
Node* index = graph()->NewNode(
common()->Phi(MachineRepresentation::kTagged, 2),
jsgraph()->OneConstant(),
jsgraph()->ConstantNoHole(JSArray::kMaxCopyElements - 1), loop);
{
Node* check2 =
graph()->NewNode(simplified()->NumberLessThan(), index, length);
Node* branch2 = graph()->NewNode(common()->Branch(), check2, loop);
if_true1 = graph()->NewNode(common()->IfFalse(), branch2);
etrue1 = eloop;
Node* control2 = graph()->NewNode(common()->IfTrue(), branch2);
Node* effect2 = etrue1;
ElementAccess const access =
AccessBuilder::ForFixedArrayElement(kind);
static_assert(JSArray::kMaxCopyElements < kSmiMaxValue);
Node* index_retyped = effect2 =
graph()->NewNode(common()->TypeGuard(Type::UnsignedSmall()),
index, effect2, control2);
Node* value2 = effect2 =
graph()->NewNode(simplified()->LoadElement(access), elements,
index_retyped, effect2, control2);
effect2 = graph()->NewNode(
simplified()->StoreElement(access), elements,
graph()->NewNode(simplified()->NumberSubtract(), index_retyped,
jsgraph()->OneConstant()),
value2, effect2, control2);
loop->ReplaceInput(1, control2);
eloop->ReplaceInput(1, effect2);
index->ReplaceInput(1,
graph()->NewNode(simplified()->NumberAdd(), index,
jsgraph()->OneConstant()));
}
Node* new_length = graph()->NewNode(simplified()->NumberSubtract(),
length, jsgraph()->OneConstant());
if (v8_flags.turbo_typer_hardening) {
new_length = etrue1 = graph()->NewNode(
simplified()->CheckBounds(p.feedback(),
CheckBoundsFlag::kAbortOnOutOfBounds),
new_length, length, etrue1, if_true1);
}
etrue1 = graph()->NewNode(
simplified()->StoreField(AccessBuilder::ForJSArrayLength(kind)),
receiver, new_length, etrue1, if_true1);
etrue1 = graph()->NewNode(
simplified()->StoreElement(AccessBuilder::ForFixedArrayElement(
GetHoleyElementsKind(kind))),
elements, new_length, jsgraph()->TheHoleConstant(), etrue1,
if_true1);
}
Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1);
Node* efalse1 = efalse0;
Node* vfalse1;
{
const Builtin builtin = Builtin::kArrayShift;
auto call_descriptor = Linkage::GetCPPBuiltinCallDescriptor(
graph()->zone(), BuiltinArguments::kNumExtraArgsWithReceiver,
Builtins::name(builtin), node->op()->properties(),
CallDescriptor::kNeedsFrameState);
const bool has_builtin_exit_frame = true;
Node* stub_code = jsgraph()->CEntryStubConstant(1, ArgvMode::kStack,
has_builtin_exit_frame);
Address builtin_entry = Builtins::CppEntryOf(builtin);
Node* entry = jsgraph()->ExternalConstant(
ExternalReference::Create(builtin_entry));
Node* argc = jsgraph()->ConstantNoHole(
BuiltinArguments::kNumExtraArgsWithReceiver);
static_assert(BuiltinArguments::kNewTargetIndex == 0);
static_assert(BuiltinArguments::kTargetIndex == 1);
static_assert(BuiltinArguments::kArgcIndex == 2);
static_assert(BuiltinArguments::kPaddingIndex == 3);
if_false1 = efalse1 = vfalse1 =
graph()->NewNode(common()->Call(call_descriptor), stub_code,
receiver, jsgraph()->PaddingConstant(), argc,
target, jsgraph()->UndefinedConstant(), entry,
argc, context, frame_state, efalse1, if_false1);
}
if_false0 = graph()->NewNode(common()->Merge(2), if_true1, if_false1);
efalse0 =
graph()->NewNode(common()->EffectPhi(2), etrue1, efalse1, if_false0);
vfalse0 =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue1, vfalse1, if_false0);
}
control = graph()->NewNode(common()->Merge(2), if_true0, if_false0);
effect = graph()->NewNode(common()->EffectPhi(2), etrue0, efalse0, control);
value = graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue0, vfalse0, control);
if (IsHoleyElementsKind(kind)) {
value =
graph()->NewNode(simplified()->ConvertTaggedHoleToUndefined(), value);
}
controls_to_merge.push_back(control);
effects_to_merge.push_back(effect);
values_to_merge.push_back(value);
}
if (controls_to_merge.size() > 1) {
int const count = static_cast<int>(controls_to_merge.size());
control = graph()->NewNode(common()->Merge(count), count,
&controls_to_merge.front());
effects_to_merge.push_back(control);
effect = graph()->NewNode(common()->EffectPhi(count), count + 1,
&effects_to_merge.front());
values_to_merge.push_back(control);
value =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, count),
count + 1, &values_to_merge.front());
}
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceArrayPrototypeSlice(Node* node) {
if (!v8_flags.turbo_inline_array_builtins) return NoChange();
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Node* start = n.ArgumentOr(0, jsgraph()->ZeroConstant());
Node* end = n.ArgumentOrUndefined(1, jsgraph());
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
if (!NumberMatcher(start).Is(0) ||
!HeapObjectMatcher(end).Is(factory()->undefined_value())) {
return NoChange();
}
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
bool can_be_holey = false;
for (MapRef receiver_map : receiver_maps) {
if (!receiver_map.supports_fast_array_iteration(broker())) {
return inference.NoChange();
}
if (IsHoleyElementsKind(receiver_map.elements_kind())) {
can_be_holey = true;
}
}
if (!dependencies()->DependOnArraySpeciesProtector()) {
return inference.NoChange();
}
if (can_be_holey && !dependencies()->DependOnNoElementsProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
Callable callable =
Builtins::CallableFor(isolate(), Builtin::kCloneFastJSArray);
auto call_descriptor = Linkage::GetStubCallDescriptor(
graph()->zone(), callable.descriptor(),
callable.descriptor().GetStackParameterCount(), CallDescriptor::kNoFlags,
Operator::kNoThrow | Operator::kNoDeopt);
Node* clone = effect =
graph()->NewNode(common()->Call(call_descriptor),
jsgraph()->HeapConstantNoHole(callable.code()), receiver,
context, effect, control);
ReplaceWithValue(node, clone, effect, control);
return Replace(clone);
}
Reduction JSCallReducer::ReduceArrayIsArray(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->FalseConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Effect effect = n.effect();
Control control = n.control();
Node* context = n.context();
FrameState frame_state = n.frame_state();
Node* object = n.Argument(0);
node->ReplaceInput(0, object);
node->ReplaceInput(1, context);
node->ReplaceInput(2, frame_state);
node->ReplaceInput(3, effect);
node->ReplaceInput(4, control);
node->TrimInputCount(5);
NodeProperties::ChangeOp(node, javascript()->ObjectIsArray());
return Changed(node);
}
Reduction JSCallReducer::ReduceArrayIterator(Node* node,
ArrayIteratorKind array_kind,
IterationKind iteration_kind) {
JSCallNode n(node);
Node* receiver = n.receiver();
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAreJSReceiver()) {
return NoChange();
}
if (array_kind == ArrayIteratorKind::kTypedArray &&
!inference.AllOfInstanceTypesAre(InstanceType::JS_TYPED_ARRAY_TYPE)) {
return NoChange();
}
if (array_kind == ArrayIteratorKind::kTypedArray) {
if (!dependencies()->DependOnArrayBufferDetachingProtector()) {
CallParameters const& p = CallParametersOf(node->op());
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
std::set<ElementsKind> elements_kinds;
for (MapRef map : inference.GetMaps()) {
elements_kinds.insert(map.elements_kind());
}
inference.RelyOnMapsPreferStability(
dependencies(), jsgraph(), &effect, control,
CallParametersOf(node->op()).feedback());
JSCallReducerAssembler a(this, node);
a.CheckIfTypedArrayWasDetachedOrOutOfBounds(
TNode<JSTypedArray>::UncheckedCast(receiver),
std::move(elements_kinds), p.feedback());
std::tie(effect, control) = ReleaseEffectAndControlFromAssembler(&a);
}
}
ReplaceWithValue(node, node, node, control);
node->ReplaceInput(0, receiver);
node->ReplaceInput(1, context);
node->ReplaceInput(2, effect);
node->ReplaceInput(3, control);
node->TrimInputCount(4);
NodeProperties::ChangeOp(node,
javascript()->CreateArrayIterator(iteration_kind));
return Changed(node);
}
Reduction JSCallReducer::ReduceArrayIteratorPrototypeNext(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
Node* iterator = n.receiver();
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (iterator->opcode() != IrOpcode::kJSCreateArrayIterator) return NoChange();
IterationKind const iteration_kind =
CreateArrayIteratorParametersOf(iterator->op()).kind();
Node* iterated_object = NodeProperties::GetValueInput(iterator, 0);
Effect iterator_effect{NodeProperties::GetEffectInput(iterator)};
MapInference inference(broker(), iterated_object, iterator_effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& iterated_object_maps = inference.GetMaps();
ElementsKind elements_kind = iterated_object_maps[0].elements_kind();
if (IsTypedArrayElementsKind(elements_kind)) {
if (elements_kind == BIGUINT64_ELEMENTS ||
elements_kind == BIGINT64_ELEMENTS) {
return inference.NoChange();
}
for (MapRef iterated_object_map : iterated_object_maps) {
if (iterated_object_map.elements_kind() != elements_kind) {
return inference.NoChange();
}
}
} else {
if (!CanInlineArrayIteratingBuiltin(broker(), iterated_object_maps,
&elements_kind)) {
return inference.NoChange();
}
}
if (IsHoleyElementsKind(elements_kind) &&
!dependencies()->DependOnNoElementsProtector()) {
return inference.NoChange();
}
inference.InsertMapChecks(jsgraph(), &effect, control, p.feedback());
if (IsTypedArrayElementsKind(elements_kind)) {
if (!dependencies()->DependOnArrayBufferDetachingProtector()) {
Node* buffer = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayBufferViewBuffer()),
iterated_object, effect, control);
Node* buffer_bit_field = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayBufferBitField()),
buffer, effect, control);
Node* check = graph()->NewNode(
simplified()->NumberEqual(),
graph()->NewNode(
simplified()->NumberBitwiseAnd(), buffer_bit_field,
jsgraph()->ConstantNoHole(JSArrayBuffer::WasDetachedBit::kMask)),
jsgraph()->ZeroConstant());
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kArrayBufferWasDetached,
p.feedback()),
check, effect, control);
}
}
FieldAccess index_access = AccessBuilder::ForJSArrayIteratorNextIndex();
if (!IsTypedArrayElementsKind(elements_kind)) {
index_access.type = TypeCache::Get()->kJSArrayLengthType;
}
Node* index = effect = graph()->NewNode(simplified()->LoadField(index_access),
iterator, effect, control);
Node* elements = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSObjectElements()),
iterated_object, effect, control);
Node* length;
if (IsTypedArrayElementsKind(elements_kind)) {
Node* byte_length = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSTypedArrayByteLength()),
iterated_object, effect, control);
Node* byte_length_shifted = graph()->NewNode(
jsgraph()->machine()->WordShr(), byte_length,
jsgraph()->UintPtrConstant(ElementsKindToShiftSize(elements_kind)));
length = graph()->NewNode(
common()->ExitMachineGraph(MachineType::PointerRepresentation(),
TypeCache::Get()->kJSTypedArrayLengthType),
byte_length_shifted);
} else {
length = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayLength(elements_kind)),
iterated_object, effect, control);
}
Node* check = graph()->NewNode(simplified()->NumberLessThan(), index, length);
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kNone), check, control);
Node* done_true;
Node* value_true;
Node* etrue = effect;
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
{
if (v8_flags.turbo_typer_hardening) {
index = etrue = graph()->NewNode(
simplified()->CheckBounds(
p.feedback(), CheckBoundsFlag::kAbortOnOutOfBounds |
(IsTypedArrayElementsKind(elements_kind)
? CheckBoundsFlag::kAllow64BitBounds
: CheckBoundsFlag(0))),
index, length, etrue, if_true);
}
done_true = jsgraph()->FalseConstant();
if (iteration_kind == IterationKind::kKeys) {
value_true = index;
} else {
DCHECK(iteration_kind == IterationKind::kEntries ||
iteration_kind == IterationKind::kValues);
if (IsTypedArrayElementsKind(elements_kind)) {
Node* base_ptr = etrue =
graph()->NewNode(simplified()->LoadField(
AccessBuilder::ForJSTypedArrayBasePointer()),
iterated_object, etrue, if_true);
Node* external_ptr = etrue = graph()->NewNode(
simplified()->LoadField(
AccessBuilder::ForJSTypedArrayExternalPointer()),
iterated_object, etrue, if_true);
ExternalArrayType array_type = kExternalInt8Array;
switch (elements_kind) {
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype) \
case TYPE##_ELEMENTS: \
array_type = kExternal##Type##Array; \
break;
TYPED_ARRAYS(TYPED_ARRAY_CASE)
default:
UNREACHABLE();
#undef TYPED_ARRAY_CASE
}
Node* buffer = etrue =
graph()->NewNode(simplified()->LoadField(
AccessBuilder::ForJSArrayBufferViewBuffer()),
iterated_object, etrue, if_true);
value_true = etrue =
graph()->NewNode(simplified()->LoadTypedElement(array_type), buffer,
base_ptr, external_ptr, index, etrue, if_true);
} else {
value_true = etrue = graph()->NewNode(
simplified()->LoadElement(
AccessBuilder::ForFixedArrayElement(elements_kind)),
elements, index, etrue, if_true);
if (IsHoleyElementsKind(elements_kind)) {
value_true = ConvertHoleToUndefined(value_true, elements_kind);
}
}
if (iteration_kind == IterationKind::kEntries) {
value_true = etrue =
graph()->NewNode(javascript()->CreateKeyValueArray(), index,
value_true, context, etrue);
} else {
DCHECK_EQ(IterationKind::kValues, iteration_kind);
}
}
Node* next_index = graph()->NewNode(simplified()->NumberAdd(), index,
jsgraph()->OneConstant());
etrue = graph()->NewNode(simplified()->StoreField(index_access), iterator,
next_index, etrue, if_true);
}
Node* done_false;
Node* value_false;
Node* efalse = effect;
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
{
done_false = jsgraph()->TrueConstant();
value_false = jsgraph()->UndefinedConstant();
Node* end_index = jsgraph()->ConstantNoHole(index_access.type.Max());
efalse = graph()->NewNode(simplified()->StoreField(index_access), iterator,
end_index, efalse, if_false);
}
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
Node* value =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
value_true, value_false, control);
Node* done =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
done_true, done_false, control);
value = effect = graph()->NewNode(javascript()->CreateIterResultObject(),
value, done, context, effect);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceStringPrototypeStringCharCodeAt(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation &&
p.speculation_mode() !=
SpeculationMode::kDisallowBoundsCheckSpeculation) {
return NoChange();
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeCharCodeAt(p.speculation_mode());
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceStringPrototypeStringCodePointAt(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Node* index = n.ArgumentOr(0, jsgraph()->ZeroConstant());
Effect effect = n.effect();
Control control = n.control();
receiver = effect = graph()->NewNode(simplified()->CheckString(p.feedback()),
receiver, effect, control);
Node* receiver_length =
graph()->NewNode(simplified()->StringLength(), receiver);
index = effect = graph()->NewNode(simplified()->CheckBounds(p.feedback()),
index, receiver_length, effect, control);
Node* value = effect = graph()->NewNode(simplified()->StringCodePointAt(),
receiver, index, effect, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceStringPrototypeStartsWith(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
TNode<Object> search_element = n.ArgumentOrUndefined(0, jsgraph());
HeapObjectMatcher search_element_matcher(search_element);
if (search_element_matcher.HasResolvedValue()) {
ObjectRef target_ref = search_element_matcher.Ref(broker());
if (!target_ref.IsString()) return NoChange();
StringRef search_element_string = target_ref.AsString();
if (!search_element_string.IsContentAccessible()) return NoChange();
int length = search_element_string.length();
if (length <= kMaxInlineMatchSequence) {
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeStartsWith(search_element_string);
return ReplaceWithSubgraph(&a, subgraph);
}
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeStartsWith();
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceStringPrototypeEndsWith(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
TNode<Object> search_element = n.ArgumentOrUndefined(0, jsgraph());
HeapObjectMatcher search_element_matcher(search_element);
if (search_element_matcher.HasResolvedValue()) {
ObjectRef target_ref = search_element_matcher.Ref(broker());
if (!target_ref.IsString()) return NoChange();
StringRef search_element_string = target_ref.AsString();
if (!search_element_string.IsContentAccessible()) return NoChange();
int length = search_element_string.length();
if (length <= kMaxInlineMatchSequence) {
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeEndsWith(search_element_string);
return ReplaceWithSubgraph(&a, subgraph);
}
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeEndsWith();
return ReplaceWithSubgraph(&a, subgraph);
}
Reduction JSCallReducer::ReduceStringPrototypeCharAt(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation &&
p.speculation_mode() !=
SpeculationMode::kDisallowBoundsCheckSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Node* index = n.ArgumentOr(0, jsgraph()->ZeroConstant());
HeapObjectMatcher receiver_matcher(receiver);
NumberMatcher index_matcher(index);
if (receiver_matcher.HasResolvedValue()) {
HeapObjectRef receiver_ref = receiver_matcher.Ref(broker());
if (!receiver_ref.IsString()) return NoChange();
StringRef receiver_string = receiver_ref.AsString();
bool is_content_accessible = receiver_string.IsContentAccessible();
bool is_integer_in_max_range =
index_matcher.IsInteger() &&
index_matcher.IsInRange(
0.0, static_cast<double>(JSObject::kMaxElementIndex));
if (is_content_accessible && is_integer_in_max_range) {
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeCharAt(
receiver_string,
static_cast<uint32_t>(index_matcher.ResolvedValue()));
return ReplaceWithSubgraph(&a, subgraph);
}
}
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceStringPrototypeCharAt(p.speculation_mode());
return ReplaceWithSubgraph(&a, subgraph);
}
#ifdef V8_INTL_SUPPORT
Reduction JSCallReducer::ReduceStringPrototypeToLowerCaseIntl(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
FrameState frame_state = n.frame_state();
Node* receiver = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), n.receiver(), effect, control);
Node* value = effect = control =
graph()->NewNode(simplified()->StringToLowerCaseIntl(), receiver,
frame_state, n.context(), effect, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceStringPrototypeToUpperCaseIntl(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
FrameState frame_state = n.frame_state();
Node* receiver = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), n.receiver(), effect, control);
Node* value = effect = control =
graph()->NewNode(simplified()->StringToUpperCaseIntl(), receiver,
frame_state, n.context(), effect, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
#endif
Reduction JSCallReducer::ReduceStringFromCharCode(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() == 1) {
Effect effect = n.effect();
Control control = n.control();
Node* input = n.Argument(0);
input = effect = graph()->NewNode(
simplified()->SpeculativeToNumber(NumberOperationHint::kNumberOrOddball,
p.feedback()),
input, effect, control);
Node* value =
graph()->NewNode(simplified()->StringFromSingleCharCode(), input);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
return NoChange();
}
Reduction JSCallReducer::ReduceStringFromCodePoint(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() != 1) return NoChange();
Effect effect = n.effect();
Control control = n.control();
Node* input = n.Argument(0);
input = effect = graph()->NewNode(
simplified()->CheckBounds(p.feedback(),
CheckBoundsFlag::kConvertStringAndMinusZero),
input, jsgraph()->ConstantNoHole(0x10FFFF + 1), effect, control);
Node* value =
graph()->NewNode(simplified()->StringFromSingleCodePoint(), input);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
Reduction JSCallReducer::ReduceStringPrototypeIterator(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* effect = NodeProperties::GetEffectInput(node);
Node* control = NodeProperties::GetControlInput(node);
Node* receiver = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), n.receiver(), effect, control);
Node* iterator = effect =
graph()->NewNode(javascript()->CreateStringIterator(), receiver,
jsgraph()->NoContextConstant(), effect);
ReplaceWithValue(node, iterator, effect, control);
return Replace(iterator);
}
#ifdef V8_INTL_SUPPORT
Reduction JSCallReducer::ReduceStringPrototypeLocaleCompareIntl(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1 || n.ArgumentCount() > 3) {
return NoChange();
}
{
DirectHandle<Object> locales;
{
HeapObjectMatcher m(n.ArgumentOrUndefined(1, jsgraph()));
if (!m.HasResolvedValue()) return NoChange();
if (m.Is(factory()->undefined_value())) {
locales = factory()->undefined_value();
} else {
ObjectRef ref = m.Ref(broker());
if (!ref.IsString()) return NoChange();
StringRef sref = ref.AsString();
if (std::optional<Handle<String>> maybe_locales =
sref.ObjectIfContentAccessible(broker())) {
locales = *maybe_locales;
} else {
return NoChange();
}
}
}
TNode<Object> options = n.ArgumentOrUndefined(2, jsgraph());
{
HeapObjectMatcher m(options);
if (!m.Is(factory()->undefined_value())) {
return NoChange();
}
}
if (Intl::CompareStringsOptionsFor(broker()->local_isolate_or_isolate(),
locales, factory()->undefined_value()) !=
Intl::CompareStringsOptions::kTryFastPath) {
return NoChange();
}
}
Callable callable =
Builtins::CallableFor(isolate(), Builtin::kStringFastLocaleCompare);
auto call_descriptor = Linkage::GetStubCallDescriptor(
graph()->zone(), callable.descriptor(),
callable.descriptor().GetStackParameterCount(),
CallDescriptor::kNeedsFrameState);
node->RemoveInput(n.FeedbackVectorIndex());
if (n.ArgumentCount() == 3) {
node->RemoveInput(n.ArgumentIndex(2));
} else if (n.ArgumentCount() == 1) {
node->InsertInput(graph()->zone(), n.LastArgumentIndex() + 1,
jsgraph()->UndefinedConstant());
} else {
DCHECK_EQ(2, n.ArgumentCount());
}
node->InsertInput(graph()->zone(), 0,
jsgraph()->HeapConstantNoHole(callable.code()));
NodeProperties::ChangeOp(node, common()->Call(call_descriptor));
return Changed(node);
}
#endif
Reduction JSCallReducer::ReduceStringIteratorPrototypeNext(Node* node) {
JSCallNode n(node);
Node* receiver = n.receiver();
Effect effect = n.effect();
Control control = n.control();
Node* context = n.context();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(JS_STRING_ITERATOR_TYPE)) {
return NoChange();
}
Node* string = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSStringIteratorString()),
receiver, effect, control);
Node* index = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSStringIteratorIndex()),
receiver, effect, control);
Node* length = graph()->NewNode(simplified()->StringLength(), string);
Node* check0 =
graph()->NewNode(simplified()->NumberLessThan(), index, length);
Node* branch0 =
graph()->NewNode(common()->Branch(BranchHint::kNone), check0, control);
Node* etrue0 = effect;
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
Node* done_true;
Node* vtrue0;
{
done_true = jsgraph()->FalseConstant();
vtrue0 = etrue0 = graph()->NewNode(simplified()->StringFromCodePointAt(),
string, index, etrue0, if_true0);
Node* char_length = graph()->NewNode(simplified()->StringLength(), vtrue0);
index = graph()->NewNode(simplified()->NumberAdd(), index, char_length);
etrue0 = graph()->NewNode(
simplified()->StoreField(AccessBuilder::ForJSStringIteratorIndex()),
receiver, index, etrue0, if_true0);
}
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
Node* done_false;
Node* vfalse0;
{
vfalse0 = jsgraph()->UndefinedConstant();
done_false = jsgraph()->TrueConstant();
}
control = graph()->NewNode(common()->Merge(2), if_true0, if_false0);
effect = graph()->NewNode(common()->EffectPhi(2), etrue0, effect, control);
Node* value =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2), vtrue0,
vfalse0, control);
Node* done =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
done_true, done_false, control);
value = effect = graph()->NewNode(javascript()->CreateIterResultObject(),
value, done, context, effect);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceStringPrototypeConcat(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
const int parameter_count = n.ArgumentCount();
if (parameter_count > 1) return NoChange();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
Node* receiver = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), n.receiver(), effect, control);
if (parameter_count == 0) {
ReplaceWithValue(node, receiver, effect, control);
return Replace(receiver);
}
Node* argument = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), n.Argument(0), effect, control);
Node* receiver_length =
graph()->NewNode(simplified()->StringLength(), receiver);
Node* argument_length =
graph()->NewNode(simplified()->StringLength(), argument);
Node* length = graph()->NewNode(simplified()->NumberAdd(), receiver_length,
argument_length);
length = effect = graph()->NewNode(
simplified()->CheckBounds(p.feedback()), length,
jsgraph()->ConstantNoHole(String::kMaxLength + 1), effect, control);
Node* value = graph()->NewNode(simplified()->StringConcat(), length, receiver,
argument);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
namespace {
FrameState CreateStringCreateLazyDeoptContinuationFrameState(
JSGraph* graph, SharedFunctionInfoRef shared, Node* target, Node* context,
Node* outer_frame_state) {
Node* const receiver = graph->TheHoleConstant();
Node* stack_parameters[]{receiver};
const int stack_parameter_count = arraysize(stack_parameters);
return CreateJavaScriptBuiltinContinuationFrameState(
graph, shared, Builtin::kStringCreateLazyDeoptContinuation, target,
context, stack_parameters, stack_parameter_count, outer_frame_state,
ContinuationFrameStateMode::LAZY);
}
}
Reduction JSCallReducer::ReduceStringConstructor(Node* node,
JSFunctionRef constructor) {
JSConstructNode n(node);
if (n.target() != n.new_target()) return NoChange();
DCHECK_EQ(constructor, native_context().string_function(broker_));
DCHECK(constructor.initial_map(broker_).IsJSPrimitiveWrapperMap());
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
Node* primitive_value;
if (n.ArgumentCount() == 0) {
primitive_value = jsgraph()->EmptyStringConstant();
} else {
frame_state = CreateConstructInvokeStubFrameState(
node, frame_state, constructor.shared(broker_), context, common(),
graph());
Node* continuation_frame_state =
CreateStringCreateLazyDeoptContinuationFrameState(
jsgraph(), constructor.shared(broker_), n.target(), context,
frame_state);
primitive_value = effect = control =
graph()->NewNode(javascript()->ToString(), n.Argument(0), context,
continuation_frame_state, effect, control);
Node* on_exception = nullptr;
if (NodeProperties::IsExceptionalCall(node, &on_exception)) {
Node* if_exception =
graph()->NewNode(common()->IfException(), effect, control);
control = graph()->NewNode(common()->IfSuccess(), control);
Node* merge =
graph()->NewNode(common()->Merge(2), if_exception, on_exception);
Node* ephi = graph()->NewNode(common()->EffectPhi(2), if_exception,
on_exception, merge);
Node* phi =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
if_exception, on_exception, merge);
ReplaceWithValue(on_exception, phi, ephi, merge);
merge->ReplaceInput(1, on_exception);
ephi->ReplaceInput(1, on_exception);
phi->ReplaceInput(1, on_exception);
}
}
RelaxControls(node, control);
node->ReplaceInput(0, primitive_value);
node->ReplaceInput(1, context);
node->ReplaceInput(2, effect);
node->TrimInputCount(3);
NodeProperties::ChangeOp(node, javascript()->CreateStringWrapper());
return Changed(node);
}
Reduction JSCallReducer::ReducePromiseConstructor(Node* node) {
PromiseBuiltinReducerAssembler a(this, node);
if (a.ConstructArity() < 1) return NoChange();
if (a.TargetInput() != a.NewTargetInput()) return NoChange();
if (!dependencies()->DependOnPromiseHookProtector()) return NoChange();
TNode<Object> subgraph = a.ReducePromiseConstructor(native_context());
return ReplaceWithSubgraph(&a, subgraph);
}
bool JSCallReducer::DoPromiseChecks(MapInference* inference) {
if (!inference->HaveMaps()) return false;
ZoneRefSet<Map> const& receiver_maps = inference->GetMaps();
for (MapRef receiver_map : receiver_maps) {
if (!receiver_map.IsJSPromiseMap()) return false;
HeapObjectRef prototype = receiver_map.prototype(broker());
if (!prototype.equals(native_context().promise_prototype(broker()))) {
return false;
}
}
return true;
}
Reduction JSCallReducer::ReducePromisePrototypeCatch(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
int arity = p.arity_without_implicit_args();
Node* receiver = n.receiver();
Effect effect = n.effect();
Control control = n.control();
MapInference inference(broker(), receiver, effect);
if (!DoPromiseChecks(&inference)) return inference.NoChange();
if (!dependencies()->DependOnPromiseThenProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
Node* target = jsgraph()->ConstantNoHole(
native_context().promise_then(broker()), broker());
NodeProperties::ReplaceValueInput(node, target, 0);
NodeProperties::ReplaceEffectInput(node, effect);
for (; arity > 1; --arity) node->RemoveInput(3);
for (; arity < 2; ++arity) {
node->InsertInput(graph()->zone(), 2, jsgraph()->UndefinedConstant());
}
NodeProperties::ChangeOp(
node, javascript()->Call(
JSCallNode::ArityForArgc(arity), p.frequency(), p.feedback(),
ConvertReceiverMode::kNotNullOrUndefined, p.speculation_mode(),
CallFeedbackRelation::kUnrelated));
return Changed(node).FollowedBy(ReducePromisePrototypeThen(node));
}
Node* JSCallReducer::CreateClosureFromBuiltinSharedFunctionInfo(
SharedFunctionInfoRef shared, Node* context, Node* effect, Node* control) {
DCHECK(shared.HasBuiltinId());
Handle<FeedbackCell> feedback_cell =
isolate()->factory()->many_closures_cell();
Callable const callable =
Builtins::CallableFor(isolate(), shared.builtin_id());
CodeRef code = MakeRef(broker(), *callable.code());
return graph()->NewNode(javascript()->CreateClosure(shared, code),
jsgraph()->HeapConstantNoHole(feedback_cell), context,
effect, control);
}
Reduction JSCallReducer::ReducePromisePrototypeFinally(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
int arity = p.arity_without_implicit_args();
Node* receiver = n.receiver();
Node* on_finally = n.ArgumentOrUndefined(0, jsgraph());
Effect effect = n.effect();
Control control = n.control();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
MapInference inference(broker(), receiver, effect);
if (!DoPromiseChecks(&inference)) return inference.NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
if (!dependencies()->DependOnPromiseHookProtector()) {
return inference.NoChange();
}
if (!dependencies()->DependOnPromiseThenProtector()) {
return inference.NoChange();
}
if (!dependencies()->DependOnPromiseSpeciesProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
Node* check = graph()->NewNode(simplified()->ObjectIsCallable(), on_finally);
Node* branch =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control);
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* catch_true;
Node* then_true;
{
Node* context = jsgraph()->ConstantNoHole(native_context(), broker());
Node* constructor = jsgraph()->ConstantNoHole(
native_context().promise_function(broker()), broker());
context = etrue = graph()->NewNode(
javascript()->CreateFunctionContext(
native_context().scope_info(broker()),
int{PromiseBuiltins::kPromiseFinallyContextLength} -
Context::MIN_CONTEXT_SLOTS,
FUNCTION_SCOPE),
context, etrue, if_true);
etrue = graph()->NewNode(
simplified()->StoreField(
AccessBuilder::ForContextSlot(PromiseBuiltins::kOnFinallySlot)),
context, on_finally, etrue, if_true);
etrue = graph()->NewNode(
simplified()->StoreField(
AccessBuilder::ForContextSlot(PromiseBuiltins::kConstructorSlot)),
context, constructor, etrue, if_true);
SharedFunctionInfoRef promise_catch_finally =
MakeRef(broker(), factory()->promise_catch_finally_shared_fun());
catch_true = etrue = CreateClosureFromBuiltinSharedFunctionInfo(
promise_catch_finally, context, etrue, if_true);
SharedFunctionInfoRef promise_then_finally =
MakeRef(broker(), factory()->promise_then_finally_shared_fun());
then_true = etrue = CreateClosureFromBuiltinSharedFunctionInfo(
promise_then_finally, context, etrue, if_true);
}
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
Node* catch_false = on_finally;
Node* then_false = on_finally;
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
Node* catch_finally =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
catch_true, catch_false, control);
Node* then_finally =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
then_true, then_false, control);
{
effect = graph()->NewNode(simplified()->MapGuard(receiver_maps), receiver,
effect, control);
}
Node* target = jsgraph()->ConstantNoHole(
native_context().promise_then(broker()), broker());
NodeProperties::ReplaceValueInput(node, target, n.TargetIndex());
NodeProperties::ReplaceEffectInput(node, effect);
NodeProperties::ReplaceControlInput(node, control);
for (; arity > 2; --arity) node->RemoveInput(2);
for (; arity < 2; ++arity) {
node->InsertInput(graph()->zone(), 2, then_finally);
}
node->ReplaceInput(2, then_finally);
node->ReplaceInput(3, catch_finally);
NodeProperties::ChangeOp(
node, javascript()->Call(
JSCallNode::ArityForArgc(arity), p.frequency(), p.feedback(),
ConvertReceiverMode::kNotNullOrUndefined, p.speculation_mode(),
CallFeedbackRelation::kUnrelated));
return Changed(node).FollowedBy(ReducePromisePrototypeThen(node));
}
Reduction JSCallReducer::ReducePromisePrototypeThen(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Node* on_fulfilled = n.ArgumentOrUndefined(0, jsgraph());
Node* on_rejected = n.ArgumentOrUndefined(1, jsgraph());
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
FrameState frame_state = n.frame_state();
MapInference inference(broker(), receiver, effect);
if (!DoPromiseChecks(&inference)) return inference.NoChange();
if (!dependencies()->DependOnPromiseHookProtector()) {
return inference.NoChange();
}
if (!dependencies()->DependOnPromiseSpeciesProtector()) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
on_fulfilled = graph()->NewNode(
common()->Select(MachineRepresentation::kTagged, BranchHint::kTrue),
graph()->NewNode(simplified()->ObjectIsCallable(), on_fulfilled),
on_fulfilled, jsgraph()->UndefinedConstant());
on_rejected = graph()->NewNode(
common()->Select(MachineRepresentation::kTagged, BranchHint::kTrue),
graph()->NewNode(simplified()->ObjectIsCallable(), on_rejected),
on_rejected, jsgraph()->UndefinedConstant());
Node* promise = effect =
graph()->NewNode(javascript()->CreatePromise(), context, effect);
promise = effect = graph()->NewNode(
javascript()->PerformPromiseThen(), receiver, on_fulfilled, on_rejected,
promise, context, frame_state, effect, control);
MapRef promise_map =
native_context().promise_function(broker()).initial_map(broker());
effect =
graph()->NewNode(simplified()->MapGuard(ZoneRefSet<Map>(promise_map)),
promise, effect, control);
ReplaceWithValue(node, promise, effect, control);
return Replace(promise);
}
Reduction JSCallReducer::ReducePromiseResolveTrampoline(Node* node) {
JSCallNode n(node);
Node* receiver = n.receiver();
Node* value = n.ArgumentOrUndefined(0, jsgraph());
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
FrameState frame_state = n.frame_state();
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAreJSReceiver()) {
return NoChange();
}
node->ReplaceInput(0, receiver);
node->ReplaceInput(1, value);
node->ReplaceInput(2, context);
node->ReplaceInput(3, frame_state);
node->ReplaceInput(4, effect);
node->ReplaceInput(5, control);
node->TrimInputCount(6);
NodeProperties::ChangeOp(node, javascript()->PromiseResolve());
return Changed(node);
}
Reduction JSCallReducer::ReduceTypedArrayConstructor(
Node* node, SharedFunctionInfoRef shared) {
JSConstructNode n(node);
Node* target = n.target();
Node* arg0 = n.ArgumentOrUndefined(0, jsgraph());
Node* arg1 = n.ArgumentOrUndefined(1, jsgraph());
Node* arg2 = n.ArgumentOrUndefined(2, jsgraph());
Node* new_target = n.new_target();
Node* context = n.context();
FrameState frame_state = n.frame_state();
Effect effect = n.effect();
Control control = n.control();
frame_state = CreateConstructInvokeStubFrameState(node, frame_state, shared,
context, common(), graph());
Node* const receiver = jsgraph()->TheHoleConstant();
Node* continuation_frame_state = CreateGenericLazyDeoptContinuationFrameState(
jsgraph(), shared, target, context, receiver, frame_state);
Node* result = graph()->NewNode(javascript()->CreateTypedArray(), target,
new_target, arg0, arg1, arg2, context,
continuation_frame_state, effect, control);
return Replace(result);
}
Reduction JSCallReducer::ReduceTypedArrayPrototypeToStringTag(Node* node) {
Node* receiver = NodeProperties::GetValueInput(node, 1);
Node* effect = NodeProperties::GetEffectInput(node);
Node* control = NodeProperties::GetControlInput(node);
NodeVector values(graph()->zone());
NodeVector effects(graph()->zone());
NodeVector controls(graph()->zone());
Node* smi_check = graph()->NewNode(simplified()->ObjectIsSmi(), receiver);
control = graph()->NewNode(common()->Branch(BranchHint::kFalse), smi_check,
control);
values.push_back(jsgraph()->UndefinedConstant());
effects.push_back(effect);
controls.push_back(graph()->NewNode(common()->IfTrue(), control));
control = graph()->NewNode(common()->IfFalse(), control);
Node* receiver_map = effect =
graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()),
receiver, effect, control);
Node* receiver_bit_field2 = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForMapBitField2()), receiver_map,
effect, control);
Node* receiver_elements_kind = graph()->NewNode(
simplified()->NumberShiftRightLogical(),
graph()->NewNode(
simplified()->NumberBitwiseAnd(), receiver_bit_field2,
jsgraph()->ConstantNoHole(Map::Bits2::ElementsKindBits::kMask)),
jsgraph()->ConstantNoHole(Map::Bits2::ElementsKindBits::kShift));
receiver_elements_kind = graph()->NewNode(
simplified()->NumberSubtract(), receiver_elements_kind,
jsgraph()->ConstantNoHole(FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND));
static_assert(LAST_FIXED_TYPED_ARRAY_ELEMENTS_KIND + 1 ==
FIRST_RAB_GSAB_FIXED_TYPED_ARRAY_ELEMENTS_KIND);
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype) \
do { \
Node* check = graph()->NewNode( \
simplified()->NumberEqual(), receiver_elements_kind, \
jsgraph()->ConstantNoHole(TYPE##_ELEMENTS - \
FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND)); \
control = graph()->NewNode(common()->Branch(), check, control); \
values.push_back(jsgraph()->ConstantNoHole( \
broker()->GetTypedArrayStringTag(TYPE##_ELEMENTS), broker())); \
effects.push_back(effect); \
controls.push_back(graph()->NewNode(common()->IfTrue(), control)); \
control = graph()->NewNode(common()->IfFalse(), control); \
} while (false);
TYPED_ARRAYS(TYPED_ARRAY_CASE)
RAB_GSAB_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE
values.push_back(jsgraph()->UndefinedConstant());
effects.push_back(effect);
controls.push_back(control);
int const count = static_cast<int>(controls.size());
control = graph()->NewNode(common()->Merge(count), count, &controls.front());
effects.push_back(control);
effect =
graph()->NewNode(common()->EffectPhi(count), count + 1, &effects.front());
values.push_back(control);
Node* value =
graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, count),
count + 1, &values.front());
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceArrayBufferViewByteLengthAccessor(
Node* node, InstanceType instance_type, Builtin builtin) {
DCHECK(instance_type == JS_TYPED_ARRAY_TYPE ||
instance_type == JS_DATA_VIEW_TYPE);
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(instance_type)) {
return inference.NoChange();
}
std::set<ElementsKind> elements_kinds;
bool maybe_rab_gsab = false;
if (instance_type == JS_TYPED_ARRAY_TYPE) {
for (MapRef map : inference.GetMaps()) {
ElementsKind kind = map.elements_kind();
elements_kinds.insert(kind);
if (IsRabGsabTypedArrayElementsKind(kind)) maybe_rab_gsab = true;
}
}
if (!maybe_rab_gsab) {
Reduction unused_reduction = inference.NoChange();
USE(unused_reduction);
return ReduceArrayBufferViewAccessor(
node, instance_type, AccessBuilder::ForJSArrayBufferViewByteLength(),
builtin);
}
const CallParameters& p = CallParametersOf(node->op());
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return inference.NoChange();
}
DCHECK(p.feedback().IsValid());
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
const bool depended_on_detaching_protector =
dependencies()->DependOnArrayBufferDetachingProtector();
if (!depended_on_detaching_protector && instance_type == JS_DATA_VIEW_TYPE) {
return inference.NoChange();
}
JSCallReducerAssembler a(this, node);
TNode<JSTypedArray> typed_array =
TNode<JSTypedArray>::UncheckedCast(receiver);
TNode<Number> length = a.ArrayBufferViewByteLength(
typed_array, instance_type, std::move(elements_kinds), a.ContextInput());
return ReplaceWithSubgraph(&a, length);
}
Reduction JSCallReducer::ReduceArrayBufferViewByteOffsetAccessor(
Node* node, InstanceType instance_type, Builtin builtin) {
DCHECK(instance_type == JS_TYPED_ARRAY_TYPE ||
instance_type == JS_DATA_VIEW_TYPE);
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(instance_type)) {
return inference.NoChange();
}
std::set<ElementsKind> elements_kinds;
bool maybe_rab_gsab = false;
if (instance_type == JS_TYPED_ARRAY_TYPE) {
for (MapRef map : inference.GetMaps()) {
ElementsKind kind = map.elements_kind();
elements_kinds.insert(kind);
if (IsRabGsabTypedArrayElementsKind(kind)) maybe_rab_gsab = true;
}
}
if (!maybe_rab_gsab) {
Reduction unused_reduction = inference.NoChange();
USE(unused_reduction);
return ReduceArrayBufferViewAccessor(
node, instance_type, AccessBuilder::ForJSArrayBufferViewByteOffset(),
builtin);
}
return inference.NoChange();
}
Reduction JSCallReducer::ReduceTypedArrayPrototypeLength(Node* node) {
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(JS_TYPED_ARRAY_TYPE)) {
return inference.NoChange();
}
std::set<ElementsKind> elements_kinds;
bool maybe_rab_gsab = false;
for (MapRef map : inference.GetMaps()) {
ElementsKind kind = map.elements_kind();
elements_kinds.insert(kind);
if (IsRabGsabTypedArrayElementsKind(kind)) maybe_rab_gsab = true;
}
if (!maybe_rab_gsab) {
Reduction unused_reduction = inference.NoChange();
USE(unused_reduction);
return ReduceArrayBufferViewAccessor(
node, JS_TYPED_ARRAY_TYPE, AccessBuilder::ForJSTypedArrayByteLength(),
Builtin::kTypedArrayPrototypeLength);
}
if (!inference.RelyOnMapsViaStability(dependencies())) {
return inference.NoChange();
}
JSCallReducerAssembler a(this, node);
TNode<JSTypedArray> typed_array =
TNode<JSTypedArray>::UncheckedCast(receiver);
TNode<Number> length = a.TypedArrayLength(
typed_array, std::move(elements_kinds), a.ContextInput());
if (!dependencies()->DependOnArrayBufferDetachingProtector()) {
length =
a.MachineSelectIf<Number>(a.ArrayBufferViewDetachedBit(typed_array))
.Then([&]() { return a.NumberConstant(0); })
.Else([&]() { return length; })
.ExpectFalse()
.Value();
}
return ReplaceWithSubgraph(&a, length);
}
Reduction JSCallReducer::ReduceNumberIsFinite(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->FalseConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Node* input = n.Argument(0);
Node* value = graph()->NewNode(simplified()->ObjectIsFiniteNumber(), input);
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceNumberIsInteger(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->FalseConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Node* input = n.Argument(0);
Node* value = graph()->NewNode(simplified()->ObjectIsInteger(), input);
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceNumberIsSafeInteger(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->FalseConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Node* input = n.Argument(0);
Node* value = graph()->NewNode(simplified()->ObjectIsSafeInteger(), input);
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceNumberIsNaN(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->FalseConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Node* input = n.Argument(0);
Node* value = graph()->NewNode(simplified()->ObjectIsNaN(), input);
ReplaceWithValue(node, value);
return Replace(value);
}
Reduction JSCallReducer::ReduceMapPrototypeGet(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() != 1) return NoChange();
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
Node* key = NodeProperties::GetValueInput(node, 2);
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAre(JS_MAP_TYPE)) {
return NoChange();
}
Node* table = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionTable()), receiver,
effect, control);
Node* entry = effect = graph()->NewNode(
simplified()->FindOrderedCollectionEntry(CollectionKind::kMap), table,
key, effect, control);
Node* check = graph()->NewNode(simplified()->NumberEqual(), entry,
jsgraph()->MinusOneConstant());
Node* branch = graph()->NewNode(common()->Branch(), check, control);
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = effect;
Node* vtrue = jsgraph()->UndefinedConstant();
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = effect;
Node* vfalse = efalse = graph()->NewNode(
simplified()->LoadElement(AccessBuilder::ForOrderedHashMapEntryValue()),
table, entry, efalse, if_false);
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
Node* value = graph()->NewNode(
common()->Phi(MachineRepresentation::kTagged, 2), vtrue, vfalse, control);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
namespace {
InstanceType InstanceTypeForCollectionKind(CollectionKind kind) {
switch (kind) {
case CollectionKind::kMap:
return JS_MAP_TYPE;
case CollectionKind::kSet:
return JS_SET_TYPE;
}
UNREACHABLE();
}
}
Reduction JSCallReducer::ReduceCollectionPrototypeHas(
Node* node, CollectionKind collection_kind) {
JSCallNode n(node);
if (n.ArgumentCount() != 1) return NoChange();
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
Node* key = NodeProperties::GetValueInput(node, 2);
InstanceType instance_type = InstanceTypeForCollectionKind(collection_kind);
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(instance_type)) {
return NoChange();
}
Node* table = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionTable()), receiver,
effect, control);
Node* index = effect = graph()->NewNode(
simplified()->FindOrderedCollectionEntry(collection_kind), table, key,
effect, control);
Node* value = graph()->NewNode(simplified()->NumberEqual(), index,
jsgraph()->MinusOneConstant());
value = graph()->NewNode(simplified()->BooleanNot(), value);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceMapPrototypeHas(Node* node) {
return ReduceCollectionPrototypeHas(node, CollectionKind::kMap);
}
Reduction JSCallReducer::ReduceSetPrototypeHas(Node* node) {
return ReduceCollectionPrototypeHas(node, CollectionKind::kSet);
}
Reduction JSCallReducer::ReduceCollectionIteration(
Node* node, CollectionKind collection_kind, IterationKind iteration_kind) {
DCHECK_EQ(IrOpcode::kJSCall, node->opcode());
Node* receiver = NodeProperties::GetValueInput(node, 1);
Node* context = NodeProperties::GetContextInput(node);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
InstanceType type = InstanceTypeForCollectionKind(collection_kind);
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAre(type)) {
return NoChange();
}
Node* js_create_iterator = effect = graph()->NewNode(
javascript()->CreateCollectionIterator(collection_kind, iteration_kind),
receiver, context, effect, control);
ReplaceWithValue(node, js_create_iterator, effect);
return Replace(js_create_iterator);
}
Reduction JSCallReducer::ReduceCollectionPrototypeSize(
Node* node, CollectionKind collection_kind) {
DCHECK_EQ(IrOpcode::kJSCall, node->opcode());
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
InstanceType type = InstanceTypeForCollectionKind(collection_kind);
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAre(type)) {
return NoChange();
}
Node* table = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionTable()), receiver,
effect, control);
Node* value = effect = graph()->NewNode(
simplified()->LoadField(
AccessBuilder::ForOrderedHashMapOrSetNumberOfElements()),
table, effect, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceCollectionIteratorPrototypeNext(
Node* node, int entry_size, Handle<HeapObject> empty_collection,
InstanceType collection_iterator_instance_type_first,
InstanceType collection_iterator_instance_type_last) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Node* receiver = n.receiver();
Node* context = n.context();
Effect effect = n.effect();
Control control = n.control();
InstanceType receiver_instance_type;
{
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps()) return NoChange();
ZoneRefSet<Map> const& receiver_maps = inference.GetMaps();
receiver_instance_type = receiver_maps[0].instance_type();
for (size_t i = 1; i < receiver_maps.size(); ++i) {
if (receiver_maps[i].instance_type() != receiver_instance_type) {
return inference.NoChange();
}
}
if (receiver_instance_type < collection_iterator_instance_type_first ||
receiver_instance_type > collection_iterator_instance_type_last) {
return inference.NoChange();
}
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
}
{
Node* done_loop;
Node* done_eloop;
Node* loop = control =
graph()->NewNode(common()->Loop(2), control, control);
Node* eloop = effect =
graph()->NewNode(common()->EffectPhi(2), effect, effect, loop);
Node* terminate = graph()->NewNode(common()->Terminate(), eloop, loop);
MergeControlToEnd(graph(), common(), terminate);
Node* table = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionIteratorTable()),
receiver, effect, control);
Node* next_table = effect =
graph()->NewNode(simplified()->LoadField(
AccessBuilder::ForOrderedHashMapOrSetNextTable()),
table, effect, control);
Node* check = graph()->NewNode(simplified()->ObjectIsSmi(), next_table);
control =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control);
done_loop = graph()->NewNode(common()->IfTrue(), control);
done_eloop = effect;
control = graph()->NewNode(common()->IfFalse(), control);
Node* index = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionIteratorIndex()),
receiver, effect, control);
Callable const callable =
Builtins::CallableFor(isolate(), Builtin::kOrderedHashTableHealIndex);
auto call_descriptor = Linkage::GetStubCallDescriptor(
graph()->zone(), callable.descriptor(),
callable.descriptor().GetStackParameterCount(),
CallDescriptor::kNoFlags, Operator::kEliminatable);
index = effect =
graph()->NewNode(common()->Call(call_descriptor),
jsgraph()->HeapConstantNoHole(callable.code()), table,
index, jsgraph()->NoContextConstant(), effect);
index = effect = graph()->NewNode(
common()->TypeGuard(TypeCache::Get()->kFixedArrayLengthType), index,
effect, control);
effect = graph()->NewNode(
simplified()->StoreField(AccessBuilder::ForJSCollectionIteratorIndex()),
receiver, index, effect, control);
effect = graph()->NewNode(
simplified()->StoreField(AccessBuilder::ForJSCollectionIteratorTable()),
receiver, next_table, effect, control);
loop->ReplaceInput(1, control);
eloop->ReplaceInput(1, effect);
control = done_loop;
effect = done_eloop;
}
Node* index = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionIteratorIndex()),
receiver, effect, control);
Node* table = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSCollectionIteratorTable()),
receiver, effect, control);
Node* iterator_result = effect = graph()->NewNode(
javascript()->CreateIterResultObject(), jsgraph()->UndefinedConstant(),
jsgraph()->TrueConstant(), context, effect);
Node* controls[2];
Node* effects[3];
{
Node* number_of_buckets = effect = graph()->NewNode(
simplified()->LoadField(
AccessBuilder::ForOrderedHashMapOrSetNumberOfBuckets()),
table, effect, control);
Node* number_of_elements = effect = graph()->NewNode(
simplified()->LoadField(
AccessBuilder::ForOrderedHashMapOrSetNumberOfElements()),
table, effect, control);
Node* number_of_deleted_elements = effect = graph()->NewNode(
simplified()->LoadField(
AccessBuilder::ForOrderedHashMapOrSetNumberOfDeletedElements()),
table, effect, control);
Node* used_capacity =
graph()->NewNode(simplified()->NumberAdd(), number_of_elements,
number_of_deleted_elements);
Node* loop = graph()->NewNode(common()->Loop(2), control, control);
Node* eloop =
graph()->NewNode(common()->EffectPhi(2), effect, effect, loop);
Node* terminate = graph()->NewNode(common()->Terminate(), eloop, loop);
MergeControlToEnd(graph(), common(), terminate);
Node* iloop = graph()->NewNode(
common()->Phi(MachineRepresentation::kTagged, 2), index, index, loop);
index = effect = graph()->NewNode(
common()->TypeGuard(TypeCache::Get()->kFixedArrayLengthType), iloop,
eloop, control);
{
Node* check0 = graph()->NewNode(simplified()->NumberLessThan(), index,
used_capacity);
Node* branch0 =
graph()->NewNode(common()->Branch(BranchHint::kTrue), check0, loop);
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
Node* efalse0 = effect;
{
efalse0 = graph()->NewNode(
simplified()->StoreField(
AccessBuilder::ForJSCollectionIteratorTable()),
receiver, jsgraph()->HeapConstantNoHole(empty_collection), efalse0,
if_false0);
controls[0] = if_false0;
effects[0] = efalse0;
}
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
Node* etrue0 = effect;
{
static_assert(OrderedHashMap::HashTableStartIndex() ==
OrderedHashSet::HashTableStartIndex());
Node* entry_start_position = graph()->NewNode(
simplified()->NumberAdd(),
graph()->NewNode(
simplified()->NumberAdd(),
graph()->NewNode(simplified()->NumberMultiply(), index,
jsgraph()->ConstantNoHole(entry_size)),
number_of_buckets),
jsgraph()->ConstantNoHole(OrderedHashMap::HashTableStartIndex()));
Node* entry_key = etrue0 = graph()->NewNode(
simplified()->LoadElement(AccessBuilder::ForFixedArrayElement()),
table, entry_start_position, etrue0, if_true0);
index = graph()->NewNode(simplified()->NumberAdd(), index,
jsgraph()->OneConstant());
Node* check1 =
graph()->NewNode(simplified()->ReferenceEqual(), entry_key,
jsgraph()->HashTableHoleConstant());
Node* branch1 = graph()->NewNode(common()->Branch(BranchHint::kFalse),
check1, if_true0);
{
control = graph()->NewNode(common()->IfFalse(), branch1);
effect = etrue0;
Node* value = effect =
graph()->NewNode(common()->TypeGuard(Type::NonInternal()),
entry_key, effect, control);
Node* done = jsgraph()->FalseConstant();
effect = graph()->NewNode(
simplified()->StoreField(
AccessBuilder::ForJSCollectionIteratorIndex()),
receiver, index, effect, control);
switch (receiver_instance_type) {
case JS_MAP_KEY_ITERATOR_TYPE:
case JS_SET_VALUE_ITERATOR_TYPE:
break;
case JS_SET_KEY_VALUE_ITERATOR_TYPE:
value = effect =
graph()->NewNode(javascript()->CreateKeyValueArray(), value,
value, context, effect);
break;
case JS_MAP_VALUE_ITERATOR_TYPE:
value = effect = graph()->NewNode(
simplified()->LoadElement(
AccessBuilder::ForFixedArrayElement()),
table,
graph()->NewNode(
simplified()->NumberAdd(), entry_start_position,
jsgraph()->ConstantNoHole(OrderedHashMap::kValueOffset)),
effect, control);
break;
case JS_MAP_KEY_VALUE_ITERATOR_TYPE:
value = effect = graph()->NewNode(
simplified()->LoadElement(
AccessBuilder::ForFixedArrayElement()),
table,
graph()->NewNode(
simplified()->NumberAdd(), entry_start_position,
jsgraph()->ConstantNoHole(OrderedHashMap::kValueOffset)),
effect, control);
value = effect =
graph()->NewNode(javascript()->CreateKeyValueArray(),
entry_key, value, context, effect);
break;
default:
UNREACHABLE();
}
effect =
graph()->NewNode(simplified()->StoreField(
AccessBuilder::ForJSIteratorResultValue()),
iterator_result, value, effect, control);
effect =
graph()->NewNode(simplified()->StoreField(
AccessBuilder::ForJSIteratorResultDone()),
iterator_result, done, effect, control);
controls[1] = control;
effects[1] = effect;
}
loop->ReplaceInput(1, graph()->NewNode(common()->IfTrue(), branch1));
eloop->ReplaceInput(1, etrue0);
iloop->ReplaceInput(1, index);
}
}
control = effects[2] = graph()->NewNode(common()->Merge(2), 2, controls);
effect = graph()->NewNode(common()->EffectPhi(2), 3, effects);
}
ReplaceWithValue(node, iterator_result, effect, control);
return Replace(iterator_result);
}
Reduction JSCallReducer::ReduceArrayBufferIsView(Node* node) {
JSCallNode n(node);
Node* value = n.ArgumentOrUndefined(0, jsgraph());
RelaxEffectsAndControls(node);
node->ReplaceInput(0, value);
node->TrimInputCount(1);
NodeProperties::ChangeOp(node, simplified()->ObjectIsArrayBufferView());
return Changed(node);
}
Reduction JSCallReducer::ReduceArrayBufferViewAccessor(
Node* node, InstanceType instance_type, FieldAccess const& access,
Builtin builtin) {
TNode<JSArrayBufferView> receiver = TNode<JSArrayBufferView>::UncheckedCast(
NodeProperties::GetValueInput(node, 1));
Effect effect{NodeProperties::GetEffectInput(node)};
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(instance_type)) {
return inference.NoChange();
}
DCHECK_IMPLIES((builtin == Builtin::kTypedArrayPrototypeLength ||
builtin == Builtin::kTypedArrayPrototypeByteLength),
base::none_of(inference.GetMaps(), [](MapRef map) {
return IsRabGsabTypedArrayElementsKind(map.elements_kind());
}));
if (!inference.RelyOnMapsViaStability(dependencies())) {
return inference.NoChange();
}
const bool depended_on_detaching_protector =
dependencies()->DependOnArrayBufferDetachingProtector();
if (!depended_on_detaching_protector && instance_type == JS_DATA_VIEW_TYPE) {
return inference.NoChange();
}
JSCallReducerAssembler a(this, node, effect);
DCHECK_EQ(access.machine_type.representation(),
MachineType::PointerRepresentation());
TNode<UintPtrT> value = a.EnterMachineGraph<UintPtrT>(
a.LoadField<UintPtrT>(access, receiver), UseInfo::Word());
if (!depended_on_detaching_protector) {
value = a.MachineSelect<UintPtrT>(a.ArrayBufferViewDetachedBit(receiver),
a.UintPtrConstant(0), value,
BranchHint::kFalse);
}
if (builtin == Builtin::kTypedArrayPrototypeLength) {
TNode<UintPtrT> byte_length = value;
TNode<Map> map = a.LoadMap(TNode<HeapObject>::UncheckedCast(receiver));
TNode<Uint32T> elements_kind = a.LoadElementsKind(map);
TNode<Uint32T> shift = a.LookupByteShiftForElementsKind(elements_kind);
value = TNode<UintPtrT>::UncheckedCast(
a.WordShr(byte_length, a.ChangeUint32ToUintPtr(shift)));
}
TNode<Number> result =
a.ExitMachineGraph<Number>(value, MachineType::PointerRepresentation(),
TypeCache::Get()->kJSTypedArrayLengthType);
return ReplaceWithSubgraph(&a, result);
}
Reduction JSCallReducer::ReduceDataViewAccess(Node* node, DataViewAccess access,
ExternalArrayType element_type) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
size_t const element_size = ExternalArrayElementSize(element_type);
Effect effect = n.effect();
Control control = n.control();
Node* receiver = n.receiver();
Node* offset = n.ArgumentOr(0, jsgraph()->ZeroConstant());
Node* value = nullptr;
if (!Is64() && (element_type == kExternalBigInt64Array ||
element_type == kExternalBigUint64Array)) {
return NoChange();
}
if (access == DataViewAccess::kSet) {
value = n.ArgumentOrUndefined(1, jsgraph());
}
const int endian_index = (access == DataViewAccess::kGet ? 1 : 2);
Node* is_little_endian =
n.ArgumentOr(endian_index, jsgraph()->FalseConstant());
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() ||
!inference.AllOfInstanceTypesAre(JS_DATA_VIEW_TYPE)) {
return NoChange();
}
HeapObjectMatcher m(receiver);
if (m.HasResolvedValue() && m.Ref(broker()).IsJSDataView()) {
JSDataViewRef dataview = m.Ref(broker()).AsJSDataView();
size_t length = dataview.byte_length();
if (length < element_size) return NoChange();
Node* byte_length = jsgraph()->ConstantNoHole(length - (element_size - 1));
offset = effect =
graph()->NewNode(simplified()->CheckBounds(
p.feedback(), CheckBoundsFlag::kAllow64BitBounds),
offset, byte_length, effect, control);
} else {
Node* byte_length = effect =
graph()->NewNode(simplified()->LoadField(
AccessBuilder::ForJSArrayBufferViewByteLength()),
receiver, effect, control);
if (element_size > 1) {
byte_length = graph()->NewNode(
simplified()->NumberMax(), jsgraph()->ZeroConstant(),
graph()->NewNode(simplified()->NumberSubtract(), byte_length,
jsgraph()->ConstantNoHole(element_size - 1)));
}
offset = effect =
graph()->NewNode(simplified()->CheckBounds(
p.feedback(), CheckBoundsFlag::kAllow64BitBounds),
offset, byte_length, effect, control);
}
is_little_endian =
graph()->NewNode(simplified()->ToBoolean(), is_little_endian);
if (access == DataViewAccess::kSet) {
if (element_type == kExternalBigInt64Array ||
element_type == kExternalBigUint64Array) {
value = effect =
graph()->NewNode(simplified()->SpeculativeToBigInt(
BigIntOperationHint::kBigInt, p.feedback()),
value, effect, control);
} else {
value = effect = graph()->NewNode(
simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
value, effect, control);
}
}
Node* buffer_or_receiver = receiver;
if (!dependencies()->DependOnArrayBufferDetachingProtector()) {
Node* buffer = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayBufferViewBuffer()),
receiver, effect, control);
Node* buffer_bit_field = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSArrayBufferBitField()),
buffer, effect, control);
Node* check = graph()->NewNode(
simplified()->NumberEqual(),
graph()->NewNode(
simplified()->NumberBitwiseAnd(), buffer_bit_field,
jsgraph()->ConstantNoHole(JSArrayBuffer::WasDetachedBit::kMask)),
jsgraph()->ZeroConstant());
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kArrayBufferWasDetached,
p.feedback()),
check, effect, control);
buffer_or_receiver = buffer;
}
Node* data_pointer = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSDataViewDataPointer()),
receiver, effect, control);
switch (access) {
case DataViewAccess::kGet:
value = effect = graph()->NewNode(
simplified()->LoadDataViewElement(element_type), buffer_or_receiver,
data_pointer, offset, is_little_endian, effect, control);
break;
case DataViewAccess::kSet:
effect = graph()->NewNode(
simplified()->StoreDataViewElement(element_type), buffer_or_receiver,
data_pointer, offset, value, is_little_endian, effect, control);
value = jsgraph()->UndefinedConstant();
break;
}
ReplaceWithValue(node, value, effect, control);
return Changed(value);
}
Reduction JSCallReducer::ReduceGlobalIsFinite(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->FalseConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Effect effect = n.effect();
Control control = n.control();
Node* input = n.Argument(0);
input = effect =
graph()->NewNode(simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
input, effect, control);
Node* value = graph()->NewNode(simplified()->NumberIsFinite(), input);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
Reduction JSCallReducer::ReduceGlobalIsNaN(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->TrueConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Effect effect = n.effect();
Control control = n.control();
Node* input = n.Argument(0);
input = effect =
graph()->NewNode(simplified()->SpeculativeToNumber(
NumberOperationHint::kNumberOrOddball, p.feedback()),
input, effect, control);
Node* value = graph()->NewNode(simplified()->NumberIsNaN(), input);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
Reduction JSCallReducer::ReduceDatePrototypeGetTime(Node* node) {
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAre(JS_DATE_TYPE)) {
return NoChange();
}
Node* value = effect =
graph()->NewNode(simplified()->LoadField(AccessBuilder::ForJSDateValue()),
receiver, effect, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceDatePrototypeGetField(Node* node,
JSDate::FieldIndex field) {
DCHECK_LT(field, JSDate::kFirstUncachedField);
if (!v8_flags.turbofan_inline_date_accessors) return NoChange();
Node* receiver = NodeProperties::GetValueInput(node, 1);
Effect effect{NodeProperties::GetEffectInput(node)};
Control control{NodeProperties::GetControlInput(node)};
MapInference inference(broker(), receiver, effect);
if (!inference.HaveMaps() || !inference.AllOfInstanceTypesAre(JS_DATE_TYPE)) {
return NoChange();
}
if (!dependencies()->DependOnNoDateTimeConfigurationChangeProtector()) {
return NoChange();
}
#define __ gasm.
JSGraphAssembler gasm(broker(), jsgraph(), jsgraph()->zone(),
BranchSemantics::kJS);
gasm.InitializeEffectControl(effect, control);
Node* value =
__ LoadField<Object>(AccessBuilder::ForJSDateField(field),
TNode<HeapObject>::UncheckedCast(receiver));
ReplaceWithValue(node, value, gasm.effect(), gasm.control());
return Replace(value);
#undef __
}
Reduction JSCallReducer::ReduceDateNow(Node* node) {
Node* effect = NodeProperties::GetEffectInput(node);
Node* control = NodeProperties::GetControlInput(node);
Node* value = effect =
graph()->NewNode(simplified()->DateNow(), effect, control);
ReplaceWithValue(node, value, effect, control);
return Replace(value);
}
Reduction JSCallReducer::ReduceNumberParseInt(Node* node) {
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
Node* value = jsgraph()->NaNConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
Effect effect = n.effect();
Control control = n.control();
Node* context = n.context();
FrameState frame_state = n.frame_state();
Node* object = n.Argument(0);
Node* radix = n.ArgumentOrUndefined(1, jsgraph());
HeapObjectMatcher object_matcher(object);
HeapObjectMatcher radix_object_matcher(radix);
NumberMatcher radix_number_matcher(radix);
if (object_matcher.HasResolvedValue() &&
object_matcher.Ref(broker()).IsString() &&
(radix_object_matcher.Is(factory()->undefined_value()) ||
radix_number_matcher.HasResolvedValue())) {
StringRef input_value = object_matcher.Ref(broker()).AsString();
int radix_value = radix_object_matcher.Is(factory()->undefined_value())
? 0
: DoubleToInt32(radix_number_matcher.ResolvedValue());
if (radix_value != 0 && (radix_value < 2 || radix_value > 36)) {
Node* value = jsgraph()->NaNConstant();
ReplaceWithValue(node, value);
return Replace(value);
}
std::optional<double> number = input_value.ToInt(broker(), radix_value);
if (number.has_value()) {
Node* result = graph()->NewNode(common()->NumberConstant(number.value()));
ReplaceWithValue(node, result);
return Replace(result);
}
}
node->ReplaceInput(0, object);
node->ReplaceInput(1, radix);
node->ReplaceInput(2, context);
node->ReplaceInput(3, frame_state);
node->ReplaceInput(4, effect);
node->ReplaceInput(5, control);
node->TrimInputCount(6);
NodeProperties::ChangeOp(node, javascript()->ParseInt());
return Changed(node);
}
Reduction JSCallReducer::ReduceRegExpPrototypeTest(Node* node) {
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (v8_flags.force_slow_path) return NoChange();
if (n.ArgumentCount() < 1) return NoChange();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
Node* regexp = n.receiver();
MapRef regexp_initial_map =
native_context().regexp_function(broker()).initial_map(broker());
MapInference inference(broker(), regexp, effect);
if (!inference.Is(regexp_initial_map)) return inference.NoChange();
ZoneRefSet<Map> const& regexp_maps = inference.GetMaps();
ZoneVector<PropertyAccessInfo> access_infos(graph()->zone());
AccessInfoFactory access_info_factory(broker(), graph()->zone());
for (MapRef map : regexp_maps) {
access_infos.push_back(broker()->GetPropertyAccessInfo(
map, broker()->exec_string(), AccessMode::kLoad));
}
PropertyAccessInfo ai_exec =
access_info_factory.FinalizePropertyAccessInfosAsOne(access_infos,
AccessMode::kLoad);
if (ai_exec.IsInvalid()) return inference.NoChange();
if (!ai_exec.IsFastDataConstant()) return inference.NoChange();
OptionalJSObjectRef holder = ai_exec.holder();
if (!holder.has_value()) return inference.NoChange();
if (ai_exec.field_representation().IsDouble()) return inference.NoChange();
OptionalObjectRef constant = holder->GetOwnFastConstantDataProperty(
broker(), ai_exec.field_representation(), ai_exec.field_index(),
dependencies());
if (!constant.has_value() ||
!constant->equals(native_context().regexp_exec_function(broker()))) {
return inference.NoChange();
}
dependencies()->DependOnStablePrototypeChains(
ai_exec.lookup_start_object_maps(), kStartAtPrototype, holder.value());
inference.RelyOnMapsPreferStability(dependencies(), jsgraph(), &effect,
control, p.feedback());
Node* context = n.context();
FrameState frame_state = n.frame_state();
Node* search = n.Argument(0);
Node* search_string = effect = graph()->NewNode(
simplified()->CheckString(p.feedback()), search, effect, control);
Node* lastIndex = effect = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForJSRegExpLastIndex()), regexp,
effect, control);
Node* lastIndexSmi = effect = graph()->NewNode(
simplified()->CheckSmi(p.feedback()), lastIndex, effect, control);
Node* is_positive = graph()->NewNode(simplified()->NumberLessThanOrEqual(),
jsgraph()->ZeroConstant(), lastIndexSmi);
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kNotASmi, p.feedback()),
is_positive, effect, control);
node->ReplaceInput(0, regexp);
node->ReplaceInput(1, search_string);
node->ReplaceInput(2, context);
node->ReplaceInput(3, frame_state);
node->ReplaceInput(4, effect);
node->ReplaceInput(5, control);
node->TrimInputCount(6);
NodeProperties::ChangeOp(node, javascript()->RegExpTest());
return Changed(node);
}
Reduction JSCallReducer::ReduceNumberConstructor(Node* node) {
JSCallNode n(node);
Node* target = n.target();
Node* receiver = n.receiver();
Node* value = n.ArgumentOr(0, jsgraph()->ZeroConstant());
Node* context = n.context();
FrameState frame_state = n.frame_state();
SharedFunctionInfoRef shared_info =
native_context().number_function(broker()).shared(broker());
Node* continuation_frame_state = CreateGenericLazyDeoptContinuationFrameState(
jsgraph(), shared_info, target, context, receiver, frame_state);
NodeProperties::ReplaceValueInputs(node, value);
NodeProperties::ChangeOp(node, javascript()->ToNumberConvertBigInt());
NodeProperties::ReplaceFrameStateInput(node, continuation_frame_state);
return Changed(node);
}
Reduction JSCallReducer::ReduceBigIntConstructor(Node* node) {
if (!jsgraph()->machine()->Is64()) return NoChange();
JSCallNode n(node);
if (n.ArgumentCount() < 1) {
return NoChange();
}
Node* target = n.target();
Node* receiver = n.receiver();
Node* value = n.Argument(0);
Node* context = n.context();
FrameState frame_state = n.frame_state();
SharedFunctionInfoRef shared_info =
native_context().bigint_function(broker()).shared(broker());
Node* continuation_frame_state = CreateGenericLazyDeoptContinuationFrameState(
jsgraph(), shared_info, target, context, receiver, frame_state);
NodeProperties::ReplaceValueInputs(node, value);
NodeProperties::ChangeOp(node, javascript()->ToBigIntConvertNumber());
NodeProperties::ReplaceFrameStateInput(node, continuation_frame_state);
return Changed(node);
}
Reduction JSCallReducer::ReduceBigIntAsN(Node* node, Builtin builtin) {
DCHECK(builtin == Builtin::kBigIntAsIntN ||
builtin == Builtin::kBigIntAsUintN);
if (!jsgraph()->machine()->Is64()) return NoChange();
JSCallNode n(node);
CallParameters const& p = n.Parameters();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return NoChange();
}
if (n.ArgumentCount() < 2) {
return NoChange();
}
Effect effect = n.effect();
Control control = n.control();
Node* bits = n.Argument(0);
Node* value = n.Argument(1);
NumberMatcher matcher(bits);
if (matcher.IsInteger() && matcher.IsInRange(0, 64)) {
const int bits_value = static_cast<int>(matcher.ResolvedValue());
value = effect = graph()->NewNode(
(builtin == Builtin::kBigIntAsIntN
? simplified()->SpeculativeBigIntAsIntN(bits_value, p.feedback())
: simplified()->SpeculativeBigIntAsUintN(bits_value,
p.feedback())),
value, effect, control);
ReplaceWithValue(node, value, effect);
return Replace(value);
}
return NoChange();
}
std::optional<Reduction> JSCallReducer::TryReduceJSCallMathMinMaxWithArrayLike(
Node* node) {
if (!v8_flags.turbo_optimize_math_minmax) return std::nullopt;
JSCallWithArrayLikeNode n(node);
CallParameters const& p = n.Parameters();
Node* target = n.target();
Effect effect = n.effect();
Control control = n.control();
if (p.speculation_mode() != SpeculationMode::kAllowSpeculation) {
return std::nullopt;
}
if (n.ArgumentCount() != 1) {
return std::nullopt;
}
if (!dependencies()->DependOnNoElementsProtector()) {
return std::nullopt;
}
Node* arguments_list = n.Argument(0);
if (arguments_list->opcode() == IrOpcode::kJSCreateLiteralArray ||
arguments_list->opcode() == IrOpcode::kJSCreateArguments) {
return std::nullopt;
}
HeapObjectMatcher m(target);
if (m.HasResolvedValue()) {
ObjectRef target_ref = m.Ref(broker());
if (target_ref.IsJSFunction()) {
JSFunctionRef function = target_ref.AsJSFunction();
if (!function.native_context(broker()).equals(native_context())) {
return std::nullopt;
}
SharedFunctionInfoRef shared = function.shared(broker());
Builtin builtin =
shared.HasBuiltinId() ? shared.builtin_id() : Builtin::kNoBuiltinId;
if (builtin == Builtin::kMathMax || builtin == Builtin::kMathMin) {
return ReduceJSCallMathMinMaxWithArrayLike(node, builtin);
} else {
return std::nullopt;
}
}
}
if (ShouldUseCallICFeedback(target) &&
p.feedback_relation() == CallFeedbackRelation::kTarget &&
p.feedback().IsValid()) {
ProcessedFeedback const& feedback =
broker()->GetFeedbackForCall(p.feedback());
if (feedback.IsInsufficient()) {
return std::nullopt;
}
OptionalHeapObjectRef feedback_target = feedback.AsCall().target();
if (feedback_target.has_value() &&
feedback_target->map(broker()).is_callable()) {
Node* target_function =
jsgraph()->ConstantNoHole(*feedback_target, broker());
ObjectRef target_ref = feedback_target.value();
if (!target_ref.IsJSFunction()) {
return std::nullopt;
}
JSFunctionRef function = target_ref.AsJSFunction();
SharedFunctionInfoRef shared = function.shared(broker());
Builtin builtin =
shared.HasBuiltinId() ? shared.builtin_id() : Builtin::kNoBuiltinId;
if (builtin == Builtin::kMathMax || builtin == Builtin::kMathMin) {
Node* check = graph()->NewNode(simplified()->ReferenceEqual(), target,
target_function);
effect = graph()->NewNode(
simplified()->CheckIf(DeoptimizeReason::kWrongCallTarget), check,
effect, control);
NodeProperties::ReplaceValueInput(node, target_function,
n.TargetIndex());
NodeProperties::ReplaceEffectInput(node, effect);
return Changed(node).FollowedBy(
ReduceJSCallMathMinMaxWithArrayLike(node, builtin));
}
}
}
return std::nullopt;
}
Reduction JSCallReducer::ReduceJSCallMathMinMaxWithArrayLike(Node* node,
Builtin builtin) {
JSCallWithArrayLikeNode n(node);
DCHECK_NE(n.Parameters().speculation_mode(),
SpeculationMode::kDisallowSpeculation);
DCHECK_EQ(n.ArgumentCount(), 1);
JSCallReducerAssembler a(this, node);
Node* subgraph = a.ReduceJSCallMathMinMaxWithArrayLike(builtin);
return ReplaceWithSubgraph(&a, subgraph);
}
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
Reduction JSCallReducer::ReduceGetContinuationPreservedEmbedderData(
Node* node) {
JSCallNode n(node);
Effect effect = n.effect();
Control control = n.control();
Node* value = effect = graph()->NewNode(
simplified()->GetContinuationPreservedEmbedderData(), effect);
ReplaceWithValue(node, value, effect, control);
return Replace(node);
}
Reduction JSCallReducer::ReduceSetContinuationPreservedEmbedderData(
Node* node) {
JSCallNode n(node);
Effect effect = n.effect();
Control control = n.control();
if (n.ArgumentCount() == 0) return NoChange();
effect =
graph()->NewNode(simplified()->SetContinuationPreservedEmbedderData(),
n.Argument(0), effect);
Node* value = jsgraph()->UndefinedConstant();
ReplaceWithValue(node, value, effect, control);
return Replace(node);
}
#endif
CompilationDependencies* JSCallReducer::dependencies() const {
return broker()->dependencies();
}
TFGraph* JSCallReducer::graph() const { return jsgraph()->graph(); }
Isolate* JSCallReducer::isolate() const { return jsgraph()->isolate(); }
Factory* JSCallReducer::factory() const { return isolate()->factory(); }
NativeContextRef JSCallReducer::native_context() const {
return broker()->target_native_context();
}
CommonOperatorBuilder* JSCallReducer::common() const {
return jsgraph()->common();
}
JSOperatorBuilder* JSCallReducer::javascript() const {
return jsgraph()->javascript();
}
SimplifiedOperatorBuilder* JSCallReducer::simplified() const {
return jsgraph()->simplified();
}
}
}
}