blob: af7092a0cfdd69ea428da310bfeb6e7f3ed3fad2 [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;
Treehugger Robot2c5827a2018-05-17 22:26:08 +000041class InstructionOperands;
Vladimir Marko69d310e2017-10-09 14:12:23 +010042class OptimizingCompilerStats;
Vladimir Marko175e7862018-03-27 09:03:13 +000043class ScopedObjectAccess;
Vladimir Marko69d310e2017-10-09 14:12:23 +010044class SsaBuilder;
45class VariableSizedHandleScope;
46
47namespace mirror {
48class Class;
Orion Hodson18259d72018-04-12 11:18:23 +010049class MethodType;
Vladimir Marko69d310e2017-10-09 14:12:23 +010050} // namespace mirror
Andreas Gampe26de38b2016-07-27 17:53:11 -070051
David Brazdildee58d62016-04-07 09:54:26 +000052class HInstructionBuilder : public ValueObject {
53 public:
54 HInstructionBuilder(HGraph* graph,
55 HBasicBlockBuilder* block_builder,
56 SsaBuilder* ssa_builder,
57 const DexFile* dex_file,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080058 const CodeItemDebugInfoAccessor& accessor,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059 DataType::Type return_type,
Vladimir Markoca6fff82017-10-03 14:49:14 +010060 const DexCompilationUnit* dex_compilation_unit,
61 const DexCompilationUnit* outer_compilation_unit,
Vladimir Marko69d310e2017-10-09 14:12:23 +010062 CompilerDriver* compiler_driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000063 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080064 ArrayRef<const uint8_t> interpreter_metadata,
David Brazdildee58d62016-04-07 09:54:26 +000065 OptimizingCompilerStats* compiler_stats,
Vladimir Marko69d310e2017-10-09 14:12:23 +010066 VariableSizedHandleScope* handles,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080067 ScopedArenaAllocator* local_allocator);
David Brazdildee58d62016-04-07 09:54:26 +000068
69 bool Build();
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000070 void BuildIntrinsic(ArtMethod* method);
David Brazdildee58d62016-04-07 09:54:26 +000071
72 private:
David Brazdildee58d62016-04-07 09:54:26 +000073 void InitializeBlockLocals();
74 void PropagateLocalsToCatchBlocks();
75 void SetLoopHeaderPhiInputs();
76
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070077 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc, size_t quicken_index);
Vladimir Marko69d310e2017-10-09 14:12:23 +010078 ArenaBitVector* FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +000079
80 bool CanDecodeQuickenedInfo() const;
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070081 uint16_t LookupQuickenedInfo(uint32_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +000082
83 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
84
Vladimir Marko69d310e2017-10-09 14:12:23 +010085 ScopedArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
Mingyao Yang01b47b02017-02-03 12:09:57 -080086 // Out of line version of GetLocalsFor(), which has a fast path that is
87 // beneficial to get inlined by callers.
Vladimir Marko69d310e2017-10-09 14:12:23 +010088 ScopedArenaVector<HInstruction*>* GetLocalsForWithAllocation(
89 HBasicBlock* block, ScopedArenaVector<HInstruction*>* locals, const size_t vregs);
David Brazdildee58d62016-04-07 09:54:26 +000090 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010091 HInstruction* LoadLocal(uint32_t register_index, DataType::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010092 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000093 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
94
95 void AppendInstruction(HInstruction* instruction);
96 void InsertInstructionAtTop(HInstruction* instruction);
97 void InitializeInstruction(HInstruction* instruction);
98
99 void InitializeParameters();
100
101 // Returns whether the current method needs access check for the type.
102 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000103 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000105
106 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100107 void Unop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000108
109 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100110 void Binop_23x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000111
112 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100113 void Binop_23x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000114
115 void Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100116 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000117 ComparisonBias bias,
118 uint32_t dex_pc);
119
120 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100121 void Binop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000122
123 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100124 void Binop_12x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000125
126 template<typename T>
127 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
128
129 template<typename T>
130 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
131
132 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
133 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
134
135 void Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type input_type,
137 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000138 uint32_t dex_pc);
139
140 void BuildCheckedDivRem(uint16_t out_reg,
141 uint16_t first_reg,
142 int64_t second_reg_or_constant,
143 uint32_t dex_pc,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100144 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000145 bool second_is_lit,
146 bool is_div);
147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 void BuildReturn(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000149
150 // Builds an instance field access node and returns whether the instruction is supported.
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700151 bool BuildInstanceFieldAccess(const Instruction& instruction,
152 uint32_t dex_pc,
153 bool is_put,
154 size_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +0000155
156 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
157 uint32_t dex_pc,
158 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159 DataType::Type field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000160 // Builds a static field access node.
161 void BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
David Brazdildee58d62016-04-07 09:54:26 +0000162
163 void BuildArrayAccess(const Instruction& instruction,
164 uint32_t dex_pc,
165 bool is_get,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100166 DataType::Type anticipated_type);
David Brazdildee58d62016-04-07 09:54:26 +0000167
168 // Builds an invocation node and returns whether the instruction is supported.
169 bool BuildInvoke(const Instruction& instruction,
170 uint32_t dex_pc,
171 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000172 const InstructionOperands& operands);
David Brazdildee58d62016-04-07 09:54:26 +0000173
Orion Hodsonac141392017-01-13 11:53:47 +0000174 // Builds an invocation node for invoke-polymorphic and returns whether the
175 // instruction is supported.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100176 bool BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +0000177 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +0100178 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000179 const InstructionOperands& operands);
Orion Hodsonac141392017-01-13 11:53:47 +0000180
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100181 // Builds an invocation node for invoke-custom and returns whether the
182 // instruction is supported.
183 bool BuildInvokeCustom(uint32_t dex_pc,
184 uint32_t call_site_idx,
185 const InstructionOperands& operands);
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,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000190 const InstructionOperands& operands);
David Brazdildee58d62016-04-07 09:54:26 +0000191
192 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
193
194 // Fills the given object with data as specified in the fill-array-data
195 // instruction. Currently only used for non-reference and non-floating point
196 // arrays.
197 template <typename T>
198 void BuildFillArrayData(HInstruction* object,
199 const T* data,
200 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100201 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +0000202 uint32_t dex_pc);
203
204 // Fills the given object with data as specified in the fill-array-data
205 // instruction. The data must be for long and double arrays.
206 void BuildFillWideArrayData(HInstruction* object,
207 const int64_t* data,
208 uint32_t element_count,
209 uint32_t dex_pc);
210
211 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
212 void BuildTypeCheck(const Instruction& instruction,
213 uint8_t destination,
214 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800215 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000216 uint32_t dex_pc);
217
218 // Builds an instruction sequence for a switch statement.
219 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
220
Vladimir Marko28e012a2017-12-07 11:22:59 +0000221 // Builds a `HLoadString` loading the given `string_index`.
222 void BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc);
223
224 // Builds a `HLoadClass` loading the given `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000225 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
226
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000227 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000228 const DexFile& dex_file,
229 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000230 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000231 bool needs_access_check)
232 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000233
Vladimir Marko175e7862018-03-27 09:03:13 +0000234 Handle<mirror::Class> ResolveClass(ScopedObjectAccess& soa, dex::TypeIndex type_index)
235 REQUIRES_SHARED(Locks::mutator_lock_);
236
237 bool LoadClassNeedsAccessCheck(Handle<mirror::Class> klass)
238 REQUIRES_SHARED(Locks::mutator_lock_);
239
Orion Hodson06d10a72018-05-14 08:53:38 +0100240 // Builds a `HLoadMethodHandle` loading the given `method_handle_index`.
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100241 void BuildLoadMethodHandle(uint16_t method_handle_idx, uint32_t dex_pc);
242
Orion Hodson06d10a72018-05-14 08:53:38 +0100243 // Builds a `HLoadMethodType` loading the given `proto_index`.
244 void BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +0100245
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,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000260 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100261 const char* shorty,
David Brazdildee58d62016-04-07 09:54:26 +0000262 size_t start_index,
263 size_t* argument_index);
264
265 bool HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000266 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100267 const char* shorty,
268 bool is_unresolved,
269 HClinitCheck* clinit_check = nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000270
271 bool HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000272 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100273 const char* shorty);
David Brazdildee58d62016-04-07 09:54:26 +0000274 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
275
276 HClinitCheck* ProcessClinitCheckForInvoke(
277 uint32_t dex_pc,
278 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000279 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000281
282 // Build a HNewInstance instruction.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700283 HNewInstance* BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
284
285 // Build a HConstructorFence for HNewInstance and HNewArray instructions. This ensures the
286 // happens-before ordering for default-initialization of the object referred to by new_instance.
287 void BuildConstructorFenceForAllocation(HInstruction* allocation);
David Brazdildee58d62016-04-07 09:54:26 +0000288
289 // Return whether the compiler can assume `cls` is initialized.
290 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700291 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000292
293 // Try to resolve a method using the class linker. Return null if a method could
294 // not be resolved.
295 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
296
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000297 // Try to resolve a field using the class linker. Return null if it could not
298 // be found.
299 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
300
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000301 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
302 const DexCompilationUnit& compilation_unit) const
303 REQUIRES_SHARED(Locks::mutator_lock_);
304
305 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
306
Vladimir Markoca6fff82017-10-03 14:49:14 +0100307 ArenaAllocator* const allocator_;
David Brazdildee58d62016-04-07 09:54:26 +0000308 HGraph* const graph_;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100309 VariableSizedHandleScope* const handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000310
311 // The dex file where the method being compiled is, and the bytecode data.
312 const DexFile* const dex_file_;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800313 const CodeItemDebugInfoAccessor code_item_accessor_; // null for intrinsic graph.
David Brazdildee58d62016-04-07 09:54:26 +0000314
315 // The return type of the method being compiled.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100316 const DataType::Type return_type_;
David Brazdildee58d62016-04-07 09:54:26 +0000317
Vladimir Marko69d310e2017-10-09 14:12:23 +0100318 HBasicBlockBuilder* const block_builder_;
319 SsaBuilder* const ssa_builder_;
David Brazdildee58d62016-04-07 09:54:26 +0000320
321 CompilerDriver* const compiler_driver_;
322
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000323 CodeGenerator* const code_generator_;
324
David Brazdildee58d62016-04-07 09:54:26 +0000325 // The compilation unit of the current method being compiled. Note that
326 // it can be an inlined method.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100327 const DexCompilationUnit* const dex_compilation_unit_;
David Brazdildee58d62016-04-07 09:54:26 +0000328
329 // The compilation unit of the outermost method being compiled. That is the
330 // method being compiled (and not inlined), and potentially inlining other
331 // methods.
332 const DexCompilationUnit* const outer_compilation_unit_;
333
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700334 // Original values kept after instruction quickening.
335 QuickenInfoTable quicken_info_;
David Brazdildee58d62016-04-07 09:54:26 +0000336
Vladimir Marko69d310e2017-10-09 14:12:23 +0100337 OptimizingCompilerStats* const compilation_stats_;
David Brazdildee58d62016-04-07 09:54:26 +0000338
Vladimir Marko69d310e2017-10-09 14:12:23 +0100339 ScopedArenaAllocator* const local_allocator_;
340 ScopedArenaVector<ScopedArenaVector<HInstruction*>> locals_for_;
341 HBasicBlock* current_block_;
342 ScopedArenaVector<HInstruction*>* current_locals_;
343 HInstruction* latest_result_;
344 // Current "this" parameter.
345 // Valid only after InitializeParameters() finishes.
346 // * Null for static methods.
347 // * Non-null for instance methods.
348 HParameterValue* current_this_parameter_;
349
350 ScopedArenaVector<HBasicBlock*> loop_headers_;
David Brazdildee58d62016-04-07 09:54:26 +0000351
352 static constexpr int kDefaultNumberOfLoops = 2;
353
354 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
355};
356
357} // namespace art
358
359#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_