Java Code Examples for com.android.dx.rop.code.RegOps#MOVE_RESULT_PSEUDO

The following examples show how to use com.android.dx.rop.code.RegOps#MOVE_RESULT_PSEUDO . 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: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example 2
Source File: EscapeAnalysis.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: RopTranslator.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example 4
Source File: EscapeAnalysis.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example 6
Source File: EscapeAnalysis.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 7
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example 8
Source File: EscapeAnalysis.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 9
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 10
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 11
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 12
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 13
Source File: SCCP.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Simulates a statement and set the result lattice value.
 * @param insn instruction to simulate
 */
private void simulateStmt(SsaInsn insn) {
    Insn ropInsn = insn.getOriginalRopInsn();
    if (ropInsn.getOpcode().getBranchingness() != Rop.BRANCH_NONE
            || ropInsn.getOpcode().isCallLike()) {
        simulateBranch(insn);
    }

    int opcode = insn.getOpcode().getOpcode();
    RegisterSpec result = insn.getResult();

    if (result == null) {
        // Find move-result-pseudo result for int div and int rem
        if (opcode == RegOps.DIV || opcode == RegOps.REM) {
            SsaBasicBlock succ = insn.getBlock().getPrimarySuccessor();
            result = succ.getInsns().get(0).getResult();
        } else {
            return;
        }
    }

    int resultReg = result.getReg();
    int resultValue = VARYING;
    Constant resultConstant = null;

    switch (opcode) {
        case RegOps.CONST: {
            CstInsn cstInsn = (CstInsn)ropInsn;
            resultValue = CONSTANT;
            resultConstant = cstInsn.getConstant();
            break;
        }
        case RegOps.MOVE: {
            if (insn.getSources().size() == 1) {
                int sourceReg = insn.getSources().get(0).getReg();
                resultValue = latticeValues[sourceReg];
                resultConstant = latticeConstants[sourceReg];
            }
            break;
        }
        case RegOps.ADD:
        case RegOps.SUB:
        case RegOps.MUL:
        case RegOps.DIV:
        case RegOps.AND:
        case RegOps.OR:
        case RegOps.XOR:
        case RegOps.SHL:
        case RegOps.SHR:
        case RegOps.USHR:
        case RegOps.REM: {
            resultConstant = simulateMath(insn, result.getBasicType());
            if (resultConstant != null) {
                resultValue = CONSTANT;
            }
            break;
        }
        case RegOps.MOVE_RESULT_PSEUDO: {
            if (latticeValues[resultReg] == CONSTANT) {
                resultValue = latticeValues[resultReg];
                resultConstant = latticeConstants[resultReg];
            }
            break;
        }
        // TODO: Handle non-int arithmetic.
        // TODO: Eliminate check casts that we can prove the type of.
        default: {}
    }
    if (setLatticeValueTo(resultReg, resultValue, resultConstant)) {
        addUsersToWorklist(resultReg, resultValue);
    }
}
 
Example 14
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainInsn(PlainInsn insn) {
    Rop rop = insn.getOpcode();
    if (rop.getOpcode() == RegOps.MARK_LOCAL) {
        /*
         * Ignore these. They're dealt with by
         * the LocalVariableAwareTranslationVisitor
         */
        return;
    }
    if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
        // These get skipped
        return;
    }

    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    DalvInsn di;

    switch (rop.getBranchingness()) {
        case Rop.BRANCH_NONE:
        case Rop.BRANCH_RETURN:
        case Rop.BRANCH_THROW: {
            di = new SimpleInsn(opcode, pos, getRegs(insn));
            break;
        }
        case Rop.BRANCH_GOTO: {
            /*
             * Code in the main translation loop will emit a
             * goto if necessary (if the branch isn't to the
             * immediately subsequent block).
             */
            return;
        }
        case Rop.BRANCH_IF: {
            int target = block.getSuccessors().get(1);
            di = new TargetInsn(opcode, pos, getRegs(insn),
                                addresses.getStart(target));
            break;
        }
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    addOutput(di);
}
 
Example 15
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
      @Override
public void visitPlainInsn(PlainInsn insn) {
          Rop rop = insn.getOpcode();
          if (rop.getOpcode() == RegOps.MARK_LOCAL) {
              /*
               * Ignore these. They're dealt with by
               * the LocalVariableAwareTranslationVisitor
               */
              return;
          }
          if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
              // These get skipped
              return;
          }

          SourcePosition pos = insn.getPosition();
          Dop opcode = RopToDop.dopFor(insn);
          DalvInsn di;

          switch (rop.getBranchingness()) {
              case Rop.BRANCH_NONE:
              case Rop.BRANCH_RETURN:
              case Rop.BRANCH_THROW: {
                  di = new SimpleInsn(opcode, pos, getRegs(insn));
                  break;
              }
              case Rop.BRANCH_GOTO: {
                  /*
                   * Code in the main translation loop will emit a
                   * goto if necessary (if the branch isn't to the
                   * immediately subsequent block).
                   */
                  return;
              }
              case Rop.BRANCH_IF: {
                  int target = block.getSuccessors().get(1);
                  di = new TargetInsn(opcode, pos, getRegs(insn),
                                      addresses.getStart(target));
                  break;
              }
              default: {
                  throw new RuntimeException("shouldn't happen");
              }
          }

          addOutput(di);
      }
 
