blob: 99851febb7abefe8ad92efec5782fbf618a597f2 [file] [log] [blame]
// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.ir.optimize.library;
import static com.android.tools.r8.ir.analysis.type.Nullability.definitelyNotNull;
import com.android.tools.r8.graph.AppView;
import com.android.tools.r8.graph.DexEncodedMethod;
import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.ir.analysis.type.TypeElement;
import com.android.tools.r8.ir.code.Assume;
import com.android.tools.r8.ir.code.Assume.DynamicTypeAssumption;
import com.android.tools.r8.ir.code.ConstClass;
import com.android.tools.r8.ir.code.IRCode;
import com.android.tools.r8.ir.code.InstructionListIterator;
import com.android.tools.r8.ir.code.InvokeMethod;
import com.android.tools.r8.ir.code.Position;
import com.android.tools.r8.ir.code.Value;
import java.util.Set;
public class EnumMethodOptimizer implements LibraryMethodModelCollection {
private final AppView<?> appView;
EnumMethodOptimizer(AppView<?> appView) {
this.appView = appView;
}
@Override
public DexType getType() {
return appView.dexItemFactory().enumType;
}
@Override
public void optimize(
IRCode code,
InstructionListIterator instructionIterator,
InvokeMethod invoke,
DexEncodedMethod singleTarget,
Set<Value> affectedValues) {
if (singleTarget.method == appView.dexItemFactory().enumMethods.valueOf
&& invoke.inValues().get(0).isConstClass()) {
insertAssumeDynamicType(code, instructionIterator, invoke);
}
}
private void insertAssumeDynamicType(
IRCode code, InstructionListIterator instructionIterator, InvokeMethod invoke) {
// TODO(b/152516470): Support unboxing enums with Enum#valueOf in try-catch.
if (invoke.getBlock().hasCatchHandlers()) {
return;
}
ConstClass constClass = invoke.inValues().get(0).getConstInstruction().asConstClass();
TypeElement dynamicUpperBoundType =
TypeElement.fromDexType(constClass.getValue(), definitelyNotNull(), appView);
Value outValue = invoke.outValue();
// Replace usages of out-value by the out-value of the AssumeDynamicType instruction.
Value specializedOutValue = code.createValue(outValue.getType(), outValue.getLocalInfo());
outValue.replaceUsers(specializedOutValue);
// Insert AssumeDynamicType instruction.
Assume<DynamicTypeAssumption> assumeInstruction =
Assume.createAssumeDynamicTypeInstruction(
dynamicUpperBoundType, null, specializedOutValue, outValue, invoke, appView);
assumeInstruction.setPosition(appView.options().debug ? invoke.getPosition() : Position.none());
instructionIterator.add(assumeInstruction);
}
}