blob: 95ffa6b0547ca096ba514bebd459c63fae72e110 [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
Mathieu Chartier210531f2018-01-12 10:15:51 -080020#include "base/array_ref.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010021#include "base/scoped_arena_allocator.h"
22#include "base/scoped_arena_containers.h"
23#include "data_type.h"
David Sehr9e734c72018-01-04 17:56:19 -080024#include "dex/code_item_accessors.h"
25#include "dex/dex_file.h"
26#include "dex/dex_file_types.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010027#include "handle.h"
David Brazdildee58d62016-04-07 09:54:26 +000028#include "nodes.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070029#include "quicken_info.h"
David Brazdildee58d62016-04-07 09:54:26 +000030
31namespace art {
32
Vladimir Marko69d310e2017-10-09 14:12:23 +010033class ArenaBitVector;
34class ArtField;
35class ArtMethod;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000036class CodeGenerator;
Vladimir Marko69d310e2017-10-09 14:12:23 +010037class CompilerDriver;
38class DexCompilationUnit;
39class HBasicBlockBuilder;
Andreas Gampe26de38b2016-07-27 17:53:11 -070040class Instruction;
Vladimir Marko69d310e2017-10-09 14:12:23 +010041class OptimizingCompilerStats;
Vladimir Marko175e7862018-03-27 09:03:13 +000042class ScopedObjectAccess;
Vladimir Marko69d310e2017-10-09 14:12:23 +010043class SsaBuilder;
44class VariableSizedHandleScope;
45
46namespace mirror {
47class Class;
Orion Hodson18259d72018-04-12 11:18:23 +010048class MethodType;
Vladimir Marko69d310e2017-10-09 14:12:23 +010049} // namespace mirror
Andreas Gampe26de38b2016-07-27 17:53:11 -070050
David Brazdildee58d62016-04-07 09:54:26 +000051class HInstructionBuilder : public ValueObject {
52 public:
53 HInstructionBuilder(HGraph* graph,
54 HBasicBlockBuilder* block_builder,
55 SsaBuilder* ssa_builder,
56 const DexFile* dex_file,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080057 const CodeItemDebugInfoAccessor& accessor,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 DataType::Type return_type,
Vladimir Markoca6fff82017-10-03 14:49:14 +010059 const DexCompilationUnit* dex_compilation_unit,
60 const DexCompilationUnit* outer_compilation_unit,
Vladimir Marko69d310e2017-10-09 14:12:23 +010061 CompilerDriver* compiler_driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000062 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080063 ArrayRef<const uint8_t> interpreter_metadata,
David Brazdildee58d62016-04-07 09:54:26 +000064 OptimizingCompilerStats* compiler_stats,
Vladimir Marko69d310e2017-10-09 14:12:23 +010065 VariableSizedHandleScope* handles,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080066 ScopedArenaAllocator* local_allocator);
David Brazdildee58d62016-04-07 09:54:26 +000067
68 bool Build();
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000069 void BuildIntrinsic(ArtMethod* method);
David Brazdildee58d62016-04-07 09:54:26 +000070
71 private:
David Brazdildee58d62016-04-07 09:54:26 +000072 void InitializeBlockLocals();
73 void PropagateLocalsToCatchBlocks();
74 void SetLoopHeaderPhiInputs();
75
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070076 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc, size_t quicken_index);
Vladimir Marko69d310e2017-10-09 14:12:23 +010077 ArenaBitVector* FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +000078
79 bool CanDecodeQuickenedInfo() const;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070080 uint16_t LookupQuickenedInfo(uint32_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +000081
82 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
83
Vladimir Marko69d310e2017-10-09 14:12:23 +010084 ScopedArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
Mingyao Yang01b47b02017-02-03 12:09:57 -080085 // Out of line version of GetLocalsFor(), which has a fast path that is
86 // beneficial to get inlined by callers.
Vladimir Marko69d310e2017-10-09 14:12:23 +010087 ScopedArenaVector<HInstruction*>* GetLocalsForWithAllocation(
88 HBasicBlock* block, ScopedArenaVector<HInstruction*>* locals, const size_t vregs);
David Brazdildee58d62016-04-07 09:54:26 +000089 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 HInstruction* LoadLocal(uint32_t register_index, DataType::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010091 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000092 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
93
94 void AppendInstruction(HInstruction* instruction);
95 void InsertInstructionAtTop(HInstruction* instruction);
96 void InitializeInstruction(HInstruction* instruction);
97
98 void InitializeParameters();
99
100 // Returns whether the current method needs access check for the type.
101 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000102 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000104
105 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 void Unop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000107
108 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100109 void Binop_23x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000110
111 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100112 void Binop_23x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000113
114 void Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100115 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000116 ComparisonBias bias,
117 uint32_t dex_pc);
118
119 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100120 void Binop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000121
122 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100123 void Binop_12x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000124
125 template<typename T>
126 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
127
128 template<typename T>
129 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
130
131 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
132 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
133
134 void Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100135 DataType::Type input_type,
136 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000137 uint32_t dex_pc);
138
139 void BuildCheckedDivRem(uint16_t out_reg,
140 uint16_t first_reg,
141 int64_t second_reg_or_constant,
142 uint32_t dex_pc,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000144 bool second_is_lit,
145 bool is_div);
146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 void BuildReturn(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000148
149 // Builds an instance field access node and returns whether the instruction is supported.
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700150 bool BuildInstanceFieldAccess(const Instruction& instruction,
151 uint32_t dex_pc,
152 bool is_put,
153 size_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +0000154
155 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
156 uint32_t dex_pc,
157 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158 DataType::Type field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000159 // Builds a static field access node.
160 void BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
David Brazdildee58d62016-04-07 09:54:26 +0000161
162 void BuildArrayAccess(const Instruction& instruction,
163 uint32_t dex_pc,
164 bool is_get,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100165 DataType::Type anticipated_type);
David Brazdildee58d62016-04-07 09:54:26 +0000166
167 // Builds an invocation node and returns whether the instruction is supported.
168 bool BuildInvoke(const Instruction& instruction,
169 uint32_t dex_pc,
170 uint32_t method_idx,
171 uint32_t number_of_vreg_arguments,
172 bool is_range,
173 uint32_t* args,
174 uint32_t register_index);
175
Orion Hodsonac141392017-01-13 11:53:47 +0000176 // Builds an invocation node for invoke-polymorphic and returns whether the
177 // instruction is supported.
178 bool BuildInvokePolymorphic(const Instruction& instruction,
179 uint32_t dex_pc,
180 uint32_t method_idx,
181 uint32_t proto_idx,
182 uint32_t number_of_vreg_arguments,
183 bool is_range,
184 uint32_t* args,
185 uint32_t register_index);
186
David Brazdildee58d62016-04-07 09:54:26 +0000187 // Builds a new array node and the instructions that fill it.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700188 HNewArray* BuildFilledNewArray(uint32_t dex_pc,
189 dex::TypeIndex type_index,
190 uint32_t number_of_vreg_arguments,
191 bool is_range,
192 uint32_t* args,
193 uint32_t register_index);
David Brazdildee58d62016-04-07 09:54:26 +0000194
195 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
196
197 // Fills the given object with data as specified in the fill-array-data
198 // instruction. Currently only used for non-reference and non-floating point
199 // arrays.
200 template <typename T>
201 void BuildFillArrayData(HInstruction* object,
202 const T* data,
203 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100204 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +0000205 uint32_t dex_pc);
206
207 // Fills the given object with data as specified in the fill-array-data
208 // instruction. The data must be for long and double arrays.
209 void BuildFillWideArrayData(HInstruction* object,
210 const int64_t* data,
211 uint32_t element_count,
212 uint32_t dex_pc);
213
214 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
215 void BuildTypeCheck(const Instruction& instruction,
216 uint8_t destination,
217 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800218 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000219 uint32_t dex_pc);
220
221 // Builds an instruction sequence for a switch statement.
222 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
223
Vladimir Marko28e012a2017-12-07 11:22:59 +0000224 // Builds a `HLoadString` loading the given `string_index`.
225 void BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc);
226
227 // Builds a `HLoadClass` loading the given `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000228 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
229
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000230 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000231 const DexFile& dex_file,
232 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000233 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000234 bool needs_access_check)
235 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000236
Vladimir Marko175e7862018-03-27 09:03:13 +0000237 Handle<mirror::Class> ResolveClass(ScopedObjectAccess& soa, dex::TypeIndex type_index)
238 REQUIRES_SHARED(Locks::mutator_lock_);
239
240 bool LoadClassNeedsAccessCheck(Handle<mirror::Class> klass)
241 REQUIRES_SHARED(Locks::mutator_lock_);
242
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100243 // Builds a `HLoadMethodHandle` loading the given `method_handle_idx`.
244 void BuildLoadMethodHandle(uint16_t method_handle_idx, uint32_t dex_pc);
245
246 // Builds a `HLoadMethodType` loading the given `proto_idx`.
Orion Hodson18259d72018-04-12 11:18:23 +0100247 void BuildLoadMethodType(uint16_t proto_idx, uint32_t dex_pc);
248
David Brazdildee58d62016-04-07 09:54:26 +0000249 // Returns the outer-most compiling method's class.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000250 ObjPtr<mirror::Class> GetOutermostCompilingClass() const;
David Brazdildee58d62016-04-07 09:54:26 +0000251
252 // Returns the class whose method is being compiled.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000253 ObjPtr<mirror::Class> GetCompilingClass() const;
David Brazdildee58d62016-04-07 09:54:26 +0000254
255 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800256 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000257
258 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
259 uint32_t dex_pc,
260 HInvoke* invoke);
261
262 bool SetupInvokeArguments(HInvoke* invoke,
263 uint32_t number_of_vreg_arguments,
264 uint32_t* args,
265 uint32_t register_index,
266 bool is_range,
267 const char* descriptor,
268 size_t start_index,
269 size_t* argument_index);
270
271 bool HandleInvoke(HInvoke* invoke,
272 uint32_t number_of_vreg_arguments,
273 uint32_t* args,
274 uint32_t register_index,
275 bool is_range,
276 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700277 HClinitCheck* clinit_check,
278 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000279
280 bool HandleStringInit(HInvoke* invoke,
281 uint32_t number_of_vreg_arguments,
282 uint32_t* args,
283 uint32_t register_index,
284 bool is_range,
285 const char* descriptor);
286 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
287
288 HClinitCheck* ProcessClinitCheckForInvoke(
289 uint32_t dex_pc,
290 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000291 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700292 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000293
294 // Build a HNewInstance instruction.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700295 HNewInstance* BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
296
297 // Build a HConstructorFence for HNewInstance and HNewArray instructions. This ensures the
298 // happens-before ordering for default-initialization of the object referred to by new_instance.
299 void BuildConstructorFenceForAllocation(HInstruction* allocation);
David Brazdildee58d62016-04-07 09:54:26 +0000300
301 // Return whether the compiler can assume `cls` is initialized.
302 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000304
305 // Try to resolve a method using the class linker. Return null if a method could
306 // not be resolved.
307 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
308
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000309 // Try to resolve a field using the class linker. Return null if it could not
310 // be found.
311 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
312
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000313 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
314 const DexCompilationUnit& compilation_unit) const
315 REQUIRES_SHARED(Locks::mutator_lock_);
316
317 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
318
Vladimir Markoca6fff82017-10-03 14:49:14 +0100319 ArenaAllocator* const allocator_;
David Brazdildee58d62016-04-07 09:54:26 +0000320 HGraph* const graph_;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100321 VariableSizedHandleScope* const handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000322
323 // The dex file where the method being compiled is, and the bytecode data.
324 const DexFile* const dex_file_;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800325 const CodeItemDebugInfoAccessor code_item_accessor_; // null for intrinsic graph.
David Brazdildee58d62016-04-07 09:54:26 +0000326
327 // The return type of the method being compiled.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100328 const DataType::Type return_type_;
David Brazdildee58d62016-04-07 09:54:26 +0000329
Vladimir Marko69d310e2017-10-09 14:12:23 +0100330 HBasicBlockBuilder* const block_builder_;
331 SsaBuilder* const ssa_builder_;
David Brazdildee58d62016-04-07 09:54:26 +0000332
333 CompilerDriver* const compiler_driver_;
334
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000335 CodeGenerator* const code_generator_;
336
David Brazdildee58d62016-04-07 09:54:26 +0000337 // The compilation unit of the current method being compiled. Note that
338 // it can be an inlined method.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100339 const DexCompilationUnit* const dex_compilation_unit_;
David Brazdildee58d62016-04-07 09:54:26 +0000340
341 // The compilation unit of the outermost method being compiled. That is the
342 // method being compiled (and not inlined), and potentially inlining other
343 // methods.
344 const DexCompilationUnit* const outer_compilation_unit_;
345
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700346 // Original values kept after instruction quickening.
347 QuickenInfoTable quicken_info_;
David Brazdildee58d62016-04-07 09:54:26 +0000348
Vladimir Marko69d310e2017-10-09 14:12:23 +0100349 OptimizingCompilerStats* const compilation_stats_;
David Brazdildee58d62016-04-07 09:54:26 +0000350
Vladimir Marko69d310e2017-10-09 14:12:23 +0100351 ScopedArenaAllocator* const local_allocator_;
352 ScopedArenaVector<ScopedArenaVector<HInstruction*>> locals_for_;
353 HBasicBlock* current_block_;
354 ScopedArenaVector<HInstruction*>* current_locals_;
355 HInstruction* latest_result_;
356 // Current "this" parameter.
357 // Valid only after InitializeParameters() finishes.
358 // * Null for static methods.
359 // * Non-null for instance methods.
360 HParameterValue* current_this_parameter_;
361
362 ScopedArenaVector<HBasicBlock*> loop_headers_;
David Brazdildee58d62016-04-07 09:54:26 +0000363
364 static constexpr int kDefaultNumberOfLoops = 2;
365
366 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
367};
368
369} // namespace art
370
371#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_