| OLD | NEW |
| 1 // Copyright (c) 2011, 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 interface OptimizationPhase { | 5 interface OptimizationPhase { |
| 6 String get name(); | 6 String get name(); |
| 7 void visitGraph(HGraph graph); | 7 void visitGraph(HGraph graph); |
| 8 } | 8 } |
| 9 | 9 |
| 10 class SsaOptimizerTask extends CompilerTask { | 10 class SsaOptimizerTask extends CompilerTask { |
| 11 final JavaScriptBackend backend; | 11 final JavaScriptBackend backend; |
| (...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 node.block.addBefore(node, newInstruction); | 704 node.block.addBefore(node, newInstruction); |
| 705 node.block.rewrite(node, newInstruction); | 705 node.block.rewrite(node, newInstruction); |
| 706 node.block.remove(node); | 706 node.block.remove(node); |
| 707 } | 707 } |
| 708 } | 708 } |
| 709 | 709 |
| 710 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { | 710 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { |
| 711 final String name = "SsaDeadCodeEliminator"; | 711 final String name = "SsaDeadCodeEliminator"; |
| 712 | 712 |
| 713 static bool isDeadCode(HInstruction instruction) { | 713 static bool isDeadCode(HInstruction instruction) { |
| 714 // TODO(ngeoffray): the way we handle side effects is not right | |
| 715 // (e.g. branching instructions have side effects). | |
| 716 return !instruction.hasSideEffects() | 714 return !instruction.hasSideEffects() |
| 717 && instruction.usedBy.isEmpty() | 715 && instruction.usedBy.isEmpty() |
| 718 && instruction is !HCheck | 716 && instruction is !HCheck |
| 719 && instruction is !HTypeGuard; | 717 && instruction is !HTypeGuard |
| 718 && !instruction.isControlFlow(); |
| 720 } | 719 } |
| 721 | 720 |
| 722 void visitGraph(HGraph graph) { | 721 void visitGraph(HGraph graph) { |
| 723 visitPostDominatorTree(graph); | 722 visitPostDominatorTree(graph); |
| 724 } | 723 } |
| 725 | 724 |
| 726 void visitBasicBlock(HBasicBlock block) { | 725 void visitBasicBlock(HBasicBlock block) { |
| 727 HInstruction instruction = block.last; | 726 HInstruction instruction = block.last; |
| 728 while (instruction !== null) { | 727 while (instruction !== null) { |
| 729 var previous = instruction.previous; | 728 var previous = instruction.previous; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 897 } | 896 } |
| 898 } | 897 } |
| 899 | 898 |
| 900 bool isInputDefinedAfterDominator(HInstruction input, | 899 bool isInputDefinedAfterDominator(HInstruction input, |
| 901 HBasicBlock dominator) { | 900 HBasicBlock dominator) { |
| 902 return input.block.id > dominator.id; | 901 return input.block.id > dominator.id; |
| 903 } | 902 } |
| 904 | 903 |
| 905 void visitBasicBlock(HBasicBlock block, ValueSet values) { | 904 void visitBasicBlock(HBasicBlock block, ValueSet values) { |
| 906 HInstruction instruction = block.first; | 905 HInstruction instruction = block.first; |
| 906 if (block.isLoopHeader()) { |
| 907 int flags = loopChangesFlags[block.id]; |
| 908 values.kill(flags); |
| 909 } |
| 907 while (instruction !== null) { | 910 while (instruction !== null) { |
| 908 HInstruction next = instruction.next; | 911 HInstruction next = instruction.next; |
| 909 int flags = instruction.getChangesFlags(); | 912 int flags = instruction.getChangesFlags(); |
| 910 if (flags != 0) { | 913 assert(flags == 0 || !instruction.useGvn()); |
| 911 assert(!instruction.useGvn()); | 914 values.kill(flags); |
| 912 values.kill(flags); | 915 if (instruction.useGvn()) { |
| 913 } else if (instruction.useGvn()) { | |
| 914 HInstruction other = values.lookup(instruction); | 916 HInstruction other = values.lookup(instruction); |
| 915 if (other !== null) { | 917 if (other !== null) { |
| 916 assert(other.gvnEquals(instruction) && instruction.gvnEquals(other)); | 918 assert(other.gvnEquals(instruction) && instruction.gvnEquals(other)); |
| 917 block.rewrite(instruction, other); | 919 block.rewrite(instruction, other); |
| 918 block.remove(instruction); | 920 block.remove(instruction); |
| 919 } else { | 921 } else { |
| 920 values.add(instruction); | 922 values.add(instruction); |
| 921 } | 923 } |
| 922 } | 924 } |
| 923 instruction = next; | 925 instruction = next; |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1162 // the if block terminates. So any use of the instruction | 1164 // the if block terminates. So any use of the instruction |
| 1163 // after the join block should be changed to the new | 1165 // after the join block should be changed to the new |
| 1164 // instruction. | 1166 // instruction. |
| 1165 changeUsesDominatedBy(ifUser.joinBlock, input, convertedType); | 1167 changeUsesDominatedBy(ifUser.joinBlock, input, convertedType); |
| 1166 } | 1168 } |
| 1167 // TODO(ngeoffray): Also change uses for the then block on a HType | 1169 // TODO(ngeoffray): Also change uses for the then block on a HType |
| 1168 // that knows it is not of a specific Type. | 1170 // that knows it is not of a specific Type. |
| 1169 } | 1171 } |
| 1170 } | 1172 } |
| 1171 } | 1173 } |
| OLD | NEW |