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/block_scheduler.h" | 5 #include "vm/block_scheduler.h" |
6 | 6 |
7 #include "vm/allocation.h" | 7 #include "vm/allocation.h" |
8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
9 #include "vm/flow_graph.h" | 9 #include "vm/flow_graph.h" |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 DEFINE_FLAG(bool, emit_edge_counters, true, "Emit edge counters at targets."); | 13 DEFINE_FLAG(bool, emit_edge_counters, true, "Emit edge counters at targets."); |
14 | 14 |
15 // Compute the edge count at the deopt id of a TargetEntry or Goto. | 15 static intptr_t GetEdgeCount(const Array& edge_counters, intptr_t edge_id) { |
16 static intptr_t ComputeEdgeCount( | |
17 const Code& unoptimized_code, | |
18 const ZoneGrowableArray<uword>& deopt_id_pc_pairs, | |
19 intptr_t deopt_id) { | |
20 ASSERT(deopt_id != Isolate::kNoDeoptId); | |
21 | |
22 if (!FLAG_emit_edge_counters) { | 16 if (!FLAG_emit_edge_counters) { |
23 // Assume everything was visited once. | 17 // Assume everything was visited once. |
24 return 1; | 18 return 1; |
25 } | 19 } |
26 | 20 return Smi::Value(Smi::RawCast(edge_counters.At(edge_id))); |
27 for (intptr_t i = 0; i < deopt_id_pc_pairs.length(); i += 2) { | |
28 intptr_t deopt_id_entry = static_cast<intptr_t>(deopt_id_pc_pairs[i]); | |
29 uword pc = deopt_id_pc_pairs[i + 1]; | |
30 if (deopt_id_entry == deopt_id) { | |
31 Array& array = Array::Handle(); | |
32 array ^= CodePatcher::GetEdgeCounterAt(pc, unoptimized_code); | |
33 ASSERT(!array.IsNull()); | |
34 return Smi::Value(Smi::RawCast(array.At(0))); | |
35 } | |
36 } | |
37 | |
38 UNREACHABLE(); | |
39 return 1; | |
40 } | 21 } |
41 | 22 |
42 | 23 |
43 // There is an edge from instruction->successor. Set its weight (edge count | 24 // There is an edge from instruction->successor. Set its weight (edge count |
44 // per function entry). | 25 // per function entry). |
45 static void SetEdgeWeight(Instruction* instruction, | 26 static void SetEdgeWeight(BlockEntryInstr* block, |
46 BlockEntryInstr* successor, | 27 BlockEntryInstr* successor, |
47 const Code& unoptimized_code, | 28 const Array& edge_counters, |
48 const ZoneGrowableArray<uword>& deopt_id_pc_pairs, | |
49 intptr_t entry_count) { | 29 intptr_t entry_count) { |
50 TargetEntryInstr* target = successor->AsTargetEntry(); | 30 TargetEntryInstr* target = successor->AsTargetEntry(); |
51 if (target != NULL) { | 31 if (target != NULL) { |
52 // If this block ends in a goto, the edge count of this edge is the same | 32 // If this block ends in a goto, the edge count of this edge is the same |
53 // as the count on the single outgoing edge. This is true as long as the | 33 // as the count on the single outgoing edge. This is true as long as the |
54 // block does not throw an exception. | 34 // block does not throw an exception. |
55 GotoInstr* jump = target->last_instruction()->AsGoto(); | 35 intptr_t count = GetEdgeCount(edge_counters, target->preorder_number()); |
56 const intptr_t deopt_id = | |
57 (jump != NULL) ? jump->deopt_id() : target->deopt_id(); | |
58 intptr_t count = ComputeEdgeCount(unoptimized_code, | |
59 deopt_id_pc_pairs, | |
60 deopt_id); | |
61 if ((count >= 0) && (entry_count != 0)) { | 36 if ((count >= 0) && (entry_count != 0)) { |
62 double weight = | 37 double weight = |
63 static_cast<double>(count) / static_cast<double>(entry_count); | 38 static_cast<double>(count) / static_cast<double>(entry_count); |
64 target->set_edge_weight(weight); | 39 target->set_edge_weight(weight); |
65 } | 40 } |
66 } else { | 41 } else { |
67 GotoInstr* jump = instruction->AsGoto(); | 42 GotoInstr* jump = block->last_instruction()->AsGoto(); |
68 if (jump != NULL) { | 43 if (jump != NULL) { |
69 intptr_t count = ComputeEdgeCount(unoptimized_code, | 44 intptr_t count = |
70 deopt_id_pc_pairs, | 45 GetEdgeCount(edge_counters, block->preorder_number()); |
71 jump->deopt_id()); | |
72 if ((count >= 0) && (entry_count != 0)) { | 46 if ((count >= 0) && (entry_count != 0)) { |
73 double weight = | 47 double weight = |
74 static_cast<double>(count) / static_cast<double>(entry_count); | 48 static_cast<double>(count) / static_cast<double>(entry_count); |
75 jump->set_edge_weight(weight); | 49 jump->set_edge_weight(weight); |
76 } | 50 } |
77 } | 51 } |
78 } | 52 } |
79 } | 53 } |
80 | 54 |
81 | 55 |
82 void BlockScheduler::AssignEdgeWeights() const { | 56 void BlockScheduler::AssignEdgeWeights() const { |
83 if (!FLAG_emit_edge_counters) { | 57 if (!FLAG_emit_edge_counters) { |
84 return; | 58 return; |
85 } | 59 } |
86 const Code& unoptimized_code = flow_graph()->parsed_function().code(); | |
87 ASSERT(!unoptimized_code.IsNull()); | |
88 | 60 |
89 ZoneGrowableArray<uword>* deopt_id_pc_pairs = new ZoneGrowableArray<uword>(); | 61 const Array& ic_data_array = Array::Handle(flow_graph()->zone(), |
90 const PcDescriptors& descriptors = | 62 flow_graph()->parsed_function().function().ic_data_array()); |
91 PcDescriptors::Handle(unoptimized_code.pc_descriptors()); | 63 Array& edge_counters = Array::Handle(); |
92 PcDescriptors::Iterator iter(descriptors, RawPcDescriptors::kDeopt); | 64 edge_counters ^= ic_data_array.At(0); |
93 uword entry = | |
94 Instructions::Handle(unoptimized_code.instructions()).EntryPoint(); | |
95 while (iter.MoveNext()) { | |
96 intptr_t deopt_id = iter.DeoptId(); | |
97 ASSERT(deopt_id != Isolate::kNoDeoptId); | |
98 uint32_t pc_offset = iter.PcOffset(); | |
99 deopt_id_pc_pairs->Add(static_cast<uword>(deopt_id)); | |
100 deopt_id_pc_pairs->Add(entry + pc_offset); | |
101 } | |
102 | 65 |
103 intptr_t entry_count = | 66 intptr_t entry_count = GetEdgeCount( |
104 ComputeEdgeCount(unoptimized_code, | 67 edge_counters, |
105 *deopt_id_pc_pairs, | 68 flow_graph()->graph_entry()->normal_entry()->preorder_number()); |
106 flow_graph()->graph_entry()->normal_entry()->deopt_id()); | |
107 flow_graph()->graph_entry()->set_entry_count(entry_count); | 69 flow_graph()->graph_entry()->set_entry_count(entry_count); |
108 | 70 |
109 for (BlockIterator it = flow_graph()->reverse_postorder_iterator(); | 71 for (BlockIterator it = flow_graph()->reverse_postorder_iterator(); |
110 !it.Done(); | 72 !it.Done(); |
111 it.Advance()) { | 73 it.Advance()) { |
112 BlockEntryInstr* block = it.Current(); | 74 BlockEntryInstr* block = it.Current(); |
113 Instruction* last = block->last_instruction(); | 75 Instruction* last = block->last_instruction(); |
114 for (intptr_t i = 0; i < last->SuccessorCount(); ++i) { | 76 for (intptr_t i = 0; i < last->SuccessorCount(); ++i) { |
115 BlockEntryInstr* succ = last->SuccessorAt(i); | 77 BlockEntryInstr* succ = last->SuccessorAt(i); |
116 SetEdgeWeight(last, succ, | 78 SetEdgeWeight(block, succ, edge_counters, entry_count); |
117 unoptimized_code, *deopt_id_pc_pairs, entry_count); | |
118 } | 79 } |
119 } | 80 } |
120 } | 81 } |
121 | 82 |
122 | 83 |
123 // A weighted control-flow graph edge. | 84 // A weighted control-flow graph edge. |
124 struct Edge { | 85 struct Edge { |
125 Edge(BlockEntryInstr* source, BlockEntryInstr* target, double weight) | 86 Edge(BlockEntryInstr* source, BlockEntryInstr* target, double weight) |
126 : source(source), target(target), weight(weight) { } | 87 : source(source), target(target), weight(weight) { } |
127 | 88 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 for (intptr_t i = block_count - 1; i >= 0; --i) { | 204 for (intptr_t i = block_count - 1; i >= 0; --i) { |
244 if (chains[i]->first->block == flow_graph()->postorder()[i]) { | 205 if (chains[i]->first->block == flow_graph()->postorder()[i]) { |
245 for (Link* link = chains[i]->first; link != NULL; link = link->next) { | 206 for (Link* link = chains[i]->first; link != NULL; link = link->next) { |
246 flow_graph()->CodegenBlockOrder(true)->Add(link->block); | 207 flow_graph()->CodegenBlockOrder(true)->Add(link->block); |
247 } | 208 } |
248 } | 209 } |
249 } | 210 } |
250 } | 211 } |
251 | 212 |
252 } // namespace dart | 213 } // namespace dart |
OLD | NEW |