Search in sources :

Example 6 with RopMethod

use of com.android.dx.rop.code.RopMethod in project buck by facebook.

the class Optimizer method optimize.

/**
     * Runs optimization algorthims over this method, and returns a new
     * instance of RopMethod with the changes.
     *
     * @param rmeth method to process
     * @param paramWidth the total width, in register-units, of this method's
     * parameters
     * @param isStatic true if this method has no 'this' pointer argument.
     * @param inPreserveLocals true if local variable info should be preserved,
     * at the cost of some registers and insns
     * @param inAdvice {@code non-null;} translation advice
     * @param steps set of optional optimization steps to run
     * @return optimized method
     */
public static RopMethod optimize(RopMethod rmeth, int paramWidth, boolean isStatic, boolean inPreserveLocals, TranslationAdvice inAdvice, EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth = null;
    preserveLocals = inPreserveLocals;
    advice = inAdvice;
    ssaMeth = SsaConverter.convertToSsaMethod(rmeth, paramWidth, isStatic);
    runSsaFormSteps(ssaMeth, steps);
    RopMethod resultMeth = SsaToRop.convertToRopMethod(ssaMeth, false);
    if (resultMeth.getBlocks().getRegCount() > advice.getMaxOptimalRegisterCount()) {
        // Try to see if we can squeeze it under the register count bar
        resultMeth = optimizeMinimizeRegisters(rmeth, paramWidth, isStatic, steps);
    }
    return resultMeth;
}
Also used : RopMethod(com.android.dx.rop.code.RopMethod)

Example 7 with RopMethod

use of com.android.dx.rop.code.RopMethod in project buck by facebook.

the class Optimizer method optimizeMinimizeRegisters.

/**
     * Runs the optimizer with a strategy to minimize the number of rop-form
     * registers used by the end result. Dex bytecode does not have instruction
     * forms that take register numbers larger than 15 for all instructions.
     * If we've produced a method that uses more than 16 registers, try again
     * with a different strategy to see if we can get under the bar. The end
     * result will be much more efficient.
     *
     * @param rmeth method to process
     * @param paramWidth the total width, in register-units, of this method's
     * parameters
     * @param isStatic true if this method has no 'this' pointer argument.
     * @param steps set of optional optimization steps to run
     * @return optimized method
     */
private static RopMethod optimizeMinimizeRegisters(RopMethod rmeth, int paramWidth, boolean isStatic, EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth;
    RopMethod resultMeth;
    ssaMeth = SsaConverter.convertToSsaMethod(rmeth, paramWidth, isStatic);
    EnumSet<OptionalStep> newSteps = steps.clone();
    /*
         * CONST_COLLECTOR trades insns for registers, which is not an
         * appropriate strategy here.
         */
    newSteps.remove(OptionalStep.CONST_COLLECTOR);
    runSsaFormSteps(ssaMeth, newSteps);
    resultMeth = SsaToRop.convertToRopMethod(ssaMeth, true);
    return resultMeth;
}
Also used : RopMethod(com.android.dx.rop.code.RopMethod)

Example 8 with RopMethod

use of com.android.dx.rop.code.RopMethod in project J2ME-Loader by nikita36078.

the class IdenticalBlockCombiner method process.

/**
 * Runs algorithm. TODO: This is n^2, and could be made linear-ish with
 * a hash. In particular, hash the contents of each block and only
 * compare blocks with the same hash.
 *
 * @return {@code non-null;} new method that has been processed
 */
public RopMethod process() {
    int szBlocks = blocks.size();
    // indexed by label
    BitSet toDelete = new BitSet(blocks.getMaxLabel());
    // For each non-deleted block...
    for (int bindex = 0; bindex < szBlocks; bindex++) {
        BasicBlock b = blocks.get(bindex);
        if (toDelete.get(b.getLabel())) {
            // doomed block
            continue;
        }
        IntList preds = ropMethod.labelToPredecessors(b.getLabel());
        // ...look at all of it's predecessors that have only one succ...
        int szPreds = preds.size();
        for (int i = 0; i < szPreds; i++) {
            int iLabel = preds.get(i);
            BasicBlock iBlock = blocks.labelToBlock(iLabel);
            if (toDelete.get(iLabel) || iBlock.getSuccessors().size() > 1 || iBlock.getFirstInsn().getOpcode().getOpcode() == RegOps.MOVE_RESULT) {
                continue;
            }
            IntList toCombine = new IntList();
            // ...and see if they can be combined with any other preds...
            for (int j = i + 1; j < szPreds; j++) {
                int jLabel = preds.get(j);
                BasicBlock jBlock = blocks.labelToBlock(jLabel);
                if (jBlock.getSuccessors().size() == 1 && compareInsns(iBlock, jBlock)) {
                    toCombine.add(jLabel);
                    toDelete.set(jLabel);
                }
            }
            combineBlocks(iLabel, toCombine);
        }
    }
    for (int i = szBlocks - 1; i >= 0; i--) {
        if (toDelete.get(newBlocks.get(i).getLabel())) {
            newBlocks.set(i, null);
        }
    }
    newBlocks.shrinkToFit();
    newBlocks.setImmutable();
    return new RopMethod(newBlocks, ropMethod.getFirstLabel());
}
Also used : RopMethod(com.android.dx.rop.code.RopMethod) BitSet(java.util.BitSet) BasicBlock(com.android.dx.rop.code.BasicBlock) IntList(com.android.dx.util.IntList)

