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