Search in sources :

Example 16 with TypeBearer

use of com.taobao.android.dx.rop.type.TypeBearer in project atlas by alibaba.

the class BaseMachine method getLocalTarget.

/**
     * Gets the target local register spec of the current operation, if any.
     * The local target spec is the combination of the values indicated
     * by a previous call to {@link #localTarget} with the type of what
     * should be the sole result set by a call to {@link #setResult} (or
     * the combination {@link #clearResult} then {@link #addResult}.
     *
     * @param isMove {@code true} if the operation being performed on the
     * local is a move. This will cause constant values to be propagated
     * to the returned local
     * @return {@code null-ok;} the salient register spec or {@code null} if no
     * local target was set since the last time {@link #clearArgs} was
     * called
     */
protected final RegisterSpec getLocalTarget(boolean isMove) {
    if (localTarget == null) {
        return null;
    }
    if (resultCount != 1) {
        throw new SimException("local target with " + ((resultCount == 0) ? "no" : "multiple") + " results");
    }
    TypeBearer result = results[0];
    Type resultType = result.getType();
    Type localType = localTarget.getType();
    if (resultType == localType) {
        /*
             * If this is to be a move operation and the result is a
             * known value, make the returned localTarget embody that
             * value.
             */
        if (isMove) {
            return localTarget.withType(result);
        } else {
            return localTarget;
        }
    }
    if (!Merger.isPossiblyAssignableFrom(localType, resultType)) {
        // The result and local types are inconsistent. Complain!
        throwLocalMismatch(resultType, localType);
        return null;
    }
    if (localType == Type.OBJECT) {
        /*
             * The result type is more specific than the local type,
             * so use that instead.
             */
        localTarget = localTarget.withType(result);
    }
    return localTarget;
}
Also used : Type(com.taobao.android.dx.rop.type.Type) TypeBearer(com.taobao.android.dx.rop.type.TypeBearer)

Example 17 with TypeBearer

use of com.taobao.android.dx.rop.type.TypeBearer in project atlas by alibaba.

the class PlainInsn method withSourceLiteral.

/** {@inheritDoc} */
@Override
public Insn withSourceLiteral() {
    RegisterSpecList sources = getSources();
    int szSources = sources.size();
    if (szSources == 0) {
        return this;
    }
    TypeBearer lastType = sources.get(szSources - 1).getTypeBearer();
    if (!lastType.isConstant()) {
        // Check for reverse subtraction, where first source is constant
        TypeBearer firstType = sources.get(0).getTypeBearer();
        if (szSources == 2 && firstType.isConstant()) {
            Constant cst = (Constant) firstType;
            RegisterSpecList newSources = sources.withoutFirst();
            Rop newRop = Rops.ropFor(getOpcode().getOpcode(), getResult(), newSources, cst);
            return new PlainCstInsn(newRop, getPosition(), getResult(), newSources, cst);
        }
        return this;
    } else {
        Constant cst = (Constant) lastType;
        RegisterSpecList newSources = sources.withoutLast();
        Rop newRop;
        try {
            // Check for constant subtraction and flip it to be addition
            int opcode = getOpcode().getOpcode();
            if (opcode == RegOps.SUB && cst instanceof CstInteger) {
                opcode = RegOps.ADD;
                cst = CstInteger.make(-((CstInteger) cst).getValue());
            }
            newRop = Rops.ropFor(opcode, getResult(), newSources, cst);
        } catch (IllegalArgumentException ex) {
            // There's no rop for this case
            return this;
        }
        return new PlainCstInsn(newRop, getPosition(), getResult(), newSources, cst);
    }
}
Also used : Constant(com.taobao.android.dx.rop.cst.Constant) CstInteger(com.taobao.android.dx.rop.cst.CstInteger) TypeBearer(com.taobao.android.dx.rop.type.TypeBearer)

Example 18 with TypeBearer

use of com.taobao.android.dx.rop.type.TypeBearer in project atlas by alibaba.

the class RegisterSpec method withSimpleType.

/**
     * Returns an instance that is identical to this one, except that
     * the type bearer is replaced by the actual underlying type
     * (thereby stripping off non-type information) with any
     * initialization information stripped away as well.
     *
     * @return {@code non-null;} an appropriately-constructed instance
     */
public RegisterSpec withSimpleType() {
    TypeBearer orig = type;
    Type newType;
    if (orig instanceof Type) {
        newType = (Type) orig;
    } else {
        newType = orig.getType();
    }
    if (newType.isUninitialized()) {
        newType = newType.getInitializedType();
    }
    if (newType == orig) {
        return this;
    }
    return makeLocalOptional(reg, newType, local);
}
Also used : Type(com.taobao.android.dx.rop.type.Type) TypeBearer(com.taobao.android.dx.rop.type.TypeBearer)

Example 19 with TypeBearer

use of com.taobao.android.dx.rop.type.TypeBearer in project atlas by alibaba.

the class ConstCollector method getConstsSortedByCountUse.

/**
     * Gets all of the collectable constant values used in this method,
     * sorted by most used first. Skips non-collectable consts, such as
     * non-string object constants
     *
     * @return {@code non-null;} list of constants in most-to-least used order
     */
private ArrayList<TypedConstant> getConstsSortedByCountUse() {
    int regSz = ssaMeth.getRegCount();
    final HashMap<TypedConstant, Integer> countUses = new HashMap<TypedConstant, Integer>();
    /*
         * Each collected constant can be used by just one local
         * (used only if COLLECT_ONE_LOCAL is true).
         */
    final HashSet<TypedConstant> usedByLocal = new HashSet<TypedConstant>();
    // Count how many times each const value is used.
    for (int i = 0; i < regSz; i++) {
        SsaInsn insn = ssaMeth.getDefinitionForRegister(i);
        if (insn == null || insn.getOpcode() == null)
            continue;
        RegisterSpec result = insn.getResult();
        TypeBearer typeBearer = result.getTypeBearer();
        if (!typeBearer.isConstant())
            continue;
        TypedConstant cst = (TypedConstant) typeBearer;
        // Find defining instruction for move-result-pseudo instructions
        if (insn.getOpcode().getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
            int pred = insn.getBlock().getPredecessors().nextSetBit(0);
            ArrayList<SsaInsn> predInsns;
            predInsns = ssaMeth.getBlocks().get(pred).getInsns();
            insn = predInsns.get(predInsns.size() - 1);
        }
        if (insn.canThrow()) {
            /*
                 * Don't move anything other than strings -- the risk
                 * of changing where an exception is thrown is too high.
                 */
            if (!(cst instanceof CstString) || !COLLECT_STRINGS) {
                continue;
            }
            /*
                 * We can't move any throwable const whose throw will be
                 * caught, so don't count them.
                 */
            if (insn.getBlock().getSuccessors().cardinality() > 1) {
                continue;
            }
        }
        /*
             * TODO: Might be nice to try and figure out which local
             * wins most when collected.
             */
        if (ssaMeth.isRegALocal(result)) {
            if (!COLLECT_ONE_LOCAL) {
                continue;
            } else {
                if (usedByLocal.contains(cst)) {
                    // Count one local usage only.
                    continue;
                } else {
                    usedByLocal.add(cst);
                }
            }
        }
        Integer has = countUses.get(cst);
        if (has == null) {
            countUses.put(cst, 1);
        } else {
            countUses.put(cst, has + 1);
        }
    }
    // Collect constants that have been reused.
    ArrayList<TypedConstant> constantList = new ArrayList<TypedConstant>();
    for (Map.Entry<TypedConstant, Integer> entry : countUses.entrySet()) {
        if (entry.getValue() > 1) {
            constantList.add(entry.getKey());
        }
    }
    // Sort by use, with most used at the beginning of the list.
    Collections.sort(constantList, new Comparator<Constant>() {

        public int compare(Constant a, Constant b) {
            int ret;
            ret = countUses.get(b) - countUses.get(a);
            if (ret == 0) {
                /*
                     * Provide sorting determinisim for constants with same
                     * usage count.
                     */
                ret = a.compareTo(b);
            }
            return ret;
        }

        @Override
        public boolean equals(Object obj) {
            return obj == this;
        }
    });
    return constantList;
}
Also used : HashMap(java.util.HashMap) Constant(com.taobao.android.dx.rop.cst.Constant) TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant) CstString(com.taobao.android.dx.rop.cst.CstString) ArrayList(java.util.ArrayList) TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant) TypeBearer(com.taobao.android.dx.rop.type.TypeBearer) HashMap(java.util.HashMap) Map(java.util.Map) RegisterSpec(com.taobao.android.dx.rop.code.RegisterSpec) HashSet(java.util.HashSet)

Aggregations

TypeBearer (com.taobao.android.dx.rop.type.TypeBearer)19 RegisterSpec (com.taobao.android.dx.rop.code.RegisterSpec)7 Type (com.taobao.android.dx.rop.type.Type)5 Constant (com.taobao.android.dx.rop.cst.Constant)4 TypedConstant (com.taobao.android.dx.rop.cst.TypedConstant)4 Insn (com.taobao.android.dx.rop.code.Insn)3 PlainCstInsn (com.taobao.android.dx.rop.code.PlainCstInsn)3 PlainInsn (com.taobao.android.dx.rop.code.PlainInsn)3 RegisterSpecList (com.taobao.android.dx.rop.code.RegisterSpecList)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 FillArrayDataInsn (com.taobao.android.dx.rop.code.FillArrayDataInsn)2 Rop (com.taobao.android.dx.rop.code.Rop)2 ThrowingCstInsn (com.taobao.android.dx.rop.code.ThrowingCstInsn)2 ThrowingInsn (com.taobao.android.dx.rop.code.ThrowingInsn)2 CstInteger (com.taobao.android.dx.rop.cst.CstInteger)2 CstLiteralBits (com.taobao.android.dx.rop.cst.CstLiteralBits)2 SourcePosition (com.taobao.android.dx.rop.code.SourcePosition)1 SwitchInsn (com.taobao.android.dx.rop.code.SwitchInsn)1 CstFieldRef (com.taobao.android.dx.rop.cst.CstFieldRef)1