Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate constants in useless array assignment and reference #115

Open
CalebFenton opened this issue Oct 5, 2018 · 2 comments
Open

Propagate constants in useless array assignment and reference #115

CalebFenton opened this issue Oct 5, 2018 · 2 comments

Comments

@CalebFenton
Copy link
Owner

Here's a short example:

Object[] arrayOfObject = new Object[2];
arrayOfObject[0] = "hello";
arrayOfObject[1] = world";
someMethod(arrayOfObject[0], arrayOfObject[1])

I think this could work by replacing aget on a known array with a constant-izable value with the constant op equivalent. Dead code analysis would then notice arrayOfObject isn't used. For an easy example of this, see #94.

@CalebFenton
Copy link
Owner Author

#94 actually contains a harder version of this where the array elements are unknown. But it can still be optimized assuming the register isn't reused between aput and aget, which will probably typically be the case with code that's been unreflected.

@CalebFenton
Copy link
Owner Author

CalebFenton commented Oct 5, 2018

Here's how it might look in Smali:

.method protected findClass(Ljava/lang/String;)Ljava/lang/Class;
    .registers 8
    iget-object v0, p0, Lo/We;->c:Ljava/lang/Object;

    const/4 v1, 0x2
    new-array v1, v1, [Ljava/lang/Object;

    const/4 v2, 0x0
    aput-object p1, v1, v2

    const/4 v2, 0x1
    aput-object p0, v1, v2

    const/4 v2, 0x0
    aget-object v2, v1, v2
    check-cast v2, Ljava/lang/String;

    const/4 v3, 0x1
    aget-object v3, v1, v3
    check-cast v3, Ljava/lang/ClassLoader;

    invoke-virtual {v0, v2, v3}, Ldalvik/system/DexFile;->loadClass(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/Class;
    move-result-object v0

    # ...
.end method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant