| 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 class BailoutInfo { | 5 class BailoutInfo { |
| 6 int instructionId; | 6 int instructionId; |
| 7 int bailoutId; | 7 int bailoutId; |
| 8 BailoutInfo(this.instructionId, this.bailoutId); | 8 BailoutInfo(this.instructionId, this.bailoutId); |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Visits the graph in dominator order and inserts TypeGuards in places where | 77 * Visits the graph in dominator order and inserts TypeGuards in places where |
| 78 * we consider the guard to be of value. | 78 * we consider the guard to be of value. |
| 79 * | 79 * |
| 80 * Might modify the [:propagatedType:] fields of the instructions in an | 80 * Might modify the [:propagatedType:] fields of the instructions in an |
| 81 * inconsistent way. No further analysis should rely on them. | 81 * inconsistent way. No further analysis should rely on them. |
| 82 */ | 82 */ |
| 83 class SsaTypeGuardInserter extends HGraphVisitor implements OptimizationPhase { | 83 class SsaTypeGuardInserter extends HGraphVisitor implements OptimizationPhase { |
| 84 static final int SMALL_METHOD_BLOCK_LIMIT = 5; | |
| 85 | |
| 86 final Compiler compiler; | 84 final Compiler compiler; |
| 87 final String name = 'SsaTypeGuardInserter'; | 85 final String name = 'SsaTypeGuardInserter'; |
| 88 final WorkItem work; | 86 final WorkItem work; |
| 89 bool smallMethodNoLoops = false; | 87 bool calledInLoop = false; |
| 90 bool isRecursiveMethod = false; | 88 bool isRecursiveMethod = false; |
| 91 int stateId = 1; | 89 int stateId = 1; |
| 92 | 90 |
| 93 SsaTypeGuardInserter(this.compiler, this.work); | 91 SsaTypeGuardInserter(this.compiler, this.work); |
| 94 | 92 |
| 95 void visitGraph(HGraph graph) { | 93 void visitGraph(HGraph graph) { |
| 96 isRecursiveMethod = graph.isRecursiveMethod; | 94 isRecursiveMethod = graph.isRecursiveMethod; |
| 97 var blocks = graph.blocks; | 95 calledInLoop = graph.calledInLoop; |
| 98 if (blocks.length < SMALL_METHOD_BLOCK_LIMIT) { | |
| 99 smallMethodNoLoops = true; | |
| 100 for (var i = 0; i < blocks.length; i++) { | |
| 101 if (blocks[i].enclosingLoopHeader !== null) { | |
| 102 smallMethodNoLoops = false; | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 work.guards = <HTypeGuard>[]; | 96 work.guards = <HTypeGuard>[]; |
| 107 visitDominatorTree(graph); | 97 visitDominatorTree(graph); |
| 108 } | 98 } |
| 109 | 99 |
| 110 void visitBasicBlock(HBasicBlock block) { | 100 void visitBasicBlock(HBasicBlock block) { |
| 111 block.forEachPhi(visitInstruction); | 101 block.forEachPhi(visitInstruction); |
| 112 | 102 |
| 113 HInstruction instruction = block.first; | 103 HInstruction instruction = block.first; |
| 114 while (instruction !== null) { | 104 while (instruction !== null) { |
| 115 // Note that visitInstruction (from the phis and here) might insert an | 105 // Note that visitInstruction (from the phis and here) might insert an |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 return false; | 146 return false; |
| 157 } | 147 } |
| 158 | 148 |
| 159 // If the instruction is not in a loop then the header will be null. | 149 // If the instruction is not in a loop then the header will be null. |
| 160 HBasicBlock currentLoopHeader = instruction.block.enclosingLoopHeader; | 150 HBasicBlock currentLoopHeader = instruction.block.enclosingLoopHeader; |
| 161 for (HInstruction user in instruction.usedBy) { | 151 for (HInstruction user in instruction.usedBy) { |
| 162 HBasicBlock userLoopHeader = user.block.enclosingLoopHeader; | 152 HBasicBlock userLoopHeader = user.block.enclosingLoopHeader; |
| 163 if (isNested(userLoopHeader, currentLoopHeader)) return true; | 153 if (isNested(userLoopHeader, currentLoopHeader)) return true; |
| 164 } | 154 } |
| 165 | 155 |
| 166 // Insert type guards for small methods with no loops and multiple | 156 // Insert type guards if the method is likely to be called in a |
| 167 // uses. These are expected to be helper methods that could | 157 // loop. |
| 168 // benefit from type guards. If there is a loop, we expect the | 158 return calledInLoop; |
| 169 // loop to take most of the time and that inserting a type guard | |
| 170 // for something not used in the loop will not be valuable. | |
| 171 if (smallMethodNoLoops && instruction.usedBy.length > 2) return true; | |
| 172 | |
| 173 return false; | |
| 174 } | 159 } |
| 175 | 160 |
| 176 bool shouldInsertTypeGuard(HInstruction instruction) { | 161 bool shouldInsertTypeGuard(HInstruction instruction) { |
| 177 HType speculativeType = instruction.propagatedType; | 162 HType speculativeType = instruction.propagatedType; |
| 178 HType computedType = instruction.computeTypeFromInputTypes(); | 163 HType computedType = instruction.computeTypeFromInputTypes(); |
| 179 // Start by reverting the propagated type. If we add a type guard then the | 164 // Start by reverting the propagated type. If we add a type guard then the |
| 180 // guard will expose the speculative type. If we don't add a type guard | 165 // guard will expose the speculative type. If we don't add a type guard |
| 181 // then this avoids subsequent instructions to use the the wrong type. | 166 // then this avoids subsequent instructions to use the the wrong type. |
| 182 // | 167 // |
| 183 // Note that just setting the propagatedType of the instruction is not | 168 // Note that just setting the propagatedType of the instruction is not |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 } | 444 } |
| 460 } | 445 } |
| 461 } | 446 } |
| 462 | 447 |
| 463 visitTypeGuard(HTypeGuard guard) { | 448 visitTypeGuard(HTypeGuard guard) { |
| 464 blocks.forEach((HBasicBlock block) { | 449 blocks.forEach((HBasicBlock block) { |
| 465 block.guards.add(guard); | 450 block.guards.add(guard); |
| 466 }); | 451 }); |
| 467 } | 452 } |
| 468 } | 453 } |
| OLD | NEW |