Java Code Examples for com.android.dx.rop.type.TypeBearer#isConstant()

The following examples show how to use com.android.dx.rop.type.TypeBearer#isConstant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiteralOpUpgrader.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: LiteralOpUpgrader.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: LiteralOpUpgrader.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: LiteralOpUpgrader.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: SCCP.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces TypeBearers in source register specs with constant type
 * bearers if possible. These are then referenced in later optimization
 * steps.
 */
private void replaceConstants() {
    for (int reg = 0; reg < regCount; reg++) {
        if (latticeValues[reg] != CONSTANT) {
            continue;
        }
        if (!(latticeConstants[reg] instanceof TypedConstant)) {
            // We can't do much with these
            continue;
        }

        SsaInsn defn = ssaMeth.getDefinitionForRegister(reg);
        TypeBearer typeBearer = defn.getResult().getTypeBearer();

        if (typeBearer.isConstant()) {
            /*
             * The definition was a constant already.
             * The uses should be as well.
             */
            continue;
        }

        // Update the destination RegisterSpec with the constant value
        RegisterSpec dest = defn.getResult();
        RegisterSpec newDest
                = dest.withType((TypedConstant)latticeConstants[reg]);
        defn.setResult(newDest);

        /*
         * Update the sources RegisterSpec's of all non-move uses.
         * These will be used in later steps.
         */
        for (SsaInsn insn : ssaMeth.getUseListForRegister(reg)) {
            if (insn.isPhiOrMove()) {
                continue;
            }

            NormalSsaInsn nInsn = (NormalSsaInsn) insn;
            RegisterSpecList sources = insn.getSources();

            int index = sources.indexOfRegister(reg);

            RegisterSpec spec = sources.get(index);
            RegisterSpec newSpec
                    = spec.withType((TypedConstant)latticeConstants[reg]);

            nInsn.changeOneSource(index, newSpec);
        }
    }
}
 
Example 6
Source File: ConstCollector.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Updates all uses of various consts to use the values in the newly
 * assigned registers.
 *
 * @param newRegs {@code non-null;} mapping between constant and new reg
 * @param origRegCount {@code >=0;} original SSA reg count, not including
 * newly added constant regs
 */
private void updateConstUses(HashMap<TypedConstant, RegisterSpec> newRegs,
        int origRegCount) {

    /*
     * set of constants associated with a local variable; used
     * only if COLLECT_ONE_LOCAL is true.
     */
    final HashSet<TypedConstant> usedByLocal
            = new HashSet<TypedConstant>();

    final ArrayList<SsaInsn>[] useList = ssaMeth.getUseListCopy();

    for (int i = 0; i < origRegCount; i++) {
        SsaInsn insn = ssaMeth.getDefinitionForRegister(i);

        if (insn == null) {
            continue;
        }

        final RegisterSpec origReg = insn.getResult();
        TypeBearer typeBearer = insn.getResult().getTypeBearer();

        if (!typeBearer.isConstant()) continue;

        TypedConstant cst = (TypedConstant) typeBearer;
        final RegisterSpec newReg = newRegs.get(cst);

        if (newReg == null) {
            continue;
        }

        if (ssaMeth.isRegALocal(origReg)) {
            if (!COLLECT_ONE_LOCAL) {
                continue;
            } else {
                /*
                 * TODO: If the same local gets the same cst
                 * multiple times, it would be nice to reuse the
                 * register.
                 */
                if (usedByLocal.contains(cst)) {
                    continue;
                } else {
                    usedByLocal.add(cst);
                    fixLocalAssignment(origReg, newRegs.get(cst));
                }
            }
        }

        // maps an original const register to the new collected register
        RegisterMapper mapper = new RegisterMapper() {
            @Override
            public int getNewRegisterCount() {
                return ssaMeth.getRegCount();
            }

            @Override
            public RegisterSpec map(RegisterSpec registerSpec) {
                if (registerSpec.getReg() == origReg.getReg()) {
                    return newReg.withLocalItem(
                            registerSpec.getLocalItem());
                }

                return registerSpec;
            }
        };

        for (SsaInsn use : useList[origReg.getReg()]) {
            if (use.canThrow()
                    && use.getBlock().getSuccessors().cardinality() > 1) {
                continue;
            }
            use.mapSourceRegisters(mapper);
        }
    }
}
 
