blob: f480b70062173b067db5d36a25b2f490f9f40ae6 [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.
100 bool NeedsAccessCheck(uint32_t type_index, /*out*/bool* finalizable) const;
101
102 template<typename T>
103 void Unop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
104
105 template<typename T>
106 void Binop_23x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
107
108 template<typename T>
109 void Binop_23x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
110
111 void Binop_23x_cmp(const Instruction& instruction,
112 Primitive::Type type,
113 ComparisonBias bias,
114 uint32_t dex_pc);
115
116 template<typename T>
117 void Binop_12x(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
118
119 template<typename T>
120 void Binop_12x_shift(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
121
122 template<typename T>
123 void Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc);
124
125 template<typename T>
126 void Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc);
127
128 template<typename T> void If_21t(const Instruction& instruction, uint32_t dex_pc);
129 template<typename T> void If_22t(const Instruction& instruction, uint32_t dex_pc);
130
131 void Conversion_12x(const Instruction& instruction,
132 Primitive::Type input_type,
133 Primitive::Type result_type,
134 uint32_t dex_pc);
135
136 void BuildCheckedDivRem(uint16_t out_reg,
137 uint16_t first_reg,
138 int64_t second_reg_or_constant,
139 uint32_t dex_pc,
140 Primitive::Type type,
141 bool second_is_lit,
142 bool is_div);
143
144 void BuildReturn(const Instruction& instruction, Primitive::Type type, uint32_t dex_pc);
145
146 // Builds an instance field access node and returns whether the instruction is supported.
147 bool BuildInstanceFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
148
149 void BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
150 uint32_t dex_pc,
151 bool is_put,
152 Primitive::Type field_type);
153 // Builds a static field access node and returns whether the instruction is supported.
154 bool BuildStaticFieldAccess(const Instruction& instruction, uint32_t dex_pc, bool is_put);
155
156 void BuildArrayAccess(const Instruction& instruction,
157 uint32_t dex_pc,
158 bool is_get,
159 Primitive::Type anticipated_type);
160
161 // Builds an invocation node and returns whether the instruction is supported.
162 bool BuildInvoke(const Instruction& instruction,
163 uint32_t dex_pc,
164 uint32_t method_idx,
165 uint32_t number_of_vreg_arguments,
166 bool is_range,
167 uint32_t* args,
168 uint32_t register_index);
169
170 // Builds a new array node and the instructions that fill it.
171 void BuildFilledNewArray(uint32_t dex_pc,
172 uint32_t type_index,
173 uint32_t number_of_vreg_arguments,
174 bool is_range,
175 uint32_t* args,
176 uint32_t register_index);
177
178 void BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc);
179
180 // Fills the given object with data as specified in the fill-array-data
181 // instruction. Currently only used for non-reference and non-floating point
182 // arrays.
183 template <typename T>
184 void BuildFillArrayData(HInstruction* object,
185 const T* data,
186 uint32_t element_count,
187 Primitive::Type anticipated_type,
188 uint32_t dex_pc);
189
190 // Fills the given object with data as specified in the fill-array-data
191 // instruction. The data must be for long and double arrays.
192 void BuildFillWideArrayData(HInstruction* object,
193 const int64_t* data,
194 uint32_t element_count,
195 uint32_t dex_pc);
196
197 // Builds a `HInstanceOf`, or a `HCheckCast` instruction.
198 void BuildTypeCheck(const Instruction& instruction,
199 uint8_t destination,
200 uint8_t reference,
201 uint16_t type_index,
202 uint32_t dex_pc);
203
204 // Builds an instruction sequence for a switch statement.
205 void BuildSwitch(const Instruction& instruction, uint32_t dex_pc);
206
207 // Returns the outer-most compiling method's class.
208 mirror::Class* GetOutermostCompilingClass() const;
209
210 // Returns the class whose method is being compiled.
211 mirror::Class* GetCompilingClass() const;
212
213 // Returns whether `type_index` points to the outer-most compiling method's class.
214 bool IsOutermostCompilingClass(uint16_t type_index) const;
215
216 void PotentiallySimplifyFakeString(uint16_t original_dex_register,
217 uint32_t dex_pc,
218 HInvoke* invoke);
219
220 bool SetupInvokeArguments(HInvoke* invoke,
221 uint32_t number_of_vreg_arguments,
222 uint32_t* args,
223 uint32_t register_index,
224 bool is_range,
225 const char* descriptor,
226 size_t start_index,
227 size_t* argument_index);
228
229 bool HandleInvoke(HInvoke* invoke,
230 uint32_t number_of_vreg_arguments,
231 uint32_t* args,
232 uint32_t register_index,
233 bool is_range,
234 const char* descriptor,
235 HClinitCheck* clinit_check);
236
237 bool HandleStringInit(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);
243 void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
244
245 HClinitCheck* ProcessClinitCheckForInvoke(
246 uint32_t dex_pc,
247 ArtMethod* method,
248 uint32_t method_idx,
249 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
250 SHARED_REQUIRES(Locks::mutator_lock_);
251
252 // Build a HNewInstance instruction.
253 bool BuildNewInstance(uint16_t type_index, uint32_t dex_pc);
254
255 // Return whether the compiler can assume `cls` is initialized.
256 bool IsInitialized(Handle<mirror::Class> cls) const
257 SHARED_REQUIRES(Locks::mutator_lock_);
258
259 // Try to resolve a method using the class linker. Return null if a method could
260 // not be resolved.
261 ArtMethod* ResolveMethod(uint16_t method_idx, InvokeType invoke_type);
262
263 ArenaAllocator* const arena_;
264 HGraph* const graph_;
265
266 // The dex file where the method being compiled is, and the bytecode data.
267 const DexFile* const dex_file_;
268 const DexFile::CodeItem& code_item_;
269
270 // The return type of the method being compiled.
271 const Primitive::Type return_type_;
272
273 HBasicBlockBuilder* block_builder_;
274 SsaBuilder* ssa_builder_;
275
276 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
277 HBasicBlock* current_block_;
278 ArenaVector<HInstruction*>* current_locals_;
279 HInstruction* latest_result_;
280
281 CompilerDriver* const compiler_driver_;
282
283 // The compilation unit of the current method being compiled. Note that
284 // it can be an inlined method.
285 DexCompilationUnit* const dex_compilation_unit_;
286
287 // The compilation unit of the outermost method being compiled. That is the
288 // method being compiled (and not inlined), and potentially inlining other
289 // methods.
290 const DexCompilationUnit* const outer_compilation_unit_;
291
292 // Original values kept after instruction quickening. This is a data buffer
293 // of Leb128-encoded (dex_pc, value) pairs sorted by dex_pc.
294 const uint8_t* interpreter_metadata_;
295
296 // InstructionBuilder does not parse instructions in dex_pc order. Quickening
297 // info for out-of-order dex_pcs is stored in a map until the positions
298 // are eventually visited.
299 ArenaSafeMap<uint32_t, uint16_t> skipped_interpreter_metadata_;
300
301 OptimizingCompilerStats* compilation_stats_;
302 Handle<mirror::DexCache> dex_cache_;
303
304 ArenaVector<HBasicBlock*> loop_headers_;
305
306 static constexpr int kDefaultNumberOfLoops = 2;
307
308 DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
309};
310
311} // namespace art
312
313#endif // ART_COMPILER_OPTIMIZING_INSTRUCTION_BUILDER_H_