Search in sources :

Example 21 with RegisterSpec

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

the class FirstFitLocalCombiningAllocator method handleNormalUnassociated.

/**
     * Maps all non-parameter, non-local variable registers.
     */
private void handleNormalUnassociated() {
    int szSsaRegs = ssaMeth.getRegCount();
    for (int ssaReg = 0; ssaReg < szSsaRegs; ssaReg++) {
        if (ssaRegsMapped.get(ssaReg)) {
            // We already did this one
            continue;
        }
        RegisterSpec ssaSpec = getDefinitionSpecForSsaReg(ssaReg);
        if (ssaSpec == null)
            continue;
        int category = ssaSpec.getCategory();
        // Find a rop reg that does not interfere
        int ropReg = findNextUnreservedRopReg(paramRangeEnd, category);
        while (!canMapReg(ssaSpec, ropReg)) {
            ropReg = findNextUnreservedRopReg(ropReg + 1, category);
        }
        addMapping(ssaSpec, ropReg);
    }
}
Also used : RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Example 22 with RegisterSpec

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

the class RopperMachine method run.

/** {@inheritDoc} */
@Override
public void run(Frame frame, int offset, int opcode) {
    /*
         * This is the stack pointer after the opcode's arguments have been
         * popped.
         */
    int stackPointer = maxLocals + frame.getStack().size();
    // The sources have to be retrieved before super.run() gets called.
    RegisterSpecList sources = getSources(opcode, stackPointer);
    int sourceCount = sources.size();
    super.run(frame, offset, opcode);
    SourcePosition pos = method.makeSourcePosistion(offset);
    RegisterSpec localTarget = getLocalTarget(opcode == ByteOps.ISTORE);
    int destCount = resultCount();
    RegisterSpec dest;
    if (destCount == 0) {
        dest = null;
        switch(opcode) {
            case ByteOps.POP:
            case ByteOps.POP2:
                {
                    // These simply don't appear in the rop form.
                    return;
                }
        }
    } else if (localTarget != null) {
        dest = localTarget;
    } else if (destCount == 1) {
        dest = RegisterSpec.make(stackPointer, result(0));
    } else {
        /*
             * This clause only ever applies to the stack manipulation
             * ops that have results (that is, dup* and swap but not
             * pop*).
             *
             * What we do is first move all the source registers into
             * the "temporary stack" area defined for the method, and
             * then move stuff back down onto the main "stack" in the
             * arrangement specified by the stack op pattern.
             *
             * Note: This code ends up emitting a lot of what will
             * turn out to be superfluous moves (e.g., moving back and
             * forth to the same local when doing a dup); however,
             * that makes this code a bit easier (and goodness knows
             * it doesn't need any extra complexity), and all the SSA
             * stuff is going to want to deal with this sort of
             * superfluous assignment anyway, so it should be a wash
             * in the end.
             */
        int scratchAt = ropper.getFirstTempStackReg();
        RegisterSpec[] scratchRegs = new RegisterSpec[sourceCount];
        for (int i = 0; i < sourceCount; i++) {
            RegisterSpec src = sources.get(i);
            TypeBearer type = src.getTypeBearer();
            RegisterSpec scratch = src.withReg(scratchAt);
            insns.add(new PlainInsn(Rops.opMove(type), pos, scratch, src));
            scratchRegs[i] = scratch;
            scratchAt += src.getCategory();
        }
        for (int pattern = getAuxInt(); pattern != 0; pattern >>= 4) {
            int which = (pattern & 0x0f) - 1;
            RegisterSpec scratch = scratchRegs[which];
            TypeBearer type = scratch.getTypeBearer();
            insns.add(new PlainInsn(Rops.opMove(type), pos, scratch.withReg(stackPointer), scratch));
            stackPointer += type.getType().getCategory();
        }
        return;
    }
    TypeBearer destType = (dest != null) ? dest : Type.VOID;
    Constant cst = getAuxCst();
    int ropOpcode;
    Rop rop;
    Insn insn;
    if (opcode == ByteOps.MULTIANEWARRAY) {
        blockCanThrow = true;
        // Add the extra instructions for handling multianewarray.
        extraBlockCount = 6;
        /*
             * Add an array constructor for the int[] containing all the
             * dimensions.
             */
        RegisterSpec dimsReg = RegisterSpec.make(dest.getNextReg(), Type.INT_ARRAY);
        rop = Rops.opFilledNewArray(Type.INT_ARRAY, sourceCount);
        insn = new ThrowingCstInsn(rop, pos, sources, catches, CstType.INT_ARRAY);
        insns.add(insn);
        // Add a move-result for the new-filled-array
        rop = Rops.opMoveResult(Type.INT_ARRAY);
        insn = new PlainInsn(rop, pos, dimsReg, RegisterSpecList.EMPTY);
        insns.add(insn);
        /*
             * Add a const-class instruction for the specified array
             * class.
             */
        /*
             * Remove as many dimensions from the originally specified
             * class as are given in the explicit list of dimensions,
             * so as to pass the right component class to the standard
             * Java library array constructor.
             */
        Type componentType = ((CstType) cst).getClassType();
        for (int i = 0; i < sourceCount; i++) {
            componentType = componentType.getComponentType();
        }
        RegisterSpec classReg = RegisterSpec.make(dest.getReg(), Type.CLASS);
        if (componentType.isPrimitive()) {
            /*
                 * The component type is primitive (e.g., int as opposed
                 * to Integer), so we have to fetch the corresponding
                 * TYPE class.
                 */
            CstFieldRef typeField = CstFieldRef.forPrimitiveType(componentType);
            insn = new ThrowingCstInsn(Rops.GET_STATIC_OBJECT, pos, RegisterSpecList.EMPTY, catches, typeField);
        } else {
            /*
                 * The component type is an object type, so just make a
                 * normal class reference.
                 */
            insn = new ThrowingCstInsn(Rops.CONST_OBJECT, pos, RegisterSpecList.EMPTY, catches, new CstType(componentType));
        }
        insns.add(insn);
        // Add a move-result-pseudo for the get-static or const
        rop = Rops.opMoveResultPseudo(classReg.getType());
        insn = new PlainInsn(rop, pos, classReg, RegisterSpecList.EMPTY);
        insns.add(insn);
        /*
             * Add a call to the "multianewarray method," that is,
             * Array.newInstance(class, dims). Note: The result type
             * of newInstance() is Object, which is why the last
             * instruction in this sequence is a cast to the right
             * type for the original instruction.
             */
        RegisterSpec objectReg = RegisterSpec.make(dest.getReg(), Type.OBJECT);
        insn = new ThrowingCstInsn(Rops.opInvokeStatic(MULTIANEWARRAY_METHOD.getPrototype()), pos, RegisterSpecList.make(classReg, dimsReg), catches, MULTIANEWARRAY_METHOD);
        insns.add(insn);
        // Add a move-result.
        rop = Rops.opMoveResult(MULTIANEWARRAY_METHOD.getPrototype().getReturnType());
        insn = new PlainInsn(rop, pos, objectReg, RegisterSpecList.EMPTY);
        insns.add(insn);
        /*
             * And finally, set up for the remainder of this method to
             * add an appropriate cast.
             */
        opcode = ByteOps.CHECKCAST;
        sources = RegisterSpecList.make(objectReg);
    } else if (opcode == ByteOps.JSR) {
        // JSR has no Rop instruction
        hasJsr = true;
        return;
    } else if (opcode == ByteOps.RET) {
        try {
            returnAddress = (ReturnAddress) arg(0);
        } catch (ClassCastException ex) {
            throw new RuntimeException("Argument to RET was not a ReturnAddress", ex);
        }
        // RET has no Rop instruction.
        return;
    }
    ropOpcode = jopToRopOpcode(opcode, cst);
    rop = Rops.ropFor(ropOpcode, destType, sources, cst);
    Insn moveResult = null;
    if (dest != null && rop.isCallLike()) {
        /*
             * We're going to want to have a move-result in the next
             * basic block.
             */
        extraBlockCount++;
        moveResult = new PlainInsn(Rops.opMoveResult(((CstMethodRef) cst).getPrototype().getReturnType()), pos, dest, RegisterSpecList.EMPTY);
        dest = null;
    } else if (dest != null && rop.canThrow()) {
        /*
             * We're going to want to have a move-result-pseudo in the
             * next basic block.
             */
        extraBlockCount++;
        moveResult = new PlainInsn(Rops.opMoveResultPseudo(dest.getTypeBearer()), pos, dest, RegisterSpecList.EMPTY);
        dest = null;
    }
    if (ropOpcode == RegOps.NEW_ARRAY) {
        /*
             * In the original bytecode, this was either a primitive
             * array constructor "newarray" or an object array
             * constructor "anewarray". In the former case, there is
             * no explicit constant, and in the latter, the constant
             * is for the element type and not the array type. The rop
             * instruction form for both of these is supposed to be
             * the resulting array type, so we initialize / alter
             * "cst" here, accordingly. Conveniently enough, the rop
             * opcode already gets constructed with the proper array
             * type.
             */
        cst = CstType.intern(rop.getResult());
    } else if ((cst == null) && (sourceCount == 2)) {
        TypeBearer firstType = sources.get(0).getTypeBearer();
        TypeBearer lastType = sources.get(1).getTypeBearer();
        if ((lastType.isConstant() || firstType.isConstant()) && advice.hasConstantOperation(rop, sources.get(0), sources.get(1))) {
            if (lastType.isConstant()) {
                /*
                     * The target architecture has an instruction that can
                     * build in the constant found in the second argument,
                     * so pull it out of the sources and just use it as a
                     * constant here.
                     */
                cst = (Constant) lastType;
                sources = sources.withoutLast();
                // For subtraction, change to addition and invert constant
                if (rop.getOpcode() == RegOps.SUB) {
                    ropOpcode = RegOps.ADD;
                    CstInteger cstInt = (CstInteger) lastType;
                    cst = CstInteger.make(-cstInt.getValue());
                }
            } else {
                /*
                     * The target architecture has an instruction that can
                     * build in the constant found in the first argument,
                     * so pull it out of the sources and just use it as a
                     * constant here.
                     */
                cst = (Constant) firstType;
                sources = sources.withoutFirst();
            }
            rop = Rops.ropFor(ropOpcode, destType, sources, cst);
        }
    }
    SwitchList cases = getAuxCases();
    ArrayList<Constant> initValues = getInitValues();
    boolean canThrow = rop.canThrow();
    blockCanThrow |= canThrow;
    if (cases != null) {
        if (cases.size() == 0) {
            // It's a default-only switch statement. It can happen!
            insn = new PlainInsn(Rops.GOTO, pos, null, RegisterSpecList.EMPTY);
            primarySuccessorIndex = 0;
        } else {
            IntList values = cases.getValues();
            insn = new SwitchInsn(rop, pos, dest, sources, values);
            primarySuccessorIndex = values.size();
        }
    } else if (ropOpcode == RegOps.RETURN) {
        /*
             * Returns get turned into the combination of a move (if
             * non-void and if the return doesn't already mention
             * register 0) and a goto (to the return block).
             */
        if (sources.size() != 0) {
            RegisterSpec source = sources.get(0);
            TypeBearer type = source.getTypeBearer();
            if (source.getReg() != 0) {
                insns.add(new PlainInsn(Rops.opMove(type), pos, RegisterSpec.make(0, type), source));
            }
        }
        insn = new PlainInsn(Rops.GOTO, pos, null, RegisterSpecList.EMPTY);
        primarySuccessorIndex = 0;
        updateReturnOp(rop, pos);
        returns = true;
    } else if (cst != null) {
        if (canThrow) {
            insn = new ThrowingCstInsn(rop, pos, sources, catches, cst);
            catchesUsed = true;
            primarySuccessorIndex = catches.size();
        } else {
            insn = new PlainCstInsn(rop, pos, dest, sources, cst);
        }
    } else if (canThrow) {
        insn = new ThrowingInsn(rop, pos, sources, catches);
        catchesUsed = true;
        if (opcode == ByteOps.ATHROW) {
            /*
                 * The op athrow is the only one where it's possible
                 * to have non-empty successors and yet not have a
                 * primary successor.
                 */
            primarySuccessorIndex = -1;
        } else {
            primarySuccessorIndex = catches.size();
        }
    } else {
        insn = new PlainInsn(rop, pos, dest, sources);
    }
    insns.add(insn);
    if (moveResult != null) {
        insns.add(moveResult);
    }
    /*
         * If initValues is non-null, it means that the parser has
         * seen a group of compatible constant initialization
         * bytecodes that are applied to the current newarray. The
         * action we take here is to convert these initialization
         * bytecodes into a single fill-array-data ROP which lays out
         * all the constant values in a table.
         */
    if (initValues != null) {
        extraBlockCount++;
        insn = new FillArrayDataInsn(Rops.FILL_ARRAY_DATA, pos, RegisterSpecList.make(moveResult.getResult()), initValues, cst);
        insns.add(insn);
    }
}
Also used : ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) ThrowingInsn(com.android.dx.rop.code.ThrowingInsn) PlainInsn(com.android.dx.rop.code.PlainInsn) FillArrayDataInsn(com.android.dx.rop.code.FillArrayDataInsn) Insn(com.android.dx.rop.code.Insn) SwitchInsn(com.android.dx.rop.code.SwitchInsn) PlainCstInsn(com.android.dx.rop.code.PlainCstInsn) ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) Constant(com.android.dx.rop.cst.Constant) ThrowingInsn(com.android.dx.rop.code.ThrowingInsn) RegisterSpecList(com.android.dx.rop.code.RegisterSpecList) CstInteger(com.android.dx.rop.cst.CstInteger) SourcePosition(com.android.dx.rop.code.SourcePosition) RegisterSpec(com.android.dx.rop.code.RegisterSpec) FillArrayDataInsn(com.android.dx.rop.code.FillArrayDataInsn) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) CstMethodRef(com.android.dx.rop.cst.CstMethodRef) SwitchInsn(com.android.dx.rop.code.SwitchInsn) IntList(com.android.dx.util.IntList) PlainCstInsn(com.android.dx.rop.code.PlainCstInsn) PlainInsn(com.android.dx.rop.code.PlainInsn) Rop(com.android.dx.rop.code.Rop) Type(com.android.dx.rop.type.Type) CstType(com.android.dx.rop.cst.CstType) CstType(com.android.dx.rop.cst.CstType) TypeBearer(com.android.dx.rop.type.TypeBearer)