Example 7
Source File: SCCP.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces TypeBearers in source register specs with constant type
 * bearers if possible. These are then referenced in later optimization
 * steps.
 */
private void replaceConstants() {
    for (int reg = 0; reg < regCount; reg++) {
        if (latticeValues[reg] != CONSTANT) {
            continue;
        }
        if (!(latticeConstants[reg] instanceof TypedConstant)) {
            // We can't do much with these
            continue;
        }

        SsaInsn defn = ssaMeth.getDefinitionForRegister(reg);
        TypeBearer typeBearer = defn.getResult().getTypeBearer();

        if (typeBearer.isConstant()) {
            /*
             * The definition was a constant already.
             * The uses should be as well.
             */
            continue;
        }

        // Update the destination RegisterSpec with the constant value
        RegisterSpec dest = defn.getResult();
        RegisterSpec newDest
                = dest.withType((TypedConstant)latticeConstants[reg]);
        defn.setResult(newDest);

        /*
         * Update the sources RegisterSpec's of all non-move uses.
         * These will be used in later steps.
         */
        for (SsaInsn insn : ssaMeth.getUseListForRegister(reg)) {
            if (insn.isPhiOrMove()) {
                continue;
            }

            NormalSsaInsn nInsn = (NormalSsaInsn) insn;
            RegisterSpecList sources = insn.getSources();

            int index = sources.indexOfRegister(reg);

            RegisterSpec spec = sources.get(index);
            RegisterSpec newSpec
                    = spec.withType((TypedConstant)latticeConstants[reg]);

            nInsn.changeOneSource(index, newSpec);
        }
    }
}
 
Example 8
Source File: ConstCollector.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Updates all uses of various consts to use the values in the newly
 * assigned registers.
 *
 * @param newRegs {@code non-null;} mapping between constant and new reg
 * @param origRegCount {@code >=0;} original SSA reg count, not including
 * newly added constant regs
 */
private void updateConstUses(HashMap<TypedConstant, RegisterSpec> newRegs,
        int origRegCount) {

    /*
     * set of constants associated with a local variable; used
     * only if COLLECT_ONE_LOCAL is true.
     */
    final HashSet<TypedConstant> usedByLocal
            = new HashSet<TypedConstant>();

    final ArrayList<SsaInsn>[] useList = ssaMeth.getUseListCopy();

    for (int i = 0; i < origRegCount; i++) {
        SsaInsn insn = ssaMeth.getDefinitionForRegister(i);

        if (insn == null) {
            continue;
        }

        final RegisterSpec origReg = insn.getResult();
        TypeBearer typeBearer = insn.getResult().getTypeBearer();

        if (!typeBearer.isConstant()) continue;

        TypedConstant cst = (TypedConstant) typeBearer;
        final RegisterSpec newReg = newRegs.get(cst);

        if (newReg == null) {
            continue;
        }

        if (ssaMeth.isRegALocal(origReg)) {
            if (!COLLECT_ONE_LOCAL) {
                continue;
            } else {
                /*
                 * TODO: If the same local gets the same cst
                 * multiple times, it would be nice to reuse the
                 * register.
                 */
                if (usedByLocal.contains(cst)) {
                    continue;
                } else {
                    usedByLocal.add(cst);
                    fixLocalAssignment(origReg, newRegs.get(cst));
                }
            }
        }

        // maps an original const register to the new collected register
        RegisterMapper mapper = new RegisterMapper() {
            @Override
            public int getNewRegisterCount() {
                return ssaMeth.getRegCount();
            }

            @Override
            public RegisterSpec map(RegisterSpec registerSpec) {
                if (registerSpec.getReg() == origReg.getReg()) {
                    return newReg.withLocalItem(
                            registerSpec.getLocalItem());
                }

                return registerSpec;
            }
        };

        for (SsaInsn use : useList[origReg.getReg()]) {
            if (use.canThrow()
                    && use.getBlock().getSuccessors().cardinality() > 1) {
                continue;
            }
            use.mapSourceRegisters(mapper);
        }
    }
}
 
