reference_type_propagation.h revision 16e528957869c7debb1f6758c9a364819e15ee1a
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(ObjPtr<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  // Fix the reference type for an instruction whose inputs have changed.
58  // For a select instruction, the reference types of the inputs are merged
59  // and the resulting reference type is set on the select instruction.
60  static void FixUpInstructionType(HInstruction* instruction,
61                                   VariableSizedHandleScope* handle_scope);
62
63 private:
64  class HandleCache {
65   public:
66    explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
67
68    template <typename T>
69    MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
70      return handles_->NewHandle(object);
71    }
72
73    template <typename T>
74    MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
75      return handles_->NewHandle(object);
76    }
77
78    ReferenceTypeInfo::TypeHandle GetObjectClassHandle();
79    ReferenceTypeInfo::TypeHandle GetClassClassHandle();
80    ReferenceTypeInfo::TypeHandle GetStringClassHandle();
81    ReferenceTypeInfo::TypeHandle GetThrowableClassHandle();
82
83   private:
84    VariableSizedHandleScope* handles_;
85
86    ReferenceTypeInfo::TypeHandle object_class_handle_;
87    ReferenceTypeInfo::TypeHandle class_class_handle_;
88    ReferenceTypeInfo::TypeHandle string_class_handle_;
89    ReferenceTypeInfo::TypeHandle throwable_class_handle_;
90  };
91
92  class RTPVisitor;
93
94  void VisitPhi(HPhi* phi);
95  void VisitBasicBlock(HBasicBlock* block);
96  void UpdateBoundType(HBoundType* bound_type) REQUIRES_SHARED(Locks::mutator_lock_);
97  void UpdatePhi(HPhi* phi) REQUIRES_SHARED(Locks::mutator_lock_);
98  void BoundTypeForIfNotNull(HBasicBlock* block);
99  void BoundTypeForIfInstanceOf(HBasicBlock* block);
100  void ProcessWorklist();
101  void AddToWorklist(HInstruction* instr);
102  void AddDependentInstructionsToWorklist(HInstruction* instr);
103
104  bool UpdateNullability(HInstruction* instr);
105  bool UpdateReferenceTypeInfo(HInstruction* instr);
106
107  static void UpdateArrayGet(HArrayGet* instr, HandleCache* handle_cache)
108      REQUIRES_SHARED(Locks::mutator_lock_);
109
110  static ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a,
111                                      const ReferenceTypeInfo& b,
112                                      HandleCache* handle_cache)
113      REQUIRES_SHARED(Locks::mutator_lock_);
114
115  void ValidateTypes();
116
117  Handle<mirror::ClassLoader> class_loader_;
118
119  // Note: hint_dex_cache_ is usually, but not necessarily, the dex cache associated with
120  // graph_->GetDexFile(). Since we may look up also in other dex files, it's used only
121  // as a hint, to reduce the number of calls to the costly ClassLinker::FindDexCache().
122  Handle<mirror::DexCache> hint_dex_cache_;
123  HandleCache handle_cache_;
124
125  ArenaVector<HInstruction*> worklist_;
126
127  // Whether this reference type propagation is the first run we are doing.
128  const bool is_first_run_;
129
130  static constexpr size_t kDefaultWorklistSize = 8;
131
132  friend class ReferenceTypePropagationTest;
133
134  DISALLOW_COPY_AND_ASSIGN(ReferenceTypePropagation);
135};
136
137}  // namespace art
138
139#endif  // ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_
140