Search in sources :

Example 1 with IntegerValue

use of de.mirkosertic.bytecoder.ssa.IntegerValue 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 2 with IntegerValue

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

the class WASMSSAWriter method writeArrayStoreExpression.

private void writeArrayStoreExpression(ArrayStoreExpression aExpression) {
    List<Value> theIncomingData = aExpression.incomingDataFlows();
    Value theArray = theIncomingData.get(0);
    Value theIndex = theIncomingData.get(1);
    Value theValue = theIncomingData.get(2);
    // If the index is a constant, we can precompute the offset.
    if (theIndex instanceof IntegerValue) {
        int offset = 20 + ((IntegerValue) theIndex).getIntValue() * 4;
        switch(aExpression.getArrayType().resolve()) {
            case DOUBLE:
            case FLOAT:
                {
                    print("(f32.store ");
                    break;
                }
            default:
                {
                    print("(i32.store ");
                    break;
                }
        }
        print("offset=" + offset + " ");
        writeValue(theArray);
        print(" ");
        writeValue(theValue);
        println(")");
        return;
    }
    switch(aExpression.getArrayType().resolve()) {
        case DOUBLE:
        case FLOAT:
            {
                println("(f32.store offset=20 ");
                break;
            }
        default:
            {
                println("(i32.store offset=20 ");
                break;
            }
    }
    WASMSSAWriter theChild = withDeeperIndent();
    theChild.print("(i32.add ");
    theChild.writeValue(theArray);
    theChild.print(" (i32.mul ");
    theChild.writeValue(theIndex);
    theChild.print(" (i32.const 4)");
    theChild.println("))");
    theChild.writeValue(theValue);
    theChild.println();
    println(")");
}
Also used : IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) StringValue(de.mirkosertic.bytecoder.ssa.StringValue) ByteValue(de.mirkosertic.bytecoder.ssa.ByteValue) Value(de.mirkosertic.bytecoder.ssa.Value) ClassReferenceValue(de.mirkosertic.bytecoder.ssa.ClassReferenceValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) NullValue(de.mirkosertic.bytecoder.ssa.NullValue) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) ShortValue(de.mirkosertic.bytecoder.ssa.ShortValue)

Example 3 with IntegerValue

use of de.mirkosertic.bytecoder.ssa.IntegerValue 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 4 with IntegerValue

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

the class OpenCLWriter method printValue.

private void printValue(Value aValue) {
    if (aValue instanceof Variable) {
        Variable theVariable = (Variable) aValue;
        print(theVariable.getName());
    } else if (aValue instanceof InvokeVirtualMethodExpression) {
        printInvokeVirtual((InvokeVirtualMethodExpression) aValue);
    } else if (aValue instanceof InvokeStaticMethodExpression) {
        printInvokeStatic((InvokeStaticMethodExpression) aValue);
    } else if (aValue instanceof GetFieldExpression) {
        printGetFieldValue((GetFieldExpression) aValue);
    } else if (aValue instanceof ArrayEntryExpression) {
        printArrayEntryValue((ArrayEntryExpression) aValue);
    } else if (aValue instanceof BinaryExpression) {
        printBinaryValue((BinaryExpression) aValue);
    } else if (aValue instanceof IntegerValue) {
        printIntegerValue((IntegerValue) aValue);
    } else if (aValue instanceof LongValue) {
        printLongValue((LongValue) aValue);
    } else if (aValue instanceof FloatValue) {
        printFloatValue((FloatValue) aValue);
    } else if (aValue instanceof DoubleValue) {
        printDoubleValue((DoubleValue) aValue);
    } else if (aValue instanceof TypeConversionExpression) {
        printTypeConversionValue((TypeConversionExpression) aValue);
    } else if (aValue instanceof CompareExpression) {
        printCompareExpression((CompareExpression) aValue);
    } else if (aValue instanceof DirectInvokeMethodExpression) {
        printDirectInvokeMethodExpression((DirectInvokeMethodExpression) aValue);
    } else {
        throw new IllegalArgumentException("Not supported : " + aValue);
    }
}
Also used : Variable(de.mirkosertic.bytecoder.ssa.Variable) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) GetFieldExpression(de.mirkosertic.bytecoder.ssa.GetFieldExpression) CompareExpression(de.mirkosertic.bytecoder.ssa.CompareExpression) DirectInvokeMethodExpression(de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression) InvokeStaticMethodExpression(de.mirkosertic.bytecoder.ssa.InvokeStaticMethodExpression) InvokeVirtualMethodExpression(de.mirkosertic.bytecoder.ssa.InvokeVirtualMethodExpression) BinaryExpression(de.mirkosertic.bytecoder.ssa.BinaryExpression) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) TypeConversionExpression(de.mirkosertic.bytecoder.ssa.TypeConversionExpression) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) ArrayEntryExpression(de.mirkosertic.bytecoder.ssa.ArrayEntryExpression)

Aggregations

IntegerValue (de.mirkosertic.bytecoder.ssa.IntegerValue)4 BinaryExpression (de.mirkosertic.bytecoder.ssa.BinaryExpression)2 ControlFlowGraph (de.mirkosertic.bytecoder.ssa.ControlFlowGraph)2 DoubleValue (de.mirkosertic.bytecoder.ssa.DoubleValue)2 ExpressionList (de.mirkosertic.bytecoder.ssa.ExpressionList)2 FloatValue (de.mirkosertic.bytecoder.ssa.FloatValue)2 LongValue (de.mirkosertic.bytecoder.ssa.LongValue)2 Program (de.mirkosertic.bytecoder.ssa.Program)2 RegionNode (de.mirkosertic.bytecoder.ssa.RegionNode)2 Variable (de.mirkosertic.bytecoder.ssa.Variable)2 Test (org.junit.Test)2 BytecodeOpcodeAddress (de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress)1 ArrayEntryExpression (de.mirkosertic.bytecoder.ssa.ArrayEntryExpression)1 ByteValue (de.mirkosertic.bytecoder.ssa.ByteValue)1 ClassReferenceValue (de.mirkosertic.bytecoder.ssa.ClassReferenceValue)1 CompareExpression (de.mirkosertic.bytecoder.ssa.CompareExpression)1 DirectInvokeMethodExpression (de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression)1 GetFieldExpression (de.mirkosertic.bytecoder.ssa.GetFieldExpression)1 GotoExpression (de.mirkosertic.bytecoder.ssa.GotoExpression)1 IFExpression (de.mirkosertic.bytecoder.ssa.IFExpression)1