Example 9
Source File: SCCP.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces TypeBearers in source register specs with constant type
 * bearers if possible. These are then referenced in later optimization
 * steps.
 */
private void replaceConstants() {
    for (int reg = 0; reg < regCount; reg++) {
        if (latticeValues[reg] != CONSTANT) {
            continue;
        }
        if (!(latticeConstants[reg] instanceof TypedConstant)) {
            // We can't do much with these
            continue;
        }

        SsaInsn defn = ssaMeth.getDefinitionForRegister(reg);
        TypeBearer typeBearer = defn.getResult().getTypeBearer();

        if (typeBearer.isConstant()) {
            /*
             * The definition was a constant already.
             * The uses should be as well.
             */
            continue;
        }

        // Update the destination RegisterSpec with the constant value
        RegisterSpec dest = defn.getResult();
        RegisterSpec newDest
                = dest.withType((TypedConstant)latticeConstants[reg]);
        defn.setResult(newDest);

        /*
         * Update the sources RegisterSpec's of all non-move uses.
         * These will be used in later steps.
         */
        for (SsaInsn insn : ssaMeth.getUseListForRegister(reg)) {
            if (insn.isPhiOrMove()) {
                continue;
            }

            NormalSsaInsn nInsn = (NormalSsaInsn) insn;
            RegisterSpecList sources = insn.getSources();

            int index = sources.indexOfRegister(reg);

            RegisterSpec spec = sources.get(index);
            RegisterSpec newSpec
                    = spec.withType((TypedConstant)latticeConstants[reg]);

            nInsn.changeOneSource(index, newSpec);
        }
    }
}
 
Example 10
Source File: ConstCollector.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Updates all uses of various consts to use the values in the newly
 * assigned registers.
 *
 * @param newRegs {@code non-null;} mapping between constant and new reg
 * @param origRegCount {@code >=0;} original SSA reg count, not including
 * newly added constant regs
 */
private void updateConstUses(HashMap<TypedConstant, RegisterSpec> newRegs,
        int origRegCount) {

    /*
     * set of constants associated with a local variable; used
     * only if COLLECT_ONE_LOCAL is true.
     */
    final HashSet<TypedConstant> usedByLocal
            = new HashSet<TypedConstant>();

    final ArrayList<SsaInsn>[] useList = ssaMeth.getUseListCopy();

    for (int i = 0; i < origRegCount; i++) {
        SsaInsn insn = ssaMeth.getDefinitionForRegister(i);

        if (insn == null) {
            continue;
        }

        final RegisterSpec origReg = insn.getResult();
        TypeBearer typeBearer = insn.getResult().getTypeBearer();

        if (!typeBearer.isConstant()) continue;

        TypedConstant cst = (TypedConstant) typeBearer;
        final RegisterSpec newReg = newRegs.get(cst);

        if (newReg == null) {
            continue;
        }

        if (ssaMeth.isRegALocal(origReg)) {
            if (!COLLECT_ONE_LOCAL) {
                continue;
            } else {
                /*
                 * TODO: If the same local gets the same cst
                 * multiple times, it would be nice to reuse the
                 * register.
                 */
                if (usedByLocal.contains(cst)) {
                    continue;
                } else {
                    usedByLocal.add(cst);
                    fixLocalAssignment(origReg, newRegs.get(cst));
                }
            }
        }

        // maps an original const register to the new collected register
        RegisterMapper mapper = new RegisterMapper() {
            @Override
            public int getNewRegisterCount() {
                return ssaMeth.getRegCount();
            }

            @Override
            public RegisterSpec map(RegisterSpec registerSpec) {
                if (registerSpec.getReg() == origReg.getReg()) {
                    return newReg.withLocalItem(
                            registerSpec.getLocalItem());
                }

                return registerSpec;
            }
        };

        for (SsaInsn use : useList[origReg.getReg()]) {
            if (use.canThrow()
                    && use.getBlock().getSuccessors().cardinality() > 1) {
                continue;
            }
            use.mapSourceRegisters(mapper);
        }
    }
}
 
