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