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