| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/flow_graph_optimizer.h" | 5 #include "vm/flow_graph_optimizer.h" |
| 6 | 6 |
| 7 #include "vm/bit_vector.h" | 7 #include "vm/bit_vector.h" |
| 8 #include "vm/cha.h" | 8 #include "vm/cha.h" |
| 9 #include "vm/dart_entry.h" | 9 #include "vm/dart_entry.h" |
| 10 #include "vm/flow_graph_builder.h" | 10 #include "vm/flow_graph_builder.h" |
| (...skipping 6210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6221 } | 6221 } |
| 6222 | 6222 |
| 6223 | 6223 |
| 6224 void ConstantPropagator::VisitStringFromCharCode( | 6224 void ConstantPropagator::VisitStringFromCharCode( |
| 6225 StringFromCharCodeInstr* instr) { | 6225 StringFromCharCodeInstr* instr) { |
| 6226 SetValue(instr, non_constant_); | 6226 SetValue(instr, non_constant_); |
| 6227 } | 6227 } |
| 6228 | 6228 |
| 6229 | 6229 |
| 6230 void ConstantPropagator::VisitStringInterpolate(StringInterpolateInstr* instr) { | 6230 void ConstantPropagator::VisitStringInterpolate(StringInterpolateInstr* instr) { |
| 6231 // TODO(srdjan): Remove the code below and enable constant folding once | |
| 6232 // issue resolved. | |
| 6233 SetValue(instr, non_constant_); | 6231 SetValue(instr, non_constant_); |
| 6234 return; | 6232 return; |
| 6235 | |
| 6236 if (IsNonConstant(instr->constant_value())) { | |
| 6237 // Do not bother with costly analysis if we already know that the | |
| 6238 // instruction is not a constant. | |
| 6239 SetValue(instr, non_constant_); | |
| 6240 return; | |
| 6241 } | |
| 6242 // If all inputs are constant strings, numbers, booleans or null, then | |
| 6243 // constant fold. | |
| 6244 // TODO(srdjan): Also constant fold an interval of constant arguments. | |
| 6245 // v2 <- CreateArray(v0) | |
| 6246 // StoreIndexed(v2, v3, v4) -- v3:constant index, v4: value. | |
| 6247 // .. | |
| 6248 // v8 <- StringInterpolate(v2) | |
| 6249 CreateArrayInstr* create_array = | |
| 6250 instr->value()->definition()->AsCreateArray(); | |
| 6251 ASSERT(create_array != NULL); | |
| 6252 | |
| 6253 // Check if the string interpolation has only constant inputs. | |
| 6254 for (Value::Iterator it(create_array->input_use_list()); | |
| 6255 !it.Done(); | |
| 6256 it.Advance()) { | |
| 6257 Instruction* curr = it.Current()->instruction(); | |
| 6258 if (curr != instr) { | |
| 6259 StoreIndexedInstr* store = curr->AsStoreIndexed(); | |
| 6260 ASSERT(store != NULL); | |
| 6261 const Object& value = store->value()->definition()->constant_value(); | |
| 6262 if (IsNonConstant(value)) { | |
| 6263 SetValue(instr, non_constant_); | |
| 6264 return; | |
| 6265 } else if (IsUnknown(value)) { | |
| 6266 ASSERT(IsUnknown(instr->constant_value())); | |
| 6267 return; | |
| 6268 } | |
| 6269 } | |
| 6270 } | |
| 6271 // Interpolate string at compile time. | |
| 6272 const Array& value_arr = | |
| 6273 Array::Handle(Array::New(create_array->num_elements())); | |
| 6274 // Build array of literal values to interpolate. | |
| 6275 for (Value::Iterator it(create_array->input_use_list()); | |
| 6276 !it.Done(); | |
| 6277 it.Advance()) { | |
| 6278 Instruction* curr = it.Current()->instruction(); | |
| 6279 // Skip StringInterpolateInstr. | |
| 6280 if (curr != instr) { | |
| 6281 StoreIndexedInstr* store = curr->AsStoreIndexed(); | |
| 6282 ASSERT(store != NULL); | |
| 6283 Value* index_value = store->index(); | |
| 6284 ASSERT(index_value->BindsToConstant() && index_value->IsSmiValue()); | |
| 6285 const intptr_t ix = Smi::Cast(index_value->BoundConstant()).Value(); | |
| 6286 ASSERT(IsConstant(store->value()->definition()->constant_value())); | |
| 6287 value_arr.SetAt(ix, store->value()->definition()->constant_value()); | |
| 6288 } | |
| 6289 } | |
| 6290 // Build argument array to pass to the interpolation function. | |
| 6291 const Array& interpolate_arg = Array::Handle(Array::New(1)); | |
| 6292 interpolate_arg.SetAt(0, value_arr); | |
| 6293 // Call interpolation function. | |
| 6294 String& concatenated = String::ZoneHandle(); | |
| 6295 concatenated ^= | |
| 6296 DartEntry::InvokeFunction(instr->CallFunction(), interpolate_arg); | |
| 6297 if (concatenated.IsUnhandledException()) { | |
| 6298 SetValue(instr, non_constant_); | |
| 6299 return; | |
| 6300 } | |
| 6301 | |
| 6302 concatenated = Symbols::New(concatenated); | |
| 6303 SetValue(instr, concatenated); | |
| 6304 } | 6233 } |
| 6305 | 6234 |
| 6306 | 6235 |
| 6307 void ConstantPropagator::VisitLoadIndexed(LoadIndexedInstr* instr) { | 6236 void ConstantPropagator::VisitLoadIndexed(LoadIndexedInstr* instr) { |
| 6308 SetValue(instr, non_constant_); | 6237 SetValue(instr, non_constant_); |
| 6309 } | 6238 } |
| 6310 | 6239 |
| 6311 | 6240 |
| 6312 void ConstantPropagator::VisitStoreIndexed(StoreIndexedInstr* instr) { | 6241 void ConstantPropagator::VisitStoreIndexed(StoreIndexedInstr* instr) { |
| 6313 SetValue(instr, instr->value()->definition()->constant_value()); | 6242 SetValue(instr, instr->value()->definition()->constant_value()); |
| (...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7069 !defn->IsStoreStaticField() && | 6998 !defn->IsStoreStaticField() && |
| 7070 !defn->IsStoreVMField()) { | 6999 !defn->IsStoreVMField()) { |
| 7071 if (FLAG_trace_constant_propagation) { | 7000 if (FLAG_trace_constant_propagation) { |
| 7072 OS::Print("Constant v%" Pd " = %s\n", | 7001 OS::Print("Constant v%" Pd " = %s\n", |
| 7073 defn->ssa_temp_index(), | 7002 defn->ssa_temp_index(), |
| 7074 defn->constant_value().ToCString()); | 7003 defn->constant_value().ToCString()); |
| 7075 } | 7004 } |
| 7076 ConstantInstr* constant = graph_->GetConstant(defn->constant_value()); | 7005 ConstantInstr* constant = graph_->GetConstant(defn->constant_value()); |
| 7077 defn->ReplaceUsesWith(constant); | 7006 defn->ReplaceUsesWith(constant); |
| 7078 i.RemoveCurrentFromGraph(); | 7007 i.RemoveCurrentFromGraph(); |
| 7079 if (defn->IsStringInterpolate()) { | |
| 7080 CreateArrayInstr* create_array = defn->AsStringInterpolate()-> | |
| 7081 value()->definition()->AsCreateArray(); | |
| 7082 for (Value* use = create_array->input_use_list(); | |
| 7083 use != NULL; | |
| 7084 use = create_array->input_use_list()) { | |
| 7085 use->instruction()->RemoveFromGraph(); | |
| 7086 } | |
| 7087 create_array->RemoveFromGraph(); | |
| 7088 } | |
| 7089 } | 7008 } |
| 7090 } | 7009 } |
| 7091 | 7010 |
| 7092 // Replace branches where one target is unreachable with jumps. | 7011 // Replace branches where one target is unreachable with jumps. |
| 7093 BranchInstr* branch = block->last_instruction()->AsBranch(); | 7012 BranchInstr* branch = block->last_instruction()->AsBranch(); |
| 7094 if (branch != NULL) { | 7013 if (branch != NULL) { |
| 7095 TargetEntryInstr* if_true = branch->true_successor(); | 7014 TargetEntryInstr* if_true = branch->true_successor(); |
| 7096 TargetEntryInstr* if_false = branch->false_successor(); | 7015 TargetEntryInstr* if_false = branch->false_successor(); |
| 7097 JoinEntryInstr* join = NULL; | 7016 JoinEntryInstr* join = NULL; |
| 7098 Instruction* next = NULL; | 7017 Instruction* next = NULL; |
| (...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7764 } | 7683 } |
| 7765 | 7684 |
| 7766 // Insert materializations at environment uses. | 7685 // Insert materializations at environment uses. |
| 7767 for (intptr_t i = 0; i < exits.length(); i++) { | 7686 for (intptr_t i = 0; i < exits.length(); i++) { |
| 7768 CreateMaterializationAt(exits[i], alloc, alloc->cls(), *fields); | 7687 CreateMaterializationAt(exits[i], alloc, alloc->cls(), *fields); |
| 7769 } | 7688 } |
| 7770 } | 7689 } |
| 7771 | 7690 |
| 7772 | 7691 |
| 7773 } // namespace dart | 7692 } // namespace dart |
| OLD | NEW |