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
33namespace art {
34
35class HGraphBuilder : public ValueObject {
36 public:
37  HGraphBuilder(HGraph* graph,
38                DexCompilationUnit* dex_compilation_unit,
39                const DexCompilationUnit* const outer_compilation_unit,
40                const DexFile* dex_file,
41                const DexFile::CodeItem& code_item,
42                CompilerDriver* driver,
43                OptimizingCompilerStats* compiler_stats,
44                const uint8_t* interpreter_metadata,
45                Handle<mirror::DexCache> dex_cache,
46                StackHandleScopeCollection* handles)
47      : graph_(graph),
48        dex_file_(dex_file),
49        code_item_(code_item),
50        dex_compilation_unit_(dex_compilation_unit),
51        compiler_driver_(driver),
52        compilation_stats_(compiler_stats),
53        block_builder_(graph, dex_file, code_item),
54        ssa_builder_(graph, dex_compilation_unit->GetDexCache(), handles),
55        instruction_builder_(graph,
56                             &block_builder_,
57                             &ssa_builder_,
58                             dex_file,
59                             code_item_,
60                             Primitive::GetType(dex_compilation_unit_->GetShorty()[0]),
61                             dex_compilation_unit,
62                             outer_compilation_unit,
63                             driver,
64                             interpreter_metadata,
65                             compiler_stats,
66                             dex_cache) {}
67
68  // Only for unit testing.
69  HGraphBuilder(HGraph* graph,
70                const DexFile::CodeItem& code_item,
71                StackHandleScopeCollection* handles,
72                Primitive::Type return_type = Primitive::kPrimInt)
73      : graph_(graph),
74        dex_file_(nullptr),
75        code_item_(code_item),
76        dex_compilation_unit_(nullptr),
77        compiler_driver_(nullptr),
78        null_dex_cache_(),
79        compilation_stats_(nullptr),
80        block_builder_(graph, nullptr, code_item),
81        ssa_builder_(graph, null_dex_cache_, handles),
82        instruction_builder_(graph,
83                             &block_builder_,
84                             &ssa_builder_,
85                             /* dex_file */ nullptr,
86                             code_item_,
87                             return_type,
88                             /* dex_compilation_unit */ nullptr,
89                             /* outer_compilation_unit */ nullptr,
90                             /* compiler_driver */ nullptr,
91                             /* interpreter_metadata */ nullptr,
92                             /* compiler_stats */ nullptr,
93                             null_dex_cache_) {}
94
95  GraphAnalysisResult BuildGraph();
96
97  static constexpr const char* kBuilderPassName = "builder";
98
99 private:
100  void MaybeRecordStat(MethodCompilationStat compilation_stat);
101  bool SkipCompilation(size_t number_of_branches);
102
103  HGraph* const graph_;
104  const DexFile* const dex_file_;
105  const DexFile::CodeItem& code_item_;
106
107  // The compilation unit of the current method being compiled. Note that
108  // it can be an inlined method.
109  DexCompilationUnit* const dex_compilation_unit_;
110
111  CompilerDriver* const compiler_driver_;
112
113  ScopedNullHandle<mirror::DexCache> null_dex_cache_;
114
115  OptimizingCompilerStats* compilation_stats_;
116
117  HBasicBlockBuilder block_builder_;
118  SsaBuilder ssa_builder_;
119  HInstructionBuilder instruction_builder_;
120
121  DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
122};
123
124}  // namespace art
125
126#endif  // ART_COMPILER_OPTIMIZING_BUILDER_H_
127