blob: af8e2c8a7c1ae6a526bcb231cd535880dee49957 [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,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000819 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100820 };
821 MethodReference target_method(dex_file_, method_idx);
822 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
823 arena_,
824 number_of_arguments - 1,
825 Primitive::kPrimNot /*return_type */,
826 dex_pc,
827 method_idx,
828 nullptr,
829 dispatch_info,
830 invoke_type,
831 target_method,
832 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
833 return HandleStringInit(invoke,
834 number_of_vreg_arguments,
835 args,
836 register_index,
837 is_range,
838 descriptor);
839 }
840
David Brazdildee58d62016-04-07 09:54:26 +0000841 // Potential class initialization check, in the case of a static method call.
842 HClinitCheck* clinit_check = nullptr;
843 HInvoke* invoke = nullptr;
844 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
845 // By default, consider that the called method implicitly requires
846 // an initialization check of its declaring method.
847 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
848 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
849 ScopedObjectAccess soa(Thread::Current());
850 if (invoke_type == kStatic) {
851 clinit_check = ProcessClinitCheckForInvoke(
852 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
853 } else if (invoke_type == kSuper) {
854 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100855 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000856 // we resolved to the method referenced by the instruction.
857 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000858 }
859 }
860
861 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
862 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
863 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000864 0u
David Brazdildee58d62016-04-07 09:54:26 +0000865 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100866 MethodReference target_method(resolved_method->GetDexFile(),
867 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000868 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
869 number_of_arguments,
870 return_type,
871 dex_pc,
872 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100873 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000874 dispatch_info,
875 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100876 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000877 clinit_check_requirement);
878 } else if (invoke_type == kVirtual) {
879 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
880 invoke = new (arena_) HInvokeVirtual(arena_,
881 number_of_arguments,
882 return_type,
883 dex_pc,
884 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100885 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000886 resolved_method->GetMethodIndex());
887 } else {
888 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100889 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000890 invoke = new (arena_) HInvokeInterface(arena_,
891 number_of_arguments,
892 return_type,
893 dex_pc,
894 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100895 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700896 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000897 }
898
899 return HandleInvoke(invoke,
900 number_of_vreg_arguments,
901 args,
902 register_index,
903 is_range,
904 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700905 clinit_check,
906 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000907}
908
Andreas Gampea5b09a62016-11-17 15:21:22 -0800909bool HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100910 ScopedObjectAccess soa(Thread::Current());
911 StackHandleScope<1> hs(soa.Self());
912 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
913 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
914 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
915 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
916
David Brazdildee58d62016-04-07 09:54:26 +0000917 bool finalizable;
Mingyao Yang062157f2016-03-02 10:15:36 -0800918 bool needs_access_check = NeedsAccessCheck(type_index, dex_cache, &finalizable);
David Brazdildee58d62016-04-07 09:54:26 +0000919
920 // Only the non-resolved entrypoint handles the finalizable class case. If we
921 // need access checks, then we haven't resolved the method and the class may
922 // again be finalizable.
Mingyao Yang062157f2016-03-02 10:15:36 -0800923 QuickEntrypointEnum entrypoint = (finalizable || needs_access_check)
David Brazdildee58d62016-04-07 09:54:26 +0000924 ? kQuickAllocObject
925 : kQuickAllocObjectInitialized;
926
David Brazdildee58d62016-04-07 09:54:26 +0000927 if (outer_dex_cache.Get() != dex_cache.Get()) {
928 // We currently do not support inlining allocations across dex files.
929 return false;
930 }
931
932 HLoadClass* load_class = new (arena_) HLoadClass(
933 graph_->GetCurrentMethod(),
934 type_index,
935 outer_dex_file,
936 IsOutermostCompilingClass(type_index),
937 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +0000938 needs_access_check);
David Brazdildee58d62016-04-07 09:54:26 +0000939
940 AppendInstruction(load_class);
941 HInstruction* cls = load_class;
942 if (!IsInitialized(resolved_class)) {
943 cls = new (arena_) HClinitCheck(load_class, dex_pc);
944 AppendInstruction(cls);
945 }
946
947 AppendInstruction(new (arena_) HNewInstance(
948 cls,
949 graph_->GetCurrentMethod(),
950 dex_pc,
951 type_index,
952 *dex_compilation_unit_->GetDexFile(),
Mingyao Yang062157f2016-03-02 10:15:36 -0800953 needs_access_check,
David Brazdildee58d62016-04-07 09:54:26 +0000954 finalizable,
955 entrypoint));
956 return true;
957}
958
959static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700960 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000961 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
962}
963
964bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
965 if (cls.Get() == nullptr) {
966 return false;
967 }
968
969 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
970 // check whether the class is in an image for the AOT compilation.
971 if (cls->IsInitialized() &&
972 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
973 return true;
974 }
975
976 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
977 return true;
978 }
979
980 // TODO: We should walk over the inlined methods, but we don't pass
981 // that information to the builder.
982 if (IsSubClass(GetCompilingClass(), cls.Get())) {
983 return true;
984 }
985
986 return false;
987}
988
989HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
990 uint32_t dex_pc,
991 ArtMethod* resolved_method,
992 uint32_t method_idx,
993 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
994 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
995 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100996 StackHandleScope<2> hs(self);
997 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
998 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +0000999 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1000 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
1001
1002 // The index at which the method's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001003 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001004 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1005 if (is_outer_class) {
1006 storage_index = outer_class->GetDexTypeIndex();
1007 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1008 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1009 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1010 GetCompilingClass(),
1011 resolved_method,
1012 method_idx,
1013 &storage_index);
1014 }
1015
1016 HClinitCheck* clinit_check = nullptr;
1017
1018 if (IsInitialized(resolved_method_class)) {
1019 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001020 } else if (storage_index.IsValid()) {
David Brazdildee58d62016-04-07 09:54:26 +00001021 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1022 HLoadClass* load_class = new (arena_) HLoadClass(
1023 graph_->GetCurrentMethod(),
1024 storage_index,
1025 outer_dex_file,
1026 is_outer_class,
1027 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001028 /*needs_access_check*/ false);
David Brazdildee58d62016-04-07 09:54:26 +00001029 AppendInstruction(load_class);
1030 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1031 AppendInstruction(clinit_check);
1032 }
1033 return clinit_check;
1034}
1035
1036bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1037 uint32_t number_of_vreg_arguments,
1038 uint32_t* args,
1039 uint32_t register_index,
1040 bool is_range,
1041 const char* descriptor,
1042 size_t start_index,
1043 size_t* argument_index) {
1044 uint32_t descriptor_index = 1; // Skip the return type.
1045
1046 for (size_t i = start_index;
1047 // Make sure we don't go over the expected arguments or over the number of
1048 // dex registers given. If the instruction was seen as dead by the verifier,
1049 // it hasn't been properly checked.
1050 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1051 i++, (*argument_index)++) {
1052 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1053 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1054 if (!is_range
1055 && is_wide
1056 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1057 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1058 // reject any class where this is violated. However, the verifier only does these checks
1059 // on non trivially dead instructions, so we just bailout the compilation.
1060 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001061 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001062 << " because of non-sequential dex register pair in wide argument";
1063 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1064 return false;
1065 }
1066 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1067 invoke->SetArgumentAt(*argument_index, arg);
1068 if (is_wide) {
1069 i++;
1070 }
1071 }
1072
1073 if (*argument_index != invoke->GetNumberOfArguments()) {
1074 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001075 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001076 << " because of wrong number of arguments in invoke instruction";
1077 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1078 return false;
1079 }
1080
1081 if (invoke->IsInvokeStaticOrDirect() &&
1082 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1083 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1084 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1085 (*argument_index)++;
1086 }
1087
1088 return true;
1089}
1090
1091bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1092 uint32_t number_of_vreg_arguments,
1093 uint32_t* args,
1094 uint32_t register_index,
1095 bool is_range,
1096 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001097 HClinitCheck* clinit_check,
1098 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001099 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1100
1101 size_t start_index = 0;
1102 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001103 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001104 uint32_t obj_reg = is_range ? register_index : args[0];
1105 HInstruction* arg = is_unresolved
1106 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1107 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001108 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001109 start_index = 1;
1110 argument_index = 1;
1111 }
1112
1113 if (!SetupInvokeArguments(invoke,
1114 number_of_vreg_arguments,
1115 args,
1116 register_index,
1117 is_range,
1118 descriptor,
1119 start_index,
1120 &argument_index)) {
1121 return false;
1122 }
1123
1124 if (clinit_check != nullptr) {
1125 // Add the class initialization check as last input of `invoke`.
1126 DCHECK(invoke->IsInvokeStaticOrDirect());
1127 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1128 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1129 invoke->SetArgumentAt(argument_index, clinit_check);
1130 argument_index++;
1131 }
1132
1133 AppendInstruction(invoke);
1134 latest_result_ = invoke;
1135
1136 return true;
1137}
1138
1139bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1140 uint32_t number_of_vreg_arguments,
1141 uint32_t* args,
1142 uint32_t register_index,
1143 bool is_range,
1144 const char* descriptor) {
1145 DCHECK(invoke->IsInvokeStaticOrDirect());
1146 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1147
1148 size_t start_index = 1;
1149 size_t argument_index = 0;
1150 if (!SetupInvokeArguments(invoke,
1151 number_of_vreg_arguments,
1152 args,
1153 register_index,
1154 is_range,
1155 descriptor,
1156 start_index,
1157 &argument_index)) {
1158 return false;
1159 }
1160
1161 AppendInstruction(invoke);
1162
1163 // This is a StringFactory call, not an actual String constructor. Its result
1164 // replaces the empty String pre-allocated by NewInstance.
1165 uint32_t orig_this_reg = is_range ? register_index : args[0];
1166 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1167
1168 // Replacing the NewInstance might render it redundant. Keep a list of these
1169 // to be visited once it is clear whether it is has remaining uses.
1170 if (arg_this->IsNewInstance()) {
1171 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1172 } else {
1173 DCHECK(arg_this->IsPhi());
1174 // NewInstance is not the direct input of the StringFactory call. It might
1175 // be redundant but optimizing this case is not worth the effort.
1176 }
1177
1178 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1179 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1180 if ((*current_locals_)[vreg] == arg_this) {
1181 (*current_locals_)[vreg] = invoke;
1182 }
1183 }
1184
1185 return true;
1186}
1187
1188static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1189 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1190 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1191 return Primitive::GetType(type[0]);
1192}
1193
1194bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1195 uint32_t dex_pc,
1196 bool is_put) {
1197 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1198 uint32_t obj_reg = instruction.VRegB_22c();
1199 uint16_t field_index;
1200 if (instruction.IsQuickened()) {
1201 if (!CanDecodeQuickenedInfo()) {
1202 return false;
1203 }
1204 field_index = LookupQuickenedInfo(dex_pc);
1205 } else {
1206 field_index = instruction.VRegC_22c();
1207 }
1208
1209 ScopedObjectAccess soa(Thread::Current());
1210 ArtField* resolved_field =
1211 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1212
1213
Aart Bik14154132016-06-02 17:53:58 -07001214 // Generate an explicit null check on the reference, unless the field access
1215 // is unresolved. In that case, we rely on the runtime to perform various
1216 // checks first, followed by a null check.
1217 HInstruction* object = (resolved_field == nullptr)
1218 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1219 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001220
1221 Primitive::Type field_type = (resolved_field == nullptr)
1222 ? GetFieldAccessType(*dex_file_, field_index)
1223 : resolved_field->GetTypeAsPrimitiveType();
1224 if (is_put) {
1225 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1226 HInstruction* field_set = nullptr;
1227 if (resolved_field == nullptr) {
1228 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001229 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001230 value,
1231 field_type,
1232 field_index,
1233 dex_pc);
1234 } else {
1235 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001236 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001237 value,
1238 field_type,
1239 resolved_field->GetOffset(),
1240 resolved_field->IsVolatile(),
1241 field_index,
1242 class_def_index,
1243 *dex_file_,
1244 dex_compilation_unit_->GetDexCache(),
1245 dex_pc);
1246 }
1247 AppendInstruction(field_set);
1248 } else {
1249 HInstruction* field_get = nullptr;
1250 if (resolved_field == nullptr) {
1251 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001252 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001253 field_type,
1254 field_index,
1255 dex_pc);
1256 } else {
1257 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001258 field_get = new (arena_) HInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001259 field_type,
1260 resolved_field->GetOffset(),
1261 resolved_field->IsVolatile(),
1262 field_index,
1263 class_def_index,
1264 *dex_file_,
1265 dex_compilation_unit_->GetDexCache(),
1266 dex_pc);
1267 }
1268 AppendInstruction(field_get);
1269 UpdateLocal(source_or_dest_reg, field_get);
1270 }
1271
1272 return true;
1273}
1274
1275static mirror::Class* GetClassFrom(CompilerDriver* driver,
1276 const DexCompilationUnit& compilation_unit) {
1277 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001278 StackHandleScope<1> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +00001279 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001280 soa.Decode<mirror::ClassLoader>(compilation_unit.GetClassLoader())));
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001281 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001282
1283 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1284}
1285
1286mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1287 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1288}
1289
1290mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1291 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1292}
1293
Andreas Gampea5b09a62016-11-17 15:21:22 -08001294bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001295 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001296 StackHandleScope<3> hs(soa.Self());
1297 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001298 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001299 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001300 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1301 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1302 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1303
1304 // GetOutermostCompilingClass returns null when the class is unresolved
1305 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1306 // we are compiling it.
1307 // When this happens we cannot establish a direct relation between the current
1308 // class and the outer class, so we return false.
1309 // (Note that this is only used for optimizing invokes and field accesses)
1310 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1311}
1312
1313void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1314 uint32_t dex_pc,
1315 bool is_put,
1316 Primitive::Type field_type) {
1317 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1318 uint16_t field_index = instruction.VRegB_21c();
1319
1320 if (is_put) {
1321 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1322 AppendInstruction(
1323 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1324 } else {
1325 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1326 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1327 }
1328}
1329
1330bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1331 uint32_t dex_pc,
1332 bool is_put) {
1333 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1334 uint16_t field_index = instruction.VRegB_21c();
1335
1336 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001337 StackHandleScope<3> hs(soa.Self());
1338 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001339 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001340 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001341 ArtField* resolved_field = compiler_driver_->ResolveField(
1342 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1343
1344 if (resolved_field == nullptr) {
1345 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1346 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1347 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1348 return true;
1349 }
1350
1351 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
1352 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001353 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001354 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1355
1356 // The index at which the field's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001357 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001358 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1359 if (is_outer_class) {
1360 storage_index = outer_class->GetDexTypeIndex();
1361 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1362 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1363 return false;
1364 } else {
1365 // TODO: This is rather expensive. Perf it and cache the results if needed.
1366 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1367 outer_dex_cache.Get(),
1368 GetCompilingClass(),
1369 resolved_field,
1370 field_index,
1371 &storage_index);
1372 bool can_easily_access = is_put ? pair.second : pair.first;
1373 if (!can_easily_access) {
1374 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1375 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1376 return true;
1377 }
1378 }
1379
David Brazdildee58d62016-04-07 09:54:26 +00001380 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1381 storage_index,
1382 outer_dex_file,
1383 is_outer_class,
1384 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001385 /*needs_access_check*/ false);
David Brazdildee58d62016-04-07 09:54:26 +00001386 AppendInstruction(constant);
1387
1388 HInstruction* cls = constant;
1389
1390 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1391 if (!IsInitialized(klass)) {
1392 cls = new (arena_) HClinitCheck(constant, dex_pc);
1393 AppendInstruction(cls);
1394 }
1395
1396 uint16_t class_def_index = klass->GetDexClassDefIndex();
1397 if (is_put) {
1398 // We need to keep the class alive before loading the value.
1399 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1400 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1401 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1402 value,
1403 field_type,
1404 resolved_field->GetOffset(),
1405 resolved_field->IsVolatile(),
1406 field_index,
1407 class_def_index,
1408 *dex_file_,
1409 dex_cache_,
1410 dex_pc));
1411 } else {
1412 AppendInstruction(new (arena_) HStaticFieldGet(cls,
1413 field_type,
1414 resolved_field->GetOffset(),
1415 resolved_field->IsVolatile(),
1416 field_index,
1417 class_def_index,
1418 *dex_file_,
1419 dex_cache_,
1420 dex_pc));
1421 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1422 }
1423 return true;
1424}
1425
1426void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1427 uint16_t first_vreg,
1428 int64_t second_vreg_or_constant,
1429 uint32_t dex_pc,
1430 Primitive::Type type,
1431 bool second_is_constant,
1432 bool isDiv) {
1433 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1434
1435 HInstruction* first = LoadLocal(first_vreg, type);
1436 HInstruction* second = nullptr;
1437 if (second_is_constant) {
1438 if (type == Primitive::kPrimInt) {
1439 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1440 } else {
1441 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1442 }
1443 } else {
1444 second = LoadLocal(second_vreg_or_constant, type);
1445 }
1446
1447 if (!second_is_constant
1448 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1449 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1450 second = new (arena_) HDivZeroCheck(second, dex_pc);
1451 AppendInstruction(second);
1452 }
1453
1454 if (isDiv) {
1455 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1456 } else {
1457 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1458 }
1459 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1460}
1461
1462void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1463 uint32_t dex_pc,
1464 bool is_put,
1465 Primitive::Type anticipated_type) {
1466 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1467 uint8_t array_reg = instruction.VRegB_23x();
1468 uint8_t index_reg = instruction.VRegC_23x();
1469
David Brazdilc120bbe2016-04-22 16:57:00 +01001470 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001471 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1472 AppendInstruction(length);
1473 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1474 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1475 AppendInstruction(index);
1476 if (is_put) {
1477 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1478 // TODO: Insert a type check node if the type is Object.
1479 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1480 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1481 AppendInstruction(aset);
1482 } else {
1483 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1484 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1485 AppendInstruction(aget);
1486 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1487 }
1488 graph_->SetHasBoundsChecks(true);
1489}
1490
1491void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001492 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001493 uint32_t number_of_vreg_arguments,
1494 bool is_range,
1495 uint32_t* args,
1496 uint32_t register_index) {
1497 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
1498 bool finalizable;
1499 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
1500 ? kQuickAllocArrayWithAccessCheck
1501 : kQuickAllocArray;
1502 HInstruction* object = new (arena_) HNewArray(length,
1503 graph_->GetCurrentMethod(),
1504 dex_pc,
1505 type_index,
1506 *dex_compilation_unit_->GetDexFile(),
1507 entrypoint);
1508 AppendInstruction(object);
1509
1510 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1511 DCHECK_EQ(descriptor[0], '[') << descriptor;
1512 char primitive = descriptor[1];
1513 DCHECK(primitive == 'I'
1514 || primitive == 'L'
1515 || primitive == '[') << descriptor;
1516 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1517 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1518
1519 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1520 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1521 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1522 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1523 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1524 AppendInstruction(aset);
1525 }
1526 latest_result_ = object;
1527}
1528
1529template <typename T>
1530void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1531 const T* data,
1532 uint32_t element_count,
1533 Primitive::Type anticipated_type,
1534 uint32_t dex_pc) {
1535 for (uint32_t i = 0; i < element_count; ++i) {
1536 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1537 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1538 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1539 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1540 AppendInstruction(aset);
1541 }
1542}
1543
1544void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001545 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001546
1547 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1548 const Instruction::ArrayDataPayload* payload =
1549 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1550 const uint8_t* data = payload->data;
1551 uint32_t element_count = payload->element_count;
1552
Vladimir Markoc69fba22016-09-06 16:49:15 +01001553 if (element_count == 0u) {
1554 // For empty payload we emit only the null check above.
1555 return;
1556 }
1557
1558 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1559 AppendInstruction(length);
1560
David Brazdildee58d62016-04-07 09:54:26 +00001561 // Implementation of this DEX instruction seems to be that the bounds check is
1562 // done before doing any stores.
1563 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1564 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1565
1566 switch (payload->element_width) {
1567 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001568 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001569 reinterpret_cast<const int8_t*>(data),
1570 element_count,
1571 Primitive::kPrimByte,
1572 dex_pc);
1573 break;
1574 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001575 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001576 reinterpret_cast<const int16_t*>(data),
1577 element_count,
1578 Primitive::kPrimShort,
1579 dex_pc);
1580 break;
1581 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001582 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001583 reinterpret_cast<const int32_t*>(data),
1584 element_count,
1585 Primitive::kPrimInt,
1586 dex_pc);
1587 break;
1588 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001589 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001590 reinterpret_cast<const int64_t*>(data),
1591 element_count,
1592 dex_pc);
1593 break;
1594 default:
1595 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1596 }
1597 graph_->SetHasBoundsChecks(true);
1598}
1599
1600void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1601 const int64_t* data,
1602 uint32_t element_count,
1603 uint32_t dex_pc) {
1604 for (uint32_t i = 0; i < element_count; ++i) {
1605 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1606 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1607 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1608 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1609 AppendInstruction(aset);
1610 }
1611}
1612
1613static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001614 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001615 if (cls.Get() == nullptr) {
1616 return TypeCheckKind::kUnresolvedCheck;
1617 } else if (cls->IsInterface()) {
1618 return TypeCheckKind::kInterfaceCheck;
1619 } else if (cls->IsArrayClass()) {
1620 if (cls->GetComponentType()->IsObjectClass()) {
1621 return TypeCheckKind::kArrayObjectCheck;
1622 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1623 return TypeCheckKind::kExactCheck;
1624 } else {
1625 return TypeCheckKind::kArrayCheck;
1626 }
1627 } else if (cls->IsFinal()) {
1628 return TypeCheckKind::kExactCheck;
1629 } else if (cls->IsAbstract()) {
1630 return TypeCheckKind::kAbstractClassCheck;
1631 } else {
1632 return TypeCheckKind::kClassHierarchyCheck;
1633 }
1634}
1635
1636void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1637 uint8_t destination,
1638 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001639 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001640 uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001641 ScopedObjectAccess soa(Thread::Current());
1642 StackHandleScope<1> hs(soa.Self());
1643 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1644 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1645 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1646
David Brazdildee58d62016-04-07 09:54:26 +00001647 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1648 dex_compilation_unit_->GetDexMethodIndex(),
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001649 dex_cache,
1650 type_index);
David Brazdildee58d62016-04-07 09:54:26 +00001651
1652 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
1653 HLoadClass* cls = new (arena_) HLoadClass(
1654 graph_->GetCurrentMethod(),
1655 type_index,
1656 dex_file,
1657 IsOutermostCompilingClass(type_index),
1658 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001659 !can_access);
David Brazdildee58d62016-04-07 09:54:26 +00001660 AppendInstruction(cls);
1661
1662 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
1663 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1664 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1665 UpdateLocal(destination, current_block_->GetLastInstruction());
1666 } else {
1667 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1668 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1669 // which may throw. If it succeeds BoundType sets the new type of `object`
1670 // for all subsequent uses.
1671 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1672 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1673 UpdateLocal(reference, current_block_->GetLastInstruction());
1674 }
1675}
1676
Andreas Gampea5b09a62016-11-17 15:21:22 -08001677bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index,
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001678 Handle<mirror::DexCache> dex_cache,
1679 bool* finalizable) const {
David Brazdildee58d62016-04-07 09:54:26 +00001680 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001681 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index, finalizable);
1682}
1683
Andreas Gampea5b09a62016-11-17 15:21:22 -08001684bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001685 ScopedObjectAccess soa(Thread::Current());
1686 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1687 return NeedsAccessCheck(type_index, dex_cache, finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001688}
1689
1690bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1691 return interpreter_metadata_ != nullptr;
1692}
1693
1694uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1695 DCHECK(interpreter_metadata_ != nullptr);
1696
1697 // First check if the info has already been decoded from `interpreter_metadata_`.
1698 auto it = skipped_interpreter_metadata_.find(dex_pc);
1699 if (it != skipped_interpreter_metadata_.end()) {
1700 // Remove the entry from the map and return the parsed info.
1701 uint16_t value_in_map = it->second;
1702 skipped_interpreter_metadata_.erase(it);
1703 return value_in_map;
1704 }
1705
1706 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1707 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1708 while (true) {
1709 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1710 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1711 DCHECK_LE(dex_pc_in_map, dex_pc);
1712
1713 if (dex_pc_in_map == dex_pc) {
1714 return value_in_map;
1715 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001716 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1717 // the same dex_pc. This is OK, because the compiler does not care about those
1718 // entries.
1719 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001720 }
1721 }
1722}
1723
1724bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1725 switch (instruction.Opcode()) {
1726 case Instruction::CONST_4: {
1727 int32_t register_index = instruction.VRegA();
1728 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1729 UpdateLocal(register_index, constant);
1730 break;
1731 }
1732
1733 case Instruction::CONST_16: {
1734 int32_t register_index = instruction.VRegA();
1735 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1736 UpdateLocal(register_index, constant);
1737 break;
1738 }
1739
1740 case Instruction::CONST: {
1741 int32_t register_index = instruction.VRegA();
1742 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1743 UpdateLocal(register_index, constant);
1744 break;
1745 }
1746
1747 case Instruction::CONST_HIGH16: {
1748 int32_t register_index = instruction.VRegA();
1749 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1750 UpdateLocal(register_index, constant);
1751 break;
1752 }
1753
1754 case Instruction::CONST_WIDE_16: {
1755 int32_t register_index = instruction.VRegA();
1756 // Get 16 bits of constant value, sign extended to 64 bits.
1757 int64_t value = instruction.VRegB_21s();
1758 value <<= 48;
1759 value >>= 48;
1760 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1761 UpdateLocal(register_index, constant);
1762 break;
1763 }
1764
1765 case Instruction::CONST_WIDE_32: {
1766 int32_t register_index = instruction.VRegA();
1767 // Get 32 bits of constant value, sign extended to 64 bits.
1768 int64_t value = instruction.VRegB_31i();
1769 value <<= 32;
1770 value >>= 32;
1771 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1772 UpdateLocal(register_index, constant);
1773 break;
1774 }
1775
1776 case Instruction::CONST_WIDE: {
1777 int32_t register_index = instruction.VRegA();
1778 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1779 UpdateLocal(register_index, constant);
1780 break;
1781 }
1782
1783 case Instruction::CONST_WIDE_HIGH16: {
1784 int32_t register_index = instruction.VRegA();
1785 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1786 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1787 UpdateLocal(register_index, constant);
1788 break;
1789 }
1790
1791 // Note that the SSA building will refine the types.
1792 case Instruction::MOVE:
1793 case Instruction::MOVE_FROM16:
1794 case Instruction::MOVE_16: {
1795 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1796 UpdateLocal(instruction.VRegA(), value);
1797 break;
1798 }
1799
1800 // Note that the SSA building will refine the types.
1801 case Instruction::MOVE_WIDE:
1802 case Instruction::MOVE_WIDE_FROM16:
1803 case Instruction::MOVE_WIDE_16: {
1804 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1805 UpdateLocal(instruction.VRegA(), value);
1806 break;
1807 }
1808
1809 case Instruction::MOVE_OBJECT:
1810 case Instruction::MOVE_OBJECT_16:
1811 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001812 // The verifier has no notion of a null type, so a move-object of constant 0
1813 // will lead to the same constant 0 in the destination register. To mimic
1814 // this behavior, we just pretend we haven't seen a type change (int to reference)
1815 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1816 // types correct.
1817 uint32_t reg_number = instruction.VRegB();
1818 HInstruction* value = (*current_locals_)[reg_number];
1819 if (value->IsIntConstant()) {
1820 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1821 } else if (value->IsPhi()) {
1822 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1823 } else {
1824 value = LoadLocal(reg_number, Primitive::kPrimNot);
1825 }
David Brazdildee58d62016-04-07 09:54:26 +00001826 UpdateLocal(instruction.VRegA(), value);
1827 break;
1828 }
1829
1830 case Instruction::RETURN_VOID_NO_BARRIER:
1831 case Instruction::RETURN_VOID: {
1832 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1833 break;
1834 }
1835
1836#define IF_XX(comparison, cond) \
1837 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1838 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1839
1840 IF_XX(HEqual, EQ);
1841 IF_XX(HNotEqual, NE);
1842 IF_XX(HLessThan, LT);
1843 IF_XX(HLessThanOrEqual, LE);
1844 IF_XX(HGreaterThan, GT);
1845 IF_XX(HGreaterThanOrEqual, GE);
1846
1847 case Instruction::GOTO:
1848 case Instruction::GOTO_16:
1849 case Instruction::GOTO_32: {
1850 AppendInstruction(new (arena_) HGoto(dex_pc));
1851 current_block_ = nullptr;
1852 break;
1853 }
1854
1855 case Instruction::RETURN: {
1856 BuildReturn(instruction, return_type_, dex_pc);
1857 break;
1858 }
1859
1860 case Instruction::RETURN_OBJECT: {
1861 BuildReturn(instruction, return_type_, dex_pc);
1862 break;
1863 }
1864
1865 case Instruction::RETURN_WIDE: {
1866 BuildReturn(instruction, return_type_, dex_pc);
1867 break;
1868 }
1869
1870 case Instruction::INVOKE_DIRECT:
1871 case Instruction::INVOKE_INTERFACE:
1872 case Instruction::INVOKE_STATIC:
1873 case Instruction::INVOKE_SUPER:
1874 case Instruction::INVOKE_VIRTUAL:
1875 case Instruction::INVOKE_VIRTUAL_QUICK: {
1876 uint16_t method_idx;
1877 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1878 if (!CanDecodeQuickenedInfo()) {
1879 return false;
1880 }
1881 method_idx = LookupQuickenedInfo(dex_pc);
1882 } else {
1883 method_idx = instruction.VRegB_35c();
1884 }
1885 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1886 uint32_t args[5];
1887 instruction.GetVarArgs(args);
1888 if (!BuildInvoke(instruction, dex_pc, method_idx,
1889 number_of_vreg_arguments, false, args, -1)) {
1890 return false;
1891 }
1892 break;
1893 }
1894
1895 case Instruction::INVOKE_DIRECT_RANGE:
1896 case Instruction::INVOKE_INTERFACE_RANGE:
1897 case Instruction::INVOKE_STATIC_RANGE:
1898 case Instruction::INVOKE_SUPER_RANGE:
1899 case Instruction::INVOKE_VIRTUAL_RANGE:
1900 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1901 uint16_t method_idx;
1902 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1903 if (!CanDecodeQuickenedInfo()) {
1904 return false;
1905 }
1906 method_idx = LookupQuickenedInfo(dex_pc);
1907 } else {
1908 method_idx = instruction.VRegB_3rc();
1909 }
1910 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1911 uint32_t register_index = instruction.VRegC();
1912 if (!BuildInvoke(instruction, dex_pc, method_idx,
1913 number_of_vreg_arguments, true, nullptr, register_index)) {
1914 return false;
1915 }
1916 break;
1917 }
1918
1919 case Instruction::NEG_INT: {
1920 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1921 break;
1922 }
1923
1924 case Instruction::NEG_LONG: {
1925 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1926 break;
1927 }
1928
1929 case Instruction::NEG_FLOAT: {
1930 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1931 break;
1932 }
1933
1934 case Instruction::NEG_DOUBLE: {
1935 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1936 break;
1937 }
1938
1939 case Instruction::NOT_INT: {
1940 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1941 break;
1942 }
1943
1944 case Instruction::NOT_LONG: {
1945 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1946 break;
1947 }
1948
1949 case Instruction::INT_TO_LONG: {
1950 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
1951 break;
1952 }
1953
1954 case Instruction::INT_TO_FLOAT: {
1955 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
1956 break;
1957 }
1958
1959 case Instruction::INT_TO_DOUBLE: {
1960 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
1961 break;
1962 }
1963
1964 case Instruction::LONG_TO_INT: {
1965 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
1966 break;
1967 }
1968
1969 case Instruction::LONG_TO_FLOAT: {
1970 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
1971 break;
1972 }
1973
1974 case Instruction::LONG_TO_DOUBLE: {
1975 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
1976 break;
1977 }
1978
1979 case Instruction::FLOAT_TO_INT: {
1980 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1981 break;
1982 }
1983
1984 case Instruction::FLOAT_TO_LONG: {
1985 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
1986 break;
1987 }
1988
1989 case Instruction::FLOAT_TO_DOUBLE: {
1990 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1991 break;
1992 }
1993
1994 case Instruction::DOUBLE_TO_INT: {
1995 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1996 break;
1997 }
1998
1999 case Instruction::DOUBLE_TO_LONG: {
2000 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2001 break;
2002 }
2003
2004 case Instruction::DOUBLE_TO_FLOAT: {
2005 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2006 break;
2007 }
2008
2009 case Instruction::INT_TO_BYTE: {
2010 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2011 break;
2012 }
2013
2014 case Instruction::INT_TO_SHORT: {
2015 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2016 break;
2017 }
2018
2019 case Instruction::INT_TO_CHAR: {
2020 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2021 break;
2022 }
2023
2024 case Instruction::ADD_INT: {
2025 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2026 break;
2027 }
2028
2029 case Instruction::ADD_LONG: {
2030 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2031 break;
2032 }
2033
2034 case Instruction::ADD_DOUBLE: {
2035 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2036 break;
2037 }
2038
2039 case Instruction::ADD_FLOAT: {
2040 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2041 break;
2042 }
2043
2044 case Instruction::SUB_INT: {
2045 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2046 break;
2047 }
2048
2049 case Instruction::SUB_LONG: {
2050 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2051 break;
2052 }
2053
2054 case Instruction::SUB_FLOAT: {
2055 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2056 break;
2057 }
2058
2059 case Instruction::SUB_DOUBLE: {
2060 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2061 break;
2062 }
2063
2064 case Instruction::ADD_INT_2ADDR: {
2065 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2066 break;
2067 }
2068
2069 case Instruction::MUL_INT: {
2070 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2071 break;
2072 }
2073
2074 case Instruction::MUL_LONG: {
2075 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2076 break;
2077 }
2078
2079 case Instruction::MUL_FLOAT: {
2080 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2081 break;
2082 }
2083
2084 case Instruction::MUL_DOUBLE: {
2085 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2086 break;
2087 }
2088
2089 case Instruction::DIV_INT: {
2090 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2091 dex_pc, Primitive::kPrimInt, false, true);
2092 break;
2093 }
2094
2095 case Instruction::DIV_LONG: {
2096 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2097 dex_pc, Primitive::kPrimLong, false, true);
2098 break;
2099 }
2100
2101 case Instruction::DIV_FLOAT: {
2102 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2103 break;
2104 }
2105
2106 case Instruction::DIV_DOUBLE: {
2107 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2108 break;
2109 }
2110
2111 case Instruction::REM_INT: {
2112 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2113 dex_pc, Primitive::kPrimInt, false, false);
2114 break;
2115 }
2116
2117 case Instruction::REM_LONG: {
2118 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2119 dex_pc, Primitive::kPrimLong, false, false);
2120 break;
2121 }
2122
2123 case Instruction::REM_FLOAT: {
2124 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2125 break;
2126 }
2127
2128 case Instruction::REM_DOUBLE: {
2129 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2130 break;
2131 }
2132
2133 case Instruction::AND_INT: {
2134 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2135 break;
2136 }
2137
2138 case Instruction::AND_LONG: {
2139 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2140 break;
2141 }
2142
2143 case Instruction::SHL_INT: {
2144 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2145 break;
2146 }
2147
2148 case Instruction::SHL_LONG: {
2149 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2150 break;
2151 }
2152
2153 case Instruction::SHR_INT: {
2154 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2155 break;
2156 }
2157
2158 case Instruction::SHR_LONG: {
2159 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2160 break;
2161 }
2162
2163 case Instruction::USHR_INT: {
2164 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2165 break;
2166 }
2167
2168 case Instruction::USHR_LONG: {
2169 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2170 break;
2171 }
2172
2173 case Instruction::OR_INT: {
2174 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2175 break;
2176 }
2177
2178 case Instruction::OR_LONG: {
2179 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2180 break;
2181 }
2182
2183 case Instruction::XOR_INT: {
2184 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2185 break;
2186 }
2187
2188 case Instruction::XOR_LONG: {
2189 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2190 break;
2191 }
2192
2193 case Instruction::ADD_LONG_2ADDR: {
2194 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2195 break;
2196 }
2197
2198 case Instruction::ADD_DOUBLE_2ADDR: {
2199 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2200 break;
2201 }
2202
2203 case Instruction::ADD_FLOAT_2ADDR: {
2204 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2205 break;
2206 }
2207
2208 case Instruction::SUB_INT_2ADDR: {
2209 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2210 break;
2211 }
2212
2213 case Instruction::SUB_LONG_2ADDR: {
2214 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2215 break;
2216 }
2217
2218 case Instruction::SUB_FLOAT_2ADDR: {
2219 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2220 break;
2221 }
2222
2223 case Instruction::SUB_DOUBLE_2ADDR: {
2224 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2225 break;
2226 }
2227
2228 case Instruction::MUL_INT_2ADDR: {
2229 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2230 break;
2231 }
2232
2233 case Instruction::MUL_LONG_2ADDR: {
2234 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2235 break;
2236 }
2237
2238 case Instruction::MUL_FLOAT_2ADDR: {
2239 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2240 break;
2241 }
2242
2243 case Instruction::MUL_DOUBLE_2ADDR: {
2244 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2245 break;
2246 }
2247
2248 case Instruction::DIV_INT_2ADDR: {
2249 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2250 dex_pc, Primitive::kPrimInt, false, true);
2251 break;
2252 }
2253
2254 case Instruction::DIV_LONG_2ADDR: {
2255 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2256 dex_pc, Primitive::kPrimLong, false, true);
2257 break;
2258 }
2259
2260 case Instruction::REM_INT_2ADDR: {
2261 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2262 dex_pc, Primitive::kPrimInt, false, false);
2263 break;
2264 }
2265
2266 case Instruction::REM_LONG_2ADDR: {
2267 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2268 dex_pc, Primitive::kPrimLong, false, false);
2269 break;
2270 }
2271
2272 case Instruction::REM_FLOAT_2ADDR: {
2273 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2274 break;
2275 }
2276
2277 case Instruction::REM_DOUBLE_2ADDR: {
2278 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2279 break;
2280 }
2281
2282 case Instruction::SHL_INT_2ADDR: {
2283 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2284 break;
2285 }
2286
2287 case Instruction::SHL_LONG_2ADDR: {
2288 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2289 break;
2290 }
2291
2292 case Instruction::SHR_INT_2ADDR: {
2293 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2294 break;
2295 }
2296
2297 case Instruction::SHR_LONG_2ADDR: {
2298 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2299 break;
2300 }
2301
2302 case Instruction::USHR_INT_2ADDR: {
2303 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2304 break;
2305 }
2306
2307 case Instruction::USHR_LONG_2ADDR: {
2308 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2309 break;
2310 }
2311
2312 case Instruction::DIV_FLOAT_2ADDR: {
2313 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2314 break;
2315 }
2316
2317 case Instruction::DIV_DOUBLE_2ADDR: {
2318 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2319 break;
2320 }
2321
2322 case Instruction::AND_INT_2ADDR: {
2323 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2324 break;
2325 }
2326
2327 case Instruction::AND_LONG_2ADDR: {
2328 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2329 break;
2330 }
2331
2332 case Instruction::OR_INT_2ADDR: {
2333 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2334 break;
2335 }
2336
2337 case Instruction::OR_LONG_2ADDR: {
2338 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2339 break;
2340 }
2341
2342 case Instruction::XOR_INT_2ADDR: {
2343 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2344 break;
2345 }
2346
2347 case Instruction::XOR_LONG_2ADDR: {
2348 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2349 break;
2350 }
2351
2352 case Instruction::ADD_INT_LIT16: {
2353 Binop_22s<HAdd>(instruction, false, dex_pc);
2354 break;
2355 }
2356
2357 case Instruction::AND_INT_LIT16: {
2358 Binop_22s<HAnd>(instruction, false, dex_pc);
2359 break;
2360 }
2361
2362 case Instruction::OR_INT_LIT16: {
2363 Binop_22s<HOr>(instruction, false, dex_pc);
2364 break;
2365 }
2366
2367 case Instruction::XOR_INT_LIT16: {
2368 Binop_22s<HXor>(instruction, false, dex_pc);
2369 break;
2370 }
2371
2372 case Instruction::RSUB_INT: {
2373 Binop_22s<HSub>(instruction, true, dex_pc);
2374 break;
2375 }
2376
2377 case Instruction::MUL_INT_LIT16: {
2378 Binop_22s<HMul>(instruction, false, dex_pc);
2379 break;
2380 }
2381
2382 case Instruction::ADD_INT_LIT8: {
2383 Binop_22b<HAdd>(instruction, false, dex_pc);
2384 break;
2385 }
2386
2387 case Instruction::AND_INT_LIT8: {
2388 Binop_22b<HAnd>(instruction, false, dex_pc);
2389 break;
2390 }
2391
2392 case Instruction::OR_INT_LIT8: {
2393 Binop_22b<HOr>(instruction, false, dex_pc);
2394 break;
2395 }
2396
2397 case Instruction::XOR_INT_LIT8: {
2398 Binop_22b<HXor>(instruction, false, dex_pc);
2399 break;
2400 }
2401
2402 case Instruction::RSUB_INT_LIT8: {
2403 Binop_22b<HSub>(instruction, true, dex_pc);
2404 break;
2405 }
2406
2407 case Instruction::MUL_INT_LIT8: {
2408 Binop_22b<HMul>(instruction, false, dex_pc);
2409 break;
2410 }
2411
2412 case Instruction::DIV_INT_LIT16:
2413 case Instruction::DIV_INT_LIT8: {
2414 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2415 dex_pc, Primitive::kPrimInt, true, true);
2416 break;
2417 }
2418
2419 case Instruction::REM_INT_LIT16:
2420 case Instruction::REM_INT_LIT8: {
2421 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2422 dex_pc, Primitive::kPrimInt, true, false);
2423 break;
2424 }
2425
2426 case Instruction::SHL_INT_LIT8: {
2427 Binop_22b<HShl>(instruction, false, dex_pc);
2428 break;
2429 }
2430
2431 case Instruction::SHR_INT_LIT8: {
2432 Binop_22b<HShr>(instruction, false, dex_pc);
2433 break;
2434 }
2435
2436 case Instruction::USHR_INT_LIT8: {
2437 Binop_22b<HUShr>(instruction, false, dex_pc);
2438 break;
2439 }
2440
2441 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002442 if (!BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +00002443 return false;
2444 }
2445 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2446 break;
2447 }
2448
2449 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002450 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002451 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
2452 bool finalizable;
2453 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
2454 ? kQuickAllocArrayWithAccessCheck
2455 : kQuickAllocArray;
2456 AppendInstruction(new (arena_) HNewArray(length,
2457 graph_->GetCurrentMethod(),
2458 dex_pc,
2459 type_index,
2460 *dex_compilation_unit_->GetDexFile(),
2461 entrypoint));
2462 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2463 break;
2464 }
2465
2466 case Instruction::FILLED_NEW_ARRAY: {
2467 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002468 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002469 uint32_t args[5];
2470 instruction.GetVarArgs(args);
2471 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2472 break;
2473 }
2474
2475 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2476 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002477 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002478 uint32_t register_index = instruction.VRegC_3rc();
2479 BuildFilledNewArray(
2480 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2481 break;
2482 }
2483
2484 case Instruction::FILL_ARRAY_DATA: {
2485 BuildFillArrayData(instruction, dex_pc);
2486 break;
2487 }
2488
2489 case Instruction::MOVE_RESULT:
2490 case Instruction::MOVE_RESULT_WIDE:
2491 case Instruction::MOVE_RESULT_OBJECT: {
2492 DCHECK(latest_result_ != nullptr);
2493 UpdateLocal(instruction.VRegA(), latest_result_);
2494 latest_result_ = nullptr;
2495 break;
2496 }
2497
2498 case Instruction::CMP_LONG: {
2499 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2500 break;
2501 }
2502
2503 case Instruction::CMPG_FLOAT: {
2504 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2505 break;
2506 }
2507
2508 case Instruction::CMPG_DOUBLE: {
2509 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2510 break;
2511 }
2512
2513 case Instruction::CMPL_FLOAT: {
2514 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2515 break;
2516 }
2517
2518 case Instruction::CMPL_DOUBLE: {
2519 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2520 break;
2521 }
2522
2523 case Instruction::NOP:
2524 break;
2525
2526 case Instruction::IGET:
2527 case Instruction::IGET_QUICK:
2528 case Instruction::IGET_WIDE:
2529 case Instruction::IGET_WIDE_QUICK:
2530 case Instruction::IGET_OBJECT:
2531 case Instruction::IGET_OBJECT_QUICK:
2532 case Instruction::IGET_BOOLEAN:
2533 case Instruction::IGET_BOOLEAN_QUICK:
2534 case Instruction::IGET_BYTE:
2535 case Instruction::IGET_BYTE_QUICK:
2536 case Instruction::IGET_CHAR:
2537 case Instruction::IGET_CHAR_QUICK:
2538 case Instruction::IGET_SHORT:
2539 case Instruction::IGET_SHORT_QUICK: {
2540 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2541 return false;
2542 }
2543 break;
2544 }
2545
2546 case Instruction::IPUT:
2547 case Instruction::IPUT_QUICK:
2548 case Instruction::IPUT_WIDE:
2549 case Instruction::IPUT_WIDE_QUICK:
2550 case Instruction::IPUT_OBJECT:
2551 case Instruction::IPUT_OBJECT_QUICK:
2552 case Instruction::IPUT_BOOLEAN:
2553 case Instruction::IPUT_BOOLEAN_QUICK:
2554 case Instruction::IPUT_BYTE:
2555 case Instruction::IPUT_BYTE_QUICK:
2556 case Instruction::IPUT_CHAR:
2557 case Instruction::IPUT_CHAR_QUICK:
2558 case Instruction::IPUT_SHORT:
2559 case Instruction::IPUT_SHORT_QUICK: {
2560 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2561 return false;
2562 }
2563 break;
2564 }
2565
2566 case Instruction::SGET:
2567 case Instruction::SGET_WIDE:
2568 case Instruction::SGET_OBJECT:
2569 case Instruction::SGET_BOOLEAN:
2570 case Instruction::SGET_BYTE:
2571 case Instruction::SGET_CHAR:
2572 case Instruction::SGET_SHORT: {
2573 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2574 return false;
2575 }
2576 break;
2577 }
2578
2579 case Instruction::SPUT:
2580 case Instruction::SPUT_WIDE:
2581 case Instruction::SPUT_OBJECT:
2582 case Instruction::SPUT_BOOLEAN:
2583 case Instruction::SPUT_BYTE:
2584 case Instruction::SPUT_CHAR:
2585 case Instruction::SPUT_SHORT: {
2586 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2587 return false;
2588 }
2589 break;
2590 }
2591
2592#define ARRAY_XX(kind, anticipated_type) \
2593 case Instruction::AGET##kind: { \
2594 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2595 break; \
2596 } \
2597 case Instruction::APUT##kind: { \
2598 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2599 break; \
2600 }
2601
2602 ARRAY_XX(, Primitive::kPrimInt);
2603 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2604 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2605 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2606 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2607 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2608 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2609
2610 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002611 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002612 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2613 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2614 break;
2615 }
2616
2617 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002618 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002619 AppendInstruction(
2620 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2621 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2622 break;
2623 }
2624
2625 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002626 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002627 AppendInstruction(
2628 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2629 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2630 break;
2631 }
2632
2633 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002634 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002635 // `CanAccessTypeWithoutChecks` will tell whether the method being
2636 // built is trying to access its own class, so that the generated
2637 // code can optimize for this case. However, the optimization does not
2638 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002639 ScopedObjectAccess soa(Thread::Current());
2640 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00002641 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002642 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index);
David Brazdildee58d62016-04-07 09:54:26 +00002643 AppendInstruction(new (arena_) HLoadClass(
2644 graph_->GetCurrentMethod(),
2645 type_index,
2646 *dex_file_,
2647 IsOutermostCompilingClass(type_index),
2648 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00002649 !can_access));
David Brazdildee58d62016-04-07 09:54:26 +00002650 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2651 break;
2652 }
2653
2654 case Instruction::MOVE_EXCEPTION: {
2655 AppendInstruction(new (arena_) HLoadException(dex_pc));
2656 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2657 AppendInstruction(new (arena_) HClearException(dex_pc));
2658 break;
2659 }
2660
2661 case Instruction::THROW: {
2662 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2663 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2664 // We finished building this block. Set the current block to null to avoid
2665 // adding dead instructions to it.
2666 current_block_ = nullptr;
2667 break;
2668 }
2669
2670 case Instruction::INSTANCE_OF: {
2671 uint8_t destination = instruction.VRegA_22c();
2672 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002673 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002674 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2675 break;
2676 }
2677
2678 case Instruction::CHECK_CAST: {
2679 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002680 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002681 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2682 break;
2683 }
2684
2685 case Instruction::MONITOR_ENTER: {
2686 AppendInstruction(new (arena_) HMonitorOperation(
2687 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2688 HMonitorOperation::OperationKind::kEnter,
2689 dex_pc));
2690 break;
2691 }
2692
2693 case Instruction::MONITOR_EXIT: {
2694 AppendInstruction(new (arena_) HMonitorOperation(
2695 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2696 HMonitorOperation::OperationKind::kExit,
2697 dex_pc));
2698 break;
2699 }
2700
2701 case Instruction::SPARSE_SWITCH:
2702 case Instruction::PACKED_SWITCH: {
2703 BuildSwitch(instruction, dex_pc);
2704 break;
2705 }
2706
2707 default:
2708 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002709 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002710 << " because of unhandled instruction "
2711 << instruction.Name();
2712 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2713 return false;
2714 }
2715 return true;
2716} // NOLINT(readability/fn_size)
2717
2718} // namespace art