blob: 5a75a018678a842be7a8a70e585dd650f23e56ce [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());
671 StackHandleScope<3> hs(soa.Self());
672
673 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
674 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700675 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +0000676 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100677 // We fetch the referenced class eagerly (that is, the class pointed by in the MethodId
678 // at method_idx), as `CanAccessResolvedMethod` expects it be be in the dex cache.
679 Handle<mirror::Class> methods_class(hs.NewHandle(class_linker->ResolveReferencedClassOfMethod(
680 method_idx, dex_compilation_unit_->GetDexCache(), class_loader)));
681
682 if (UNLIKELY(methods_class.Get() == nullptr)) {
683 // Clean up any exception left by type resolution.
684 soa.Self()->ClearException();
685 return nullptr;
686 }
David Brazdildee58d62016-04-07 09:54:26 +0000687
688 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
689 *dex_compilation_unit_->GetDexFile(),
690 method_idx,
691 dex_compilation_unit_->GetDexCache(),
692 class_loader,
693 /* referrer */ nullptr,
694 invoke_type);
695
696 if (UNLIKELY(resolved_method == nullptr)) {
697 // Clean up any exception left by type resolution.
698 soa.Self()->ClearException();
699 return nullptr;
700 }
701
702 // Check access. The class linker has a fast path for looking into the dex cache
703 // and does not check the access if it hits it.
704 if (compiling_class.Get() == nullptr) {
705 if (!resolved_method->IsPublic()) {
706 return nullptr;
707 }
708 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
709 resolved_method,
710 dex_compilation_unit_->GetDexCache().Get(),
711 method_idx)) {
712 return nullptr;
713 }
714
715 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
716 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
717 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
718 // which require runtime handling.
719 if (invoke_type == kSuper) {
720 if (compiling_class.Get() == nullptr) {
721 // We could not determine the method's class we need to wait until runtime.
722 DCHECK(Runtime::Current()->IsAotCompiler());
723 return nullptr;
724 }
Aart Bikf663e342016-04-04 17:28:59 -0700725 if (!methods_class->IsAssignableFrom(compiling_class.Get())) {
726 // We cannot statically determine the target method. The runtime will throw a
727 // NoSuchMethodError on this one.
728 return nullptr;
729 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100730 ArtMethod* actual_method;
731 if (methods_class->IsInterface()) {
732 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
733 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000734 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100735 uint16_t vtable_index = resolved_method->GetMethodIndex();
736 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
737 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000738 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100739 if (actual_method != resolved_method &&
740 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
741 // The back-end code generator relies on this check in order to ensure that it will not
742 // attempt to read the dex_cache with a dex_method_index that is not from the correct
743 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
744 // builder, which means that the code-generator (and compiler driver during sharpening and
745 // inliner, maybe) might invoke an incorrect method.
746 // TODO: The actual method could still be referenced in the current dex file, so we
747 // could try locating it.
748 // TODO: Remove the dex_file restriction.
749 return nullptr;
750 }
751 if (!actual_method->IsInvokable()) {
752 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
753 // could resolve the callee to the wrong method.
754 return nullptr;
755 }
756 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000757 }
758
759 // Check for incompatible class changes. The class linker has a fast path for
760 // looking into the dex cache and does not check incompatible class changes if it hits it.
761 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
762 return nullptr;
763 }
764
765 return resolved_method;
766}
767
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100768static bool IsStringConstructor(ArtMethod* method) {
769 ScopedObjectAccess soa(Thread::Current());
770 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
771}
772
David Brazdildee58d62016-04-07 09:54:26 +0000773bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
774 uint32_t dex_pc,
775 uint32_t method_idx,
776 uint32_t number_of_vreg_arguments,
777 bool is_range,
778 uint32_t* args,
779 uint32_t register_index) {
780 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
781 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
782 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
783
784 // Remove the return type from the 'proto'.
785 size_t number_of_arguments = strlen(descriptor) - 1;
786 if (invoke_type != kStatic) { // instance call
787 // One extra argument for 'this'.
788 number_of_arguments++;
789 }
790
David Brazdildee58d62016-04-07 09:54:26 +0000791 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
792
793 if (UNLIKELY(resolved_method == nullptr)) {
794 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
795 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
796 number_of_arguments,
797 return_type,
798 dex_pc,
799 method_idx,
800 invoke_type);
801 return HandleInvoke(invoke,
802 number_of_vreg_arguments,
803 args,
804 register_index,
805 is_range,
806 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700807 nullptr, /* clinit_check */
808 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000809 }
810
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100811 // Replace calls to String.<init> with StringFactory.
812 if (IsStringConstructor(resolved_method)) {
813 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
814 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
815 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
816 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000817 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100818 };
819 MethodReference target_method(dex_file_, method_idx);
820 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
821 arena_,
822 number_of_arguments - 1,
823 Primitive::kPrimNot /*return_type */,
824 dex_pc,
825 method_idx,
826 nullptr,
827 dispatch_info,
828 invoke_type,
829 target_method,
830 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
831 return HandleStringInit(invoke,
832 number_of_vreg_arguments,
833 args,
834 register_index,
835 is_range,
836 descriptor);
837 }
838
David Brazdildee58d62016-04-07 09:54:26 +0000839 // Potential class initialization check, in the case of a static method call.
840 HClinitCheck* clinit_check = nullptr;
841 HInvoke* invoke = nullptr;
842 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
843 // By default, consider that the called method implicitly requires
844 // an initialization check of its declaring method.
845 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
846 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
847 ScopedObjectAccess soa(Thread::Current());
848 if (invoke_type == kStatic) {
849 clinit_check = ProcessClinitCheckForInvoke(
850 dex_pc, resolved_method, method_idx, &clinit_check_requirement);
851 } else if (invoke_type == kSuper) {
852 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100853 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000854 // we resolved to the method referenced by the instruction.
855 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000856 }
857 }
858
859 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
860 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
861 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000862 0u
David Brazdildee58d62016-04-07 09:54:26 +0000863 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100864 MethodReference target_method(resolved_method->GetDexFile(),
865 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000866 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
867 number_of_arguments,
868 return_type,
869 dex_pc,
870 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100871 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000872 dispatch_info,
873 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100874 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000875 clinit_check_requirement);
876 } else if (invoke_type == kVirtual) {
877 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
878 invoke = new (arena_) HInvokeVirtual(arena_,
879 number_of_arguments,
880 return_type,
881 dex_pc,
882 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100883 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000884 resolved_method->GetMethodIndex());
885 } else {
886 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100887 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000888 invoke = new (arena_) HInvokeInterface(arena_,
889 number_of_arguments,
890 return_type,
891 dex_pc,
892 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100893 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700894 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000895 }
896
897 return HandleInvoke(invoke,
898 number_of_vreg_arguments,
899 args,
900 register_index,
901 is_range,
902 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700903 clinit_check,
904 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000905}
906
Andreas Gampea5b09a62016-11-17 15:21:22 -0800907bool HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100908 ScopedObjectAccess soa(Thread::Current());
909 StackHandleScope<1> hs(soa.Self());
910 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
911 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
912 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
913 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
914
David Brazdildee58d62016-04-07 09:54:26 +0000915 bool finalizable;
Mingyao Yang062157f2016-03-02 10:15:36 -0800916 bool needs_access_check = NeedsAccessCheck(type_index, dex_cache, &finalizable);
David Brazdildee58d62016-04-07 09:54:26 +0000917
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000918 // Only the non-resolved entrypoint handles the finalizable class case. If we
David Brazdildee58d62016-04-07 09:54:26 +0000919 // need access checks, then we haven't resolved the method and the class may
920 // again be finalizable.
Mingyao Yang062157f2016-03-02 10:15:36 -0800921 QuickEntrypointEnum entrypoint = (finalizable || needs_access_check)
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000922 ? kQuickAllocObject
David Brazdildee58d62016-04-07 09:54:26 +0000923 : kQuickAllocObjectInitialized;
924
David Brazdildee58d62016-04-07 09:54:26 +0000925 if (outer_dex_cache.Get() != dex_cache.Get()) {
926 // We currently do not support inlining allocations across dex files.
927 return false;
928 }
929
930 HLoadClass* load_class = new (arena_) HLoadClass(
931 graph_->GetCurrentMethod(),
932 type_index,
933 outer_dex_file,
934 IsOutermostCompilingClass(type_index),
935 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +0000936 needs_access_check);
David Brazdildee58d62016-04-07 09:54:26 +0000937
938 AppendInstruction(load_class);
939 HInstruction* cls = load_class;
940 if (!IsInitialized(resolved_class)) {
941 cls = new (arena_) HClinitCheck(load_class, dex_pc);
942 AppendInstruction(cls);
943 }
944
945 AppendInstruction(new (arena_) HNewInstance(
946 cls,
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000947 graph_->GetCurrentMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000948 dex_pc,
949 type_index,
950 *dex_compilation_unit_->GetDexFile(),
Mingyao Yang062157f2016-03-02 10:15:36 -0800951 needs_access_check,
David Brazdildee58d62016-04-07 09:54:26 +0000952 finalizable,
953 entrypoint));
954 return true;
955}
956
957static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700958 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000959 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
960}
961
962bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
963 if (cls.Get() == nullptr) {
964 return false;
965 }
966
967 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
968 // check whether the class is in an image for the AOT compilation.
969 if (cls->IsInitialized() &&
970 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
971 return true;
972 }
973
974 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
975 return true;
976 }
977
978 // TODO: We should walk over the inlined methods, but we don't pass
979 // that information to the builder.
980 if (IsSubClass(GetCompilingClass(), cls.Get())) {
981 return true;
982 }
983
984 return false;
985}
986
987HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
988 uint32_t dex_pc,
989 ArtMethod* resolved_method,
990 uint32_t method_idx,
991 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
992 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
993 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100994 StackHandleScope<2> hs(self);
995 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
996 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +0000997 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
998 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
999
1000 // The index at which the method's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001001 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001002 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1003 if (is_outer_class) {
1004 storage_index = outer_class->GetDexTypeIndex();
1005 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1006 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1007 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1008 GetCompilingClass(),
1009 resolved_method,
1010 method_idx,
1011 &storage_index);
1012 }
1013
1014 HClinitCheck* clinit_check = nullptr;
1015
1016 if (IsInitialized(resolved_method_class)) {
1017 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001018 } else if (storage_index.IsValid()) {
David Brazdildee58d62016-04-07 09:54:26 +00001019 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1020 HLoadClass* load_class = new (arena_) HLoadClass(
1021 graph_->GetCurrentMethod(),
1022 storage_index,
1023 outer_dex_file,
1024 is_outer_class,
1025 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001026 /*needs_access_check*/ false);
David Brazdildee58d62016-04-07 09:54:26 +00001027 AppendInstruction(load_class);
1028 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1029 AppendInstruction(clinit_check);
1030 }
1031 return clinit_check;
1032}
1033
1034bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1035 uint32_t number_of_vreg_arguments,
1036 uint32_t* args,
1037 uint32_t register_index,
1038 bool is_range,
1039 const char* descriptor,
1040 size_t start_index,
1041 size_t* argument_index) {
1042 uint32_t descriptor_index = 1; // Skip the return type.
1043
1044 for (size_t i = start_index;
1045 // Make sure we don't go over the expected arguments or over the number of
1046 // dex registers given. If the instruction was seen as dead by the verifier,
1047 // it hasn't been properly checked.
1048 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1049 i++, (*argument_index)++) {
1050 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1051 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1052 if (!is_range
1053 && is_wide
1054 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1055 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1056 // reject any class where this is violated. However, the verifier only does these checks
1057 // on non trivially dead instructions, so we just bailout the compilation.
1058 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001059 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001060 << " because of non-sequential dex register pair in wide argument";
1061 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1062 return false;
1063 }
1064 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1065 invoke->SetArgumentAt(*argument_index, arg);
1066 if (is_wide) {
1067 i++;
1068 }
1069 }
1070
1071 if (*argument_index != invoke->GetNumberOfArguments()) {
1072 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001073 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001074 << " because of wrong number of arguments in invoke instruction";
1075 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1076 return false;
1077 }
1078
1079 if (invoke->IsInvokeStaticOrDirect() &&
1080 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1081 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1082 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1083 (*argument_index)++;
1084 }
1085
1086 return true;
1087}
1088
1089bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1090 uint32_t number_of_vreg_arguments,
1091 uint32_t* args,
1092 uint32_t register_index,
1093 bool is_range,
1094 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001095 HClinitCheck* clinit_check,
1096 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001097 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1098
1099 size_t start_index = 0;
1100 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001101 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001102 uint32_t obj_reg = is_range ? register_index : args[0];
1103 HInstruction* arg = is_unresolved
1104 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1105 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001106 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001107 start_index = 1;
1108 argument_index = 1;
1109 }
1110
1111 if (!SetupInvokeArguments(invoke,
1112 number_of_vreg_arguments,
1113 args,
1114 register_index,
1115 is_range,
1116 descriptor,
1117 start_index,
1118 &argument_index)) {
1119 return false;
1120 }
1121
1122 if (clinit_check != nullptr) {
1123 // Add the class initialization check as last input of `invoke`.
1124 DCHECK(invoke->IsInvokeStaticOrDirect());
1125 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1126 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1127 invoke->SetArgumentAt(argument_index, clinit_check);
1128 argument_index++;
1129 }
1130
1131 AppendInstruction(invoke);
1132 latest_result_ = invoke;
1133
1134 return true;
1135}
1136
1137bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1138 uint32_t number_of_vreg_arguments,
1139 uint32_t* args,
1140 uint32_t register_index,
1141 bool is_range,
1142 const char* descriptor) {
1143 DCHECK(invoke->IsInvokeStaticOrDirect());
1144 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1145
1146 size_t start_index = 1;
1147 size_t argument_index = 0;
1148 if (!SetupInvokeArguments(invoke,
1149 number_of_vreg_arguments,
1150 args,
1151 register_index,
1152 is_range,
1153 descriptor,
1154 start_index,
1155 &argument_index)) {
1156 return false;
1157 }
1158
1159 AppendInstruction(invoke);
1160
1161 // This is a StringFactory call, not an actual String constructor. Its result
1162 // replaces the empty String pre-allocated by NewInstance.
1163 uint32_t orig_this_reg = is_range ? register_index : args[0];
1164 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1165
1166 // Replacing the NewInstance might render it redundant. Keep a list of these
1167 // to be visited once it is clear whether it is has remaining uses.
1168 if (arg_this->IsNewInstance()) {
1169 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1170 } else {
1171 DCHECK(arg_this->IsPhi());
1172 // NewInstance is not the direct input of the StringFactory call. It might
1173 // be redundant but optimizing this case is not worth the effort.
1174 }
1175
1176 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1177 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1178 if ((*current_locals_)[vreg] == arg_this) {
1179 (*current_locals_)[vreg] = invoke;
1180 }
1181 }
1182
1183 return true;
1184}
1185
1186static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1187 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1188 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1189 return Primitive::GetType(type[0]);
1190}
1191
1192bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1193 uint32_t dex_pc,
1194 bool is_put) {
1195 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1196 uint32_t obj_reg = instruction.VRegB_22c();
1197 uint16_t field_index;
1198 if (instruction.IsQuickened()) {
1199 if (!CanDecodeQuickenedInfo()) {
1200 return false;
1201 }
1202 field_index = LookupQuickenedInfo(dex_pc);
1203 } else {
1204 field_index = instruction.VRegC_22c();
1205 }
1206
1207 ScopedObjectAccess soa(Thread::Current());
1208 ArtField* resolved_field =
1209 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1210
1211
Aart Bik14154132016-06-02 17:53:58 -07001212 // Generate an explicit null check on the reference, unless the field access
1213 // is unresolved. In that case, we rely on the runtime to perform various
1214 // checks first, followed by a null check.
1215 HInstruction* object = (resolved_field == nullptr)
1216 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1217 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001218
1219 Primitive::Type field_type = (resolved_field == nullptr)
1220 ? GetFieldAccessType(*dex_file_, field_index)
1221 : resolved_field->GetTypeAsPrimitiveType();
1222 if (is_put) {
1223 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1224 HInstruction* field_set = nullptr;
1225 if (resolved_field == nullptr) {
1226 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001227 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001228 value,
1229 field_type,
1230 field_index,
1231 dex_pc);
1232 } else {
1233 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001234 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001235 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001236 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001237 field_type,
1238 resolved_field->GetOffset(),
1239 resolved_field->IsVolatile(),
1240 field_index,
1241 class_def_index,
1242 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001243 dex_pc);
1244 }
1245 AppendInstruction(field_set);
1246 } else {
1247 HInstruction* field_get = nullptr;
1248 if (resolved_field == nullptr) {
1249 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001250 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001251 field_type,
1252 field_index,
1253 dex_pc);
1254 } else {
1255 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001256 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001257 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001258 field_type,
1259 resolved_field->GetOffset(),
1260 resolved_field->IsVolatile(),
1261 field_index,
1262 class_def_index,
1263 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001264 dex_pc);
1265 }
1266 AppendInstruction(field_get);
1267 UpdateLocal(source_or_dest_reg, field_get);
1268 }
1269
1270 return true;
1271}
1272
1273static mirror::Class* GetClassFrom(CompilerDriver* driver,
1274 const DexCompilationUnit& compilation_unit) {
1275 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001276 StackHandleScope<1> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +00001277 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001278 soa.Decode<mirror::ClassLoader>(compilation_unit.GetClassLoader())));
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001279 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001280
1281 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1282}
1283
1284mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1285 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1286}
1287
1288mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1289 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1290}
1291
Andreas Gampea5b09a62016-11-17 15:21:22 -08001292bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001293 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001294 StackHandleScope<3> hs(soa.Self());
1295 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001296 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001297 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001298 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1299 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1300 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1301
1302 // GetOutermostCompilingClass returns null when the class is unresolved
1303 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1304 // we are compiling it.
1305 // When this happens we cannot establish a direct relation between the current
1306 // class and the outer class, so we return false.
1307 // (Note that this is only used for optimizing invokes and field accesses)
1308 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1309}
1310
1311void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001312 uint32_t dex_pc,
1313 bool is_put,
1314 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001315 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1316 uint16_t field_index = instruction.VRegB_21c();
1317
1318 if (is_put) {
1319 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1320 AppendInstruction(
1321 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1322 } else {
1323 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1324 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1325 }
1326}
1327
1328bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1329 uint32_t dex_pc,
1330 bool is_put) {
1331 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1332 uint16_t field_index = instruction.VRegB_21c();
1333
1334 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001335 StackHandleScope<3> hs(soa.Self());
1336 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001337 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001338 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001339 ArtField* resolved_field = compiler_driver_->ResolveField(
1340 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1341
1342 if (resolved_field == nullptr) {
1343 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1344 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1345 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1346 return true;
1347 }
1348
1349 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
1350 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001351 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001352 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1353
1354 // The index at which the field's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001355 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001356 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1357 if (is_outer_class) {
1358 storage_index = outer_class->GetDexTypeIndex();
1359 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1360 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1361 return false;
1362 } else {
1363 // TODO: This is rather expensive. Perf it and cache the results if needed.
1364 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1365 outer_dex_cache.Get(),
1366 GetCompilingClass(),
1367 resolved_field,
1368 field_index,
1369 &storage_index);
1370 bool can_easily_access = is_put ? pair.second : pair.first;
1371 if (!can_easily_access) {
1372 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1373 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1374 return true;
1375 }
1376 }
1377
David Brazdildee58d62016-04-07 09:54:26 +00001378 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1379 storage_index,
1380 outer_dex_file,
1381 is_outer_class,
1382 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001383 /*needs_access_check*/ false);
David Brazdildee58d62016-04-07 09:54:26 +00001384 AppendInstruction(constant);
1385
1386 HInstruction* cls = constant;
1387
1388 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1389 if (!IsInitialized(klass)) {
1390 cls = new (arena_) HClinitCheck(constant, dex_pc);
1391 AppendInstruction(cls);
1392 }
1393
1394 uint16_t class_def_index = klass->GetDexClassDefIndex();
1395 if (is_put) {
1396 // We need to keep the class alive before loading the value.
1397 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1398 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1399 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1400 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001401 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001402 field_type,
1403 resolved_field->GetOffset(),
1404 resolved_field->IsVolatile(),
1405 field_index,
1406 class_def_index,
1407 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001408 dex_pc));
1409 } else {
1410 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001411 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001412 field_type,
1413 resolved_field->GetOffset(),
1414 resolved_field->IsVolatile(),
1415 field_index,
1416 class_def_index,
1417 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001418 dex_pc));
1419 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1420 }
1421 return true;
1422}
1423
1424void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1425 uint16_t first_vreg,
1426 int64_t second_vreg_or_constant,
1427 uint32_t dex_pc,
1428 Primitive::Type type,
1429 bool second_is_constant,
1430 bool isDiv) {
1431 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1432
1433 HInstruction* first = LoadLocal(first_vreg, type);
1434 HInstruction* second = nullptr;
1435 if (second_is_constant) {
1436 if (type == Primitive::kPrimInt) {
1437 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1438 } else {
1439 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1440 }
1441 } else {
1442 second = LoadLocal(second_vreg_or_constant, type);
1443 }
1444
1445 if (!second_is_constant
1446 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1447 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1448 second = new (arena_) HDivZeroCheck(second, dex_pc);
1449 AppendInstruction(second);
1450 }
1451
1452 if (isDiv) {
1453 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1454 } else {
1455 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1456 }
1457 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1458}
1459
1460void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1461 uint32_t dex_pc,
1462 bool is_put,
1463 Primitive::Type anticipated_type) {
1464 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1465 uint8_t array_reg = instruction.VRegB_23x();
1466 uint8_t index_reg = instruction.VRegC_23x();
1467
David Brazdilc120bbe2016-04-22 16:57:00 +01001468 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001469 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1470 AppendInstruction(length);
1471 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1472 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1473 AppendInstruction(index);
1474 if (is_put) {
1475 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1476 // TODO: Insert a type check node if the type is Object.
1477 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1478 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1479 AppendInstruction(aset);
1480 } else {
1481 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1482 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1483 AppendInstruction(aget);
1484 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1485 }
1486 graph_->SetHasBoundsChecks(true);
1487}
1488
1489void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001490 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001491 uint32_t number_of_vreg_arguments,
1492 bool is_range,
1493 uint32_t* args,
1494 uint32_t register_index) {
1495 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
1496 bool finalizable;
1497 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
1498 ? kQuickAllocArrayWithAccessCheck
1499 : kQuickAllocArray;
1500 HInstruction* object = new (arena_) HNewArray(length,
1501 graph_->GetCurrentMethod(),
1502 dex_pc,
1503 type_index,
1504 *dex_compilation_unit_->GetDexFile(),
1505 entrypoint);
1506 AppendInstruction(object);
1507
1508 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1509 DCHECK_EQ(descriptor[0], '[') << descriptor;
1510 char primitive = descriptor[1];
1511 DCHECK(primitive == 'I'
1512 || primitive == 'L'
1513 || primitive == '[') << descriptor;
1514 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1515 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1516
1517 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1518 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1519 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1520 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1521 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1522 AppendInstruction(aset);
1523 }
1524 latest_result_ = object;
1525}
1526
1527template <typename T>
1528void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1529 const T* data,
1530 uint32_t element_count,
1531 Primitive::Type anticipated_type,
1532 uint32_t dex_pc) {
1533 for (uint32_t i = 0; i < element_count; ++i) {
1534 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1535 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1536 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1537 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1538 AppendInstruction(aset);
1539 }
1540}
1541
1542void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001543 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001544
1545 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1546 const Instruction::ArrayDataPayload* payload =
1547 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1548 const uint8_t* data = payload->data;
1549 uint32_t element_count = payload->element_count;
1550
Vladimir Markoc69fba22016-09-06 16:49:15 +01001551 if (element_count == 0u) {
1552 // For empty payload we emit only the null check above.
1553 return;
1554 }
1555
1556 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1557 AppendInstruction(length);
1558
David Brazdildee58d62016-04-07 09:54:26 +00001559 // Implementation of this DEX instruction seems to be that the bounds check is
1560 // done before doing any stores.
1561 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1562 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1563
1564 switch (payload->element_width) {
1565 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001566 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001567 reinterpret_cast<const int8_t*>(data),
1568 element_count,
1569 Primitive::kPrimByte,
1570 dex_pc);
1571 break;
1572 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001573 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001574 reinterpret_cast<const int16_t*>(data),
1575 element_count,
1576 Primitive::kPrimShort,
1577 dex_pc);
1578 break;
1579 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001580 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001581 reinterpret_cast<const int32_t*>(data),
1582 element_count,
1583 Primitive::kPrimInt,
1584 dex_pc);
1585 break;
1586 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001587 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001588 reinterpret_cast<const int64_t*>(data),
1589 element_count,
1590 dex_pc);
1591 break;
1592 default:
1593 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1594 }
1595 graph_->SetHasBoundsChecks(true);
1596}
1597
1598void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1599 const int64_t* data,
1600 uint32_t element_count,
1601 uint32_t dex_pc) {
1602 for (uint32_t i = 0; i < element_count; ++i) {
1603 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1604 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1605 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1606 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1607 AppendInstruction(aset);
1608 }
1609}
1610
1611static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001612 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001613 if (cls.Get() == nullptr) {
1614 return TypeCheckKind::kUnresolvedCheck;
1615 } else if (cls->IsInterface()) {
1616 return TypeCheckKind::kInterfaceCheck;
1617 } else if (cls->IsArrayClass()) {
1618 if (cls->GetComponentType()->IsObjectClass()) {
1619 return TypeCheckKind::kArrayObjectCheck;
1620 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1621 return TypeCheckKind::kExactCheck;
1622 } else {
1623 return TypeCheckKind::kArrayCheck;
1624 }
1625 } else if (cls->IsFinal()) {
1626 return TypeCheckKind::kExactCheck;
1627 } else if (cls->IsAbstract()) {
1628 return TypeCheckKind::kAbstractClassCheck;
1629 } else {
1630 return TypeCheckKind::kClassHierarchyCheck;
1631 }
1632}
1633
1634void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1635 uint8_t destination,
1636 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001637 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001638 uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001639 ScopedObjectAccess soa(Thread::Current());
1640 StackHandleScope<1> hs(soa.Self());
1641 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1642 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1643 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1644
David Brazdildee58d62016-04-07 09:54:26 +00001645 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1646 dex_compilation_unit_->GetDexMethodIndex(),
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001647 dex_cache,
1648 type_index);
David Brazdildee58d62016-04-07 09:54:26 +00001649
1650 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
1651 HLoadClass* cls = new (arena_) HLoadClass(
1652 graph_->GetCurrentMethod(),
1653 type_index,
1654 dex_file,
1655 IsOutermostCompilingClass(type_index),
1656 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001657 !can_access);
David Brazdildee58d62016-04-07 09:54:26 +00001658 AppendInstruction(cls);
1659
1660 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
1661 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1662 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1663 UpdateLocal(destination, current_block_->GetLastInstruction());
1664 } else {
1665 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1666 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1667 // which may throw. If it succeeds BoundType sets the new type of `object`
1668 // for all subsequent uses.
1669 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1670 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1671 UpdateLocal(reference, current_block_->GetLastInstruction());
1672 }
1673}
1674
Andreas Gampea5b09a62016-11-17 15:21:22 -08001675bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index,
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001676 Handle<mirror::DexCache> dex_cache,
1677 bool* finalizable) const {
David Brazdildee58d62016-04-07 09:54:26 +00001678 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001679 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index, finalizable);
1680}
1681
Andreas Gampea5b09a62016-11-17 15:21:22 -08001682bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001683 ScopedObjectAccess soa(Thread::Current());
1684 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1685 return NeedsAccessCheck(type_index, dex_cache, finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001686}
1687
1688bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1689 return interpreter_metadata_ != nullptr;
1690}
1691
1692uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1693 DCHECK(interpreter_metadata_ != nullptr);
1694
1695 // First check if the info has already been decoded from `interpreter_metadata_`.
1696 auto it = skipped_interpreter_metadata_.find(dex_pc);
1697 if (it != skipped_interpreter_metadata_.end()) {
1698 // Remove the entry from the map and return the parsed info.
1699 uint16_t value_in_map = it->second;
1700 skipped_interpreter_metadata_.erase(it);
1701 return value_in_map;
1702 }
1703
1704 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1705 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1706 while (true) {
1707 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1708 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1709 DCHECK_LE(dex_pc_in_map, dex_pc);
1710
1711 if (dex_pc_in_map == dex_pc) {
1712 return value_in_map;
1713 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001714 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1715 // the same dex_pc. This is OK, because the compiler does not care about those
1716 // entries.
1717 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001718 }
1719 }
1720}
1721
1722bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1723 switch (instruction.Opcode()) {
1724 case Instruction::CONST_4: {
1725 int32_t register_index = instruction.VRegA();
1726 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1727 UpdateLocal(register_index, constant);
1728 break;
1729 }
1730
1731 case Instruction::CONST_16: {
1732 int32_t register_index = instruction.VRegA();
1733 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1734 UpdateLocal(register_index, constant);
1735 break;
1736 }
1737
1738 case Instruction::CONST: {
1739 int32_t register_index = instruction.VRegA();
1740 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1741 UpdateLocal(register_index, constant);
1742 break;
1743 }
1744
1745 case Instruction::CONST_HIGH16: {
1746 int32_t register_index = instruction.VRegA();
1747 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1748 UpdateLocal(register_index, constant);
1749 break;
1750 }
1751
1752 case Instruction::CONST_WIDE_16: {
1753 int32_t register_index = instruction.VRegA();
1754 // Get 16 bits of constant value, sign extended to 64 bits.
1755 int64_t value = instruction.VRegB_21s();
1756 value <<= 48;
1757 value >>= 48;
1758 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1759 UpdateLocal(register_index, constant);
1760 break;
1761 }
1762
1763 case Instruction::CONST_WIDE_32: {
1764 int32_t register_index = instruction.VRegA();
1765 // Get 32 bits of constant value, sign extended to 64 bits.
1766 int64_t value = instruction.VRegB_31i();
1767 value <<= 32;
1768 value >>= 32;
1769 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1770 UpdateLocal(register_index, constant);
1771 break;
1772 }
1773
1774 case Instruction::CONST_WIDE: {
1775 int32_t register_index = instruction.VRegA();
1776 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1777 UpdateLocal(register_index, constant);
1778 break;
1779 }
1780
1781 case Instruction::CONST_WIDE_HIGH16: {
1782 int32_t register_index = instruction.VRegA();
1783 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1784 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1785 UpdateLocal(register_index, constant);
1786 break;
1787 }
1788
1789 // Note that the SSA building will refine the types.
1790 case Instruction::MOVE:
1791 case Instruction::MOVE_FROM16:
1792 case Instruction::MOVE_16: {
1793 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1794 UpdateLocal(instruction.VRegA(), value);
1795 break;
1796 }
1797
1798 // Note that the SSA building will refine the types.
1799 case Instruction::MOVE_WIDE:
1800 case Instruction::MOVE_WIDE_FROM16:
1801 case Instruction::MOVE_WIDE_16: {
1802 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1803 UpdateLocal(instruction.VRegA(), value);
1804 break;
1805 }
1806
1807 case Instruction::MOVE_OBJECT:
1808 case Instruction::MOVE_OBJECT_16:
1809 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001810 // The verifier has no notion of a null type, so a move-object of constant 0
1811 // will lead to the same constant 0 in the destination register. To mimic
1812 // this behavior, we just pretend we haven't seen a type change (int to reference)
1813 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1814 // types correct.
1815 uint32_t reg_number = instruction.VRegB();
1816 HInstruction* value = (*current_locals_)[reg_number];
1817 if (value->IsIntConstant()) {
1818 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1819 } else if (value->IsPhi()) {
1820 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1821 } else {
1822 value = LoadLocal(reg_number, Primitive::kPrimNot);
1823 }
David Brazdildee58d62016-04-07 09:54:26 +00001824 UpdateLocal(instruction.VRegA(), value);
1825 break;
1826 }
1827
1828 case Instruction::RETURN_VOID_NO_BARRIER:
1829 case Instruction::RETURN_VOID: {
1830 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1831 break;
1832 }
1833
1834#define IF_XX(comparison, cond) \
1835 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1836 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1837
1838 IF_XX(HEqual, EQ);
1839 IF_XX(HNotEqual, NE);
1840 IF_XX(HLessThan, LT);
1841 IF_XX(HLessThanOrEqual, LE);
1842 IF_XX(HGreaterThan, GT);
1843 IF_XX(HGreaterThanOrEqual, GE);
1844
1845 case Instruction::GOTO:
1846 case Instruction::GOTO_16:
1847 case Instruction::GOTO_32: {
1848 AppendInstruction(new (arena_) HGoto(dex_pc));
1849 current_block_ = nullptr;
1850 break;
1851 }
1852
1853 case Instruction::RETURN: {
1854 BuildReturn(instruction, return_type_, dex_pc);
1855 break;
1856 }
1857
1858 case Instruction::RETURN_OBJECT: {
1859 BuildReturn(instruction, return_type_, dex_pc);
1860 break;
1861 }
1862
1863 case Instruction::RETURN_WIDE: {
1864 BuildReturn(instruction, return_type_, dex_pc);
1865 break;
1866 }
1867
1868 case Instruction::INVOKE_DIRECT:
1869 case Instruction::INVOKE_INTERFACE:
1870 case Instruction::INVOKE_STATIC:
1871 case Instruction::INVOKE_SUPER:
1872 case Instruction::INVOKE_VIRTUAL:
1873 case Instruction::INVOKE_VIRTUAL_QUICK: {
1874 uint16_t method_idx;
1875 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1876 if (!CanDecodeQuickenedInfo()) {
1877 return false;
1878 }
1879 method_idx = LookupQuickenedInfo(dex_pc);
1880 } else {
1881 method_idx = instruction.VRegB_35c();
1882 }
1883 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1884 uint32_t args[5];
1885 instruction.GetVarArgs(args);
1886 if (!BuildInvoke(instruction, dex_pc, method_idx,
1887 number_of_vreg_arguments, false, args, -1)) {
1888 return false;
1889 }
1890 break;
1891 }
1892
1893 case Instruction::INVOKE_DIRECT_RANGE:
1894 case Instruction::INVOKE_INTERFACE_RANGE:
1895 case Instruction::INVOKE_STATIC_RANGE:
1896 case Instruction::INVOKE_SUPER_RANGE:
1897 case Instruction::INVOKE_VIRTUAL_RANGE:
1898 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1899 uint16_t method_idx;
1900 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1901 if (!CanDecodeQuickenedInfo()) {
1902 return false;
1903 }
1904 method_idx = LookupQuickenedInfo(dex_pc);
1905 } else {
1906 method_idx = instruction.VRegB_3rc();
1907 }
1908 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1909 uint32_t register_index = instruction.VRegC();
1910 if (!BuildInvoke(instruction, dex_pc, method_idx,
1911 number_of_vreg_arguments, true, nullptr, register_index)) {
1912 return false;
1913 }
1914 break;
1915 }
1916
1917 case Instruction::NEG_INT: {
1918 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1919 break;
1920 }
1921
1922 case Instruction::NEG_LONG: {
1923 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1924 break;
1925 }
1926
1927 case Instruction::NEG_FLOAT: {
1928 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1929 break;
1930 }
1931
1932 case Instruction::NEG_DOUBLE: {
1933 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1934 break;
1935 }
1936
1937 case Instruction::NOT_INT: {
1938 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1939 break;
1940 }
1941
1942 case Instruction::NOT_LONG: {
1943 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1944 break;
1945 }
1946
1947 case Instruction::INT_TO_LONG: {
1948 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
1949 break;
1950 }
1951
1952 case Instruction::INT_TO_FLOAT: {
1953 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
1954 break;
1955 }
1956
1957 case Instruction::INT_TO_DOUBLE: {
1958 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
1959 break;
1960 }
1961
1962 case Instruction::LONG_TO_INT: {
1963 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
1964 break;
1965 }
1966
1967 case Instruction::LONG_TO_FLOAT: {
1968 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
1969 break;
1970 }
1971
1972 case Instruction::LONG_TO_DOUBLE: {
1973 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
1974 break;
1975 }
1976
1977 case Instruction::FLOAT_TO_INT: {
1978 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1979 break;
1980 }
1981
1982 case Instruction::FLOAT_TO_LONG: {
1983 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
1984 break;
1985 }
1986
1987 case Instruction::FLOAT_TO_DOUBLE: {
1988 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1989 break;
1990 }
1991
1992 case Instruction::DOUBLE_TO_INT: {
1993 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1994 break;
1995 }
1996
1997 case Instruction::DOUBLE_TO_LONG: {
1998 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1999 break;
2000 }
2001
2002 case Instruction::DOUBLE_TO_FLOAT: {
2003 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2004 break;
2005 }
2006
2007 case Instruction::INT_TO_BYTE: {
2008 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2009 break;
2010 }
2011
2012 case Instruction::INT_TO_SHORT: {
2013 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2014 break;
2015 }
2016
2017 case Instruction::INT_TO_CHAR: {
2018 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2019 break;
2020 }
2021
2022 case Instruction::ADD_INT: {
2023 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2024 break;
2025 }
2026
2027 case Instruction::ADD_LONG: {
2028 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2029 break;
2030 }
2031
2032 case Instruction::ADD_DOUBLE: {
2033 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2034 break;
2035 }
2036
2037 case Instruction::ADD_FLOAT: {
2038 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2039 break;
2040 }
2041
2042 case Instruction::SUB_INT: {
2043 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2044 break;
2045 }
2046
2047 case Instruction::SUB_LONG: {
2048 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2049 break;
2050 }
2051
2052 case Instruction::SUB_FLOAT: {
2053 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2054 break;
2055 }
2056
2057 case Instruction::SUB_DOUBLE: {
2058 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2059 break;
2060 }
2061
2062 case Instruction::ADD_INT_2ADDR: {
2063 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2064 break;
2065 }
2066
2067 case Instruction::MUL_INT: {
2068 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2069 break;
2070 }
2071
2072 case Instruction::MUL_LONG: {
2073 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2074 break;
2075 }
2076
2077 case Instruction::MUL_FLOAT: {
2078 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::MUL_DOUBLE: {
2083 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::DIV_INT: {
2088 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2089 dex_pc, Primitive::kPrimInt, false, true);
2090 break;
2091 }
2092
2093 case Instruction::DIV_LONG: {
2094 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2095 dex_pc, Primitive::kPrimLong, false, true);
2096 break;
2097 }
2098
2099 case Instruction::DIV_FLOAT: {
2100 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2101 break;
2102 }
2103
2104 case Instruction::DIV_DOUBLE: {
2105 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2106 break;
2107 }
2108
2109 case Instruction::REM_INT: {
2110 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2111 dex_pc, Primitive::kPrimInt, false, false);
2112 break;
2113 }
2114
2115 case Instruction::REM_LONG: {
2116 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2117 dex_pc, Primitive::kPrimLong, false, false);
2118 break;
2119 }
2120
2121 case Instruction::REM_FLOAT: {
2122 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2123 break;
2124 }
2125
2126 case Instruction::REM_DOUBLE: {
2127 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2128 break;
2129 }
2130
2131 case Instruction::AND_INT: {
2132 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2133 break;
2134 }
2135
2136 case Instruction::AND_LONG: {
2137 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2138 break;
2139 }
2140
2141 case Instruction::SHL_INT: {
2142 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2143 break;
2144 }
2145
2146 case Instruction::SHL_LONG: {
2147 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2148 break;
2149 }
2150
2151 case Instruction::SHR_INT: {
2152 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2153 break;
2154 }
2155
2156 case Instruction::SHR_LONG: {
2157 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2158 break;
2159 }
2160
2161 case Instruction::USHR_INT: {
2162 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2163 break;
2164 }
2165
2166 case Instruction::USHR_LONG: {
2167 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2168 break;
2169 }
2170
2171 case Instruction::OR_INT: {
2172 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2173 break;
2174 }
2175
2176 case Instruction::OR_LONG: {
2177 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2178 break;
2179 }
2180
2181 case Instruction::XOR_INT: {
2182 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2183 break;
2184 }
2185
2186 case Instruction::XOR_LONG: {
2187 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2188 break;
2189 }
2190
2191 case Instruction::ADD_LONG_2ADDR: {
2192 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2193 break;
2194 }
2195
2196 case Instruction::ADD_DOUBLE_2ADDR: {
2197 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2198 break;
2199 }
2200
2201 case Instruction::ADD_FLOAT_2ADDR: {
2202 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2203 break;
2204 }
2205
2206 case Instruction::SUB_INT_2ADDR: {
2207 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2208 break;
2209 }
2210
2211 case Instruction::SUB_LONG_2ADDR: {
2212 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2213 break;
2214 }
2215
2216 case Instruction::SUB_FLOAT_2ADDR: {
2217 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2218 break;
2219 }
2220
2221 case Instruction::SUB_DOUBLE_2ADDR: {
2222 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2223 break;
2224 }
2225
2226 case Instruction::MUL_INT_2ADDR: {
2227 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2228 break;
2229 }
2230
2231 case Instruction::MUL_LONG_2ADDR: {
2232 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2233 break;
2234 }
2235
2236 case Instruction::MUL_FLOAT_2ADDR: {
2237 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2238 break;
2239 }
2240
2241 case Instruction::MUL_DOUBLE_2ADDR: {
2242 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2243 break;
2244 }
2245
2246 case Instruction::DIV_INT_2ADDR: {
2247 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2248 dex_pc, Primitive::kPrimInt, false, true);
2249 break;
2250 }
2251
2252 case Instruction::DIV_LONG_2ADDR: {
2253 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2254 dex_pc, Primitive::kPrimLong, false, true);
2255 break;
2256 }
2257
2258 case Instruction::REM_INT_2ADDR: {
2259 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2260 dex_pc, Primitive::kPrimInt, false, false);
2261 break;
2262 }
2263
2264 case Instruction::REM_LONG_2ADDR: {
2265 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2266 dex_pc, Primitive::kPrimLong, false, false);
2267 break;
2268 }
2269
2270 case Instruction::REM_FLOAT_2ADDR: {
2271 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2272 break;
2273 }
2274
2275 case Instruction::REM_DOUBLE_2ADDR: {
2276 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2277 break;
2278 }
2279
2280 case Instruction::SHL_INT_2ADDR: {
2281 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2282 break;
2283 }
2284
2285 case Instruction::SHL_LONG_2ADDR: {
2286 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2287 break;
2288 }
2289
2290 case Instruction::SHR_INT_2ADDR: {
2291 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2292 break;
2293 }
2294
2295 case Instruction::SHR_LONG_2ADDR: {
2296 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2297 break;
2298 }
2299
2300 case Instruction::USHR_INT_2ADDR: {
2301 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2302 break;
2303 }
2304
2305 case Instruction::USHR_LONG_2ADDR: {
2306 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2307 break;
2308 }
2309
2310 case Instruction::DIV_FLOAT_2ADDR: {
2311 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2312 break;
2313 }
2314
2315 case Instruction::DIV_DOUBLE_2ADDR: {
2316 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2317 break;
2318 }
2319
2320 case Instruction::AND_INT_2ADDR: {
2321 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2322 break;
2323 }
2324
2325 case Instruction::AND_LONG_2ADDR: {
2326 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2327 break;
2328 }
2329
2330 case Instruction::OR_INT_2ADDR: {
2331 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2332 break;
2333 }
2334
2335 case Instruction::OR_LONG_2ADDR: {
2336 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2337 break;
2338 }
2339
2340 case Instruction::XOR_INT_2ADDR: {
2341 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2342 break;
2343 }
2344
2345 case Instruction::XOR_LONG_2ADDR: {
2346 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2347 break;
2348 }
2349
2350 case Instruction::ADD_INT_LIT16: {
2351 Binop_22s<HAdd>(instruction, false, dex_pc);
2352 break;
2353 }
2354
2355 case Instruction::AND_INT_LIT16: {
2356 Binop_22s<HAnd>(instruction, false, dex_pc);
2357 break;
2358 }
2359
2360 case Instruction::OR_INT_LIT16: {
2361 Binop_22s<HOr>(instruction, false, dex_pc);
2362 break;
2363 }
2364
2365 case Instruction::XOR_INT_LIT16: {
2366 Binop_22s<HXor>(instruction, false, dex_pc);
2367 break;
2368 }
2369
2370 case Instruction::RSUB_INT: {
2371 Binop_22s<HSub>(instruction, true, dex_pc);
2372 break;
2373 }
2374
2375 case Instruction::MUL_INT_LIT16: {
2376 Binop_22s<HMul>(instruction, false, dex_pc);
2377 break;
2378 }
2379
2380 case Instruction::ADD_INT_LIT8: {
2381 Binop_22b<HAdd>(instruction, false, dex_pc);
2382 break;
2383 }
2384
2385 case Instruction::AND_INT_LIT8: {
2386 Binop_22b<HAnd>(instruction, false, dex_pc);
2387 break;
2388 }
2389
2390 case Instruction::OR_INT_LIT8: {
2391 Binop_22b<HOr>(instruction, false, dex_pc);
2392 break;
2393 }
2394
2395 case Instruction::XOR_INT_LIT8: {
2396 Binop_22b<HXor>(instruction, false, dex_pc);
2397 break;
2398 }
2399
2400 case Instruction::RSUB_INT_LIT8: {
2401 Binop_22b<HSub>(instruction, true, dex_pc);
2402 break;
2403 }
2404
2405 case Instruction::MUL_INT_LIT8: {
2406 Binop_22b<HMul>(instruction, false, dex_pc);
2407 break;
2408 }
2409
2410 case Instruction::DIV_INT_LIT16:
2411 case Instruction::DIV_INT_LIT8: {
2412 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2413 dex_pc, Primitive::kPrimInt, true, true);
2414 break;
2415 }
2416
2417 case Instruction::REM_INT_LIT16:
2418 case Instruction::REM_INT_LIT8: {
2419 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2420 dex_pc, Primitive::kPrimInt, true, false);
2421 break;
2422 }
2423
2424 case Instruction::SHL_INT_LIT8: {
2425 Binop_22b<HShl>(instruction, false, dex_pc);
2426 break;
2427 }
2428
2429 case Instruction::SHR_INT_LIT8: {
2430 Binop_22b<HShr>(instruction, false, dex_pc);
2431 break;
2432 }
2433
2434 case Instruction::USHR_INT_LIT8: {
2435 Binop_22b<HUShr>(instruction, false, dex_pc);
2436 break;
2437 }
2438
2439 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002440 if (!BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +00002441 return false;
2442 }
2443 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2444 break;
2445 }
2446
2447 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002448 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002449 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
2450 bool finalizable;
2451 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
2452 ? kQuickAllocArrayWithAccessCheck
2453 : kQuickAllocArray;
2454 AppendInstruction(new (arena_) HNewArray(length,
2455 graph_->GetCurrentMethod(),
2456 dex_pc,
2457 type_index,
2458 *dex_compilation_unit_->GetDexFile(),
2459 entrypoint));
2460 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2461 break;
2462 }
2463
2464 case Instruction::FILLED_NEW_ARRAY: {
2465 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002466 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002467 uint32_t args[5];
2468 instruction.GetVarArgs(args);
2469 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2470 break;
2471 }
2472
2473 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2474 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002475 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002476 uint32_t register_index = instruction.VRegC_3rc();
2477 BuildFilledNewArray(
2478 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2479 break;
2480 }
2481
2482 case Instruction::FILL_ARRAY_DATA: {
2483 BuildFillArrayData(instruction, dex_pc);
2484 break;
2485 }
2486
2487 case Instruction::MOVE_RESULT:
2488 case Instruction::MOVE_RESULT_WIDE:
2489 case Instruction::MOVE_RESULT_OBJECT: {
2490 DCHECK(latest_result_ != nullptr);
2491 UpdateLocal(instruction.VRegA(), latest_result_);
2492 latest_result_ = nullptr;
2493 break;
2494 }
2495
2496 case Instruction::CMP_LONG: {
2497 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2498 break;
2499 }
2500
2501 case Instruction::CMPG_FLOAT: {
2502 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2503 break;
2504 }
2505
2506 case Instruction::CMPG_DOUBLE: {
2507 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2508 break;
2509 }
2510
2511 case Instruction::CMPL_FLOAT: {
2512 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2513 break;
2514 }
2515
2516 case Instruction::CMPL_DOUBLE: {
2517 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2518 break;
2519 }
2520
2521 case Instruction::NOP:
2522 break;
2523
2524 case Instruction::IGET:
2525 case Instruction::IGET_QUICK:
2526 case Instruction::IGET_WIDE:
2527 case Instruction::IGET_WIDE_QUICK:
2528 case Instruction::IGET_OBJECT:
2529 case Instruction::IGET_OBJECT_QUICK:
2530 case Instruction::IGET_BOOLEAN:
2531 case Instruction::IGET_BOOLEAN_QUICK:
2532 case Instruction::IGET_BYTE:
2533 case Instruction::IGET_BYTE_QUICK:
2534 case Instruction::IGET_CHAR:
2535 case Instruction::IGET_CHAR_QUICK:
2536 case Instruction::IGET_SHORT:
2537 case Instruction::IGET_SHORT_QUICK: {
2538 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2539 return false;
2540 }
2541 break;
2542 }
2543
2544 case Instruction::IPUT:
2545 case Instruction::IPUT_QUICK:
2546 case Instruction::IPUT_WIDE:
2547 case Instruction::IPUT_WIDE_QUICK:
2548 case Instruction::IPUT_OBJECT:
2549 case Instruction::IPUT_OBJECT_QUICK:
2550 case Instruction::IPUT_BOOLEAN:
2551 case Instruction::IPUT_BOOLEAN_QUICK:
2552 case Instruction::IPUT_BYTE:
2553 case Instruction::IPUT_BYTE_QUICK:
2554 case Instruction::IPUT_CHAR:
2555 case Instruction::IPUT_CHAR_QUICK:
2556 case Instruction::IPUT_SHORT:
2557 case Instruction::IPUT_SHORT_QUICK: {
2558 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2559 return false;
2560 }
2561 break;
2562 }
2563
2564 case Instruction::SGET:
2565 case Instruction::SGET_WIDE:
2566 case Instruction::SGET_OBJECT:
2567 case Instruction::SGET_BOOLEAN:
2568 case Instruction::SGET_BYTE:
2569 case Instruction::SGET_CHAR:
2570 case Instruction::SGET_SHORT: {
2571 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2572 return false;
2573 }
2574 break;
2575 }
2576
2577 case Instruction::SPUT:
2578 case Instruction::SPUT_WIDE:
2579 case Instruction::SPUT_OBJECT:
2580 case Instruction::SPUT_BOOLEAN:
2581 case Instruction::SPUT_BYTE:
2582 case Instruction::SPUT_CHAR:
2583 case Instruction::SPUT_SHORT: {
2584 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2585 return false;
2586 }
2587 break;
2588 }
2589
2590#define ARRAY_XX(kind, anticipated_type) \
2591 case Instruction::AGET##kind: { \
2592 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2593 break; \
2594 } \
2595 case Instruction::APUT##kind: { \
2596 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2597 break; \
2598 }
2599
2600 ARRAY_XX(, Primitive::kPrimInt);
2601 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2602 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2603 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2604 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2605 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2606 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2607
2608 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002609 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002610 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2611 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2612 break;
2613 }
2614
2615 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002616 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002617 AppendInstruction(
2618 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2619 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2620 break;
2621 }
2622
2623 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002624 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002625 AppendInstruction(
2626 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2627 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2628 break;
2629 }
2630
2631 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002632 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002633 // `CanAccessTypeWithoutChecks` will tell whether the method being
2634 // built is trying to access its own class, so that the generated
2635 // code can optimize for this case. However, the optimization does not
2636 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002637 ScopedObjectAccess soa(Thread::Current());
2638 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00002639 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002640 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index);
David Brazdildee58d62016-04-07 09:54:26 +00002641 AppendInstruction(new (arena_) HLoadClass(
2642 graph_->GetCurrentMethod(),
2643 type_index,
2644 *dex_file_,
2645 IsOutermostCompilingClass(type_index),
2646 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00002647 !can_access));
David Brazdildee58d62016-04-07 09:54:26 +00002648 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2649 break;
2650 }
2651
2652 case Instruction::MOVE_EXCEPTION: {
2653 AppendInstruction(new (arena_) HLoadException(dex_pc));
2654 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2655 AppendInstruction(new (arena_) HClearException(dex_pc));
2656 break;
2657 }
2658
2659 case Instruction::THROW: {
2660 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2661 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2662 // We finished building this block. Set the current block to null to avoid
2663 // adding dead instructions to it.
2664 current_block_ = nullptr;
2665 break;
2666 }
2667
2668 case Instruction::INSTANCE_OF: {
2669 uint8_t destination = instruction.VRegA_22c();
2670 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002671 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002672 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2673 break;
2674 }
2675
2676 case Instruction::CHECK_CAST: {
2677 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002678 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002679 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2680 break;
2681 }
2682
2683 case Instruction::MONITOR_ENTER: {
2684 AppendInstruction(new (arena_) HMonitorOperation(
2685 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2686 HMonitorOperation::OperationKind::kEnter,
2687 dex_pc));
2688 break;
2689 }
2690
2691 case Instruction::MONITOR_EXIT: {
2692 AppendInstruction(new (arena_) HMonitorOperation(
2693 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2694 HMonitorOperation::OperationKind::kExit,
2695 dex_pc));
2696 break;
2697 }
2698
2699 case Instruction::SPARSE_SWITCH:
2700 case Instruction::PACKED_SWITCH: {
2701 BuildSwitch(instruction, dex_pc);
2702 break;
2703 }
2704
2705 default:
2706 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002707 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002708 << " because of unhandled instruction "
2709 << instruction.Name();
2710 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2711 return false;
2712 }
2713 return true;
2714} // NOLINT(readability/fn_size)
2715
2716} // namespace art