Search in sources :

Example 1 with InsnList

use of jadx.core.utils.InsnList in project jadx by skylot.

the class CodeShrinker method shrinkBlock.

private static void shrinkBlock(MethodNode mth, BlockNode block) {
    if (block.getInstructions().isEmpty()) {
        return;
    }
    InsnList insnList = new InsnList(block.getInstructions());
    int insnCount = insnList.size();
    List<ArgsInfo> argsList = new ArrayList<ArgsInfo>(insnCount);
    for (int i = 0; i < insnCount; i++) {
        argsList.add(new ArgsInfo(insnList.get(i), argsList, i));
    }
    List<WrapInfo> wrapList = new ArrayList<WrapInfo>();
    for (ArgsInfo argsInfo : argsList) {
        List<RegisterArg> args = argsInfo.getArgs();
        if (args.isEmpty()) {
            continue;
        }
        ListIterator<RegisterArg> it = args.listIterator(args.size());
        while (it.hasPrevious()) {
            RegisterArg arg = it.previous();
            //				if (arg.getName() != null) {
            //					continue;
            //				}
            SSAVar sVar = arg.getSVar();
            // allow inline only one use arg or 'this'
            if (sVar == null || sVar.getVariableUseCount() != 1 && !arg.isThis() || sVar.contains(AFlag.DONT_INLINE)) {
                continue;
            }
            InsnNode assignInsn = sVar.getAssign().getParentInsn();
            if (assignInsn == null || assignInsn.contains(AFlag.DONT_INLINE)) {
                continue;
            }
            int assignPos = insnList.getIndex(assignInsn);
            if (assignPos != -1) {
                WrapInfo wrapInfo = argsInfo.checkInline(assignPos, arg);
                if (wrapInfo != null) {
                    wrapList.add(wrapInfo);
                }
            } else {
                // another block
                BlockNode assignBlock = BlockUtils.getBlockByInsn(mth, assignInsn);
                if (assignBlock != null && assignInsn != arg.getParentInsn() && canMoveBetweenBlocks(assignInsn, assignBlock, block, argsInfo.getInsn())) {
                    inline(arg, assignInsn, assignBlock);
                }
            }
        }
    }
    if (!wrapList.isEmpty()) {
        for (WrapInfo wrapInfo : wrapList) {
            inline(wrapInfo.getArg(), wrapInfo.getInsn(), block);
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ArrayList(java.util.ArrayList) InsnList(jadx.core.utils.InsnList) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) SSAVar(jadx.core.dex.instructions.args.SSAVar)

Aggregations

RegisterArg (jadx.core.dex.instructions.args.RegisterArg)1 SSAVar (jadx.core.dex.instructions.args.SSAVar)1 BlockNode (jadx.core.dex.nodes.BlockNode)1 InsnNode (jadx.core.dex.nodes.InsnNode)1 InsnList (jadx.core.utils.InsnList)1 ArrayList (java.util.ArrayList)1