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