Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cutoff recomputation in NodeList #211

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ public void removeNode(N node) {
cutoff.remove(target);
}

nodes.remove(node);
// need to shift all successive cutoff back by one
List<Integer> interesting = cutoff.stream().filter(i -> i >= target).sorted().collect(Collectors.toList());
cutoff.removeAll(interesting);
interesting.forEach(i -> cutoff.add(i - 1));

if (target != 0)
if (target != nodes.size() - 1) {
if (target != nodes.size()) { // we don't remove 1 since we want to
// compare wrt the 'old' position
N pred = nodes.get(target - 1);
N succ = nodes.get(target + 1);
N succ = nodes.get(target); // before was at target+1
NodeEdges<G, N, E> predEdges = extraEdges.get(pred);
if (predEdges != null) {
E seq = sequentialSingleton.newInstance(pred, succ);
Expand All @@ -184,12 +191,6 @@ public void removeNode(N node) {
cutoff.add(target - 1);
}

nodes.remove(node);
// need to shift all successive cutoff back by one
List<Integer> interesting = cutoff.stream().filter(i -> i > target).sorted().collect(Collectors.toList());
cutoff.removeAll(interesting);
interesting.forEach(i -> cutoff.add(i - 1));

recomputeOffsets();
}

Expand Down Expand Up @@ -252,19 +253,20 @@ public void removeEdge(E e) {
if (e.isUnconditional() && src == dest - 1)
// just add the cutoff
cutoff.add(src);
else {
NodeEdges<G, N, E> edges = extraEdges.get(e.getSource());
if (edges != null) {
edges.outgoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getSource());
}
edges = extraEdges.get(e.getDestination());
if (edges != null) {
edges.ingoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getDestination());
}

// the edge might still be inside the extraEdges
// if this method has been invoked by removeNode
NodeEdges<G, N, E> edges = extraEdges.get(e.getSource());
if (edges != null) {
edges.outgoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getSource());
}
edges = extraEdges.get(e.getDestination());
if (edges != null) {
edges.ingoing.remove(e);
if (edges.ingoing.isEmpty() && edges.outgoing.isEmpty())
extraEdges.remove(e.getDestination());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,38 @@ public void testSimplificationAtTheEndWithBranch() throws ProgramValidationExcep
ControlFlowStructure act = first.getControlFlowStructures().iterator().next();
assertEquals("Simplification did not update control flow structures", exp, act);
}

@Test
public void testIssue210() throws ProgramValidationException {
SourceCodeLocation unknownLocation = new SourceCodeLocation("fake", 0, 0);
SourceCodeLocation unknownLocation2 = new SourceCodeLocation("fake", 0, 1);
CompilationUnit unit = new CompilationUnit(unknownLocation, "foo", false);
CFG first = new CFG(new CFGDescriptor(unknownLocation, unit, true, "foo"));
Assignment assign = new Assignment(first, unknownLocation, new VariableRef(first, unknownLocation, "x"),
new Int32Literal(first, unknownLocation, 5));
GreaterThan gt = new GreaterThan(first, unknownLocation, new VariableRef(first, unknownLocation, "x"),
new Int32Literal(first, unknownLocation, 2));
NoOp noop = new NoOp(first, unknownLocation);
Length print1 = new Length(first, unknownLocation, new StringLiteral(first, unknownLocation, "f"));
Return ret1 = new Return(first, unknownLocation, new VariableRef(first, unknownLocation, "x"));

Length print2 = new Length(first, unknownLocation2, new StringLiteral(first, unknownLocation2, "f"));
Return ret2 = new Return(first, unknownLocation2, new VariableRef(first, unknownLocation2, "f"));

first.addNode(assign, true);
first.addNode(gt);
first.addNode(noop);
first.addNode(print1);
first.addNode(ret1);
first.addNode(print2);
first.addNode(ret2);
first.addEdge(new SequentialEdge(assign, gt));
first.addEdge(new TrueEdge(gt, print1));
first.addEdge(new SequentialEdge(print1, ret1));
first.addEdge(new FalseEdge(gt, print2));
first.addEdge(new SequentialEdge(print2, ret2));

first.simplify();
first.validate();
}
}