OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 class SsaTypePropagator extends HGraphVisitor implements OptimizationPhase { | |
6 | |
7 final Map<int, HInstruction> workmap; | |
8 final List<int> worklist; | |
9 final Compiler compiler; | |
10 final String name = 'type propagator'; | |
11 | |
12 SsaTypePropagator(Compiler this.compiler) | |
13 : workmap = new Map<int, HInstruction>(), | |
14 worklist = new List<int>(); | |
15 | |
16 void visitGraph(HGraph graph) { | |
17 visitDominatorTree(graph); | |
18 processWorklist(); | |
19 } | |
20 | |
21 visitBasicBlock(HBasicBlock block) { | |
22 if (block.isLoopHeader()) { | |
23 block.forEachPhi((HPhi phi) { | |
24 phi.setInitialTypeForLoopPhi(); | |
25 addToWorkList(phi); | |
26 }); | |
27 } else { | |
28 block.forEachPhi((HPhi phi) { | |
29 if (phi.updateType()) addUsersAndInputsToWorklist(phi); | |
30 }); | |
31 } | |
32 | |
33 HInstruction instruction = block.first; | |
34 while (instruction !== null) { | |
35 if (instruction.updateType()) addUsersAndInputsToWorklist(instruction); | |
36 instruction = instruction.next; | |
37 } | |
38 } | |
39 | |
40 void processWorklist() { | |
41 while (!worklist.isEmpty()) { | |
42 int id = worklist.removeLast(); | |
43 HInstruction instruction = workmap[id]; | |
44 assert(instruction !== null); | |
45 workmap.remove(id); | |
46 if (instruction.updateType()) addUsersAndInputsToWorklist(instruction); | |
47 } | |
48 } | |
49 | |
50 void addUsersAndInputsToWorklist(HInstruction instruction) { | |
51 for (int i = 0, length = instruction.usedBy.length; i < length; i++) { | |
52 addToWorkList(instruction.usedBy[i]); | |
53 } | |
54 for (int i = 0, length = instruction.inputs.length; i < length; i++) { | |
55 addToWorkList(instruction.inputs[i]); | |
56 } | |
57 } | |
58 | |
59 void addToWorkList(HInstruction instruction) { | |
60 final int id = instruction.id; | |
61 if (!workmap.containsKey(id)) { | |
62 worklist.add(id); | |
63 workmap[id] = instruction; | |
64 } | |
65 } | |
66 } | |
OLD | NEW |