blob: 3fde54cffa27c04e157427a6939bba18089ec82e [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 Hodson18259d72018-04-12 11:18:23 +0100243 // Builds a `HLoadMethodType` loading the given `proto_index`.
244 void BuildLoadMethodType(uint16_t proto_idx, uint32_t dex_pc);
245
David Brazdildee58d62016-04-07 09:54:26 +0000246 // Returns the outer-most compiling method's class.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000247 ObjPtr<mirror::Class> GetOutermostCompilingClass() const;
David Brazdildee58d62016-04-07 09:54:26 +0000248
249 // Returns the class whose method is being compiled.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000250 ObjPtr<mirror::Class> GetCompilingClass() const;
David Brazdildee58d62016-04-07 09:54:26 +0000251
252 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800253 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000254
255 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
256 uint32_t dex_pc,
257 HInvoke* invoke);
258
259 bool SetupInvokeArguments(HInvoke* invoke,
260 uint32_t number_of_vreg_arguments,
261 uint32_t* args,
262 uint32_t register_index,
263 bool is_range,
264 const char* descriptor,
265 size_t start_index,
266 size_t* argument_index);
267
268 bool HandleInvoke(HInvoke* invoke,
269 uint32_t number_of_vreg_arguments,
270 uint32_t* args,
271 uint32_t register_index,
272 bool is_range,
273 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700274 HClinitCheck* clinit_check,
275 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000276
277 bool HandleStringInit(HInvoke* invoke,
278 uint32_t number_of_vreg_arguments,
279 uint32_t* args,
280 uint32_t register_index,
281 bool is_range,
282 const char* descriptor);
283 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
284
285 HClinitCheck* ProcessClinitCheckForInvoke(
286 uint32_t dex_pc,
287 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000288 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700289 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000290
291 // Build a HNewInstance instruction.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700292 HNewInstance* BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
293
294 // Build a HConstructorFence for HNewInstance and HNewArray instructions. This ensures the
295 // happens-before ordering for default-initialization of the object referred to by new_instance.
296 void BuildConstructorFenceForAllocation(HInstruction* allocation);
David Brazdildee58d62016-04-07 09:54:26 +0000297
298 // Return whether the compiler can assume `cls` is initialized.
299 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700300 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000301
302 // Try to resolve a method using the class linker. Return null if a method could
303 // not be resolved.
304 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
305
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000306 // Try to resolve a field using the class linker. Return null if it could not
307 // be found.
308 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
309
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000310 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
311 const DexCompilationUnit& compilation_unit) const
312 REQUIRES_SHARED(Locks::mutator_lock_);
313
314 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
315
Vladimir Markoca6fff82017-10-03 14:49:14 +0100316 ArenaAllocator* const allocator_;
David Brazdildee58d62016-04-07 09:54:26 +0000317 HGraph* const graph_;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100318 VariableSizedHandleScope* const handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000319
320 // The dex file where the method being compiled is, and the bytecode data.
321 const DexFile* const dex_file_;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800322 const CodeItemDebugInfoAccessor code_item_accessor_; // null for intrinsic graph.
David Brazdildee58d62016-04-07 09:54:26 +0000323
324 // The return type of the method being compiled.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100325 const DataType::Type return_type_;
David Brazdildee58d62016-04-07 09:54:26 +0000326
Vladimir Marko69d310e2017-10-09 14:12:23 +0100327 HBasicBlockBuilder* const block_builder_;
328 SsaBuilder* const ssa_builder_;
David Brazdildee58d62016-04-07 09:54:26 +0000329
330 CompilerDriver* const compiler_driver_;
331
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000332 CodeGenerator* const code_generator_;
333
David Brazdildee58d62016-04-07 09:54:26 +0000334 // The compilation unit of the current method being compiled. Note that
335 // it can be an inlined method.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100336 const DexCompilationUnit* const dex_compilation_unit_;
David Brazdildee58d62016-04-07 09:54:26 +0000337
338 // The compilation unit of the outermost method being compiled. That is the
339 // method being compiled (and not inlined), and potentially inlining other
340 // methods.
341 const DexCompilationUnit* const outer_compilation_unit_;
342
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700343 // Original values kept after instruction quickening.
344 QuickenInfoTable quicken_info_;
David Brazdildee58d62016-04-07 09:54:26 +0000345
Vladimir Marko69d310e2017-10-09 14:12:23 +0100346 OptimizingCompilerStats* const compilation_stats_;
David Brazdildee58d62016-04-07 09:54:26 +0000347
Vladimir Marko69d310e2017-10-09 14:12:23 +0100348 ScopedArenaAllocator* const local_allocator_;
349 ScopedArenaVector<ScopedArenaVector<HInstruction*>> locals_for_;
350 HBasicBlock* current_block_;
351 ScopedArenaVector<HInstruction*>* current_locals_;
352 HInstruction* latest_result_;
353 // Current "this" parameter.
354 // Valid only after InitializeParameters() finishes.
355 // * Null for static methods.
356 // * Non-null for instance methods.
357 HParameterValue* current_this_parameter_;
358
359 ScopedArenaVector<HBasicBlock*> loop_headers_;
David Brazdildee58d62016-04-07 09:54:26 +0000360
361 static constexpr int kDefaultNumberOfLoops = 2;
362
363 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
364};
365
366} // namespace art
367
368#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_