blob: 0e20a65a2d31836f22a0afd23a7639757335bc81 [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"
David Brazdildee58d62016-04-07 09:54:26 +000023#include "class_linker.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010024#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070025#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010027#include "driver/compiler_driver-inl.h"
28#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,
50 CompilerDriver* compiler_driver,
51 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080052 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080053 OptimizingCompilerStats* compiler_stats,
54 VariableSizedHandleScope* handles,
55 ScopedArenaAllocator* local_allocator)
56 : allocator_(graph->GetAllocator()),
57 graph_(graph),
58 handles_(handles),
59 dex_file_(dex_file),
60 code_item_accessor_(accessor),
61 return_type_(return_type),
62 block_builder_(block_builder),
63 ssa_builder_(ssa_builder),
64 compiler_driver_(compiler_driver),
65 code_generator_(code_generator),
66 dex_compilation_unit_(dex_compilation_unit),
67 outer_compilation_unit_(outer_compilation_unit),
68 quicken_info_(interpreter_metadata),
69 compilation_stats_(compiler_stats),
70 local_allocator_(local_allocator),
71 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
72 current_block_(nullptr),
73 current_locals_(nullptr),
74 latest_result_(nullptr),
75 current_this_parameter_(nullptr),
76 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
77 loop_headers_.reserve(kDefaultNumberOfLoops);
78}
79
David Brazdildee58d62016-04-07 09:54:26 +000080HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
81 return block_builder_->GetBlockAt(dex_pc);
82}
83
Vladimir Marko69d310e2017-10-09 14:12:23 +010084inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
85 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000086 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080087 if (locals->size() == vregs) {
88 return locals;
89 }
90 return GetLocalsForWithAllocation(block, locals, vregs);
91}
David Brazdildee58d62016-04-07 09:54:26 +000092
Vladimir Marko69d310e2017-10-09 14:12:23 +010093ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080094 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010095 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080096 const size_t vregs) {
97 DCHECK_NE(locals->size(), vregs);
98 locals->resize(vregs, nullptr);
99 if (block->IsCatchBlock()) {
100 // We record incoming inputs of catch phis at throwing instructions and
101 // must therefore eagerly create the phis. Phis for undefined vregs will
102 // be deleted when the first throwing instruction with the vreg undefined
103 // is encountered. Unused phis will be removed by dead phi analysis.
104 for (size_t i = 0; i < vregs; ++i) {
105 // No point in creating the catch phi if it is already undefined at
106 // the first throwing instruction.
107 HInstruction* current_local_value = (*current_locals_)[i];
108 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109 HPhi* phi = new (allocator_) HPhi(
110 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800111 i,
112 0,
113 current_local_value->GetType());
114 block->AddPhi(phi);
115 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000116 }
117 }
118 }
119 return locals;
120}
121
Mingyao Yang01b47b02017-02-03 12:09:57 -0800122inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100123 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000124 return (*locals)[local];
125}
126
127void HInstructionBuilder::InitializeBlockLocals() {
128 current_locals_ = GetLocalsFor(current_block_);
129
130 if (current_block_->IsCatchBlock()) {
131 // Catch phis were already created and inputs collected from throwing sites.
132 if (kIsDebugBuild) {
133 // Make sure there was at least one throwing instruction which initialized
134 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
135 // visited already (from HTryBoundary scoping and reverse post order).
136 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100137 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000138 if (current == current_block_) {
139 catch_block_visited = true;
140 } else if (current->IsTryBlock()) {
141 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
142 if (try_entry.HasExceptionHandler(*current_block_)) {
143 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
144 }
145 }
146 }
147 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
148 << "No instructions throwing into a live catch block.";
149 }
150 } else if (current_block_->IsLoopHeader()) {
151 // If the block is a loop header, we know we only have visited the pre header
152 // because we are visiting in reverse post order. We create phis for all initialized
153 // locals from the pre header. Their inputs will be populated at the end of
154 // the analysis.
155 for (size_t local = 0; local < current_locals_->size(); ++local) {
156 HInstruction* incoming =
157 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
158 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HPhi* phi = new (allocator_) HPhi(
160 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000161 local,
162 0,
163 incoming->GetType());
164 current_block_->AddPhi(phi);
165 (*current_locals_)[local] = phi;
166 }
167 }
168
169 // Save the loop header so that the last phase of the analysis knows which
170 // blocks need to be updated.
171 loop_headers_.push_back(current_block_);
172 } else if (current_block_->GetPredecessors().size() > 0) {
173 // All predecessors have already been visited because we are visiting in reverse post order.
174 // We merge the values of all locals, creating phis if those values differ.
175 for (size_t local = 0; local < current_locals_->size(); ++local) {
176 bool one_predecessor_has_no_value = false;
177 bool is_different = false;
178 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
179
180 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
181 HInstruction* current = ValueOfLocalAt(predecessor, local);
182 if (current == nullptr) {
183 one_predecessor_has_no_value = true;
184 break;
185 } else if (current != value) {
186 is_different = true;
187 }
188 }
189
190 if (one_predecessor_has_no_value) {
191 // If one predecessor has no value for this local, we trust the verifier has
192 // successfully checked that there is a store dominating any read after this block.
193 continue;
194 }
195
196 if (is_different) {
197 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100198 HPhi* phi = new (allocator_) HPhi(
199 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000200 local,
201 current_block_->GetPredecessors().size(),
202 first_input->GetType());
203 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
204 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
205 phi->SetRawInputAt(i, pred_value);
206 }
207 current_block_->AddPhi(phi);
208 value = phi;
209 }
210 (*current_locals_)[local] = value;
211 }
212 }
213}
214
215void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
216 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
217 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100218 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000219 DCHECK_EQ(handler_locals->size(), current_locals_->size());
220 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
221 HInstruction* handler_value = (*handler_locals)[vreg];
222 if (handler_value == nullptr) {
223 // Vreg was undefined at a previously encountered throwing instruction
224 // and the catch phi was deleted. Do not record the local value.
225 continue;
226 }
227 DCHECK(handler_value->IsPhi());
228
229 HInstruction* local_value = (*current_locals_)[vreg];
230 if (local_value == nullptr) {
231 // This is the first instruction throwing into `catch_block` where
232 // `vreg` is undefined. Delete the catch phi.
233 catch_block->RemovePhi(handler_value->AsPhi());
234 (*handler_locals)[vreg] = nullptr;
235 } else {
236 // Vreg has been defined at all instructions throwing into `catch_block`
237 // encountered so far. Record the local value in the catch phi.
238 handler_value->AsPhi()->AddInput(local_value);
239 }
240 }
241 }
242}
243
244void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
245 current_block_->AddInstruction(instruction);
246 InitializeInstruction(instruction);
247}
248
249void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
250 if (current_block_->GetInstructions().IsEmpty()) {
251 current_block_->AddInstruction(instruction);
252 } else {
253 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
254 }
255 InitializeInstruction(instruction);
256}
257
258void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
259 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100260 HEnvironment* environment = new (allocator_) HEnvironment(
261 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000262 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000263 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000264 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000265 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100266 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000267 instruction->SetRawEnvironment(environment);
268 }
269}
270
David Brazdilc120bbe2016-04-22 16:57:00 +0100271HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100272 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100273 if (!ref->CanBeNull()) {
274 return ref;
275 }
276
Vladimir Markoca6fff82017-10-03 14:49:14 +0100277 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100278 AppendInstruction(null_check);
279 return null_check;
280}
281
David Brazdildee58d62016-04-07 09:54:26 +0000282void HInstructionBuilder::SetLoopHeaderPhiInputs() {
283 for (size_t i = loop_headers_.size(); i > 0; --i) {
284 HBasicBlock* block = loop_headers_[i - 1];
285 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
286 HPhi* phi = it.Current()->AsPhi();
287 size_t vreg = phi->GetRegNumber();
288 for (HBasicBlock* predecessor : block->GetPredecessors()) {
289 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
290 if (value == nullptr) {
291 // Vreg is undefined at this predecessor. Mark it dead and leave with
292 // fewer inputs than predecessors. SsaChecker will fail if not removed.
293 phi->SetDead();
294 break;
295 } else {
296 phi->AddInput(value);
297 }
298 }
299 }
300 }
301}
302
303static bool IsBlockPopulated(HBasicBlock* block) {
304 if (block->IsLoopHeader()) {
305 // Suspend checks were inserted into loop headers during building of dominator tree.
306 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
307 return block->GetFirstInstruction() != block->GetLastInstruction();
308 } else {
309 return !block->GetInstructions().IsEmpty();
310 }
311}
312
313bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800314 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100315 locals_for_.resize(
316 graph_->GetBlocks().size(),
317 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000318
319 // Find locations where we want to generate extra stackmaps for native debugging.
320 // This allows us to generate the info only at interesting points (for example,
321 // at start of java statement) rather than before every dex instruction.
322 const bool native_debuggable = compiler_driver_ != nullptr &&
323 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
324 ArenaBitVector* native_debug_info_locations = nullptr;
325 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100326 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000327 }
328
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100329 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
330 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000331 uint32_t block_dex_pc = current_block_->GetDexPc();
332
333 InitializeBlockLocals();
334
335 if (current_block_->IsEntryBlock()) {
336 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100337 AppendInstruction(new (allocator_) HSuspendCheck(0u));
338 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000339 continue;
340 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100341 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000342 continue;
343 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000345 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
346 // This is slightly odd because the loop header might not be empty (TryBoundary).
347 // But we're still creating the environment with locals from the top of the block.
348 InsertInstructionAtTop(suspend_check);
349 }
350
351 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
352 // Synthetic block that does not need to be populated.
353 DCHECK(IsBlockPopulated(current_block_));
354 continue;
355 }
356
357 DCHECK(!IsBlockPopulated(current_block_));
358
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700359 uint32_t quicken_index = 0;
360 if (CanDecodeQuickenedInfo()) {
361 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
362 }
363
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800364 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000365 if (current_block_ == nullptr) {
366 // The previous instruction ended this block.
367 break;
368 }
369
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800370 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000371 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
372 // This dex_pc starts a new basic block.
373 break;
374 }
375
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800376 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000377 PropagateLocalsToCatchBlocks();
378 }
379
380 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100381 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000382 }
383
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800384 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000385 return false;
386 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700387
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800388 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700389 ++quicken_index;
390 }
David Brazdildee58d62016-04-07 09:54:26 +0000391 }
392
393 if (current_block_ != nullptr) {
394 // Branching instructions clear current_block, so we know the last
395 // instruction of the current block is not a branching instruction.
396 // We add an unconditional Goto to the next block.
397 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100398 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000399 }
400 }
401
402 SetLoopHeaderPhiInputs();
403
404 return true;
405}
406
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000407void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800408 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000409 DCHECK(method->IsIntrinsic());
410
411 locals_for_.resize(
412 graph_->GetBlocks().size(),
413 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
414
415 // Fill the entry block. Do not add suspend check, we do not want a suspend
416 // check in intrinsics; intrinsic methods are supposed to be fast.
417 current_block_ = graph_->GetEntryBlock();
418 InitializeBlockLocals();
419 InitializeParameters();
420 AppendInstruction(new (allocator_) HGoto(0u));
421
422 // Fill the body.
423 current_block_ = current_block_->GetSingleSuccessor();
424 InitializeBlockLocals();
425 DCHECK(!IsBlockPopulated(current_block_));
426
427 // Add the invoke and return instruction. Use HInvokeStaticOrDirect even
428 // for methods that would normally use an HInvokeVirtual (sharpen the call).
429 size_t in_vregs = graph_->GetNumberOfInVRegs();
430 size_t number_of_arguments =
431 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
432 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
433 MethodReference target_method(dex_file_, method_idx);
434 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
435 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
436 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
437 /* method_load_data */ 0u
438 };
439 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
440 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
441 allocator_,
442 number_of_arguments,
443 return_type_,
444 kNoDexPc,
445 method_idx,
446 method,
447 dispatch_info,
448 invoke_type,
449 target_method,
450 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
451 HandleInvoke(invoke,
452 in_vregs,
453 /* args */ nullptr,
454 graph_->GetNumberOfVRegs() - in_vregs,
455 /* is_range */ true,
456 dex_file_->GetMethodShorty(method_idx),
457 /* clinit_check */ nullptr,
458 /* is_unresolved */ false);
459
460 // Add the return instruction.
461 if (return_type_ == DataType::Type::kVoid) {
462 AppendInstruction(new (allocator_) HReturnVoid());
463 } else {
464 AppendInstruction(new (allocator_) HReturn(invoke));
465 }
466
467 // Fill the exit block.
468 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
469 current_block_ = graph_->GetExitBlock();
470 InitializeBlockLocals();
471 AppendInstruction(new (allocator_) HExit());
472}
473
Vladimir Marko69d310e2017-10-09 14:12:23 +0100474ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
David Brazdildee58d62016-04-07 09:54:26 +0000475 // The callback gets called when the line number changes.
476 // In other words, it marks the start of new java statement.
477 struct Callback {
478 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
479 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
480 return false;
481 }
482 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100483 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800484 code_item_accessor_.InsnsSizeInCodeUnits(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100485 /* expandable */ false,
486 kArenaAllocGraphBuilder);
487 locations->ClearAllBits();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800488 dex_file_->DecodeDebugPositionInfo(code_item_accessor_.DebugInfoOffset(),
489 Callback::Position,
490 locations);
David Brazdildee58d62016-04-07 09:54:26 +0000491 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800492 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800493 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000494 case Instruction::MOVE_EXCEPTION: {
495 // Stop in native debugger after the exception has been moved.
496 // The compiler also expects the move at the start of basic block so
497 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800498 locations->ClearBit(inst.DexPc());
499 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
500 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800501 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700502 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000503 }
504 break;
505 }
506 default:
507 break;
508 }
509 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100510 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000511}
512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100513HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000514 HInstruction* value = (*current_locals_)[reg_number];
515 DCHECK(value != nullptr);
516
517 // If the operation requests a specific type, we make sure its input is of that type.
518 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100519 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700520 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100521 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700522 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000523 }
Aart Bik31883642016-06-06 15:02:44 -0700524 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000525 }
526
527 return value;
528}
529
530void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 DataType::Type stored_type = stored_value->GetType();
532 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000533
534 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
535 // registers. Consider the following cases:
536 // (1) Storing a wide value must overwrite previous values in both `reg_number`
537 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
538 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
539 // must invalidate it. We store `nullptr` in `reg_number-1`.
540 // Consequently, storing a wide value into the high vreg of another wide value
541 // will invalidate both `reg_number-1` and `reg_number+1`.
542
543 if (reg_number != 0) {
544 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100545 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000546 // The vreg we are storing into was previously the high vreg of a pair.
547 // We need to invalidate its low vreg.
548 DCHECK((*current_locals_)[reg_number] == nullptr);
549 (*current_locals_)[reg_number - 1] = nullptr;
550 }
551 }
552
553 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100554 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000555 // We are storing a pair. Invalidate the instruction in the high vreg.
556 (*current_locals_)[reg_number + 1] = nullptr;
557 }
558}
559
560void HInstructionBuilder::InitializeParameters() {
561 DCHECK(current_block_->IsEntryBlock());
562
Vladimir Marko69d310e2017-10-09 14:12:23 +0100563 // outer_compilation_unit_ is null only when unit testing.
564 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000565 return;
566 }
567
568 const char* shorty = dex_compilation_unit_->GetShorty();
569 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
570 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
571 uint16_t parameter_index = 0;
572
573 const DexFile::MethodId& referrer_method_id =
574 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
575 if (!dex_compilation_unit_->IsStatic()) {
576 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100577 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000578 referrer_method_id.class_idx_,
579 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100580 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700581 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000582 AppendInstruction(parameter);
583 UpdateLocal(locals_index++, parameter);
584 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700585 current_this_parameter_ = parameter;
586 } else {
587 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000588 }
589
590 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
591 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
592 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100593 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000594 *dex_file_,
595 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
596 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100597 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700598 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000599 ++shorty_pos;
600 AppendInstruction(parameter);
601 // Store the parameter value in the local that the dex code will use
602 // to reference that parameter.
603 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000605 i++;
606 locals_index++;
607 parameter_index++;
608 }
609 }
610}
611
612template<typename T>
613void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100614 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
615 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000617 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100618 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000619 current_block_ = nullptr;
620}
621
622template<typename T>
623void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100624 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100625 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000626 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100627 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000628 current_block_ = nullptr;
629}
630
631template<typename T>
632void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100633 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000634 uint32_t dex_pc) {
635 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000637 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
638}
639
640void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100641 DataType::Type input_type,
642 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000643 uint32_t dex_pc) {
644 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100645 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000646 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
647}
648
649template<typename T>
650void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100651 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000652 uint32_t dex_pc) {
653 HInstruction* first = LoadLocal(instruction.VRegB(), type);
654 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100655 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000656 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
657}
658
659template<typename T>
660void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100661 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000662 uint32_t dex_pc) {
663 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100665 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000666 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
667}
668
669void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100670 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000671 ComparisonBias bias,
672 uint32_t dex_pc) {
673 HInstruction* first = LoadLocal(instruction.VRegB(), type);
674 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100675 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000676 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
677}
678
679template<typename T>
680void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100681 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000682 uint32_t dex_pc) {
683 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100684 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100685 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000686 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
687}
688
689template<typename T>
690void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100691 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000692 uint32_t dex_pc) {
693 HInstruction* first = LoadLocal(instruction.VRegA(), type);
694 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000696 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
697}
698
699template<typename T>
700void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100701 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000702 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
703 if (reverse) {
704 std::swap(first, second);
705 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100706 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000707 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
708}
709
710template<typename T>
711void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100712 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000713 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
714 if (reverse) {
715 std::swap(first, second);
716 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100717 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000718 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
719}
720
Igor Murashkind01745e2017-04-05 16:40:31 -0700721// Does the method being compiled need any constructor barriers being inserted?
722// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700723static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700724 // Can be null in unit tests only.
725 if (UNLIKELY(cu == nullptr)) {
726 return false;
727 }
728
David Brazdildee58d62016-04-07 09:54:26 +0000729 Thread* self = Thread::Current();
730 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700731 && !cu->IsStatic()
732 // RequiresConstructorBarrier must only be queried for <init> methods;
733 // it's effectively "false" for every other method.
734 //
735 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700736 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000737}
738
739// Returns true if `block` has only one successor which starts at the next
740// dex_pc after `instruction` at `dex_pc`.
741static bool IsFallthroughInstruction(const Instruction& instruction,
742 uint32_t dex_pc,
743 HBasicBlock* block) {
744 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
745 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
746}
747
748void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100749 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000750 DexSwitchTable table(instruction, dex_pc);
751
752 if (table.GetNumEntries() == 0) {
753 // Empty Switch. Code falls through to the next block.
754 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100755 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000756 } else if (table.ShouldBuildDecisionTree()) {
757 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
758 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000760 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100761 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000762
763 if (!it.IsLast()) {
764 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
765 }
766 }
767 } else {
768 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100769 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000770 }
771
772 current_block_ = nullptr;
773}
774
775void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100776 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000777 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100778 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700779 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700780 // This may insert additional redundant constructor fences from the super constructors.
781 // TODO: remove redundant constructor fences (b/36656456).
782 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700783 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100784 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700785
786 HInstruction* fence_target = current_this_parameter_;
787 DCHECK(fence_target != nullptr);
788
Vladimir Markoca6fff82017-10-03 14:49:14 +0100789 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700790 MaybeRecordStat(
791 compilation_stats_,
792 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000793 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100794 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000795 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700796 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000797 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100798 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000799 }
800 current_block_ = nullptr;
801}
802
803static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
804 switch (opcode) {
805 case Instruction::INVOKE_STATIC:
806 case Instruction::INVOKE_STATIC_RANGE:
807 return kStatic;
808 case Instruction::INVOKE_DIRECT:
809 case Instruction::INVOKE_DIRECT_RANGE:
810 return kDirect;
811 case Instruction::INVOKE_VIRTUAL:
812 case Instruction::INVOKE_VIRTUAL_QUICK:
813 case Instruction::INVOKE_VIRTUAL_RANGE:
814 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
815 return kVirtual;
816 case Instruction::INVOKE_INTERFACE:
817 case Instruction::INVOKE_INTERFACE_RANGE:
818 return kInterface;
819 case Instruction::INVOKE_SUPER_RANGE:
820 case Instruction::INVOKE_SUPER:
821 return kSuper;
822 default:
823 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
824 UNREACHABLE();
825 }
826}
827
828ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
829 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000830
831 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000832 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100833
Vladimir Markoba118822017-06-12 15:41:56 +0100834 ArtMethod* resolved_method =
835 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100836 method_idx,
837 dex_compilation_unit_->GetDexCache(),
838 class_loader,
839 graph_->GetArtMethod(),
840 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000841
842 if (UNLIKELY(resolved_method == nullptr)) {
843 // Clean up any exception left by type resolution.
844 soa.Self()->ClearException();
845 return nullptr;
846 }
847
Vladimir Markoba118822017-06-12 15:41:56 +0100848 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
849 // resolved because, for example, we don't find a superclass in the classpath.
850 if (graph_->GetArtMethod() == nullptr) {
851 // The class linker cannot check access without a referrer, so we have to do it.
852 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000853 if (!resolved_method->IsPublic()) {
854 return nullptr;
855 }
David Brazdildee58d62016-04-07 09:54:26 +0000856 }
857
858 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
859 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
860 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
861 // which require runtime handling.
862 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100863 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800864 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000865 // We could not determine the method's class we need to wait until runtime.
866 DCHECK(Runtime::Current()->IsAotCompiler());
867 return nullptr;
868 }
Vladimir Markoba118822017-06-12 15:41:56 +0100869 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100870 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
871 dex_compilation_unit_->GetDexCache().Get(),
872 class_loader.Get());
873 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
874 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700875 // We cannot statically determine the target method. The runtime will throw a
876 // NoSuchMethodError on this one.
877 return nullptr;
878 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100879 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100880 if (referenced_class->IsInterface()) {
881 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100882 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000883 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100884 uint16_t vtable_index = resolved_method->GetMethodIndex();
885 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
886 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000887 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100888 if (actual_method != resolved_method &&
889 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
890 // The back-end code generator relies on this check in order to ensure that it will not
891 // attempt to read the dex_cache with a dex_method_index that is not from the correct
892 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
893 // builder, which means that the code-generator (and compiler driver during sharpening and
894 // inliner, maybe) might invoke an incorrect method.
895 // TODO: The actual method could still be referenced in the current dex file, so we
896 // could try locating it.
897 // TODO: Remove the dex_file restriction.
898 return nullptr;
899 }
900 if (!actual_method->IsInvokable()) {
901 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
902 // could resolve the callee to the wrong method.
903 return nullptr;
904 }
905 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000906 }
907
David Brazdildee58d62016-04-07 09:54:26 +0000908 return resolved_method;
909}
910
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100911static bool IsStringConstructor(ArtMethod* method) {
912 ScopedObjectAccess soa(Thread::Current());
913 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
914}
915
David Brazdildee58d62016-04-07 09:54:26 +0000916bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
917 uint32_t dex_pc,
918 uint32_t method_idx,
919 uint32_t number_of_vreg_arguments,
920 bool is_range,
921 uint32_t* args,
922 uint32_t register_index) {
923 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
924 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100925 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000926
927 // Remove the return type from the 'proto'.
928 size_t number_of_arguments = strlen(descriptor) - 1;
929 if (invoke_type != kStatic) { // instance call
930 // One extra argument for 'this'.
931 number_of_arguments++;
932 }
933
David Brazdildee58d62016-04-07 09:54:26 +0000934 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
935
936 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700937 MaybeRecordStat(compilation_stats_,
938 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100939 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
940 number_of_arguments,
941 return_type,
942 dex_pc,
943 method_idx,
944 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000945 return HandleInvoke(invoke,
946 number_of_vreg_arguments,
947 args,
948 register_index,
949 is_range,
950 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700951 nullptr, /* clinit_check */
952 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000953 }
954
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100955 // Replace calls to String.<init> with StringFactory.
956 if (IsStringConstructor(resolved_method)) {
957 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
958 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
959 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
960 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000961 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100962 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000963 ScopedObjectAccess soa(Thread::Current());
964 MethodReference target_method(resolved_method->GetDexFile(),
965 resolved_method->GetDexMethodIndex());
966 // We pass null for the resolved_method to ensure optimizations
967 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100968 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
969 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100970 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100971 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100972 dex_pc,
973 method_idx,
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000974 nullptr /* resolved_method */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100975 dispatch_info,
976 invoke_type,
977 target_method,
978 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
979 return HandleStringInit(invoke,
980 number_of_vreg_arguments,
981 args,
982 register_index,
983 is_range,
984 descriptor);
985 }
986
David Brazdildee58d62016-04-07 09:54:26 +0000987 // Potential class initialization check, in the case of a static method call.
988 HClinitCheck* clinit_check = nullptr;
989 HInvoke* invoke = nullptr;
990 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
991 // By default, consider that the called method implicitly requires
992 // an initialization check of its declaring method.
993 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
994 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
995 ScopedObjectAccess soa(Thread::Current());
996 if (invoke_type == kStatic) {
997 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000998 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000999 } else if (invoke_type == kSuper) {
1000 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001001 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +00001002 // we resolved to the method referenced by the instruction.
1003 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +00001004 }
1005 }
1006
1007 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01001008 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +00001009 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00001010 0u
David Brazdildee58d62016-04-07 09:54:26 +00001011 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001012 MethodReference target_method(resolved_method->GetDexFile(),
1013 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001014 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1015 number_of_arguments,
1016 return_type,
1017 dex_pc,
1018 method_idx,
1019 resolved_method,
1020 dispatch_info,
1021 invoke_type,
1022 target_method,
1023 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +00001024 } else if (invoke_type == kVirtual) {
1025 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +01001026 invoke = new (allocator_) HInvokeVirtual(allocator_,
1027 number_of_arguments,
1028 return_type,
1029 dex_pc,
1030 method_idx,
1031 resolved_method,
1032 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +00001033 } else {
1034 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001035 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001036 invoke = new (allocator_) HInvokeInterface(allocator_,
1037 number_of_arguments,
1038 return_type,
1039 dex_pc,
1040 method_idx,
1041 resolved_method,
1042 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001043 }
1044
1045 return HandleInvoke(invoke,
1046 number_of_vreg_arguments,
1047 args,
1048 register_index,
1049 is_range,
1050 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001051 clinit_check,
1052 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +00001053}
1054
Orion Hodsonac141392017-01-13 11:53:47 +00001055bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
1056 uint32_t dex_pc,
1057 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001058 dex::ProtoIndex proto_idx,
Orion Hodsonac141392017-01-13 11:53:47 +00001059 uint32_t number_of_vreg_arguments,
1060 bool is_range,
1061 uint32_t* args,
1062 uint32_t register_index) {
1063 const char* descriptor = dex_file_->GetShorty(proto_idx);
1064 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001065 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +00001066 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001067 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1068 number_of_arguments,
1069 return_type,
1070 dex_pc,
1071 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00001072 return HandleInvoke(invoke,
1073 number_of_vreg_arguments,
1074 args,
1075 register_index,
1076 is_range,
1077 descriptor,
1078 nullptr /* clinit_check */,
1079 false /* is_unresolved */);
1080}
1081
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001082HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001083 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001084
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001085 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001086
David Brazdildee58d62016-04-07 09:54:26 +00001087 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001088 Handle<mirror::Class> klass = load_class->GetClass();
1089
1090 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001091 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001092 AppendInstruction(cls);
1093 }
1094
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001095 // Only the access check entrypoint handles the finalizable class case. If we
1096 // need access checks, then we haven't resolved the method and the class may
1097 // again be finalizable.
1098 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1099 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1100 entrypoint = kQuickAllocObjectWithChecks;
1101 }
1102
1103 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001104 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001105
Vladimir Markoca6fff82017-10-03 14:49:14 +01001106 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001107 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001108 dex_pc,
1109 type_index,
1110 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001111 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001112 entrypoint);
1113 AppendInstruction(new_instance);
1114
1115 return new_instance;
1116}
1117
1118void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1119 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001120 (allocation->IsNewInstance() ||
1121 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001122
1123 if (allocation->IsNewInstance()) {
1124 // STRING SPECIAL HANDLING:
1125 // -------------------------------
1126 // Strings have a real HNewInstance node but they end up always having 0 uses.
1127 // All uses of a String HNewInstance are always transformed to replace their input
1128 // of the HNewInstance with an input of the invoke to StringFactory.
1129 //
1130 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1131 // optimizations (to pass checker tests that rely on those optimizations).
1132 HNewInstance* new_inst = allocation->AsNewInstance();
1133 HLoadClass* load_class = new_inst->GetLoadClass();
1134
1135 Thread* self = Thread::Current();
1136 ScopedObjectAccess soa(self);
1137 StackHandleScope<1> hs(self);
1138 Handle<mirror::Class> klass = load_class->GetClass();
1139 if (klass != nullptr && klass->IsStringClass()) {
1140 return;
1141 // Note: Do not use allocation->IsStringAlloc which requires
1142 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1143 // propagation (and instruction builder is too early).
1144 }
1145 // (In terms of correctness, the StringFactory needs to provide its own
1146 // default initialization barrier, see below.)
1147 }
1148
1149 // JLS 17.4.5 "Happens-before Order" describes:
1150 //
1151 // The default initialization of any object happens-before any other actions (other than
1152 // default-writes) of a program.
1153 //
1154 // In our implementation the default initialization of an object to type T means
1155 // setting all of its initial data (object[0..size)) to 0, and setting the
1156 // object's class header (i.e. object.getClass() == T.class).
1157 //
1158 // In practice this fence ensures that the writes to the object header
1159 // are visible to other threads if this object escapes the current thread.
1160 // (and in theory the 0-initializing, but that happens automatically
1161 // when new memory pages are mapped in by the OS).
1162 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001163 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001164 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001165 MaybeRecordStat(
1166 compilation_stats_,
1167 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001168}
1169
Vladimir Marko28e012a2017-12-07 11:22:59 +00001170static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001171 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001172 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1173}
1174
1175bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001176 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001177 return false;
1178 }
1179
1180 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1181 // check whether the class is in an image for the AOT compilation.
1182 if (cls->IsInitialized() &&
1183 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1184 return true;
1185 }
1186
1187 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1188 return true;
1189 }
1190
1191 // TODO: We should walk over the inlined methods, but we don't pass
1192 // that information to the builder.
1193 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1194 return true;
1195 }
1196
1197 return false;
1198}
1199
1200HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1201 uint32_t dex_pc,
1202 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001203 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001204 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001205
1206 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001207 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001208 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001209 } else {
1210 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1211 klass->GetDexFile(),
1212 klass,
1213 dex_pc,
1214 /* needs_access_check */ false);
1215 if (cls != nullptr) {
1216 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001217 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001218 AppendInstruction(clinit_check);
1219 }
David Brazdildee58d62016-04-07 09:54:26 +00001220 }
1221 return clinit_check;
1222}
1223
1224bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1225 uint32_t number_of_vreg_arguments,
1226 uint32_t* args,
1227 uint32_t register_index,
1228 bool is_range,
1229 const char* descriptor,
1230 size_t start_index,
1231 size_t* argument_index) {
1232 uint32_t descriptor_index = 1; // Skip the return type.
1233
1234 for (size_t i = start_index;
1235 // Make sure we don't go over the expected arguments or over the number of
1236 // dex registers given. If the instruction was seen as dead by the verifier,
1237 // it hasn't been properly checked.
1238 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1239 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001240 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1241 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001242 if (!is_range
1243 && is_wide
1244 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1245 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1246 // reject any class where this is violated. However, the verifier only does these checks
1247 // on non trivially dead instructions, so we just bailout the compilation.
1248 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001249 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001250 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001251 MaybeRecordStat(compilation_stats_,
1252 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001253 return false;
1254 }
1255 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1256 invoke->SetArgumentAt(*argument_index, arg);
1257 if (is_wide) {
1258 i++;
1259 }
1260 }
1261
1262 if (*argument_index != invoke->GetNumberOfArguments()) {
1263 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001264 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001265 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001266 MaybeRecordStat(compilation_stats_,
1267 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001268 return false;
1269 }
1270
1271 if (invoke->IsInvokeStaticOrDirect() &&
1272 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1273 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1274 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1275 (*argument_index)++;
1276 }
1277
1278 return true;
1279}
1280
1281bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1282 uint32_t number_of_vreg_arguments,
1283 uint32_t* args,
1284 uint32_t register_index,
1285 bool is_range,
1286 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001287 HClinitCheck* clinit_check,
1288 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001289 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1290
1291 size_t start_index = 0;
1292 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001293 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001294 uint32_t obj_reg = is_range ? register_index : args[0];
1295 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001296 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001297 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001298 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001299 start_index = 1;
1300 argument_index = 1;
1301 }
1302
1303 if (!SetupInvokeArguments(invoke,
1304 number_of_vreg_arguments,
1305 args,
1306 register_index,
1307 is_range,
1308 descriptor,
1309 start_index,
1310 &argument_index)) {
1311 return false;
1312 }
1313
1314 if (clinit_check != nullptr) {
1315 // Add the class initialization check as last input of `invoke`.
1316 DCHECK(invoke->IsInvokeStaticOrDirect());
1317 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1318 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1319 invoke->SetArgumentAt(argument_index, clinit_check);
1320 argument_index++;
1321 }
1322
1323 AppendInstruction(invoke);
1324 latest_result_ = invoke;
1325
1326 return true;
1327}
1328
1329bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1330 uint32_t number_of_vreg_arguments,
1331 uint32_t* args,
1332 uint32_t register_index,
1333 bool is_range,
1334 const char* descriptor) {
1335 DCHECK(invoke->IsInvokeStaticOrDirect());
1336 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1337
1338 size_t start_index = 1;
1339 size_t argument_index = 0;
1340 if (!SetupInvokeArguments(invoke,
1341 number_of_vreg_arguments,
1342 args,
1343 register_index,
1344 is_range,
1345 descriptor,
1346 start_index,
1347 &argument_index)) {
1348 return false;
1349 }
1350
1351 AppendInstruction(invoke);
1352
1353 // This is a StringFactory call, not an actual String constructor. Its result
1354 // replaces the empty String pre-allocated by NewInstance.
1355 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001356 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001357
1358 // Replacing the NewInstance might render it redundant. Keep a list of these
1359 // to be visited once it is clear whether it is has remaining uses.
1360 if (arg_this->IsNewInstance()) {
1361 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1362 } else {
1363 DCHECK(arg_this->IsPhi());
1364 // NewInstance is not the direct input of the StringFactory call. It might
1365 // be redundant but optimizing this case is not worth the effort.
1366 }
1367
1368 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1369 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1370 if ((*current_locals_)[vreg] == arg_this) {
1371 (*current_locals_)[vreg] = invoke;
1372 }
1373 }
1374
1375 return true;
1376}
1377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001378static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001379 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1380 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001381 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001382}
1383
1384bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1385 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001386 bool is_put,
1387 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001388 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1389 uint32_t obj_reg = instruction.VRegB_22c();
1390 uint16_t field_index;
1391 if (instruction.IsQuickened()) {
1392 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001393 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1394 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001395 return false;
1396 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001397 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001398 } else {
1399 field_index = instruction.VRegC_22c();
1400 }
1401
1402 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001403 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001404
Aart Bik14154132016-06-02 17:53:58 -07001405 // Generate an explicit null check on the reference, unless the field access
1406 // is unresolved. In that case, we rely on the runtime to perform various
1407 // checks first, followed by a null check.
1408 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001410 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001412 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001413 if (is_put) {
1414 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1415 HInstruction* field_set = nullptr;
1416 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001417 MaybeRecordStat(compilation_stats_,
1418 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001419 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1420 value,
1421 field_type,
1422 field_index,
1423 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001424 } else {
1425 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001426 field_set = new (allocator_) HInstanceFieldSet(object,
1427 value,
1428 resolved_field,
1429 field_type,
1430 resolved_field->GetOffset(),
1431 resolved_field->IsVolatile(),
1432 field_index,
1433 class_def_index,
1434 *dex_file_,
1435 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001436 }
1437 AppendInstruction(field_set);
1438 } else {
1439 HInstruction* field_get = nullptr;
1440 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001441 MaybeRecordStat(compilation_stats_,
1442 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001443 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1444 field_type,
1445 field_index,
1446 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001447 } else {
1448 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001449 field_get = new (allocator_) HInstanceFieldGet(object,
1450 resolved_field,
1451 field_type,
1452 resolved_field->GetOffset(),
1453 resolved_field->IsVolatile(),
1454 field_index,
1455 class_def_index,
1456 *dex_file_,
1457 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001458 }
1459 AppendInstruction(field_get);
1460 UpdateLocal(source_or_dest_reg, field_get);
1461 }
1462
1463 return true;
1464}
1465
Vladimir Marko28e012a2017-12-07 11:22:59 +00001466static ObjPtr<mirror::Class> GetClassFrom(CompilerDriver* driver,
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001467 const DexCompilationUnit& compilation_unit) {
David Brazdildee58d62016-04-07 09:54:26 +00001468 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001469 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001470 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001471
1472 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1473}
1474
Vladimir Marko28e012a2017-12-07 11:22:59 +00001475ObjPtr<mirror::Class> HInstructionBuilder::GetOutermostCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001476 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1477}
1478
Vladimir Marko28e012a2017-12-07 11:22:59 +00001479ObjPtr<mirror::Class> HInstructionBuilder::GetCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001480 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1481}
1482
Andreas Gampea5b09a62016-11-17 15:21:22 -08001483bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001484 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001485 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001486 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001487 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001488 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1489 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1490 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1491
1492 // GetOutermostCompilingClass returns null when the class is unresolved
1493 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1494 // we are compiling it.
1495 // When this happens we cannot establish a direct relation between the current
1496 // class and the outer class, so we return false.
1497 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001498 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001499}
1500
1501void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001502 uint32_t dex_pc,
1503 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001504 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001505 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1506 uint16_t field_index = instruction.VRegB_21c();
1507
1508 if (is_put) {
1509 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1510 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001511 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001512 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001513 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001514 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1515 }
1516}
1517
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001518ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1519 ScopedObjectAccess soa(Thread::Current());
1520 StackHandleScope<2> hs(soa.Self());
1521
1522 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001523 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001524 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1525
Vladimir Markoe11dd502017-12-08 14:09:45 +00001526 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001527 dex_compilation_unit_->GetDexCache(),
1528 class_loader,
1529 is_static);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001530 if (UNLIKELY(resolved_field == nullptr)) {
1531 // Clean up any exception left by type resolution.
1532 soa.Self()->ClearException();
1533 return nullptr;
1534 }
1535
1536 // Check static/instance. The class linker has a fast path for looking into the dex cache
1537 // and does not check static/instance if it hits it.
1538 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1539 return nullptr;
1540 }
1541
1542 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001543 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001544 if (!resolved_field->IsPublic()) {
1545 return nullptr;
1546 }
1547 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1548 resolved_field,
1549 dex_compilation_unit_->GetDexCache().Get(),
1550 field_idx)) {
1551 return nullptr;
1552 }
1553
1554 if (is_put &&
1555 resolved_field->IsFinal() &&
1556 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1557 // Final fields can only be updated within their own class.
1558 // TODO: Only allow it in constructors. b/34966607.
1559 return nullptr;
1560 }
1561
1562 return resolved_field;
1563}
1564
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001565void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001566 uint32_t dex_pc,
1567 bool is_put) {
1568 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1569 uint16_t field_index = instruction.VRegB_21c();
1570
1571 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001572 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001573
1574 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001575 MaybeRecordStat(compilation_stats_,
1576 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001578 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001579 return;
David Brazdildee58d62016-04-07 09:54:26 +00001580 }
1581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001582 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001583
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001584 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1585 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1586 klass->GetDexFile(),
1587 klass,
1588 dex_pc,
1589 /* needs_access_check */ false);
1590
1591 if (constant == nullptr) {
1592 // The class cannot be referenced from this compiled code. Generate
1593 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001594 MaybeRecordStat(compilation_stats_,
1595 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001596 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001597 return;
David Brazdildee58d62016-04-07 09:54:26 +00001598 }
1599
David Brazdildee58d62016-04-07 09:54:26 +00001600 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001601 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001602 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001603 AppendInstruction(cls);
1604 }
1605
1606 uint16_t class_def_index = klass->GetDexClassDefIndex();
1607 if (is_put) {
1608 // We need to keep the class alive before loading the value.
1609 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1610 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001611 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1612 value,
1613 resolved_field,
1614 field_type,
1615 resolved_field->GetOffset(),
1616 resolved_field->IsVolatile(),
1617 field_index,
1618 class_def_index,
1619 *dex_file_,
1620 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001621 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001622 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1623 resolved_field,
1624 field_type,
1625 resolved_field->GetOffset(),
1626 resolved_field->IsVolatile(),
1627 field_index,
1628 class_def_index,
1629 *dex_file_,
1630 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001631 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1632 }
David Brazdildee58d62016-04-07 09:54:26 +00001633}
1634
1635void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001636 uint16_t first_vreg,
1637 int64_t second_vreg_or_constant,
1638 uint32_t dex_pc,
1639 DataType::Type type,
1640 bool second_is_constant,
1641 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001642 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001643
1644 HInstruction* first = LoadLocal(first_vreg, type);
1645 HInstruction* second = nullptr;
1646 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001647 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001648 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1649 } else {
1650 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1651 }
1652 } else {
1653 second = LoadLocal(second_vreg_or_constant, type);
1654 }
1655
1656 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001657 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1658 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001659 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001660 AppendInstruction(second);
1661 }
1662
1663 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001664 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001665 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001666 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001667 }
1668 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1669}
1670
1671void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1672 uint32_t dex_pc,
1673 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001675 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1676 uint8_t array_reg = instruction.VRegB_23x();
1677 uint8_t index_reg = instruction.VRegC_23x();
1678
David Brazdilc120bbe2016-04-22 16:57:00 +01001679 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001680 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001681 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001683 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001684 AppendInstruction(index);
1685 if (is_put) {
1686 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1687 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001688 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001689 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1690 AppendInstruction(aset);
1691 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001692 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001693 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1694 AppendInstruction(aget);
1695 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1696 }
1697 graph_->SetHasBoundsChecks(true);
1698}
1699
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001700HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1701 dex::TypeIndex type_index,
1702 uint32_t number_of_vreg_arguments,
1703 bool is_range,
1704 uint32_t* args,
1705 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001706 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001707 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001708 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001709 AppendInstruction(object);
1710
1711 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1712 DCHECK_EQ(descriptor[0], '[') << descriptor;
1713 char primitive = descriptor[1];
1714 DCHECK(primitive == 'I'
1715 || primitive == 'L'
1716 || primitive == '[') << descriptor;
1717 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001719
1720 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1721 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1722 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001723 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001724 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1725 AppendInstruction(aset);
1726 }
1727 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001728
1729 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001730}
1731
1732template <typename T>
1733void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1734 const T* data,
1735 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001736 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001737 uint32_t dex_pc) {
1738 for (uint32_t i = 0; i < element_count; ++i) {
1739 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1740 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001741 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001742 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1743 AppendInstruction(aset);
1744 }
1745}
1746
1747void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001748 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001749
1750 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1751 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001752 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1753 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001754 const uint8_t* data = payload->data;
1755 uint32_t element_count = payload->element_count;
1756
Vladimir Markoc69fba22016-09-06 16:49:15 +01001757 if (element_count == 0u) {
1758 // For empty payload we emit only the null check above.
1759 return;
1760 }
1761
Vladimir Markoca6fff82017-10-03 14:49:14 +01001762 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001763 AppendInstruction(length);
1764
David Brazdildee58d62016-04-07 09:54:26 +00001765 // Implementation of this DEX instruction seems to be that the bounds check is
1766 // done before doing any stores.
1767 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001768 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001769
1770 switch (payload->element_width) {
1771 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001772 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001773 reinterpret_cast<const int8_t*>(data),
1774 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001776 dex_pc);
1777 break;
1778 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001779 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001780 reinterpret_cast<const int16_t*>(data),
1781 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001782 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001783 dex_pc);
1784 break;
1785 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001786 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001787 reinterpret_cast<const int32_t*>(data),
1788 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001789 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001790 dex_pc);
1791 break;
1792 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001793 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001794 reinterpret_cast<const int64_t*>(data),
1795 element_count,
1796 dex_pc);
1797 break;
1798 default:
1799 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1800 }
1801 graph_->SetHasBoundsChecks(true);
1802}
1803
1804void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1805 const int64_t* data,
1806 uint32_t element_count,
1807 uint32_t dex_pc) {
1808 for (uint32_t i = 0; i < element_count; ++i) {
1809 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1810 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001811 HArraySet* aset =
1812 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001813 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1814 AppendInstruction(aset);
1815 }
1816}
1817
Vladimir Marko28e012a2017-12-07 11:22:59 +00001818void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1819 HLoadString* load_string =
1820 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1821 HSharpening::ProcessLoadString(load_string,
1822 code_generator_,
1823 compiler_driver_,
1824 *dex_compilation_unit_,
1825 handles_);
1826 AppendInstruction(load_string);
1827}
1828
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001829HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001830 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001831 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00001832 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
1833 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001834 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1835}
1836
1837HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1838 const DexFile& dex_file,
1839 Handle<mirror::Class> klass,
1840 uint32_t dex_pc,
1841 bool needs_access_check) {
1842 // Try to find a reference in the compiling dex file.
1843 const DexFile* actual_dex_file = &dex_file;
1844 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1845 dex::TypeIndex local_type_index =
1846 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1847 if (local_type_index.IsValid()) {
1848 type_index = local_type_index;
1849 actual_dex_file = dex_compilation_unit_->GetDexFile();
1850 }
1851 }
1852
1853 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001854 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001855 graph_->GetCurrentMethod(),
1856 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001857 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001858 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001859 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001860 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001861 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001862
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001863 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1864 code_generator_,
1865 compiler_driver_,
1866 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001867
1868 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1869 // We actually cannot reference this class, we're forced to bail.
1870 return nullptr;
1871 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00001872 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001873 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001874 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001875 return load_class;
1876}
1877
Vladimir Marko175e7862018-03-27 09:03:13 +00001878Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
1879 dex::TypeIndex type_index) {
1880 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
1881 ObjPtr<mirror::Class> klass = compiler_driver_->ResolveClass(
1882 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_);
1883 // TODO: Avoid creating excessive handles if the method references the same class repeatedly.
1884 // (Use a map on the local_allocator_.)
1885 return handles_->NewHandle(klass);
1886}
1887
1888bool HInstructionBuilder::LoadClassNeedsAccessCheck(Handle<mirror::Class> klass) {
1889 if (klass == nullptr) {
1890 return true;
1891 } else if (klass->IsPublic()) {
1892 return false;
1893 } else {
1894 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
1895 return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
1896 }
1897}
1898
Orion Hodson06d10a72018-05-14 08:53:38 +01001899void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001900 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01001901 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
1902 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001903 AppendInstruction(load_method_handle);
1904}
1905
Orion Hodson06d10a72018-05-14 08:53:38 +01001906void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01001907 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1908 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01001909 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01001910 AppendInstruction(load_method_type);
1911}
1912
David Brazdildee58d62016-04-07 09:54:26 +00001913void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1914 uint8_t destination,
1915 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001916 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001917 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001919
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001920 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00001921 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1922 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
1923 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
1924 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
1925 klass.Get(), code_generator_, compiler_driver_, needs_access_check);
1926
1927 HInstruction* class_or_null = nullptr;
1928 HIntConstant* bitstring_path_to_root = nullptr;
1929 HIntConstant* bitstring_mask = nullptr;
1930 if (check_kind == TypeCheckKind::kBitstringCheck) {
1931 // TODO: Allow using the bitstring check also if we need an access check.
1932 DCHECK(!needs_access_check);
1933 class_or_null = graph_->GetNullConstant(dex_pc);
1934 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
1935 uint32_t path_to_root =
1936 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
1937 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
1938 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
1939 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
1940 } else {
1941 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1942 }
1943 DCHECK(class_or_null != nullptr);
1944
David Brazdildee58d62016-04-07 09:54:26 +00001945 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00001946 AppendInstruction(new (allocator_) HInstanceOf(object,
1947 class_or_null,
1948 check_kind,
1949 klass,
1950 dex_pc,
1951 allocator_,
1952 bitstring_path_to_root,
1953 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00001954 UpdateLocal(destination, current_block_->GetLastInstruction());
1955 } else {
1956 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1957 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1958 // which may throw. If it succeeds BoundType sets the new type of `object`
1959 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00001960 AppendInstruction(
1961 new (allocator_) HCheckCast(object,
1962 class_or_null,
1963 check_kind,
1964 klass,
1965 dex_pc,
1966 allocator_,
1967 bitstring_path_to_root,
1968 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001969 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001970 UpdateLocal(reference, current_block_->GetLastInstruction());
1971 }
1972}
1973
Vladimir Marko0b66d612017-03-13 14:50:04 +00001974bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001975 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1976 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001977}
1978
1979bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001980 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001981}
1982
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001983uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1984 DCHECK(CanDecodeQuickenedInfo());
1985 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001986}
1987
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001988bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1989 uint32_t dex_pc,
1990 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001991 switch (instruction.Opcode()) {
1992 case Instruction::CONST_4: {
1993 int32_t register_index = instruction.VRegA();
1994 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1995 UpdateLocal(register_index, constant);
1996 break;
1997 }
1998
1999 case Instruction::CONST_16: {
2000 int32_t register_index = instruction.VRegA();
2001 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2002 UpdateLocal(register_index, constant);
2003 break;
2004 }
2005
2006 case Instruction::CONST: {
2007 int32_t register_index = instruction.VRegA();
2008 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2009 UpdateLocal(register_index, constant);
2010 break;
2011 }
2012
2013 case Instruction::CONST_HIGH16: {
2014 int32_t register_index = instruction.VRegA();
2015 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2016 UpdateLocal(register_index, constant);
2017 break;
2018 }
2019
2020 case Instruction::CONST_WIDE_16: {
2021 int32_t register_index = instruction.VRegA();
2022 // Get 16 bits of constant value, sign extended to 64 bits.
2023 int64_t value = instruction.VRegB_21s();
2024 value <<= 48;
2025 value >>= 48;
2026 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2027 UpdateLocal(register_index, constant);
2028 break;
2029 }
2030
2031 case Instruction::CONST_WIDE_32: {
2032 int32_t register_index = instruction.VRegA();
2033 // Get 32 bits of constant value, sign extended to 64 bits.
2034 int64_t value = instruction.VRegB_31i();
2035 value <<= 32;
2036 value >>= 32;
2037 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2038 UpdateLocal(register_index, constant);
2039 break;
2040 }
2041
2042 case Instruction::CONST_WIDE: {
2043 int32_t register_index = instruction.VRegA();
2044 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2045 UpdateLocal(register_index, constant);
2046 break;
2047 }
2048
2049 case Instruction::CONST_WIDE_HIGH16: {
2050 int32_t register_index = instruction.VRegA();
2051 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2052 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2053 UpdateLocal(register_index, constant);
2054 break;
2055 }
2056
2057 // Note that the SSA building will refine the types.
2058 case Instruction::MOVE:
2059 case Instruction::MOVE_FROM16:
2060 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002062 UpdateLocal(instruction.VRegA(), value);
2063 break;
2064 }
2065
2066 // Note that the SSA building will refine the types.
2067 case Instruction::MOVE_WIDE:
2068 case Instruction::MOVE_WIDE_FROM16:
2069 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002071 UpdateLocal(instruction.VRegA(), value);
2072 break;
2073 }
2074
2075 case Instruction::MOVE_OBJECT:
2076 case Instruction::MOVE_OBJECT_16:
2077 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002078 // The verifier has no notion of a null type, so a move-object of constant 0
2079 // will lead to the same constant 0 in the destination register. To mimic
2080 // this behavior, we just pretend we haven't seen a type change (int to reference)
2081 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2082 // types correct.
2083 uint32_t reg_number = instruction.VRegB();
2084 HInstruction* value = (*current_locals_)[reg_number];
2085 if (value->IsIntConstant()) {
2086 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2087 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2089 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002090 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002092 }
David Brazdildee58d62016-04-07 09:54:26 +00002093 UpdateLocal(instruction.VRegA(), value);
2094 break;
2095 }
2096
2097 case Instruction::RETURN_VOID_NO_BARRIER:
2098 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002100 break;
2101 }
2102
2103#define IF_XX(comparison, cond) \
2104 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2105 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2106
2107 IF_XX(HEqual, EQ);
2108 IF_XX(HNotEqual, NE);
2109 IF_XX(HLessThan, LT);
2110 IF_XX(HLessThanOrEqual, LE);
2111 IF_XX(HGreaterThan, GT);
2112 IF_XX(HGreaterThanOrEqual, GE);
2113
2114 case Instruction::GOTO:
2115 case Instruction::GOTO_16:
2116 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002117 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002118 current_block_ = nullptr;
2119 break;
2120 }
2121
2122 case Instruction::RETURN: {
2123 BuildReturn(instruction, return_type_, dex_pc);
2124 break;
2125 }
2126
2127 case Instruction::RETURN_OBJECT: {
2128 BuildReturn(instruction, return_type_, dex_pc);
2129 break;
2130 }
2131
2132 case Instruction::RETURN_WIDE: {
2133 BuildReturn(instruction, return_type_, dex_pc);
2134 break;
2135 }
2136
2137 case Instruction::INVOKE_DIRECT:
2138 case Instruction::INVOKE_INTERFACE:
2139 case Instruction::INVOKE_STATIC:
2140 case Instruction::INVOKE_SUPER:
2141 case Instruction::INVOKE_VIRTUAL:
2142 case Instruction::INVOKE_VIRTUAL_QUICK: {
2143 uint16_t method_idx;
2144 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2145 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002146 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2147 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002148 return false;
2149 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002150 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002151 } else {
2152 method_idx = instruction.VRegB_35c();
2153 }
2154 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2155 uint32_t args[5];
2156 instruction.GetVarArgs(args);
2157 if (!BuildInvoke(instruction, dex_pc, method_idx,
2158 number_of_vreg_arguments, false, args, -1)) {
2159 return false;
2160 }
2161 break;
2162 }
2163
2164 case Instruction::INVOKE_DIRECT_RANGE:
2165 case Instruction::INVOKE_INTERFACE_RANGE:
2166 case Instruction::INVOKE_STATIC_RANGE:
2167 case Instruction::INVOKE_SUPER_RANGE:
2168 case Instruction::INVOKE_VIRTUAL_RANGE:
2169 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2170 uint16_t method_idx;
2171 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2172 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002173 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2174 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002175 return false;
2176 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002177 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002178 } else {
2179 method_idx = instruction.VRegB_3rc();
2180 }
2181 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2182 uint32_t register_index = instruction.VRegC();
2183 if (!BuildInvoke(instruction, dex_pc, method_idx,
2184 number_of_vreg_arguments, true, nullptr, register_index)) {
2185 return false;
2186 }
2187 break;
2188 }
2189
Orion Hodsonac141392017-01-13 11:53:47 +00002190 case Instruction::INVOKE_POLYMORPHIC: {
2191 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002192 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002193 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2194 uint32_t args[5];
2195 instruction.GetVarArgs(args);
2196 return BuildInvokePolymorphic(instruction,
2197 dex_pc,
2198 method_idx,
2199 proto_idx,
2200 number_of_vreg_arguments,
2201 false,
2202 args,
2203 -1);
2204 }
2205
2206 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2207 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002208 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Orion Hodsonac141392017-01-13 11:53:47 +00002209 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2210 uint32_t register_index = instruction.VRegC_4rcc();
2211 return BuildInvokePolymorphic(instruction,
2212 dex_pc,
2213 method_idx,
2214 proto_idx,
2215 number_of_vreg_arguments,
2216 true,
2217 nullptr,
2218 register_index);
2219 }
2220
David Brazdildee58d62016-04-07 09:54:26 +00002221 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002223 break;
2224 }
2225
2226 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002228 break;
2229 }
2230
2231 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002233 break;
2234 }
2235
2236 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002238 break;
2239 }
2240
2241 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002243 break;
2244 }
2245
2246 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002248 break;
2249 }
2250
2251 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002253 break;
2254 }
2255
2256 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002258 break;
2259 }
2260
2261 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002263 break;
2264 }
2265
2266 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002267 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002268 break;
2269 }
2270
2271 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002272 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002273 break;
2274 }
2275
2276 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002278 break;
2279 }
2280
2281 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002283 break;
2284 }
2285
2286 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002287 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002288 break;
2289 }
2290
2291 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002293 break;
2294 }
2295
2296 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002298 break;
2299 }
2300
2301 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002303 break;
2304 }
2305
2306 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002308 break;
2309 }
2310
2311 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002313 break;
2314 }
2315
2316 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002318 break;
2319 }
2320
2321 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002323 break;
2324 }
2325
2326 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002328 break;
2329 }
2330
2331 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002332 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002333 break;
2334 }
2335
2336 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002338 break;
2339 }
2340
2341 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002343 break;
2344 }
2345
2346 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002348 break;
2349 }
2350
2351 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002353 break;
2354 }
2355
2356 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002358 break;
2359 }
2360
2361 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002363 break;
2364 }
2365
2366 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002368 break;
2369 }
2370
2371 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002373 break;
2374 }
2375
2376 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002378 break;
2379 }
2380
2381 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002382 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002383 break;
2384 }
2385
2386 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002388 break;
2389 }
2390
2391 case Instruction::DIV_INT: {
2392 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002394 break;
2395 }
2396
2397 case Instruction::DIV_LONG: {
2398 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002400 break;
2401 }
2402
2403 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002405 break;
2406 }
2407
2408 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002410 break;
2411 }
2412
2413 case Instruction::REM_INT: {
2414 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002416 break;
2417 }
2418
2419 case Instruction::REM_LONG: {
2420 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002422 break;
2423 }
2424
2425 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002427 break;
2428 }
2429
2430 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002432 break;
2433 }
2434
2435 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002437 break;
2438 }
2439
2440 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002442 break;
2443 }
2444
2445 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002447 break;
2448 }
2449
2450 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002452 break;
2453 }
2454
2455 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002457 break;
2458 }
2459
2460 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002462 break;
2463 }
2464
2465 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002467 break;
2468 }
2469
2470 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002472 break;
2473 }
2474
2475 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002477 break;
2478 }
2479
2480 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002482 break;
2483 }
2484
2485 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002487 break;
2488 }
2489
2490 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002492 break;
2493 }
2494
2495 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002497 break;
2498 }
2499
2500 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002502 break;
2503 }
2504
2505 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002507 break;
2508 }
2509
2510 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002512 break;
2513 }
2514
2515 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002517 break;
2518 }
2519
2520 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002522 break;
2523 }
2524
2525 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002527 break;
2528 }
2529
2530 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002532 break;
2533 }
2534
2535 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002537 break;
2538 }
2539
2540 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002542 break;
2543 }
2544
2545 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002547 break;
2548 }
2549
2550 case Instruction::DIV_INT_2ADDR: {
2551 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002553 break;
2554 }
2555
2556 case Instruction::DIV_LONG_2ADDR: {
2557 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002558 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002559 break;
2560 }
2561
2562 case Instruction::REM_INT_2ADDR: {
2563 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002565 break;
2566 }
2567
2568 case Instruction::REM_LONG_2ADDR: {
2569 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002571 break;
2572 }
2573
2574 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002576 break;
2577 }
2578
2579 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002581 break;
2582 }
2583
2584 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002585 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002586 break;
2587 }
2588
2589 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002591 break;
2592 }
2593
2594 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002596 break;
2597 }
2598
2599 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002601 break;
2602 }
2603
2604 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002606 break;
2607 }
2608
2609 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002611 break;
2612 }
2613
2614 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002616 break;
2617 }
2618
2619 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002621 break;
2622 }
2623
2624 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002625 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002626 break;
2627 }
2628
2629 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002631 break;
2632 }
2633
2634 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002636 break;
2637 }
2638
2639 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002641 break;
2642 }
2643
2644 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002646 break;
2647 }
2648
2649 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002651 break;
2652 }
2653
2654 case Instruction::ADD_INT_LIT16: {
2655 Binop_22s<HAdd>(instruction, false, dex_pc);
2656 break;
2657 }
2658
2659 case Instruction::AND_INT_LIT16: {
2660 Binop_22s<HAnd>(instruction, false, dex_pc);
2661 break;
2662 }
2663
2664 case Instruction::OR_INT_LIT16: {
2665 Binop_22s<HOr>(instruction, false, dex_pc);
2666 break;
2667 }
2668
2669 case Instruction::XOR_INT_LIT16: {
2670 Binop_22s<HXor>(instruction, false, dex_pc);
2671 break;
2672 }
2673
2674 case Instruction::RSUB_INT: {
2675 Binop_22s<HSub>(instruction, true, dex_pc);
2676 break;
2677 }
2678
2679 case Instruction::MUL_INT_LIT16: {
2680 Binop_22s<HMul>(instruction, false, dex_pc);
2681 break;
2682 }
2683
2684 case Instruction::ADD_INT_LIT8: {
2685 Binop_22b<HAdd>(instruction, false, dex_pc);
2686 break;
2687 }
2688
2689 case Instruction::AND_INT_LIT8: {
2690 Binop_22b<HAnd>(instruction, false, dex_pc);
2691 break;
2692 }
2693
2694 case Instruction::OR_INT_LIT8: {
2695 Binop_22b<HOr>(instruction, false, dex_pc);
2696 break;
2697 }
2698
2699 case Instruction::XOR_INT_LIT8: {
2700 Binop_22b<HXor>(instruction, false, dex_pc);
2701 break;
2702 }
2703
2704 case Instruction::RSUB_INT_LIT8: {
2705 Binop_22b<HSub>(instruction, true, dex_pc);
2706 break;
2707 }
2708
2709 case Instruction::MUL_INT_LIT8: {
2710 Binop_22b<HMul>(instruction, false, dex_pc);
2711 break;
2712 }
2713
2714 case Instruction::DIV_INT_LIT16:
2715 case Instruction::DIV_INT_LIT8: {
2716 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002718 break;
2719 }
2720
2721 case Instruction::REM_INT_LIT16:
2722 case Instruction::REM_INT_LIT8: {
2723 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002724 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002725 break;
2726 }
2727
2728 case Instruction::SHL_INT_LIT8: {
2729 Binop_22b<HShl>(instruction, false, dex_pc);
2730 break;
2731 }
2732
2733 case Instruction::SHR_INT_LIT8: {
2734 Binop_22b<HShr>(instruction, false, dex_pc);
2735 break;
2736 }
2737
2738 case Instruction::USHR_INT_LIT8: {
2739 Binop_22b<HUShr>(instruction, false, dex_pc);
2740 break;
2741 }
2742
2743 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002744 HNewInstance* new_instance =
2745 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2746 DCHECK(new_instance != nullptr);
2747
David Brazdildee58d62016-04-07 09:54:26 +00002748 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002749 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002750 break;
2751 }
2752
2753 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002754 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002756 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002757
Vladimir Markoca6fff82017-10-03 14:49:14 +01002758 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002759 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002760 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002761 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002762 break;
2763 }
2764
2765 case Instruction::FILLED_NEW_ARRAY: {
2766 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002767 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002768 uint32_t args[5];
2769 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002770 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2771 type_index,
2772 number_of_vreg_arguments,
2773 /* is_range */ false,
2774 args,
2775 /* register_index */ 0);
2776 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002777 break;
2778 }
2779
2780 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2781 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002782 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002783 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002784 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2785 type_index,
2786 number_of_vreg_arguments,
2787 /* is_range */ true,
2788 /* args*/ nullptr,
2789 register_index);
2790 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002791 break;
2792 }
2793
2794 case Instruction::FILL_ARRAY_DATA: {
2795 BuildFillArrayData(instruction, dex_pc);
2796 break;
2797 }
2798
2799 case Instruction::MOVE_RESULT:
2800 case Instruction::MOVE_RESULT_WIDE:
2801 case Instruction::MOVE_RESULT_OBJECT: {
2802 DCHECK(latest_result_ != nullptr);
2803 UpdateLocal(instruction.VRegA(), latest_result_);
2804 latest_result_ = nullptr;
2805 break;
2806 }
2807
2808 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002810 break;
2811 }
2812
2813 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002815 break;
2816 }
2817
2818 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002820 break;
2821 }
2822
2823 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002825 break;
2826 }
2827
2828 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002830 break;
2831 }
2832
2833 case Instruction::NOP:
2834 break;
2835
2836 case Instruction::IGET:
2837 case Instruction::IGET_QUICK:
2838 case Instruction::IGET_WIDE:
2839 case Instruction::IGET_WIDE_QUICK:
2840 case Instruction::IGET_OBJECT:
2841 case Instruction::IGET_OBJECT_QUICK:
2842 case Instruction::IGET_BOOLEAN:
2843 case Instruction::IGET_BOOLEAN_QUICK:
2844 case Instruction::IGET_BYTE:
2845 case Instruction::IGET_BYTE_QUICK:
2846 case Instruction::IGET_CHAR:
2847 case Instruction::IGET_CHAR_QUICK:
2848 case Instruction::IGET_SHORT:
2849 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002850 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002851 return false;
2852 }
2853 break;
2854 }
2855
2856 case Instruction::IPUT:
2857 case Instruction::IPUT_QUICK:
2858 case Instruction::IPUT_WIDE:
2859 case Instruction::IPUT_WIDE_QUICK:
2860 case Instruction::IPUT_OBJECT:
2861 case Instruction::IPUT_OBJECT_QUICK:
2862 case Instruction::IPUT_BOOLEAN:
2863 case Instruction::IPUT_BOOLEAN_QUICK:
2864 case Instruction::IPUT_BYTE:
2865 case Instruction::IPUT_BYTE_QUICK:
2866 case Instruction::IPUT_CHAR:
2867 case Instruction::IPUT_CHAR_QUICK:
2868 case Instruction::IPUT_SHORT:
2869 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002870 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002871 return false;
2872 }
2873 break;
2874 }
2875
2876 case Instruction::SGET:
2877 case Instruction::SGET_WIDE:
2878 case Instruction::SGET_OBJECT:
2879 case Instruction::SGET_BOOLEAN:
2880 case Instruction::SGET_BYTE:
2881 case Instruction::SGET_CHAR:
2882 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002883 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00002884 break;
2885 }
2886
2887 case Instruction::SPUT:
2888 case Instruction::SPUT_WIDE:
2889 case Instruction::SPUT_OBJECT:
2890 case Instruction::SPUT_BOOLEAN:
2891 case Instruction::SPUT_BYTE:
2892 case Instruction::SPUT_CHAR:
2893 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002894 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002895 break;
2896 }
2897
2898#define ARRAY_XX(kind, anticipated_type) \
2899 case Instruction::AGET##kind: { \
2900 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2901 break; \
2902 } \
2903 case Instruction::APUT##kind: { \
2904 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2905 break; \
2906 }
2907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 ARRAY_XX(, DataType::Type::kInt32);
2909 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2910 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2911 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2912 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2913 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2914 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002915
2916 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002917 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002918 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002919 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2920 break;
2921 }
2922
2923 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002924 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002925 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002926 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2927 break;
2928 }
2929
2930 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002931 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002932 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002933 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2934 break;
2935 }
2936
2937 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002938 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002939 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002940 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2941 break;
2942 }
2943
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002944 case Instruction::CONST_METHOD_HANDLE: {
2945 uint16_t method_handle_idx = instruction.VRegB_21c();
2946 BuildLoadMethodHandle(method_handle_idx, dex_pc);
2947 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2948 break;
2949 }
2950
Orion Hodson18259d72018-04-12 11:18:23 +01002951 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01002952 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01002953 BuildLoadMethodType(proto_idx, dex_pc);
2954 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2955 break;
2956 }
2957
David Brazdildee58d62016-04-07 09:54:26 +00002958 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002959 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002960 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002961 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002962 break;
2963 }
2964
2965 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002967 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002968 // We finished building this block. Set the current block to null to avoid
2969 // adding dead instructions to it.
2970 current_block_ = nullptr;
2971 break;
2972 }
2973
2974 case Instruction::INSTANCE_OF: {
2975 uint8_t destination = instruction.VRegA_22c();
2976 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002977 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002978 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2979 break;
2980 }
2981
2982 case Instruction::CHECK_CAST: {
2983 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002984 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002985 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2986 break;
2987 }
2988
2989 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002990 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002992 HMonitorOperation::OperationKind::kEnter,
2993 dex_pc));
2994 break;
2995 }
2996
2997 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002998 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002999 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003000 HMonitorOperation::OperationKind::kExit,
3001 dex_pc));
3002 break;
3003 }
3004
3005 case Instruction::SPARSE_SWITCH:
3006 case Instruction::PACKED_SWITCH: {
3007 BuildSwitch(instruction, dex_pc);
3008 break;
3009 }
3010
3011 default:
3012 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003013 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003014 << " because of unhandled instruction "
3015 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003016 MaybeRecordStat(compilation_stats_,
3017 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003018 return false;
3019 }
3020 return true;
3021} // NOLINT(readability/fn_size)
3022
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003023ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3024 dex::TypeIndex type_index,
3025 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003026 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003027 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3028}
3029
3030ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3031 // TODO: Cache the result in a Handle<mirror::Class>.
3032 const DexFile::MethodId& method_id =
3033 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3034 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3035}
3036
David Brazdildee58d62016-04-07 09:54:26 +00003037} // namespace art