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