blob: d6b25ee82297096ec7d37ed52fbc98389f6c1d72 [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080020#include "base/arena_object.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010022#include "dex_file-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010023#include "driver/compiler_driver.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000024#include "driver/dex_compilation_unit.h"
Calin Juravle48c2b032014-12-09 18:11:36 +000025#include "optimizing_compiler_stats.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010026#include "primitive.h"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000027#include "utils/growable_array.h"
Dave Allison20dfc792014-06-16 20:44:29 -070028#include "nodes.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029
30namespace art {
31
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class Instruction;
Andreas Gampee4d4d322014-12-04 09:09:57 -080033class SwitchTable;
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,
Calin Juravle48c2b032014-12-09 18:11:36 +000041 CompilerDriver* driver,
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000042 OptimizingCompilerStats* compiler_stats,
43 const uint8_t* interpreter_metadata)
David Brazdil5e8b1372015-01-23 14:39:08 +000044 : arena_(graph->GetArena()),
45 branch_targets_(graph->GetArena(), 0),
46 locals_(graph->GetArena(), 0),
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047 entry_block_(nullptr),
48 exit_block_(nullptr),
49 current_block_(nullptr),
David Brazdil5e8b1372015-01-23 14:39:08 +000050 graph_(graph),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000051 dex_file_(dex_file),
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])),
56 code_start_(nullptr),
Calin Juravle48c2b032014-12-09 18:11:36 +000057 latest_result_(nullptr),
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010058 can_use_baseline_for_string_init_(true),
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000059 compilation_stats_(compiler_stats),
60 interpreter_metadata_(interpreter_metadata) {}
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010061
62 // Only for unit testing.
David Brazdil5e8b1372015-01-23 14:39:08 +000063 HGraphBuilder(HGraph* graph, Primitive::Type return_type = Primitive::kPrimInt)
64 : arena_(graph->GetArena()),
65 branch_targets_(graph->GetArena(), 0),
66 locals_(graph->GetArena(), 0),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010067 entry_block_(nullptr),
68 exit_block_(nullptr),
69 current_block_(nullptr),
David Brazdil5e8b1372015-01-23 14:39:08 +000070 graph_(graph),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010071 dex_file_(nullptr),
72 dex_compilation_unit_(nullptr),
73 compiler_driver_(nullptr),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000074 outer_compilation_unit_(nullptr),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +010075 return_type_(return_type),
76 code_start_(nullptr),
Calin Juravle48c2b032014-12-09 18:11:36 +000077 latest_result_(nullptr),
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010078 can_use_baseline_for_string_init_(true),
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +020079 compilation_stats_(nullptr),
80 interpreter_metadata_(nullptr) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
David Brazdil5e8b1372015-01-23 14:39:08 +000082 bool BuildGraph(const DexFile::CodeItem& code);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +010084 bool CanUseBaselineForStringInit() const {
85 return can_use_baseline_for_string_init_;
86 }
87
Andreas Gampe7c3952f2015-02-19 18:21:24 -080088 static constexpr const char* kBuilderPassName = "builder";
89
Nicolas Geoffray818f2102014-02-18 16:43:35 +000090 private:
91 // Analyzes the dex instruction and adds HInstruction to the graph
92 // to execute that instruction. Returns whether the instruction can
93 // be handled.
Calin Juravle225ff812014-11-13 16:46:39 +000094 bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000095
96 // Finds all instructions that start a new block, and populates branch_targets_ with
97 // the newly created blocks.
Nicolas Geoffray43a539f2014-12-02 10:19:51 +000098 // As a side effect, also compute the number of dex instructions, blocks, and
99 // branches.
Calin Juravle702d2602015-04-30 19:28:21 +0100100 // Returns true if all the branches fall inside the method code, false otherwise.
101 // (In normal cases this should always return true but someone can artificially
102 // create a code unit in which branches fall-through out of it).
103 bool ComputeBranchTargets(const uint16_t* start,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000104 const uint16_t* end,
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000105 size_t* number_of_branches);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000106 void MaybeUpdateCurrentBlock(size_t dex_pc);
107 HBasicBlock* FindBlockStartingAt(int32_t dex_pc) const;
108 HBasicBlock* FindOrCreateBlockStartingAt(int32_t dex_pc);
David Brazdil56e1acc2015-06-30 15:41:36 +0100109
David Brazdil49bace12015-07-01 15:28:26 +0100110 // Adds new blocks to `branch_targets_` starting at the limits of TryItems and
111 // their exception handlers.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000112 void CreateBlocksForTryCatch(const DexFile::CodeItem& code_item);
David Brazdil49bace12015-07-01 15:28:26 +0100113
114 // Splits edges which cross the boundaries of TryItems, inserts TryBoundary
115 // instructions and links them to the corresponding catch blocks.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000116 void InsertTryBoundaryBlocks(const DexFile::CodeItem& code_item);
David Brazdil49bace12015-07-01 15:28:26 +0100117
118 // Splits a single edge, inserting a TryBoundary of given `kind` and linking
119 // it to exception handlers of `try_item`.
David Brazdil56e1acc2015-06-30 15:41:36 +0100120 void SplitTryBoundaryEdge(HBasicBlock* predecessor,
121 HBasicBlock* successor,
122 HTryBoundary::BoundaryKind kind,
123 const DexFile::CodeItem& code_item,
124 const DexFile::TryItem& try_item);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000126 bool CanDecodeQuickenedInfo() const;
127 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
128
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100129 void InitializeLocals(uint16_t count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000130 HLocal* GetLocalAt(int register_index) const;
131 void UpdateLocal(int register_index, HInstruction* instruction) const;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100132 HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
David Brazdil852eaff2015-02-02 15:23:05 +0000133 void PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +0000134 void InitializeParameters(uint16_t number_of_parameters);
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +0000135 bool NeedsAccessCheck(uint32_t type_index) const;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100136
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100137 template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100138 void Unop_12x(const Instruction& instruction, Primitive::Type type);
139
140 template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100141 void Binop_23x(const Instruction& instruction, Primitive::Type type);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100142
143 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000144 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
145
146 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000147 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type);
148
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 void Binop_23x_cmp(const Instruction& instruction,
150 Primitive::Type type,
Mark Mendellc4701932015-04-10 13:18:51 -0400151 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700152 uint32_t dex_pc);
Calin Juravleddb7df22014-11-25 20:56:51 +0000153
Calin Juravle9aec02f2014-11-18 23:06:35 +0000154 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100155 void Binop_12x(const Instruction& instruction, Primitive::Type type);
156
157 template<typename T>
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000158 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
159
160 template<typename T>
Calin Juravle9aec02f2014-11-18 23:06:35 +0000161 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type);
162
163 template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100164 void Binop_22b(const Instruction& instruction, bool reverse);
165
166 template<typename T>
167 void Binop_22s(const Instruction& instruction, bool reverse);
168
Calin Juravle225ff812014-11-13 16:46:39 +0000169 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
170 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100171
Roland Levillaindff1f282014-11-05 14:15:05 +0000172 void Conversion_12x(const Instruction& instruction,
173 Primitive::Type input_type,
Roland Levillain624279f2014-12-04 11:54:28 +0000174 Primitive::Type result_type,
175 uint32_t dex_pc);
Roland Levillaindff1f282014-11-05 14:15:05 +0000176
Calin Juravlebacfec32014-11-14 15:54:36 +0000177 void BuildCheckedDivRem(uint16_t out_reg,
178 uint16_t first_reg,
179 int64_t second_reg_or_constant,
180 uint32_t dex_pc,
181 Primitive::Type type,
182 bool second_is_lit,
183 bool is_div);
Calin Juravled0d48522014-11-04 16:40:20 +0000184
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100185 void BuildReturn(const Instruction& instruction, Primitive::Type type);
186
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100187 // Builds an instance field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000188 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100189
190 // Builds a static field access node and returns whether the instruction is supported.
Calin Juravle225ff812014-11-13 16:46:39 +0000191 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100193 void BuildArrayAccess(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000194 uint32_t dex_pc,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100195 bool is_get,
196 Primitive::Type anticipated_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100197
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100198 // Builds an invocation node and returns whether the instruction is supported.
199 bool BuildInvoke(const Instruction& instruction,
Calin Juravle225ff812014-11-13 16:46:39 +0000200 uint32_t dex_pc,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100201 uint32_t method_idx,
202 uint32_t number_of_vreg_arguments,
203 bool is_range,
204 uint32_t* args,
205 uint32_t register_index);
206
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100207 // Builds a new array node and the instructions that fill it.
Calin Juravle225ff812014-11-13 16:46:39 +0000208 void BuildFilledNewArray(uint32_t dex_pc,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100209 uint32_t type_index,
210 uint32_t number_of_vreg_arguments,
211 bool is_range,
212 uint32_t* args,
213 uint32_t register_index);
214
Calin Juravle225ff812014-11-13 16:46:39 +0000215 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
Calin Juravled0d48522014-11-04 16:40:20 +0000216
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100217 // Fills the given object with data as specified in the fill-array-data
218 // instruction. Currently only used for non-reference and non-floating point
219 // arrays.
220 template <typename T>
221 void BuildFillArrayData(HInstruction* object,
222 const T* data,
223 uint32_t element_count,
224 Primitive::Type anticipated_type,
Calin Juravle225ff812014-11-13 16:46:39 +0000225 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100226
227 // Fills the given object with data as specified in the fill-array-data
228 // instruction. The data must be for long and double arrays.
229 void BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100230 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100231 uint32_t element_count,
Calin Juravle225ff812014-11-13 16:46:39 +0000232 uint32_t dex_pc);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100233
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000234 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
235 // Returns whether we succeeded in building the instruction.
236 bool BuildTypeCheck(const Instruction& instruction,
237 uint8_t destination,
238 uint8_t reference,
239 uint16_t type_index,
Calin Juravle225ff812014-11-13 16:46:39 +0000240 uint32_t dex_pc);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000241
Andreas Gampee4d4d322014-12-04 09:09:57 -0800242 // Builds an instruction sequence for a packed switch statement.
Calin Juravle48c2b032014-12-09 18:11:36 +0000243 void BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gamped881df52014-11-24 23:28:39 -0800244
Andreas Gampee4d4d322014-12-04 09:09:57 -0800245 // Builds an instruction sequence for a sparse switch statement.
Calin Juravle48c2b032014-12-09 18:11:36 +0000246 void BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc);
Andreas Gampee4d4d322014-12-04 09:09:57 -0800247
248 void BuildSwitchCaseHelper(const Instruction& instruction, size_t index,
249 bool is_last_case, const SwitchTable& table,
250 HInstruction* value, int32_t case_value_int,
251 int32_t target_offset, uint32_t dex_pc);
252
David Brazdil1b498722015-03-31 11:37:18 +0100253 bool SkipCompilation(const DexFile::CodeItem& code_item, size_t number_of_branches);
Calin Juravle48c2b032014-12-09 18:11:36 +0000254
255 void MaybeRecordStat(MethodCompilationStat compilation_stat);
256
Nicolas Geoffray30451742015-06-19 13:32:41 +0100257 // Returns the outer-most compiling method's class.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000258 mirror::Class* GetOutermostCompilingClass() const;
259
Nicolas Geoffray30451742015-06-19 13:32:41 +0100260 // Returns the class whose method is being compiled.
261 mirror::Class* GetCompilingClass() const;
262
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000263 // Returns whether `type_index` points to the outer-most compiling method's class.
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000264 bool IsOutermostCompilingClass(uint16_t type_index) const;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000265
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100266 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
267 uint32_t dex_pc,
268 HInvoke* invoke);
269
Vladimir Marko58155012015-08-19 12:49:41 +0000270 HInvokeStaticOrDirect::DispatchInfo ComputeDispatchInfo(bool is_string_init,
271 int32_t string_init_offset,
272 MethodReference target_method,
273 uintptr_t direct_method,
274 uintptr_t direct_code);
275
Calin Juravle0eedd7e2015-08-20 14:48:00 +0100276 bool SetupArgumentsAndAddInvoke(HInvoke* invoke,
277 uint32_t number_of_vreg_arguments,
278 uint32_t* args,
279 uint32_t register_index,
280 bool is_range,
281 const char* descriptor,
282 HClinitCheck* clinit_check);
Calin Juravle68ad6492015-08-18 17:08:12 +0100283
284 HClinitCheck* ProcessClinitCheckForInvoke(
285 uint32_t dex_pc,
286 uint32_t method_idx,
287 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement);
288
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290
291 // A list of the size of the dex code holding block information for
292 // the method. If an entry contains a block, then the dex instruction
293 // starting at that entry is the first instruction of a new block.
294 GrowableArray<HBasicBlock*> branch_targets_;
295
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000296 GrowableArray<HLocal*> locals_;
297
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000298 HBasicBlock* entry_block_;
299 HBasicBlock* exit_block_;
300 HBasicBlock* current_block_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000301 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000303 // The dex file where the method being compiled is.
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000304 const DexFile* const dex_file_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000305
306 // The compilation unit of the current method being compiled. Note that
307 // it can be an inlined method.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100308 DexCompilationUnit* const dex_compilation_unit_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000309
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100310 CompilerDriver* const compiler_driver_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000311
312 // The compilation unit of the outermost method being compiled. That is the
313 // method being compiled (and not inlined), and potentially inlining other
314 // methods.
315 const DexCompilationUnit* const outer_compilation_unit_;
316
317 // The return type of the method being compiled.
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100318 const Primitive::Type return_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000319
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100320 // The pointer in the dex file where the instructions of the code item
321 // being currently compiled start.
322 const uint16_t* code_start_;
323
324 // The last invoke or fill-new-array being built. Only to be
325 // used by move-result instructions.
326 HInstruction* latest_result_;
327
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +0100328 // We need to know whether we have built a graph that has calls to StringFactory
329 // and hasn't gone through the verifier. If the following flag is `false`, then
330 // we cannot compile with baseline.
331 bool can_use_baseline_for_string_init_;
332
Calin Juravle48c2b032014-12-09 18:11:36 +0000333 OptimizingCompilerStats* compilation_stats_;
334
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +0000335 const uint8_t* interpreter_metadata_;
336
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337 DISALLOW_COPY_AND_ASSIGN(HGraphBuilder);
338};
339
340} // namespace art
341
342#endif // ART_COMPILER_OPTIMIZING_BUILDER_H_