| 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 /** | 5 /** |
| 6 * Instead of emitting each SSA instruction with a temporary variable | 6 * Instead of emitting each SSA instruction with a temporary variable |
| 7 * mark instructions that can be emitted at their use-site. | 7 * mark instructions that can be emitted at their use-site. |
| 8 * For example, in: | 8 * For example, in: |
| 9 * t0 = 4; | 9 * t0 = 4; |
| 10 * t1 = 3; | 10 * t1 = 3; |
| 11 * t2 = add(t0, t1); | 11 * t2 = add(t0, t1); |
| 12 * t0 and t1 would be marked and the resulting code would then be: | 12 * t0 and t1 would be marked and the resulting code would then be: |
| 13 * t2 = add(4, 3); | 13 * t2 = add(4, 3); |
| 14 */ | 14 */ |
| 15 class SsaInstructionMerger extends HGraphVisitor { | 15 class SsaInstructionMerger extends HBaseVisitor { |
| 16 List<HInstruction> expectedInputs; |
| 17 |
| 16 void visitGraph(HGraph graph) { | 18 void visitGraph(HGraph graph) { |
| 17 visitDominatorTree(graph); | 19 visitDominatorTree(graph); |
| 18 } | 20 } |
| 19 | 21 |
| 20 bool usedOnlyByPhis(instruction) { | 22 bool usedOnlyByPhis(instruction) { |
| 21 for (HInstruction user in instruction.usedBy) { | 23 for (HInstruction user in instruction.usedBy) { |
| 22 if (user is !HPhi) return false; | 24 if (user is !HPhi) return false; |
| 23 } | 25 } |
| 24 return true; | 26 return true; |
| 25 } | 27 } |
| 26 | 28 |
| 29 void visitInstruction(HInstruction instruction) { |
| 30 for (HInstruction input in instruction.inputs) { |
| 31 if (!input.generateAtUseSite() && input.usedBy.length == 1) { |
| 32 expectedInputs.add(input); |
| 33 } |
| 34 } |
| 35 } |
| 36 |
| 37 // The codegen might use the input multiple times, so it must not be |
| 38 // set generate at use site. |
| 39 void visitIs(HIs instruction) {} |
| 40 |
| 27 void visitBasicBlock(HBasicBlock block) { | 41 void visitBasicBlock(HBasicBlock block) { |
| 28 // Visit each instruction of the basic block in last-to-first order. | 42 // Visit each instruction of the basic block in last-to-first order. |
| 29 // Keep a list of expected inputs of the current "expression" being | 43 // Keep a list of expected inputs of the current "expression" being |
| 30 // merged. If instructions occur in the expected order, they are | 44 // merged. If instructions occur in the expected order, they are |
| 31 // included in the expression. | 45 // included in the expression. |
| 32 | 46 |
| 33 // The expectedInputs list holds non-trivial instructions that may | 47 // The expectedInputs list holds non-trivial instructions that may |
| 34 // be generated at their use site, if they occur in the correct order. | 48 // be generated at their use site, if they occur in the correct order. |
| 35 List<HInstruction> expectedInputs = new List<HInstruction>(); | 49 expectedInputs = new List<HInstruction>(); |
| 36 // Add non-trivial inputs of instruction to expectedInputs, in | 50 // Add non-trivial inputs of instruction to expectedInputs, in |
| 37 // evaluation order. | 51 // evaluation order. |
| 38 void addInputs(HInstruction instruction) { | 52 void addInputs(HInstruction instruction) { |
| 39 for (HInstruction input in instruction.inputs) { | 53 instruction.accept(this); |
| 40 if (!input.generateAtUseSite() && input.usedBy.length == 1) { | |
| 41 expectedInputs.add(input); | |
| 42 } | |
| 43 } | |
| 44 } | 54 } |
| 45 // Pop instructions from expectedInputs until instruction is found. | 55 // Pop instructions from expectedInputs until instruction is found. |
| 46 // Return true if it is found, or false if not. | 56 // Return true if it is found, or false if not. |
| 47 bool findInInputs(HInstruction instruction) { | 57 bool findInInputs(HInstruction instruction) { |
| 48 while (!expectedInputs.isEmpty()) { | 58 while (!expectedInputs.isEmpty()) { |
| 49 HInstruction nextInput = expectedInputs.removeLast(); | 59 HInstruction nextInput = expectedInputs.removeLast(); |
| 50 assert(!nextInput.generateAtUseSite()); | 60 assert(!nextInput.generateAtUseSite()); |
| 51 assert(nextInput.usedBy.length == 1); | 61 assert(nextInput.usedBy.length == 1); |
| 52 if (nextInput == instruction) { | 62 if (nextInput == instruction) { |
| 53 return true; | 63 return true; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 73 } | 83 } |
| 74 if (instruction is HForeign) { | 84 if (instruction is HForeign) { |
| 75 // Never try to merge inputs to HForeign. | 85 // Never try to merge inputs to HForeign. |
| 76 continue; | 86 continue; |
| 77 } else if (instruction.generateAtUseSite() || | 87 } else if (instruction.generateAtUseSite() || |
| 78 usedOnlyByPhis(instruction)) { | 88 usedOnlyByPhis(instruction)) { |
| 79 // In all other cases, try merging all non-trivial inputs. | 89 // In all other cases, try merging all non-trivial inputs. |
| 80 addInputs(instruction); | 90 addInputs(instruction); |
| 81 } | 91 } |
| 82 } | 92 } |
| 93 expectedInputs = null; |
| 83 } | 94 } |
| 84 } | 95 } |
| 85 | 96 |
| 86 /** | 97 /** |
| 87 * In order to generate efficient code that works with bailouts, we | 98 * In order to generate efficient code that works with bailouts, we |
| 88 * rewrite users of check instruction to use the input of the | 99 * rewrite users of check instruction to use the input of the |
| 89 * instruction instead of the check itself. | 100 * instruction instead of the check itself. |
| 90 */ | 101 */ |
| 91 class SsaCheckInstructionUnuser extends HBaseVisitor { | 102 class SsaCheckInstructionUnuser extends HBaseVisitor { |
| 92 void visitGraph(HGraph graph) { | 103 void visitGraph(HGraph graph) { |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 secondBlock.last.tryGenerateAtUseSite(); | 257 secondBlock.last.tryGenerateAtUseSite(); |
| 247 } | 258 } |
| 248 | 259 |
| 249 void visitBasicBlock(HBasicBlock block) { | 260 void visitBasicBlock(HBasicBlock block) { |
| 250 if (!block.phis.isEmpty() && | 261 if (!block.phis.isEmpty() && |
| 251 block.phis.first == block.phis.last) { | 262 block.phis.first == block.phis.last) { |
| 252 detectLogicControlFlow(block.phis.first); | 263 detectLogicControlFlow(block.phis.first); |
| 253 } | 264 } |
| 254 } | 265 } |
| 255 } | 266 } |
| OLD | NEW |