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