xref: /aosp_15_r20/art/compiler/optimizing/reference_type_propagation.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 
20 #include "base/arena_containers.h"
21 #include "base/macros.h"
22 #include "mirror/class-inl.h"
23 #include "nodes.h"
24 #include "obj_ptr.h"
25 #include "optimization.h"
26 
27 namespace art HIDDEN {
28 
29 class HandleCache;
30 
31 /**
32  * Propagates reference types to instructions.
33  */
34 class ReferenceTypePropagation : public HOptimization {
35  public:
36   ReferenceTypePropagation(HGraph* graph,
37                            Handle<mirror::DexCache> hint_dex_cache,
38                            bool is_first_run,
39                            const char* name = kReferenceTypePropagationPassName);
40 
41   // Visit a single instruction.
42   // Used when a pass, such as Inliner or LSE, adds a single instruction.
43   void Visit(HInstruction* instruction);
44 
45   // Visit instructions and process dependencies between them.
46   // Used when a pass, such as LSE, adds multiple dependent instructions, including Phis.
47   void Visit(ArrayRef<HInstruction* const> instructions);
48 
49   bool Run() override;
50 
51   // Returns true if klass is admissible to the propagation: non-null and resolved.
52   // For an array type, we also check if the component type is admissible.
IsAdmissible(ObjPtr<mirror::Class> klass)53   static bool IsAdmissible(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
54     while (klass != nullptr && klass->IsArrayClass()) {
55       DCHECK(klass->IsResolved());
56       klass = klass->GetComponentType();
57     }
58     return klass != nullptr && klass->IsResolved();
59   }
60 
61   static constexpr const char* kReferenceTypePropagationPassName = "reference_type_propagation";
62 
63   // Fix the reference type for an HSelect instruction whose inputs have changed. The reference
64   // types of the inputs are merged and the resulting reference type is set on the HSelect
65   // instruction.
66   static void FixUpSelectType(HSelect* select, HandleCache* handle_cache);
67 
68  private:
69   class RTPVisitor;
70 
71   static ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a,
72                                       const ReferenceTypeInfo& b,
73                                       HandleCache* handle_cache)
74       REQUIRES_SHARED(Locks::mutator_lock_);
75 
76   // Note: hint_dex_cache_ is usually, but not necessarily, the dex cache associated with
77   // graph_->GetDexFile(). Since we may look up also in other dex files, it's used only
78   // as a hint, to reduce the number of calls to the costly ClassLinker::FindDexCache().
79   Handle<mirror::DexCache> hint_dex_cache_;
80 
81   // Whether this reference type propagation is the first run we are doing.
82   const bool is_first_run_;
83 
84   template<typename T>
85   friend class ReferenceTypePropagationTestBase;
86 
87   DISALLOW_COPY_AND_ASSIGN(ReferenceTypePropagation);
88 };
89 
90 }  // namespace art
91 
92 #endif  // ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_
93