blob: cb2af91d872321d11e1bdc63292b82acd9778d45 [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 */
31class ReferenceTypePropagationTest : public CommonCompilerTest {
32 public:
Andreas Gamped9911ee2017-03-27 13:27:24 -070033 ReferenceTypePropagationTest() : pool_(), allocator_(&pool_), propagation_(nullptr) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070034 graph_ = CreateGraph(&allocator_);
35 }
36
37 ~ReferenceTypePropagationTest() { }
38
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070039 void SetupPropagation(VariableSizedHandleScope* handles) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070040 graph_->InitializeInexactObjectRTI(handles);
Vladimir Marko5eed0c52016-04-20 11:22:16 +010041 propagation_ = new (&allocator_) ReferenceTypePropagation(graph_,
Vladimir Marko8d6768d2017-03-14 10:13:21 +000042 Handle<mirror::ClassLoader>(),
Vladimir Marko5eed0c52016-04-20 11:22:16 +010043 Handle<mirror::DexCache>(),
44 handles,
45 true,
46 "test_prop");
Aart Bik8b3f9b22016-04-06 11:22:12 -070047 }
48
49 // Relay method to merge type in reference type propagation.
50 ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 const ReferenceTypeInfo& b) REQUIRES_SHARED(Locks::mutator_lock_) {
Mads Ager16e52892017-07-14 13:11:37 +020052 return propagation_->MergeTypes(a, b, &propagation_->handle_cache_);
Aart Bik8b3f9b22016-04-06 11:22:12 -070053 }
54
55 // Helper method to construct an invalid type.
56 ReferenceTypeInfo InvalidType() {
57 return ReferenceTypeInfo::CreateInvalid();
58 }
59
60 // Helper method to construct the Object type.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 ReferenceTypeInfo ObjectType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070062 return ReferenceTypeInfo::Create(propagation_->handle_cache_.GetObjectClassHandle(), is_exact);
63 }
64
65 // Helper method to construct the String type.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 ReferenceTypeInfo StringType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) {
Aart Bik8b3f9b22016-04-06 11:22:12 -070067 return ReferenceTypeInfo::Create(propagation_->handle_cache_.GetStringClassHandle(), is_exact);
68 }
69
Aart Bik8b3f9b22016-04-06 11:22:12 -070070 // General building fields.
71 ArenaPool pool_;
72 ArenaAllocator allocator_;
73 HGraph* graph_;
74
75 ReferenceTypePropagation* propagation_;
76};
77
78//
79// The actual ReferenceTypePropgation unit tests.
80//
81
82TEST_F(ReferenceTypePropagationTest, ProperSetup) {
83 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070084 VariableSizedHandleScope handles(soa.Self());
Aart Bik8b3f9b22016-04-06 11:22:12 -070085 SetupPropagation(&handles);
86
87 EXPECT_TRUE(propagation_ != nullptr);
88 EXPECT_TRUE(graph_->GetInexactObjectRti().IsEqual(ObjectType(false)));
89}
90
91TEST_F(ReferenceTypePropagationTest, MergeInvalidTypes) {
92 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070093 VariableSizedHandleScope handles(soa.Self());
Aart Bik8b3f9b22016-04-06 11:22:12 -070094 SetupPropagation(&handles);
95
96 // Two invalid types.
97 ReferenceTypeInfo t1(MergeTypes(InvalidType(), InvalidType()));
98 EXPECT_FALSE(t1.IsValid());
99 EXPECT_FALSE(t1.IsExact());
100 EXPECT_TRUE(t1.IsEqual(InvalidType()));
101
102 // Valid type on right.
103 ReferenceTypeInfo t2(MergeTypes(InvalidType(), ObjectType()));
104 EXPECT_TRUE(t2.IsValid());
105 EXPECT_TRUE(t2.IsExact());
106 EXPECT_TRUE(t2.IsEqual(ObjectType()));
107 ReferenceTypeInfo t3(MergeTypes(InvalidType(), StringType()));
108 EXPECT_TRUE(t3.IsValid());
109 EXPECT_TRUE(t3.IsExact());
110 EXPECT_TRUE(t3.IsEqual(StringType()));
111
112 // Valid type on left.
113 ReferenceTypeInfo t4(MergeTypes(ObjectType(), InvalidType()));
114 EXPECT_TRUE(t4.IsValid());
115 EXPECT_TRUE(t4.IsExact());
116 EXPECT_TRUE(t4.IsEqual(ObjectType()));
117 ReferenceTypeInfo t5(MergeTypes(StringType(), InvalidType()));
118 EXPECT_TRUE(t5.IsValid());
119 EXPECT_TRUE(t5.IsExact());
120 EXPECT_TRUE(t5.IsEqual(StringType()));
121}
122
Aart Bik8b3f9b22016-04-06 11:22:12 -0700123TEST_F(ReferenceTypePropagationTest, MergeValidTypes) {
124 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700125 VariableSizedHandleScope handles(soa.Self());
Aart Bik8b3f9b22016-04-06 11:22:12 -0700126 SetupPropagation(&handles);
127
128 // Same types.
129 ReferenceTypeInfo t1(MergeTypes(ObjectType(), ObjectType()));
130 EXPECT_TRUE(t1.IsValid());
131 EXPECT_TRUE(t1.IsExact());
132 EXPECT_TRUE(t1.IsEqual(ObjectType()));
133 ReferenceTypeInfo t2(MergeTypes(StringType(), StringType()));
134 EXPECT_TRUE(t2.IsValid());
135 EXPECT_TRUE(t2.IsExact());
136 EXPECT_TRUE(t2.IsEqual(StringType()));
137
138 // Left is super class of right.
139 ReferenceTypeInfo t3(MergeTypes(ObjectType(), StringType()));
140 EXPECT_TRUE(t3.IsValid());
141 EXPECT_FALSE(t3.IsExact());
142 EXPECT_TRUE(t3.IsEqual(ObjectType(false)));
143
144 // Right is super class of left.
145 ReferenceTypeInfo t4(MergeTypes(StringType(), ObjectType()));
146 EXPECT_TRUE(t4.IsValid());
147 EXPECT_FALSE(t4.IsExact());
148 EXPECT_TRUE(t4.IsEqual(ObjectType(false)));
149
150 // Same types, but one or both are inexact.
151 ReferenceTypeInfo t5(MergeTypes(ObjectType(false), ObjectType()));
152 EXPECT_TRUE(t5.IsValid());
153 EXPECT_FALSE(t5.IsExact());
154 EXPECT_TRUE(t5.IsEqual(ObjectType(false)));
155 ReferenceTypeInfo t6(MergeTypes(ObjectType(), ObjectType(false)));
156 EXPECT_TRUE(t6.IsValid());
157 EXPECT_FALSE(t6.IsExact());
158 EXPECT_TRUE(t6.IsEqual(ObjectType(false)));
159 ReferenceTypeInfo t7(MergeTypes(ObjectType(false), ObjectType(false)));
160 EXPECT_TRUE(t7.IsValid());
161 EXPECT_FALSE(t7.IsExact());
162 EXPECT_TRUE(t7.IsEqual(ObjectType(false)));
163}
164
165} // namespace art