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