Commit 27fb7b83 authored by bmeurer's avatar bmeurer Committed by Commit bot

[turbofan] Constant-fold concatenation of strings.

For a + b, where both a and b are known strings at compile
time, and the combined length of a and b doesn't overflow
the maximum allowed string length, we can constant-fold the
string concatenation during compilation.

R=yangguo@chromium.org

Review-Url: https://codereview.chromium.org/2713093003
Cr-Commit-Position: refs/heads/master@{#43401}
parent 59eb62d4
......@@ -77,6 +77,8 @@ JSNativeContextSpecialization::JSNativeContextSpecialization(
Reduction JSNativeContextSpecialization::Reduce(Node* node) {
switch (node->opcode()) {
case IrOpcode::kJSAdd:
return ReduceJSAdd(node);
case IrOpcode::kJSGetSuperConstructor:
return ReduceJSGetSuperConstructor(node);
case IrOpcode::kJSInstanceOf:
......@@ -107,6 +109,30 @@ Reduction JSNativeContextSpecialization::Reduce(Node* node) {
return NoChange();
}
Reduction JSNativeContextSpecialization::ReduceJSAdd(Node* node) {
// TODO(turbofan): This has to run together with the inlining and
// native context specialization to be able to leverage the string
// constant-folding for optimizing property access, but we should
// nevertheless find a better home for this at some point.
DCHECK_EQ(IrOpcode::kJSAdd, node->opcode());
// Constant-fold string concatenation.
HeapObjectBinopMatcher m(node);
if (m.left().HasValue() && m.left().Value()->IsString() &&
m.right().HasValue() && m.right().Value()->IsString()) {
Handle<String> left = Handle<String>::cast(m.left().Value());
Handle<String> right = Handle<String>::cast(m.right().Value());
if (left->length() + right->length() <= String::kMaxLength) {
Handle<String> result =
factory()->NewConsString(left, right).ToHandleChecked();
Node* value = jsgraph()->HeapConstant(result);
ReplaceWithValue(node, value);
return Replace(value);
}
}
return NoChange();
}
Reduction JSNativeContextSpecialization::ReduceJSGetSuperConstructor(
Node* node) {
DCHECK_EQ(IrOpcode::kJSGetSuperConstructor, node->opcode());
......
......@@ -53,6 +53,7 @@ class JSNativeContextSpecialization final : public AdvancedReducer {
Reduction Reduce(Node* node) final;
private:
Reduction ReduceJSAdd(Node* node);
Reduction ReduceJSGetSuperConstructor(Node* node);
Reduction ReduceJSInstanceOf(Node* node);
Reduction ReduceJSOrdinaryHasInstance(Node* node);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment