Java Code Examples for com.android.dx.rop.code.RegisterSpecList#size()

The following examples show how to use com.android.dx.rop.code.RegisterSpecList#size() . 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: Form22c.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    if (!((insn instanceof CstInsn) &&
          (regs.size() == 2) &&
          unsignedFitsInNibble(regs.get(0).getReg()) &&
          unsignedFitsInNibble(regs.get(1).getReg()))) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    Constant cst = ci.getConstant();
    return (cst instanceof CstType) ||
        (cst instanceof CstFieldRef);
}
 
Example 2
Source File: Form11n.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();

    if (!((insn instanceof CstInsn) &&
          (regs.size() == 1) &&
          unsignedFitsInNibble(regs.get(0).getReg()))) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    Constant cst = ci.getConstant();

    if (!(cst instanceof CstLiteralBits)) {
        return false;
    }

    CstLiteralBits cb = (CstLiteralBits) cst;

    return cb.fitsInInt() && signedFitsInNibble(cb.getIntBits());
}
 
Example 3
Source File: InsnFormat.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to determine if a list of registers are sequential,
 * including degenerate cases for empty or single-element lists.
 *
 * @param list {@code non-null;} the list of registers
 * @return {@code true} iff the list is sequentially ordered
 */
protected static boolean isRegListSequential(RegisterSpecList list) {
    int sz = list.size();

    if (sz < 2) {
        return true;
    }

    int first = list.get(0).getReg();
    int next = first;

    for (int i = 0; i < sz; i++) {
        RegisterSpec one = list.get(i);
        if (one.getReg() != next) {
            return false;
        }
        next += one.getCategory();
    }

    return true;
}
 
Example 4
Source File: HighRegisterPrefix.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #codeSize} and {@link #writeTo} which sets up
 * {@link #insns} if not already done.
 */
private void calculateInsnsIfNecessary() {
    if (insns != null) {
        return;
    }

    RegisterSpecList registers = getRegisters();
    int sz = registers.size();

    insns = new SimpleInsn[sz];

    for (int i = 0, outAt = 0; i < sz; i++) {
      RegisterSpec src = registers.get(i);
      insns[i] = moveInsnFor(src, outAt);
      outAt += src.getCategory();
    }
}
 
Example 5
Source File: HighRegisterPrefix.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected String listingString0(boolean noteIndices) {
    RegisterSpecList registers = getRegisters();
    int sz = registers.size();
    StringBuffer sb = new StringBuffer(100);

    for (int i = 0, outAt = 0; i < sz; i++) {
        RegisterSpec src = registers.get(i);
        SimpleInsn insn = moveInsnFor(src, outAt);

        if (i != 0) {
            sb.append('\n');
        }

        sb.append(insn.listingString0(noteIndices));

        outAt += src.getCategory();
    }

    return sb.toString();
}
 
Example 6
Source File: NormalSsaInsn.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Changes one of the insn's sources. New source should be of same type
 * and category.
 *
 * @param index {@code >=0;} index of source to change
 * @param newSpec spec for new source
 */
public final void changeOneSource(int index, RegisterSpec newSpec) {
    RegisterSpecList origSources = insn.getSources();
    int sz = origSources.size();
    RegisterSpecList newSources = new RegisterSpecList(sz);

    for (int i = 0; i < sz; i++) {
        newSources.set(i, i == index ? newSpec : origSources.get(i));
    }

    newSources.setImmutable();

    RegisterSpec origSpec = origSources.get(index);
    if (origSpec.getReg() != newSpec.getReg()) {
        /*
         * If the register remains unchanged, we're only changing
         * the type or local var name so don't update use list
         */
        getBlock().getParent().onSourceChanged(this, origSpec, newSpec);
    }

    insn = insn.withNewRegisters(getResult(), newSources);
}
 
Example 7
Source File: Form21s.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    if (!((insn instanceof CstInsn) &&
          (regs.size() == 1) &&
          unsignedFitsInByte(regs.get(0).getReg()))) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    Constant cst = ci.getConstant();

    if (!(cst instanceof CstLiteralBits)) {
        return false;
    }

    CstLiteralBits cb = (CstLiteralBits) cst;

    return cb.fitsInInt() && signedFitsInShort(cb.getIntBits());
}
 
Example 8
Source File: Form22b.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();
    if (!((insn instanceof CstInsn) &&
          (regs.size() == 2) &&
          unsignedFitsInByte(regs.get(0).getReg()) &&
          unsignedFitsInByte(regs.get(1).getReg()))) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    Constant cst = ci.getConstant();

    if (!(cst instanceof CstLiteralBits)) {
        return false;
    }

    CstLiteralBits cb = (CstLiteralBits) cst;

    return cb.fitsInInt() && signedFitsInByte(cb.getIntBits());
}
 
Example 9
Source File: DeadCodeRemover.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all instructions from every unreachable block.
 */
private void pruneDeadInstructions() {
    HashSet<SsaInsn> deletedInsns = new HashSet<SsaInsn>();

    ssaMeth.computeReachability();

    for (SsaBasicBlock block : ssaMeth.getBlocks()) {
        if (block.isReachable()) continue;

        // Prune instructions from unreachable blocks
        for (int i = 0; i < block.getInsns().size(); i++) {
            SsaInsn insn = block.getInsns().get(i);
            RegisterSpecList sources = insn.getSources();
            int sourcesSize = sources.size();

            // Delete this instruction completely if it has sources
            if (sourcesSize != 0) {
                deletedInsns.add(insn);
            }

            // Delete this instruction from all usage lists.
            for (int j = 0; j < sourcesSize; j++) {
                RegisterSpec source = sources.get(j);
                useList[source.getReg()].remove(insn);
            }

            // Remove this instruction result from the sources of any phis
            RegisterSpec result = insn.getResult();
            if (result == null) continue;
            for (SsaInsn use : useList[result.getReg()]) {
                if (use instanceof PhiInsn) {
                    PhiInsn phiUse = (PhiInsn) use;
                    phiUse.removePhiRegister(result);
                }
            }
        }
    }

    ssaMeth.deleteInsns(deletedInsns);
}
 
Example 10
Source File: NormalSsaInsn.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the source list of the insn. New source list should be the
 * same size and consist of sources of identical types.
 *
 * @param newSources non-null new sources list.
 */
public final void setNewSources (RegisterSpecList newSources) {
    RegisterSpecList origSources = insn.getSources();

    if (origSources.size() != newSources.size()) {
        throw new RuntimeException("Sources counts don't match");
    }

    insn = insn.withNewRegisters(getResult(), newSources);
}
 
Example 11
Source File: NormalSsaInsn.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the source list of the insn. New source list should be the
 * same size and consist of sources of identical types.
 *
 * @param newSources non-null new sources list.
 */
public final void setNewSources (RegisterSpecList newSources) {
    RegisterSpecList origSources = insn.getSources();

    if (origSources.size() != newSources.size()) {
        throw new RuntimeException("Sources counts don't match");
    }

    insn = insn.withNewRegisters(getResult(), newSources);
}
 
Example 12
Source File: SsaMethod.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a given {@code insn} from the use lists for the given
 * {@code oldSources} (rather than the sources currently
 * returned by insn.getSources()).
 *
 * @param insn {@code non-null;} insn in question
 * @param oldSources {@code null-ok;} registers whose use lists
 * {@code insn} should be removed form
 */
private void removeFromUseList(SsaInsn insn, RegisterSpecList oldSources) {
    if (oldSources == null) {
        return;
    }

    int szNew = oldSources.size();
    for (int i = 0; i < szNew; i++) {
        if (!useList[oldSources.get(i).getReg()].remove(insn)) {
            throw new RuntimeException("use not found");
        }
    }
}
 
Example 13
Source File: SCCP.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Simulates a PHI node and set the lattice for the result
 * to the appropriate value.
 * Meet values:
 * TOP x anything = TOP
 * VARYING x anything = VARYING
 * CONSTANT x CONSTANT = CONSTANT if equal constants, VARYING otherwise
 * @param insn PHI to simulate.
 */
