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