blob: 50a13344dfc9b079f78436443e5bf6594a1e026e [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
David Brazdil86ea7ee2016-02-16 09:26:07 +000022#include "block_builder.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_file.h"
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010024#include "dex_file-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#include "driver/compiler_driver.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000026#include "driver/dex_compilation_unit.h"
Calin Juravle48c2b032014-12-09 18:11:36 +000027#include "optimizing_compiler_stats.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "primitive.h"
Dave Allison20dfc792014-06-16 20:44:29 -070029#include "nodes.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030
31namespace art {
32
David Brazdil60328912016-04-04 17:47:42 +000033class Instruction;
34
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HGraphBuilder : public ValueObject {
36 public:
David Brazdil5e8b1372015-01-23 14:39:08 +000037 HGraphBuilder(HGraph* graph,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010038 DexCompilationUnit* dex_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000039 const DexCompilationUnit* const outer_compilation_unit,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010040 const DexFile* dex_file,
David Brazdil86ea7ee2016-02-16 09:26:07 +000041 const DexFile::CodeItem& code_item,
Calin Juravle48c2b032014-12-09 18:11:36 +000042 CompilerDriver* driver,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000043 OptimizingCompilerStats* compiler_stats,
Mathieu Chartier736b5602015-09-02 14:54:11 -070044 const uint8_t* interpreter_metadata,
David Brazdil60328912016-04-04 17:47:42 +000045 Handle<mirror::DexCache> dex_cache)
46 : arena_(graph->GetArena()),
47 locals_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)),
48 current_block_(nullptr),
49 graph_(graph),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000050 dex_file_(dex_file),
David Brazdil86ea7ee2016-02-16 09:26:07 +000051 code_item_(code_item),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052 dex_compilation_unit_(dex_compilation_unit),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 compiler_driver_(driver),
David Brazdil60328912016-04-04 17:47:42 +000054 outer_compilation_unit_(outer_compilation_unit),
55 return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])),
56 code_start_(code_item.insns_),
David Brazdile3ff7b22016-03-02 16:48:20 +000057 block_builder_(graph, dex_file, code_item),
David Brazdil60328912016-04-04 17:47:42 +000058 latest_result_(nullptr),
59 compilation_stats_(compiler_stats),
60 interpreter_metadata_(interpreter_metadata),
61 dex_cache_(dex_cache) {}
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010062
63 // Only for unit testing.
David Brazdil86ea7ee2016-02-16 09:26:07 +000064 HGraphBuilder(HGraph* graph,
65 const DexFile::CodeItem& code_item,
66 Primitive::Type return_type = Primitive::kPrimInt)
David Brazdil60328912016-04-04 17:47:42 +000067 : arena_(graph->GetArena()),
68 locals_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)),
69 current_block_(nullptr),
70 graph_(graph),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010071 dex_file_(nullptr),
David Brazdil86ea7ee2016-02-16 09:26:07 +000072 code_item_(code_item),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010073 dex_compilation_unit_(nullptr),
74 compiler_driver_(nullptr),
David Brazdil60328912016-04-04 17:47:42 +000075 outer_compilation_unit_(nullptr),
76 return_type_(return_type),
77 code_start_(code_item.insns_),
David Brazdile3ff7b22016-03-02 16:48:20 +000078 block_builder_(graph, nullptr, code_item),
David Brazdil60328912016-04-04 17:47:42 +000079 latest_result_(nullptr),
80 compilation_stats_(nullptr),
81 interpreter_metadata_(nullptr),
82 null_dex_cache_(),
83 dex_cache_(null_dex_cache_) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
David Brazdil60328912016-04-04 17:47:42 +000085 GraphAnalysisResult BuildGraph(StackHandleScopeCollection* handles);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000086
Andreas Gampe7c3952f2015-02-19 18:21:24 -080087 static constexpr const char* kBuilderPassName = "builder";
88
Nicolas Geoffray818f2102014-02-18 16:43:35 +000089 private:
David Brazdil60328912016-04-04 17:47:42 +000090 bool GenerateInstructions();
91 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
92
93 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
94
95 bool CanDecodeQuickenedInfo() const;
96 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
97
98 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const {
99 return block_builder_.GetBlockAt(dex_pc);
100 }
101
102 void InitializeLocals(uint16_t count);
103 HLocal* GetLocalAt(uint32_t register_index) const;
104 void UpdateLocal(uint32_t register_index, HInstruction* instruction, uint32_t dex_pc) const;
105 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type, uint32_t dex_pc) const;
106 void InitializeParameters(uint16_t number_of_parameters);
107
108 // Returns whether the current method needs access check for the type.
109 // Output parameter finalizable is set to whether the type is finalizable.
110 bool NeedsAccessCheck(uint32_t type_index, /*out*/bool* finalizable) const;
111
112 template<typename T>
113 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
114
115 template<typename T>
116 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
117
118 template<typename T>
119 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
120
121 void Binop_23x_cmp(const Instruction& instruction,
122 Primitive::Type type,
123 ComparisonBias bias,
124 uint32_t dex_pc);
125
126 template<typename T>
127 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
128
129 template<typename T>
130 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
131
132 template<typename T>
133 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
134
135 template<typename T>
136 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
137
138 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
139 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
140
141 void Conversion_12x(const Instruction& instruction,
142 Primitive::Type input_type,
143 Primitive::Type result_type,
144 uint32_t dex_pc);
145
146 void BuildCheckedDivRem(uint16_t out_reg,
147 uint16_t first_reg,
148 int64_t second_reg_or_constant,
149 uint32_t dex_pc,
150 Primitive::Type type,
151 bool second_is_lit,
152 bool is_div);
153
154 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
155
156 // Builds an instance field access node and returns whether the instruction is supported.
157 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
158
159 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
160 uint32_t dex_pc,
161 bool is_put,
162 Primitive::Type field_type);
163 // Builds a static field access node and returns whether the instruction is supported.
164 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
165
166 void BuildArrayAccess(const Instruction& instruction,
167 uint32_t dex_pc,
168 bool is_get,
169 Primitive::Type anticipated_type);
170
171 // Builds an invocation node and returns whether the instruction is supported.
172 bool BuildInvoke(const Instruction& instruction,
173 uint32_t dex_pc,
174 uint32_t method_idx,
175 uint32_t number_of_vreg_arguments,
176 bool is_range,
177 uint32_t* args,
178 uint32_t register_index);
179
180 // Builds a new array node and the instructions that fill it.
181 void BuildFilledNewArray(uint32_t dex_pc,
182 uint32_t type_index,
183 uint32_t number_of_vreg_arguments,
184 bool is_range,
185 uint32_t* args,
186 uint32_t register_index);
187
188 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
189
190 // Fills the given object with data as specified in the fill-array-data
191 // instruction. Currently only used for non-reference and non-floating point
192 // arrays.
193 template <typename T>
194 void BuildFillArrayData(HInstruction* object,
195 const T* data,
196 uint32_t element_count,
197 Primitive::Type anticipated_type,
198 uint32_t dex_pc);
199
200 // Fills the given object with data as specified in the fill-array-data
201 // instruction. The data must be for long and double arrays.
202 void BuildFillWideArrayData(HInstruction* object,
203 const int64_t* data,
204 uint32_t element_count,
205 uint32_t dex_pc);
206
207 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
208 void BuildTypeCheck(const Instruction& instruction,
209 uint8_t destination,
210 uint8_t reference,
211 uint16_t type_index,
212 uint32_t dex_pc);
213
214 // Builds an instruction sequence for a switch statement.
215 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
216
David Brazdil86ea7ee2016-02-16 09:26:07 +0000217 bool SkipCompilation(size_t number_of_branches);
Calin Juravle48c2b032014-12-09 18:11:36 +0000218
David Brazdil60328912016-04-04 17:47:42 +0000219 void MaybeRecordStat(MethodCompilationStat compilation_stat);
220
221 // Returns the outer-most compiling method's class.
222 mirror::Class* GetOutermostCompilingClass() const;
223
224 // Returns the class whose method is being compiled.
225 mirror::Class* GetCompilingClass() const;
226
227 // Returns whether `type_index` points to the outer-most compiling method's class.
228 bool IsOutermostCompilingClass(uint16_t type_index) const;
229
230 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
231 uint32_t dex_pc,
232 HInvoke* invoke);
233
234 bool SetupInvokeArguments(HInvoke* invoke,
235 uint32_t number_of_vreg_arguments,
236 uint32_t* args,
237 uint32_t register_index,
238 bool is_range,
239 const char* descriptor,
240 size_t start_index,
241 size_t* argument_index);
242
243 bool HandleInvoke(HInvoke* invoke,
244 uint32_t number_of_vreg_arguments,
245 uint32_t* args,
246 uint32_t register_index,
247 bool is_range,
248 const char* descriptor,
249 HClinitCheck* clinit_check);
250
251 bool HandleStringInit(HInvoke* invoke,
252 uint32_t number_of_vreg_arguments,
253 uint32_t* args,
254 uint32_t register_index,
255 bool is_range,
256 const char* descriptor);
257
258 HClinitCheck* ProcessClinitCheckForInvoke(
259 uint32_t dex_pc,
260 ArtMethod* method,
261 uint32_t method_idx,
262 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
263 SHARED_REQUIRES(Locks::mutator_lock_);
264
265 // Build a HNewInstance instruction.
266 bool BuildNewInstance(uint16_t type_index, uint32_t dex_pc);
267
268 // Return whether the compiler can assume `cls` is initialized.
269 bool IsInitialized(Handle<mirror::Class> cls) const
270 SHARED_REQUIRES(Locks::mutator_lock_);
271
272 // Try to resolve a method using the class linker. Return null if a method could
273 // not be resolved.
274 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
275
276 ArenaAllocator* const arena_;
277
278 ArenaVector<HLocal*> locals_;
279
280 HBasicBlock* current_block_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000281 HGraph* const graph_;
David Brazdil60328912016-04-04 17:47:42 +0000282
283 // The dex file where the method being compiled is, and the bytecode data.
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000284 const DexFile* const dex_file_;
David Brazdil86ea7ee2016-02-16 09:26:07 +0000285 const DexFile::CodeItem& code_item_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000286
287 // The compilation unit of the current method being compiled. Note that
288 // it can be an inlined method.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100289 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000290
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100291 CompilerDriver* const compiler_driver_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000292
David Brazdil60328912016-04-04 17:47:42 +0000293 // The compilation unit of the outermost method being compiled. That is the
294 // method being compiled (and not inlined), and potentially inlining other
295 // methods.
296 const DexCompilationUnit* const outer_compilation_unit_;
297
298 // The return type of the method being compiled.
299 const Primitive::Type return_type_;
300
301 // The pointer in the dex file where the instructions of the code item
302 // being currently compiled start.
303 const uint16_t* code_start_;
304
305 HBasicBlockBuilder block_builder_;
306
307 // The last invoke or fill-new-array being built. Only to be
308 // used by move-result instructions.
309 HInstruction* latest_result_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100310
Calin Juravle48c2b032014-12-09 18:11:36 +0000311 OptimizingCompilerStats* compilation_stats_;
312
David Brazdil60328912016-04-04 17:47:42 +0000313 const uint8_t* interpreter_metadata_;
314
315 // Dex cache for dex_file_.
316 ScopedNullHandle<mirror::DexCache> null_dex_cache_;
317 Handle<mirror::DexCache> dex_cache_;
Mathieu Chartier736b5602015-09-02 14:54:11 -0700318
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
320};
321
322} // namespace art
323
324#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_