Example 16
Source File: SCCP.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Simulates a statement and set the result lattice value.
 * @param insn instruction to simulate
 */
private void simulateStmt(SsaInsn insn) {
    Insn ropInsn = insn.getOriginalRopInsn();
    if (ropInsn.getOpcode().getBranchingness() != Rop.BRANCH_NONE
            || ropInsn.getOpcode().isCallLike()) {
        simulateBranch(insn);
    }

    int opcode = insn.getOpcode().getOpcode();
    RegisterSpec result = insn.getResult();

    if (result == null) {
        // Find move-result-pseudo result for int div and int rem
        if (opcode == RegOps.DIV || opcode == RegOps.REM) {
            SsaBasicBlock succ = insn.getBlock().getPrimarySuccessor();
            result = succ.getInsns().get(0).getResult();
        } else {
            return;
        }
    }

    int resultReg = result.getReg();
    int resultValue = VARYING;
    Constant resultConstant = null;

    switch (opcode) {
        case RegOps.CONST: {
            CstInsn cstInsn = (CstInsn)ropInsn;
            resultValue = CONSTANT;
            resultConstant = cstInsn.getConstant();
            break;
        }
        case RegOps.MOVE: {
            if (insn.getSources().size() == 1) {
                int sourceReg = insn.getSources().get(0).getReg();
                resultValue = latticeValues[sourceReg];
                resultConstant = latticeConstants[sourceReg];
            }
            break;
        }
        case RegOps.ADD:
        case RegOps.SUB:
        case RegOps.MUL:
        case RegOps.DIV:
        case RegOps.AND:
        case RegOps.OR:
        case RegOps.XOR:
        case RegOps.SHL:
        case RegOps.SHR:
        case RegOps.USHR:
        case RegOps.REM: {
            resultConstant = simulateMath(insn, result.getBasicType());
            if (resultConstant != null) {
                resultValue = CONSTANT;
            }
            break;
        }
        case RegOps.MOVE_RESULT_PSEUDO: {
            if (latticeValues[resultReg] == CONSTANT) {
                resultValue = latticeValues[resultReg];
                resultConstant = latticeConstants[resultReg];
            }
            break;
        }
        // TODO: Handle non-int arithmetic.
        // TODO: Eliminate check casts that we can prove the type of.
        default: {}
    }
    if (setLatticeValueTo(resultReg, resultValue, resultConstant)) {
        addUsersToWorklist(resultReg, resultValue);
    }
}
 
Example 17
Source File: RopTranslator.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void visitPlainInsn(PlainInsn insn) {
    Rop rop = insn.getOpcode();
    if (rop.getOpcode() == RegOps.MARK_LOCAL) {
        /*
         * Ignore these. They're dealt with by
         * the LocalVariableAwareTranslationVisitor
         */
        return;
    }
    if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
        // These get skipped
        return;
    }

    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    DalvInsn di;

    switch (rop.getBranchingness()) {
        case Rop.BRANCH_NONE:
        case Rop.BRANCH_RETURN:
        case Rop.BRANCH_THROW: {
            di = new SimpleInsn(opcode, pos, getRegs(insn));
            break;
        }
        case Rop.BRANCH_GOTO: {
            /*
             * Code in the main translation loop will emit a
             * goto if necessary (if the branch isn't to the
             * immediately subsequent block).
             */
            return;
        }
        case Rop.BRANCH_IF: {
            int target = block.getSuccessors().get(1);
            di = new TargetInsn(opcode, pos, getRegs(insn),
                                addresses.getStart(target));
            break;
        }
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    addOutput(di);
}
 
Example 18
Source File: SCCP.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Simulates a statement and set the result lattice value.
 * @param insn instruction to simulate
 */