Example 9 with RopMethod

use of com.android.dx.rop.code.RopMethod in project J2ME-Loader by nikita36078.

the class SsaToRop method convert.

/**
 * Performs the conversion.
 *
 * @return {@code non-null;} rop-form output
 */
private RopMethod convert() {
    if (DEBUG) {
        interference.dumpToStdout();
    }
    // These are other allocators for debugging or historical comparison:
    // allocator = new NullRegisterAllocator(ssaMeth, interference);
    // allocator = new FirstFitAllocator(ssaMeth, interference);
    RegisterAllocator allocator = new FirstFitLocalCombiningAllocator(ssaMeth, interference, minimizeRegisters);
    RegisterMapper mapper = allocator.allocateRegisters();
    if (DEBUG) {
        System.out.println("Printing reg map");
        System.out.println(((BasicRegisterMapper) mapper).toHuman());
    }
    ssaMeth.setBackMode();
    ssaMeth.mapRegisters(mapper);
    removePhiFunctions();
    if (allocator.wantsParamsMovedHigh()) {
        moveParametersToHighRegisters();
    }
    removeEmptyGotos();
    RopMethod ropMethod = new RopMethod(convertBasicBlocks(), ssaMeth.blockIndexToRopLabel(ssaMeth.getEntryBlockIndex()));
    ropMethod = new IdenticalBlockCombiner(ropMethod).process();
    return ropMethod;
}
Also used : RegisterMapper(com.android.dx.ssa.RegisterMapper) BasicRegisterMapper(com.android.dx.ssa.BasicRegisterMapper) RopMethod(com.android.dx.rop.code.RopMethod)

Example 10 with RopMethod

use of com.android.dx.rop.code.RopMethod in project buck by facebook.

the class IdenticalBlockCombiner method process.

/**
     * Runs algorithm. TODO: This is n^2, and could be made linear-ish with
     * a hash. In particular, hash the contents of each block and only
     * compare blocks with the same hash.
     *
     * @return {@code non-null;} new method that has been processed
     */
public RopMethod process() {
    int szBlocks = blocks.size();
    // indexed by label
    BitSet toDelete = new BitSet(blocks.getMaxLabel());
    // For each non-deleted block...
    for (int bindex = 0; bindex < szBlocks; bindex++) {
        BasicBlock b = blocks.get(bindex);
        if (toDelete.get(b.getLabel())) {
            // doomed block
            continue;
        }
        IntList preds = ropMethod.labelToPredecessors(b.getLabel());
        // ...look at all of it's predecessors that have only one succ...
        int szPreds = preds.size();
        for (int i = 0; i < szPreds; i++) {
            int iLabel = preds.get(i);
            BasicBlock iBlock = blocks.labelToBlock(iLabel);
            if (toDelete.get(iLabel) || iBlock.getSuccessors().size() > 1 || iBlock.getFirstInsn().getOpcode().getOpcode() == RegOps.MOVE_RESULT) {
                continue;
            }
            IntList toCombine = new IntList();
            // ...and see if they can be combined with any other preds...
            for (int j = i + 1; j < szPreds; j++) {
                int jLabel = preds.get(j);
                BasicBlock jBlock = blocks.labelToBlock(jLabel);
                if (jBlock.getSuccessors().size() == 1 && compareInsns(iBlock, jBlock)) {
                    toCombine.add(jLabel);
                    toDelete.set(jLabel);
                }
            }
            combineBlocks(iLabel, toCombine);
        }
    }
    for (int i = szBlocks - 1; i >= 0; i--) {
        if (toDelete.get(newBlocks.get(i).getLabel())) {
            newBlocks.set(i, null);
        }
    }
    newBlocks.shrinkToFit();
    newBlocks.setImmutable();
    return new RopMethod(newBlocks, ropMethod.getFirstLabel());
}
Also used : RopMethod(com.android.dx.rop.code.RopMethod) BitSet(java.util.BitSet) BasicBlock(com.android.dx.rop.code.BasicBlock) IntList(com.android.dx.util.IntList)

Aggregations

RopMethod (com.android.dx.rop.code.RopMethod)17 DexTranslationAdvice (com.android.dx.rop.code.DexTranslationAdvice)5 TranslationAdvice (com.android.dx.rop.code.TranslationAdvice)5 IntList (com.android.dx.util.IntList)5 ConcreteMethod (com.android.dx.cf.code.ConcreteMethod)4 Method (com.android.dx.cf.iface.Method)4 BasicBlock (com.android.dx.rop.code.BasicBlock)4 BasicBlockList (com.android.dx.rop.code.BasicBlockList)4 BitSet (java.util.BitSet)3 MethodList (com.android.dx.cf.iface.MethodList)2 DalvCode (com.android.dx.dex.code.DalvCode)2 EncodedMethod (com.android.dx.dex.file.EncodedMethod)2 Annotations (com.android.dx.rop.annotation.Annotations)2 AnnotationsList (com.android.dx.rop.annotation.AnnotationsList)2 LocalVariableInfo (com.android.dx.rop.code.LocalVariableInfo)2 CstMethodRef (com.android.dx.rop.cst.CstMethodRef)2 CstString (com.android.dx.rop.cst.CstString)2 CstType (com.android.dx.rop.cst.CstType)2 TypeList (com.android.dx.rop.type.TypeList)2 BasicRegisterMapper (com.android.dx.ssa.BasicRegisterMapper)2