Rewrite Byte/Character/Short/Integer.hashCode to pass through value
The hash code for these types matches the value so we can just eliminate the method call.
Test: tools/test.py --dex_vm all --no-internal -v *Backport*Test*
Test: tools/test.py --no-internal -v *GenerateBackportMethods*
Change-Id: I8cd75074cd5ef436f57b5152aa4126482d4c43d3
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
index 730a743..86417a1 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
@@ -405,7 +405,7 @@
DexString name = factory.createString("hashCode");
DexProto proto = factory.createProto(factory.intType, factory.byteType);
DexMethod method = factory.createMethod(type, proto, name);
- addProvider(new MethodGenerator(method, BackportedMethods::ByteMethods_hashCode));
+ addProvider(new InvokeRewriter(method, NumericMethodRewrites::rewriteAsIdentity));
// Short
type = factory.boxedShortType;
@@ -413,7 +413,7 @@
name = factory.createString("hashCode");
proto = factory.createProto(factory.intType, factory.shortType);
method = factory.createMethod(type, proto, name);
- addProvider(new MethodGenerator(method, BackportedMethods::ShortMethods_hashCode));
+ addProvider(new InvokeRewriter(method, NumericMethodRewrites::rewriteAsIdentity));
// Integer
type = factory.boxedIntType;
@@ -422,7 +422,7 @@
name = factory.createString("hashCode");
proto = factory.createProto(factory.intType, factory.intType);
method = factory.createMethod(type, proto, name);
- addProvider(new MethodGenerator(method, BackportedMethods::IntegerMethods_hashCode));
+ addProvider(new InvokeRewriter(method, NumericMethodRewrites::rewriteAsIdentity));
// int Integer.max(int a, int b)
name = factory.createString("max");
@@ -569,7 +569,7 @@
name = factory.createString("hashCode");
proto = factory.createProto(factory.intType, factory.charType);
method = factory.createMethod(type, proto, name);
- addProvider(new MethodGenerator(method, BackportedMethods::CharacterMethods_hashCode));
+ addProvider(new InvokeRewriter(method, NumericMethodRewrites::rewriteAsIdentity));
// Objects
type = factory.objectsType;
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/backports/BackportedMethods.java b/src/main/java/com/android/tools/r8/ir/desugar/backports/BackportedMethods.java
index d9beda9..796e31e 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/backports/BackportedMethods.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/backports/BackportedMethods.java
@@ -145,19 +145,6 @@
ImmutableList.of());
}
- public static CfCode ByteMethods_hashCode(
- InternalOptions options, DexMethod method, String name) {
- CfLabel label0 = new CfLabel();
- CfLabel label1 = new CfLabel();
- return new CfCode(
- method.holder,
- 1,
- 1,
- ImmutableList.of(label0, new CfLoad(ValueType.INT, 0), new CfReturn(ValueType.INT), label1),
- ImmutableList.of(),
- ImmutableList.of());
- }
-
public static CfCode ByteMethods_toUnsignedInt(
InternalOptions options, DexMethod method, String name) {
CfLabel label0 = new CfLabel();
@@ -216,19 +203,6 @@
ImmutableList.of());
}
- public static CfCode CharacterMethods_hashCode(
- InternalOptions options, DexMethod method, String name) {
- CfLabel label0 = new CfLabel();
- CfLabel label1 = new CfLabel();
- return new CfCode(
- method.holder,
- 1,
- 1,
- ImmutableList.of(label0, new CfLoad(ValueType.INT, 0), new CfReturn(ValueType.INT), label1),
- ImmutableList.of(),
- ImmutableList.of());
- }
-
public static CfCode CharacterMethods_toStringCodepoint(
InternalOptions options, DexMethod method, String name) {
CfLabel label0 = new CfLabel();
@@ -862,19 +836,6 @@
ImmutableList.of());
}
- public static CfCode IntegerMethods_hashCode(
- InternalOptions options, DexMethod method, String name) {
- CfLabel label0 = new CfLabel();
- CfLabel label1 = new CfLabel();
- return new CfCode(
- method.holder,
- 1,
- 1,
- ImmutableList.of(label0, new CfLoad(ValueType.INT, 0), new CfReturn(ValueType.INT), label1),
- ImmutableList.of(),
- ImmutableList.of());
- }
-
public static CfCode IntegerMethods_parseUnsignedInt(
InternalOptions options, DexMethod method, String name) {
CfLabel label0 = new CfLabel();
@@ -5783,19 +5744,6 @@
ImmutableList.of());
}
- public static CfCode ShortMethods_hashCode(
- InternalOptions options, DexMethod method, String name) {
- CfLabel label0 = new CfLabel();
- CfLabel label1 = new CfLabel();
- return new CfCode(
- method.holder,
- 1,
- 1,
- ImmutableList.of(label0, new CfLoad(ValueType.INT, 0), new CfReturn(ValueType.INT), label1),
- ImmutableList.of(),
- ImmutableList.of());
- }
-
public static CfCode ShortMethods_toUnsignedInt(
InternalOptions options, DexMethod method, String name) {
CfLabel label0 = new CfLabel();
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/backports/NumericMethodRewrites.java b/src/main/java/com/android/tools/r8/ir/desugar/backports/NumericMethodRewrites.java
index aef0fc5..86d7494 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/backports/NumericMethodRewrites.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/backports/NumericMethodRewrites.java
@@ -28,6 +28,14 @@
iterator.replaceCurrentInstruction(add);
}
+ public static void rewriteAsIdentity(InvokeMethod invoke, InstructionListIterator iterator,
+ DexItemFactory factory) {
+ List<Value> values = invoke.inValues();
+ assert values.size() == 1;
+ invoke.outValue().replaceUsers(values.get(0));
+ iterator.remove();
+ }
+
private NumericMethodRewrites() {
}
}
diff --git a/src/test/java/com/android/tools/r8/ir/desugar/backports/ByteMethods.java b/src/test/java/com/android/tools/r8/ir/desugar/backports/ByteMethods.java
index 6aaa078..b9f66bb 100644
--- a/src/test/java/com/android/tools/r8/ir/desugar/backports/ByteMethods.java
+++ b/src/test/java/com/android/tools/r8/ir/desugar/backports/ByteMethods.java
@@ -6,10 +6,6 @@
public final class ByteMethods {
- public static int hashCode(byte i) {
- return i;
- }
-
public static int compare(byte a, byte b) {
return (int) a - (int) b;
}
diff --git a/src/test/java/com/android/tools/r8/ir/desugar/backports/CharacterMethods.java b/src/test/java/com/android/tools/r8/ir/desugar/backports/CharacterMethods.java
index ea21398..38c3cad 100644
--- a/src/test/java/com/android/tools/r8/ir/desugar/backports/CharacterMethods.java
+++ b/src/test/java/com/android/tools/r8/ir/desugar/backports/CharacterMethods.java
@@ -6,10 +6,6 @@
public final class CharacterMethods {
- public static int hashCode(char i) {
- return i;
- }
-
public static int compare(char a, char b) {
return (int) a - (int) b;
}
diff --git a/src/test/java/com/android/tools/r8/ir/desugar/backports/IntegerMethods.java b/src/test/java/com/android/tools/r8/ir/desugar/backports/IntegerMethods.java
index 8cc53b7..0560b97 100644
--- a/src/test/java/com/android/tools/r8/ir/desugar/backports/IntegerMethods.java
+++ b/src/test/java/com/android/tools/r8/ir/desugar/backports/IntegerMethods.java
@@ -6,10 +6,6 @@
public final class IntegerMethods {
- public static int hashCode(int i) {
- return i;
- }
-
public static int compare(int a, int b) {
return a == b ? 0 : a < b ? -1 : 1;
}
diff --git a/src/test/java/com/android/tools/r8/ir/desugar/backports/ShortMethods.java b/src/test/java/com/android/tools/r8/ir/desugar/backports/ShortMethods.java
index 23fadb4..18550aa 100644
--- a/src/test/java/com/android/tools/r8/ir/desugar/backports/ShortMethods.java
+++ b/src/test/java/com/android/tools/r8/ir/desugar/backports/ShortMethods.java
@@ -6,10 +6,6 @@
public final class ShortMethods {
- public static int hashCode(short i) {
- return i;
- }
-
public static int compare(short a, short b) {
return (int) a - (int) b;
}