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