| 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 part of ssa; | 5 part of ssa; |
| 6 | 6 |
| 7 abstract class OptimizationPhase { | 7 abstract class OptimizationPhase { |
| 8 String get name; | 8 String get name; |
| 9 void visitGraph(HGraph graph); | 9 void visitGraph(HGraph graph); |
| 10 } | 10 } |
| (...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 node, node.receiver, graph.addConstantInt(0, backend.compiler)); | 975 node, node.receiver, graph.addConstantInt(0, backend.compiler)); |
| 976 } | 976 } |
| 977 } | 977 } |
| 978 | 978 |
| 979 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { | 979 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { |
| 980 final String name = "SsaDeadCodeEliminator"; | 980 final String name = "SsaDeadCodeEliminator"; |
| 981 | 981 |
| 982 final Compiler compiler; | 982 final Compiler compiler; |
| 983 final SsaOptimizerTask optimizer; | 983 final SsaOptimizerTask optimizer; |
| 984 SsaLiveBlockAnalyzer analyzer; | 984 SsaLiveBlockAnalyzer analyzer; |
| 985 Map<HInstruction, bool> trivialDeadStoreReceivers = |
| 986 new Maplet<HInstruction, bool>(); |
| 985 bool eliminatedSideEffects = false; | 987 bool eliminatedSideEffects = false; |
| 986 SsaDeadCodeEliminator(this.compiler, this.optimizer); | 988 SsaDeadCodeEliminator(this.compiler, this.optimizer); |
| 987 | 989 |
| 988 HInstruction zapInstructionCache; | 990 HInstruction zapInstructionCache; |
| 989 HInstruction get zapInstruction { | 991 HInstruction get zapInstruction { |
| 990 if (zapInstructionCache == null) { | 992 if (zapInstructionCache == null) { |
| 991 // A constant with no type does not pollute types at phi nodes. | 993 // A constant with no type does not pollute types at phi nodes. |
| 992 ConstantValue constant = | 994 ConstantValue constant = |
| 993 new DummyConstantValue(const TypeMask.nonNullEmpty()); | 995 new DummyConstantValue(const TypeMask.nonNullEmpty()); |
| 994 zapInstructionCache = analyzer.graph.addConstant(constant, compiler); | 996 zapInstructionCache = analyzer.graph.addConstant(constant, compiler); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1015 // just jumps to a single predecessor, visit this predecessor. | 1017 // just jumps to a single predecessor, visit this predecessor. |
| 1016 assert(current.block.successors.length == 1); | 1018 assert(current.block.successors.length == 1); |
| 1017 current = current.block.successors[0].first; | 1019 current = current.block.successors[0].first; |
| 1018 } else { | 1020 } else { |
| 1019 current = current.next; | 1021 current = current.next; |
| 1020 } | 1022 } |
| 1021 } while (current != null); | 1023 } while (current != null); |
| 1022 return false; | 1024 return false; |
| 1023 } | 1025 } |
| 1024 | 1026 |
| 1027 bool isTrivialDeadStoreReceiver(HInstruction instruction) { |
| 1028 // For an allocation, if all the loads are dead (awaiting removal after |
| 1029 // SsaLoadElimination) and the only other uses are stores, then the |
| 1030 // allocation does not escape which makes all the stores dead too. |
| 1031 bool isDeadUse(HInstruction use) { |
| 1032 if (use is HFieldSet) { |
| 1033 // The use must be the receiver. Even if the use is also the argument, |
| 1034 // i.e. a.x = a, the store is still dead if all other uses are dead. |
| 1035 if (use.getDartReceiver(compiler) == instruction) return true; |
| 1036 } else if (use is HFieldGet) { |
| 1037 assert(use.getDartReceiver(compiler) == instruction); |
| 1038 if (isDeadCode(use)) return true; |
| 1039 } |
| 1040 return false; |
| 1041 } |
| 1042 return instruction is HForeignNew |
| 1043 && trivialDeadStoreReceivers.putIfAbsent(instruction, |
| 1044 () => instruction.usedBy.every(isDeadUse)); |
| 1045 } |
| 1046 |
| 1047 bool isTrivialDeadStore(HInstruction instruction) { |
| 1048 return instruction is HFieldSet |
| 1049 && isTrivialDeadStoreReceiver(instruction.getDartReceiver(compiler)); |
| 1050 } |
| 1051 |
| 1025 bool isDeadCode(HInstruction instruction) { | 1052 bool isDeadCode(HInstruction instruction) { |
| 1026 if (!instruction.usedBy.isEmpty) return false; | 1053 if (!instruction.usedBy.isEmpty) return false; |
| 1054 if (isTrivialDeadStore(instruction)) return true; |
| 1027 if (instruction.sideEffects.hasSideEffects()) return false; | 1055 if (instruction.sideEffects.hasSideEffects()) return false; |
| 1028 if (instruction.canThrow() | 1056 if (instruction.canThrow() |
| 1029 && instruction.onlyThrowsNSM() | 1057 && instruction.onlyThrowsNSM() |
| 1030 && hasFollowingThrowingNSM(instruction)) { | 1058 && hasFollowingThrowingNSM(instruction)) { |
| 1031 return true; | 1059 return true; |
| 1032 } | 1060 } |
| 1033 return !instruction.canThrow() | 1061 return !instruction.canThrow() |
| 1034 && instruction is !HParameterValue | 1062 && instruction is !HParameterValue |
| 1035 && instruction is !HLocalSet; | 1063 && instruction is !HLocalSet; |
| 1036 } | 1064 } |
| (...skipping 1123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2160 | 2188 |
| 2161 keyedValues.forEach((receiver, values) { | 2189 keyedValues.forEach((receiver, values) { |
| 2162 result.keyedValues[receiver] = | 2190 result.keyedValues[receiver] = |
| 2163 new Map<HInstruction, HInstruction>.from(values); | 2191 new Map<HInstruction, HInstruction>.from(values); |
| 2164 }); | 2192 }); |
| 2165 | 2193 |
| 2166 result.nonEscapingReceivers.addAll(nonEscapingReceivers); | 2194 result.nonEscapingReceivers.addAll(nonEscapingReceivers); |
| 2167 return result; | 2195 return result; |
| 2168 } | 2196 } |
| 2169 } | 2197 } |
| OLD | NEW |