blob: f7d67db5b20ad01232f2f3cb4d80ae10f5682288 [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"
David Brazdildee58d62016-04-07 09:54:26 +000020#include "bytecode_utils.h"
21#include "class_linker.h"
Andreas Gampe26de38b2016-07-27 17:53:11 -070022#include "dex_instruction-inl.h"
David Brazdildee58d62016-04-07 09:54:26 +000023#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070024#include "imtable-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
David Brazdildee58d62016-04-07 09:54:26 +000026
27namespace art {
28
29void HInstructionBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
30 if (compilation_stats_ != nullptr) {
31 compilation_stats_->RecordStat(compilation_stat);
32 }
33}
34
35HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
36 return block_builder_->GetBlockAt(dex_pc);
37}
38
39ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
40 ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
41 const size_t vregs = graph_->GetNumberOfVRegs();
42 if (locals->size() != vregs) {
43 locals->resize(vregs, nullptr);
44
45 if (block->IsCatchBlock()) {
46 // We record incoming inputs of catch phis at throwing instructions and
47 // must therefore eagerly create the phis. Phis for undefined vregs will
48 // be deleted when the first throwing instruction with the vreg undefined
49 // is encountered. Unused phis will be removed by dead phi analysis.
50 for (size_t i = 0; i < vregs; ++i) {
51 // No point in creating the catch phi if it is already undefined at
52 // the first throwing instruction.
53 HInstruction* current_local_value = (*current_locals_)[i];
54 if (current_local_value != nullptr) {
55 HPhi* phi = new (arena_) HPhi(
56 arena_,
57 i,
58 0,
59 current_local_value->GetType());
60 block->AddPhi(phi);
61 (*locals)[i] = phi;
62 }
63 }
64 }
65 }
66 return locals;
67}
68
69HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
70 ArenaVector<HInstruction*>* locals = GetLocalsFor(block);
71 return (*locals)[local];
72}
73
74void HInstructionBuilder::InitializeBlockLocals() {
75 current_locals_ = GetLocalsFor(current_block_);
76
77 if (current_block_->IsCatchBlock()) {
78 // Catch phis were already created and inputs collected from throwing sites.
79 if (kIsDebugBuild) {
80 // Make sure there was at least one throwing instruction which initialized
81 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
82 // visited already (from HTryBoundary scoping and reverse post order).
83 bool catch_block_visited = false;
84 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
85 HBasicBlock* current = it.Current();
86 if (current == current_block_) {
87 catch_block_visited = true;
88 } else if (current->IsTryBlock()) {
89 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
90 if (try_entry.HasExceptionHandler(*current_block_)) {
91 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
92 }
93 }
94 }
95 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
96 << "No instructions throwing into a live catch block.";
97 }
98 } else if (current_block_->IsLoopHeader()) {
99 // If the block is a loop header, we know we only have visited the pre header
100 // because we are visiting in reverse post order. We create phis for all initialized
101 // locals from the pre header. Their inputs will be populated at the end of
102 // the analysis.
103 for (size_t local = 0; local < current_locals_->size(); ++local) {
104 HInstruction* incoming =
105 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
106 if (incoming != nullptr) {
107 HPhi* phi = new (arena_) HPhi(
108 arena_,
109 local,
110 0,
111 incoming->GetType());
112 current_block_->AddPhi(phi);
113 (*current_locals_)[local] = phi;
114 }
115 }
116
117 // Save the loop header so that the last phase of the analysis knows which
118 // blocks need to be updated.
119 loop_headers_.push_back(current_block_);
120 } else if (current_block_->GetPredecessors().size() > 0) {
121 // All predecessors have already been visited because we are visiting in reverse post order.
122 // We merge the values of all locals, creating phis if those values differ.
123 for (size_t local = 0; local < current_locals_->size(); ++local) {
124 bool one_predecessor_has_no_value = false;
125 bool is_different = false;
126 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
127
128 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
129 HInstruction* current = ValueOfLocalAt(predecessor, local);
130 if (current == nullptr) {
131 one_predecessor_has_no_value = true;
132 break;
133 } else if (current != value) {
134 is_different = true;
135 }
136 }
137
138 if (one_predecessor_has_no_value) {
139 // If one predecessor has no value for this local, we trust the verifier has
140 // successfully checked that there is a store dominating any read after this block.
141 continue;
142 }
143
144 if (is_different) {
145 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
146 HPhi* phi = new (arena_) HPhi(
147 arena_,
148 local,
149 current_block_->GetPredecessors().size(),
150 first_input->GetType());
151 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
152 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
153 phi->SetRawInputAt(i, pred_value);
154 }
155 current_block_->AddPhi(phi);
156 value = phi;
157 }
158 (*current_locals_)[local] = value;
159 }
160 }
161}
162
163void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
164 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
165 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
166 ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
167 DCHECK_EQ(handler_locals->size(), current_locals_->size());
168 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
169 HInstruction* handler_value = (*handler_locals)[vreg];
170 if (handler_value == nullptr) {
171 // Vreg was undefined at a previously encountered throwing instruction
172 // and the catch phi was deleted. Do not record the local value.
173 continue;
174 }
175 DCHECK(handler_value->IsPhi());
176
177 HInstruction* local_value = (*current_locals_)[vreg];
178 if (local_value == nullptr) {
179 // This is the first instruction throwing into `catch_block` where
180 // `vreg` is undefined. Delete the catch phi.
181 catch_block->RemovePhi(handler_value->AsPhi());
182 (*handler_locals)[vreg] = nullptr;
183 } else {
184 // Vreg has been defined at all instructions throwing into `catch_block`
185 // encountered so far. Record the local value in the catch phi.
186 handler_value->AsPhi()->AddInput(local_value);
187 }
188 }
189 }
190}
191
192void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
193 current_block_->AddInstruction(instruction);
194 InitializeInstruction(instruction);
195}
196
197void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
198 if (current_block_->GetInstructions().IsEmpty()) {
199 current_block_->AddInstruction(instruction);
200 } else {
201 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
202 }
203 InitializeInstruction(instruction);
204}
205
206void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
207 if (instruction->NeedsEnvironment()) {
208 HEnvironment* environment = new (arena_) HEnvironment(
209 arena_,
210 current_locals_->size(),
211 graph_->GetDexFile(),
212 graph_->GetMethodIdx(),
213 instruction->GetDexPc(),
214 graph_->GetInvokeType(),
215 instruction);
216 environment->CopyFrom(*current_locals_);
217 instruction->SetRawEnvironment(environment);
218 }
219}
220
David Brazdilc120bbe2016-04-22 16:57:00 +0100221HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
222 HInstruction* ref = LoadLocal(register_index, Primitive::kPrimNot);
223 if (!ref->CanBeNull()) {
224 return ref;
225 }
226
227 HNullCheck* null_check = new (arena_) HNullCheck(ref, dex_pc);
228 AppendInstruction(null_check);
229 return null_check;
230}
231
David Brazdildee58d62016-04-07 09:54:26 +0000232void HInstructionBuilder::SetLoopHeaderPhiInputs() {
233 for (size_t i = loop_headers_.size(); i > 0; --i) {
234 HBasicBlock* block = loop_headers_[i - 1];
235 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
236 HPhi* phi = it.Current()->AsPhi();
237 size_t vreg = phi->GetRegNumber();
238 for (HBasicBlock* predecessor : block->GetPredecessors()) {
239 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
240 if (value == nullptr) {
241 // Vreg is undefined at this predecessor. Mark it dead and leave with
242 // fewer inputs than predecessors. SsaChecker will fail if not removed.
243 phi->SetDead();
244 break;
245 } else {
246 phi->AddInput(value);
247 }
248 }
249 }
250 }
251}
252
253static bool IsBlockPopulated(HBasicBlock* block) {
254 if (block->IsLoopHeader()) {
255 // Suspend checks were inserted into loop headers during building of dominator tree.
256 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
257 return block->GetFirstInstruction() != block->GetLastInstruction();
258 } else {
259 return !block->GetInstructions().IsEmpty();
260 }
261}
262
263bool HInstructionBuilder::Build() {
264 locals_for_.resize(graph_->GetBlocks().size(),
265 ArenaVector<HInstruction*>(arena_->Adapter(kArenaAllocGraphBuilder)));
266
267 // Find locations where we want to generate extra stackmaps for native debugging.
268 // This allows us to generate the info only at interesting points (for example,
269 // at start of java statement) rather than before every dex instruction.
270 const bool native_debuggable = compiler_driver_ != nullptr &&
271 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
272 ArenaBitVector* native_debug_info_locations = nullptr;
273 if (native_debuggable) {
274 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
275 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
276 FindNativeDebugInfoLocations(native_debug_info_locations);
277 }
278
279 for (HReversePostOrderIterator block_it(*graph_); !block_it.Done(); block_it.Advance()) {
280 current_block_ = block_it.Current();
281 uint32_t block_dex_pc = current_block_->GetDexPc();
282
283 InitializeBlockLocals();
284
285 if (current_block_->IsEntryBlock()) {
286 InitializeParameters();
287 AppendInstruction(new (arena_) HSuspendCheck(0u));
288 AppendInstruction(new (arena_) HGoto(0u));
289 continue;
290 } else if (current_block_->IsExitBlock()) {
291 AppendInstruction(new (arena_) HExit());
292 continue;
293 } else if (current_block_->IsLoopHeader()) {
294 HSuspendCheck* suspend_check = new (arena_) HSuspendCheck(current_block_->GetDexPc());
295 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
296 // This is slightly odd because the loop header might not be empty (TryBoundary).
297 // But we're still creating the environment with locals from the top of the block.
298 InsertInstructionAtTop(suspend_check);
299 }
300
301 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
302 // Synthetic block that does not need to be populated.
303 DCHECK(IsBlockPopulated(current_block_));
304 continue;
305 }
306
307 DCHECK(!IsBlockPopulated(current_block_));
308
309 for (CodeItemIterator it(code_item_, block_dex_pc); !it.Done(); it.Advance()) {
310 if (current_block_ == nullptr) {
311 // The previous instruction ended this block.
312 break;
313 }
314
315 uint32_t dex_pc = it.CurrentDexPc();
316 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
317 // This dex_pc starts a new basic block.
318 break;
319 }
320
321 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(it.CurrentInstruction())) {
322 PropagateLocalsToCatchBlocks();
323 }
324
325 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
326 AppendInstruction(new (arena_) HNativeDebugInfo(dex_pc));
327 }
328
329 if (!ProcessDexInstruction(it.CurrentInstruction(), dex_pc)) {
330 return false;
331 }
332 }
333
334 if (current_block_ != nullptr) {
335 // Branching instructions clear current_block, so we know the last
336 // instruction of the current block is not a branching instruction.
337 // We add an unconditional Goto to the next block.
338 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
339 AppendInstruction(new (arena_) HGoto());
340 }
341 }
342
343 SetLoopHeaderPhiInputs();
344
345 return true;
346}
347
348void HInstructionBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
349 // The callback gets called when the line number changes.
350 // In other words, it marks the start of new java statement.
351 struct Callback {
352 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
353 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
354 return false;
355 }
356 };
357 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
358 // Instruction-specific tweaks.
359 const Instruction* const begin = Instruction::At(code_item_.insns_);
360 const Instruction* const end = begin->RelativeAt(code_item_.insns_size_in_code_units_);
361 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
362 switch (inst->Opcode()) {
363 case Instruction::MOVE_EXCEPTION: {
364 // Stop in native debugger after the exception has been moved.
365 // The compiler also expects the move at the start of basic block so
366 // we do not want to interfere by inserting native-debug-info before it.
367 locations->ClearBit(inst->GetDexPc(code_item_.insns_));
368 const Instruction* next = inst->Next();
369 if (next < end) {
370 locations->SetBit(next->GetDexPc(code_item_.insns_));
371 }
372 break;
373 }
374 default:
375 break;
376 }
377 }
378}
379
380HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, Primitive::Type type) const {
381 HInstruction* value = (*current_locals_)[reg_number];
382 DCHECK(value != nullptr);
383
384 // If the operation requests a specific type, we make sure its input is of that type.
385 if (type != value->GetType()) {
386 if (Primitive::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700387 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
David Brazdildee58d62016-04-07 09:54:26 +0000388 } else if (type == Primitive::kPrimNot) {
Aart Bik31883642016-06-06 15:02:44 -0700389 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000390 }
Aart Bik31883642016-06-06 15:02:44 -0700391 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000392 }
393
394 return value;
395}
396
397void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
398 Primitive::Type stored_type = stored_value->GetType();
399 DCHECK_NE(stored_type, Primitive::kPrimVoid);
400
401 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
402 // registers. Consider the following cases:
403 // (1) Storing a wide value must overwrite previous values in both `reg_number`
404 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
405 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
406 // must invalidate it. We store `nullptr` in `reg_number-1`.
407 // Consequently, storing a wide value into the high vreg of another wide value
408 // will invalidate both `reg_number-1` and `reg_number+1`.
409
410 if (reg_number != 0) {
411 HInstruction* local_low = (*current_locals_)[reg_number - 1];
412 if (local_low != nullptr && Primitive::Is64BitType(local_low->GetType())) {
413 // The vreg we are storing into was previously the high vreg of a pair.
414 // We need to invalidate its low vreg.
415 DCHECK((*current_locals_)[reg_number] == nullptr);
416 (*current_locals_)[reg_number - 1] = nullptr;
417 }
418 }
419
420 (*current_locals_)[reg_number] = stored_value;
421 if (Primitive::Is64BitType(stored_type)) {
422 // We are storing a pair. Invalidate the instruction in the high vreg.
423 (*current_locals_)[reg_number + 1] = nullptr;
424 }
425}
426
427void HInstructionBuilder::InitializeParameters() {
428 DCHECK(current_block_->IsEntryBlock());
429
430 // dex_compilation_unit_ is null only when unit testing.
431 if (dex_compilation_unit_ == nullptr) {
432 return;
433 }
434
435 const char* shorty = dex_compilation_unit_->GetShorty();
436 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
437 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
438 uint16_t parameter_index = 0;
439
440 const DexFile::MethodId& referrer_method_id =
441 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
442 if (!dex_compilation_unit_->IsStatic()) {
443 // Add the implicit 'this' argument, not expressed in the signature.
444 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
445 referrer_method_id.class_idx_,
446 parameter_index++,
447 Primitive::kPrimNot,
448 true);
449 AppendInstruction(parameter);
450 UpdateLocal(locals_index++, parameter);
451 number_of_parameters--;
452 }
453
454 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
455 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
456 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
457 HParameterValue* parameter = new (arena_) HParameterValue(
458 *dex_file_,
459 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
460 parameter_index++,
461 Primitive::GetType(shorty[shorty_pos]),
462 false);
463 ++shorty_pos;
464 AppendInstruction(parameter);
465 // Store the parameter value in the local that the dex code will use
466 // to reference that parameter.
467 UpdateLocal(locals_index++, parameter);
468 if (Primitive::Is64BitType(parameter->GetType())) {
469 i++;
470 locals_index++;
471 parameter_index++;
472 }
473 }
474}
475
476template<typename T>
477void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
478 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
479 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
480 T* comparison = new (arena_) T(first, second, dex_pc);
481 AppendInstruction(comparison);
482 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
483 current_block_ = nullptr;
484}
485
486template<typename T>
487void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
488 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
489 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
490 AppendInstruction(comparison);
491 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
492 current_block_ = nullptr;
493}
494
495template<typename T>
496void HInstructionBuilder::Unop_12x(const Instruction& instruction,
497 Primitive::Type type,
498 uint32_t dex_pc) {
499 HInstruction* first = LoadLocal(instruction.VRegB(), type);
500 AppendInstruction(new (arena_) T(type, first, dex_pc));
501 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
502}
503
504void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
505 Primitive::Type input_type,
506 Primitive::Type result_type,
507 uint32_t dex_pc) {
508 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
509 AppendInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
510 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
511}
512
513template<typename T>
514void HInstructionBuilder::Binop_23x(const Instruction& instruction,
515 Primitive::Type type,
516 uint32_t dex_pc) {
517 HInstruction* first = LoadLocal(instruction.VRegB(), type);
518 HInstruction* second = LoadLocal(instruction.VRegC(), type);
519 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
520 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
521}
522
523template<typename T>
524void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
525 Primitive::Type type,
526 uint32_t dex_pc) {
527 HInstruction* first = LoadLocal(instruction.VRegB(), type);
528 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
529 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
530 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
531}
532
533void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
534 Primitive::Type type,
535 ComparisonBias bias,
536 uint32_t dex_pc) {
537 HInstruction* first = LoadLocal(instruction.VRegB(), type);
538 HInstruction* second = LoadLocal(instruction.VRegC(), type);
539 AppendInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
540 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
541}
542
543template<typename T>
544void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
545 Primitive::Type type,
546 uint32_t dex_pc) {
547 HInstruction* first = LoadLocal(instruction.VRegA(), type);
548 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
549 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
550 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
551}
552
553template<typename T>
554void HInstructionBuilder::Binop_12x(const Instruction& instruction,
555 Primitive::Type type,
556 uint32_t dex_pc) {
557 HInstruction* first = LoadLocal(instruction.VRegA(), type);
558 HInstruction* second = LoadLocal(instruction.VRegB(), type);
559 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
560 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
561}
562
563template<typename T>
564void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
565 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
566 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
567 if (reverse) {
568 std::swap(first, second);
569 }
570 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
571 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
572}
573
574template<typename T>
575void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
576 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
577 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
578 if (reverse) {
579 std::swap(first, second);
580 }
581 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
582 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
583}
584
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700585static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
David Brazdildee58d62016-04-07 09:54:26 +0000586 Thread* self = Thread::Current();
587 return cu->IsConstructor()
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700588 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000589}
590
591// Returns true if `block` has only one successor which starts at the next
592// dex_pc after `instruction` at `dex_pc`.
593static bool IsFallthroughInstruction(const Instruction& instruction,
594 uint32_t dex_pc,
595 HBasicBlock* block) {
596 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
597 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
598}
599
600void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
601 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
602 DexSwitchTable table(instruction, dex_pc);
603
604 if (table.GetNumEntries() == 0) {
605 // Empty Switch. Code falls through to the next block.
606 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
607 AppendInstruction(new (arena_) HGoto(dex_pc));
608 } else if (table.ShouldBuildDecisionTree()) {
609 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
610 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
611 HEqual* comparison = new (arena_) HEqual(value, case_value, dex_pc);
612 AppendInstruction(comparison);
613 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
614
615 if (!it.IsLast()) {
616 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
617 }
618 }
619 } else {
620 AppendInstruction(
621 new (arena_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
622 }
623
624 current_block_ = nullptr;
625}
626
627void HInstructionBuilder::BuildReturn(const Instruction& instruction,
628 Primitive::Type type,
629 uint32_t dex_pc) {
630 if (type == Primitive::kPrimVoid) {
631 if (graph_->ShouldGenerateConstructorBarrier()) {
632 // The compilation unit is null during testing.
633 if (dex_compilation_unit_ != nullptr) {
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700634 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_))
David Brazdildee58d62016-04-07 09:54:26 +0000635 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
636 }
637 AppendInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
638 }
639 AppendInstruction(new (arena_) HReturnVoid(dex_pc));
640 } else {
641 HInstruction* value = LoadLocal(instruction.VRegA(), type);
642 AppendInstruction(new (arena_) HReturn(value, dex_pc));
643 }
644 current_block_ = nullptr;
645}
646
647static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
648 switch (opcode) {
649 case Instruction::INVOKE_STATIC:
650 case Instruction::INVOKE_STATIC_RANGE:
651 return kStatic;
652 case Instruction::INVOKE_DIRECT:
653 case Instruction::INVOKE_DIRECT_RANGE:
654 return kDirect;
655 case Instruction::INVOKE_VIRTUAL:
656 case Instruction::INVOKE_VIRTUAL_QUICK:
657 case Instruction::INVOKE_VIRTUAL_RANGE:
658 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
659 return kVirtual;
660 case Instruction::INVOKE_INTERFACE:
661 case Instruction::INVOKE_INTERFACE_RANGE:
662 return kInterface;
663 case Instruction::INVOKE_SUPER_RANGE:
664 case Instruction::INVOKE_SUPER:
665 return kSuper;
666 default:
667 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
668 UNREACHABLE();
669 }
670}
671
672ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
673 ScopedObjectAccess soa(Thread::Current());
674 StackHandleScope<3> hs(soa.Self());
675
676 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
677 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700678 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +0000679 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100680 // We fetch the referenced class eagerly (that is, the class pointed by in the MethodId
681 // at method_idx), as `CanAccessResolvedMethod` expects it be be in the dex cache.
682 Handle<mirror::Class> methods_class(hs.NewHandle(class_linker->ResolveReferencedClassOfMethod(
683 method_idx, dex_compilation_unit_->GetDexCache(), class_loader)));
684
685 if (UNLIKELY(methods_class.Get() == nullptr)) {
686 // Clean up any exception left by type resolution.
687 soa.Self()->ClearException();
688 return nullptr;
689 }
David Brazdildee58d62016-04-07 09:54:26 +0000690
691 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
692 *dex_compilation_unit_->GetDexFile(),
693 method_idx,
694 dex_compilation_unit_->GetDexCache(),
695 class_loader,
696 /* referrer */ nullptr,
697 invoke_type);
698
699 if (UNLIKELY(resolved_method == nullptr)) {
700 // Clean up any exception left by type resolution.
701 soa.Self()->ClearException();
702 return nullptr;
703 }
704
705 // Check access. The class linker has a fast path for looking into the dex cache
706 // and does not check the access if it hits it.
707 if (compiling_class.Get() == nullptr) {
708 if (!resolved_method->IsPublic()) {
709 return nullptr;
710 }
711 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
712 resolved_method,
713 dex_compilation_unit_->GetDexCache().Get(),
714 method_idx)) {
715 return nullptr;
716 }
717
718 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
719 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
720 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
721 // which require runtime handling.
722 if (invoke_type == kSuper) {
723 if (compiling_class.Get() == nullptr) {
724 // We could not determine the method's class we need to wait until runtime.
725 DCHECK(Runtime::Current()->IsAotCompiler());
726 return nullptr;
727 }
Aart Bikf663e342016-04-04 17:28:59 -0700728 if (!methods_class->IsAssignableFrom(compiling_class.Get())) {
729 // We cannot statically determine the target method. The runtime will throw a
730 // NoSuchMethodError on this one.
731 return nullptr;
732 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100733 ArtMethod* actual_method;
734 if (methods_class->IsInterface()) {
735 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
736 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000737 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100738 uint16_t vtable_index = resolved_method->GetMethodIndex();
739 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
740 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000741 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100742 if (actual_method != resolved_method &&
743 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
744 // The back-end code generator relies on this check in order to ensure that it will not
745 // attempt to read the dex_cache with a dex_method_index that is not from the correct
746 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
747 // builder, which means that the code-generator (and compiler driver during sharpening and
748 // inliner, maybe) might invoke an incorrect method.
749 // TODO: The actual method could still be referenced in the current dex file, so we
750 // could try locating it.
751 // TODO: Remove the dex_file restriction.
752 return nullptr;
753 }
754 if (!actual_method->IsInvokable()) {
755 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
756 // could resolve the callee to the wrong method.
757 return nullptr;
758 }
759 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000760 }
761
762 // Check for incompatible class changes. The class linker has a fast path for
763 // looking into the dex cache and does not check incompatible class changes if it hits it.
764 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
765 return nullptr;
766 }
767
768 return resolved_method;
769}
770
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100771static bool IsStringConstructor(ArtMethod* method) {
772 ScopedObjectAccess soa(Thread::Current());
773 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
774}
775
David Brazdildee58d62016-04-07 09:54:26 +0000776bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
777 uint32_t dex_pc,
778 uint32_t method_idx,
779 uint32_t number_of_vreg_arguments,
780 bool is_range,
781 uint32_t* args,
782 uint32_t register_index) {
783 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
784 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
785 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
786
787 // Remove the return type from the 'proto'.
788 size_t number_of_arguments = strlen(descriptor) - 1;
789 if (invoke_type != kStatic) { // instance call
790 // One extra argument for 'this'.
791 number_of_arguments++;
792 }
793
David Brazdildee58d62016-04-07 09:54:26 +0000794 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
795
796 if (UNLIKELY(resolved_method == nullptr)) {
797 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
798 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
799 number_of_arguments,
800 return_type,
801 dex_pc,
802 method_idx,
803 invoke_type);
804 return HandleInvoke(invoke,
805 number_of_vreg_arguments,
806 args,
807 register_index,
808 is_range,
809 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700810 nullptr, /* clinit_check */
811 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000812 }
813
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100814 // Replace calls to String.<init> with StringFactory.
815 if (IsStringConstructor(resolved_method)) {
816 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
817 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
818 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
819 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
820 dchecked_integral_cast<uint64_t>(string_init_entry_point),
821 0U
822 };
823 MethodReference target_method(dex_file_, method_idx);
824 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
825 arena_,
826 number_of_arguments - 1,
827 Primitive::kPrimNot /*return_type */,
828 dex_pc,
829 method_idx,
830 nullptr,
831 dispatch_info,
832 invoke_type,
833 target_method,
834 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
835 return HandleStringInit(invoke,
836 number_of_vreg_arguments,
837 args,
838 register_index,
839 is_range,
840 descriptor);
841 }
842
David Brazdildee58d62016-04-07 09:54:26 +0000843 // Potential class initialization check, in the case of a static method call.
844 HClinitCheck* clinit_check = nullptr;
845 HInvoke* invoke = nullptr;
846 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
847 // By default, consider that the called method implicitly requires
848 // an initialization check of its declaring method.
849 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
850 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
851 ScopedObjectAccess soa(Thread::Current());
852 if (invoke_type == kStatic) {
853 clinit_check = ProcessClinitCheckForInvoke(
854 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
855 } else if (invoke_type == kSuper) {
856 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100857 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000858 // we resolved to the method referenced by the instruction.
859 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000860 }
861 }
862
863 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
864 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
865 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
866 0u,
867 0U
868 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100869 MethodReference target_method(resolved_method->GetDexFile(),
870 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000871 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
872 number_of_arguments,
873 return_type,
874 dex_pc,
875 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100876 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000877 dispatch_info,
878 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100879 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000880 clinit_check_requirement);
881 } else if (invoke_type == kVirtual) {
882 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
883 invoke = new (arena_) HInvokeVirtual(arena_,
884 number_of_arguments,
885 return_type,
886 dex_pc,
887 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100888 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000889 resolved_method->GetMethodIndex());
890 } else {
891 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100892 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000893 invoke = new (arena_) HInvokeInterface(arena_,
894 number_of_arguments,
895 return_type,
896 dex_pc,
897 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100898 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700899 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000900 }
901
902 return HandleInvoke(invoke,
903 number_of_vreg_arguments,
904 args,
905 register_index,
906 is_range,
907 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700908 clinit_check,
909 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000910}
911
912bool HInstructionBuilder::BuildNewInstance(uint16_t type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100913 ScopedObjectAccess soa(Thread::Current());
914 StackHandleScope<1> hs(soa.Self());
915 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
916 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
917 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
918 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
919
David Brazdildee58d62016-04-07 09:54:26 +0000920 bool finalizable;
Mingyao Yang062157f2016-03-02 10:15:36 -0800921 bool needs_access_check = NeedsAccessCheck(type_index, dex_cache, &finalizable);
David Brazdildee58d62016-04-07 09:54:26 +0000922
923 // Only the non-resolved entrypoint handles the finalizable class case. If we
924 // need access checks, then we haven't resolved the method and the class may
925 // again be finalizable.
Mingyao Yang062157f2016-03-02 10:15:36 -0800926 QuickEntrypointEnum entrypoint = (finalizable || needs_access_check)
David Brazdildee58d62016-04-07 09:54:26 +0000927 ? kQuickAllocObject
928 : kQuickAllocObjectInitialized;
929
David Brazdildee58d62016-04-07 09:54:26 +0000930 if (outer_dex_cache.Get() != dex_cache.Get()) {
931 // We currently do not support inlining allocations across dex files.
932 return false;
933 }
934
935 HLoadClass* load_class = new (arena_) HLoadClass(
936 graph_->GetCurrentMethod(),
937 type_index,
938 outer_dex_file,
939 IsOutermostCompilingClass(type_index),
940 dex_pc,
Mingyao Yang062157f2016-03-02 10:15:36 -0800941 needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700942 /* is_in_dex_cache */ false,
943 /* is_in_boot_image */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000944
945 AppendInstruction(load_class);
946 HInstruction* cls = load_class;
947 if (!IsInitialized(resolved_class)) {
948 cls = new (arena_) HClinitCheck(load_class, dex_pc);
949 AppendInstruction(cls);
950 }
951
952 AppendInstruction(new (arena_) HNewInstance(
953 cls,
954 graph_->GetCurrentMethod(),
955 dex_pc,
956 type_index,
957 *dex_compilation_unit_->GetDexFile(),
Mingyao Yang062157f2016-03-02 10:15:36 -0800958 needs_access_check,
David Brazdildee58d62016-04-07 09:54:26 +0000959 finalizable,
960 entrypoint));
961 return true;
962}
963
964static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700965 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000966 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
967}
968
969bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
970 if (cls.Get() == nullptr) {
971 return false;
972 }
973
974 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
975 // check whether the class is in an image for the AOT compilation.
976 if (cls->IsInitialized() &&
977 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
978 return true;
979 }
980
981 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
982 return true;
983 }
984
985 // TODO: We should walk over the inlined methods, but we don't pass
986 // that information to the builder.
987 if (IsSubClass(GetCompilingClass(), cls.Get())) {
988 return true;
989 }
990
991 return false;
992}
993
994HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
995 uint32_t dex_pc,
996 ArtMethod* resolved_method,
997 uint32_t method_idx,
998 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
999 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1000 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001001 StackHandleScope<2> hs(self);
1002 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1003 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001004 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1005 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
1006
1007 // The index at which the method's class is stored in the DexCache's type array.
1008 uint32_t storage_index = DexFile::kDexNoIndex;
1009 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1010 if (is_outer_class) {
1011 storage_index = outer_class->GetDexTypeIndex();
1012 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1013 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1014 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1015 GetCompilingClass(),
1016 resolved_method,
1017 method_idx,
1018 &storage_index);
1019 }
1020
1021 HClinitCheck* clinit_check = nullptr;
1022
1023 if (IsInitialized(resolved_method_class)) {
1024 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1025 } else if (storage_index != DexFile::kDexNoIndex) {
1026 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1027 HLoadClass* load_class = new (arena_) HLoadClass(
1028 graph_->GetCurrentMethod(),
1029 storage_index,
1030 outer_dex_file,
1031 is_outer_class,
1032 dex_pc,
1033 /*needs_access_check*/ false,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07001034 /* is_in_dex_cache */ false,
1035 /* is_in_boot_image */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001036 AppendInstruction(load_class);
1037 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1038 AppendInstruction(clinit_check);
1039 }
1040 return clinit_check;
1041}
1042
1043bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1044 uint32_t number_of_vreg_arguments,
1045 uint32_t* args,
1046 uint32_t register_index,
1047 bool is_range,
1048 const char* descriptor,
1049 size_t start_index,
1050 size_t* argument_index) {
1051 uint32_t descriptor_index = 1; // Skip the return type.
1052
1053 for (size_t i = start_index;
1054 // Make sure we don't go over the expected arguments or over the number of
1055 // dex registers given. If the instruction was seen as dead by the verifier,
1056 // it hasn't been properly checked.
1057 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1058 i++, (*argument_index)++) {
1059 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1060 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1061 if (!is_range
1062 && is_wide
1063 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1064 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1065 // reject any class where this is violated. However, the verifier only does these checks
1066 // on non trivially dead instructions, so we just bailout the compilation.
1067 VLOG(compiler) << "Did not compile "
1068 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1069 << " because of non-sequential dex register pair in wide argument";
1070 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1071 return false;
1072 }
1073 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1074 invoke->SetArgumentAt(*argument_index, arg);
1075 if (is_wide) {
1076 i++;
1077 }
1078 }
1079
1080 if (*argument_index != invoke->GetNumberOfArguments()) {
1081 VLOG(compiler) << "Did not compile "
1082 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1083 << " because of wrong number of arguments in invoke instruction";
1084 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1085 return false;
1086 }
1087
1088 if (invoke->IsInvokeStaticOrDirect() &&
1089 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1090 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1091 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1092 (*argument_index)++;
1093 }
1094
1095 return true;
1096}
1097
1098bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1099 uint32_t number_of_vreg_arguments,
1100 uint32_t* args,
1101 uint32_t register_index,
1102 bool is_range,
1103 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001104 HClinitCheck* clinit_check,
1105 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001106 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1107
1108 size_t start_index = 0;
1109 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001110 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001111 uint32_t obj_reg = is_range ? register_index : args[0];
1112 HInstruction* arg = is_unresolved
1113 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1114 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001115 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001116 start_index = 1;
1117 argument_index = 1;
1118 }
1119
1120 if (!SetupInvokeArguments(invoke,
1121 number_of_vreg_arguments,
1122 args,
1123 register_index,
1124 is_range,
1125 descriptor,
1126 start_index,
1127 &argument_index)) {
1128 return false;
1129 }
1130
1131 if (clinit_check != nullptr) {
1132 // Add the class initialization check as last input of `invoke`.
1133 DCHECK(invoke->IsInvokeStaticOrDirect());
1134 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1135 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1136 invoke->SetArgumentAt(argument_index, clinit_check);
1137 argument_index++;
1138 }
1139
1140 AppendInstruction(invoke);
1141 latest_result_ = invoke;
1142
1143 return true;
1144}
1145
1146bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1147 uint32_t number_of_vreg_arguments,
1148 uint32_t* args,
1149 uint32_t register_index,
1150 bool is_range,
1151 const char* descriptor) {
1152 DCHECK(invoke->IsInvokeStaticOrDirect());
1153 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1154
1155 size_t start_index = 1;
1156 size_t argument_index = 0;
1157 if (!SetupInvokeArguments(invoke,
1158 number_of_vreg_arguments,
1159 args,
1160 register_index,
1161 is_range,
1162 descriptor,
1163 start_index,
1164 &argument_index)) {
1165 return false;
1166 }
1167
1168 AppendInstruction(invoke);
1169
1170 // This is a StringFactory call, not an actual String constructor. Its result
1171 // replaces the empty String pre-allocated by NewInstance.
1172 uint32_t orig_this_reg = is_range ? register_index : args[0];
1173 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1174
1175 // Replacing the NewInstance might render it redundant. Keep a list of these
1176 // to be visited once it is clear whether it is has remaining uses.
1177 if (arg_this->IsNewInstance()) {
1178 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1179 } else {
1180 DCHECK(arg_this->IsPhi());
1181 // NewInstance is not the direct input of the StringFactory call. It might
1182 // be redundant but optimizing this case is not worth the effort.
1183 }
1184
1185 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1186 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1187 if ((*current_locals_)[vreg] == arg_this) {
1188 (*current_locals_)[vreg] = invoke;
1189 }
1190 }
1191
1192 return true;
1193}
1194
1195static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1196 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1197 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1198 return Primitive::GetType(type[0]);
1199}
1200
1201bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1202 uint32_t dex_pc,
1203 bool is_put) {
1204 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1205 uint32_t obj_reg = instruction.VRegB_22c();
1206 uint16_t field_index;
1207 if (instruction.IsQuickened()) {
1208 if (!CanDecodeQuickenedInfo()) {
1209 return false;
1210 }
1211 field_index = LookupQuickenedInfo(dex_pc);
1212 } else {
1213 field_index = instruction.VRegC_22c();
1214 }
1215
1216 ScopedObjectAccess soa(Thread::Current());
1217 ArtField* resolved_field =
1218 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1219
1220
Aart Bik14154132016-06-02 17:53:58 -07001221 // Generate an explicit null check on the reference, unless the field access
1222 // is unresolved. In that case, we rely on the runtime to perform various
1223 // checks first, followed by a null check.
1224 HInstruction* object = (resolved_field == nullptr)
1225 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1226 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001227
1228 Primitive::Type field_type = (resolved_field == nullptr)
1229 ? GetFieldAccessType(*dex_file_, field_index)
1230 : resolved_field->GetTypeAsPrimitiveType();
1231 if (is_put) {
1232 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1233 HInstruction* field_set = nullptr;
1234 if (resolved_field == nullptr) {
1235 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001236 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001237 value,
1238 field_type,
1239 field_index,
1240 dex_pc);
1241 } else {
1242 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001243 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001244 value,
1245 field_type,
1246 resolved_field->GetOffset(),
1247 resolved_field->IsVolatile(),
1248 field_index,
1249 class_def_index,
1250 *dex_file_,
1251 dex_compilation_unit_->GetDexCache(),
1252 dex_pc);
1253 }
1254 AppendInstruction(field_set);
1255 } else {
1256 HInstruction* field_get = nullptr;
1257 if (resolved_field == nullptr) {
1258 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001259 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001260 field_type,
1261 field_index,
1262 dex_pc);
1263 } else {
1264 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001265 field_get = new (arena_) HInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001266 field_type,
1267 resolved_field->GetOffset(),
1268 resolved_field->IsVolatile(),
1269 field_index,
1270 class_def_index,
1271 *dex_file_,
1272 dex_compilation_unit_->GetDexCache(),
1273 dex_pc);
1274 }
1275 AppendInstruction(field_get);
1276 UpdateLocal(source_or_dest_reg, field_get);
1277 }
1278
1279 return true;
1280}
1281
1282static mirror::Class* GetClassFrom(CompilerDriver* driver,
1283 const DexCompilationUnit& compilation_unit) {
1284 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001285 StackHandleScope<1> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +00001286 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001287 soa.Decode<mirror::ClassLoader>(compilation_unit.GetClassLoader())));
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001288 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001289
1290 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1291}
1292
1293mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1294 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1295}
1296
1297mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1298 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1299}
1300
1301bool HInstructionBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1302 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001303 StackHandleScope<3> hs(soa.Self());
1304 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001305 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001306 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001307 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1308 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1309 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1310
1311 // GetOutermostCompilingClass returns null when the class is unresolved
1312 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1313 // we are compiling it.
1314 // When this happens we cannot establish a direct relation between the current
1315 // class and the outer class, so we return false.
1316 // (Note that this is only used for optimizing invokes and field accesses)
1317 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1318}
1319
1320void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1321 uint32_t dex_pc,
1322 bool is_put,
1323 Primitive::Type field_type) {
1324 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1325 uint16_t field_index = instruction.VRegB_21c();
1326
1327 if (is_put) {
1328 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1329 AppendInstruction(
1330 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1331 } else {
1332 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1333 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1334 }
1335}
1336
1337bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1338 uint32_t dex_pc,
1339 bool is_put) {
1340 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1341 uint16_t field_index = instruction.VRegB_21c();
1342
1343 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001344 StackHandleScope<3> hs(soa.Self());
1345 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001346 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001347 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001348 ArtField* resolved_field = compiler_driver_->ResolveField(
1349 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1350
1351 if (resolved_field == nullptr) {
1352 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1353 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1354 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1355 return true;
1356 }
1357
1358 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
1359 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001360 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001361 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1362
1363 // The index at which the field's class is stored in the DexCache's type array.
1364 uint32_t storage_index;
1365 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1366 if (is_outer_class) {
1367 storage_index = outer_class->GetDexTypeIndex();
1368 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1369 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1370 return false;
1371 } else {
1372 // TODO: This is rather expensive. Perf it and cache the results if needed.
1373 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1374 outer_dex_cache.Get(),
1375 GetCompilingClass(),
1376 resolved_field,
1377 field_index,
1378 &storage_index);
1379 bool can_easily_access = is_put ? pair.second : pair.first;
1380 if (!can_easily_access) {
1381 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1382 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1383 return true;
1384 }
1385 }
1386
David Brazdildee58d62016-04-07 09:54:26 +00001387 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1388 storage_index,
1389 outer_dex_file,
1390 is_outer_class,
1391 dex_pc,
1392 /*needs_access_check*/ false,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07001393 /* is_in_dex_cache */ false,
1394 /* is_in_boot_image */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001395 AppendInstruction(constant);
1396
1397 HInstruction* cls = constant;
1398
1399 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1400 if (!IsInitialized(klass)) {
1401 cls = new (arena_) HClinitCheck(constant, dex_pc);
1402 AppendInstruction(cls);
1403 }
1404
1405 uint16_t class_def_index = klass->GetDexClassDefIndex();
1406 if (is_put) {
1407 // We need to keep the class alive before loading the value.
1408 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1409 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1410 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1411 value,
1412 field_type,
1413 resolved_field->GetOffset(),
1414 resolved_field->IsVolatile(),
1415 field_index,
1416 class_def_index,
1417 *dex_file_,
1418 dex_cache_,
1419 dex_pc));
1420 } else {
1421 AppendInstruction(new (arena_) HStaticFieldGet(cls,
1422 field_type,
1423 resolved_field->GetOffset(),
1424 resolved_field->IsVolatile(),
1425 field_index,
1426 class_def_index,
1427 *dex_file_,
1428 dex_cache_,
1429 dex_pc));
1430 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1431 }
1432 return true;
1433}
1434
1435void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1436 uint16_t first_vreg,
1437 int64_t second_vreg_or_constant,
1438 uint32_t dex_pc,
1439 Primitive::Type type,
1440 bool second_is_constant,
1441 bool isDiv) {
1442 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1443
1444 HInstruction* first = LoadLocal(first_vreg, type);
1445 HInstruction* second = nullptr;
1446 if (second_is_constant) {
1447 if (type == Primitive::kPrimInt) {
1448 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1449 } else {
1450 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1451 }
1452 } else {
1453 second = LoadLocal(second_vreg_or_constant, type);
1454 }
1455
1456 if (!second_is_constant
1457 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1458 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1459 second = new (arena_) HDivZeroCheck(second, dex_pc);
1460 AppendInstruction(second);
1461 }
1462
1463 if (isDiv) {
1464 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1465 } else {
1466 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1467 }
1468 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1469}
1470
1471void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1472 uint32_t dex_pc,
1473 bool is_put,
1474 Primitive::Type anticipated_type) {
1475 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1476 uint8_t array_reg = instruction.VRegB_23x();
1477 uint8_t index_reg = instruction.VRegC_23x();
1478
David Brazdilc120bbe2016-04-22 16:57:00 +01001479 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001480 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1481 AppendInstruction(length);
1482 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1483 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1484 AppendInstruction(index);
1485 if (is_put) {
1486 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1487 // TODO: Insert a type check node if the type is Object.
1488 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1489 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1490 AppendInstruction(aset);
1491 } else {
1492 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1493 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1494 AppendInstruction(aget);
1495 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1496 }
1497 graph_->SetHasBoundsChecks(true);
1498}
1499
1500void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1501 uint32_t type_index,
1502 uint32_t number_of_vreg_arguments,
1503 bool is_range,
1504 uint32_t* args,
1505 uint32_t register_index) {
1506 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
1507 bool finalizable;
1508 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
1509 ? kQuickAllocArrayWithAccessCheck
1510 : kQuickAllocArray;
1511 HInstruction* object = new (arena_) HNewArray(length,
1512 graph_->GetCurrentMethod(),
1513 dex_pc,
1514 type_index,
1515 *dex_compilation_unit_->GetDexFile(),
1516 entrypoint);
1517 AppendInstruction(object);
1518
1519 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1520 DCHECK_EQ(descriptor[0], '[') << descriptor;
1521 char primitive = descriptor[1];
1522 DCHECK(primitive == 'I'
1523 || primitive == 'L'
1524 || primitive == '[') << descriptor;
1525 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1526 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1527
1528 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1529 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1530 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1531 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1532 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1533 AppendInstruction(aset);
1534 }
1535 latest_result_ = object;
1536}
1537
1538template <typename T>
1539void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1540 const T* data,
1541 uint32_t element_count,
1542 Primitive::Type anticipated_type,
1543 uint32_t dex_pc) {
1544 for (uint32_t i = 0; i < element_count; ++i) {
1545 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1546 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1547 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1548 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1549 AppendInstruction(aset);
1550 }
1551}
1552
1553void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001554 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001555
1556 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1557 const Instruction::ArrayDataPayload* payload =
1558 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1559 const uint8_t* data = payload->data;
1560 uint32_t element_count = payload->element_count;
1561
Vladimir Markoc69fba22016-09-06 16:49:15 +01001562 if (element_count == 0u) {
1563 // For empty payload we emit only the null check above.
1564 return;
1565 }
1566
1567 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1568 AppendInstruction(length);
1569
David Brazdildee58d62016-04-07 09:54:26 +00001570 // Implementation of this DEX instruction seems to be that the bounds check is
1571 // done before doing any stores.
1572 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1573 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1574
1575 switch (payload->element_width) {
1576 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001577 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001578 reinterpret_cast<const int8_t*>(data),
1579 element_count,
1580 Primitive::kPrimByte,
1581 dex_pc);
1582 break;
1583 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001584 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001585 reinterpret_cast<const int16_t*>(data),
1586 element_count,
1587 Primitive::kPrimShort,
1588 dex_pc);
1589 break;
1590 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001591 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001592 reinterpret_cast<const int32_t*>(data),
1593 element_count,
1594 Primitive::kPrimInt,
1595 dex_pc);
1596 break;
1597 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001598 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001599 reinterpret_cast<const int64_t*>(data),
1600 element_count,
1601 dex_pc);
1602 break;
1603 default:
1604 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1605 }
1606 graph_->SetHasBoundsChecks(true);
1607}
1608
1609void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1610 const int64_t* data,
1611 uint32_t element_count,
1612 uint32_t dex_pc) {
1613 for (uint32_t i = 0; i < element_count; ++i) {
1614 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1615 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1616 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1617 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1618 AppendInstruction(aset);
1619 }
1620}
1621
1622static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001623 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001624 if (cls.Get() == nullptr) {
1625 return TypeCheckKind::kUnresolvedCheck;
1626 } else if (cls->IsInterface()) {
1627 return TypeCheckKind::kInterfaceCheck;
1628 } else if (cls->IsArrayClass()) {
1629 if (cls->GetComponentType()->IsObjectClass()) {
1630 return TypeCheckKind::kArrayObjectCheck;
1631 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1632 return TypeCheckKind::kExactCheck;
1633 } else {
1634 return TypeCheckKind::kArrayCheck;
1635 }
1636 } else if (cls->IsFinal()) {
1637 return TypeCheckKind::kExactCheck;
1638 } else if (cls->IsAbstract()) {
1639 return TypeCheckKind::kAbstractClassCheck;
1640 } else {
1641 return TypeCheckKind::kClassHierarchyCheck;
1642 }
1643}
1644
1645void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1646 uint8_t destination,
1647 uint8_t reference,
1648 uint16_t type_index,
1649 uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001650 ScopedObjectAccess soa(Thread::Current());
1651 StackHandleScope<1> hs(soa.Self());
1652 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1653 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1654 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1655
David Brazdildee58d62016-04-07 09:54:26 +00001656 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1657 dex_compilation_unit_->GetDexMethodIndex(),
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001658 dex_cache,
1659 type_index);
David Brazdildee58d62016-04-07 09:54:26 +00001660
1661 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
1662 HLoadClass* cls = new (arena_) HLoadClass(
1663 graph_->GetCurrentMethod(),
1664 type_index,
1665 dex_file,
1666 IsOutermostCompilingClass(type_index),
1667 dex_pc,
1668 !can_access,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07001669 /* is_in_dex_cache */ false,
1670 /* is_in_boot_image */ false);
David Brazdildee58d62016-04-07 09:54:26 +00001671 AppendInstruction(cls);
1672
1673 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
1674 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1675 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1676 UpdateLocal(destination, current_block_->GetLastInstruction());
1677 } else {
1678 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1679 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1680 // which may throw. If it succeeds BoundType sets the new type of `object`
1681 // for all subsequent uses.
1682 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1683 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1684 UpdateLocal(reference, current_block_->GetLastInstruction());
1685 }
1686}
1687
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001688bool HInstructionBuilder::NeedsAccessCheck(uint32_t type_index,
1689 Handle<mirror::DexCache> dex_cache,
1690 bool* finalizable) const {
David Brazdildee58d62016-04-07 09:54:26 +00001691 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001692 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index, finalizable);
1693}
1694
1695bool HInstructionBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
1696 ScopedObjectAccess soa(Thread::Current());
1697 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1698 return NeedsAccessCheck(type_index, dex_cache, finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001699}
1700
1701bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1702 return interpreter_metadata_ != nullptr;
1703}
1704
1705uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1706 DCHECK(interpreter_metadata_ != nullptr);
1707
1708 // First check if the info has already been decoded from `interpreter_metadata_`.
1709 auto it = skipped_interpreter_metadata_.find(dex_pc);
1710 if (it != skipped_interpreter_metadata_.end()) {
1711 // Remove the entry from the map and return the parsed info.
1712 uint16_t value_in_map = it->second;
1713 skipped_interpreter_metadata_.erase(it);
1714 return value_in_map;
1715 }
1716
1717 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1718 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1719 while (true) {
1720 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1721 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1722 DCHECK_LE(dex_pc_in_map, dex_pc);
1723
1724 if (dex_pc_in_map == dex_pc) {
1725 return value_in_map;
1726 } else {
1727 skipped_interpreter_metadata_.Put(dex_pc_in_map, value_in_map);
1728 }
1729 }
1730}
1731
1732bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1733 switch (instruction.Opcode()) {
1734 case Instruction::CONST_4: {
1735 int32_t register_index = instruction.VRegA();
1736 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1737 UpdateLocal(register_index, constant);
1738 break;
1739 }
1740
1741 case Instruction::CONST_16: {
1742 int32_t register_index = instruction.VRegA();
1743 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1744 UpdateLocal(register_index, constant);
1745 break;
1746 }
1747
1748 case Instruction::CONST: {
1749 int32_t register_index = instruction.VRegA();
1750 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1751 UpdateLocal(register_index, constant);
1752 break;
1753 }
1754
1755 case Instruction::CONST_HIGH16: {
1756 int32_t register_index = instruction.VRegA();
1757 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1758 UpdateLocal(register_index, constant);
1759 break;
1760 }
1761
1762 case Instruction::CONST_WIDE_16: {
1763 int32_t register_index = instruction.VRegA();
1764 // Get 16 bits of constant value, sign extended to 64 bits.
1765 int64_t value = instruction.VRegB_21s();
1766 value <<= 48;
1767 value >>= 48;
1768 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1769 UpdateLocal(register_index, constant);
1770 break;
1771 }
1772
1773 case Instruction::CONST_WIDE_32: {
1774 int32_t register_index = instruction.VRegA();
1775 // Get 32 bits of constant value, sign extended to 64 bits.
1776 int64_t value = instruction.VRegB_31i();
1777 value <<= 32;
1778 value >>= 32;
1779 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1780 UpdateLocal(register_index, constant);
1781 break;
1782 }
1783
1784 case Instruction::CONST_WIDE: {
1785 int32_t register_index = instruction.VRegA();
1786 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1787 UpdateLocal(register_index, constant);
1788 break;
1789 }
1790
1791 case Instruction::CONST_WIDE_HIGH16: {
1792 int32_t register_index = instruction.VRegA();
1793 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1794 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1795 UpdateLocal(register_index, constant);
1796 break;
1797 }
1798
1799 // Note that the SSA building will refine the types.
1800 case Instruction::MOVE:
1801 case Instruction::MOVE_FROM16:
1802 case Instruction::MOVE_16: {
1803 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1804 UpdateLocal(instruction.VRegA(), value);
1805 break;
1806 }
1807
1808 // Note that the SSA building will refine the types.
1809 case Instruction::MOVE_WIDE:
1810 case Instruction::MOVE_WIDE_FROM16:
1811 case Instruction::MOVE_WIDE_16: {
1812 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1813 UpdateLocal(instruction.VRegA(), value);
1814 break;
1815 }
1816
1817 case Instruction::MOVE_OBJECT:
1818 case Instruction::MOVE_OBJECT_16:
1819 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001820 // The verifier has no notion of a null type, so a move-object of constant 0
1821 // will lead to the same constant 0 in the destination register. To mimic
1822 // this behavior, we just pretend we haven't seen a type change (int to reference)
1823 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1824 // types correct.
1825 uint32_t reg_number = instruction.VRegB();
1826 HInstruction* value = (*current_locals_)[reg_number];
1827 if (value->IsIntConstant()) {
1828 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1829 } else if (value->IsPhi()) {
1830 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1831 } else {
1832 value = LoadLocal(reg_number, Primitive::kPrimNot);
1833 }
David Brazdildee58d62016-04-07 09:54:26 +00001834 UpdateLocal(instruction.VRegA(), value);
1835 break;
1836 }
1837
1838 case Instruction::RETURN_VOID_NO_BARRIER:
1839 case Instruction::RETURN_VOID: {
1840 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1841 break;
1842 }
1843
1844#define IF_XX(comparison, cond) \
1845 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1846 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1847
1848 IF_XX(HEqual, EQ);
1849 IF_XX(HNotEqual, NE);
1850 IF_XX(HLessThan, LT);
1851 IF_XX(HLessThanOrEqual, LE);
1852 IF_XX(HGreaterThan, GT);
1853 IF_XX(HGreaterThanOrEqual, GE);
1854
1855 case Instruction::GOTO:
1856 case Instruction::GOTO_16:
1857 case Instruction::GOTO_32: {
1858 AppendInstruction(new (arena_) HGoto(dex_pc));
1859 current_block_ = nullptr;
1860 break;
1861 }
1862
1863 case Instruction::RETURN: {
1864 BuildReturn(instruction, return_type_, dex_pc);
1865 break;
1866 }
1867
1868 case Instruction::RETURN_OBJECT: {
1869 BuildReturn(instruction, return_type_, dex_pc);
1870 break;
1871 }
1872
1873 case Instruction::RETURN_WIDE: {
1874 BuildReturn(instruction, return_type_, dex_pc);
1875 break;
1876 }
1877
1878 case Instruction::INVOKE_DIRECT:
1879 case Instruction::INVOKE_INTERFACE:
1880 case Instruction::INVOKE_STATIC:
1881 case Instruction::INVOKE_SUPER:
1882 case Instruction::INVOKE_VIRTUAL:
1883 case Instruction::INVOKE_VIRTUAL_QUICK: {
1884 uint16_t method_idx;
1885 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1886 if (!CanDecodeQuickenedInfo()) {
1887 return false;
1888 }
1889 method_idx = LookupQuickenedInfo(dex_pc);
1890 } else {
1891 method_idx = instruction.VRegB_35c();
1892 }
1893 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1894 uint32_t args[5];
1895 instruction.GetVarArgs(args);
1896 if (!BuildInvoke(instruction, dex_pc, method_idx,
1897 number_of_vreg_arguments, false, args, -1)) {
1898 return false;
1899 }
1900 break;
1901 }
1902
1903 case Instruction::INVOKE_DIRECT_RANGE:
1904 case Instruction::INVOKE_INTERFACE_RANGE:
1905 case Instruction::INVOKE_STATIC_RANGE:
1906 case Instruction::INVOKE_SUPER_RANGE:
1907 case Instruction::INVOKE_VIRTUAL_RANGE:
1908 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1909 uint16_t method_idx;
1910 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1911 if (!CanDecodeQuickenedInfo()) {
1912 return false;
1913 }
1914 method_idx = LookupQuickenedInfo(dex_pc);
1915 } else {
1916 method_idx = instruction.VRegB_3rc();
1917 }
1918 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1919 uint32_t register_index = instruction.VRegC();
1920 if (!BuildInvoke(instruction, dex_pc, method_idx,
1921 number_of_vreg_arguments, true, nullptr, register_index)) {
1922 return false;
1923 }
1924 break;
1925 }
1926
1927 case Instruction::NEG_INT: {
1928 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1929 break;
1930 }
1931
1932 case Instruction::NEG_LONG: {
1933 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1934 break;
1935 }
1936
1937 case Instruction::NEG_FLOAT: {
1938 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1939 break;
1940 }
1941
1942 case Instruction::NEG_DOUBLE: {
1943 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1944 break;
1945 }
1946
1947 case Instruction::NOT_INT: {
1948 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1949 break;
1950 }
1951
1952 case Instruction::NOT_LONG: {
1953 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1954 break;
1955 }
1956
1957 case Instruction::INT_TO_LONG: {
1958 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
1959 break;
1960 }
1961
1962 case Instruction::INT_TO_FLOAT: {
1963 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
1964 break;
1965 }
1966
1967 case Instruction::INT_TO_DOUBLE: {
1968 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
1969 break;
1970 }
1971
1972 case Instruction::LONG_TO_INT: {
1973 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
1974 break;
1975 }
1976
1977 case Instruction::LONG_TO_FLOAT: {
1978 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
1979 break;
1980 }
1981
1982 case Instruction::LONG_TO_DOUBLE: {
1983 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
1984 break;
1985 }
1986
1987 case Instruction::FLOAT_TO_INT: {
1988 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1989 break;
1990 }
1991
1992 case Instruction::FLOAT_TO_LONG: {
1993 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
1994 break;
1995 }
1996
1997 case Instruction::FLOAT_TO_DOUBLE: {
1998 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1999 break;
2000 }
2001
2002 case Instruction::DOUBLE_TO_INT: {
2003 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2004 break;
2005 }
2006
2007 case Instruction::DOUBLE_TO_LONG: {
2008 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2009 break;
2010 }
2011
2012 case Instruction::DOUBLE_TO_FLOAT: {
2013 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2014 break;
2015 }
2016
2017 case Instruction::INT_TO_BYTE: {
2018 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2019 break;
2020 }
2021
2022 case Instruction::INT_TO_SHORT: {
2023 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2024 break;
2025 }
2026
2027 case Instruction::INT_TO_CHAR: {
2028 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2029 break;
2030 }
2031
2032 case Instruction::ADD_INT: {
2033 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2034 break;
2035 }
2036
2037 case Instruction::ADD_LONG: {
2038 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2039 break;
2040 }
2041
2042 case Instruction::ADD_DOUBLE: {
2043 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2044 break;
2045 }
2046
2047 case Instruction::ADD_FLOAT: {
2048 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2049 break;
2050 }
2051
2052 case Instruction::SUB_INT: {
2053 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2054 break;
2055 }
2056
2057 case Instruction::SUB_LONG: {
2058 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2059 break;
2060 }
2061
2062 case Instruction::SUB_FLOAT: {
2063 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2064 break;
2065 }
2066
2067 case Instruction::SUB_DOUBLE: {
2068 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2069 break;
2070 }
2071
2072 case Instruction::ADD_INT_2ADDR: {
2073 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2074 break;
2075 }
2076
2077 case Instruction::MUL_INT: {
2078 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::MUL_LONG: {
2083 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::MUL_FLOAT: {
2088 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2089 break;
2090 }
2091
2092 case Instruction::MUL_DOUBLE: {
2093 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2094 break;
2095 }
2096
2097 case Instruction::DIV_INT: {
2098 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2099 dex_pc, Primitive::kPrimInt, false, true);
2100 break;
2101 }
2102
2103 case Instruction::DIV_LONG: {
2104 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2105 dex_pc, Primitive::kPrimLong, false, true);
2106 break;
2107 }
2108
2109 case Instruction::DIV_FLOAT: {
2110 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2111 break;
2112 }
2113
2114 case Instruction::DIV_DOUBLE: {
2115 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2116 break;
2117 }
2118
2119 case Instruction::REM_INT: {
2120 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2121 dex_pc, Primitive::kPrimInt, false, false);
2122 break;
2123 }
2124
2125 case Instruction::REM_LONG: {
2126 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2127 dex_pc, Primitive::kPrimLong, false, false);
2128 break;
2129 }
2130
2131 case Instruction::REM_FLOAT: {
2132 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2133 break;
2134 }
2135
2136 case Instruction::REM_DOUBLE: {
2137 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2138 break;
2139 }
2140
2141 case Instruction::AND_INT: {
2142 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2143 break;
2144 }
2145
2146 case Instruction::AND_LONG: {
2147 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2148 break;
2149 }
2150
2151 case Instruction::SHL_INT: {
2152 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2153 break;
2154 }
2155
2156 case Instruction::SHL_LONG: {
2157 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2158 break;
2159 }
2160
2161 case Instruction::SHR_INT: {
2162 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2163 break;
2164 }
2165
2166 case Instruction::SHR_LONG: {
2167 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2168 break;
2169 }
2170
2171 case Instruction::USHR_INT: {
2172 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2173 break;
2174 }
2175
2176 case Instruction::USHR_LONG: {
2177 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2178 break;
2179 }
2180
2181 case Instruction::OR_INT: {
2182 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2183 break;
2184 }
2185
2186 case Instruction::OR_LONG: {
2187 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2188 break;
2189 }
2190
2191 case Instruction::XOR_INT: {
2192 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2193 break;
2194 }
2195
2196 case Instruction::XOR_LONG: {
2197 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2198 break;
2199 }
2200
2201 case Instruction::ADD_LONG_2ADDR: {
2202 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2203 break;
2204 }
2205
2206 case Instruction::ADD_DOUBLE_2ADDR: {
2207 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2208 break;
2209 }
2210
2211 case Instruction::ADD_FLOAT_2ADDR: {
2212 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2213 break;
2214 }
2215
2216 case Instruction::SUB_INT_2ADDR: {
2217 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2218 break;
2219 }
2220
2221 case Instruction::SUB_LONG_2ADDR: {
2222 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2223 break;
2224 }
2225
2226 case Instruction::SUB_FLOAT_2ADDR: {
2227 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2228 break;
2229 }
2230
2231 case Instruction::SUB_DOUBLE_2ADDR: {
2232 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2233 break;
2234 }
2235
2236 case Instruction::MUL_INT_2ADDR: {
2237 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2238 break;
2239 }
2240
2241 case Instruction::MUL_LONG_2ADDR: {
2242 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2243 break;
2244 }
2245
2246 case Instruction::MUL_FLOAT_2ADDR: {
2247 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2248 break;
2249 }
2250
2251 case Instruction::MUL_DOUBLE_2ADDR: {
2252 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2253 break;
2254 }
2255
2256 case Instruction::DIV_INT_2ADDR: {
2257 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2258 dex_pc, Primitive::kPrimInt, false, true);
2259 break;
2260 }
2261
2262 case Instruction::DIV_LONG_2ADDR: {
2263 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2264 dex_pc, Primitive::kPrimLong, false, true);
2265 break;
2266 }
2267
2268 case Instruction::REM_INT_2ADDR: {
2269 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2270 dex_pc, Primitive::kPrimInt, false, false);
2271 break;
2272 }
2273
2274 case Instruction::REM_LONG_2ADDR: {
2275 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2276 dex_pc, Primitive::kPrimLong, false, false);
2277 break;
2278 }
2279
2280 case Instruction::REM_FLOAT_2ADDR: {
2281 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2282 break;
2283 }
2284
2285 case Instruction::REM_DOUBLE_2ADDR: {
2286 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2287 break;
2288 }
2289
2290 case Instruction::SHL_INT_2ADDR: {
2291 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2292 break;
2293 }
2294
2295 case Instruction::SHL_LONG_2ADDR: {
2296 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2297 break;
2298 }
2299
2300 case Instruction::SHR_INT_2ADDR: {
2301 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2302 break;
2303 }
2304
2305 case Instruction::SHR_LONG_2ADDR: {
2306 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2307 break;
2308 }
2309
2310 case Instruction::USHR_INT_2ADDR: {
2311 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2312 break;
2313 }
2314
2315 case Instruction::USHR_LONG_2ADDR: {
2316 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2317 break;
2318 }
2319
2320 case Instruction::DIV_FLOAT_2ADDR: {
2321 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2322 break;
2323 }
2324
2325 case Instruction::DIV_DOUBLE_2ADDR: {
2326 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2327 break;
2328 }
2329
2330 case Instruction::AND_INT_2ADDR: {
2331 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2332 break;
2333 }
2334
2335 case Instruction::AND_LONG_2ADDR: {
2336 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2337 break;
2338 }
2339
2340 case Instruction::OR_INT_2ADDR: {
2341 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2342 break;
2343 }
2344
2345 case Instruction::OR_LONG_2ADDR: {
2346 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2347 break;
2348 }
2349
2350 case Instruction::XOR_INT_2ADDR: {
2351 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2352 break;
2353 }
2354
2355 case Instruction::XOR_LONG_2ADDR: {
2356 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2357 break;
2358 }
2359
2360 case Instruction::ADD_INT_LIT16: {
2361 Binop_22s<HAdd>(instruction, false, dex_pc);
2362 break;
2363 }
2364
2365 case Instruction::AND_INT_LIT16: {
2366 Binop_22s<HAnd>(instruction, false, dex_pc);
2367 break;
2368 }
2369
2370 case Instruction::OR_INT_LIT16: {
2371 Binop_22s<HOr>(instruction, false, dex_pc);
2372 break;
2373 }
2374
2375 case Instruction::XOR_INT_LIT16: {
2376 Binop_22s<HXor>(instruction, false, dex_pc);
2377 break;
2378 }
2379
2380 case Instruction::RSUB_INT: {
2381 Binop_22s<HSub>(instruction, true, dex_pc);
2382 break;
2383 }
2384
2385 case Instruction::MUL_INT_LIT16: {
2386 Binop_22s<HMul>(instruction, false, dex_pc);
2387 break;
2388 }
2389
2390 case Instruction::ADD_INT_LIT8: {
2391 Binop_22b<HAdd>(instruction, false, dex_pc);
2392 break;
2393 }
2394
2395 case Instruction::AND_INT_LIT8: {
2396 Binop_22b<HAnd>(instruction, false, dex_pc);
2397 break;
2398 }
2399
2400 case Instruction::OR_INT_LIT8: {
2401 Binop_22b<HOr>(instruction, false, dex_pc);
2402 break;
2403 }
2404
2405 case Instruction::XOR_INT_LIT8: {
2406 Binop_22b<HXor>(instruction, false, dex_pc);
2407 break;
2408 }
2409
2410 case Instruction::RSUB_INT_LIT8: {
2411 Binop_22b<HSub>(instruction, true, dex_pc);
2412 break;
2413 }
2414
2415 case Instruction::MUL_INT_LIT8: {
2416 Binop_22b<HMul>(instruction, false, dex_pc);
2417 break;
2418 }
2419
2420 case Instruction::DIV_INT_LIT16:
2421 case Instruction::DIV_INT_LIT8: {
2422 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2423 dex_pc, Primitive::kPrimInt, true, true);
2424 break;
2425 }
2426
2427 case Instruction::REM_INT_LIT16:
2428 case Instruction::REM_INT_LIT8: {
2429 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2430 dex_pc, Primitive::kPrimInt, true, false);
2431 break;
2432 }
2433
2434 case Instruction::SHL_INT_LIT8: {
2435 Binop_22b<HShl>(instruction, false, dex_pc);
2436 break;
2437 }
2438
2439 case Instruction::SHR_INT_LIT8: {
2440 Binop_22b<HShr>(instruction, false, dex_pc);
2441 break;
2442 }
2443
2444 case Instruction::USHR_INT_LIT8: {
2445 Binop_22b<HUShr>(instruction, false, dex_pc);
2446 break;
2447 }
2448
2449 case Instruction::NEW_INSTANCE: {
2450 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2451 return false;
2452 }
2453 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2454 break;
2455 }
2456
2457 case Instruction::NEW_ARRAY: {
2458 uint16_t type_index = instruction.VRegC_22c();
2459 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
2460 bool finalizable;
2461 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
2462 ? kQuickAllocArrayWithAccessCheck
2463 : kQuickAllocArray;
2464 AppendInstruction(new (arena_) HNewArray(length,
2465 graph_->GetCurrentMethod(),
2466 dex_pc,
2467 type_index,
2468 *dex_compilation_unit_->GetDexFile(),
2469 entrypoint));
2470 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2471 break;
2472 }
2473
2474 case Instruction::FILLED_NEW_ARRAY: {
2475 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2476 uint32_t type_index = instruction.VRegB_35c();
2477 uint32_t args[5];
2478 instruction.GetVarArgs(args);
2479 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2480 break;
2481 }
2482
2483 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2484 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2485 uint32_t type_index = instruction.VRegB_3rc();
2486 uint32_t register_index = instruction.VRegC_3rc();
2487 BuildFilledNewArray(
2488 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2489 break;
2490 }
2491
2492 case Instruction::FILL_ARRAY_DATA: {
2493 BuildFillArrayData(instruction, dex_pc);
2494 break;
2495 }
2496
2497 case Instruction::MOVE_RESULT:
2498 case Instruction::MOVE_RESULT_WIDE:
2499 case Instruction::MOVE_RESULT_OBJECT: {
2500 DCHECK(latest_result_ != nullptr);
2501 UpdateLocal(instruction.VRegA(), latest_result_);
2502 latest_result_ = nullptr;
2503 break;
2504 }
2505
2506 case Instruction::CMP_LONG: {
2507 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2508 break;
2509 }
2510
2511 case Instruction::CMPG_FLOAT: {
2512 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2513 break;
2514 }
2515
2516 case Instruction::CMPG_DOUBLE: {
2517 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2518 break;
2519 }
2520
2521 case Instruction::CMPL_FLOAT: {
2522 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2523 break;
2524 }
2525
2526 case Instruction::CMPL_DOUBLE: {
2527 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2528 break;
2529 }
2530
2531 case Instruction::NOP:
2532 break;
2533
2534 case Instruction::IGET:
2535 case Instruction::IGET_QUICK:
2536 case Instruction::IGET_WIDE:
2537 case Instruction::IGET_WIDE_QUICK:
2538 case Instruction::IGET_OBJECT:
2539 case Instruction::IGET_OBJECT_QUICK:
2540 case Instruction::IGET_BOOLEAN:
2541 case Instruction::IGET_BOOLEAN_QUICK:
2542 case Instruction::IGET_BYTE:
2543 case Instruction::IGET_BYTE_QUICK:
2544 case Instruction::IGET_CHAR:
2545 case Instruction::IGET_CHAR_QUICK:
2546 case Instruction::IGET_SHORT:
2547 case Instruction::IGET_SHORT_QUICK: {
2548 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2549 return false;
2550 }
2551 break;
2552 }
2553
2554 case Instruction::IPUT:
2555 case Instruction::IPUT_QUICK:
2556 case Instruction::IPUT_WIDE:
2557 case Instruction::IPUT_WIDE_QUICK:
2558 case Instruction::IPUT_OBJECT:
2559 case Instruction::IPUT_OBJECT_QUICK:
2560 case Instruction::IPUT_BOOLEAN:
2561 case Instruction::IPUT_BOOLEAN_QUICK:
2562 case Instruction::IPUT_BYTE:
2563 case Instruction::IPUT_BYTE_QUICK:
2564 case Instruction::IPUT_CHAR:
2565 case Instruction::IPUT_CHAR_QUICK:
2566 case Instruction::IPUT_SHORT:
2567 case Instruction::IPUT_SHORT_QUICK: {
2568 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2569 return false;
2570 }
2571 break;
2572 }
2573
2574 case Instruction::SGET:
2575 case Instruction::SGET_WIDE:
2576 case Instruction::SGET_OBJECT:
2577 case Instruction::SGET_BOOLEAN:
2578 case Instruction::SGET_BYTE:
2579 case Instruction::SGET_CHAR:
2580 case Instruction::SGET_SHORT: {
2581 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2582 return false;
2583 }
2584 break;
2585 }
2586
2587 case Instruction::SPUT:
2588 case Instruction::SPUT_WIDE:
2589 case Instruction::SPUT_OBJECT:
2590 case Instruction::SPUT_BOOLEAN:
2591 case Instruction::SPUT_BYTE:
2592 case Instruction::SPUT_CHAR:
2593 case Instruction::SPUT_SHORT: {
2594 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2595 return false;
2596 }
2597 break;
2598 }
2599
2600#define ARRAY_XX(kind, anticipated_type) \
2601 case Instruction::AGET##kind: { \
2602 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2603 break; \
2604 } \
2605 case Instruction::APUT##kind: { \
2606 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2607 break; \
2608 }
2609
2610 ARRAY_XX(, Primitive::kPrimInt);
2611 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2612 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2613 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2614 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2615 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2616 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2617
2618 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002619 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002620 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2621 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2622 break;
2623 }
2624
2625 case Instruction::CONST_STRING: {
2626 uint32_t string_index = instruction.VRegB_21c();
2627 AppendInstruction(
2628 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2629 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2630 break;
2631 }
2632
2633 case Instruction::CONST_STRING_JUMBO: {
2634 uint32_t string_index = instruction.VRegB_31c();
2635 AppendInstruction(
2636 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2637 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2638 break;
2639 }
2640
2641 case Instruction::CONST_CLASS: {
2642 uint16_t type_index = instruction.VRegB_21c();
David Brazdildee58d62016-04-07 09:54:26 +00002643 // `CanAccessTypeWithoutChecks` will tell whether the method being
2644 // built is trying to access its own class, so that the generated
2645 // code can optimize for this case. However, the optimization does not
2646 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002647 ScopedObjectAccess soa(Thread::Current());
2648 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00002649 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002650 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index);
David Brazdildee58d62016-04-07 09:54:26 +00002651 AppendInstruction(new (arena_) HLoadClass(
2652 graph_->GetCurrentMethod(),
2653 type_index,
2654 *dex_file_,
2655 IsOutermostCompilingClass(type_index),
2656 dex_pc,
2657 !can_access,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07002658 /* is_in_dex_cache */ false,
2659 /* is_in_boot_image */ false));
David Brazdildee58d62016-04-07 09:54:26 +00002660 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2661 break;
2662 }
2663
2664 case Instruction::MOVE_EXCEPTION: {
2665 AppendInstruction(new (arena_) HLoadException(dex_pc));
2666 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2667 AppendInstruction(new (arena_) HClearException(dex_pc));
2668 break;
2669 }
2670
2671 case Instruction::THROW: {
2672 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2673 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2674 // We finished building this block. Set the current block to null to avoid
2675 // adding dead instructions to it.
2676 current_block_ = nullptr;
2677 break;
2678 }
2679
2680 case Instruction::INSTANCE_OF: {
2681 uint8_t destination = instruction.VRegA_22c();
2682 uint8_t reference = instruction.VRegB_22c();
2683 uint16_t type_index = instruction.VRegC_22c();
2684 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2685 break;
2686 }
2687
2688 case Instruction::CHECK_CAST: {
2689 uint8_t reference = instruction.VRegA_21c();
2690 uint16_t type_index = instruction.VRegB_21c();
2691 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2692 break;
2693 }
2694
2695 case Instruction::MONITOR_ENTER: {
2696 AppendInstruction(new (arena_) HMonitorOperation(
2697 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2698 HMonitorOperation::OperationKind::kEnter,
2699 dex_pc));
2700 break;
2701 }
2702
2703 case Instruction::MONITOR_EXIT: {
2704 AppendInstruction(new (arena_) HMonitorOperation(
2705 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2706 HMonitorOperation::OperationKind::kExit,
2707 dex_pc));
2708 break;
2709 }
2710
2711 case Instruction::SPARSE_SWITCH:
2712 case Instruction::PACKED_SWITCH: {
2713 BuildSwitch(instruction, dex_pc);
2714 break;
2715 }
2716
2717 default:
2718 VLOG(compiler) << "Did not compile "
2719 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2720 << " because of unhandled instruction "
2721 << instruction.Name();
2722 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2723 return false;
2724 }
2725 return true;
2726} // NOLINT(readability/fn_size)
2727
2728} // namespace art