1 /*
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
17 #include "reference_type_propagation.h"
18
19 #include "base/arena_allocator.h"
20 #include "builder.h"
21 #include "nodes.h"
22 #include "object_lock.h"
23 #include "optimizing_unit_test.h"
24
25 namespace 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 */
31 class ReferenceTypePropagationTest : public CommonCompilerTest, public OptimizingUnitTestHelper {
32 public:
ReferenceTypePropagationTest()33 ReferenceTypePropagationTest() : graph_(nullptr), propagation_(nullptr) { }
34
~ReferenceTypePropagationTest()35 ~ReferenceTypePropagationTest() { }
36
SetupPropagation(VariableSizedHandleScope * handles)37 void SetupPropagation(VariableSizedHandleScope* handles) {
38 graph_ = CreateGraph(handles);
39 propagation_ = new (GetAllocator()) ReferenceTypePropagation(graph_,
40 Handle<mirror::ClassLoader>(),
41 Handle<mirror::DexCache>(),
42 true,
43 "test_prop");
44 }
45
46 // Relay method to merge type in reference type propagation.
MergeTypes(const ReferenceTypeInfo & a,const ReferenceTypeInfo & b)47 ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a,
48 const ReferenceTypeInfo& b) REQUIRES_SHARED(Locks::mutator_lock_) {
49 return propagation_->MergeTypes(a, b, graph_->GetHandleCache());
50 }
51
52 // Helper method to construct an invalid type.
InvalidType()53 ReferenceTypeInfo InvalidType() {
54 return ReferenceTypeInfo::CreateInvalid();
55 }
56
57 // Helper method to construct the Object type.
ObjectType(bool is_exact=true)58 ReferenceTypeInfo ObjectType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) {
59 return ReferenceTypeInfo::Create(graph_->GetHandleCache()->GetObjectClassHandle(), is_exact);
60 }
61
62 // Helper method to construct the String type.
StringType(bool is_exact=true)63 ReferenceTypeInfo StringType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) {
64 return ReferenceTypeInfo::Create(graph_->GetHandleCache()->GetStringClassHandle(), is_exact);
65 }
66
67 // General building fields.
68 HGraph* graph_;
69
70 ReferenceTypePropagation* propagation_;
71 };
72
73 //
74 // The actual ReferenceTypePropgation unit tests.
75 //
76
TEST_F(ReferenceTypePropagationTest,ProperSetup)77 TEST_F(ReferenceTypePropagationTest, ProperSetup) {
78 ScopedObjectAccess soa(Thread::Current());
79 VariableSizedHandleScope handles(soa.Self());
80 SetupPropagation(&handles);
81
82 EXPECT_TRUE(propagation_ != nullptr);
83 EXPECT_TRUE(graph_->GetInexactObjectRti().IsEqual(ObjectType(false)));
84 }
85
TEST_F(ReferenceTypePropagationTest,MergeInvalidTypes)86 TEST_F(ReferenceTypePropagationTest, MergeInvalidTypes) {
87 ScopedObjectAccess soa(Thread::Current());
88 VariableSizedHandleScope handles(soa.Self());
89 SetupPropagation(&handles);
90
91 // Two invalid types.
92 ReferenceTypeInfo t1(MergeTypes(InvalidType(), InvalidType()));
93 EXPECT_FALSE(t1.IsValid());
94 EXPECT_FALSE(t1.IsExact());
95 EXPECT_TRUE(t1.IsEqual(InvalidType()));
96
97 // Valid type on right.
98 ReferenceTypeInfo t2(MergeTypes(InvalidType(), ObjectType()));
99 EXPECT_TRUE(t2.IsValid());
100 EXPECT_TRUE(t2.IsExact());
101 EXPECT_TRUE(t2.IsEqual(ObjectType()));
102 ReferenceTypeInfo t3(MergeTypes(InvalidType(), StringType()));
103 EXPECT_TRUE(t3.IsValid());
104 EXPECT_TRUE(t3.IsExact());
105 EXPECT_TRUE(t3.IsEqual(StringType()));
106
107 // Valid type on left.
108 ReferenceTypeInfo t4(MergeTypes(ObjectType(), InvalidType()));
109 EXPECT_TRUE(t4.IsValid());
110 EXPECT_TRUE(t4.IsExact());
111 EXPECT_TRUE(t4.IsEqual(ObjectType()));
112 ReferenceTypeInfo t5(MergeTypes(StringType(), InvalidType()));
113 EXPECT_TRUE(t5.IsValid());
114 EXPECT_TRUE(t5.IsExact());
115 EXPECT_TRUE(t5.IsEqual(StringType()));
116 }
117
TEST_F(ReferenceTypePropagationTest,MergeValidTypes)118 TEST_F(ReferenceTypePropagationTest, MergeValidTypes) {
119 ScopedObjectAccess soa(Thread::Current());
120 VariableSizedHandleScope handles(soa.Self());
121 SetupPropagation(&handles);
122
123 // Same types.
124 ReferenceTypeInfo t1(MergeTypes(ObjectType(), ObjectType()));
125 EXPECT_TRUE(t1.IsValid());
126 EXPECT_TRUE(t1.IsExact());
127 EXPECT_TRUE(t1.IsEqual(ObjectType()));
128 ReferenceTypeInfo t2(MergeTypes(StringType(), StringType()));
129 EXPECT_TRUE(t2.IsValid());
130 EXPECT_TRUE(t2.IsExact());
131 EXPECT_TRUE(t2.IsEqual(StringType()));
132
133 // Left is super class of right.
134 ReferenceTypeInfo t3(MergeTypes(ObjectType(), StringType()));
135 EXPECT_TRUE(t3.IsValid());
136 EXPECT_FALSE(t3.IsExact());
137 EXPECT_TRUE(t3.IsEqual(ObjectType(false)));
138
139 // Right is super class of left.
140 ReferenceTypeInfo t4(MergeTypes(StringType(), ObjectType()));
141 EXPECT_TRUE(t4.IsValid());
142 EXPECT_FALSE(t4.IsExact());
143 EXPECT_TRUE(t4.IsEqual(ObjectType(false)));
144
145 // Same types, but one or both are inexact.
146 ReferenceTypeInfo t5(MergeTypes(ObjectType(false), ObjectType()));
147 EXPECT_TRUE(t5.IsValid());
148 EXPECT_FALSE(t5.IsExact());
149 EXPECT_TRUE(t5.IsEqual(ObjectType(false)));
150 ReferenceTypeInfo t6(MergeTypes(ObjectType(), ObjectType(false)));
151 EXPECT_TRUE(t6.IsValid());
152 EXPECT_FALSE(t6.IsExact());
153 EXPECT_TRUE(t6.IsEqual(ObjectType(false)));
154 ReferenceTypeInfo t7(MergeTypes(ObjectType(false), ObjectType(false)));
155 EXPECT_TRUE(t7.IsValid());
156 EXPECT_FALSE(t7.IsExact());
157 EXPECT_TRUE(t7.IsEqual(ObjectType(false)));
158 }
159
160 } // namespace art
161