use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class Form21s method isCompatible.
/** {@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());
}
use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class Form22b method isCompatible.
/** {@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());
}
use of com.taobao.android.dx.rop.cst.Constant in project atlas by alibaba.
the class CodeItem method addContents.
/** {@inheritDoc} */
public void addContents(DexFile file) {
MixedItemSection byteData = file.getByteData();
TypeIdsSection typeIds = file.getTypeIds();
if (code.hasPositions() || code.hasLocals()) {
debugInfo = new DebugInfoItem(code, isStatic, ref);
byteData.add(debugInfo);
}
if (code.hasAnyCatches()) {
for (Type type : code.getCatchTypes()) {
typeIds.intern(type);
}
catches = new CatchStructs(code);
}
for (Constant c : code.getInsnConstants()) {
file.internIfAppropriate(c);
}
}
use of com.taobao.android.dx.rop.cst.Constant 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);
}
}
use of com.taobao.android.dx.rop.cst.Constant 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;
}
Aggregations