blob: 805414092416b9047f0bfa2597455449a78ad587 [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.
Vladimir Markoba118822017-06-12 15:41:56 +0100667 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700668
669 HInstruction* fence_target = current_this_parameter_;
670 DCHECK(fence_target != nullptr);
671
672 AppendInstruction(new (arena_) HConstructorFence(fence_target, dex_pc, arena_));
David Brazdildee58d62016-04-07 09:54:26 +0000673 }
674 AppendInstruction(new (arena_) HReturnVoid(dex_pc));
675 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700676 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000677 HInstruction* value = LoadLocal(instruction.VRegA(), type);
678 AppendInstruction(new (arena_) HReturn(value, dex_pc));
679 }
680 current_block_ = nullptr;
681}
682
683static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
684 switch (opcode) {
685 case Instruction::INVOKE_STATIC:
686 case Instruction::INVOKE_STATIC_RANGE:
687 return kStatic;
688 case Instruction::INVOKE_DIRECT:
689 case Instruction::INVOKE_DIRECT_RANGE:
690 return kDirect;
691 case Instruction::INVOKE_VIRTUAL:
692 case Instruction::INVOKE_VIRTUAL_QUICK:
693 case Instruction::INVOKE_VIRTUAL_RANGE:
694 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
695 return kVirtual;
696 case Instruction::INVOKE_INTERFACE:
697 case Instruction::INVOKE_INTERFACE_RANGE:
698 return kInterface;
699 case Instruction::INVOKE_SUPER_RANGE:
700 case Instruction::INVOKE_SUPER:
701 return kSuper;
702 default:
703 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
704 UNREACHABLE();
705 }
706}
707
708ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
709 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000710
711 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000712 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100713
Vladimir Markoba118822017-06-12 15:41:56 +0100714 ArtMethod* resolved_method =
715 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
716 *dex_compilation_unit_->GetDexFile(),
717 method_idx,
718 dex_compilation_unit_->GetDexCache(),
719 class_loader,
720 graph_->GetArtMethod(),
721 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000722
723 if (UNLIKELY(resolved_method == nullptr)) {
724 // Clean up any exception left by type resolution.
725 soa.Self()->ClearException();
726 return nullptr;
727 }
728
Vladimir Markoba118822017-06-12 15:41:56 +0100729 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
730 // resolved because, for example, we don't find a superclass in the classpath.
731 if (graph_->GetArtMethod() == nullptr) {
732 // The class linker cannot check access without a referrer, so we have to do it.
733 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000734 if (!resolved_method->IsPublic()) {
735 return nullptr;
736 }
David Brazdildee58d62016-04-07 09:54:26 +0000737 }
738
739 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
740 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
741 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
742 // which require runtime handling.
743 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100744 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800745 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000746 // We could not determine the method's class we need to wait until runtime.
747 DCHECK(Runtime::Current()->IsAotCompiler());
748 return nullptr;
749 }
Vladimir Markoba118822017-06-12 15:41:56 +0100750 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
751 *dex_compilation_unit_->GetDexFile(),
752 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
753 dex_compilation_unit_->GetDexCache().Get(),
754 class_loader.Get());
755 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
756 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700757 // We cannot statically determine the target method. The runtime will throw a
758 // NoSuchMethodError on this one.
759 return nullptr;
760 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100761 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100762 if (referenced_class->IsInterface()) {
763 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100764 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000765 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100766 uint16_t vtable_index = resolved_method->GetMethodIndex();
767 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
768 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000769 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100770 if (actual_method != resolved_method &&
771 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
772 // The back-end code generator relies on this check in order to ensure that it will not
773 // attempt to read the dex_cache with a dex_method_index that is not from the correct
774 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
775 // builder, which means that the code-generator (and compiler driver during sharpening and
776 // inliner, maybe) might invoke an incorrect method.
777 // TODO: The actual method could still be referenced in the current dex file, so we
778 // could try locating it.
779 // TODO: Remove the dex_file restriction.
780 return nullptr;
781 }
782 if (!actual_method->IsInvokable()) {
783 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
784 // could resolve the callee to the wrong method.
785 return nullptr;
786 }
787 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000788 }
789
David Brazdildee58d62016-04-07 09:54:26 +0000790 return resolved_method;
791}
792
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100793static bool IsStringConstructor(ArtMethod* method) {
794 ScopedObjectAccess soa(Thread::Current());
795 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
796}
797
David Brazdildee58d62016-04-07 09:54:26 +0000798bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
799 uint32_t dex_pc,
800 uint32_t method_idx,
801 uint32_t number_of_vreg_arguments,
802 bool is_range,
803 uint32_t* args,
804 uint32_t register_index) {
805 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
806 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
807 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
808
809 // Remove the return type from the 'proto'.
810 size_t number_of_arguments = strlen(descriptor) - 1;
811 if (invoke_type != kStatic) { // instance call
812 // One extra argument for 'this'.
813 number_of_arguments++;
814 }
815
David Brazdildee58d62016-04-07 09:54:26 +0000816 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
817
818 if (UNLIKELY(resolved_method == nullptr)) {
819 MaybeRecordStat(MethodCompilationStat::kUnresolvedMethod);
820 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
821 number_of_arguments,
822 return_type,
823 dex_pc,
824 method_idx,
825 invoke_type);
826 return HandleInvoke(invoke,
827 number_of_vreg_arguments,
828 args,
829 register_index,
830 is_range,
831 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700832 nullptr, /* clinit_check */
833 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000834 }
835
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100836 // Replace calls to String.<init> with StringFactory.
837 if (IsStringConstructor(resolved_method)) {
838 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
839 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
840 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
841 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000842 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100843 };
844 MethodReference target_method(dex_file_, method_idx);
845 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
846 arena_,
847 number_of_arguments - 1,
848 Primitive::kPrimNot /*return_type */,
849 dex_pc,
850 method_idx,
851 nullptr,
852 dispatch_info,
853 invoke_type,
854 target_method,
855 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
856 return HandleStringInit(invoke,
857 number_of_vreg_arguments,
858 args,
859 register_index,
860 is_range,
861 descriptor);
862 }
863
David Brazdildee58d62016-04-07 09:54:26 +0000864 // Potential class initialization check, in the case of a static method call.
865 HClinitCheck* clinit_check = nullptr;
866 HInvoke* invoke = nullptr;
867 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
868 // By default, consider that the called method implicitly requires
869 // an initialization check of its declaring method.
870 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
871 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
872 ScopedObjectAccess soa(Thread::Current());
873 if (invoke_type == kStatic) {
874 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000875 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000876 } else if (invoke_type == kSuper) {
877 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100878 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000879 // we resolved to the method referenced by the instruction.
880 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000881 }
882 }
883
884 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100885 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000886 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000887 0u
David Brazdildee58d62016-04-07 09:54:26 +0000888 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100889 MethodReference target_method(resolved_method->GetDexFile(),
890 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000891 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
892 number_of_arguments,
893 return_type,
894 dex_pc,
895 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100896 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000897 dispatch_info,
898 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100899 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000900 clinit_check_requirement);
901 } else if (invoke_type == kVirtual) {
902 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
903 invoke = new (arena_) HInvokeVirtual(arena_,
904 number_of_arguments,
905 return_type,
906 dex_pc,
907 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100908 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000909 resolved_method->GetMethodIndex());
910 } else {
911 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100912 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000913 invoke = new (arena_) HInvokeInterface(arena_,
914 number_of_arguments,
915 return_type,
916 dex_pc,
917 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100918 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700919 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000920 }
921
922 return HandleInvoke(invoke,
923 number_of_vreg_arguments,
924 args,
925 register_index,
926 is_range,
927 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700928 clinit_check,
929 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000930}
931
Orion Hodsonac141392017-01-13 11:53:47 +0000932bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
933 uint32_t dex_pc,
934 uint32_t method_idx,
935 uint32_t proto_idx,
936 uint32_t number_of_vreg_arguments,
937 bool is_range,
938 uint32_t* args,
939 uint32_t register_index) {
940 const char* descriptor = dex_file_->GetShorty(proto_idx);
941 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
942 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
943 size_t number_of_arguments = strlen(descriptor);
944 HInvoke* invoke = new (arena_) HInvokePolymorphic(arena_,
945 number_of_arguments,
946 return_type,
947 dex_pc,
948 method_idx);
949 return HandleInvoke(invoke,
950 number_of_vreg_arguments,
951 args,
952 register_index,
953 is_range,
954 descriptor,
955 nullptr /* clinit_check */,
956 false /* is_unresolved */);
957}
958
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700959HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100960 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000961
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000962 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000963
David Brazdildee58d62016-04-07 09:54:26 +0000964 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000965 Handle<mirror::Class> klass = load_class->GetClass();
966
967 if (!IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +0000968 cls = new (arena_) HClinitCheck(load_class, dex_pc);
969 AppendInstruction(cls);
970 }
971
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000972 // Only the access check entrypoint handles the finalizable class case. If we
973 // need access checks, then we haven't resolved the method and the class may
974 // again be finalizable.
975 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
976 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
977 entrypoint = kQuickAllocObjectWithChecks;
978 }
979
980 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800981 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000982
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700983 HNewInstance* new_instance = new (arena_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000984 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000985 dex_pc,
986 type_index,
987 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +0000988 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700989 entrypoint);
990 AppendInstruction(new_instance);
991
992 return new_instance;
993}
994
995void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
996 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -0700997 (allocation->IsNewInstance() ||
998 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700999
1000 if (allocation->IsNewInstance()) {
1001 // STRING SPECIAL HANDLING:
1002 // -------------------------------
1003 // Strings have a real HNewInstance node but they end up always having 0 uses.
1004 // All uses of a String HNewInstance are always transformed to replace their input
1005 // of the HNewInstance with an input of the invoke to StringFactory.
1006 //
1007 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1008 // optimizations (to pass checker tests that rely on those optimizations).
1009 HNewInstance* new_inst = allocation->AsNewInstance();
1010 HLoadClass* load_class = new_inst->GetLoadClass();
1011
1012 Thread* self = Thread::Current();
1013 ScopedObjectAccess soa(self);
1014 StackHandleScope<1> hs(self);
1015 Handle<mirror::Class> klass = load_class->GetClass();
1016 if (klass != nullptr && klass->IsStringClass()) {
1017 return;
1018 // Note: Do not use allocation->IsStringAlloc which requires
1019 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1020 // propagation (and instruction builder is too early).
1021 }
1022 // (In terms of correctness, the StringFactory needs to provide its own
1023 // default initialization barrier, see below.)
1024 }
1025
1026 // JLS 17.4.5 "Happens-before Order" describes:
1027 //
1028 // The default initialization of any object happens-before any other actions (other than
1029 // default-writes) of a program.
1030 //
1031 // In our implementation the default initialization of an object to type T means
1032 // setting all of its initial data (object[0..size)) to 0, and setting the
1033 // object's class header (i.e. object.getClass() == T.class).
1034 //
1035 // In practice this fence ensures that the writes to the object header
1036 // are visible to other threads if this object escapes the current thread.
1037 // (and in theory the 0-initializing, but that happens automatically
1038 // when new memory pages are mapped in by the OS).
1039 HConstructorFence* ctor_fence =
1040 new (arena_) HConstructorFence(allocation, allocation->GetDexPc(), arena_);
1041 AppendInstruction(ctor_fence);
David Brazdildee58d62016-04-07 09:54:26 +00001042}
1043
1044static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001045 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001046 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1047}
1048
1049bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001050 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001051 return false;
1052 }
1053
1054 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1055 // check whether the class is in an image for the AOT compilation.
1056 if (cls->IsInitialized() &&
1057 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1058 return true;
1059 }
1060
1061 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1062 return true;
1063 }
1064
1065 // TODO: We should walk over the inlined methods, but we don't pass
1066 // that information to the builder.
1067 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1068 return true;
1069 }
1070
1071 return false;
1072}
1073
1074HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1075 uint32_t dex_pc,
1076 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001077 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001078 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001079
1080 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001081 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001082 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001083 } else {
1084 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1085 klass->GetDexFile(),
1086 klass,
1087 dex_pc,
1088 /* needs_access_check */ false);
1089 if (cls != nullptr) {
1090 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1091 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
1092 AppendInstruction(clinit_check);
1093 }
David Brazdildee58d62016-04-07 09:54:26 +00001094 }
1095 return clinit_check;
1096}
1097
1098bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1099 uint32_t number_of_vreg_arguments,
1100 uint32_t* args,
1101 uint32_t register_index,
1102 bool is_range,
1103 const char* descriptor,
1104 size_t start_index,
1105 size_t* argument_index) {
1106 uint32_t descriptor_index = 1; // Skip the return type.
1107
1108 for (size_t i = start_index;
1109 // Make sure we don't go over the expected arguments or over the number of
1110 // dex registers given. If the instruction was seen as dead by the verifier,
1111 // it hasn't been properly checked.
1112 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1113 i++, (*argument_index)++) {
1114 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1115 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1116 if (!is_range
1117 && is_wide
1118 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1119 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1120 // reject any class where this is violated. However, the verifier only does these checks
1121 // on non trivially dead instructions, so we just bailout the compilation.
1122 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001123 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001124 << " because of non-sequential dex register pair in wide argument";
1125 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1126 return false;
1127 }
1128 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1129 invoke->SetArgumentAt(*argument_index, arg);
1130 if (is_wide) {
1131 i++;
1132 }
1133 }
1134
1135 if (*argument_index != invoke->GetNumberOfArguments()) {
1136 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001137 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001138 << " because of wrong number of arguments in invoke instruction";
1139 MaybeRecordStat(MethodCompilationStat::kNotCompiledMalformedOpcode);
1140 return false;
1141 }
1142
1143 if (invoke->IsInvokeStaticOrDirect() &&
1144 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1145 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1146 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1147 (*argument_index)++;
1148 }
1149
1150 return true;
1151}
1152
1153bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1154 uint32_t number_of_vreg_arguments,
1155 uint32_t* args,
1156 uint32_t register_index,
1157 bool is_range,
1158 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001159 HClinitCheck* clinit_check,
1160 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001161 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1162
1163 size_t start_index = 0;
1164 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001165 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001166 uint32_t obj_reg = is_range ? register_index : args[0];
1167 HInstruction* arg = is_unresolved
1168 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1169 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001170 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001171 start_index = 1;
1172 argument_index = 1;
1173 }
1174
1175 if (!SetupInvokeArguments(invoke,
1176 number_of_vreg_arguments,
1177 args,
1178 register_index,
1179 is_range,
1180 descriptor,
1181 start_index,
1182 &argument_index)) {
1183 return false;
1184 }
1185
1186 if (clinit_check != nullptr) {
1187 // Add the class initialization check as last input of `invoke`.
1188 DCHECK(invoke->IsInvokeStaticOrDirect());
1189 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1190 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1191 invoke->SetArgumentAt(argument_index, clinit_check);
1192 argument_index++;
1193 }
1194
1195 AppendInstruction(invoke);
1196 latest_result_ = invoke;
1197
1198 return true;
1199}
1200
1201bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1202 uint32_t number_of_vreg_arguments,
1203 uint32_t* args,
1204 uint32_t register_index,
1205 bool is_range,
1206 const char* descriptor) {
1207 DCHECK(invoke->IsInvokeStaticOrDirect());
1208 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1209
1210 size_t start_index = 1;
1211 size_t argument_index = 0;
1212 if (!SetupInvokeArguments(invoke,
1213 number_of_vreg_arguments,
1214 args,
1215 register_index,
1216 is_range,
1217 descriptor,
1218 start_index,
1219 &argument_index)) {
1220 return false;
1221 }
1222
1223 AppendInstruction(invoke);
1224
1225 // This is a StringFactory call, not an actual String constructor. Its result
1226 // replaces the empty String pre-allocated by NewInstance.
1227 uint32_t orig_this_reg = is_range ? register_index : args[0];
1228 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1229
1230 // Replacing the NewInstance might render it redundant. Keep a list of these
1231 // to be visited once it is clear whether it is has remaining uses.
1232 if (arg_this->IsNewInstance()) {
1233 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1234 } else {
1235 DCHECK(arg_this->IsPhi());
1236 // NewInstance is not the direct input of the StringFactory call. It might
1237 // be redundant but optimizing this case is not worth the effort.
1238 }
1239
1240 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1241 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1242 if ((*current_locals_)[vreg] == arg_this) {
1243 (*current_locals_)[vreg] = invoke;
1244 }
1245 }
1246
1247 return true;
1248}
1249
1250static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1251 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1252 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1253 return Primitive::GetType(type[0]);
1254}
1255
1256bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1257 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001258 bool is_put,
1259 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001260 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1261 uint32_t obj_reg = instruction.VRegB_22c();
1262 uint16_t field_index;
1263 if (instruction.IsQuickened()) {
1264 if (!CanDecodeQuickenedInfo()) {
1265 return false;
1266 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001267 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001268 } else {
1269 field_index = instruction.VRegC_22c();
1270 }
1271
1272 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001273 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001274
Aart Bik14154132016-06-02 17:53:58 -07001275 // Generate an explicit null check on the reference, unless the field access
1276 // is unresolved. In that case, we rely on the runtime to perform various
1277 // checks first, followed by a null check.
1278 HInstruction* object = (resolved_field == nullptr)
1279 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1280 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001281
1282 Primitive::Type field_type = (resolved_field == nullptr)
1283 ? GetFieldAccessType(*dex_file_, field_index)
1284 : resolved_field->GetTypeAsPrimitiveType();
1285 if (is_put) {
1286 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1287 HInstruction* field_set = nullptr;
1288 if (resolved_field == nullptr) {
1289 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001290 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001291 value,
1292 field_type,
1293 field_index,
1294 dex_pc);
1295 } else {
1296 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001297 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001298 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001299 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001300 field_type,
1301 resolved_field->GetOffset(),
1302 resolved_field->IsVolatile(),
1303 field_index,
1304 class_def_index,
1305 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001306 dex_pc);
1307 }
1308 AppendInstruction(field_set);
1309 } else {
1310 HInstruction* field_get = nullptr;
1311 if (resolved_field == nullptr) {
1312 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001313 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001314 field_type,
1315 field_index,
1316 dex_pc);
1317 } else {
1318 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001319 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001320 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001321 field_type,
1322 resolved_field->GetOffset(),
1323 resolved_field->IsVolatile(),
1324 field_index,
1325 class_def_index,
1326 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001327 dex_pc);
1328 }
1329 AppendInstruction(field_get);
1330 UpdateLocal(source_or_dest_reg, field_get);
1331 }
1332
1333 return true;
1334}
1335
1336static mirror::Class* GetClassFrom(CompilerDriver* driver,
1337 const DexCompilationUnit& compilation_unit) {
1338 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001339 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001340 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001341
1342 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1343}
1344
1345mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1346 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1347}
1348
1349mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1350 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1351}
1352
Andreas Gampea5b09a62016-11-17 15:21:22 -08001353bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001354 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001355 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001356 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001357 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001358 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1359 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1360 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1361
1362 // GetOutermostCompilingClass returns null when the class is unresolved
1363 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1364 // we are compiling it.
1365 // When this happens we cannot establish a direct relation between the current
1366 // class and the outer class, so we return false.
1367 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001368 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001369}
1370
1371void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001372 uint32_t dex_pc,
1373 bool is_put,
1374 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001375 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1376 uint16_t field_index = instruction.VRegB_21c();
1377
1378 if (is_put) {
1379 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1380 AppendInstruction(
1381 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1382 } else {
1383 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1384 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1385 }
1386}
1387
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001388ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1389 ScopedObjectAccess soa(Thread::Current());
1390 StackHandleScope<2> hs(soa.Self());
1391
1392 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001393 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001394 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1395
1396 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1397 field_idx,
1398 dex_compilation_unit_->GetDexCache(),
1399 class_loader,
1400 is_static);
1401
1402 if (UNLIKELY(resolved_field == nullptr)) {
1403 // Clean up any exception left by type resolution.
1404 soa.Self()->ClearException();
1405 return nullptr;
1406 }
1407
1408 // Check static/instance. The class linker has a fast path for looking into the dex cache
1409 // and does not check static/instance if it hits it.
1410 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1411 return nullptr;
1412 }
1413
1414 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001415 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001416 if (!resolved_field->IsPublic()) {
1417 return nullptr;
1418 }
1419 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1420 resolved_field,
1421 dex_compilation_unit_->GetDexCache().Get(),
1422 field_idx)) {
1423 return nullptr;
1424 }
1425
1426 if (is_put &&
1427 resolved_field->IsFinal() &&
1428 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1429 // Final fields can only be updated within their own class.
1430 // TODO: Only allow it in constructors. b/34966607.
1431 return nullptr;
1432 }
1433
1434 return resolved_field;
1435}
1436
David Brazdildee58d62016-04-07 09:54:26 +00001437bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1438 uint32_t dex_pc,
1439 bool is_put) {
1440 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1441 uint16_t field_index = instruction.VRegB_21c();
1442
1443 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001444 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001445
1446 if (resolved_field == nullptr) {
1447 MaybeRecordStat(MethodCompilationStat::kUnresolvedField);
1448 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1449 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1450 return true;
1451 }
1452
1453 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
David Brazdildee58d62016-04-07 09:54:26 +00001454
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001455 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1456 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1457 klass->GetDexFile(),
1458 klass,
1459 dex_pc,
1460 /* needs_access_check */ false);
1461
1462 if (constant == nullptr) {
1463 // The class cannot be referenced from this compiled code. Generate
1464 // an unresolved access.
1465 MaybeRecordStat(MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
1466 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1467 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001468 }
1469
David Brazdildee58d62016-04-07 09:54:26 +00001470 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001471 if (!IsInitialized(klass)) {
1472 cls = new (arena_) HClinitCheck(constant, dex_pc);
1473 AppendInstruction(cls);
1474 }
1475
1476 uint16_t class_def_index = klass->GetDexClassDefIndex();
1477 if (is_put) {
1478 // We need to keep the class alive before loading the value.
1479 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1480 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1481 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1482 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001483 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001484 field_type,
1485 resolved_field->GetOffset(),
1486 resolved_field->IsVolatile(),
1487 field_index,
1488 class_def_index,
1489 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001490 dex_pc));
1491 } else {
1492 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001493 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001494 field_type,
1495 resolved_field->GetOffset(),
1496 resolved_field->IsVolatile(),
1497 field_index,
1498 class_def_index,
1499 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001500 dex_pc));
1501 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1502 }
1503 return true;
1504}
1505
1506void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1507 uint16_t first_vreg,
1508 int64_t second_vreg_or_constant,
1509 uint32_t dex_pc,
1510 Primitive::Type type,
1511 bool second_is_constant,
1512 bool isDiv) {
1513 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1514
1515 HInstruction* first = LoadLocal(first_vreg, type);
1516 HInstruction* second = nullptr;
1517 if (second_is_constant) {
1518 if (type == Primitive::kPrimInt) {
1519 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1520 } else {
1521 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1522 }
1523 } else {
1524 second = LoadLocal(second_vreg_or_constant, type);
1525 }
1526
1527 if (!second_is_constant
1528 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1529 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1530 second = new (arena_) HDivZeroCheck(second, dex_pc);
1531 AppendInstruction(second);
1532 }
1533
1534 if (isDiv) {
1535 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1536 } else {
1537 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1538 }
1539 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1540}
1541
1542void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1543 uint32_t dex_pc,
1544 bool is_put,
1545 Primitive::Type anticipated_type) {
1546 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1547 uint8_t array_reg = instruction.VRegB_23x();
1548 uint8_t index_reg = instruction.VRegC_23x();
1549
David Brazdilc120bbe2016-04-22 16:57:00 +01001550 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001551 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1552 AppendInstruction(length);
1553 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1554 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1555 AppendInstruction(index);
1556 if (is_put) {
1557 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1558 // TODO: Insert a type check node if the type is Object.
1559 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1560 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1561 AppendInstruction(aset);
1562 } else {
1563 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1564 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1565 AppendInstruction(aget);
1566 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1567 }
1568 graph_->SetHasBoundsChecks(true);
1569}
1570
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001571HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1572 dex::TypeIndex type_index,
1573 uint32_t number_of_vreg_arguments,
1574 bool is_range,
1575 uint32_t* args,
1576 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001577 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001578 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001579 HNewArray* const object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001580 AppendInstruction(object);
1581
1582 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1583 DCHECK_EQ(descriptor[0], '[') << descriptor;
1584 char primitive = descriptor[1];
1585 DCHECK(primitive == 'I'
1586 || primitive == 'L'
1587 || primitive == '[') << descriptor;
1588 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1589 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1590
1591 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1592 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1593 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1594 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1595 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1596 AppendInstruction(aset);
1597 }
1598 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001599
1600 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001601}
1602
1603template <typename T>
1604void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1605 const T* data,
1606 uint32_t element_count,
1607 Primitive::Type anticipated_type,
1608 uint32_t dex_pc) {
1609 for (uint32_t i = 0; i < element_count; ++i) {
1610 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1611 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1612 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1613 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1614 AppendInstruction(aset);
1615 }
1616}
1617
1618void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001619 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001620
1621 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1622 const Instruction::ArrayDataPayload* payload =
1623 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1624 const uint8_t* data = payload->data;
1625 uint32_t element_count = payload->element_count;
1626
Vladimir Markoc69fba22016-09-06 16:49:15 +01001627 if (element_count == 0u) {
1628 // For empty payload we emit only the null check above.
1629 return;
1630 }
1631
1632 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1633 AppendInstruction(length);
1634
David Brazdildee58d62016-04-07 09:54:26 +00001635 // Implementation of this DEX instruction seems to be that the bounds check is
1636 // done before doing any stores.
1637 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1638 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1639
1640 switch (payload->element_width) {
1641 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001642 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001643 reinterpret_cast<const int8_t*>(data),
1644 element_count,
1645 Primitive::kPrimByte,
1646 dex_pc);
1647 break;
1648 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001649 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001650 reinterpret_cast<const int16_t*>(data),
1651 element_count,
1652 Primitive::kPrimShort,
1653 dex_pc);
1654 break;
1655 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001656 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001657 reinterpret_cast<const int32_t*>(data),
1658 element_count,
1659 Primitive::kPrimInt,
1660 dex_pc);
1661 break;
1662 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001663 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001664 reinterpret_cast<const int64_t*>(data),
1665 element_count,
1666 dex_pc);
1667 break;
1668 default:
1669 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1670 }
1671 graph_->SetHasBoundsChecks(true);
1672}
1673
1674void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1675 const int64_t* data,
1676 uint32_t element_count,
1677 uint32_t dex_pc) {
1678 for (uint32_t i = 0; i < element_count; ++i) {
1679 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1680 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1681 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1682 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1683 AppendInstruction(aset);
1684 }
1685}
1686
1687static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001688 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001689 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001690 return TypeCheckKind::kUnresolvedCheck;
1691 } else if (cls->IsInterface()) {
1692 return TypeCheckKind::kInterfaceCheck;
1693 } else if (cls->IsArrayClass()) {
1694 if (cls->GetComponentType()->IsObjectClass()) {
1695 return TypeCheckKind::kArrayObjectCheck;
1696 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1697 return TypeCheckKind::kExactCheck;
1698 } else {
1699 return TypeCheckKind::kArrayCheck;
1700 }
1701 } else if (cls->IsFinal()) {
1702 return TypeCheckKind::kExactCheck;
1703 } else if (cls->IsAbstract()) {
1704 return TypeCheckKind::kAbstractClassCheck;
1705 } else {
1706 return TypeCheckKind::kClassHierarchyCheck;
1707 }
1708}
1709
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001710HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001711 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001712 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001713 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001714 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001715 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001716
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001717 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001718 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001719 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001720 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001721 } else {
1722 mirror::Class* compiling_class = GetCompilingClass();
1723 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001724 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001725 }
1726 }
1727 }
1728
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001729 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1730}
1731
1732HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1733 const DexFile& dex_file,
1734 Handle<mirror::Class> klass,
1735 uint32_t dex_pc,
1736 bool needs_access_check) {
1737 // Try to find a reference in the compiling dex file.
1738 const DexFile* actual_dex_file = &dex_file;
1739 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1740 dex::TypeIndex local_type_index =
1741 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1742 if (local_type_index.IsValid()) {
1743 type_index = local_type_index;
1744 actual_dex_file = dex_compilation_unit_->GetDexFile();
1745 }
1746 }
1747
1748 // Note: `klass` must be from `handles_`.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001749 HLoadClass* load_class = new (arena_) HLoadClass(
1750 graph_->GetCurrentMethod(),
1751 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001752 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001753 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001754 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001755 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001756 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001757
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001758 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1759 code_generator_,
1760 compiler_driver_,
1761 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001762
1763 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1764 // We actually cannot reference this class, we're forced to bail.
1765 return nullptr;
1766 }
1767 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001768 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001769 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001770 return load_class;
1771}
1772
David Brazdildee58d62016-04-07 09:54:26 +00001773void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1774 uint8_t destination,
1775 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001776 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001777 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001778 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001779 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001780
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001781 ScopedObjectAccess soa(Thread::Current());
1782 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001783 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1784 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1785 UpdateLocal(destination, current_block_->GetLastInstruction());
1786 } else {
1787 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1788 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1789 // which may throw. If it succeeds BoundType sets the new type of `object`
1790 // for all subsequent uses.
1791 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1792 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1793 UpdateLocal(reference, current_block_->GetLastInstruction());
1794 }
1795}
1796
Vladimir Marko0b66d612017-03-13 14:50:04 +00001797bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001798 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1799 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001800}
1801
1802bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001803 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001804}
1805
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001806uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1807 DCHECK(CanDecodeQuickenedInfo());
1808 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001809}
1810
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001811bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1812 uint32_t dex_pc,
1813 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001814 switch (instruction.Opcode()) {
1815 case Instruction::CONST_4: {
1816 int32_t register_index = instruction.VRegA();
1817 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1818 UpdateLocal(register_index, constant);
1819 break;
1820 }
1821
1822 case Instruction::CONST_16: {
1823 int32_t register_index = instruction.VRegA();
1824 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1825 UpdateLocal(register_index, constant);
1826 break;
1827 }
1828
1829 case Instruction::CONST: {
1830 int32_t register_index = instruction.VRegA();
1831 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1832 UpdateLocal(register_index, constant);
1833 break;
1834 }
1835
1836 case Instruction::CONST_HIGH16: {
1837 int32_t register_index = instruction.VRegA();
1838 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1839 UpdateLocal(register_index, constant);
1840 break;
1841 }
1842
1843 case Instruction::CONST_WIDE_16: {
1844 int32_t register_index = instruction.VRegA();
1845 // Get 16 bits of constant value, sign extended to 64 bits.
1846 int64_t value = instruction.VRegB_21s();
1847 value <<= 48;
1848 value >>= 48;
1849 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1850 UpdateLocal(register_index, constant);
1851 break;
1852 }
1853
1854 case Instruction::CONST_WIDE_32: {
1855 int32_t register_index = instruction.VRegA();
1856 // Get 32 bits of constant value, sign extended to 64 bits.
1857 int64_t value = instruction.VRegB_31i();
1858 value <<= 32;
1859 value >>= 32;
1860 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1861 UpdateLocal(register_index, constant);
1862 break;
1863 }
1864
1865 case Instruction::CONST_WIDE: {
1866 int32_t register_index = instruction.VRegA();
1867 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1868 UpdateLocal(register_index, constant);
1869 break;
1870 }
1871
1872 case Instruction::CONST_WIDE_HIGH16: {
1873 int32_t register_index = instruction.VRegA();
1874 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1875 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1876 UpdateLocal(register_index, constant);
1877 break;
1878 }
1879
1880 // Note that the SSA building will refine the types.
1881 case Instruction::MOVE:
1882 case Instruction::MOVE_FROM16:
1883 case Instruction::MOVE_16: {
1884 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1885 UpdateLocal(instruction.VRegA(), value);
1886 break;
1887 }
1888
1889 // Note that the SSA building will refine the types.
1890 case Instruction::MOVE_WIDE:
1891 case Instruction::MOVE_WIDE_FROM16:
1892 case Instruction::MOVE_WIDE_16: {
1893 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1894 UpdateLocal(instruction.VRegA(), value);
1895 break;
1896 }
1897
1898 case Instruction::MOVE_OBJECT:
1899 case Instruction::MOVE_OBJECT_16:
1900 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001901 // The verifier has no notion of a null type, so a move-object of constant 0
1902 // will lead to the same constant 0 in the destination register. To mimic
1903 // this behavior, we just pretend we haven't seen a type change (int to reference)
1904 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1905 // types correct.
1906 uint32_t reg_number = instruction.VRegB();
1907 HInstruction* value = (*current_locals_)[reg_number];
1908 if (value->IsIntConstant()) {
1909 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1910 } else if (value->IsPhi()) {
1911 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1912 } else {
1913 value = LoadLocal(reg_number, Primitive::kPrimNot);
1914 }
David Brazdildee58d62016-04-07 09:54:26 +00001915 UpdateLocal(instruction.VRegA(), value);
1916 break;
1917 }
1918
1919 case Instruction::RETURN_VOID_NO_BARRIER:
1920 case Instruction::RETURN_VOID: {
1921 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1922 break;
1923 }
1924
1925#define IF_XX(comparison, cond) \
1926 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1927 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1928
1929 IF_XX(HEqual, EQ);
1930 IF_XX(HNotEqual, NE);
1931 IF_XX(HLessThan, LT);
1932 IF_XX(HLessThanOrEqual, LE);
1933 IF_XX(HGreaterThan, GT);
1934 IF_XX(HGreaterThanOrEqual, GE);
1935
1936 case Instruction::GOTO:
1937 case Instruction::GOTO_16:
1938 case Instruction::GOTO_32: {
1939 AppendInstruction(new (arena_) HGoto(dex_pc));
1940 current_block_ = nullptr;
1941 break;
1942 }
1943
1944 case Instruction::RETURN: {
1945 BuildReturn(instruction, return_type_, dex_pc);
1946 break;
1947 }
1948
1949 case Instruction::RETURN_OBJECT: {
1950 BuildReturn(instruction, return_type_, dex_pc);
1951 break;
1952 }
1953
1954 case Instruction::RETURN_WIDE: {
1955 BuildReturn(instruction, return_type_, dex_pc);
1956 break;
1957 }
1958
1959 case Instruction::INVOKE_DIRECT:
1960 case Instruction::INVOKE_INTERFACE:
1961 case Instruction::INVOKE_STATIC:
1962 case Instruction::INVOKE_SUPER:
1963 case Instruction::INVOKE_VIRTUAL:
1964 case Instruction::INVOKE_VIRTUAL_QUICK: {
1965 uint16_t method_idx;
1966 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1967 if (!CanDecodeQuickenedInfo()) {
1968 return false;
1969 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001970 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001971 } else {
1972 method_idx = instruction.VRegB_35c();
1973 }
1974 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1975 uint32_t args[5];
1976 instruction.GetVarArgs(args);
1977 if (!BuildInvoke(instruction, dex_pc, method_idx,
1978 number_of_vreg_arguments, false, args, -1)) {
1979 return false;
1980 }
1981 break;
1982 }
1983
1984 case Instruction::INVOKE_DIRECT_RANGE:
1985 case Instruction::INVOKE_INTERFACE_RANGE:
1986 case Instruction::INVOKE_STATIC_RANGE:
1987 case Instruction::INVOKE_SUPER_RANGE:
1988 case Instruction::INVOKE_VIRTUAL_RANGE:
1989 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1990 uint16_t method_idx;
1991 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
1992 if (!CanDecodeQuickenedInfo()) {
1993 return false;
1994 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001995 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001996 } else {
1997 method_idx = instruction.VRegB_3rc();
1998 }
1999 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2000 uint32_t register_index = instruction.VRegC();
2001 if (!BuildInvoke(instruction, dex_pc, method_idx,
2002 number_of_vreg_arguments, true, nullptr, register_index)) {
2003 return false;
2004 }
2005 break;
2006 }
2007
Orion Hodsonac141392017-01-13 11:53:47 +00002008 case Instruction::INVOKE_POLYMORPHIC: {
2009 uint16_t method_idx = instruction.VRegB_45cc();
2010 uint16_t proto_idx = instruction.VRegH_45cc();
2011 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2012 uint32_t args[5];
2013 instruction.GetVarArgs(args);
2014 return BuildInvokePolymorphic(instruction,
2015 dex_pc,
2016 method_idx,
2017 proto_idx,
2018 number_of_vreg_arguments,
2019 false,
2020 args,
2021 -1);
2022 }
2023
2024 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2025 uint16_t method_idx = instruction.VRegB_4rcc();
2026 uint16_t proto_idx = instruction.VRegH_4rcc();
2027 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2028 uint32_t register_index = instruction.VRegC_4rcc();
2029 return BuildInvokePolymorphic(instruction,
2030 dex_pc,
2031 method_idx,
2032 proto_idx,
2033 number_of_vreg_arguments,
2034 true,
2035 nullptr,
2036 register_index);
2037 }
2038
David Brazdildee58d62016-04-07 09:54:26 +00002039 case Instruction::NEG_INT: {
2040 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
2041 break;
2042 }
2043
2044 case Instruction::NEG_LONG: {
2045 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
2046 break;
2047 }
2048
2049 case Instruction::NEG_FLOAT: {
2050 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
2051 break;
2052 }
2053
2054 case Instruction::NEG_DOUBLE: {
2055 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
2056 break;
2057 }
2058
2059 case Instruction::NOT_INT: {
2060 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
2061 break;
2062 }
2063
2064 case Instruction::NOT_LONG: {
2065 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
2066 break;
2067 }
2068
2069 case Instruction::INT_TO_LONG: {
2070 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2071 break;
2072 }
2073
2074 case Instruction::INT_TO_FLOAT: {
2075 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2076 break;
2077 }
2078
2079 case Instruction::INT_TO_DOUBLE: {
2080 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2081 break;
2082 }
2083
2084 case Instruction::LONG_TO_INT: {
2085 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2086 break;
2087 }
2088
2089 case Instruction::LONG_TO_FLOAT: {
2090 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2091 break;
2092 }
2093
2094 case Instruction::LONG_TO_DOUBLE: {
2095 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2096 break;
2097 }
2098
2099 case Instruction::FLOAT_TO_INT: {
2100 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2101 break;
2102 }
2103
2104 case Instruction::FLOAT_TO_LONG: {
2105 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2106 break;
2107 }
2108
2109 case Instruction::FLOAT_TO_DOUBLE: {
2110 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2111 break;
2112 }
2113
2114 case Instruction::DOUBLE_TO_INT: {
2115 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2116 break;
2117 }
2118
2119 case Instruction::DOUBLE_TO_LONG: {
2120 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2121 break;
2122 }
2123
2124 case Instruction::DOUBLE_TO_FLOAT: {
2125 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2126 break;
2127 }
2128
2129 case Instruction::INT_TO_BYTE: {
2130 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2131 break;
2132 }
2133
2134 case Instruction::INT_TO_SHORT: {
2135 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2136 break;
2137 }
2138
2139 case Instruction::INT_TO_CHAR: {
2140 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2141 break;
2142 }
2143
2144 case Instruction::ADD_INT: {
2145 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2146 break;
2147 }
2148
2149 case Instruction::ADD_LONG: {
2150 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2151 break;
2152 }
2153
2154 case Instruction::ADD_DOUBLE: {
2155 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2156 break;
2157 }
2158
2159 case Instruction::ADD_FLOAT: {
2160 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2161 break;
2162 }
2163
2164 case Instruction::SUB_INT: {
2165 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2166 break;
2167 }
2168
2169 case Instruction::SUB_LONG: {
2170 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2171 break;
2172 }
2173
2174 case Instruction::SUB_FLOAT: {
2175 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2176 break;
2177 }
2178
2179 case Instruction::SUB_DOUBLE: {
2180 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2181 break;
2182 }
2183
2184 case Instruction::ADD_INT_2ADDR: {
2185 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2186 break;
2187 }
2188
2189 case Instruction::MUL_INT: {
2190 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2191 break;
2192 }
2193
2194 case Instruction::MUL_LONG: {
2195 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2196 break;
2197 }
2198
2199 case Instruction::MUL_FLOAT: {
2200 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2201 break;
2202 }
2203
2204 case Instruction::MUL_DOUBLE: {
2205 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2206 break;
2207 }
2208
2209 case Instruction::DIV_INT: {
2210 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2211 dex_pc, Primitive::kPrimInt, false, true);
2212 break;
2213 }
2214
2215 case Instruction::DIV_LONG: {
2216 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2217 dex_pc, Primitive::kPrimLong, false, true);
2218 break;
2219 }
2220
2221 case Instruction::DIV_FLOAT: {
2222 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2223 break;
2224 }
2225
2226 case Instruction::DIV_DOUBLE: {
2227 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2228 break;
2229 }
2230
2231 case Instruction::REM_INT: {
2232 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2233 dex_pc, Primitive::kPrimInt, false, false);
2234 break;
2235 }
2236
2237 case Instruction::REM_LONG: {
2238 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2239 dex_pc, Primitive::kPrimLong, false, false);
2240 break;
2241 }
2242
2243 case Instruction::REM_FLOAT: {
2244 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2245 break;
2246 }
2247
2248 case Instruction::REM_DOUBLE: {
2249 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2250 break;
2251 }
2252
2253 case Instruction::AND_INT: {
2254 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2255 break;
2256 }
2257
2258 case Instruction::AND_LONG: {
2259 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2260 break;
2261 }
2262
2263 case Instruction::SHL_INT: {
2264 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2265 break;
2266 }
2267
2268 case Instruction::SHL_LONG: {
2269 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2270 break;
2271 }
2272
2273 case Instruction::SHR_INT: {
2274 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2275 break;
2276 }
2277
2278 case Instruction::SHR_LONG: {
2279 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2280 break;
2281 }
2282
2283 case Instruction::USHR_INT: {
2284 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2285 break;
2286 }
2287
2288 case Instruction::USHR_LONG: {
2289 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2290 break;
2291 }
2292
2293 case Instruction::OR_INT: {
2294 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2295 break;
2296 }
2297
2298 case Instruction::OR_LONG: {
2299 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2300 break;
2301 }
2302
2303 case Instruction::XOR_INT: {
2304 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2305 break;
2306 }
2307
2308 case Instruction::XOR_LONG: {
2309 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2310 break;
2311 }
2312
2313 case Instruction::ADD_LONG_2ADDR: {
2314 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2315 break;
2316 }
2317
2318 case Instruction::ADD_DOUBLE_2ADDR: {
2319 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2320 break;
2321 }
2322
2323 case Instruction::ADD_FLOAT_2ADDR: {
2324 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2325 break;
2326 }
2327
2328 case Instruction::SUB_INT_2ADDR: {
2329 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2330 break;
2331 }
2332
2333 case Instruction::SUB_LONG_2ADDR: {
2334 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2335 break;
2336 }
2337
2338 case Instruction::SUB_FLOAT_2ADDR: {
2339 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2340 break;
2341 }
2342
2343 case Instruction::SUB_DOUBLE_2ADDR: {
2344 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2345 break;
2346 }
2347
2348 case Instruction::MUL_INT_2ADDR: {
2349 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2350 break;
2351 }
2352
2353 case Instruction::MUL_LONG_2ADDR: {
2354 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2355 break;
2356 }
2357
2358 case Instruction::MUL_FLOAT_2ADDR: {
2359 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2360 break;
2361 }
2362
2363 case Instruction::MUL_DOUBLE_2ADDR: {
2364 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2365 break;
2366 }
2367
2368 case Instruction::DIV_INT_2ADDR: {
2369 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2370 dex_pc, Primitive::kPrimInt, false, true);
2371 break;
2372 }
2373
2374 case Instruction::DIV_LONG_2ADDR: {
2375 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2376 dex_pc, Primitive::kPrimLong, false, true);
2377 break;
2378 }
2379
2380 case Instruction::REM_INT_2ADDR: {
2381 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2382 dex_pc, Primitive::kPrimInt, false, false);
2383 break;
2384 }
2385
2386 case Instruction::REM_LONG_2ADDR: {
2387 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2388 dex_pc, Primitive::kPrimLong, false, false);
2389 break;
2390 }
2391
2392 case Instruction::REM_FLOAT_2ADDR: {
2393 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2394 break;
2395 }
2396
2397 case Instruction::REM_DOUBLE_2ADDR: {
2398 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2399 break;
2400 }
2401
2402 case Instruction::SHL_INT_2ADDR: {
2403 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2404 break;
2405 }
2406
2407 case Instruction::SHL_LONG_2ADDR: {
2408 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2409 break;
2410 }
2411
2412 case Instruction::SHR_INT_2ADDR: {
2413 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2414 break;
2415 }
2416
2417 case Instruction::SHR_LONG_2ADDR: {
2418 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2419 break;
2420 }
2421
2422 case Instruction::USHR_INT_2ADDR: {
2423 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2424 break;
2425 }
2426
2427 case Instruction::USHR_LONG_2ADDR: {
2428 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2429 break;
2430 }
2431
2432 case Instruction::DIV_FLOAT_2ADDR: {
2433 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2434 break;
2435 }
2436
2437 case Instruction::DIV_DOUBLE_2ADDR: {
2438 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2439 break;
2440 }
2441
2442 case Instruction::AND_INT_2ADDR: {
2443 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2444 break;
2445 }
2446
2447 case Instruction::AND_LONG_2ADDR: {
2448 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2449 break;
2450 }
2451
2452 case Instruction::OR_INT_2ADDR: {
2453 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2454 break;
2455 }
2456
2457 case Instruction::OR_LONG_2ADDR: {
2458 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2459 break;
2460 }
2461
2462 case Instruction::XOR_INT_2ADDR: {
2463 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2464 break;
2465 }
2466
2467 case Instruction::XOR_LONG_2ADDR: {
2468 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2469 break;
2470 }
2471
2472 case Instruction::ADD_INT_LIT16: {
2473 Binop_22s<HAdd>(instruction, false, dex_pc);
2474 break;
2475 }
2476
2477 case Instruction::AND_INT_LIT16: {
2478 Binop_22s<HAnd>(instruction, false, dex_pc);
2479 break;
2480 }
2481
2482 case Instruction::OR_INT_LIT16: {
2483 Binop_22s<HOr>(instruction, false, dex_pc);
2484 break;
2485 }
2486
2487 case Instruction::XOR_INT_LIT16: {
2488 Binop_22s<HXor>(instruction, false, dex_pc);
2489 break;
2490 }
2491
2492 case Instruction::RSUB_INT: {
2493 Binop_22s<HSub>(instruction, true, dex_pc);
2494 break;
2495 }
2496
2497 case Instruction::MUL_INT_LIT16: {
2498 Binop_22s<HMul>(instruction, false, dex_pc);
2499 break;
2500 }
2501
2502 case Instruction::ADD_INT_LIT8: {
2503 Binop_22b<HAdd>(instruction, false, dex_pc);
2504 break;
2505 }
2506
2507 case Instruction::AND_INT_LIT8: {
2508 Binop_22b<HAnd>(instruction, false, dex_pc);
2509 break;
2510 }
2511
2512 case Instruction::OR_INT_LIT8: {
2513 Binop_22b<HOr>(instruction, false, dex_pc);
2514 break;
2515 }
2516
2517 case Instruction::XOR_INT_LIT8: {
2518 Binop_22b<HXor>(instruction, false, dex_pc);
2519 break;
2520 }
2521
2522 case Instruction::RSUB_INT_LIT8: {
2523 Binop_22b<HSub>(instruction, true, dex_pc);
2524 break;
2525 }
2526
2527 case Instruction::MUL_INT_LIT8: {
2528 Binop_22b<HMul>(instruction, false, dex_pc);
2529 break;
2530 }
2531
2532 case Instruction::DIV_INT_LIT16:
2533 case Instruction::DIV_INT_LIT8: {
2534 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2535 dex_pc, Primitive::kPrimInt, true, true);
2536 break;
2537 }
2538
2539 case Instruction::REM_INT_LIT16:
2540 case Instruction::REM_INT_LIT8: {
2541 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2542 dex_pc, Primitive::kPrimInt, true, false);
2543 break;
2544 }
2545
2546 case Instruction::SHL_INT_LIT8: {
2547 Binop_22b<HShl>(instruction, false, dex_pc);
2548 break;
2549 }
2550
2551 case Instruction::SHR_INT_LIT8: {
2552 Binop_22b<HShr>(instruction, false, dex_pc);
2553 break;
2554 }
2555
2556 case Instruction::USHR_INT_LIT8: {
2557 Binop_22b<HUShr>(instruction, false, dex_pc);
2558 break;
2559 }
2560
2561 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002562 HNewInstance* new_instance =
2563 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2564 DCHECK(new_instance != nullptr);
2565
David Brazdildee58d62016-04-07 09:54:26 +00002566 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002567 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002568 break;
2569 }
2570
2571 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002572 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002573 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002574 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002575
2576 HNewArray* new_array = new (arena_) HNewArray(cls, length, dex_pc);
2577 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002578 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002579 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002580 break;
2581 }
2582
2583 case Instruction::FILLED_NEW_ARRAY: {
2584 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002585 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002586 uint32_t args[5];
2587 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002588 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2589 type_index,
2590 number_of_vreg_arguments,
2591 /* is_range */ false,
2592 args,
2593 /* register_index */ 0);
2594 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002595 break;
2596 }
2597
2598 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2599 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002600 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002601 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002602 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2603 type_index,
2604 number_of_vreg_arguments,
2605 /* is_range */ true,
2606 /* args*/ nullptr,
2607 register_index);
2608 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002609 break;
2610 }
2611
2612 case Instruction::FILL_ARRAY_DATA: {
2613 BuildFillArrayData(instruction, dex_pc);
2614 break;
2615 }
2616
2617 case Instruction::MOVE_RESULT:
2618 case Instruction::MOVE_RESULT_WIDE:
2619 case Instruction::MOVE_RESULT_OBJECT: {
2620 DCHECK(latest_result_ != nullptr);
2621 UpdateLocal(instruction.VRegA(), latest_result_);
2622 latest_result_ = nullptr;
2623 break;
2624 }
2625
2626 case Instruction::CMP_LONG: {
2627 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2628 break;
2629 }
2630
2631 case Instruction::CMPG_FLOAT: {
2632 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2633 break;
2634 }
2635
2636 case Instruction::CMPG_DOUBLE: {
2637 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2638 break;
2639 }
2640
2641 case Instruction::CMPL_FLOAT: {
2642 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2643 break;
2644 }
2645
2646 case Instruction::CMPL_DOUBLE: {
2647 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2648 break;
2649 }
2650
2651 case Instruction::NOP:
2652 break;
2653
2654 case Instruction::IGET:
2655 case Instruction::IGET_QUICK:
2656 case Instruction::IGET_WIDE:
2657 case Instruction::IGET_WIDE_QUICK:
2658 case Instruction::IGET_OBJECT:
2659 case Instruction::IGET_OBJECT_QUICK:
2660 case Instruction::IGET_BOOLEAN:
2661 case Instruction::IGET_BOOLEAN_QUICK:
2662 case Instruction::IGET_BYTE:
2663 case Instruction::IGET_BYTE_QUICK:
2664 case Instruction::IGET_CHAR:
2665 case Instruction::IGET_CHAR_QUICK:
2666 case Instruction::IGET_SHORT:
2667 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002668 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002669 return false;
2670 }
2671 break;
2672 }
2673
2674 case Instruction::IPUT:
2675 case Instruction::IPUT_QUICK:
2676 case Instruction::IPUT_WIDE:
2677 case Instruction::IPUT_WIDE_QUICK:
2678 case Instruction::IPUT_OBJECT:
2679 case Instruction::IPUT_OBJECT_QUICK:
2680 case Instruction::IPUT_BOOLEAN:
2681 case Instruction::IPUT_BOOLEAN_QUICK:
2682 case Instruction::IPUT_BYTE:
2683 case Instruction::IPUT_BYTE_QUICK:
2684 case Instruction::IPUT_CHAR:
2685 case Instruction::IPUT_CHAR_QUICK:
2686 case Instruction::IPUT_SHORT:
2687 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002688 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002689 return false;
2690 }
2691 break;
2692 }
2693
2694 case Instruction::SGET:
2695 case Instruction::SGET_WIDE:
2696 case Instruction::SGET_OBJECT:
2697 case Instruction::SGET_BOOLEAN:
2698 case Instruction::SGET_BYTE:
2699 case Instruction::SGET_CHAR:
2700 case Instruction::SGET_SHORT: {
2701 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2702 return false;
2703 }
2704 break;
2705 }
2706
2707 case Instruction::SPUT:
2708 case Instruction::SPUT_WIDE:
2709 case Instruction::SPUT_OBJECT:
2710 case Instruction::SPUT_BOOLEAN:
2711 case Instruction::SPUT_BYTE:
2712 case Instruction::SPUT_CHAR:
2713 case Instruction::SPUT_SHORT: {
2714 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2715 return false;
2716 }
2717 break;
2718 }
2719
2720#define ARRAY_XX(kind, anticipated_type) \
2721 case Instruction::AGET##kind: { \
2722 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2723 break; \
2724 } \
2725 case Instruction::APUT##kind: { \
2726 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2727 break; \
2728 }
2729
2730 ARRAY_XX(, Primitive::kPrimInt);
2731 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2732 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2733 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2734 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2735 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2736 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2737
2738 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002739 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002740 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2741 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2742 break;
2743 }
2744
2745 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002746 dex::StringIndex string_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002747 AppendInstruction(
2748 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2749 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2750 break;
2751 }
2752
2753 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002754 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002755 AppendInstruction(
2756 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2757 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2758 break;
2759 }
2760
2761 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002762 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002763 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002764 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2765 break;
2766 }
2767
2768 case Instruction::MOVE_EXCEPTION: {
2769 AppendInstruction(new (arena_) HLoadException(dex_pc));
2770 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2771 AppendInstruction(new (arena_) HClearException(dex_pc));
2772 break;
2773 }
2774
2775 case Instruction::THROW: {
2776 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2777 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2778 // We finished building this block. Set the current block to null to avoid
2779 // adding dead instructions to it.
2780 current_block_ = nullptr;
2781 break;
2782 }
2783
2784 case Instruction::INSTANCE_OF: {
2785 uint8_t destination = instruction.VRegA_22c();
2786 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002787 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002788 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2789 break;
2790 }
2791
2792 case Instruction::CHECK_CAST: {
2793 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002794 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002795 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2796 break;
2797 }
2798
2799 case Instruction::MONITOR_ENTER: {
2800 AppendInstruction(new (arena_) HMonitorOperation(
2801 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2802 HMonitorOperation::OperationKind::kEnter,
2803 dex_pc));
2804 break;
2805 }
2806
2807 case Instruction::MONITOR_EXIT: {
2808 AppendInstruction(new (arena_) HMonitorOperation(
2809 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2810 HMonitorOperation::OperationKind::kExit,
2811 dex_pc));
2812 break;
2813 }
2814
2815 case Instruction::SPARSE_SWITCH:
2816 case Instruction::PACKED_SWITCH: {
2817 BuildSwitch(instruction, dex_pc);
2818 break;
2819 }
2820
2821 default:
2822 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002823 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002824 << " because of unhandled instruction "
2825 << instruction.Name();
2826 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction);
2827 return false;
2828 }
2829 return true;
2830} // NOLINT(readability/fn_size)
2831
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002832ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2833 dex::TypeIndex type_index,
2834 const DexCompilationUnit& compilation_unit) const {
2835 return ClassLinker::LookupResolvedType(
2836 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2837}
2838
2839ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2840 // TODO: Cache the result in a Handle<mirror::Class>.
2841 const DexFile::MethodId& method_id =
2842 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2843 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2844}
2845
David Brazdildee58d62016-04-07 09:54:26 +00002846} // namespace art