blob: 88f67fae046534129c14f1f3b8df4ab0ed8f1848 [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 Marko8d6768d2017-03-14 10:13:21 +0000680 StackHandleScope<2> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +0000681
682 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000683 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +0000684 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100685 // We fetch the referenced class eagerly (that is, the class pointed by in the MethodId
686 // at method_idx), as `CanAccessResolvedMethod` expects it be be in the dex cache.
687 Handle<mirror::Class> methods_class(hs.NewHandle(class_linker->ResolveReferencedClassOfMethod(
688 method_idx, dex_compilation_unit_->GetDexCache(), class_loader)));
689
Andreas Gampefa4333d2017-02-14 11:10:34 -0800690 if (UNLIKELY(methods_class == nullptr)) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100691 // Clean up any exception left by type resolution.
692 soa.Self()->ClearException();
693 return nullptr;
694 }
David Brazdildee58d62016-04-07 09:54:26 +0000695
696 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
697 *dex_compilation_unit_->GetDexFile(),
698 method_idx,
699 dex_compilation_unit_->GetDexCache(),
700 class_loader,
701 /* referrer */ nullptr,
702 invoke_type);
703
704 if (UNLIKELY(resolved_method == nullptr)) {
705 // Clean up any exception left by type resolution.
706 soa.Self()->ClearException();
707 return nullptr;
708 }
709
710 // Check access. The class linker has a fast path for looking into the dex cache
711 // and does not check the access if it hits it.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800712 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000713 if (!resolved_method->IsPublic()) {
714 return nullptr;
715 }
716 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
717 resolved_method,
718 dex_compilation_unit_->GetDexCache().Get(),
719 method_idx)) {
720 return nullptr;
721 }
722
723 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
724 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
725 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
726 // which require runtime handling.
727 if (invoke_type == kSuper) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800728 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000729 // We could not determine the method's class we need to wait until runtime.
730 DCHECK(Runtime::Current()->IsAotCompiler());
731 return nullptr;
732 }
Aart Bikf663e342016-04-04 17:28:59 -0700733 if (!methods_class->IsAssignableFrom(compiling_class.Get())) {
734 // We cannot statically determine the target method. The runtime will throw a
735 // NoSuchMethodError on this one.
736 return nullptr;
737 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100738 ArtMethod* actual_method;
739 if (methods_class->IsInterface()) {
740 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
741 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000742 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100743 uint16_t vtable_index = resolved_method->GetMethodIndex();
744 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
745 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000746 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100747 if (actual_method != resolved_method &&
748 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
749 // The back-end code generator relies on this check in order to ensure that it will not
750 // attempt to read the dex_cache with a dex_method_index that is not from the correct
751 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
752 // builder, which means that the code-generator (and compiler driver during sharpening and
753 // inliner, maybe) might invoke an incorrect method.
754 // TODO: The actual method could still be referenced in the current dex file, so we
755 // could try locating it.
756 // TODO: Remove the dex_file restriction.
757 return nullptr;
758 }
759 if (!actual_method->IsInvokable()) {
760 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
761 // could resolve the callee to the wrong method.
762 return nullptr;
763 }
764 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000765 }
766
767 // Check for incompatible class changes. The class linker has a fast path for
768 // looking into the dex cache and does not check incompatible class changes if it hits it.
769 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
770 return nullptr;
771 }
772
773 return resolved_method;
774}
775
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100776static bool IsStringConstructor(ArtMethod* method) {
777 ScopedObjectAccess soa(Thread::Current());
778 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
779}
780
David Brazdildee58d62016-04-07 09:54:26 +0000781bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
782 uint32_t dex_pc,
783 uint32_t method_idx,
784 uint32_t number_of_vreg_arguments,
785 bool is_range,
786 uint32_t* args,
787 uint32_t register_index) {
788 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
789 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
790 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
791
792 // Remove the return type from the 'proto'.
793 size_t number_of_arguments = strlen(descriptor) - 1;
794 if (invoke_type != kStatic) { // instance call
795 // One extra argument for 'this'.
796 number_of_arguments++;
797 }
798
David Brazdildee58d62016-04-07 09:54:26 +0000799 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
800
801 if (UNLIKELY(resolved_method == nullptr)) {
802 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
803 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
804 number_of_arguments,
805 return_type,
806 dex_pc,
807 method_idx,
808 invoke_type);
809 return HandleInvoke(invoke,
810 number_of_vreg_arguments,
811 args,
812 register_index,
813 is_range,
814 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700815 nullptr, /* clinit_check */
816 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000817 }
818
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100819 // Replace calls to String.<init> with StringFactory.
820 if (IsStringConstructor(resolved_method)) {
821 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
822 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
823 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
824 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000825 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100826 };
827 MethodReference target_method(dex_file_, method_idx);
828 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
829 arena_,
830 number_of_arguments - 1,
831 Primitive::kPrimNot /*return_type */,
832 dex_pc,
833 method_idx,
834 nullptr,
835 dispatch_info,
836 invoke_type,
837 target_method,
838 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
839 return HandleStringInit(invoke,
840 number_of_vreg_arguments,
841 args,
842 register_index,
843 is_range,
844 descriptor);
845 }
846
David Brazdildee58d62016-04-07 09:54:26 +0000847 // Potential class initialization check, in the case of a static method call.
848 HClinitCheck* clinit_check = nullptr;
849 HInvoke* invoke = nullptr;
850 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
851 // By default, consider that the called method implicitly requires
852 // an initialization check of its declaring method.
853 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
854 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
855 ScopedObjectAccess soa(Thread::Current());
856 if (invoke_type == kStatic) {
857 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000858 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000859 } else if (invoke_type == kSuper) {
860 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100861 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000862 // we resolved to the method referenced by the instruction.
863 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000864 }
865 }
866
867 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
868 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
869 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000870 0u
David Brazdildee58d62016-04-07 09:54:26 +0000871 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100872 MethodReference target_method(resolved_method->GetDexFile(),
873 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000874 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
875 number_of_arguments,
876 return_type,
877 dex_pc,
878 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100879 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000880 dispatch_info,
881 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100882 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000883 clinit_check_requirement);
884 } else if (invoke_type == kVirtual) {
885 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
886 invoke = new (arena_) HInvokeVirtual(arena_,
887 number_of_arguments,
888 return_type,
889 dex_pc,
890 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100891 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000892 resolved_method->GetMethodIndex());
893 } else {
894 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100895 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000896 invoke = new (arena_) HInvokeInterface(arena_,
897 number_of_arguments,
898 return_type,
899 dex_pc,
900 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100901 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700902 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000903 }
904
905 return HandleInvoke(invoke,
906 number_of_vreg_arguments,
907 args,
908 register_index,
909 is_range,
910 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700911 clinit_check,
912 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000913}
914
Orion Hodsonac141392017-01-13 11:53:47 +0000915bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
916 uint32_t dex_pc,
917 uint32_t method_idx,
918 uint32_t proto_idx,
919 uint32_t number_of_vreg_arguments,
920 bool is_range,
921 uint32_t* args,
922 uint32_t register_index) {
923 const char* descriptor = dex_file_->GetShorty(proto_idx);
924 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
925 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
926 size_t number_of_arguments = strlen(descriptor);
927 HInvoke* invoke = new (arena_) HInvokePolymorphic(arena_,
928 number_of_arguments,
929 return_type,
930 dex_pc,
931 method_idx);
932 return HandleInvoke(invoke,
933 number_of_vreg_arguments,
934 args,
935 register_index,
936 is_range,
937 descriptor,
938 nullptr /* clinit_check */,
939 false /* is_unresolved */);
940}
941
Andreas Gampea5b09a62016-11-17 15:21:22 -0800942bool HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100943 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000944
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000945 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000946
David Brazdildee58d62016-04-07 09:54:26 +0000947 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000948 Handle<mirror::Class> klass = load_class->GetClass();
949
950 if (!IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +0000951 cls = new (arena_) HClinitCheck(load_class, dex_pc);
952 AppendInstruction(cls);
953 }
954
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000955 // Only the access check entrypoint handles the finalizable class case. If we
956 // need access checks, then we haven't resolved the method and the class may
957 // again be finalizable.
958 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
959 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
960 entrypoint = kQuickAllocObjectWithChecks;
961 }
962
963 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800964 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000965
David Brazdildee58d62016-04-07 09:54:26 +0000966 AppendInstruction(new (arena_) HNewInstance(
967 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000968 dex_pc,
969 type_index,
970 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +0000971 finalizable,
972 entrypoint));
973 return true;
974}
975
976static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700977 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +0000978 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
979}
980
981bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800982 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000983 return false;
984 }
985
986 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
987 // check whether the class is in an image for the AOT compilation.
988 if (cls->IsInitialized() &&
989 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
990 return true;
991 }
992
993 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
994 return true;
995 }
996
997 // TODO: We should walk over the inlined methods, but we don't pass
998 // that information to the builder.
999 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1000 return true;
1001 }
1002
1003 return false;
1004}
1005
1006HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1007 uint32_t dex_pc,
1008 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001009 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001010 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001011
1012 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001013 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001014 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001015 } else {
1016 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1017 klass->GetDexFile(),
1018 klass,
1019 dex_pc,
1020 /* needs_access_check */ false);
1021 if (cls != nullptr) {
1022 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1023 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
1024 AppendInstruction(clinit_check);
1025 }
David Brazdildee58d62016-04-07 09:54:26 +00001026 }
1027 return clinit_check;
1028}
1029
1030bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1031 uint32_t number_of_vreg_arguments,
1032 uint32_t* args,
1033 uint32_t register_index,
1034 bool is_range,
1035 const char* descriptor,
1036 size_t start_index,
1037 size_t* argument_index) {
1038 uint32_t descriptor_index = 1; // Skip the return type.
1039
1040 for (size_t i = start_index;
1041 // Make sure we don't go over the expected arguments or over the number of
1042 // dex registers given. If the instruction was seen as dead by the verifier,
1043 // it hasn't been properly checked.
1044 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1045 i++, (*argument_index)++) {
1046 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1047 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1048 if (!is_range
1049 && is_wide
1050 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1051 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1052 // reject any class where this is violated. However, the verifier only does these checks
1053 // on non trivially dead instructions, so we just bailout the compilation.
1054 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001055 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001056 << " because of non-sequential dex register pair in wide argument";
1057 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1058 return false;
1059 }
1060 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1061 invoke->SetArgumentAt(*argument_index, arg);
1062 if (is_wide) {
1063 i++;
1064 }
1065 }
1066
1067 if (*argument_index != invoke->GetNumberOfArguments()) {
1068 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001069 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001070 << " because of wrong number of arguments in invoke instruction";
1071 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1072 return false;
1073 }
1074
1075 if (invoke->IsInvokeStaticOrDirect() &&
1076 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1077 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1078 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1079 (*argument_index)++;
1080 }
1081
1082 return true;
1083}
1084
1085bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1086 uint32_t number_of_vreg_arguments,
1087 uint32_t* args,
1088 uint32_t register_index,
1089 bool is_range,
1090 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001091 HClinitCheck* clinit_check,
1092 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001093 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1094
1095 size_t start_index = 0;
1096 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001097 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001098 uint32_t obj_reg = is_range ? register_index : args[0];
1099 HInstruction* arg = is_unresolved
1100 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1101 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001102 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001103 start_index = 1;
1104 argument_index = 1;
1105 }
1106
1107 if (!SetupInvokeArguments(invoke,
1108 number_of_vreg_arguments,
1109 args,
1110 register_index,
1111 is_range,
1112 descriptor,
1113 start_index,
1114 &argument_index)) {
1115 return false;
1116 }
1117
1118 if (clinit_check != nullptr) {
1119 // Add the class initialization check as last input of `invoke`.
1120 DCHECK(invoke->IsInvokeStaticOrDirect());
1121 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1122 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1123 invoke->SetArgumentAt(argument_index, clinit_check);
1124 argument_index++;
1125 }
1126
1127 AppendInstruction(invoke);
1128 latest_result_ = invoke;
1129
1130 return true;
1131}
1132
1133bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1134 uint32_t number_of_vreg_arguments,
1135 uint32_t* args,
1136 uint32_t register_index,
1137 bool is_range,
1138 const char* descriptor) {
1139 DCHECK(invoke->IsInvokeStaticOrDirect());
1140 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1141
1142 size_t start_index = 1;
1143 size_t argument_index = 0;
1144 if (!SetupInvokeArguments(invoke,
1145 number_of_vreg_arguments,
1146 args,
1147 register_index,
1148 is_range,
1149 descriptor,
1150 start_index,
1151 &argument_index)) {
1152 return false;
1153 }
1154
1155 AppendInstruction(invoke);
1156
1157 // This is a StringFactory call, not an actual String constructor. Its result
1158 // replaces the empty String pre-allocated by NewInstance.
1159 uint32_t orig_this_reg = is_range ? register_index : args[0];
1160 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1161
1162 // Replacing the NewInstance might render it redundant. Keep a list of these
1163 // to be visited once it is clear whether it is has remaining uses.
1164 if (arg_this->IsNewInstance()) {
1165 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1166 } else {
1167 DCHECK(arg_this->IsPhi());
1168 // NewInstance is not the direct input of the StringFactory call. It might
1169 // be redundant but optimizing this case is not worth the effort.
1170 }
1171
1172 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1173 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1174 if ((*current_locals_)[vreg] == arg_this) {
1175 (*current_locals_)[vreg] = invoke;
1176 }
1177 }
1178
1179 return true;
1180}
1181
1182static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1183 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1184 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1185 return Primitive::GetType(type[0]);
1186}
1187
1188bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1189 uint32_t dex_pc,
1190 bool is_put) {
1191 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1192 uint32_t obj_reg = instruction.VRegB_22c();
1193 uint16_t field_index;
1194 if (instruction.IsQuickened()) {
1195 if (!CanDecodeQuickenedInfo()) {
1196 return false;
1197 }
1198 field_index = LookupQuickenedInfo(dex_pc);
1199 } else {
1200 field_index = instruction.VRegC_22c();
1201 }
1202
1203 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001204 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001205
Aart Bik14154132016-06-02 17:53:58 -07001206 // Generate an explicit null check on the reference, unless the field access
1207 // is unresolved. In that case, we rely on the runtime to perform various
1208 // checks first, followed by a null check.
1209 HInstruction* object = (resolved_field == nullptr)
1210 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1211 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001212
1213 Primitive::Type field_type = (resolved_field == nullptr)
1214 ? GetFieldAccessType(*dex_file_, field_index)
1215 : resolved_field->GetTypeAsPrimitiveType();
1216 if (is_put) {
1217 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1218 HInstruction* field_set = nullptr;
1219 if (resolved_field == nullptr) {
1220 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001221 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001222 value,
1223 field_type,
1224 field_index,
1225 dex_pc);
1226 } else {
1227 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001228 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001229 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001230 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001231 field_type,
1232 resolved_field->GetOffset(),
1233 resolved_field->IsVolatile(),
1234 field_index,
1235 class_def_index,
1236 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001237 dex_pc);
1238 }
1239 AppendInstruction(field_set);
1240 } else {
1241 HInstruction* field_get = nullptr;
1242 if (resolved_field == nullptr) {
1243 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001244 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001245 field_type,
1246 field_index,
1247 dex_pc);
1248 } else {
1249 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001250 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001251 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001252 field_type,
1253 resolved_field->GetOffset(),
1254 resolved_field->IsVolatile(),
1255 field_index,
1256 class_def_index,
1257 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001258 dex_pc);
1259 }
1260 AppendInstruction(field_get);
1261 UpdateLocal(source_or_dest_reg, field_get);
1262 }
1263
1264 return true;
1265}
1266
1267static mirror::Class* GetClassFrom(CompilerDriver* driver,
1268 const DexCompilationUnit& compilation_unit) {
1269 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001270 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001271 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001272
1273 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1274}
1275
1276mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1277 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1278}
1279
1280mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1281 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1282}
1283
Andreas Gampea5b09a62016-11-17 15:21:22 -08001284bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001285 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001286 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001287 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001288 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001289 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1290 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1291 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1292
1293 // GetOutermostCompilingClass returns null when the class is unresolved
1294 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1295 // we are compiling it.
1296 // When this happens we cannot establish a direct relation between the current
1297 // class and the outer class, so we return false.
1298 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001299 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001300}
1301
1302void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001303 uint32_t dex_pc,
1304 bool is_put,
1305 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001306 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1307 uint16_t field_index = instruction.VRegB_21c();
1308
1309 if (is_put) {
1310 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1311 AppendInstruction(
1312 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1313 } else {
1314 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1315 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1316 }
1317}
1318
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001319ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1320 ScopedObjectAccess soa(Thread::Current());
1321 StackHandleScope<2> hs(soa.Self());
1322
1323 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001324 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001325 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1326
1327 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1328 field_idx,
1329 dex_compilation_unit_->GetDexCache(),
1330 class_loader,
1331 is_static);
1332
1333 if (UNLIKELY(resolved_field == nullptr)) {
1334 // Clean up any exception left by type resolution.
1335 soa.Self()->ClearException();
1336 return nullptr;
1337 }
1338
1339 // Check static/instance. The class linker has a fast path for looking into the dex cache
1340 // and does not check static/instance if it hits it.
1341 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1342 return nullptr;
1343 }
1344
1345 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001346 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001347 if (!resolved_field->IsPublic()) {
1348 return nullptr;
1349 }
1350 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1351 resolved_field,
1352 dex_compilation_unit_->GetDexCache().Get(),
1353 field_idx)) {
1354 return nullptr;
1355 }
1356
1357 if (is_put &&
1358 resolved_field->IsFinal() &&
1359 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1360 // Final fields can only be updated within their own class.
1361 // TODO: Only allow it in constructors. b/34966607.
1362 return nullptr;
1363 }
1364
1365 return resolved_field;
1366}
1367
David Brazdildee58d62016-04-07 09:54:26 +00001368bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1369 uint32_t dex_pc,
1370 bool is_put) {
1371 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1372 uint16_t field_index = instruction.VRegB_21c();
1373
1374 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001375 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001376
1377 if (resolved_field == nullptr) {
1378 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1379 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1380 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1381 return true;
1382 }
1383
1384 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
David Brazdildee58d62016-04-07 09:54:26 +00001385
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001386 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1387 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1388 klass->GetDexFile(),
1389 klass,
1390 dex_pc,
1391 /* needs_access_check */ false);
1392
1393 if (constant == nullptr) {
1394 // The class cannot be referenced from this compiled code. Generate
1395 // an unresolved access.
1396 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1397 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1398 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001399 }
1400
David Brazdildee58d62016-04-07 09:54:26 +00001401 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001402 if (!IsInitialized(klass)) {
1403 cls = new (arena_) HClinitCheck(constant, dex_pc);
1404 AppendInstruction(cls);
1405 }
1406
1407 uint16_t class_def_index = klass->GetDexClassDefIndex();
1408 if (is_put) {
1409 // We need to keep the class alive before loading the value.
1410 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1411 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1412 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1413 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001414 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001415 field_type,
1416 resolved_field->GetOffset(),
1417 resolved_field->IsVolatile(),
1418 field_index,
1419 class_def_index,
1420 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001421 dex_pc));
1422 } else {
1423 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001424 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001425 field_type,
1426 resolved_field->GetOffset(),
1427 resolved_field->IsVolatile(),
1428 field_index,
1429 class_def_index,
1430 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001431 dex_pc));
1432 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1433 }
1434 return true;
1435}
1436
1437void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1438 uint16_t first_vreg,
1439 int64_t second_vreg_or_constant,
1440 uint32_t dex_pc,
1441 Primitive::Type type,
1442 bool second_is_constant,
1443 bool isDiv) {
1444 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1445
1446 HInstruction* first = LoadLocal(first_vreg, type);
1447 HInstruction* second = nullptr;
1448 if (second_is_constant) {
1449 if (type == Primitive::kPrimInt) {
1450 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1451 } else {
1452 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1453 }
1454 } else {
1455 second = LoadLocal(second_vreg_or_constant, type);
1456 }
1457
1458 if (!second_is_constant
1459 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1460 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1461 second = new (arena_) HDivZeroCheck(second, dex_pc);
1462 AppendInstruction(second);
1463 }
1464
1465 if (isDiv) {
1466 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1467 } else {
1468 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1469 }
1470 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1471}
1472
1473void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1474 uint32_t dex_pc,
1475 bool is_put,
1476 Primitive::Type anticipated_type) {
1477 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1478 uint8_t array_reg = instruction.VRegB_23x();
1479 uint8_t index_reg = instruction.VRegC_23x();
1480
David Brazdilc120bbe2016-04-22 16:57:00 +01001481 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001482 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1483 AppendInstruction(length);
1484 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1485 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1486 AppendInstruction(index);
1487 if (is_put) {
1488 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1489 // TODO: Insert a type check node if the type is Object.
1490 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1491 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1492 AppendInstruction(aset);
1493 } else {
1494 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1495 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1496 AppendInstruction(aget);
1497 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1498 }
1499 graph_->SetHasBoundsChecks(true);
1500}
1501
1502void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001503 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001504 uint32_t number_of_vreg_arguments,
1505 bool is_range,
1506 uint32_t* args,
1507 uint32_t register_index) {
1508 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001509 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001510 HInstruction* object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001511 AppendInstruction(object);
1512
1513 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1514 DCHECK_EQ(descriptor[0], '[') << descriptor;
1515 char primitive = descriptor[1];
1516 DCHECK(primitive == 'I'
1517 || primitive == 'L'
1518 || primitive == '[') << descriptor;
1519 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1520 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1521
1522 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1523 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1524 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1525 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1526 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1527 AppendInstruction(aset);
1528 }
1529 latest_result_ = object;
1530}
1531
1532template <typename T>
1533void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1534 const T* data,
1535 uint32_t element_count,
1536 Primitive::Type anticipated_type,
1537 uint32_t dex_pc) {
1538 for (uint32_t i = 0; i < element_count; ++i) {
1539 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1540 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1541 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1542 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1543 AppendInstruction(aset);
1544 }
1545}
1546
1547void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001548 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001549
1550 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1551 const Instruction::ArrayDataPayload* payload =
1552 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1553 const uint8_t* data = payload->data;
1554 uint32_t element_count = payload->element_count;
1555
Vladimir Markoc69fba22016-09-06 16:49:15 +01001556 if (element_count == 0u) {
1557 // For empty payload we emit only the null check above.
1558 return;
1559 }
1560
1561 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1562 AppendInstruction(length);
1563
David Brazdildee58d62016-04-07 09:54:26 +00001564 // Implementation of this DEX instruction seems to be that the bounds check is
1565 // done before doing any stores.
1566 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1567 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1568
1569 switch (payload->element_width) {
1570 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001571 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001572 reinterpret_cast<const int8_t*>(data),
1573 element_count,
1574 Primitive::kPrimByte,
1575 dex_pc);
1576 break;
1577 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001578 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001579 reinterpret_cast<const int16_t*>(data),
1580 element_count,
1581 Primitive::kPrimShort,
1582 dex_pc);
1583 break;
1584 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001585 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001586 reinterpret_cast<const int32_t*>(data),
1587 element_count,
1588 Primitive::kPrimInt,
1589 dex_pc);
1590 break;
1591 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001592 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001593 reinterpret_cast<const int64_t*>(data),
1594 element_count,
1595 dex_pc);
1596 break;
1597 default:
1598 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1599 }
1600 graph_->SetHasBoundsChecks(true);
1601}
1602
1603void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1604 const int64_t* data,
1605 uint32_t element_count,
1606 uint32_t dex_pc) {
1607 for (uint32_t i = 0; i < element_count; ++i) {
1608 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1609 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1610 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1611 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1612 AppendInstruction(aset);
1613 }
1614}
1615
1616static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001617 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001618 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001619 return TypeCheckKind::kUnresolvedCheck;
1620 } else if (cls->IsInterface()) {
1621 return TypeCheckKind::kInterfaceCheck;
1622 } else if (cls->IsArrayClass()) {
1623 if (cls->GetComponentType()->IsObjectClass()) {
1624 return TypeCheckKind::kArrayObjectCheck;
1625 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1626 return TypeCheckKind::kExactCheck;
1627 } else {
1628 return TypeCheckKind::kArrayCheck;
1629 }
1630 } else if (cls->IsFinal()) {
1631 return TypeCheckKind::kExactCheck;
1632 } else if (cls->IsAbstract()) {
1633 return TypeCheckKind::kAbstractClassCheck;
1634 } else {
1635 return TypeCheckKind::kClassHierarchyCheck;
1636 }
1637}
1638
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001639HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001640 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001641 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001642 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001643 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001644 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001645
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001646 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001647 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001648 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001649 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001650 } else {
1651 mirror::Class* compiling_class = GetCompilingClass();
1652 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001653 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001654 }
1655 }
1656 }
1657
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001658 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1659}
1660
1661HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1662 const DexFile& dex_file,
1663 Handle<mirror::Class> klass,
1664 uint32_t dex_pc,
1665 bool needs_access_check) {
1666 // Try to find a reference in the compiling dex file.
1667 const DexFile* actual_dex_file = &dex_file;
1668 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1669 dex::TypeIndex local_type_index =
1670 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1671 if (local_type_index.IsValid()) {
1672 type_index = local_type_index;
1673 actual_dex_file = dex_compilation_unit_->GetDexFile();
1674 }
1675 }
1676
1677 // Note: `klass` must be from `handles_`.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001678 HLoadClass* load_class = new (arena_) HLoadClass(
1679 graph_->GetCurrentMethod(),
1680 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001681 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001682 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001683 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001684 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001685 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001686
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001687 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1688 code_generator_,
1689 compiler_driver_,
1690 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001691
1692 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1693 // We actually cannot reference this class, we're forced to bail.
1694 return nullptr;
1695 }
1696 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001697 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001698 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001699 return load_class;
1700}
1701
David Brazdildee58d62016-04-07 09:54:26 +00001702void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1703 uint8_t destination,
1704 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001705 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001706 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001707 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001708 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001709
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001710 ScopedObjectAccess soa(Thread::Current());
1711 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001712 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1713 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1714 UpdateLocal(destination, current_block_->GetLastInstruction());
1715 } else {
1716 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1717 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1718 // which may throw. If it succeeds BoundType sets the new type of `object`
1719 // for all subsequent uses.
1720 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1721 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1722 UpdateLocal(reference, current_block_->GetLastInstruction());
1723 }
1724}
1725
Vladimir Marko0b66d612017-03-13 14:50:04 +00001726bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001727 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1728 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001729}
1730
1731bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1732 return interpreter_metadata_ != nullptr;
1733}
1734
1735uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1736 DCHECK(interpreter_metadata_ != nullptr);
1737
1738 // First check if the info has already been decoded from `interpreter_metadata_`.
1739 auto it = skipped_interpreter_metadata_.find(dex_pc);
1740 if (it != skipped_interpreter_metadata_.end()) {
1741 // Remove the entry from the map and return the parsed info.
1742 uint16_t value_in_map = it->second;
1743 skipped_interpreter_metadata_.erase(it);
1744 return value_in_map;
1745 }
1746
1747 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1748 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1749 while (true) {
1750 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1751 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1752 DCHECK_LE(dex_pc_in_map, dex_pc);
1753
1754 if (dex_pc_in_map == dex_pc) {
1755 return value_in_map;
1756 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001757 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1758 // the same dex_pc. This is OK, because the compiler does not care about those
1759 // entries.
1760 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001761 }
1762 }
1763}
1764
1765bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1766 switch (instruction.Opcode()) {
1767 case Instruction::CONST_4: {
1768 int32_t register_index = instruction.VRegA();
1769 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1770 UpdateLocal(register_index, constant);
1771 break;
1772 }
1773
1774 case Instruction::CONST_16: {
1775 int32_t register_index = instruction.VRegA();
1776 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1777 UpdateLocal(register_index, constant);
1778 break;
1779 }
1780
1781 case Instruction::CONST: {
1782 int32_t register_index = instruction.VRegA();
1783 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1784 UpdateLocal(register_index, constant);
1785 break;
1786 }
1787
1788 case Instruction::CONST_HIGH16: {
1789 int32_t register_index = instruction.VRegA();
1790 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1791 UpdateLocal(register_index, constant);
1792 break;
1793 }
1794
1795 case Instruction::CONST_WIDE_16: {
1796 int32_t register_index = instruction.VRegA();
1797 // Get 16 bits of constant value, sign extended to 64 bits.
1798 int64_t value = instruction.VRegB_21s();
1799 value <<= 48;
1800 value >>= 48;
1801 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1802 UpdateLocal(register_index, constant);
1803 break;
1804 }
1805
1806 case Instruction::CONST_WIDE_32: {
1807 int32_t register_index = instruction.VRegA();
1808 // Get 32 bits of constant value, sign extended to 64 bits.
1809 int64_t value = instruction.VRegB_31i();
1810 value <<= 32;
1811 value >>= 32;
1812 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1813 UpdateLocal(register_index, constant);
1814 break;
1815 }
1816
1817 case Instruction::CONST_WIDE: {
1818 int32_t register_index = instruction.VRegA();
1819 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1820 UpdateLocal(register_index, constant);
1821 break;
1822 }
1823
1824 case Instruction::CONST_WIDE_HIGH16: {
1825 int32_t register_index = instruction.VRegA();
1826 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1827 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1828 UpdateLocal(register_index, constant);
1829 break;
1830 }
1831
1832 // Note that the SSA building will refine the types.
1833 case Instruction::MOVE:
1834 case Instruction::MOVE_FROM16:
1835 case Instruction::MOVE_16: {
1836 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1837 UpdateLocal(instruction.VRegA(), value);
1838 break;
1839 }
1840
1841 // Note that the SSA building will refine the types.
1842 case Instruction::MOVE_WIDE:
1843 case Instruction::MOVE_WIDE_FROM16:
1844 case Instruction::MOVE_WIDE_16: {
1845 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1846 UpdateLocal(instruction.VRegA(), value);
1847 break;
1848 }
1849
1850 case Instruction::MOVE_OBJECT:
1851 case Instruction::MOVE_OBJECT_16:
1852 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001853 // The verifier has no notion of a null type, so a move-object of constant 0
1854 // will lead to the same constant 0 in the destination register. To mimic
1855 // this behavior, we just pretend we haven't seen a type change (int to reference)
1856 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1857 // types correct.
1858 uint32_t reg_number = instruction.VRegB();
1859 HInstruction* value = (*current_locals_)[reg_number];
1860 if (value->IsIntConstant()) {
1861 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1862 } else if (value->IsPhi()) {
1863 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1864 } else {
1865 value = LoadLocal(reg_number, Primitive::kPrimNot);
1866 }
David Brazdildee58d62016-04-07 09:54:26 +00001867 UpdateLocal(instruction.VRegA(), value);
1868 break;
1869 }
1870
1871 case Instruction::RETURN_VOID_NO_BARRIER:
1872 case Instruction::RETURN_VOID: {
1873 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1874 break;
1875 }
1876
1877#define IF_XX(comparison, cond) \
1878 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1879 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1880
1881 IF_XX(HEqual, EQ);
1882 IF_XX(HNotEqual, NE);
1883 IF_XX(HLessThan, LT);
1884 IF_XX(HLessThanOrEqual, LE);
1885 IF_XX(HGreaterThan, GT);
1886 IF_XX(HGreaterThanOrEqual, GE);
1887
1888 case Instruction::GOTO:
1889 case Instruction::GOTO_16:
1890 case Instruction::GOTO_32: {
1891 AppendInstruction(new (arena_) HGoto(dex_pc));
1892 current_block_ = nullptr;
1893 break;
1894 }
1895
1896 case Instruction::RETURN: {
1897 BuildReturn(instruction, return_type_, dex_pc);
1898 break;
1899 }
1900
1901 case Instruction::RETURN_OBJECT: {
1902 BuildReturn(instruction, return_type_, dex_pc);
1903 break;
1904 }
1905
1906 case Instruction::RETURN_WIDE: {
1907 BuildReturn(instruction, return_type_, dex_pc);
1908 break;
1909 }
1910
1911 case Instruction::INVOKE_DIRECT:
1912 case Instruction::INVOKE_INTERFACE:
1913 case Instruction::INVOKE_STATIC:
1914 case Instruction::INVOKE_SUPER:
1915 case Instruction::INVOKE_VIRTUAL:
1916 case Instruction::INVOKE_VIRTUAL_QUICK: {
1917 uint16_t method_idx;
1918 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1919 if (!CanDecodeQuickenedInfo()) {
1920 return false;
1921 }
1922 method_idx = LookupQuickenedInfo(dex_pc);
1923 } else {
1924 method_idx = instruction.VRegB_35c();
1925 }
1926 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1927 uint32_t args[5];
1928 instruction.GetVarArgs(args);
1929 if (!BuildInvoke(instruction, dex_pc, method_idx,
1930 number_of_vreg_arguments, false, args, -1)) {
1931 return false;
1932 }
1933 break;
1934 }
1935
1936 case Instruction::INVOKE_DIRECT_RANGE:
1937 case Instruction::INVOKE_INTERFACE_RANGE:
1938 case Instruction::INVOKE_STATIC_RANGE:
1939 case Instruction::INVOKE_SUPER_RANGE:
1940 case Instruction::INVOKE_VIRTUAL_RANGE:
1941 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1942 uint16_t method_idx;
1943 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1944 if (!CanDecodeQuickenedInfo()) {
1945 return false;
1946 }
1947 method_idx = LookupQuickenedInfo(dex_pc);
1948 } else {
1949 method_idx = instruction.VRegB_3rc();
1950 }
1951 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1952 uint32_t register_index = instruction.VRegC();
1953 if (!BuildInvoke(instruction, dex_pc, method_idx,
1954 number_of_vreg_arguments, true, nullptr, register_index)) {
1955 return false;
1956 }
1957 break;
1958 }
1959
Orion Hodsonac141392017-01-13 11:53:47 +00001960 case Instruction::INVOKE_POLYMORPHIC: {
1961 uint16_t method_idx = instruction.VRegB_45cc();
1962 uint16_t proto_idx = instruction.VRegH_45cc();
1963 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
1964 uint32_t args[5];
1965 instruction.GetVarArgs(args);
1966 return BuildInvokePolymorphic(instruction,
1967 dex_pc,
1968 method_idx,
1969 proto_idx,
1970 number_of_vreg_arguments,
1971 false,
1972 args,
1973 -1);
1974 }
1975
1976 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
1977 uint16_t method_idx = instruction.VRegB_4rcc();
1978 uint16_t proto_idx = instruction.VRegH_4rcc();
1979 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
1980 uint32_t register_index = instruction.VRegC_4rcc();
1981 return BuildInvokePolymorphic(instruction,
1982 dex_pc,
1983 method_idx,
1984 proto_idx,
1985 number_of_vreg_arguments,
1986 true,
1987 nullptr,
1988 register_index);
1989 }
1990
David Brazdildee58d62016-04-07 09:54:26 +00001991 case Instruction::NEG_INT: {
1992 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1993 break;
1994 }
1995
1996 case Instruction::NEG_LONG: {
1997 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1998 break;
1999 }
2000
2001 case Instruction::NEG_FLOAT: {
2002 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
2003 break;
2004 }
2005
2006 case Instruction::NEG_DOUBLE: {
2007 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
2008 break;
2009 }
2010
2011 case Instruction::NOT_INT: {
2012 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
2013 break;
2014 }
2015
2016 case Instruction::NOT_LONG: {
2017 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
2018 break;
2019 }
2020
2021 case Instruction::INT_TO_LONG: {
2022 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2023 break;
2024 }
2025
2026 case Instruction::INT_TO_FLOAT: {
2027 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2028 break;
2029 }
2030
2031 case Instruction::INT_TO_DOUBLE: {
2032 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2033 break;
2034 }
2035
2036 case Instruction::LONG_TO_INT: {
2037 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2038 break;
2039 }
2040
2041 case Instruction::LONG_TO_FLOAT: {
2042 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2043 break;
2044 }
2045
2046 case Instruction::LONG_TO_DOUBLE: {
2047 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2048 break;
2049 }
2050
2051 case Instruction::FLOAT_TO_INT: {
2052 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2053 break;
2054 }
2055
2056 case Instruction::FLOAT_TO_LONG: {
2057 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2058 break;
2059 }
2060
2061 case Instruction::FLOAT_TO_DOUBLE: {
2062 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2063 break;
2064 }
2065
2066 case Instruction::DOUBLE_TO_INT: {
2067 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2068 break;
2069 }
2070
2071 case Instruction::DOUBLE_TO_LONG: {
2072 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2073 break;
2074 }
2075
2076 case Instruction::DOUBLE_TO_FLOAT: {
2077 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2078 break;
2079 }
2080
2081 case Instruction::INT_TO_BYTE: {
2082 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2083 break;
2084 }
2085
2086 case Instruction::INT_TO_SHORT: {
2087 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2088 break;
2089 }
2090
2091 case Instruction::INT_TO_CHAR: {
2092 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2093 break;
2094 }
2095
2096 case Instruction::ADD_INT: {
2097 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2098 break;
2099 }
2100
2101 case Instruction::ADD_LONG: {
2102 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2103 break;
2104 }
2105
2106 case Instruction::ADD_DOUBLE: {
2107 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2108 break;
2109 }
2110
2111 case Instruction::ADD_FLOAT: {
2112 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2113 break;
2114 }
2115
2116 case Instruction::SUB_INT: {
2117 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2118 break;
2119 }
2120
2121 case Instruction::SUB_LONG: {
2122 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2123 break;
2124 }
2125
2126 case Instruction::SUB_FLOAT: {
2127 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2128 break;
2129 }
2130
2131 case Instruction::SUB_DOUBLE: {
2132 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2133 break;
2134 }
2135
2136 case Instruction::ADD_INT_2ADDR: {
2137 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2138 break;
2139 }
2140
2141 case Instruction::MUL_INT: {
2142 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2143 break;
2144 }
2145
2146 case Instruction::MUL_LONG: {
2147 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2148 break;
2149 }
2150
2151 case Instruction::MUL_FLOAT: {
2152 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2153 break;
2154 }
2155
2156 case Instruction::MUL_DOUBLE: {
2157 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2158 break;
2159 }
2160
2161 case Instruction::DIV_INT: {
2162 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2163 dex_pc, Primitive::kPrimInt, false, true);
2164 break;
2165 }
2166
2167 case Instruction::DIV_LONG: {
2168 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2169 dex_pc, Primitive::kPrimLong, false, true);
2170 break;
2171 }
2172
2173 case Instruction::DIV_FLOAT: {
2174 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2175 break;
2176 }
2177
2178 case Instruction::DIV_DOUBLE: {
2179 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2180 break;
2181 }
2182
2183 case Instruction::REM_INT: {
2184 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2185 dex_pc, Primitive::kPrimInt, false, false);
2186 break;
2187 }
2188
2189 case Instruction::REM_LONG: {
2190 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2191 dex_pc, Primitive::kPrimLong, false, false);
2192 break;
2193 }
2194
2195 case Instruction::REM_FLOAT: {
2196 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2197 break;
2198 }
2199
2200 case Instruction::REM_DOUBLE: {
2201 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2202 break;
2203 }
2204
2205 case Instruction::AND_INT: {
2206 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2207 break;
2208 }
2209
2210 case Instruction::AND_LONG: {
2211 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2212 break;
2213 }
2214
2215 case Instruction::SHL_INT: {
2216 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2217 break;
2218 }
2219
2220 case Instruction::SHL_LONG: {
2221 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2222 break;
2223 }
2224
2225 case Instruction::SHR_INT: {
2226 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2227 break;
2228 }
2229
2230 case Instruction::SHR_LONG: {
2231 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2232 break;
2233 }
2234
2235 case Instruction::USHR_INT: {
2236 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2237 break;
2238 }
2239
2240 case Instruction::USHR_LONG: {
2241 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2242 break;
2243 }
2244
2245 case Instruction::OR_INT: {
2246 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2247 break;
2248 }
2249
2250 case Instruction::OR_LONG: {
2251 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2252 break;
2253 }
2254
2255 case Instruction::XOR_INT: {
2256 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2257 break;
2258 }
2259
2260 case Instruction::XOR_LONG: {
2261 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2262 break;
2263 }
2264
2265 case Instruction::ADD_LONG_2ADDR: {
2266 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2267 break;
2268 }
2269
2270 case Instruction::ADD_DOUBLE_2ADDR: {
2271 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2272 break;
2273 }
2274
2275 case Instruction::ADD_FLOAT_2ADDR: {
2276 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2277 break;
2278 }
2279
2280 case Instruction::SUB_INT_2ADDR: {
2281 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2282 break;
2283 }
2284
2285 case Instruction::SUB_LONG_2ADDR: {
2286 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2287 break;
2288 }
2289
2290 case Instruction::SUB_FLOAT_2ADDR: {
2291 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2292 break;
2293 }
2294
2295 case Instruction::SUB_DOUBLE_2ADDR: {
2296 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2297 break;
2298 }
2299
2300 case Instruction::MUL_INT_2ADDR: {
2301 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2302 break;
2303 }
2304
2305 case Instruction::MUL_LONG_2ADDR: {
2306 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2307 break;
2308 }
2309
2310 case Instruction::MUL_FLOAT_2ADDR: {
2311 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2312 break;
2313 }
2314
2315 case Instruction::MUL_DOUBLE_2ADDR: {
2316 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2317 break;
2318 }
2319
2320 case Instruction::DIV_INT_2ADDR: {
2321 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2322 dex_pc, Primitive::kPrimInt, false, true);
2323 break;
2324 }
2325
2326 case Instruction::DIV_LONG_2ADDR: {
2327 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2328 dex_pc, Primitive::kPrimLong, false, true);
2329 break;
2330 }
2331
2332 case Instruction::REM_INT_2ADDR: {
2333 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2334 dex_pc, Primitive::kPrimInt, false, false);
2335 break;
2336 }
2337
2338 case Instruction::REM_LONG_2ADDR: {
2339 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2340 dex_pc, Primitive::kPrimLong, false, false);
2341 break;
2342 }
2343
2344 case Instruction::REM_FLOAT_2ADDR: {
2345 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2346 break;
2347 }
2348
2349 case Instruction::REM_DOUBLE_2ADDR: {
2350 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2351 break;
2352 }
2353
2354 case Instruction::SHL_INT_2ADDR: {
2355 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2356 break;
2357 }
2358
2359 case Instruction::SHL_LONG_2ADDR: {
2360 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2361 break;
2362 }
2363
2364 case Instruction::SHR_INT_2ADDR: {
2365 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2366 break;
2367 }
2368
2369 case Instruction::SHR_LONG_2ADDR: {
2370 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2371 break;
2372 }
2373
2374 case Instruction::USHR_INT_2ADDR: {
2375 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2376 break;
2377 }
2378
2379 case Instruction::USHR_LONG_2ADDR: {
2380 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2381 break;
2382 }
2383
2384 case Instruction::DIV_FLOAT_2ADDR: {
2385 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2386 break;
2387 }
2388
2389 case Instruction::DIV_DOUBLE_2ADDR: {
2390 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2391 break;
2392 }
2393
2394 case Instruction::AND_INT_2ADDR: {
2395 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2396 break;
2397 }
2398
2399 case Instruction::AND_LONG_2ADDR: {
2400 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2401 break;
2402 }
2403
2404 case Instruction::OR_INT_2ADDR: {
2405 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2406 break;
2407 }
2408
2409 case Instruction::OR_LONG_2ADDR: {
2410 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2411 break;
2412 }
2413
2414 case Instruction::XOR_INT_2ADDR: {
2415 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2416 break;
2417 }
2418
2419 case Instruction::XOR_LONG_2ADDR: {
2420 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2421 break;
2422 }
2423
2424 case Instruction::ADD_INT_LIT16: {
2425 Binop_22s<HAdd>(instruction, false, dex_pc);
2426 break;
2427 }
2428
2429 case Instruction::AND_INT_LIT16: {
2430 Binop_22s<HAnd>(instruction, false, dex_pc);
2431 break;
2432 }
2433
2434 case Instruction::OR_INT_LIT16: {
2435 Binop_22s<HOr>(instruction, false, dex_pc);
2436 break;
2437 }
2438
2439 case Instruction::XOR_INT_LIT16: {
2440 Binop_22s<HXor>(instruction, false, dex_pc);
2441 break;
2442 }
2443
2444 case Instruction::RSUB_INT: {
2445 Binop_22s<HSub>(instruction, true, dex_pc);
2446 break;
2447 }
2448
2449 case Instruction::MUL_INT_LIT16: {
2450 Binop_22s<HMul>(instruction, false, dex_pc);
2451 break;
2452 }
2453
2454 case Instruction::ADD_INT_LIT8: {
2455 Binop_22b<HAdd>(instruction, false, dex_pc);
2456 break;
2457 }
2458
2459 case Instruction::AND_INT_LIT8: {
2460 Binop_22b<HAnd>(instruction, false, dex_pc);
2461 break;
2462 }
2463
2464 case Instruction::OR_INT_LIT8: {
2465 Binop_22b<HOr>(instruction, false, dex_pc);
2466 break;
2467 }
2468
2469 case Instruction::XOR_INT_LIT8: {
2470 Binop_22b<HXor>(instruction, false, dex_pc);
2471 break;
2472 }
2473
2474 case Instruction::RSUB_INT_LIT8: {
2475 Binop_22b<HSub>(instruction, true, dex_pc);
2476 break;
2477 }
2478
2479 case Instruction::MUL_INT_LIT8: {
2480 Binop_22b<HMul>(instruction, false, dex_pc);
2481 break;
2482 }
2483
2484 case Instruction::DIV_INT_LIT16:
2485 case Instruction::DIV_INT_LIT8: {
2486 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2487 dex_pc, Primitive::kPrimInt, true, true);
2488 break;
2489 }
2490
2491 case Instruction::REM_INT_LIT16:
2492 case Instruction::REM_INT_LIT8: {
2493 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2494 dex_pc, Primitive::kPrimInt, true, false);
2495 break;
2496 }
2497
2498 case Instruction::SHL_INT_LIT8: {
2499 Binop_22b<HShl>(instruction, false, dex_pc);
2500 break;
2501 }
2502
2503 case Instruction::SHR_INT_LIT8: {
2504 Binop_22b<HShr>(instruction, false, dex_pc);
2505 break;
2506 }
2507
2508 case Instruction::USHR_INT_LIT8: {
2509 Binop_22b<HUShr>(instruction, false, dex_pc);
2510 break;
2511 }
2512
2513 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002514 if (!BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +00002515 return false;
2516 }
2517 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2518 break;
2519 }
2520
2521 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002522 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002523 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002524 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00002525 AppendInstruction(new (arena_) HNewArray(cls, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002526 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2527 break;
2528 }
2529
2530 case Instruction::FILLED_NEW_ARRAY: {
2531 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002532 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002533 uint32_t args[5];
2534 instruction.GetVarArgs(args);
2535 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2536 break;
2537 }
2538
2539 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2540 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002541 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002542 uint32_t register_index = instruction.VRegC_3rc();
2543 BuildFilledNewArray(
2544 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2545 break;
2546 }
2547
2548 case Instruction::FILL_ARRAY_DATA: {
2549 BuildFillArrayData(instruction, dex_pc);
2550 break;
2551 }
2552
2553 case Instruction::MOVE_RESULT:
2554 case Instruction::MOVE_RESULT_WIDE:
2555 case Instruction::MOVE_RESULT_OBJECT: {
2556 DCHECK(latest_result_ != nullptr);
2557 UpdateLocal(instruction.VRegA(), latest_result_);
2558 latest_result_ = nullptr;
2559 break;
2560 }
2561
2562 case Instruction::CMP_LONG: {
2563 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2564 break;
2565 }
2566
2567 case Instruction::CMPG_FLOAT: {
2568 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2569 break;
2570 }
2571
2572 case Instruction::CMPG_DOUBLE: {
2573 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2574 break;
2575 }
2576
2577 case Instruction::CMPL_FLOAT: {
2578 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2579 break;
2580 }
2581
2582 case Instruction::CMPL_DOUBLE: {
2583 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2584 break;
2585 }
2586
2587 case Instruction::NOP:
2588 break;
2589
2590 case Instruction::IGET:
2591 case Instruction::IGET_QUICK:
2592 case Instruction::IGET_WIDE:
2593 case Instruction::IGET_WIDE_QUICK:
2594 case Instruction::IGET_OBJECT:
2595 case Instruction::IGET_OBJECT_QUICK:
2596 case Instruction::IGET_BOOLEAN:
2597 case Instruction::IGET_BOOLEAN_QUICK:
2598 case Instruction::IGET_BYTE:
2599 case Instruction::IGET_BYTE_QUICK:
2600 case Instruction::IGET_CHAR:
2601 case Instruction::IGET_CHAR_QUICK:
2602 case Instruction::IGET_SHORT:
2603 case Instruction::IGET_SHORT_QUICK: {
2604 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2605 return false;
2606 }
2607 break;
2608 }
2609
2610 case Instruction::IPUT:
2611 case Instruction::IPUT_QUICK:
2612 case Instruction::IPUT_WIDE:
2613 case Instruction::IPUT_WIDE_QUICK:
2614 case Instruction::IPUT_OBJECT:
2615 case Instruction::IPUT_OBJECT_QUICK:
2616 case Instruction::IPUT_BOOLEAN:
2617 case Instruction::IPUT_BOOLEAN_QUICK:
2618 case Instruction::IPUT_BYTE:
2619 case Instruction::IPUT_BYTE_QUICK:
2620 case Instruction::IPUT_CHAR:
2621 case Instruction::IPUT_CHAR_QUICK:
2622 case Instruction::IPUT_SHORT:
2623 case Instruction::IPUT_SHORT_QUICK: {
2624 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2625 return false;
2626 }
2627 break;
2628 }
2629
2630 case Instruction::SGET:
2631 case Instruction::SGET_WIDE:
2632 case Instruction::SGET_OBJECT:
2633 case Instruction::SGET_BOOLEAN:
2634 case Instruction::SGET_BYTE:
2635 case Instruction::SGET_CHAR:
2636 case Instruction::SGET_SHORT: {
2637 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2638 return false;
2639 }
2640 break;
2641 }
2642
2643 case Instruction::SPUT:
2644 case Instruction::SPUT_WIDE:
2645 case Instruction::SPUT_OBJECT:
2646 case Instruction::SPUT_BOOLEAN:
2647 case Instruction::SPUT_BYTE:
2648 case Instruction::SPUT_CHAR:
2649 case Instruction::SPUT_SHORT: {
2650 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2651 return false;
2652 }
2653 break;
2654 }
2655
2656#define ARRAY_XX(kind, anticipated_type) \
2657 case Instruction::AGET##kind: { \
2658 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2659 break; \
2660 } \
2661 case Instruction::APUT##kind: { \
2662 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2663 break; \
2664 }
2665
2666 ARRAY_XX(, Primitive::kPrimInt);
2667 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2668 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2669 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2670 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2671 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2672 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2673
2674 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002675 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002676 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2677 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2678 break;
2679 }
2680
2681 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002682 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002683 AppendInstruction(
2684 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2685 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2686 break;
2687 }
2688
2689 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002690 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002691 AppendInstruction(
2692 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2693 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2694 break;
2695 }
2696
2697 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002698 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002699 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002700 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2701 break;
2702 }
2703
2704 case Instruction::MOVE_EXCEPTION: {
2705 AppendInstruction(new (arena_) HLoadException(dex_pc));
2706 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2707 AppendInstruction(new (arena_) HClearException(dex_pc));
2708 break;
2709 }
2710
2711 case Instruction::THROW: {
2712 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2713 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2714 // We finished building this block. Set the current block to null to avoid
2715 // adding dead instructions to it.
2716 current_block_ = nullptr;
2717 break;
2718 }
2719
2720 case Instruction::INSTANCE_OF: {
2721 uint8_t destination = instruction.VRegA_22c();
2722 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002723 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002724 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2725 break;
2726 }
2727
2728 case Instruction::CHECK_CAST: {
2729 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002730 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002731 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2732 break;
2733 }
2734
2735 case Instruction::MONITOR_ENTER: {
2736 AppendInstruction(new (arena_) HMonitorOperation(
2737 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2738 HMonitorOperation::OperationKind::kEnter,
2739 dex_pc));
2740 break;
2741 }
2742
2743 case Instruction::MONITOR_EXIT: {
2744 AppendInstruction(new (arena_) HMonitorOperation(
2745 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2746 HMonitorOperation::OperationKind::kExit,
2747 dex_pc));
2748 break;
2749 }
2750
2751 case Instruction::SPARSE_SWITCH:
2752 case Instruction::PACKED_SWITCH: {
2753 BuildSwitch(instruction, dex_pc);
2754 break;
2755 }
2756
2757 default:
2758 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002759 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002760 << " because of unhandled instruction "
2761 << instruction.Name();
2762 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2763 return false;
2764 }
2765 return true;
2766} // NOLINT(readability/fn_size)
2767
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002768ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2769 dex::TypeIndex type_index,
2770 const DexCompilationUnit& compilation_unit) const {
2771 return ClassLinker::LookupResolvedType(
2772 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2773}
2774
2775ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2776 // TODO: Cache the result in a Handle<mirror::Class>.
2777 const DexFile::MethodId& method_id =
2778 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2779 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2780}
2781
David Brazdildee58d62016-04-07 09:54:26 +00002782} // namespace art