blob: 028b6d3b7925b3eb00d651413f4e0cfcdbcfc8f0 [file] [log] [blame]
Aart Bik8b3f9b22016-04-06 11:22:12 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "reference_type_propagation.h"
18
Aart Bik8b3f9b22016-04-06 11:22:12 -070019#include "base/arena_allocator.h"
20#include "builder.h"
21#include "nodes.h"
22#include "object_lock.h"
23#include "optimizing_unit_test.h"
Aart Bik8b3f9b22016-04-06 11:22:12 -070024
25namespace art {
26
27/**
28 * Fixture class for unit testing the ReferenceTypePropagation phase. Used to verify the
29 * functionality of methods and situations that are hard to set up with checker tests.
30 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010031class ReferenceTypePropagationTest : public OptimizingUnitTest {
Aart Bik8b3f9b22016-04-06 11:22:12 -070032 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +010033 ReferenceTypePropagationTest() : graph_(CreateGraph()), propagation_(nullptr) { }
Aart Bik8b3f9b22016-04-06 11:22:12 -070034
35 ~ReferenceTypePropagationTest() { }
36
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070037 void SetupPropagation(VariableSizedHandleScope* handles) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070038 graph_->InitializeInexactObjectRTI(handles);
Vladimir Markoca6fff82017-10-03 14:49:14 +010039 propagation_ = new (GetAllocator()) ReferenceTypePropagation(graph_,
40 Handle<mirror::ClassLoader>(),
41 Handle<mirror::DexCache>(),
42 handles,
43 true,
44 "test_prop");
Aart Bik8b3f9b22016-04-06 11:22:12 -070045 }
46
47 // Relay method to merge type in reference type propagation.
48 ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 const ReferenceTypeInfo& b) REQUIRES_SHARED(Locks::mutator_lock_) {
Mads Ager16e52892017-07-14 13:11:37 +020050 return propagation_->MergeTypes(a, b, &propagation_->handle_cache_);
Aart Bik8b3f9b22016-04-06 11:22:12 -070051 }
52
53 // Helper method to construct an invalid type.
54 ReferenceTypeInfo InvalidType() {
55 return ReferenceTypeInfo::CreateInvalid();
56 }
57
58 // Helper method to construct the Object type.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 ReferenceTypeInfo ObjectType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070060 return ReferenceTypeInfo::Create(propagation_->handle_cache_.GetObjectClassHandle(), is_exact);
61 }
62
63 // Helper method to construct the String type.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 ReferenceTypeInfo StringType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070065 return ReferenceTypeInfo::Create(propagation_->handle_cache_.GetStringClassHandle(), is_exact);
66 }
67
Aart Bik8b3f9b22016-04-06 11:22:12 -070068 // General building fields.
Aart Bik8b3f9b22016-04-06 11:22:12 -070069 HGraph* graph_;
70
71 ReferenceTypePropagation* propagation_;
72};
73
74//
75// The actual ReferenceTypePropgation unit tests.
76//
77
78TEST_F(ReferenceTypePropagationTest, ProperSetup) {
79 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070080 VariableSizedHandleScope handles(soa.Self());
Aart Bik8b3f9b22016-04-06 11:22:12 -070081 SetupPropagation(&handles);
82
83 EXPECT_TRUE(propagation_ != nullptr);
84 EXPECT_TRUE(graph_->GetInexactObjectRti().IsEqual(ObjectType(false)));
85}
86
87TEST_F(ReferenceTypePropagationTest, MergeInvalidTypes) {
88 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070089 VariableSizedHandleScope handles(soa.Self());
Aart Bik8b3f9b22016-04-06 11:22:12 -070090 SetupPropagation(&handles);
91
92 // Two invalid types.
93 ReferenceTypeInfo t1(MergeTypes(InvalidType(), InvalidType()));
94 EXPECT_FALSE(t1.IsValid());
95 EXPECT_FALSE(t1.IsExact());
96 EXPECT_TRUE(t1.IsEqual(InvalidType()));
97
98 // Valid type on right.
99 ReferenceTypeInfo t2(MergeTypes(InvalidType(), ObjectType()));
100 EXPECT_TRUE(t2.IsValid());
101 EXPECT_TRUE(t2.IsExact());
102 EXPECT_TRUE(t2.IsEqual(ObjectType()));
103 ReferenceTypeInfo t3(MergeTypes(InvalidType(), StringType()));
104 EXPECT_TRUE(t3.IsValid());
105 EXPECT_TRUE(t3.IsExact());
106 EXPECT_TRUE(t3.IsEqual(StringType()));
107
108 // Valid type on left.
109 ReferenceTypeInfo t4(MergeTypes(ObjectType(), InvalidType()));
110 EXPECT_TRUE(t4.IsValid());
111 EXPECT_TRUE(t4.IsExact());
112 EXPECT_TRUE(t4.IsEqual(ObjectType()));
113 ReferenceTypeInfo t5(MergeTypes(StringType(), InvalidType()));
114 EXPECT_TRUE(t5.IsValid());
115 EXPECT_TRUE(t5.IsExact());
116 EXPECT_TRUE(t5.IsEqual(StringType()));
117}
118
Aart Bik8b3f9b22016-04-06 11:22:12 -0700119TEST_F(ReferenceTypePropagationTest, MergeValidTypes) {
120 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700121 VariableSizedHandleScope handles(soa.Self());
Aart Bik8b3f9b22016-04-06 11:22:12 -0700122 SetupPropagation(&handles);
123
124 // Same types.
125 ReferenceTypeInfo t1(MergeTypes(ObjectType(), ObjectType()));
126 EXPECT_TRUE(t1.IsValid());
127 EXPECT_TRUE(t1.IsExact());
128 EXPECT_TRUE(t1.IsEqual(ObjectType()));
129 ReferenceTypeInfo t2(MergeTypes(StringType(), StringType()));
130 EXPECT_TRUE(t2.IsValid());
131 EXPECT_TRUE(t2.IsExact());
132 EXPECT_TRUE(t2.IsEqual(StringType()));
133
134 // Left is super class of right.
135 ReferenceTypeInfo t3(MergeTypes(ObjectType(), StringType()));
136 EXPECT_TRUE(t3.IsValid());
137 EXPECT_FALSE(t3.IsExact());
138 EXPECT_TRUE(t3.IsEqual(ObjectType(false)));
139
140 // Right is super class of left.
141 ReferenceTypeInfo t4(MergeTypes(StringType(), ObjectType()));
142 EXPECT_TRUE(t4.IsValid());
143 EXPECT_FALSE(t4.IsExact());
144 EXPECT_TRUE(t4.IsEqual(ObjectType(false)));
145
146 // Same types, but one or both are inexact.
147 ReferenceTypeInfo t5(MergeTypes(ObjectType(false), ObjectType()));
148 EXPECT_TRUE(t5.IsValid());
149 EXPECT_FALSE(t5.IsExact());
150 EXPECT_TRUE(t5.IsEqual(ObjectType(false)));
151 ReferenceTypeInfo t6(MergeTypes(ObjectType(), ObjectType(false)));
152 EXPECT_TRUE(t6.IsValid());
153 EXPECT_FALSE(t6.IsExact());
154 EXPECT_TRUE(t6.IsEqual(ObjectType(false)));
155 ReferenceTypeInfo t7(MergeTypes(ObjectType(false), ObjectType(false)));
156 EXPECT_TRUE(t7.IsValid());
157 EXPECT_FALSE(t7.IsExact());
158 EXPECT_TRUE(t7.IsEqual(ObjectType(false)));
159}
160
161} // namespace art