blob: e968760d847408968248f051fc6166ff9716942d [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
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000034class CodeGenerator;
Andreas Gampe26de38b2016-07-27 17:53:11 -070035class Instruction;
36
David Brazdildee58d62016-04-07 09:54:26 +000037class HInstructionBuilder : public ValueObject {
38 public:
39 HInstructionBuilder(HGraph* graph,
40 HBasicBlockBuilder* block_builder,
41 SsaBuilder* ssa_builder,
42 const DexFile* dex_file,
43 const DexFile::CodeItem& code_item,
44 Primitive::Type return_type,
45 DexCompilationUnit* dex_compilation_unit,
46 const DexCompilationUnit* const outer_compilation_unit,
47 CompilerDriver* driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000048 CodeGenerator* code_generator,
David Brazdildee58d62016-04-07 09:54:26 +000049 const uint8_t* interpreter_metadata,
50 OptimizingCompilerStats* compiler_stats,
Nicolas Geoffray5247c082017-01-13 14:17:29 +000051 Handle<mirror::DexCache> dex_cache,
52 VariableSizedHandleScope* handles)
David Brazdildee58d62016-04-07 09:54:26 +000053 : arena_(graph->GetArena()),
54 graph_(graph),
Nicolas Geoffray5247c082017-01-13 14:17:29 +000055 handles_(handles),
David Brazdildee58d62016-04-07 09:54:26 +000056 dex_file_(dex_file),
57 code_item_(code_item),
58 return_type_(return_type),
59 block_builder_(block_builder),
60 ssa_builder_(ssa_builder),
61 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)),
62 current_block_(nullptr),
63 current_locals_(nullptr),
64 latest_result_(nullptr),
Igor Murashkind01745e2017-04-05 16:40:31 -070065 current_this_parameter_(nullptr),
David Brazdildee58d62016-04-07 09:54:26 +000066 compiler_driver_(driver),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000067 code_generator_(code_generator),
David Brazdildee58d62016-04-07 09:54:26 +000068 dex_compilation_unit_(dex_compilation_unit),
69 outer_compilation_unit_(outer_compilation_unit),
70 interpreter_metadata_(interpreter_metadata),
71 skipped_interpreter_metadata_(std::less<uint32_t>(),
72 arena_->Adapter(kArenaAllocGraphBuilder)),
73 compilation_stats_(compiler_stats),
74 dex_cache_(dex_cache),
75 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
76 loop_headers_.reserve(kDefaultNumberOfLoops);
77 }
78
79 bool Build();
80
81 private:
82 void MaybeRecordStat(MethodCompilationStat compilation_stat);
83
84 void InitializeBlockLocals();
85 void PropagateLocalsToCatchBlocks();
86 void SetLoopHeaderPhiInputs();
87
88 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc);
89 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
90
91 bool CanDecodeQuickenedInfo() const;
92 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
93
94 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
95
96 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
Mingyao Yang01b47b02017-02-03 12:09:57 -080097 // Out of line version of GetLocalsFor(), which has a fast path that is
98 // beneficial to get inlined by callers.
99 ArenaVector<HInstruction*>* GetLocalsForWithAllocation(
100 HBasicBlock* block, ArenaVector<HInstruction*>* locals, const size_t vregs);
David Brazdildee58d62016-04-07 09:54:26 +0000101 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
102 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +0100103 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000104 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
105
106 void AppendInstruction(HInstruction* instruction);
107 void InsertInstructionAtTop(HInstruction* instruction);
108 void InitializeInstruction(HInstruction* instruction);
109
110 void InitializeParameters();
111
112 // Returns whether the current method needs access check for the type.
113 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000114 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000116
117 template<typename T>
118 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
119
120 template<typename T>
121 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
122
123 template<typename T>
124 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
125
126 void Binop_23x_cmp(const Instruction& instruction,
127 Primitive::Type type,
128 ComparisonBias bias,
129 uint32_t dex_pc);
130
131 template<typename T>
132 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
133
134 template<typename T>
135 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
136
137 template<typename T>
138 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
139
140 template<typename T>
141 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
142
143 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
144 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
145
146 void Conversion_12x(const Instruction& instruction,
147 Primitive::Type input_type,
148 Primitive::Type result_type,
149 uint32_t dex_pc);
150
151 void BuildCheckedDivRem(uint16_t out_reg,
152 uint16_t first_reg,
153 int64_t second_reg_or_constant,
154 uint32_t dex_pc,
155 Primitive::Type type,
156 bool second_is_lit,
157 bool is_div);
158
159 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
160
161 // Builds an instance field access node and returns whether the instruction is supported.
162 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
163
164 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
165 uint32_t dex_pc,
166 bool is_put,
167 Primitive::Type field_type);
168 // Builds a static field access node and returns whether the instruction is supported.
169 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
170
171 void BuildArrayAccess(const Instruction& instruction,
172 uint32_t dex_pc,
173 bool is_get,
174 Primitive::Type anticipated_type);
175
176 // Builds an invocation node and returns whether the instruction is supported.
177 bool BuildInvoke(const Instruction& instruction,
178 uint32_t dex_pc,
179 uint32_t method_idx,
180 uint32_t number_of_vreg_arguments,
181 bool is_range,
182 uint32_t* args,
183 uint32_t register_index);
184
Orion Hodsonac141392017-01-13 11:53:47 +0000185 // Builds an invocation node for invoke-polymorphic and returns whether the
186 // instruction is supported.
187 bool BuildInvokePolymorphic(const Instruction& instruction,
188 uint32_t dex_pc,
189 uint32_t method_idx,
190 uint32_t proto_idx,
191 uint32_t number_of_vreg_arguments,
192 bool is_range,
193 uint32_t* args,
194 uint32_t register_index);
195
David Brazdildee58d62016-04-07 09:54:26 +0000196 // Builds a new array node and the instructions that fill it.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700197 HNewArray* BuildFilledNewArray(uint32_t dex_pc,
198 dex::TypeIndex type_index,
199 uint32_t number_of_vreg_arguments,
200 bool is_range,
201 uint32_t* args,
202 uint32_t register_index);
David Brazdildee58d62016-04-07 09:54:26 +0000203
204 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
205
206 // Fills the given object with data as specified in the fill-array-data
207 // instruction. Currently only used for non-reference and non-floating point
208 // arrays.
209 template <typename T>
210 void BuildFillArrayData(HInstruction* object,
211 const T* data,
212 uint32_t element_count,
213 Primitive::Type anticipated_type,
214 uint32_t dex_pc);
215
216 // Fills the given object with data as specified in the fill-array-data
217 // instruction. The data must be for long and double arrays.
218 void BuildFillWideArrayData(HInstruction* object,
219 const int64_t* data,
220 uint32_t element_count,
221 uint32_t dex_pc);
222
223 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
224 void BuildTypeCheck(const Instruction& instruction,
225 uint8_t destination,
226 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800227 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000228 uint32_t dex_pc);
229
230 // Builds an instruction sequence for a switch statement.
231 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
232
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000233 // Builds a `HLoadClass` loading the given `type_index`. If `outer` is true,
234 // this method will use the outer class's dex file to lookup the type at
235 // `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000236 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
237
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000238 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000239 const DexFile& dex_file,
240 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000241 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000242 bool needs_access_check)
243 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000244
David Brazdildee58d62016-04-07 09:54:26 +0000245 // Returns the outer-most compiling method's class.
246 mirror::Class* GetOutermostCompilingClass() const;
247
248 // Returns the class whose method is being compiled.
249 mirror::Class* GetCompilingClass() const;
250
251 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800252 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000253
254 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
255 uint32_t dex_pc,
256 HInvoke* invoke);
257
258 bool SetupInvokeArguments(HInvoke* invoke,
259 uint32_t number_of_vreg_arguments,
260 uint32_t* args,
261 uint32_t register_index,
262 bool is_range,
263 const char* descriptor,
264 size_t start_index,
265 size_t* argument_index);
266
267 bool HandleInvoke(HInvoke* invoke,
268 uint32_t number_of_vreg_arguments,
269 uint32_t* args,
270 uint32_t register_index,
271 bool is_range,
272 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700273 HClinitCheck* clinit_check,
274 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000275
276 bool HandleStringInit(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 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
283
284 HClinitCheck* ProcessClinitCheckForInvoke(
285 uint32_t dex_pc,
286 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000287 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000289
290 // Build a HNewInstance instruction.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700291 HNewInstance* BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
292
293 // Build a HConstructorFence for HNewInstance and HNewArray instructions. This ensures the
294 // happens-before ordering for default-initialization of the object referred to by new_instance.
295 void BuildConstructorFenceForAllocation(HInstruction* allocation);
David Brazdildee58d62016-04-07 09:54:26 +0000296
297 // Return whether the compiler can assume `cls` is initialized.
298 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700299 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000300
301 // Try to resolve a method using the class linker. Return null if a method could
302 // not be resolved.
303 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
304
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000305 // Try to resolve a field using the class linker. Return null if it could not
306 // be found.
307 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
308
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000309 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
310 const DexCompilationUnit& compilation_unit) const
311 REQUIRES_SHARED(Locks::mutator_lock_);
312
313 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
314
David Brazdildee58d62016-04-07 09:54:26 +0000315 ArenaAllocator* const arena_;
316 HGraph* const graph_;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000317 VariableSizedHandleScope* handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000318
319 // The dex file where the method being compiled is, and the bytecode data.
320 const DexFile* const dex_file_;
321 const DexFile::CodeItem& code_item_;
322
323 // The return type of the method being compiled.
324 const Primitive::Type return_type_;
325
326 HBasicBlockBuilder* block_builder_;
327 SsaBuilder* ssa_builder_;
328
329 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
330 HBasicBlock* current_block_;
331 ArenaVector<HInstruction*>* current_locals_;
332 HInstruction* latest_result_;
Igor Murashkind01745e2017-04-05 16:40:31 -0700333 // Current "this" parameter.
334 // Valid only after InitializeParameters() finishes.
335 // * Null for static methods.
336 // * Non-null for instance methods.
337 HParameterValue* current_this_parameter_;
David Brazdildee58d62016-04-07 09:54:26 +0000338
339 CompilerDriver* const compiler_driver_;
340
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000341 CodeGenerator* const code_generator_;
342
David Brazdildee58d62016-04-07 09:54:26 +0000343 // The compilation unit of the current method being compiled. Note that
344 // it can be an inlined method.
345 DexCompilationUnit* const dex_compilation_unit_;
346
347 // The compilation unit of the outermost method being compiled. That is the
348 // method being compiled (and not inlined), and potentially inlining other
349 // methods.
350 const DexCompilationUnit* const outer_compilation_unit_;
351
352 // Original values kept after instruction quickening. This is a data buffer
353 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
354 const uint8_t* interpreter_metadata_;
355
356 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
357 // info for out-of-order dex_pcs is stored in a map until the positions
358 // are eventually visited.
359 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
360
361 OptimizingCompilerStats* compilation_stats_;
362 Handle<mirror::DexCache> dex_cache_;
363
364 ArenaVector<HBasicBlock*> loop_headers_;
365
366 static constexpr int kDefaultNumberOfLoops = 2;
367
368 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
369};
370
371} // namespace art
372
373#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_