reference_type_propagation.h revision bfb80d25eaeb7a604d5dd25a370e3869e96a33ab
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 "driver/dex_compilation_unit.h"
22#include "handle_scope-inl.h"
23#include "nodes.h"
24#include "obj_ptr.h"
25#include "optimization.h"
26#include "optimizing_compiler_stats.h"
27
28namespace art {
29
30/**
31 * Propagates reference types to instructions.
32 */
33class ReferenceTypePropagation : public HOptimization {
34 public:
35  ReferenceTypePropagation(HGraph* graph,
36                           Handle<mirror::ClassLoader> class_loader,
37                           Handle<mirror::DexCache> hint_dex_cache,
38                           VariableSizedHandleScope* handles,
39                           bool is_first_run,
40                           const char* name = kReferenceTypePropagationPassName);
41
42  // Visit a single instruction.
43  void Visit(HInstruction* instruction);
44
45  void Run() OVERRIDE;
46
47  // Returns true if klass is admissible to the propagation: non-null and resolved.
48  // For an array type, we also check if the component type is admissible.
49  static bool IsAdmissible(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_) {
50    return klass != nullptr &&
51           klass->IsResolved() &&
52           (!klass->IsArrayClass() || IsAdmissible(klass->GetComponentType()));
53  }
54
55  static constexpr const char* kReferenceTypePropagationPassName = "reference_type_propagation";
56
57 private:
58  class HandleCache {
59   public:
60    explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
61
62    template <typename T>
63    MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
64      return handles_->NewHandle(object);
65    }
66
67    template <typename T>
68    MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
69      return handles_->NewHandle(object);
70    }
71
72    ReferenceTypeInfo::TypeHandle GetObjectClassHandle();
73    ReferenceTypeInfo::TypeHandle GetClassClassHandle();
74    ReferenceTypeInfo::TypeHandle GetStringClassHandle();
75    ReferenceTypeInfo::TypeHandle GetThrowableClassHandle();
76
77   private:
78    VariableSizedHandleScope* handles_;
79
80    ReferenceTypeInfo::TypeHandle object_class_handle_;
81    ReferenceTypeInfo::TypeHandle class_class_handle_;
82    ReferenceTypeInfo::TypeHandle string_class_handle_;
83    ReferenceTypeInfo::TypeHandle throwable_class_handle_;
84  };
85
86  class RTPVisitor;
87
88  void VisitPhi(HPhi* phi);
89  void VisitBasicBlock(HBasicBlock* block);
90  void UpdateBoundType(HBoundType* bound_type) REQUIRES_SHARED(Locks::mutator_lock_);
91  void UpdatePhi(HPhi* phi) REQUIRES_SHARED(Locks::mutator_lock_);
92  void BoundTypeForIfNotNull(HBasicBlock* block);
93  void BoundTypeForIfInstanceOf(HBasicBlock* block);
94  void ProcessWorklist();
95  void AddToWorklist(HInstruction* instr);
96  void AddDependentInstructionsToWorklist(HInstruction* instr);
97
98  bool UpdateNullability(HInstruction* instr);
99  bool UpdateReferenceTypeInfo(HInstruction* instr);
100
101  static void UpdateArrayGet(HArrayGet* instr, HandleCache* handle_cache)
102      REQUIRES_SHARED(Locks::mutator_lock_);
103
104  ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a, const ReferenceTypeInfo& b)
105      REQUIRES_SHARED(Locks::mutator_lock_);
106
107  void ValidateTypes();
108
109  Handle<mirror::ClassLoader> class_loader_;
110
111  // Note: hint_dex_cache_ is usually, but not necessarily, the dex cache associated with
112  // graph_->GetDexFile(). Since we may look up also in other dex files, it's used only
113  // as a hint, to reduce the number of calls to the costly ClassLinker::FindDexCache().
114  Handle<mirror::DexCache> hint_dex_cache_;
115  HandleCache handle_cache_;
116
117  ArenaVector<HInstruction*> worklist_;
118
119  // Whether this reference type propagation is the first run we are doing.
120  const bool is_first_run_;
121
122  static constexpr size_t kDefaultWorklistSize = 8;
123
124  friend class ReferenceTypePropagationTest;
125
126  DISALLOW_COPY_AND_ASSIGN(ReferenceTypePropagation);
127};
128
129}  // namespace art
130
131#endif  // ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_
132