Search in sources :

Example 1 with ControlFlowGraph

use of de.mirkosertic.bytecoder.ssa.ControlFlowGraph in project Bytecoder by mirkosertic.

the class InlineGotoOptimizerTest method testSimpleFlow.

@Test
public void testSimpleFlow() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = theProgram.getControlFlowGraph();
    RegionNode theNode = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    RegionNode theNode2 = theGraph.createAt(new BytecodeOpcodeAddress(20), RegionNode.BlockType.NORMAL);
    theNode.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(20)));
    theNode.addSuccessor(theNode2);
    theNode2.getExpressions().add(new ReturnExpression());
    theGraph.calculateReachabilityAndMarkBackEdges();
    InlineGotoOptimizer theOptimizer = new InlineGotoOptimizer();
    theOptimizer.optimize(theGraph, null);
    Assert.assertEquals(1, theGraph.getKnownNodes().size(), 0);
    Assert.assertEquals(1, theGraph.getDominatedNodes().size(), 0);
    Assert.assertTrue(theGraph.getKnownNodes().contains(theNode));
    Assert.assertEquals(1, theNode.getExpressions().size(), 0);
    Assert.assertTrue(theNode.getExpressions().toList().get(0) instanceof ReturnExpression);
    Assert.assertEquals(0, theNode.getSuccessors().size(), 0);
    Relooper theRelooper = new Relooper();
    Relooper.Block theRoot = theRelooper.reloop(theGraph);
}
Also used : ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) Program(de.mirkosertic.bytecoder.ssa.Program) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) Relooper(de.mirkosertic.bytecoder.relooper.Relooper) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) BytecodeOpcodeAddress(de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) Test(org.junit.Test)

Example 2 with ControlFlowGraph

use of de.mirkosertic.bytecoder.ssa.ControlFlowGraph in project Bytecoder by mirkosertic.

the class InlineGotoOptimizerTest method testIf.

@Test
public void testIf() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = theProgram.getControlFlowGraph();
    RegionNode theNode = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    RegionNode theNode1 = theGraph.createAt(new BytecodeOpcodeAddress(20), RegionNode.BlockType.NORMAL);
    RegionNode theNode2 = theGraph.createAt(new BytecodeOpcodeAddress(40), RegionNode.BlockType.NORMAL);
    RegionNode theNode3 = theGraph.createAt(new BytecodeOpcodeAddress(60), RegionNode.BlockType.NORMAL);
    theNode.addSuccessor(theNode1);
    theNode.addSuccessor(theNode2);
    ExpressionList theTrueClause = new ExpressionList();
    theTrueClause.add(new GotoExpression(new BytecodeOpcodeAddress(20)));
    IFExpression theIF = new IFExpression(new BytecodeOpcodeAddress(0), new BytecodeOpcodeAddress(20), new IntegerValue(10), theTrueClause);
    theNode.getExpressions().add(theIF);
    theNode.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(40)));
    theNode1.addSuccessor(theNode3);
    theNode1.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(60)));
    theNode2.addSuccessor(theNode3);
    theNode2.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(60)));
    theNode3.getExpressions().add(new ReturnExpression());
    theGraph.calculateReachabilityAndMarkBackEdges();
    InlineGotoOptimizer theOptimizer = new InlineGotoOptimizer();
    theOptimizer.optimize(theGraph, null);
    System.out.println(theGraph.toDOT());
}
Also used : ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) Program(de.mirkosertic.bytecoder.ssa.Program) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) BytecodeOpcodeAddress(de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress) IFExpression(de.mirkosertic.bytecoder.ssa.IFExpression) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) ExpressionList(de.mirkosertic.bytecoder.ssa.ExpressionList) Test(org.junit.Test)

Example 3 with ControlFlowGraph

use of de.mirkosertic.bytecoder.ssa.ControlFlowGraph in project Bytecoder by mirkosertic.

the class RelooperTest method testEndlessLoop.

@Test
public void testEndlessLoop() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = new ControlFlowGraph(theProgram);
    RegionNode theNode1 = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    theNode1.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(10)));
    RegionNode theNode2 = theGraph.createAt(new BytecodeOpcodeAddress(10), RegionNode.BlockType.NORMAL);
    theNode2.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(20)));
    RegionNode theNode3 = theGraph.createAt(new BytecodeOpcodeAddress(20), RegionNode.BlockType.NORMAL);
    theNode3.getExpressions().add(new GotoExpression(BytecodeOpcodeAddress.START_AT_ZERO));
    theNode1.addSuccessor(theNode2);
    theNode2.addSuccessor(theNode3);
    theNode3.addSuccessor(theNode1);
    theGraph.calculateReachabilityAndMarkBackEdges();
    Relooper theRelooper = new Relooper();
    Relooper.Block theBlock = theRelooper.reloop(theGraph);
    theRelooper.debugPrint(System.out, theBlock);
}
Also used : Program(de.mirkosertic.bytecoder.ssa.Program) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) BytecodeOpcodeAddress(de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) Test(org.junit.Test)

Example 4 with ControlFlowGraph

use of de.mirkosertic.bytecoder.ssa.ControlFlowGraph in project Bytecoder by mirkosertic.

