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