private void simulateStmt(SsaInsn insn) {
    Insn ropInsn = insn.getOriginalRopInsn();
    if (ropInsn.getOpcode().getBranchingness() != Rop.BRANCH_NONE
            || ropInsn.getOpcode().isCallLike()) {
        simulateBranch(insn);
    }

    int opcode = insn.getOpcode().getOpcode();
    RegisterSpec result = insn.getResult();

    if (result == null) {
        // Find move-result-pseudo result for int div and int rem
        if (opcode == RegOps.DIV || opcode == RegOps.REM) {
            SsaBasicBlock succ = insn.getBlock().getPrimarySuccessor();
            result = succ.getInsns().get(0).getResult();
        } else {
            return;
        }
    }

    int resultReg = result.getReg();
    int resultValue = VARYING;
    Constant resultConstant = null;

    switch (opcode) {
        case RegOps.CONST: {
            CstInsn cstInsn = (CstInsn)ropInsn;
            resultValue = CONSTANT;
            resultConstant = cstInsn.getConstant();
            break;
        }
        case RegOps.MOVE: {
            if (insn.getSources().size() == 1) {
                int sourceReg = insn.getSources().get(0).getReg();
                resultValue = latticeValues[sourceReg];
                resultConstant = latticeConstants[sourceReg];
            }
            break;
        }
        case RegOps.ADD:
        case RegOps.SUB:
        case RegOps.MUL:
        case RegOps.DIV:
        case RegOps.AND:
        case RegOps.OR:
        case RegOps.XOR:
        case RegOps.SHL:
        case RegOps.SHR:
        case RegOps.USHR:
        case RegOps.REM: {
            resultConstant = simulateMath(insn, result.getBasicType());
            if (resultConstant != null) {
                resultValue = CONSTANT;
            }
            break;
        }
        case RegOps.MOVE_RESULT_PSEUDO: {
            if (latticeValues[resultReg] == CONSTANT) {
                resultValue = latticeValues[resultReg];
                resultConstant = latticeConstants[resultReg];
            }
            break;
        }
        // TODO: Handle non-int arithmetic.
        // TODO: Eliminate check casts that we can prove the type of.
        default: {}
    }
    if (setLatticeValueTo(resultReg, resultValue, resultConstant)) {
        addUsersToWorklist(resultReg, resultValue);
    }
}
 
Example 19
Source File: SCCP.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Simulates a statement and set the result lattice value.
 * @param insn instruction to simulate
 */
private void simulateStmt(SsaInsn insn) {
    Insn ropInsn = insn.getOriginalRopInsn();
    if (ropInsn.getOpcode().getBranchingness() != Rop.BRANCH_NONE
            || ropInsn.getOpcode().isCallLike()) {
        simulateBranch(insn);
    }

    int opcode = insn.getOpcode().getOpcode();
    RegisterSpec result = insn.getResult();

    if (result == null) {
        // Find move-result-pseudo result for int div and int rem
        if (opcode == RegOps.DIV || opcode == RegOps.REM) {
            SsaBasicBlock succ = insn.getBlock().getPrimarySuccessor();
            result = succ.getInsns().get(0).getResult();
        } else {
            return;
        }
    }

    int resultReg = result.getReg();
    int resultValue = VARYING;
    Constant resultConstant = null;

    switch (opcode) {
        case RegOps.CONST: {
            CstInsn cstInsn = (CstInsn)ropInsn;
            resultValue = CONSTANT;
            resultConstant = cstInsn.getConstant();
            break;
        }
        case RegOps.MOVE: {
            if (insn.getSources().size() == 1) {
                int sourceReg = insn.getSources().get(0).getReg();
                resultValue = latticeValues[sourceReg];
                resultConstant = latticeConstants[sourceReg];
            }
            break;
        }
        case RegOps.ADD:
        case RegOps.SUB:
        case RegOps.MUL:
        case RegOps.DIV:
        case RegOps.AND:
        case RegOps.OR:
        case RegOps.XOR:
        case RegOps.SHL:
        case RegOps.SHR:
        case RegOps.USHR:
        case RegOps.REM: {
            resultConstant = simulateMath(insn, result.getBasicType());
            if (resultConstant != null) {
                resultValue = CONSTANT;
            }
            break;
        }
        case RegOps.MOVE_RESULT_PSEUDO: {
            if (latticeValues[resultReg] == CONSTANT) {
                resultValue = latticeValues[resultReg];
                resultConstant = latticeConstants[resultReg];
            }
            break;
        }
        // TODO: Handle non-int arithmetic.
        // TODO: Eliminate check casts that we can prove the type of.
        default: {}
    }
    if (setLatticeValueTo(resultReg, resultValue, resultConstant)) {
        addUsersToWorklist(resultReg, resultValue);
    }
}
 
Example 20
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainInsn(PlainInsn insn) {
    Rop rop = insn.getOpcode();
    if (rop.getOpcode() == RegOps.MARK_LOCAL) {
        /*
         * Ignore these. They're dealt with by
         * the LocalVariableAwareTranslationVisitor
         */
        return;
    }
    if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
        // These get skipped
        return;
    }

    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    DalvInsn di;

    switch (rop.getBranchingness()) {
        case Rop.BRANCH_NONE:
        case Rop.BRANCH_RETURN:
        case Rop.BRANCH_THROW: {
            di = new SimpleInsn(opcode, pos, getRegs(insn));
            break;
        }
        case Rop.BRANCH_GOTO: {
            /*
             * Code in the main translation loop will emit a
             * goto if necessary (if the branch isn't to the
             * immediately subsequent block).
             */
            return;
        }
        case Rop.BRANCH_IF: {
            int target = block.getSuccessors().get(1);
            di = new TargetInsn(opcode, pos, getRegs(insn),
                                addresses.getStart(target));
            break;
        }
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    addOutput(di);
}