Example 11
Source File: SCCP.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces TypeBearers in source register specs with constant type
 * bearers if possible. These are then referenced in later optimization
 * steps.
 */
private void replaceConstants() {
    for (int reg = 0; reg < regCount; reg++) {
        if (latticeValues[reg] != CONSTANT) {
            continue;
        }
        if (!(latticeConstants[reg] instanceof TypedConstant)) {
            // We can't do much with these
            continue;
        }

        SsaInsn defn = ssaMeth.getDefinitionForRegister(reg);
        TypeBearer typeBearer = defn.getResult().getTypeBearer();

        if (typeBearer.isConstant()) {
            /*
             * The definition was a constant already.
             * The uses should be as well.
             */
            continue;
        }

        // Update the destination RegisterSpec with the constant value
        RegisterSpec dest = defn.getResult();
        RegisterSpec newDest
                = dest.withType((TypedConstant)latticeConstants[reg]);
        defn.setResult(newDest);

        /*
         * Update the sources RegisterSpec's of all non-move uses.
         * These will be used in later steps.
         */
        for (SsaInsn insn : ssaMeth.getUseListForRegister(reg)) {
            if (insn.isPhiOrMove()) {
                continue;
            }

            NormalSsaInsn nInsn = (NormalSsaInsn) insn;
            RegisterSpecList sources = insn.getSources();

            int index = sources.indexOfRegister(reg);

            RegisterSpec spec = sources.get(index);
            RegisterSpec newSpec
                    = spec.withType((TypedConstant)latticeConstants[reg]);

            nInsn.changeOneSource(index, newSpec);
        }
    }
}
 
Example 12
Source File: ConstCollector.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Updates all uses of various consts to use the values in the newly
 * assigned registers.
 *
 * @param newRegs {@code non-null;} mapping between constant and new reg
 * @param origRegCount {@code >=0;} original SSA reg count, not including
 * newly added constant regs
 */
private void updateConstUses(HashMap<TypedConstant, RegisterSpec> newRegs,
        int origRegCount) {

    /*
     * set of constants associated with a local variable; used
     * only if COLLECT_ONE_LOCAL is true.
     */
    final HashSet<TypedConstant> usedByLocal
            = new HashSet<TypedConstant>();

    final ArrayList<SsaInsn>[] useList = ssaMeth.getUseListCopy();

    for (int i = 0; i < origRegCount; i++) {
        SsaInsn insn = ssaMeth.getDefinitionForRegister(i);

        if (insn == null) {
            continue;
        }

        final RegisterSpec origReg = insn.getResult();
        TypeBearer typeBearer = insn.getResult().getTypeBearer();

        if (!typeBearer.isConstant()) continue;

        TypedConstant cst = (TypedConstant) typeBearer;
        final RegisterSpec newReg = newRegs.get(cst);

        if (newReg == null) {
            continue;
        }

        if (ssaMeth.isRegALocal(origReg)) {
            if (!COLLECT_ONE_LOCAL) {
                continue;
            } else {
                /*
                 * TODO: If the same local gets the same cst
                 * multiple times, it would be nice to reuse the
                 * register.
                 */
                if (usedByLocal.contains(cst)) {
                    continue;
                } else {
                    usedByLocal.add(cst);
                    fixLocalAssignment(origReg, newRegs.get(cst));
                }
            }
        }

        // maps an original const register to the new collected register
        RegisterMapper mapper = new RegisterMapper() {
            @Override
            public int getNewRegisterCount() {
                return ssaMeth.getRegCount();
            }

            @Override
            public RegisterSpec map(RegisterSpec registerSpec) {
                if (registerSpec.getReg() == origReg.getReg()) {
                    return newReg.withLocalItem(
                            registerSpec.getLocalItem());
                }

                return registerSpec;
            }
        };

        for (SsaInsn use : useList[origReg.getReg()]) {
            if (use.canThrow()
                    && use.getBlock().getSuccessors().cardinality() > 1) {
                continue;
            }
            use.mapSourceRegisters(mapper);
        }
    }
}