blob: a684bf40e688ed0df52a958aecbf69e19ccdca84 [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-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "driver/compiler_driver.h"
David Brazdildee58d62016-04-07 09:54:26 +000026#include "driver/dex_compilation_unit.h"
27#include "mirror/dex_cache.h"
28#include "nodes.h"
29#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070030#include "quicken_info.h"
David Brazdildee58d62016-04-07 09:54:26 +000031#include "ssa_builder.h"
32
33namespace art {
34
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000035class CodeGenerator;
Andreas Gampe26de38b2016-07-27 17:53:11 -070036class Instruction;
37
David Brazdildee58d62016-04-07 09:54:26 +000038class HInstructionBuilder : public ValueObject {
39 public:
40 HInstructionBuilder(HGraph* graph,
41 HBasicBlockBuilder* block_builder,
42 SsaBuilder* ssa_builder,
43 const DexFile* dex_file,
44 const DexFile::CodeItem& code_item,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010045 DataType::Type return_type,
David Brazdildee58d62016-04-07 09:54:26 +000046 DexCompilationUnit* dex_compilation_unit,
47 const DexCompilationUnit* const outer_compilation_unit,
48 CompilerDriver* driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000049 CodeGenerator* code_generator,
David Brazdildee58d62016-04-07 09:54:26 +000050 const uint8_t* interpreter_metadata,
51 OptimizingCompilerStats* compiler_stats,
Nicolas Geoffray5247c082017-01-13 14:17:29 +000052 Handle<mirror::DexCache> dex_cache,
53 VariableSizedHandleScope* handles)
David Brazdildee58d62016-04-07 09:54:26 +000054 : arena_(graph->GetArena()),
55 graph_(graph),
Nicolas Geoffray5247c082017-01-13 14:17:29 +000056 handles_(handles),
David Brazdildee58d62016-04-07 09:54:26 +000057 dex_file_(dex_file),
58 code_item_(code_item),
59 return_type_(return_type),
60 block_builder_(block_builder),
61 ssa_builder_(ssa_builder),
62 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)),
63 current_block_(nullptr),
64 current_locals_(nullptr),
65 latest_result_(nullptr),
Igor Murashkind01745e2017-04-05 16:40:31 -070066 current_this_parameter_(nullptr),
David Brazdildee58d62016-04-07 09:54:26 +000067 compiler_driver_(driver),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000068 code_generator_(code_generator),
David Brazdildee58d62016-04-07 09:54:26 +000069 dex_compilation_unit_(dex_compilation_unit),
70 outer_compilation_unit_(outer_compilation_unit),
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070071 quicken_info_(interpreter_metadata),
David Brazdildee58d62016-04-07 09:54:26 +000072 compilation_stats_(compiler_stats),
73 dex_cache_(dex_cache),
74 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
75 loop_headers_.reserve(kDefaultNumberOfLoops);
76 }
77
78 bool Build();
79
80 private:
David Brazdildee58d62016-04-07 09:54:26 +000081 void InitializeBlockLocals();
82 void PropagateLocalsToCatchBlocks();
83 void SetLoopHeaderPhiInputs();
84
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070085 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc, size_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +000086 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
87
88 bool CanDecodeQuickenedInfo() const;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070089 uint16_t LookupQuickenedInfo(uint32_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +000090
91 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
92
93 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
Mingyao Yang01b47b02017-02-03 12:09:57 -080094 // Out of line version of GetLocalsFor(), which has a fast path that is
95 // beneficial to get inlined by callers.
96 ArenaVector<HInstruction*>* GetLocalsForWithAllocation(
97 HBasicBlock* block, ArenaVector<HInstruction*>* locals, const size_t vregs);
David Brazdildee58d62016-04-07 09:54:26 +000098 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010099 HInstruction* LoadLocal(uint32_t register_index, DataType::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +0100100 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000101 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
102
103 void AppendInstruction(HInstruction* instruction);
104 void InsertInstructionAtTop(HInstruction* instruction);
105 void InitializeInstruction(HInstruction* instruction);
106
107 void InitializeParameters();
108
109 // Returns whether the current method needs access check for the type.
110 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000111 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000113
114 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100115 void Unop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000116
117 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100118 void Binop_23x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000119
120 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100121 void Binop_23x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000122
123 void Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100124 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000125 ComparisonBias bias,
126 uint32_t dex_pc);
127
128 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 void Binop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000130
131 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132 void Binop_12x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000133
134 template<typename T>
135 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
136
137 template<typename T>
138 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
139
140 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
141 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
142
143 void Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100144 DataType::Type input_type,
145 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000146 uint32_t dex_pc);
147
148 void BuildCheckedDivRem(uint16_t out_reg,
149 uint16_t first_reg,
150 int64_t second_reg_or_constant,
151 uint32_t dex_pc,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000153 bool second_is_lit,
154 bool is_div);
155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100156 void BuildReturn(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000157
158 // Builds an instance field access node and returns whether the instruction is supported.
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700159 bool BuildInstanceFieldAccess(const Instruction& instruction,
160 uint32_t dex_pc,
161 bool is_put,
162 size_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +0000163
164 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
165 uint32_t dex_pc,
166 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100167 DataType::Type field_type);
David Brazdildee58d62016-04-07 09:54:26 +0000168 // 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,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100174 DataType::Type anticipated_type);
David Brazdildee58d62016-04-07 09:54:26 +0000175
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,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100213 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +0000214 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.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100324 const DataType::Type return_type_;
David Brazdildee58d62016-04-07 09:54:26 +0000325
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
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700352 // Original values kept after instruction quickening.
353 QuickenInfoTable quicken_info_;
David Brazdildee58d62016-04-07 09:54:26 +0000354
355 OptimizingCompilerStats* compilation_stats_;
356 Handle<mirror::DexCache> dex_cache_;
357
358 ArenaVector<HBasicBlock*> loop_headers_;
359
360 static constexpr int kDefaultNumberOfLoops = 2;
361
362 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
363};
364
365} // namespace art
366
367#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_