blob: e735a0c46d463de43e21c8171baa6f7abdf23f58 [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
20#include "base/arena_containers.h"
21#include "base/arena_object.h"
22#include "block_builder.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080023#include "dex_file_types.h"
David Brazdildee58d62016-04-07 09:54:26 +000024#include "driver/compiler_driver.h"
25#include "driver/compiler_driver-inl.h"
26#include "driver/dex_compilation_unit.h"
27#include "mirror/dex_cache.h"
28#include "nodes.h"
29#include "optimizing_compiler_stats.h"
30#include "ssa_builder.h"
31
32namespace art {
33
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000034class CodeGenerator;
Andreas Gampe26de38b2016-07-27 17:53:11 -070035class Instruction;
36
David Brazdildee58d62016-04-07 09:54:26 +000037class HInstructionBuilder : public ValueObject {
38 public:
39 HInstructionBuilder(HGraph* graph,
40 HBasicBlockBuilder* block_builder,
41 SsaBuilder* ssa_builder,
42 const DexFile* dex_file,
43 const DexFile::CodeItem& code_item,
44 Primitive::Type return_type,
45 DexCompilationUnit* dex_compilation_unit,
46 const DexCompilationUnit* const outer_compilation_unit,
47 CompilerDriver* driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000048 CodeGenerator* code_generator,
David Brazdildee58d62016-04-07 09:54:26 +000049 const uint8_t* interpreter_metadata,
50 OptimizingCompilerStats* compiler_stats,
Nicolas Geoffray5247c082017-01-13 14:17:29 +000051 Handle<mirror::DexCache> dex_cache,
52 VariableSizedHandleScope* handles)
David Brazdildee58d62016-04-07 09:54:26 +000053 : arena_(graph->GetArena()),
54 graph_(graph),
Nicolas Geoffray5247c082017-01-13 14:17:29 +000055 handles_(handles),
David Brazdildee58d62016-04-07 09:54:26 +000056 dex_file_(dex_file),
57 code_item_(code_item),
58 return_type_(return_type),
59 block_builder_(block_builder),
60 ssa_builder_(ssa_builder),
61 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)),
62 current_block_(nullptr),
63 current_locals_(nullptr),
64 latest_result_(nullptr),
65 compiler_driver_(driver),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000066 code_generator_(code_generator),
David Brazdildee58d62016-04-07 09:54:26 +000067 dex_compilation_unit_(dex_compilation_unit),
68 outer_compilation_unit_(outer_compilation_unit),
69 interpreter_metadata_(interpreter_metadata),
70 skipped_interpreter_metadata_(std::less<uint32_t>(),
71 arena_->Adapter(kArenaAllocGraphBuilder)),
72 compilation_stats_(compiler_stats),
73 dex_cache_(dex_cache),
74 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
75 loop_headers_.reserve(kDefaultNumberOfLoops);
76 }
77
78 bool Build();
79
80 private:
81 void MaybeRecordStat(MethodCompilationStat compilation_stat);
82
83 void InitializeBlockLocals();
84 void PropagateLocalsToCatchBlocks();
85 void SetLoopHeaderPhiInputs();
86
87 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc);
88 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
89
90 bool CanDecodeQuickenedInfo() const;
91 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
92
93 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
94
95 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
96 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
97 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010098 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000099 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
100
101 void AppendInstruction(HInstruction* instruction);
102 void InsertInstructionAtTop(HInstruction* instruction);
103 void InitializeInstruction(HInstruction* instruction);
104
105 void InitializeParameters();
106
107 // Returns whether the current method needs access check for the type.
108 // Output parameter finalizable is set to whether the type is finalizable.
Vladimir Markobfb80d22017-02-14 14:08:12 +0000109 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000111
112 template<typename T>
113 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
114
115 template<typename T>
116 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
117
118 template<typename T>
119 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
120
121 void Binop_23x_cmp(const Instruction& instruction,
122 Primitive::Type type,
123 ComparisonBias bias,
124 uint32_t dex_pc);
125
126 template<typename T>
127 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
128
129 template<typename T>
130 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
131
132 template<typename T>
133 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
134
135 template<typename T>
136 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
137
138 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
139 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
140
141 void Conversion_12x(const Instruction& instruction,
142 Primitive::Type input_type,
143 Primitive::Type result_type,
144 uint32_t dex_pc);
145
146 void BuildCheckedDivRem(uint16_t out_reg,
147 uint16_t first_reg,
148 int64_t second_reg_or_constant,
149 uint32_t dex_pc,
150 Primitive::Type type,
151 bool second_is_lit,
152 bool is_div);
153
154 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
155
156 // Builds an instance field access node and returns whether the instruction is supported.
157 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
158
159 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
160 uint32_t dex_pc,
161 bool is_put,
162 Primitive::Type field_type);
163 // Builds a static field access node and returns whether the instruction is supported.
164 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
165
166 void BuildArrayAccess(const Instruction& instruction,
167 uint32_t dex_pc,
168 bool is_get,
169 Primitive::Type anticipated_type);
170
171 // Builds an invocation node and returns whether the instruction is supported.
172 bool BuildInvoke(const Instruction& instruction,
173 uint32_t dex_pc,
174 uint32_t method_idx,
175 uint32_t number_of_vreg_arguments,
176 bool is_range,
177 uint32_t* args,
178 uint32_t register_index);
179
Orion Hodsonac141392017-01-13 11:53:47 +0000180 // Builds an invocation node for invoke-polymorphic and returns whether the
181 // instruction is supported.
182 bool BuildInvokePolymorphic(const Instruction& instruction,
183 uint32_t dex_pc,
184 uint32_t method_idx,
185 uint32_t proto_idx,
186 uint32_t number_of_vreg_arguments,
187 bool is_range,
188 uint32_t* args,
189 uint32_t register_index);
190
David Brazdildee58d62016-04-07 09:54:26 +0000191 // Builds a new array node and the instructions that fill it.
192 void BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800193 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000194 uint32_t number_of_vreg_arguments,
195 bool is_range,
196 uint32_t* args,
197 uint32_t register_index);
198
199 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
200
201 // Fills the given object with data as specified in the fill-array-data
202 // instruction. Currently only used for non-reference and non-floating point
203 // arrays.
204 template <typename T>
205 void BuildFillArrayData(HInstruction* object,
206 const T* data,
207 uint32_t element_count,
208 Primitive::Type anticipated_type,
209 uint32_t dex_pc);
210
211 // Fills the given object with data as specified in the fill-array-data
212 // instruction. The data must be for long and double arrays.
213 void BuildFillWideArrayData(HInstruction* object,
214 const int64_t* data,
215 uint32_t element_count,
216 uint32_t dex_pc);
217
218 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
219 void BuildTypeCheck(const Instruction& instruction,
220 uint8_t destination,
221 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800222 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000223 uint32_t dex_pc);
224
225 // Builds an instruction sequence for a switch statement.
226 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
227
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000228 // Builds a `HLoadClass` loading the given `type_index`. If `outer` is true,
229 // this method will use the outer class's dex file to lookup the type at
230 // `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000231 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
232
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000233 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000234 const DexFile& dex_file,
235 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000236 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000237 bool needs_access_check)
238 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000239
David Brazdildee58d62016-04-07 09:54:26 +0000240 // Returns the outer-most compiling method's class.
241 mirror::Class* GetOutermostCompilingClass() const;
242
243 // Returns the class whose method is being compiled.
244 mirror::Class* GetCompilingClass() const;
245
246 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800247 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000248
249 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
250 uint32_t dex_pc,
251 HInvoke* invoke);
252
253 bool SetupInvokeArguments(HInvoke* invoke,
254 uint32_t number_of_vreg_arguments,
255 uint32_t* args,
256 uint32_t register_index,
257 bool is_range,
258 const char* descriptor,
259 size_t start_index,
260 size_t* argument_index);
261
262 bool HandleInvoke(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,
Aart Bik296fbb42016-06-07 13:49:12 -0700268 HClinitCheck* clinit_check,
269 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000270
271 bool HandleStringInit(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);
277 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
278
279 HClinitCheck* ProcessClinitCheckForInvoke(
280 uint32_t dex_pc,
281 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000282 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000284
285 // Build a HNewInstance instruction.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800286 bool BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000287
288 // Return whether the compiler can assume `cls` is initialized.
289 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700290 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000291
292 // Try to resolve a method using the class linker. Return null if a method could
293 // not be resolved.
294 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
295
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000296 // Try to resolve a field using the class linker. Return null if it could not
297 // be found.
298 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
299
Vladimir Markobfb80d22017-02-14 14:08:12 +0000300 ObjPtr<mirror::Class> LookupResolvedType(dex::TypeIndex type_index,
301 const DexCompilationUnit& compilation_unit) const
302 REQUIRES_SHARED(Locks::mutator_lock_);
303
304 ObjPtr<mirror::Class> LookupReferrerClass() const REQUIRES_SHARED(Locks::mutator_lock_);
305
David Brazdildee58d62016-04-07 09:54:26 +0000306 ArenaAllocator* const arena_;
307 HGraph* const graph_;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000308 VariableSizedHandleScope* handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000309
310 // The dex file where the method being compiled is, and the bytecode data.
311 const DexFile* const dex_file_;
312 const DexFile::CodeItem& code_item_;
313
314 // The return type of the method being compiled.
315 const Primitive::Type return_type_;
316
317 HBasicBlockBuilder* block_builder_;
318 SsaBuilder* ssa_builder_;
319
320 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
321 HBasicBlock* current_block_;
322 ArenaVector<HInstruction*>* current_locals_;
323 HInstruction* latest_result_;
324
325 CompilerDriver* const compiler_driver_;
326
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000327 CodeGenerator* const code_generator_;
328
David Brazdildee58d62016-04-07 09:54:26 +0000329 // The compilation unit of the current method being compiled. Note that
330 // it can be an inlined method.
331 DexCompilationUnit* const dex_compilation_unit_;
332
333 // The compilation unit of the outermost method being compiled. That is the
334 // method being compiled (and not inlined), and potentially inlining other
335 // methods.
336 const DexCompilationUnit* const outer_compilation_unit_;
337
338 // Original values kept after instruction quickening. This is a data buffer
339 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
340 const uint8_t* interpreter_metadata_;
341
342 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
343 // info for out-of-order dex_pcs is stored in a map until the positions
344 // are eventually visited.
345 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
346
347 OptimizingCompilerStats* compilation_stats_;
348 Handle<mirror::DexCache> dex_cache_;
349
350 ArenaVector<HBasicBlock*> loop_headers_;
351
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_