blob: 3d911d77ba091ff0cf9b46e9944f5f4cbddac58b [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 Markoec786222016-12-20 16:24:13 +0000671 StackHandleScope<2> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +0000672
673 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Markoec786222016-12-20 16:24:13 +0000674 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +0000675 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100676 // We fetch the referenced class eagerly (that is, the class pointed by in the MethodId
677 // at method_idx), as `CanAccessResolvedMethod` expects it be be in the dex cache.
678 Handle<mirror::Class> methods_class(hs.NewHandle(class_linker->ResolveReferencedClassOfMethod(
679 method_idx, dex_compilation_unit_->GetDexCache(), class_loader)));
680
681 if (UNLIKELY(methods_class.Get() == nullptr)) {
682 // Clean up any exception left by type resolution.
683 soa.Self()->ClearException();
684 return nullptr;
685 }
David Brazdildee58d62016-04-07 09:54:26 +0000686
687 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
688 *dex_compilation_unit_->GetDexFile(),
689 method_idx,
690 dex_compilation_unit_->GetDexCache(),
691 class_loader,
692 /* referrer */ nullptr,
693 invoke_type);
694
695 if (UNLIKELY(resolved_method == nullptr)) {
696 // Clean up any exception left by type resolution.
697 soa.Self()->ClearException();
698 return nullptr;
699 }
700
701 // Check access. The class linker has a fast path for looking into the dex cache
702 // and does not check the access if it hits it.
703 if (compiling_class.Get() == nullptr) {
704 if (!resolved_method->IsPublic()) {
705 return nullptr;
706 }
707 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
708 resolved_method,
709 dex_compilation_unit_->GetDexCache().Get(),
710 method_idx)) {
711 return nullptr;
712 }
713
714 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
715 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
716 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
717 // which require runtime handling.
718 if (invoke_type == kSuper) {
719 if (compiling_class.Get() == nullptr) {
720 // We could not determine the method's class we need to wait until runtime.
721 DCHECK(Runtime::Current()->IsAotCompiler());
722 return nullptr;
723 }
Aart Bikf663e342016-04-04 17:28:59 -0700724 if (!methods_class->IsAssignableFrom(compiling_class.Get())) {
725 // We cannot statically determine the target method. The runtime will throw a
726 // NoSuchMethodError on this one.
727 return nullptr;
728 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100729 ArtMethod* actual_method;
730 if (methods_class->IsInterface()) {
731 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
732 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000733 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100734 uint16_t vtable_index = resolved_method->GetMethodIndex();
735 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
736 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000737 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100738 if (actual_method != resolved_method &&
739 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
740 // The back-end code generator relies on this check in order to ensure that it will not
741 // attempt to read the dex_cache with a dex_method_index that is not from the correct
742 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
743 // builder, which means that the code-generator (and compiler driver during sharpening and
744 // inliner, maybe) might invoke an incorrect method.
745 // TODO: The actual method could still be referenced in the current dex file, so we
746 // could try locating it.
747 // TODO: Remove the dex_file restriction.
748 return nullptr;
749 }
750 if (!actual_method->IsInvokable()) {
751 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
752 // could resolve the callee to the wrong method.
753 return nullptr;
754 }
755 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000756 }
757
758 // Check for incompatible class changes. The class linker has a fast path for
759 // looking into the dex cache and does not check incompatible class changes if it hits it.
760 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
761 return nullptr;
762 }
763
764 return resolved_method;
765}
766
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100767static bool IsStringConstructor(ArtMethod* method) {
768 ScopedObjectAccess soa(Thread::Current());
769 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
770}
771
David Brazdildee58d62016-04-07 09:54:26 +0000772bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
773 uint32_t dex_pc,
774 uint32_t method_idx,
775 uint32_t number_of_vreg_arguments,
776 bool is_range,
777 uint32_t* args,
778 uint32_t register_index) {
779 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
780 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
781 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
782
783 // Remove the return type from the 'proto'.
784 size_t number_of_arguments = strlen(descriptor) - 1;
785 if (invoke_type != kStatic) { // instance call
786 // One extra argument for 'this'.
787 number_of_arguments++;
788 }
789
David Brazdildee58d62016-04-07 09:54:26 +0000790 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
791
792 if (UNLIKELY(resolved_method == nullptr)) {
793 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
794 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
795 number_of_arguments,
796 return_type,
797 dex_pc,
798 method_idx,
799 invoke_type);
800 return HandleInvoke(invoke,
801 number_of_vreg_arguments,
802 args,
803 register_index,
804 is_range,
805 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700806 nullptr, /* clinit_check */
807 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000808 }
809
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100810 // Replace calls to String.<init> with StringFactory.
811 if (IsStringConstructor(resolved_method)) {
812 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
813 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
814 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
815 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000816 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100817 };
818 MethodReference target_method(dex_file_, method_idx);
819 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
820 arena_,
821 number_of_arguments - 1,
822 Primitive::kPrimNot /*return_type */,
823 dex_pc,
824 method_idx,
825 nullptr,
826 dispatch_info,
827 invoke_type,
828 target_method,
829 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
830 return HandleStringInit(invoke,
831 number_of_vreg_arguments,
832 args,
833 register_index,
834 is_range,
835 descriptor);
836 }
837
David Brazdildee58d62016-04-07 09:54:26 +0000838 // Potential class initialization check, in the case of a static method call.
839 HClinitCheck* clinit_check = nullptr;
840 HInvoke* invoke = nullptr;
841 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
842 // By default, consider that the called method implicitly requires
843 // an initialization check of its declaring method.
844 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
845 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
846 ScopedObjectAccess soa(Thread::Current());
847 if (invoke_type == kStatic) {
848 clinit_check = ProcessClinitCheckForInvoke(
849 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
850 } else if (invoke_type == kSuper) {
851 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100852 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000853 // we resolved to the method referenced by the instruction.
854 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000855 }
856 }
857
858 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
859 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
860 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000861 0u
David Brazdildee58d62016-04-07 09:54:26 +0000862 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100863 MethodReference target_method(resolved_method->GetDexFile(),
864 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000865 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
866 number_of_arguments,
867 return_type,
868 dex_pc,
869 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100870 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000871 dispatch_info,
872 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100873 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000874 clinit_check_requirement);
875 } else if (invoke_type == kVirtual) {
876 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
877 invoke = new (arena_) HInvokeVirtual(arena_,
878 number_of_arguments,
879 return_type,
880 dex_pc,
881 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100882 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000883 resolved_method->GetMethodIndex());
884 } else {
885 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100886 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000887 invoke = new (arena_) HInvokeInterface(arena_,
888 number_of_arguments,
889 return_type,
890 dex_pc,
891 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100892 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700893 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000894 }
895
896 return HandleInvoke(invoke,
897 number_of_vreg_arguments,
898 args,
899 register_index,
900 is_range,
901 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700902 clinit_check,
903 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000904}
905
Orion Hodsonac141392017-01-13 11:53:47 +0000906bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
907 uint32_t dex_pc,
908 uint32_t method_idx,
909 uint32_t proto_idx,
910 uint32_t number_of_vreg_arguments,
911 bool is_range,
912 uint32_t* args,
913 uint32_t register_index) {
914 const char* descriptor = dex_file_->GetShorty(proto_idx);
915 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
916 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
917 size_t number_of_arguments = strlen(descriptor);
918 HInvoke* invoke = new (arena_) HInvokePolymorphic(arena_,
919 number_of_arguments,
920 return_type,
921 dex_pc,
922 method_idx);
923 return HandleInvoke(invoke,
924 number_of_vreg_arguments,
925 args,
926 register_index,
927 is_range,
928 descriptor,
929 nullptr /* clinit_check */,
930 false /* is_unresolved */);
931}
932
Andreas Gampea5b09a62016-11-17 15:21:22 -0800933bool HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100934 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100935 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100936 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
937
David Brazdildee58d62016-04-07 09:54:26 +0000938 if (outer_dex_cache.Get() != dex_cache.Get()) {
939 // We currently do not support inlining allocations across dex files.
940 return false;
941 }
942
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000943 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000944
David Brazdildee58d62016-04-07 09:54:26 +0000945 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000946 Handle<mirror::Class> klass = load_class->GetClass();
947
948 if (!IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +0000949 cls = new (arena_) HClinitCheck(load_class, dex_pc);
950 AppendInstruction(cls);
951 }
952
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000953 // Only the access check entrypoint handles the finalizable class case. If we
954 // need access checks, then we haven't resolved the method and the class may
955 // again be finalizable.
956 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
957 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
958 entrypoint = kQuickAllocObjectWithChecks;
959 }
960
961 // Consider classes we haven't resolved as potentially finalizable.
962 bool finalizable = (klass.Get() == nullptr) || klass->IsFinalizable();
963
David Brazdildee58d62016-04-07 09:54:26 +0000964 AppendInstruction(new (arena_) HNewInstance(
965 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000966 dex_pc,
967 type_index,
968 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +0000969 finalizable,
970 entrypoint));
971 return true;
972}
973
974static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700975 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000976 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
977}
978
979bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
980 if (cls.Get() == nullptr) {
981 return false;
982 }
983
984 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
985 // check whether the class is in an image for the AOT compilation.
986 if (cls->IsInitialized() &&
987 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
988 return true;
989 }
990
991 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
992 return true;
993 }
994
995 // TODO: We should walk over the inlined methods, but we don't pass
996 // that information to the builder.
997 if (IsSubClass(GetCompilingClass(), cls.Get())) {
998 return true;
999 }
1000
1001 return false;
1002}
1003
1004HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1005 uint32_t dex_pc,
1006 ArtMethod* resolved_method,
1007 uint32_t method_idx,
1008 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
David Brazdildee58d62016-04-07 09:54:26 +00001009 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001010 StackHandleScope<2> hs(self);
1011 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1012 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001013 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1014 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
1015
1016 // The index at which the method's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001017 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001018 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1019 if (is_outer_class) {
1020 storage_index = outer_class->GetDexTypeIndex();
1021 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1022 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1023 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1024 GetCompilingClass(),
1025 resolved_method,
1026 method_idx,
1027 &storage_index);
1028 }
1029
1030 HClinitCheck* clinit_check = nullptr;
1031
1032 if (IsInitialized(resolved_method_class)) {
1033 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001034 } else if (storage_index.IsValid()) {
David Brazdildee58d62016-04-07 09:54:26 +00001035 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001036 HLoadClass* cls = BuildLoadClass(
1037 storage_index, dex_pc, /* check_access */ false, /* outer */ true);
1038 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001039 AppendInstruction(clinit_check);
1040 }
1041 return clinit_check;
1042}
1043
1044bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1045 uint32_t number_of_vreg_arguments,
1046 uint32_t* args,
1047 uint32_t register_index,
1048 bool is_range,
1049 const char* descriptor,
1050 size_t start_index,
1051 size_t* argument_index) {
1052 uint32_t descriptor_index = 1; // Skip the return type.
1053
1054 for (size_t i = start_index;
1055 // Make sure we don't go over the expected arguments or over the number of
1056 // dex registers given. If the instruction was seen as dead by the verifier,
1057 // it hasn't been properly checked.
1058 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1059 i++, (*argument_index)++) {
1060 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1061 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1062 if (!is_range
1063 && is_wide
1064 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1065 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1066 // reject any class where this is violated. However, the verifier only does these checks
1067 // on non trivially dead instructions, so we just bailout the compilation.
1068 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001069 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001070 << " because of non-sequential dex register pair in wide argument";
1071 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1072 return false;
1073 }
1074 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1075 invoke->SetArgumentAt(*argument_index, arg);
1076 if (is_wide) {
1077 i++;
1078 }
1079 }
1080
1081 if (*argument_index != invoke->GetNumberOfArguments()) {
1082 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001083 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001084 << " because of wrong number of arguments in invoke instruction";
1085 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1086 return false;
1087 }
1088
1089 if (invoke->IsInvokeStaticOrDirect() &&
1090 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1091 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1092 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1093 (*argument_index)++;
1094 }
1095
1096 return true;
1097}
1098
1099bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1100 uint32_t number_of_vreg_arguments,
1101 uint32_t* args,
1102 uint32_t register_index,
1103 bool is_range,
1104 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001105 HClinitCheck* clinit_check,
1106 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001107 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1108
1109 size_t start_index = 0;
1110 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001111 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001112 uint32_t obj_reg = is_range ? register_index : args[0];
1113 HInstruction* arg = is_unresolved
1114 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1115 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001116 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001117 start_index = 1;
1118 argument_index = 1;
1119 }
1120
1121 if (!SetupInvokeArguments(invoke,
1122 number_of_vreg_arguments,
1123 args,
1124 register_index,
1125 is_range,
1126 descriptor,
1127 start_index,
1128 &argument_index)) {
1129 return false;
1130 }
1131
1132 if (clinit_check != nullptr) {
1133 // Add the class initialization check as last input of `invoke`.
1134 DCHECK(invoke->IsInvokeStaticOrDirect());
1135 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1136 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1137 invoke->SetArgumentAt(argument_index, clinit_check);
1138 argument_index++;
1139 }
1140
1141 AppendInstruction(invoke);
1142 latest_result_ = invoke;
1143
1144 return true;
1145}
1146
1147bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1148 uint32_t number_of_vreg_arguments,
1149 uint32_t* args,
1150 uint32_t register_index,
1151 bool is_range,
1152 const char* descriptor) {
1153 DCHECK(invoke->IsInvokeStaticOrDirect());
1154 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1155
1156 size_t start_index = 1;
1157 size_t argument_index = 0;
1158 if (!SetupInvokeArguments(invoke,
1159 number_of_vreg_arguments,
1160 args,
1161 register_index,
1162 is_range,
1163 descriptor,
1164 start_index,
1165 &argument_index)) {
1166 return false;
1167 }
1168
1169 AppendInstruction(invoke);
1170
1171 // This is a StringFactory call, not an actual String constructor. Its result
1172 // replaces the empty String pre-allocated by NewInstance.
1173 uint32_t orig_this_reg = is_range ? register_index : args[0];
1174 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1175
1176 // Replacing the NewInstance might render it redundant. Keep a list of these
1177 // to be visited once it is clear whether it is has remaining uses.
1178 if (arg_this->IsNewInstance()) {
1179 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1180 } else {
1181 DCHECK(arg_this->IsPhi());
1182 // NewInstance is not the direct input of the StringFactory call. It might
1183 // be redundant but optimizing this case is not worth the effort.
1184 }
1185
1186 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1187 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1188 if ((*current_locals_)[vreg] == arg_this) {
1189 (*current_locals_)[vreg] = invoke;
1190 }
1191 }
1192
1193 return true;
1194}
1195
1196static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1197 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1198 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1199 return Primitive::GetType(type[0]);
1200}
1201
1202bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1203 uint32_t dex_pc,
1204 bool is_put) {
1205 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1206 uint32_t obj_reg = instruction.VRegB_22c();
1207 uint16_t field_index;
1208 if (instruction.IsQuickened()) {
1209 if (!CanDecodeQuickenedInfo()) {
1210 return false;
1211 }
1212 field_index = LookupQuickenedInfo(dex_pc);
1213 } else {
1214 field_index = instruction.VRegC_22c();
1215 }
1216
1217 ScopedObjectAccess soa(Thread::Current());
1218 ArtField* resolved_field =
1219 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1220
1221
Aart Bik14154132016-06-02 17:53:58 -07001222 // Generate an explicit null check on the reference, unless the field access
1223 // is unresolved. In that case, we rely on the runtime to perform various
1224 // checks first, followed by a null check.
1225 HInstruction* object = (resolved_field == nullptr)
1226 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1227 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001228
1229 Primitive::Type field_type = (resolved_field == nullptr)
1230 ? GetFieldAccessType(*dex_file_, field_index)
1231 : resolved_field->GetTypeAsPrimitiveType();
1232 if (is_put) {
1233 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1234 HInstruction* field_set = nullptr;
1235 if (resolved_field == nullptr) {
1236 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001237 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001238 value,
1239 field_type,
1240 field_index,
1241 dex_pc);
1242 } else {
1243 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001244 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001245 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001246 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001247 field_type,
1248 resolved_field->GetOffset(),
1249 resolved_field->IsVolatile(),
1250 field_index,
1251 class_def_index,
1252 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001253 dex_pc);
1254 }
1255 AppendInstruction(field_set);
1256 } else {
1257 HInstruction* field_get = nullptr;
1258 if (resolved_field == nullptr) {
1259 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001260 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001261 field_type,
1262 field_index,
1263 dex_pc);
1264 } else {
1265 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001266 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001267 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001268 field_type,
1269 resolved_field->GetOffset(),
1270 resolved_field->IsVolatile(),
1271 field_index,
1272 class_def_index,
1273 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001274 dex_pc);
1275 }
1276 AppendInstruction(field_get);
1277 UpdateLocal(source_or_dest_reg, field_get);
1278 }
1279
1280 return true;
1281}
1282
1283static mirror::Class* GetClassFrom(CompilerDriver* driver,
1284 const DexCompilationUnit& compilation_unit) {
1285 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoec786222016-12-20 16:24:13 +00001286 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001287 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001288
1289 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1290}
1291
1292mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1293 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1294}
1295
1296mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1297 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1298}
1299
Andreas Gampea5b09a62016-11-17 15:21:22 -08001300bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001301 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoec786222016-12-20 16:24:13 +00001302 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001303 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Markoec786222016-12-20 16:24:13 +00001304 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001305 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1306 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1307 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1308
1309 // GetOutermostCompilingClass returns null when the class is unresolved
1310 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1311 // we are compiling it.
1312 // When this happens we cannot establish a direct relation between the current
1313 // class and the outer class, so we return false.
1314 // (Note that this is only used for optimizing invokes and field accesses)
1315 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1316}
1317
1318void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001319 uint32_t dex_pc,
1320 bool is_put,
1321 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001322 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1323 uint16_t field_index = instruction.VRegB_21c();
1324
1325 if (is_put) {
1326 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1327 AppendInstruction(
1328 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1329 } else {
1330 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1331 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1332 }
1333}
1334
1335bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1336 uint32_t dex_pc,
1337 bool is_put) {
1338 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1339 uint16_t field_index = instruction.VRegB_21c();
1340
1341 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001342 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Markoec786222016-12-20 16:24:13 +00001343 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001344 ArtField* resolved_field = compiler_driver_->ResolveField(
1345 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1346
1347 if (resolved_field == nullptr) {
1348 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1349 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1350 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1351 return true;
1352 }
1353
Vladimir Markoec786222016-12-20 16:24:13 +00001354 StackHandleScope<2> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +00001355 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001356 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001357 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1358
1359 // The index at which the field's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001360 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001361 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1362 if (is_outer_class) {
1363 storage_index = outer_class->GetDexTypeIndex();
1364 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1365 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1366 return false;
1367 } else {
1368 // TODO: This is rather expensive. Perf it and cache the results if needed.
1369 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1370 outer_dex_cache.Get(),
1371 GetCompilingClass(),
1372 resolved_field,
1373 field_index,
1374 &storage_index);
1375 bool can_easily_access = is_put ? pair.second : pair.first;
1376 if (!can_easily_access) {
1377 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1378 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1379 return true;
1380 }
1381 }
1382
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001383 HLoadClass* constant = BuildLoadClass(
1384 storage_index, dex_pc, /* check_access */ false, /* outer */ true);
David Brazdildee58d62016-04-07 09:54:26 +00001385
1386 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001387 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1388 if (!IsInitialized(klass)) {
1389 cls = new (arena_) HClinitCheck(constant, dex_pc);
1390 AppendInstruction(cls);
1391 }
1392
1393 uint16_t class_def_index = klass->GetDexClassDefIndex();
1394 if (is_put) {
1395 // We need to keep the class alive before loading the value.
1396 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1397 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1398 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1399 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001400 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001401 field_type,
1402 resolved_field->GetOffset(),
1403 resolved_field->IsVolatile(),
1404 field_index,
1405 class_def_index,
1406 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001407 dex_pc));
1408 } else {
1409 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001410 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001411 field_type,
1412 resolved_field->GetOffset(),
1413 resolved_field->IsVolatile(),
1414 field_index,
1415 class_def_index,
1416 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001417 dex_pc));
1418 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1419 }
1420 return true;
1421}
1422
1423void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1424 uint16_t first_vreg,
1425 int64_t second_vreg_or_constant,
1426 uint32_t dex_pc,
1427 Primitive::Type type,
1428 bool second_is_constant,
1429 bool isDiv) {
1430 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1431
1432 HInstruction* first = LoadLocal(first_vreg, type);
1433 HInstruction* second = nullptr;
1434 if (second_is_constant) {
1435 if (type == Primitive::kPrimInt) {
1436 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1437 } else {
1438 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1439 }
1440 } else {
1441 second = LoadLocal(second_vreg_or_constant, type);
1442 }
1443
1444 if (!second_is_constant
1445 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1446 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1447 second = new (arena_) HDivZeroCheck(second, dex_pc);
1448 AppendInstruction(second);
1449 }
1450
1451 if (isDiv) {
1452 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1453 } else {
1454 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1455 }
1456 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1457}
1458
1459void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1460 uint32_t dex_pc,
1461 bool is_put,
1462 Primitive::Type anticipated_type) {
1463 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1464 uint8_t array_reg = instruction.VRegB_23x();
1465 uint8_t index_reg = instruction.VRegC_23x();
1466
David Brazdilc120bbe2016-04-22 16:57:00 +01001467 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001468 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1469 AppendInstruction(length);
1470 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1471 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1472 AppendInstruction(index);
1473 if (is_put) {
1474 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1475 // TODO: Insert a type check node if the type is Object.
1476 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1477 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1478 AppendInstruction(aset);
1479 } else {
1480 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1481 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1482 AppendInstruction(aget);
1483 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1484 }
1485 graph_->SetHasBoundsChecks(true);
1486}
1487
1488void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001489 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001490 uint32_t number_of_vreg_arguments,
1491 bool is_range,
1492 uint32_t* args,
1493 uint32_t register_index) {
1494 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001495 HLoadClass* cls = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
1496 HInstruction* object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001497 AppendInstruction(object);
1498
1499 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1500 DCHECK_EQ(descriptor[0], '[') << descriptor;
1501 char primitive = descriptor[1];
1502 DCHECK(primitive == 'I'
1503 || primitive == 'L'
1504 || primitive == '[') << descriptor;
1505 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1506 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1507
1508 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1509 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1510 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1511 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1512 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1513 AppendInstruction(aset);
1514 }
1515 latest_result_ = object;
1516}
1517
1518template <typename T>
1519void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1520 const T* data,
1521 uint32_t element_count,
1522 Primitive::Type anticipated_type,
1523 uint32_t dex_pc) {
1524 for (uint32_t i = 0; i < element_count; ++i) {
1525 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1526 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1527 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1528 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1529 AppendInstruction(aset);
1530 }
1531}
1532
1533void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001534 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001535
1536 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1537 const Instruction::ArrayDataPayload* payload =
1538 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1539 const uint8_t* data = payload->data;
1540 uint32_t element_count = payload->element_count;
1541
Vladimir Markoc69fba22016-09-06 16:49:15 +01001542 if (element_count == 0u) {
1543 // For empty payload we emit only the null check above.
1544 return;
1545 }
1546
1547 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1548 AppendInstruction(length);
1549
David Brazdildee58d62016-04-07 09:54:26 +00001550 // Implementation of this DEX instruction seems to be that the bounds check is
1551 // done before doing any stores.
1552 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1553 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1554
1555 switch (payload->element_width) {
1556 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001557 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001558 reinterpret_cast<const int8_t*>(data),
1559 element_count,
1560 Primitive::kPrimByte,
1561 dex_pc);
1562 break;
1563 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001564 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001565 reinterpret_cast<const int16_t*>(data),
1566 element_count,
1567 Primitive::kPrimShort,
1568 dex_pc);
1569 break;
1570 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001571 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001572 reinterpret_cast<const int32_t*>(data),
1573 element_count,
1574 Primitive::kPrimInt,
1575 dex_pc);
1576 break;
1577 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001578 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001579 reinterpret_cast<const int64_t*>(data),
1580 element_count,
1581 dex_pc);
1582 break;
1583 default:
1584 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1585 }
1586 graph_->SetHasBoundsChecks(true);
1587}
1588
1589void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1590 const int64_t* data,
1591 uint32_t element_count,
1592 uint32_t dex_pc) {
1593 for (uint32_t i = 0; i < element_count; ++i) {
1594 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1595 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1596 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1597 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1598 AppendInstruction(aset);
1599 }
1600}
1601
1602static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001603 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001604 if (cls.Get() == nullptr) {
1605 return TypeCheckKind::kUnresolvedCheck;
1606 } else if (cls->IsInterface()) {
1607 return TypeCheckKind::kInterfaceCheck;
1608 } else if (cls->IsArrayClass()) {
1609 if (cls->GetComponentType()->IsObjectClass()) {
1610 return TypeCheckKind::kArrayObjectCheck;
1611 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1612 return TypeCheckKind::kExactCheck;
1613 } else {
1614 return TypeCheckKind::kArrayCheck;
1615 }
1616 } else if (cls->IsFinal()) {
1617 return TypeCheckKind::kExactCheck;
1618 } else if (cls->IsAbstract()) {
1619 return TypeCheckKind::kAbstractClassCheck;
1620 } else {
1621 return TypeCheckKind::kClassHierarchyCheck;
1622 }
1623}
1624
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001625HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1626 uint32_t dex_pc,
1627 bool check_access,
1628 bool outer) {
1629 ScopedObjectAccess soa(Thread::Current());
1630 const DexCompilationUnit* compilation_unit =
1631 outer ? outer_compilation_unit_ : dex_compilation_unit_;
1632 const DexFile& dex_file = *compilation_unit->GetDexFile();
Vladimir Markoec786222016-12-20 16:24:13 +00001633 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001634 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
1635 soa, compilation_unit->GetDexCache(), class_loader, type_index, compilation_unit));
1636
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001637 bool is_accessible = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001638 if (!check_access) {
1639 is_accessible = true;
1640 } else if (klass.Get() != nullptr) {
1641 if (klass->IsPublic()) {
1642 is_accessible = true;
1643 } else {
1644 mirror::Class* compiling_class = GetCompilingClass();
1645 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
1646 is_accessible = true;
1647 }
1648 }
1649 }
1650
1651 HLoadClass* load_class = new (arena_) HLoadClass(
1652 graph_->GetCurrentMethod(),
1653 type_index,
1654 dex_file,
1655 klass,
1656 klass.Get() != nullptr && (klass.Get() == GetOutermostCompilingClass()),
1657 dex_pc,
1658 !is_accessible);
1659
1660 AppendInstruction(load_class);
1661 return load_class;
1662}
1663
David Brazdildee58d62016-04-07 09:54:26 +00001664void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1665 uint8_t destination,
1666 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001667 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001668 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001669 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001670 HLoadClass* cls = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
David Brazdildee58d62016-04-07 09:54:26 +00001671
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001672 ScopedObjectAccess soa(Thread::Current());
1673 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001674 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1675 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1676 UpdateLocal(destination, current_block_->GetLastInstruction());
1677 } else {
1678 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1679 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1680 // which may throw. If it succeeds BoundType sets the new type of `object`
1681 // for all subsequent uses.
1682 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1683 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1684 UpdateLocal(reference, current_block_->GetLastInstruction());
1685 }
1686}
1687
Andreas Gampea5b09a62016-11-17 15:21:22 -08001688bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Markoec786222016-12-20 16:24:13 +00001689 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1690 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001691}
1692
1693bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1694 return interpreter_metadata_ != nullptr;
1695}
1696
1697uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1698 DCHECK(interpreter_metadata_ != nullptr);
1699
1700 // First check if the info has already been decoded from `interpreter_metadata_`.
1701 auto it = skipped_interpreter_metadata_.find(dex_pc);
1702 if (it != skipped_interpreter_metadata_.end()) {
1703 // Remove the entry from the map and return the parsed info.
1704 uint16_t value_in_map = it->second;
1705 skipped_interpreter_metadata_.erase(it);
1706 return value_in_map;
1707 }
1708
1709 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1710 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1711 while (true) {
1712 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1713 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1714 DCHECK_LE(dex_pc_in_map, dex_pc);
1715
1716 if (dex_pc_in_map == dex_pc) {
1717 return value_in_map;
1718 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001719 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1720 // the same dex_pc. This is OK, because the compiler does not care about those
1721 // entries.
1722 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001723 }
1724 }
1725}
1726
1727bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1728 switch (instruction.Opcode()) {
1729 case Instruction::CONST_4: {
1730 int32_t register_index = instruction.VRegA();
1731 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1732 UpdateLocal(register_index, constant);
1733 break;
1734 }
1735
1736 case Instruction::CONST_16: {
1737 int32_t register_index = instruction.VRegA();
1738 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1739 UpdateLocal(register_index, constant);
1740 break;
1741 }
1742
1743 case Instruction::CONST: {
1744 int32_t register_index = instruction.VRegA();
1745 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1746 UpdateLocal(register_index, constant);
1747 break;
1748 }
1749
1750 case Instruction::CONST_HIGH16: {
1751 int32_t register_index = instruction.VRegA();
1752 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1753 UpdateLocal(register_index, constant);
1754 break;
1755 }
1756
1757 case Instruction::CONST_WIDE_16: {
1758 int32_t register_index = instruction.VRegA();
1759 // Get 16 bits of constant value, sign extended to 64 bits.
1760 int64_t value = instruction.VRegB_21s();
1761 value <<= 48;
1762 value >>= 48;
1763 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1764 UpdateLocal(register_index, constant);
1765 break;
1766 }
1767
1768 case Instruction::CONST_WIDE_32: {
1769 int32_t register_index = instruction.VRegA();
1770 // Get 32 bits of constant value, sign extended to 64 bits.
1771 int64_t value = instruction.VRegB_31i();
1772 value <<= 32;
1773 value >>= 32;
1774 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1775 UpdateLocal(register_index, constant);
1776 break;
1777 }
1778
1779 case Instruction::CONST_WIDE: {
1780 int32_t register_index = instruction.VRegA();
1781 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1782 UpdateLocal(register_index, constant);
1783 break;
1784 }
1785
1786 case Instruction::CONST_WIDE_HIGH16: {
1787 int32_t register_index = instruction.VRegA();
1788 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1789 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1790 UpdateLocal(register_index, constant);
1791 break;
1792 }
1793
1794 // Note that the SSA building will refine the types.
1795 case Instruction::MOVE:
1796 case Instruction::MOVE_FROM16:
1797 case Instruction::MOVE_16: {
1798 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1799 UpdateLocal(instruction.VRegA(), value);
1800 break;
1801 }
1802
1803 // Note that the SSA building will refine the types.
1804 case Instruction::MOVE_WIDE:
1805 case Instruction::MOVE_WIDE_FROM16:
1806 case Instruction::MOVE_WIDE_16: {
1807 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1808 UpdateLocal(instruction.VRegA(), value);
1809 break;
1810 }
1811
1812 case Instruction::MOVE_OBJECT:
1813 case Instruction::MOVE_OBJECT_16:
1814 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001815 // The verifier has no notion of a null type, so a move-object of constant 0
1816 // will lead to the same constant 0 in the destination register. To mimic
1817 // this behavior, we just pretend we haven't seen a type change (int to reference)
1818 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1819 // types correct.
1820 uint32_t reg_number = instruction.VRegB();
1821 HInstruction* value = (*current_locals_)[reg_number];
1822 if (value->IsIntConstant()) {
1823 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1824 } else if (value->IsPhi()) {
1825 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1826 } else {
1827 value = LoadLocal(reg_number, Primitive::kPrimNot);
1828 }
David Brazdildee58d62016-04-07 09:54:26 +00001829 UpdateLocal(instruction.VRegA(), value);
1830 break;
1831 }
1832
1833 case Instruction::RETURN_VOID_NO_BARRIER:
1834 case Instruction::RETURN_VOID: {
1835 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1836 break;
1837 }
1838
1839#define IF_XX(comparison, cond) \
1840 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1841 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1842
1843 IF_XX(HEqual, EQ);
1844 IF_XX(HNotEqual, NE);
1845 IF_XX(HLessThan, LT);
1846 IF_XX(HLessThanOrEqual, LE);
1847 IF_XX(HGreaterThan, GT);
1848 IF_XX(HGreaterThanOrEqual, GE);
1849
1850 case Instruction::GOTO:
1851 case Instruction::GOTO_16:
1852 case Instruction::GOTO_32: {
1853 AppendInstruction(new (arena_) HGoto(dex_pc));
1854 current_block_ = nullptr;
1855 break;
1856 }
1857
1858 case Instruction::RETURN: {
1859 BuildReturn(instruction, return_type_, dex_pc);
1860 break;
1861 }
1862
1863 case Instruction::RETURN_OBJECT: {
1864 BuildReturn(instruction, return_type_, dex_pc);
1865 break;
1866 }
1867
1868 case Instruction::RETURN_WIDE: {
1869 BuildReturn(instruction, return_type_, dex_pc);
1870 break;
1871 }
1872
1873 case Instruction::INVOKE_DIRECT:
1874 case Instruction::INVOKE_INTERFACE:
1875 case Instruction::INVOKE_STATIC:
1876 case Instruction::INVOKE_SUPER:
1877 case Instruction::INVOKE_VIRTUAL:
1878 case Instruction::INVOKE_VIRTUAL_QUICK: {
1879 uint16_t method_idx;
1880 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1881 if (!CanDecodeQuickenedInfo()) {
1882 return false;
1883 }
1884 method_idx = LookupQuickenedInfo(dex_pc);
1885 } else {
1886 method_idx = instruction.VRegB_35c();
1887 }
1888 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1889 uint32_t args[5];
1890 instruction.GetVarArgs(args);
1891 if (!BuildInvoke(instruction, dex_pc, method_idx,
1892 number_of_vreg_arguments, false, args, -1)) {
1893 return false;
1894 }
1895 break;
1896 }
1897
1898 case Instruction::INVOKE_DIRECT_RANGE:
1899 case Instruction::INVOKE_INTERFACE_RANGE:
1900 case Instruction::INVOKE_STATIC_RANGE:
1901 case Instruction::INVOKE_SUPER_RANGE:
1902 case Instruction::INVOKE_VIRTUAL_RANGE:
1903 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1904 uint16_t method_idx;
1905 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1906 if (!CanDecodeQuickenedInfo()) {
1907 return false;
1908 }
1909 method_idx = LookupQuickenedInfo(dex_pc);
1910 } else {
1911 method_idx = instruction.VRegB_3rc();
1912 }
1913 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1914 uint32_t register_index = instruction.VRegC();
1915 if (!BuildInvoke(instruction, dex_pc, method_idx,
1916 number_of_vreg_arguments, true, nullptr, register_index)) {
1917 return false;
1918 }
1919 break;
1920 }
1921
Orion Hodsonac141392017-01-13 11:53:47 +00001922 case Instruction::INVOKE_POLYMORPHIC: {
1923 uint16_t method_idx = instruction.VRegB_45cc();
1924 uint16_t proto_idx = instruction.VRegH_45cc();
1925 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
1926 uint32_t args[5];
1927 instruction.GetVarArgs(args);
1928 return BuildInvokePolymorphic(instruction,
1929 dex_pc,
1930 method_idx,
1931 proto_idx,
1932 number_of_vreg_arguments,
1933 false,
1934 args,
1935 -1);
1936 }
1937
1938 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
1939 uint16_t method_idx = instruction.VRegB_4rcc();
1940 uint16_t proto_idx = instruction.VRegH_4rcc();
1941 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
1942 uint32_t register_index = instruction.VRegC_4rcc();
1943 return BuildInvokePolymorphic(instruction,
1944 dex_pc,
1945 method_idx,
1946 proto_idx,
1947 number_of_vreg_arguments,
1948 true,
1949 nullptr,
1950 register_index);
1951 }
1952
David Brazdildee58d62016-04-07 09:54:26 +00001953 case Instruction::NEG_INT: {
1954 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1955 break;
1956 }
1957
1958 case Instruction::NEG_LONG: {
1959 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1960 break;
1961 }
1962
1963 case Instruction::NEG_FLOAT: {
1964 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1965 break;
1966 }
1967
1968 case Instruction::NEG_DOUBLE: {
1969 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1970 break;
1971 }
1972
1973 case Instruction::NOT_INT: {
1974 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1975 break;
1976 }
1977
1978 case Instruction::NOT_LONG: {
1979 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1980 break;
1981 }
1982
1983 case Instruction::INT_TO_LONG: {
1984 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
1985 break;
1986 }
1987
1988 case Instruction::INT_TO_FLOAT: {
1989 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
1990 break;
1991 }
1992
1993 case Instruction::INT_TO_DOUBLE: {
1994 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
1995 break;
1996 }
1997
1998 case Instruction::LONG_TO_INT: {
1999 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2000 break;
2001 }
2002
2003 case Instruction::LONG_TO_FLOAT: {
2004 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2005 break;
2006 }
2007
2008 case Instruction::LONG_TO_DOUBLE: {
2009 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2010 break;
2011 }
2012
2013 case Instruction::FLOAT_TO_INT: {
2014 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2015 break;
2016 }
2017
2018 case Instruction::FLOAT_TO_LONG: {
2019 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2020 break;
2021 }
2022
2023 case Instruction::FLOAT_TO_DOUBLE: {
2024 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2025 break;
2026 }
2027
2028 case Instruction::DOUBLE_TO_INT: {
2029 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2030 break;
2031 }
2032
2033 case Instruction::DOUBLE_TO_LONG: {
2034 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2035 break;
2036 }
2037
2038 case Instruction::DOUBLE_TO_FLOAT: {
2039 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2040 break;
2041 }
2042
2043 case Instruction::INT_TO_BYTE: {
2044 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2045 break;
2046 }
2047
2048 case Instruction::INT_TO_SHORT: {
2049 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2050 break;
2051 }
2052
2053 case Instruction::INT_TO_CHAR: {
2054 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2055 break;
2056 }
2057
2058 case Instruction::ADD_INT: {
2059 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2060 break;
2061 }
2062
2063 case Instruction::ADD_LONG: {
2064 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2065 break;
2066 }
2067
2068 case Instruction::ADD_DOUBLE: {
2069 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2070 break;
2071 }
2072
2073 case Instruction::ADD_FLOAT: {
2074 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2075 break;
2076 }
2077
2078 case Instruction::SUB_INT: {
2079 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2080 break;
2081 }
2082
2083 case Instruction::SUB_LONG: {
2084 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2085 break;
2086 }
2087
2088 case Instruction::SUB_FLOAT: {
2089 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2090 break;
2091 }
2092
2093 case Instruction::SUB_DOUBLE: {
2094 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2095 break;
2096 }
2097
2098 case Instruction::ADD_INT_2ADDR: {
2099 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2100 break;
2101 }
2102
2103 case Instruction::MUL_INT: {
2104 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2105 break;
2106 }
2107
2108 case Instruction::MUL_LONG: {
2109 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2110 break;
2111 }
2112
2113 case Instruction::MUL_FLOAT: {
2114 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2115 break;
2116 }
2117
2118 case Instruction::MUL_DOUBLE: {
2119 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2120 break;
2121 }
2122
2123 case Instruction::DIV_INT: {
2124 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2125 dex_pc, Primitive::kPrimInt, false, true);
2126 break;
2127 }
2128
2129 case Instruction::DIV_LONG: {
2130 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2131 dex_pc, Primitive::kPrimLong, false, true);
2132 break;
2133 }
2134
2135 case Instruction::DIV_FLOAT: {
2136 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2137 break;
2138 }
2139
2140 case Instruction::DIV_DOUBLE: {
2141 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2142 break;
2143 }
2144
2145 case Instruction::REM_INT: {
2146 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2147 dex_pc, Primitive::kPrimInt, false, false);
2148 break;
2149 }
2150
2151 case Instruction::REM_LONG: {
2152 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2153 dex_pc, Primitive::kPrimLong, false, false);
2154 break;
2155 }
2156
2157 case Instruction::REM_FLOAT: {
2158 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2159 break;
2160 }
2161
2162 case Instruction::REM_DOUBLE: {
2163 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2164 break;
2165 }
2166
2167 case Instruction::AND_INT: {
2168 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2169 break;
2170 }
2171
2172 case Instruction::AND_LONG: {
2173 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2174 break;
2175 }
2176
2177 case Instruction::SHL_INT: {
2178 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2179 break;
2180 }
2181
2182 case Instruction::SHL_LONG: {
2183 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2184 break;
2185 }
2186
2187 case Instruction::SHR_INT: {
2188 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2189 break;
2190 }
2191
2192 case Instruction::SHR_LONG: {
2193 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2194 break;
2195 }
2196
2197 case Instruction::USHR_INT: {
2198 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2199 break;
2200 }
2201
2202 case Instruction::USHR_LONG: {
2203 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2204 break;
2205 }
2206
2207 case Instruction::OR_INT: {
2208 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2209 break;
2210 }
2211
2212 case Instruction::OR_LONG: {
2213 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2214 break;
2215 }
2216
2217 case Instruction::XOR_INT: {
2218 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2219 break;
2220 }
2221
2222 case Instruction::XOR_LONG: {
2223 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2224 break;
2225 }
2226
2227 case Instruction::ADD_LONG_2ADDR: {
2228 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2229 break;
2230 }
2231
2232 case Instruction::ADD_DOUBLE_2ADDR: {
2233 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2234 break;
2235 }
2236
2237 case Instruction::ADD_FLOAT_2ADDR: {
2238 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2239 break;
2240 }
2241
2242 case Instruction::SUB_INT_2ADDR: {
2243 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2244 break;
2245 }
2246
2247 case Instruction::SUB_LONG_2ADDR: {
2248 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2249 break;
2250 }
2251
2252 case Instruction::SUB_FLOAT_2ADDR: {
2253 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2254 break;
2255 }
2256
2257 case Instruction::SUB_DOUBLE_2ADDR: {
2258 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2259 break;
2260 }
2261
2262 case Instruction::MUL_INT_2ADDR: {
2263 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2264 break;
2265 }
2266
2267 case Instruction::MUL_LONG_2ADDR: {
2268 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2269 break;
2270 }
2271
2272 case Instruction::MUL_FLOAT_2ADDR: {
2273 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2274 break;
2275 }
2276
2277 case Instruction::MUL_DOUBLE_2ADDR: {
2278 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2279 break;
2280 }
2281
2282 case Instruction::DIV_INT_2ADDR: {
2283 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2284 dex_pc, Primitive::kPrimInt, false, true);
2285 break;
2286 }
2287
2288 case Instruction::DIV_LONG_2ADDR: {
2289 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2290 dex_pc, Primitive::kPrimLong, false, true);
2291 break;
2292 }
2293
2294 case Instruction::REM_INT_2ADDR: {
2295 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2296 dex_pc, Primitive::kPrimInt, false, false);
2297 break;
2298 }
2299
2300 case Instruction::REM_LONG_2ADDR: {
2301 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2302 dex_pc, Primitive::kPrimLong, false, false);
2303 break;
2304 }
2305
2306 case Instruction::REM_FLOAT_2ADDR: {
2307 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2308 break;
2309 }
2310
2311 case Instruction::REM_DOUBLE_2ADDR: {
2312 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2313 break;
2314 }
2315
2316 case Instruction::SHL_INT_2ADDR: {
2317 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2318 break;
2319 }
2320
2321 case Instruction::SHL_LONG_2ADDR: {
2322 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2323 break;
2324 }
2325
2326 case Instruction::SHR_INT_2ADDR: {
2327 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2328 break;
2329 }
2330
2331 case Instruction::SHR_LONG_2ADDR: {
2332 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2333 break;
2334 }
2335
2336 case Instruction::USHR_INT_2ADDR: {
2337 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2338 break;
2339 }
2340
2341 case Instruction::USHR_LONG_2ADDR: {
2342 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2343 break;
2344 }
2345
2346 case Instruction::DIV_FLOAT_2ADDR: {
2347 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2348 break;
2349 }
2350
2351 case Instruction::DIV_DOUBLE_2ADDR: {
2352 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2353 break;
2354 }
2355
2356 case Instruction::AND_INT_2ADDR: {
2357 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2358 break;
2359 }
2360
2361 case Instruction::AND_LONG_2ADDR: {
2362 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2363 break;
2364 }
2365
2366 case Instruction::OR_INT_2ADDR: {
2367 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2368 break;
2369 }
2370
2371 case Instruction::OR_LONG_2ADDR: {
2372 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2373 break;
2374 }
2375
2376 case Instruction::XOR_INT_2ADDR: {
2377 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2378 break;
2379 }
2380
2381 case Instruction::XOR_LONG_2ADDR: {
2382 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2383 break;
2384 }
2385
2386 case Instruction::ADD_INT_LIT16: {
2387 Binop_22s<HAdd>(instruction, false, dex_pc);
2388 break;
2389 }
2390
2391 case Instruction::AND_INT_LIT16: {
2392 Binop_22s<HAnd>(instruction, false, dex_pc);
2393 break;
2394 }
2395
2396 case Instruction::OR_INT_LIT16: {
2397 Binop_22s<HOr>(instruction, false, dex_pc);
2398 break;
2399 }
2400
2401 case Instruction::XOR_INT_LIT16: {
2402 Binop_22s<HXor>(instruction, false, dex_pc);
2403 break;
2404 }
2405
2406 case Instruction::RSUB_INT: {
2407 Binop_22s<HSub>(instruction, true, dex_pc);
2408 break;
2409 }
2410
2411 case Instruction::MUL_INT_LIT16: {
2412 Binop_22s<HMul>(instruction, false, dex_pc);
2413 break;
2414 }
2415
2416 case Instruction::ADD_INT_LIT8: {
2417 Binop_22b<HAdd>(instruction, false, dex_pc);
2418 break;
2419 }
2420
2421 case Instruction::AND_INT_LIT8: {
2422 Binop_22b<HAnd>(instruction, false, dex_pc);
2423 break;
2424 }
2425
2426 case Instruction::OR_INT_LIT8: {
2427 Binop_22b<HOr>(instruction, false, dex_pc);
2428 break;
2429 }
2430
2431 case Instruction::XOR_INT_LIT8: {
2432 Binop_22b<HXor>(instruction, false, dex_pc);
2433 break;
2434 }
2435
2436 case Instruction::RSUB_INT_LIT8: {
2437 Binop_22b<HSub>(instruction, true, dex_pc);
2438 break;
2439 }
2440
2441 case Instruction::MUL_INT_LIT8: {
2442 Binop_22b<HMul>(instruction, false, dex_pc);
2443 break;
2444 }
2445
2446 case Instruction::DIV_INT_LIT16:
2447 case Instruction::DIV_INT_LIT8: {
2448 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2449 dex_pc, Primitive::kPrimInt, true, true);
2450 break;
2451 }
2452
2453 case Instruction::REM_INT_LIT16:
2454 case Instruction::REM_INT_LIT8: {
2455 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2456 dex_pc, Primitive::kPrimInt, true, false);
2457 break;
2458 }
2459
2460 case Instruction::SHL_INT_LIT8: {
2461 Binop_22b<HShl>(instruction, false, dex_pc);
2462 break;
2463 }
2464
2465 case Instruction::SHR_INT_LIT8: {
2466 Binop_22b<HShr>(instruction, false, dex_pc);
2467 break;
2468 }
2469
2470 case Instruction::USHR_INT_LIT8: {
2471 Binop_22b<HUShr>(instruction, false, dex_pc);
2472 break;
2473 }
2474
2475 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002476 if (!BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +00002477 return false;
2478 }
2479 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2480 break;
2481 }
2482
2483 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002484 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002485 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00002486 HLoadClass* cls = BuildLoadClass(type_index, dex_pc, /* check_access */ true);
2487 AppendInstruction(new (arena_) HNewArray(cls, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002488 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2489 break;
2490 }
2491
2492 case Instruction::FILLED_NEW_ARRAY: {
2493 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002494 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002495 uint32_t args[5];
2496 instruction.GetVarArgs(args);
2497 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2498 break;
2499 }
2500
2501 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2502 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002503 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002504 uint32_t register_index = instruction.VRegC_3rc();
2505 BuildFilledNewArray(
2506 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2507 break;
2508 }
2509
2510 case Instruction::FILL_ARRAY_DATA: {
2511 BuildFillArrayData(instruction, dex_pc);
2512 break;
2513 }
2514
2515 case Instruction::MOVE_RESULT:
2516 case Instruction::MOVE_RESULT_WIDE:
2517 case Instruction::MOVE_RESULT_OBJECT: {
2518 DCHECK(latest_result_ != nullptr);
2519 UpdateLocal(instruction.VRegA(), latest_result_);
2520 latest_result_ = nullptr;
2521 break;
2522 }
2523
2524 case Instruction::CMP_LONG: {
2525 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2526 break;
2527 }
2528
2529 case Instruction::CMPG_FLOAT: {
2530 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2531 break;
2532 }
2533
2534 case Instruction::CMPG_DOUBLE: {
2535 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2536 break;
2537 }
2538
2539 case Instruction::CMPL_FLOAT: {
2540 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2541 break;
2542 }
2543
2544 case Instruction::CMPL_DOUBLE: {
2545 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2546 break;
2547 }
2548
2549 case Instruction::NOP:
2550 break;
2551
2552 case Instruction::IGET:
2553 case Instruction::IGET_QUICK:
2554 case Instruction::IGET_WIDE:
2555 case Instruction::IGET_WIDE_QUICK:
2556 case Instruction::IGET_OBJECT:
2557 case Instruction::IGET_OBJECT_QUICK:
2558 case Instruction::IGET_BOOLEAN:
2559 case Instruction::IGET_BOOLEAN_QUICK:
2560 case Instruction::IGET_BYTE:
2561 case Instruction::IGET_BYTE_QUICK:
2562 case Instruction::IGET_CHAR:
2563 case Instruction::IGET_CHAR_QUICK:
2564 case Instruction::IGET_SHORT:
2565 case Instruction::IGET_SHORT_QUICK: {
2566 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2567 return false;
2568 }
2569 break;
2570 }
2571
2572 case Instruction::IPUT:
2573 case Instruction::IPUT_QUICK:
2574 case Instruction::IPUT_WIDE:
2575 case Instruction::IPUT_WIDE_QUICK:
2576 case Instruction::IPUT_OBJECT:
2577 case Instruction::IPUT_OBJECT_QUICK:
2578 case Instruction::IPUT_BOOLEAN:
2579 case Instruction::IPUT_BOOLEAN_QUICK:
2580 case Instruction::IPUT_BYTE:
2581 case Instruction::IPUT_BYTE_QUICK:
2582 case Instruction::IPUT_CHAR:
2583 case Instruction::IPUT_CHAR_QUICK:
2584 case Instruction::IPUT_SHORT:
2585 case Instruction::IPUT_SHORT_QUICK: {
2586 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2587 return false;
2588 }
2589 break;
2590 }
2591
2592 case Instruction::SGET:
2593 case Instruction::SGET_WIDE:
2594 case Instruction::SGET_OBJECT:
2595 case Instruction::SGET_BOOLEAN:
2596 case Instruction::SGET_BYTE:
2597 case Instruction::SGET_CHAR:
2598 case Instruction::SGET_SHORT: {
2599 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2600 return false;
2601 }
2602 break;
2603 }
2604
2605 case Instruction::SPUT:
2606 case Instruction::SPUT_WIDE:
2607 case Instruction::SPUT_OBJECT:
2608 case Instruction::SPUT_BOOLEAN:
2609 case Instruction::SPUT_BYTE:
2610 case Instruction::SPUT_CHAR:
2611 case Instruction::SPUT_SHORT: {
2612 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2613 return false;
2614 }
2615 break;
2616 }
2617
2618#define ARRAY_XX(kind, anticipated_type) \
2619 case Instruction::AGET##kind: { \
2620 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2621 break; \
2622 } \
2623 case Instruction::APUT##kind: { \
2624 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2625 break; \
2626 }
2627
2628 ARRAY_XX(, Primitive::kPrimInt);
2629 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2630 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2631 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2632 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2633 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2634 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2635
2636 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002637 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002638 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2639 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2640 break;
2641 }
2642
2643 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002644 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002645 AppendInstruction(
2646 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2647 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2648 break;
2649 }
2650
2651 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002652 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002653 AppendInstruction(
2654 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2655 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2656 break;
2657 }
2658
2659 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002660 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002661 BuildLoadClass(type_index, dex_pc, /* check_access */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002662 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2663 break;
2664 }
2665
2666 case Instruction::MOVE_EXCEPTION: {
2667 AppendInstruction(new (arena_) HLoadException(dex_pc));
2668 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2669 AppendInstruction(new (arena_) HClearException(dex_pc));
2670 break;
2671 }
2672
2673 case Instruction::THROW: {
2674 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2675 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2676 // We finished building this block. Set the current block to null to avoid
2677 // adding dead instructions to it.
2678 current_block_ = nullptr;
2679 break;
2680 }
2681
2682 case Instruction::INSTANCE_OF: {
2683 uint8_t destination = instruction.VRegA_22c();
2684 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002685 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002686 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2687 break;
2688 }
2689
2690 case Instruction::CHECK_CAST: {
2691 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002692 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002693 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2694 break;
2695 }
2696
2697 case Instruction::MONITOR_ENTER: {
2698 AppendInstruction(new (arena_) HMonitorOperation(
2699 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2700 HMonitorOperation::OperationKind::kEnter,
2701 dex_pc));
2702 break;
2703 }
2704
2705 case Instruction::MONITOR_EXIT: {
2706 AppendInstruction(new (arena_) HMonitorOperation(
2707 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2708 HMonitorOperation::OperationKind::kExit,
2709 dex_pc));
2710 break;
2711 }
2712
2713 case Instruction::SPARSE_SWITCH:
2714 case Instruction::PACKED_SWITCH: {
2715 BuildSwitch(instruction, dex_pc);
2716 break;
2717 }
2718
2719 default:
2720 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002721 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002722 << " because of unhandled instruction "
2723 << instruction.Name();
2724 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2725 return false;
2726 }
2727 return true;
2728} // NOLINT(readability/fn_size)
2729
Vladimir Markoec786222016-12-20 16:24:13 +00002730ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2731 dex::TypeIndex type_index,
2732 const DexCompilationUnit& compilation_unit) const {
2733 return ClassLinker::LookupResolvedType(
2734 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2735}
2736
2737ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2738 // TODO: Cache the result in a Handle<mirror::Class>.
2739 const DexFile::MethodId& method_id =
2740 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2741 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2742}
2743
David Brazdildee58d62016-04-07 09:54:26 +00002744} // namespace art