Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Unified Diff: runtime/vm/flow_graph_optimizer.cc

Issue 24837002: Optimize string interpolation by running it at compile time if all inputs are constant. This is don… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/flow_graph_optimizer.h ('k') | runtime/vm/intermediate_language.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/flow_graph_optimizer.cc
===================================================================
--- runtime/vm/flow_graph_optimizer.cc (revision 28173)
+++ runtime/vm/flow_graph_optimizer.cc (working copy)
@@ -6035,7 +6035,78 @@
void ConstantPropagator::VisitStaticCall(StaticCallInstr* instr) {
- SetValue(instr, non_constant_);
+ if (IsNonConstant(instr->constant_value())) {
+ // Do not bother with costly analysis if we already know that the
+ // instruction is not a constant.
+ SetValue(instr, non_constant_);
+ return;
+ }
+ MethodRecognizer::Kind recognized_kind =
+ MethodRecognizer::RecognizeKind(instr->function());
+ if (recognized_kind == MethodRecognizer::kStringBaseInterpolate) {
+ // static String _interpolate(List values)
+ //
+ // Code for calling interpolate is generated by the compiler:
+ // v2 <- CreateArray(v0)
+ // StoreIndexed(v2, v3, v4) -- v3:constant index, v4: value.
+ // ..
+ // PushArgument(v2)
+ // v8 <- StaticCall(_interpolate, v2)
+ // Detect that all values are constant, interpolate at compile
+ // time.
+ ASSERT(instr->ArgumentCount() == 1);
+ CreateArrayInstr* create_array = instr->ArgumentAt(0)->AsCreateArray();
+ ASSERT(create_array != NULL);
+ // Check if the string interpolation has only constant inputs.
+ for (Value::Iterator it(create_array->input_use_list());
+ !it.Done();
+ it.Advance()) {
+ Instruction* curr = it.Current()->instruction();
+ StoreIndexedInstr* store = curr->AsStoreIndexed();
+ // 'store' is NULL fir PushArgument instruction: skip it.
+ if ((store != NULL) &&
+ (IsNonConstant(store->value()->definition()->constant_value()))) {
+ SetValue(instr, non_constant_);
+ return;
+ }
+ }
+ // Interpolate string at compile time.
+ const Array& value_arr =
+ Array::Handle(Array::New(create_array->num_elements()));
+ // Build the array of literal values to interpolate, abort if a value is
+ // not literal.
+ for (Value::Iterator it(create_array->input_use_list());
+ !it.Done();
+ it.Advance()) {
+ Instruction* curr = it.Current()->instruction();
+ StoreIndexedInstr* store = curr->AsStoreIndexed();
+ if (store == NULL) {
+ ASSERT(curr == instr->PushArgumentAt(0));
+ } else {
+ Value* index_value = store->index();
+ ASSERT(index_value->BindsToConstant() && index_value->IsSmiValue());
+ const intptr_t ix = Smi::Cast(index_value->BoundConstant()).Value();
+ ASSERT(IsConstant(store->value()->definition()->constant_value()));
+ value_arr.SetAt(ix, store->value()->definition()->constant_value());
+ }
+ }
+ // Build argument array to pass to the interpolation function.
+ const Array& interpolate_arg = Array::Handle(Array::New(1));
+ interpolate_arg.SetAt(0, value_arr);
+ // Call interpolation function.
+ String& concatenated = String::ZoneHandle();
+ concatenated ^=
+ DartEntry::InvokeFunction(instr->function(), interpolate_arg);
+ if (concatenated.IsUnhandledException()) {
+ SetValue(instr, non_constant_);
+ return;
+ }
+
+ concatenated = Symbols::New(concatenated);
+ SetValue(instr, concatenated);
+ } else {
+ SetValue(instr, non_constant_);
+ }
}
@@ -6868,6 +6939,27 @@
}
+// Code for calling interpolate is generated by the compiler:
+// v2 <- CreateArray(v0)
+// StoreIndexed(v2, v3, v4) -- v3:constant index, v4: value.
+// ..
+// PushArgument(v2)
+// v8 <- StaticCall(_interpolate, v2)
+// Remove the inputs.
+void ConstantPropagator::RemoveInterpolationInputs(
+ const StaticCallInstr& call) {
+ ASSERT(call.ArgumentCount() == 1);
+ CreateArrayInstr* create_array = call.ArgumentAt(0)->AsCreateArray();
+ ASSERT(create_array != NULL);
+ for (Value* use = create_array->input_use_list();
+ use != NULL;
+ use = create_array->input_use_list()) {
+ use->instruction()->RemoveFromGraph();
+ }
+ create_array->RemoveFromGraph();
+}
+
+
void ConstantPropagator::Transform() {
if (FLAG_trace_constant_propagation) {
OS::Print("\n==== Before constant propagation ====\n");
@@ -6975,6 +7067,13 @@
ConstantInstr* constant = graph_->GetConstant(defn->constant_value());
defn->ReplaceUsesWith(constant);
i.RemoveCurrentFromGraph();
+ if (defn->IsStaticCall()) {
+ MethodRecognizer::Kind recognized_kind =
+ MethodRecognizer::RecognizeKind(defn->AsStaticCall()->function());
+ if (recognized_kind == MethodRecognizer::kStringBaseInterpolate) {
+ RemoveInterpolationInputs(*defn->AsStaticCall());
+ }
+ }
}
}
« no previous file with comments | « runtime/vm/flow_graph_optimizer.h ('k') | runtime/vm/intermediate_language.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698