blob: f78829232d48deec00a37843a6e465f5d7a712fa [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;
48} // namespace mirror
Andreas Gampe26de38b2016-07-27 17:53:11 -070049
David Brazdildee58d62016-04-07 09:54:26 +000050class HInstructionBuilder : public ValueObject {
51 public:
52 HInstructionBuilder(HGraph* graph,
53 HBasicBlockBuilder* block_builder,
54 SsaBuilder* ssa_builder,
55 const DexFile* dex_file,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080056 const CodeItemDebugInfoAccessor& accessor,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010057 DataType::Type return_type,
Vladimir Markoca6fff82017-10-03 14:49:14 +010058 const DexCompilationUnit* dex_compilation_unit,
59 const DexCompilationUnit* outer_compilation_unit,
Vladimir Marko69d310e2017-10-09 14:12:23 +010060 CompilerDriver* compiler_driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000061 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080062 ArrayRef<const uint8_t> interpreter_metadata,
David Brazdildee58d62016-04-07 09:54:26 +000063 OptimizingCompilerStats* compiler_stats,
Vladimir Marko69d310e2017-10-09 14:12:23 +010064 VariableSizedHandleScope* handles,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080065 ScopedArenaAllocator* local_allocator);
David Brazdildee58d62016-04-07 09:54:26 +000066
67 bool Build();
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000068 void BuildIntrinsic(ArtMethod* method);
David Brazdildee58d62016-04-07 09:54:26 +000069
70 private:
David Brazdildee58d62016-04-07 09:54:26 +000071 void InitializeBlockLocals();
72 void PropagateLocalsToCatchBlocks();
73 void SetLoopHeaderPhiInputs();
74
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070075 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc, size_t quicken_index);
Vladimir Marko69d310e2017-10-09 14:12:23 +010076 ArenaBitVector* FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +000077
78 bool CanDecodeQuickenedInfo() const;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070079 uint16_t LookupQuickenedInfo(uint32_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +000080
81 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
82
Vladimir Marko69d310e2017-10-09 14:12:23 +010083 ScopedArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
Mingyao Yang01b47b02017-02-03 12:09:57 -080084 // Out of line version of GetLocalsFor(), which has a fast path that is
85 // beneficial to get inlined by callers.
Vladimir Marko69d310e2017-10-09 14:12:23 +010086 ScopedArenaVector<HInstruction*>* GetLocalsForWithAllocation(
87 HBasicBlock* block, ScopedArenaVector<HInstruction*>* locals, const size_t vregs);
David Brazdildee58d62016-04-07 09:54:26 +000088 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010089 HInstruction* LoadLocal(uint32_t register_index, DataType::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010090 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000091 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
92
93 void AppendInstruction(HInstruction* instruction);
94 void InsertInstructionAtTop(HInstruction* instruction);
95 void InitializeInstruction(HInstruction* instruction);
96
97 void InitializeParameters();
98
99 // Returns whether the current method needs access check for the type.
100 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000101 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000103
104 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100105 void Unop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000106
107 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108 void Binop_23x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000109
110 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100111 void Binop_23x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000112
113 void Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100114 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000115 ComparisonBias bias,
116 uint32_t dex_pc);
117
118 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100119 void Binop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000120
121 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100122 void Binop_12x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000123
124 template<typename T>
125 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
126
127 template<typename T>
128 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
129
130 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
131 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
132
133 void Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134 DataType::Type input_type,
135 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000136 uint32_t dex_pc);
137
138 void BuildCheckedDivRem(uint16_t out_reg,
139 uint16_t first_reg,
140 int64_t second_reg_or_constant,
141 uint32_t dex_pc,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000143 bool second_is_lit,
144 bool is_div);
145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100146 void BuildReturn(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000147
148 // Builds an instance field access node and returns whether the instruction is supported.
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700149 bool BuildInstanceFieldAccess(const Instruction& instruction,
150 uint32_t dex_pc,
151 bool is_put,
152 size_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +0000153
154 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
155 uint32_t dex_pc,
156 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 DataType::Type field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000158 // Builds a static field access node.
159 void BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
David Brazdildee58d62016-04-07 09:54:26 +0000160
161 void BuildArrayAccess(const Instruction& instruction,
162 uint32_t dex_pc,
163 bool is_get,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100164 DataType::Type anticipated_type);
David Brazdildee58d62016-04-07 09:54:26 +0000165
166 // Builds an invocation node and returns whether the instruction is supported.
167 bool BuildInvoke(const Instruction& instruction,
168 uint32_t dex_pc,
169 uint32_t method_idx,
170 uint32_t number_of_vreg_arguments,
171 bool is_range,
172 uint32_t* args,
173 uint32_t register_index);
174
Orion Hodsonac141392017-01-13 11:53:47 +0000175 // Builds an invocation node for invoke-polymorphic and returns whether the
176 // instruction is supported.
177 bool BuildInvokePolymorphic(const Instruction& instruction,
178 uint32_t dex_pc,
179 uint32_t method_idx,
180 uint32_t proto_idx,
181 uint32_t number_of_vreg_arguments,
182 bool is_range,
183 uint32_t* args,
184 uint32_t register_index);
185
David Brazdildee58d62016-04-07 09:54:26 +0000186 // Builds a new array node and the instructions that fill it.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700187 HNewArray* BuildFilledNewArray(uint32_t dex_pc,
188 dex::TypeIndex type_index,
189 uint32_t number_of_vreg_arguments,
190 bool is_range,
191 uint32_t* args,
192 uint32_t register_index);
David Brazdildee58d62016-04-07 09:54:26 +0000193
194 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
195
196 // Fills the given object with data as specified in the fill-array-data
197 // instruction. Currently only used for non-reference and non-floating point
198 // arrays.
199 template <typename T>
200 void BuildFillArrayData(HInstruction* object,
201 const T* data,
202 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100203 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +0000204 uint32_t dex_pc);
205
206 // Fills the given object with data as specified in the fill-array-data
207 // instruction. The data must be for long and double arrays.
208 void BuildFillWideArrayData(HInstruction* object,
209 const int64_t* data,
210 uint32_t element_count,
211 uint32_t dex_pc);
212
213 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
214 void BuildTypeCheck(const Instruction& instruction,
215 uint8_t destination,
216 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800217 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000218 uint32_t dex_pc);
219
220 // Builds an instruction sequence for a switch statement.
221 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
222
Vladimir Marko28e012a2017-12-07 11:22:59 +0000223 // Builds a `HLoadString` loading the given `string_index`.
224 void BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc);
225
226 // Builds a `HLoadClass` loading the given `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000227 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
228
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000229 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000230 const DexFile& dex_file,
231 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000232 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000233 bool needs_access_check)
234 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000235
Vladimir Marko175e7862018-03-27 09:03:13 +0000236 Handle<mirror::Class> ResolveClass(ScopedObjectAccess& soa, dex::TypeIndex type_index)
237 REQUIRES_SHARED(Locks::mutator_lock_);
238
239 bool LoadClassNeedsAccessCheck(Handle<mirror::Class> klass)
240 REQUIRES_SHARED(Locks::mutator_lock_);
241
David Brazdildee58d62016-04-07 09:54:26 +0000242 // Returns the outer-most compiling method's class.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000243 ObjPtr<mirror::Class> GetOutermostCompilingClass() const;
David Brazdildee58d62016-04-07 09:54:26 +0000244
245 // Returns the class whose method is being compiled.
Vladimir Marko28e012a2017-12-07 11:22:59 +0000246 ObjPtr<mirror::Class> GetCompilingClass() const;
David Brazdildee58d62016-04-07 09:54:26 +0000247
248 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800249 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000250
251 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
252 uint32_t dex_pc,
253 HInvoke* invoke);
254
255 bool SetupInvokeArguments(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,
261 size_t start_index,
262 size_t* argument_index);
263
264 bool HandleInvoke(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,
Aart Bik296fbb42016-06-07 13:49:12 -0700270 HClinitCheck* clinit_check,
271 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000272
273 bool HandleStringInit(HInvoke* invoke,
274 uint32_t number_of_vreg_arguments,
275 uint32_t* args,
276 uint32_t register_index,
277 bool is_range,
278 const char* descriptor);
279 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
280
281 HClinitCheck* ProcessClinitCheckForInvoke(
282 uint32_t dex_pc,
283 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000284 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000286
287 // Build a HNewInstance instruction.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700288 HNewInstance* BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
289
290 // Build a HConstructorFence for HNewInstance and HNewArray instructions. This ensures the
291 // happens-before ordering for default-initialization of the object referred to by new_instance.
292 void BuildConstructorFenceForAllocation(HInstruction* allocation);
David Brazdildee58d62016-04-07 09:54:26 +0000293
294 // Return whether the compiler can assume `cls` is initialized.
295 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700296 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000297
298 // Try to resolve a method using the class linker. Return null if a method could
299 // not be resolved.
300 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
301
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000302 // Try to resolve a field using the class linker. Return null if it could not
303 // be found.
304 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
305
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000306 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
307 const DexCompilationUnit& compilation_unit) const
308 REQUIRES_SHARED(Locks::mutator_lock_);
309
310 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
311
Vladimir Markoca6fff82017-10-03 14:49:14 +0100312 ArenaAllocator* const allocator_;
David Brazdildee58d62016-04-07 09:54:26 +0000313 HGraph* const graph_;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100314 VariableSizedHandleScope* const handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000315
316 // The dex file where the method being compiled is, and the bytecode data.
317 const DexFile* const dex_file_;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800318 const CodeItemDebugInfoAccessor code_item_accessor_; // null for intrinsic graph.
David Brazdildee58d62016-04-07 09:54:26 +0000319
320 // The return type of the method being compiled.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100321 const DataType::Type return_type_;
David Brazdildee58d62016-04-07 09:54:26 +0000322
Vladimir Marko69d310e2017-10-09 14:12:23 +0100323 HBasicBlockBuilder* const block_builder_;
324 SsaBuilder* const ssa_builder_;
David Brazdildee58d62016-04-07 09:54:26 +0000325
326 CompilerDriver* const compiler_driver_;
327
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000328 CodeGenerator* const code_generator_;
329
David Brazdildee58d62016-04-07 09:54:26 +0000330 // The compilation unit of the current method being compiled. Note that
331 // it can be an inlined method.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100332 const DexCompilationUnit* const dex_compilation_unit_;
David Brazdildee58d62016-04-07 09:54:26 +0000333
334 // The compilation unit of the outermost method being compiled. That is the
335 // method being compiled (and not inlined), and potentially inlining other
336 // methods.
337 const DexCompilationUnit* const outer_compilation_unit_;
338
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700339 // Original values kept after instruction quickening.
340 QuickenInfoTable quicken_info_;
David Brazdildee58d62016-04-07 09:54:26 +0000341
Vladimir Marko69d310e2017-10-09 14:12:23 +0100342 OptimizingCompilerStats* const compilation_stats_;
David Brazdildee58d62016-04-07 09:54:26 +0000343
Vladimir Marko69d310e2017-10-09 14:12:23 +0100344 ScopedArenaAllocator* const local_allocator_;
345 ScopedArenaVector<ScopedArenaVector<HInstruction*>> locals_for_;
346 HBasicBlock* current_block_;
347 ScopedArenaVector<HInstruction*>* current_locals_;
348 HInstruction* latest_result_;
349 // Current "this" parameter.
350 // Valid only after InitializeParameters() finishes.
351 // * Null for static methods.
352 // * Non-null for instance methods.
353 HParameterValue* current_this_parameter_;
354
355 ScopedArenaVector<HBasicBlock*> loop_headers_;
David Brazdildee58d62016-04-07 09:54:26 +0000356
357 static constexpr int kDefaultNumberOfLoops = 2;
358
359 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
360};
361
362} // namespace art
363
364#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_