blob: 6e3b078dbb35c2b8d52cd7a347004d56847d792b [file] [log] [blame]
David Brazdildee58d62016-04-07 09:54:26 +00001/*
2 * Copyright (C) 2016 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_INSTRUCTION_BUILDER_H_
18#define ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_
19
20#include "base/arena_containers.h"
21#include "base/arena_object.h"
22#include "block_builder.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080023#include "dex_file_types.h"
David Brazdildee58d62016-04-07 09:54:26 +000024#include "driver/compiler_driver.h"
25#include "driver/compiler_driver-inl.h"
26#include "driver/dex_compilation_unit.h"
27#include "mirror/dex_cache.h"
28#include "nodes.h"
29#include "optimizing_compiler_stats.h"
30#include "ssa_builder.h"
31
32namespace art {
33
Andreas Gampe26de38b2016-07-27 17:53:11 -070034class Instruction;
35
David Brazdildee58d62016-04-07 09:54:26 +000036class HInstructionBuilder : public ValueObject {
37 public:
38 HInstructionBuilder(HGraph* graph,
39 HBasicBlockBuilder* block_builder,
40 SsaBuilder* ssa_builder,
41 const DexFile* dex_file,
42 const DexFile::CodeItem& code_item,
43 Primitive::Type return_type,
44 DexCompilationUnit* dex_compilation_unit,
45 const DexCompilationUnit* const outer_compilation_unit,
46 CompilerDriver* driver,
47 const uint8_t* interpreter_metadata,
48 OptimizingCompilerStats* compiler_stats,
Nicolas Geoffray5247c082017-01-13 14:17:29 +000049 Handle<mirror::DexCache> dex_cache,
50 VariableSizedHandleScope* handles)
David Brazdildee58d62016-04-07 09:54:26 +000051 : arena_(graph->GetArena()),
52 graph_(graph),
Nicolas Geoffray5247c082017-01-13 14:17:29 +000053 handles_(handles),
David Brazdildee58d62016-04-07 09:54:26 +000054 dex_file_(dex_file),
55 code_item_(code_item),
56 return_type_(return_type),
57 block_builder_(block_builder),
58 ssa_builder_(ssa_builder),
59 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)),
60 current_block_(nullptr),
61 current_locals_(nullptr),
62 latest_result_(nullptr),
63 compiler_driver_(driver),
64 dex_compilation_unit_(dex_compilation_unit),
65 outer_compilation_unit_(outer_compilation_unit),
66 interpreter_metadata_(interpreter_metadata),
67 skipped_interpreter_metadata_(std::less<uint32_t>(),
68 arena_->Adapter(kArenaAllocGraphBuilder)),
69 compilation_stats_(compiler_stats),
70 dex_cache_(dex_cache),
71 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
72 loop_headers_.reserve(kDefaultNumberOfLoops);
73 }
74
75 bool Build();
76
77 private:
78 void MaybeRecordStat(MethodCompilationStat compilation_stat);
79
80 void InitializeBlockLocals();
81 void PropagateLocalsToCatchBlocks();
82 void SetLoopHeaderPhiInputs();
83
84 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc);
85 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
86
87 bool CanDecodeQuickenedInfo() const;
88 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
89
90 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
91
92 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
93 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
94 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010095 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000096 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
97
98 void AppendInstruction(HInstruction* instruction);
99 void InsertInstructionAtTop(HInstruction* instruction);
100 void InitializeInstruction(HInstruction* instruction);
101
102 void InitializeParameters();
103
104 // Returns whether the current method needs access check for the type.
105 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Markoec786222016-12-20 16:24:13 +0000106 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000108
109 template<typename T>
110 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
111
112 template<typename T>
113 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
114
115 template<typename T>
116 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
117
118 void Binop_23x_cmp(const Instruction& instruction,
119 Primitive::Type type,
120 ComparisonBias bias,
121 uint32_t dex_pc);
122
123 template<typename T>
124 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
125
126 template<typename T>
127 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
128
129 template<typename T>
130 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
131
132 template<typename T>
133 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
134
135 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
136 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
137
138 void Conversion_12x(const Instruction& instruction,
139 Primitive::Type input_type,
140 Primitive::Type result_type,
141 uint32_t dex_pc);
142
143 void BuildCheckedDivRem(uint16_t out_reg,
144 uint16_t first_reg,
145 int64_t second_reg_or_constant,
146 uint32_t dex_pc,
147 Primitive::Type type,
148 bool second_is_lit,
149 bool is_div);
150
151 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
152
153 // Builds an instance field access node and returns whether the instruction is supported.
154 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
155
156 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
157 uint32_t dex_pc,
158 bool is_put,
159 Primitive::Type field_type);
160 // Builds a static field access node and returns whether the instruction is supported.
161 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
162
163 void BuildArrayAccess(const Instruction& instruction,
164 uint32_t dex_pc,
165 bool is_get,
166 Primitive::Type anticipated_type);
167
168 // Builds an invocation node and returns whether the instruction is supported.
169 bool BuildInvoke(const Instruction& instruction,
170 uint32_t dex_pc,
171 uint32_t method_idx,
172 uint32_t number_of_vreg_arguments,
173 bool is_range,
174 uint32_t* args,
175 uint32_t register_index);
176
Orion Hodsonac141392017-01-13 11:53:47 +0000177 // Builds an invocation node for invoke-polymorphic and returns whether the
178 // instruction is supported.
179 bool BuildInvokePolymorphic(const Instruction& instruction,
180 uint32_t dex_pc,
181 uint32_t method_idx,
182 uint32_t proto_idx,
183 uint32_t number_of_vreg_arguments,
184 bool is_range,
185 uint32_t* args,
186 uint32_t register_index);
187
David Brazdildee58d62016-04-07 09:54:26 +0000188 // Builds a new array node and the instructions that fill it.
189 void BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800190 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000191 uint32_t number_of_vreg_arguments,
192 bool is_range,
193 uint32_t* args,
194 uint32_t register_index);
195
196 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
197
198 // Fills the given object with data as specified in the fill-array-data
199 // instruction. Currently only used for non-reference and non-floating point
200 // arrays.
201 template <typename T>
202 void BuildFillArrayData(HInstruction* object,
203 const T* data,
204 uint32_t element_count,
205 Primitive::Type anticipated_type,
206 uint32_t dex_pc);
207
208 // Fills the given object with data as specified in the fill-array-data
209 // instruction. The data must be for long and double arrays.
210 void BuildFillWideArrayData(HInstruction* object,
211 const int64_t* data,
212 uint32_t element_count,
213 uint32_t dex_pc);
214
215 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
216 void BuildTypeCheck(const Instruction& instruction,
217 uint8_t destination,
218 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800219 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000220 uint32_t dex_pc);
221
222 // Builds an instruction sequence for a switch statement.
223 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
224
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000225 // Builds a `HLoadClass` loading the given `type_index`. If `outer` is true,
226 // this method will use the outer class's dex file to lookup the type at
227 // `type_index`.
228 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
229 uint32_t dex_pc,
230 bool check_access,
231 bool outer = false);
232
David Brazdildee58d62016-04-07 09:54:26 +0000233 // Returns the outer-most compiling method's class.
234 mirror::Class* GetOutermostCompilingClass() const;
235
236 // Returns the class whose method is being compiled.
237 mirror::Class* GetCompilingClass() const;
238
239 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800240 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000241
242 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
243 uint32_t dex_pc,
244 HInvoke* invoke);
245
246 bool SetupInvokeArguments(HInvoke* invoke,
247 uint32_t number_of_vreg_arguments,
248 uint32_t* args,
249 uint32_t register_index,
250 bool is_range,
251 const char* descriptor,
252 size_t start_index,
253 size_t* argument_index);
254
255 bool HandleInvoke(HInvoke* invoke,
256 uint32_t number_of_vreg_arguments,
257 uint32_t* args,
258 uint32_t register_index,
259 bool is_range,
260 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700261 HClinitCheck* clinit_check,
262 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000263
264 bool HandleStringInit(HInvoke* invoke,
265 uint32_t number_of_vreg_arguments,
266 uint32_t* args,
267 uint32_t register_index,
268 bool is_range,
269 const char* descriptor);
270 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
271
272 HClinitCheck* ProcessClinitCheckForInvoke(
273 uint32_t dex_pc,
274 ArtMethod* method,
275 uint32_t method_idx,
276 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700277 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000278
279 // Build a HNewInstance instruction.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800280 bool BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000281
282 // Return whether the compiler can assume `cls` is initialized.
283 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000285
286 // Try to resolve a method using the class linker. Return null if a method could
287 // not be resolved.
288 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
289
Vladimir Markoec786222016-12-20 16:24:13 +0000290 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
291 const DexCompilationUnit& compilation_unit) const
292 REQUIRES_SHARED(Locks::mutator_lock_);
293
294 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
295
David Brazdildee58d62016-04-07 09:54:26 +0000296 ArenaAllocator* const arena_;
297 HGraph* const graph_;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000298 VariableSizedHandleScope* handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000299
300 // The dex file where the method being compiled is, and the bytecode data.
301 const DexFile* const dex_file_;
302 const DexFile::CodeItem& code_item_;
303
304 // The return type of the method being compiled.
305 const Primitive::Type return_type_;
306
307 HBasicBlockBuilder* block_builder_;
308 SsaBuilder* ssa_builder_;
309
310 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
311 HBasicBlock* current_block_;
312 ArenaVector<HInstruction*>* current_locals_;
313 HInstruction* latest_result_;
314
315 CompilerDriver* const compiler_driver_;
316
317 // The compilation unit of the current method being compiled. Note that
318 // it can be an inlined method.
319 DexCompilationUnit* const dex_compilation_unit_;
320
321 // The compilation unit of the outermost method being compiled. That is the
322 // method being compiled (and not inlined), and potentially inlining other
323 // methods.
324 const DexCompilationUnit* const outer_compilation_unit_;
325
326 // Original values kept after instruction quickening. This is a data buffer
327 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
328 const uint8_t* interpreter_metadata_;
329
330 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
331 // info for out-of-order dex_pcs is stored in a map until the positions
332 // are eventually visited.
333 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
334
335 OptimizingCompilerStats* compilation_stats_;
336 Handle<mirror::DexCache> dex_cache_;
337
338 ArenaVector<HBasicBlock*> loop_headers_;
339
340 static constexpr int kDefaultNumberOfLoops = 2;
341
342 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
343};
344
345} // namespace art
346
347#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_