blob: f29e522040abbc2589cfc72e000904a9764318a7 [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,
49 Handle<mirror::DexCache> dex_cache)
50 : arena_(graph->GetArena()),
51 graph_(graph),
52 dex_file_(dex_file),
53 code_item_(code_item),
54 return_type_(return_type),
55 block_builder_(block_builder),
56 ssa_builder_(ssa_builder),
57 locals_for_(arena_->Adapter(kArenaAllocGraphBuilder)),
58 current_block_(nullptr),
59 current_locals_(nullptr),
60 latest_result_(nullptr),
61 compiler_driver_(driver),
62 dex_compilation_unit_(dex_compilation_unit),
63 outer_compilation_unit_(outer_compilation_unit),
64 interpreter_metadata_(interpreter_metadata),
65 skipped_interpreter_metadata_(std::less<uint32_t>(),
66 arena_->Adapter(kArenaAllocGraphBuilder)),
67 compilation_stats_(compiler_stats),
68 dex_cache_(dex_cache),
69 loop_headers_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
70 loop_headers_.reserve(kDefaultNumberOfLoops);
71 }
72
73 bool Build();
74
75 private:
76 void MaybeRecordStat(MethodCompilationStat compilation_stat);
77
78 void InitializeBlockLocals();
79 void PropagateLocalsToCatchBlocks();
80 void SetLoopHeaderPhiInputs();
81
82 bool ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc);
83 void FindNativeDebugInfoLocations(ArenaBitVector* locations);
84
85 bool CanDecodeQuickenedInfo() const;
86 uint16_t LookupQuickenedInfo(uint32_t dex_pc);
87
88 HBasicBlock* FindBlockStartingAt(uint32_t dex_pc) const;
89
90 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
91 HInstruction* ValueOfLocalAt(HBasicBlock* block, size_t local);
92 HInstruction* LoadLocal(uint32_t register_index, Primitive::Type type) const;
David Brazdilc120bbe2016-04-22 16:57:00 +010093 HInstruction* LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +000094 void UpdateLocal(uint32_t register_index, HInstruction* instruction);
95
96 void AppendInstruction(HInstruction* instruction);
97 void InsertInstructionAtTop(HInstruction* instruction);
98 void InitializeInstruction(HInstruction* instruction);
99
100 void InitializeParameters();
101
102 // Returns whether the current method needs access check for the type.
103 // Output parameter finalizable is set to whether the type is finalizable.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800104 bool NeedsAccessCheck(dex::TypeIndex type_index,
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100105 Handle<mirror::DexCache> dex_cache,
106 /*out*/bool* finalizable) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800108 bool NeedsAccessCheck(dex::TypeIndex type_index, /*out*/bool* finalizable) const;
David Brazdildee58d62016-04-07 09:54:26 +0000109
110 template<typename T>
111 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
112
113 template<typename T>
114 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
115
116 template<typename T>
117 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
118
119 void Binop_23x_cmp(const Instruction& instruction,
120 Primitive::Type type,
121 ComparisonBias bias,
122 uint32_t dex_pc);
123
124 template<typename T>
125 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
126
127 template<typename T>
128 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
129
130 template<typename T>
131 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
132
133 template<typename T>
134 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
135
136 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
137 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
138
139 void Conversion_12x(const Instruction& instruction,
140 Primitive::Type input_type,
141 Primitive::Type result_type,
142 uint32_t dex_pc);
143
144 void BuildCheckedDivRem(uint16_t out_reg,
145 uint16_t first_reg,
146 int64_t second_reg_or_constant,
147 uint32_t dex_pc,
148 Primitive::Type type,
149 bool second_is_lit,
150 bool is_div);
151
152 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
153
154 // Builds an instance field access node and returns whether the instruction is supported.
155 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
156
157 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
158 uint32_t dex_pc,
159 bool is_put,
160 Primitive::Type field_type);
161 // Builds a static field access node and returns whether the instruction is supported.
162 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
163
164 void BuildArrayAccess(const Instruction& instruction,
165 uint32_t dex_pc,
166 bool is_get,
167 Primitive::Type anticipated_type);
168
169 // Builds an invocation node and returns whether the instruction is supported.
170 bool BuildInvoke(const Instruction& instruction,
171 uint32_t dex_pc,
172 uint32_t method_idx,
173 uint32_t number_of_vreg_arguments,
174 bool is_range,
175 uint32_t* args,
176 uint32_t register_index);
177
178 // Builds a new array node and the instructions that fill it.
179 void BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800180 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000181 uint32_t number_of_vreg_arguments,
182 bool is_range,
183 uint32_t* args,
184 uint32_t register_index);
185
186 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
187
188 // Fills the given object with data as specified in the fill-array-data
189 // instruction. Currently only used for non-reference and non-floating point
190 // arrays.
191 template <typename T>
192 void BuildFillArrayData(HInstruction* object,
193 const T* data,
194 uint32_t element_count,
195 Primitive::Type anticipated_type,
196 uint32_t dex_pc);
197
198 // Fills the given object with data as specified in the fill-array-data
199 // instruction. The data must be for long and double arrays.
200 void BuildFillWideArrayData(HInstruction* object,
201 const int64_t* data,
202 uint32_t element_count,
203 uint32_t dex_pc);
204
205 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
206 void BuildTypeCheck(const Instruction& instruction,
207 uint8_t destination,
208 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -0800209 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +0000210 uint32_t dex_pc);
211
212 // Builds an instruction sequence for a switch statement.
213 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
214
215 // Returns the outer-most compiling method's class.
216 mirror::Class* GetOutermostCompilingClass() const;
217
218 // Returns the class whose method is being compiled.
219 mirror::Class* GetCompilingClass() const;
220
221 // Returns whether `type_index` points to the outer-most compiling method's class.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800222 bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
David Brazdildee58d62016-04-07 09:54:26 +0000223
224 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
225 uint32_t dex_pc,
226 HInvoke* invoke);
227
228 bool SetupInvokeArguments(HInvoke* invoke,
229 uint32_t number_of_vreg_arguments,
230 uint32_t* args,
231 uint32_t register_index,
232 bool is_range,
233 const char* descriptor,
234 size_t start_index,
235 size_t* argument_index);
236
237 bool HandleInvoke(HInvoke* invoke,
238 uint32_t number_of_vreg_arguments,
239 uint32_t* args,
240 uint32_t register_index,
241 bool is_range,
242 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700243 HClinitCheck* clinit_check,
244 bool is_unresolved);
David Brazdildee58d62016-04-07 09:54:26 +0000245
246 bool HandleStringInit(HInvoke* invoke,
247 uint32_t number_of_vreg_arguments,
248 uint32_t* args,
249 uint32_t register_index,
250 bool is_range,
251 const char* descriptor);
252 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
253
254 HClinitCheck* ProcessClinitCheckForInvoke(
255 uint32_t dex_pc,
256 ArtMethod* method,
257 uint32_t method_idx,
258 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000260
261 // Build a HNewInstance instruction.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800262 bool BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000263
264 // Return whether the compiler can assume `cls` is initialized.
265 bool IsInitialized(Handle<mirror::Class> cls) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdildee58d62016-04-07 09:54:26 +0000267
268 // Try to resolve a method using the class linker. Return null if a method could
269 // not be resolved.
270 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
271
272 ArenaAllocator* const arena_;
273 HGraph* const graph_;
274
275 // The dex file where the method being compiled is, and the bytecode data.
276 const DexFile* const dex_file_;
277 const DexFile::CodeItem& code_item_;
278
279 // The return type of the method being compiled.
280 const Primitive::Type return_type_;
281
282 HBasicBlockBuilder* block_builder_;
283 SsaBuilder* ssa_builder_;
284
285 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
286 HBasicBlock* current_block_;
287 ArenaVector<HInstruction*>* current_locals_;
288 HInstruction* latest_result_;
289
290 CompilerDriver* const compiler_driver_;
291
292 // The compilation unit of the current method being compiled. Note that
293 // it can be an inlined method.
294 DexCompilationUnit* const dex_compilation_unit_;
295
296 // The compilation unit of the outermost method being compiled. That is the
297 // method being compiled (and not inlined), and potentially inlining other
298 // methods.
299 const DexCompilationUnit* const outer_compilation_unit_;
300
301 // Original values kept after instruction quickening. This is a data buffer
302 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
303 const uint8_t* interpreter_metadata_;
304
305 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
306 // info for out-of-order dex_pcs is stored in a map until the positions
307 // are eventually visited.
308 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
309
310 OptimizingCompilerStats* compilation_stats_;
311 Handle<mirror::DexCache> dex_cache_;
312
313 ArenaVector<HBasicBlock*> loop_headers_;
314
315 static constexpr int kDefaultNumberOfLoops = 2;
316
317 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
318};
319
320} // namespace art
321
322#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_