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