diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/pseudocode/PseudocodeImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/pseudocode/PseudocodeImpl.java index 4e539b36ea1..ff2f033d666 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/pseudocode/PseudocodeImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/pseudocode/PseudocodeImpl.java @@ -17,7 +17,6 @@ package org.jetbrains.jet.lang.cfg.pseudocode; import com.google.common.collect.*; -import com.intellij.util.containers.MultiMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.cfg.Label; @@ -162,13 +161,13 @@ public class PseudocodeImpl implements Pseudocode { public List getReversedInstructions() { if (reversedInstructions == null) { LinkedHashSet traversedInstructions = Sets.newLinkedHashSet(); - traverseInstructionsInReverseOrder(sinkInstruction, traversedInstructions); + traverseFollowingInstructions(sinkInstruction, traversedInstructions, false); if (traversedInstructions.size() < instructions.size()) { List simplyReversedInstructions = Lists.newArrayList(instructions); Collections.reverse(simplyReversedInstructions); for (Instruction instruction : simplyReversedInstructions) { if (!traversedInstructions.contains(instruction)) { - traverseInstructionsInReverseOrder(instruction, traversedInstructions); + traverseFollowingInstructions(instruction, traversedInstructions, false); } } } @@ -177,29 +176,6 @@ public class PseudocodeImpl implements Pseudocode { return reversedInstructions; } - private static void traverseInstructionsInReverseOrder(@NotNull Instruction rootInstruction, - @NotNull LinkedHashSet instructions) { - Deque stack = Queues.newArrayDeque(); - stack.push(rootInstruction); - - List previousInstructions = Lists.newArrayList(); - while ( !stack.isEmpty() ) { - final Instruction instruction = stack.pop(); - - if ( instructions.contains(instruction) ) - continue; - - instructions.add(instruction); - - // Reverse iteration order to match the original recursive order. - previousInstructions.addAll(instruction.getPreviousInstructions()); - for (Instruction previousInstruction : Lists.reverse(previousInstructions)) { - stack.push(previousInstruction); - } - previousInstructions.clear(); - } - } - //for tests only @NotNull public List getAllInstructions() { @@ -363,7 +339,7 @@ public class PseudocodeImpl implements Pseudocode { private Set collectReachableInstructions() { Set visited = Sets.newHashSet(); - traverseNextInstructions(getEnterInstruction(), visited); + traverseFollowingInstructions(getEnterInstruction(), visited, true); if (!visited.contains(getExitInstruction())) { visited.add(getExitInstruction()); } @@ -376,28 +352,26 @@ public class PseudocodeImpl implements Pseudocode { return visited; } - private static void traverseNextInstructions(@NotNull Instruction rootInstruction, @NotNull Set visited) { + private static void traverseFollowingInstructions( + @NotNull Instruction rootInstruction, + @NotNull Set visited, + boolean directOrder + ) { Deque stack = Queues.newArrayDeque(); stack.push(rootInstruction); - List nextInstructions = Lists.newArrayList(); while (!stack.isEmpty()) { - final Instruction instruction = stack.pop(); - - if (visited.contains(instruction)) - continue; - + Instruction instruction = stack.pop(); visited.add(instruction); - // Reverse iteration order to match the original recursive order. - nextInstructions.addAll(instruction.getNextInstructions()); - for (Instruction nextInstruction : Lists.reverse(nextInstructions)) { - if ( nextInstruction == null ) - continue; + Collection followingInstructions = + directOrder ? instruction.getNextInstructions() : instruction.getPreviousInstructions(); - stack.push(nextInstruction); + for (Instruction followingInstruction : followingInstructions) { + if (followingInstruction != null && !visited.contains(followingInstruction)) { + stack.push(followingInstruction); + } } - nextInstructions.clear(); } }