reversed traverse in depth

This commit is contained in:
Svetlana Isakova
2012-05-25 21:07:33 +04:00
parent fb08e13da9
commit 5c7b787e68
@@ -24,10 +24,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.cfg.pseudocode.*; import org.jetbrains.jet.lang.cfg.pseudocode.*;
import java.util.Collection; import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/** /**
* @author svtk * @author svtk
@@ -53,6 +50,11 @@ public class JetControlFlowGraphTraverser<D> {
return straightDirection ? pseudocode.getEnterInstruction() : pseudocode.getSinkInstruction(); return straightDirection ? pseudocode.getEnterInstruction() : pseudocode.getSinkInstruction();
} }
@NotNull
public Map<Instruction, Pair<D, D>> getDataMap() {
return dataMap;
}
public void collectInformationFromInstructionGraph( public void collectInformationFromInstructionGraph(
@NotNull D initialDataValue, @NotNull D initialDataValue,
@NotNull D initialDataValueForEnterInstruction, @NotNull D initialDataValueForEnterInstruction,
@@ -82,6 +84,22 @@ public class JetControlFlowGraphTraverser<D> {
} }
} }
private static List<Instruction> reverseInstructions(@NotNull Pseudocode pseudocode) {
LinkedHashSet<Instruction> traversedInstructions = Sets.newLinkedHashSet();
Instruction sinkInstruction = pseudocode.getSinkInstruction();
traverseInstructions(sinkInstruction, traversedInstructions);
return Lists.newArrayList(traversedInstructions);
}
private static void traverseInstructions(@NotNull Instruction instruction, @NotNull LinkedHashSet<Instruction> instructions) {
if (((InstructionImpl)instruction).isDead()) return;
if (instructions.contains(instruction)) return;
instructions.add(instruction);
for (Instruction previousInstruction : instruction.getPreviousInstructions()) {
traverseInstructions(previousInstruction, instructions);
}
}
private void traverseSubGraph( private void traverseSubGraph(
@NotNull Pseudocode pseudocode, @NotNull Pseudocode pseudocode,
@NotNull InstructionDataMergeStrategy<D> instructionDataMergeStrategy, @NotNull InstructionDataMergeStrategy<D> instructionDataMergeStrategy,
@@ -92,8 +110,7 @@ public class JetControlFlowGraphTraverser<D> {
Instruction startInstruction = getStartInstruction(pseudocode); Instruction startInstruction = getStartInstruction(pseudocode);
if (!straightDirection) { if (!straightDirection) {
instructions = Lists.newArrayList(instructions); instructions = reverseInstructions(pseudocode);
Collections.reverse(instructions);
} }
for (Instruction instruction : instructions) { for (Instruction instruction : instructions) {
boolean isStart = straightDirection ? instruction instanceof SubroutineEnterInstruction : instruction instanceof SubroutineSinkInstruction; boolean isStart = straightDirection ? instruction instanceof SubroutineEnterInstruction : instruction instanceof SubroutineSinkInstruction;
@@ -170,11 +187,11 @@ public class JetControlFlowGraphTraverser<D> {
return dataMap.get(pseudocode.getExitInstruction()).getFirst(); return dataMap.get(pseudocode.getExitInstruction()).getFirst();
} }
interface InstructionDataMergeStrategy<D> { public interface InstructionDataMergeStrategy<D> {
Pair<D, D> execute(@NotNull Instruction instruction, @NotNull Collection<D> incomingEdgesData); Pair<D, D> execute(@NotNull Instruction instruction, @NotNull Collection<D> incomingEdgesData);
} }
interface InstructionDataAnalyzeStrategy<D> { public interface InstructionDataAnalyzeStrategy<D> {
void execute(@NotNull Instruction instruction, @Nullable D enterData, @Nullable D exitData); void execute(@NotNull Instruction instruction, @Nullable D enterData, @Nullable D exitData);
} }
} }