blob: cac385ce3c332178d5d9277582117d7fd4a17ba2 [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(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000210 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000211 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000212 instruction);
213 environment->CopyFrom(*current_locals_);
214 instruction->SetRawEnvironment(environment);
215 }
216}
217
David Brazdilc120bbe2016-04-22 16:57:00 +0100218HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
219 HInstruction* ref = LoadLocal(register_index, Primitive::kPrimNot);
220 if (!ref->CanBeNull()) {
221 return ref;
222 }
223
224 HNullCheck* null_check = new (arena_) HNullCheck(ref, dex_pc);
225 AppendInstruction(null_check);
226 return null_check;
227}
228
David Brazdildee58d62016-04-07 09:54:26 +0000229void HInstructionBuilder::SetLoopHeaderPhiInputs() {
230 for (size_t i = loop_headers_.size(); i > 0; --i) {
231 HBasicBlock* block = loop_headers_[i - 1];
232 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
233 HPhi* phi = it.Current()->AsPhi();
234 size_t vreg = phi->GetRegNumber();
235 for (HBasicBlock* predecessor : block->GetPredecessors()) {
236 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
237 if (value == nullptr) {
238 // Vreg is undefined at this predecessor. Mark it dead and leave with
239 // fewer inputs than predecessors. SsaChecker will fail if not removed.
240 phi->SetDead();
241 break;
242 } else {
243 phi->AddInput(value);
244 }
245 }
246 }
247 }
248}
249
250static bool IsBlockPopulated(HBasicBlock* block) {
251 if (block->IsLoopHeader()) {
252 // Suspend checks were inserted into loop headers during building of dominator tree.
253 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
254 return block->GetFirstInstruction() != block->GetLastInstruction();
255 } else {
256 return !block->GetInstructions().IsEmpty();
257 }
258}
259
260bool HInstructionBuilder::Build() {
261 locals_for_.resize(graph_->GetBlocks().size(),
262 ArenaVector<HInstruction*>(arena_->Adapter(kArenaAllocGraphBuilder)));
263
264 // Find locations where we want to generate extra stackmaps for native debugging.
265 // This allows us to generate the info only at interesting points (for example,
266 // at start of java statement) rather than before every dex instruction.
267 const bool native_debuggable = compiler_driver_ != nullptr &&
268 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
269 ArenaBitVector* native_debug_info_locations = nullptr;
270 if (native_debuggable) {
271 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
272 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
273 FindNativeDebugInfoLocations(native_debug_info_locations);
274 }
275
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100276 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
277 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000278 uint32_t block_dex_pc = current_block_->GetDexPc();
279
280 InitializeBlockLocals();
281
282 if (current_block_->IsEntryBlock()) {
283 InitializeParameters();
284 AppendInstruction(new (arena_) HSuspendCheck(0u));
285 AppendInstruction(new (arena_) HGoto(0u));
286 continue;
287 } else if (current_block_->IsExitBlock()) {
288 AppendInstruction(new (arena_) HExit());
289 continue;
290 } else if (current_block_->IsLoopHeader()) {
291 HSuspendCheck* suspend_check = new (arena_) HSuspendCheck(current_block_->GetDexPc());
292 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
293 // This is slightly odd because the loop header might not be empty (TryBoundary).
294 // But we're still creating the environment with locals from the top of the block.
295 InsertInstructionAtTop(suspend_check);
296 }
297
298 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
299 // Synthetic block that does not need to be populated.
300 DCHECK(IsBlockPopulated(current_block_));
301 continue;
302 }
303
304 DCHECK(!IsBlockPopulated(current_block_));
305
306 for (CodeItemIterator it(code_item_, block_dex_pc); !it.Done(); it.Advance()) {
307 if (current_block_ == nullptr) {
308 // The previous instruction ended this block.
309 break;
310 }
311
312 uint32_t dex_pc = it.CurrentDexPc();
313 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
314 // This dex_pc starts a new basic block.
315 break;
316 }
317
318 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(it.CurrentInstruction())) {
319 PropagateLocalsToCatchBlocks();
320 }
321
322 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
323 AppendInstruction(new (arena_) HNativeDebugInfo(dex_pc));
324 }
325
326 if (!ProcessDexInstruction(it.CurrentInstruction(), dex_pc)) {
327 return false;
328 }
329 }
330
331 if (current_block_ != nullptr) {
332 // Branching instructions clear current_block, so we know the last
333 // instruction of the current block is not a branching instruction.
334 // We add an unconditional Goto to the next block.
335 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
336 AppendInstruction(new (arena_) HGoto());
337 }
338 }
339
340 SetLoopHeaderPhiInputs();
341
342 return true;
343}
344
345void HInstructionBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
346 // The callback gets called when the line number changes.
347 // In other words, it marks the start of new java statement.
348 struct Callback {
349 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
350 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
351 return false;
352 }
353 };
354 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
355 // Instruction-specific tweaks.
356 const Instruction* const begin = Instruction::At(code_item_.insns_);
357 const Instruction* const end = begin->RelativeAt(code_item_.insns_size_in_code_units_);
358 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
359 switch (inst->Opcode()) {
360 case Instruction::MOVE_EXCEPTION: {
361 // Stop in native debugger after the exception has been moved.
362 // The compiler also expects the move at the start of basic block so
363 // we do not want to interfere by inserting native-debug-info before it.
364 locations->ClearBit(inst->GetDexPc(code_item_.insns_));
365 const Instruction* next = inst->Next();
366 if (next < end) {
367 locations->SetBit(next->GetDexPc(code_item_.insns_));
368 }
369 break;
370 }
371 default:
372 break;
373 }
374 }
375}
376
377HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, Primitive::Type type) const {
378 HInstruction* value = (*current_locals_)[reg_number];
379 DCHECK(value != nullptr);
380
381 // If the operation requests a specific type, we make sure its input is of that type.
382 if (type != value->GetType()) {
383 if (Primitive::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700384 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
David Brazdildee58d62016-04-07 09:54:26 +0000385 } else if (type == Primitive::kPrimNot) {
Aart Bik31883642016-06-06 15:02:44 -0700386 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000387 }
Aart Bik31883642016-06-06 15:02:44 -0700388 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000389 }
390
391 return value;
392}
393
394void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
395 Primitive::Type stored_type = stored_value->GetType();
396 DCHECK_NE(stored_type, Primitive::kPrimVoid);
397
398 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
399 // registers. Consider the following cases:
400 // (1) Storing a wide value must overwrite previous values in both `reg_number`
401 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
402 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
403 // must invalidate it. We store `nullptr` in `reg_number-1`.
404 // Consequently, storing a wide value into the high vreg of another wide value
405 // will invalidate both `reg_number-1` and `reg_number+1`.
406
407 if (reg_number != 0) {
408 HInstruction* local_low = (*current_locals_)[reg_number - 1];
409 if (local_low != nullptr && Primitive::Is64BitType(local_low->GetType())) {
410 // The vreg we are storing into was previously the high vreg of a pair.
411 // We need to invalidate its low vreg.
412 DCHECK((*current_locals_)[reg_number] == nullptr);
413 (*current_locals_)[reg_number - 1] = nullptr;
414 }
415 }
416
417 (*current_locals_)[reg_number] = stored_value;
418 if (Primitive::Is64BitType(stored_type)) {
419 // We are storing a pair. Invalidate the instruction in the high vreg.
420 (*current_locals_)[reg_number + 1] = nullptr;
421 }
422}
423
424void HInstructionBuilder::InitializeParameters() {
425 DCHECK(current_block_->IsEntryBlock());
426
427 // dex_compilation_unit_ is null only when unit testing.
428 if (dex_compilation_unit_ == nullptr) {
429 return;
430 }
431
432 const char* shorty = dex_compilation_unit_->GetShorty();
433 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
434 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
435 uint16_t parameter_index = 0;
436
437 const DexFile::MethodId& referrer_method_id =
438 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
439 if (!dex_compilation_unit_->IsStatic()) {
440 // Add the implicit 'this' argument, not expressed in the signature.
441 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
442 referrer_method_id.class_idx_,
443 parameter_index++,
444 Primitive::kPrimNot,
445 true);
446 AppendInstruction(parameter);
447 UpdateLocal(locals_index++, parameter);
448 number_of_parameters--;
449 }
450
451 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
452 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
453 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
454 HParameterValue* parameter = new (arena_) HParameterValue(
455 *dex_file_,
456 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
457 parameter_index++,
458 Primitive::GetType(shorty[shorty_pos]),
459 false);
460 ++shorty_pos;
461 AppendInstruction(parameter);
462 // Store the parameter value in the local that the dex code will use
463 // to reference that parameter.
464 UpdateLocal(locals_index++, parameter);
465 if (Primitive::Is64BitType(parameter->GetType())) {
466 i++;
467 locals_index++;
468 parameter_index++;
469 }
470 }
471}
472
473template<typename T>
474void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
475 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
476 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
477 T* comparison = new (arena_) T(first, second, dex_pc);
478 AppendInstruction(comparison);
479 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
480 current_block_ = nullptr;
481}
482
483template<typename T>
484void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
485 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
486 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
487 AppendInstruction(comparison);
488 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
489 current_block_ = nullptr;
490}
491
492template<typename T>
493void HInstructionBuilder::Unop_12x(const Instruction& instruction,
494 Primitive::Type type,
495 uint32_t dex_pc) {
496 HInstruction* first = LoadLocal(instruction.VRegB(), type);
497 AppendInstruction(new (arena_) T(type, first, dex_pc));
498 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
499}
500
501void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
502 Primitive::Type input_type,
503 Primitive::Type result_type,
504 uint32_t dex_pc) {
505 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
506 AppendInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
507 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
508}
509
510template<typename T>
511void HInstructionBuilder::Binop_23x(const Instruction& instruction,
512 Primitive::Type type,
513 uint32_t dex_pc) {
514 HInstruction* first = LoadLocal(instruction.VRegB(), type);
515 HInstruction* second = LoadLocal(instruction.VRegC(), type);
516 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
517 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
518}
519
520template<typename T>
521void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
522 Primitive::Type type,
523 uint32_t dex_pc) {
524 HInstruction* first = LoadLocal(instruction.VRegB(), type);
525 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
526 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
527 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
528}
529
530void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
531 Primitive::Type type,
532 ComparisonBias bias,
533 uint32_t dex_pc) {
534 HInstruction* first = LoadLocal(instruction.VRegB(), type);
535 HInstruction* second = LoadLocal(instruction.VRegC(), type);
536 AppendInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
537 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
538}
539
540template<typename T>
541void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
542 Primitive::Type type,
543 uint32_t dex_pc) {
544 HInstruction* first = LoadLocal(instruction.VRegA(), type);
545 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
546 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
547 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
548}
549
550template<typename T>
551void HInstructionBuilder::Binop_12x(const Instruction& instruction,
552 Primitive::Type type,
553 uint32_t dex_pc) {
554 HInstruction* first = LoadLocal(instruction.VRegA(), type);
555 HInstruction* second = LoadLocal(instruction.VRegB(), type);
556 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
557 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
558}
559
560template<typename T>
561void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
562 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
563 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
564 if (reverse) {
565 std::swap(first, second);
566 }
567 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
568 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
569}
570
571template<typename T>
572void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
573 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
574 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
575 if (reverse) {
576 std::swap(first, second);
577 }
578 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
579 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
580}
581
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700582static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
David Brazdildee58d62016-04-07 09:54:26 +0000583 Thread* self = Thread::Current();
584 return cu->IsConstructor()
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700585 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000586}
587
588// Returns true if `block` has only one successor which starts at the next
589// dex_pc after `instruction` at `dex_pc`.
590static bool IsFallthroughInstruction(const Instruction& instruction,
591 uint32_t dex_pc,
592 HBasicBlock* block) {
593 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
594 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
595}
596
597void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
598 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
599 DexSwitchTable table(instruction, dex_pc);
600
601 if (table.GetNumEntries() == 0) {
602 // Empty Switch. Code falls through to the next block.
603 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
604 AppendInstruction(new (arena_) HGoto(dex_pc));
605 } else if (table.ShouldBuildDecisionTree()) {
606 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
607 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
608 HEqual* comparison = new (arena_) HEqual(value, case_value, dex_pc);
609 AppendInstruction(comparison);
610 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
611
612 if (!it.IsLast()) {
613 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
614 }
615 }
616 } else {
617 AppendInstruction(
618 new (arena_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
619 }
620
621 current_block_ = nullptr;
622}
623
624void HInstructionBuilder::BuildReturn(const Instruction& instruction,
625 Primitive::Type type,
626 uint32_t dex_pc) {
627 if (type == Primitive::kPrimVoid) {
628 if (graph_->ShouldGenerateConstructorBarrier()) {
629 // The compilation unit is null during testing.
630 if (dex_compilation_unit_ != nullptr) {
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700631 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_))
David Brazdildee58d62016-04-07 09:54:26 +0000632 << "Inconsistent use of ShouldGenerateConstructorBarrier. Should not generate a barrier.";
633 }
634 AppendInstruction(new (arena_) HMemoryBarrier(kStoreStore, dex_pc));
635 }
636 AppendInstruction(new (arena_) HReturnVoid(dex_pc));
637 } else {
638 HInstruction* value = LoadLocal(instruction.VRegA(), type);
639 AppendInstruction(new (arena_) HReturn(value, dex_pc));
640 }
641 current_block_ = nullptr;
642}
643
644static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
645 switch (opcode) {
646 case Instruction::INVOKE_STATIC:
647 case Instruction::INVOKE_STATIC_RANGE:
648 return kStatic;
649 case Instruction::INVOKE_DIRECT:
650 case Instruction::INVOKE_DIRECT_RANGE:
651 return kDirect;
652 case Instruction::INVOKE_VIRTUAL:
653 case Instruction::INVOKE_VIRTUAL_QUICK:
654 case Instruction::INVOKE_VIRTUAL_RANGE:
655 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
656 return kVirtual;
657 case Instruction::INVOKE_INTERFACE:
658 case Instruction::INVOKE_INTERFACE_RANGE:
659 return kInterface;
660 case Instruction::INVOKE_SUPER_RANGE:
661 case Instruction::INVOKE_SUPER:
662 return kSuper;
663 default:
664 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
665 UNREACHABLE();
666 }
667}
668
669ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
670 ScopedObjectAccess soa(Thread::Current());
Vladimir Markod16363a2017-02-01 14:09:13 +0000671 StackHandleScope<3> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +0000672
673 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Markod16363a2017-02-01 14:09:13 +0000674 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
675 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +0000676 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100677 // We fetch the referenced class eagerly (that is, the class pointed by in the MethodId
678 // at method_idx), as `CanAccessResolvedMethod` expects it be be in the dex cache.
679 Handle<mirror::Class> methods_class(hs.NewHandle(class_linker->ResolveReferencedClassOfMethod(
680 method_idx, dex_compilation_unit_->GetDexCache(), class_loader)));
681
682 if (UNLIKELY(methods_class.Get() == nullptr)) {
683 // Clean up any exception left by type resolution.
684 soa.Self()->ClearException();
685 return nullptr;
686 }
David Brazdildee58d62016-04-07 09:54:26 +0000687
688 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
689 *dex_compilation_unit_->GetDexFile(),
690 method_idx,
691 dex_compilation_unit_->GetDexCache(),
692 class_loader,
693 /* referrer */ nullptr,
694 invoke_type);
695
696 if (UNLIKELY(resolved_method == nullptr)) {
697 // Clean up any exception left by type resolution.
698 soa.Self()->ClearException();
699 return nullptr;
700 }
701
702 // Check access. The class linker has a fast path for looking into the dex cache
703 // and does not check the access if it hits it.
704 if (compiling_class.Get() == nullptr) {
705 if (!resolved_method->IsPublic()) {
706 return nullptr;
707 }
708 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
709 resolved_method,
710 dex_compilation_unit_->GetDexCache().Get(),
711 method_idx)) {
712 return nullptr;
713 }
714
715 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
716 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
717 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
718 // which require runtime handling.
719 if (invoke_type == kSuper) {
720 if (compiling_class.Get() == nullptr) {
721 // We could not determine the method's class we need to wait until runtime.
722 DCHECK(Runtime::Current()->IsAotCompiler());
723 return nullptr;
724 }
Aart Bikf663e342016-04-04 17:28:59 -0700725 if (!methods_class->IsAssignableFrom(compiling_class.Get())) {
726 // We cannot statically determine the target method. The runtime will throw a
727 // NoSuchMethodError on this one.
728 return nullptr;
729 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100730 ArtMethod* actual_method;
731 if (methods_class->IsInterface()) {
732 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
733 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000734 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100735 uint16_t vtable_index = resolved_method->GetMethodIndex();
736 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
737 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000738 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100739 if (actual_method != resolved_method &&
740 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
741 // The back-end code generator relies on this check in order to ensure that it will not
742 // attempt to read the dex_cache with a dex_method_index that is not from the correct
743 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
744 // builder, which means that the code-generator (and compiler driver during sharpening and
745 // inliner, maybe) might invoke an incorrect method.
746 // TODO: The actual method could still be referenced in the current dex file, so we
747 // could try locating it.
748 // TODO: Remove the dex_file restriction.
749 return nullptr;
750 }
751 if (!actual_method->IsInvokable()) {
752 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
753 // could resolve the callee to the wrong method.
754 return nullptr;
755 }
756 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000757 }
758
759 // Check for incompatible class changes. The class linker has a fast path for
760 // looking into the dex cache and does not check incompatible class changes if it hits it.
761 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
762 return nullptr;
763 }
764
765 return resolved_method;
766}
767
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100768static bool IsStringConstructor(ArtMethod* method) {
769 ScopedObjectAccess soa(Thread::Current());
770 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
771}
772
David Brazdildee58d62016-04-07 09:54:26 +0000773bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
774 uint32_t dex_pc,
775 uint32_t method_idx,
776 uint32_t number_of_vreg_arguments,
777 bool is_range,
778 uint32_t* args,
779 uint32_t register_index) {
780 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
781 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
782 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
783
784 // Remove the return type from the 'proto'.
785 size_t number_of_arguments = strlen(descriptor) - 1;
786 if (invoke_type != kStatic) { // instance call
787 // One extra argument for 'this'.
788 number_of_arguments++;
789 }
790
David Brazdildee58d62016-04-07 09:54:26 +0000791 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
792
793 if (UNLIKELY(resolved_method == nullptr)) {
794 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
795 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
796 number_of_arguments,
797 return_type,
798 dex_pc,
799 method_idx,
800 invoke_type);
801 return HandleInvoke(invoke,
802 number_of_vreg_arguments,
803 args,
804 register_index,
805 is_range,
806 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700807 nullptr, /* clinit_check */
808 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000809 }
810
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100811 // Replace calls to String.<init> with StringFactory.
812 if (IsStringConstructor(resolved_method)) {
813 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
814 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
815 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
816 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000817 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100818 };
819 MethodReference target_method(dex_file_, method_idx);
820 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
821 arena_,
822 number_of_arguments - 1,
823 Primitive::kPrimNot /*return_type */,
824 dex_pc,
825 method_idx,
826 nullptr,
827 dispatch_info,
828 invoke_type,
829 target_method,
830 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
831 return HandleStringInit(invoke,
832 number_of_vreg_arguments,
833 args,
834 register_index,
835 is_range,
836 descriptor);
837 }
838
David Brazdildee58d62016-04-07 09:54:26 +0000839 // Potential class initialization check, in the case of a static method call.
840 HClinitCheck* clinit_check = nullptr;
841 HInvoke* invoke = nullptr;
842 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
843 // By default, consider that the called method implicitly requires
844 // an initialization check of its declaring method.
845 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
846 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
847 ScopedObjectAccess soa(Thread::Current());
848 if (invoke_type == kStatic) {
849 clinit_check = ProcessClinitCheckForInvoke(
850 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
851 } else if (invoke_type == kSuper) {
852 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100853 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000854 // we resolved to the method referenced by the instruction.
855 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000856 }
857 }
858
859 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
860 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
861 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000862 0u
David Brazdildee58d62016-04-07 09:54:26 +0000863 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100864 MethodReference target_method(resolved_method->GetDexFile(),
865 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000866 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
867 number_of_arguments,
868 return_type,
869 dex_pc,
870 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100871 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000872 dispatch_info,
873 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100874 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000875 clinit_check_requirement);
876 } else if (invoke_type == kVirtual) {
877 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
878 invoke = new (arena_) HInvokeVirtual(arena_,
879 number_of_arguments,
880 return_type,
881 dex_pc,
882 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100883 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000884 resolved_method->GetMethodIndex());
885 } else {
886 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100887 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000888 invoke = new (arena_) HInvokeInterface(arena_,
889 number_of_arguments,
890 return_type,
891 dex_pc,
892 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100893 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700894 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000895 }
896
897 return HandleInvoke(invoke,
898 number_of_vreg_arguments,
899 args,
900 register_index,
901 is_range,
902 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700903 clinit_check,
904 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000905}
906
Orion Hodsonac141392017-01-13 11:53:47 +0000907bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
908 uint32_t dex_pc,
909 uint32_t method_idx,
910 uint32_t proto_idx,
911 uint32_t number_of_vreg_arguments,
912 bool is_range,
913 uint32_t* args,
914 uint32_t register_index) {
915 const char* descriptor = dex_file_->GetShorty(proto_idx);
916 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
917 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
918 size_t number_of_arguments = strlen(descriptor);
919 HInvoke* invoke = new (arena_) HInvokePolymorphic(arena_,
920 number_of_arguments,
921 return_type,
922 dex_pc,
923 method_idx);
924 return HandleInvoke(invoke,
925 number_of_vreg_arguments,
926 args,
927 register_index,
928 is_range,
929 descriptor,
930 nullptr /* clinit_check */,
931 false /* is_unresolved */);
932}
933
Andreas Gampea5b09a62016-11-17 15:21:22 -0800934bool HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100935 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100936 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100937 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
938
David Brazdildee58d62016-04-07 09:54:26 +0000939 if (outer_dex_cache.Get() != dex_cache.Get()) {
940 // We currently do not support inlining allocations across dex files.
941 return false;
942 }
943
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000944 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000945
David Brazdildee58d62016-04-07 09:54:26 +0000946 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000947 Handle<mirror::Class> klass = load_class->GetClass();
948
949 if (!IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +0000950 cls = new (arena_) HClinitCheck(load_class, dex_pc);
951 AppendInstruction(cls);
952 }
953
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000954 // Only the access check entrypoint handles the finalizable class case. If we
955 // need access checks, then we haven't resolved the method and the class may
956 // again be finalizable.
957 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
958 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
959 entrypoint = kQuickAllocObjectWithChecks;
960 }
961
962 // Consider classes we haven't resolved as potentially finalizable.
963 bool finalizable = (klass.Get() == nullptr) || klass->IsFinalizable();
964
David Brazdildee58d62016-04-07 09:54:26 +0000965 AppendInstruction(new (arena_) HNewInstance(
966 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000967 dex_pc,
968 type_index,
969 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +0000970 finalizable,
971 entrypoint));
972 return true;
973}
974
975static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700976 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000977 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
978}
979
980bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
981 if (cls.Get() == nullptr) {
982 return false;
983 }
984
985 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
986 // check whether the class is in an image for the AOT compilation.
987 if (cls->IsInitialized() &&
988 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
989 return true;
990 }
991
992 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
993 return true;
994 }
995
996 // TODO: We should walk over the inlined methods, but we don't pass
997 // that information to the builder.
998 if (IsSubClass(GetCompilingClass(), cls.Get())) {
999 return true;
1000 }
1001
1002 return false;
1003}
1004
1005HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1006 uint32_t dex_pc,
1007 ArtMethod* resolved_method,
1008 uint32_t method_idx,
1009 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
David Brazdildee58d62016-04-07 09:54:26 +00001010 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001011 StackHandleScope<2> hs(self);
1012 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1013 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001014 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1015 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
1016
1017 // The index at which the method's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001018 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001019 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1020 if (is_outer_class) {
1021 storage_index = outer_class->GetDexTypeIndex();
1022 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1023 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1024 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1025 GetCompilingClass(),
1026 resolved_method,
1027 method_idx,
1028 &storage_index);
1029 }
1030
1031 HClinitCheck* clinit_check = nullptr;
1032
1033 if (IsInitialized(resolved_method_class)) {
1034 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001035 } else if (storage_index.IsValid()) {
David Brazdildee58d62016-04-07 09:54:26 +00001036 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001037 HLoadClass* cls = BuildLoadClass(
1038 storage_index, dex_pc, /* check_access */ false, /* outer */ true);
1039 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001040 AppendInstruction(clinit_check);
1041 }
1042 return clinit_check;
1043}
1044
1045bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1046 uint32_t number_of_vreg_arguments,
1047 uint32_t* args,
1048 uint32_t register_index,
1049 bool is_range,
1050 const char* descriptor,
1051 size_t start_index,
1052 size_t* argument_index) {
1053 uint32_t descriptor_index = 1; // Skip the return type.
1054
1055 for (size_t i = start_index;
1056 // Make sure we don't go over the expected arguments or over the number of
1057 // dex registers given. If the instruction was seen as dead by the verifier,
1058 // it hasn't been properly checked.
1059 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1060 i++, (*argument_index)++) {
1061 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1062 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1063 if (!is_range
1064 && is_wide
1065 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1066 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1067 // reject any class where this is violated. However, the verifier only does these checks
1068 // on non trivially dead instructions, so we just bailout the compilation.
1069 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001070 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001071 << " because of non-sequential dex register pair in wide argument";
1072 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1073 return false;
1074 }
1075 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1076 invoke->SetArgumentAt(*argument_index, arg);
1077 if (is_wide) {
1078 i++;
1079 }
1080 }
1081
1082 if (*argument_index != invoke->GetNumberOfArguments()) {
1083 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001084 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001085 << " because of wrong number of arguments in invoke instruction";
1086 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1087 return false;
1088 }
1089
1090 if (invoke->IsInvokeStaticOrDirect() &&
1091 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1092 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1093 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1094 (*argument_index)++;
1095 }
1096
1097 return true;
1098}
1099
1100bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1101 uint32_t number_of_vreg_arguments,
1102 uint32_t* args,
1103 uint32_t register_index,
1104 bool is_range,
1105 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001106 HClinitCheck* clinit_check,
1107 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001108 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1109
1110 size_t start_index = 0;
1111 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001112 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001113 uint32_t obj_reg = is_range ? register_index : args[0];
1114 HInstruction* arg = is_unresolved
1115 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1116 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001117 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001118 start_index = 1;
1119 argument_index = 1;
1120 }
1121
1122 if (!SetupInvokeArguments(invoke,
1123 number_of_vreg_arguments,
1124 args,
1125 register_index,
1126 is_range,
1127 descriptor,
1128 start_index,
1129 &argument_index)) {
1130 return false;
1131 }
1132
1133 if (clinit_check != nullptr) {
1134 // Add the class initialization check as last input of `invoke`.
1135 DCHECK(invoke->IsInvokeStaticOrDirect());
1136 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1137 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1138 invoke->SetArgumentAt(argument_index, clinit_check);
1139 argument_index++;
1140 }
1141
1142 AppendInstruction(invoke);
1143 latest_result_ = invoke;
1144
1145 return true;
1146}
1147
1148bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1149 uint32_t number_of_vreg_arguments,
1150 uint32_t* args,
1151 uint32_t register_index,
1152 bool is_range,
1153 const char* descriptor) {
1154 DCHECK(invoke->IsInvokeStaticOrDirect());
1155 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1156
1157 size_t start_index = 1;
1158 size_t argument_index = 0;
1159 if (!SetupInvokeArguments(invoke,
1160 number_of_vreg_arguments,
1161 args,
1162 register_index,
1163 is_range,
1164 descriptor,
1165 start_index,
1166 &argument_index)) {
1167 return false;
1168 }
1169
1170 AppendInstruction(invoke);
1171
1172 // This is a StringFactory call, not an actual String constructor. Its result
1173 // replaces the empty String pre-allocated by NewInstance.
1174 uint32_t orig_this_reg = is_range ? register_index : args[0];
1175 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1176
1177 // Replacing the NewInstance might render it redundant. Keep a list of these
1178 // to be visited once it is clear whether it is has remaining uses.
1179 if (arg_this->IsNewInstance()) {
1180 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1181 } else {
1182 DCHECK(arg_this->IsPhi());
1183 // NewInstance is not the direct input of the StringFactory call. It might
1184 // be redundant but optimizing this case is not worth the effort.
1185 }
1186
1187 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1188 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1189 if ((*current_locals_)[vreg] == arg_this) {
1190 (*current_locals_)[vreg] = invoke;
1191 }
1192 }
1193
1194 return true;
1195}
1196
1197static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1198 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1199 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1200 return Primitive::GetType(type[0]);
1201}
1202
1203bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1204 uint32_t dex_pc,
1205 bool is_put) {
1206 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1207 uint32_t obj_reg = instruction.VRegB_22c();
1208 uint16_t field_index;
1209 if (instruction.IsQuickened()) {
1210 if (!CanDecodeQuickenedInfo()) {
1211 return false;
1212 }
1213 field_index = LookupQuickenedInfo(dex_pc);
1214 } else {
1215 field_index = instruction.VRegC_22c();
1216 }
1217
1218 ScopedObjectAccess soa(Thread::Current());
1219 ArtField* resolved_field =
1220 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1221
1222
Aart Bik14154132016-06-02 17:53:58 -07001223 // Generate an explicit null check on the reference, unless the field access
1224 // is unresolved. In that case, we rely on the runtime to perform various
1225 // checks first, followed by a null check.
1226 HInstruction* object = (resolved_field == nullptr)
1227 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1228 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001229
1230 Primitive::Type field_type = (resolved_field == nullptr)
1231 ? GetFieldAccessType(*dex_file_, field_index)
1232 : resolved_field->GetTypeAsPrimitiveType();
1233 if (is_put) {
1234 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1235 HInstruction* field_set = nullptr;
1236 if (resolved_field == nullptr) {
1237 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001238 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001239 value,
1240 field_type,
1241 field_index,
1242 dex_pc);
1243 } else {
1244 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001245 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001246 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001247 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001248 field_type,
1249 resolved_field->GetOffset(),
1250 resolved_field->IsVolatile(),
1251 field_index,
1252 class_def_index,
1253 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001254 dex_pc);
1255 }
1256 AppendInstruction(field_set);
1257 } else {
1258 HInstruction* field_get = nullptr;
1259 if (resolved_field == nullptr) {
1260 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001261 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001262 field_type,
1263 field_index,
1264 dex_pc);
1265 } else {
1266 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001267 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001268 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001269 field_type,
1270 resolved_field->GetOffset(),
1271 resolved_field->IsVolatile(),
1272 field_index,
1273 class_def_index,
1274 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001275 dex_pc);
1276 }
1277 AppendInstruction(field_get);
1278 UpdateLocal(source_or_dest_reg, field_get);
1279 }
1280
1281 return true;
1282}
1283
1284static mirror::Class* GetClassFrom(CompilerDriver* driver,
1285 const DexCompilationUnit& compilation_unit) {
1286 ScopedObjectAccess soa(Thread::Current());
Vladimir Markod16363a2017-02-01 14:09:13 +00001287 StackHandleScope<1> hs(soa.Self());
1288 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1289 soa.Decode<mirror::ClassLoader>(compilation_unit.GetClassLoader())));
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001290 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001291
1292 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1293}
1294
1295mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1296 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1297}
1298
1299mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1300 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1301}
1302
Andreas Gampea5b09a62016-11-17 15:21:22 -08001303bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001304 ScopedObjectAccess soa(Thread::Current());
Vladimir Markod16363a2017-02-01 14:09:13 +00001305 StackHandleScope<3> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001306 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Markod16363a2017-02-01 14:09:13 +00001307 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1308 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001309 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1310 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1311 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1312
1313 // GetOutermostCompilingClass returns null when the class is unresolved
1314 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1315 // we are compiling it.
1316 // When this happens we cannot establish a direct relation between the current
1317 // class and the outer class, so we return false.
1318 // (Note that this is only used for optimizing invokes and field accesses)
1319 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1320}
1321
1322void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001323 uint32_t dex_pc,
1324 bool is_put,
1325 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001326 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1327 uint16_t field_index = instruction.VRegB_21c();
1328
1329 if (is_put) {
1330 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1331 AppendInstruction(
1332 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1333 } else {
1334 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1335 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1336 }
1337}
1338
1339bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1340 uint32_t dex_pc,
1341 bool is_put) {
1342 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1343 uint16_t field_index = instruction.VRegB_21c();
1344
1345 ScopedObjectAccess soa(Thread::Current());
Vladimir Markod16363a2017-02-01 14:09:13 +00001346 StackHandleScope<3> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001347 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Markod16363a2017-02-01 14:09:13 +00001348 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1349 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001350 ArtField* resolved_field = compiler_driver_->ResolveField(
1351 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1352
1353 if (resolved_field == nullptr) {
1354 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1355 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1356 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1357 return true;
1358 }
1359
1360 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001361 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001362 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1363
1364 // The index at which the field's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001365 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001366 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1367 if (is_outer_class) {
1368 storage_index = outer_class->GetDexTypeIndex();
1369 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1370 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1371 return false;
1372 } else {
1373 // TODO: This is rather expensive. Perf it and cache the results if needed.
1374 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1375 outer_dex_cache.Get(),
1376 GetCompilingClass(),
1377 resolved_field,
1378 field_index,
1379 &storage_index);
1380 bool can_easily_access = is_put ? pair.second : pair.first;
1381 if (!can_easily_access) {
1382 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1383 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1384 return true;
1385 }
1386 }
1387
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001388 HLoadClass* constant = BuildLoadClass(
1389 storage_index, dex_pc, /* check_access */ false, /* outer */ true);
David Brazdildee58d62016-04-07 09:54:26 +00001390
1391 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001392 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1393 if (!IsInitialized(klass)) {
1394 cls = new (arena_) HClinitCheck(constant, dex_pc);
1395 AppendInstruction(cls);
1396 }
1397
1398 uint16_t class_def_index = klass->GetDexClassDefIndex();
1399 if (is_put) {
1400 // We need to keep the class alive before loading the value.
1401 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1402 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1403 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1404 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001405 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001406 field_type,
1407 resolved_field->GetOffset(),
1408 resolved_field->IsVolatile(),
1409 field_index,
1410 class_def_index,
1411 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001412 dex_pc));
1413 } else {
1414 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001415 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001416 field_type,
1417 resolved_field->GetOffset(),
1418 resolved_field->IsVolatile(),
1419 field_index,
1420 class_def_index,
1421 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001422 dex_pc));
1423 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1424 }
1425 return true;
1426}
1427
1428void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1429 uint16_t first_vreg,
1430 int64_t second_vreg_or_constant,
1431 uint32_t dex_pc,
1432 Primitive::Type type,
1433 bool second_is_constant,
1434 bool isDiv) {
1435 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1436
1437 HInstruction* first = LoadLocal(first_vreg, type);
1438 HInstruction* second = nullptr;
1439 if (second_is_constant) {
1440 if (type == Primitive::kPrimInt) {
1441 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1442 } else {
1443 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1444 }
1445 } else {
1446 second = LoadLocal(second_vreg_or_constant, type);
1447 }
1448
1449 if (!second_is_constant
1450 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1451 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1452 second = new (arena_) HDivZeroCheck(second, dex_pc);
1453 AppendInstruction(second);
1454 }
1455
1456 if (isDiv) {
1457 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1458 } else {
1459 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1460 }
1461 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1462}
1463
1464void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1465 uint32_t dex_pc,
1466 bool is_put,
1467 Primitive::Type anticipated_type) {
1468 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1469 uint8_t array_reg = instruction.VRegB_23x();
1470 uint8_t index_reg = instruction.VRegC_23x();
1471
David Brazdilc120bbe2016-04-22 16:57:00 +01001472 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001473 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1474 AppendInstruction(length);
1475 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1476 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1477 AppendInstruction(index);
1478 if (is_put) {
1479 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1480 // TODO: Insert a type check node if the type is Object.
1481 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1482 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1483 AppendInstruction(aset);
1484 } else {
1485 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1486 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1487 AppendInstruction(aget);
1488 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1489 }
1490 graph_->SetHasBoundsChecks(true);
1491}
1492
1493void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001494 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001495 uint32_t number_of_vreg_arguments,
1496 bool is_range,
1497 uint32_t* args,
1498 uint32_t register_index) {
1499 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001500 HLoadClass* cls = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
1501 HInstruction* object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001502 AppendInstruction(object);
1503
1504 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1505 DCHECK_EQ(descriptor[0], '[') << descriptor;
1506 char primitive = descriptor[1];
1507 DCHECK(primitive == 'I'
1508 || primitive == 'L'
1509 || primitive == '[') << descriptor;
1510 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1511 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1512
1513 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1514 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1515 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1516 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1517 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1518 AppendInstruction(aset);
1519 }
1520 latest_result_ = object;
1521}
1522
1523template <typename T>
1524void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1525 const T* data,
1526 uint32_t element_count,
1527 Primitive::Type anticipated_type,
1528 uint32_t dex_pc) {
1529 for (uint32_t i = 0; i < element_count; ++i) {
1530 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1531 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1532 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1533 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1534 AppendInstruction(aset);
1535 }
1536}
1537
1538void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001539 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001540
1541 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1542 const Instruction::ArrayDataPayload* payload =
1543 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1544 const uint8_t* data = payload->data;
1545 uint32_t element_count = payload->element_count;
1546
Vladimir Markoc69fba22016-09-06 16:49:15 +01001547 if (element_count == 0u) {
1548 // For empty payload we emit only the null check above.
1549 return;
1550 }
1551
1552 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1553 AppendInstruction(length);
1554
David Brazdildee58d62016-04-07 09:54:26 +00001555 // Implementation of this DEX instruction seems to be that the bounds check is
1556 // done before doing any stores.
1557 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1558 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1559
1560 switch (payload->element_width) {
1561 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001562 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001563 reinterpret_cast<const int8_t*>(data),
1564 element_count,
1565 Primitive::kPrimByte,
1566 dex_pc);
1567 break;
1568 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001569 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001570 reinterpret_cast<const int16_t*>(data),
1571 element_count,
1572 Primitive::kPrimShort,
1573 dex_pc);
1574 break;
1575 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001576 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001577 reinterpret_cast<const int32_t*>(data),
1578 element_count,
1579 Primitive::kPrimInt,
1580 dex_pc);
1581 break;
1582 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001583 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001584 reinterpret_cast<const int64_t*>(data),
1585 element_count,
1586 dex_pc);
1587 break;
1588 default:
1589 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1590 }
1591 graph_->SetHasBoundsChecks(true);
1592}
1593
1594void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1595 const int64_t* data,
1596 uint32_t element_count,
1597 uint32_t dex_pc) {
1598 for (uint32_t i = 0; i < element_count; ++i) {
1599 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1600 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1601 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1602 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1603 AppendInstruction(aset);
1604 }
1605}
1606
1607static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001608 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001609 if (cls.Get() == nullptr) {
1610 return TypeCheckKind::kUnresolvedCheck;
1611 } else if (cls->IsInterface()) {
1612 return TypeCheckKind::kInterfaceCheck;
1613 } else if (cls->IsArrayClass()) {
1614 if (cls->GetComponentType()->IsObjectClass()) {
1615 return TypeCheckKind::kArrayObjectCheck;
1616 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1617 return TypeCheckKind::kExactCheck;
1618 } else {
1619 return TypeCheckKind::kArrayCheck;
1620 }
1621 } else if (cls->IsFinal()) {
1622 return TypeCheckKind::kExactCheck;
1623 } else if (cls->IsAbstract()) {
1624 return TypeCheckKind::kAbstractClassCheck;
1625 } else {
1626 return TypeCheckKind::kClassHierarchyCheck;
1627 }
1628}
1629
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001630HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1631 uint32_t dex_pc,
1632 bool check_access,
1633 bool outer) {
1634 ScopedObjectAccess soa(Thread::Current());
1635 const DexCompilationUnit* compilation_unit =
1636 outer ? outer_compilation_unit_ : dex_compilation_unit_;
1637 const DexFile& dex_file = *compilation_unit->GetDexFile();
Vladimir Markod16363a2017-02-01 14:09:13 +00001638 StackHandleScope<1> hs(soa.Self());
1639 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1640 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001641 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
1642 soa, compilation_unit->GetDexCache(), class_loader, type_index, compilation_unit));
1643
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001644 bool is_accessible = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001645 if (!check_access) {
1646 is_accessible = true;
1647 } else if (klass.Get() != nullptr) {
1648 if (klass->IsPublic()) {
1649 is_accessible = true;
1650 } else {
1651 mirror::Class* compiling_class = GetCompilingClass();
1652 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
1653 is_accessible = true;
1654 }
1655 }
1656 }
1657
1658 HLoadClass* load_class = new (arena_) HLoadClass(
1659 graph_->GetCurrentMethod(),
1660 type_index,
1661 dex_file,
1662 klass,
1663 klass.Get() != nullptr && (klass.Get() == GetOutermostCompilingClass()),
1664 dex_pc,
1665 !is_accessible);
1666
1667 AppendInstruction(load_class);
1668 return load_class;
1669}
1670
David Brazdildee58d62016-04-07 09:54:26 +00001671void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1672 uint8_t destination,
1673 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001674 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001675 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001676 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001677 HLoadClass* cls = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
David Brazdildee58d62016-04-07 09:54:26 +00001678
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001679 ScopedObjectAccess soa(Thread::Current());
1680 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001681 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1682 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1683 UpdateLocal(destination, current_block_->GetLastInstruction());
1684 } else {
1685 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1686 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1687 // which may throw. If it succeeds BoundType sets the new type of `object`
1688 // for all subsequent uses.
1689 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1690 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1691 UpdateLocal(reference, current_block_->GetLastInstruction());
1692 }
1693}
1694
Vladimir Markod16363a2017-02-01 14:09:13 +00001695bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index,
1696 Handle<mirror::DexCache> dex_cache,
1697 bool* finalizable) const {
Vladimir Markoec786222016-12-20 16:24:13 +00001698 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Vladimir Markod16363a2017-02-01 14:09:13 +00001699 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index, finalizable);
1700}
1701
1702bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
1703 ScopedObjectAccess soa(Thread::Current());
1704 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1705 return NeedsAccessCheck(type_index, dex_cache, finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001706}
1707
1708bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1709 return interpreter_metadata_ != nullptr;
1710}
1711
1712uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1713 DCHECK(interpreter_metadata_ != nullptr);
1714
1715 // First check if the info has already been decoded from `interpreter_metadata_`.
1716 auto it = skipped_interpreter_metadata_.find(dex_pc);
1717 if (it != skipped_interpreter_metadata_.end()) {
1718 // Remove the entry from the map and return the parsed info.
1719 uint16_t value_in_map = it->second;
1720 skipped_interpreter_metadata_.erase(it);
1721 return value_in_map;
1722 }
1723
1724 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1725 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1726 while (true) {
1727 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1728 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1729 DCHECK_LE(dex_pc_in_map, dex_pc);
1730
1731 if (dex_pc_in_map == dex_pc) {
1732 return value_in_map;
1733 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001734 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1735 // the same dex_pc. This is OK, because the compiler does not care about those
1736 // entries.
1737 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001738 }
1739 }
1740}
1741
1742bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1743 switch (instruction.Opcode()) {
1744 case Instruction::CONST_4: {
1745 int32_t register_index = instruction.VRegA();
1746 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1747 UpdateLocal(register_index, constant);
1748 break;
1749 }
1750
1751 case Instruction::CONST_16: {
1752 int32_t register_index = instruction.VRegA();
1753 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1754 UpdateLocal(register_index, constant);
1755 break;
1756 }
1757
1758 case Instruction::CONST: {
1759 int32_t register_index = instruction.VRegA();
1760 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1761 UpdateLocal(register_index, constant);
1762 break;
1763 }
1764
1765 case Instruction::CONST_HIGH16: {
1766 int32_t register_index = instruction.VRegA();
1767 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1768 UpdateLocal(register_index, constant);
1769 break;
1770 }
1771
1772 case Instruction::CONST_WIDE_16: {
1773 int32_t register_index = instruction.VRegA();
1774 // Get 16 bits of constant value, sign extended to 64 bits.
1775 int64_t value = instruction.VRegB_21s();
1776 value <<= 48;
1777 value >>= 48;
1778 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1779 UpdateLocal(register_index, constant);
1780 break;
1781 }
1782
1783 case Instruction::CONST_WIDE_32: {
1784 int32_t register_index = instruction.VRegA();
1785 // Get 32 bits of constant value, sign extended to 64 bits.
1786 int64_t value = instruction.VRegB_31i();
1787 value <<= 32;
1788 value >>= 32;
1789 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1790 UpdateLocal(register_index, constant);
1791 break;
1792 }
1793
1794 case Instruction::CONST_WIDE: {
1795 int32_t register_index = instruction.VRegA();
1796 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1797 UpdateLocal(register_index, constant);
1798 break;
1799 }
1800
1801 case Instruction::CONST_WIDE_HIGH16: {
1802 int32_t register_index = instruction.VRegA();
1803 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1804 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1805 UpdateLocal(register_index, constant);
1806 break;
1807 }
1808
1809 // Note that the SSA building will refine the types.
1810 case Instruction::MOVE:
1811 case Instruction::MOVE_FROM16:
1812 case Instruction::MOVE_16: {
1813 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1814 UpdateLocal(instruction.VRegA(), value);
1815 break;
1816 }
1817
1818 // Note that the SSA building will refine the types.
1819 case Instruction::MOVE_WIDE:
1820 case Instruction::MOVE_WIDE_FROM16:
1821 case Instruction::MOVE_WIDE_16: {
1822 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1823 UpdateLocal(instruction.VRegA(), value);
1824 break;
1825 }
1826
1827 case Instruction::MOVE_OBJECT:
1828 case Instruction::MOVE_OBJECT_16:
1829 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001830 // The verifier has no notion of a null type, so a move-object of constant 0
1831 // will lead to the same constant 0 in the destination register. To mimic
1832 // this behavior, we just pretend we haven't seen a type change (int to reference)
1833 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1834 // types correct.
1835 uint32_t reg_number = instruction.VRegB();
1836 HInstruction* value = (*current_locals_)[reg_number];
1837 if (value->IsIntConstant()) {
1838 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1839 } else if (value->IsPhi()) {
1840 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1841 } else {
1842 value = LoadLocal(reg_number, Primitive::kPrimNot);
1843 }
David Brazdildee58d62016-04-07 09:54:26 +00001844 UpdateLocal(instruction.VRegA(), value);
1845 break;
1846 }
1847
1848 case Instruction::RETURN_VOID_NO_BARRIER:
1849 case Instruction::RETURN_VOID: {
1850 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1851 break;
1852 }
1853
1854#define IF_XX(comparison, cond) \
1855 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1856 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1857
1858 IF_XX(HEqual, EQ);
1859 IF_XX(HNotEqual, NE);
1860 IF_XX(HLessThan, LT);
1861 IF_XX(HLessThanOrEqual, LE);
1862 IF_XX(HGreaterThan, GT);
1863 IF_XX(HGreaterThanOrEqual, GE);
1864
1865 case Instruction::GOTO:
1866 case Instruction::GOTO_16:
1867 case Instruction::GOTO_32: {
1868 AppendInstruction(new (arena_) HGoto(dex_pc));
1869 current_block_ = nullptr;
1870 break;
1871 }
1872
1873 case Instruction::RETURN: {
1874 BuildReturn(instruction, return_type_, dex_pc);
1875 break;
1876 }
1877
1878 case Instruction::RETURN_OBJECT: {
1879 BuildReturn(instruction, return_type_, dex_pc);
1880 break;
1881 }
1882
1883 case Instruction::RETURN_WIDE: {
1884 BuildReturn(instruction, return_type_, dex_pc);
1885 break;
1886 }
1887
1888 case Instruction::INVOKE_DIRECT:
1889 case Instruction::INVOKE_INTERFACE:
1890 case Instruction::INVOKE_STATIC:
1891 case Instruction::INVOKE_SUPER:
1892 case Instruction::INVOKE_VIRTUAL:
1893 case Instruction::INVOKE_VIRTUAL_QUICK: {
1894 uint16_t method_idx;
1895 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1896 if (!CanDecodeQuickenedInfo()) {
1897 return false;
1898 }
1899 method_idx = LookupQuickenedInfo(dex_pc);
1900 } else {
1901 method_idx = instruction.VRegB_35c();
1902 }
1903 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1904 uint32_t args[5];
1905 instruction.GetVarArgs(args);
1906 if (!BuildInvoke(instruction, dex_pc, method_idx,
1907 number_of_vreg_arguments, false, args, -1)) {
1908 return false;
1909 }
1910 break;
1911 }
1912
1913 case Instruction::INVOKE_DIRECT_RANGE:
1914 case Instruction::INVOKE_INTERFACE_RANGE:
1915 case Instruction::INVOKE_STATIC_RANGE:
1916 case Instruction::INVOKE_SUPER_RANGE:
1917 case Instruction::INVOKE_VIRTUAL_RANGE:
1918 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1919 uint16_t method_idx;
1920 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1921 if (!CanDecodeQuickenedInfo()) {
1922 return false;
1923 }
1924 method_idx = LookupQuickenedInfo(dex_pc);
1925 } else {
1926 method_idx = instruction.VRegB_3rc();
1927 }
1928 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1929 uint32_t register_index = instruction.VRegC();
1930 if (!BuildInvoke(instruction, dex_pc, method_idx,
1931 number_of_vreg_arguments, true, nullptr, register_index)) {
1932 return false;
1933 }
1934 break;
1935 }
1936
Orion Hodsonac141392017-01-13 11:53:47 +00001937 case Instruction::INVOKE_POLYMORPHIC: {
1938 uint16_t method_idx = instruction.VRegB_45cc();
1939 uint16_t proto_idx = instruction.VRegH_45cc();
1940 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
1941 uint32_t args[5];
1942 instruction.GetVarArgs(args);
1943 return BuildInvokePolymorphic(instruction,
1944 dex_pc,
1945 method_idx,
1946 proto_idx,
1947 number_of_vreg_arguments,
1948 false,
1949 args,
1950 -1);
1951 }
1952
1953 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
1954 uint16_t method_idx = instruction.VRegB_4rcc();
1955 uint16_t proto_idx = instruction.VRegH_4rcc();
1956 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
1957 uint32_t register_index = instruction.VRegC_4rcc();
1958 return BuildInvokePolymorphic(instruction,
1959 dex_pc,
1960 method_idx,
1961 proto_idx,
1962 number_of_vreg_arguments,
1963 true,
1964 nullptr,
1965 register_index);
1966 }
1967
David Brazdildee58d62016-04-07 09:54:26 +00001968 case Instruction::NEG_INT: {
1969 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1970 break;
1971 }
1972
1973 case Instruction::NEG_LONG: {
1974 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1975 break;
1976 }
1977
1978 case Instruction::NEG_FLOAT: {
1979 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1980 break;
1981 }
1982
1983 case Instruction::NEG_DOUBLE: {
1984 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1985 break;
1986 }
1987
1988 case Instruction::NOT_INT: {
1989 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1990 break;
1991 }
1992
1993 case Instruction::NOT_LONG: {
1994 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1995 break;
1996 }
1997
1998 case Instruction::INT_TO_LONG: {
1999 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2000 break;
2001 }
2002
2003 case Instruction::INT_TO_FLOAT: {
2004 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2005 break;
2006 }
2007
2008 case Instruction::INT_TO_DOUBLE: {
2009 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2010 break;
2011 }
2012
2013 case Instruction::LONG_TO_INT: {
2014 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2015 break;
2016 }
2017
2018 case Instruction::LONG_TO_FLOAT: {
2019 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2020 break;
2021 }
2022
2023 case Instruction::LONG_TO_DOUBLE: {
2024 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2025 break;
2026 }
2027
2028 case Instruction::FLOAT_TO_INT: {
2029 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2030 break;
2031 }
2032
2033 case Instruction::FLOAT_TO_LONG: {
2034 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2035 break;
2036 }
2037
2038 case Instruction::FLOAT_TO_DOUBLE: {
2039 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2040 break;
2041 }
2042
2043 case Instruction::DOUBLE_TO_INT: {
2044 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2045 break;
2046 }
2047
2048 case Instruction::DOUBLE_TO_LONG: {
2049 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2050 break;
2051 }
2052
2053 case Instruction::DOUBLE_TO_FLOAT: {
2054 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2055 break;
2056 }
2057
2058 case Instruction::INT_TO_BYTE: {
2059 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2060 break;
2061 }
2062
2063 case Instruction::INT_TO_SHORT: {
2064 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2065 break;
2066 }
2067
2068 case Instruction::INT_TO_CHAR: {
2069 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2070 break;
2071 }
2072
2073 case Instruction::ADD_INT: {
2074 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2075 break;
2076 }
2077
2078 case Instruction::ADD_LONG: {
2079 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2080 break;
2081 }
2082
2083 case Instruction::ADD_DOUBLE: {
2084 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2085 break;
2086 }
2087
2088 case Instruction::ADD_FLOAT: {
2089 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2090 break;
2091 }
2092
2093 case Instruction::SUB_INT: {
2094 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2095 break;
2096 }
2097
2098 case Instruction::SUB_LONG: {
2099 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2100 break;
2101 }
2102
2103 case Instruction::SUB_FLOAT: {
2104 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2105 break;
2106 }
2107
2108 case Instruction::SUB_DOUBLE: {
2109 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2110 break;
2111 }
2112
2113 case Instruction::ADD_INT_2ADDR: {
2114 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2115 break;
2116 }
2117
2118 case Instruction::MUL_INT: {
2119 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2120 break;
2121 }
2122
2123 case Instruction::MUL_LONG: {
2124 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2125 break;
2126 }
2127
2128 case Instruction::MUL_FLOAT: {
2129 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2130 break;
2131 }
2132
2133 case Instruction::MUL_DOUBLE: {
2134 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2135 break;
2136 }
2137
2138 case Instruction::DIV_INT: {
2139 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2140 dex_pc, Primitive::kPrimInt, false, true);
2141 break;
2142 }
2143
2144 case Instruction::DIV_LONG: {
2145 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2146 dex_pc, Primitive::kPrimLong, false, true);
2147 break;
2148 }
2149
2150 case Instruction::DIV_FLOAT: {
2151 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2152 break;
2153 }
2154
2155 case Instruction::DIV_DOUBLE: {
2156 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2157 break;
2158 }
2159
2160 case Instruction::REM_INT: {
2161 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2162 dex_pc, Primitive::kPrimInt, false, false);
2163 break;
2164 }
2165
2166 case Instruction::REM_LONG: {
2167 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2168 dex_pc, Primitive::kPrimLong, false, false);
2169 break;
2170 }
2171
2172 case Instruction::REM_FLOAT: {
2173 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2174 break;
2175 }
2176
2177 case Instruction::REM_DOUBLE: {
2178 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2179 break;
2180 }
2181
2182 case Instruction::AND_INT: {
2183 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2184 break;
2185 }
2186
2187 case Instruction::AND_LONG: {
2188 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2189 break;
2190 }
2191
2192 case Instruction::SHL_INT: {
2193 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2194 break;
2195 }
2196
2197 case Instruction::SHL_LONG: {
2198 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2199 break;
2200 }
2201
2202 case Instruction::SHR_INT: {
2203 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2204 break;
2205 }
2206
2207 case Instruction::SHR_LONG: {
2208 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2209 break;
2210 }
2211
2212 case Instruction::USHR_INT: {
2213 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2214 break;
2215 }
2216
2217 case Instruction::USHR_LONG: {
2218 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2219 break;
2220 }
2221
2222 case Instruction::OR_INT: {
2223 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2224 break;
2225 }
2226
2227 case Instruction::OR_LONG: {
2228 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2229 break;
2230 }
2231
2232 case Instruction::XOR_INT: {
2233 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2234 break;
2235 }
2236
2237 case Instruction::XOR_LONG: {
2238 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2239 break;
2240 }
2241
2242 case Instruction::ADD_LONG_2ADDR: {
2243 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2244 break;
2245 }
2246
2247 case Instruction::ADD_DOUBLE_2ADDR: {
2248 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2249 break;
2250 }
2251
2252 case Instruction::ADD_FLOAT_2ADDR: {
2253 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2254 break;
2255 }
2256
2257 case Instruction::SUB_INT_2ADDR: {
2258 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2259 break;
2260 }
2261
2262 case Instruction::SUB_LONG_2ADDR: {
2263 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2264 break;
2265 }
2266
2267 case Instruction::SUB_FLOAT_2ADDR: {
2268 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2269 break;
2270 }
2271
2272 case Instruction::SUB_DOUBLE_2ADDR: {
2273 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2274 break;
2275 }
2276
2277 case Instruction::MUL_INT_2ADDR: {
2278 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2279 break;
2280 }
2281
2282 case Instruction::MUL_LONG_2ADDR: {
2283 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2284 break;
2285 }
2286
2287 case Instruction::MUL_FLOAT_2ADDR: {
2288 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2289 break;
2290 }
2291
2292 case Instruction::MUL_DOUBLE_2ADDR: {
2293 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2294 break;
2295 }
2296
2297 case Instruction::DIV_INT_2ADDR: {
2298 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2299 dex_pc, Primitive::kPrimInt, false, true);
2300 break;
2301 }
2302
2303 case Instruction::DIV_LONG_2ADDR: {
2304 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2305 dex_pc, Primitive::kPrimLong, false, true);
2306 break;
2307 }
2308
2309 case Instruction::REM_INT_2ADDR: {
2310 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2311 dex_pc, Primitive::kPrimInt, false, false);
2312 break;
2313 }
2314
2315 case Instruction::REM_LONG_2ADDR: {
2316 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2317 dex_pc, Primitive::kPrimLong, false, false);
2318 break;
2319 }
2320
2321 case Instruction::REM_FLOAT_2ADDR: {
2322 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2323 break;
2324 }
2325
2326 case Instruction::REM_DOUBLE_2ADDR: {
2327 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2328 break;
2329 }
2330
2331 case Instruction::SHL_INT_2ADDR: {
2332 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2333 break;
2334 }
2335
2336 case Instruction::SHL_LONG_2ADDR: {
2337 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2338 break;
2339 }
2340
2341 case Instruction::SHR_INT_2ADDR: {
2342 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2343 break;
2344 }
2345
2346 case Instruction::SHR_LONG_2ADDR: {
2347 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2348 break;
2349 }
2350
2351 case Instruction::USHR_INT_2ADDR: {
2352 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2353 break;
2354 }
2355
2356 case Instruction::USHR_LONG_2ADDR: {
2357 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2358 break;
2359 }
2360
2361 case Instruction::DIV_FLOAT_2ADDR: {
2362 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2363 break;
2364 }
2365
2366 case Instruction::DIV_DOUBLE_2ADDR: {
2367 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2368 break;
2369 }
2370
2371 case Instruction::AND_INT_2ADDR: {
2372 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2373 break;
2374 }
2375
2376 case Instruction::AND_LONG_2ADDR: {
2377 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2378 break;
2379 }
2380
2381 case Instruction::OR_INT_2ADDR: {
2382 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2383 break;
2384 }
2385
2386 case Instruction::OR_LONG_2ADDR: {
2387 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2388 break;
2389 }
2390
2391 case Instruction::XOR_INT_2ADDR: {
2392 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2393 break;
2394 }
2395
2396 case Instruction::XOR_LONG_2ADDR: {
2397 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2398 break;
2399 }
2400
2401 case Instruction::ADD_INT_LIT16: {
2402 Binop_22s<HAdd>(instruction, false, dex_pc);
2403 break;
2404 }
2405
2406 case Instruction::AND_INT_LIT16: {
2407 Binop_22s<HAnd>(instruction, false, dex_pc);
2408 break;
2409 }
2410
2411 case Instruction::OR_INT_LIT16: {
2412 Binop_22s<HOr>(instruction, false, dex_pc);
2413 break;
2414 }
2415
2416 case Instruction::XOR_INT_LIT16: {
2417 Binop_22s<HXor>(instruction, false, dex_pc);
2418 break;
2419 }
2420
2421 case Instruction::RSUB_INT: {
2422 Binop_22s<HSub>(instruction, true, dex_pc);
2423 break;
2424 }
2425
2426 case Instruction::MUL_INT_LIT16: {
2427 Binop_22s<HMul>(instruction, false, dex_pc);
2428 break;
2429 }
2430
2431 case Instruction::ADD_INT_LIT8: {
2432 Binop_22b<HAdd>(instruction, false, dex_pc);
2433 break;
2434 }
2435
2436 case Instruction::AND_INT_LIT8: {
2437 Binop_22b<HAnd>(instruction, false, dex_pc);
2438 break;
2439 }
2440
2441 case Instruction::OR_INT_LIT8: {
2442 Binop_22b<HOr>(instruction, false, dex_pc);
2443 break;
2444 }
2445
2446 case Instruction::XOR_INT_LIT8: {
2447 Binop_22b<HXor>(instruction, false, dex_pc);
2448 break;
2449 }
2450
2451 case Instruction::RSUB_INT_LIT8: {
2452 Binop_22b<HSub>(instruction, true, dex_pc);
2453 break;
2454 }
2455
2456 case Instruction::MUL_INT_LIT8: {
2457 Binop_22b<HMul>(instruction, false, dex_pc);
2458 break;
2459 }
2460
2461 case Instruction::DIV_INT_LIT16:
2462 case Instruction::DIV_INT_LIT8: {
2463 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2464 dex_pc, Primitive::kPrimInt, true, true);
2465 break;
2466 }
2467
2468 case Instruction::REM_INT_LIT16:
2469 case Instruction::REM_INT_LIT8: {
2470 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2471 dex_pc, Primitive::kPrimInt, true, false);
2472 break;
2473 }
2474
2475 case Instruction::SHL_INT_LIT8: {
2476 Binop_22b<HShl>(instruction, false, dex_pc);
2477 break;
2478 }
2479
2480 case Instruction::SHR_INT_LIT8: {
2481 Binop_22b<HShr>(instruction, false, dex_pc);
2482 break;
2483 }
2484
2485 case Instruction::USHR_INT_LIT8: {
2486 Binop_22b<HUShr>(instruction, false, dex_pc);
2487 break;
2488 }
2489
2490 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002491 if (!BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +00002492 return false;
2493 }
2494 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2495 break;
2496 }
2497
2498 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002499 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002500 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00002501 HLoadClass* cls = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
2502 AppendInstruction(new (arena_) HNewArray(cls, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002503 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2504 break;
2505 }
2506
2507 case Instruction::FILLED_NEW_ARRAY: {
2508 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002509 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002510 uint32_t args[5];
2511 instruction.GetVarArgs(args);
2512 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2513 break;
2514 }
2515
2516 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2517 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002518 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002519 uint32_t register_index = instruction.VRegC_3rc();
2520 BuildFilledNewArray(
2521 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2522 break;
2523 }
2524
2525 case Instruction::FILL_ARRAY_DATA: {
2526 BuildFillArrayData(instruction, dex_pc);
2527 break;
2528 }
2529
2530 case Instruction::MOVE_RESULT:
2531 case Instruction::MOVE_RESULT_WIDE:
2532 case Instruction::MOVE_RESULT_OBJECT: {
2533 DCHECK(latest_result_ != nullptr);
2534 UpdateLocal(instruction.VRegA(), latest_result_);
2535 latest_result_ = nullptr;
2536 break;
2537 }
2538
2539 case Instruction::CMP_LONG: {
2540 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2541 break;
2542 }
2543
2544 case Instruction::CMPG_FLOAT: {
2545 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2546 break;
2547 }
2548
2549 case Instruction::CMPG_DOUBLE: {
2550 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2551 break;
2552 }
2553
2554 case Instruction::CMPL_FLOAT: {
2555 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2556 break;
2557 }
2558
2559 case Instruction::CMPL_DOUBLE: {
2560 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2561 break;
2562 }
2563
2564 case Instruction::NOP:
2565 break;
2566
2567 case Instruction::IGET:
2568 case Instruction::IGET_QUICK:
2569 case Instruction::IGET_WIDE:
2570 case Instruction::IGET_WIDE_QUICK:
2571 case Instruction::IGET_OBJECT:
2572 case Instruction::IGET_OBJECT_QUICK:
2573 case Instruction::IGET_BOOLEAN:
2574 case Instruction::IGET_BOOLEAN_QUICK:
2575 case Instruction::IGET_BYTE:
2576 case Instruction::IGET_BYTE_QUICK:
2577 case Instruction::IGET_CHAR:
2578 case Instruction::IGET_CHAR_QUICK:
2579 case Instruction::IGET_SHORT:
2580 case Instruction::IGET_SHORT_QUICK: {
2581 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2582 return false;
2583 }
2584 break;
2585 }
2586
2587 case Instruction::IPUT:
2588 case Instruction::IPUT_QUICK:
2589 case Instruction::IPUT_WIDE:
2590 case Instruction::IPUT_WIDE_QUICK:
2591 case Instruction::IPUT_OBJECT:
2592 case Instruction::IPUT_OBJECT_QUICK:
2593 case Instruction::IPUT_BOOLEAN:
2594 case Instruction::IPUT_BOOLEAN_QUICK:
2595 case Instruction::IPUT_BYTE:
2596 case Instruction::IPUT_BYTE_QUICK:
2597 case Instruction::IPUT_CHAR:
2598 case Instruction::IPUT_CHAR_QUICK:
2599 case Instruction::IPUT_SHORT:
2600 case Instruction::IPUT_SHORT_QUICK: {
2601 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2602 return false;
2603 }
2604 break;
2605 }
2606
2607 case Instruction::SGET:
2608 case Instruction::SGET_WIDE:
2609 case Instruction::SGET_OBJECT:
2610 case Instruction::SGET_BOOLEAN:
2611 case Instruction::SGET_BYTE:
2612 case Instruction::SGET_CHAR:
2613 case Instruction::SGET_SHORT: {
2614 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2615 return false;
2616 }
2617 break;
2618 }
2619
2620 case Instruction::SPUT:
2621 case Instruction::SPUT_WIDE:
2622 case Instruction::SPUT_OBJECT:
2623 case Instruction::SPUT_BOOLEAN:
2624 case Instruction::SPUT_BYTE:
2625 case Instruction::SPUT_CHAR:
2626 case Instruction::SPUT_SHORT: {
2627 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2628 return false;
2629 }
2630 break;
2631 }
2632
2633#define ARRAY_XX(kind, anticipated_type) \
2634 case Instruction::AGET##kind: { \
2635 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2636 break; \
2637 } \
2638 case Instruction::APUT##kind: { \
2639 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2640 break; \
2641 }
2642
2643 ARRAY_XX(, Primitive::kPrimInt);
2644 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2645 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2646 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2647 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2648 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2649 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2650
2651 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002652 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002653 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2654 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2655 break;
2656 }
2657
2658 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002659 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002660 AppendInstruction(
2661 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2662 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2663 break;
2664 }
2665
2666 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002667 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002668 AppendInstruction(
2669 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2670 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2671 break;
2672 }
2673
2674 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002675 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002676 BuildLoadClass(type_index, dex_pc, /* check_access */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002677 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2678 break;
2679 }
2680
2681 case Instruction::MOVE_EXCEPTION: {
2682 AppendInstruction(new (arena_) HLoadException(dex_pc));
2683 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2684 AppendInstruction(new (arena_) HClearException(dex_pc));
2685 break;
2686 }
2687
2688 case Instruction::THROW: {
2689 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2690 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2691 // We finished building this block. Set the current block to null to avoid
2692 // adding dead instructions to it.
2693 current_block_ = nullptr;
2694 break;
2695 }
2696
2697 case Instruction::INSTANCE_OF: {
2698 uint8_t destination = instruction.VRegA_22c();
2699 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002700 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002701 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2702 break;
2703 }
2704
2705 case Instruction::CHECK_CAST: {
2706 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002707 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002708 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2709 break;
2710 }
2711
2712 case Instruction::MONITOR_ENTER: {
2713 AppendInstruction(new (arena_) HMonitorOperation(
2714 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2715 HMonitorOperation::OperationKind::kEnter,
2716 dex_pc));
2717 break;
2718 }
2719
2720 case Instruction::MONITOR_EXIT: {
2721 AppendInstruction(new (arena_) HMonitorOperation(
2722 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2723 HMonitorOperation::OperationKind::kExit,
2724 dex_pc));
2725 break;
2726 }
2727
2728 case Instruction::SPARSE_SWITCH:
2729 case Instruction::PACKED_SWITCH: {
2730 BuildSwitch(instruction, dex_pc);
2731 break;
2732 }
2733
2734 default:
2735 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002736 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002737 << " because of unhandled instruction "
2738 << instruction.Name();
2739 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2740 return false;
2741 }
2742 return true;
2743} // NOLINT(readability/fn_size)
2744
David Brazdildee58d62016-04-07 09:54:26 +00002745} // namespace art