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
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class Instruction;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35class 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,
45 Handle<mirror::DexCache> dex_cache)
David Brazdil5e8b1372015-01-23 14:39:08 +000046 : arena_(graph->GetArena()),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010047 locals_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048 current_block_(nullptr),
David Brazdil5e8b1372015-01-23 14:39:08 +000049 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),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000054 outer_compilation_unit_(outer_compilation_unit),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010055 return_type_(Primitive::GetType(dex_compilation_unit_->GetShorty()[0])),
David Brazdil86ea7ee2016-02-16 09:26:07 +000056 code_start_(code_item.insns_),
57 block_builder_(graph, dex_file, code_item),
Calin Juravle48c2b032014-12-09 18:11:36 +000058 latest_result_(nullptr),
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000059 compilation_stats_(compiler_stats),
Mathieu Chartier736b5602015-09-02 14:54:11 -070060 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 Brazdil5e8b1372015-01-23 14:39:08 +000067 : arena_(graph->GetArena()),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010068 locals_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010069 current_block_(nullptr),
David Brazdil5e8b1372015-01-23 14:39:08 +000070 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),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000075 outer_compilation_unit_(nullptr),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010076 return_type_(return_type),
David Brazdil86ea7ee2016-02-16 09:26:07 +000077 code_start_(code_item.insns_),
78 block_builder_(graph, nullptr, code_item),
Calin Juravle48c2b032014-12-09 18:11:36 +000079 latest_result_(nullptr),
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020080 compilation_stats_(nullptr),
Mathieu Chartier736b5602015-09-02 14:54:11 -070081 interpreter_metadata_(nullptr),
Mathieu Chartier9865bde2015-12-21 09:58:16 -080082 null_dex_cache_(),
83 dex_cache_(null_dex_cache_) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
David Brazdil86ea7ee2016-02-16 09:26:07 +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 Brazdil86ea7ee2016-02-16 09:26:07 +000090 bool GenerateInstructions();
Calin Juravle225ff812014-11-13 16:46:39 +000091 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092
David Brazdil86ea7ee2016-02-16 09:26:07 +000093 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000094
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000095 bool CanDecodeQuickenedInfo() const;
96 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
97
David Brazdil86ea7ee2016-02-16 09:26:07 +000098 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const {
99 return block_builder_.GetBlockAt(dex_pc);
100 }
101
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100102 void InitializeLocals(uint16_t count);
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100103 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;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000106 void InitializeParameters(uint16_t number_of_parameters);
Mingyao Yangfb8464a2015-11-02 10:56:59 -0800107
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;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100111
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100112 template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600113 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100114
115 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
117
118 template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600119 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000120
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 void Binop_23x_cmp(const Instruction& instruction,
122 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400123 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 uint32_t dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000125
Calin Juravle9aec02f2014-11-18 23:06:35 +0000126 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000127 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
128
129 template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600130 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
Calin Juravle9aec02f2014-11-18 23:06:35 +0000131
132 template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600133 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100134
135 template<typename T>
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600136 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100137
Calin Juravle225ff812014-11-13 16:46:39 +0000138 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);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140
Roland Levillaindff1f282014-11-05 14:15:05 +0000141 void Conversion_12x(const Instruction& instruction,
142 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000143 Primitive::Type result_type,
144 uint32_t dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +0000145
Calin Juravlebacfec32014-11-14 15:54:36 +0000146 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);
Calin Juravled0d48522014-11-04 16:40:20 +0000153
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600154 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100155
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100156 // Builds an instance field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000157 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100158
Calin Juravle07380a22015-09-17 14:15:12 +0100159 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
160 uint32_t dex_pc,
161 bool is_put,
162 Primitive::Type field_type);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100163 // Builds a static field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000164 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 void BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000167 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 bool is_get,
169 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100170
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100171 // Builds an invocation node and returns whether the instruction is supported.
172 bool BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000173 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100174 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
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100180 // Builds a new array node and the instructions that fill it.
Calin Juravle225ff812014-11-13 16:46:39 +0000181 void BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100182 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
Calin Juravle225ff812014-11-13 16:46:39 +0000188 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000189
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100190 // 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,
Calin Juravle225ff812014-11-13 16:46:39 +0000198 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100199
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,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100203 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100204 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000205 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100206
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000207 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
Calin Juravle98893e12015-10-02 21:05:03 +0100208 void BuildTypeCheck(const Instruction& instruction,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000209 uint8_t destination,
210 uint8_t reference,
211 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000212 uint32_t dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000213
David Brazdil86ea7ee2016-02-16 09:26:07 +0000214 // Builds an instruction sequence for a switch statement.
215 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800216
David Brazdil86ea7ee2016-02-16 09:26:07 +0000217 bool SkipCompilation(size_t number_of_branches);
Calin Juravle48c2b032014-12-09 18:11:36 +0000218
219 void MaybeRecordStat(MethodCompilationStat compilation_stat);
220
Nicolas Geoffray30451742015-06-19 13:32:41 +0100221 // Returns the outer-most compiling method's class.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000222 mirror::Class* GetOutermostCompilingClass() const;
223
Nicolas Geoffray30451742015-06-19 13:32:41 +0100224 // Returns the class whose method is being compiled.
225 mirror::Class* GetCompilingClass() const;
226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000227 // Returns whether `type_index` points to the outer-most compiling method's class.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000228 bool IsOutermostCompilingClass(uint16_t type_index) const;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000229
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100230 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
231 uint32_t dex_pc,
232 HInvoke* invoke);
233
Calin Juravle5d01db12015-08-25 15:02:42 +0100234 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);
Calin Juravle68ad6492015-08-18 17:08:12 +0100257
258 HClinitCheck* ProcessClinitCheckForInvoke(
259 uint32_t dex_pc,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000260 ArtMethod* method,
Calin Juravle68ad6492015-08-18 17:08:12 +0100261 uint32_t method_idx,
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000262 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
263 SHARED_REQUIRES(Locks::mutator_lock_);
Calin Juravle68ad6492015-08-18 17:08:12 +0100264
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000265 // Build a HNewInstance instruction.
266 bool BuildNewInstance(uint16_t type_index, uint32_t dex_pc);
267
Nicolas Geoffrayd9dc6f42015-11-24 14:06:57 +0000268 // Return whether the compiler can assume `cls` is initialized.
269 bool IsInitialized(Handle<mirror::Class> cls) const
Nicolas Geoffray729645a2015-11-19 13:29:02 +0000270 SHARED_REQUIRES(Locks::mutator_lock_);
271
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000272 // 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
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000277
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100278 ArenaVector<HLocal*> locals_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000279
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 HBasicBlock* current_block_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000281 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000282
David Brazdil86ea7ee2016-02-16 09:26:07 +0000283 // 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
293 // 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.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100299 const Primitive::Type return_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000300
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100301 // 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
David Brazdil86ea7ee2016-02-16 09:26:07 +0000305 HBasicBlockBuilder block_builder_;
306
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100307 // The last invoke or fill-new-array being built. Only to be
308 // used by move-result instructions.
309 HInstruction* latest_result_;
310
Calin Juravle48c2b032014-12-09 18:11:36 +0000311 OptimizingCompilerStats* compilation_stats_;
312
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000313 const uint8_t* interpreter_metadata_;
314
Mathieu Chartier736b5602015-09-02 14:54:11 -0700315 // Dex cache for dex_file_.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800316 ScopedNullHandle<mirror::DexCache> null_dex_cache_;
Mathieu Chartier736b5602015-09-02 14:54:11 -0700317 Handle<mirror::DexCache> dex_cache_;
318
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_