Rename: getAllInstructions -> getInstructionsIncludingDeadCode

Added this method to 'Pseudocode' interface instead of 'getDeadInstructions',
added 'isDead' to 'Instruction' interface
This commit is contained in:
Svetlana Isakova
2014-06-11 12:53:03 +04:00
parent 4efeb2eae2
commit 67fd858432
10 changed files with 31 additions and 41 deletions
@@ -258,15 +258,16 @@ public class JetFlowInformationProvider {
private Set<JetElement> collectUnreachableCode() {
Set<JetElement> unreachableElements = Sets.newHashSet();
for (Instruction deadInstruction : pseudocode.getDeadInstructions()) {
if (!(deadInstruction instanceof JetElementInstruction)
|| deadInstruction instanceof LoadUnitValueInstruction
|| deadInstruction instanceof MergeInstruction
|| (deadInstruction instanceof MagicInstruction && ((MagicInstruction) deadInstruction).getSynthetic())) continue;
for (Instruction instruction : pseudocode.getDeadInstructions()) {
if (!PseudocodeUtil.isDeadInAllCopies(instruction)) continue;
if (!(instruction instanceof JetElementInstruction)
|| instruction instanceof LoadUnitValueInstruction
|| instruction instanceof MergeInstruction
|| (instruction instanceof MagicInstruction && ((MagicInstruction) instruction).getSynthetic())) continue;
JetElement element = ((JetElementInstruction) deadInstruction).getElement();
JetElement element = ((JetElementInstruction) instruction).getElement();
if (deadInstruction instanceof JumpInstruction) {
if (instruction instanceof JumpInstruction) {
boolean isJumpElement = element instanceof JetBreakExpression
|| element instanceof JetContinueExpression
|| element instanceof JetReturnExpression
@@ -45,7 +45,7 @@ public interface Pseudocode {
List<Instruction> getReversedInstructions();
@NotNull
List<Instruction> getDeadInstructions();
List<Instruction> getInstructionsIncludingDeadCode();
@NotNull
SubroutineExitInstruction getExitInstruction();
@@ -183,30 +183,10 @@ public class PseudocodeImpl implements Pseudocode {
return Lists.newArrayList(traversedInstructions);
}
//for tests only
@NotNull
public List<Instruction> getAllInstructions() {
return mutableInstructionList;
}
@Override
@NotNull
public List<Instruction> getDeadInstructions() {
List<Instruction> deadInstructions = Lists.newArrayList();
for (Instruction instruction : mutableInstructionList) {
if (isDead(instruction)) {
deadInstructions.add(instruction);
}
}
return deadInstructions;
}
private static boolean isDead(@NotNull Instruction instruction) {
if (!((InstructionImpl)instruction).getDead()) return false;
for (Instruction copy : instruction.getCopies()) {
if (!((InstructionImpl)copy).getDead()) return false;
}
return true;
public List<Instruction> getInstructionsIncludingDeadCode() {
return mutableInstructionList;
}
//for tests only
@@ -85,4 +85,12 @@ public class PseudocodeUtil {
}
return BindingContextUtils.extractVariableDescriptorIfAny(bindingContext, element, onlyReference);
}
public static boolean isDeadInAllCopies(@NotNull Instruction instruction) {
boolean allCopiesAreDead = instruction.getDead();
for (Instruction copy : instruction.getCopies()) {
allCopiesAreDead = allCopiesAreDead && copy.getDead();
}
return allCopiesAreDead;
}
}
@@ -26,6 +26,8 @@ public trait Instruction {
public val previousInstructions: MutableCollection<Instruction>
public val nextInstructions: Collection<Instruction>
public val dead: Boolean
public val lexicalScope: LexicalScope
public val inputValues: List<PseudoValue>
@@ -53,7 +53,7 @@ public abstract class InstructionImpl(public override val lexicalScope: LexicalS
_dead = true
}
public val dead: Boolean get() = _dead
override val dead: Boolean get() = _dead
public fun copy(): Instruction {
return updateCopyInfo(createCopy())
@@ -19,9 +19,8 @@ package org.jetbrains.jet.cfg;
import kotlin.Function3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.Instruction;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.InstructionImpl;
import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeImpl;
import org.jetbrains.jet.lang.cfg.pseudocode.instructions.Instruction;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.*;
@@ -34,7 +33,7 @@ public abstract class AbstractControlFlowTest extends AbstractPseudocodeTest {
@NotNull StringBuilder out,
@NotNull BindingContext bindingContext
) {
final int nextInstructionsColumnWidth = countNextInstructionsColumnWidth(pseudocode.getAllInstructions());
final int nextInstructionsColumnWidth = countNextInstructionsColumnWidth(pseudocode.getInstructionsIncludingDeadCode());
dumpInstructions(pseudocode, out, new Function3<Instruction, Instruction, Instruction, String>() {
@Override
@@ -107,9 +106,9 @@ public abstract class AbstractControlFlowTest extends AbstractPseudocodeTest {
@Override
protected void checkPseudocode(PseudocodeImpl pseudocode) {
//check edges directions
Collection<Instruction> instructions = pseudocode.getAllInstructions();
Collection<Instruction> instructions = pseudocode.getInstructionsIncludingDeadCode();
for (Instruction instruction : instructions) {
if (!((InstructionImpl)instruction).getDead()) {
if (!instruction.getDead()) {
for (Instruction nextInstruction : instruction.getNextInstructions()) {
assertTrue("instruction '" + instruction + "' has '" + nextInstruction + "' among next instructions list, but not vice versa",
nextInstruction.getPreviousInstructions().contains(instruction));
@@ -49,7 +49,7 @@ public abstract class AbstractDataFlowTest extends AbstractPseudocodeTest {
pseudocodeVariablesData.getVariableUseStatusData();
final String initPrefix = " INIT:";
final String usePrefix = " USE:";
final int initializersColumnWidth = countDataColumnWidth(initPrefix, pseudocode.getAllInstructions(), variableInitializers);
final int initializersColumnWidth = countDataColumnWidth(initPrefix, pseudocode.getInstructionsIncludingDeadCode(), variableInitializers);
dumpInstructions(pseudocode, out, new Function3<Instruction, Instruction, Instruction, String>() {
@Override
@@ -179,7 +179,7 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment {
@NotNull StringBuilder out,
@NotNull Function3<Instruction, /*next*/Instruction, /*prev*/Instruction, String> getInstructionData
) {
List<Instruction> instructions = pseudocode.getAllInstructions();
List<Instruction> instructions = pseudocode.getInstructionsIncludingDeadCode();
Set<Instruction> remainedAfterPostProcessInstructions = Sets.newHashSet(pseudocode.getInstructions());
List<PseudocodeImpl.PseudocodeLabel> labels = pseudocode.getLabels();
int instructionColumnWidth = countInstructionColumnWidth(instructions);
@@ -44,7 +44,7 @@ public class CFGraphToDotFilePrinter {
int[] count = new int[1];
Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
for (Pseudocode pseudocode : pseudocodes) {
dumpNodes(((PseudocodeImpl)pseudocode).getAllInstructions(), out, count, nodeToName, Sets
dumpNodes(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName, Sets
.newHashSet(pseudocode.getInstructions()));
}
int i = 0;
@@ -61,7 +61,7 @@ public class CFGraphToDotFilePrinter {
out.println("subgraph cluster_" + i + " {\n" +
"label=\"" + label + "\";\n" +
"color=blue;\n");
dumpEdges(((PseudocodeImpl)pseudocode).getAllInstructions(), out, count, nodeToName);
dumpEdges(pseudocode.getInstructionsIncludingDeadCode(), out, count, nodeToName);
out.println("}");
i++;
}
@@ -76,7 +76,7 @@ public class CFGraphToDotFilePrinter {
public void visitLocalFunctionDeclarationInstruction(LocalFunctionDeclarationInstruction instruction) {
int index = count[0];
// instruction.getBody().dumpSubgraph(out, "subgraph cluster_" + index, count, "color=blue;\nlabel = \"f" + index + "\";", nodeToName);
printEdge(out, nodeToName.get(instruction), nodeToName.get(((PseudocodeImpl)instruction.getBody()).getAllInstructions().get(0)), null);
printEdge(out, nodeToName.get(instruction), nodeToName.get(instruction.getBody().getInstructionsIncludingDeadCode().get(0)), null);
visitInstructionWithNext(instruction);
}