Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_ |
| 19 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 21 | #include "mirror/class-inl.h" |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 22 | #include "nodes.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 23 | #include "obj_ptr.h" |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 24 | #include "optimization.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | /** |
| 29 | * Propagates reference types to instructions. |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 30 | */ |
Calin Juravle | 6c0c4f2 | 2015-06-12 15:40:42 +0000 | [diff] [blame] | 31 | class ReferenceTypePropagation : public HOptimization { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 32 | public: |
Calin Juravle | a5ae3c3 | 2015-07-28 14:40:50 +0000 | [diff] [blame] | 33 | ReferenceTypePropagation(HGraph* graph, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 34 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 35 | Handle<mirror::DexCache> hint_dex_cache, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 36 | VariableSizedHandleScope* handles, |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 37 | bool is_first_run, |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 38 | const char* name = kReferenceTypePropagationPassName); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 39 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 40 | // Visit a single instruction. |
| 41 | void Visit(HInstruction* instruction); |
| 42 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 43 | bool Run() OVERRIDE; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 44 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 45 | // Returns true if klass is admissible to the propagation: non-null and resolved. |
| 46 | // For an array type, we also check if the component type is admissible. |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 47 | static bool IsAdmissible(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 48 | return klass != nullptr && |
| 49 | klass->IsResolved() && |
| 50 | (!klass->IsArrayClass() || IsAdmissible(klass->GetComponentType())); |
| 51 | } |
| 52 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 53 | static constexpr const char* kReferenceTypePropagationPassName = "reference_type_propagation"; |
| 54 | |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 55 | // Fix the reference type for an instruction whose inputs have changed. |
| 56 | // For a select instruction, the reference types of the inputs are merged |
| 57 | // and the resulting reference type is set on the select instruction. |
| 58 | static void FixUpInstructionType(HInstruction* instruction, |
| 59 | VariableSizedHandleScope* handle_scope); |
| 60 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 61 | private: |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 62 | class HandleCache { |
| 63 | public: |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 64 | explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { } |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 65 | |
| 66 | template <typename T> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 67 | MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 68 | return handles_->NewHandle(object); |
| 69 | } |
| 70 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 71 | template <typename T> |
| 72 | MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 73 | return handles_->NewHandle(object); |
| 74 | } |
| 75 | |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 76 | ReferenceTypeInfo::TypeHandle GetObjectClassHandle(); |
| 77 | ReferenceTypeInfo::TypeHandle GetClassClassHandle(); |
Orion Hodson | dbaa5c7 | 2018-05-10 08:22:46 +0100 | [diff] [blame] | 78 | ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle(); |
Orion Hodson | 18259d7 | 2018-04-12 11:18:23 +0100 | [diff] [blame] | 79 | ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle(); |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 80 | ReferenceTypeInfo::TypeHandle GetStringClassHandle(); |
| 81 | ReferenceTypeInfo::TypeHandle GetThrowableClassHandle(); |
| 82 | |
| 83 | private: |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 84 | VariableSizedHandleScope* handles_; |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 85 | |
| 86 | ReferenceTypeInfo::TypeHandle object_class_handle_; |
| 87 | ReferenceTypeInfo::TypeHandle class_class_handle_; |
Orion Hodson | dbaa5c7 | 2018-05-10 08:22:46 +0100 | [diff] [blame] | 88 | ReferenceTypeInfo::TypeHandle method_handle_class_handle_; |
Orion Hodson | 18259d7 | 2018-04-12 11:18:23 +0100 | [diff] [blame] | 89 | ReferenceTypeInfo::TypeHandle method_type_class_handle_; |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 90 | ReferenceTypeInfo::TypeHandle string_class_handle_; |
| 91 | ReferenceTypeInfo::TypeHandle throwable_class_handle_; |
| 92 | }; |
| 93 | |
| 94 | class RTPVisitor; |
| 95 | |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 96 | static ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a, |
| 97 | const ReferenceTypeInfo& b, |
| 98 | HandleCache* handle_cache) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 99 | REQUIRES_SHARED(Locks::mutator_lock_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 100 | |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 101 | void ValidateTypes(); |
| 102 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 103 | Handle<mirror::ClassLoader> class_loader_; |
| 104 | |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 105 | // Note: hint_dex_cache_ is usually, but not necessarily, the dex cache associated with |
| 106 | // graph_->GetDexFile(). Since we may look up also in other dex files, it's used only |
| 107 | // as a hint, to reduce the number of calls to the costly ClassLinker::FindDexCache(). |
| 108 | Handle<mirror::DexCache> hint_dex_cache_; |
Vladimir Marko | 7d1fbf3 | 2016-01-26 15:01:12 +0000 | [diff] [blame] | 109 | HandleCache handle_cache_; |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 110 | |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 111 | // Whether this reference type propagation is the first run we are doing. |
| 112 | const bool is_first_run_; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 113 | |
Aart Bik | 8b3f9b2 | 2016-04-06 11:22:12 -0700 | [diff] [blame] | 114 | friend class ReferenceTypePropagationTest; |
| 115 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 116 | DISALLOW_COPY_AND_ASSIGN(ReferenceTypePropagation); |
| 117 | }; |
| 118 | |
| 119 | } // namespace art |
| 120 | |
| 121 | #endif // ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_ |