blob: 839f328a4f278d5f373865acb031a04ff8b92b0e [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"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070025#include "quicken_info.h"
Nicolas Geoffray83c8e272017-01-31 14:36:37 +000026#include "sharpening.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
David Brazdildee58d62016-04-07 09:54:26 +000028
29namespace art {
30
31void HInstructionBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) {
32 if (compilation_stats_ != nullptr) {
33 compilation_stats_->RecordStat(compilation_stat);
34 }
35}
36
37HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
38 return block_builder_->GetBlockAt(dex_pc);
39}
40
Mingyao Yang01b47b02017-02-03 12:09:57 -080041inline ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
David Brazdildee58d62016-04-07 09:54:26 +000042 ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
43 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080044 if (locals->size() == vregs) {
45 return locals;
46 }
47 return GetLocalsForWithAllocation(block, locals, vregs);
48}
David Brazdildee58d62016-04-07 09:54:26 +000049
Mingyao Yang01b47b02017-02-03 12:09:57 -080050ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
51 HBasicBlock* block,
52 ArenaVector<HInstruction*>* locals,
53 const size_t vregs) {
54 DCHECK_NE(locals->size(), vregs);
55 locals->resize(vregs, nullptr);
56 if (block->IsCatchBlock()) {
57 // We record incoming inputs of catch phis at throwing instructions and
58 // must therefore eagerly create the phis. Phis for undefined vregs will
59 // be deleted when the first throwing instruction with the vreg undefined
60 // is encountered. Unused phis will be removed by dead phi analysis.
61 for (size_t i = 0; i < vregs; ++i) {
62 // No point in creating the catch phi if it is already undefined at
63 // the first throwing instruction.
64 HInstruction* current_local_value = (*current_locals_)[i];
65 if (current_local_value != nullptr) {
66 HPhi* phi = new (arena_) HPhi(
67 arena_,
68 i,
69 0,
70 current_local_value->GetType());
71 block->AddPhi(phi);
72 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +000073 }
74 }
75 }
76 return locals;
77}
78
Mingyao Yang01b47b02017-02-03 12:09:57 -080079inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
David Brazdildee58d62016-04-07 09:54:26 +000080 ArenaVector<HInstruction*>* locals = GetLocalsFor(block);
81 return (*locals)[local];
82}
83
84void HInstructionBuilder::InitializeBlockLocals() {
85 current_locals_ = GetLocalsFor(current_block_);
86
87 if (current_block_->IsCatchBlock()) {
88 // Catch phis were already created and inputs collected from throwing sites.
89 if (kIsDebugBuild) {
90 // Make sure there was at least one throwing instruction which initialized
91 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
92 // visited already (from HTryBoundary scoping and reverse post order).
93 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +010094 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +000095 if (current == current_block_) {
96 catch_block_visited = true;
97 } else if (current->IsTryBlock()) {
98 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
99 if (try_entry.HasExceptionHandler(*current_block_)) {
100 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
101 }
102 }
103 }
104 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
105 << "No instructions throwing into a live catch block.";
106 }
107 } else if (current_block_->IsLoopHeader()) {
108 // If the block is a loop header, we know we only have visited the pre header
109 // because we are visiting in reverse post order. We create phis for all initialized
110 // locals from the pre header. Their inputs will be populated at the end of
111 // the analysis.
112 for (size_t local = 0; local < current_locals_->size(); ++local) {
113 HInstruction* incoming =
114 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
115 if (incoming != nullptr) {
116 HPhi* phi = new (arena_) HPhi(
117 arena_,
118 local,
119 0,
120 incoming->GetType());
121 current_block_->AddPhi(phi);
122 (*current_locals_)[local] = phi;
123 }
124 }
125
126 // Save the loop header so that the last phase of the analysis knows which
127 // blocks need to be updated.
128 loop_headers_.push_back(current_block_);
129 } else if (current_block_->GetPredecessors().size() > 0) {
130 // All predecessors have already been visited because we are visiting in reverse post order.
131 // We merge the values of all locals, creating phis if those values differ.
132 for (size_t local = 0; local < current_locals_->size(); ++local) {
133 bool one_predecessor_has_no_value = false;
134 bool is_different = false;
135 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
136
137 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
138 HInstruction* current = ValueOfLocalAt(predecessor, local);
139 if (current == nullptr) {
140 one_predecessor_has_no_value = true;
141 break;
142 } else if (current != value) {
143 is_different = true;
144 }
145 }
146
147 if (one_predecessor_has_no_value) {
148 // If one predecessor has no value for this local, we trust the verifier has
149 // successfully checked that there is a store dominating any read after this block.
150 continue;
151 }
152
153 if (is_different) {
154 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
155 HPhi* phi = new (arena_) HPhi(
156 arena_,
157 local,
158 current_block_->GetPredecessors().size(),
159 first_input->GetType());
160 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
161 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
162 phi->SetRawInputAt(i, pred_value);
163 }
164 current_block_->AddPhi(phi);
165 value = phi;
166 }
167 (*current_locals_)[local] = value;
168 }
169 }
170}
171
172void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
173 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
174 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
175 ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
176 DCHECK_EQ(handler_locals->size(), current_locals_->size());
177 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
178 HInstruction* handler_value = (*handler_locals)[vreg];
179 if (handler_value == nullptr) {
180 // Vreg was undefined at a previously encountered throwing instruction
181 // and the catch phi was deleted. Do not record the local value.
182 continue;
183 }
184 DCHECK(handler_value->IsPhi());
185
186 HInstruction* local_value = (*current_locals_)[vreg];
187 if (local_value == nullptr) {
188 // This is the first instruction throwing into `catch_block` where
189 // `vreg` is undefined. Delete the catch phi.
190 catch_block->RemovePhi(handler_value->AsPhi());
191 (*handler_locals)[vreg] = nullptr;
192 } else {
193 // Vreg has been defined at all instructions throwing into `catch_block`
194 // encountered so far. Record the local value in the catch phi.
195 handler_value->AsPhi()->AddInput(local_value);
196 }
197 }
198 }
199}
200
201void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
202 current_block_->AddInstruction(instruction);
203 InitializeInstruction(instruction);
204}
205
206void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
207 if (current_block_->GetInstructions().IsEmpty()) {
208 current_block_->AddInstruction(instruction);
209 } else {
210 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
211 }
212 InitializeInstruction(instruction);
213}
214
215void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
216 if (instruction->NeedsEnvironment()) {
217 HEnvironment* environment = new (arena_) HEnvironment(
218 arena_,
219 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000220 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000221 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000222 instruction);
223 environment->CopyFrom(*current_locals_);
224 instruction->SetRawEnvironment(environment);
225 }
226}
227
David Brazdilc120bbe2016-04-22 16:57:00 +0100228HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
229 HInstruction* ref = LoadLocal(register_index, Primitive::kPrimNot);
230 if (!ref->CanBeNull()) {
231 return ref;
232 }
233
234 HNullCheck* null_check = new (arena_) HNullCheck(ref, dex_pc);
235 AppendInstruction(null_check);
236 return null_check;
237}
238
David Brazdildee58d62016-04-07 09:54:26 +0000239void HInstructionBuilder::SetLoopHeaderPhiInputs() {
240 for (size_t i = loop_headers_.size(); i > 0; --i) {
241 HBasicBlock* block = loop_headers_[i - 1];
242 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
243 HPhi* phi = it.Current()->AsPhi();
244 size_t vreg = phi->GetRegNumber();
245 for (HBasicBlock* predecessor : block->GetPredecessors()) {
246 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
247 if (value == nullptr) {
248 // Vreg is undefined at this predecessor. Mark it dead and leave with
249 // fewer inputs than predecessors. SsaChecker will fail if not removed.
250 phi->SetDead();
251 break;
252 } else {
253 phi->AddInput(value);
254 }
255 }
256 }
257 }
258}
259
260static bool IsBlockPopulated(HBasicBlock* block) {
261 if (block->IsLoopHeader()) {
262 // Suspend checks were inserted into loop headers during building of dominator tree.
263 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
264 return block->GetFirstInstruction() != block->GetLastInstruction();
265 } else {
266 return !block->GetInstructions().IsEmpty();
267 }
268}
269
270bool HInstructionBuilder::Build() {
271 locals_for_.resize(graph_->GetBlocks().size(),
272 ArenaVector<HInstruction*>(arena_->Adapter(kArenaAllocGraphBuilder)));
273
274 // Find locations where we want to generate extra stackmaps for native debugging.
275 // This allows us to generate the info only at interesting points (for example,
276 // at start of java statement) rather than before every dex instruction.
277 const bool native_debuggable = compiler_driver_ != nullptr &&
278 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
279 ArenaBitVector* native_debug_info_locations = nullptr;
280 if (native_debuggable) {
281 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
282 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
283 FindNativeDebugInfoLocations(native_debug_info_locations);
284 }
285
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100286 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
287 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000288 uint32_t block_dex_pc = current_block_->GetDexPc();
289
290 InitializeBlockLocals();
291
292 if (current_block_->IsEntryBlock()) {
293 InitializeParameters();
294 AppendInstruction(new (arena_) HSuspendCheck(0u));
295 AppendInstruction(new (arena_) HGoto(0u));
296 continue;
297 } else if (current_block_->IsExitBlock()) {
298 AppendInstruction(new (arena_) HExit());
299 continue;
300 } else if (current_block_->IsLoopHeader()) {
301 HSuspendCheck* suspend_check = new (arena_) HSuspendCheck(current_block_->GetDexPc());
302 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
303 // This is slightly odd because the loop header might not be empty (TryBoundary).
304 // But we're still creating the environment with locals from the top of the block.
305 InsertInstructionAtTop(suspend_check);
306 }
307
308 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
309 // Synthetic block that does not need to be populated.
310 DCHECK(IsBlockPopulated(current_block_));
311 continue;
312 }
313
314 DCHECK(!IsBlockPopulated(current_block_));
315
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700316 uint32_t quicken_index = 0;
317 if (CanDecodeQuickenedInfo()) {
318 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
319 }
320
David Brazdildee58d62016-04-07 09:54:26 +0000321 for (CodeItemIterator it(code_item_, block_dex_pc); !it.Done(); it.Advance()) {
322 if (current_block_ == nullptr) {
323 // The previous instruction ended this block.
324 break;
325 }
326
327 uint32_t dex_pc = it.CurrentDexPc();
328 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
329 // This dex_pc starts a new basic block.
330 break;
331 }
332
333 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(it.CurrentInstruction())) {
334 PropagateLocalsToCatchBlocks();
335 }
336
337 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
338 AppendInstruction(new (arena_) HNativeDebugInfo(dex_pc));
339 }
340
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700341 if (!ProcessDexInstruction(it.CurrentInstruction(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000342 return false;
343 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700344
345 if (QuickenInfoTable::NeedsIndexForInstruction(&it.CurrentInstruction())) {
346 ++quicken_index;
347 }
David Brazdildee58d62016-04-07 09:54:26 +0000348 }
349
350 if (current_block_ != nullptr) {
351 // Branching instructions clear current_block, so we know the last
352 // instruction of the current block is not a branching instruction.
353 // We add an unconditional Goto to the next block.
354 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
355 AppendInstruction(new (arena_) HGoto());
356 }
357 }
358
359 SetLoopHeaderPhiInputs();
360
361 return true;
362}
363
364void HInstructionBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
365 // The callback gets called when the line number changes.
366 // In other words, it marks the start of new java statement.
367 struct Callback {
368 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
369 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
370 return false;
371 }
372 };
373 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
374 // Instruction-specific tweaks.
375 const Instruction* const begin = Instruction::At(code_item_.insns_);
376 const Instruction* const end = begin->RelativeAt(code_item_.insns_size_in_code_units_);
377 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
378 switch (inst->Opcode()) {
379 case Instruction::MOVE_EXCEPTION: {
380 // Stop in native debugger after the exception has been moved.
381 // The compiler also expects the move at the start of basic block so
382 // we do not want to interfere by inserting native-debug-info before it.
383 locations->ClearBit(inst->GetDexPc(code_item_.insns_));
384 const Instruction* next = inst->Next();
385 if (next < end) {
386 locations->SetBit(next->GetDexPc(code_item_.insns_));
387 }
388 break;
389 }
390 default:
391 break;
392 }
393 }
394}
395
396HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, Primitive::Type type) const {
397 HInstruction* value = (*current_locals_)[reg_number];
398 DCHECK(value != nullptr);
399
400 // If the operation requests a specific type, we make sure its input is of that type.
401 if (type != value->GetType()) {
402 if (Primitive::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700403 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
David Brazdildee58d62016-04-07 09:54:26 +0000404 } else if (type == Primitive::kPrimNot) {
Aart Bik31883642016-06-06 15:02:44 -0700405 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000406 }
Aart Bik31883642016-06-06 15:02:44 -0700407 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000408 }
409
410 return value;
411}
412
413void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
414 Primitive::Type stored_type = stored_value->GetType();
415 DCHECK_NE(stored_type, Primitive::kPrimVoid);
416
417 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
418 // registers. Consider the following cases:
419 // (1) Storing a wide value must overwrite previous values in both `reg_number`
420 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
421 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
422 // must invalidate it. We store `nullptr` in `reg_number-1`.
423 // Consequently, storing a wide value into the high vreg of another wide value
424 // will invalidate both `reg_number-1` and `reg_number+1`.
425
426 if (reg_number != 0) {
427 HInstruction* local_low = (*current_locals_)[reg_number - 1];
428 if (local_low != nullptr && Primitive::Is64BitType(local_low->GetType())) {
429 // The vreg we are storing into was previously the high vreg of a pair.
430 // We need to invalidate its low vreg.
431 DCHECK((*current_locals_)[reg_number] == nullptr);
432 (*current_locals_)[reg_number - 1] = nullptr;
433 }
434 }
435
436 (*current_locals_)[reg_number] = stored_value;
437 if (Primitive::Is64BitType(stored_type)) {
438 // We are storing a pair. Invalidate the instruction in the high vreg.
439 (*current_locals_)[reg_number + 1] = nullptr;
440 }
441}
442
443void HInstructionBuilder::InitializeParameters() {
444 DCHECK(current_block_->IsEntryBlock());
445
446 // dex_compilation_unit_ is null only when unit testing.
447 if (dex_compilation_unit_ == nullptr) {
448 return;
449 }
450
451 const char* shorty = dex_compilation_unit_->GetShorty();
452 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
453 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
454 uint16_t parameter_index = 0;
455
456 const DexFile::MethodId& referrer_method_id =
457 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
458 if (!dex_compilation_unit_->IsStatic()) {
459 // Add the implicit 'this' argument, not expressed in the signature.
460 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
461 referrer_method_id.class_idx_,
462 parameter_index++,
463 Primitive::kPrimNot,
Igor Murashkind01745e2017-04-05 16:40:31 -0700464 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000465 AppendInstruction(parameter);
466 UpdateLocal(locals_index++, parameter);
467 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700468 current_this_parameter_ = parameter;
469 } else {
470 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000471 }
472
473 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
474 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
475 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
476 HParameterValue* parameter = new (arena_) HParameterValue(
477 *dex_file_,
478 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
479 parameter_index++,
480 Primitive::GetType(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700481 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000482 ++shorty_pos;
483 AppendInstruction(parameter);
484 // Store the parameter value in the local that the dex code will use
485 // to reference that parameter.
486 UpdateLocal(locals_index++, parameter);
487 if (Primitive::Is64BitType(parameter->GetType())) {
488 i++;
489 locals_index++;
490 parameter_index++;
491 }
492 }
493}
494
495template<typename T>
496void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
497 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
498 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
499 T* comparison = new (arena_) T(first, second, dex_pc);
500 AppendInstruction(comparison);
501 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
502 current_block_ = nullptr;
503}
504
505template<typename T>
506void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
507 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
508 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
509 AppendInstruction(comparison);
510 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
511 current_block_ = nullptr;
512}
513
514template<typename T>
515void HInstructionBuilder::Unop_12x(const Instruction& instruction,
516 Primitive::Type type,
517 uint32_t dex_pc) {
518 HInstruction* first = LoadLocal(instruction.VRegB(), type);
519 AppendInstruction(new (arena_) T(type, first, dex_pc));
520 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
521}
522
523void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
524 Primitive::Type input_type,
525 Primitive::Type result_type,
526 uint32_t dex_pc) {
527 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
528 AppendInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
529 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
530}
531
532template<typename T>
533void HInstructionBuilder::Binop_23x(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(), type);
538 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
539 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
540}
541
542template<typename T>
543void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
544 Primitive::Type type,
545 uint32_t dex_pc) {
546 HInstruction* first = LoadLocal(instruction.VRegB(), type);
547 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
548 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
549 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
550}
551
552void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
553 Primitive::Type type,
554 ComparisonBias bias,
555 uint32_t dex_pc) {
556 HInstruction* first = LoadLocal(instruction.VRegB(), type);
557 HInstruction* second = LoadLocal(instruction.VRegC(), type);
558 AppendInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
559 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
560}
561
562template<typename T>
563void HInstructionBuilder::Binop_12x_shift(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(), Primitive::kPrimInt);
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_12x(const Instruction& instruction,
574 Primitive::Type type,
575 uint32_t dex_pc) {
576 HInstruction* first = LoadLocal(instruction.VRegA(), type);
577 HInstruction* second = LoadLocal(instruction.VRegB(), type);
578 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
579 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
580}
581
582template<typename T>
583void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
584 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
585 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
586 if (reverse) {
587 std::swap(first, second);
588 }
589 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
590 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
591}
592
593template<typename T>
594void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
595 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
596 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
597 if (reverse) {
598 std::swap(first, second);
599 }
600 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
601 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
602}
603
Igor Murashkind01745e2017-04-05 16:40:31 -0700604// Does the method being compiled need any constructor barriers being inserted?
605// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700606static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700607 // Can be null in unit tests only.
608 if (UNLIKELY(cu == nullptr)) {
609 return false;
610 }
611
David Brazdildee58d62016-04-07 09:54:26 +0000612 Thread* self = Thread::Current();
613 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700614 && !cu->IsStatic()
615 // RequiresConstructorBarrier must only be queried for <init> methods;
616 // it's effectively "false" for every other method.
617 //
618 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700619 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000620}
621
622// Returns true if `block` has only one successor which starts at the next
623// dex_pc after `instruction` at `dex_pc`.
624static bool IsFallthroughInstruction(const Instruction& instruction,
625 uint32_t dex_pc,
626 HBasicBlock* block) {
627 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
628 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
629}
630
631void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
632 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
633 DexSwitchTable table(instruction, dex_pc);
634
635 if (table.GetNumEntries() == 0) {
636 // Empty Switch. Code falls through to the next block.
637 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
638 AppendInstruction(new (arena_) HGoto(dex_pc));
639 } else if (table.ShouldBuildDecisionTree()) {
640 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
641 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
642 HEqual* comparison = new (arena_) HEqual(value, case_value, dex_pc);
643 AppendInstruction(comparison);
644 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
645
646 if (!it.IsLast()) {
647 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
648 }
649 }
650 } else {
651 AppendInstruction(
652 new (arena_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
653 }
654
655 current_block_ = nullptr;
656}
657
658void HInstructionBuilder::BuildReturn(const Instruction& instruction,
659 Primitive::Type type,
660 uint32_t dex_pc) {
661 if (type == Primitive::kPrimVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700662 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700663 // This may insert additional redundant constructor fences from the super constructors.
664 // TODO: remove redundant constructor fences (b/36656456).
665 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700666 // Compiling instance constructor.
667 if (kIsDebugBuild) {
668 std::string method_name = graph_->GetMethodName();
669 CHECK_EQ(std::string("<init>"), method_name);
670 }
671
672 HInstruction* fence_target = current_this_parameter_;
673 DCHECK(fence_target != nullptr);
674
675 AppendInstruction(new (arena_) HConstructorFence(fence_target, dex_pc, arena_));
David Brazdildee58d62016-04-07 09:54:26 +0000676 }
677 AppendInstruction(new (arena_) HReturnVoid(dex_pc));
678 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700679 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000680 HInstruction* value = LoadLocal(instruction.VRegA(), type);
681 AppendInstruction(new (arena_) HReturn(value, dex_pc));
682 }
683 current_block_ = nullptr;
684}
685
686static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
687 switch (opcode) {
688 case Instruction::INVOKE_STATIC:
689 case Instruction::INVOKE_STATIC_RANGE:
690 return kStatic;
691 case Instruction::INVOKE_DIRECT:
692 case Instruction::INVOKE_DIRECT_RANGE:
693 return kDirect;
694 case Instruction::INVOKE_VIRTUAL:
695 case Instruction::INVOKE_VIRTUAL_QUICK:
696 case Instruction::INVOKE_VIRTUAL_RANGE:
697 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
698 return kVirtual;
699 case Instruction::INVOKE_INTERFACE:
700 case Instruction::INVOKE_INTERFACE_RANGE:
701 return kInterface;
702 case Instruction::INVOKE_SUPER_RANGE:
703 case Instruction::INVOKE_SUPER:
704 return kSuper;
705 default:
706 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
707 UNREACHABLE();
708 }
709}
710
711ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
712 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000713 StackHandleScope<2> hs(soa.Self());
David Brazdildee58d62016-04-07 09:54:26 +0000714
715 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000716 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +0000717 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100718 // We fetch the referenced class eagerly (that is, the class pointed by in the MethodId
719 // at method_idx), as `CanAccessResolvedMethod` expects it be be in the dex cache.
720 Handle<mirror::Class> methods_class(hs.NewHandle(class_linker->ResolveReferencedClassOfMethod(
721 method_idx, dex_compilation_unit_->GetDexCache(), class_loader)));
722
Andreas Gampefa4333d2017-02-14 11:10:34 -0800723 if (UNLIKELY(methods_class == nullptr)) {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100724 // Clean up any exception left by type resolution.
725 soa.Self()->ClearException();
726 return nullptr;
727 }
David Brazdildee58d62016-04-07 09:54:26 +0000728
729 ArtMethod* resolved_method = class_linker->ResolveMethod<ClassLinker::kForceICCECheck>(
730 *dex_compilation_unit_->GetDexFile(),
731 method_idx,
732 dex_compilation_unit_->GetDexCache(),
733 class_loader,
734 /* referrer */ nullptr,
735 invoke_type);
736
737 if (UNLIKELY(resolved_method == nullptr)) {
738 // Clean up any exception left by type resolution.
739 soa.Self()->ClearException();
740 return nullptr;
741 }
742
743 // Check access. The class linker has a fast path for looking into the dex cache
744 // and does not check the access if it hits it.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800745 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000746 if (!resolved_method->IsPublic()) {
747 return nullptr;
748 }
749 } else if (!compiling_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
750 resolved_method,
751 dex_compilation_unit_->GetDexCache().Get(),
752 method_idx)) {
753 return nullptr;
754 }
755
756 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
757 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
758 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
759 // which require runtime handling.
760 if (invoke_type == kSuper) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800761 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000762 // We could not determine the method's class we need to wait until runtime.
763 DCHECK(Runtime::Current()->IsAotCompiler());
764 return nullptr;
765 }
Aart Bikf663e342016-04-04 17:28:59 -0700766 if (!methods_class->IsAssignableFrom(compiling_class.Get())) {
767 // We cannot statically determine the target method. The runtime will throw a
768 // NoSuchMethodError on this one.
769 return nullptr;
770 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100771 ArtMethod* actual_method;
772 if (methods_class->IsInterface()) {
773 actual_method = methods_class->FindVirtualMethodForInterfaceSuper(
774 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000775 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100776 uint16_t vtable_index = resolved_method->GetMethodIndex();
777 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
778 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000779 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100780 if (actual_method != resolved_method &&
781 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
782 // The back-end code generator relies on this check in order to ensure that it will not
783 // attempt to read the dex_cache with a dex_method_index that is not from the correct
784 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
785 // builder, which means that the code-generator (and compiler driver during sharpening and
786 // inliner, maybe) might invoke an incorrect method.
787 // TODO: The actual method could still be referenced in the current dex file, so we
788 // could try locating it.
789 // TODO: Remove the dex_file restriction.
790 return nullptr;
791 }
792 if (!actual_method->IsInvokable()) {
793 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
794 // could resolve the callee to the wrong method.
795 return nullptr;
796 }
797 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000798 }
799
800 // Check for incompatible class changes. The class linker has a fast path for
801 // looking into the dex cache and does not check incompatible class changes if it hits it.
802 if (resolved_method->CheckIncompatibleClassChange(invoke_type)) {
803 return nullptr;
804 }
805
806 return resolved_method;
807}
808
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100809static bool IsStringConstructor(ArtMethod* method) {
810 ScopedObjectAccess soa(Thread::Current());
811 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
812}
813
David Brazdildee58d62016-04-07 09:54:26 +0000814bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
815 uint32_t dex_pc,
816 uint32_t method_idx,
817 uint32_t number_of_vreg_arguments,
818 bool is_range,
819 uint32_t* args,
820 uint32_t register_index) {
821 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
822 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
823 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
824
825 // Remove the return type from the 'proto'.
826 size_t number_of_arguments = strlen(descriptor) - 1;
827 if (invoke_type != kStatic) { // instance call
828 // One extra argument for 'this'.
829 number_of_arguments++;
830 }
831
David Brazdildee58d62016-04-07 09:54:26 +0000832 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
833
834 if (UNLIKELY(resolved_method == nullptr)) {
835 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
836 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
837 number_of_arguments,
838 return_type,
839 dex_pc,
840 method_idx,
841 invoke_type);
842 return HandleInvoke(invoke,
843 number_of_vreg_arguments,
844 args,
845 register_index,
846 is_range,
847 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700848 nullptr, /* clinit_check */
849 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000850 }
851
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100852 // Replace calls to String.<init> with StringFactory.
853 if (IsStringConstructor(resolved_method)) {
854 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
855 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
856 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
857 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000858 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100859 };
860 MethodReference target_method(dex_file_, method_idx);
861 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
862 arena_,
863 number_of_arguments - 1,
864 Primitive::kPrimNot /*return_type */,
865 dex_pc,
866 method_idx,
867 nullptr,
868 dispatch_info,
869 invoke_type,
870 target_method,
871 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
872 return HandleStringInit(invoke,
873 number_of_vreg_arguments,
874 args,
875 register_index,
876 is_range,
877 descriptor);
878 }
879
David Brazdildee58d62016-04-07 09:54:26 +0000880 // Potential class initialization check, in the case of a static method call.
881 HClinitCheck* clinit_check = nullptr;
882 HInvoke* invoke = nullptr;
883 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
884 // By default, consider that the called method implicitly requires
885 // an initialization check of its declaring method.
886 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
887 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
888 ScopedObjectAccess soa(Thread::Current());
889 if (invoke_type == kStatic) {
890 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000891 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000892 } else if (invoke_type == kSuper) {
893 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100894 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000895 // we resolved to the method referenced by the instruction.
896 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000897 }
898 }
899
900 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100901 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000902 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000903 0u
David Brazdildee58d62016-04-07 09:54:26 +0000904 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100905 MethodReference target_method(resolved_method->GetDexFile(),
906 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000907 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
908 number_of_arguments,
909 return_type,
910 dex_pc,
911 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100912 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000913 dispatch_info,
914 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100915 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000916 clinit_check_requirement);
917 } else if (invoke_type == kVirtual) {
918 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
919 invoke = new (arena_) HInvokeVirtual(arena_,
920 number_of_arguments,
921 return_type,
922 dex_pc,
923 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100924 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000925 resolved_method->GetMethodIndex());
926 } else {
927 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100928 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000929 invoke = new (arena_) HInvokeInterface(arena_,
930 number_of_arguments,
931 return_type,
932 dex_pc,
933 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100934 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700935 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000936 }
937
938 return HandleInvoke(invoke,
939 number_of_vreg_arguments,
940 args,
941 register_index,
942 is_range,
943 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700944 clinit_check,
945 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000946}
947
Orion Hodsonac141392017-01-13 11:53:47 +0000948bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
949 uint32_t dex_pc,
950 uint32_t method_idx,
951 uint32_t proto_idx,
952 uint32_t number_of_vreg_arguments,
953 bool is_range,
954 uint32_t* args,
955 uint32_t register_index) {
956 const char* descriptor = dex_file_->GetShorty(proto_idx);
957 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
958 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
959 size_t number_of_arguments = strlen(descriptor);
960 HInvoke* invoke = new (arena_) HInvokePolymorphic(arena_,
961 number_of_arguments,
962 return_type,
963 dex_pc,
964 method_idx);
965 return HandleInvoke(invoke,
966 number_of_vreg_arguments,
967 args,
968 register_index,
969 is_range,
970 descriptor,
971 nullptr /* clinit_check */,
972 false /* is_unresolved */);
973}
974
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700975HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100976 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000977
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000978 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000979
David Brazdildee58d62016-04-07 09:54:26 +0000980 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000981 Handle<mirror::Class> klass = load_class->GetClass();
982
983 if (!IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +0000984 cls = new (arena_) HClinitCheck(load_class, dex_pc);
985 AppendInstruction(cls);
986 }
987
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000988 // Only the access check entrypoint handles the finalizable class case. If we
989 // need access checks, then we haven't resolved the method and the class may
990 // again be finalizable.
991 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
992 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
993 entrypoint = kQuickAllocObjectWithChecks;
994 }
995
996 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800997 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000998
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700999 HNewInstance* new_instance = new (arena_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001000 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001001 dex_pc,
1002 type_index,
1003 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001004 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001005 entrypoint);
1006 AppendInstruction(new_instance);
1007
1008 return new_instance;
1009}
1010
1011void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1012 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001013 (allocation->IsNewInstance() ||
1014 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001015
1016 if (allocation->IsNewInstance()) {
1017 // STRING SPECIAL HANDLING:
1018 // -------------------------------
1019 // Strings have a real HNewInstance node but they end up always having 0 uses.
1020 // All uses of a String HNewInstance are always transformed to replace their input
1021 // of the HNewInstance with an input of the invoke to StringFactory.
1022 //
1023 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1024 // optimizations (to pass checker tests that rely on those optimizations).
1025 HNewInstance* new_inst = allocation->AsNewInstance();
1026 HLoadClass* load_class = new_inst->GetLoadClass();
1027
1028 Thread* self = Thread::Current();
1029 ScopedObjectAccess soa(self);
1030 StackHandleScope<1> hs(self);
1031 Handle<mirror::Class> klass = load_class->GetClass();
1032 if (klass != nullptr && klass->IsStringClass()) {
1033 return;
1034 // Note: Do not use allocation->IsStringAlloc which requires
1035 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1036 // propagation (and instruction builder is too early).
1037 }
1038 // (In terms of correctness, the StringFactory needs to provide its own
1039 // default initialization barrier, see below.)
1040 }
1041
1042 // JLS 17.4.5 "Happens-before Order" describes:
1043 //
1044 // The default initialization of any object happens-before any other actions (other than
1045 // default-writes) of a program.
1046 //
1047 // In our implementation the default initialization of an object to type T means
1048 // setting all of its initial data (object[0..size)) to 0, and setting the
1049 // object's class header (i.e. object.getClass() == T.class).
1050 //
1051 // In practice this fence ensures that the writes to the object header
1052 // are visible to other threads if this object escapes the current thread.
1053 // (and in theory the 0-initializing, but that happens automatically
1054 // when new memory pages are mapped in by the OS).
1055 HConstructorFence* ctor_fence =
1056 new (arena_) HConstructorFence(allocation, allocation->GetDexPc(), arena_);
1057 AppendInstruction(ctor_fence);
David Brazdildee58d62016-04-07 09:54:26 +00001058}
1059
1060static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001061 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001062 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1063}
1064
1065bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001066 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001067 return false;
1068 }
1069
1070 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1071 // check whether the class is in an image for the AOT compilation.
1072 if (cls->IsInitialized() &&
1073 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1074 return true;
1075 }
1076
1077 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1078 return true;
1079 }
1080
1081 // TODO: We should walk over the inlined methods, but we don't pass
1082 // that information to the builder.
1083 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1084 return true;
1085 }
1086
1087 return false;
1088}
1089
1090HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1091 uint32_t dex_pc,
1092 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001093 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001094 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001095
1096 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001097 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001098 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001099 } else {
1100 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1101 klass->GetDexFile(),
1102 klass,
1103 dex_pc,
1104 /* needs_access_check */ false);
1105 if (cls != nullptr) {
1106 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1107 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
1108 AppendInstruction(clinit_check);
1109 }
David Brazdildee58d62016-04-07 09:54:26 +00001110 }
1111 return clinit_check;
1112}
1113
1114bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1115 uint32_t number_of_vreg_arguments,
1116 uint32_t* args,
1117 uint32_t register_index,
1118 bool is_range,
1119 const char* descriptor,
1120 size_t start_index,
1121 size_t* argument_index) {
1122 uint32_t descriptor_index = 1; // Skip the return type.
1123
1124 for (size_t i = start_index;
1125 // Make sure we don't go over the expected arguments or over the number of
1126 // dex registers given. If the instruction was seen as dead by the verifier,
1127 // it hasn't been properly checked.
1128 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1129 i++, (*argument_index)++) {
1130 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1131 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1132 if (!is_range
1133 && is_wide
1134 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1135 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1136 // reject any class where this is violated. However, the verifier only does these checks
1137 // on non trivially dead instructions, so we just bailout the compilation.
1138 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001139 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001140 << " because of non-sequential dex register pair in wide argument";
1141 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1142 return false;
1143 }
1144 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1145 invoke->SetArgumentAt(*argument_index, arg);
1146 if (is_wide) {
1147 i++;
1148 }
1149 }
1150
1151 if (*argument_index != invoke->GetNumberOfArguments()) {
1152 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001153 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001154 << " because of wrong number of arguments in invoke instruction";
1155 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1156 return false;
1157 }
1158
1159 if (invoke->IsInvokeStaticOrDirect() &&
1160 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1161 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1162 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1163 (*argument_index)++;
1164 }
1165
1166 return true;
1167}
1168
1169bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1170 uint32_t number_of_vreg_arguments,
1171 uint32_t* args,
1172 uint32_t register_index,
1173 bool is_range,
1174 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001175 HClinitCheck* clinit_check,
1176 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001177 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1178
1179 size_t start_index = 0;
1180 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001181 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001182 uint32_t obj_reg = is_range ? register_index : args[0];
1183 HInstruction* arg = is_unresolved
1184 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1185 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001186 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001187 start_index = 1;
1188 argument_index = 1;
1189 }
1190
1191 if (!SetupInvokeArguments(invoke,
1192 number_of_vreg_arguments,
1193 args,
1194 register_index,
1195 is_range,
1196 descriptor,
1197 start_index,
1198 &argument_index)) {
1199 return false;
1200 }
1201
1202 if (clinit_check != nullptr) {
1203 // Add the class initialization check as last input of `invoke`.
1204 DCHECK(invoke->IsInvokeStaticOrDirect());
1205 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1206 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1207 invoke->SetArgumentAt(argument_index, clinit_check);
1208 argument_index++;
1209 }
1210
1211 AppendInstruction(invoke);
1212 latest_result_ = invoke;
1213
1214 return true;
1215}
1216
1217bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1218 uint32_t number_of_vreg_arguments,
1219 uint32_t* args,
1220 uint32_t register_index,
1221 bool is_range,
1222 const char* descriptor) {
1223 DCHECK(invoke->IsInvokeStaticOrDirect());
1224 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1225
1226 size_t start_index = 1;
1227 size_t argument_index = 0;
1228 if (!SetupInvokeArguments(invoke,
1229 number_of_vreg_arguments,
1230 args,
1231 register_index,
1232 is_range,
1233 descriptor,
1234 start_index,
1235 &argument_index)) {
1236 return false;
1237 }
1238
1239 AppendInstruction(invoke);
1240
1241 // This is a StringFactory call, not an actual String constructor. Its result
1242 // replaces the empty String pre-allocated by NewInstance.
1243 uint32_t orig_this_reg = is_range ? register_index : args[0];
1244 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1245
1246 // Replacing the NewInstance might render it redundant. Keep a list of these
1247 // to be visited once it is clear whether it is has remaining uses.
1248 if (arg_this->IsNewInstance()) {
1249 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1250 } else {
1251 DCHECK(arg_this->IsPhi());
1252 // NewInstance is not the direct input of the StringFactory call. It might
1253 // be redundant but optimizing this case is not worth the effort.
1254 }
1255
1256 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1257 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1258 if ((*current_locals_)[vreg] == arg_this) {
1259 (*current_locals_)[vreg] = invoke;
1260 }
1261 }
1262
1263 return true;
1264}
1265
1266static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1267 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1268 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1269 return Primitive::GetType(type[0]);
1270}
1271
1272bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1273 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001274 bool is_put,
1275 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001276 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1277 uint32_t obj_reg = instruction.VRegB_22c();
1278 uint16_t field_index;
1279 if (instruction.IsQuickened()) {
1280 if (!CanDecodeQuickenedInfo()) {
1281 return false;
1282 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001283 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001284 } else {
1285 field_index = instruction.VRegC_22c();
1286 }
1287
1288 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001289 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001290
Aart Bik14154132016-06-02 17:53:58 -07001291 // Generate an explicit null check on the reference, unless the field access
1292 // is unresolved. In that case, we rely on the runtime to perform various
1293 // checks first, followed by a null check.
1294 HInstruction* object = (resolved_field == nullptr)
1295 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1296 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001297
1298 Primitive::Type field_type = (resolved_field == nullptr)
1299 ? GetFieldAccessType(*dex_file_, field_index)
1300 : resolved_field->GetTypeAsPrimitiveType();
1301 if (is_put) {
1302 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1303 HInstruction* field_set = nullptr;
1304 if (resolved_field == nullptr) {
1305 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001306 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001307 value,
1308 field_type,
1309 field_index,
1310 dex_pc);
1311 } else {
1312 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001313 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001314 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001315 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001316 field_type,
1317 resolved_field->GetOffset(),
1318 resolved_field->IsVolatile(),
1319 field_index,
1320 class_def_index,
1321 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001322 dex_pc);
1323 }
1324 AppendInstruction(field_set);
1325 } else {
1326 HInstruction* field_get = nullptr;
1327 if (resolved_field == nullptr) {
1328 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001329 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001330 field_type,
1331 field_index,
1332 dex_pc);
1333 } else {
1334 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001335 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001336 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001337 field_type,
1338 resolved_field->GetOffset(),
1339 resolved_field->IsVolatile(),
1340 field_index,
1341 class_def_index,
1342 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001343 dex_pc);
1344 }
1345 AppendInstruction(field_get);
1346 UpdateLocal(source_or_dest_reg, field_get);
1347 }
1348
1349 return true;
1350}
1351
1352static mirror::Class* GetClassFrom(CompilerDriver* driver,
1353 const DexCompilationUnit& compilation_unit) {
1354 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001355 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001356 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001357
1358 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1359}
1360
1361mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1362 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1363}
1364
1365mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1366 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1367}
1368
Andreas Gampea5b09a62016-11-17 15:21:22 -08001369bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001370 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001371 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001372 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001373 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001374 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1375 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1376 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1377
1378 // GetOutermostCompilingClass returns null when the class is unresolved
1379 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1380 // we are compiling it.
1381 // When this happens we cannot establish a direct relation between the current
1382 // class and the outer class, so we return false.
1383 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001384 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001385}
1386
1387void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001388 uint32_t dex_pc,
1389 bool is_put,
1390 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001391 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1392 uint16_t field_index = instruction.VRegB_21c();
1393
1394 if (is_put) {
1395 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1396 AppendInstruction(
1397 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1398 } else {
1399 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1400 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1401 }
1402}
1403
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001404ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1405 ScopedObjectAccess soa(Thread::Current());
1406 StackHandleScope<2> hs(soa.Self());
1407
1408 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001409 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001410 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1411
1412 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1413 field_idx,
1414 dex_compilation_unit_->GetDexCache(),
1415 class_loader,
1416 is_static);
1417
1418 if (UNLIKELY(resolved_field == nullptr)) {
1419 // Clean up any exception left by type resolution.
1420 soa.Self()->ClearException();
1421 return nullptr;
1422 }
1423
1424 // Check static/instance. The class linker has a fast path for looking into the dex cache
1425 // and does not check static/instance if it hits it.
1426 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1427 return nullptr;
1428 }
1429
1430 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001431 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001432 if (!resolved_field->IsPublic()) {
1433 return nullptr;
1434 }
1435 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1436 resolved_field,
1437 dex_compilation_unit_->GetDexCache().Get(),
1438 field_idx)) {
1439 return nullptr;
1440 }
1441
1442 if (is_put &&
1443 resolved_field->IsFinal() &&
1444 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1445 // Final fields can only be updated within their own class.
1446 // TODO: Only allow it in constructors. b/34966607.
1447 return nullptr;
1448 }
1449
1450 return resolved_field;
1451}
1452
David Brazdildee58d62016-04-07 09:54:26 +00001453bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1454 uint32_t dex_pc,
1455 bool is_put) {
1456 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1457 uint16_t field_index = instruction.VRegB_21c();
1458
1459 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001460 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001461
1462 if (resolved_field == nullptr) {
1463 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1464 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1465 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1466 return true;
1467 }
1468
1469 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
David Brazdildee58d62016-04-07 09:54:26 +00001470
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001471 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1472 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1473 klass->GetDexFile(),
1474 klass,
1475 dex_pc,
1476 /* needs_access_check */ false);
1477
1478 if (constant == nullptr) {
1479 // The class cannot be referenced from this compiled code. Generate
1480 // an unresolved access.
1481 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1482 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1483 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001484 }
1485
David Brazdildee58d62016-04-07 09:54:26 +00001486 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001487 if (!IsInitialized(klass)) {
1488 cls = new (arena_) HClinitCheck(constant, dex_pc);
1489 AppendInstruction(cls);
1490 }
1491
1492 uint16_t class_def_index = klass->GetDexClassDefIndex();
1493 if (is_put) {
1494 // We need to keep the class alive before loading the value.
1495 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1496 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1497 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1498 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001499 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001500 field_type,
1501 resolved_field->GetOffset(),
1502 resolved_field->IsVolatile(),
1503 field_index,
1504 class_def_index,
1505 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001506 dex_pc));
1507 } else {
1508 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001509 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001510 field_type,
1511 resolved_field->GetOffset(),
1512 resolved_field->IsVolatile(),
1513 field_index,
1514 class_def_index,
1515 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001516 dex_pc));
1517 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1518 }
1519 return true;
1520}
1521
1522void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1523 uint16_t first_vreg,
1524 int64_t second_vreg_or_constant,
1525 uint32_t dex_pc,
1526 Primitive::Type type,
1527 bool second_is_constant,
1528 bool isDiv) {
1529 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1530
1531 HInstruction* first = LoadLocal(first_vreg, type);
1532 HInstruction* second = nullptr;
1533 if (second_is_constant) {
1534 if (type == Primitive::kPrimInt) {
1535 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1536 } else {
1537 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1538 }
1539 } else {
1540 second = LoadLocal(second_vreg_or_constant, type);
1541 }
1542
1543 if (!second_is_constant
1544 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1545 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1546 second = new (arena_) HDivZeroCheck(second, dex_pc);
1547 AppendInstruction(second);
1548 }
1549
1550 if (isDiv) {
1551 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1552 } else {
1553 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1554 }
1555 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1556}
1557
1558void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1559 uint32_t dex_pc,
1560 bool is_put,
1561 Primitive::Type anticipated_type) {
1562 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1563 uint8_t array_reg = instruction.VRegB_23x();
1564 uint8_t index_reg = instruction.VRegC_23x();
1565
David Brazdilc120bbe2016-04-22 16:57:00 +01001566 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001567 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1568 AppendInstruction(length);
1569 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1570 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1571 AppendInstruction(index);
1572 if (is_put) {
1573 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1574 // TODO: Insert a type check node if the type is Object.
1575 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1576 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1577 AppendInstruction(aset);
1578 } else {
1579 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1580 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1581 AppendInstruction(aget);
1582 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1583 }
1584 graph_->SetHasBoundsChecks(true);
1585}
1586
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001587HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1588 dex::TypeIndex type_index,
1589 uint32_t number_of_vreg_arguments,
1590 bool is_range,
1591 uint32_t* args,
1592 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001593 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001594 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001595 HNewArray* const object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001596 AppendInstruction(object);
1597
1598 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1599 DCHECK_EQ(descriptor[0], '[') << descriptor;
1600 char primitive = descriptor[1];
1601 DCHECK(primitive == 'I'
1602 || primitive == 'L'
1603 || primitive == '[') << descriptor;
1604 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1605 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1606
1607 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1608 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1609 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1610 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1611 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1612 AppendInstruction(aset);
1613 }
1614 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001615
1616 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001617}
1618
1619template <typename T>
1620void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1621 const T* data,
1622 uint32_t element_count,
1623 Primitive::Type anticipated_type,
1624 uint32_t dex_pc) {
1625 for (uint32_t i = 0; i < element_count; ++i) {
1626 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1627 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1628 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1629 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1630 AppendInstruction(aset);
1631 }
1632}
1633
1634void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001635 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001636
1637 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1638 const Instruction::ArrayDataPayload* payload =
1639 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1640 const uint8_t* data = payload->data;
1641 uint32_t element_count = payload->element_count;
1642
Vladimir Markoc69fba22016-09-06 16:49:15 +01001643 if (element_count == 0u) {
1644 // For empty payload we emit only the null check above.
1645 return;
1646 }
1647
1648 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1649 AppendInstruction(length);
1650
David Brazdildee58d62016-04-07 09:54:26 +00001651 // Implementation of this DEX instruction seems to be that the bounds check is
1652 // done before doing any stores.
1653 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1654 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1655
1656 switch (payload->element_width) {
1657 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001658 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001659 reinterpret_cast<const int8_t*>(data),
1660 element_count,
1661 Primitive::kPrimByte,
1662 dex_pc);
1663 break;
1664 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001665 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001666 reinterpret_cast<const int16_t*>(data),
1667 element_count,
1668 Primitive::kPrimShort,
1669 dex_pc);
1670 break;
1671 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001672 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001673 reinterpret_cast<const int32_t*>(data),
1674 element_count,
1675 Primitive::kPrimInt,
1676 dex_pc);
1677 break;
1678 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001679 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001680 reinterpret_cast<const int64_t*>(data),
1681 element_count,
1682 dex_pc);
1683 break;
1684 default:
1685 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1686 }
1687 graph_->SetHasBoundsChecks(true);
1688}
1689
1690void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1691 const int64_t* data,
1692 uint32_t element_count,
1693 uint32_t dex_pc) {
1694 for (uint32_t i = 0; i < element_count; ++i) {
1695 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1696 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1697 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1698 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1699 AppendInstruction(aset);
1700 }
1701}
1702
1703static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001704 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001705 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001706 return TypeCheckKind::kUnresolvedCheck;
1707 } else if (cls->IsInterface()) {
1708 return TypeCheckKind::kInterfaceCheck;
1709 } else if (cls->IsArrayClass()) {
1710 if (cls->GetComponentType()->IsObjectClass()) {
1711 return TypeCheckKind::kArrayObjectCheck;
1712 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1713 return TypeCheckKind::kExactCheck;
1714 } else {
1715 return TypeCheckKind::kArrayCheck;
1716 }
1717 } else if (cls->IsFinal()) {
1718 return TypeCheckKind::kExactCheck;
1719 } else if (cls->IsAbstract()) {
1720 return TypeCheckKind::kAbstractClassCheck;
1721 } else {
1722 return TypeCheckKind::kClassHierarchyCheck;
1723 }
1724}
1725
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001726HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001727 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001728 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001729 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001730 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001731 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001732
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001733 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001734 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001735 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001736 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001737 } else {
1738 mirror::Class* compiling_class = GetCompilingClass();
1739 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001740 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001741 }
1742 }
1743 }
1744
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001745 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1746}
1747
1748HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1749 const DexFile& dex_file,
1750 Handle<mirror::Class> klass,
1751 uint32_t dex_pc,
1752 bool needs_access_check) {
1753 // Try to find a reference in the compiling dex file.
1754 const DexFile* actual_dex_file = &dex_file;
1755 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1756 dex::TypeIndex local_type_index =
1757 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1758 if (local_type_index.IsValid()) {
1759 type_index = local_type_index;
1760 actual_dex_file = dex_compilation_unit_->GetDexFile();
1761 }
1762 }
1763
1764 // Note: `klass` must be from `handles_`.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001765 HLoadClass* load_class = new (arena_) HLoadClass(
1766 graph_->GetCurrentMethod(),
1767 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001768 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001769 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001770 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001771 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001772 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001773
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001774 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1775 code_generator_,
1776 compiler_driver_,
1777 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001778
1779 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1780 // We actually cannot reference this class, we're forced to bail.
1781 return nullptr;
1782 }
1783 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001784 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001785 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001786 return load_class;
1787}
1788
David Brazdildee58d62016-04-07 09:54:26 +00001789void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1790 uint8_t destination,
1791 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001792 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001793 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001794 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001795 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001796
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001797 ScopedObjectAccess soa(Thread::Current());
1798 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001799 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1800 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1801 UpdateLocal(destination, current_block_->GetLastInstruction());
1802 } else {
1803 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1804 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1805 // which may throw. If it succeeds BoundType sets the new type of `object`
1806 // for all subsequent uses.
1807 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1808 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1809 UpdateLocal(reference, current_block_->GetLastInstruction());
1810 }
1811}
1812
Vladimir Marko0b66d612017-03-13 14:50:04 +00001813bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001814 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1815 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001816}
1817
1818bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001819 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001820}
1821
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001822uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1823 DCHECK(CanDecodeQuickenedInfo());
1824 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001825}
1826
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001827bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1828 uint32_t dex_pc,
1829 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001830 switch (instruction.Opcode()) {
1831 case Instruction::CONST_4: {
1832 int32_t register_index = instruction.VRegA();
1833 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1834 UpdateLocal(register_index, constant);
1835 break;
1836 }
1837
1838 case Instruction::CONST_16: {
1839 int32_t register_index = instruction.VRegA();
1840 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1841 UpdateLocal(register_index, constant);
1842 break;
1843 }
1844
1845 case Instruction::CONST: {
1846 int32_t register_index = instruction.VRegA();
1847 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1848 UpdateLocal(register_index, constant);
1849 break;
1850 }
1851
1852 case Instruction::CONST_HIGH16: {
1853 int32_t register_index = instruction.VRegA();
1854 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1855 UpdateLocal(register_index, constant);
1856 break;
1857 }
1858
1859 case Instruction::CONST_WIDE_16: {
1860 int32_t register_index = instruction.VRegA();
1861 // Get 16 bits of constant value, sign extended to 64 bits.
1862 int64_t value = instruction.VRegB_21s();
1863 value <<= 48;
1864 value >>= 48;
1865 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1866 UpdateLocal(register_index, constant);
1867 break;
1868 }
1869
1870 case Instruction::CONST_WIDE_32: {
1871 int32_t register_index = instruction.VRegA();
1872 // Get 32 bits of constant value, sign extended to 64 bits.
1873 int64_t value = instruction.VRegB_31i();
1874 value <<= 32;
1875 value >>= 32;
1876 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1877 UpdateLocal(register_index, constant);
1878 break;
1879 }
1880
1881 case Instruction::CONST_WIDE: {
1882 int32_t register_index = instruction.VRegA();
1883 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1884 UpdateLocal(register_index, constant);
1885 break;
1886 }
1887
1888 case Instruction::CONST_WIDE_HIGH16: {
1889 int32_t register_index = instruction.VRegA();
1890 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1891 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1892 UpdateLocal(register_index, constant);
1893 break;
1894 }
1895
1896 // Note that the SSA building will refine the types.
1897 case Instruction::MOVE:
1898 case Instruction::MOVE_FROM16:
1899 case Instruction::MOVE_16: {
1900 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1901 UpdateLocal(instruction.VRegA(), value);
1902 break;
1903 }
1904
1905 // Note that the SSA building will refine the types.
1906 case Instruction::MOVE_WIDE:
1907 case Instruction::MOVE_WIDE_FROM16:
1908 case Instruction::MOVE_WIDE_16: {
1909 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1910 UpdateLocal(instruction.VRegA(), value);
1911 break;
1912 }
1913
1914 case Instruction::MOVE_OBJECT:
1915 case Instruction::MOVE_OBJECT_16:
1916 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001917 // The verifier has no notion of a null type, so a move-object of constant 0
1918 // will lead to the same constant 0 in the destination register. To mimic
1919 // this behavior, we just pretend we haven't seen a type change (int to reference)
1920 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1921 // types correct.
1922 uint32_t reg_number = instruction.VRegB();
1923 HInstruction* value = (*current_locals_)[reg_number];
1924 if (value->IsIntConstant()) {
1925 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1926 } else if (value->IsPhi()) {
1927 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1928 } else {
1929 value = LoadLocal(reg_number, Primitive::kPrimNot);
1930 }
David Brazdildee58d62016-04-07 09:54:26 +00001931 UpdateLocal(instruction.VRegA(), value);
1932 break;
1933 }
1934
1935 case Instruction::RETURN_VOID_NO_BARRIER:
1936 case Instruction::RETURN_VOID: {
1937 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1938 break;
1939 }
1940
1941#define IF_XX(comparison, cond) \
1942 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1943 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1944
1945 IF_XX(HEqual, EQ);
1946 IF_XX(HNotEqual, NE);
1947 IF_XX(HLessThan, LT);
1948 IF_XX(HLessThanOrEqual, LE);
1949 IF_XX(HGreaterThan, GT);
1950 IF_XX(HGreaterThanOrEqual, GE);
1951
1952 case Instruction::GOTO:
1953 case Instruction::GOTO_16:
1954 case Instruction::GOTO_32: {
1955 AppendInstruction(new (arena_) HGoto(dex_pc));
1956 current_block_ = nullptr;
1957 break;
1958 }
1959
1960 case Instruction::RETURN: {
1961 BuildReturn(instruction, return_type_, dex_pc);
1962 break;
1963 }
1964
1965 case Instruction::RETURN_OBJECT: {
1966 BuildReturn(instruction, return_type_, dex_pc);
1967 break;
1968 }
1969
1970 case Instruction::RETURN_WIDE: {
1971 BuildReturn(instruction, return_type_, dex_pc);
1972 break;
1973 }
1974
1975 case Instruction::INVOKE_DIRECT:
1976 case Instruction::INVOKE_INTERFACE:
1977 case Instruction::INVOKE_STATIC:
1978 case Instruction::INVOKE_SUPER:
1979 case Instruction::INVOKE_VIRTUAL:
1980 case Instruction::INVOKE_VIRTUAL_QUICK: {
1981 uint16_t method_idx;
1982 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1983 if (!CanDecodeQuickenedInfo()) {
1984 return false;
1985 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001986 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001987 } else {
1988 method_idx = instruction.VRegB_35c();
1989 }
1990 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1991 uint32_t args[5];
1992 instruction.GetVarArgs(args);
1993 if (!BuildInvoke(instruction, dex_pc, method_idx,
1994 number_of_vreg_arguments, false, args, -1)) {
1995 return false;
1996 }
1997 break;
1998 }
1999
2000 case Instruction::INVOKE_DIRECT_RANGE:
2001 case Instruction::INVOKE_INTERFACE_RANGE:
2002 case Instruction::INVOKE_STATIC_RANGE:
2003 case Instruction::INVOKE_SUPER_RANGE:
2004 case Instruction::INVOKE_VIRTUAL_RANGE:
2005 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2006 uint16_t method_idx;
2007 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2008 if (!CanDecodeQuickenedInfo()) {
2009 return false;
2010 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002011 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002012 } else {
2013 method_idx = instruction.VRegB_3rc();
2014 }
2015 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2016 uint32_t register_index = instruction.VRegC();
2017 if (!BuildInvoke(instruction, dex_pc, method_idx,
2018 number_of_vreg_arguments, true, nullptr, register_index)) {
2019 return false;
2020 }
2021 break;
2022 }
2023
Orion Hodsonac141392017-01-13 11:53:47 +00002024 case Instruction::INVOKE_POLYMORPHIC: {
2025 uint16_t method_idx = instruction.VRegB_45cc();
2026 uint16_t proto_idx = instruction.VRegH_45cc();
2027 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2028 uint32_t args[5];
2029 instruction.GetVarArgs(args);
2030 return BuildInvokePolymorphic(instruction,
2031 dex_pc,
2032 method_idx,
2033 proto_idx,
2034 number_of_vreg_arguments,
2035 false,
2036 args,
2037 -1);
2038 }
2039
2040 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2041 uint16_t method_idx = instruction.VRegB_4rcc();
2042 uint16_t proto_idx = instruction.VRegH_4rcc();
2043 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2044 uint32_t register_index = instruction.VRegC_4rcc();
2045 return BuildInvokePolymorphic(instruction,
2046 dex_pc,
2047 method_idx,
2048 proto_idx,
2049 number_of_vreg_arguments,
2050 true,
2051 nullptr,
2052 register_index);
2053 }
2054
David Brazdildee58d62016-04-07 09:54:26 +00002055 case Instruction::NEG_INT: {
2056 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
2057 break;
2058 }
2059
2060 case Instruction::NEG_LONG: {
2061 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
2062 break;
2063 }
2064
2065 case Instruction::NEG_FLOAT: {
2066 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
2067 break;
2068 }
2069
2070 case Instruction::NEG_DOUBLE: {
2071 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
2072 break;
2073 }
2074
2075 case Instruction::NOT_INT: {
2076 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
2077 break;
2078 }
2079
2080 case Instruction::NOT_LONG: {
2081 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
2082 break;
2083 }
2084
2085 case Instruction::INT_TO_LONG: {
2086 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2087 break;
2088 }
2089
2090 case Instruction::INT_TO_FLOAT: {
2091 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2092 break;
2093 }
2094
2095 case Instruction::INT_TO_DOUBLE: {
2096 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2097 break;
2098 }
2099
2100 case Instruction::LONG_TO_INT: {
2101 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2102 break;
2103 }
2104
2105 case Instruction::LONG_TO_FLOAT: {
2106 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2107 break;
2108 }
2109
2110 case Instruction::LONG_TO_DOUBLE: {
2111 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2112 break;
2113 }
2114
2115 case Instruction::FLOAT_TO_INT: {
2116 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2117 break;
2118 }
2119
2120 case Instruction::FLOAT_TO_LONG: {
2121 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2122 break;
2123 }
2124
2125 case Instruction::FLOAT_TO_DOUBLE: {
2126 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2127 break;
2128 }
2129
2130 case Instruction::DOUBLE_TO_INT: {
2131 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2132 break;
2133 }
2134
2135 case Instruction::DOUBLE_TO_LONG: {
2136 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2137 break;
2138 }
2139
2140 case Instruction::DOUBLE_TO_FLOAT: {
2141 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2142 break;
2143 }
2144
2145 case Instruction::INT_TO_BYTE: {
2146 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2147 break;
2148 }
2149
2150 case Instruction::INT_TO_SHORT: {
2151 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2152 break;
2153 }
2154
2155 case Instruction::INT_TO_CHAR: {
2156 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2157 break;
2158 }
2159
2160 case Instruction::ADD_INT: {
2161 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2162 break;
2163 }
2164
2165 case Instruction::ADD_LONG: {
2166 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2167 break;
2168 }
2169
2170 case Instruction::ADD_DOUBLE: {
2171 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2172 break;
2173 }
2174
2175 case Instruction::ADD_FLOAT: {
2176 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2177 break;
2178 }
2179
2180 case Instruction::SUB_INT: {
2181 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2182 break;
2183 }
2184
2185 case Instruction::SUB_LONG: {
2186 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2187 break;
2188 }
2189
2190 case Instruction::SUB_FLOAT: {
2191 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2192 break;
2193 }
2194
2195 case Instruction::SUB_DOUBLE: {
2196 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2197 break;
2198 }
2199
2200 case Instruction::ADD_INT_2ADDR: {
2201 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2202 break;
2203 }
2204
2205 case Instruction::MUL_INT: {
2206 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2207 break;
2208 }
2209
2210 case Instruction::MUL_LONG: {
2211 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2212 break;
2213 }
2214
2215 case Instruction::MUL_FLOAT: {
2216 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2217 break;
2218 }
2219
2220 case Instruction::MUL_DOUBLE: {
2221 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2222 break;
2223 }
2224
2225 case Instruction::DIV_INT: {
2226 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2227 dex_pc, Primitive::kPrimInt, false, true);
2228 break;
2229 }
2230
2231 case Instruction::DIV_LONG: {
2232 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2233 dex_pc, Primitive::kPrimLong, false, true);
2234 break;
2235 }
2236
2237 case Instruction::DIV_FLOAT: {
2238 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2239 break;
2240 }
2241
2242 case Instruction::DIV_DOUBLE: {
2243 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2244 break;
2245 }
2246
2247 case Instruction::REM_INT: {
2248 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2249 dex_pc, Primitive::kPrimInt, false, false);
2250 break;
2251 }
2252
2253 case Instruction::REM_LONG: {
2254 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2255 dex_pc, Primitive::kPrimLong, false, false);
2256 break;
2257 }
2258
2259 case Instruction::REM_FLOAT: {
2260 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2261 break;
2262 }
2263
2264 case Instruction::REM_DOUBLE: {
2265 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2266 break;
2267 }
2268
2269 case Instruction::AND_INT: {
2270 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2271 break;
2272 }
2273
2274 case Instruction::AND_LONG: {
2275 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2276 break;
2277 }
2278
2279 case Instruction::SHL_INT: {
2280 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2281 break;
2282 }
2283
2284 case Instruction::SHL_LONG: {
2285 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2286 break;
2287 }
2288
2289 case Instruction::SHR_INT: {
2290 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2291 break;
2292 }
2293
2294 case Instruction::SHR_LONG: {
2295 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2296 break;
2297 }
2298
2299 case Instruction::USHR_INT: {
2300 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2301 break;
2302 }
2303
2304 case Instruction::USHR_LONG: {
2305 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2306 break;
2307 }
2308
2309 case Instruction::OR_INT: {
2310 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2311 break;
2312 }
2313
2314 case Instruction::OR_LONG: {
2315 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2316 break;
2317 }
2318
2319 case Instruction::XOR_INT: {
2320 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2321 break;
2322 }
2323
2324 case Instruction::XOR_LONG: {
2325 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2326 break;
2327 }
2328
2329 case Instruction::ADD_LONG_2ADDR: {
2330 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2331 break;
2332 }
2333
2334 case Instruction::ADD_DOUBLE_2ADDR: {
2335 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2336 break;
2337 }
2338
2339 case Instruction::ADD_FLOAT_2ADDR: {
2340 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2341 break;
2342 }
2343
2344 case Instruction::SUB_INT_2ADDR: {
2345 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2346 break;
2347 }
2348
2349 case Instruction::SUB_LONG_2ADDR: {
2350 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2351 break;
2352 }
2353
2354 case Instruction::SUB_FLOAT_2ADDR: {
2355 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2356 break;
2357 }
2358
2359 case Instruction::SUB_DOUBLE_2ADDR: {
2360 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2361 break;
2362 }
2363
2364 case Instruction::MUL_INT_2ADDR: {
2365 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2366 break;
2367 }
2368
2369 case Instruction::MUL_LONG_2ADDR: {
2370 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2371 break;
2372 }
2373
2374 case Instruction::MUL_FLOAT_2ADDR: {
2375 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2376 break;
2377 }
2378
2379 case Instruction::MUL_DOUBLE_2ADDR: {
2380 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2381 break;
2382 }
2383
2384 case Instruction::DIV_INT_2ADDR: {
2385 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2386 dex_pc, Primitive::kPrimInt, false, true);
2387 break;
2388 }
2389
2390 case Instruction::DIV_LONG_2ADDR: {
2391 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2392 dex_pc, Primitive::kPrimLong, false, true);
2393 break;
2394 }
2395
2396 case Instruction::REM_INT_2ADDR: {
2397 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2398 dex_pc, Primitive::kPrimInt, false, false);
2399 break;
2400 }
2401
2402 case Instruction::REM_LONG_2ADDR: {
2403 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2404 dex_pc, Primitive::kPrimLong, false, false);
2405 break;
2406 }
2407
2408 case Instruction::REM_FLOAT_2ADDR: {
2409 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2410 break;
2411 }
2412
2413 case Instruction::REM_DOUBLE_2ADDR: {
2414 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2415 break;
2416 }
2417
2418 case Instruction::SHL_INT_2ADDR: {
2419 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2420 break;
2421 }
2422
2423 case Instruction::SHL_LONG_2ADDR: {
2424 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2425 break;
2426 }
2427
2428 case Instruction::SHR_INT_2ADDR: {
2429 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2430 break;
2431 }
2432
2433 case Instruction::SHR_LONG_2ADDR: {
2434 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2435 break;
2436 }
2437
2438 case Instruction::USHR_INT_2ADDR: {
2439 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2440 break;
2441 }
2442
2443 case Instruction::USHR_LONG_2ADDR: {
2444 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2445 break;
2446 }
2447
2448 case Instruction::DIV_FLOAT_2ADDR: {
2449 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2450 break;
2451 }
2452
2453 case Instruction::DIV_DOUBLE_2ADDR: {
2454 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2455 break;
2456 }
2457
2458 case Instruction::AND_INT_2ADDR: {
2459 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2460 break;
2461 }
2462
2463 case Instruction::AND_LONG_2ADDR: {
2464 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2465 break;
2466 }
2467
2468 case Instruction::OR_INT_2ADDR: {
2469 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2470 break;
2471 }
2472
2473 case Instruction::OR_LONG_2ADDR: {
2474 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2475 break;
2476 }
2477
2478 case Instruction::XOR_INT_2ADDR: {
2479 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2480 break;
2481 }
2482
2483 case Instruction::XOR_LONG_2ADDR: {
2484 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2485 break;
2486 }
2487
2488 case Instruction::ADD_INT_LIT16: {
2489 Binop_22s<HAdd>(instruction, false, dex_pc);
2490 break;
2491 }
2492
2493 case Instruction::AND_INT_LIT16: {
2494 Binop_22s<HAnd>(instruction, false, dex_pc);
2495 break;
2496 }
2497
2498 case Instruction::OR_INT_LIT16: {
2499 Binop_22s<HOr>(instruction, false, dex_pc);
2500 break;
2501 }
2502
2503 case Instruction::XOR_INT_LIT16: {
2504 Binop_22s<HXor>(instruction, false, dex_pc);
2505 break;
2506 }
2507
2508 case Instruction::RSUB_INT: {
2509 Binop_22s<HSub>(instruction, true, dex_pc);
2510 break;
2511 }
2512
2513 case Instruction::MUL_INT_LIT16: {
2514 Binop_22s<HMul>(instruction, false, dex_pc);
2515 break;
2516 }
2517
2518 case Instruction::ADD_INT_LIT8: {
2519 Binop_22b<HAdd>(instruction, false, dex_pc);
2520 break;
2521 }
2522
2523 case Instruction::AND_INT_LIT8: {
2524 Binop_22b<HAnd>(instruction, false, dex_pc);
2525 break;
2526 }
2527
2528 case Instruction::OR_INT_LIT8: {
2529 Binop_22b<HOr>(instruction, false, dex_pc);
2530 break;
2531 }
2532
2533 case Instruction::XOR_INT_LIT8: {
2534 Binop_22b<HXor>(instruction, false, dex_pc);
2535 break;
2536 }
2537
2538 case Instruction::RSUB_INT_LIT8: {
2539 Binop_22b<HSub>(instruction, true, dex_pc);
2540 break;
2541 }
2542
2543 case Instruction::MUL_INT_LIT8: {
2544 Binop_22b<HMul>(instruction, false, dex_pc);
2545 break;
2546 }
2547
2548 case Instruction::DIV_INT_LIT16:
2549 case Instruction::DIV_INT_LIT8: {
2550 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2551 dex_pc, Primitive::kPrimInt, true, true);
2552 break;
2553 }
2554
2555 case Instruction::REM_INT_LIT16:
2556 case Instruction::REM_INT_LIT8: {
2557 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2558 dex_pc, Primitive::kPrimInt, true, false);
2559 break;
2560 }
2561
2562 case Instruction::SHL_INT_LIT8: {
2563 Binop_22b<HShl>(instruction, false, dex_pc);
2564 break;
2565 }
2566
2567 case Instruction::SHR_INT_LIT8: {
2568 Binop_22b<HShr>(instruction, false, dex_pc);
2569 break;
2570 }
2571
2572 case Instruction::USHR_INT_LIT8: {
2573 Binop_22b<HUShr>(instruction, false, dex_pc);
2574 break;
2575 }
2576
2577 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002578 HNewInstance* new_instance =
2579 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2580 DCHECK(new_instance != nullptr);
2581
David Brazdildee58d62016-04-07 09:54:26 +00002582 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002583 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002584 break;
2585 }
2586
2587 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002588 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002589 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002590 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002591
2592 HNewArray* new_array = new (arena_) HNewArray(cls, length, dex_pc);
2593 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002594 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002595 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002596 break;
2597 }
2598
2599 case Instruction::FILLED_NEW_ARRAY: {
2600 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002601 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002602 uint32_t args[5];
2603 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002604 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2605 type_index,
2606 number_of_vreg_arguments,
2607 /* is_range */ false,
2608 args,
2609 /* register_index */ 0);
2610 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002611 break;
2612 }
2613
2614 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2615 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002616 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002617 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002618 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2619 type_index,
2620 number_of_vreg_arguments,
2621 /* is_range */ true,
2622 /* args*/ nullptr,
2623 register_index);
2624 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002625 break;
2626 }
2627
2628 case Instruction::FILL_ARRAY_DATA: {
2629 BuildFillArrayData(instruction, dex_pc);
2630 break;
2631 }
2632
2633 case Instruction::MOVE_RESULT:
2634 case Instruction::MOVE_RESULT_WIDE:
2635 case Instruction::MOVE_RESULT_OBJECT: {
2636 DCHECK(latest_result_ != nullptr);
2637 UpdateLocal(instruction.VRegA(), latest_result_);
2638 latest_result_ = nullptr;
2639 break;
2640 }
2641
2642 case Instruction::CMP_LONG: {
2643 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2644 break;
2645 }
2646
2647 case Instruction::CMPG_FLOAT: {
2648 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2649 break;
2650 }
2651
2652 case Instruction::CMPG_DOUBLE: {
2653 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2654 break;
2655 }
2656
2657 case Instruction::CMPL_FLOAT: {
2658 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2659 break;
2660 }
2661
2662 case Instruction::CMPL_DOUBLE: {
2663 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2664 break;
2665 }
2666
2667 case Instruction::NOP:
2668 break;
2669
2670 case Instruction::IGET:
2671 case Instruction::IGET_QUICK:
2672 case Instruction::IGET_WIDE:
2673 case Instruction::IGET_WIDE_QUICK:
2674 case Instruction::IGET_OBJECT:
2675 case Instruction::IGET_OBJECT_QUICK:
2676 case Instruction::IGET_BOOLEAN:
2677 case Instruction::IGET_BOOLEAN_QUICK:
2678 case Instruction::IGET_BYTE:
2679 case Instruction::IGET_BYTE_QUICK:
2680 case Instruction::IGET_CHAR:
2681 case Instruction::IGET_CHAR_QUICK:
2682 case Instruction::IGET_SHORT:
2683 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002684 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002685 return false;
2686 }
2687 break;
2688 }
2689
2690 case Instruction::IPUT:
2691 case Instruction::IPUT_QUICK:
2692 case Instruction::IPUT_WIDE:
2693 case Instruction::IPUT_WIDE_QUICK:
2694 case Instruction::IPUT_OBJECT:
2695 case Instruction::IPUT_OBJECT_QUICK:
2696 case Instruction::IPUT_BOOLEAN:
2697 case Instruction::IPUT_BOOLEAN_QUICK:
2698 case Instruction::IPUT_BYTE:
2699 case Instruction::IPUT_BYTE_QUICK:
2700 case Instruction::IPUT_CHAR:
2701 case Instruction::IPUT_CHAR_QUICK:
2702 case Instruction::IPUT_SHORT:
2703 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002704 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002705 return false;
2706 }
2707 break;
2708 }
2709
2710 case Instruction::SGET:
2711 case Instruction::SGET_WIDE:
2712 case Instruction::SGET_OBJECT:
2713 case Instruction::SGET_BOOLEAN:
2714 case Instruction::SGET_BYTE:
2715 case Instruction::SGET_CHAR:
2716 case Instruction::SGET_SHORT: {
2717 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2718 return false;
2719 }
2720 break;
2721 }
2722
2723 case Instruction::SPUT:
2724 case Instruction::SPUT_WIDE:
2725 case Instruction::SPUT_OBJECT:
2726 case Instruction::SPUT_BOOLEAN:
2727 case Instruction::SPUT_BYTE:
2728 case Instruction::SPUT_CHAR:
2729 case Instruction::SPUT_SHORT: {
2730 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2731 return false;
2732 }
2733 break;
2734 }
2735
2736#define ARRAY_XX(kind, anticipated_type) \
2737 case Instruction::AGET##kind: { \
2738 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2739 break; \
2740 } \
2741 case Instruction::APUT##kind: { \
2742 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2743 break; \
2744 }
2745
2746 ARRAY_XX(, Primitive::kPrimInt);
2747 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2748 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2749 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2750 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2751 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2752 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2753
2754 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002755 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002756 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2757 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2758 break;
2759 }
2760
2761 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002762 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002763 AppendInstruction(
2764 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2765 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2766 break;
2767 }
2768
2769 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002770 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002771 AppendInstruction(
2772 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2773 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2774 break;
2775 }
2776
2777 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002778 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002779 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002780 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2781 break;
2782 }
2783
2784 case Instruction::MOVE_EXCEPTION: {
2785 AppendInstruction(new (arena_) HLoadException(dex_pc));
2786 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2787 AppendInstruction(new (arena_) HClearException(dex_pc));
2788 break;
2789 }
2790
2791 case Instruction::THROW: {
2792 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2793 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2794 // We finished building this block. Set the current block to null to avoid
2795 // adding dead instructions to it.
2796 current_block_ = nullptr;
2797 break;
2798 }
2799
2800 case Instruction::INSTANCE_OF: {
2801 uint8_t destination = instruction.VRegA_22c();
2802 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002803 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002804 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2805 break;
2806 }
2807
2808 case Instruction::CHECK_CAST: {
2809 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002810 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002811 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2812 break;
2813 }
2814
2815 case Instruction::MONITOR_ENTER: {
2816 AppendInstruction(new (arena_) HMonitorOperation(
2817 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2818 HMonitorOperation::OperationKind::kEnter,
2819 dex_pc));
2820 break;
2821 }
2822
2823 case Instruction::MONITOR_EXIT: {
2824 AppendInstruction(new (arena_) HMonitorOperation(
2825 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2826 HMonitorOperation::OperationKind::kExit,
2827 dex_pc));
2828 break;
2829 }
2830
2831 case Instruction::SPARSE_SWITCH:
2832 case Instruction::PACKED_SWITCH: {
2833 BuildSwitch(instruction, dex_pc);
2834 break;
2835 }
2836
2837 default:
2838 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002839 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002840 << " because of unhandled instruction "
2841 << instruction.Name();
2842 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2843 return false;
2844 }
2845 return true;
2846} // NOLINT(readability/fn_size)
2847
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002848ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2849 dex::TypeIndex type_index,
2850 const DexCompilationUnit& compilation_unit) const {
2851 return ClassLinker::LookupResolvedType(
2852 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2853}
2854
2855ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2856 // TODO: Cache the result in a Handle<mirror::Class>.
2857 const DexFile::MethodId& method_id =
2858 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2859 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2860}
2861
David Brazdildee58d62016-04-07 09:54:26 +00002862} // namespace art