| 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 #ifndef VM_FLOW_GRAPH_OPTIMIZER_H_ | 5 #ifndef VM_FLOW_GRAPH_OPTIMIZER_H_ |
| 6 #define VM_FLOW_GRAPH_OPTIMIZER_H_ | 6 #define VM_FLOW_GRAPH_OPTIMIZER_H_ |
| 7 | 7 |
| 8 #include "vm/intermediate_language.h" | 8 #include "vm/intermediate_language.h" |
| 9 #include "vm/flow_graph.h" | 9 #include "vm/flow_graph.h" |
| 10 | 10 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 public: | 149 public: |
| 150 static void Optimize(FlowGraph* graph); | 150 static void Optimize(FlowGraph* graph); |
| 151 | 151 |
| 152 private: | 152 private: |
| 153 static void OptimizeRecursive( | 153 static void OptimizeRecursive( |
| 154 BlockEntryInstr* entry, | 154 BlockEntryInstr* entry, |
| 155 DirectChainedHashMap<Definition*>* map); | 155 DirectChainedHashMap<Definition*>* map); |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 | 158 |
| 159 // Sparse conditional constant propagation and unreachable code elimination. | |
| 160 // Assumes that use lists are computed and preserves them. | |
| 161 class ConstantPropagator : public FlowGraphVisitor { | |
| 162 public: | |
| 163 explicit ConstantPropagator(FlowGraph* graph); | |
| 164 | |
| 165 static void Optimize(FlowGraph* graph) { | |
| 166 ConstantPropagator cp(graph); | |
| 167 cp.Analyze(); | |
| 168 cp.Transform(); | |
| 169 } | |
| 170 | |
| 171 // Used to initialize the abstract value of definitions. | |
| 172 static RawObject* Unknown() { return Object::transition_sentinel(); } | |
| 173 | |
| 174 private: | |
| 175 void Analyze(); | |
| 176 void Transform(); | |
| 177 | |
| 178 void SetReachable(BlockEntryInstr* block); | |
| 179 void SetValue(Definition* definition, const Object& value); | |
| 180 | |
| 181 // Assign the join (least upper bound) of a pair of abstract values to the | |
| 182 // first one. | |
| 183 void Join(Object* left, const Object& right); | |
| 184 | |
| 185 bool IsUnknown(const Object& value) { | |
| 186 return value.raw() == unknown_.raw(); | |
| 187 } | |
| 188 bool IsNonConstant(const Object& value) { | |
| 189 return value.raw() == non_constant_.raw(); | |
| 190 } | |
| 191 bool IsConstant(const Object& value) { | |
| 192 return !IsNonConstant(value) && !IsUnknown(value); | |
| 193 } | |
| 194 | |
| 195 virtual void VisitBlocks() { UNREACHABLE(); } | |
| 196 | |
| 197 #define DECLARE_VISIT(type) virtual void Visit##type(type##Instr* instr); | |
| 198 FOR_EACH_INSTRUCTION(DECLARE_VISIT) | |
| 199 #undef DECLARE_VISIT | |
| 200 | |
| 201 FlowGraph* graph_; | |
| 202 | |
| 203 // Sentinels for unknown constant and non-constant values. | |
| 204 const Object& unknown_; | |
| 205 const Object& non_constant_; | |
| 206 | |
| 207 // Analysis results. For each block, a reachability bit. Indexed by | |
| 208 // preorder number. | |
| 209 BitVector* reachable_; | |
| 210 | |
| 211 // Definitions can move up the lattice twice, so we use a mark bit to | |
| 212 // indicate that they are already on the worklist in order to avoid adding | |
| 213 // them again. Indexed by SSA temp index. | |
| 214 BitVector* definition_marks_; | |
| 215 | |
| 216 // Worklists of blocks and definitions. | |
| 217 GrowableArray<BlockEntryInstr*> block_worklist_; | |
| 218 GrowableArray<Definition*> definition_worklist_; | |
| 219 }; | |
| 220 | |
| 221 | |
| 222 } // namespace dart | 159 } // namespace dart |
| 223 | 160 |
| 224 #endif // VM_FLOW_GRAPH_OPTIMIZER_H_ | 161 #endif // VM_FLOW_GRAPH_OPTIMIZER_H_ |
| OLD | NEW |