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