From 405520af55ebcd517e721b13a24cb436f7d11e16 Mon Sep 17 00:00:00 2001 From: Christoffer Quist Adamsen Date: Tue, 9 May 2023 18:49:01 +0200 Subject: [PATCH] Prepare inliner for constructor inlining Change-Id: I40daca6a7eaf14c7242afc76ba818327378b6828 --- .../tools/r8/ir/optimize/DefaultInliningOracle.java | 11 ++++++++--- .../com/android/tools/r8/utils/InternalOptions.java | 10 ++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java b/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java index 6f524f6404..79ba9d170d 100644 --- a/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java +++ b/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java @@ -478,6 +478,10 @@ public boolean canInlineInstanceInitializer( ProgramMethod singleTarget, InliningIRProvider inliningIRProvider, WhyAreYouNotInliningReporter whyAreYouNotInliningReporter) { + if (!inlinerOptions.isConstructorInliningEnabled()) { + return false; + } + IRCode inlinee = inliningIRProvider.getInliningIR(invoke, singleTarget); // In the Java VM Specification section "4.10.2.4. Instance Initialization Methods and @@ -517,7 +521,6 @@ public boolean canInlineInstanceInitializer( // ... // } // } - // TODO(b/278679664): Relax requirement (3) when targeting DEX. Value thisValue = inlinee.entryBlock().entry().asArgument().outValue(); List initCallsOnThis = new ArrayList<>(); @@ -526,8 +529,10 @@ public boolean canInlineInstanceInitializer( InvokeDirect initCall = instruction.asInvokeDirect(); Value receiver = initCall.getReceiver().getAliasedValue(); if (receiver == thisValue) { - // The () call of the constructor must be on the same class. - if (calleeMethodHolder != initCall.getInvokedMethod().getHolderType()) { + // The () call of the constructor must be on the same class when targeting the JVM + // and Dalvik. + if (!options.canInitNewInstanceUsingSuperclassConstructor() + && calleeMethodHolder != initCall.getInvokedMethod().getHolderType()) { whyAreYouNotInliningReporter .reportUnsafeConstructorInliningDueToIndirectConstructorCall(initCall); return false; diff --git a/src/main/java/com/android/tools/r8/utils/InternalOptions.java b/src/main/java/com/android/tools/r8/utils/InternalOptions.java index c8936dddb3..dbd49aa28b 100644 --- a/src/main/java/com/android/tools/r8/utils/InternalOptions.java +++ b/src/main/java/com/android/tools/r8/utils/InternalOptions.java @@ -1626,6 +1626,8 @@ public interface ApplyInliningToInlineePredicate { public static class InlinerOptions { + public boolean enableConstructorInlining = true; + public boolean enableInlining = !parseSystemPropertyForDevelopmentOrDefault("com.android.tools.r8.disableinlining", false); @@ -1683,6 +1685,14 @@ public int getSimpleInliningInstructionLimit() { return 5; } + public boolean isConstructorInliningEnabled() { + return enableConstructorInlining; + } + + public void setEnableConstructorInlining(boolean enableConstructorInlining) { + this.enableConstructorInlining = enableConstructorInlining; + } + public boolean shouldApplyInliningToInlinee( AppView appView, ProgramMethod inlinee, int inliningDepth) { if (applyInliningToInlineePredicateForTesting != null) {