Example 23 with RegisterSpec

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

the class EscapeAnalysis method processInsn.

/**
     * Process a single instruction, looking for new objects resulting from
     * move result or move param.
     *
     * @param insn {@code non-null;} instruction to process
     */
private void processInsn(SsaInsn insn) {
    int op = insn.getOpcode().getOpcode();
    RegisterSpec result = insn.getResult();
    EscapeSet escSet;
    // Identify new objects
    if (op == RegOps.MOVE_RESULT_PSEUDO && result.getTypeBearer().getBasicType() == Type.BT_OBJECT) {
        // Handle objects generated through move_result_pseudo
        escSet = processMoveResultPseudoInsn(insn);
        processRegister(result, escSet);
    } else if (op == RegOps.MOVE_PARAM && result.getTypeBearer().getBasicType() == Type.BT_OBJECT) {
        // Track method arguments that are objects
        escSet = new EscapeSet(result.getReg(), regCount, EscapeState.NONE);
        latticeValues.add(escSet);
        processRegister(result, escSet);
    } else if (op == RegOps.MOVE_RESULT && result.getTypeBearer().getBasicType() == Type.BT_OBJECT) {
        // Track method return values that are objects
        escSet = new EscapeSet(result.getReg(), regCount, EscapeState.NONE);
        latticeValues.add(escSet);
        processRegister(result, escSet);
    }
}
Also used : RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Example 24 with RegisterSpec

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

the class EscapeAnalysis method processMoveResultPseudoInsn.

/**
     * Determine the origin of a move result pseudo instruction that generates
     * an object. Creates a new EscapeSet for the new object accordingly.
     *
     * @param insn {@code non-null;} move result pseudo instruction to process
     * @return {@code non-null;} an EscapeSet for the object referred to by the
     * move result pseudo instruction
     */
private EscapeSet processMoveResultPseudoInsn(SsaInsn insn) {
    RegisterSpec result = insn.getResult();
    SsaInsn prevSsaInsn = getInsnForMove(insn);
    int prevOpcode = prevSsaInsn.getOpcode().getOpcode();
    EscapeSet escSet;
    RegisterSpec prevSource;
    switch(prevOpcode) {
        // New instance / Constant
        case RegOps.NEW_INSTANCE:
        case RegOps.CONST:
            escSet = new EscapeSet(result.getReg(), regCount, EscapeState.NONE);
            break;
        // New array
        case RegOps.NEW_ARRAY:
        case RegOps.FILLED_NEW_ARRAY:
            prevSource = prevSsaInsn.getSources().get(0);
            if (prevSource.getTypeBearer().isConstant()) {
                // New fixed array
                escSet = new EscapeSet(result.getReg(), regCount, EscapeState.NONE);
                escSet.replaceableArray = true;
            } else {
                // New variable array
                escSet = new EscapeSet(result.getReg(), regCount, EscapeState.GLOBAL);
            }
            break;
        // Loading a static object
        case RegOps.GET_STATIC:
            escSet = new EscapeSet(result.getReg(), regCount, EscapeState.GLOBAL);
            break;
        // Type cast / load an object from a field or array
        case RegOps.CHECK_CAST:
        case RegOps.GET_FIELD:
        case RegOps.AGET:
            prevSource = prevSsaInsn.getSources().get(0);
            int setIndex = findSetIndex(prevSource);
            // Set should already exist, try to find it
            if (setIndex != latticeValues.size()) {
                escSet = latticeValues.get(setIndex);
                escSet.regSet.set(result.getReg());
                return escSet;
            }
            // Set not found, must be either null or unknown
            if (prevSource.getType() == Type.KNOWN_NULL) {
                escSet = new EscapeSet(result.getReg(), regCount, EscapeState.NONE);
            } else {
                escSet = new EscapeSet(result.getReg(), regCount, EscapeState.GLOBAL);
            }
            break;
        default:
            return null;
    }
    // Add the newly created escSet to the lattice and return it
    latticeValues.add(escSet);
    return escSet;
}
Also used : RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Example 25 with RegisterSpec

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

