blob: be1706a44ded88a32c653968d9464e38f7ccfff2 [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 Markoe2727152019-10-10 10:46:42 +010021#include "base/macros.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010022#include "base/scoped_arena_allocator.h"
23#include "base/scoped_arena_containers.h"
24#include "data_type.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/code_item_accessors.h"
26#include "dex/dex_file.h"
27#include "dex/dex_file_types.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010028#include "handle.h"
David Brazdildee58d62016-04-07 09:54:26 +000029#include "nodes.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070030#include "quicken_info.h"
David Brazdildee58d62016-04-07 09:54:26 +000031
Vladimir Markoe2727152019-10-10 10:46:42 +010032namespace art HIDDEN {
David Brazdildee58d62016-04-07 09:54:26 +000033
Vladimir Marko69d310e2017-10-09 14:12:23 +010034class ArenaBitVector;
35class ArtField;
36class ArtMethod;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000037class CodeGenerator;
Vladimir Marko69d310e2017-10-09 14:12:23 +010038class 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,
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
David Brazdildee58d62016-04-07 09:54:26 +0000100 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100101 void Unop_12x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000102
103 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100104 void Binop_23x(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000105
106 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100107 void Binop_23x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000108
109 void Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100110 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000111 ComparisonBias bias,
112 uint32_t dex_pc);
113
114 template<typename T>
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100115 void Binop_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_12x_shift(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000119
120 template<typename T>
121 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
122
123 template<typename T>
124 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
125
126 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
127 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
128
129 void Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130 DataType::Type input_type,
131 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000132 uint32_t dex_pc);
133
134 void BuildCheckedDivRem(uint16_t out_reg,
135 uint16_t first_reg,
136 int64_t second_reg_or_constant,
137 uint32_t dex_pc,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100138 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000139 bool second_is_lit,
140 bool is_div);
141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100142 void BuildReturn(const Instruction& instruction, DataType::Type type, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000143
144 // Builds an instance field access node and returns whether the instruction is supported.
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700145 bool BuildInstanceFieldAccess(const Instruction& instruction,
146 uint32_t dex_pc,
147 bool is_put,
148 size_t quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +0000149
150 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
151 uint32_t dex_pc,
152 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 DataType::Type field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +0000154 // Builds a static field access node.
155 void BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
David Brazdildee58d62016-04-07 09:54:26 +0000156
157 void BuildArrayAccess(const Instruction& instruction,
158 uint32_t dex_pc,
159 bool is_get,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100160 DataType::Type anticipated_type);
David Brazdildee58d62016-04-07 09:54:26 +0000161
162 // Builds an invocation node and returns whether the instruction is supported.
163 bool BuildInvoke(const Instruction& instruction,
164 uint32_t dex_pc,
165 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000166 const InstructionOperands& operands);
David Brazdildee58d62016-04-07 09:54:26 +0000167
Orion Hodsonac141392017-01-13 11:53:47 +0000168 // Builds an invocation node for invoke-polymorphic and returns whether the
169 // instruction is supported.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100170 bool BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +0000171 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +0100172 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000173 const InstructionOperands& operands);
Orion Hodsonac141392017-01-13 11:53:47 +0000174
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100175 // Builds an invocation node for invoke-custom and returns whether the
176 // instruction is supported.
177 bool BuildInvokeCustom(uint32_t dex_pc,
178 uint32_t call_site_idx,
179 const InstructionOperands& operands);
180
Vladimir Markob5461632018-10-15 14:24:21 +0100181 // Builds a new array node.
182 HNewArray* BuildNewArray(uint32_t dex_pc, dex::TypeIndex type_index, HInstruction* length);
183
David Brazdildee58d62016-04-07 09:54:26 +0000184 // Builds a new array node and the instructions that fill it.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700185 HNewArray* BuildFilledNewArray(uint32_t dex_pc,
186 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000187 const InstructionOperands& operands);
David Brazdildee58d62016-04-07 09:54:26 +0000188
189 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
190
191 // Fills the given object with data as specified in the fill-array-data
192 // instruction. Currently only used for non-reference and non-floating point
193 // arrays.
194 template <typename T>
195 void BuildFillArrayData(HInstruction* object,
196 const T* data,
197 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100198 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +0000199 uint32_t dex_pc);
200
201 // Fills the given object with data as specified in the fill-array-data
202 // instruction. The data must be for long and double arrays.
203 void BuildFillWideArrayData(HInstruction* object,
204 const int64_t* data,
205 uint32_t element_count,
206 uint32_t dex_pc);
207
208 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
209 void BuildTypeCheck(const Instruction& instruction,
210 uint8_t destination,
211 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800212 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000213 uint32_t dex_pc);
214
215 // Builds an instruction sequence for a switch statement.
216 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
217
Vladimir Marko28e012a2017-12-07 11:22:59 +0000218 // Builds a `HLoadString` loading the given `string_index`.
219 void BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc);
220
221 // Builds a `HLoadClass` loading the given `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000222 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
223
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000224 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000225 const DexFile& dex_file,
226 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000227 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000228 bool needs_access_check)
229 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000230
Vladimir Marko175e7862018-03-27 09:03:13 +0000231 Handle<mirror::Class> ResolveClass(ScopedObjectAccess& soa, dex::TypeIndex type_index)
232 REQUIRES_SHARED(Locks::mutator_lock_);
233
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000234 bool LoadClassNeedsAccessCheck(Handle<mirror::Class> klass)
Vladimir Marko175e7862018-03-27 09:03:13 +0000235 REQUIRES_SHARED(Locks::mutator_lock_);
236
Orion Hodson06d10a72018-05-14 08:53:38 +0100237 // Builds a `HLoadMethodHandle` loading the given `method_handle_index`.
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100238 void BuildLoadMethodHandle(uint16_t method_handle_idx, uint32_t dex_pc);
239
Orion Hodson06d10a72018-05-14 08:53:38 +0100240 // Builds a `HLoadMethodType` loading the given `proto_index`.
241 void BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +0100242
David Brazdildee58d62016-04-07 09:54:26 +0000243 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
244 uint32_t dex_pc,
245 HInvoke* invoke);
246
247 bool SetupInvokeArguments(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000248 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100249 const char* shorty,
David Brazdildee58d62016-04-07 09:54:26 +0000250 size_t start_index,
251 size_t* argument_index);
252
253 bool HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000254 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100255 const char* shorty,
256 bool is_unresolved,
257 HClinitCheck* clinit_check = nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000258
259 bool HandleStringInit(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 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
263
264 HClinitCheck* ProcessClinitCheckForInvoke(
265 uint32_t dex_pc,
266 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000267 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000269
270 // Build a HNewInstance instruction.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700271 HNewInstance* BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
272
273 // Build a HConstructorFence for HNewInstance and HNewArray instructions. This ensures the
274 // happens-before ordering for default-initialization of the object referred to by new_instance.
275 void BuildConstructorFenceForAllocation(HInstruction* allocation);
David Brazdildee58d62016-04-07 09:54:26 +0000276
277 // Return whether the compiler can assume `cls` is initialized.
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000278 bool IsInitialized(Handle<mirror::Class> cls) const
Vladimir Markofca0b492018-07-23 15:30:52 +0100279 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000280
281 // Try to resolve a method using the class linker. Return null if a method could
282 // not be resolved.
283 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
284
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000285 // Try to resolve a field using the class linker. Return null if it could not
286 // be found.
287 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
288
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000289 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
290 const DexCompilationUnit& compilation_unit) const
291 REQUIRES_SHARED(Locks::mutator_lock_);
292
293 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
294
Vladimir Markoca6fff82017-10-03 14:49:14 +0100295 ArenaAllocator* const allocator_;
David Brazdildee58d62016-04-07 09:54:26 +0000296 HGraph* const graph_;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100297 VariableSizedHandleScope* const handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000298
299 // The dex file where the method being compiled is, and the bytecode data.
300 const DexFile* const dex_file_;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800301 const CodeItemDebugInfoAccessor code_item_accessor_; // null for intrinsic graph.
David Brazdildee58d62016-04-07 09:54:26 +0000302
303 // The return type of the method being compiled.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100304 const DataType::Type return_type_;
David Brazdildee58d62016-04-07 09:54:26 +0000305
Vladimir Marko69d310e2017-10-09 14:12:23 +0100306 HBasicBlockBuilder* const block_builder_;
307 SsaBuilder* const ssa_builder_;
David Brazdildee58d62016-04-07 09:54:26 +0000308
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000309 CodeGenerator* const code_generator_;
310
David Brazdildee58d62016-04-07 09:54:26 +0000311 // The compilation unit of the current method being compiled. Note that
312 // it can be an inlined method.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100313 const DexCompilationUnit* const dex_compilation_unit_;
David Brazdildee58d62016-04-07 09:54:26 +0000314
315 // The compilation unit of the outermost method being compiled. That is the
316 // method being compiled (and not inlined), and potentially inlining other
317 // methods.
318 const DexCompilationUnit* const outer_compilation_unit_;
319
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700320 // Original values kept after instruction quickening.
321 QuickenInfoTable quicken_info_;
David Brazdildee58d62016-04-07 09:54:26 +0000322
Vladimir Marko69d310e2017-10-09 14:12:23 +0100323 OptimizingCompilerStats* const compilation_stats_;
David Brazdildee58d62016-04-07 09:54:26 +0000324
Vladimir Marko69d310e2017-10-09 14:12:23 +0100325 ScopedArenaAllocator* const local_allocator_;
326 ScopedArenaVector<ScopedArenaVector<HInstruction*>> locals_for_;
327 HBasicBlock* current_block_;
328 ScopedArenaVector<HInstruction*>* current_locals_;
329 HInstruction* latest_result_;
330 // Current "this" parameter.
331 // Valid only after InitializeParameters() finishes.
332 // * Null for static methods.
333 // * Non-null for instance methods.
334 HParameterValue* current_this_parameter_;
335
336 ScopedArenaVector<HBasicBlock*> loop_headers_;
David Brazdildee58d62016-04-07 09:54:26 +0000337
Vladimir Marko3b506202018-10-31 14:33:58 +0000338 // Cached resolved types for the current compilation unit's DexFile.
339 // Handle<>s reference entries in the `handles_`.
340 ScopedArenaSafeMap<dex::TypeIndex, Handle<mirror::Class>> class_cache_;
341
David Brazdildee58d62016-04-07 09:54:26 +0000342 static constexpr int kDefaultNumberOfLoops = 2;
343
344 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
345};
346
347} // namespace art
348
349#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_