refactor CFG to separate instructions from CFGs; this is to allow code like CDGs to be reused without an IR.

This commit is contained in:
Julian Dolby 2016-09-26 13:37:51 -04:00
parent 9b5845034f
commit f3b5d41af9
7 changed files with 78 additions and 52 deletions

View File

@ -108,7 +108,7 @@ public class PDFControlDependenceGraph {
}
System.err.println(ir.toString());
ControlDependenceGraph<SSAInstruction, ISSABasicBlock> cdg = new ControlDependenceGraph<SSAInstruction, ISSABasicBlock>(ir.getControlFlowGraph());
ControlDependenceGraph<ISSABasicBlock> cdg = new ControlDependenceGraph<ISSABasicBlock>(ir.getControlFlowGraph());
Properties wp = null;
try {

View File

@ -20,17 +20,7 @@ import com.ibm.wala.util.intset.BitVector;
/**
* An interface that is common to the Shrike and SSA CFG implementations.
*/
public interface ControlFlowGraph<I, T extends IBasicBlock<I>> extends NumberedGraph<T> {
/**
* Return the entry basic block in the CFG
*/
public T entry();
/**
* @return the synthetic exit block for the cfg
*/
public T exit();
public interface ControlFlowGraph<I, T extends IBasicBlock<I>> extends NumberedGraph<T>, MinimalCFG<T> {
/**
* @return the indices of the catch blocks, as a bit vector
@ -60,33 +50,4 @@ public interface ControlFlowGraph<I, T extends IBasicBlock<I>> extends NumberedG
* @return the Method this CFG represents
*/
public IMethod getMethod();
/**
* The order of blocks returned must indicate the exception-handling scope. So the first block is the first candidate catch block,
* and so on. With this invariant one can compute the exceptional control flow for a given exception type.
*
* @return the basic blocks which may be reached from b via exceptional control flow
*/
public List<T> getExceptionalSuccessors(T b);
/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks which may be reached from b via normal control flow
*/
public Collection<T> getNormalSuccessors(T b);
/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via exceptional control flow
*/
public Collection<T> getExceptionalPredecessors(T b);
/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via normal control flow
*/
public Collection<T> getNormalPredecessors(T b);
}

View File

@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.cfg;
import java.util.Collection;
import java.util.List;
import com.ibm.wala.util.graph.NumberedGraph;
public interface MinimalCFG<T> extends NumberedGraph<T> {
/**
* Return the entry basic block in the CFG
*/
public T entry();
/**
* @return the synthetic exit block for the cfg
*/
public T exit();
/**
* The order of blocks returned must indicate the exception-handling scope. So the first block is the first candidate catch block,
* and so on. With this invariant one can compute the exceptional control flow for a given exception type.
*
* @return the basic blocks which may be reached from b via exceptional control flow
*/
public List<T> getExceptionalSuccessors(T b);
/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks which may be reached from b via normal control flow
*/
public Collection<T> getNormalSuccessors(T b);
/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via exceptional control flow
*/
public Collection<T> getExceptionalPredecessors(T b);
/**
* The order of blocks returned should be arbitrary but deterministic.
*
* @return the basic blocks from which b may be reached via normal control flow
*/
public Collection<T> getNormalPredecessors(T b);
}

View File

@ -15,8 +15,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.ibm.wala.cfg.ControlFlowGraph;
import com.ibm.wala.cfg.IBasicBlock;
import com.ibm.wala.cfg.MinimalCFG;
import com.ibm.wala.util.collections.EmptyIterator;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.collections.HashSetFactory;
@ -33,12 +32,12 @@ import com.ibm.wala.util.intset.MutableIntSet;
/**
* Control Dependence Graph
*/
public class ControlDependenceGraph<I, T extends IBasicBlock<I>> extends AbstractNumberedGraph<T> {
public class ControlDependenceGraph<T> extends AbstractNumberedGraph<T> {
/**
* Governing control flow-graph. The control dependence graph is computed from this cfg.
*/
private final ControlFlowGraph<I, T> cfg;
private final MinimalCFG<T> cfg;
/**
* the EdgeManager for the CDG. It implements the edge part of the standard Graph abstraction, using the control-dependence edges
@ -127,7 +126,7 @@ public class ControlDependenceGraph<I, T extends IBasicBlock<I>> extends Abstrac
MutableIntSet x = IntSetUtil.make();
if (backwardEdges.containsKey(node)) {
for(T pred : backwardEdges.get(node)) {
x.add(pred.getNumber());
x.add(cfg.getNumber(pred));
}
}
return x;
@ -154,7 +153,7 @@ public class ControlDependenceGraph<I, T extends IBasicBlock<I>> extends Abstrac
MutableIntSet x = IntSetUtil.make();
if (forwardEdges.containsKey(node)) {
for(T succ : forwardEdges.get(node)) {
x.add(succ.getNumber());
x.add(cfg.getNumber(succ));
}
}
return x;
@ -223,7 +222,7 @@ public class ControlDependenceGraph<I, T extends IBasicBlock<I>> extends Abstrac
* @param cfg governing control flow graph
* @param wantEdgeLabels whether to compute edge labels for CDG edges
*/
public ControlDependenceGraph(ControlFlowGraph<I, T> cfg, boolean wantEdgeLabels) {
public ControlDependenceGraph(MinimalCFG<T> cfg, boolean wantEdgeLabels) {
if (cfg == null) {
throw new IllegalArgumentException("null cfg");
}
@ -234,11 +233,11 @@ public class ControlDependenceGraph<I, T extends IBasicBlock<I>> extends Abstrac
/**
* @param cfg governing control flow graph
*/
public ControlDependenceGraph(ControlFlowGraph<I, T> cfg) {
public ControlDependenceGraph(MinimalCFG<T> cfg) {
this(cfg, false);
}
public ControlFlowGraph getControlFlowGraph() {
public MinimalCFG getControlFlowGraph() {
return cfg;
}

View File

@ -251,7 +251,7 @@ public class PDG<T extends InstanceKey> implements NumberedGraph<Statement> {
Assertions.productionAssertion(cOptions.equals(ControlDependenceOptions.FULL));
}
ControlDependenceGraph<SSAInstruction, ISSABasicBlock> cdg = new ControlDependenceGraph<SSAInstruction, ISSABasicBlock>(
ControlDependenceGraph<ISSABasicBlock> cdg = new ControlDependenceGraph<ISSABasicBlock>(
controlFlowGraph);
for (ISSABasicBlock bb : cdg) {
if (bb.isExitBlock()) {

View File

@ -16,7 +16,7 @@ import java.util.Set;
import com.ibm.wala.util.graph.AbstractNumberedGraph;
import com.ibm.wala.util.intset.IntSet;
public abstract class AbstractNumberedLabeledGraph<T, U> extends AbstractNumberedGraph<T> implements LabeledGraph<T, U> {
public abstract class AbstractNumberedLabeledGraph<T, U> extends AbstractNumberedGraph<T> implements LabeledGraph<T, U>, NumberedLabeledGraph<T, U> {
/**
* @return the object which manages edges in the graph

View File

@ -0,0 +1,7 @@
package com.ibm.wala.util.graph.labeled;
import com.ibm.wala.util.graph.NumberedGraph;
public interface NumberedLabeledGraph<T, I> extends NumberedGraph<T>, NumberedLabeledEdgeManager<T,I> {
}