the class EscapeAnalysis method processUse.

/**
     * Handles non-phi uses of new objects. Checks to see how instruction is
     * used and updates the escape state accordingly.
     *
     * @param def {@code non-null;} register holding definition of new object
     * @param use {@code non-null;} use of object being processed
     * @param escSet {@code non-null;} EscapeSet for the object
     * @param regWorklist {@code non-null;} worklist of instructions left to
     * process for this object
     */
private void processUse(RegisterSpec def, SsaInsn use, EscapeSet escSet, ArrayList<RegisterSpec> regWorklist) {
    int useOpcode = use.getOpcode().getOpcode();
    switch(useOpcode) {
        case RegOps.MOVE:
            // Follow uses of the move by adding it to the worklist
            escSet.regSet.set(use.getResult().getReg());
            regWorklist.add(use.getResult());
            break;
        case RegOps.IF_EQ:
        case RegOps.IF_NE:
        case RegOps.CHECK_CAST:
            // Compared objects can't be replaced, so promote if necessary
            if (escSet.escape.compareTo(EscapeState.METHOD) < 0) {
                escSet.escape = EscapeState.METHOD;
            }
            break;
        case RegOps.APUT:
            // For array puts, check for a constant array index
            RegisterSpec putIndex = use.getSources().get(2);
            if (!putIndex.getTypeBearer().isConstant()) {
                // If not constant, array can't be replaced
                escSet.replaceableArray = false;
            }
        // Intentional fallthrough
        case RegOps.PUT_FIELD:
            // Skip non-object puts
            RegisterSpec putValue = use.getSources().get(0);
            if (putValue.getTypeBearer().getBasicType() != Type.BT_OBJECT) {
                break;
            }
            escSet.replaceableArray = false;
            // Raise 1st object's escape state to 2nd if 2nd is higher
            RegisterSpecList sources = use.getSources();
            if (sources.get(0).getReg() == def.getReg()) {
                int setIndex = findSetIndex(sources.get(1));
                if (setIndex != latticeValues.size()) {
                    EscapeSet parentSet = latticeValues.get(setIndex);
                    addEdge(parentSet, escSet);
                    if (escSet.escape.compareTo(parentSet.escape) < 0) {
                        escSet.escape = parentSet.escape;
                    }
                }
            } else {
                int setIndex = findSetIndex(sources.get(0));
                if (setIndex != latticeValues.size()) {
                    EscapeSet childSet = latticeValues.get(setIndex);
                    addEdge(escSet, childSet);
                    if (childSet.escape.compareTo(escSet.escape) < 0) {
                        childSet.escape = escSet.escape;
                    }
                }
            }
            break;
        case RegOps.AGET:
            // For array gets, check for a constant array index
            RegisterSpec getIndex = use.getSources().get(1);
            if (!getIndex.getTypeBearer().isConstant()) {
                // If not constant, array can't be replaced
                escSet.replaceableArray = false;
            }
            break;
        case RegOps.PUT_STATIC:
            // Static puts cause an object to escape globally
            escSet.escape = EscapeState.GLOBAL;
            break;
        case RegOps.INVOKE_STATIC:
        case RegOps.INVOKE_VIRTUAL:
        case RegOps.INVOKE_SUPER:
        case RegOps.INVOKE_DIRECT:
        case RegOps.INVOKE_INTERFACE:
        case RegOps.RETURN:
        case RegOps.THROW:
            // These operations cause an object to escape interprocedurally
            escSet.escape = EscapeState.INTER;
            break;
        default:
            break;
    }
}
Also used : RegisterSpecList(com.android.dx.rop.code.RegisterSpecList) RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Aggregations