the class RedundantAssignmentOptimizerTest method testBinaryOptimization.

@Test
public void testBinaryOptimization() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = new ControlFlowGraph(theProgram);
    RegionNode theStart = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    ExpressionList theExpressions = theStart.getExpressions();
    Variable theVariable1 = new Variable(TypeRef.Native.INT, "var1");
    Variable theVariable2 = new Variable(TypeRef.Native.INT, "var2");
    IntegerValue theInt1 = new IntegerValue(10);
    theVariable1.initializeWith(theInt1);
    theExpressions.add(new VariableAssignmentExpression(theVariable1, theInt1));
    IntegerValue theInt2 = new IntegerValue(20);
    theVariable2.initializeWith(theInt2);
    theExpressions.add(new VariableAssignmentExpression(theVariable2, theInt2));
    Variable theVariable3 = new Variable(TypeRef.Native.INT, "var3");
    BinaryExpression theBinary = new BinaryExpression(TypeRef.Native.INT, theVariable1, BinaryExpression.Operator.ADD, theVariable2);
    theVariable3.initializeWith(theBinary);
    theExpressions.add(new VariableAssignmentExpression(theVariable3, theBinary));
    theExpressions.add(new ReturnValueExpression(theVariable3));
    RedundantAssignmentOptimizer theOptimizer = new RedundantAssignmentOptimizer();
    theOptimizer.optimize(theGraph, null);
    System.out.println(theExpressions);
}
Also used : ReturnValueExpression(de.mirkosertic.bytecoder.ssa.ReturnValueExpression) Program(de.mirkosertic.bytecoder.ssa.Program) Variable(de.mirkosertic.bytecoder.ssa.Variable) BinaryExpression(de.mirkosertic.bytecoder.ssa.BinaryExpression) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) ExpressionList(de.mirkosertic.bytecoder.ssa.ExpressionList) VariableAssignmentExpression(de.mirkosertic.bytecoder.ssa.VariableAssignmentExpression) Test(org.junit.Test)

Example 5 with ControlFlowGraph

use of de.mirkosertic.bytecoder.ssa.ControlFlowGraph in project Bytecoder by mirkosertic.

the class InlineGotoOptimizerTest method testSimpleFlowTwoLevels.

@Test
public void testSimpleFlowTwoLevels() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = theProgram.getControlFlowGraph();
    RegionNode theNode = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    RegionNode theNode1 = theGraph.createAt(new BytecodeOpcodeAddress(20), RegionNode.BlockType.NORMAL);
    RegionNode theNode2 = theGraph.createAt(new BytecodeOpcodeAddress(40), RegionNode.BlockType.NORMAL);
    theNode.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(20)));
    theNode.addSuccessor(theNode1);
    theNode1.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(40)));
    theNode1.addSuccessor(theNode2);
    theNode2.getExpressions().add(new ReturnExpression());
    theGraph.calculateReachabilityAndMarkBackEdges();
    InlineGotoOptimizer theOptimizer = new InlineGotoOptimizer();
    theOptimizer.optimize(theGraph, null);
    Assert.assertEquals(1, theGraph.getKnownNodes().size(), 0);
    Assert.assertEquals(1, theGraph.getDominatedNodes().size(), 0);
    Assert.assertTrue(theGraph.getKnownNodes().contains(theNode));
    Assert.assertEquals(1, theNode.getExpressions().size(), 0);
    Assert.assertTrue(theNode.getExpressions().toList().get(0) instanceof ReturnExpression);
    Assert.assertEquals(0, theNode.getSuccessors().size(), 0);
    Relooper theRelooper = new Relooper();
    Relooper.Block theRoot = theRelooper.reloop(theGraph);
}
Also used : ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) Program(de.mirkosertic.bytecoder.ssa.Program) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) Relooper(de.mirkosertic.bytecoder.relooper.Relooper) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) BytecodeOpcodeAddress(de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) Test(org.junit.Test)

Aggregations

ControlFlowGraph (de.mirkosertic.bytecoder.ssa.ControlFlowGraph)9 Program (de.mirkosertic.bytecoder.ssa.Program)9 RegionNode (de.mirkosertic.bytecoder.ssa.RegionNode)9 Test (org.junit.Test)9 BytecodeOpcodeAddress (de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress)8 GotoExpression (de.mirkosertic.bytecoder.ssa.GotoExpression)6 ReturnExpression (de.mirkosertic.bytecoder.ssa.ReturnExpression)6 Relooper (de.mirkosertic.bytecoder.relooper.Relooper)2 ExpressionList (de.mirkosertic.bytecoder.ssa.ExpressionList)2 IntegerValue (de.mirkosertic.bytecoder.ssa.IntegerValue)2 BinaryExpression (de.mirkosertic.bytecoder.ssa.BinaryExpression)1 IFExpression (de.mirkosertic.bytecoder.ssa.IFExpression)1 ReturnValueExpression (de.mirkosertic.bytecoder.ssa.ReturnValueExpression)1 Variable (de.mirkosertic.bytecoder.ssa.Variable)1 VariableAssignmentExpression (de.mirkosertic.bytecoder.ssa.VariableAssignmentExpression)1