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 "driver/dex_compilation_unit.h"
21 #include "handle_scope-inl.h"
22 #include "nodes.h"
23 #include "optimization.h"
24 #include "optimizing_compiler_stats.h"
25 
26 namespace art {
27 
28 /**
29  * Propagates reference types to instructions.
30  */
31 class ReferenceTypePropagation : public HOptimization {
32  public:
ReferenceTypePropagation(HGraph * graph,const DexFile & dex_file,const DexCompilationUnit & dex_compilation_unit,StackHandleScopeCollection * handles)33   ReferenceTypePropagation(HGraph* graph,
34                            const DexFile& dex_file,
35                            const DexCompilationUnit& dex_compilation_unit,
36                            StackHandleScopeCollection* handles)
37     : HOptimization(graph, true, kReferenceTypePropagationPassName),
38       dex_file_(dex_file),
39       dex_compilation_unit_(dex_compilation_unit),
40       handles_(handles),
41       worklist_(graph->GetArena(), kDefaultWorklistSize) {}
42 
43   void Run() OVERRIDE;
44 
45   static constexpr const char* kReferenceTypePropagationPassName = "reference_type_propagation";
46 
47  private:
48   void VisitNewInstance(HNewInstance* new_instance);
49   void VisitLoadClass(HLoadClass* load_class);
50   void VisitPhi(HPhi* phi);
51   void VisitBasicBlock(HBasicBlock* block);
52 
53   void UpdateBoundType(HBoundType* bound_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
54   void UpdatePhi(HPhi* phi) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
55 
56   void BoundTypeForIfNotNull(HBasicBlock* block);
57   void BoundTypeForIfInstanceOf(HBasicBlock* block);
58 
59   void ProcessWorklist();
60   void AddToWorklist(HInstruction* instr);
61   void AddDependentInstructionsToWorklist(HInstruction* instr);
62 
63   bool UpdateNullability(HInstruction* instr);
64   bool UpdateReferenceTypeInfo(HInstruction* instr);
65 
66   ReferenceTypeInfo MergeTypes(const ReferenceTypeInfo& a, const ReferenceTypeInfo& b)
67       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68 
69   const DexFile& dex_file_;
70   const DexCompilationUnit& dex_compilation_unit_;
71   StackHandleScopeCollection* handles_;
72 
73   GrowableArray<HInstruction*> worklist_;
74 
75   static constexpr size_t kDefaultWorklistSize = 8;
76 
77   DISALLOW_COPY_AND_ASSIGN(ReferenceTypePropagation);
78 };
79 
80 }  // namespace art
81 
82 #endif  // ART_COMPILER_OPTIMIZING_REFERENCE_TYPE_PROPAGATION_H_
83