OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "vm/flow_graph_optimizer.h" | |
6 | |
7 #include "vm/flow_graph_builder.h" | |
8 #include "vm/il_printer.h" | |
9 #include "vm/object_store.h" | |
10 | |
11 namespace dart { | |
12 | |
13 DECLARE_FLAG(bool, print_flow_graph); | |
14 DECLARE_FLAG(bool, trace_optimization); | |
15 | |
16 void FlowGraphOptimizer::ApplyICData() { | |
17 VisitBlocks(); | |
18 if (FLAG_print_flow_graph) { | |
19 OS::Print("After Optimizations:\n"); | |
20 intptr_t length = block_order_.length(); | |
21 GrowableArray<BlockEntryInstr*> reverse_postorder(length); | |
22 for (intptr_t i = length - 1; i >= 0; --i) { | |
23 reverse_postorder.Add(block_order_[i]); | |
24 } | |
25 FlowGraphPrinter printer(Function::Handle(), reverse_postorder); | |
26 printer.PrintBlocks(); | |
27 } | |
28 } | |
29 | |
30 | |
31 void FlowGraphOptimizer::VisitBlocks() { | |
32 for (intptr_t i = 0; i < block_order_.length(); ++i) { | |
33 Instruction* instr = block_order_[i]->Accept(this); | |
34 // Compile all successors until an exit, branch, or a block entry. | |
35 while ((instr != NULL) && !instr->IsBlockEntry()) { | |
36 instr = instr->Accept(this); | |
37 } | |
38 } | |
39 } | |
40 | |
41 | |
42 static Token::Kind GetBinaryOp(const String& name) { | |
43 if (name.Length() == 1) { | |
44 switch (name.CharAt(0)) { | |
45 case '+' : return Token::kADD; | |
46 default: return Token::kILLEGAL; | |
47 } | |
48 } | |
49 return Token::kILLEGAL; | |
50 } | |
51 | |
52 | |
53 static bool ICDataHasTwoReceiverClasses(const ICData& ic_data, | |
54 const Class& cls1, | |
55 const Class& cls2) { | |
56 ASSERT(!cls1.IsNull() && !cls2.IsNull()); | |
57 if (ic_data.num_args_tested() != 2) { | |
58 return false; | |
59 } | |
60 for (intptr_t i = 0; i < ic_data.NumberOfChecks(); i++) { | |
61 GrowableArray<const Class*> classes; | |
62 Function& target = Function::Handle(); | |
63 ic_data.GetCheckAt(i, &classes, &target); | |
64 if (!classes.is_empty()) { | |
65 if (classes[0]->raw() == cls1.raw()) { | |
66 if (classes[0]->raw() == cls2.raw()) { | |
Florian Schneider
2012/05/24 00:20:39
I think this should be classes[1] here:
if (class
srdjan
2012/05/24 01:36:26
Aargh, thanks.
| |
67 return true; | |
68 } | |
69 } | |
70 } | |
71 } | |
72 return false; | |
73 } | |
74 | |
75 | |
76 static bool HasTwoSmi(const ICData& ic_data) { | |
77 const Class& smi_class = | |
78 Class::Handle(Isolate::Current()->object_store()->smi_class()); | |
79 return ICDataHasTwoReceiverClasses(ic_data, smi_class, smi_class); | |
80 } | |
81 | |
82 | |
83 void FlowGraphOptimizer::VisitInstanceCall(InstanceCallComp* comp) { | |
84 if ((comp->ic_data() != NULL) && (!comp->ic_data()->IsNull())) { | |
85 Token::Kind op_kind = GetBinaryOp(comp->function_name()); | |
86 if (op_kind == Token::kILLEGAL) { | |
87 // Not a recognized binary operation. | |
88 return; | |
89 } | |
90 if (comp->ic_data()->NumberOfChecks() != 1) { | |
91 // TODO(srdjan): Not yet supported. | |
92 return; | |
93 } | |
94 if (!HasTwoSmi(*comp->ic_data())) { | |
95 // TODO(srdjan): Not yet supported. | |
96 return; | |
97 } | |
98 if (comp->use() == NULL) { | |
99 // TODO(srdjan): Cannot handle yet. | |
100 return; | |
101 } | |
102 ASSERT(comp->InputCount() == 2); | |
103 BinaryOpComp* bin_op = | |
104 new BinaryOpComp(op_kind, comp, comp->InputAt(0), comp->InputAt(1)); | |
105 ASSERT(bin_op->ic_data() == NULL); | |
106 bin_op->set_ic_data(comp->ic_data()); | |
107 bin_op->set_use(comp->use()); | |
108 comp->use()->definition()->replace_computation(bin_op); | |
Florian Schneider
2012/05/24 00:20:39
As discussed offline, with a pointer to the Bind/D
srdjan
2012/05/24 01:36:26
Yes. This was originally done as a step toward eli
| |
109 } | |
110 } | |
111 | |
112 | |
113 void FlowGraphOptimizer::VisitDo(DoInstr* instr) { | |
114 instr->computation()->Accept(this); | |
115 } | |
116 | |
117 | |
118 void FlowGraphOptimizer::VisitBind(BindInstr* instr) { | |
119 instr->computation()->Accept(this); | |
120 } | |
121 | |
122 | |
123 } // namespace dart | |
OLD | NEW |