blob: e9b5b5a93d6dae1ff220f5b828879563b9536080 [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#include "instruction_builder.h"
18
Matthew Gharrity465ecc82016-07-19 21:32:52 +000019#include "art_method-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
22#include "block_builder.h"
Vladimir Marko3b506202018-10-31 14:33:58 +000023#include "class_linker-inl.h"
24#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010025#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070026#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010028#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000029#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070030#include "imtable-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010031#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000032#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070034#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010037#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070038#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000039
40namespace art {
41
Mathieu Chartier808c7a52017-12-15 11:19:33 -080042HInstructionBuilder::HInstructionBuilder(HGraph* graph,
43 HBasicBlockBuilder* block_builder,
44 SsaBuilder* ssa_builder,
45 const DexFile* dex_file,
46 const CodeItemDebugInfoAccessor& accessor,
47 DataType::Type return_type,
48 const DexCompilationUnit* dex_compilation_unit,
49 const DexCompilationUnit* outer_compilation_unit,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080050 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080051 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080052 OptimizingCompilerStats* compiler_stats,
53 VariableSizedHandleScope* handles,
54 ScopedArenaAllocator* local_allocator)
55 : allocator_(graph->GetAllocator()),
56 graph_(graph),
57 handles_(handles),
58 dex_file_(dex_file),
59 code_item_accessor_(accessor),
60 return_type_(return_type),
61 block_builder_(block_builder),
62 ssa_builder_(ssa_builder),
Mathieu Chartier808c7a52017-12-15 11:19:33 -080063 code_generator_(code_generator),
64 dex_compilation_unit_(dex_compilation_unit),
65 outer_compilation_unit_(outer_compilation_unit),
66 quicken_info_(interpreter_metadata),
67 compilation_stats_(compiler_stats),
68 local_allocator_(local_allocator),
69 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
70 current_block_(nullptr),
71 current_locals_(nullptr),
72 latest_result_(nullptr),
73 current_this_parameter_(nullptr),
Vladimir Marko3b506202018-10-31 14:33:58 +000074 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
75 class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080076 loop_headers_.reserve(kDefaultNumberOfLoops);
77}
78
David Brazdildee58d62016-04-07 09:54:26 +000079HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
80 return block_builder_->GetBlockAt(dex_pc);
81}
82
Vladimir Marko69d310e2017-10-09 14:12:23 +010083inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
84 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000085 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080086 if (locals->size() == vregs) {
87 return locals;
88 }
89 return GetLocalsForWithAllocation(block, locals, vregs);
90}
David Brazdildee58d62016-04-07 09:54:26 +000091
Vladimir Marko69d310e2017-10-09 14:12:23 +010092ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080093 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010094 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080095 const size_t vregs) {
96 DCHECK_NE(locals->size(), vregs);
97 locals->resize(vregs, nullptr);
98 if (block->IsCatchBlock()) {
99 // We record incoming inputs of catch phis at throwing instructions and
100 // must therefore eagerly create the phis. Phis for undefined vregs will
101 // be deleted when the first throwing instruction with the vreg undefined
102 // is encountered. Unused phis will be removed by dead phi analysis.
103 for (size_t i = 0; i < vregs; ++i) {
104 // No point in creating the catch phi if it is already undefined at
105 // the first throwing instruction.
106 HInstruction* current_local_value = (*current_locals_)[i];
107 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100108 HPhi* phi = new (allocator_) HPhi(
109 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800110 i,
111 0,
112 current_local_value->GetType());
113 block->AddPhi(phi);
114 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000115 }
116 }
117 }
118 return locals;
119}
120
Mingyao Yang01b47b02017-02-03 12:09:57 -0800121inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100122 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000123 return (*locals)[local];
124}
125
126void HInstructionBuilder::InitializeBlockLocals() {
127 current_locals_ = GetLocalsFor(current_block_);
128
129 if (current_block_->IsCatchBlock()) {
130 // Catch phis were already created and inputs collected from throwing sites.
131 if (kIsDebugBuild) {
132 // Make sure there was at least one throwing instruction which initialized
133 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
134 // visited already (from HTryBoundary scoping and reverse post order).
135 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100136 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000137 if (current == current_block_) {
138 catch_block_visited = true;
139 } else if (current->IsTryBlock()) {
140 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
141 if (try_entry.HasExceptionHandler(*current_block_)) {
142 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
143 }
144 }
145 }
146 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
147 << "No instructions throwing into a live catch block.";
148 }
149 } else if (current_block_->IsLoopHeader()) {
150 // If the block is a loop header, we know we only have visited the pre header
151 // because we are visiting in reverse post order. We create phis for all initialized
152 // locals from the pre header. Their inputs will be populated at the end of
153 // the analysis.
154 for (size_t local = 0; local < current_locals_->size(); ++local) {
155 HInstruction* incoming =
156 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
157 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 HPhi* phi = new (allocator_) HPhi(
159 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000160 local,
161 0,
162 incoming->GetType());
163 current_block_->AddPhi(phi);
164 (*current_locals_)[local] = phi;
165 }
166 }
167
168 // Save the loop header so that the last phase of the analysis knows which
169 // blocks need to be updated.
170 loop_headers_.push_back(current_block_);
171 } else if (current_block_->GetPredecessors().size() > 0) {
172 // All predecessors have already been visited because we are visiting in reverse post order.
173 // We merge the values of all locals, creating phis if those values differ.
174 for (size_t local = 0; local < current_locals_->size(); ++local) {
175 bool one_predecessor_has_no_value = false;
176 bool is_different = false;
177 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
178
179 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
180 HInstruction* current = ValueOfLocalAt(predecessor, local);
181 if (current == nullptr) {
182 one_predecessor_has_no_value = true;
183 break;
184 } else if (current != value) {
185 is_different = true;
186 }
187 }
188
189 if (one_predecessor_has_no_value) {
190 // If one predecessor has no value for this local, we trust the verifier has
191 // successfully checked that there is a store dominating any read after this block.
192 continue;
193 }
194
195 if (is_different) {
196 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100197 HPhi* phi = new (allocator_) HPhi(
198 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000199 local,
200 current_block_->GetPredecessors().size(),
201 first_input->GetType());
202 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
203 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
204 phi->SetRawInputAt(i, pred_value);
205 }
206 current_block_->AddPhi(phi);
207 value = phi;
208 }
209 (*current_locals_)[local] = value;
210 }
211 }
212}
213
214void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
215 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
216 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100217 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000218 DCHECK_EQ(handler_locals->size(), current_locals_->size());
219 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
220 HInstruction* handler_value = (*handler_locals)[vreg];
221 if (handler_value == nullptr) {
222 // Vreg was undefined at a previously encountered throwing instruction
223 // and the catch phi was deleted. Do not record the local value.
224 continue;
225 }
226 DCHECK(handler_value->IsPhi());
227
228 HInstruction* local_value = (*current_locals_)[vreg];
229 if (local_value == nullptr) {
230 // This is the first instruction throwing into `catch_block` where
231 // `vreg` is undefined. Delete the catch phi.
232 catch_block->RemovePhi(handler_value->AsPhi());
233 (*handler_locals)[vreg] = nullptr;
234 } else {
235 // Vreg has been defined at all instructions throwing into `catch_block`
236 // encountered so far. Record the local value in the catch phi.
237 handler_value->AsPhi()->AddInput(local_value);
238 }
239 }
240 }
241}
242
243void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
244 current_block_->AddInstruction(instruction);
245 InitializeInstruction(instruction);
246}
247
248void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
249 if (current_block_->GetInstructions().IsEmpty()) {
250 current_block_->AddInstruction(instruction);
251 } else {
252 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
253 }
254 InitializeInstruction(instruction);
255}
256
257void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
258 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100259 HEnvironment* environment = new (allocator_) HEnvironment(
260 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000261 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000262 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000263 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000264 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100265 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000266 instruction->SetRawEnvironment(environment);
267 }
268}
269
David Brazdilc120bbe2016-04-22 16:57:00 +0100270HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100271 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100272 if (!ref->CanBeNull()) {
273 return ref;
274 }
275
Vladimir Markoca6fff82017-10-03 14:49:14 +0100276 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100277 AppendInstruction(null_check);
278 return null_check;
279}
280
David Brazdildee58d62016-04-07 09:54:26 +0000281void HInstructionBuilder::SetLoopHeaderPhiInputs() {
282 for (size_t i = loop_headers_.size(); i > 0; --i) {
283 HBasicBlock* block = loop_headers_[i - 1];
284 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
285 HPhi* phi = it.Current()->AsPhi();
286 size_t vreg = phi->GetRegNumber();
287 for (HBasicBlock* predecessor : block->GetPredecessors()) {
288 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
289 if (value == nullptr) {
290 // Vreg is undefined at this predecessor. Mark it dead and leave with
291 // fewer inputs than predecessors. SsaChecker will fail if not removed.
292 phi->SetDead();
293 break;
294 } else {
295 phi->AddInput(value);
296 }
297 }
298 }
299 }
300}
301
302static bool IsBlockPopulated(HBasicBlock* block) {
303 if (block->IsLoopHeader()) {
304 // Suspend checks were inserted into loop headers during building of dominator tree.
305 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
306 return block->GetFirstInstruction() != block->GetLastInstruction();
307 } else {
308 return !block->GetInstructions().IsEmpty();
309 }
310}
311
312bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800313 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100314 locals_for_.resize(
315 graph_->GetBlocks().size(),
316 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000317
318 // Find locations where we want to generate extra stackmaps for native debugging.
319 // This allows us to generate the info only at interesting points (for example,
320 // at start of java statement) rather than before every dex instruction.
Vladimir Marko3b506202018-10-31 14:33:58 +0000321 const bool native_debuggable = code_generator_ != nullptr &&
322 code_generator_->GetCompilerOptions().GetNativeDebuggable();
David Brazdildee58d62016-04-07 09:54:26 +0000323 ArenaBitVector* native_debug_info_locations = nullptr;
324 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100325 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000326 }
327
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100328 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
329 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000330 uint32_t block_dex_pc = current_block_->GetDexPc();
331
332 InitializeBlockLocals();
333
334 if (current_block_->IsEntryBlock()) {
335 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100336 AppendInstruction(new (allocator_) HSuspendCheck(0u));
337 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000338 continue;
339 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100340 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000341 continue;
342 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000344 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
345 // This is slightly odd because the loop header might not be empty (TryBoundary).
346 // But we're still creating the environment with locals from the top of the block.
347 InsertInstructionAtTop(suspend_check);
348 }
349
350 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
351 // Synthetic block that does not need to be populated.
352 DCHECK(IsBlockPopulated(current_block_));
353 continue;
354 }
355
356 DCHECK(!IsBlockPopulated(current_block_));
357
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700358 uint32_t quicken_index = 0;
359 if (CanDecodeQuickenedInfo()) {
360 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
361 }
362
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800363 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000364 if (current_block_ == nullptr) {
365 // The previous instruction ended this block.
366 break;
367 }
368
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800369 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000370 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
371 // This dex_pc starts a new basic block.
372 break;
373 }
374
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800375 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000376 PropagateLocalsToCatchBlocks();
377 }
378
379 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100380 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000381 }
382
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800383 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000384 return false;
385 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700386
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800387 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700388 ++quicken_index;
389 }
David Brazdildee58d62016-04-07 09:54:26 +0000390 }
391
392 if (current_block_ != nullptr) {
393 // Branching instructions clear current_block, so we know the last
394 // instruction of the current block is not a branching instruction.
395 // We add an unconditional Goto to the next block.
396 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100397 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000398 }
399 }
400
401 SetLoopHeaderPhiInputs();
402
403 return true;
404}
405
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000406void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800407 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000408 DCHECK(method->IsIntrinsic());
409
410 locals_for_.resize(
411 graph_->GetBlocks().size(),
412 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
413
414 // Fill the entry block. Do not add suspend check, we do not want a suspend
415 // check in intrinsics; intrinsic methods are supposed to be fast.
416 current_block_ = graph_->GetEntryBlock();
417 InitializeBlockLocals();
418 InitializeParameters();
419 AppendInstruction(new (allocator_) HGoto(0u));
420
421 // Fill the body.
422 current_block_ = current_block_->GetSingleSuccessor();
423 InitializeBlockLocals();
424 DCHECK(!IsBlockPopulated(current_block_));
425
426 // Add the invoke and return instruction. Use HInvokeStaticOrDirect even
427 // for methods that would normally use an HInvokeVirtual (sharpen the call).
428 size_t in_vregs = graph_->GetNumberOfInVRegs();
429 size_t number_of_arguments =
430 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
431 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
432 MethodReference target_method(dex_file_, method_idx);
433 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
434 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
435 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
436 /* method_load_data */ 0u
437 };
438 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
439 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
440 allocator_,
441 number_of_arguments,
442 return_type_,
443 kNoDexPc,
444 method_idx,
445 method,
446 dispatch_info,
447 invoke_type,
448 target_method,
449 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000450 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100451 HandleInvoke(invoke, operands, dex_file_->GetMethodShorty(method_idx), /* is_unresolved */ false);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000452
453 // Add the return instruction.
454 if (return_type_ == DataType::Type::kVoid) {
455 AppendInstruction(new (allocator_) HReturnVoid());
456 } else {
457 AppendInstruction(new (allocator_) HReturn(invoke));
458 }
459
460 // Fill the exit block.
461 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
462 current_block_ = graph_->GetExitBlock();
463 InitializeBlockLocals();
464 AppendInstruction(new (allocator_) HExit());
465}
466
Vladimir Marko69d310e2017-10-09 14:12:23 +0100467ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100468 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800469 code_item_accessor_.InsnsSizeInCodeUnits(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100470 /* expandable */ false,
471 kArenaAllocGraphBuilder);
472 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700473 // The visitor gets called when the line number changes.
474 // In other words, it marks the start of new java statement.
475 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
476 locations->SetBit(entry.address_);
477 return false;
478 });
David Brazdildee58d62016-04-07 09:54:26 +0000479 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800480 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800481 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000482 case Instruction::MOVE_EXCEPTION: {
483 // Stop in native debugger after the exception has been moved.
484 // The compiler also expects the move at the start of basic block so
485 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800486 locations->ClearBit(inst.DexPc());
487 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
488 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800489 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700490 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000491 }
492 break;
493 }
494 default:
495 break;
496 }
497 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100498 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000499}
500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100501HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000502 HInstruction* value = (*current_locals_)[reg_number];
503 DCHECK(value != nullptr);
504
505 // If the operation requests a specific type, we make sure its input is of that type.
506 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100507 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700508 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100509 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700510 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000511 }
Aart Bik31883642016-06-06 15:02:44 -0700512 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000513 }
514
515 return value;
516}
517
518void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100519 DataType::Type stored_type = stored_value->GetType();
520 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000521
522 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
523 // registers. Consider the following cases:
524 // (1) Storing a wide value must overwrite previous values in both `reg_number`
525 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
526 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
527 // must invalidate it. We store `nullptr` in `reg_number-1`.
528 // Consequently, storing a wide value into the high vreg of another wide value
529 // will invalidate both `reg_number-1` and `reg_number+1`.
530
531 if (reg_number != 0) {
532 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100533 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000534 // The vreg we are storing into was previously the high vreg of a pair.
535 // We need to invalidate its low vreg.
536 DCHECK((*current_locals_)[reg_number] == nullptr);
537 (*current_locals_)[reg_number - 1] = nullptr;
538 }
539 }
540
541 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100542 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000543 // We are storing a pair. Invalidate the instruction in the high vreg.
544 (*current_locals_)[reg_number + 1] = nullptr;
545 }
546}
547
548void HInstructionBuilder::InitializeParameters() {
549 DCHECK(current_block_->IsEntryBlock());
550
Vladimir Marko69d310e2017-10-09 14:12:23 +0100551 // outer_compilation_unit_ is null only when unit testing.
552 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000553 return;
554 }
555
556 const char* shorty = dex_compilation_unit_->GetShorty();
557 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
558 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
559 uint16_t parameter_index = 0;
560
561 const DexFile::MethodId& referrer_method_id =
562 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
563 if (!dex_compilation_unit_->IsStatic()) {
564 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100565 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000566 referrer_method_id.class_idx_,
567 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100568 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700569 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000570 AppendInstruction(parameter);
571 UpdateLocal(locals_index++, parameter);
572 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700573 current_this_parameter_ = parameter;
574 } else {
575 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000576 }
577
578 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
579 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
580 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100581 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000582 *dex_file_,
583 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
584 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100585 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700586 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000587 ++shorty_pos;
588 AppendInstruction(parameter);
589 // Store the parameter value in the local that the dex code will use
590 // to reference that parameter.
591 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100592 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000593 i++;
594 locals_index++;
595 parameter_index++;
596 }
597 }
598}
599
600template<typename T>
601void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100602 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
603 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100604 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000605 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100606 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000607 current_block_ = nullptr;
608}
609
610template<typename T>
611void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100612 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100613 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000614 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100615 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000616 current_block_ = nullptr;
617}
618
619template<typename T>
620void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100621 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000622 uint32_t dex_pc) {
623 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100624 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000625 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
626}
627
628void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100629 DataType::Type input_type,
630 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000631 uint32_t dex_pc) {
632 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000634 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
635}
636
637template<typename T>
638void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100639 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000640 uint32_t dex_pc) {
641 HInstruction* first = LoadLocal(instruction.VRegB(), type);
642 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100643 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000644 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
645}
646
647template<typename T>
648void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100649 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000650 uint32_t dex_pc) {
651 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100652 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000654 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
655}
656
657void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100658 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000659 ComparisonBias bias,
660 uint32_t dex_pc) {
661 HInstruction* first = LoadLocal(instruction.VRegB(), type);
662 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100663 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000664 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
665}
666
667template<typename T>
668void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100669 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000670 uint32_t dex_pc) {
671 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100672 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100673 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000674 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
675}
676
677template<typename T>
678void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100679 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000680 uint32_t dex_pc) {
681 HInstruction* first = LoadLocal(instruction.VRegA(), type);
682 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100683 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000684 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
685}
686
687template<typename T>
688void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100689 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000690 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
691 if (reverse) {
692 std::swap(first, second);
693 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100694 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000695 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
696}
697
698template<typename T>
699void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100700 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000701 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
702 if (reverse) {
703 std::swap(first, second);
704 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100705 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000706 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
707}
708
Igor Murashkind01745e2017-04-05 16:40:31 -0700709// Does the method being compiled need any constructor barriers being inserted?
710// (Always 'false' for methods that aren't <init>.)
Vladimir Markoc1c34522018-10-31 13:56:49 +0000711static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700712 // Can be null in unit tests only.
713 if (UNLIKELY(cu == nullptr)) {
714 return false;
715 }
716
Vladimir Markoc1c34522018-10-31 13:56:49 +0000717 // Constructor barriers are applicable only for <init> methods.
718 if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
719 return false;
720 }
721
722 return cu->RequiresConstructorBarrier();
David Brazdildee58d62016-04-07 09:54:26 +0000723}
724
725// Returns true if `block` has only one successor which starts at the next
726// dex_pc after `instruction` at `dex_pc`.
727static bool IsFallthroughInstruction(const Instruction& instruction,
728 uint32_t dex_pc,
729 HBasicBlock* block) {
730 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
731 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
732}
733
734void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100735 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000736 DexSwitchTable table(instruction, dex_pc);
737
738 if (table.GetNumEntries() == 0) {
739 // Empty Switch. Code falls through to the next block.
740 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100741 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000742 } else if (table.ShouldBuildDecisionTree()) {
743 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
744 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100745 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000746 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100747 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000748
749 if (!it.IsLast()) {
750 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
751 }
752 }
753 } else {
754 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100755 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000756 }
757
758 current_block_ = nullptr;
759}
760
761void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100762 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000763 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100764 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700765 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700766 // This may insert additional redundant constructor fences from the super constructors.
767 // TODO: remove redundant constructor fences (b/36656456).
Vladimir Markoc1c34522018-10-31 13:56:49 +0000768 if (RequiresConstructorBarrier(dex_compilation_unit_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700769 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100770 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700771
772 HInstruction* fence_target = current_this_parameter_;
773 DCHECK(fence_target != nullptr);
774
Vladimir Markoca6fff82017-10-03 14:49:14 +0100775 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700776 MaybeRecordStat(
777 compilation_stats_,
778 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000779 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100780 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000781 } else {
Vladimir Markoc1c34522018-10-31 13:56:49 +0000782 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
David Brazdildee58d62016-04-07 09:54:26 +0000783 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100784 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000785 }
786 current_block_ = nullptr;
787}
788
789static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
790 switch (opcode) {
791 case Instruction::INVOKE_STATIC:
792 case Instruction::INVOKE_STATIC_RANGE:
793 return kStatic;
794 case Instruction::INVOKE_DIRECT:
795 case Instruction::INVOKE_DIRECT_RANGE:
796 return kDirect;
797 case Instruction::INVOKE_VIRTUAL:
798 case Instruction::INVOKE_VIRTUAL_QUICK:
799 case Instruction::INVOKE_VIRTUAL_RANGE:
800 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
801 return kVirtual;
802 case Instruction::INVOKE_INTERFACE:
803 case Instruction::INVOKE_INTERFACE_RANGE:
804 return kInterface;
805 case Instruction::INVOKE_SUPER_RANGE:
806 case Instruction::INVOKE_SUPER:
807 return kSuper;
808 default:
809 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
810 UNREACHABLE();
811 }
812}
813
814ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
815 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000816
817 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000818 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100819
Vladimir Markoba118822017-06-12 15:41:56 +0100820 ArtMethod* resolved_method =
821 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100822 method_idx,
823 dex_compilation_unit_->GetDexCache(),
824 class_loader,
825 graph_->GetArtMethod(),
826 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000827
828 if (UNLIKELY(resolved_method == nullptr)) {
829 // Clean up any exception left by type resolution.
830 soa.Self()->ClearException();
831 return nullptr;
832 }
833
Vladimir Markoba118822017-06-12 15:41:56 +0100834 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
835 // resolved because, for example, we don't find a superclass in the classpath.
836 if (graph_->GetArtMethod() == nullptr) {
837 // The class linker cannot check access without a referrer, so we have to do it.
838 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000839 if (!resolved_method->IsPublic()) {
840 return nullptr;
841 }
David Brazdildee58d62016-04-07 09:54:26 +0000842 }
843
844 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
845 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
846 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
847 // which require runtime handling.
848 if (invoke_type == kSuper) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000849 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800850 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000851 // We could not determine the method's class we need to wait until runtime.
852 DCHECK(Runtime::Current()->IsAotCompiler());
853 return nullptr;
854 }
Vladimir Markoba118822017-06-12 15:41:56 +0100855 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100856 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
857 dex_compilation_unit_->GetDexCache().Get(),
858 class_loader.Get());
859 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
860 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700861 // We cannot statically determine the target method. The runtime will throw a
862 // NoSuchMethodError on this one.
863 return nullptr;
864 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100865 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100866 if (referenced_class->IsInterface()) {
867 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100868 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000869 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100870 uint16_t vtable_index = resolved_method->GetMethodIndex();
871 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
872 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000873 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100874 if (actual_method != resolved_method &&
875 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
876 // The back-end code generator relies on this check in order to ensure that it will not
877 // attempt to read the dex_cache with a dex_method_index that is not from the correct
878 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
Vladimir Markoc1c34522018-10-31 13:56:49 +0000879 // builder, which means that the code-generator (and sharpening and inliner, maybe)
880 // might invoke an incorrect method.
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100881 // TODO: The actual method could still be referenced in the current dex file, so we
882 // could try locating it.
883 // TODO: Remove the dex_file restriction.
884 return nullptr;
885 }
886 if (!actual_method->IsInvokable()) {
887 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
888 // could resolve the callee to the wrong method.
889 return nullptr;
890 }
891 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000892 }
893
David Brazdildee58d62016-04-07 09:54:26 +0000894 return resolved_method;
895}
896
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100897static bool IsStringConstructor(ArtMethod* method) {
898 ScopedObjectAccess soa(Thread::Current());
899 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
900}
901
David Brazdildee58d62016-04-07 09:54:26 +0000902bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
903 uint32_t dex_pc,
904 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000905 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000906 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100907 const char* shorty = dex_file_->GetMethodShorty(method_idx);
908 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000909
910 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100911 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +0000912 if (invoke_type != kStatic) { // instance call
913 // One extra argument for 'this'.
914 number_of_arguments++;
915 }
916
David Brazdildee58d62016-04-07 09:54:26 +0000917 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
918
919 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700920 MaybeRecordStat(compilation_stats_,
921 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100922 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
923 number_of_arguments,
924 return_type,
925 dex_pc,
926 method_idx,
927 invoke_type);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100928 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000929 }
930
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100931 // Replace calls to String.<init> with StringFactory.
932 if (IsStringConstructor(resolved_method)) {
933 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
934 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
935 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
936 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000937 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100938 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000939 ScopedObjectAccess soa(Thread::Current());
940 MethodReference target_method(resolved_method->GetDexFile(),
941 resolved_method->GetDexMethodIndex());
942 // We pass null for the resolved_method to ensure optimizations
943 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100944 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
945 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100946 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100947 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100948 dex_pc,
949 method_idx,
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000950 nullptr /* resolved_method */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100951 dispatch_info,
952 invoke_type,
953 target_method,
954 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100955 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100956 }
957
David Brazdildee58d62016-04-07 09:54:26 +0000958 // Potential class initialization check, in the case of a static method call.
959 HClinitCheck* clinit_check = nullptr;
960 HInvoke* invoke = nullptr;
961 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
962 // By default, consider that the called method implicitly requires
963 // an initialization check of its declaring method.
964 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
965 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
966 ScopedObjectAccess soa(Thread::Current());
967 if (invoke_type == kStatic) {
Vladimir Markofca0b492018-07-23 15:30:52 +0100968 clinit_check =
Vladimir Markoa2c211c2018-11-01 09:50:52 +0000969 ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000970 } else if (invoke_type == kSuper) {
971 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100972 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000973 // we resolved to the method referenced by the instruction.
974 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000975 }
976 }
977
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100978 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
979 HSharpening::SharpenInvokeStaticOrDirect(resolved_method, code_generator_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100980 MethodReference target_method(resolved_method->GetDexFile(),
981 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100982 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
983 number_of_arguments,
984 return_type,
985 dex_pc,
986 method_idx,
987 resolved_method,
988 dispatch_info,
989 invoke_type,
990 target_method,
991 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000992 } else if (invoke_type == kVirtual) {
993 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100994 invoke = new (allocator_) HInvokeVirtual(allocator_,
995 number_of_arguments,
996 return_type,
997 dex_pc,
998 method_idx,
999 resolved_method,
1000 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +00001001 } else {
1002 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001003 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001004 invoke = new (allocator_) HInvokeInterface(allocator_,
1005 number_of_arguments,
1006 return_type,
1007 dex_pc,
1008 method_idx,
1009 resolved_method,
1010 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001011 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001012 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false, clinit_check);
David Brazdildee58d62016-04-07 09:54:26 +00001013}
1014
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001015bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001016 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001017 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001018 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001019 const char* shorty = dex_file_->GetShorty(proto_idx);
1020 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1021 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1022 size_t number_of_arguments = strlen(shorty);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1024 number_of_arguments,
1025 return_type,
1026 dex_pc,
1027 method_idx);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001028 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false);
1029}
1030
1031
1032bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1033 uint32_t call_site_idx,
1034 const InstructionOperands& operands) {
1035 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1036 const char* shorty = dex_file_->GetShorty(proto_idx);
1037 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1038 size_t number_of_arguments = strlen(shorty) - 1;
1039 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1040 number_of_arguments,
1041 call_site_idx,
1042 return_type,
1043 dex_pc);
1044 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001045}
1046
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001047HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001048 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001049
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001050 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001051
David Brazdildee58d62016-04-07 09:54:26 +00001052 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001053 Handle<mirror::Class> klass = load_class->GetClass();
1054
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001055 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001056 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001057 AppendInstruction(cls);
1058 }
1059
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001060 // Only the access check entrypoint handles the finalizable class case. If we
1061 // need access checks, then we haven't resolved the method and the class may
1062 // again be finalizable.
1063 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1064 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1065 entrypoint = kQuickAllocObjectWithChecks;
1066 }
Alex Lightd109e302018-06-27 10:25:41 -07001067 // We will always be able to resolve the string class since it is in the BCP.
1068 if (!klass.IsNull() && klass->IsStringClass()) {
1069 entrypoint = kQuickAllocStringObject;
1070 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001071
1072 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001073 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001074
Vladimir Markoca6fff82017-10-03 14:49:14 +01001075 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001076 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001077 dex_pc,
1078 type_index,
1079 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001080 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001081 entrypoint);
1082 AppendInstruction(new_instance);
1083
1084 return new_instance;
1085}
1086
1087void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1088 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001089 (allocation->IsNewInstance() ||
1090 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001091
1092 if (allocation->IsNewInstance()) {
1093 // STRING SPECIAL HANDLING:
1094 // -------------------------------
1095 // Strings have a real HNewInstance node but they end up always having 0 uses.
1096 // All uses of a String HNewInstance are always transformed to replace their input
1097 // of the HNewInstance with an input of the invoke to StringFactory.
1098 //
1099 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1100 // optimizations (to pass checker tests that rely on those optimizations).
1101 HNewInstance* new_inst = allocation->AsNewInstance();
1102 HLoadClass* load_class = new_inst->GetLoadClass();
1103
1104 Thread* self = Thread::Current();
1105 ScopedObjectAccess soa(self);
1106 StackHandleScope<1> hs(self);
1107 Handle<mirror::Class> klass = load_class->GetClass();
1108 if (klass != nullptr && klass->IsStringClass()) {
1109 return;
1110 // Note: Do not use allocation->IsStringAlloc which requires
1111 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1112 // propagation (and instruction builder is too early).
1113 }
1114 // (In terms of correctness, the StringFactory needs to provide its own
1115 // default initialization barrier, see below.)
1116 }
1117
1118 // JLS 17.4.5 "Happens-before Order" describes:
1119 //
1120 // The default initialization of any object happens-before any other actions (other than
1121 // default-writes) of a program.
1122 //
1123 // In our implementation the default initialization of an object to type T means
1124 // setting all of its initial data (object[0..size)) to 0, and setting the
1125 // object's class header (i.e. object.getClass() == T.class).
1126 //
1127 // In practice this fence ensures that the writes to the object header
1128 // are visible to other threads if this object escapes the current thread.
1129 // (and in theory the 0-initializing, but that happens automatically
1130 // when new memory pages are mapped in by the OS).
1131 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001132 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001133 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001134 MaybeRecordStat(
1135 compilation_stats_,
1136 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001137}
1138
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001139static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1140 REQUIRES_SHARED(Locks::mutator_lock_) {
1141 if (compiler_options.IsBootImage()) {
1142 std::string temp;
1143 const char* descriptor = cls->GetDescriptor(&temp);
1144 return compiler_options.IsImageClass(descriptor);
1145 } else {
1146 return Runtime::Current()->GetHeap()->FindSpaceFromObject(cls, false)->IsImageSpace();
1147 }
1148}
1149
1150static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1151 REQUIRES_SHARED(Locks::mutator_lock_) {
1152 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1153}
1154
1155static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1156 REQUIRES_SHARED(Locks::mutator_lock_) {
1157 // Check if the class has encoded fields that trigger bytecode execution.
1158 // (Encoded fields are just a different representation of <clinit>.)
1159 if (klass->NumStaticFields() != 0u) {
1160 DCHECK(klass->GetClassDef() != nullptr);
1161 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1162 for (; it.HasNext(); it.Next()) {
1163 switch (it.GetValueType()) {
1164 case EncodedArrayValueIterator::ValueType::kBoolean:
1165 case EncodedArrayValueIterator::ValueType::kByte:
1166 case EncodedArrayValueIterator::ValueType::kShort:
1167 case EncodedArrayValueIterator::ValueType::kChar:
1168 case EncodedArrayValueIterator::ValueType::kInt:
1169 case EncodedArrayValueIterator::ValueType::kLong:
1170 case EncodedArrayValueIterator::ValueType::kFloat:
1171 case EncodedArrayValueIterator::ValueType::kDouble:
1172 case EncodedArrayValueIterator::ValueType::kNull:
1173 case EncodedArrayValueIterator::ValueType::kString:
1174 // Primitive, null or j.l.String initialization is permitted.
1175 break;
1176 case EncodedArrayValueIterator::ValueType::kType:
1177 // Type initialization can load classes and execute bytecode through a class loader
1178 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1179 // kType is rarely used (if ever).
1180 return false;
1181 default:
1182 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1183 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1184 UNREACHABLE();
1185 }
1186 }
1187 }
1188 // Check if the class has <clinit> that executes arbitrary code.
1189 // Initialization of static fields of the class itself with constants is allowed.
1190 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1191 if (clinit != nullptr) {
1192 const DexFile& dex_file = *clinit->GetDexFile();
1193 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1194 for (DexInstructionPcPair it : accessor) {
1195 switch (it->Opcode()) {
1196 case Instruction::CONST_4:
1197 case Instruction::CONST_16:
1198 case Instruction::CONST:
1199 case Instruction::CONST_HIGH16:
1200 case Instruction::CONST_WIDE_16:
1201 case Instruction::CONST_WIDE_32:
1202 case Instruction::CONST_WIDE:
1203 case Instruction::CONST_WIDE_HIGH16:
1204 case Instruction::CONST_STRING:
1205 case Instruction::CONST_STRING_JUMBO:
1206 // Primitive, null or j.l.String initialization is permitted.
1207 break;
1208 case Instruction::RETURN_VOID:
1209 case Instruction::RETURN_VOID_NO_BARRIER:
1210 break;
1211 case Instruction::SPUT:
1212 case Instruction::SPUT_WIDE:
1213 case Instruction::SPUT_OBJECT:
1214 case Instruction::SPUT_BOOLEAN:
1215 case Instruction::SPUT_BYTE:
1216 case Instruction::SPUT_CHAR:
1217 case Instruction::SPUT_SHORT:
1218 // Only initialization of a static field of the same class is permitted.
1219 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1220 return false;
1221 }
1222 break;
1223 case Instruction::NEW_ARRAY:
1224 // Only primitive arrays are permitted.
1225 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1226 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1227 return false;
1228 }
1229 break;
1230 case Instruction::APUT:
1231 case Instruction::APUT_WIDE:
1232 case Instruction::APUT_BOOLEAN:
1233 case Instruction::APUT_BYTE:
1234 case Instruction::APUT_CHAR:
1235 case Instruction::APUT_SHORT:
1236 case Instruction::FILL_ARRAY_DATA:
1237 case Instruction::NOP:
1238 // Allow initialization of primitive arrays (only constants can be stored).
1239 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1240 // (even unreferenced switch payloads if they make it through the verifier).
1241 break;
1242 default:
1243 return false;
1244 }
1245 }
1246 }
1247 return true;
1248}
1249
1250static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1251 const CompilerOptions& compiler_options)
1252 REQUIRES_SHARED(Locks::mutator_lock_) {
1253 Runtime* runtime = Runtime::Current();
1254 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1255
1256 // Check the superclass chain.
1257 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1258 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1259 break; // `klass` and its superclasses are already initialized in the boot image.
1260 }
1261 if (!HasTrivialClinit(klass, pointer_size)) {
1262 return false;
1263 }
1264 }
1265
1266 // Also check interfaces with default methods as they need to be initialized as well.
1267 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1268 DCHECK(iftable != nullptr);
1269 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1270 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1271 if (!iface->HasDefaultMethods()) {
1272 continue; // Initializing `cls` does not initialize this interface.
1273 }
1274 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1275 continue; // This interface is already initialized in the boot image.
1276 }
1277 if (!HasTrivialClinit(iface, pointer_size)) {
1278 return false;
1279 }
1280 }
1281 return true;
1282}
1283
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001284bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001285 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001286 return false;
1287 }
1288
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001289 // Check if the class will be initialized at runtime.
1290 if (cls->IsInitialized()) {
1291 Runtime* runtime = Runtime::Current();
1292 if (!runtime->IsAotCompiler()) {
1293 DCHECK(runtime->UseJitCompilation());
1294 // For JIT, the class cannot revert to an uninitialized state.
1295 return true;
1296 }
1297 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1298 // loaded because we don't even know what class loader will be used to load them.
Vladimir Marko3b506202018-10-31 14:33:58 +00001299 if (IsInBootImage(cls.Get(), code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001300 return true;
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001301 }
1302 }
1303
Vladimir Marko7f260d42018-10-30 18:09:55 +00001304 // We can avoid the class initialization check for `cls` in static methods and constructors
1305 // in the very same class; invoking a static method involves a class initialization check
1306 // and so does the instance allocation that must be executed before invoking a constructor.
1307 // Other instance methods of the same class can run on an escaped instance
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001308 // of an erroneous class. Even a superclass may need to be checked as the subclass
1309 // can be completely initialized while the superclass is initializing and the subclass
1310 // remains initialized when the superclass initializer throws afterwards. b/62478025
1311 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001312 auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
1313 REQUIRES_SHARED(Locks::mutator_lock_) {
1314 return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
1315 compilation_unit.GetCompilingClass().Get() == cls.Get();
1316 };
1317 if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
1318 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1319 // eliminated by GVN, that happens only after the decision whether to inline the
1320 // graph or not and that may depend on the presence of the ClinitCheck.
1321 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1322 // information to the builder.
1323 is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001324 return true;
1325 }
David Brazdildee58d62016-04-07 09:54:26 +00001326
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001327 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1328 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1329 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1330 // instance methods require an instance which cannot be created before doing a clinit check.
1331 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1332 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1333 // initialization weirdness.
1334 //
1335 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1336 // with default methods initialize only their own static fields using constant values), it must
1337 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1338 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1339 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1340 // bytecode shall execute (see above), therefore the instruction we're building shall be
1341 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1342 //
1343 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1344 // information to the builder. (We could also check if we're guaranteed a non-null instance
1345 // of `cls` at this location but that's outside the scope of the instruction builder.)
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001346 bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls.Get());
1347 if (dex_compilation_unit_ != outer_compilation_unit_) {
1348 is_subclass = is_subclass ||
1349 IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls.Get());
1350 }
Vladimir Marko3b506202018-10-31 14:33:58 +00001351 if (is_subclass && HasTrivialInitialization(cls.Get(), code_generator_->GetCompilerOptions())) {
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001352 return true;
1353 }
David Brazdildee58d62016-04-07 09:54:26 +00001354
1355 return false;
1356}
1357
1358HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001359 uint32_t dex_pc,
1360 ArtMethod* resolved_method,
1361 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001362 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001363
1364 HClinitCheck* clinit_check = nullptr;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001365 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001366 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001367 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001368 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001369 klass->GetDexFile(),
1370 klass,
1371 dex_pc,
1372 /* needs_access_check */ false);
1373 if (cls != nullptr) {
1374 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001375 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001376 AppendInstruction(clinit_check);
1377 }
David Brazdildee58d62016-04-07 09:54:26 +00001378 }
1379 return clinit_check;
1380}
1381
1382bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001383 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001384 const char* shorty,
David Brazdildee58d62016-04-07 09:54:26 +00001385 size_t start_index,
1386 size_t* argument_index) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001387 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001388 const size_t number_of_operands = operands.GetNumberOfOperands();
David Brazdildee58d62016-04-07 09:54:26 +00001389 for (size_t i = start_index;
1390 // Make sure we don't go over the expected arguments or over the number of
1391 // dex registers given. If the instruction was seen as dead by the verifier,
1392 // it hasn't been properly checked.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001393 (i < number_of_operands) && (*argument_index < invoke->GetNumberOfArguments());
David Brazdildee58d62016-04-07 09:54:26 +00001394 i++, (*argument_index)++) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001395 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001396 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001397 if (is_wide && ((i + 1 == number_of_operands) ||
1398 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
David Brazdildee58d62016-04-07 09:54:26 +00001399 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1400 // reject any class where this is violated. However, the verifier only does these checks
1401 // on non trivially dead instructions, so we just bailout the compilation.
1402 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001403 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001404 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001405 MaybeRecordStat(compilation_stats_,
1406 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001407 return false;
1408 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001409 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001410 invoke->SetArgumentAt(*argument_index, arg);
1411 if (is_wide) {
1412 i++;
1413 }
1414 }
1415
1416 if (*argument_index != invoke->GetNumberOfArguments()) {
1417 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001418 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001419 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001420 MaybeRecordStat(compilation_stats_,
1421 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001422 return false;
1423 }
1424
1425 if (invoke->IsInvokeStaticOrDirect() &&
1426 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1427 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1428 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1429 (*argument_index)++;
1430 }
1431
1432 return true;
1433}
1434
1435bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001436 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001437 const char* shorty,
1438 bool is_unresolved,
1439 HClinitCheck* clinit_check) {
David Brazdildee58d62016-04-07 09:54:26 +00001440 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1441
1442 size_t start_index = 0;
1443 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001444 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001445 uint32_t obj_reg = operands.GetOperand(0);
Aart Bik296fbb42016-06-07 13:49:12 -07001446 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001448 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001449 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001450 start_index = 1;
1451 argument_index = 1;
1452 }
1453
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001454 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001455 return false;
1456 }
1457
1458 if (clinit_check != nullptr) {
1459 // Add the class initialization check as last input of `invoke`.
1460 DCHECK(invoke->IsInvokeStaticOrDirect());
1461 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1462 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1463 invoke->SetArgumentAt(argument_index, clinit_check);
1464 argument_index++;
1465 }
1466
1467 AppendInstruction(invoke);
1468 latest_result_ = invoke;
1469
1470 return true;
1471}
1472
1473bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001474 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001475 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001476 DCHECK(invoke->IsInvokeStaticOrDirect());
1477 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1478
1479 size_t start_index = 1;
1480 size_t argument_index = 0;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001481 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001482 return false;
1483 }
1484
1485 AppendInstruction(invoke);
1486
1487 // This is a StringFactory call, not an actual String constructor. Its result
1488 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001489 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001490 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001491
1492 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001493 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001494 if (arg_this->IsNewInstance()) {
1495 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001496 } else {
1497 DCHECK(arg_this->IsPhi());
1498 // We can get a phi as input of a String.<init> if there is a loop between the
1499 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001500 // with `arg_this`, we keep a record of those invocations so we can later replace
1501 // the allocation with the invocation.
1502 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1503 // The input will be removed during the analysis.
1504 invoke->AddInput(arg_this);
1505 ssa_builder_->AddUninitializedStringPhi(invoke);
1506 }
1507 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1508 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1509 if ((*current_locals_)[vreg] == arg_this) {
1510 (*current_locals_)[vreg] = invoke;
1511 }
David Brazdildee58d62016-04-07 09:54:26 +00001512 }
David Brazdildee58d62016-04-07 09:54:26 +00001513 return true;
1514}
1515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001516static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001517 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1518 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001519 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001520}
1521
1522bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1523 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001524 bool is_put,
1525 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001526 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1527 uint32_t obj_reg = instruction.VRegB_22c();
1528 uint16_t field_index;
1529 if (instruction.IsQuickened()) {
1530 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001531 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1532 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001533 return false;
1534 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001535 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001536 } else {
1537 field_index = instruction.VRegC_22c();
1538 }
1539
1540 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001541 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001542
Aart Bik14154132016-06-02 17:53:58 -07001543 // Generate an explicit null check on the reference, unless the field access
1544 // is unresolved. In that case, we rely on the runtime to perform various
1545 // checks first, followed by a null check.
1546 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001547 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001548 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001550 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001551 if (is_put) {
1552 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1553 HInstruction* field_set = nullptr;
1554 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001555 MaybeRecordStat(compilation_stats_,
1556 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001557 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1558 value,
1559 field_type,
1560 field_index,
1561 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001562 } else {
1563 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001564 field_set = new (allocator_) HInstanceFieldSet(object,
1565 value,
1566 resolved_field,
1567 field_type,
1568 resolved_field->GetOffset(),
1569 resolved_field->IsVolatile(),
1570 field_index,
1571 class_def_index,
1572 *dex_file_,
1573 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001574 }
1575 AppendInstruction(field_set);
1576 } else {
1577 HInstruction* field_get = nullptr;
1578 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001579 MaybeRecordStat(compilation_stats_,
1580 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001581 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1582 field_type,
1583 field_index,
1584 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001585 } else {
1586 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001587 field_get = new (allocator_) HInstanceFieldGet(object,
1588 resolved_field,
1589 field_type,
1590 resolved_field->GetOffset(),
1591 resolved_field->IsVolatile(),
1592 field_index,
1593 class_def_index,
1594 *dex_file_,
1595 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001596 }
1597 AppendInstruction(field_get);
1598 UpdateLocal(source_or_dest_reg, field_get);
1599 }
1600
1601 return true;
1602}
1603
David Brazdildee58d62016-04-07 09:54:26 +00001604void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001605 uint32_t dex_pc,
1606 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001608 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1609 uint16_t field_index = instruction.VRegB_21c();
1610
1611 if (is_put) {
1612 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1613 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001614 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001615 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001616 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001617 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1618 }
1619}
1620
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001621ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1622 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001623
1624 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001625 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001626
Vladimir Markoe11dd502017-12-08 14:09:45 +00001627 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001628 dex_compilation_unit_->GetDexCache(),
1629 class_loader,
1630 is_static);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001631 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001632 if (UNLIKELY(resolved_field == nullptr)) {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001633 // Clean up any exception left by field resolution.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001634 soa.Self()->ClearException();
1635 return nullptr;
1636 }
1637
1638 // Check static/instance. The class linker has a fast path for looking into the dex cache
1639 // and does not check static/instance if it hits it.
1640 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1641 return nullptr;
1642 }
1643
1644 // Check access.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001645 Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -08001646 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001647 if (!resolved_field->IsPublic()) {
1648 return nullptr;
1649 }
1650 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1651 resolved_field,
1652 dex_compilation_unit_->GetDexCache().Get(),
1653 field_idx)) {
1654 return nullptr;
1655 }
1656
1657 if (is_put &&
1658 resolved_field->IsFinal() &&
1659 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1660 // Final fields can only be updated within their own class.
1661 // TODO: Only allow it in constructors. b/34966607.
1662 return nullptr;
1663 }
1664
1665 return resolved_field;
1666}
1667
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001668void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001669 uint32_t dex_pc,
1670 bool is_put) {
1671 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1672 uint16_t field_index = instruction.VRegB_21c();
1673
1674 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001675 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001676
1677 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001678 MaybeRecordStat(compilation_stats_,
1679 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001681 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001682 return;
David Brazdildee58d62016-04-07 09:54:26 +00001683 }
1684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001685 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001686
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001687 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001688 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001689 klass->GetDexFile(),
1690 klass,
1691 dex_pc,
1692 /* needs_access_check */ false);
1693
1694 if (constant == nullptr) {
1695 // The class cannot be referenced from this compiled code. Generate
1696 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001697 MaybeRecordStat(compilation_stats_,
1698 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001699 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001700 return;
David Brazdildee58d62016-04-07 09:54:26 +00001701 }
1702
David Brazdildee58d62016-04-07 09:54:26 +00001703 HInstruction* cls = constant;
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001704 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001705 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001706 AppendInstruction(cls);
1707 }
1708
1709 uint16_t class_def_index = klass->GetDexClassDefIndex();
1710 if (is_put) {
1711 // We need to keep the class alive before loading the value.
1712 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1713 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001714 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1715 value,
1716 resolved_field,
1717 field_type,
1718 resolved_field->GetOffset(),
1719 resolved_field->IsVolatile(),
1720 field_index,
1721 class_def_index,
1722 *dex_file_,
1723 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001724 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1726 resolved_field,
1727 field_type,
1728 resolved_field->GetOffset(),
1729 resolved_field->IsVolatile(),
1730 field_index,
1731 class_def_index,
1732 *dex_file_,
1733 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001734 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1735 }
David Brazdildee58d62016-04-07 09:54:26 +00001736}
1737
1738void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001739 uint16_t first_vreg,
1740 int64_t second_vreg_or_constant,
1741 uint32_t dex_pc,
1742 DataType::Type type,
1743 bool second_is_constant,
1744 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001746
1747 HInstruction* first = LoadLocal(first_vreg, type);
1748 HInstruction* second = nullptr;
1749 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001751 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1752 } else {
1753 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1754 }
1755 } else {
1756 second = LoadLocal(second_vreg_or_constant, type);
1757 }
1758
1759 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1761 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001762 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001763 AppendInstruction(second);
1764 }
1765
1766 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001767 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001768 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001769 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001770 }
1771 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1772}
1773
1774void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1775 uint32_t dex_pc,
1776 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001777 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001778 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1779 uint8_t array_reg = instruction.VRegB_23x();
1780 uint8_t index_reg = instruction.VRegC_23x();
1781
David Brazdilc120bbe2016-04-22 16:57:00 +01001782 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001783 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001784 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001786 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001787 AppendInstruction(index);
1788 if (is_put) {
1789 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1790 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001791 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001792 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1793 AppendInstruction(aset);
1794 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001795 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001796 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1797 AppendInstruction(aget);
1798 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1799 }
1800 graph_->SetHasBoundsChecks(true);
1801}
1802
Vladimir Markob5461632018-10-15 14:24:21 +01001803HNewArray* HInstructionBuilder::BuildNewArray(uint32_t dex_pc,
1804 dex::TypeIndex type_index,
1805 HInstruction* length) {
1806 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
1807
1808 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(type_index));
1809 DCHECK_EQ(descriptor[0], '[');
1810 size_t component_type_shift = Primitive::ComponentSizeShift(Primitive::GetType(descriptor[1]));
1811
1812 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc, component_type_shift);
1813 AppendInstruction(new_array);
1814 return new_array;
1815}
1816
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001817HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1818 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001819 const InstructionOperands& operands) {
1820 const size_t number_of_operands = operands.GetNumberOfOperands();
1821 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001822
Vladimir Markob5461632018-10-15 14:24:21 +01001823 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
David Brazdildee58d62016-04-07 09:54:26 +00001824 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1825 DCHECK_EQ(descriptor[0], '[') << descriptor;
1826 char primitive = descriptor[1];
1827 DCHECK(primitive == 'I'
1828 || primitive == 'L'
1829 || primitive == '[') << descriptor;
1830 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001832
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001833 for (size_t i = 0; i < number_of_operands; ++i) {
1834 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001835 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markob5461632018-10-15 14:24:21 +01001836 HArraySet* aset = new (allocator_) HArraySet(new_array, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001837 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1838 AppendInstruction(aset);
1839 }
Vladimir Markob5461632018-10-15 14:24:21 +01001840 latest_result_ = new_array;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001841
Vladimir Markob5461632018-10-15 14:24:21 +01001842 return new_array;
David Brazdildee58d62016-04-07 09:54:26 +00001843}
1844
1845template <typename T>
1846void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1847 const T* data,
1848 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001850 uint32_t dex_pc) {
1851 for (uint32_t i = 0; i < element_count; ++i) {
1852 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1853 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001854 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001855 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1856 AppendInstruction(aset);
1857 }
1858}
1859
1860void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001861 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001862
1863 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1864 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001865 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1866 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001867 const uint8_t* data = payload->data;
1868 uint32_t element_count = payload->element_count;
1869
Vladimir Markoc69fba22016-09-06 16:49:15 +01001870 if (element_count == 0u) {
1871 // For empty payload we emit only the null check above.
1872 return;
1873 }
1874
Vladimir Markoca6fff82017-10-03 14:49:14 +01001875 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001876 AppendInstruction(length);
1877
David Brazdildee58d62016-04-07 09:54:26 +00001878 // Implementation of this DEX instruction seems to be that the bounds check is
1879 // done before doing any stores.
1880 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001881 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001882
1883 switch (payload->element_width) {
1884 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001885 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001886 reinterpret_cast<const int8_t*>(data),
1887 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001889 dex_pc);
1890 break;
1891 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001892 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001893 reinterpret_cast<const int16_t*>(data),
1894 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001895 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001896 dex_pc);
1897 break;
1898 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001899 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001900 reinterpret_cast<const int32_t*>(data),
1901 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001902 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001903 dex_pc);
1904 break;
1905 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001906 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001907 reinterpret_cast<const int64_t*>(data),
1908 element_count,
1909 dex_pc);
1910 break;
1911 default:
1912 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1913 }
1914 graph_->SetHasBoundsChecks(true);
1915}
1916
1917void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1918 const int64_t* data,
1919 uint32_t element_count,
1920 uint32_t dex_pc) {
1921 for (uint32_t i = 0; i < element_count; ++i) {
1922 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1923 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001924 HArraySet* aset =
1925 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001926 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1927 AppendInstruction(aset);
1928 }
1929}
1930
Vladimir Marko28e012a2017-12-07 11:22:59 +00001931void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1932 HLoadString* load_string =
1933 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1934 HSharpening::ProcessLoadString(load_string,
1935 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00001936 *dex_compilation_unit_,
1937 handles_);
1938 AppendInstruction(load_string);
1939}
1940
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001941HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001942 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001943 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00001944 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001945 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
1946 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001947}
1948
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001949HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001950 const DexFile& dex_file,
1951 Handle<mirror::Class> klass,
1952 uint32_t dex_pc,
1953 bool needs_access_check) {
1954 // Try to find a reference in the compiling dex file.
1955 const DexFile* actual_dex_file = &dex_file;
1956 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1957 dex::TypeIndex local_type_index =
1958 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1959 if (local_type_index.IsValid()) {
1960 type_index = local_type_index;
1961 actual_dex_file = dex_compilation_unit_->GetDexFile();
1962 }
1963 }
1964
1965 // Note: `klass` must be from `handles_`.
Vladimir Markoa2c211c2018-11-01 09:50:52 +00001966 bool is_referrers_class =
1967 (klass != nullptr) && (outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001968 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001969 graph_->GetCurrentMethod(),
1970 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001971 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001972 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01001973 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001974 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001975 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001976
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001977 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1978 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001979 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001980
1981 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1982 // We actually cannot reference this class, we're forced to bail.
1983 return nullptr;
1984 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00001985 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001986 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001987 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001988 return load_class;
1989}
1990
Vladimir Marko175e7862018-03-27 09:03:13 +00001991Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
1992 dex::TypeIndex type_index) {
Vladimir Marko3b506202018-10-31 14:33:58 +00001993 auto it = class_cache_.find(type_index);
1994 if (it != class_cache_.end()) {
1995 return it->second;
1996 }
1997
1998 ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
1999 type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
2000 DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
2001 soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
2002
2003 Handle<mirror::Class> h_klass = handles_->NewHandle(klass);
2004 class_cache_.Put(type_index, h_klass);
2005 return h_klass;
Vladimir Marko175e7862018-03-27 09:03:13 +00002006}
2007
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002008bool HInstructionBuilder::LoadClassNeedsAccessCheck(Handle<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002009 if (klass == nullptr) {
2010 return true;
2011 } else if (klass->IsPublic()) {
2012 return false;
2013 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002014 ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
Vladimir Marko175e7862018-03-27 09:03:13 +00002015 return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
2016 }
2017}
2018
Orion Hodson06d10a72018-05-14 08:53:38 +01002019void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002020 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002021 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2022 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002023 AppendInstruction(load_method_handle);
2024}
2025
Orion Hodson06d10a72018-05-14 08:53:38 +01002026void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002027 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2028 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002029 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002030 AppendInstruction(load_method_type);
2031}
2032
David Brazdildee58d62016-04-07 09:54:26 +00002033void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2034 uint8_t destination,
2035 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002036 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002037 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002038 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00002039
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002040 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002041 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2042 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002043 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002044 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002045 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002046
2047 HInstruction* class_or_null = nullptr;
2048 HIntConstant* bitstring_path_to_root = nullptr;
2049 HIntConstant* bitstring_mask = nullptr;
2050 if (check_kind == TypeCheckKind::kBitstringCheck) {
2051 // TODO: Allow using the bitstring check also if we need an access check.
2052 DCHECK(!needs_access_check);
2053 class_or_null = graph_->GetNullConstant(dex_pc);
2054 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2055 uint32_t path_to_root =
2056 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2057 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2058 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2059 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2060 } else {
Vladimir Markoa2c211c2018-11-01 09:50:52 +00002061 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002062 }
2063 DCHECK(class_or_null != nullptr);
2064
David Brazdildee58d62016-04-07 09:54:26 +00002065 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002066 AppendInstruction(new (allocator_) HInstanceOf(object,
2067 class_or_null,
2068 check_kind,
2069 klass,
2070 dex_pc,
2071 allocator_,
2072 bitstring_path_to_root,
2073 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002074 UpdateLocal(destination, current_block_->GetLastInstruction());
2075 } else {
2076 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
2077 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2078 // which may throw. If it succeeds BoundType sets the new type of `object`
2079 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002080 AppendInstruction(
2081 new (allocator_) HCheckCast(object,
2082 class_or_null,
2083 check_kind,
2084 klass,
2085 dex_pc,
2086 allocator_,
2087 bitstring_path_to_root,
2088 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002089 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002090 UpdateLocal(reference, current_block_->GetLastInstruction());
2091 }
2092}
2093
David Brazdildee58d62016-04-07 09:54:26 +00002094bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002095 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00002096}
2097
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002098uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
2099 DCHECK(CanDecodeQuickenedInfo());
2100 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002101}
2102
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002103bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
2104 uint32_t dex_pc,
2105 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00002106 switch (instruction.Opcode()) {
2107 case Instruction::CONST_4: {
2108 int32_t register_index = instruction.VRegA();
2109 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2110 UpdateLocal(register_index, constant);
2111 break;
2112 }
2113
2114 case Instruction::CONST_16: {
2115 int32_t register_index = instruction.VRegA();
2116 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2117 UpdateLocal(register_index, constant);
2118 break;
2119 }
2120
2121 case Instruction::CONST: {
2122 int32_t register_index = instruction.VRegA();
2123 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2124 UpdateLocal(register_index, constant);
2125 break;
2126 }
2127
2128 case Instruction::CONST_HIGH16: {
2129 int32_t register_index = instruction.VRegA();
2130 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2131 UpdateLocal(register_index, constant);
2132 break;
2133 }
2134
2135 case Instruction::CONST_WIDE_16: {
2136 int32_t register_index = instruction.VRegA();
2137 // Get 16 bits of constant value, sign extended to 64 bits.
2138 int64_t value = instruction.VRegB_21s();
2139 value <<= 48;
2140 value >>= 48;
2141 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2142 UpdateLocal(register_index, constant);
2143 break;
2144 }
2145
2146 case Instruction::CONST_WIDE_32: {
2147 int32_t register_index = instruction.VRegA();
2148 // Get 32 bits of constant value, sign extended to 64 bits.
2149 int64_t value = instruction.VRegB_31i();
2150 value <<= 32;
2151 value >>= 32;
2152 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2153 UpdateLocal(register_index, constant);
2154 break;
2155 }
2156
2157 case Instruction::CONST_WIDE: {
2158 int32_t register_index = instruction.VRegA();
2159 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2160 UpdateLocal(register_index, constant);
2161 break;
2162 }
2163
2164 case Instruction::CONST_WIDE_HIGH16: {
2165 int32_t register_index = instruction.VRegA();
2166 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2167 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2168 UpdateLocal(register_index, constant);
2169 break;
2170 }
2171
2172 // Note that the SSA building will refine the types.
2173 case Instruction::MOVE:
2174 case Instruction::MOVE_FROM16:
2175 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002176 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002177 UpdateLocal(instruction.VRegA(), value);
2178 break;
2179 }
2180
2181 // Note that the SSA building will refine the types.
2182 case Instruction::MOVE_WIDE:
2183 case Instruction::MOVE_WIDE_FROM16:
2184 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002185 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002186 UpdateLocal(instruction.VRegA(), value);
2187 break;
2188 }
2189
2190 case Instruction::MOVE_OBJECT:
2191 case Instruction::MOVE_OBJECT_16:
2192 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002193 // The verifier has no notion of a null type, so a move-object of constant 0
2194 // will lead to the same constant 0 in the destination register. To mimic
2195 // this behavior, we just pretend we haven't seen a type change (int to reference)
2196 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2197 // types correct.
2198 uint32_t reg_number = instruction.VRegB();
2199 HInstruction* value = (*current_locals_)[reg_number];
2200 if (value->IsIntConstant()) {
2201 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2202 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2204 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002205 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002206 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002207 }
David Brazdildee58d62016-04-07 09:54:26 +00002208 UpdateLocal(instruction.VRegA(), value);
2209 break;
2210 }
2211
2212 case Instruction::RETURN_VOID_NO_BARRIER:
2213 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002215 break;
2216 }
2217
2218#define IF_XX(comparison, cond) \
2219 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2220 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2221
2222 IF_XX(HEqual, EQ);
2223 IF_XX(HNotEqual, NE);
2224 IF_XX(HLessThan, LT);
2225 IF_XX(HLessThanOrEqual, LE);
2226 IF_XX(HGreaterThan, GT);
2227 IF_XX(HGreaterThanOrEqual, GE);
2228
2229 case Instruction::GOTO:
2230 case Instruction::GOTO_16:
2231 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002232 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002233 current_block_ = nullptr;
2234 break;
2235 }
2236
2237 case Instruction::RETURN: {
2238 BuildReturn(instruction, return_type_, dex_pc);
2239 break;
2240 }
2241
2242 case Instruction::RETURN_OBJECT: {
2243 BuildReturn(instruction, return_type_, dex_pc);
2244 break;
2245 }
2246
2247 case Instruction::RETURN_WIDE: {
2248 BuildReturn(instruction, return_type_, dex_pc);
2249 break;
2250 }
2251
2252 case Instruction::INVOKE_DIRECT:
2253 case Instruction::INVOKE_INTERFACE:
2254 case Instruction::INVOKE_STATIC:
2255 case Instruction::INVOKE_SUPER:
2256 case Instruction::INVOKE_VIRTUAL:
2257 case Instruction::INVOKE_VIRTUAL_QUICK: {
2258 uint16_t method_idx;
2259 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2260 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002261 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2262 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002263 return false;
2264 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002265 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002266 } else {
2267 method_idx = instruction.VRegB_35c();
2268 }
David Brazdildee58d62016-04-07 09:54:26 +00002269 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002270 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2271 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2272 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002273 return false;
2274 }
2275 break;
2276 }
2277
2278 case Instruction::INVOKE_DIRECT_RANGE:
2279 case Instruction::INVOKE_INTERFACE_RANGE:
2280 case Instruction::INVOKE_STATIC_RANGE:
2281 case Instruction::INVOKE_SUPER_RANGE:
2282 case Instruction::INVOKE_VIRTUAL_RANGE:
2283 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2284 uint16_t method_idx;
2285 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2286 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002287 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2288 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002289 return false;
2290 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002291 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002292 } else {
2293 method_idx = instruction.VRegB_3rc();
2294 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002295 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2296 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002297 return false;
2298 }
2299 break;
2300 }
2301
Orion Hodsonac141392017-01-13 11:53:47 +00002302 case Instruction::INVOKE_POLYMORPHIC: {
2303 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002304 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002305 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002306 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2307 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002308 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002309 }
2310
2311 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2312 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002313 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002314 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002315 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2316 }
2317
2318 case Instruction::INVOKE_CUSTOM: {
2319 uint16_t call_site_idx = instruction.VRegB_35c();
2320 uint32_t args[5];
2321 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2322 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2323 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2324 }
2325
2326 case Instruction::INVOKE_CUSTOM_RANGE: {
2327 uint16_t call_site_idx = instruction.VRegB_3rc();
2328 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2329 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002330 }
2331
David Brazdildee58d62016-04-07 09:54:26 +00002332 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002334 break;
2335 }
2336
2337 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002339 break;
2340 }
2341
2342 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002343 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002344 break;
2345 }
2346
2347 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002349 break;
2350 }
2351
2352 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002354 break;
2355 }
2356
2357 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002359 break;
2360 }
2361
2362 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002364 break;
2365 }
2366
2367 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002369 break;
2370 }
2371
2372 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002374 break;
2375 }
2376
2377 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002378 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002379 break;
2380 }
2381
2382 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002383 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002384 break;
2385 }
2386
2387 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002389 break;
2390 }
2391
2392 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002394 break;
2395 }
2396
2397 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002399 break;
2400 }
2401
2402 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002404 break;
2405 }
2406
2407 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002409 break;
2410 }
2411
2412 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002414 break;
2415 }
2416
2417 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002419 break;
2420 }
2421
2422 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002424 break;
2425 }
2426
2427 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002428 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002429 break;
2430 }
2431
2432 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002433 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002434 break;
2435 }
2436
2437 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002439 break;
2440 }
2441
2442 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002444 break;
2445 }
2446
2447 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002448 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002449 break;
2450 }
2451
2452 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002453 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002454 break;
2455 }
2456
2457 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002458 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002459 break;
2460 }
2461
2462 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002463 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002464 break;
2465 }
2466
2467 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002469 break;
2470 }
2471
2472 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002474 break;
2475 }
2476
2477 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002478 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002479 break;
2480 }
2481
2482 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002484 break;
2485 }
2486
2487 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002489 break;
2490 }
2491
2492 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002494 break;
2495 }
2496
2497 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002499 break;
2500 }
2501
2502 case Instruction::DIV_INT: {
2503 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002505 break;
2506 }
2507
2508 case Instruction::DIV_LONG: {
2509 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002511 break;
2512 }
2513
2514 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002516 break;
2517 }
2518
2519 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002521 break;
2522 }
2523
2524 case Instruction::REM_INT: {
2525 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002527 break;
2528 }
2529
2530 case Instruction::REM_LONG: {
2531 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002533 break;
2534 }
2535
2536 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002538 break;
2539 }
2540
2541 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002543 break;
2544 }
2545
2546 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002548 break;
2549 }
2550
2551 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002553 break;
2554 }
2555
2556 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002558 break;
2559 }
2560
2561 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002563 break;
2564 }
2565
2566 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002568 break;
2569 }
2570
2571 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002573 break;
2574 }
2575
2576 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002578 break;
2579 }
2580
2581 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002583 break;
2584 }
2585
2586 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002588 break;
2589 }
2590
2591 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002593 break;
2594 }
2595
2596 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002598 break;
2599 }
2600
2601 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002603 break;
2604 }
2605
2606 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002608 break;
2609 }
2610
2611 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002613 break;
2614 }
2615
2616 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002618 break;
2619 }
2620
2621 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002623 break;
2624 }
2625
2626 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002627 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002628 break;
2629 }
2630
2631 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002633 break;
2634 }
2635
2636 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002638 break;
2639 }
2640
2641 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002643 break;
2644 }
2645
2646 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002648 break;
2649 }
2650
2651 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002653 break;
2654 }
2655
2656 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002658 break;
2659 }
2660
2661 case Instruction::DIV_INT_2ADDR: {
2662 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002664 break;
2665 }
2666
2667 case Instruction::DIV_LONG_2ADDR: {
2668 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002670 break;
2671 }
2672
2673 case Instruction::REM_INT_2ADDR: {
2674 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002675 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002676 break;
2677 }
2678
2679 case Instruction::REM_LONG_2ADDR: {
2680 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002682 break;
2683 }
2684
2685 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002687 break;
2688 }
2689
2690 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002692 break;
2693 }
2694
2695 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002697 break;
2698 }
2699
2700 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002702 break;
2703 }
2704
2705 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002707 break;
2708 }
2709
2710 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002712 break;
2713 }
2714
2715 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002717 break;
2718 }
2719
2720 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002722 break;
2723 }
2724
2725 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002727 break;
2728 }
2729
2730 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002732 break;
2733 }
2734
2735 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002737 break;
2738 }
2739
2740 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002742 break;
2743 }
2744
2745 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002747 break;
2748 }
2749
2750 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002752 break;
2753 }
2754
2755 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002757 break;
2758 }
2759
2760 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002762 break;
2763 }
2764
2765 case Instruction::ADD_INT_LIT16: {
2766 Binop_22s<HAdd>(instruction, false, dex_pc);
2767 break;
2768 }
2769
2770 case Instruction::AND_INT_LIT16: {
2771 Binop_22s<HAnd>(instruction, false, dex_pc);
2772 break;
2773 }
2774
2775 case Instruction::OR_INT_LIT16: {
2776 Binop_22s<HOr>(instruction, false, dex_pc);
2777 break;
2778 }
2779
2780 case Instruction::XOR_INT_LIT16: {
2781 Binop_22s<HXor>(instruction, false, dex_pc);
2782 break;
2783 }
2784
2785 case Instruction::RSUB_INT: {
2786 Binop_22s<HSub>(instruction, true, dex_pc);
2787 break;
2788 }
2789
2790 case Instruction::MUL_INT_LIT16: {
2791 Binop_22s<HMul>(instruction, false, dex_pc);
2792 break;
2793 }
2794
2795 case Instruction::ADD_INT_LIT8: {
2796 Binop_22b<HAdd>(instruction, false, dex_pc);
2797 break;
2798 }
2799
2800 case Instruction::AND_INT_LIT8: {
2801 Binop_22b<HAnd>(instruction, false, dex_pc);
2802 break;
2803 }
2804
2805 case Instruction::OR_INT_LIT8: {
2806 Binop_22b<HOr>(instruction, false, dex_pc);
2807 break;
2808 }
2809
2810 case Instruction::XOR_INT_LIT8: {
2811 Binop_22b<HXor>(instruction, false, dex_pc);
2812 break;
2813 }
2814
2815 case Instruction::RSUB_INT_LIT8: {
2816 Binop_22b<HSub>(instruction, true, dex_pc);
2817 break;
2818 }
2819
2820 case Instruction::MUL_INT_LIT8: {
2821 Binop_22b<HMul>(instruction, false, dex_pc);
2822 break;
2823 }
2824
2825 case Instruction::DIV_INT_LIT16:
2826 case Instruction::DIV_INT_LIT8: {
2827 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002829 break;
2830 }
2831
2832 case Instruction::REM_INT_LIT16:
2833 case Instruction::REM_INT_LIT8: {
2834 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002836 break;
2837 }
2838
2839 case Instruction::SHL_INT_LIT8: {
2840 Binop_22b<HShl>(instruction, false, dex_pc);
2841 break;
2842 }
2843
2844 case Instruction::SHR_INT_LIT8: {
2845 Binop_22b<HShr>(instruction, false, dex_pc);
2846 break;
2847 }
2848
2849 case Instruction::USHR_INT_LIT8: {
2850 Binop_22b<HUShr>(instruction, false, dex_pc);
2851 break;
2852 }
2853
2854 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002855 HNewInstance* new_instance =
2856 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2857 DCHECK(new_instance != nullptr);
2858
David Brazdildee58d62016-04-07 09:54:26 +00002859 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002860 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002861 break;
2862 }
2863
2864 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002865 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Vladimir Markob5461632018-10-15 14:24:21 +01002867 HNewArray* new_array = BuildNewArray(dex_pc, type_index, length);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002868
David Brazdildee58d62016-04-07 09:54:26 +00002869 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002870 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002871 break;
2872 }
2873
2874 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002875 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002876 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002877 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2878 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2879 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002880 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002881 break;
2882 }
2883
2884 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002885 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002886 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2887 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002888 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002889 break;
2890 }
2891
2892 case Instruction::FILL_ARRAY_DATA: {
2893 BuildFillArrayData(instruction, dex_pc);
2894 break;
2895 }
2896
2897 case Instruction::MOVE_RESULT:
2898 case Instruction::MOVE_RESULT_WIDE:
2899 case Instruction::MOVE_RESULT_OBJECT: {
2900 DCHECK(latest_result_ != nullptr);
2901 UpdateLocal(instruction.VRegA(), latest_result_);
2902 latest_result_ = nullptr;
2903 break;
2904 }
2905
2906 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002908 break;
2909 }
2910
2911 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002913 break;
2914 }
2915
2916 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002918 break;
2919 }
2920
2921 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002923 break;
2924 }
2925
2926 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002928 break;
2929 }
2930
2931 case Instruction::NOP:
2932 break;
2933
2934 case Instruction::IGET:
2935 case Instruction::IGET_QUICK:
2936 case Instruction::IGET_WIDE:
2937 case Instruction::IGET_WIDE_QUICK:
2938 case Instruction::IGET_OBJECT:
2939 case Instruction::IGET_OBJECT_QUICK:
2940 case Instruction::IGET_BOOLEAN:
2941 case Instruction::IGET_BOOLEAN_QUICK:
2942 case Instruction::IGET_BYTE:
2943 case Instruction::IGET_BYTE_QUICK:
2944 case Instruction::IGET_CHAR:
2945 case Instruction::IGET_CHAR_QUICK:
2946 case Instruction::IGET_SHORT:
2947 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002948 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002949 return false;
2950 }
2951 break;
2952 }
2953
2954 case Instruction::IPUT:
2955 case Instruction::IPUT_QUICK:
2956 case Instruction::IPUT_WIDE:
2957 case Instruction::IPUT_WIDE_QUICK:
2958 case Instruction::IPUT_OBJECT:
2959 case Instruction::IPUT_OBJECT_QUICK:
2960 case Instruction::IPUT_BOOLEAN:
2961 case Instruction::IPUT_BOOLEAN_QUICK:
2962 case Instruction::IPUT_BYTE:
2963 case Instruction::IPUT_BYTE_QUICK:
2964 case Instruction::IPUT_CHAR:
2965 case Instruction::IPUT_CHAR_QUICK:
2966 case Instruction::IPUT_SHORT:
2967 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002968 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002969 return false;
2970 }
2971 break;
2972 }
2973
2974 case Instruction::SGET:
2975 case Instruction::SGET_WIDE:
2976 case Instruction::SGET_OBJECT:
2977 case Instruction::SGET_BOOLEAN:
2978 case Instruction::SGET_BYTE:
2979 case Instruction::SGET_CHAR:
2980 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002981 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00002982 break;
2983 }
2984
2985 case Instruction::SPUT:
2986 case Instruction::SPUT_WIDE:
2987 case Instruction::SPUT_OBJECT:
2988 case Instruction::SPUT_BOOLEAN:
2989 case Instruction::SPUT_BYTE:
2990 case Instruction::SPUT_CHAR:
2991 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002992 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002993 break;
2994 }
2995
2996#define ARRAY_XX(kind, anticipated_type) \
2997 case Instruction::AGET##kind: { \
2998 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2999 break; \
3000 } \
3001 case Instruction::APUT##kind: { \
3002 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3003 break; \
3004 }
3005
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 ARRAY_XX(, DataType::Type::kInt32);
3007 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3008 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3009 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3010 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3011 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3012 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003013
3014 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003015 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003016 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003017 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3018 break;
3019 }
3020
3021 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003022 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003023 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003024 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3025 break;
3026 }
3027
3028 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003029 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003030 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003031 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3032 break;
3033 }
3034
3035 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003036 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003037 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003038 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3039 break;
3040 }
3041
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003042 case Instruction::CONST_METHOD_HANDLE: {
3043 uint16_t method_handle_idx = instruction.VRegB_21c();
3044 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3045 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3046 break;
3047 }
3048
Orion Hodson18259d72018-04-12 11:18:23 +01003049 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003050 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003051 BuildLoadMethodType(proto_idx, dex_pc);
3052 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3053 break;
3054 }
3055
David Brazdildee58d62016-04-07 09:54:26 +00003056 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003057 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003058 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003059 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003060 break;
3061 }
3062
3063 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003065 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003066 // We finished building this block. Set the current block to null to avoid
3067 // adding dead instructions to it.
3068 current_block_ = nullptr;
3069 break;
3070 }
3071
3072 case Instruction::INSTANCE_OF: {
3073 uint8_t destination = instruction.VRegA_22c();
3074 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003075 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003076 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3077 break;
3078 }
3079
3080 case Instruction::CHECK_CAST: {
3081 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003082 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003083 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3084 break;
3085 }
3086
3087 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003088 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003090 HMonitorOperation::OperationKind::kEnter,
3091 dex_pc));
3092 break;
3093 }
3094
3095 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003096 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003097 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003098 HMonitorOperation::OperationKind::kExit,
3099 dex_pc));
3100 break;
3101 }
3102
3103 case Instruction::SPARSE_SWITCH:
3104 case Instruction::PACKED_SWITCH: {
3105 BuildSwitch(instruction, dex_pc);
3106 break;
3107 }
3108
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003109 case Instruction::UNUSED_3E:
3110 case Instruction::UNUSED_3F:
3111 case Instruction::UNUSED_40:
3112 case Instruction::UNUSED_41:
3113 case Instruction::UNUSED_42:
3114 case Instruction::UNUSED_43:
3115 case Instruction::UNUSED_79:
3116 case Instruction::UNUSED_7A:
3117 case Instruction::UNUSED_F3:
3118 case Instruction::UNUSED_F4:
3119 case Instruction::UNUSED_F5:
3120 case Instruction::UNUSED_F6:
3121 case Instruction::UNUSED_F7:
3122 case Instruction::UNUSED_F8:
3123 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003124 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003125 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003126 << " because of unhandled instruction "
3127 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003128 MaybeRecordStat(compilation_stats_,
3129 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003130 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003131 }
David Brazdildee58d62016-04-07 09:54:26 +00003132 }
3133 return true;
3134} // NOLINT(readability/fn_size)
3135
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003136ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3137 dex::TypeIndex type_index,
3138 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003139 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003140 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3141}
3142
3143ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3144 // TODO: Cache the result in a Handle<mirror::Class>.
3145 const DexFile::MethodId& method_id =
3146 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3147 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3148}
3149
David Brazdildee58d62016-04-07 09:54:26 +00003150} // namespace art