Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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/flow_graph_builder.h" | 9 #include "vm/flow_graph_builder.h" |
| 10 #include "vm/flow_graph_compiler.h" | 10 #include "vm/flow_graph_compiler.h" |
| 11 #include "vm/hash_map.h" | 11 #include "vm/hash_map.h" |
| 12 #include "vm/il_printer.h" | 12 #include "vm/il_printer.h" |
| 13 #include "vm/intermediate_language.h" | 13 #include "vm/intermediate_language.h" |
| 14 #include "vm/object_store.h" | 14 #include "vm/object_store.h" |
| 15 #include "vm/parser.h" | 15 #include "vm/parser.h" |
| 16 #include "vm/resolver.h" | |
| 16 #include "vm/scopes.h" | 17 #include "vm/scopes.h" |
| 17 #include "vm/symbols.h" | 18 #include "vm/symbols.h" |
| 18 | 19 |
| 19 namespace dart { | 20 namespace dart { |
| 20 | 21 |
| 21 DECLARE_FLAG(bool, eliminate_type_checks); | 22 DECLARE_FLAG(bool, eliminate_type_checks); |
| 22 DECLARE_FLAG(bool, enable_type_checks); | 23 DECLARE_FLAG(bool, enable_type_checks); |
| 23 DEFINE_FLAG(bool, trace_optimization, false, "Print optimization details."); | 24 DEFINE_FLAG(bool, trace_optimization, false, "Print optimization details."); |
| 24 DECLARE_FLAG(bool, trace_type_check_elimination); | 25 DECLARE_FLAG(bool, trace_type_check_elimination); |
| 25 DEFINE_FLAG(bool, use_cha, true, "Use class hierarchy analysis."); | 26 DEFINE_FLAG(bool, use_cha, true, "Use class hierarchy analysis."); |
| 26 DEFINE_FLAG(bool, load_cse, true, "Use redundant load elimination."); | 27 DEFINE_FLAG(bool, load_cse, true, "Use redundant load elimination."); |
| 27 DEFINE_FLAG(bool, trace_range_analysis, false, "Trace range analysis progress"); | 28 DEFINE_FLAG(bool, trace_range_analysis, false, "Trace range analysis progress"); |
| 28 DEFINE_FLAG(bool, trace_constant_propagation, false, | 29 DEFINE_FLAG(bool, trace_constant_propagation, false, |
| 29 "Print constant propagation and useless code elimination."); | 30 "Print constant propagation and useless code elimination."); |
| 30 | 31 |
| 32 | |
| 31 void FlowGraphOptimizer::ApplyICData() { | 33 void FlowGraphOptimizer::ApplyICData() { |
| 32 VisitBlocks(); | 34 VisitBlocks(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 | 37 |
| 38 // Attempts to convert an instance call (IC call) using propagated class-ids, | |
| 39 // e.g., receiver class id. | |
| 40 void FlowGraphOptimizer::ApplyClassIds() { | |
| 41 ASSERT(current_iterator_ == NULL); | |
| 42 for (intptr_t i = 0; i < block_order_.length(); ++i) { | |
| 43 BlockEntryInstr* entry = block_order_[i]; | |
| 44 ForwardInstructionIterator it(entry); | |
| 45 current_iterator_ = ⁢ | |
| 46 for (; !it.Done(); it.Advance()) { | |
| 47 if (it.Current()->IsInstanceCall()) { | |
| 48 InstanceCallInstr* call = it.Current()->AsInstanceCall(); | |
| 49 if (call->HasICData()) { | |
| 50 if (TryCreateICData(call)) { | |
| 51 VisitInstanceCall(call); | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 current_iterator_ = NULL; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 | |
| 61 // Attempt to build ICData for call using propagated class-ids. | |
| 62 bool FlowGraphOptimizer::TryCreateICData(InstanceCallInstr* call) { | |
| 63 ASSERT(call->HasICData()); | |
| 64 if (call->ic_data()->NumberOfChecks() > 0) { | |
| 65 // This occurs when an instance call has too many checks. | |
| 66 // TODO(srdjan): Replace IC call with megamorphic call. | |
| 67 return false; | |
| 68 } | |
| 69 GrowableArray<intptr_t> class_ids(call->ic_data()->num_args_tested()); | |
| 70 ASSERT(call->ic_data()->num_args_tested() <= call->ArgumentCount()); | |
| 71 for (intptr_t i = 0; i < call->ic_data()->num_args_tested(); i++) { | |
| 72 intptr_t cid = call->ArgumentAt(i)->value()->ResultCid(); | |
| 73 class_ids.Add(cid); | |
| 74 } | |
| 75 // TODO(srdjan): Test for other class_ids > 1. | |
| 76 if (class_ids.length() != 1) return false; | |
| 77 if (class_ids[0] != kDynamicCid) { | |
| 78 const intptr_t num_named_arguments = call->argument_names().IsNull() ? | |
| 79 0 : call->argument_names().Length(); | |
| 80 const Class& receiver_class = Class::Handle( | |
| 81 Isolate::Current()->class_table()->At(class_ids[0])); | |
| 82 Function& function = Function::Handle(); | |
| 83 function = Resolver::ResolveDynamicForReceiverClass( | |
| 84 receiver_class, | |
| 85 call->function_name(), | |
| 86 call->ArgumentCount(), | |
| 87 num_named_arguments); | |
| 88 if (function.IsNull()) { | |
| 89 return false; | |
| 90 } | |
| 91 // Create new ICData, do not modify the one attached to instruction | |
|
Florian Schneider
2012/10/18 17:42:59
[...] to _the_ instruction
srdjan
2012/10/18 18:41:39
Done.
| |
| 92 // since it is attached to the assembly instruction itself. | |
| 93 // TODO(srdjan): Prevent modification of ICData object that is | |
| 94 // referenced in assembly code. | |
| 95 ICData& ic_data = ICData::ZoneHandle(ICData::New( | |
| 96 flow_graph_->parsed_function().function(), | |
| 97 call->function_name(), | |
| 98 call->deopt_id(), | |
| 99 class_ids.length())); | |
| 100 ic_data.AddReceiverCheck(class_ids[0], function); | |
| 101 call->set_ic_data(&ic_data); | |
| 102 return true; | |
| 103 } | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 | |
| 36 static void ReplaceCurrentInstruction(ForwardInstructionIterator* it, | 108 static void ReplaceCurrentInstruction(ForwardInstructionIterator* it, |
| 37 Instruction* current, | 109 Instruction* current, |
| 38 Instruction* replacement) { | 110 Instruction* replacement) { |
| 39 if ((replacement != NULL) && current->IsDefinition()) { | 111 if ((replacement != NULL) && current->IsDefinition()) { |
| 40 Definition* current_defn = current->AsDefinition(); | 112 Definition* current_defn = current->AsDefinition(); |
| 41 Definition* replacement_defn = replacement->AsDefinition(); | 113 Definition* replacement_defn = replacement->AsDefinition(); |
| 42 ASSERT(replacement_defn != NULL); | 114 ASSERT(replacement_defn != NULL); |
| 43 current_defn->ReplaceUsesWith(replacement_defn); | 115 current_defn->ReplaceUsesWith(replacement_defn); |
| 44 | 116 |
| 45 if (FLAG_trace_optimization) { | 117 if (FLAG_trace_optimization) { |
| (...skipping 1010 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1056 call_with_checks = false; | 1128 call_with_checks = false; |
| 1057 } else { | 1129 } else { |
| 1058 call_with_checks = true; | 1130 call_with_checks = true; |
| 1059 } | 1131 } |
| 1060 PolymorphicInstanceCallInstr* call = | 1132 PolymorphicInstanceCallInstr* call = |
| 1061 new PolymorphicInstanceCallInstr(instr, unary_checks, | 1133 new PolymorphicInstanceCallInstr(instr, unary_checks, |
| 1062 call_with_checks); | 1134 call_with_checks); |
| 1063 instr->ReplaceWith(call, current_iterator()); | 1135 instr->ReplaceWith(call, current_iterator()); |
| 1064 } | 1136 } |
| 1065 } | 1137 } |
| 1066 // An instance call without ICData should continue calling via IC calls | 1138 // An instance call without ICData will trigger deoptimization. |
| 1067 // which should trigger reoptimization of optimized code. | |
| 1068 } | 1139 } |
| 1069 | 1140 |
| 1070 | 1141 |
| 1071 void FlowGraphOptimizer::VisitStaticCall(StaticCallInstr* call) { | 1142 void FlowGraphOptimizer::VisitStaticCall(StaticCallInstr* call) { |
| 1072 MethodRecognizer::Kind recognized_kind = | 1143 MethodRecognizer::Kind recognized_kind = |
| 1073 MethodRecognizer::RecognizeKind(call->function()); | 1144 MethodRecognizer::RecognizeKind(call->function()); |
| 1074 if (recognized_kind == MethodRecognizer::kMathSqrt) { | 1145 if (recognized_kind == MethodRecognizer::kMathSqrt) { |
| 1075 MathSqrtInstr* sqrt = new MathSqrtInstr(call->ArgumentAt(0)->value(), call); | 1146 MathSqrtInstr* sqrt = new MathSqrtInstr(call->ArgumentAt(0)->value(), call); |
| 1076 call->ReplaceWith(sqrt, current_iterator()); | 1147 call->ReplaceWith(sqrt, current_iterator()); |
| 1077 RemovePushArguments(call); | 1148 RemovePushArguments(call); |
| (...skipping 2260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3338 | 3409 |
| 3339 if (FLAG_trace_constant_propagation) { | 3410 if (FLAG_trace_constant_propagation) { |
| 3340 OS::Print("\n==== After constant propagation ====\n"); | 3411 OS::Print("\n==== After constant propagation ====\n"); |
| 3341 FlowGraphPrinter printer(*graph_); | 3412 FlowGraphPrinter printer(*graph_); |
| 3342 printer.PrintBlocks(); | 3413 printer.PrintBlocks(); |
| 3343 } | 3414 } |
| 3344 } | 3415 } |
| 3345 | 3416 |
| 3346 | 3417 |
| 3347 } // namespace dart | 3418 } // namespace dart |
| OLD | NEW |