Skip to content

Commit

Permalink
Remove ToppedSetFact and replace it by real universal set
Browse files Browse the repository at this point in the history
  • Loading branch information
silverbullettt committed Nov 3, 2023
1 parent 89c3357 commit 5d45ab8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 215 deletions.
Expand Up @@ -25,7 +25,6 @@
import pascal.taie.analysis.dataflow.analysis.AbstractDataflowAnalysis;
import pascal.taie.analysis.dataflow.analysis.AnalysisDriver;
import pascal.taie.analysis.dataflow.fact.SetFact;
import pascal.taie.analysis.dataflow.fact.ToppedSetFact;
import pascal.taie.analysis.graph.cfg.CFG;
import pascal.taie.config.AnalysisConfig;
import pascal.taie.ir.exp.BinaryExp;
Expand All @@ -36,6 +35,9 @@
import pascal.taie.ir.exp.Var;
import pascal.taie.ir.stmt.DefinitionStmt;
import pascal.taie.ir.stmt.Stmt;
import pascal.taie.util.Indexer;
import pascal.taie.util.SimpleIndexer;
import pascal.taie.util.collection.IndexerBitSet;

/**
* Available expression analysis on local variables.
Expand All @@ -61,8 +63,18 @@ protected Analysis makeAnalysis(CFG<Stmt> cfg) {

private static class Analysis extends AbstractDataflowAnalysis<Stmt, SetFact<ExpWrapper>> {

private final Indexer<ExpWrapper> expIndexer;

/**
* Set of all expressions in cfg.
* Used to fast create universal set (via copy).
*/
private final SetFact<ExpWrapper> universalSet;

private Analysis(CFG<Stmt> cfg) {
super(cfg);
expIndexer = new SimpleIndexer<>();
universalSet = computeUniversalSet(cfg, expIndexer);
}

@Override
Expand All @@ -72,12 +84,12 @@ public boolean isForward() {

@Override
public SetFact<ExpWrapper> newBoundaryFact() {
return new ToppedSetFact<>(false);
return new SetFact<>(new IndexerBitSet<>(expIndexer, false));
}

@Override
public SetFact<ExpWrapper> newInitialFact() {
return new ToppedSetFact<>(true);
return universalSet.copy();
}

@Override
Expand All @@ -87,21 +99,16 @@ public void meetInto(SetFact<ExpWrapper> fact, SetFact<ExpWrapper> target) {

@Override
public boolean transferNode(Stmt stmt, SetFact<ExpWrapper> in, SetFact<ExpWrapper> out) {
if (((ToppedSetFact<ExpWrapper>) in).isTop()) {
// valid data facts have not arrived yet, just skip and return
// true to ensure that the successor Stmts will be analyzed later
return true;
}
SetFact<ExpWrapper> oldOut = out.copy();
out.set(in);
if (stmt instanceof DefinitionStmt) {
Exp lvalue = ((DefinitionStmt<?, ?>) stmt).getLValue();
if (stmt instanceof DefinitionStmt<?, ?> defStmt) {
Exp lvalue = defStmt.getLValue();
if (lvalue instanceof Var defVar) {
// kill affected expressions
out.removeIf(expWrapper ->
expWrapper.get().getUses().contains(defVar));
}
Exp rvalue = ((DefinitionStmt<?, ?>) stmt).getRValue();
Exp rvalue = defStmt.getRValue();
if (isRelevant(rvalue)) {
// generate available expressions
out.add(new ExpWrapper(rvalue));
Expand All @@ -110,6 +117,22 @@ public boolean transferNode(Stmt stmt, SetFact<ExpWrapper> in, SetFact<ExpWrappe
return !out.equals(oldOut);
}

/**
* @return a set containing all (relevant) expressions in {@code cfg}.
*/
private static SetFact<ExpWrapper> computeUniversalSet(
CFG<Stmt> cfg, Indexer<ExpWrapper> expIndexer) {
SetFact<ExpWrapper> set = new SetFact<>(
new IndexerBitSet<>(expIndexer, false));
cfg.forEach(stmt -> {
if (stmt instanceof DefinitionStmt<?,?> defStmt
&& isRelevant(defStmt.getRValue())) {
set.add(new ExpWrapper(defStmt.getRValue()));
}
});
return set;
}

/**
* Checks if an expression is relevant to available expressions.
* We only consider these expressions as available expressions.
Expand Down
179 changes: 0 additions & 179 deletions src/main/java/pascal/taie/analysis/dataflow/fact/ToppedSetFact.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/test/java/pascal/taie/analysis/dataflow/fact/FactTest.java
Expand Up @@ -25,37 +25,12 @@
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;


public class FactTest {

@Test
void testToppedSetFact() {
ToppedSetFact<Integer> top = new ToppedSetFact<>(true);
ToppedSetFact<Integer> fact1 = top.copy();
fact1.intersect(top);
assertTrue(fact1.isTop());

ToppedSetFact<Integer> fact2 = new ToppedSetFact<>(List.of(1, 2, 3));
fact1.intersect(fact2);
assertFalse(fact1.isTop());
fact2.union(top);
assertTrue(fact2.isTop());

ToppedSetFact<Integer> fact3 = new ToppedSetFact<>(List.of(8, 9, 10));
fact2.union(fact3);
assertTrue(fact2.isTop());
fact2.intersect(top);
assertTrue(fact2.isTop());
fact2.intersect(fact3);
assertFalse(fact2.isTop());
}

@Test
void testUnionNormal() {
// Union overlapped sets
Expand Down

0 comments on commit 5d45ab8

Please sign in to comment.