blob: f5e49c2235dee989d6b42f3e0327cbf8bad02a7d [file] [log] [blame]
David Brazdildee58d62016-04-07 09:54:26 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_builder.h"
18
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
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700570static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
David Brazdildee58d62016-04-07 09:54:26 +0000571 Thread* self = Thread::Current();
572 return cu->IsConstructor()
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700573 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000574}
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) {
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700619 DCHECK(RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_))
David Brazdildee58d62016-04-07 09:54:26 +0000620 << "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) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100892 ScopedObjectAccess soa(Thread::Current());
893 StackHandleScope<1> hs(soa.Self());
894 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
895 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
896 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
897 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
898
David Brazdildee58d62016-04-07 09:54:26 +0000899 bool finalizable;
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100900 bool can_throw = NeedsAccessCheck(type_index, dex_cache, &finalizable);
David Brazdildee58d62016-04-07 09:54:26 +0000901
902 // Only the non-resolved entrypoint handles the finalizable class case. If we
903 // need access checks, then we haven't resolved the method and the class may
904 // again be finalizable.
905 QuickEntrypointEnum entrypoint = (finalizable || can_throw)
906 ? kQuickAllocObject
907 : kQuickAllocObjectInitialized;
908
David Brazdildee58d62016-04-07 09:54:26 +0000909 if (outer_dex_cache.Get() != dex_cache.Get()) {
910 // We currently do not support inlining allocations across dex files.
911 return false;
912 }
913
914 HLoadClass* load_class = new (arena_) HLoadClass(
915 graph_->GetCurrentMethod(),
916 type_index,
917 outer_dex_file,
918 IsOutermostCompilingClass(type_index),
919 dex_pc,
920 /*needs_access_check*/ can_throw,
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100921 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_cache, type_index));
David Brazdildee58d62016-04-07 09:54:26 +0000922
923 AppendInstruction(load_class);
924 HInstruction* cls = load_class;
925 if (!IsInitialized(resolved_class)) {
926 cls = new (arena_) HClinitCheck(load_class, dex_pc);
927 AppendInstruction(cls);
928 }
929
930 AppendInstruction(new (arena_) HNewInstance(
931 cls,
932 graph_->GetCurrentMethod(),
933 dex_pc,
934 type_index,
935 *dex_compilation_unit_->GetDexFile(),
936 can_throw,
937 finalizable,
938 entrypoint));
939 return true;
940}
941
942static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
943 SHARED_REQUIRES(Locks::mutator_lock_) {
944 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
945}
946
947bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
948 if (cls.Get() == nullptr) {
949 return false;
950 }
951
952 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
953 // check whether the class is in an image for the AOT compilation.
954 if (cls->IsInitialized() &&
955 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
956 return true;
957 }
958
959 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
960 return true;
961 }
962
963 // TODO: We should walk over the inlined methods, but we don't pass
964 // that information to the builder.
965 if (IsSubClass(GetCompilingClass(), cls.Get())) {
966 return true;
967 }
968
969 return false;
970}
971
972HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
973 uint32_t dex_pc,
974 ArtMethod* resolved_method,
975 uint32_t method_idx,
976 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
977 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
978 Thread* self = Thread::Current();
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100979 StackHandleScope<2> hs(self);
980 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
981 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +0000982 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
983 Handle<mirror::Class> resolved_method_class(hs.NewHandle(resolved_method->GetDeclaringClass()));
984
985 // The index at which the method's class is stored in the DexCache's type array.
986 uint32_t storage_index = DexFile::kDexNoIndex;
987 bool is_outer_class = (resolved_method->GetDeclaringClass() == outer_class.Get());
988 if (is_outer_class) {
989 storage_index = outer_class->GetDexTypeIndex();
990 } else if (outer_dex_cache.Get() == dex_cache.Get()) {
991 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer.
992 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(),
993 GetCompilingClass(),
994 resolved_method,
995 method_idx,
996 &storage_index);
997 }
998
999 HClinitCheck* clinit_check = nullptr;
1000
1001 if (IsInitialized(resolved_method_class)) {
1002 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
1003 } else if (storage_index != DexFile::kDexNoIndex) {
1004 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1005 HLoadClass* load_class = new (arena_) HLoadClass(
1006 graph_->GetCurrentMethod(),
1007 storage_index,
1008 outer_dex_file,
1009 is_outer_class,
1010 dex_pc,
1011 /*needs_access_check*/ false,
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001012 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_cache, storage_index));
David Brazdildee58d62016-04-07 09:54:26 +00001013 AppendInstruction(load_class);
1014 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc);
1015 AppendInstruction(clinit_check);
1016 }
1017 return clinit_check;
1018}
1019
1020bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1021 uint32_t number_of_vreg_arguments,
1022 uint32_t* args,
1023 uint32_t register_index,
1024 bool is_range,
1025 const char* descriptor,
1026 size_t start_index,
1027 size_t* argument_index) {
1028 uint32_t descriptor_index = 1; // Skip the return type.
1029
1030 for (size_t i = start_index;
1031 // Make sure we don't go over the expected arguments or over the number of
1032 // dex registers given. If the instruction was seen as dead by the verifier,
1033 // it hasn't been properly checked.
1034 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1035 i++, (*argument_index)++) {
1036 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1037 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1038 if (!is_range
1039 && is_wide
1040 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1041 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1042 // reject any class where this is violated. However, the verifier only does these checks
1043 // on non trivially dead instructions, so we just bailout the compilation.
1044 VLOG(compiler) << "Did not compile "
1045 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1046 << " because of non-sequential dex register pair in wide argument";
1047 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1048 return false;
1049 }
1050 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1051 invoke->SetArgumentAt(*argument_index, arg);
1052 if (is_wide) {
1053 i++;
1054 }
1055 }
1056
1057 if (*argument_index != invoke->GetNumberOfArguments()) {
1058 VLOG(compiler) << "Did not compile "
1059 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
1060 << " because of wrong number of arguments in invoke instruction";
1061 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1062 return false;
1063 }
1064
1065 if (invoke->IsInvokeStaticOrDirect() &&
1066 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1067 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1068 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1069 (*argument_index)++;
1070 }
1071
1072 return true;
1073}
1074
1075bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1076 uint32_t number_of_vreg_arguments,
1077 uint32_t* args,
1078 uint32_t register_index,
1079 bool is_range,
1080 const char* descriptor,
1081 HClinitCheck* clinit_check) {
1082 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1083
1084 size_t start_index = 0;
1085 size_t argument_index = 0;
1086 if (invoke->GetOriginalInvokeType() != InvokeType::kStatic) { // Instance call.
1087 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
1088 HNullCheck* null_check = new (arena_) HNullCheck(arg, invoke->GetDexPc());
1089 AppendInstruction(null_check);
1090 invoke->SetArgumentAt(0, null_check);
1091 start_index = 1;
1092 argument_index = 1;
1093 }
1094
1095 if (!SetupInvokeArguments(invoke,
1096 number_of_vreg_arguments,
1097 args,
1098 register_index,
1099 is_range,
1100 descriptor,
1101 start_index,
1102 &argument_index)) {
1103 return false;
1104 }
1105
1106 if (clinit_check != nullptr) {
1107 // Add the class initialization check as last input of `invoke`.
1108 DCHECK(invoke->IsInvokeStaticOrDirect());
1109 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1110 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1111 invoke->SetArgumentAt(argument_index, clinit_check);
1112 argument_index++;
1113 }
1114
1115 AppendInstruction(invoke);
1116 latest_result_ = invoke;
1117
1118 return true;
1119}
1120
1121bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1122 uint32_t number_of_vreg_arguments,
1123 uint32_t* args,
1124 uint32_t register_index,
1125 bool is_range,
1126 const char* descriptor) {
1127 DCHECK(invoke->IsInvokeStaticOrDirect());
1128 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1129
1130 size_t start_index = 1;
1131 size_t argument_index = 0;
1132 if (!SetupInvokeArguments(invoke,
1133 number_of_vreg_arguments,
1134 args,
1135 register_index,
1136 is_range,
1137 descriptor,
1138 start_index,
1139 &argument_index)) {
1140 return false;
1141 }
1142
1143 AppendInstruction(invoke);
1144
1145 // This is a StringFactory call, not an actual String constructor. Its result
1146 // replaces the empty String pre-allocated by NewInstance.
1147 uint32_t orig_this_reg = is_range ? register_index : args[0];
1148 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1149
1150 // Replacing the NewInstance might render it redundant. Keep a list of these
1151 // to be visited once it is clear whether it is has remaining uses.
1152 if (arg_this->IsNewInstance()) {
1153 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1154 } else {
1155 DCHECK(arg_this->IsPhi());
1156 // NewInstance is not the direct input of the StringFactory call. It might
1157 // be redundant but optimizing this case is not worth the effort.
1158 }
1159
1160 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1161 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1162 if ((*current_locals_)[vreg] == arg_this) {
1163 (*current_locals_)[vreg] = invoke;
1164 }
1165 }
1166
1167 return true;
1168}
1169
1170static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1171 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1172 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1173 return Primitive::GetType(type[0]);
1174}
1175
1176bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1177 uint32_t dex_pc,
1178 bool is_put) {
1179 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1180 uint32_t obj_reg = instruction.VRegB_22c();
1181 uint16_t field_index;
1182 if (instruction.IsQuickened()) {
1183 if (!CanDecodeQuickenedInfo()) {
1184 return false;
1185 }
1186 field_index = LookupQuickenedInfo(dex_pc);
1187 } else {
1188 field_index = instruction.VRegC_22c();
1189 }
1190
1191 ScopedObjectAccess soa(Thread::Current());
1192 ArtField* resolved_field =
1193 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa);
1194
1195
1196 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
1197 HInstruction* null_check = new (arena_) HNullCheck(object, dex_pc);
1198 AppendInstruction(null_check);
1199
1200 Primitive::Type field_type = (resolved_field == nullptr)
1201 ? GetFieldAccessType(*dex_file_, field_index)
1202 : resolved_field->GetTypeAsPrimitiveType();
1203 if (is_put) {
1204 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1205 HInstruction* field_set = nullptr;
1206 if (resolved_field == nullptr) {
1207 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1208 field_set = new (arena_) HUnresolvedInstanceFieldSet(null_check,
1209 value,
1210 field_type,
1211 field_index,
1212 dex_pc);
1213 } else {
1214 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
1215 field_set = new (arena_) HInstanceFieldSet(null_check,
1216 value,
1217 field_type,
1218 resolved_field->GetOffset(),
1219 resolved_field->IsVolatile(),
1220 field_index,
1221 class_def_index,
1222 *dex_file_,
1223 dex_compilation_unit_->GetDexCache(),
1224 dex_pc);
1225 }
1226 AppendInstruction(field_set);
1227 } else {
1228 HInstruction* field_get = nullptr;
1229 if (resolved_field == nullptr) {
1230 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1231 field_get = new (arena_) HUnresolvedInstanceFieldGet(null_check,
1232 field_type,
1233 field_index,
1234 dex_pc);
1235 } else {
1236 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
1237 field_get = new (arena_) HInstanceFieldGet(null_check,
1238 field_type,
1239 resolved_field->GetOffset(),
1240 resolved_field->IsVolatile(),
1241 field_index,
1242 class_def_index,
1243 *dex_file_,
1244 dex_compilation_unit_->GetDexCache(),
1245 dex_pc);
1246 }
1247 AppendInstruction(field_get);
1248 UpdateLocal(source_or_dest_reg, field_get);
1249 }
1250
1251 return true;
1252}
1253
1254static mirror::Class* GetClassFrom(CompilerDriver* driver,
1255 const DexCompilationUnit& compilation_unit) {
1256 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001257 StackHandleScope<1> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +00001258 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1259 soa.Decode<mirror::ClassLoader*>(compilation_unit.GetClassLoader())));
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001260 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001261
1262 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1263}
1264
1265mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1266 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1267}
1268
1269mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1270 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1271}
1272
1273bool HInstructionBuilder::IsOutermostCompilingClass(uint16_t type_index) const {
1274 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001275 StackHandleScope<3> hs(soa.Self());
1276 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001277 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1278 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1279 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1280 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1281 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1282
1283 // GetOutermostCompilingClass returns null when the class is unresolved
1284 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1285 // we are compiling it.
1286 // When this happens we cannot establish a direct relation between the current
1287 // class and the outer class, so we return false.
1288 // (Note that this is only used for optimizing invokes and field accesses)
1289 return (cls.Get() != nullptr) && (outer_class.Get() == cls.Get());
1290}
1291
1292void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
1293 uint32_t dex_pc,
1294 bool is_put,
1295 Primitive::Type field_type) {
1296 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1297 uint16_t field_index = instruction.VRegB_21c();
1298
1299 if (is_put) {
1300 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1301 AppendInstruction(
1302 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1303 } else {
1304 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1305 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1306 }
1307}
1308
1309bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1310 uint32_t dex_pc,
1311 bool is_put) {
1312 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1313 uint16_t field_index = instruction.VRegB_21c();
1314
1315 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001316 StackHandleScope<3> hs(soa.Self());
1317 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001318 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1319 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader())));
1320 ArtField* resolved_field = compiler_driver_->ResolveField(
1321 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true);
1322
1323 if (resolved_field == nullptr) {
1324 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1325 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1326 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1327 return true;
1328 }
1329
1330 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
1331 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001332 Handle<mirror::DexCache> outer_dex_cache = outer_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001333 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1334
1335 // The index at which the field's class is stored in the DexCache's type array.
1336 uint32_t storage_index;
1337 bool is_outer_class = (outer_class.Get() == resolved_field->GetDeclaringClass());
1338 if (is_outer_class) {
1339 storage_index = outer_class->GetDexTypeIndex();
1340 } else if (outer_dex_cache.Get() != dex_cache.Get()) {
1341 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout.
1342 return false;
1343 } else {
1344 // TODO: This is rather expensive. Perf it and cache the results if needed.
1345 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField(
1346 outer_dex_cache.Get(),
1347 GetCompilingClass(),
1348 resolved_field,
1349 field_index,
1350 &storage_index);
1351 bool can_easily_access = is_put ? pair.second : pair.first;
1352 if (!can_easily_access) {
1353 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1354 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1355 return true;
1356 }
1357 }
1358
1359 bool is_in_cache =
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001360 compiler_driver_->CanAssumeTypeIsPresentInDexCache(outer_dex_cache, storage_index);
David Brazdildee58d62016-04-07 09:54:26 +00001361 HLoadClass* constant = new (arena_) HLoadClass(graph_->GetCurrentMethod(),
1362 storage_index,
1363 outer_dex_file,
1364 is_outer_class,
1365 dex_pc,
1366 /*needs_access_check*/ false,
1367 is_in_cache);
1368 AppendInstruction(constant);
1369
1370 HInstruction* cls = constant;
1371
1372 Handle<mirror::Class> klass(hs.NewHandle(resolved_field->GetDeclaringClass()));
1373 if (!IsInitialized(klass)) {
1374 cls = new (arena_) HClinitCheck(constant, dex_pc);
1375 AppendInstruction(cls);
1376 }
1377
1378 uint16_t class_def_index = klass->GetDexClassDefIndex();
1379 if (is_put) {
1380 // We need to keep the class alive before loading the value.
1381 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1382 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1383 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1384 value,
1385 field_type,
1386 resolved_field->GetOffset(),
1387 resolved_field->IsVolatile(),
1388 field_index,
1389 class_def_index,
1390 *dex_file_,
1391 dex_cache_,
1392 dex_pc));
1393 } else {
1394 AppendInstruction(new (arena_) HStaticFieldGet(cls,
1395 field_type,
1396 resolved_field->GetOffset(),
1397 resolved_field->IsVolatile(),
1398 field_index,
1399 class_def_index,
1400 *dex_file_,
1401 dex_cache_,
1402 dex_pc));
1403 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1404 }
1405 return true;
1406}
1407
1408void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1409 uint16_t first_vreg,
1410 int64_t second_vreg_or_constant,
1411 uint32_t dex_pc,
1412 Primitive::Type type,
1413 bool second_is_constant,
1414 bool isDiv) {
1415 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1416
1417 HInstruction* first = LoadLocal(first_vreg, type);
1418 HInstruction* second = nullptr;
1419 if (second_is_constant) {
1420 if (type == Primitive::kPrimInt) {
1421 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1422 } else {
1423 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1424 }
1425 } else {
1426 second = LoadLocal(second_vreg_or_constant, type);
1427 }
1428
1429 if (!second_is_constant
1430 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1431 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1432 second = new (arena_) HDivZeroCheck(second, dex_pc);
1433 AppendInstruction(second);
1434 }
1435
1436 if (isDiv) {
1437 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1438 } else {
1439 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1440 }
1441 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1442}
1443
1444void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1445 uint32_t dex_pc,
1446 bool is_put,
1447 Primitive::Type anticipated_type) {
1448 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1449 uint8_t array_reg = instruction.VRegB_23x();
1450 uint8_t index_reg = instruction.VRegC_23x();
1451
1452 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
1453 object = new (arena_) HNullCheck(object, dex_pc);
1454 AppendInstruction(object);
1455
1456 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1457 AppendInstruction(length);
1458 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1459 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1460 AppendInstruction(index);
1461 if (is_put) {
1462 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1463 // TODO: Insert a type check node if the type is Object.
1464 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1465 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1466 AppendInstruction(aset);
1467 } else {
1468 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1469 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1470 AppendInstruction(aget);
1471 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1472 }
1473 graph_->SetHasBoundsChecks(true);
1474}
1475
1476void HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1477 uint32_t type_index,
1478 uint32_t number_of_vreg_arguments,
1479 bool is_range,
1480 uint32_t* args,
1481 uint32_t register_index) {
1482 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
1483 bool finalizable;
1484 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
1485 ? kQuickAllocArrayWithAccessCheck
1486 : kQuickAllocArray;
1487 HInstruction* object = new (arena_) HNewArray(length,
1488 graph_->GetCurrentMethod(),
1489 dex_pc,
1490 type_index,
1491 *dex_compilation_unit_->GetDexFile(),
1492 entrypoint);
1493 AppendInstruction(object);
1494
1495 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1496 DCHECK_EQ(descriptor[0], '[') << descriptor;
1497 char primitive = descriptor[1];
1498 DCHECK(primitive == 'I'
1499 || primitive == 'L'
1500 || primitive == '[') << descriptor;
1501 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1502 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1503
1504 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1505 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1506 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1507 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1508 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1509 AppendInstruction(aset);
1510 }
1511 latest_result_ = object;
1512}
1513
1514template <typename T>
1515void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1516 const T* data,
1517 uint32_t element_count,
1518 Primitive::Type anticipated_type,
1519 uint32_t dex_pc) {
1520 for (uint32_t i = 0; i < element_count; ++i) {
1521 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1522 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1523 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1524 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1525 AppendInstruction(aset);
1526 }
1527}
1528
1529void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
1530 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
1531 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc);
1532 AppendInstruction(null_check);
1533
1534 HInstruction* length = new (arena_) HArrayLength(null_check, dex_pc);
1535 AppendInstruction(length);
1536
1537 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1538 const Instruction::ArrayDataPayload* payload =
1539 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1540 const uint8_t* data = payload->data;
1541 uint32_t element_count = payload->element_count;
1542
1543 // Implementation of this DEX instruction seems to be that the bounds check is
1544 // done before doing any stores.
1545 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1546 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1547
1548 switch (payload->element_width) {
1549 case 1:
1550 BuildFillArrayData(null_check,
1551 reinterpret_cast<const int8_t*>(data),
1552 element_count,
1553 Primitive::kPrimByte,
1554 dex_pc);
1555 break;
1556 case 2:
1557 BuildFillArrayData(null_check,
1558 reinterpret_cast<const int16_t*>(data),
1559 element_count,
1560 Primitive::kPrimShort,
1561 dex_pc);
1562 break;
1563 case 4:
1564 BuildFillArrayData(null_check,
1565 reinterpret_cast<const int32_t*>(data),
1566 element_count,
1567 Primitive::kPrimInt,
1568 dex_pc);
1569 break;
1570 case 8:
1571 BuildFillWideArrayData(null_check,
1572 reinterpret_cast<const int64_t*>(data),
1573 element_count,
1574 dex_pc);
1575 break;
1576 default:
1577 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1578 }
1579 graph_->SetHasBoundsChecks(true);
1580}
1581
1582void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1583 const int64_t* data,
1584 uint32_t element_count,
1585 uint32_t dex_pc) {
1586 for (uint32_t i = 0; i < element_count; ++i) {
1587 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1588 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1589 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1590 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1591 AppendInstruction(aset);
1592 }
1593}
1594
1595static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
1596 SHARED_REQUIRES(Locks::mutator_lock_) {
1597 if (cls.Get() == nullptr) {
1598 return TypeCheckKind::kUnresolvedCheck;
1599 } else if (cls->IsInterface()) {
1600 return TypeCheckKind::kInterfaceCheck;
1601 } else if (cls->IsArrayClass()) {
1602 if (cls->GetComponentType()->IsObjectClass()) {
1603 return TypeCheckKind::kArrayObjectCheck;
1604 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1605 return TypeCheckKind::kExactCheck;
1606 } else {
1607 return TypeCheckKind::kArrayCheck;
1608 }
1609 } else if (cls->IsFinal()) {
1610 return TypeCheckKind::kExactCheck;
1611 } else if (cls->IsAbstract()) {
1612 return TypeCheckKind::kAbstractClassCheck;
1613 } else {
1614 return TypeCheckKind::kClassHierarchyCheck;
1615 }
1616}
1617
1618void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1619 uint8_t destination,
1620 uint8_t reference,
1621 uint16_t type_index,
1622 uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001623 ScopedObjectAccess soa(Thread::Current());
1624 StackHandleScope<1> hs(soa.Self());
1625 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1626 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1627 Handle<mirror::Class> resolved_class(hs.NewHandle(dex_cache->GetResolvedType(type_index)));
1628
David Brazdildee58d62016-04-07 09:54:26 +00001629 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
1630 dex_compilation_unit_->GetDexMethodIndex(),
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001631 dex_cache,
1632 type_index);
David Brazdildee58d62016-04-07 09:54:26 +00001633
1634 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
1635 HLoadClass* cls = new (arena_) HLoadClass(
1636 graph_->GetCurrentMethod(),
1637 type_index,
1638 dex_file,
1639 IsOutermostCompilingClass(type_index),
1640 dex_pc,
1641 !can_access,
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001642 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_cache, type_index));
David Brazdildee58d62016-04-07 09:54:26 +00001643 AppendInstruction(cls);
1644
1645 TypeCheckKind check_kind = ComputeTypeCheckKind(resolved_class);
1646 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1647 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1648 UpdateLocal(destination, current_block_->GetLastInstruction());
1649 } else {
1650 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1651 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1652 // which may throw. If it succeeds BoundType sets the new type of `object`
1653 // for all subsequent uses.
1654 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1655 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1656 UpdateLocal(reference, current_block_->GetLastInstruction());
1657 }
1658}
1659
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001660bool HInstructionBuilder::NeedsAccessCheck(uint32_t type_index,
1661 Handle<mirror::DexCache> dex_cache,
1662 bool* finalizable) const {
David Brazdildee58d62016-04-07 09:54:26 +00001663 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001664 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index, finalizable);
1665}
1666
1667bool HInstructionBuilder::NeedsAccessCheck(uint32_t type_index, bool* finalizable) const {
1668 ScopedObjectAccess soa(Thread::Current());
1669 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
1670 return NeedsAccessCheck(type_index, dex_cache, finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001671}
1672
1673bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1674 return interpreter_metadata_ != nullptr;
1675}
1676
1677uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1678 DCHECK(interpreter_metadata_ != nullptr);
1679
1680 // First check if the info has already been decoded from `interpreter_metadata_`.
1681 auto it = skipped_interpreter_metadata_.find(dex_pc);
1682 if (it != skipped_interpreter_metadata_.end()) {
1683 // Remove the entry from the map and return the parsed info.
1684 uint16_t value_in_map = it->second;
1685 skipped_interpreter_metadata_.erase(it);
1686 return value_in_map;
1687 }
1688
1689 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1690 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1691 while (true) {
1692 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1693 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1694 DCHECK_LE(dex_pc_in_map, dex_pc);
1695
1696 if (dex_pc_in_map == dex_pc) {
1697 return value_in_map;
1698 } else {
1699 skipped_interpreter_metadata_.Put(dex_pc_in_map, value_in_map);
1700 }
1701 }
1702}
1703
1704bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1705 switch (instruction.Opcode()) {
1706 case Instruction::CONST_4: {
1707 int32_t register_index = instruction.VRegA();
1708 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1709 UpdateLocal(register_index, constant);
1710 break;
1711 }
1712
1713 case Instruction::CONST_16: {
1714 int32_t register_index = instruction.VRegA();
1715 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1716 UpdateLocal(register_index, constant);
1717 break;
1718 }
1719
1720 case Instruction::CONST: {
1721 int32_t register_index = instruction.VRegA();
1722 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1723 UpdateLocal(register_index, constant);
1724 break;
1725 }
1726
1727 case Instruction::CONST_HIGH16: {
1728 int32_t register_index = instruction.VRegA();
1729 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1730 UpdateLocal(register_index, constant);
1731 break;
1732 }
1733
1734 case Instruction::CONST_WIDE_16: {
1735 int32_t register_index = instruction.VRegA();
1736 // Get 16 bits of constant value, sign extended to 64 bits.
1737 int64_t value = instruction.VRegB_21s();
1738 value <<= 48;
1739 value >>= 48;
1740 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1741 UpdateLocal(register_index, constant);
1742 break;
1743 }
1744
1745 case Instruction::CONST_WIDE_32: {
1746 int32_t register_index = instruction.VRegA();
1747 // Get 32 bits of constant value, sign extended to 64 bits.
1748 int64_t value = instruction.VRegB_31i();
1749 value <<= 32;
1750 value >>= 32;
1751 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1752 UpdateLocal(register_index, constant);
1753 break;
1754 }
1755
1756 case Instruction::CONST_WIDE: {
1757 int32_t register_index = instruction.VRegA();
1758 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1759 UpdateLocal(register_index, constant);
1760 break;
1761 }
1762
1763 case Instruction::CONST_WIDE_HIGH16: {
1764 int32_t register_index = instruction.VRegA();
1765 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1766 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1767 UpdateLocal(register_index, constant);
1768 break;
1769 }
1770
1771 // Note that the SSA building will refine the types.
1772 case Instruction::MOVE:
1773 case Instruction::MOVE_FROM16:
1774 case Instruction::MOVE_16: {
1775 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1776 UpdateLocal(instruction.VRegA(), value);
1777 break;
1778 }
1779
1780 // Note that the SSA building will refine the types.
1781 case Instruction::MOVE_WIDE:
1782 case Instruction::MOVE_WIDE_FROM16:
1783 case Instruction::MOVE_WIDE_16: {
1784 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1785 UpdateLocal(instruction.VRegA(), value);
1786 break;
1787 }
1788
1789 case Instruction::MOVE_OBJECT:
1790 case Instruction::MOVE_OBJECT_16:
1791 case Instruction::MOVE_OBJECT_FROM16: {
1792 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
1793 UpdateLocal(instruction.VRegA(), value);
1794 break;
1795 }
1796
1797 case Instruction::RETURN_VOID_NO_BARRIER:
1798 case Instruction::RETURN_VOID: {
1799 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1800 break;
1801 }
1802
1803#define IF_XX(comparison, cond) \
1804 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1805 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1806
1807 IF_XX(HEqual, EQ);
1808 IF_XX(HNotEqual, NE);
1809 IF_XX(HLessThan, LT);
1810 IF_XX(HLessThanOrEqual, LE);
1811 IF_XX(HGreaterThan, GT);
1812 IF_XX(HGreaterThanOrEqual, GE);
1813
1814 case Instruction::GOTO:
1815 case Instruction::GOTO_16:
1816 case Instruction::GOTO_32: {
1817 AppendInstruction(new (arena_) HGoto(dex_pc));
1818 current_block_ = nullptr;
1819 break;
1820 }
1821
1822 case Instruction::RETURN: {
1823 BuildReturn(instruction, return_type_, dex_pc);
1824 break;
1825 }
1826
1827 case Instruction::RETURN_OBJECT: {
1828 BuildReturn(instruction, return_type_, dex_pc);
1829 break;
1830 }
1831
1832 case Instruction::RETURN_WIDE: {
1833 BuildReturn(instruction, return_type_, dex_pc);
1834 break;
1835 }
1836
1837 case Instruction::INVOKE_DIRECT:
1838 case Instruction::INVOKE_INTERFACE:
1839 case Instruction::INVOKE_STATIC:
1840 case Instruction::INVOKE_SUPER:
1841 case Instruction::INVOKE_VIRTUAL:
1842 case Instruction::INVOKE_VIRTUAL_QUICK: {
1843 uint16_t method_idx;
1844 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1845 if (!CanDecodeQuickenedInfo()) {
1846 return false;
1847 }
1848 method_idx = LookupQuickenedInfo(dex_pc);
1849 } else {
1850 method_idx = instruction.VRegB_35c();
1851 }
1852 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1853 uint32_t args[5];
1854 instruction.GetVarArgs(args);
1855 if (!BuildInvoke(instruction, dex_pc, method_idx,
1856 number_of_vreg_arguments, false, args, -1)) {
1857 return false;
1858 }
1859 break;
1860 }
1861
1862 case Instruction::INVOKE_DIRECT_RANGE:
1863 case Instruction::INVOKE_INTERFACE_RANGE:
1864 case Instruction::INVOKE_STATIC_RANGE:
1865 case Instruction::INVOKE_SUPER_RANGE:
1866 case Instruction::INVOKE_VIRTUAL_RANGE:
1867 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1868 uint16_t method_idx;
1869 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1870 if (!CanDecodeQuickenedInfo()) {
1871 return false;
1872 }
1873 method_idx = LookupQuickenedInfo(dex_pc);
1874 } else {
1875 method_idx = instruction.VRegB_3rc();
1876 }
1877 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1878 uint32_t register_index = instruction.VRegC();
1879 if (!BuildInvoke(instruction, dex_pc, method_idx,
1880 number_of_vreg_arguments, true, nullptr, register_index)) {
1881 return false;
1882 }
1883 break;
1884 }
1885
1886 case Instruction::NEG_INT: {
1887 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
1888 break;
1889 }
1890
1891 case Instruction::NEG_LONG: {
1892 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
1893 break;
1894 }
1895
1896 case Instruction::NEG_FLOAT: {
1897 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
1898 break;
1899 }
1900
1901 case Instruction::NEG_DOUBLE: {
1902 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
1903 break;
1904 }
1905
1906 case Instruction::NOT_INT: {
1907 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
1908 break;
1909 }
1910
1911 case Instruction::NOT_LONG: {
1912 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
1913 break;
1914 }
1915
1916 case Instruction::INT_TO_LONG: {
1917 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
1918 break;
1919 }
1920
1921 case Instruction::INT_TO_FLOAT: {
1922 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
1923 break;
1924 }
1925
1926 case Instruction::INT_TO_DOUBLE: {
1927 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
1928 break;
1929 }
1930
1931 case Instruction::LONG_TO_INT: {
1932 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
1933 break;
1934 }
1935
1936 case Instruction::LONG_TO_FLOAT: {
1937 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
1938 break;
1939 }
1940
1941 case Instruction::LONG_TO_DOUBLE: {
1942 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
1943 break;
1944 }
1945
1946 case Instruction::FLOAT_TO_INT: {
1947 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
1948 break;
1949 }
1950
1951 case Instruction::FLOAT_TO_LONG: {
1952 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
1953 break;
1954 }
1955
1956 case Instruction::FLOAT_TO_DOUBLE: {
1957 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
1958 break;
1959 }
1960
1961 case Instruction::DOUBLE_TO_INT: {
1962 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
1963 break;
1964 }
1965
1966 case Instruction::DOUBLE_TO_LONG: {
1967 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
1968 break;
1969 }
1970
1971 case Instruction::DOUBLE_TO_FLOAT: {
1972 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
1973 break;
1974 }
1975
1976 case Instruction::INT_TO_BYTE: {
1977 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
1978 break;
1979 }
1980
1981 case Instruction::INT_TO_SHORT: {
1982 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
1983 break;
1984 }
1985
1986 case Instruction::INT_TO_CHAR: {
1987 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
1988 break;
1989 }
1990
1991 case Instruction::ADD_INT: {
1992 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
1993 break;
1994 }
1995
1996 case Instruction::ADD_LONG: {
1997 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
1998 break;
1999 }
2000
2001 case Instruction::ADD_DOUBLE: {
2002 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2003 break;
2004 }
2005
2006 case Instruction::ADD_FLOAT: {
2007 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2008 break;
2009 }
2010
2011 case Instruction::SUB_INT: {
2012 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2013 break;
2014 }
2015
2016 case Instruction::SUB_LONG: {
2017 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2018 break;
2019 }
2020
2021 case Instruction::SUB_FLOAT: {
2022 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2023 break;
2024 }
2025
2026 case Instruction::SUB_DOUBLE: {
2027 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2028 break;
2029 }
2030
2031 case Instruction::ADD_INT_2ADDR: {
2032 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2033 break;
2034 }
2035
2036 case Instruction::MUL_INT: {
2037 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2038 break;
2039 }
2040
2041 case Instruction::MUL_LONG: {
2042 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2043 break;
2044 }
2045
2046 case Instruction::MUL_FLOAT: {
2047 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2048 break;
2049 }
2050
2051 case Instruction::MUL_DOUBLE: {
2052 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2053 break;
2054 }
2055
2056 case Instruction::DIV_INT: {
2057 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2058 dex_pc, Primitive::kPrimInt, false, true);
2059 break;
2060 }
2061
2062 case Instruction::DIV_LONG: {
2063 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2064 dex_pc, Primitive::kPrimLong, false, true);
2065 break;
2066 }
2067
2068 case Instruction::DIV_FLOAT: {
2069 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2070 break;
2071 }
2072
2073 case Instruction::DIV_DOUBLE: {
2074 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2075 break;
2076 }
2077
2078 case Instruction::REM_INT: {
2079 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2080 dex_pc, Primitive::kPrimInt, false, false);
2081 break;
2082 }
2083
2084 case Instruction::REM_LONG: {
2085 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2086 dex_pc, Primitive::kPrimLong, false, false);
2087 break;
2088 }
2089
2090 case Instruction::REM_FLOAT: {
2091 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2092 break;
2093 }
2094
2095 case Instruction::REM_DOUBLE: {
2096 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2097 break;
2098 }
2099
2100 case Instruction::AND_INT: {
2101 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2102 break;
2103 }
2104
2105 case Instruction::AND_LONG: {
2106 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2107 break;
2108 }
2109
2110 case Instruction::SHL_INT: {
2111 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2112 break;
2113 }
2114
2115 case Instruction::SHL_LONG: {
2116 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2117 break;
2118 }
2119
2120 case Instruction::SHR_INT: {
2121 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2122 break;
2123 }
2124
2125 case Instruction::SHR_LONG: {
2126 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2127 break;
2128 }
2129
2130 case Instruction::USHR_INT: {
2131 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2132 break;
2133 }
2134
2135 case Instruction::USHR_LONG: {
2136 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2137 break;
2138 }
2139
2140 case Instruction::OR_INT: {
2141 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2142 break;
2143 }
2144
2145 case Instruction::OR_LONG: {
2146 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2147 break;
2148 }
2149
2150 case Instruction::XOR_INT: {
2151 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2152 break;
2153 }
2154
2155 case Instruction::XOR_LONG: {
2156 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2157 break;
2158 }
2159
2160 case Instruction::ADD_LONG_2ADDR: {
2161 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2162 break;
2163 }
2164
2165 case Instruction::ADD_DOUBLE_2ADDR: {
2166 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2167 break;
2168 }
2169
2170 case Instruction::ADD_FLOAT_2ADDR: {
2171 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2172 break;
2173 }
2174
2175 case Instruction::SUB_INT_2ADDR: {
2176 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2177 break;
2178 }
2179
2180 case Instruction::SUB_LONG_2ADDR: {
2181 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2182 break;
2183 }
2184
2185 case Instruction::SUB_FLOAT_2ADDR: {
2186 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2187 break;
2188 }
2189
2190 case Instruction::SUB_DOUBLE_2ADDR: {
2191 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2192 break;
2193 }
2194
2195 case Instruction::MUL_INT_2ADDR: {
2196 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2197 break;
2198 }
2199
2200 case Instruction::MUL_LONG_2ADDR: {
2201 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2202 break;
2203 }
2204
2205 case Instruction::MUL_FLOAT_2ADDR: {
2206 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2207 break;
2208 }
2209
2210 case Instruction::MUL_DOUBLE_2ADDR: {
2211 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2212 break;
2213 }
2214
2215 case Instruction::DIV_INT_2ADDR: {
2216 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2217 dex_pc, Primitive::kPrimInt, false, true);
2218 break;
2219 }
2220
2221 case Instruction::DIV_LONG_2ADDR: {
2222 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2223 dex_pc, Primitive::kPrimLong, false, true);
2224 break;
2225 }
2226
2227 case Instruction::REM_INT_2ADDR: {
2228 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2229 dex_pc, Primitive::kPrimInt, false, false);
2230 break;
2231 }
2232
2233 case Instruction::REM_LONG_2ADDR: {
2234 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2235 dex_pc, Primitive::kPrimLong, false, false);
2236 break;
2237 }
2238
2239 case Instruction::REM_FLOAT_2ADDR: {
2240 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2241 break;
2242 }
2243
2244 case Instruction::REM_DOUBLE_2ADDR: {
2245 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2246 break;
2247 }
2248
2249 case Instruction::SHL_INT_2ADDR: {
2250 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2251 break;
2252 }
2253
2254 case Instruction::SHL_LONG_2ADDR: {
2255 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2256 break;
2257 }
2258
2259 case Instruction::SHR_INT_2ADDR: {
2260 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2261 break;
2262 }
2263
2264 case Instruction::SHR_LONG_2ADDR: {
2265 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2266 break;
2267 }
2268
2269 case Instruction::USHR_INT_2ADDR: {
2270 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2271 break;
2272 }
2273
2274 case Instruction::USHR_LONG_2ADDR: {
2275 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2276 break;
2277 }
2278
2279 case Instruction::DIV_FLOAT_2ADDR: {
2280 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2281 break;
2282 }
2283
2284 case Instruction::DIV_DOUBLE_2ADDR: {
2285 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2286 break;
2287 }
2288
2289 case Instruction::AND_INT_2ADDR: {
2290 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2291 break;
2292 }
2293
2294 case Instruction::AND_LONG_2ADDR: {
2295 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2296 break;
2297 }
2298
2299 case Instruction::OR_INT_2ADDR: {
2300 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2301 break;
2302 }
2303
2304 case Instruction::OR_LONG_2ADDR: {
2305 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2306 break;
2307 }
2308
2309 case Instruction::XOR_INT_2ADDR: {
2310 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2311 break;
2312 }
2313
2314 case Instruction::XOR_LONG_2ADDR: {
2315 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2316 break;
2317 }
2318
2319 case Instruction::ADD_INT_LIT16: {
2320 Binop_22s<HAdd>(instruction, false, dex_pc);
2321 break;
2322 }
2323
2324 case Instruction::AND_INT_LIT16: {
2325 Binop_22s<HAnd>(instruction, false, dex_pc);
2326 break;
2327 }
2328
2329 case Instruction::OR_INT_LIT16: {
2330 Binop_22s<HOr>(instruction, false, dex_pc);
2331 break;
2332 }
2333
2334 case Instruction::XOR_INT_LIT16: {
2335 Binop_22s<HXor>(instruction, false, dex_pc);
2336 break;
2337 }
2338
2339 case Instruction::RSUB_INT: {
2340 Binop_22s<HSub>(instruction, true, dex_pc);
2341 break;
2342 }
2343
2344 case Instruction::MUL_INT_LIT16: {
2345 Binop_22s<HMul>(instruction, false, dex_pc);
2346 break;
2347 }
2348
2349 case Instruction::ADD_INT_LIT8: {
2350 Binop_22b<HAdd>(instruction, false, dex_pc);
2351 break;
2352 }
2353
2354 case Instruction::AND_INT_LIT8: {
2355 Binop_22b<HAnd>(instruction, false, dex_pc);
2356 break;
2357 }
2358
2359 case Instruction::OR_INT_LIT8: {
2360 Binop_22b<HOr>(instruction, false, dex_pc);
2361 break;
2362 }
2363
2364 case Instruction::XOR_INT_LIT8: {
2365 Binop_22b<HXor>(instruction, false, dex_pc);
2366 break;
2367 }
2368
2369 case Instruction::RSUB_INT_LIT8: {
2370 Binop_22b<HSub>(instruction, true, dex_pc);
2371 break;
2372 }
2373
2374 case Instruction::MUL_INT_LIT8: {
2375 Binop_22b<HMul>(instruction, false, dex_pc);
2376 break;
2377 }
2378
2379 case Instruction::DIV_INT_LIT16:
2380 case Instruction::DIV_INT_LIT8: {
2381 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2382 dex_pc, Primitive::kPrimInt, true, true);
2383 break;
2384 }
2385
2386 case Instruction::REM_INT_LIT16:
2387 case Instruction::REM_INT_LIT8: {
2388 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2389 dex_pc, Primitive::kPrimInt, true, false);
2390 break;
2391 }
2392
2393 case Instruction::SHL_INT_LIT8: {
2394 Binop_22b<HShl>(instruction, false, dex_pc);
2395 break;
2396 }
2397
2398 case Instruction::SHR_INT_LIT8: {
2399 Binop_22b<HShr>(instruction, false, dex_pc);
2400 break;
2401 }
2402
2403 case Instruction::USHR_INT_LIT8: {
2404 Binop_22b<HUShr>(instruction, false, dex_pc);
2405 break;
2406 }
2407
2408 case Instruction::NEW_INSTANCE: {
2409 if (!BuildNewInstance(instruction.VRegB_21c(), dex_pc)) {
2410 return false;
2411 }
2412 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
2413 break;
2414 }
2415
2416 case Instruction::NEW_ARRAY: {
2417 uint16_t type_index = instruction.VRegC_22c();
2418 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
2419 bool finalizable;
2420 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index, &finalizable)
2421 ? kQuickAllocArrayWithAccessCheck
2422 : kQuickAllocArray;
2423 AppendInstruction(new (arena_) HNewArray(length,
2424 graph_->GetCurrentMethod(),
2425 dex_pc,
2426 type_index,
2427 *dex_compilation_unit_->GetDexFile(),
2428 entrypoint));
2429 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
2430 break;
2431 }
2432
2433 case Instruction::FILLED_NEW_ARRAY: {
2434 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2435 uint32_t type_index = instruction.VRegB_35c();
2436 uint32_t args[5];
2437 instruction.GetVarArgs(args);
2438 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0);
2439 break;
2440 }
2441
2442 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2443 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2444 uint32_t type_index = instruction.VRegB_3rc();
2445 uint32_t register_index = instruction.VRegC_3rc();
2446 BuildFilledNewArray(
2447 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index);
2448 break;
2449 }
2450
2451 case Instruction::FILL_ARRAY_DATA: {
2452 BuildFillArrayData(instruction, dex_pc);
2453 break;
2454 }
2455
2456 case Instruction::MOVE_RESULT:
2457 case Instruction::MOVE_RESULT_WIDE:
2458 case Instruction::MOVE_RESULT_OBJECT: {
2459 DCHECK(latest_result_ != nullptr);
2460 UpdateLocal(instruction.VRegA(), latest_result_);
2461 latest_result_ = nullptr;
2462 break;
2463 }
2464
2465 case Instruction::CMP_LONG: {
2466 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2467 break;
2468 }
2469
2470 case Instruction::CMPG_FLOAT: {
2471 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2472 break;
2473 }
2474
2475 case Instruction::CMPG_DOUBLE: {
2476 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2477 break;
2478 }
2479
2480 case Instruction::CMPL_FLOAT: {
2481 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2482 break;
2483 }
2484
2485 case Instruction::CMPL_DOUBLE: {
2486 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2487 break;
2488 }
2489
2490 case Instruction::NOP:
2491 break;
2492
2493 case Instruction::IGET:
2494 case Instruction::IGET_QUICK:
2495 case Instruction::IGET_WIDE:
2496 case Instruction::IGET_WIDE_QUICK:
2497 case Instruction::IGET_OBJECT:
2498 case Instruction::IGET_OBJECT_QUICK:
2499 case Instruction::IGET_BOOLEAN:
2500 case Instruction::IGET_BOOLEAN_QUICK:
2501 case Instruction::IGET_BYTE:
2502 case Instruction::IGET_BYTE_QUICK:
2503 case Instruction::IGET_CHAR:
2504 case Instruction::IGET_CHAR_QUICK:
2505 case Instruction::IGET_SHORT:
2506 case Instruction::IGET_SHORT_QUICK: {
2507 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2508 return false;
2509 }
2510 break;
2511 }
2512
2513 case Instruction::IPUT:
2514 case Instruction::IPUT_QUICK:
2515 case Instruction::IPUT_WIDE:
2516 case Instruction::IPUT_WIDE_QUICK:
2517 case Instruction::IPUT_OBJECT:
2518 case Instruction::IPUT_OBJECT_QUICK:
2519 case Instruction::IPUT_BOOLEAN:
2520 case Instruction::IPUT_BOOLEAN_QUICK:
2521 case Instruction::IPUT_BYTE:
2522 case Instruction::IPUT_BYTE_QUICK:
2523 case Instruction::IPUT_CHAR:
2524 case Instruction::IPUT_CHAR_QUICK:
2525 case Instruction::IPUT_SHORT:
2526 case Instruction::IPUT_SHORT_QUICK: {
2527 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2528 return false;
2529 }
2530 break;
2531 }
2532
2533 case Instruction::SGET:
2534 case Instruction::SGET_WIDE:
2535 case Instruction::SGET_OBJECT:
2536 case Instruction::SGET_BOOLEAN:
2537 case Instruction::SGET_BYTE:
2538 case Instruction::SGET_CHAR:
2539 case Instruction::SGET_SHORT: {
2540 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2541 return false;
2542 }
2543 break;
2544 }
2545
2546 case Instruction::SPUT:
2547 case Instruction::SPUT_WIDE:
2548 case Instruction::SPUT_OBJECT:
2549 case Instruction::SPUT_BOOLEAN:
2550 case Instruction::SPUT_BYTE:
2551 case Instruction::SPUT_CHAR:
2552 case Instruction::SPUT_SHORT: {
2553 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2554 return false;
2555 }
2556 break;
2557 }
2558
2559#define ARRAY_XX(kind, anticipated_type) \
2560 case Instruction::AGET##kind: { \
2561 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2562 break; \
2563 } \
2564 case Instruction::APUT##kind: { \
2565 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2566 break; \
2567 }
2568
2569 ARRAY_XX(, Primitive::kPrimInt);
2570 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2571 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2572 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2573 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2574 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2575 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2576
2577 case Instruction::ARRAY_LENGTH: {
2578 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
2579 object = new (arena_) HNullCheck(object, dex_pc);
2580 AppendInstruction(object);
2581 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2582 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2583 break;
2584 }
2585
2586 case Instruction::CONST_STRING: {
2587 uint32_t string_index = instruction.VRegB_21c();
2588 AppendInstruction(
2589 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2590 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2591 break;
2592 }
2593
2594 case Instruction::CONST_STRING_JUMBO: {
2595 uint32_t string_index = instruction.VRegB_31c();
2596 AppendInstruction(
2597 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2598 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2599 break;
2600 }
2601
2602 case Instruction::CONST_CLASS: {
2603 uint16_t type_index = instruction.VRegB_21c();
David Brazdildee58d62016-04-07 09:54:26 +00002604 // `CanAccessTypeWithoutChecks` will tell whether the method being
2605 // built is trying to access its own class, so that the generated
2606 // code can optimize for this case. However, the optimization does not
2607 // work for inlining, so we use `IsOutermostCompilingClass` instead.
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002608 ScopedObjectAccess soa(Thread::Current());
2609 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00002610 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks(
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002611 dex_compilation_unit_->GetDexMethodIndex(), dex_cache, type_index);
2612 bool is_in_dex_cache =
2613 compiler_driver_->CanAssumeTypeIsPresentInDexCache(dex_cache, type_index);
David Brazdildee58d62016-04-07 09:54:26 +00002614 AppendInstruction(new (arena_) HLoadClass(
2615 graph_->GetCurrentMethod(),
2616 type_index,
2617 *dex_file_,
2618 IsOutermostCompilingClass(type_index),
2619 dex_pc,
2620 !can_access,
Vladimir Marko3cd50df2016-04-13 19:29:26 +01002621 is_in_dex_cache));
David Brazdildee58d62016-04-07 09:54:26 +00002622 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2623 break;
2624 }
2625
2626 case Instruction::MOVE_EXCEPTION: {
2627 AppendInstruction(new (arena_) HLoadException(dex_pc));
2628 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2629 AppendInstruction(new (arena_) HClearException(dex_pc));
2630 break;
2631 }
2632
2633 case Instruction::THROW: {
2634 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2635 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2636 // We finished building this block. Set the current block to null to avoid
2637 // adding dead instructions to it.
2638 current_block_ = nullptr;
2639 break;
2640 }
2641
2642 case Instruction::INSTANCE_OF: {
2643 uint8_t destination = instruction.VRegA_22c();
2644 uint8_t reference = instruction.VRegB_22c();
2645 uint16_t type_index = instruction.VRegC_22c();
2646 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2647 break;
2648 }
2649
2650 case Instruction::CHECK_CAST: {
2651 uint8_t reference = instruction.VRegA_21c();
2652 uint16_t type_index = instruction.VRegB_21c();
2653 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2654 break;
2655 }
2656
2657 case Instruction::MONITOR_ENTER: {
2658 AppendInstruction(new (arena_) HMonitorOperation(
2659 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2660 HMonitorOperation::OperationKind::kEnter,
2661 dex_pc));
2662 break;
2663 }
2664
2665 case Instruction::MONITOR_EXIT: {
2666 AppendInstruction(new (arena_) HMonitorOperation(
2667 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2668 HMonitorOperation::OperationKind::kExit,
2669 dex_pc));
2670 break;
2671 }
2672
2673 case Instruction::SPARSE_SWITCH:
2674 case Instruction::PACKED_SWITCH: {
2675 BuildSwitch(instruction, dex_pc);
2676 break;
2677 }
2678
2679 default:
2680 VLOG(compiler) << "Did not compile "
2681 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_)
2682 << " because of unhandled instruction "
2683 << instruction.Name();
2684 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2685 return false;
2686 }
2687 return true;
2688} // NOLINT(readability/fn_size)
2689
2690} // namespace art