| 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 #ifndef VM_FLOW_GRAPH_OPTIMIZER_H_ | |
| 6 #define VM_FLOW_GRAPH_OPTIMIZER_H_ | |
| 7 | |
| 8 #include "vm/intermediate_language.h" | |
| 9 #include "vm/flow_graph.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
| 13 class CSEInstructionMap; | |
| 14 template <typename T> class GrowableArray; | |
| 15 class ParsedFunction; | |
| 16 | |
| 17 class FlowGraphOptimizer : public FlowGraphVisitor { | |
| 18 public: | |
| 19 FlowGraphOptimizer( | |
| 20 FlowGraph* flow_graph, | |
| 21 bool use_speculative_inlining, | |
| 22 GrowableArray<intptr_t>* inlining_black_list) | |
| 23 : FlowGraphVisitor(flow_graph->reverse_postorder()), | |
| 24 flow_graph_(flow_graph), | |
| 25 use_speculative_inlining_(use_speculative_inlining), | |
| 26 inlining_black_list_(inlining_black_list) { | |
| 27 ASSERT(!use_speculative_inlining || (inlining_black_list != NULL)); | |
| 28 } | |
| 29 virtual ~FlowGraphOptimizer() {} | |
| 30 | |
| 31 FlowGraph* flow_graph() const { return flow_graph_; } | |
| 32 | |
| 33 // Use ICData to optimize, replace or eliminate instructions. | |
| 34 void ApplyICData(); | |
| 35 | |
| 36 // Use propagated class ids to optimize, replace or eliminate instructions. | |
| 37 void ApplyClassIds(); | |
| 38 | |
| 39 // Optimize (a << b) & c pattern: if c is a positive Smi or zero, then the | |
| 40 // shift can be a truncating Smi shift-left and result is always Smi. | |
| 41 // Merge instructions (only per basic-block). | |
| 42 void TryOptimizePatterns(); | |
| 43 | |
| 44 virtual void VisitStaticCall(StaticCallInstr* instr); | |
| 45 virtual void VisitInstanceCall(InstanceCallInstr* instr); | |
| 46 virtual void VisitStoreInstanceField(StoreInstanceFieldInstr* instr); | |
| 47 virtual void VisitAllocateContext(AllocateContextInstr* instr); | |
| 48 virtual void VisitLoadCodeUnits(LoadCodeUnitsInstr* instr); | |
| 49 | |
| 50 void InsertBefore(Instruction* next, | |
| 51 Instruction* instr, | |
| 52 Environment* env, | |
| 53 FlowGraph::UseKind use_kind) { | |
| 54 flow_graph_->InsertBefore(next, instr, env, use_kind); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 // Attempt to build ICData for call using propagated class-ids. | |
| 59 bool TryCreateICData(InstanceCallInstr* call); | |
| 60 const ICData& TrySpecializeICData(const ICData& ic_data, intptr_t cid); | |
| 61 | |
| 62 void SpecializePolymorphicInstanceCall(PolymorphicInstanceCallInstr* call); | |
| 63 | |
| 64 bool TryReplaceWithIndexedOp(InstanceCallInstr* call); | |
| 65 | |
| 66 | |
| 67 bool TryReplaceWithBinaryOp(InstanceCallInstr* call, Token::Kind op_kind); | |
| 68 bool TryReplaceWithUnaryOp(InstanceCallInstr* call, Token::Kind op_kind); | |
| 69 | |
| 70 bool TryReplaceWithEqualityOp(InstanceCallInstr* call, Token::Kind op_kind); | |
| 71 bool TryReplaceWithRelationalOp(InstanceCallInstr* call, Token::Kind op_kind); | |
| 72 | |
| 73 bool TryInlineInstanceGetter(InstanceCallInstr* call, | |
| 74 bool allow_check = true); | |
| 75 bool TryInlineInstanceSetter(InstanceCallInstr* call, | |
| 76 const ICData& unary_ic_data, | |
| 77 bool allow_check = true); | |
| 78 | |
| 79 bool TryInlineInstanceMethod(InstanceCallInstr* call); | |
| 80 bool TryInlineFloat32x4Constructor(StaticCallInstr* call, | |
| 81 MethodRecognizer::Kind recognized_kind); | |
| 82 bool TryInlineFloat64x2Constructor(StaticCallInstr* call, | |
| 83 MethodRecognizer::Kind recognized_kind); | |
| 84 bool TryInlineInt32x4Constructor(StaticCallInstr* call, | |
| 85 MethodRecognizer::Kind recognized_kind); | |
| 86 bool TryInlineFloat32x4Method(InstanceCallInstr* call, | |
| 87 MethodRecognizer::Kind recognized_kind); | |
| 88 bool TryInlineFloat64x2Method(InstanceCallInstr* call, | |
| 89 MethodRecognizer::Kind recognized_kind); | |
| 90 bool TryInlineInt32x4Method(InstanceCallInstr* call, | |
| 91 MethodRecognizer::Kind recognized_kind); | |
| 92 void ReplaceWithInstanceOf(InstanceCallInstr* instr); | |
| 93 bool TypeCheckAsClassEquality(const AbstractType& type); | |
| 94 void ReplaceWithTypeCast(InstanceCallInstr* instr); | |
| 95 | |
| 96 bool TryReplaceInstanceCallWithInline(InstanceCallInstr* call); | |
| 97 | |
| 98 // Insert a check of 'to_check' determined by 'unary_checks'. If the | |
| 99 // check fails it will deoptimize to 'deopt_id' using the deoptimization | |
| 100 // environment 'deopt_environment'. The check is inserted immediately | |
| 101 // before 'insert_before'. | |
| 102 void AddCheckClass(Definition* to_check, | |
| 103 const ICData& unary_checks, | |
| 104 intptr_t deopt_id, | |
| 105 Environment* deopt_environment, | |
| 106 Instruction* insert_before); | |
| 107 Instruction* GetCheckClass(Definition* to_check, | |
| 108 const ICData& unary_checks, | |
| 109 intptr_t deopt_id, | |
| 110 TokenPosition token_pos); | |
| 111 | |
| 112 // Insert a Smi check if needed. | |
| 113 void AddCheckSmi(Definition* to_check, | |
| 114 intptr_t deopt_id, | |
| 115 Environment* deopt_environment, | |
| 116 Instruction* insert_before); | |
| 117 | |
| 118 // Add a class check for a call's first argument immediately before the | |
| 119 // call, using the call's IC data to determine the check, and the call's | |
| 120 // deopt ID and deoptimization environment if the check fails. | |
| 121 void AddReceiverCheck(InstanceCallInstr* call); | |
| 122 | |
| 123 void ReplaceCall(Definition* call, Definition* replacement); | |
| 124 | |
| 125 | |
| 126 bool InstanceCallNeedsClassCheck(InstanceCallInstr* call, | |
| 127 RawFunction::Kind kind) const; | |
| 128 | |
| 129 bool InlineFloat32x4Getter(InstanceCallInstr* call, | |
| 130 MethodRecognizer::Kind getter); | |
| 131 bool InlineFloat64x2Getter(InstanceCallInstr* call, | |
| 132 MethodRecognizer::Kind getter); | |
| 133 bool InlineInt32x4Getter(InstanceCallInstr* call, | |
| 134 MethodRecognizer::Kind getter); | |
| 135 bool InlineFloat32x4BinaryOp(InstanceCallInstr* call, | |
| 136 Token::Kind op_kind); | |
| 137 bool InlineInt32x4BinaryOp(InstanceCallInstr* call, | |
| 138 Token::Kind op_kind); | |
| 139 bool InlineFloat64x2BinaryOp(InstanceCallInstr* call, | |
| 140 Token::Kind op_kind); | |
| 141 bool InlineImplicitInstanceGetter(InstanceCallInstr* call, bool allow_check); | |
| 142 | |
| 143 RawBool* InstanceOfAsBool(const ICData& ic_data, | |
| 144 const AbstractType& type, | |
| 145 ZoneGrowableArray<intptr_t>* results) const; | |
| 146 | |
| 147 void ReplaceWithMathCFunction(InstanceCallInstr* call, | |
| 148 MethodRecognizer::Kind recognized_kind); | |
| 149 | |
| 150 void OptimizeLeftShiftBitAndSmiOp(Definition* bit_and_instr, | |
| 151 Definition* left_instr, | |
| 152 Definition* right_instr); | |
| 153 void TryMergeTruncDivMod(GrowableArray<BinarySmiOpInstr*>* merge_candidates); | |
| 154 void TryMergeMathUnary(GrowableArray<MathUnaryInstr*>* merge_candidates); | |
| 155 | |
| 156 void AppendExtractNthOutputForMerged(Definition* instr, intptr_t ix, | |
| 157 Representation rep, intptr_t cid); | |
| 158 bool TryStringLengthOneEquality(InstanceCallInstr* call, Token::Kind op_kind); | |
| 159 | |
| 160 void InstanceCallNoopt(InstanceCallInstr* instr); | |
| 161 | |
| 162 RawField* GetField(intptr_t class_id, const String& field_name); | |
| 163 | |
| 164 Thread* thread() const { return flow_graph_->thread(); } | |
| 165 Isolate* isolate() const { return flow_graph_->isolate(); } | |
| 166 Zone* zone() const { return flow_graph_->zone(); } | |
| 167 | |
| 168 const Function& function() const { return flow_graph_->function(); } | |
| 169 | |
| 170 bool IsBlackListedForInlining(intptr_t deopt_id); | |
| 171 | |
| 172 FlowGraph* flow_graph_; | |
| 173 | |
| 174 const bool use_speculative_inlining_; | |
| 175 | |
| 176 GrowableArray<intptr_t>* inlining_black_list_; | |
| 177 | |
| 178 DISALLOW_COPY_AND_ASSIGN(FlowGraphOptimizer); | |
| 179 }; | |
| 180 | |
| 181 | |
| 182 } // namespace dart | |
| 183 | |
| 184 #endif // VM_FLOW_GRAPH_OPTIMIZER_H_ | |
| OLD | NEW |