blob: 3cfabddf3a39a5f5c2c95acb6c31642d6f7ee673 [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());
939 StackHandleScope<1> hs(soa.Self());
940 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
941 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
942 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
943 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
944
David Brazdildee58d62016-04-07 09:54:26 +0000945 bool finalizable;
Mingyao Yang062157f2016-03-02 10:15:36 -0800946 bool needs_access_check = NeedsAccessCheck(type_index, dex_cache, &finalizable);
David Brazdildee58d62016-04-07 09:54:26 +0000947
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000948 // Only the access check entrypoint handles the finalizable class case. If we
David Brazdildee58d62016-04-07 09:54:26 +0000949 // need access checks, then we haven't resolved the method and the class may
950 // again be finalizable.
Mingyao Yang062157f2016-03-02 10:15:36 -0800951 QuickEntrypointEnum entrypoint = (finalizable || needs_access_check)
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000952 ? kQuickAllocObjectWithChecks
David Brazdildee58d62016-04-07 09:54:26 +0000953 : kQuickAllocObjectInitialized;
954
David Brazdildee58d62016-04-07 09:54:26 +0000955 if (outer_dex_cache.Get() != dex_cache.Get()) {
956 // We currently do not support inlining allocations across dex files.
957 return false;
958 }
959
960 HLoadClass* load_class = new (arena_) HLoadClass(
961 graph_->GetCurrentMethod(),
962 type_index,
963 outer_dex_file,
964 IsOutermostCompilingClass(type_index),
965 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +0000966 needs_access_check);
David Brazdildee58d62016-04-07 09:54:26 +0000967
968 AppendInstruction(load_class);
969 HInstruction* cls = load_class;
970 if (!IsInitialized(resolved_class)) {
971 cls = new (arena_) HClinitCheck(load_class, dex_pc);
972 AppendInstruction(cls);
973 }
974
975 AppendInstruction(new (arena_) HNewInstance(
976 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000977 dex_pc,
978 type_index,
979 *dex_compilation_unit_->GetDexFile(),
Mingyao Yang062157f2016-03-02 10:15:36 -0800980 needs_access_check,
David Brazdildee58d62016-04-07 09:54:26 +0000981 finalizable,
982 entrypoint));
983 return true;
984}
985
986static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700987 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000988 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
989}
990
991bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
992 if (cls.Get() == nullptr) {
993 return false;
994 }
995
996 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
997 // check whether the class is in an image for the AOT compilation.
998 if (cls->IsInitialized() &&
999 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1000 return true;
1001 }
1002
1003 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1004 return true;
1005 }
1006
1007 // TODO: We should walk over the inlined methods, but we don't pass
1008 // that information to the builder.
1009 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1010 return true;
1011 }
1012
1013 return false;
1014}
1015
1016HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1017 uint32_t dex_pc,
1018 ArtMethod* resolved_method,
1019 uint32_t method_idx,
1020 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
1021 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
1022 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001023 StackHandleScope<2> hs(self);
1024 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1025 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001026 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1027 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
1028
1029 // The index at which the method's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001030 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001031 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
1032 if (is_outer_class) {
1033 storage_index = outer_class->GetDexTypeIndex();
1034 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
1035 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
1036 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
1037 GetCompilingClass(),
1038 resolved_method,
1039 method_idx,
1040 &storage_index);
1041 }
1042
1043 HClinitCheck* clinit_check = nullptr;
1044
1045 if (IsInitialized(resolved_method_class)) {
1046 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001047 } else if (storage_index.IsValid()) {
David Brazdildee58d62016-04-07 09:54:26 +00001048 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1049 HLoadClass* load_class = new (arena_) HLoadClass(
1050 graph_->GetCurrentMethod(),
1051 storage_index,
1052 outer_dex_file,
1053 is_outer_class,
1054 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001055 /*needs_access_check*/ false);
David Brazdildee58d62016-04-07 09:54:26 +00001056 AppendInstruction(load_class);
1057 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1058 AppendInstruction(clinit_check);
1059 }
1060 return clinit_check;
1061}
1062
1063bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1064 uint32_t number_of_vreg_arguments,
1065 uint32_t* args,
1066 uint32_t register_index,
1067 bool is_range,
1068 const char* descriptor,
1069 size_t start_index,
1070 size_t* argument_index) {
1071 uint32_t descriptor_index = 1; // Skip the return type.
1072
1073 for (size_t i = start_index;
1074 // Make sure we don't go over the expected arguments or over the number of
1075 // dex registers given. If the instruction was seen as dead by the verifier,
1076 // it hasn't been properly checked.
1077 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1078 i++, (*argument_index)++) {
1079 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1080 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1081 if (!is_range
1082 && is_wide
1083 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1084 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1085 // reject any class where this is violated. However, the verifier only does these checks
1086 // on non trivially dead instructions, so we just bailout the compilation.
1087 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001088 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001089 << " because of non-sequential dex register pair in wide argument";
1090 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1091 return false;
1092 }
1093 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1094 invoke->SetArgumentAt(*argument_index, arg);
1095 if (is_wide) {
1096 i++;
1097 }
1098 }
1099
1100 if (*argument_index != invoke->GetNumberOfArguments()) {
1101 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001102 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001103 << " because of wrong number of arguments in invoke instruction";
1104 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1105 return false;
1106 }
1107
1108 if (invoke->IsInvokeStaticOrDirect() &&
1109 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1110 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1111 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1112 (*argument_index)++;
1113 }
1114
1115 return true;
1116}
1117
1118bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1119 uint32_t number_of_vreg_arguments,
1120 uint32_t* args,
1121 uint32_t register_index,
1122 bool is_range,
1123 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001124 HClinitCheck* clinit_check,
1125 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001126 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1127
1128 size_t start_index = 0;
1129 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001130 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001131 uint32_t obj_reg = is_range ? register_index : args[0];
1132 HInstruction* arg = is_unresolved
1133 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1134 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001135 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001136 start_index = 1;
1137 argument_index = 1;
1138 }
1139
1140 if (!SetupInvokeArguments(invoke,
1141 number_of_vreg_arguments,
1142 args,
1143 register_index,
1144 is_range,
1145 descriptor,
1146 start_index,
1147 &argument_index)) {
1148 return false;
1149 }
1150
1151 if (clinit_check != nullptr) {
1152 // Add the class initialization check as last input of `invoke`.
1153 DCHECK(invoke->IsInvokeStaticOrDirect());
1154 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1155 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1156 invoke->SetArgumentAt(argument_index, clinit_check);
1157 argument_index++;
1158 }
1159
1160 AppendInstruction(invoke);
1161 latest_result_ = invoke;
1162
1163 return true;
1164}
1165
1166bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1167 uint32_t number_of_vreg_arguments,
1168 uint32_t* args,
1169 uint32_t register_index,
1170 bool is_range,
1171 const char* descriptor) {
1172 DCHECK(invoke->IsInvokeStaticOrDirect());
1173 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1174
1175 size_t start_index = 1;
1176 size_t argument_index = 0;
1177 if (!SetupInvokeArguments(invoke,
1178 number_of_vreg_arguments,
1179 args,
1180 register_index,
1181 is_range,
1182 descriptor,
1183 start_index,
1184 &argument_index)) {
1185 return false;
1186 }
1187
1188 AppendInstruction(invoke);
1189
1190 // This is a StringFactory call, not an actual String constructor. Its result
1191 // replaces the empty String pre-allocated by NewInstance.
1192 uint32_t orig_this_reg = is_range ? register_index : args[0];
1193 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1194
1195 // Replacing the NewInstance might render it redundant. Keep a list of these
1196 // to be visited once it is clear whether it is has remaining uses.
1197 if (arg_this->IsNewInstance()) {
1198 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1199 } else {
1200 DCHECK(arg_this->IsPhi());
1201 // NewInstance is not the direct input of the StringFactory call. It might
1202 // be redundant but optimizing this case is not worth the effort.
1203 }
1204
1205 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1206 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1207 if ((*current_locals_)[vreg] == arg_this) {
1208 (*current_locals_)[vreg] = invoke;
1209 }
1210 }
1211
1212 return true;
1213}
1214
1215static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1216 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1217 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1218 return Primitive::GetType(type[0]);
1219}
1220
1221bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1222 uint32_t dex_pc,
1223 bool is_put) {
1224 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1225 uint32_t obj_reg = instruction.VRegB_22c();
1226 uint16_t field_index;
1227 if (instruction.IsQuickened()) {
1228 if (!CanDecodeQuickenedInfo()) {
1229 return false;
1230 }
1231 field_index = LookupQuickenedInfo(dex_pc);
1232 } else {
1233 field_index = instruction.VRegC_22c();
1234 }
1235
1236 ScopedObjectAccess soa(Thread::Current());
1237 ArtField* resolved_field =
1238 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1239
1240
Aart Bik14154132016-06-02 17:53:58 -07001241 // Generate an explicit null check on the reference, unless the field access
1242 // is unresolved. In that case, we rely on the runtime to perform various
1243 // checks first, followed by a null check.
1244 HInstruction* object = (resolved_field == nullptr)
1245 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1246 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001247
1248 Primitive::Type field_type = (resolved_field == nullptr)
1249 ? GetFieldAccessType(*dex_file_, field_index)
1250 : resolved_field->GetTypeAsPrimitiveType();
1251 if (is_put) {
1252 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1253 HInstruction* field_set = nullptr;
1254 if (resolved_field == nullptr) {
1255 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001256 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001257 value,
1258 field_type,
1259 field_index,
1260 dex_pc);
1261 } else {
1262 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001263 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001264 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001265 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001266 field_type,
1267 resolved_field->GetOffset(),
1268 resolved_field->IsVolatile(),
1269 field_index,
1270 class_def_index,
1271 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001272 dex_pc);
1273 }
1274 AppendInstruction(field_set);
1275 } else {
1276 HInstruction* field_get = nullptr;
1277 if (resolved_field == nullptr) {
1278 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001279 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001280 field_type,
1281 field_index,
1282 dex_pc);
1283 } else {
1284 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001285 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001286 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001287 field_type,
1288 resolved_field->GetOffset(),
1289 resolved_field->IsVolatile(),
1290 field_index,
1291 class_def_index,
1292 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001293 dex_pc);
1294 }
1295 AppendInstruction(field_get);
1296 UpdateLocal(source_or_dest_reg, field_get);
1297 }
1298
1299 return true;
1300}
1301
1302static mirror::Class* GetClassFrom(CompilerDriver* driver,
1303 const DexCompilationUnit& compilation_unit) {
1304 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001305 StackHandleScope<1> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +00001306 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001307 soa.Decode<mirror::ClassLoader>(compilation_unit.GetClassLoader())));
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001308 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001309
1310 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1311}
1312
1313mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1314 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1315}
1316
1317mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1318 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1319}
1320
Andreas Gampea5b09a62016-11-17 15:21:22 -08001321bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001322 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001323 StackHandleScope<3> hs(soa.Self());
1324 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001325 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001326 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001327 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1328 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1329 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1330
1331 // GetOutermostCompilingClass returns null when the class is unresolved
1332 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1333 // we are compiling it.
1334 // When this happens we cannot establish a direct relation between the current
1335 // class and the outer class, so we return false.
1336 // (Note that this is only used for optimizing invokes and field accesses)
1337 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1338}
1339
1340void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001341 uint32_t dex_pc,
1342 bool is_put,
1343 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001344 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1345 uint16_t field_index = instruction.VRegB_21c();
1346
1347 if (is_put) {
1348 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1349 AppendInstruction(
1350 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1351 } else {
1352 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1353 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1354 }
1355}
1356
1357bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1358 uint32_t dex_pc,
1359 bool is_put) {
1360 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1361 uint16_t field_index = instruction.VRegB_21c();
1362
1363 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001364 StackHandleScope<3> hs(soa.Self());
1365 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001366 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001367 soa.Decode<mirror::ClassLoader>(dex_compilation_unit_->GetClassLoader())));
David Brazdildee58d62016-04-07 09:54:26 +00001368 ArtField* resolved_field = compiler_driver_->ResolveField(
1369 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1370
1371 if (resolved_field == nullptr) {
1372 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1373 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1374 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1375 return true;
1376 }
1377
1378 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
1379 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001380 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001381 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1382
1383 // The index at which the field's class is stored in the DexCache's type array.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001384 dex::TypeIndex storage_index;
David Brazdildee58d62016-04-07 09:54:26 +00001385 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1386 if (is_outer_class) {
1387 storage_index = outer_class->GetDexTypeIndex();
1388 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1389 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1390 return false;
1391 } else {
1392 // TODO: This is rather expensive. Perf it and cache the results if needed.
1393 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1394 outer_dex_cache.Get(),
1395 GetCompilingClass(),
1396 resolved_field,
1397 field_index,
1398 &storage_index);
1399 bool can_easily_access = is_put ? pair.second : pair.first;
1400 if (!can_easily_access) {
1401 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1402 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1403 return true;
1404 }
1405 }
1406
David Brazdildee58d62016-04-07 09:54:26 +00001407 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1408 storage_index,
1409 outer_dex_file,
1410 is_outer_class,
1411 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001412 /*needs_access_check*/ false);
David Brazdildee58d62016-04-07 09:54:26 +00001413 AppendInstruction(constant);
1414
1415 HInstruction* cls = constant;
1416
1417 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1418 if (!IsInitialized(klass)) {
1419 cls = new (arena_) HClinitCheck(constant, dex_pc);
1420 AppendInstruction(cls);
1421 }
1422
1423 uint16_t class_def_index = klass->GetDexClassDefIndex();
1424 if (is_put) {
1425 // We need to keep the class alive before loading the value.
1426 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1427 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1428 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1429 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001430 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001431 field_type,
1432 resolved_field->GetOffset(),
1433 resolved_field->IsVolatile(),
1434 field_index,
1435 class_def_index,
1436 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001437 dex_pc));
1438 } else {
1439 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001440 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001441 field_type,
1442 resolved_field->GetOffset(),
1443 resolved_field->IsVolatile(),
1444 field_index,
1445 class_def_index,
1446 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001447 dex_pc));
1448 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1449 }
1450 return true;
1451}
1452
1453void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1454 uint16_t first_vreg,
1455 int64_t second_vreg_or_constant,
1456 uint32_t dex_pc,
1457 Primitive::Type type,
1458 bool second_is_constant,
1459 bool isDiv) {
1460 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1461
1462 HInstruction* first = LoadLocal(first_vreg, type);
1463 HInstruction* second = nullptr;
1464 if (second_is_constant) {
1465 if (type == Primitive::kPrimInt) {
1466 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1467 } else {
1468 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1469 }
1470 } else {
1471 second = LoadLocal(second_vreg_or_constant, type);
1472 }
1473
1474 if (!second_is_constant
1475 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1476 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1477 second = new (arena_) HDivZeroCheck(second, dex_pc);
1478 AppendInstruction(second);
1479 }
1480
1481 if (isDiv) {
1482 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1483 } else {
1484 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1485 }
1486 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1487}
1488
1489void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1490 uint32_t dex_pc,
1491 bool is_put,
1492 Primitive::Type anticipated_type) {
1493 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1494 uint8_t array_reg = instruction.VRegB_23x();
1495 uint8_t index_reg = instruction.VRegC_23x();
1496
David Brazdilc120bbe2016-04-22 16:57:00 +01001497 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001498 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1499 AppendInstruction(length);
1500 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1501 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1502 AppendInstruction(index);
1503 if (is_put) {
1504 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1505 // TODO: Insert a type check node if the type is Object.
1506 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1507 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1508 AppendInstruction(aset);
1509 } else {
1510 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1511 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1512 AppendInstruction(aget);
1513 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1514 }
1515 graph_->SetHasBoundsChecks(true);
1516}
1517
1518void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001519 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001520 uint32_t number_of_vreg_arguments,
1521 bool is_range,
1522 uint32_t* args,
1523 uint32_t register_index) {
1524 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
1525 bool finalizable;
1526 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
1527 ? kQuickAllocArrayWithAccessCheck
1528 : kQuickAllocArray;
1529 HInstruction* object = new (arena_) HNewArray(length,
1530 graph_->GetCurrentMethod(),
1531 dex_pc,
1532 type_index,
1533 *dex_compilation_unit_->GetDexFile(),
1534 entrypoint);
1535 AppendInstruction(object);
1536
1537 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1538 DCHECK_EQ(descriptor[0], '[') << descriptor;
1539 char primitive = descriptor[1];
1540 DCHECK(primitive == 'I'
1541 || primitive == 'L'
1542 || primitive == '[') << descriptor;
1543 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1544 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1545
1546 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1547 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1548 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1549 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1550 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1551 AppendInstruction(aset);
1552 }
1553 latest_result_ = object;
1554}
1555
1556template <typename T>
1557void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1558 const T* data,
1559 uint32_t element_count,
1560 Primitive::Type anticipated_type,
1561 uint32_t dex_pc) {
1562 for (uint32_t i = 0; i < element_count; ++i) {
1563 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1564 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1565 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1566 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1567 AppendInstruction(aset);
1568 }
1569}
1570
1571void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001572 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001573
1574 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1575 const Instruction::ArrayDataPayload* payload =
1576 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1577 const uint8_t* data = payload->data;
1578 uint32_t element_count = payload->element_count;
1579
Vladimir Markoc69fba22016-09-06 16:49:15 +01001580 if (element_count == 0u) {
1581 // For empty payload we emit only the null check above.
1582 return;
1583 }
1584
1585 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1586 AppendInstruction(length);
1587
David Brazdildee58d62016-04-07 09:54:26 +00001588 // Implementation of this DEX instruction seems to be that the bounds check is
1589 // done before doing any stores.
1590 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1591 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1592
1593 switch (payload->element_width) {
1594 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001595 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001596 reinterpret_cast<const int8_t*>(data),
1597 element_count,
1598 Primitive::kPrimByte,
1599 dex_pc);
1600 break;
1601 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001602 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001603 reinterpret_cast<const int16_t*>(data),
1604 element_count,
1605 Primitive::kPrimShort,
1606 dex_pc);
1607 break;
1608 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001609 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001610 reinterpret_cast<const int32_t*>(data),
1611 element_count,
1612 Primitive::kPrimInt,
1613 dex_pc);
1614 break;
1615 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001616 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001617 reinterpret_cast<const int64_t*>(data),
1618 element_count,
1619 dex_pc);
1620 break;
1621 default:
1622 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1623 }
1624 graph_->SetHasBoundsChecks(true);
1625}
1626
1627void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1628 const int64_t* data,
1629 uint32_t element_count,
1630 uint32_t dex_pc) {
1631 for (uint32_t i = 0; i < element_count; ++i) {
1632 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1633 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1634 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1635 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1636 AppendInstruction(aset);
1637 }
1638}
1639
1640static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001641 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001642 if (cls.Get() == nullptr) {
1643 return TypeCheckKind::kUnresolvedCheck;
1644 } else if (cls->IsInterface()) {
1645 return TypeCheckKind::kInterfaceCheck;
1646 } else if (cls->IsArrayClass()) {
1647 if (cls->GetComponentType()->IsObjectClass()) {
1648 return TypeCheckKind::kArrayObjectCheck;
1649 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1650 return TypeCheckKind::kExactCheck;
1651 } else {
1652 return TypeCheckKind::kArrayCheck;
1653 }
1654 } else if (cls->IsFinal()) {
1655 return TypeCheckKind::kExactCheck;
1656 } else if (cls->IsAbstract()) {
1657 return TypeCheckKind::kAbstractClassCheck;
1658 } else {
1659 return TypeCheckKind::kClassHierarchyCheck;
1660 }
1661}
1662
1663void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1664 uint8_t destination,
1665 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001666 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001667 uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001668 ScopedObjectAccess soa(Thread::Current());
1669 StackHandleScope<1> hs(soa.Self());
1670 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1671 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1672 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1673
David Brazdildee58d62016-04-07 09:54:26 +00001674 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1675 dex_compilation_unit_->GetDexMethodIndex(),
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001676 dex_cache,
1677 type_index);
David Brazdildee58d62016-04-07 09:54:26 +00001678
1679 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
1680 HLoadClass* cls = new (arena_) HLoadClass(
1681 graph_->GetCurrentMethod(),
1682 type_index,
1683 dex_file,
1684 IsOutermostCompilingClass(type_index),
1685 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00001686 !can_access);
David Brazdildee58d62016-04-07 09:54:26 +00001687 AppendInstruction(cls);
1688
1689 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
1690 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1691 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1692 UpdateLocal(destination, current_block_->GetLastInstruction());
1693 } else {
1694 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1695 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1696 // which may throw. If it succeeds BoundType sets the new type of `object`
1697 // for all subsequent uses.
1698 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1699 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1700 UpdateLocal(reference, current_block_->GetLastInstruction());
1701 }
1702}
1703
Andreas Gampea5b09a62016-11-17 15:21:22 -08001704bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index,
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001705 Handle<mirror::DexCache> dex_cache,
1706 bool* finalizable) const {
David Brazdildee58d62016-04-07 09:54:26 +00001707 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001708 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index, finalizable);
1709}
1710
Andreas Gampea5b09a62016-11-17 15:21:22 -08001711bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001712 ScopedObjectAccess soa(Thread::Current());
1713 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1714 return NeedsAccessCheck(type_index, dex_cache, finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001715}
1716
1717bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1718 return interpreter_metadata_ != nullptr;
1719}
1720
1721uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1722 DCHECK(interpreter_metadata_ != nullptr);
1723
1724 // First check if the info has already been decoded from `interpreter_metadata_`.
1725 auto it = skipped_interpreter_metadata_.find(dex_pc);
1726 if (it != skipped_interpreter_metadata_.end()) {
1727 // Remove the entry from the map and return the parsed info.
1728 uint16_t value_in_map = it->second;
1729 skipped_interpreter_metadata_.erase(it);
1730 return value_in_map;
1731 }
1732
1733 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1734 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1735 while (true) {
1736 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1737 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1738 DCHECK_LE(dex_pc_in_map, dex_pc);
1739
1740 if (dex_pc_in_map == dex_pc) {
1741 return value_in_map;
1742 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001743 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1744 // the same dex_pc. This is OK, because the compiler does not care about those
1745 // entries.
1746 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001747 }
1748 }
1749}
1750
1751bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1752 switch (instruction.Opcode()) {
1753 case Instruction::CONST_4: {
1754 int32_t register_index = instruction.VRegA();
1755 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1756 UpdateLocal(register_index, constant);
1757 break;
1758 }
1759
1760 case Instruction::CONST_16: {
1761 int32_t register_index = instruction.VRegA();
1762 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1763 UpdateLocal(register_index, constant);
1764 break;
1765 }
1766
1767 case Instruction::CONST: {
1768 int32_t register_index = instruction.VRegA();
1769 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1770 UpdateLocal(register_index, constant);
1771 break;
1772 }
1773
1774 case Instruction::CONST_HIGH16: {
1775 int32_t register_index = instruction.VRegA();
1776 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1777 UpdateLocal(register_index, constant);
1778 break;
1779 }
1780
1781 case Instruction::CONST_WIDE_16: {
1782 int32_t register_index = instruction.VRegA();
1783 // Get 16 bits of constant value, sign extended to 64 bits.
1784 int64_t value = instruction.VRegB_21s();
1785 value <<= 48;
1786 value >>= 48;
1787 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1788 UpdateLocal(register_index, constant);
1789 break;
1790 }
1791
1792 case Instruction::CONST_WIDE_32: {
1793 int32_t register_index = instruction.VRegA();
1794 // Get 32 bits of constant value, sign extended to 64 bits.
1795 int64_t value = instruction.VRegB_31i();
1796 value <<= 32;
1797 value >>= 32;
1798 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1799 UpdateLocal(register_index, constant);
1800 break;
1801 }
1802
1803 case Instruction::CONST_WIDE: {
1804 int32_t register_index = instruction.VRegA();
1805 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1806 UpdateLocal(register_index, constant);
1807 break;
1808 }
1809
1810 case Instruction::CONST_WIDE_HIGH16: {
1811 int32_t register_index = instruction.VRegA();
1812 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1813 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1814 UpdateLocal(register_index, constant);
1815 break;
1816 }
1817
1818 // Note that the SSA building will refine the types.
1819 case Instruction::MOVE:
1820 case Instruction::MOVE_FROM16:
1821 case Instruction::MOVE_16: {
1822 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1823 UpdateLocal(instruction.VRegA(), value);
1824 break;
1825 }
1826
1827 // Note that the SSA building will refine the types.
1828 case Instruction::MOVE_WIDE:
1829 case Instruction::MOVE_WIDE_FROM16:
1830 case Instruction::MOVE_WIDE_16: {
1831 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1832 UpdateLocal(instruction.VRegA(), value);
1833 break;
1834 }
1835
1836 case Instruction::MOVE_OBJECT:
1837 case Instruction::MOVE_OBJECT_16:
1838 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001839 // The verifier has no notion of a null type, so a move-object of constant 0
1840 // will lead to the same constant 0 in the destination register. To mimic
1841 // this behavior, we just pretend we haven't seen a type change (int to reference)
1842 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1843 // types correct.
1844 uint32_t reg_number = instruction.VRegB();
1845 HInstruction* value = (*current_locals_)[reg_number];
1846 if (value->IsIntConstant()) {
1847 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1848 } else if (value->IsPhi()) {
1849 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1850 } else {
1851 value = LoadLocal(reg_number, Primitive::kPrimNot);
1852 }
David Brazdildee58d62016-04-07 09:54:26 +00001853 UpdateLocal(instruction.VRegA(), value);
1854 break;
1855 }
1856
1857 case Instruction::RETURN_VOID_NO_BARRIER:
1858 case Instruction::RETURN_VOID: {
1859 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1860 break;
1861 }
1862
1863#define IF_XX(comparison, cond) \
1864 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1865 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1866
1867 IF_XX(HEqual, EQ);
1868 IF_XX(HNotEqual, NE);
1869 IF_XX(HLessThan, LT);
1870 IF_XX(HLessThanOrEqual, LE);
1871 IF_XX(HGreaterThan, GT);
1872 IF_XX(HGreaterThanOrEqual, GE);
1873
1874 case Instruction::GOTO:
1875 case Instruction::GOTO_16:
1876 case Instruction::GOTO_32: {
1877 AppendInstruction(new (arena_) HGoto(dex_pc));
1878 current_block_ = nullptr;
1879 break;
1880 }
1881
1882 case Instruction::RETURN: {
1883 BuildReturn(instruction, return_type_, dex_pc);
1884 break;
1885 }
1886
1887 case Instruction::RETURN_OBJECT: {
1888 BuildReturn(instruction, return_type_, dex_pc);
1889 break;
1890 }
1891
1892 case Instruction::RETURN_WIDE: {
1893 BuildReturn(instruction, return_type_, dex_pc);
1894 break;
1895 }
1896
1897 case Instruction::INVOKE_DIRECT:
1898 case Instruction::INVOKE_INTERFACE:
1899 case Instruction::INVOKE_STATIC:
1900 case Instruction::INVOKE_SUPER:
1901 case Instruction::INVOKE_VIRTUAL:
1902 case Instruction::INVOKE_VIRTUAL_QUICK: {
1903 uint16_t method_idx;
1904 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1905 if (!CanDecodeQuickenedInfo()) {
1906 return false;
1907 }
1908 method_idx = LookupQuickenedInfo(dex_pc);
1909 } else {
1910 method_idx = instruction.VRegB_35c();
1911 }
1912 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1913 uint32_t args[5];
1914 instruction.GetVarArgs(args);
1915 if (!BuildInvoke(instruction, dex_pc, method_idx,
1916 number_of_vreg_arguments, false, args, -1)) {
1917 return false;
1918 }
1919 break;
1920 }
1921
1922 case Instruction::INVOKE_DIRECT_RANGE:
1923 case Instruction::INVOKE_INTERFACE_RANGE:
1924 case Instruction::INVOKE_STATIC_RANGE:
1925 case Instruction::INVOKE_SUPER_RANGE:
1926 case Instruction::INVOKE_VIRTUAL_RANGE:
1927 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1928 uint16_t method_idx;
1929 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1930 if (!CanDecodeQuickenedInfo()) {
1931 return false;
1932 }
1933 method_idx = LookupQuickenedInfo(dex_pc);
1934 } else {
1935 method_idx = instruction.VRegB_3rc();
1936 }
1937 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1938 uint32_t register_index = instruction.VRegC();
1939 if (!BuildInvoke(instruction, dex_pc, method_idx,
1940 number_of_vreg_arguments, true, nullptr, register_index)) {
1941 return false;
1942 }
1943 break;
1944 }
1945
Orion Hodsonac141392017-01-13 11:53:47 +00001946 case Instruction::INVOKE_POLYMORPHIC: {
1947 uint16_t method_idx = instruction.VRegB_45cc();
1948 uint16_t proto_idx = instruction.VRegH_45cc();
1949 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
1950 uint32_t args[5];
1951 instruction.GetVarArgs(args);
1952 return BuildInvokePolymorphic(instruction,
1953 dex_pc,
1954 method_idx,
1955 proto_idx,
1956 number_of_vreg_arguments,
1957 false,
1958 args,
1959 -1);
1960 }
1961
1962 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
1963 uint16_t method_idx = instruction.VRegB_4rcc();
1964 uint16_t proto_idx = instruction.VRegH_4rcc();
1965 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
1966 uint32_t register_index = instruction.VRegC_4rcc();
1967 return BuildInvokePolymorphic(instruction,
1968 dex_pc,
1969 method_idx,
1970 proto_idx,
1971 number_of_vreg_arguments,
1972 true,
1973 nullptr,
1974 register_index);
1975 }
1976
David Brazdildee58d62016-04-07 09:54:26 +00001977 case Instruction::NEG_INT: {
1978 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1979 break;
1980 }
1981
1982 case Instruction::NEG_LONG: {
1983 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1984 break;
1985 }
1986
1987 case Instruction::NEG_FLOAT: {
1988 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1989 break;
1990 }
1991
1992 case Instruction::NEG_DOUBLE: {
1993 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1994 break;
1995 }
1996
1997 case Instruction::NOT_INT: {
1998 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1999 break;
2000 }
2001
2002 case Instruction::NOT_LONG: {
2003 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
2004 break;
2005 }
2006
2007 case Instruction::INT_TO_LONG: {
2008 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2009 break;
2010 }
2011
2012 case Instruction::INT_TO_FLOAT: {
2013 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2014 break;
2015 }
2016
2017 case Instruction::INT_TO_DOUBLE: {
2018 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2019 break;
2020 }
2021
2022 case Instruction::LONG_TO_INT: {
2023 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2024 break;
2025 }
2026
2027 case Instruction::LONG_TO_FLOAT: {
2028 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2029 break;
2030 }
2031
2032 case Instruction::LONG_TO_DOUBLE: {
2033 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2034 break;
2035 }
2036
2037 case Instruction::FLOAT_TO_INT: {
2038 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2039 break;
2040 }
2041
2042 case Instruction::FLOAT_TO_LONG: {
2043 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2044 break;
2045 }
2046
2047 case Instruction::FLOAT_TO_DOUBLE: {
2048 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2049 break;
2050 }
2051
2052 case Instruction::DOUBLE_TO_INT: {
2053 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2054 break;
2055 }
2056
2057 case Instruction::DOUBLE_TO_LONG: {
2058 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2059 break;
2060 }
2061
2062 case Instruction::DOUBLE_TO_FLOAT: {
2063 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2064 break;
2065 }
2066
2067 case Instruction::INT_TO_BYTE: {
2068 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2069 break;
2070 }
2071
2072 case Instruction::INT_TO_SHORT: {
2073 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2074 break;
2075 }
2076
2077 case Instruction::INT_TO_CHAR: {
2078 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::ADD_INT: {
2083 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::ADD_LONG: {
2088 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2089 break;
2090 }
2091
2092 case Instruction::ADD_DOUBLE: {
2093 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2094 break;
2095 }
2096
2097 case Instruction::ADD_FLOAT: {
2098 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2099 break;
2100 }
2101
2102 case Instruction::SUB_INT: {
2103 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2104 break;
2105 }
2106
2107 case Instruction::SUB_LONG: {
2108 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2109 break;
2110 }
2111
2112 case Instruction::SUB_FLOAT: {
2113 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2114 break;
2115 }
2116
2117 case Instruction::SUB_DOUBLE: {
2118 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2119 break;
2120 }
2121
2122 case Instruction::ADD_INT_2ADDR: {
2123 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2124 break;
2125 }
2126
2127 case Instruction::MUL_INT: {
2128 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2129 break;
2130 }
2131
2132 case Instruction::MUL_LONG: {
2133 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2134 break;
2135 }
2136
2137 case Instruction::MUL_FLOAT: {
2138 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2139 break;
2140 }
2141
2142 case Instruction::MUL_DOUBLE: {
2143 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2144 break;
2145 }
2146
2147 case Instruction::DIV_INT: {
2148 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2149 dex_pc, Primitive::kPrimInt, false, true);
2150 break;
2151 }
2152
2153 case Instruction::DIV_LONG: {
2154 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2155 dex_pc, Primitive::kPrimLong, false, true);
2156 break;
2157 }
2158
2159 case Instruction::DIV_FLOAT: {
2160 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2161 break;
2162 }
2163
2164 case Instruction::DIV_DOUBLE: {
2165 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2166 break;
2167 }
2168
2169 case Instruction::REM_INT: {
2170 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2171 dex_pc, Primitive::kPrimInt, false, false);
2172 break;
2173 }
2174
2175 case Instruction::REM_LONG: {
2176 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2177 dex_pc, Primitive::kPrimLong, false, false);
2178 break;
2179 }
2180
2181 case Instruction::REM_FLOAT: {
2182 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2183 break;
2184 }
2185
2186 case Instruction::REM_DOUBLE: {
2187 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2188 break;
2189 }
2190
2191 case Instruction::AND_INT: {
2192 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2193 break;
2194 }
2195
2196 case Instruction::AND_LONG: {
2197 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2198 break;
2199 }
2200
2201 case Instruction::SHL_INT: {
2202 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2203 break;
2204 }
2205
2206 case Instruction::SHL_LONG: {
2207 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2208 break;
2209 }
2210
2211 case Instruction::SHR_INT: {
2212 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2213 break;
2214 }
2215
2216 case Instruction::SHR_LONG: {
2217 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2218 break;
2219 }
2220
2221 case Instruction::USHR_INT: {
2222 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2223 break;
2224 }
2225
2226 case Instruction::USHR_LONG: {
2227 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2228 break;
2229 }
2230
2231 case Instruction::OR_INT: {
2232 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2233 break;
2234 }
2235
2236 case Instruction::OR_LONG: {
2237 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2238 break;
2239 }
2240
2241 case Instruction::XOR_INT: {
2242 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2243 break;
2244 }
2245
2246 case Instruction::XOR_LONG: {
2247 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2248 break;
2249 }
2250
2251 case Instruction::ADD_LONG_2ADDR: {
2252 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2253 break;
2254 }
2255
2256 case Instruction::ADD_DOUBLE_2ADDR: {
2257 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2258 break;
2259 }
2260
2261 case Instruction::ADD_FLOAT_2ADDR: {
2262 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2263 break;
2264 }
2265
2266 case Instruction::SUB_INT_2ADDR: {
2267 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2268 break;
2269 }
2270
2271 case Instruction::SUB_LONG_2ADDR: {
2272 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2273 break;
2274 }
2275
2276 case Instruction::SUB_FLOAT_2ADDR: {
2277 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2278 break;
2279 }
2280
2281 case Instruction::SUB_DOUBLE_2ADDR: {
2282 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2283 break;
2284 }
2285
2286 case Instruction::MUL_INT_2ADDR: {
2287 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2288 break;
2289 }
2290
2291 case Instruction::MUL_LONG_2ADDR: {
2292 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2293 break;
2294 }
2295
2296 case Instruction::MUL_FLOAT_2ADDR: {
2297 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2298 break;
2299 }
2300
2301 case Instruction::MUL_DOUBLE_2ADDR: {
2302 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2303 break;
2304 }
2305
2306 case Instruction::DIV_INT_2ADDR: {
2307 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2308 dex_pc, Primitive::kPrimInt, false, true);
2309 break;
2310 }
2311
2312 case Instruction::DIV_LONG_2ADDR: {
2313 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2314 dex_pc, Primitive::kPrimLong, false, true);
2315 break;
2316 }
2317
2318 case Instruction::REM_INT_2ADDR: {
2319 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2320 dex_pc, Primitive::kPrimInt, false, false);
2321 break;
2322 }
2323
2324 case Instruction::REM_LONG_2ADDR: {
2325 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2326 dex_pc, Primitive::kPrimLong, false, false);
2327 break;
2328 }
2329
2330 case Instruction::REM_FLOAT_2ADDR: {
2331 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2332 break;
2333 }
2334
2335 case Instruction::REM_DOUBLE_2ADDR: {
2336 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2337 break;
2338 }
2339
2340 case Instruction::SHL_INT_2ADDR: {
2341 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2342 break;
2343 }
2344
2345 case Instruction::SHL_LONG_2ADDR: {
2346 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2347 break;
2348 }
2349
2350 case Instruction::SHR_INT_2ADDR: {
2351 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2352 break;
2353 }
2354
2355 case Instruction::SHR_LONG_2ADDR: {
2356 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2357 break;
2358 }
2359
2360 case Instruction::USHR_INT_2ADDR: {
2361 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2362 break;
2363 }
2364
2365 case Instruction::USHR_LONG_2ADDR: {
2366 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2367 break;
2368 }
2369
2370 case Instruction::DIV_FLOAT_2ADDR: {
2371 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2372 break;
2373 }
2374
2375 case Instruction::DIV_DOUBLE_2ADDR: {
2376 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2377 break;
2378 }
2379
2380 case Instruction::AND_INT_2ADDR: {
2381 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2382 break;
2383 }
2384
2385 case Instruction::AND_LONG_2ADDR: {
2386 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2387 break;
2388 }
2389
2390 case Instruction::OR_INT_2ADDR: {
2391 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2392 break;
2393 }
2394
2395 case Instruction::OR_LONG_2ADDR: {
2396 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2397 break;
2398 }
2399
2400 case Instruction::XOR_INT_2ADDR: {
2401 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2402 break;
2403 }
2404
2405 case Instruction::XOR_LONG_2ADDR: {
2406 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2407 break;
2408 }
2409
2410 case Instruction::ADD_INT_LIT16: {
2411 Binop_22s<HAdd>(instruction, false, dex_pc);
2412 break;
2413 }
2414
2415 case Instruction::AND_INT_LIT16: {
2416 Binop_22s<HAnd>(instruction, false, dex_pc);
2417 break;
2418 }
2419
2420 case Instruction::OR_INT_LIT16: {
2421 Binop_22s<HOr>(instruction, false, dex_pc);
2422 break;
2423 }
2424
2425 case Instruction::XOR_INT_LIT16: {
2426 Binop_22s<HXor>(instruction, false, dex_pc);
2427 break;
2428 }
2429
2430 case Instruction::RSUB_INT: {
2431 Binop_22s<HSub>(instruction, true, dex_pc);
2432 break;
2433 }
2434
2435 case Instruction::MUL_INT_LIT16: {
2436 Binop_22s<HMul>(instruction, false, dex_pc);
2437 break;
2438 }
2439
2440 case Instruction::ADD_INT_LIT8: {
2441 Binop_22b<HAdd>(instruction, false, dex_pc);
2442 break;
2443 }
2444
2445 case Instruction::AND_INT_LIT8: {
2446 Binop_22b<HAnd>(instruction, false, dex_pc);
2447 break;
2448 }
2449
2450 case Instruction::OR_INT_LIT8: {
2451 Binop_22b<HOr>(instruction, false, dex_pc);
2452 break;
2453 }
2454
2455 case Instruction::XOR_INT_LIT8: {
2456 Binop_22b<HXor>(instruction, false, dex_pc);
2457 break;
2458 }
2459
2460 case Instruction::RSUB_INT_LIT8: {
2461 Binop_22b<HSub>(instruction, true, dex_pc);
2462 break;
2463 }
2464
2465 case Instruction::MUL_INT_LIT8: {
2466 Binop_22b<HMul>(instruction, false, dex_pc);
2467 break;
2468 }
2469
2470 case Instruction::DIV_INT_LIT16:
2471 case Instruction::DIV_INT_LIT8: {
2472 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2473 dex_pc, Primitive::kPrimInt, true, true);
2474 break;
2475 }
2476
2477 case Instruction::REM_INT_LIT16:
2478 case Instruction::REM_INT_LIT8: {
2479 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2480 dex_pc, Primitive::kPrimInt, true, false);
2481 break;
2482 }
2483
2484 case Instruction::SHL_INT_LIT8: {
2485 Binop_22b<HShl>(instruction, false, dex_pc);
2486 break;
2487 }
2488
2489 case Instruction::SHR_INT_LIT8: {
2490 Binop_22b<HShr>(instruction, false, dex_pc);
2491 break;
2492 }
2493
2494 case Instruction::USHR_INT_LIT8: {
2495 Binop_22b<HUShr>(instruction, false, dex_pc);
2496 break;
2497 }
2498
2499 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002500 if (!BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +00002501 return false;
2502 }
2503 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2504 break;
2505 }
2506
2507 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002508 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002509 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
2510 bool finalizable;
2511 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
2512 ? kQuickAllocArrayWithAccessCheck
2513 : kQuickAllocArray;
2514 AppendInstruction(new (arena_) HNewArray(length,
2515 graph_->GetCurrentMethod(),
2516 dex_pc,
2517 type_index,
2518 *dex_compilation_unit_->GetDexFile(),
2519 entrypoint));
2520 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2521 break;
2522 }
2523
2524 case Instruction::FILLED_NEW_ARRAY: {
2525 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002526 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002527 uint32_t args[5];
2528 instruction.GetVarArgs(args);
2529 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2530 break;
2531 }
2532
2533 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2534 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002535 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002536 uint32_t register_index = instruction.VRegC_3rc();
2537 BuildFilledNewArray(
2538 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2539 break;
2540 }
2541
2542 case Instruction::FILL_ARRAY_DATA: {
2543 BuildFillArrayData(instruction, dex_pc);
2544 break;
2545 }
2546
2547 case Instruction::MOVE_RESULT:
2548 case Instruction::MOVE_RESULT_WIDE:
2549 case Instruction::MOVE_RESULT_OBJECT: {
2550 DCHECK(latest_result_ != nullptr);
2551 UpdateLocal(instruction.VRegA(), latest_result_);
2552 latest_result_ = nullptr;
2553 break;
2554 }
2555
2556 case Instruction::CMP_LONG: {
2557 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2558 break;
2559 }
2560
2561 case Instruction::CMPG_FLOAT: {
2562 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2563 break;
2564 }
2565
2566 case Instruction::CMPG_DOUBLE: {
2567 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2568 break;
2569 }
2570
2571 case Instruction::CMPL_FLOAT: {
2572 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2573 break;
2574 }
2575
2576 case Instruction::CMPL_DOUBLE: {
2577 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2578 break;
2579 }
2580
2581 case Instruction::NOP:
2582 break;
2583
2584 case Instruction::IGET:
2585 case Instruction::IGET_QUICK:
2586 case Instruction::IGET_WIDE:
2587 case Instruction::IGET_WIDE_QUICK:
2588 case Instruction::IGET_OBJECT:
2589 case Instruction::IGET_OBJECT_QUICK:
2590 case Instruction::IGET_BOOLEAN:
2591 case Instruction::IGET_BOOLEAN_QUICK:
2592 case Instruction::IGET_BYTE:
2593 case Instruction::IGET_BYTE_QUICK:
2594 case Instruction::IGET_CHAR:
2595 case Instruction::IGET_CHAR_QUICK:
2596 case Instruction::IGET_SHORT:
2597 case Instruction::IGET_SHORT_QUICK: {
2598 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2599 return false;
2600 }
2601 break;
2602 }
2603
2604 case Instruction::IPUT:
2605 case Instruction::IPUT_QUICK:
2606 case Instruction::IPUT_WIDE:
2607 case Instruction::IPUT_WIDE_QUICK:
2608 case Instruction::IPUT_OBJECT:
2609 case Instruction::IPUT_OBJECT_QUICK:
2610 case Instruction::IPUT_BOOLEAN:
2611 case Instruction::IPUT_BOOLEAN_QUICK:
2612 case Instruction::IPUT_BYTE:
2613 case Instruction::IPUT_BYTE_QUICK:
2614 case Instruction::IPUT_CHAR:
2615 case Instruction::IPUT_CHAR_QUICK:
2616 case Instruction::IPUT_SHORT:
2617 case Instruction::IPUT_SHORT_QUICK: {
2618 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2619 return false;
2620 }
2621 break;
2622 }
2623
2624 case Instruction::SGET:
2625 case Instruction::SGET_WIDE:
2626 case Instruction::SGET_OBJECT:
2627 case Instruction::SGET_BOOLEAN:
2628 case Instruction::SGET_BYTE:
2629 case Instruction::SGET_CHAR:
2630 case Instruction::SGET_SHORT: {
2631 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2632 return false;
2633 }
2634 break;
2635 }
2636
2637 case Instruction::SPUT:
2638 case Instruction::SPUT_WIDE:
2639 case Instruction::SPUT_OBJECT:
2640 case Instruction::SPUT_BOOLEAN:
2641 case Instruction::SPUT_BYTE:
2642 case Instruction::SPUT_CHAR:
2643 case Instruction::SPUT_SHORT: {
2644 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2645 return false;
2646 }
2647 break;
2648 }
2649
2650#define ARRAY_XX(kind, anticipated_type) \
2651 case Instruction::AGET##kind: { \
2652 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2653 break; \
2654 } \
2655 case Instruction::APUT##kind: { \
2656 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2657 break; \
2658 }
2659
2660 ARRAY_XX(, Primitive::kPrimInt);
2661 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2662 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2663 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2664 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2665 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2666 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2667
2668 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002669 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002670 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2671 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2672 break;
2673 }
2674
2675 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002676 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002677 AppendInstruction(
2678 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2679 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2680 break;
2681 }
2682
2683 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002684 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002685 AppendInstruction(
2686 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2687 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2688 break;
2689 }
2690
2691 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002692 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002693 // `CanAccessTypeWithoutChecks` will tell whether the method being
2694 // built is trying to access its own class, so that the generated
2695 // code can optimize for this case. However, the optimization does not
2696 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002697 ScopedObjectAccess soa(Thread::Current());
2698 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00002699 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002700 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index);
David Brazdildee58d62016-04-07 09:54:26 +00002701 AppendInstruction(new (arena_) HLoadClass(
2702 graph_->GetCurrentMethod(),
2703 type_index,
2704 *dex_file_,
2705 IsOutermostCompilingClass(type_index),
2706 dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00002707 !can_access));
David Brazdildee58d62016-04-07 09:54:26 +00002708 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2709 break;
2710 }
2711
2712 case Instruction::MOVE_EXCEPTION: {
2713 AppendInstruction(new (arena_) HLoadException(dex_pc));
2714 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2715 AppendInstruction(new (arena_) HClearException(dex_pc));
2716 break;
2717 }
2718
2719 case Instruction::THROW: {
2720 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2721 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2722 // We finished building this block. Set the current block to null to avoid
2723 // adding dead instructions to it.
2724 current_block_ = nullptr;
2725 break;
2726 }
2727
2728 case Instruction::INSTANCE_OF: {
2729 uint8_t destination = instruction.VRegA_22c();
2730 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002731 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002732 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2733 break;
2734 }
2735
2736 case Instruction::CHECK_CAST: {
2737 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002738 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002739 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2740 break;
2741 }
2742
2743 case Instruction::MONITOR_ENTER: {
2744 AppendInstruction(new (arena_) HMonitorOperation(
2745 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2746 HMonitorOperation::OperationKind::kEnter,
2747 dex_pc));
2748 break;
2749 }
2750
2751 case Instruction::MONITOR_EXIT: {
2752 AppendInstruction(new (arena_) HMonitorOperation(
2753 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2754 HMonitorOperation::OperationKind::kExit,
2755 dex_pc));
2756 break;
2757 }
2758
2759 case Instruction::SPARSE_SWITCH:
2760 case Instruction::PACKED_SWITCH: {
2761 BuildSwitch(instruction, dex_pc);
2762 break;
2763 }
2764
2765 default:
2766 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002767 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002768 << " because of unhandled instruction "
2769 << instruction.Name();
2770 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2771 return false;
2772 }
2773 return true;
2774} // NOLINT(readability/fn_size)
2775
2776} // namespace art