blob: 3bb680ce4463fa74986f1447f6f490bcc7649290 [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 Markod16363a2017-02-01 14:09:13 +0000109 bool NeedsAccessCheck(dex::TypeIndex type_index,
110 Handle<mirror::DexCache> dex_cache,
111 /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markod16363a2017-02-01 14:09:13 +0000113 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const;
David Brazdildee58d62016-04-07 09:54:26 +0000114
115 template<typename T>
116 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
117
118 template<typename T>
119 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
120
121 template<typename T>
122 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
123
124 void Binop_23x_cmp(const Instruction& instruction,
125 Primitive::Type type,
126 ComparisonBias bias,
127 uint32_t dex_pc);
128
129 template<typename T>
130 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
131
132 template<typename T>
133 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
134
135 template<typename T>
136 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
137
138 template<typename T>
139 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
140
141 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
142 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
143
144 void Conversion_12x(const Instruction& instruction,
145 Primitive::Type input_type,
146 Primitive::Type result_type,
147 uint32_t dex_pc);
148
149 void BuildCheckedDivRem(uint16_t out_reg,
150 uint16_t first_reg,
151 int64_t second_reg_or_constant,
152 uint32_t dex_pc,
153 Primitive::Type type,
154 bool second_is_lit,
155 bool is_div);
156
157 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
158
159 // Builds an instance field access node and returns whether the instruction is supported.
160 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
161
162 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
163 uint32_t dex_pc,
164 bool is_put,
165 Primitive::Type field_type);
166 // Builds a static field access node and returns whether the instruction is supported.
167 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
168
169 void BuildArrayAccess(const Instruction& instruction,
170 uint32_t dex_pc,
171 bool is_get,
172 Primitive::Type anticipated_type);
173
174 // Builds an invocation node and returns whether the instruction is supported.
175 bool BuildInvoke(const Instruction& instruction,
176 uint32_t dex_pc,
177 uint32_t method_idx,
178 uint32_t number_of_vreg_arguments,
179 bool is_range,
180 uint32_t* args,
181 uint32_t register_index);
182
Orion Hodsonac141392017-01-13 11:53:47 +0000183 // Builds an invocation node for invoke-polymorphic and returns whether the
184 // instruction is supported.
185 bool BuildInvokePolymorphic(const Instruction& instruction,
186 uint32_t dex_pc,
187 uint32_t method_idx,
188 uint32_t proto_idx,
189 uint32_t number_of_vreg_arguments,
190 bool is_range,
191 uint32_t* args,
192 uint32_t register_index);
193
David Brazdildee58d62016-04-07 09:54:26 +0000194 // Builds a new array node and the instructions that fill it.
195 void BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800196 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000197 uint32_t number_of_vreg_arguments,
198 bool is_range,
199 uint32_t* args,
200 uint32_t register_index);
201
202 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
203
204 // Fills the given object with data as specified in the fill-array-data
205 // instruction. Currently only used for non-reference and non-floating point
206 // arrays.
207 template <typename T>
208 void BuildFillArrayData(HInstruction* object,
209 const T* data,
210 uint32_t element_count,
211 Primitive::Type anticipated_type,
212 uint32_t dex_pc);
213
214 // Fills the given object with data as specified in the fill-array-data
215 // instruction. The data must be for long and double arrays.
216 void BuildFillWideArrayData(HInstruction* object,
217 const int64_t* data,
218 uint32_t element_count,
219 uint32_t dex_pc);
220
221 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
222 void BuildTypeCheck(const Instruction& instruction,
223 uint8_t destination,
224 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800225 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000226 uint32_t dex_pc);
227
228 // Builds an instruction sequence for a switch statement.
229 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
230
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000231 // Builds a `HLoadClass` loading the given `type_index`. If `outer` is true,
232 // this method will use the outer class's dex file to lookup the type at
233 // `type_index`.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000234 HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
235
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000236 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000237 const DexFile& dex_file,
238 Handle<mirror::Class> klass,
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000239 uint32_t dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000240 bool needs_access_check)
241 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000242
David Brazdildee58d62016-04-07 09:54:26 +0000243 // Returns the outer-most compiling method's class.
244 mirror::Class* GetOutermostCompilingClass() const;
245
246 // Returns the class whose method is being compiled.
247 mirror::Class* GetCompilingClass() const;
248
249 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800250 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000251
252 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
253 uint32_t dex_pc,
254 HInvoke* invoke);
255
256 bool SetupInvokeArguments(HInvoke* invoke,
257 uint32_t number_of_vreg_arguments,
258 uint32_t* args,
259 uint32_t register_index,
260 bool is_range,
261 const char* descriptor,
262 size_t start_index,
263 size_t* argument_index);
264
265 bool HandleInvoke(HInvoke* invoke,
266 uint32_t number_of_vreg_arguments,
267 uint32_t* args,
268 uint32_t register_index,
269 bool is_range,
270 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700271 HClinitCheck* clinit_check,
272 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000273
274 bool HandleStringInit(HInvoke* invoke,
275 uint32_t number_of_vreg_arguments,
276 uint32_t* args,
277 uint32_t register_index,
278 bool is_range,
279 const char* descriptor);
280 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
281
282 HClinitCheck* ProcessClinitCheckForInvoke(
283 uint32_t dex_pc,
284 ArtMethod* method,
David Brazdildee58d62016-04-07 09:54:26 +0000285 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700286 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000287
288 // Build a HNewInstance instruction.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800289 bool BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000290
291 // Return whether the compiler can assume `cls` is initialized.
292 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700293 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000294
295 // Try to resolve a method using the class linker. Return null if a method could
296 // not be resolved.
297 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
298
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000299 // Try to resolve a field using the class linker. Return null if it could not
300 // be found.
301 ArtField* ResolveField(uint16_t field_idx, bool is_static, bool is_put);
302
David Brazdildee58d62016-04-07 09:54:26 +0000303 ArenaAllocator* const arena_;
304 HGraph* const graph_;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000305 VariableSizedHandleScope* handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000306
307 // The dex file where the method being compiled is, and the bytecode data.
308 const DexFile* const dex_file_;
309 const DexFile::CodeItem& code_item_;
310
311 // The return type of the method being compiled.
312 const Primitive::Type return_type_;
313
314 HBasicBlockBuilder* block_builder_;
315 SsaBuilder* ssa_builder_;
316
317 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
318 HBasicBlock* current_block_;
319 ArenaVector<HInstruction*>* current_locals_;
320 HInstruction* latest_result_;
321
322 CompilerDriver* const compiler_driver_;
323
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000324 CodeGenerator* const code_generator_;
325
David Brazdildee58d62016-04-07 09:54:26 +0000326 // The compilation unit of the current method being compiled. Note that
327 // it can be an inlined method.
328 DexCompilationUnit* const dex_compilation_unit_;
329
330 // The compilation unit of the outermost method being compiled. That is the
331 // method being compiled (and not inlined), and potentially inlining other
332 // methods.
333 const DexCompilationUnit* const outer_compilation_unit_;
334
335 // Original values kept after instruction quickening. This is a data buffer
336 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
337 const uint8_t* interpreter_metadata_;
338
339 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
340 // info for out-of-order dex_pcs is stored in a map until the positions
341 // are eventually visited.
342 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
343
344 OptimizingCompilerStats* compilation_stats_;
345 Handle<mirror::DexCache> dex_cache_;
346
347 ArenaVector<HBasicBlock*> loop_headers_;
348
349 static constexpr int kDefaultNumberOfLoops = 2;
350
351 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
352};
353
354} // namespace art
355
356#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_