Skip to content

Commit

Permalink
changed CopyPropagator acc to definition, added tc
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilagichani14 committed Aug 16, 2024
1 parent e59c26c commit f4bab2a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/bodyinterceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ Obviously, the code segment<code>l2 = 2; l3 = 3;</code>is unreachable. It will b

### CopyPropagator

CopyPropagator is a<code>BodyInterceptor</code>that supports the global copy propagation and constant propagation.
CopyPropagator is a<code>BodyInterceptor</code>that supports the global copy propagation and constant propagation.

Refer [Sreekala, S. K. and Vineeth Kumar Paleri. “Copy Propagation subsumes Constant Propagation.” ArXiv abs/2207.03894 (2022): n. pag.](https://arxiv.org/pdf/2207.03894)

Example for global copy propagation:

Expand Down
Binary file not shown.
11 changes: 11 additions & 0 deletions shared-test-resources/interceptors/CopyPropagatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class CopyPropagatorTest {

void tc1(int w) {
int y, z = 0;
y = w;
w = 10;
z = 20;
int x = y + z;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package sootup.java.bytecode.interceptors;

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

import categories.TestCategories;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import sootup.core.graph.MutableStmtGraph;
Expand All @@ -28,6 +30,9 @@
import sootup.core.signatures.MethodSignature;
import sootup.core.types.VoidType;
import sootup.core.util.ImmutableUtils;
import sootup.core.util.Utils;
import sootup.core.views.View;
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.bytecode.inputlocation.PathBasedAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.interceptors.CopyPropagator;
Expand Down Expand Up @@ -128,6 +133,44 @@ public class CopyPropagatorTest {
JAssignStmt estmt13 =
JavaJimple.newAssignStmt(r5, NullConstant.getInstance(), noStmtPositionInfo);

public View setUp() {
String baseDir = "../shared-test-resources/interceptors/";
JavaClassPathAnalysisInputLocation inputLocation =
new JavaClassPathAnalysisInputLocation(
baseDir, SourceType.Library, Collections.emptyList());
final JavaView view = new JavaView(Arrays.asList(inputLocation));
return view;
}

@Test
public void testCopyPropagationWithRedefinition() {
View view = setUp();
final MethodSignature methodSignature =
view.getIdentifierFactory()
.getMethodSignature(
"CopyPropagatorTest", "tc1", "void", Collections.singletonList("int"));
Body bodyBefore = view.getMethod(methodSignature).get().getBody();
final Body.BodyBuilder builder = Body.builder(bodyBefore, Collections.emptySet());
new CopyPropagator().interceptBody(builder, view);
Body bodyAfter = builder.build();
assertEquals(
Stream.of(
"CopyPropagatorTest this",
"int l1",
"unknown l2, l3, l4",
"this := @this: CopyPropagatorTest",
"l1 := @parameter0: int",
"l3 = 0",
"l2 = l1",
"l1 = 10",
"l3 = 20",
// l2 should not be replaced with l1 as l1 gets redefined
"l4 = l2 + 20",
"return")
.collect(Collectors.toList()),
Utils.filterJimple(bodyAfter.toString()));
}

@Test
public void testEqualStmt() {
assertTrue(eestmt4.equivTo(eestmt4.withRValue(NullConstant.getInstance())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

import com.google.common.collect.Lists;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -91,7 +92,48 @@ else if (rhs instanceof JCastExpr && rhs.getType() instanceof ReferenceType) {
}
// if rhs is a local, then replace use, if it is possible
else if (rhs instanceof Local && !rhs.equivTo(use)) {
newStmt = replaceUse(stmtGraph, newStmt, use, rhs);
Local m = (Local) rhs;
if (use != m) {
Integer defCount = m.getDefs(stmtGraph.getStmts()).size();
if (defCount == null || defCount == 0) {
throw new RuntimeException("Variable " + m + " used without definition!");
} else if (defCount == 1) {
newStmt = replaceUse(stmtGraph, newStmt, use, rhs);
continue;
}

List<Stmt> path = stmtGraph.getExtendedBasicBlockPathBetween(defStmt, newStmt);
if (path == null) {
// no path in the extended basic block
continue;
}
{
boolean isRedefined = false;
Iterator<Stmt> pathIt = path.iterator();
// Skip first node
pathIt.next();
// Make sure that m is not redefined along path
while (pathIt.hasNext()) {
Stmt s = (Stmt) pathIt.next();
if (newStmt == s) {
// Don't look at the last statement
// since it is evaluated after the uses.
break;
}
if (s instanceof AbstractDefinitionStmt) {
if (((AbstractDefinitionStmt) s).getLeftOp() == m) {
isRedefined = true;
break;
}
}
}

if (isRedefined) {
continue;
}
}
newStmt = replaceUse(stmtGraph, newStmt, use, rhs);
}
}
}
}
Expand Down

0 comments on commit f4bab2a

Please sign in to comment.