1 /*
2  * Copyright (C) 2014 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_BUILDER_H_
18 #define ART_COMPILER_OPTIMIZING_BUILDER_H_
19 
20 #include "base/arena_containers.h"
21 #include "base/arena_object.h"
22 #include "block_builder.h"
23 #include "dex_file.h"
24 #include "dex_file-inl.h"
25 #include "driver/compiler_driver.h"
26 #include "driver/dex_compilation_unit.h"
27 #include "instruction_builder.h"
28 #include "optimizing_compiler_stats.h"
29 #include "primitive.h"
30 #include "nodes.h"
31 #include "ssa_builder.h"
32 
33 namespace art {
34 
35 class CodeGenerator;
36 
37 class HGraphBuilder : public ValueObject {
38  public:
HGraphBuilder(HGraph * graph,DexCompilationUnit * dex_compilation_unit,const DexCompilationUnit * const outer_compilation_unit,const DexFile * dex_file,const DexFile::CodeItem & code_item,CompilerDriver * driver,CodeGenerator * code_generator,OptimizingCompilerStats * compiler_stats,const uint8_t * interpreter_metadata,Handle<mirror::DexCache> dex_cache,VariableSizedHandleScope * handles)39   HGraphBuilder(HGraph* graph,
40                 DexCompilationUnit* dex_compilation_unit,
41                 const DexCompilationUnit* const outer_compilation_unit,
42                 const DexFile* dex_file,
43                 const DexFile::CodeItem& code_item,
44                 CompilerDriver* driver,
45                 CodeGenerator* code_generator,
46                 OptimizingCompilerStats* compiler_stats,
47                 const uint8_t* interpreter_metadata,
48                 Handle<mirror::DexCache> dex_cache,
49                 VariableSizedHandleScope* handles)
50       : graph_(graph),
51         dex_file_(dex_file),
52         code_item_(code_item),
53         dex_compilation_unit_(dex_compilation_unit),
54         compiler_driver_(driver),
55         compilation_stats_(compiler_stats),
56         block_builder_(graph, dex_file, code_item),
57         ssa_builder_(graph,
58                      dex_compilation_unit->GetClassLoader(),
59                      dex_compilation_unit->GetDexCache(),
60                      handles),
61         instruction_builder_(graph,
62                              &block_builder_,
63                              &ssa_builder_,
64                              dex_file,
65                              code_item_,
66                              Primitive::GetType(dex_compilation_unit_->GetShorty()[0]),
67                              dex_compilation_unit,
68                              outer_compilation_unit,
69                              driver,
70                              code_generator,
71                              interpreter_metadata,
72                              compiler_stats,
73                              dex_cache,
74                              handles) {}
75 
76   // Only for unit testing.
77   HGraphBuilder(HGraph* graph,
78                 const DexFile::CodeItem& code_item,
79                 VariableSizedHandleScope* handles,
80                 Primitive::Type return_type = Primitive::kPrimInt)
graph_(graph)81       : graph_(graph),
82         dex_file_(nullptr),
83         code_item_(code_item),
84         dex_compilation_unit_(nullptr),
85         compiler_driver_(nullptr),
86         compilation_stats_(nullptr),
87         block_builder_(graph, nullptr, code_item),
88         ssa_builder_(graph,
89                      handles->NewHandle<mirror::ClassLoader>(nullptr),
90                      handles->NewHandle<mirror::DexCache>(nullptr),
91                      handles),
92         instruction_builder_(graph,
93                              &block_builder_,
94                              &ssa_builder_,
95                              /* dex_file */ nullptr,
96                              code_item_,
97                              return_type,
98                              /* dex_compilation_unit */ nullptr,
99                              /* outer_compilation_unit */ nullptr,
100                              /* compiler_driver */ nullptr,
101                              /* code_generator */ nullptr,
102                              /* interpreter_metadata */ nullptr,
103                              /* compiler_stats */ nullptr,
104                              handles->NewHandle<mirror::DexCache>(nullptr),
105                              handles) {}
106 
107   GraphAnalysisResult BuildGraph();
108 
109   static constexpr const char* kBuilderPassName = "builder";
110 
111  private:
112   void MaybeRecordStat(MethodCompilationStat compilation_stat);
113   bool SkipCompilation(size_t number_of_branches);
114 
115   HGraph* const graph_;
116   const DexFile* const dex_file_;
117   const DexFile::CodeItem& code_item_;
118 
119   // The compilation unit of the current method being compiled. Note that
120   // it can be an inlined method.
121   DexCompilationUnit* const dex_compilation_unit_;
122 
123   CompilerDriver* const compiler_driver_;
124 
125   OptimizingCompilerStats* compilation_stats_;
126 
127   HBasicBlockBuilder block_builder_;
128   SsaBuilder ssa_builder_;
129   HInstructionBuilder instruction_builder_;
130 
131   DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
132 };
133 
134 }  // namespace art
135 
136 #endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
137