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