From 744a1c687fb92050828e188838b0ce5e0474f94a Mon Sep 17 00:00:00 2001 From: David Brazdil Date: Mon, 28 Dec 2015 10:53:34 +0000 Subject: [PATCH] ART: Don't set initial RTI for BoundType if input untyped ReferenceTypePropagation will create a BoundType with upper bound [Object, inexact, not null] for each if-not-null branch. The logic setting its initial RTI will, however, set it straight to Object if the input is untyped (loop phi or its derivate). This patch changes the logic to leave the BoundType untyped and set it during fix-point iteration. Bug: 26330326 Change-Id: Ic492e2179a4c51f577908e60fbcf70d728b98a6f --- compiler/optimizing/reference_type_propagation.cc | 15 +++++-- test/559-checker-rtp-ifnotnull/expected.txt | 0 test/559-checker-rtp-ifnotnull/info.txt | 2 + test/559-checker-rtp-ifnotnull/src/Main.java | 53 +++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/559-checker-rtp-ifnotnull/expected.txt create mode 100644 test/559-checker-rtp-ifnotnull/info.txt create mode 100644 test/559-checker-rtp-ifnotnull/src/Main.java diff --git a/compiler/optimizing/reference_type_propagation.cc b/compiler/optimizing/reference_type_propagation.cc index e55061c92..1c25e4824 100644 --- a/compiler/optimizing/reference_type_propagation.cc +++ b/compiler/optimizing/reference_type_propagation.cc @@ -583,11 +583,18 @@ void RTPVisitor::VisitBoundType(HBoundType* instr) { if (class_rti.GetTypeHandle()->CannotBeAssignedFromOtherTypes()) { instr->SetReferenceTypeInfo( ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ true)); - } else if (obj_rti.IsValid() && class_rti.IsSupertypeOf(obj_rti)) { - instr->SetReferenceTypeInfo(obj_rti); + } else if (obj_rti.IsValid()) { + if (class_rti.IsSupertypeOf(obj_rti)) { + // Object type is more specific. + instr->SetReferenceTypeInfo(obj_rti); + } else { + // Upper bound is more specific. + instr->SetReferenceTypeInfo( + ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false)); + } } else { - instr->SetReferenceTypeInfo( - ReferenceTypeInfo::Create(class_rti.GetTypeHandle(), /* is_exact */ false)); + // Object not typed yet. Leave BoundType untyped for now rather than + // assign the type conservatively. } instr->SetCanBeNull(obj->CanBeNull() && instr->GetUpperCanBeNull()); } else { diff --git a/test/559-checker-rtp-ifnotnull/expected.txt b/test/559-checker-rtp-ifnotnull/expected.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/559-checker-rtp-ifnotnull/info.txt b/test/559-checker-rtp-ifnotnull/info.txt new file mode 100644 index 000000000..c08aa0c5c --- /dev/null +++ b/test/559-checker-rtp-ifnotnull/info.txt @@ -0,0 +1,2 @@ +Tests that BoundType created for if-not-null does not force untyped loop phis +to Object. \ No newline at end of file diff --git a/test/559-checker-rtp-ifnotnull/src/Main.java b/test/559-checker-rtp-ifnotnull/src/Main.java new file mode 100644 index 000000000..8f401292d --- /dev/null +++ b/test/559-checker-rtp-ifnotnull/src/Main.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +public class Main { + + /// CHECK-START: void Main.boundTypeForIfNotNull() ssa_builder (after) + /// CHECK-DAG: <> CurrentMethod + /// CHECK-DAG: <> NullConstant + /// CHECK-DAG: <> IntConstant 5 + /// CHECK-DAG: <> IntConstant 10 + + /// CHECK-DAG: InvokeVirtual [<>] + /// CHECK-DAG: <> NullCheck [<>] klass:int[] + /// CHECK-DAG: <> Phi [<>,<>] klass:int[] + + /// CHECK-DAG: <> BoundType [<>] klass:int[] can_be_null:false + /// CHECK-DAG: <> NewArray [<>,<>] klass:int[] + /// CHECK-DAG: <> Phi [<>,<>] klass:int[] + + /// CHECK-DAG: <> NewArray [<>,<>] klass:int[] + /// CHECK-DAG: <> Phi [<>,<>] klass:int[] + + public static void boundTypeForIfNotNull() { + int[] array = null; + for (int i = -1; i < 10; ++i) { + if (array == null) { + array = new int[5]; + } else { + if (i == 5) { + array = new int[10]; + } + array[i] = i; + } + } + array.hashCode(); + } + + public static void main(String[] args) { } +} -- 2.11.0