blob: 6532ec123dc8c8cd48fb2c64185d9f66f1f3f79a [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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "sharpening.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070028#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000029
30namespace art {
31
David Brazdildee58d62016-04-07 09:54:26 +000032HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
33 return block_builder_->GetBlockAt(dex_pc);
34}
35
Mingyao Yang01b47b02017-02-03 12:09:57 -080036inline ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
David Brazdildee58d62016-04-07 09:54:26 +000037 ArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
38 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080039 if (locals->size() == vregs) {
40 return locals;
41 }
42 return GetLocalsForWithAllocation(block, locals, vregs);
43}
David Brazdildee58d62016-04-07 09:54:26 +000044
Mingyao Yang01b47b02017-02-03 12:09:57 -080045ArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
46 HBasicBlock* block,
47 ArenaVector<HInstruction*>* locals,
48 const size_t vregs) {
49 DCHECK_NE(locals->size(), vregs);
50 locals->resize(vregs, nullptr);
51 if (block->IsCatchBlock()) {
52 // We record incoming inputs of catch phis at throwing instructions and
53 // must therefore eagerly create the phis. Phis for undefined vregs will
54 // be deleted when the first throwing instruction with the vreg undefined
55 // is encountered. Unused phis will be removed by dead phi analysis.
56 for (size_t i = 0; i < vregs; ++i) {
57 // No point in creating the catch phi if it is already undefined at
58 // the first throwing instruction.
59 HInstruction* current_local_value = (*current_locals_)[i];
60 if (current_local_value != nullptr) {
61 HPhi* phi = new (arena_) HPhi(
62 arena_,
63 i,
64 0,
65 current_local_value->GetType());
66 block->AddPhi(phi);
67 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +000068 }
69 }
70 }
71 return locals;
72}
73
Mingyao Yang01b47b02017-02-03 12:09:57 -080074inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
David Brazdildee58d62016-04-07 09:54:26 +000075 ArenaVector<HInstruction*>* locals = GetLocalsFor(block);
76 return (*locals)[local];
77}
78
79void HInstructionBuilder::InitializeBlockLocals() {
80 current_locals_ = GetLocalsFor(current_block_);
81
82 if (current_block_->IsCatchBlock()) {
83 // Catch phis were already created and inputs collected from throwing sites.
84 if (kIsDebugBuild) {
85 // Make sure there was at least one throwing instruction which initialized
86 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
87 // visited already (from HTryBoundary scoping and reverse post order).
88 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +010089 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +000090 if (current == current_block_) {
91 catch_block_visited = true;
92 } else if (current->IsTryBlock()) {
93 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
94 if (try_entry.HasExceptionHandler(*current_block_)) {
95 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
96 }
97 }
98 }
99 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
100 << "No instructions throwing into a live catch block.";
101 }
102 } else if (current_block_->IsLoopHeader()) {
103 // If the block is a loop header, we know we only have visited the pre header
104 // because we are visiting in reverse post order. We create phis for all initialized
105 // locals from the pre header. Their inputs will be populated at the end of
106 // the analysis.
107 for (size_t local = 0; local < current_locals_->size(); ++local) {
108 HInstruction* incoming =
109 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
110 if (incoming != nullptr) {
111 HPhi* phi = new (arena_) HPhi(
112 arena_,
113 local,
114 0,
115 incoming->GetType());
116 current_block_->AddPhi(phi);
117 (*current_locals_)[local] = phi;
118 }
119 }
120
121 // Save the loop header so that the last phase of the analysis knows which
122 // blocks need to be updated.
123 loop_headers_.push_back(current_block_);
124 } else if (current_block_->GetPredecessors().size() > 0) {
125 // All predecessors have already been visited because we are visiting in reverse post order.
126 // We merge the values of all locals, creating phis if those values differ.
127 for (size_t local = 0; local < current_locals_->size(); ++local) {
128 bool one_predecessor_has_no_value = false;
129 bool is_different = false;
130 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
131
132 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
133 HInstruction* current = ValueOfLocalAt(predecessor, local);
134 if (current == nullptr) {
135 one_predecessor_has_no_value = true;
136 break;
137 } else if (current != value) {
138 is_different = true;
139 }
140 }
141
142 if (one_predecessor_has_no_value) {
143 // If one predecessor has no value for this local, we trust the verifier has
144 // successfully checked that there is a store dominating any read after this block.
145 continue;
146 }
147
148 if (is_different) {
149 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
150 HPhi* phi = new (arena_) HPhi(
151 arena_,
152 local,
153 current_block_->GetPredecessors().size(),
154 first_input->GetType());
155 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
156 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
157 phi->SetRawInputAt(i, pred_value);
158 }
159 current_block_->AddPhi(phi);
160 value = phi;
161 }
162 (*current_locals_)[local] = value;
163 }
164 }
165}
166
167void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
168 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
169 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
170 ArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
171 DCHECK_EQ(handler_locals->size(), current_locals_->size());
172 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
173 HInstruction* handler_value = (*handler_locals)[vreg];
174 if (handler_value == nullptr) {
175 // Vreg was undefined at a previously encountered throwing instruction
176 // and the catch phi was deleted. Do not record the local value.
177 continue;
178 }
179 DCHECK(handler_value->IsPhi());
180
181 HInstruction* local_value = (*current_locals_)[vreg];
182 if (local_value == nullptr) {
183 // This is the first instruction throwing into `catch_block` where
184 // `vreg` is undefined. Delete the catch phi.
185 catch_block->RemovePhi(handler_value->AsPhi());
186 (*handler_locals)[vreg] = nullptr;
187 } else {
188 // Vreg has been defined at all instructions throwing into `catch_block`
189 // encountered so far. Record the local value in the catch phi.
190 handler_value->AsPhi()->AddInput(local_value);
191 }
192 }
193 }
194}
195
196void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
197 current_block_->AddInstruction(instruction);
198 InitializeInstruction(instruction);
199}
200
201void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
202 if (current_block_->GetInstructions().IsEmpty()) {
203 current_block_->AddInstruction(instruction);
204 } else {
205 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
206 }
207 InitializeInstruction(instruction);
208}
209
210void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
211 if (instruction->NeedsEnvironment()) {
212 HEnvironment* environment = new (arena_) HEnvironment(
213 arena_,
214 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000215 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000216 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000217 instruction);
218 environment->CopyFrom(*current_locals_);
219 instruction->SetRawEnvironment(environment);
220 }
221}
222
David Brazdilc120bbe2016-04-22 16:57:00 +0100223HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
224 HInstruction* ref = LoadLocal(register_index, Primitive::kPrimNot);
225 if (!ref->CanBeNull()) {
226 return ref;
227 }
228
229 HNullCheck* null_check = new (arena_) HNullCheck(ref, dex_pc);
230 AppendInstruction(null_check);
231 return null_check;
232}
233
David Brazdildee58d62016-04-07 09:54:26 +0000234void HInstructionBuilder::SetLoopHeaderPhiInputs() {
235 for (size_t i = loop_headers_.size(); i > 0; --i) {
236 HBasicBlock* block = loop_headers_[i - 1];
237 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
238 HPhi* phi = it.Current()->AsPhi();
239 size_t vreg = phi->GetRegNumber();
240 for (HBasicBlock* predecessor : block->GetPredecessors()) {
241 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
242 if (value == nullptr) {
243 // Vreg is undefined at this predecessor. Mark it dead and leave with
244 // fewer inputs than predecessors. SsaChecker will fail if not removed.
245 phi->SetDead();
246 break;
247 } else {
248 phi->AddInput(value);
249 }
250 }
251 }
252 }
253}
254
255static bool IsBlockPopulated(HBasicBlock* block) {
256 if (block->IsLoopHeader()) {
257 // Suspend checks were inserted into loop headers during building of dominator tree.
258 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
259 return block->GetFirstInstruction() != block->GetLastInstruction();
260 } else {
261 return !block->GetInstructions().IsEmpty();
262 }
263}
264
265bool HInstructionBuilder::Build() {
266 locals_for_.resize(graph_->GetBlocks().size(),
267 ArenaVector<HInstruction*>(arena_->Adapter(kArenaAllocGraphBuilder)));
268
269 // Find locations where we want to generate extra stackmaps for native debugging.
270 // This allows us to generate the info only at interesting points (for example,
271 // at start of java statement) rather than before every dex instruction.
272 const bool native_debuggable = compiler_driver_ != nullptr &&
273 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
274 ArenaBitVector* native_debug_info_locations = nullptr;
275 if (native_debuggable) {
276 const uint32_t num_instructions = code_item_.insns_size_in_code_units_;
277 native_debug_info_locations = new (arena_) ArenaBitVector (arena_, num_instructions, false);
278 FindNativeDebugInfoLocations(native_debug_info_locations);
279 }
280
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100281 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
282 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000283 uint32_t block_dex_pc = current_block_->GetDexPc();
284
285 InitializeBlockLocals();
286
287 if (current_block_->IsEntryBlock()) {
288 InitializeParameters();
289 AppendInstruction(new (arena_) HSuspendCheck(0u));
290 AppendInstruction(new (arena_) HGoto(0u));
291 continue;
292 } else if (current_block_->IsExitBlock()) {
293 AppendInstruction(new (arena_) HExit());
294 continue;
295 } else if (current_block_->IsLoopHeader()) {
296 HSuspendCheck* suspend_check = new (arena_) HSuspendCheck(current_block_->GetDexPc());
297 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
298 // This is slightly odd because the loop header might not be empty (TryBoundary).
299 // But we're still creating the environment with locals from the top of the block.
300 InsertInstructionAtTop(suspend_check);
301 }
302
303 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
304 // Synthetic block that does not need to be populated.
305 DCHECK(IsBlockPopulated(current_block_));
306 continue;
307 }
308
309 DCHECK(!IsBlockPopulated(current_block_));
310
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700311 uint32_t quicken_index = 0;
312 if (CanDecodeQuickenedInfo()) {
313 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
314 }
315
David Brazdildee58d62016-04-07 09:54:26 +0000316 for (CodeItemIterator it(code_item_, block_dex_pc); !it.Done(); it.Advance()) {
317 if (current_block_ == nullptr) {
318 // The previous instruction ended this block.
319 break;
320 }
321
322 uint32_t dex_pc = it.CurrentDexPc();
323 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
324 // This dex_pc starts a new basic block.
325 break;
326 }
327
328 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(it.CurrentInstruction())) {
329 PropagateLocalsToCatchBlocks();
330 }
331
332 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
333 AppendInstruction(new (arena_) HNativeDebugInfo(dex_pc));
334 }
335
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700336 if (!ProcessDexInstruction(it.CurrentInstruction(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000337 return false;
338 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700339
340 if (QuickenInfoTable::NeedsIndexForInstruction(&it.CurrentInstruction())) {
341 ++quicken_index;
342 }
David Brazdildee58d62016-04-07 09:54:26 +0000343 }
344
345 if (current_block_ != nullptr) {
346 // Branching instructions clear current_block, so we know the last
347 // instruction of the current block is not a branching instruction.
348 // We add an unconditional Goto to the next block.
349 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
350 AppendInstruction(new (arena_) HGoto());
351 }
352 }
353
354 SetLoopHeaderPhiInputs();
355
356 return true;
357}
358
359void HInstructionBuilder::FindNativeDebugInfoLocations(ArenaBitVector* locations) {
360 // The callback gets called when the line number changes.
361 // In other words, it marks the start of new java statement.
362 struct Callback {
363 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
364 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
365 return false;
366 }
367 };
368 dex_file_->DecodeDebugPositionInfo(&code_item_, Callback::Position, locations);
369 // Instruction-specific tweaks.
370 const Instruction* const begin = Instruction::At(code_item_.insns_);
371 const Instruction* const end = begin->RelativeAt(code_item_.insns_size_in_code_units_);
372 for (const Instruction* inst = begin; inst < end; inst = inst->Next()) {
373 switch (inst->Opcode()) {
374 case Instruction::MOVE_EXCEPTION: {
375 // Stop in native debugger after the exception has been moved.
376 // The compiler also expects the move at the start of basic block so
377 // we do not want to interfere by inserting native-debug-info before it.
378 locations->ClearBit(inst->GetDexPc(code_item_.insns_));
379 const Instruction* next = inst->Next();
380 if (next < end) {
381 locations->SetBit(next->GetDexPc(code_item_.insns_));
382 }
383 break;
384 }
385 default:
386 break;
387 }
388 }
389}
390
391HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, Primitive::Type type) const {
392 HInstruction* value = (*current_locals_)[reg_number];
393 DCHECK(value != nullptr);
394
395 // If the operation requests a specific type, we make sure its input is of that type.
396 if (type != value->GetType()) {
397 if (Primitive::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700398 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
David Brazdildee58d62016-04-07 09:54:26 +0000399 } else if (type == Primitive::kPrimNot) {
Aart Bik31883642016-06-06 15:02:44 -0700400 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000401 }
Aart Bik31883642016-06-06 15:02:44 -0700402 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000403 }
404
405 return value;
406}
407
408void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
409 Primitive::Type stored_type = stored_value->GetType();
410 DCHECK_NE(stored_type, Primitive::kPrimVoid);
411
412 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
413 // registers. Consider the following cases:
414 // (1) Storing a wide value must overwrite previous values in both `reg_number`
415 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
416 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
417 // must invalidate it. We store `nullptr` in `reg_number-1`.
418 // Consequently, storing a wide value into the high vreg of another wide value
419 // will invalidate both `reg_number-1` and `reg_number+1`.
420
421 if (reg_number != 0) {
422 HInstruction* local_low = (*current_locals_)[reg_number - 1];
423 if (local_low != nullptr && Primitive::Is64BitType(local_low->GetType())) {
424 // The vreg we are storing into was previously the high vreg of a pair.
425 // We need to invalidate its low vreg.
426 DCHECK((*current_locals_)[reg_number] == nullptr);
427 (*current_locals_)[reg_number - 1] = nullptr;
428 }
429 }
430
431 (*current_locals_)[reg_number] = stored_value;
432 if (Primitive::Is64BitType(stored_type)) {
433 // We are storing a pair. Invalidate the instruction in the high vreg.
434 (*current_locals_)[reg_number + 1] = nullptr;
435 }
436}
437
438void HInstructionBuilder::InitializeParameters() {
439 DCHECK(current_block_->IsEntryBlock());
440
441 // dex_compilation_unit_ is null only when unit testing.
442 if (dex_compilation_unit_ == nullptr) {
443 return;
444 }
445
446 const char* shorty = dex_compilation_unit_->GetShorty();
447 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
448 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
449 uint16_t parameter_index = 0;
450
451 const DexFile::MethodId& referrer_method_id =
452 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
453 if (!dex_compilation_unit_->IsStatic()) {
454 // Add the implicit 'this' argument, not expressed in the signature.
455 HParameterValue* parameter = new (arena_) HParameterValue(*dex_file_,
456 referrer_method_id.class_idx_,
457 parameter_index++,
458 Primitive::kPrimNot,
Igor Murashkind01745e2017-04-05 16:40:31 -0700459 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000460 AppendInstruction(parameter);
461 UpdateLocal(locals_index++, parameter);
462 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700463 current_this_parameter_ = parameter;
464 } else {
465 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000466 }
467
468 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
469 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
470 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
471 HParameterValue* parameter = new (arena_) HParameterValue(
472 *dex_file_,
473 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
474 parameter_index++,
475 Primitive::GetType(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700476 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000477 ++shorty_pos;
478 AppendInstruction(parameter);
479 // Store the parameter value in the local that the dex code will use
480 // to reference that parameter.
481 UpdateLocal(locals_index++, parameter);
482 if (Primitive::Is64BitType(parameter->GetType())) {
483 i++;
484 locals_index++;
485 parameter_index++;
486 }
487 }
488}
489
490template<typename T>
491void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
492 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
493 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
494 T* comparison = new (arena_) T(first, second, dex_pc);
495 AppendInstruction(comparison);
496 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
497 current_block_ = nullptr;
498}
499
500template<typename T>
501void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
502 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
503 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
504 AppendInstruction(comparison);
505 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
506 current_block_ = nullptr;
507}
508
509template<typename T>
510void HInstructionBuilder::Unop_12x(const Instruction& instruction,
511 Primitive::Type type,
512 uint32_t dex_pc) {
513 HInstruction* first = LoadLocal(instruction.VRegB(), type);
514 AppendInstruction(new (arena_) T(type, first, dex_pc));
515 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
516}
517
518void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
519 Primitive::Type input_type,
520 Primitive::Type result_type,
521 uint32_t dex_pc) {
522 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
523 AppendInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc));
524 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
525}
526
527template<typename T>
528void HInstructionBuilder::Binop_23x(const Instruction& instruction,
529 Primitive::Type type,
530 uint32_t dex_pc) {
531 HInstruction* first = LoadLocal(instruction.VRegB(), type);
532 HInstruction* second = LoadLocal(instruction.VRegC(), type);
533 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
534 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
535}
536
537template<typename T>
538void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
539 Primitive::Type type,
540 uint32_t dex_pc) {
541 HInstruction* first = LoadLocal(instruction.VRegB(), type);
542 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt);
543 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
544 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
545}
546
547void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
548 Primitive::Type type,
549 ComparisonBias bias,
550 uint32_t dex_pc) {
551 HInstruction* first = LoadLocal(instruction.VRegB(), type);
552 HInstruction* second = LoadLocal(instruction.VRegC(), type);
553 AppendInstruction(new (arena_) HCompare(type, first, second, bias, dex_pc));
554 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
555}
556
557template<typename T>
558void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
559 Primitive::Type type,
560 uint32_t dex_pc) {
561 HInstruction* first = LoadLocal(instruction.VRegA(), type);
562 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
563 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
564 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
565}
566
567template<typename T>
568void HInstructionBuilder::Binop_12x(const Instruction& instruction,
569 Primitive::Type type,
570 uint32_t dex_pc) {
571 HInstruction* first = LoadLocal(instruction.VRegA(), type);
572 HInstruction* second = LoadLocal(instruction.VRegB(), type);
573 AppendInstruction(new (arena_) T(type, first, second, dex_pc));
574 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
575}
576
577template<typename T>
578void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
579 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
580 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
581 if (reverse) {
582 std::swap(first, second);
583 }
584 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
585 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
586}
587
588template<typename T>
589void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
590 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
591 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
592 if (reverse) {
593 std::swap(first, second);
594 }
595 AppendInstruction(new (arena_) T(Primitive::kPrimInt, first, second, dex_pc));
596 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
597}
598
Igor Murashkind01745e2017-04-05 16:40:31 -0700599// Does the method being compiled need any constructor barriers being inserted?
600// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700601static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700602 // Can be null in unit tests only.
603 if (UNLIKELY(cu == nullptr)) {
604 return false;
605 }
606
David Brazdildee58d62016-04-07 09:54:26 +0000607 Thread* self = Thread::Current();
608 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700609 && !cu->IsStatic()
610 // RequiresConstructorBarrier must only be queried for <init> methods;
611 // it's effectively "false" for every other method.
612 //
613 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700614 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000615}
616
617// Returns true if `block` has only one successor which starts at the next
618// dex_pc after `instruction` at `dex_pc`.
619static bool IsFallthroughInstruction(const Instruction& instruction,
620 uint32_t dex_pc,
621 HBasicBlock* block) {
622 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
623 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
624}
625
626void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
627 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
628 DexSwitchTable table(instruction, dex_pc);
629
630 if (table.GetNumEntries() == 0) {
631 // Empty Switch. Code falls through to the next block.
632 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
633 AppendInstruction(new (arena_) HGoto(dex_pc));
634 } else if (table.ShouldBuildDecisionTree()) {
635 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
636 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
637 HEqual* comparison = new (arena_) HEqual(value, case_value, dex_pc);
638 AppendInstruction(comparison);
639 AppendInstruction(new (arena_) HIf(comparison, dex_pc));
640
641 if (!it.IsLast()) {
642 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
643 }
644 }
645 } else {
646 AppendInstruction(
647 new (arena_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
648 }
649
650 current_block_ = nullptr;
651}
652
653void HInstructionBuilder::BuildReturn(const Instruction& instruction,
654 Primitive::Type type,
655 uint32_t dex_pc) {
656 if (type == Primitive::kPrimVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700657 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700658 // This may insert additional redundant constructor fences from the super constructors.
659 // TODO: remove redundant constructor fences (b/36656456).
660 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700661 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100662 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700663
664 HInstruction* fence_target = current_this_parameter_;
665 DCHECK(fence_target != nullptr);
666
667 AppendInstruction(new (arena_) HConstructorFence(fence_target, dex_pc, arena_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700668 MaybeRecordStat(
669 compilation_stats_,
670 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000671 }
672 AppendInstruction(new (arena_) HReturnVoid(dex_pc));
673 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700674 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000675 HInstruction* value = LoadLocal(instruction.VRegA(), type);
676 AppendInstruction(new (arena_) HReturn(value, dex_pc));
677 }
678 current_block_ = nullptr;
679}
680
681static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
682 switch (opcode) {
683 case Instruction::INVOKE_STATIC:
684 case Instruction::INVOKE_STATIC_RANGE:
685 return kStatic;
686 case Instruction::INVOKE_DIRECT:
687 case Instruction::INVOKE_DIRECT_RANGE:
688 return kDirect;
689 case Instruction::INVOKE_VIRTUAL:
690 case Instruction::INVOKE_VIRTUAL_QUICK:
691 case Instruction::INVOKE_VIRTUAL_RANGE:
692 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
693 return kVirtual;
694 case Instruction::INVOKE_INTERFACE:
695 case Instruction::INVOKE_INTERFACE_RANGE:
696 return kInterface;
697 case Instruction::INVOKE_SUPER_RANGE:
698 case Instruction::INVOKE_SUPER:
699 return kSuper;
700 default:
701 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
702 UNREACHABLE();
703 }
704}
705
706ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
707 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000708
709 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000710 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100711
Vladimir Markoba118822017-06-12 15:41:56 +0100712 ArtMethod* resolved_method =
713 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
714 *dex_compilation_unit_->GetDexFile(),
715 method_idx,
716 dex_compilation_unit_->GetDexCache(),
717 class_loader,
718 graph_->GetArtMethod(),
719 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000720
721 if (UNLIKELY(resolved_method == nullptr)) {
722 // Clean up any exception left by type resolution.
723 soa.Self()->ClearException();
724 return nullptr;
725 }
726
Vladimir Markoba118822017-06-12 15:41:56 +0100727 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
728 // resolved because, for example, we don't find a superclass in the classpath.
729 if (graph_->GetArtMethod() == nullptr) {
730 // The class linker cannot check access without a referrer, so we have to do it.
731 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000732 if (!resolved_method->IsPublic()) {
733 return nullptr;
734 }
David Brazdildee58d62016-04-07 09:54:26 +0000735 }
736
737 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
738 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
739 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
740 // which require runtime handling.
741 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100742 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800743 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000744 // We could not determine the method's class we need to wait until runtime.
745 DCHECK(Runtime::Current()->IsAotCompiler());
746 return nullptr;
747 }
Vladimir Markoba118822017-06-12 15:41:56 +0100748 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
749 *dex_compilation_unit_->GetDexFile(),
750 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
751 dex_compilation_unit_->GetDexCache().Get(),
752 class_loader.Get());
753 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
754 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700755 // We cannot statically determine the target method. The runtime will throw a
756 // NoSuchMethodError on this one.
757 return nullptr;
758 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100759 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100760 if (referenced_class->IsInterface()) {
761 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100762 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000763 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100764 uint16_t vtable_index = resolved_method->GetMethodIndex();
765 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
766 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000767 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100768 if (actual_method != resolved_method &&
769 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
770 // The back-end code generator relies on this check in order to ensure that it will not
771 // attempt to read the dex_cache with a dex_method_index that is not from the correct
772 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
773 // builder, which means that the code-generator (and compiler driver during sharpening and
774 // inliner, maybe) might invoke an incorrect method.
775 // TODO: The actual method could still be referenced in the current dex file, so we
776 // could try locating it.
777 // TODO: Remove the dex_file restriction.
778 return nullptr;
779 }
780 if (!actual_method->IsInvokable()) {
781 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
782 // could resolve the callee to the wrong method.
783 return nullptr;
784 }
785 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000786 }
787
David Brazdildee58d62016-04-07 09:54:26 +0000788 return resolved_method;
789}
790
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100791static bool IsStringConstructor(ArtMethod* method) {
792 ScopedObjectAccess soa(Thread::Current());
793 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
794}
795
David Brazdildee58d62016-04-07 09:54:26 +0000796bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
797 uint32_t dex_pc,
798 uint32_t method_idx,
799 uint32_t number_of_vreg_arguments,
800 bool is_range,
801 uint32_t* args,
802 uint32_t register_index) {
803 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
804 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
805 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
806
807 // Remove the return type from the 'proto'.
808 size_t number_of_arguments = strlen(descriptor) - 1;
809 if (invoke_type != kStatic) { // instance call
810 // One extra argument for 'this'.
811 number_of_arguments++;
812 }
813
David Brazdildee58d62016-04-07 09:54:26 +0000814 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
815
816 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700817 MaybeRecordStat(compilation_stats_,
818 MethodCompilationStat::kUnresolvedMethod);
David Brazdildee58d62016-04-07 09:54:26 +0000819 HInvoke* invoke = new (arena_) HInvokeUnresolved(arena_,
820 number_of_arguments,
821 return_type,
822 dex_pc,
823 method_idx,
824 invoke_type);
825 return HandleInvoke(invoke,
826 number_of_vreg_arguments,
827 args,
828 register_index,
829 is_range,
830 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700831 nullptr, /* clinit_check */
832 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000833 }
834
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100835 // Replace calls to String.<init> with StringFactory.
836 if (IsStringConstructor(resolved_method)) {
837 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
838 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
839 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
840 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000841 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100842 };
843 MethodReference target_method(dex_file_, method_idx);
844 HInvoke* invoke = new (arena_) HInvokeStaticOrDirect(
845 arena_,
846 number_of_arguments - 1,
847 Primitive::kPrimNot /*return_type */,
848 dex_pc,
849 method_idx,
850 nullptr,
851 dispatch_info,
852 invoke_type,
853 target_method,
854 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
855 return HandleStringInit(invoke,
856 number_of_vreg_arguments,
857 args,
858 register_index,
859 is_range,
860 descriptor);
861 }
862
David Brazdildee58d62016-04-07 09:54:26 +0000863 // Potential class initialization check, in the case of a static method call.
864 HClinitCheck* clinit_check = nullptr;
865 HInvoke* invoke = nullptr;
866 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
867 // By default, consider that the called method implicitly requires
868 // an initialization check of its declaring method.
869 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
870 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
871 ScopedObjectAccess soa(Thread::Current());
872 if (invoke_type == kStatic) {
873 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000874 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000875 } else if (invoke_type == kSuper) {
876 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100877 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000878 // we resolved to the method referenced by the instruction.
879 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000880 }
881 }
882
883 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100884 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000885 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000886 0u
David Brazdildee58d62016-04-07 09:54:26 +0000887 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100888 MethodReference target_method(resolved_method->GetDexFile(),
889 resolved_method->GetDexMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000890 invoke = new (arena_) HInvokeStaticOrDirect(arena_,
891 number_of_arguments,
892 return_type,
893 dex_pc,
894 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100895 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000896 dispatch_info,
897 invoke_type,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100898 target_method,
David Brazdildee58d62016-04-07 09:54:26 +0000899 clinit_check_requirement);
900 } else if (invoke_type == kVirtual) {
901 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
902 invoke = new (arena_) HInvokeVirtual(arena_,
903 number_of_arguments,
904 return_type,
905 dex_pc,
906 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100907 resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +0000908 resolved_method->GetMethodIndex());
909 } else {
910 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100911 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
David Brazdildee58d62016-04-07 09:54:26 +0000912 invoke = new (arena_) HInvokeInterface(arena_,
913 number_of_arguments,
914 return_type,
915 dex_pc,
916 method_idx,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100917 resolved_method,
Andreas Gampe75a7db62016-09-26 12:04:26 -0700918 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +0000919 }
920
921 return HandleInvoke(invoke,
922 number_of_vreg_arguments,
923 args,
924 register_index,
925 is_range,
926 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700927 clinit_check,
928 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000929}
930
Orion Hodsonac141392017-01-13 11:53:47 +0000931bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
932 uint32_t dex_pc,
933 uint32_t method_idx,
934 uint32_t proto_idx,
935 uint32_t number_of_vreg_arguments,
936 bool is_range,
937 uint32_t* args,
938 uint32_t register_index) {
939 const char* descriptor = dex_file_->GetShorty(proto_idx);
940 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
941 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
942 size_t number_of_arguments = strlen(descriptor);
943 HInvoke* invoke = new (arena_) HInvokePolymorphic(arena_,
944 number_of_arguments,
945 return_type,
946 dex_pc,
947 method_idx);
948 return HandleInvoke(invoke,
949 number_of_vreg_arguments,
950 args,
951 register_index,
952 is_range,
953 descriptor,
954 nullptr /* clinit_check */,
955 false /* is_unresolved */);
956}
957
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700958HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +0100959 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000960
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000961 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000962
David Brazdildee58d62016-04-07 09:54:26 +0000963 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000964 Handle<mirror::Class> klass = load_class->GetClass();
965
966 if (!IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +0000967 cls = new (arena_) HClinitCheck(load_class, dex_pc);
968 AppendInstruction(cls);
969 }
970
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000971 // Only the access check entrypoint handles the finalizable class case. If we
972 // need access checks, then we haven't resolved the method and the class may
973 // again be finalizable.
974 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
975 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
976 entrypoint = kQuickAllocObjectWithChecks;
977 }
978
979 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800980 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000981
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700982 HNewInstance* new_instance = new (arena_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +0000983 cls,
David Brazdildee58d62016-04-07 09:54:26 +0000984 dex_pc,
985 type_index,
986 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +0000987 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700988 entrypoint);
989 AppendInstruction(new_instance);
990
991 return new_instance;
992}
993
994void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
995 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -0700996 (allocation->IsNewInstance() ||
997 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700998
999 if (allocation->IsNewInstance()) {
1000 // STRING SPECIAL HANDLING:
1001 // -------------------------------
1002 // Strings have a real HNewInstance node but they end up always having 0 uses.
1003 // All uses of a String HNewInstance are always transformed to replace their input
1004 // of the HNewInstance with an input of the invoke to StringFactory.
1005 //
1006 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1007 // optimizations (to pass checker tests that rely on those optimizations).
1008 HNewInstance* new_inst = allocation->AsNewInstance();
1009 HLoadClass* load_class = new_inst->GetLoadClass();
1010
1011 Thread* self = Thread::Current();
1012 ScopedObjectAccess soa(self);
1013 StackHandleScope<1> hs(self);
1014 Handle<mirror::Class> klass = load_class->GetClass();
1015 if (klass != nullptr && klass->IsStringClass()) {
1016 return;
1017 // Note: Do not use allocation->IsStringAlloc which requires
1018 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1019 // propagation (and instruction builder is too early).
1020 }
1021 // (In terms of correctness, the StringFactory needs to provide its own
1022 // default initialization barrier, see below.)
1023 }
1024
1025 // JLS 17.4.5 "Happens-before Order" describes:
1026 //
1027 // The default initialization of any object happens-before any other actions (other than
1028 // default-writes) of a program.
1029 //
1030 // In our implementation the default initialization of an object to type T means
1031 // setting all of its initial data (object[0..size)) to 0, and setting the
1032 // object's class header (i.e. object.getClass() == T.class).
1033 //
1034 // In practice this fence ensures that the writes to the object header
1035 // are visible to other threads if this object escapes the current thread.
1036 // (and in theory the 0-initializing, but that happens automatically
1037 // when new memory pages are mapped in by the OS).
1038 HConstructorFence* ctor_fence =
1039 new (arena_) HConstructorFence(allocation, allocation->GetDexPc(), arena_);
1040 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001041 MaybeRecordStat(
1042 compilation_stats_,
1043 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001044}
1045
1046static bool IsSubClass(mirror::Class* to_test, mirror::Class* super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001047 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001048 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1049}
1050
1051bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001052 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001053 return false;
1054 }
1055
1056 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1057 // check whether the class is in an image for the AOT compilation.
1058 if (cls->IsInitialized() &&
1059 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1060 return true;
1061 }
1062
1063 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1064 return true;
1065 }
1066
1067 // TODO: We should walk over the inlined methods, but we don't pass
1068 // that information to the builder.
1069 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1070 return true;
1071 }
1072
1073 return false;
1074}
1075
1076HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1077 uint32_t dex_pc,
1078 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001079 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001080 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001081
1082 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001083 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001084 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001085 } else {
1086 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1087 klass->GetDexFile(),
1088 klass,
1089 dex_pc,
1090 /* needs_access_check */ false);
1091 if (cls != nullptr) {
1092 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
1093 clinit_check = new (arena_) HClinitCheck(cls, dex_pc);
1094 AppendInstruction(clinit_check);
1095 }
David Brazdildee58d62016-04-07 09:54:26 +00001096 }
1097 return clinit_check;
1098}
1099
1100bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1101 uint32_t number_of_vreg_arguments,
1102 uint32_t* args,
1103 uint32_t register_index,
1104 bool is_range,
1105 const char* descriptor,
1106 size_t start_index,
1107 size_t* argument_index) {
1108 uint32_t descriptor_index = 1; // Skip the return type.
1109
1110 for (size_t i = start_index;
1111 // Make sure we don't go over the expected arguments or over the number of
1112 // dex registers given. If the instruction was seen as dead by the verifier,
1113 // it hasn't been properly checked.
1114 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1115 i++, (*argument_index)++) {
1116 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
1117 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
1118 if (!is_range
1119 && is_wide
1120 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1121 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1122 // reject any class where this is violated. However, the verifier only does these checks
1123 // on non trivially dead instructions, so we just bailout the compilation.
1124 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001125 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001126 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001127 MaybeRecordStat(compilation_stats_,
1128 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001129 return false;
1130 }
1131 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1132 invoke->SetArgumentAt(*argument_index, arg);
1133 if (is_wide) {
1134 i++;
1135 }
1136 }
1137
1138 if (*argument_index != invoke->GetNumberOfArguments()) {
1139 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001140 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001141 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001142 MaybeRecordStat(compilation_stats_,
1143 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001144 return false;
1145 }
1146
1147 if (invoke->IsInvokeStaticOrDirect() &&
1148 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1149 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1150 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1151 (*argument_index)++;
1152 }
1153
1154 return true;
1155}
1156
1157bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1158 uint32_t number_of_vreg_arguments,
1159 uint32_t* args,
1160 uint32_t register_index,
1161 bool is_range,
1162 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001163 HClinitCheck* clinit_check,
1164 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001165 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1166
1167 size_t start_index = 0;
1168 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001169 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001170 uint32_t obj_reg = is_range ? register_index : args[0];
1171 HInstruction* arg = is_unresolved
1172 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1173 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001174 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001175 start_index = 1;
1176 argument_index = 1;
1177 }
1178
1179 if (!SetupInvokeArguments(invoke,
1180 number_of_vreg_arguments,
1181 args,
1182 register_index,
1183 is_range,
1184 descriptor,
1185 start_index,
1186 &argument_index)) {
1187 return false;
1188 }
1189
1190 if (clinit_check != nullptr) {
1191 // Add the class initialization check as last input of `invoke`.
1192 DCHECK(invoke->IsInvokeStaticOrDirect());
1193 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1194 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1195 invoke->SetArgumentAt(argument_index, clinit_check);
1196 argument_index++;
1197 }
1198
1199 AppendInstruction(invoke);
1200 latest_result_ = invoke;
1201
1202 return true;
1203}
1204
1205bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1206 uint32_t number_of_vreg_arguments,
1207 uint32_t* args,
1208 uint32_t register_index,
1209 bool is_range,
1210 const char* descriptor) {
1211 DCHECK(invoke->IsInvokeStaticOrDirect());
1212 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1213
1214 size_t start_index = 1;
1215 size_t argument_index = 0;
1216 if (!SetupInvokeArguments(invoke,
1217 number_of_vreg_arguments,
1218 args,
1219 register_index,
1220 is_range,
1221 descriptor,
1222 start_index,
1223 &argument_index)) {
1224 return false;
1225 }
1226
1227 AppendInstruction(invoke);
1228
1229 // This is a StringFactory call, not an actual String constructor. Its result
1230 // replaces the empty String pre-allocated by NewInstance.
1231 uint32_t orig_this_reg = is_range ? register_index : args[0];
1232 HInstruction* arg_this = LoadLocal(orig_this_reg, Primitive::kPrimNot);
1233
1234 // Replacing the NewInstance might render it redundant. Keep a list of these
1235 // to be visited once it is clear whether it is has remaining uses.
1236 if (arg_this->IsNewInstance()) {
1237 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1238 } else {
1239 DCHECK(arg_this->IsPhi());
1240 // NewInstance is not the direct input of the StringFactory call. It might
1241 // be redundant but optimizing this case is not worth the effort.
1242 }
1243
1244 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1245 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1246 if ((*current_locals_)[vreg] == arg_this) {
1247 (*current_locals_)[vreg] = invoke;
1248 }
1249 }
1250
1251 return true;
1252}
1253
1254static Primitive::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
1255 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1256 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
1257 return Primitive::GetType(type[0]);
1258}
1259
1260bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1261 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001262 bool is_put,
1263 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001264 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1265 uint32_t obj_reg = instruction.VRegB_22c();
1266 uint16_t field_index;
1267 if (instruction.IsQuickened()) {
1268 if (!CanDecodeQuickenedInfo()) {
1269 return false;
1270 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001271 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001272 } else {
1273 field_index = instruction.VRegC_22c();
1274 }
1275
1276 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001277 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001278
Aart Bik14154132016-06-02 17:53:58 -07001279 // Generate an explicit null check on the reference, unless the field access
1280 // is unresolved. In that case, we rely on the runtime to perform various
1281 // checks first, followed by a null check.
1282 HInstruction* object = (resolved_field == nullptr)
1283 ? LoadLocal(obj_reg, Primitive::kPrimNot)
1284 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001285
1286 Primitive::Type field_type = (resolved_field == nullptr)
1287 ? GetFieldAccessType(*dex_file_, field_index)
1288 : resolved_field->GetTypeAsPrimitiveType();
1289 if (is_put) {
1290 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1291 HInstruction* field_set = nullptr;
1292 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001293 MaybeRecordStat(compilation_stats_,
1294 MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001295 field_set = new (arena_) HUnresolvedInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001296 value,
1297 field_type,
1298 field_index,
1299 dex_pc);
1300 } else {
1301 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001302 field_set = new (arena_) HInstanceFieldSet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001303 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001304 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001305 field_type,
1306 resolved_field->GetOffset(),
1307 resolved_field->IsVolatile(),
1308 field_index,
1309 class_def_index,
1310 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001311 dex_pc);
1312 }
1313 AppendInstruction(field_set);
1314 } else {
1315 HInstruction* field_get = nullptr;
1316 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001317 MaybeRecordStat(compilation_stats_,
1318 MethodCompilationStat::kUnresolvedField);
David Brazdilc120bbe2016-04-22 16:57:00 +01001319 field_get = new (arena_) HUnresolvedInstanceFieldGet(object,
David Brazdildee58d62016-04-07 09:54:26 +00001320 field_type,
1321 field_index,
1322 dex_pc);
1323 } else {
1324 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
David Brazdilc120bbe2016-04-22 16:57:00 +01001325 field_get = new (arena_) HInstanceFieldGet(object,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001326 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001327 field_type,
1328 resolved_field->GetOffset(),
1329 resolved_field->IsVolatile(),
1330 field_index,
1331 class_def_index,
1332 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001333 dex_pc);
1334 }
1335 AppendInstruction(field_get);
1336 UpdateLocal(source_or_dest_reg, field_get);
1337 }
1338
1339 return true;
1340}
1341
1342static mirror::Class* GetClassFrom(CompilerDriver* driver,
1343 const DexCompilationUnit& compilation_unit) {
1344 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001345 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001346 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001347
1348 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1349}
1350
1351mirror::Class* HInstructionBuilder::GetOutermostCompilingClass() const {
1352 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1353}
1354
1355mirror::Class* HInstructionBuilder::GetCompilingClass() const {
1356 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1357}
1358
Andreas Gampea5b09a62016-11-17 15:21:22 -08001359bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001360 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001361 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001362 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001363 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001364 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1365 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1366 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1367
1368 // GetOutermostCompilingClass returns null when the class is unresolved
1369 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1370 // we are compiling it.
1371 // When this happens we cannot establish a direct relation between the current
1372 // class and the outer class, so we return false.
1373 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001374 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001375}
1376
1377void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001378 uint32_t dex_pc,
1379 bool is_put,
1380 Primitive::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001381 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1382 uint16_t field_index = instruction.VRegB_21c();
1383
1384 if (is_put) {
1385 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1386 AppendInstruction(
1387 new (arena_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
1388 } else {
1389 AppendInstruction(new (arena_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
1390 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1391 }
1392}
1393
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001394ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1395 ScopedObjectAccess soa(Thread::Current());
1396 StackHandleScope<2> hs(soa.Self());
1397
1398 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001399 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001400 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1401
1402 ArtField* resolved_field = class_linker->ResolveField(*dex_compilation_unit_->GetDexFile(),
1403 field_idx,
1404 dex_compilation_unit_->GetDexCache(),
1405 class_loader,
1406 is_static);
1407
1408 if (UNLIKELY(resolved_field == nullptr)) {
1409 // Clean up any exception left by type resolution.
1410 soa.Self()->ClearException();
1411 return nullptr;
1412 }
1413
1414 // Check static/instance. The class linker has a fast path for looking into the dex cache
1415 // and does not check static/instance if it hits it.
1416 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1417 return nullptr;
1418 }
1419
1420 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001421 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001422 if (!resolved_field->IsPublic()) {
1423 return nullptr;
1424 }
1425 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1426 resolved_field,
1427 dex_compilation_unit_->GetDexCache().Get(),
1428 field_idx)) {
1429 return nullptr;
1430 }
1431
1432 if (is_put &&
1433 resolved_field->IsFinal() &&
1434 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1435 // Final fields can only be updated within their own class.
1436 // TODO: Only allow it in constructors. b/34966607.
1437 return nullptr;
1438 }
1439
1440 return resolved_field;
1441}
1442
David Brazdildee58d62016-04-07 09:54:26 +00001443bool HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
1444 uint32_t dex_pc,
1445 bool is_put) {
1446 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1447 uint16_t field_index = instruction.VRegB_21c();
1448
1449 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001450 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001451
1452 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001453 MaybeRecordStat(compilation_stats_,
1454 MethodCompilationStat::kUnresolvedField);
David Brazdildee58d62016-04-07 09:54:26 +00001455 Primitive::Type field_type = GetFieldAccessType(*dex_file_, field_index);
1456 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1457 return true;
1458 }
1459
1460 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
David Brazdildee58d62016-04-07 09:54:26 +00001461
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001462 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1463 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1464 klass->GetDexFile(),
1465 klass,
1466 dex_pc,
1467 /* needs_access_check */ false);
1468
1469 if (constant == nullptr) {
1470 // The class cannot be referenced from this compiled code. Generate
1471 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001472 MaybeRecordStat(compilation_stats_,
1473 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001474 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
1475 return true;
David Brazdildee58d62016-04-07 09:54:26 +00001476 }
1477
David Brazdildee58d62016-04-07 09:54:26 +00001478 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001479 if (!IsInitialized(klass)) {
1480 cls = new (arena_) HClinitCheck(constant, dex_pc);
1481 AppendInstruction(cls);
1482 }
1483
1484 uint16_t class_def_index = klass->GetDexClassDefIndex();
1485 if (is_put) {
1486 // We need to keep the class alive before loading the value.
1487 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1488 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
1489 AppendInstruction(new (arena_) HStaticFieldSet(cls,
1490 value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001491 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001492 field_type,
1493 resolved_field->GetOffset(),
1494 resolved_field->IsVolatile(),
1495 field_index,
1496 class_def_index,
1497 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001498 dex_pc));
1499 } else {
1500 AppendInstruction(new (arena_) HStaticFieldGet(cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001501 resolved_field,
David Brazdildee58d62016-04-07 09:54:26 +00001502 field_type,
1503 resolved_field->GetOffset(),
1504 resolved_field->IsVolatile(),
1505 field_index,
1506 class_def_index,
1507 *dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +00001508 dex_pc));
1509 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1510 }
1511 return true;
1512}
1513
1514void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
1515 uint16_t first_vreg,
1516 int64_t second_vreg_or_constant,
1517 uint32_t dex_pc,
1518 Primitive::Type type,
1519 bool second_is_constant,
1520 bool isDiv) {
1521 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1522
1523 HInstruction* first = LoadLocal(first_vreg, type);
1524 HInstruction* second = nullptr;
1525 if (second_is_constant) {
1526 if (type == Primitive::kPrimInt) {
1527 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1528 } else {
1529 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1530 }
1531 } else {
1532 second = LoadLocal(second_vreg_or_constant, type);
1533 }
1534
1535 if (!second_is_constant
1536 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0)
1537 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) {
1538 second = new (arena_) HDivZeroCheck(second, dex_pc);
1539 AppendInstruction(second);
1540 }
1541
1542 if (isDiv) {
1543 AppendInstruction(new (arena_) HDiv(type, first, second, dex_pc));
1544 } else {
1545 AppendInstruction(new (arena_) HRem(type, first, second, dex_pc));
1546 }
1547 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1548}
1549
1550void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1551 uint32_t dex_pc,
1552 bool is_put,
1553 Primitive::Type anticipated_type) {
1554 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1555 uint8_t array_reg = instruction.VRegB_23x();
1556 uint8_t index_reg = instruction.VRegC_23x();
1557
David Brazdilc120bbe2016-04-22 16:57:00 +01001558 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001559 HInstruction* length = new (arena_) HArrayLength(object, dex_pc);
1560 AppendInstruction(length);
1561 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
1562 index = new (arena_) HBoundsCheck(index, length, dex_pc);
1563 AppendInstruction(index);
1564 if (is_put) {
1565 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1566 // TODO: Insert a type check node if the type is Object.
1567 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1568 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1569 AppendInstruction(aset);
1570 } else {
1571 HArrayGet* aget = new (arena_) HArrayGet(object, index, anticipated_type, dex_pc);
1572 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1573 AppendInstruction(aget);
1574 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1575 }
1576 graph_->SetHasBoundsChecks(true);
1577}
1578
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001579HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1580 dex::TypeIndex type_index,
1581 uint32_t number_of_vreg_arguments,
1582 bool is_range,
1583 uint32_t* args,
1584 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001585 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001586 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001587 HNewArray* const object = new (arena_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001588 AppendInstruction(object);
1589
1590 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1591 DCHECK_EQ(descriptor[0], '[') << descriptor;
1592 char primitive = descriptor[1];
1593 DCHECK(primitive == 'I'
1594 || primitive == 'L'
1595 || primitive == '[') << descriptor;
1596 bool is_reference_array = (primitive == 'L') || (primitive == '[');
1597 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
1598
1599 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1600 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1601 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1602 HArraySet* aset = new (arena_) HArraySet(object, index, value, type, dex_pc);
1603 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1604 AppendInstruction(aset);
1605 }
1606 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001607
1608 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001609}
1610
1611template <typename T>
1612void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1613 const T* data,
1614 uint32_t element_count,
1615 Primitive::Type anticipated_type,
1616 uint32_t dex_pc) {
1617 for (uint32_t i = 0; i < element_count; ++i) {
1618 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1619 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
1620 HArraySet* aset = new (arena_) HArraySet(object, index, value, anticipated_type, dex_pc);
1621 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1622 AppendInstruction(aset);
1623 }
1624}
1625
1626void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001627 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001628
1629 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1630 const Instruction::ArrayDataPayload* payload =
1631 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_.insns_ + payload_offset);
1632 const uint8_t* data = payload->data;
1633 uint32_t element_count = payload->element_count;
1634
Vladimir Markoc69fba22016-09-06 16:49:15 +01001635 if (element_count == 0u) {
1636 // For empty payload we emit only the null check above.
1637 return;
1638 }
1639
1640 HInstruction* length = new (arena_) HArrayLength(array, dex_pc);
1641 AppendInstruction(length);
1642
David Brazdildee58d62016-04-07 09:54:26 +00001643 // Implementation of this DEX instruction seems to be that the bounds check is
1644 // done before doing any stores.
1645 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
1646 AppendInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc));
1647
1648 switch (payload->element_width) {
1649 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001650 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001651 reinterpret_cast<const int8_t*>(data),
1652 element_count,
1653 Primitive::kPrimByte,
1654 dex_pc);
1655 break;
1656 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001657 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001658 reinterpret_cast<const int16_t*>(data),
1659 element_count,
1660 Primitive::kPrimShort,
1661 dex_pc);
1662 break;
1663 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001664 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001665 reinterpret_cast<const int32_t*>(data),
1666 element_count,
1667 Primitive::kPrimInt,
1668 dex_pc);
1669 break;
1670 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001671 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001672 reinterpret_cast<const int64_t*>(data),
1673 element_count,
1674 dex_pc);
1675 break;
1676 default:
1677 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1678 }
1679 graph_->SetHasBoundsChecks(true);
1680}
1681
1682void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1683 const int64_t* data,
1684 uint32_t element_count,
1685 uint32_t dex_pc) {
1686 for (uint32_t i = 0; i < element_count; ++i) {
1687 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1688 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
1689 HArraySet* aset = new (arena_) HArraySet(object, index, value, Primitive::kPrimLong, dex_pc);
1690 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1691 AppendInstruction(aset);
1692 }
1693}
1694
1695static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001696 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001697 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001698 return TypeCheckKind::kUnresolvedCheck;
1699 } else if (cls->IsInterface()) {
1700 return TypeCheckKind::kInterfaceCheck;
1701 } else if (cls->IsArrayClass()) {
1702 if (cls->GetComponentType()->IsObjectClass()) {
1703 return TypeCheckKind::kArrayObjectCheck;
1704 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1705 return TypeCheckKind::kExactCheck;
1706 } else {
1707 return TypeCheckKind::kArrayCheck;
1708 }
1709 } else if (cls->IsFinal()) {
1710 return TypeCheckKind::kExactCheck;
1711 } else if (cls->IsAbstract()) {
1712 return TypeCheckKind::kAbstractClassCheck;
1713 } else {
1714 return TypeCheckKind::kClassHierarchyCheck;
1715 }
1716}
1717
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001718HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001719 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001720 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001721 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001722 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001723 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001724
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001725 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001726 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001727 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001728 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001729 } else {
1730 mirror::Class* compiling_class = GetCompilingClass();
1731 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001732 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001733 }
1734 }
1735 }
1736
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001737 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1738}
1739
1740HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1741 const DexFile& dex_file,
1742 Handle<mirror::Class> klass,
1743 uint32_t dex_pc,
1744 bool needs_access_check) {
1745 // Try to find a reference in the compiling dex file.
1746 const DexFile* actual_dex_file = &dex_file;
1747 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1748 dex::TypeIndex local_type_index =
1749 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1750 if (local_type_index.IsValid()) {
1751 type_index = local_type_index;
1752 actual_dex_file = dex_compilation_unit_->GetDexFile();
1753 }
1754 }
1755
1756 // Note: `klass` must be from `handles_`.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001757 HLoadClass* load_class = new (arena_) HLoadClass(
1758 graph_->GetCurrentMethod(),
1759 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001760 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001761 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001762 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001763 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001764 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001765
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001766 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1767 code_generator_,
1768 compiler_driver_,
1769 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001770
1771 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1772 // We actually cannot reference this class, we're forced to bail.
1773 return nullptr;
1774 }
1775 // Append the instruction first, as setting the load kind affects the inputs.
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001776 AppendInstruction(load_class);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001777 load_class->SetLoadKind(load_kind);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001778 return load_class;
1779}
1780
David Brazdildee58d62016-04-07 09:54:26 +00001781void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1782 uint8_t destination,
1783 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001784 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001785 uint32_t dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00001786 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001787 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001788
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001789 ScopedObjectAccess soa(Thread::Current());
1790 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001791 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
1792 AppendInstruction(new (arena_) HInstanceOf(object, cls, check_kind, dex_pc));
1793 UpdateLocal(destination, current_block_->GetLastInstruction());
1794 } else {
1795 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1796 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1797 // which may throw. If it succeeds BoundType sets the new type of `object`
1798 // for all subsequent uses.
1799 AppendInstruction(new (arena_) HCheckCast(object, cls, check_kind, dex_pc));
1800 AppendInstruction(new (arena_) HBoundType(object, dex_pc));
1801 UpdateLocal(reference, current_block_->GetLastInstruction());
1802 }
1803}
1804
Vladimir Marko0b66d612017-03-13 14:50:04 +00001805bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001806 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1807 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001808}
1809
1810bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001811 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001812}
1813
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001814uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1815 DCHECK(CanDecodeQuickenedInfo());
1816 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001817}
1818
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001819bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1820 uint32_t dex_pc,
1821 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001822 switch (instruction.Opcode()) {
1823 case Instruction::CONST_4: {
1824 int32_t register_index = instruction.VRegA();
1825 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1826 UpdateLocal(register_index, constant);
1827 break;
1828 }
1829
1830 case Instruction::CONST_16: {
1831 int32_t register_index = instruction.VRegA();
1832 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1833 UpdateLocal(register_index, constant);
1834 break;
1835 }
1836
1837 case Instruction::CONST: {
1838 int32_t register_index = instruction.VRegA();
1839 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1840 UpdateLocal(register_index, constant);
1841 break;
1842 }
1843
1844 case Instruction::CONST_HIGH16: {
1845 int32_t register_index = instruction.VRegA();
1846 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1847 UpdateLocal(register_index, constant);
1848 break;
1849 }
1850
1851 case Instruction::CONST_WIDE_16: {
1852 int32_t register_index = instruction.VRegA();
1853 // Get 16 bits of constant value, sign extended to 64 bits.
1854 int64_t value = instruction.VRegB_21s();
1855 value <<= 48;
1856 value >>= 48;
1857 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1858 UpdateLocal(register_index, constant);
1859 break;
1860 }
1861
1862 case Instruction::CONST_WIDE_32: {
1863 int32_t register_index = instruction.VRegA();
1864 // Get 32 bits of constant value, sign extended to 64 bits.
1865 int64_t value = instruction.VRegB_31i();
1866 value <<= 32;
1867 value >>= 32;
1868 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1869 UpdateLocal(register_index, constant);
1870 break;
1871 }
1872
1873 case Instruction::CONST_WIDE: {
1874 int32_t register_index = instruction.VRegA();
1875 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1876 UpdateLocal(register_index, constant);
1877 break;
1878 }
1879
1880 case Instruction::CONST_WIDE_HIGH16: {
1881 int32_t register_index = instruction.VRegA();
1882 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1883 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1884 UpdateLocal(register_index, constant);
1885 break;
1886 }
1887
1888 // Note that the SSA building will refine the types.
1889 case Instruction::MOVE:
1890 case Instruction::MOVE_FROM16:
1891 case Instruction::MOVE_16: {
1892 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
1893 UpdateLocal(instruction.VRegA(), value);
1894 break;
1895 }
1896
1897 // Note that the SSA building will refine the types.
1898 case Instruction::MOVE_WIDE:
1899 case Instruction::MOVE_WIDE_FROM16:
1900 case Instruction::MOVE_WIDE_16: {
1901 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
1902 UpdateLocal(instruction.VRegA(), value);
1903 break;
1904 }
1905
1906 case Instruction::MOVE_OBJECT:
1907 case Instruction::MOVE_OBJECT_16:
1908 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001909 // The verifier has no notion of a null type, so a move-object of constant 0
1910 // will lead to the same constant 0 in the destination register. To mimic
1911 // this behavior, we just pretend we haven't seen a type change (int to reference)
1912 // for the 0 constant and phis. We rely on our type propagation to eventually get the
1913 // types correct.
1914 uint32_t reg_number = instruction.VRegB();
1915 HInstruction* value = (*current_locals_)[reg_number];
1916 if (value->IsIntConstant()) {
1917 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
1918 } else if (value->IsPhi()) {
1919 DCHECK(value->GetType() == Primitive::kPrimInt || value->GetType() == Primitive::kPrimNot);
1920 } else {
1921 value = LoadLocal(reg_number, Primitive::kPrimNot);
1922 }
David Brazdildee58d62016-04-07 09:54:26 +00001923 UpdateLocal(instruction.VRegA(), value);
1924 break;
1925 }
1926
1927 case Instruction::RETURN_VOID_NO_BARRIER:
1928 case Instruction::RETURN_VOID: {
1929 BuildReturn(instruction, Primitive::kPrimVoid, dex_pc);
1930 break;
1931 }
1932
1933#define IF_XX(comparison, cond) \
1934 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
1935 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
1936
1937 IF_XX(HEqual, EQ);
1938 IF_XX(HNotEqual, NE);
1939 IF_XX(HLessThan, LT);
1940 IF_XX(HLessThanOrEqual, LE);
1941 IF_XX(HGreaterThan, GT);
1942 IF_XX(HGreaterThanOrEqual, GE);
1943
1944 case Instruction::GOTO:
1945 case Instruction::GOTO_16:
1946 case Instruction::GOTO_32: {
1947 AppendInstruction(new (arena_) HGoto(dex_pc));
1948 current_block_ = nullptr;
1949 break;
1950 }
1951
1952 case Instruction::RETURN: {
1953 BuildReturn(instruction, return_type_, dex_pc);
1954 break;
1955 }
1956
1957 case Instruction::RETURN_OBJECT: {
1958 BuildReturn(instruction, return_type_, dex_pc);
1959 break;
1960 }
1961
1962 case Instruction::RETURN_WIDE: {
1963 BuildReturn(instruction, return_type_, dex_pc);
1964 break;
1965 }
1966
1967 case Instruction::INVOKE_DIRECT:
1968 case Instruction::INVOKE_INTERFACE:
1969 case Instruction::INVOKE_STATIC:
1970 case Instruction::INVOKE_SUPER:
1971 case Instruction::INVOKE_VIRTUAL:
1972 case Instruction::INVOKE_VIRTUAL_QUICK: {
1973 uint16_t method_idx;
1974 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
1975 if (!CanDecodeQuickenedInfo()) {
1976 return false;
1977 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001978 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001979 } else {
1980 method_idx = instruction.VRegB_35c();
1981 }
1982 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1983 uint32_t args[5];
1984 instruction.GetVarArgs(args);
1985 if (!BuildInvoke(instruction, dex_pc, method_idx,
1986 number_of_vreg_arguments, false, args, -1)) {
1987 return false;
1988 }
1989 break;
1990 }
1991
1992 case Instruction::INVOKE_DIRECT_RANGE:
1993 case Instruction::INVOKE_INTERFACE_RANGE:
1994 case Instruction::INVOKE_STATIC_RANGE:
1995 case Instruction::INVOKE_SUPER_RANGE:
1996 case Instruction::INVOKE_VIRTUAL_RANGE:
1997 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
1998 uint16_t method_idx;
1999 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2000 if (!CanDecodeQuickenedInfo()) {
2001 return false;
2002 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002003 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002004 } else {
2005 method_idx = instruction.VRegB_3rc();
2006 }
2007 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2008 uint32_t register_index = instruction.VRegC();
2009 if (!BuildInvoke(instruction, dex_pc, method_idx,
2010 number_of_vreg_arguments, true, nullptr, register_index)) {
2011 return false;
2012 }
2013 break;
2014 }
2015
Orion Hodsonac141392017-01-13 11:53:47 +00002016 case Instruction::INVOKE_POLYMORPHIC: {
2017 uint16_t method_idx = instruction.VRegB_45cc();
2018 uint16_t proto_idx = instruction.VRegH_45cc();
2019 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2020 uint32_t args[5];
2021 instruction.GetVarArgs(args);
2022 return BuildInvokePolymorphic(instruction,
2023 dex_pc,
2024 method_idx,
2025 proto_idx,
2026 number_of_vreg_arguments,
2027 false,
2028 args,
2029 -1);
2030 }
2031
2032 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2033 uint16_t method_idx = instruction.VRegB_4rcc();
2034 uint16_t proto_idx = instruction.VRegH_4rcc();
2035 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2036 uint32_t register_index = instruction.VRegC_4rcc();
2037 return BuildInvokePolymorphic(instruction,
2038 dex_pc,
2039 method_idx,
2040 proto_idx,
2041 number_of_vreg_arguments,
2042 true,
2043 nullptr,
2044 register_index);
2045 }
2046
David Brazdildee58d62016-04-07 09:54:26 +00002047 case Instruction::NEG_INT: {
2048 Unop_12x<HNeg>(instruction, Primitive::kPrimInt, dex_pc);
2049 break;
2050 }
2051
2052 case Instruction::NEG_LONG: {
2053 Unop_12x<HNeg>(instruction, Primitive::kPrimLong, dex_pc);
2054 break;
2055 }
2056
2057 case Instruction::NEG_FLOAT: {
2058 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat, dex_pc);
2059 break;
2060 }
2061
2062 case Instruction::NEG_DOUBLE: {
2063 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble, dex_pc);
2064 break;
2065 }
2066
2067 case Instruction::NOT_INT: {
2068 Unop_12x<HNot>(instruction, Primitive::kPrimInt, dex_pc);
2069 break;
2070 }
2071
2072 case Instruction::NOT_LONG: {
2073 Unop_12x<HNot>(instruction, Primitive::kPrimLong, dex_pc);
2074 break;
2075 }
2076
2077 case Instruction::INT_TO_LONG: {
2078 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::INT_TO_FLOAT: {
2083 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::INT_TO_DOUBLE: {
2088 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc);
2089 break;
2090 }
2091
2092 case Instruction::LONG_TO_INT: {
2093 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc);
2094 break;
2095 }
2096
2097 case Instruction::LONG_TO_FLOAT: {
2098 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc);
2099 break;
2100 }
2101
2102 case Instruction::LONG_TO_DOUBLE: {
2103 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc);
2104 break;
2105 }
2106
2107 case Instruction::FLOAT_TO_INT: {
2108 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc);
2109 break;
2110 }
2111
2112 case Instruction::FLOAT_TO_LONG: {
2113 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc);
2114 break;
2115 }
2116
2117 case Instruction::FLOAT_TO_DOUBLE: {
2118 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc);
2119 break;
2120 }
2121
2122 case Instruction::DOUBLE_TO_INT: {
2123 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc);
2124 break;
2125 }
2126
2127 case Instruction::DOUBLE_TO_LONG: {
2128 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc);
2129 break;
2130 }
2131
2132 case Instruction::DOUBLE_TO_FLOAT: {
2133 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc);
2134 break;
2135 }
2136
2137 case Instruction::INT_TO_BYTE: {
2138 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc);
2139 break;
2140 }
2141
2142 case Instruction::INT_TO_SHORT: {
2143 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc);
2144 break;
2145 }
2146
2147 case Instruction::INT_TO_CHAR: {
2148 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc);
2149 break;
2150 }
2151
2152 case Instruction::ADD_INT: {
2153 Binop_23x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2154 break;
2155 }
2156
2157 case Instruction::ADD_LONG: {
2158 Binop_23x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2159 break;
2160 }
2161
2162 case Instruction::ADD_DOUBLE: {
2163 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2164 break;
2165 }
2166
2167 case Instruction::ADD_FLOAT: {
2168 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2169 break;
2170 }
2171
2172 case Instruction::SUB_INT: {
2173 Binop_23x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2174 break;
2175 }
2176
2177 case Instruction::SUB_LONG: {
2178 Binop_23x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2179 break;
2180 }
2181
2182 case Instruction::SUB_FLOAT: {
2183 Binop_23x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2184 break;
2185 }
2186
2187 case Instruction::SUB_DOUBLE: {
2188 Binop_23x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2189 break;
2190 }
2191
2192 case Instruction::ADD_INT_2ADDR: {
2193 Binop_12x<HAdd>(instruction, Primitive::kPrimInt, dex_pc);
2194 break;
2195 }
2196
2197 case Instruction::MUL_INT: {
2198 Binop_23x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2199 break;
2200 }
2201
2202 case Instruction::MUL_LONG: {
2203 Binop_23x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2204 break;
2205 }
2206
2207 case Instruction::MUL_FLOAT: {
2208 Binop_23x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2209 break;
2210 }
2211
2212 case Instruction::MUL_DOUBLE: {
2213 Binop_23x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2214 break;
2215 }
2216
2217 case Instruction::DIV_INT: {
2218 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2219 dex_pc, Primitive::kPrimInt, false, true);
2220 break;
2221 }
2222
2223 case Instruction::DIV_LONG: {
2224 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2225 dex_pc, Primitive::kPrimLong, false, true);
2226 break;
2227 }
2228
2229 case Instruction::DIV_FLOAT: {
2230 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2231 break;
2232 }
2233
2234 case Instruction::DIV_DOUBLE: {
2235 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2236 break;
2237 }
2238
2239 case Instruction::REM_INT: {
2240 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2241 dex_pc, Primitive::kPrimInt, false, false);
2242 break;
2243 }
2244
2245 case Instruction::REM_LONG: {
2246 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2247 dex_pc, Primitive::kPrimLong, false, false);
2248 break;
2249 }
2250
2251 case Instruction::REM_FLOAT: {
2252 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2253 break;
2254 }
2255
2256 case Instruction::REM_DOUBLE: {
2257 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2258 break;
2259 }
2260
2261 case Instruction::AND_INT: {
2262 Binop_23x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2263 break;
2264 }
2265
2266 case Instruction::AND_LONG: {
2267 Binop_23x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2268 break;
2269 }
2270
2271 case Instruction::SHL_INT: {
2272 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2273 break;
2274 }
2275
2276 case Instruction::SHL_LONG: {
2277 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2278 break;
2279 }
2280
2281 case Instruction::SHR_INT: {
2282 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2283 break;
2284 }
2285
2286 case Instruction::SHR_LONG: {
2287 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2288 break;
2289 }
2290
2291 case Instruction::USHR_INT: {
2292 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2293 break;
2294 }
2295
2296 case Instruction::USHR_LONG: {
2297 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2298 break;
2299 }
2300
2301 case Instruction::OR_INT: {
2302 Binop_23x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2303 break;
2304 }
2305
2306 case Instruction::OR_LONG: {
2307 Binop_23x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2308 break;
2309 }
2310
2311 case Instruction::XOR_INT: {
2312 Binop_23x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2313 break;
2314 }
2315
2316 case Instruction::XOR_LONG: {
2317 Binop_23x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2318 break;
2319 }
2320
2321 case Instruction::ADD_LONG_2ADDR: {
2322 Binop_12x<HAdd>(instruction, Primitive::kPrimLong, dex_pc);
2323 break;
2324 }
2325
2326 case Instruction::ADD_DOUBLE_2ADDR: {
2327 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble, dex_pc);
2328 break;
2329 }
2330
2331 case Instruction::ADD_FLOAT_2ADDR: {
2332 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat, dex_pc);
2333 break;
2334 }
2335
2336 case Instruction::SUB_INT_2ADDR: {
2337 Binop_12x<HSub>(instruction, Primitive::kPrimInt, dex_pc);
2338 break;
2339 }
2340
2341 case Instruction::SUB_LONG_2ADDR: {
2342 Binop_12x<HSub>(instruction, Primitive::kPrimLong, dex_pc);
2343 break;
2344 }
2345
2346 case Instruction::SUB_FLOAT_2ADDR: {
2347 Binop_12x<HSub>(instruction, Primitive::kPrimFloat, dex_pc);
2348 break;
2349 }
2350
2351 case Instruction::SUB_DOUBLE_2ADDR: {
2352 Binop_12x<HSub>(instruction, Primitive::kPrimDouble, dex_pc);
2353 break;
2354 }
2355
2356 case Instruction::MUL_INT_2ADDR: {
2357 Binop_12x<HMul>(instruction, Primitive::kPrimInt, dex_pc);
2358 break;
2359 }
2360
2361 case Instruction::MUL_LONG_2ADDR: {
2362 Binop_12x<HMul>(instruction, Primitive::kPrimLong, dex_pc);
2363 break;
2364 }
2365
2366 case Instruction::MUL_FLOAT_2ADDR: {
2367 Binop_12x<HMul>(instruction, Primitive::kPrimFloat, dex_pc);
2368 break;
2369 }
2370
2371 case Instruction::MUL_DOUBLE_2ADDR: {
2372 Binop_12x<HMul>(instruction, Primitive::kPrimDouble, dex_pc);
2373 break;
2374 }
2375
2376 case Instruction::DIV_INT_2ADDR: {
2377 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2378 dex_pc, Primitive::kPrimInt, false, true);
2379 break;
2380 }
2381
2382 case Instruction::DIV_LONG_2ADDR: {
2383 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2384 dex_pc, Primitive::kPrimLong, false, true);
2385 break;
2386 }
2387
2388 case Instruction::REM_INT_2ADDR: {
2389 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2390 dex_pc, Primitive::kPrimInt, false, false);
2391 break;
2392 }
2393
2394 case Instruction::REM_LONG_2ADDR: {
2395 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
2396 dex_pc, Primitive::kPrimLong, false, false);
2397 break;
2398 }
2399
2400 case Instruction::REM_FLOAT_2ADDR: {
2401 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc);
2402 break;
2403 }
2404
2405 case Instruction::REM_DOUBLE_2ADDR: {
2406 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc);
2407 break;
2408 }
2409
2410 case Instruction::SHL_INT_2ADDR: {
2411 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt, dex_pc);
2412 break;
2413 }
2414
2415 case Instruction::SHL_LONG_2ADDR: {
2416 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong, dex_pc);
2417 break;
2418 }
2419
2420 case Instruction::SHR_INT_2ADDR: {
2421 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt, dex_pc);
2422 break;
2423 }
2424
2425 case Instruction::SHR_LONG_2ADDR: {
2426 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong, dex_pc);
2427 break;
2428 }
2429
2430 case Instruction::USHR_INT_2ADDR: {
2431 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt, dex_pc);
2432 break;
2433 }
2434
2435 case Instruction::USHR_LONG_2ADDR: {
2436 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong, dex_pc);
2437 break;
2438 }
2439
2440 case Instruction::DIV_FLOAT_2ADDR: {
2441 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc);
2442 break;
2443 }
2444
2445 case Instruction::DIV_DOUBLE_2ADDR: {
2446 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc);
2447 break;
2448 }
2449
2450 case Instruction::AND_INT_2ADDR: {
2451 Binop_12x<HAnd>(instruction, Primitive::kPrimInt, dex_pc);
2452 break;
2453 }
2454
2455 case Instruction::AND_LONG_2ADDR: {
2456 Binop_12x<HAnd>(instruction, Primitive::kPrimLong, dex_pc);
2457 break;
2458 }
2459
2460 case Instruction::OR_INT_2ADDR: {
2461 Binop_12x<HOr>(instruction, Primitive::kPrimInt, dex_pc);
2462 break;
2463 }
2464
2465 case Instruction::OR_LONG_2ADDR: {
2466 Binop_12x<HOr>(instruction, Primitive::kPrimLong, dex_pc);
2467 break;
2468 }
2469
2470 case Instruction::XOR_INT_2ADDR: {
2471 Binop_12x<HXor>(instruction, Primitive::kPrimInt, dex_pc);
2472 break;
2473 }
2474
2475 case Instruction::XOR_LONG_2ADDR: {
2476 Binop_12x<HXor>(instruction, Primitive::kPrimLong, dex_pc);
2477 break;
2478 }
2479
2480 case Instruction::ADD_INT_LIT16: {
2481 Binop_22s<HAdd>(instruction, false, dex_pc);
2482 break;
2483 }
2484
2485 case Instruction::AND_INT_LIT16: {
2486 Binop_22s<HAnd>(instruction, false, dex_pc);
2487 break;
2488 }
2489
2490 case Instruction::OR_INT_LIT16: {
2491 Binop_22s<HOr>(instruction, false, dex_pc);
2492 break;
2493 }
2494
2495 case Instruction::XOR_INT_LIT16: {
2496 Binop_22s<HXor>(instruction, false, dex_pc);
2497 break;
2498 }
2499
2500 case Instruction::RSUB_INT: {
2501 Binop_22s<HSub>(instruction, true, dex_pc);
2502 break;
2503 }
2504
2505 case Instruction::MUL_INT_LIT16: {
2506 Binop_22s<HMul>(instruction, false, dex_pc);
2507 break;
2508 }
2509
2510 case Instruction::ADD_INT_LIT8: {
2511 Binop_22b<HAdd>(instruction, false, dex_pc);
2512 break;
2513 }
2514
2515 case Instruction::AND_INT_LIT8: {
2516 Binop_22b<HAnd>(instruction, false, dex_pc);
2517 break;
2518 }
2519
2520 case Instruction::OR_INT_LIT8: {
2521 Binop_22b<HOr>(instruction, false, dex_pc);
2522 break;
2523 }
2524
2525 case Instruction::XOR_INT_LIT8: {
2526 Binop_22b<HXor>(instruction, false, dex_pc);
2527 break;
2528 }
2529
2530 case Instruction::RSUB_INT_LIT8: {
2531 Binop_22b<HSub>(instruction, true, dex_pc);
2532 break;
2533 }
2534
2535 case Instruction::MUL_INT_LIT8: {
2536 Binop_22b<HMul>(instruction, false, dex_pc);
2537 break;
2538 }
2539
2540 case Instruction::DIV_INT_LIT16:
2541 case Instruction::DIV_INT_LIT8: {
2542 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2543 dex_pc, Primitive::kPrimInt, true, true);
2544 break;
2545 }
2546
2547 case Instruction::REM_INT_LIT16:
2548 case Instruction::REM_INT_LIT8: {
2549 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
2550 dex_pc, Primitive::kPrimInt, true, false);
2551 break;
2552 }
2553
2554 case Instruction::SHL_INT_LIT8: {
2555 Binop_22b<HShl>(instruction, false, dex_pc);
2556 break;
2557 }
2558
2559 case Instruction::SHR_INT_LIT8: {
2560 Binop_22b<HShr>(instruction, false, dex_pc);
2561 break;
2562 }
2563
2564 case Instruction::USHR_INT_LIT8: {
2565 Binop_22b<HUShr>(instruction, false, dex_pc);
2566 break;
2567 }
2568
2569 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002570 HNewInstance* new_instance =
2571 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2572 DCHECK(new_instance != nullptr);
2573
David Brazdildee58d62016-04-07 09:54:26 +00002574 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002575 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002576 break;
2577 }
2578
2579 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002580 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002581 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002582 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002583
2584 HNewArray* new_array = new (arena_) HNewArray(cls, length, dex_pc);
2585 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002586 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002587 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002588 break;
2589 }
2590
2591 case Instruction::FILLED_NEW_ARRAY: {
2592 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002593 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002594 uint32_t args[5];
2595 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002596 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2597 type_index,
2598 number_of_vreg_arguments,
2599 /* is_range */ false,
2600 args,
2601 /* register_index */ 0);
2602 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002603 break;
2604 }
2605
2606 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2607 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002608 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002609 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002610 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2611 type_index,
2612 number_of_vreg_arguments,
2613 /* is_range */ true,
2614 /* args*/ nullptr,
2615 register_index);
2616 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002617 break;
2618 }
2619
2620 case Instruction::FILL_ARRAY_DATA: {
2621 BuildFillArrayData(instruction, dex_pc);
2622 break;
2623 }
2624
2625 case Instruction::MOVE_RESULT:
2626 case Instruction::MOVE_RESULT_WIDE:
2627 case Instruction::MOVE_RESULT_OBJECT: {
2628 DCHECK(latest_result_ != nullptr);
2629 UpdateLocal(instruction.VRegA(), latest_result_);
2630 latest_result_ = nullptr;
2631 break;
2632 }
2633
2634 case Instruction::CMP_LONG: {
2635 Binop_23x_cmp(instruction, Primitive::kPrimLong, ComparisonBias::kNoBias, dex_pc);
2636 break;
2637 }
2638
2639 case Instruction::CMPG_FLOAT: {
2640 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kGtBias, dex_pc);
2641 break;
2642 }
2643
2644 case Instruction::CMPG_DOUBLE: {
2645 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kGtBias, dex_pc);
2646 break;
2647 }
2648
2649 case Instruction::CMPL_FLOAT: {
2650 Binop_23x_cmp(instruction, Primitive::kPrimFloat, ComparisonBias::kLtBias, dex_pc);
2651 break;
2652 }
2653
2654 case Instruction::CMPL_DOUBLE: {
2655 Binop_23x_cmp(instruction, Primitive::kPrimDouble, ComparisonBias::kLtBias, dex_pc);
2656 break;
2657 }
2658
2659 case Instruction::NOP:
2660 break;
2661
2662 case Instruction::IGET:
2663 case Instruction::IGET_QUICK:
2664 case Instruction::IGET_WIDE:
2665 case Instruction::IGET_WIDE_QUICK:
2666 case Instruction::IGET_OBJECT:
2667 case Instruction::IGET_OBJECT_QUICK:
2668 case Instruction::IGET_BOOLEAN:
2669 case Instruction::IGET_BOOLEAN_QUICK:
2670 case Instruction::IGET_BYTE:
2671 case Instruction::IGET_BYTE_QUICK:
2672 case Instruction::IGET_CHAR:
2673 case Instruction::IGET_CHAR_QUICK:
2674 case Instruction::IGET_SHORT:
2675 case Instruction::IGET_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002676 if (!BuildInstanceFieldAccess(instruction, dex_pc, false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002677 return false;
2678 }
2679 break;
2680 }
2681
2682 case Instruction::IPUT:
2683 case Instruction::IPUT_QUICK:
2684 case Instruction::IPUT_WIDE:
2685 case Instruction::IPUT_WIDE_QUICK:
2686 case Instruction::IPUT_OBJECT:
2687 case Instruction::IPUT_OBJECT_QUICK:
2688 case Instruction::IPUT_BOOLEAN:
2689 case Instruction::IPUT_BOOLEAN_QUICK:
2690 case Instruction::IPUT_BYTE:
2691 case Instruction::IPUT_BYTE_QUICK:
2692 case Instruction::IPUT_CHAR:
2693 case Instruction::IPUT_CHAR_QUICK:
2694 case Instruction::IPUT_SHORT:
2695 case Instruction::IPUT_SHORT_QUICK: {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002696 if (!BuildInstanceFieldAccess(instruction, dex_pc, true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002697 return false;
2698 }
2699 break;
2700 }
2701
2702 case Instruction::SGET:
2703 case Instruction::SGET_WIDE:
2704 case Instruction::SGET_OBJECT:
2705 case Instruction::SGET_BOOLEAN:
2706 case Instruction::SGET_BYTE:
2707 case Instruction::SGET_CHAR:
2708 case Instruction::SGET_SHORT: {
2709 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) {
2710 return false;
2711 }
2712 break;
2713 }
2714
2715 case Instruction::SPUT:
2716 case Instruction::SPUT_WIDE:
2717 case Instruction::SPUT_OBJECT:
2718 case Instruction::SPUT_BOOLEAN:
2719 case Instruction::SPUT_BYTE:
2720 case Instruction::SPUT_CHAR:
2721 case Instruction::SPUT_SHORT: {
2722 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) {
2723 return false;
2724 }
2725 break;
2726 }
2727
2728#define ARRAY_XX(kind, anticipated_type) \
2729 case Instruction::AGET##kind: { \
2730 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2731 break; \
2732 } \
2733 case Instruction::APUT##kind: { \
2734 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2735 break; \
2736 }
2737
2738 ARRAY_XX(, Primitive::kPrimInt);
2739 ARRAY_XX(_WIDE, Primitive::kPrimLong);
2740 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
2741 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
2742 ARRAY_XX(_BYTE, Primitive::kPrimByte);
2743 ARRAY_XX(_CHAR, Primitive::kPrimChar);
2744 ARRAY_XX(_SHORT, Primitive::kPrimShort);
2745
2746 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002747 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002748 AppendInstruction(new (arena_) HArrayLength(object, dex_pc));
2749 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2750 break;
2751 }
2752
2753 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002754 dex::StringIndex string_index(instruction.VRegB_21c());
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_21c(), current_block_->GetLastInstruction());
2758 break;
2759 }
2760
2761 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002762 dex::StringIndex string_index(instruction.VRegB_31c());
David Brazdildee58d62016-04-07 09:54:26 +00002763 AppendInstruction(
2764 new (arena_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc));
2765 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2766 break;
2767 }
2768
2769 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002770 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002771 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002772 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2773 break;
2774 }
2775
2776 case Instruction::MOVE_EXCEPTION: {
2777 AppendInstruction(new (arena_) HLoadException(dex_pc));
2778 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
2779 AppendInstruction(new (arena_) HClearException(dex_pc));
2780 break;
2781 }
2782
2783 case Instruction::THROW: {
2784 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot);
2785 AppendInstruction(new (arena_) HThrow(exception, dex_pc));
2786 // We finished building this block. Set the current block to null to avoid
2787 // adding dead instructions to it.
2788 current_block_ = nullptr;
2789 break;
2790 }
2791
2792 case Instruction::INSTANCE_OF: {
2793 uint8_t destination = instruction.VRegA_22c();
2794 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002795 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002796 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2797 break;
2798 }
2799
2800 case Instruction::CHECK_CAST: {
2801 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002802 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002803 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2804 break;
2805 }
2806
2807 case Instruction::MONITOR_ENTER: {
2808 AppendInstruction(new (arena_) HMonitorOperation(
2809 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2810 HMonitorOperation::OperationKind::kEnter,
2811 dex_pc));
2812 break;
2813 }
2814
2815 case Instruction::MONITOR_EXIT: {
2816 AppendInstruction(new (arena_) HMonitorOperation(
2817 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
2818 HMonitorOperation::OperationKind::kExit,
2819 dex_pc));
2820 break;
2821 }
2822
2823 case Instruction::SPARSE_SWITCH:
2824 case Instruction::PACKED_SWITCH: {
2825 BuildSwitch(instruction, dex_pc);
2826 break;
2827 }
2828
2829 default:
2830 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002831 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002832 << " because of unhandled instruction "
2833 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002834 MaybeRecordStat(compilation_stats_,
2835 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002836 return false;
2837 }
2838 return true;
2839} // NOLINT(readability/fn_size)
2840
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002841ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2842 dex::TypeIndex type_index,
2843 const DexCompilationUnit& compilation_unit) const {
2844 return ClassLinker::LookupResolvedType(
2845 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2846}
2847
2848ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2849 // TODO: Cache the result in a Handle<mirror::Class>.
2850 const DexFile::MethodId& method_id =
2851 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2852 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2853}
2854
David Brazdildee58d62016-04-07 09:54:26 +00002855} // namespace art