Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "reference_type_propagation.h" |
| 18 | |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 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" |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 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 { |
| 32 | public: |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 33 | ReferenceTypePropagationTest() : pool_(), allocator_(&pool_), propagation_(nullptr) { |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 34 | graph_ = CreateGraph(&allocator_); |
| 35 | } |
| 36 | |
| 37 | ~ReferenceTypePropagationTest() { } |
| 38 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 39 | void SetupPropagation(VariableSizedHandleScope* handles) { |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 40 | graph_->InitializeInexactObjectRTI(handles); |
Vladimir Marko | 5eed0c5 | 2016-04-20 11:22:16 +0100 | [diff] [blame] | 41 | propagation_ = new (&allocator_) ReferenceTypePropagation(graph_, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 42 | Handle<mirror::ClassLoader>(), |
Vladimir Marko | 5eed0c5 | 2016-04-20 11:22:16 +0100 | [diff] [blame] | 43 | Handle<mirror::DexCache>(), |
| 44 | handles, |
| 45 | true, |
| 46 | "test_prop"); |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Relay method to merge type in reference type propagation. |
| 50 | ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 51 | const ReferenceTypeInfo& b) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 52 | return propagation_->MergeTypes(a, b, &propagation_->handle_cache_); |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 53 | } |
| 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 Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 61 | ReferenceTypeInfo ObjectType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) { |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 62 | return ReferenceTypeInfo::Create(propagation_->handle_cache_.GetObjectClassHandle(), is_exact); |
| 63 | } |
| 64 | |
| 65 | // Helper method to construct the String type. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 66 | ReferenceTypeInfo StringType(bool is_exact = true) REQUIRES_SHARED(Locks::mutator_lock_) { |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 67 | return ReferenceTypeInfo::Create(propagation_->handle_cache_.GetStringClassHandle(), is_exact); |
| 68 | } |
| 69 | |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 70 | // 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 | |
| 82 | TEST_F(ReferenceTypePropagationTest, ProperSetup) { |
| 83 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 84 | VariableSizedHandleScope handles(soa.Self()); |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 85 | SetupPropagation(&handles); |
| 86 | |
| 87 | EXPECT_TRUE(propagation_ != nullptr); |
| 88 | EXPECT_TRUE(graph_->GetInexactObjectRti().IsEqual(ObjectType(false))); |
| 89 | } |
| 90 | |
| 91 | TEST_F(ReferenceTypePropagationTest, MergeInvalidTypes) { |
| 92 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 93 | VariableSizedHandleScope handles(soa.Self()); |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 94 | 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 Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 123 | TEST_F(ReferenceTypePropagationTest, MergeValidTypes) { |
| 124 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 125 | VariableSizedHandleScope handles(soa.Self()); |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 126 | 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 |