Refactor use of DexType.toBaseType()
* No need to check for array type before calling DexType.toBaseType()
* Let Enqueuer.markTypeAsLive() filter out primitive types
Bug:
Change-Id: Ibfec7a8c26f6e8c52427f2b49fe54481a13f2b0b
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java b/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java
index 7b9ce55..332094c 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java
@@ -499,10 +499,7 @@
}
private void addClassInitializerTarget(DexType type) {
- if (type.isArrayType()) {
- type = type.toBaseType(appInfo.dexItemFactory);
- }
- DexClass clazz = appInfo.definitionFor(type);
+ DexClass clazz = appInfo.definitionFor(type.toBaseType(appInfo.dexItemFactory));
if (clazz != null) {
addClassInitializerTarget(clazz);
}
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index 4bc7f71..bdac04a 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -1633,10 +1633,7 @@
// If the cast type is not accessible in the current context, we should not remove the cast
// in order to preserve IllegalAccessError. Note that JVM and ART behave differently: see
// {@link com.android.tools.r8.ir.optimize.checkcast.IllegalAccessErrorTest}.
- DexType baseCastType = castType;
- if (baseCastType.isArrayType()) {
- baseCastType = baseCastType.toBaseType(appInfo.dexItemFactory);
- }
+ DexType baseCastType = castType.toBaseType(appInfo.dexItemFactory);
DexClass castClass = definitionFor(baseCastType);
if (castClass != null) {
ConstraintWithTarget classVisibility = ConstraintWithTarget.deriveConstraint(
diff --git a/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java b/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
index 459fe7a..0c47df1 100644
--- a/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
+++ b/src/main/java/com/android/tools/r8/shaking/AnnotationRemover.java
@@ -82,10 +82,7 @@
}
private boolean isAnnotationTypeLive(DexAnnotation annotation) {
- DexType annotationType = annotation.annotation.type;
- if (annotationType.isArrayType()) {
- annotationType = annotationType.toBaseType(appInfo.dexItemFactory);
- }
+ DexType annotationType = annotation.annotation.type.toBaseType(appInfo.dexItemFactory);
DexClass definition = appInfo.definitionFor(annotationType);
// TODO(73102187): How to handle annotations without definition.
if (options.enableTreeShaking && definition == null) {
diff --git a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
index 7a0aacb..9145de1 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -297,10 +297,7 @@
private <S extends DexItem, T extends Descriptor<S, T>> boolean registerItemWithTarget(
Map<DexType, Set<T>> seen, T item) {
- DexType holder = item.getHolder();
- if (holder.isArrayType()) {
- holder = holder.toBaseType(appInfo.dexItemFactory);
- }
+ DexType holder = item.getHolder().toBaseType(appInfo.dexItemFactory);
if (!holder.isClassType()) {
return false;
}
@@ -310,10 +307,7 @@
private <S extends DexItem, T extends Descriptor<S, T>> boolean registerItemWithTargetAndContext(
Map<DexType, Set<TargetWithContext<T>>> seen, T item, DexEncodedMethod context) {
- DexType holder = item.getHolder();
- if (holder.isArrayType()) {
- holder = holder.toBaseType(appInfo.dexItemFactory);
- }
+ DexType holder = item.getHolder().toBaseType(appInfo.dexItemFactory);
if (!holder.isClassType()) {
return false;
}
@@ -495,12 +489,8 @@
@Override
public boolean registerTypeReference(DexType type) {
- DexType baseType = type.toBaseType(appInfo.dexItemFactory);
- if (baseType.isClassType()) {
- markTypeAsLive(baseType);
- return true;
- }
- return false;
+ markTypeAsLive(type);
+ return true;
}
@Override
@@ -652,7 +642,11 @@
//
private void markTypeAsLive(DexType type) {
- assert type.isClassType();
+ type = type.toBaseType(appInfo.dexItemFactory);
+ if (!type.isClassType()) {
+ // Ignore primitive types.
+ return;
+ }
if (liveTypes.add(type)) {
if (Log.ENABLED) {
Log.verbose(getClass(), "Type `%s` has become live.", type);
@@ -951,13 +945,8 @@
// Mark the type live here, so that the class exists at runtime. Note that this also marks all
// supertypes as live, so even if the field is actually on a supertype, its class will be live.
markTypeAsLive(field.clazz);
- DexType fieldType = field.type;
- if (fieldType.isArrayType()) {
- fieldType = fieldType.toBaseType(appInfo.dexItemFactory);
- }
- if (fieldType.isClassType()) {
- markTypeAsLive(fieldType);
- }
+ markTypeAsLive(field.type);
+
// Find the actual field.
DexEncodedField encodedField = appInfo.resolveFieldOn(field.clazz, field);
if (encodedField == null) {
@@ -987,13 +976,7 @@
private void markInstanceFieldAsLive(DexEncodedField field, KeepReason reason) {
assert field != null;
markTypeAsLive(field.field.clazz);
- DexType fieldType = field.field.type;
- if (fieldType.isArrayType()) {
- fieldType = fieldType.toBaseType(appInfo.dexItemFactory);
- }
- if (fieldType.isClassType()) {
- markTypeAsLive(fieldType);
- }
+ markTypeAsLive(field.field.type);
if (Log.ENABLED) {
Log.verbose(getClass(), "Adding instance field `%s` to live set.", field.field);
}
@@ -1091,10 +1074,7 @@
// like an invoke on a direct subtype of java.lang.Object that has no further subtypes.
// As it has no subtypes, it cannot affect liveness of the program we are processing.
// Ergo, we can ignore it. We need to make sure that the element type is available, though.
- DexType baseType = method.holder.toBaseType(appInfo.dexItemFactory);
- if (baseType.isClassType()) {
- markTypeAsLive(baseType);
- }
+ markTypeAsLive(method.holder);
return;
}
DexClass holder = appInfo.definitionFor(method.holder);
@@ -1429,20 +1409,9 @@
private void markParameterAndReturnTypesAsLive(DexEncodedMethod method) {
for (DexType parameterType : method.method.proto.parameters.values) {
- if (parameterType.isArrayType()) {
- parameterType = parameterType.toBaseType(appInfo.dexItemFactory);
- }
- if (parameterType.isClassType()) {
- markTypeAsLive(parameterType);
- }
+ markTypeAsLive(parameterType);
}
- DexType returnType = method.method.proto.returnType;
- if (returnType.isArrayType()) {
- returnType = returnType.toBaseType(appInfo.dexItemFactory);
- }
- if (returnType.isClassType()) {
- markTypeAsLive(returnType);
- }
+ markTypeAsLive(method.method.proto.returnType);
}
private void collectProguardCompatibilityRule(KeepReason reason) {
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
index 03272a5..66801c0 100644
--- a/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMerger.java
@@ -557,9 +557,7 @@
}
private boolean typeMayReferenceMergedSourceOrTarget(DexType type) {
- if (type.isArrayType()) {
- type = type.toBaseType(appInfo.dexItemFactory);
- }
+ type = type.toBaseType(appInfo.dexItemFactory);
if (type.isClassType()) {
if (mergeeCandidates.contains(type)) {
return true;