summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Hiroshi Yamauchi <yamauchi@google.com> 2016-02-11 21:39:41 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2016-02-11 21:39:41 +0000
commitd3caabd4f85f86dd744da432993e12935d843a83 (patch)
treeb7fb9f36c2725e78e0ba7f61fc8ff14c60666077
parentd3df33e6c24e3cd62991b2a65833f16dc05a17b8 (diff)
parent86503785cd6414b8692e5c83cadaa2972b6a099b (diff)
Merge "Fix x86-64 Baker's read barrier fast path for CheckCast."
-rw-r--r--compiler/optimizing/code_generator_x86_64.cc75
-rw-r--r--compiler/optimizing/graph_visualizer.cc2
-rw-r--r--compiler/optimizing/nodes.cc22
-rw-r--r--compiler/optimizing/nodes.h2
-rw-r--r--test/573-checker-checkcast-regression/expected.txt1
-rw-r--r--test/573-checker-checkcast-regression/info.txt4
-rw-r--r--test/573-checker-checkcast-regression/src/Main.java49
7 files changed, 144 insertions, 11 deletions
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 99396cd983..4f0f5f0ff8 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -5840,19 +5840,20 @@ void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
is_type_check_slow_path_fatal);
codegen_->AddSlowPath(type_check_slow_path);
- NearLabel done;
- // Avoid null check if we know obj is not null.
- if (instruction->MustDoNullCheck()) {
- __ testl(obj, obj);
- __ j(kEqual, &done);
- }
-
- // /* HeapReference<Class> */ temp = obj->klass_
- GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
-
switch (type_check_kind) {
case TypeCheckKind::kExactCheck:
case TypeCheckKind::kArrayCheck: {
+ NearLabel done;
+ // Avoid null check if we know obj is not null.
+ if (instruction->MustDoNullCheck()) {
+ __ testl(obj, obj);
+ __ j(kEqual, &done);
+ }
+
+ // /* HeapReference<Class> */ temp = obj->klass_
+ GenerateReferenceLoadTwoRegisters(
+ instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
+
if (cls.IsRegister()) {
__ cmpl(temp, cls.AsRegister<CpuRegister>());
} else {
@@ -5862,10 +5863,22 @@ void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
// Jump to slow path for throwing the exception or doing a
// more involved array check.
__ j(kNotEqual, type_check_slow_path->GetEntryLabel());
+ __ Bind(&done);
break;
}
case TypeCheckKind::kAbstractClassCheck: {
+ NearLabel done;
+ // Avoid null check if we know obj is not null.
+ if (instruction->MustDoNullCheck()) {
+ __ testl(obj, obj);
+ __ j(kEqual, &done);
+ }
+
+ // /* HeapReference<Class> */ temp = obj->klass_
+ GenerateReferenceLoadTwoRegisters(
+ instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
+
// If the class is abstract, we eagerly fetch the super class of the
// object to avoid doing a comparison we know will fail.
NearLabel loop, compare_classes;
@@ -5896,10 +5909,22 @@ void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
__ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
}
__ j(kNotEqual, &loop);
+ __ Bind(&done);
break;
}
case TypeCheckKind::kClassHierarchyCheck: {
+ NearLabel done;
+ // Avoid null check if we know obj is not null.
+ if (instruction->MustDoNullCheck()) {
+ __ testl(obj, obj);
+ __ j(kEqual, &done);
+ }
+
+ // /* HeapReference<Class> */ temp = obj->klass_
+ GenerateReferenceLoadTwoRegisters(
+ instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
+
// Walk over the class hierarchy to find a match.
NearLabel loop;
__ Bind(&loop);
@@ -5927,10 +5952,26 @@ void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
GenerateReferenceLoadTwoRegisters(
instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
__ jmp(type_check_slow_path->GetEntryLabel());
+ __ Bind(&done);
break;
}
case TypeCheckKind::kArrayObjectCheck: {
+ // We cannot use a NearLabel here, as its range might be too
+ // short in some cases when read barriers are enabled. This has
+ // been observed for instance when the code emitted for this
+ // case uses high x86-64 registers (R8-R15).
+ Label done;
+ // Avoid null check if we know obj is not null.
+ if (instruction->MustDoNullCheck()) {
+ __ testl(obj, obj);
+ __ j(kEqual, &done);
+ }
+
+ // /* HeapReference<Class> */ temp = obj->klass_
+ GenerateReferenceLoadTwoRegisters(
+ instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
+
// Do an exact check.
NearLabel check_non_primitive_component_type;
if (cls.IsRegister()) {
@@ -5969,11 +6010,23 @@ void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
GenerateReferenceLoadTwoRegisters(
instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
__ jmp(type_check_slow_path->GetEntryLabel());
+ __ Bind(&done);
break;
}
case TypeCheckKind::kUnresolvedCheck:
case TypeCheckKind::kInterfaceCheck:
+ NearLabel done;
+ // Avoid null check if we know obj is not null.
+ if (instruction->MustDoNullCheck()) {
+ __ testl(obj, obj);
+ __ j(kEqual, &done);
+ }
+
+ // /* HeapReference<Class> */ temp = obj->klass_
+ GenerateReferenceLoadTwoRegisters(
+ instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
+
// We always go into the type check slow path for the unresolved
// and interface check cases.
//
@@ -5992,9 +6045,9 @@ void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
// call to the runtime not using a type checking slow path).
// This should also be beneficial for the other cases above.
__ jmp(type_check_slow_path->GetEntryLabel());
+ __ Bind(&done);
break;
}
- __ Bind(&done);
__ Bind(type_check_slow_path->GetExitLabel());
}
diff --git a/compiler/optimizing/graph_visualizer.cc b/compiler/optimizing/graph_visualizer.cc
index 9d796c1004..63b8fd05c0 100644
--- a/compiler/optimizing/graph_visualizer.cc
+++ b/compiler/optimizing/graph_visualizer.cc
@@ -368,11 +368,13 @@ class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
}
void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
+ StartAttributeStream("check_kind") << check_cast->GetTypeCheckKind();
StartAttributeStream("must_do_null_check") << std::boolalpha
<< check_cast->MustDoNullCheck() << std::noboolalpha;
}
void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
+ StartAttributeStream("check_kind") << instance_of->GetTypeCheckKind();
StartAttributeStream("must_do_null_check") << std::boolalpha
<< instance_of->MustDoNullCheck() << std::noboolalpha;
}
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index f269885907..453571451c 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -2387,4 +2387,26 @@ std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs) {
return os;
}
+std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs) {
+ switch (rhs) {
+ case TypeCheckKind::kUnresolvedCheck:
+ return os << "unresolved_check";
+ case TypeCheckKind::kExactCheck:
+ return os << "exact_check";
+ case TypeCheckKind::kClassHierarchyCheck:
+ return os << "class_hierarchy_check";
+ case TypeCheckKind::kAbstractClassCheck:
+ return os << "abstract_class_check";
+ case TypeCheckKind::kInterfaceCheck:
+ return os << "interface_check";
+ case TypeCheckKind::kArrayObjectCheck:
+ return os << "array_object_check";
+ case TypeCheckKind::kArrayCheck:
+ return os << "array_check";
+ default:
+ LOG(FATAL) << "Unknown TypeCheckKind: " << static_cast<int>(rhs);
+ UNREACHABLE();
+ }
+}
+
} // namespace art
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index daec096f3e..7f463a36e9 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -5451,6 +5451,8 @@ enum class TypeCheckKind {
kArrayCheck // No optimization yet when checking against a generic array.
};
+std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
+
class HInstanceOf : public HExpression<2> {
public:
HInstanceOf(HInstruction* object,
diff --git a/test/573-checker-checkcast-regression/expected.txt b/test/573-checker-checkcast-regression/expected.txt
new file mode 100644
index 0000000000..b8626c4cff
--- /dev/null
+++ b/test/573-checker-checkcast-regression/expected.txt
@@ -0,0 +1 @@
+4
diff --git a/test/573-checker-checkcast-regression/info.txt b/test/573-checker-checkcast-regression/info.txt
new file mode 100644
index 0000000000..74a6d6ec00
--- /dev/null
+++ b/test/573-checker-checkcast-regression/info.txt
@@ -0,0 +1,4 @@
+Regression test for the x86-64 Baker's read barrier fast path compiler
+instrumentation of CheckCasts, where we used to use an
+art::x86_64::NearLabel, the range of which was sometimes too short
+with Baker's read barriers enabled.
diff --git a/test/573-checker-checkcast-regression/src/Main.java b/test/573-checker-checkcast-regression/src/Main.java
new file mode 100644
index 0000000000..473a2b164e
--- /dev/null
+++ b/test/573-checker-checkcast-regression/src/Main.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2016 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 {
+
+ public static void main(String[] args) {
+ Object[] array = { new Integer(1), new Integer(2), new Integer(3) };
+ int result = test(array, 0, 2);
+ System.out.println(result);
+ }
+
+ // This test method uses two integers (`index1` and `index2`) to
+ // force the register allocator to use some high registers (R8-R15)
+ // on x86-64 in the code generated for the first CheckCast (which
+ // converts `new_array` to an `Object[]`), so as to produce code
+ // containing a conditional jump whose offset does not fit in a
+ // NearLabel when using Baker's read barrier fast path (because
+ // x86-64 instructions using these high registers have a larger
+ // encoding).
+ //
+ // The intent of this artifical constraint is to ensure the initial
+ // failure is properly tested by this regression test.
+
+ /// CHECK-START: int Main.test(java.lang.Object, int, int) register (after)
+ /// CHECK-DAG: CheckCast check_kind:array_object_check
+ /// CHECK-DAG: CheckCast check_kind:exact_check
+ /// CHECK-DAG: CheckCast check_kind:exact_check
+
+ static public int test(Object new_array, int index1, int index2) {
+ Object[] objectArray = (Object[]) new_array;
+ Integer integer1 = (Integer) objectArray[index1];
+ Integer integer2 = (Integer) objectArray[index2];
+ return integer1.intValue() + integer2.intValue();
+ }
+
+}