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