use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.
the class LiteralOpUpgrader method tryReplacingWithConstant.
/**
* 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;
}
use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.
the class NormalSsaInsn method hasSideEffect.
/**
* {@inheritDoc}
*
* TODO: Increase the scope of this.
*/
@Override
public boolean hasSideEffect() {
Rop opcode = getOpcode();
if (opcode.getBranchingness() != Rop.BRANCH_NONE) {
return true;
}
boolean hasLocalSideEffect = Optimizer.getPreserveLocals() && getLocalAssignment() != null;
switch(opcode.getOpcode()) {
case RegOps.MOVE_RESULT:
case RegOps.MOVE:
case RegOps.CONST:
return hasLocalSideEffect;
default:
return true;
}
}
use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.
the class SCCP method simulateBranch.
/**
* Simulates branch insns, if possible. Adds reachable successor blocks
* to the CFG worklists.
* @param insn branch to simulate
*/
private void simulateBranch(SsaInsn insn) {
Rop opcode = insn.getOpcode();
RegisterSpecList sources = insn.getSources();
boolean constantBranch = false;
boolean constantSuccessor = false;
// Check if the insn is a branch with a constant condition
if (opcode.getBranchingness() == Rop.BRANCH_IF) {
Constant cA = null;
Constant cB = null;
RegisterSpec specA = sources.get(0);
int regA = specA.getReg();
if (!ssaMeth.isRegALocal(specA) && latticeValues[regA] == CONSTANT) {
cA = latticeConstants[regA];
}
if (sources.size() == 2) {
RegisterSpec specB = sources.get(1);
int regB = specB.getReg();
if (!ssaMeth.isRegALocal(specB) && latticeValues[regB] == CONSTANT) {
cB = latticeConstants[regB];
}
}
// Calculate the result of the condition
if (cA != null && sources.size() == 1) {
switch(((TypedConstant) cA).getBasicType()) {
case Type.BT_INT:
constantBranch = true;
int vA = ((CstInteger) cA).getValue();
switch(opcode.getOpcode()) {
case RegOps.IF_EQ:
constantSuccessor = (vA == 0);
break;
case RegOps.IF_NE:
constantSuccessor = (vA != 0);
break;
case RegOps.IF_LT:
constantSuccessor = (vA < 0);
break;
case RegOps.IF_GE:
constantSuccessor = (vA >= 0);
break;
case RegOps.IF_LE:
constantSuccessor = (vA <= 0);
break;
case RegOps.IF_GT:
constantSuccessor = (vA > 0);
break;
default:
throw new RuntimeException("Unexpected op");
}
break;
default:
}
} else if (cA != null && cB != null) {
switch(((TypedConstant) cA).getBasicType()) {
case Type.BT_INT:
constantBranch = true;
int vA = ((CstInteger) cA).getValue();
int vB = ((CstInteger) cB).getValue();
switch(opcode.getOpcode()) {
case RegOps.IF_EQ:
constantSuccessor = (vA == vB);
break;
case RegOps.IF_NE:
constantSuccessor = (vA != vB);
break;
case RegOps.IF_LT:
constantSuccessor = (vA < vB);
break;
case RegOps.IF_GE:
constantSuccessor = (vA >= vB);
break;
case RegOps.IF_LE:
constantSuccessor = (vA <= vB);
break;
case RegOps.IF_GT:
constantSuccessor = (vA > vB);
break;
default:
throw new RuntimeException("Unexpected op");
}
break;
default:
}
}
}
/*
* If condition is constant, add only the target block to the
* worklist. Otherwise, add all successors to the worklist.
*/
SsaBasicBlock block = insn.getBlock();
if (constantBranch) {
int successorBlock;
if (constantSuccessor) {
successorBlock = block.getSuccessorList().get(1);
} else {
successorBlock = block.getSuccessorList().get(0);
}
addBlockToWorklist(ssaMeth.getBlocks().get(successorBlock));
branchWorklist.add(insn);
} else {
for (int i = 0; i < block.getSuccessorList().size(); i++) {
int successorBlock = block.getSuccessorList().get(i);
addBlockToWorklist(ssaMeth.getBlocks().get(successorBlock));
}
}
}
use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.
the class FirstFitLocalCombiningAllocator method getParameterIndexForReg.
/**
* Gets the parameter index for SSA registers that are method parameters.
* {@code -1} is returned for non-parameter registers.
*
* @param ssaReg {@code >=0;} SSA register to look up
* @return parameter index or {@code -1} if not a parameter
*/
private int getParameterIndexForReg(int ssaReg) {
SsaInsn defInsn = ssaMeth.getDefinitionForRegister(ssaReg);
if (defInsn == null) {
return -1;
}
Rop opcode = defInsn.getOpcode();
// opcode == null for phi insns.
if (opcode != null && opcode.getOpcode() == RegOps.MOVE_PARAM) {
CstInsn origInsn = (CstInsn) defInsn.getOriginalRopInsn();
return ((CstInteger) origInsn.getConstant()).getValue();
}
return -1;
}
use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.
the class SsaToRop method verifyValidExitPredecessor.
/**
* Validates that a basic block is a valid end predecessor. It must
* end in a RETURN or a THROW. Throws a runtime exception on error.
*
* @param b {@code non-null;} block to validate
* @throws RuntimeException on error
*/
private void verifyValidExitPredecessor(SsaBasicBlock b) {
ArrayList<SsaInsn> insns = b.getInsns();
SsaInsn lastInsn = insns.get(insns.size() - 1);
Rop opcode = lastInsn.getOpcode();
if (opcode.getBranchingness() != Rop.BRANCH_RETURN && opcode != Rops.THROW) {
throw new RuntimeException("Exit predecessor must end" + " in valid exit statement.");
}
}
Aggregations