blob: 40fafb0ae5da58c0403318cdfb8ab4e3c52c59bb [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
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700965HNewInstance* 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
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700989 HNewInstance* new_instance = new (arena_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000990 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,
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700995 entrypoint);
996 AppendInstruction(new_instance);
997
998 return new_instance;
999}
1000
1001void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1002 DCHECK(allocation != nullptr &&
1003 allocation->IsNewInstance() ||
1004 allocation->IsNewArray()); // corresponding to "new" keyword in JLS.
1005
1006 if (allocation->IsNewInstance()) {
1007 // STRING SPECIAL HANDLING:
1008 // -------------------------------
1009 // Strings have a real HNewInstance node but they end up always having 0 uses.
1010 // All uses of a String HNewInstance are always transformed to replace their input
1011 // of the HNewInstance with an input of the invoke to StringFactory.
1012 //
1013 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1014 // optimizations (to pass checker tests that rely on those optimizations).
1015 HNewInstance* new_inst = allocation->AsNewInstance();
1016 HLoadClass* load_class = new_inst->GetLoadClass();
1017
1018 Thread* self = Thread::Current();
1019 ScopedObjectAccess soa(self);
1020 StackHandleScope<1> hs(self);
1021 Handle<mirror::Class> klass = load_class->GetClass();
1022 if (klass != nullptr && klass->IsStringClass()) {
1023 return;
1024 // Note: Do not use allocation->IsStringAlloc which requires
1025 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1026 // propagation (and instruction builder is too early).
1027 }
1028 // (In terms of correctness, the StringFactory needs to provide its own
1029 // default initialization barrier, see below.)
1030 }
1031
1032 // JLS 17.4.5 "Happens-before Order" describes:
1033 //
1034 // The default initialization of any object happens-before any other actions (other than
1035 // default-writes) of a program.
1036 //
1037 // In our implementation the default initialization of an object to type T means
1038 // setting all of its initial data (object[0..size)) to 0, and setting the
1039 // object's class header (i.e. object.getClass() == T.class).
1040 //
1041 // In practice this fence ensures that the writes to the object header
1042 // are visible to other threads if this object escapes the current thread.
1043 // (and in theory the 0-initializing, but that happens automatically
1044 // when new memory pages are mapped in by the OS).
1045 HConstructorFence* ctor_fence =
1046 new (arena_) HConstructorFence(allocation, allocation->GetDexPc(), arena_);
1047 AppendInstruction(ctor_fence);
David Brazdildee58d62016-04-07 09:54:26 +00001048}
1049
1050static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001051 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001052 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1053}
1054
1055bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001056 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001057 return false;
1058 }
1059
1060 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1061 // check whether the class is in an image for the AOT compilation.
1062 if (cls->IsInitialized() &&
1063 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1064 return true;
1065 }
1066
1067 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1068 return true;
1069 }
1070
1071 // TODO: We should walk over the inlined methods, but we don't pass
1072 // that information to the builder.
1073 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1074 return true;
1075 }
1076
1077 return false;
1078}
1079
1080HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1081 uint32_t dex_pc,
1082 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001083 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001084 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001085
1086 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001087 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001088 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001089 } else {
1090 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1091 klass->GetDexFile(),
1092 klass,
1093 dex_pc,
1094 /* needs_access_check */ false);
1095 if (cls != nullptr) {
1096 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1097 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
1098 AppendInstruction(clinit_check);
1099 }
David Brazdildee58d62016-04-07 09:54:26 +00001100 }
1101 return clinit_check;
1102}
1103
1104bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1105 uint32_t number_of_vreg_arguments,
1106 uint32_t* args,
1107 uint32_t register_index,
1108 bool is_range,
1109 const char* descriptor,
1110 size_t start_index,
1111 size_t* argument_index) {
1112 uint32_t descriptor_index = 1; // Skip the return type.
1113
1114 for (size_t i = start_index;
1115 // Make sure we don't go over the expected arguments or over the number of
1116 // dex registers given. If the instruction was seen as dead by the verifier,
1117 // it hasn't been properly checked.
1118 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1119 i++, (*argument_index)++) {
1120 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1121 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1122 if (!is_range
1123 && is_wide
1124 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1125 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1126 // reject any class where this is violated. However, the verifier only does these checks
1127 // on non trivially dead instructions, so we just bailout the compilation.
1128 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001129 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001130 << " because of non-sequential dex register pair in wide argument";
1131 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1132 return false;
1133 }
1134 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1135 invoke->SetArgumentAt(*argument_index, arg);
1136 if (is_wide) {
1137 i++;
1138 }
1139 }
1140
1141 if (*argument_index != invoke->GetNumberOfArguments()) {
1142 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001143 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001144 << " because of wrong number of arguments in invoke instruction";
1145 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1146 return false;
1147 }
1148
1149 if (invoke->IsInvokeStaticOrDirect() &&
1150 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1151 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1152 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1153 (*argument_index)++;
1154 }
1155
1156 return true;
1157}
1158
1159bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1160 uint32_t number_of_vreg_arguments,
1161 uint32_t* args,
1162 uint32_t register_index,
1163 bool is_range,
1164 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001165 HClinitCheck* clinit_check,
1166 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001167 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1168
1169 size_t start_index = 0;
1170 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001171 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001172 uint32_t obj_reg = is_range ? register_index : args[0];
1173 HInstruction* arg = is_unresolved
1174 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1175 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001176 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001177 start_index = 1;
1178 argument_index = 1;
1179 }
1180
1181 if (!SetupInvokeArguments(invoke,
1182 number_of_vreg_arguments,
1183 args,
1184 register_index,
1185 is_range,
1186 descriptor,
1187 start_index,
1188 &argument_index)) {
1189 return false;
1190 }
1191
1192 if (clinit_check != nullptr) {
1193 // Add the class initialization check as last input of `invoke`.
1194 DCHECK(invoke->IsInvokeStaticOrDirect());
1195 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1196 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1197 invoke->SetArgumentAt(argument_index, clinit_check);
1198 argument_index++;
1199 }
1200
1201 AppendInstruction(invoke);
1202 latest_result_ = invoke;
1203
1204 return true;
1205}
1206
1207bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1208 uint32_t number_of_vreg_arguments,
1209 uint32_t* args,
1210 uint32_t register_index,
1211 bool is_range,
1212 const char* descriptor) {
1213 DCHECK(invoke->IsInvokeStaticOrDirect());
1214 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1215
1216 size_t start_index = 1;
1217 size_t argument_index = 0;
1218 if (!SetupInvokeArguments(invoke,
1219 number_of_vreg_arguments,
1220 args,
1221 register_index,
1222 is_range,
1223 descriptor,
1224 start_index,
1225 &argument_index)) {
1226 return false;
1227 }
1228
1229 AppendInstruction(invoke);
1230
1231 // This is a StringFactory call, not an actual String constructor. Its result
1232 // replaces the empty String pre-allocated by NewInstance.
1233 uint32_t orig_this_reg = is_range ? register_index : args[0];
1234 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1235
1236 // Replacing the NewInstance might render it redundant. Keep a list of these
1237 // to be visited once it is clear whether it is has remaining uses.
1238 if (arg_this->IsNewInstance()) {
1239 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1240 } else {
1241 DCHECK(arg_this->IsPhi());
1242 // NewInstance is not the direct input of the StringFactory call. It might
1243 // be redundant but optimizing this case is not worth the effort.
1244 }
1245
1246 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1247 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1248 if ((*current_locals_)[vreg] == arg_this) {
1249 (*current_locals_)[vreg] = invoke;
1250 }
1251 }
1252
1253 return true;
1254}
1255
1256static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1257 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1258 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1259 return Primitive::GetType(type[0]);
1260}
1261
1262bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1263 uint32_t dex_pc,
1264 bool is_put) {
1265 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1266 uint32_t obj_reg = instruction.VRegB_22c();
1267 uint16_t field_index;
1268 if (instruction.IsQuickened()) {
1269 if (!CanDecodeQuickenedInfo()) {
1270 return false;
1271 }
1272 field_index = LookupQuickenedInfo(dex_pc);
1273 } else {
1274 field_index = instruction.VRegC_22c();
1275 }
1276
1277 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001278 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001279
Aart Bik14154132016-06-02 17:53:58 -07001280 // Generate an explicit null check on the reference, unless the field access
1281 // is unresolved. In that case, we rely on the runtime to perform various
1282 // checks first, followed by a null check.
1283 HInstruction* object = (resolved_field == nullptr)
1284 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1285 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001286
1287 Primitive::Type field_type = (resolved_field == nullptr)
1288 ? GetFieldAccessType(*dex_file_, field_index)
1289 : resolved_field->GetTypeAsPrimitiveType();
1290 if (is_put) {
1291 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1292 HInstruction* field_set = nullptr;
1293 if (resolved_field == nullptr) {
1294 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001295 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001296 value,
1297 field_type,
1298 field_index,
1299 dex_pc);
1300 } else {
1301 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001302 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001303 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001304 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001305 field_type,
1306 resolved_field->GetOffset(),
1307 resolved_field->IsVolatile(),
1308 field_index,
1309 class_def_index,
1310 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001311 dex_pc);
1312 }
1313 AppendInstruction(field_set);
1314 } else {
1315 HInstruction* field_get = nullptr;
1316 if (resolved_field == nullptr) {
1317 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001318 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001319 field_type,
1320 field_index,
1321 dex_pc);
1322 } else {
1323 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001324 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001325 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001326 field_type,
1327 resolved_field->GetOffset(),
1328 resolved_field->IsVolatile(),
1329 field_index,
1330 class_def_index,
1331 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001332 dex_pc);
1333 }
1334 AppendInstruction(field_get);
1335 UpdateLocal(source_or_dest_reg, field_get);
1336 }
1337
1338 return true;
1339}
1340
1341static mirror::Class* GetClassFrom(CompilerDriver* driver,
1342 const DexCompilationUnit& compilation_unit) {
1343 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001344 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001345 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001346
1347 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1348}
1349
1350mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1351 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1352}
1353
1354mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1355 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1356}
1357
Andreas Gampea5b09a62016-11-17 15:21:22 -08001358bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001359 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001360 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001361 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001362 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001363 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1364 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1365 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1366
1367 // GetOutermostCompilingClass returns null when the class is unresolved
1368 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1369 // we are compiling it.
1370 // When this happens we cannot establish a direct relation between the current
1371 // class and the outer class, so we return false.
1372 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001373 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001374}
1375
1376void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001377 uint32_t dex_pc,
1378 bool is_put,
1379 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001380 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1381 uint16_t field_index = instruction.VRegB_21c();
1382
1383 if (is_put) {
1384 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1385 AppendInstruction(
1386 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1387 } else {
1388 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1389 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1390 }
1391}
1392
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001393ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1394 ScopedObjectAccess soa(Thread::Current());
1395 StackHandleScope<2> hs(soa.Self());
1396
1397 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001398 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001399 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1400
1401 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1402 field_idx,
1403 dex_compilation_unit_->GetDexCache(),
1404 class_loader,
1405 is_static);
1406
1407 if (UNLIKELY(resolved_field == nullptr)) {
1408 // Clean up any exception left by type resolution.
1409 soa.Self()->ClearException();
1410 return nullptr;
1411 }
1412
1413 // Check static/instance. The class linker has a fast path for looking into the dex cache
1414 // and does not check static/instance if it hits it.
1415 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1416 return nullptr;
1417 }
1418
1419 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001420 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001421 if (!resolved_field->IsPublic()) {
1422 return nullptr;
1423 }
1424 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1425 resolved_field,
1426 dex_compilation_unit_->GetDexCache().Get(),
1427 field_idx)) {
1428 return nullptr;
1429 }
1430
1431 if (is_put &&
1432 resolved_field->IsFinal() &&
1433 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1434 // Final fields can only be updated within their own class.
1435 // TODO: Only allow it in constructors. b/34966607.
1436 return nullptr;
1437 }
1438
1439 return resolved_field;
1440}
1441
David Brazdildee58d62016-04-07 09:54:26 +00001442bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1443 uint32_t dex_pc,
1444 bool is_put) {
1445 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1446 uint16_t field_index = instruction.VRegB_21c();
1447
1448 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001449 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001450
1451 if (resolved_field == nullptr) {
1452 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1453 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1454 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1455 return true;
1456 }
1457
1458 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
David Brazdildee58d62016-04-07 09:54:26 +00001459
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001460 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1461 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1462 klass->GetDexFile(),
1463 klass,
1464 dex_pc,
1465 /* needs_access_check */ false);
1466
1467 if (constant == nullptr) {
1468 // The class cannot be referenced from this compiled code. Generate
1469 // an unresolved access.
1470 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1471 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1472 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001473 }
1474
David Brazdildee58d62016-04-07 09:54:26 +00001475 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001476 if (!IsInitialized(klass)) {
1477 cls = new (arena_) HClinitCheck(constant, dex_pc);
1478 AppendInstruction(cls);
1479 }
1480
1481 uint16_t class_def_index = klass->GetDexClassDefIndex();
1482 if (is_put) {
1483 // We need to keep the class alive before loading the value.
1484 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1485 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1486 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1487 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001488 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001489 field_type,
1490 resolved_field->GetOffset(),
1491 resolved_field->IsVolatile(),
1492 field_index,
1493 class_def_index,
1494 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001495 dex_pc));
1496 } else {
1497 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001498 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001499 field_type,
1500 resolved_field->GetOffset(),
1501 resolved_field->IsVolatile(),
1502 field_index,
1503 class_def_index,
1504 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001505 dex_pc));
1506 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1507 }
1508 return true;
1509}
1510
1511void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1512 uint16_t first_vreg,
1513 int64_t second_vreg_or_constant,
1514 uint32_t dex_pc,
1515 Primitive::Type type,
1516 bool second_is_constant,
1517 bool isDiv) {
1518 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1519
1520 HInstruction* first = LoadLocal(first_vreg, type);
1521 HInstruction* second = nullptr;
1522 if (second_is_constant) {
1523 if (type == Primitive::kPrimInt) {
1524 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1525 } else {
1526 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1527 }
1528 } else {
1529 second = LoadLocal(second_vreg_or_constant, type);
1530 }
1531
1532 if (!second_is_constant
1533 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1534 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1535 second = new (arena_) HDivZeroCheck(second, dex_pc);
1536 AppendInstruction(second);
1537 }
1538
1539 if (isDiv) {
1540 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1541 } else {
1542 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1543 }
1544 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1545}
1546
1547void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1548 uint32_t dex_pc,
1549 bool is_put,
1550 Primitive::Type anticipated_type) {
1551 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1552 uint8_t array_reg = instruction.VRegB_23x();
1553 uint8_t index_reg = instruction.VRegC_23x();
1554
David Brazdilc120bbe2016-04-22 16:57:00 +01001555 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001556 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1557 AppendInstruction(length);
1558 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1559 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1560 AppendInstruction(index);
1561 if (is_put) {
1562 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1563 // TODO: Insert a type check node if the type is Object.
1564 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1565 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1566 AppendInstruction(aset);
1567 } else {
1568 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1569 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1570 AppendInstruction(aget);
1571 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1572 }
1573 graph_->SetHasBoundsChecks(true);
1574}
1575
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001576HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1577 dex::TypeIndex type_index,
1578 uint32_t number_of_vreg_arguments,
1579 bool is_range,
1580 uint32_t* args,
1581 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001582 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001583 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001584 HNewArray* const object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001585 AppendInstruction(object);
1586
1587 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1588 DCHECK_EQ(descriptor[0], '[') << descriptor;
1589 char primitive = descriptor[1];
1590 DCHECK(primitive == 'I'
1591 || primitive == 'L'
1592 || primitive == '[') << descriptor;
1593 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1594 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1595
1596 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1597 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1598 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1599 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1600 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1601 AppendInstruction(aset);
1602 }
1603 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001604
1605 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001606}
1607
1608template <typename T>
1609void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1610 const T* data,
1611 uint32_t element_count,
1612 Primitive::Type anticipated_type,
1613 uint32_t dex_pc) {
1614 for (uint32_t i = 0; i < element_count; ++i) {
1615 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1616 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1617 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1618 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1619 AppendInstruction(aset);
1620 }
1621}
1622
1623void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001624 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001625
1626 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1627 const Instruction::ArrayDataPayload* payload =
1628 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1629 const uint8_t* data = payload->data;
1630 uint32_t element_count = payload->element_count;
1631
Vladimir Markoc69fba22016-09-06 16:49:15 +01001632 if (element_count == 0u) {
1633 // For empty payload we emit only the null check above.
1634 return;
1635 }
1636
1637 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1638 AppendInstruction(length);
1639
David Brazdildee58d62016-04-07 09:54:26 +00001640 // Implementation of this DEX instruction seems to be that the bounds check is
1641 // done before doing any stores.
1642 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1643 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1644
1645 switch (payload->element_width) {
1646 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001647 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001648 reinterpret_cast<const int8_t*>(data),
1649 element_count,
1650 Primitive::kPrimByte,
1651 dex_pc);
1652 break;
1653 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001654 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001655 reinterpret_cast<const int16_t*>(data),
1656 element_count,
1657 Primitive::kPrimShort,
1658 dex_pc);
1659 break;
1660 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001661 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001662 reinterpret_cast<const int32_t*>(data),
1663 element_count,
1664 Primitive::kPrimInt,
1665 dex_pc);
1666 break;
1667 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001668 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001669 reinterpret_cast<const int64_t*>(data),
1670 element_count,
1671 dex_pc);
1672 break;
1673 default:
1674 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1675 }
1676 graph_->SetHasBoundsChecks(true);
1677}
1678
1679void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1680 const int64_t* data,
1681 uint32_t element_count,
1682 uint32_t dex_pc) {
1683 for (uint32_t i = 0; i < element_count; ++i) {
1684 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1685 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1686 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1687 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1688 AppendInstruction(aset);
1689 }
1690}
1691
1692static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001693 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001694 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001695 return TypeCheckKind::kUnresolvedCheck;
1696 } else if (cls->IsInterface()) {
1697 return TypeCheckKind::kInterfaceCheck;
1698 } else if (cls->IsArrayClass()) {
1699 if (cls->GetComponentType()->IsObjectClass()) {
1700 return TypeCheckKind::kArrayObjectCheck;
1701 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1702 return TypeCheckKind::kExactCheck;
1703 } else {
1704 return TypeCheckKind::kArrayCheck;
1705 }
1706 } else if (cls->IsFinal()) {
1707 return TypeCheckKind::kExactCheck;
1708 } else if (cls->IsAbstract()) {
1709 return TypeCheckKind::kAbstractClassCheck;
1710 } else {
1711 return TypeCheckKind::kClassHierarchyCheck;
1712 }
1713}
1714
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001715HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001716 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001717 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001718 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001719 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001720 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001721
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001722 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001723 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001724 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001725 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001726 } else {
1727 mirror::Class* compiling_class = GetCompilingClass();
1728 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001729 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001730 }
1731 }
1732 }
1733
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001734 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1735}
1736
1737HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1738 const DexFile& dex_file,
1739 Handle<mirror::Class> klass,
1740 uint32_t dex_pc,
1741 bool needs_access_check) {
1742 // Try to find a reference in the compiling dex file.
1743 const DexFile* actual_dex_file = &dex_file;
1744 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1745 dex::TypeIndex local_type_index =
1746 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1747 if (local_type_index.IsValid()) {
1748 type_index = local_type_index;
1749 actual_dex_file = dex_compilation_unit_->GetDexFile();
1750 }
1751 }
1752
1753 // Note: `klass` must be from `handles_`.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001754 HLoadClass* load_class = new (arena_) HLoadClass(
1755 graph_->GetCurrentMethod(),
1756 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001757 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001758 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001759 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001760 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001761 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001762
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001763 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1764 code_generator_,
1765 compiler_driver_,
1766 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001767
1768 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1769 // We actually cannot reference this class, we're forced to bail.
1770 return nullptr;
1771 }
1772 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001773 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001774 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001775 return load_class;
1776}
1777
David Brazdildee58d62016-04-07 09:54:26 +00001778void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1779 uint8_t destination,
1780 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001781 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001782 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001783 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001784 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001785
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001786 ScopedObjectAccess soa(Thread::Current());
1787 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001788 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1789 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1790 UpdateLocal(destination, current_block_->GetLastInstruction());
1791 } else {
1792 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1793 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1794 // which may throw. If it succeeds BoundType sets the new type of `object`
1795 // for all subsequent uses.
1796 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1797 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1798 UpdateLocal(reference, current_block_->GetLastInstruction());
1799 }
1800}
1801
Vladimir Marko0b66d612017-03-13 14:50:04 +00001802bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001803 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1804 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001805}
1806
1807bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
1808 return interpreter_metadata_ != nullptr;
1809}
1810
1811uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t dex_pc) {
1812 DCHECK(interpreter_metadata_ != nullptr);
1813
1814 // First check if the info has already been decoded from `interpreter_metadata_`.
1815 auto it = skipped_interpreter_metadata_.find(dex_pc);
1816 if (it != skipped_interpreter_metadata_.end()) {
1817 // Remove the entry from the map and return the parsed info.
1818 uint16_t value_in_map = it->second;
1819 skipped_interpreter_metadata_.erase(it);
1820 return value_in_map;
1821 }
1822
1823 // Otherwise start parsing `interpreter_metadata_` until the slot for `dex_pc`
1824 // is found. Store skipped values in the `skipped_interpreter_metadata_` map.
1825 while (true) {
1826 uint32_t dex_pc_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1827 uint16_t value_in_map = DecodeUnsignedLeb128(&interpreter_metadata_);
1828 DCHECK_LE(dex_pc_in_map, dex_pc);
1829
1830 if (dex_pc_in_map == dex_pc) {
1831 return value_in_map;
1832 } else {
Nicolas Geoffray01b70e82016-11-17 10:58:36 +00001833 // Overwrite and not Put, as quickened CHECK-CAST has two entries with
1834 // the same dex_pc. This is OK, because the compiler does not care about those
1835 // entries.
1836 skipped_interpreter_metadata_.Overwrite(dex_pc_in_map, value_in_map);
David Brazdildee58d62016-04-07 09:54:26 +00001837 }
1838 }
1839}
1840
1841bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction, uint32_t dex_pc) {
1842 switch (instruction.Opcode()) {
1843 case Instruction::CONST_4: {
1844 int32_t register_index = instruction.VRegA();
1845 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1846 UpdateLocal(register_index, constant);
1847 break;
1848 }
1849
1850 case Instruction::CONST_16: {
1851 int32_t register_index = instruction.VRegA();
1852 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1853 UpdateLocal(register_index, constant);
1854 break;
1855 }
1856
1857 case Instruction::CONST: {
1858 int32_t register_index = instruction.VRegA();
1859 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1860 UpdateLocal(register_index, constant);
1861 break;
1862 }
1863
1864 case Instruction::CONST_HIGH16: {
1865 int32_t register_index = instruction.VRegA();
1866 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1867 UpdateLocal(register_index, constant);
1868 break;
1869 }
1870
1871 case Instruction::CONST_WIDE_16: {
1872 int32_t register_index = instruction.VRegA();
1873 // Get 16 bits of constant value, sign extended to 64 bits.
1874 int64_t value = instruction.VRegB_21s();
1875 value <<= 48;
1876 value >>= 48;
1877 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1878 UpdateLocal(register_index, constant);
1879 break;
1880 }
1881
1882 case Instruction::CONST_WIDE_32: {
1883 int32_t register_index = instruction.VRegA();
1884 // Get 32 bits of constant value, sign extended to 64 bits.
1885 int64_t value = instruction.VRegB_31i();
1886 value <<= 32;
1887 value >>= 32;
1888 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1889 UpdateLocal(register_index, constant);
1890 break;
1891 }
1892
1893 case Instruction::CONST_WIDE: {
1894 int32_t register_index = instruction.VRegA();
1895 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1896 UpdateLocal(register_index, constant);
1897 break;
1898 }
1899
1900 case Instruction::CONST_WIDE_HIGH16: {
1901 int32_t register_index = instruction.VRegA();
1902 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1903 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1904 UpdateLocal(register_index, constant);
1905 break;
1906 }
1907
1908 // Note that the SSA building will refine the types.
1909 case Instruction::MOVE:
1910 case Instruction::MOVE_FROM16:
1911 case Instruction::MOVE_16: {
1912 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1913 UpdateLocal(instruction.VRegA(), value);
1914 break;
1915 }
1916
1917 // Note that the SSA building will refine the types.
1918 case Instruction::MOVE_WIDE:
1919 case Instruction::MOVE_WIDE_FROM16:
1920 case Instruction::MOVE_WIDE_16: {
1921 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1922 UpdateLocal(instruction.VRegA(), value);
1923 break;
1924 }
1925
1926 case Instruction::MOVE_OBJECT:
1927 case Instruction::MOVE_OBJECT_16:
1928 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001929 // The verifier has no notion of a null type, so a move-object of constant 0
1930 // will lead to the same constant 0 in the destination register. To mimic
1931 // this behavior, we just pretend we haven't seen a type change (int to reference)
1932 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1933 // types correct.
1934 uint32_t reg_number = instruction.VRegB();
1935 HInstruction* value = (*current_locals_)[reg_number];
1936 if (value->IsIntConstant()) {
1937 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1938 } else if (value->IsPhi()) {
1939 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1940 } else {
1941 value = LoadLocal(reg_number, Primitive::kPrimNot);
1942 }
David Brazdildee58d62016-04-07 09:54:26 +00001943 UpdateLocal(instruction.VRegA(), value);
1944 break;
1945 }
1946
1947 case Instruction::RETURN_VOID_NO_BARRIER:
1948 case Instruction::RETURN_VOID: {
1949 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1950 break;
1951 }
1952
1953#define IF_XX(comparison, cond) \
1954 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1955 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1956
1957 IF_XX(HEqual, EQ);
1958 IF_XX(HNotEqual, NE);
1959 IF_XX(HLessThan, LT);
1960 IF_XX(HLessThanOrEqual, LE);
1961 IF_XX(HGreaterThan, GT);
1962 IF_XX(HGreaterThanOrEqual, GE);
1963
1964 case Instruction::GOTO:
1965 case Instruction::GOTO_16:
1966 case Instruction::GOTO_32: {
1967 AppendInstruction(new (arena_) HGoto(dex_pc));
1968 current_block_ = nullptr;
1969 break;
1970 }
1971
1972 case Instruction::RETURN: {
1973 BuildReturn(instruction, return_type_, dex_pc);
1974 break;
1975 }
1976
1977 case Instruction::RETURN_OBJECT: {
1978 BuildReturn(instruction, return_type_, dex_pc);
1979 break;
1980 }
1981
1982 case Instruction::RETURN_WIDE: {
1983 BuildReturn(instruction, return_type_, dex_pc);
1984 break;
1985 }
1986
1987 case Instruction::INVOKE_DIRECT:
1988 case Instruction::INVOKE_INTERFACE:
1989 case Instruction::INVOKE_STATIC:
1990 case Instruction::INVOKE_SUPER:
1991 case Instruction::INVOKE_VIRTUAL:
1992 case Instruction::INVOKE_VIRTUAL_QUICK: {
1993 uint16_t method_idx;
1994 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1995 if (!CanDecodeQuickenedInfo()) {
1996 return false;
1997 }
1998 method_idx = LookupQuickenedInfo(dex_pc);
1999 } else {
2000 method_idx = instruction.VRegB_35c();
2001 }
2002 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2003 uint32_t args[5];
2004 instruction.GetVarArgs(args);
2005 if (!BuildInvoke(instruction, dex_pc, method_idx,
2006 number_of_vreg_arguments, false, args, -1)) {
2007 return false;
2008 }
2009 break;
2010 }
2011
2012 case Instruction::INVOKE_DIRECT_RANGE:
2013 case Instruction::INVOKE_INTERFACE_RANGE:
2014 case Instruction::INVOKE_STATIC_RANGE:
2015 case Instruction::INVOKE_SUPER_RANGE:
2016 case Instruction::INVOKE_VIRTUAL_RANGE:
2017 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2018 uint16_t method_idx;
2019 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2020 if (!CanDecodeQuickenedInfo()) {
2021 return false;
2022 }
2023 method_idx = LookupQuickenedInfo(dex_pc);
2024 } else {
2025 method_idx = instruction.VRegB_3rc();
2026 }
2027 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2028 uint32_t register_index = instruction.VRegC();
2029 if (!BuildInvoke(instruction, dex_pc, method_idx,
2030 number_of_vreg_arguments, true, nullptr, register_index)) {
2031 return false;
2032 }
2033 break;
2034 }
2035
Orion Hodsonac141392017-01-13 11:53:47 +00002036 case Instruction::INVOKE_POLYMORPHIC: {
2037 uint16_t method_idx = instruction.VRegB_45cc();
2038 uint16_t proto_idx = instruction.VRegH_45cc();
2039 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2040 uint32_t args[5];
2041 instruction.GetVarArgs(args);
2042 return BuildInvokePolymorphic(instruction,
2043 dex_pc,
2044 method_idx,
2045 proto_idx,
2046 number_of_vreg_arguments,
2047 false,
2048 args,
2049 -1);
2050 }
2051
2052 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2053 uint16_t method_idx = instruction.VRegB_4rcc();
2054 uint16_t proto_idx = instruction.VRegH_4rcc();
2055 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2056 uint32_t register_index = instruction.VRegC_4rcc();
2057 return BuildInvokePolymorphic(instruction,
2058 dex_pc,
2059 method_idx,
2060 proto_idx,
2061 number_of_vreg_arguments,
2062 true,
2063 nullptr,
2064 register_index);
2065 }
2066
David Brazdildee58d62016-04-07 09:54:26 +00002067 case Instruction::NEG_INT: {
2068 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
2069 break;
2070 }
2071
2072 case Instruction::NEG_LONG: {
2073 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
2074 break;
2075 }
2076
2077 case Instruction::NEG_FLOAT: {
2078 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::NEG_DOUBLE: {
2083 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::NOT_INT: {
2088 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
2089 break;
2090 }
2091
2092 case Instruction::NOT_LONG: {
2093 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
2094 break;
2095 }
2096
2097 case Instruction::INT_TO_LONG: {
2098 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2099 break;
2100 }
2101
2102 case Instruction::INT_TO_FLOAT: {
2103 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2104 break;
2105 }
2106
2107 case Instruction::INT_TO_DOUBLE: {
2108 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2109 break;
2110 }
2111
2112 case Instruction::LONG_TO_INT: {
2113 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2114 break;
2115 }
2116
2117 case Instruction::LONG_TO_FLOAT: {
2118 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2119 break;
2120 }
2121
2122 case Instruction::LONG_TO_DOUBLE: {
2123 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2124 break;
2125 }
2126
2127 case Instruction::FLOAT_TO_INT: {
2128 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2129 break;
2130 }
2131
2132 case Instruction::FLOAT_TO_LONG: {
2133 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2134 break;
2135 }
2136
2137 case Instruction::FLOAT_TO_DOUBLE: {
2138 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2139 break;
2140 }
2141
2142 case Instruction::DOUBLE_TO_INT: {
2143 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2144 break;
2145 }
2146
2147 case Instruction::DOUBLE_TO_LONG: {
2148 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2149 break;
2150 }
2151
2152 case Instruction::DOUBLE_TO_FLOAT: {
2153 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2154 break;
2155 }
2156
2157 case Instruction::INT_TO_BYTE: {
2158 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2159 break;
2160 }
2161
2162 case Instruction::INT_TO_SHORT: {
2163 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2164 break;
2165 }
2166
2167 case Instruction::INT_TO_CHAR: {
2168 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2169 break;
2170 }
2171
2172 case Instruction::ADD_INT: {
2173 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2174 break;
2175 }
2176
2177 case Instruction::ADD_LONG: {
2178 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2179 break;
2180 }
2181
2182 case Instruction::ADD_DOUBLE: {
2183 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2184 break;
2185 }
2186
2187 case Instruction::ADD_FLOAT: {
2188 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2189 break;
2190 }
2191
2192 case Instruction::SUB_INT: {
2193 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2194 break;
2195 }
2196
2197 case Instruction::SUB_LONG: {
2198 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2199 break;
2200 }
2201
2202 case Instruction::SUB_FLOAT: {
2203 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2204 break;
2205 }
2206
2207 case Instruction::SUB_DOUBLE: {
2208 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2209 break;
2210 }
2211
2212 case Instruction::ADD_INT_2ADDR: {
2213 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2214 break;
2215 }
2216
2217 case Instruction::MUL_INT: {
2218 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2219 break;
2220 }
2221
2222 case Instruction::MUL_LONG: {
2223 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2224 break;
2225 }
2226
2227 case Instruction::MUL_FLOAT: {
2228 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2229 break;
2230 }
2231
2232 case Instruction::MUL_DOUBLE: {
2233 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2234 break;
2235 }
2236
2237 case Instruction::DIV_INT: {
2238 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2239 dex_pc, Primitive::kPrimInt, false, true);
2240 break;
2241 }
2242
2243 case Instruction::DIV_LONG: {
2244 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2245 dex_pc, Primitive::kPrimLong, false, true);
2246 break;
2247 }
2248
2249 case Instruction::DIV_FLOAT: {
2250 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2251 break;
2252 }
2253
2254 case Instruction::DIV_DOUBLE: {
2255 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2256 break;
2257 }
2258
2259 case Instruction::REM_INT: {
2260 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2261 dex_pc, Primitive::kPrimInt, false, false);
2262 break;
2263 }
2264
2265 case Instruction::REM_LONG: {
2266 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2267 dex_pc, Primitive::kPrimLong, false, false);
2268 break;
2269 }
2270
2271 case Instruction::REM_FLOAT: {
2272 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2273 break;
2274 }
2275
2276 case Instruction::REM_DOUBLE: {
2277 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2278 break;
2279 }
2280
2281 case Instruction::AND_INT: {
2282 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2283 break;
2284 }
2285
2286 case Instruction::AND_LONG: {
2287 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2288 break;
2289 }
2290
2291 case Instruction::SHL_INT: {
2292 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2293 break;
2294 }
2295
2296 case Instruction::SHL_LONG: {
2297 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2298 break;
2299 }
2300
2301 case Instruction::SHR_INT: {
2302 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2303 break;
2304 }
2305
2306 case Instruction::SHR_LONG: {
2307 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2308 break;
2309 }
2310
2311 case Instruction::USHR_INT: {
2312 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2313 break;
2314 }
2315
2316 case Instruction::USHR_LONG: {
2317 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2318 break;
2319 }
2320
2321 case Instruction::OR_INT: {
2322 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2323 break;
2324 }
2325
2326 case Instruction::OR_LONG: {
2327 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2328 break;
2329 }
2330
2331 case Instruction::XOR_INT: {
2332 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2333 break;
2334 }
2335
2336 case Instruction::XOR_LONG: {
2337 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2338 break;
2339 }
2340
2341 case Instruction::ADD_LONG_2ADDR: {
2342 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2343 break;
2344 }
2345
2346 case Instruction::ADD_DOUBLE_2ADDR: {
2347 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2348 break;
2349 }
2350
2351 case Instruction::ADD_FLOAT_2ADDR: {
2352 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2353 break;
2354 }
2355
2356 case Instruction::SUB_INT_2ADDR: {
2357 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2358 break;
2359 }
2360
2361 case Instruction::SUB_LONG_2ADDR: {
2362 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2363 break;
2364 }
2365
2366 case Instruction::SUB_FLOAT_2ADDR: {
2367 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2368 break;
2369 }
2370
2371 case Instruction::SUB_DOUBLE_2ADDR: {
2372 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2373 break;
2374 }
2375
2376 case Instruction::MUL_INT_2ADDR: {
2377 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2378 break;
2379 }
2380
2381 case Instruction::MUL_LONG_2ADDR: {
2382 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2383 break;
2384 }
2385
2386 case Instruction::MUL_FLOAT_2ADDR: {
2387 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2388 break;
2389 }
2390
2391 case Instruction::MUL_DOUBLE_2ADDR: {
2392 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2393 break;
2394 }
2395
2396 case Instruction::DIV_INT_2ADDR: {
2397 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2398 dex_pc, Primitive::kPrimInt, false, true);
2399 break;
2400 }
2401
2402 case Instruction::DIV_LONG_2ADDR: {
2403 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2404 dex_pc, Primitive::kPrimLong, false, true);
2405 break;
2406 }
2407
2408 case Instruction::REM_INT_2ADDR: {
2409 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2410 dex_pc, Primitive::kPrimInt, false, false);
2411 break;
2412 }
2413
2414 case Instruction::REM_LONG_2ADDR: {
2415 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2416 dex_pc, Primitive::kPrimLong, false, false);
2417 break;
2418 }
2419
2420 case Instruction::REM_FLOAT_2ADDR: {
2421 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2422 break;
2423 }
2424
2425 case Instruction::REM_DOUBLE_2ADDR: {
2426 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2427 break;
2428 }
2429
2430 case Instruction::SHL_INT_2ADDR: {
2431 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2432 break;
2433 }
2434
2435 case Instruction::SHL_LONG_2ADDR: {
2436 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2437 break;
2438 }
2439
2440 case Instruction::SHR_INT_2ADDR: {
2441 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2442 break;
2443 }
2444
2445 case Instruction::SHR_LONG_2ADDR: {
2446 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2447 break;
2448 }
2449
2450 case Instruction::USHR_INT_2ADDR: {
2451 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2452 break;
2453 }
2454
2455 case Instruction::USHR_LONG_2ADDR: {
2456 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2457 break;
2458 }
2459
2460 case Instruction::DIV_FLOAT_2ADDR: {
2461 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2462 break;
2463 }
2464
2465 case Instruction::DIV_DOUBLE_2ADDR: {
2466 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2467 break;
2468 }
2469
2470 case Instruction::AND_INT_2ADDR: {
2471 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2472 break;
2473 }
2474
2475 case Instruction::AND_LONG_2ADDR: {
2476 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2477 break;
2478 }
2479
2480 case Instruction::OR_INT_2ADDR: {
2481 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2482 break;
2483 }
2484
2485 case Instruction::OR_LONG_2ADDR: {
2486 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2487 break;
2488 }
2489
2490 case Instruction::XOR_INT_2ADDR: {
2491 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2492 break;
2493 }
2494
2495 case Instruction::XOR_LONG_2ADDR: {
2496 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2497 break;
2498 }
2499
2500 case Instruction::ADD_INT_LIT16: {
2501 Binop_22s<HAdd>(instruction, false, dex_pc);
2502 break;
2503 }
2504
2505 case Instruction::AND_INT_LIT16: {
2506 Binop_22s<HAnd>(instruction, false, dex_pc);
2507 break;
2508 }
2509
2510 case Instruction::OR_INT_LIT16: {
2511 Binop_22s<HOr>(instruction, false, dex_pc);
2512 break;
2513 }
2514
2515 case Instruction::XOR_INT_LIT16: {
2516 Binop_22s<HXor>(instruction, false, dex_pc);
2517 break;
2518 }
2519
2520 case Instruction::RSUB_INT: {
2521 Binop_22s<HSub>(instruction, true, dex_pc);
2522 break;
2523 }
2524
2525 case Instruction::MUL_INT_LIT16: {
2526 Binop_22s<HMul>(instruction, false, dex_pc);
2527 break;
2528 }
2529
2530 case Instruction::ADD_INT_LIT8: {
2531 Binop_22b<HAdd>(instruction, false, dex_pc);
2532 break;
2533 }
2534
2535 case Instruction::AND_INT_LIT8: {
2536 Binop_22b<HAnd>(instruction, false, dex_pc);
2537 break;
2538 }
2539
2540 case Instruction::OR_INT_LIT8: {
2541 Binop_22b<HOr>(instruction, false, dex_pc);
2542 break;
2543 }
2544
2545 case Instruction::XOR_INT_LIT8: {
2546 Binop_22b<HXor>(instruction, false, dex_pc);
2547 break;
2548 }
2549
2550 case Instruction::RSUB_INT_LIT8: {
2551 Binop_22b<HSub>(instruction, true, dex_pc);
2552 break;
2553 }
2554
2555 case Instruction::MUL_INT_LIT8: {
2556 Binop_22b<HMul>(instruction, false, dex_pc);
2557 break;
2558 }
2559
2560 case Instruction::DIV_INT_LIT16:
2561 case Instruction::DIV_INT_LIT8: {
2562 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2563 dex_pc, Primitive::kPrimInt, true, true);
2564 break;
2565 }
2566
2567 case Instruction::REM_INT_LIT16:
2568 case Instruction::REM_INT_LIT8: {
2569 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2570 dex_pc, Primitive::kPrimInt, true, false);
2571 break;
2572 }
2573
2574 case Instruction::SHL_INT_LIT8: {
2575 Binop_22b<HShl>(instruction, false, dex_pc);
2576 break;
2577 }
2578
2579 case Instruction::SHR_INT_LIT8: {
2580 Binop_22b<HShr>(instruction, false, dex_pc);
2581 break;
2582 }
2583
2584 case Instruction::USHR_INT_LIT8: {
2585 Binop_22b<HUShr>(instruction, false, dex_pc);
2586 break;
2587 }
2588
2589 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002590 HNewInstance* new_instance =
2591 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2592 DCHECK(new_instance != nullptr);
2593
David Brazdildee58d62016-04-07 09:54:26 +00002594 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002595 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002596 break;
2597 }
2598
2599 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002600 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002601 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002602 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002603
2604 HNewArray* new_array = new (arena_) HNewArray(cls, length, dex_pc);
2605 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002606 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002607 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002608 break;
2609 }
2610
2611 case Instruction::FILLED_NEW_ARRAY: {
2612 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002613 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002614 uint32_t args[5];
2615 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002616 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2617 type_index,
2618 number_of_vreg_arguments,
2619 /* is_range */ false,
2620 args,
2621 /* register_index */ 0);
2622 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002623 break;
2624 }
2625
2626 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2627 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002628 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002629 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002630 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2631 type_index,
2632 number_of_vreg_arguments,
2633 /* is_range */ true,
2634 /* args*/ nullptr,
2635 register_index);
2636 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002637 break;
2638 }
2639
2640 case Instruction::FILL_ARRAY_DATA: {
2641 BuildFillArrayData(instruction, dex_pc);
2642 break;
2643 }
2644
2645 case Instruction::MOVE_RESULT:
2646 case Instruction::MOVE_RESULT_WIDE:
2647 case Instruction::MOVE_RESULT_OBJECT: {
2648 DCHECK(latest_result_ != nullptr);
2649 UpdateLocal(instruction.VRegA(), latest_result_);
2650 latest_result_ = nullptr;
2651 break;
2652 }
2653
2654 case Instruction::CMP_LONG: {
2655 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2656 break;
2657 }
2658
2659 case Instruction::CMPG_FLOAT: {
2660 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2661 break;
2662 }
2663
2664 case Instruction::CMPG_DOUBLE: {
2665 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2666 break;
2667 }
2668
2669 case Instruction::CMPL_FLOAT: {
2670 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2671 break;
2672 }
2673
2674 case Instruction::CMPL_DOUBLE: {
2675 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2676 break;
2677 }
2678
2679 case Instruction::NOP:
2680 break;
2681
2682 case Instruction::IGET:
2683 case Instruction::IGET_QUICK:
2684 case Instruction::IGET_WIDE:
2685 case Instruction::IGET_WIDE_QUICK:
2686 case Instruction::IGET_OBJECT:
2687 case Instruction::IGET_OBJECT_QUICK:
2688 case Instruction::IGET_BOOLEAN:
2689 case Instruction::IGET_BOOLEAN_QUICK:
2690 case Instruction::IGET_BYTE:
2691 case Instruction::IGET_BYTE_QUICK:
2692 case Instruction::IGET_CHAR:
2693 case Instruction::IGET_CHAR_QUICK:
2694 case Instruction::IGET_SHORT:
2695 case Instruction::IGET_SHORT_QUICK: {
2696 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) {
2697 return false;
2698 }
2699 break;
2700 }
2701
2702 case Instruction::IPUT:
2703 case Instruction::IPUT_QUICK:
2704 case Instruction::IPUT_WIDE:
2705 case Instruction::IPUT_WIDE_QUICK:
2706 case Instruction::IPUT_OBJECT:
2707 case Instruction::IPUT_OBJECT_QUICK:
2708 case Instruction::IPUT_BOOLEAN:
2709 case Instruction::IPUT_BOOLEAN_QUICK:
2710 case Instruction::IPUT_BYTE:
2711 case Instruction::IPUT_BYTE_QUICK:
2712 case Instruction::IPUT_CHAR:
2713 case Instruction::IPUT_CHAR_QUICK:
2714 case Instruction::IPUT_SHORT:
2715 case Instruction::IPUT_SHORT_QUICK: {
2716 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) {
2717 return false;
2718 }
2719 break;
2720 }
2721
2722 case Instruction::SGET:
2723 case Instruction::SGET_WIDE:
2724 case Instruction::SGET_OBJECT:
2725 case Instruction::SGET_BOOLEAN:
2726 case Instruction::SGET_BYTE:
2727 case Instruction::SGET_CHAR:
2728 case Instruction::SGET_SHORT: {
2729 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2730 return false;
2731 }
2732 break;
2733 }
2734
2735 case Instruction::SPUT:
2736 case Instruction::SPUT_WIDE:
2737 case Instruction::SPUT_OBJECT:
2738 case Instruction::SPUT_BOOLEAN:
2739 case Instruction::SPUT_BYTE:
2740 case Instruction::SPUT_CHAR:
2741 case Instruction::SPUT_SHORT: {
2742 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2743 return false;
2744 }
2745 break;
2746 }
2747
2748#define ARRAY_XX(kind, anticipated_type) \
2749 case Instruction::AGET##kind: { \
2750 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2751 break; \
2752 } \
2753 case Instruction::APUT##kind: { \
2754 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2755 break; \
2756 }
2757
2758 ARRAY_XX(, Primitive::kPrimInt);
2759 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2760 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2761 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2762 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2763 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2764 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2765
2766 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002767 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002768 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2769 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2770 break;
2771 }
2772
2773 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002774 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002775 AppendInstruction(
2776 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2777 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2778 break;
2779 }
2780
2781 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002782 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002783 AppendInstruction(
2784 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2785 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2786 break;
2787 }
2788
2789 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002790 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002791 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002792 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2793 break;
2794 }
2795
2796 case Instruction::MOVE_EXCEPTION: {
2797 AppendInstruction(new (arena_) HLoadException(dex_pc));
2798 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2799 AppendInstruction(new (arena_) HClearException(dex_pc));
2800 break;
2801 }
2802
2803 case Instruction::THROW: {
2804 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2805 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2806 // We finished building this block. Set the current block to null to avoid
2807 // adding dead instructions to it.
2808 current_block_ = nullptr;
2809 break;
2810 }
2811
2812 case Instruction::INSTANCE_OF: {
2813 uint8_t destination = instruction.VRegA_22c();
2814 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002815 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002816 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2817 break;
2818 }
2819
2820 case Instruction::CHECK_CAST: {
2821 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002822 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002823 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2824 break;
2825 }
2826
2827 case Instruction::MONITOR_ENTER: {
2828 AppendInstruction(new (arena_) HMonitorOperation(
2829 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2830 HMonitorOperation::OperationKind::kEnter,
2831 dex_pc));
2832 break;
2833 }
2834
2835 case Instruction::MONITOR_EXIT: {
2836 AppendInstruction(new (arena_) HMonitorOperation(
2837 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2838 HMonitorOperation::OperationKind::kExit,
2839 dex_pc));
2840 break;
2841 }
2842
2843 case Instruction::SPARSE_SWITCH:
2844 case Instruction::PACKED_SWITCH: {
2845 BuildSwitch(instruction, dex_pc);
2846 break;
2847 }
2848
2849 default:
2850 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002851 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002852 << " because of unhandled instruction "
2853 << instruction.Name();
2854 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2855 return false;
2856 }
2857 return true;
2858} // NOLINT(readability/fn_size)
2859
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002860ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2861 dex::TypeIndex type_index,
2862 const DexCompilationUnit& compilation_unit) const {
2863 return ClassLinker::LookupResolvedType(
2864 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2865}
2866
2867ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2868 // TODO: Cache the result in a Handle<mirror::Class>.
2869 const DexFile::MethodId& method_id =
2870 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2871 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2872}
2873
David Brazdildee58d62016-04-07 09:54:26 +00002874} // namespace art