| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 SsaTypePropagator extends HGraphVisitor implements OptimizationPhase { | 5 class SsaTypePropagator extends HGraphVisitor implements OptimizationPhase { |
| 6 | 6 |
| 7 final Map<int, HInstruction> workmap; | 7 final Map<int, HInstruction> workmap; |
| 8 final List<int> worklist; | 8 final List<int> worklist; |
| 9 final Compiler compiler; | 9 final Compiler compiler; |
| 10 final String name = 'type propagator'; | 10 String get name() => 'type propagator'; |
| 11 | 11 |
| 12 SsaTypePropagator(Compiler this.compiler) | 12 SsaTypePropagator(Compiler this.compiler) |
| 13 : workmap = new Map<int, HInstruction>(), | 13 : workmap = new Map<int, HInstruction>(), |
| 14 worklist = new List<int>(); | 14 worklist = new List<int>(); |
| 15 | 15 |
| 16 |
| 17 HType computeType(HInstruction instruction) => instruction.computeType(); |
| 18 |
| 19 // Re-compute and update the type of the instruction. Returns |
| 20 // whether or not the type was changed. |
| 21 bool updateType(HInstruction instruction) { |
| 22 if (instruction.type.isConflicting()) return false; |
| 23 // Constants have the type they have. It can't be changed. |
| 24 if (instruction.isConstant()) return false; |
| 25 |
| 26 HType oldType = instruction.type; |
| 27 HType newType = computeType(instruction); |
| 28 instruction.type = oldType.combine(newType); |
| 29 return oldType !== instruction.type; |
| 30 } |
| 31 |
| 16 void visitGraph(HGraph graph) { | 32 void visitGraph(HGraph graph) { |
| 17 visitDominatorTree(graph); | 33 visitDominatorTree(graph); |
| 18 processWorklist(); | 34 processWorklist(); |
| 19 } | 35 } |
| 20 | 36 |
| 21 visitBasicBlock(HBasicBlock block) { | 37 visitBasicBlock(HBasicBlock block) { |
| 22 if (block.isLoopHeader()) { | 38 if (block.isLoopHeader()) { |
| 23 block.forEachPhi((HPhi phi) { | 39 block.forEachPhi((HPhi phi) { |
| 24 phi.setInitialTypeForLoopPhi(); | 40 // Set the initial type for the phi. |
| 41 phi.type = phi.inputs[0].type; |
| 25 addToWorkList(phi); | 42 addToWorkList(phi); |
| 26 }); | 43 }); |
| 27 } else { | 44 } else { |
| 28 block.forEachPhi((HPhi phi) { | 45 block.forEachPhi((HPhi phi) { |
| 29 if (phi.updateType()) addUsersAndInputsToWorklist(phi); | 46 if (updateType(phi)) addUsersAndInputsToWorklist(phi); |
| 30 }); | 47 }); |
| 31 } | 48 } |
| 32 | 49 |
| 33 HInstruction instruction = block.first; | 50 HInstruction instruction = block.first; |
| 34 while (instruction !== null) { | 51 while (instruction !== null) { |
| 35 if (instruction.updateType()) addUsersAndInputsToWorklist(instruction); | 52 if (updateType(instruction)) addUsersAndInputsToWorklist(instruction); |
| 36 instruction = instruction.next; | 53 instruction = instruction.next; |
| 37 } | 54 } |
| 38 } | 55 } |
| 39 | 56 |
| 40 void processWorklist() { | 57 void processWorklist() { |
| 41 while (!worklist.isEmpty()) { | 58 while (!worklist.isEmpty()) { |
| 42 int id = worklist.removeLast(); | 59 int id = worklist.removeLast(); |
| 43 HInstruction instruction = workmap[id]; | 60 HInstruction instruction = workmap[id]; |
| 44 assert(instruction !== null); | 61 assert(instruction !== null); |
| 45 workmap.remove(id); | 62 workmap.remove(id); |
| 46 if (instruction.updateType()) addUsersAndInputsToWorklist(instruction); | 63 if (updateType(instruction)) addUsersAndInputsToWorklist(instruction); |
| 47 } | 64 } |
| 48 } | 65 } |
| 49 | 66 |
| 50 void addUsersAndInputsToWorklist(HInstruction instruction) { | 67 void addUsersAndInputsToWorklist(HInstruction instruction) { |
| 51 for (int i = 0, length = instruction.usedBy.length; i < length; i++) { | 68 for (int i = 0, length = instruction.usedBy.length; i < length; i++) { |
| 52 addToWorkList(instruction.usedBy[i]); | 69 addToWorkList(instruction.usedBy[i]); |
| 53 } | 70 } |
| 54 for (int i = 0, length = instruction.inputs.length; i < length; i++) { | 71 for (int i = 0, length = instruction.inputs.length; i < length; i++) { |
| 55 addToWorkList(instruction.inputs[i]); | 72 addToWorkList(instruction.inputs[i]); |
| 56 } | 73 } |
| 57 } | 74 } |
| 58 | 75 |
| 59 void addToWorkList(HInstruction instruction) { | 76 void addToWorkList(HInstruction instruction) { |
| 60 final int id = instruction.id; | 77 final int id = instruction.id; |
| 61 if (!workmap.containsKey(id)) { | 78 if (!workmap.containsKey(id)) { |
| 62 worklist.add(id); | 79 worklist.add(id); |
| 63 workmap[id] = instruction; | 80 workmap[id] = instruction; |
| 64 } | 81 } |
| 65 } | 82 } |
| 66 } | 83 } |
| 84 |
| 85 class SsaSpeculativeTypePropagator extends SsaTypePropagator { |
| 86 final String name = 'speculative type propagator'; |
| 87 SsaSpeculativeTypePropagator(Compiler compiler) : super(compiler); |
| 88 |
| 89 HType computeType(HInstruction instruction) { |
| 90 HType newType = super.computeType(instruction); |
| 91 HType desiredType = instruction.computeDesiredType(); |
| 92 HType combined = newType.combine(desiredType); |
| 93 // If the propagated type [newType] does not conflict with the |
| 94 // speculated type [desiredType], use it. |
| 95 if (combined.isKnown()) return combined; |
| 96 return newType; |
| 97 } |
| 98 } |
| OLD | NEW |