Skip to content

Commit

Permalink
Prepare inliner for constructor inlining
Browse files Browse the repository at this point in the history
Change-Id: I40daca6a7eaf14c7242afc76ba818327378b6828
  • Loading branch information
christofferqa committed May 9, 2023
1 parent 34ac3d6 commit 405520a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -517,7 +521,6 @@ public boolean canInlineInstanceInitializer(
// ...
// }
// }
// TODO(b/278679664): Relax requirement (3) when targeting DEX.
Value thisValue = inlinee.entryBlock().entry().asArgument().outValue();

List<InvokeDirect> initCallsOnThis = new ArrayList<>();
Expand All @@ -526,8 +529,10 @@ public boolean canInlineInstanceInitializer(
InvokeDirect initCall = instruction.asInvokeDirect();
Value receiver = initCall.getReceiver().getAliasedValue();
if (receiver == thisValue) {
// The <init>() call of the constructor must be on the same class.
if (calleeMethodHolder != initCall.getInvokedMethod().getHolderType()) {
// The <init>() 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;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/android/tools/r8/utils/InternalOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 405520a

Please sign in to comment.