private void simulatePhi(PhiInsn insn) {
    int phiResultReg = insn.getResult().getReg();

    if (latticeValues[phiResultReg] == VARYING) {
        return;
    }

    RegisterSpecList sources = insn.getSources();
    int phiResultValue = TOP;
    Constant phiConstant = null;
    int sourceSize = sources.size();

    for (int i = 0; i < sourceSize; i++) {
        int predBlockIndex = insn.predBlockIndexForSourcesIndex(i);
        int sourceReg = sources.get(i).getReg();
        int sourceRegValue = latticeValues[sourceReg];

        if (!executableBlocks.get(predBlockIndex)) {
            continue;
        }

        if (sourceRegValue == CONSTANT) {
            if (phiConstant == null) {
                phiConstant = latticeConstants[sourceReg];
                phiResultValue = CONSTANT;
             } else if (!latticeConstants[sourceReg].equals(phiConstant)){
                phiResultValue = VARYING;
                break;
            }
        } else {
            phiResultValue = sourceRegValue;
            break;
        }
    }
    if (setLatticeValueTo(phiResultReg, phiResultValue, phiConstant)) {
        addUsersToWorklist(phiResultReg, phiResultValue);
    }
}
 
Example 14
Source File: Form31t.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();

    if (!((insn instanceof TargetInsn) &&
          (regs.size() == 1) &&
          unsignedFitsInByte(regs.get(0).getReg()))) {
        return false;
    }

    return true;
}
 
Example 15
Source File: Form22x.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();

    return (insn instanceof SimpleInsn) &&
        (regs.size() == 2) &&
        unsignedFitsInByte(regs.get(0).getReg()) &&
        unsignedFitsInShort(regs.get(1).getReg());
}
 
Example 16
Source File: Form21t.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    RegisterSpecList regs = insn.getRegisters();

    if (!((insn instanceof TargetInsn) &&
          (regs.size() == 1) &&
          unsignedFitsInByte(regs.get(0).getReg()))) {
        return false;
    }

    TargetInsn ti = (TargetInsn) insn;
    return ti.hasTargetOffset() ? branchFits(ti) : true;
}
 
Example 17
Source File: FirstFitLocalCombiningAllocator.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to build a plan for fitting a range of sources into rop
 * registers.
 *
 * @param ropReg {@code >= 0;} rop reg that begins range
 * @param insn {@code non-null;} insn to plan range for
 * @param categoriesForIndex {@code non-null;} indexed by source index;
 * the category for each source
 * @param outMovesRequired {@code non-null;} an output parameter indexed by
 * source index that will contain the set of sources which need
 * moves inserted
 * @return the width of the fit that that does not involve added moves or
 * {@code -1} if "no fit possible"
 */
private int fitPlanForRange(int ropReg, NormalSsaInsn insn,
        int[] categoriesForIndex, BitSet outMovesRequired) {
    RegisterSpecList sources = insn.getSources();
    int szSources = sources.size();
    int fitWidth = 0;
    IntSet liveOut = insn.getBlock().getLiveOutRegs();
    RegisterSpecList liveOutSpecs = ssaSetToSpecs(liveOut);

    // An SSA reg may only be mapped into a range once.
    BitSet seen = new BitSet(ssaMeth.getRegCount());

    for (int i = 0; i < szSources ; i++) {
        RegisterSpec ssaSpec = sources.get(i);
        int ssaReg = ssaSpec.getReg();
        int category = categoriesForIndex[i];

        if (i != 0) {
            ropReg += categoriesForIndex[i-1];
        }

        if (ssaRegsMapped.get(ssaReg)
                && mapper.oldToNew(ssaReg) == ropReg) {
            // This is a register that is already mapped appropriately.
            fitWidth += category;
        } else if (rangeContainsReserved(ropReg, category)) {
            fitWidth = -1;
            break;
        } else if (!ssaRegsMapped.get(ssaReg)
                && canMapReg(ssaSpec, ropReg)
                && !seen.get(ssaReg)) {
            // This is a register that can be mapped appropriately.
            fitWidth += category;
        } else if (!mapper.areAnyPinned(liveOutSpecs, ropReg, category)
                && !mapper.areAnyPinned(sources, ropReg, category)) {
            /*
             * This is a source that can be moved. We can insert a
             * move as long as:
             *
             *   * no SSA register pinned to the desired rop reg
             *     is live out on the block
             *
             *   * no SSA register pinned to desired rop reg is
             *     a source of this insn (since this may require
             *     overlapping moves, which we can't presently handle)
             */

            outMovesRequired.set(i);
        } else {
            fitWidth = -1;
            break;
        }

        seen.set(ssaReg);
    }
    return fitWidth;
}
 
Example 18
Source File: Form21c.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    RegisterSpecList regs = insn.getRegisters();
    RegisterSpec reg;

    switch (regs.size()) {
        case 1: {
            reg = regs.get(0);
            break;
        }
        case 2: {
            /*
             * This format is allowed for ops that are effectively
             * 2-arg but where the two args are identical.
             */
            reg = regs.get(0);
            if (reg.getReg() != regs.get(1).getReg()) {
                return false;
            }
            break;
        }
        default: {
            return false;
        }
    }

    if (!unsignedFitsInByte(reg.getReg())) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();
    Constant cst = ci.getConstant();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    return cst instanceof CstType ||
        cst instanceof CstFieldRef ||
        cst instanceof CstString ||
        cst instanceof CstMethodHandle ||
        cst instanceof CstProtoRef;
}
 
Example 19
Source File: Form31c.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    RegisterSpecList regs = insn.getRegisters();
    RegisterSpec reg;

    switch (regs.size()) {
        case 1: {
            reg = regs.get(0);
            break;
        }
        case 2: {
            /*
             * This format is allowed for ops that are effectively
             * 2-arg but where the two args are identical.
             */
            reg = regs.get(0);
            if (reg.getReg() != regs.get(1).getReg()) {
                return false;
            }
            break;
        }
        default: {
            return false;
        }
    }

    if (!unsignedFitsInByte(reg.getReg())) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    Constant cst = ci.getConstant();

    return (cst instanceof CstType) ||
        (cst instanceof CstFieldRef) ||
        (cst instanceof CstString);
}
 
Example 20
Source File: FirstFitLocalCombiningAllocator.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to map the sources and result of a phi to a common register.
 * Will try existing mappings first, from most to least common. If none
 * of the registers have mappings yet, a new mapping is created.
 */
private void processPhiInsn(PhiInsn insn) {
    RegisterSpec result = insn.getResult();
    int resultReg = result.getReg();
    int category = result.getCategory();

    RegisterSpecList sources = insn.getSources();
    int sourcesSize = sources.size();

    // List of phi sources / result that need mapping
    ArrayList<RegisterSpec> ssaRegs = new ArrayList<RegisterSpec>();

    // Track how many times a particular mapping is found
    Multiset mapSet = new Multiset(sourcesSize + 1);

    /*
     * If the result of the phi has an existing mapping, get it.
     * Otherwise, add it to the list of regs that need mapping.
     */
    if (ssaRegsMapped.get(resultReg)) {
        mapSet.add(mapper.oldToNew(resultReg));
    } else {
        ssaRegs.add(result);
    }

    for (int i = 0; i < sourcesSize; i++) {
        RegisterSpec source = sources.get(i);
        SsaInsn def = ssaMeth.getDefinitionForRegister(source.getReg());
        RegisterSpec sourceDef = def.getResult();
        int sourceReg = sourceDef.getReg();

        /*
         * If a source of the phi has an existing mapping, get it.
         * Otherwise, add it to the list of regs that need mapping.
         */
        if (ssaRegsMapped.get(sourceReg)) {
            mapSet.add(mapper.oldToNew(sourceReg));
        } else {
            ssaRegs.add(sourceDef);
        }
    }

    // Try all existing mappings, with the most common ones first
    for (int i = 0; i < mapSet.getSize(); i++) {
        int maxReg = mapSet.getAndRemoveHighestCount();
        tryMapRegs(ssaRegs, maxReg, category, false);
    }

    // Map any remaining unmapped regs with whatever fits
    int mapReg = findNextUnreservedRopReg(paramRangeEnd, category);
    while (!tryMapRegs(ssaRegs, mapReg, category, false)) {
        mapReg = findNextUnreservedRopReg(mapReg + 1, category);
    }
}