RegisterSpec (com.android.dx.rop.code.RegisterSpec)135 RegisterSpecList (com.android.dx.rop.code.RegisterSpecList)50 PlainInsn (com.android.dx.rop.code.PlainInsn)24 Constant (com.android.dx.rop.cst.Constant)20 TypedConstant (com.android.dx.rop.cst.TypedConstant)16 TypeBearer (com.android.dx.rop.type.TypeBearer)16 ArrayList (java.util.ArrayList)16 Insn (com.android.dx.rop.code.Insn)14 PlainCstInsn (com.android.dx.rop.code.PlainCstInsn)14 Rop (com.android.dx.rop.code.Rop)14 ThrowingCstInsn (com.android.dx.rop.code.ThrowingCstInsn)14 CstType (com.android.dx.rop.cst.CstType)14 BitSet (java.util.BitSet)11 LocalItem (com.android.dx.rop.code.LocalItem)10 RegisterSpecSet (com.android.dx.rop.code.RegisterSpecSet)10 ThrowingInsn (com.android.dx.rop.code.ThrowingInsn)10 CstString (com.android.dx.rop.cst.CstString)10 HashSet (java.util.HashSet)10 SourcePosition (com.android.dx.rop.code.SourcePosition)8 CstFieldRef (com.android.dx.rop.cst.CstFieldRef)8