blob: 5efe95094ccaf706a989b862b693d398eb04ca3e [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
Andreas Gampe26de38b2016-07-27 17:53:11 -070034class Instruction;
35
David Brazdildee58d62016-04-07 09:54:26 +000036class HInstructionBuilder : public ValueObject {
37 public:
38 HInstructionBuilder(HGraph* graph,
39 HBasicBlockBuilder* block_builder,
40 SsaBuilder* ssa_builder,
41 const DexFile* dex_file,
42 const DexFile::CodeItem& code_item,
43 Primitive::Type return_type,
44 DexCompilationUnit* dex_compilation_unit,
45 const DexCompilationUnit* const outer_compilation_unit,
46 CompilerDriver* driver,
47 const uint8_t* interpreter_metadata,
48 OptimizingCompilerStats* compiler_stats,
Nicolas Geoffray5247c082017-01-13 14:17:29 +000049 Handle<mirror::DexCache> dex_cache,
50 VariableSizedHandleScope* handles)
David Brazdildee58d62016-04-07 09:54:26 +000051 : arena_(graph->GetArena()),
52 graph_(graph),
Nicolas Geoffray5247c082017-01-13 14:17:29 +000053 handles_(handles),
David Brazdildee58d62016-04-07 09:54:26 +000054 dex_file_(dex_file),
55 code_item_(code_item),
56 return_type_(return_type),
57 block_builder_(block_builder),
58 ssa_builder_(ssa_builder),
59 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)),
60 current_block_(nullptr),
61 current_locals_(nullptr),
62 latest_result_(nullptr),
63 compiler_driver_(driver),
64 dex_compilation_unit_(dex_compilation_unit),
65 outer_compilation_unit_(outer_compilation_unit),
66 interpreter_metadata_(interpreter_metadata),
67 skipped_interpreter_metadata_(std::less<uint32_t>(),
68 arena_->Adapter(kArenaAllocGraphBuilder)),
69 compilation_stats_(compiler_stats),
70 dex_cache_(dex_cache),
71 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
72 loop_headers_.reserve(kDefaultNumberOfLoops);
73 }
74
75 bool Build();
76
77 private:
78 void MaybeRecordStat(MethodCompilationStat compilation_stat);
79
80 void InitializeBlockLocals();
81 void PropagateLocalsToCatchBlocks();
82 void SetLoopHeaderPhiInputs();
83
84 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc);
85 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
86
87 bool CanDecodeQuickenedInfo() const;
88 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
89
90 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
91
92 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
93 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
94 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010095 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000096 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
97
98 void AppendInstruction(HInstruction* instruction);
99 void InsertInstructionAtTop(HInstruction* instruction);
100 void InitializeInstruction(HInstruction* instruction);
101
102 void InitializeParameters();
103
104 // Returns whether the current method needs access check for the type.
105 // Output parameter finalizable is set to whether the type is finalizable.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800106 bool NeedsAccessCheck(dex::TypeIndex type_index,
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100107 Handle<mirror::DexCache> dex_cache,
108 /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800110 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const;
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`.
231 HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
232 uint32_t dex_pc,
233 bool check_access,
234 bool outer = false);
235
David Brazdildee58d62016-04-07 09:54:26 +0000236 // Returns the outer-most compiling method's class.
237 mirror::Class* GetOutermostCompilingClass() const;
238
239 // Returns the class whose method is being compiled.
240 mirror::Class* GetCompilingClass() const;
241
242 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800243 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000244
245 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
246 uint32_t dex_pc,
247 HInvoke* invoke);
248
249 bool SetupInvokeArguments(HInvoke* invoke,
250 uint32_t number_of_vreg_arguments,
251 uint32_t* args,
252 uint32_t register_index,
253 bool is_range,
254 const char* descriptor,
255 size_t start_index,
256 size_t* argument_index);
257
258 bool HandleInvoke(HInvoke* invoke,
259 uint32_t number_of_vreg_arguments,
260 uint32_t* args,
261 uint32_t register_index,
262 bool is_range,
263 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700264 HClinitCheck* clinit_check,
265 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000266
267 bool HandleStringInit(HInvoke* invoke,
268 uint32_t number_of_vreg_arguments,
269 uint32_t* args,
270 uint32_t register_index,
271 bool is_range,
272 const char* descriptor);
273 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
274
275 HClinitCheck* ProcessClinitCheckForInvoke(
276 uint32_t dex_pc,
277 ArtMethod* method,
278 uint32_t method_idx,
279 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.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800283 bool BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000284
285 // Return whether the compiler can assume `cls` is initialized.
286 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700287 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000288
289 // Try to resolve a method using the class linker. Return null if a method could
290 // not be resolved.
291 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
292
293 ArenaAllocator* const arena_;
294 HGraph* const graph_;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000295 VariableSizedHandleScope* handles_;
David Brazdildee58d62016-04-07 09:54:26 +0000296
297 // The dex file where the method being compiled is, and the bytecode data.
298 const DexFile* const dex_file_;
299 const DexFile::CodeItem& code_item_;
300
301 // The return type of the method being compiled.
302 const Primitive::Type return_type_;
303
304 HBasicBlockBuilder* block_builder_;
305 SsaBuilder* ssa_builder_;
306
307 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
308 HBasicBlock* current_block_;
309 ArenaVector<HInstruction*>* current_locals_;
310 HInstruction* latest_result_;
311
312 CompilerDriver* const compiler_driver_;
313
314 // The compilation unit of the current method being compiled. Note that
315 // it can be an inlined method.
316 DexCompilationUnit* const dex_compilation_unit_;
317
318 // The compilation unit of the outermost method being compiled. That is the
319 // method being compiled (and not inlined), and potentially inlining other
320 // methods.
321 const DexCompilationUnit* const outer_compilation_unit_;
322
323 // Original values kept after instruction quickening. This is a data buffer
324 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
325 const uint8_t* interpreter_metadata_;
326
327 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
328 // info for out-of-order dex_pcs is stored in a map until the positions
329 // are eventually visited.
330 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
331
332 OptimizingCompilerStats* compilation_stats_;
333 Handle<mirror::DexCache> dex_cache_;
334
335 ArenaVector<HBasicBlock*> loop_headers_;
336
337 static constexpr int kDefaultNumberOfLoops = 2;
338
339 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
340};
341
342} // namespace art
343
344#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_