refactored refinement policies for better code sharing
git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2496 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
parent
2fc1dd3442
commit
f8df8a93c7
|
@ -50,7 +50,7 @@ public class AlwaysRefineCGPolicy implements CallGraphRefinePolicy {
|
|||
return true;
|
||||
}
|
||||
|
||||
public boolean startNewPass() {
|
||||
public boolean nextPass() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -61,6 +61,6 @@ public interface CallGraphRefinePolicy {
|
|||
* @return <code>true</code> if more refinement can be done, and hence
|
||||
* another pass can be attempted; <code>false</code> otherwise
|
||||
*/
|
||||
public boolean startNewPass();
|
||||
public boolean nextPass();
|
||||
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ public class ManualCGRefinePolicy implements CallGraphRefinePolicy {
|
|||
return true;
|
||||
}
|
||||
|
||||
public boolean startNewPass() {
|
||||
public boolean nextPass() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,42 +44,26 @@ import com.ibm.wala.ipa.cha.ClassHierarchy;
|
|||
* @author Manu Sridharan
|
||||
*
|
||||
*/
|
||||
public class ManualRefinementPolicy implements RefinementPolicy {
|
||||
public class ManualRefinementPolicy extends AbstractRefinementPolicy {
|
||||
|
||||
private static final int PASS_BUDGET = 12000;
|
||||
|
||||
private static final int NUM_PASSES = 1;
|
||||
|
||||
private final FieldRefinePolicy fieldRefinePolicy;
|
||||
|
||||
private final CallGraphRefinePolicy callGraphRefinePolicy;
|
||||
|
||||
private ManualRefinementPolicy(ClassHierarchy cha) {
|
||||
fieldRefinePolicy = new ManualFieldPolicy(cha);
|
||||
callGraphRefinePolicy = new AlwaysRefineCGPolicy();
|
||||
super(new ManualFieldPolicy(cha), new AlwaysRefineCGPolicy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBudgetForPass(int passNum) {
|
||||
return PASS_BUDGET;
|
||||
}
|
||||
|
||||
public CallGraphRefinePolicy getCallGraphRefinePolicy() {
|
||||
return callGraphRefinePolicy;
|
||||
}
|
||||
|
||||
public FieldRefinePolicy getFieldRefinePolicy() {
|
||||
return fieldRefinePolicy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumPasses() {
|
||||
return NUM_PASSES;
|
||||
}
|
||||
|
||||
public boolean nextPass() {
|
||||
boolean moreFieldRefine = fieldRefinePolicy.nextPass();
|
||||
boolean moreCallRefine = callGraphRefinePolicy.startNewPass();
|
||||
return moreFieldRefine || moreCallRefine;
|
||||
}
|
||||
|
||||
public static class Factory implements RefinementPolicyFactory {
|
||||
private final ClassHierarchy cha;
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class NeverRefineCGPolicy implements CallGraphRefinePolicy {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean startNewPass() {
|
||||
public boolean nextPass() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,43 +43,32 @@ package com.ibm.wala.demandpa.alg.refinepolicy;
|
|||
* @author Manu Sridharan
|
||||
*
|
||||
*/
|
||||
public class SinglePassRefinementPolicy implements RefinementPolicy {
|
||||
|
||||
private final FieldRefinePolicy fieldRefinePolicy;
|
||||
|
||||
private final CallGraphRefinePolicy cgRefinePolicy;
|
||||
public class SinglePassRefinementPolicy extends AbstractRefinementPolicy {
|
||||
|
||||
private final int budget;
|
||||
|
||||
private SinglePassRefinementPolicy(FieldRefinePolicy fieldRefinePolicy, CallGraphRefinePolicy cgRefinePolicy, int budget) {
|
||||
this.fieldRefinePolicy = fieldRefinePolicy;
|
||||
this.cgRefinePolicy = cgRefinePolicy;
|
||||
super(fieldRefinePolicy, cgRefinePolicy);
|
||||
this.budget = budget;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return traversal budget
|
||||
*/
|
||||
@Override
|
||||
public int getBudgetForPass(int passNum) {
|
||||
return budget;
|
||||
}
|
||||
|
||||
public CallGraphRefinePolicy getCallGraphRefinePolicy() {
|
||||
return cgRefinePolicy;
|
||||
}
|
||||
|
||||
public FieldRefinePolicy getFieldRefinePolicy() {
|
||||
return fieldRefinePolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return <code>1</code>, since we only want a single pass
|
||||
*/
|
||||
@Override
|
||||
public int getNumPasses() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean nextPass() {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -37,118 +37,19 @@
|
|||
*/
|
||||
package com.ibm.wala.demandpa.alg.refinepolicy;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.ibm.wala.classLoader.IClass;
|
||||
import com.ibm.wala.classLoader.IField;
|
||||
import com.ibm.wala.demandpa.util.ArrayContents;
|
||||
import com.ibm.wala.ipa.cha.ClassHierarchy;
|
||||
import com.ibm.wala.types.ClassLoaderReference;
|
||||
import com.ibm.wala.types.TypeReference;
|
||||
import com.ibm.wala.util.collections.HashSetFactory;
|
||||
|
||||
/**
|
||||
* A refinement policy that iteratively adds more types to refine, based on
|
||||
* which type was encountered first in each analysis pass.
|
||||
* A refinement policy that iteratively adds more types to refine, based on which type was encountered first in each
|
||||
* analysis pass.
|
||||
*
|
||||
* @author Manu Sridharan
|
||||
*
|
||||
*/
|
||||
public class TunedRefinementPolicy implements RefinementPolicy {
|
||||
|
||||
private final ClassHierarchy cha;
|
||||
|
||||
private class TunedFieldRefinementPolicy implements FieldRefinePolicy {
|
||||
|
||||
private final Collection<IClass> typesToRefine = HashSetFactory.make();
|
||||
|
||||
private IClass firstSkippedClass = null;
|
||||
|
||||
public boolean nextPass() {
|
||||
if (firstSkippedClass != null) {
|
||||
typesToRefine.add(firstSkippedClass);
|
||||
firstSkippedClass = null;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldRefine(IField field) {
|
||||
if (field == ArrayContents.v()) {
|
||||
return true;
|
||||
}
|
||||
IClass classToCheck = removeInner(field.getDeclaringClass());
|
||||
if (superOfAnyEncountered(classToCheck)) {
|
||||
return true;
|
||||
} else {
|
||||
if (firstSkippedClass == null) {
|
||||
firstSkippedClass = classToCheck;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean superOfAnyEncountered(IClass klass) {
|
||||
for (IClass toRefine : typesToRefine) {
|
||||
if (cha.isAssignableFrom(klass, toRefine)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param klass
|
||||
* @return the top-level {@link IClass} where klass is declared, or klass
|
||||
* itself if klass is top-level or if top-level class not loaded
|
||||
*/
|
||||
private IClass removeInner(IClass klass) {
|
||||
ClassLoaderReference cl = klass.getClassLoader().getReference();
|
||||
String klassStr = klass.getName().toString();
|
||||
int dollarIndex = klassStr.indexOf('$');
|
||||
if (dollarIndex == -1) {
|
||||
return klass;
|
||||
} else {
|
||||
String topMostName = klassStr.substring(0, dollarIndex);
|
||||
IClass topMostClass = cha.lookupClass(TypeReference.findOrCreate(cl, topMostName));
|
||||
return (topMostClass != null) ? topMostClass : klass;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final CallGraphRefinePolicy cgRefinePolicy = new AlwaysRefineCGPolicy();
|
||||
|
||||
private final FieldRefinePolicy fieldRefinePolicy = new TunedFieldRefinementPolicy();
|
||||
|
||||
private static final int NUM_PASSES = 3;
|
||||
|
||||
private static final int[] BUDGET_PER_PASS = { 1000, 12000, 12000 };
|
||||
public class TunedRefinementPolicy extends AbstractRefinementPolicy {
|
||||
|
||||
public TunedRefinementPolicy(ClassHierarchy cha) {
|
||||
this.cha = cha;
|
||||
}
|
||||
|
||||
public int getBudgetForPass(int passNum) {
|
||||
return BUDGET_PER_PASS[passNum];
|
||||
}
|
||||
|
||||
public CallGraphRefinePolicy getCallGraphRefinePolicy() {
|
||||
return cgRefinePolicy;
|
||||
}
|
||||
|
||||
public FieldRefinePolicy getFieldRefinePolicy() {
|
||||
return fieldRefinePolicy;
|
||||
}
|
||||
|
||||
public int getNumPasses() {
|
||||
return NUM_PASSES;
|
||||
}
|
||||
|
||||
public boolean nextPass() {
|
||||
return fieldRefinePolicy.nextPass();
|
||||
super(new TunedFieldRefinementPolicy(cha), new AlwaysRefineCGPolicy());
|
||||
}
|
||||
|
||||
public static class Factory implements RefinementPolicyFactory {
|
||||
|
|
Loading…
Reference in New Issue