blob: b576f8399d5ded33102b20b3eff407c970aa4b7e [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"
Vladimir Marko69d310e2017-10-09 14:12:23 +010020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
22#include "block_builder.h"
David Brazdildee58d62016-04-07 09:54:26 +000023#include "class_linker.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010024#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070025#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010027#include "driver/compiler_driver-inl.h"
28#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000029#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070030#include "imtable-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010031#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000032#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070034#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010037#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070038#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000039
40namespace art {
41
Mathieu Chartier808c7a52017-12-15 11:19:33 -080042HInstructionBuilder::HInstructionBuilder(HGraph* graph,
43 HBasicBlockBuilder* block_builder,
44 SsaBuilder* ssa_builder,
45 const DexFile* dex_file,
46 const CodeItemDebugInfoAccessor& accessor,
47 DataType::Type return_type,
48 const DexCompilationUnit* dex_compilation_unit,
49 const DexCompilationUnit* outer_compilation_unit,
50 CompilerDriver* compiler_driver,
51 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080052 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080053 OptimizingCompilerStats* compiler_stats,
54 VariableSizedHandleScope* handles,
55 ScopedArenaAllocator* local_allocator)
56 : allocator_(graph->GetAllocator()),
57 graph_(graph),
58 handles_(handles),
59 dex_file_(dex_file),
60 code_item_accessor_(accessor),
61 return_type_(return_type),
62 block_builder_(block_builder),
63 ssa_builder_(ssa_builder),
64 compiler_driver_(compiler_driver),
65 code_generator_(code_generator),
66 dex_compilation_unit_(dex_compilation_unit),
67 outer_compilation_unit_(outer_compilation_unit),
68 quicken_info_(interpreter_metadata),
69 compilation_stats_(compiler_stats),
70 local_allocator_(local_allocator),
71 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
72 current_block_(nullptr),
73 current_locals_(nullptr),
74 latest_result_(nullptr),
75 current_this_parameter_(nullptr),
76 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
77 loop_headers_.reserve(kDefaultNumberOfLoops);
78}
79
David Brazdildee58d62016-04-07 09:54:26 +000080HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
81 return block_builder_->GetBlockAt(dex_pc);
82}
83
Vladimir Marko69d310e2017-10-09 14:12:23 +010084inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
85 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000086 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080087 if (locals->size() == vregs) {
88 return locals;
89 }
90 return GetLocalsForWithAllocation(block, locals, vregs);
91}
David Brazdildee58d62016-04-07 09:54:26 +000092
Vladimir Marko69d310e2017-10-09 14:12:23 +010093ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080094 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010095 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080096 const size_t vregs) {
97 DCHECK_NE(locals->size(), vregs);
98 locals->resize(vregs, nullptr);
99 if (block->IsCatchBlock()) {
100 // We record incoming inputs of catch phis at throwing instructions and
101 // must therefore eagerly create the phis. Phis for undefined vregs will
102 // be deleted when the first throwing instruction with the vreg undefined
103 // is encountered. Unused phis will be removed by dead phi analysis.
104 for (size_t i = 0; i < vregs; ++i) {
105 // No point in creating the catch phi if it is already undefined at
106 // the first throwing instruction.
107 HInstruction* current_local_value = (*current_locals_)[i];
108 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109 HPhi* phi = new (allocator_) HPhi(
110 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800111 i,
112 0,
113 current_local_value->GetType());
114 block->AddPhi(phi);
115 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000116 }
117 }
118 }
119 return locals;
120}
121
Mingyao Yang01b47b02017-02-03 12:09:57 -0800122inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100123 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000124 return (*locals)[local];
125}
126
127void HInstructionBuilder::InitializeBlockLocals() {
128 current_locals_ = GetLocalsFor(current_block_);
129
130 if (current_block_->IsCatchBlock()) {
131 // Catch phis were already created and inputs collected from throwing sites.
132 if (kIsDebugBuild) {
133 // Make sure there was at least one throwing instruction which initialized
134 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
135 // visited already (from HTryBoundary scoping and reverse post order).
136 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100137 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000138 if (current == current_block_) {
139 catch_block_visited = true;
140 } else if (current->IsTryBlock()) {
141 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
142 if (try_entry.HasExceptionHandler(*current_block_)) {
143 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
144 }
145 }
146 }
147 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
148 << "No instructions throwing into a live catch block.";
149 }
150 } else if (current_block_->IsLoopHeader()) {
151 // If the block is a loop header, we know we only have visited the pre header
152 // because we are visiting in reverse post order. We create phis for all initialized
153 // locals from the pre header. Their inputs will be populated at the end of
154 // the analysis.
155 for (size_t local = 0; local < current_locals_->size(); ++local) {
156 HInstruction* incoming =
157 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
158 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HPhi* phi = new (allocator_) HPhi(
160 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000161 local,
162 0,
163 incoming->GetType());
164 current_block_->AddPhi(phi);
165 (*current_locals_)[local] = phi;
166 }
167 }
168
169 // Save the loop header so that the last phase of the analysis knows which
170 // blocks need to be updated.
171 loop_headers_.push_back(current_block_);
172 } else if (current_block_->GetPredecessors().size() > 0) {
173 // All predecessors have already been visited because we are visiting in reverse post order.
174 // We merge the values of all locals, creating phis if those values differ.
175 for (size_t local = 0; local < current_locals_->size(); ++local) {
176 bool one_predecessor_has_no_value = false;
177 bool is_different = false;
178 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
179
180 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
181 HInstruction* current = ValueOfLocalAt(predecessor, local);
182 if (current == nullptr) {
183 one_predecessor_has_no_value = true;
184 break;
185 } else if (current != value) {
186 is_different = true;
187 }
188 }
189
190 if (one_predecessor_has_no_value) {
191 // If one predecessor has no value for this local, we trust the verifier has
192 // successfully checked that there is a store dominating any read after this block.
193 continue;
194 }
195
196 if (is_different) {
197 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100198 HPhi* phi = new (allocator_) HPhi(
199 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000200 local,
201 current_block_->GetPredecessors().size(),
202 first_input->GetType());
203 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
204 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
205 phi->SetRawInputAt(i, pred_value);
206 }
207 current_block_->AddPhi(phi);
208 value = phi;
209 }
210 (*current_locals_)[local] = value;
211 }
212 }
213}
214
215void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
216 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
217 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100218 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000219 DCHECK_EQ(handler_locals->size(), current_locals_->size());
220 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
221 HInstruction* handler_value = (*handler_locals)[vreg];
222 if (handler_value == nullptr) {
223 // Vreg was undefined at a previously encountered throwing instruction
224 // and the catch phi was deleted. Do not record the local value.
225 continue;
226 }
227 DCHECK(handler_value->IsPhi());
228
229 HInstruction* local_value = (*current_locals_)[vreg];
230 if (local_value == nullptr) {
231 // This is the first instruction throwing into `catch_block` where
232 // `vreg` is undefined. Delete the catch phi.
233 catch_block->RemovePhi(handler_value->AsPhi());
234 (*handler_locals)[vreg] = nullptr;
235 } else {
236 // Vreg has been defined at all instructions throwing into `catch_block`
237 // encountered so far. Record the local value in the catch phi.
238 handler_value->AsPhi()->AddInput(local_value);
239 }
240 }
241 }
242}
243
244void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
245 current_block_->AddInstruction(instruction);
246 InitializeInstruction(instruction);
247}
248
249void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
250 if (current_block_->GetInstructions().IsEmpty()) {
251 current_block_->AddInstruction(instruction);
252 } else {
253 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
254 }
255 InitializeInstruction(instruction);
256}
257
258void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
259 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100260 HEnvironment* environment = new (allocator_) HEnvironment(
261 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000262 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000263 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000264 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000265 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100266 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000267 instruction->SetRawEnvironment(environment);
268 }
269}
270
David Brazdilc120bbe2016-04-22 16:57:00 +0100271HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100272 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100273 if (!ref->CanBeNull()) {
274 return ref;
275 }
276
Vladimir Markoca6fff82017-10-03 14:49:14 +0100277 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100278 AppendInstruction(null_check);
279 return null_check;
280}
281
David Brazdildee58d62016-04-07 09:54:26 +0000282void HInstructionBuilder::SetLoopHeaderPhiInputs() {
283 for (size_t i = loop_headers_.size(); i > 0; --i) {
284 HBasicBlock* block = loop_headers_[i - 1];
285 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
286 HPhi* phi = it.Current()->AsPhi();
287 size_t vreg = phi->GetRegNumber();
288 for (HBasicBlock* predecessor : block->GetPredecessors()) {
289 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
290 if (value == nullptr) {
291 // Vreg is undefined at this predecessor. Mark it dead and leave with
292 // fewer inputs than predecessors. SsaChecker will fail if not removed.
293 phi->SetDead();
294 break;
295 } else {
296 phi->AddInput(value);
297 }
298 }
299 }
300 }
301}
302
303static bool IsBlockPopulated(HBasicBlock* block) {
304 if (block->IsLoopHeader()) {
305 // Suspend checks were inserted into loop headers during building of dominator tree.
306 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
307 return block->GetFirstInstruction() != block->GetLastInstruction();
308 } else {
309 return !block->GetInstructions().IsEmpty();
310 }
311}
312
313bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800314 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100315 locals_for_.resize(
316 graph_->GetBlocks().size(),
317 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000318
319 // Find locations where we want to generate extra stackmaps for native debugging.
320 // This allows us to generate the info only at interesting points (for example,
321 // at start of java statement) rather than before every dex instruction.
322 const bool native_debuggable = compiler_driver_ != nullptr &&
323 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
324 ArenaBitVector* native_debug_info_locations = nullptr;
325 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100326 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000327 }
328
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100329 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
330 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000331 uint32_t block_dex_pc = current_block_->GetDexPc();
332
333 InitializeBlockLocals();
334
335 if (current_block_->IsEntryBlock()) {
336 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100337 AppendInstruction(new (allocator_) HSuspendCheck(0u));
338 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000339 continue;
340 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100341 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000342 continue;
343 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000345 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
346 // This is slightly odd because the loop header might not be empty (TryBoundary).
347 // But we're still creating the environment with locals from the top of the block.
348 InsertInstructionAtTop(suspend_check);
349 }
350
351 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
352 // Synthetic block that does not need to be populated.
353 DCHECK(IsBlockPopulated(current_block_));
354 continue;
355 }
356
357 DCHECK(!IsBlockPopulated(current_block_));
358
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700359 uint32_t quicken_index = 0;
360 if (CanDecodeQuickenedInfo()) {
361 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
362 }
363
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800364 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000365 if (current_block_ == nullptr) {
366 // The previous instruction ended this block.
367 break;
368 }
369
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800370 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000371 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
372 // This dex_pc starts a new basic block.
373 break;
374 }
375
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800376 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000377 PropagateLocalsToCatchBlocks();
378 }
379
380 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100381 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000382 }
383
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800384 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000385 return false;
386 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700387
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800388 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700389 ++quicken_index;
390 }
David Brazdildee58d62016-04-07 09:54:26 +0000391 }
392
393 if (current_block_ != nullptr) {
394 // Branching instructions clear current_block, so we know the last
395 // instruction of the current block is not a branching instruction.
396 // We add an unconditional Goto to the next block.
397 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100398 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000399 }
400 }
401
402 SetLoopHeaderPhiInputs();
403
404 return true;
405}
406
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000407void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800408 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000409 DCHECK(method->IsIntrinsic());
410
411 locals_for_.resize(
412 graph_->GetBlocks().size(),
413 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
414
415 // Fill the entry block. Do not add suspend check, we do not want a suspend
416 // check in intrinsics; intrinsic methods are supposed to be fast.
417 current_block_ = graph_->GetEntryBlock();
418 InitializeBlockLocals();
419 InitializeParameters();
420 AppendInstruction(new (allocator_) HGoto(0u));
421
422 // Fill the body.
423 current_block_ = current_block_->GetSingleSuccessor();
424 InitializeBlockLocals();
425 DCHECK(!IsBlockPopulated(current_block_));
426
427 // Add the invoke and return instruction. Use HInvokeStaticOrDirect even
428 // for methods that would normally use an HInvokeVirtual (sharpen the call).
429 size_t in_vregs = graph_->GetNumberOfInVRegs();
430 size_t number_of_arguments =
431 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
432 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
433 MethodReference target_method(dex_file_, method_idx);
434 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
435 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
436 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
437 /* method_load_data */ 0u
438 };
439 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
440 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
441 allocator_,
442 number_of_arguments,
443 return_type_,
444 kNoDexPc,
445 method_idx,
446 method,
447 dispatch_info,
448 invoke_type,
449 target_method,
450 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000451 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100452 HandleInvoke(invoke, operands, dex_file_->GetMethodShorty(method_idx), /* is_unresolved */ false);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000453
454 // Add the return instruction.
455 if (return_type_ == DataType::Type::kVoid) {
456 AppendInstruction(new (allocator_) HReturnVoid());
457 } else {
458 AppendInstruction(new (allocator_) HReturn(invoke));
459 }
460
461 // Fill the exit block.
462 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
463 current_block_ = graph_->GetExitBlock();
464 InitializeBlockLocals();
465 AppendInstruction(new (allocator_) HExit());
466}
467
Vladimir Marko69d310e2017-10-09 14:12:23 +0100468ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100469 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800470 code_item_accessor_.InsnsSizeInCodeUnits(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100471 /* expandable */ false,
472 kArenaAllocGraphBuilder);
473 locations->ClearAllBits();
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700474 // The visitor gets called when the line number changes.
475 // In other words, it marks the start of new java statement.
476 code_item_accessor_.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
477 locations->SetBit(entry.address_);
478 return false;
479 });
David Brazdildee58d62016-04-07 09:54:26 +0000480 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800481 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800482 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000483 case Instruction::MOVE_EXCEPTION: {
484 // Stop in native debugger after the exception has been moved.
485 // The compiler also expects the move at the start of basic block so
486 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800487 locations->ClearBit(inst.DexPc());
488 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
489 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800490 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700491 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000492 }
493 break;
494 }
495 default:
496 break;
497 }
498 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100499 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000500}
501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000503 HInstruction* value = (*current_locals_)[reg_number];
504 DCHECK(value != nullptr);
505
506 // If the operation requests a specific type, we make sure its input is of that type.
507 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100508 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700509 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100510 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700511 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000512 }
Aart Bik31883642016-06-06 15:02:44 -0700513 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000514 }
515
516 return value;
517}
518
519void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100520 DataType::Type stored_type = stored_value->GetType();
521 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000522
523 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
524 // registers. Consider the following cases:
525 // (1) Storing a wide value must overwrite previous values in both `reg_number`
526 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
527 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
528 // must invalidate it. We store `nullptr` in `reg_number-1`.
529 // Consequently, storing a wide value into the high vreg of another wide value
530 // will invalidate both `reg_number-1` and `reg_number+1`.
531
532 if (reg_number != 0) {
533 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100534 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000535 // The vreg we are storing into was previously the high vreg of a pair.
536 // We need to invalidate its low vreg.
537 DCHECK((*current_locals_)[reg_number] == nullptr);
538 (*current_locals_)[reg_number - 1] = nullptr;
539 }
540 }
541
542 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100543 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000544 // We are storing a pair. Invalidate the instruction in the high vreg.
545 (*current_locals_)[reg_number + 1] = nullptr;
546 }
547}
548
549void HInstructionBuilder::InitializeParameters() {
550 DCHECK(current_block_->IsEntryBlock());
551
Vladimir Marko69d310e2017-10-09 14:12:23 +0100552 // outer_compilation_unit_ is null only when unit testing.
553 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000554 return;
555 }
556
557 const char* shorty = dex_compilation_unit_->GetShorty();
558 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
559 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
560 uint16_t parameter_index = 0;
561
562 const DexFile::MethodId& referrer_method_id =
563 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
564 if (!dex_compilation_unit_->IsStatic()) {
565 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100566 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000567 referrer_method_id.class_idx_,
568 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100569 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700570 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000571 AppendInstruction(parameter);
572 UpdateLocal(locals_index++, parameter);
573 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700574 current_this_parameter_ = parameter;
575 } else {
576 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000577 }
578
579 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
580 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
581 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100582 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000583 *dex_file_,
584 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
585 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100586 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700587 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000588 ++shorty_pos;
589 AppendInstruction(parameter);
590 // Store the parameter value in the local that the dex code will use
591 // to reference that parameter.
592 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100593 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000594 i++;
595 locals_index++;
596 parameter_index++;
597 }
598 }
599}
600
601template<typename T>
602void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100603 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
604 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100605 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000606 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100607 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000608 current_block_ = nullptr;
609}
610
611template<typename T>
612void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100613 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100614 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000615 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000617 current_block_ = nullptr;
618}
619
620template<typename T>
621void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100622 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000623 uint32_t dex_pc) {
624 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100625 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000626 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
627}
628
629void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100630 DataType::Type input_type,
631 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000632 uint32_t dex_pc) {
633 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100634 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000635 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
636}
637
638template<typename T>
639void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100640 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000641 uint32_t dex_pc) {
642 HInstruction* first = LoadLocal(instruction.VRegB(), type);
643 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100644 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000645 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
646}
647
648template<typename T>
649void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100650 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000651 uint32_t dex_pc) {
652 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100653 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100654 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000655 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
656}
657
658void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100659 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000660 ComparisonBias bias,
661 uint32_t dex_pc) {
662 HInstruction* first = LoadLocal(instruction.VRegB(), type);
663 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100664 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000665 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
666}
667
668template<typename T>
669void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100670 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000671 uint32_t dex_pc) {
672 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100673 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100674 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000675 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
676}
677
678template<typename T>
679void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100680 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000681 uint32_t dex_pc) {
682 HInstruction* first = LoadLocal(instruction.VRegA(), type);
683 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100684 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000685 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
686}
687
688template<typename T>
689void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100690 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000691 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
692 if (reverse) {
693 std::swap(first, second);
694 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000696 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
697}
698
699template<typename T>
700void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100701 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000702 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
703 if (reverse) {
704 std::swap(first, second);
705 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100706 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000707 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
708}
709
Igor Murashkind01745e2017-04-05 16:40:31 -0700710// Does the method being compiled need any constructor barriers being inserted?
711// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700712static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700713 // Can be null in unit tests only.
714 if (UNLIKELY(cu == nullptr)) {
715 return false;
716 }
717
David Brazdildee58d62016-04-07 09:54:26 +0000718 Thread* self = Thread::Current();
719 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700720 && !cu->IsStatic()
721 // RequiresConstructorBarrier must only be queried for <init> methods;
722 // it's effectively "false" for every other method.
723 //
724 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700725 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000726}
727
728// Returns true if `block` has only one successor which starts at the next
729// dex_pc after `instruction` at `dex_pc`.
730static bool IsFallthroughInstruction(const Instruction& instruction,
731 uint32_t dex_pc,
732 HBasicBlock* block) {
733 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
734 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
735}
736
737void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100738 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000739 DexSwitchTable table(instruction, dex_pc);
740
741 if (table.GetNumEntries() == 0) {
742 // Empty Switch. Code falls through to the next block.
743 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100744 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000745 } else if (table.ShouldBuildDecisionTree()) {
746 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
747 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100748 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000749 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100750 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000751
752 if (!it.IsLast()) {
753 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
754 }
755 }
756 } else {
757 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100758 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000759 }
760
761 current_block_ = nullptr;
762}
763
764void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100765 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000766 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100767 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700768 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700769 // This may insert additional redundant constructor fences from the super constructors.
770 // TODO: remove redundant constructor fences (b/36656456).
771 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700772 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100773 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700774
775 HInstruction* fence_target = current_this_parameter_;
776 DCHECK(fence_target != nullptr);
777
Vladimir Markoca6fff82017-10-03 14:49:14 +0100778 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700779 MaybeRecordStat(
780 compilation_stats_,
781 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000782 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100783 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000784 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700785 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000786 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100787 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000788 }
789 current_block_ = nullptr;
790}
791
792static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
793 switch (opcode) {
794 case Instruction::INVOKE_STATIC:
795 case Instruction::INVOKE_STATIC_RANGE:
796 return kStatic;
797 case Instruction::INVOKE_DIRECT:
798 case Instruction::INVOKE_DIRECT_RANGE:
799 return kDirect;
800 case Instruction::INVOKE_VIRTUAL:
801 case Instruction::INVOKE_VIRTUAL_QUICK:
802 case Instruction::INVOKE_VIRTUAL_RANGE:
803 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
804 return kVirtual;
805 case Instruction::INVOKE_INTERFACE:
806 case Instruction::INVOKE_INTERFACE_RANGE:
807 return kInterface;
808 case Instruction::INVOKE_SUPER_RANGE:
809 case Instruction::INVOKE_SUPER:
810 return kSuper;
811 default:
812 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
813 UNREACHABLE();
814 }
815}
816
817ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
818 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000819
820 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000821 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100822
Vladimir Markoba118822017-06-12 15:41:56 +0100823 ArtMethod* resolved_method =
824 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100825 method_idx,
826 dex_compilation_unit_->GetDexCache(),
827 class_loader,
828 graph_->GetArtMethod(),
829 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000830
831 if (UNLIKELY(resolved_method == nullptr)) {
832 // Clean up any exception left by type resolution.
833 soa.Self()->ClearException();
834 return nullptr;
835 }
836
Vladimir Markoba118822017-06-12 15:41:56 +0100837 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
838 // resolved because, for example, we don't find a superclass in the classpath.
839 if (graph_->GetArtMethod() == nullptr) {
840 // The class linker cannot check access without a referrer, so we have to do it.
841 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000842 if (!resolved_method->IsPublic()) {
843 return nullptr;
844 }
David Brazdildee58d62016-04-07 09:54:26 +0000845 }
846
847 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
848 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
849 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
850 // which require runtime handling.
851 if (invoke_type == kSuper) {
Vladimir Markofca0b492018-07-23 15:30:52 +0100852 ObjPtr<mirror::Class> compiling_class = ResolveCompilingClass(soa);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800853 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000854 // We could not determine the method's class we need to wait until runtime.
855 DCHECK(Runtime::Current()->IsAotCompiler());
856 return nullptr;
857 }
Vladimir Markoba118822017-06-12 15:41:56 +0100858 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100859 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
860 dex_compilation_unit_->GetDexCache().Get(),
861 class_loader.Get());
862 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
863 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700864 // We cannot statically determine the target method. The runtime will throw a
865 // NoSuchMethodError on this one.
866 return nullptr;
867 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100868 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100869 if (referenced_class->IsInterface()) {
870 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100871 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000872 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100873 uint16_t vtable_index = resolved_method->GetMethodIndex();
874 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
875 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000876 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100877 if (actual_method != resolved_method &&
878 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
879 // The back-end code generator relies on this check in order to ensure that it will not
880 // attempt to read the dex_cache with a dex_method_index that is not from the correct
881 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
882 // builder, which means that the code-generator (and compiler driver during sharpening and
883 // inliner, maybe) might invoke an incorrect method.
884 // TODO: The actual method could still be referenced in the current dex file, so we
885 // could try locating it.
886 // TODO: Remove the dex_file restriction.
887 return nullptr;
888 }
889 if (!actual_method->IsInvokable()) {
890 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
891 // could resolve the callee to the wrong method.
892 return nullptr;
893 }
894 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000895 }
896
David Brazdildee58d62016-04-07 09:54:26 +0000897 return resolved_method;
898}
899
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100900static bool IsStringConstructor(ArtMethod* method) {
901 ScopedObjectAccess soa(Thread::Current());
902 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
903}
904
David Brazdildee58d62016-04-07 09:54:26 +0000905bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
906 uint32_t dex_pc,
907 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000908 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000909 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100910 const char* shorty = dex_file_->GetMethodShorty(method_idx);
911 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000912
913 // Remove the return type from the 'proto'.
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100914 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +0000915 if (invoke_type != kStatic) { // instance call
916 // One extra argument for 'this'.
917 number_of_arguments++;
918 }
919
David Brazdildee58d62016-04-07 09:54:26 +0000920 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
921
922 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700923 MaybeRecordStat(compilation_stats_,
924 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100925 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
926 number_of_arguments,
927 return_type,
928 dex_pc,
929 method_idx,
930 invoke_type);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100931 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000932 }
933
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100934 // Replace calls to String.<init> with StringFactory.
935 if (IsStringConstructor(resolved_method)) {
936 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
937 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
938 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
939 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000940 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100941 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000942 ScopedObjectAccess soa(Thread::Current());
943 MethodReference target_method(resolved_method->GetDexFile(),
944 resolved_method->GetDexMethodIndex());
945 // We pass null for the resolved_method to ensure optimizations
946 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100947 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
948 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100949 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100950 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100951 dex_pc,
952 method_idx,
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000953 nullptr /* resolved_method */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100954 dispatch_info,
955 invoke_type,
956 target_method,
957 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100958 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100959 }
960
David Brazdildee58d62016-04-07 09:54:26 +0000961 // Potential class initialization check, in the case of a static method call.
962 HClinitCheck* clinit_check = nullptr;
963 HInvoke* invoke = nullptr;
964 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
965 // By default, consider that the called method implicitly requires
966 // an initialization check of its declaring method.
967 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
968 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
969 ScopedObjectAccess soa(Thread::Current());
970 if (invoke_type == kStatic) {
Vladimir Markofca0b492018-07-23 15:30:52 +0100971 clinit_check =
972 ProcessClinitCheckForInvoke(soa, dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000973 } else if (invoke_type == kSuper) {
974 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100975 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000976 // we resolved to the method referenced by the instruction.
977 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000978 }
979 }
980
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100981 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
982 HSharpening::SharpenInvokeStaticOrDirect(resolved_method, code_generator_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100983 MethodReference target_method(resolved_method->GetDexFile(),
984 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100985 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
986 number_of_arguments,
987 return_type,
988 dex_pc,
989 method_idx,
990 resolved_method,
991 dispatch_info,
992 invoke_type,
993 target_method,
994 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000995 } else if (invoke_type == kVirtual) {
996 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100997 invoke = new (allocator_) HInvokeVirtual(allocator_,
998 number_of_arguments,
999 return_type,
1000 dex_pc,
1001 method_idx,
1002 resolved_method,
1003 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +00001004 } else {
1005 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001006 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001007 invoke = new (allocator_) HInvokeInterface(allocator_,
1008 number_of_arguments,
1009 return_type,
1010 dex_pc,
1011 method_idx,
1012 resolved_method,
1013 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001014 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001015 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false, clinit_check);
David Brazdildee58d62016-04-07 09:54:26 +00001016}
1017
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001018bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001019 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001020 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001021 const InstructionOperands& operands) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001022 const char* shorty = dex_file_->GetShorty(proto_idx);
1023 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1024 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1025 size_t number_of_arguments = strlen(shorty);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001026 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1027 number_of_arguments,
1028 return_type,
1029 dex_pc,
1030 method_idx);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001031 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false);
1032}
1033
1034
1035bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1036 uint32_t call_site_idx,
1037 const InstructionOperands& operands) {
1038 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1039 const char* shorty = dex_file_->GetShorty(proto_idx);
1040 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1041 size_t number_of_arguments = strlen(shorty) - 1;
1042 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1043 number_of_arguments,
1044 call_site_idx,
1045 return_type,
1046 dex_pc);
1047 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001048}
1049
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001050HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001051 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001052
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001053 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001054
David Brazdildee58d62016-04-07 09:54:26 +00001055 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001056 Handle<mirror::Class> klass = load_class->GetClass();
1057
Vladimir Markofca0b492018-07-23 15:30:52 +01001058 if (!IsInitialized(soa, klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001059 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001060 AppendInstruction(cls);
1061 }
1062
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001063 // Only the access check entrypoint handles the finalizable class case. If we
1064 // need access checks, then we haven't resolved the method and the class may
1065 // again be finalizable.
1066 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1067 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1068 entrypoint = kQuickAllocObjectWithChecks;
1069 }
Alex Lightd109e302018-06-27 10:25:41 -07001070 // We will always be able to resolve the string class since it is in the BCP.
1071 if (!klass.IsNull() && klass->IsStringClass()) {
1072 entrypoint = kQuickAllocStringObject;
1073 }
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001074
1075 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001076 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001077
Vladimir Markoca6fff82017-10-03 14:49:14 +01001078 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001079 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001080 dex_pc,
1081 type_index,
1082 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001083 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001084 entrypoint);
1085 AppendInstruction(new_instance);
1086
1087 return new_instance;
1088}
1089
1090void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1091 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001092 (allocation->IsNewInstance() ||
1093 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001094
1095 if (allocation->IsNewInstance()) {
1096 // STRING SPECIAL HANDLING:
1097 // -------------------------------
1098 // Strings have a real HNewInstance node but they end up always having 0 uses.
1099 // All uses of a String HNewInstance are always transformed to replace their input
1100 // of the HNewInstance with an input of the invoke to StringFactory.
1101 //
1102 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1103 // optimizations (to pass checker tests that rely on those optimizations).
1104 HNewInstance* new_inst = allocation->AsNewInstance();
1105 HLoadClass* load_class = new_inst->GetLoadClass();
1106
1107 Thread* self = Thread::Current();
1108 ScopedObjectAccess soa(self);
1109 StackHandleScope<1> hs(self);
1110 Handle<mirror::Class> klass = load_class->GetClass();
1111 if (klass != nullptr && klass->IsStringClass()) {
1112 return;
1113 // Note: Do not use allocation->IsStringAlloc which requires
1114 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1115 // propagation (and instruction builder is too early).
1116 }
1117 // (In terms of correctness, the StringFactory needs to provide its own
1118 // default initialization barrier, see below.)
1119 }
1120
1121 // JLS 17.4.5 "Happens-before Order" describes:
1122 //
1123 // The default initialization of any object happens-before any other actions (other than
1124 // default-writes) of a program.
1125 //
1126 // In our implementation the default initialization of an object to type T means
1127 // setting all of its initial data (object[0..size)) to 0, and setting the
1128 // object's class header (i.e. object.getClass() == T.class).
1129 //
1130 // In practice this fence ensures that the writes to the object header
1131 // are visible to other threads if this object escapes the current thread.
1132 // (and in theory the 0-initializing, but that happens automatically
1133 // when new memory pages are mapped in by the OS).
1134 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001135 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001136 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001137 MaybeRecordStat(
1138 compilation_stats_,
1139 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001140}
1141
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001142static bool IsInBootImage(ObjPtr<mirror::Class> cls, const CompilerOptions& compiler_options)
1143 REQUIRES_SHARED(Locks::mutator_lock_) {
1144 if (compiler_options.IsBootImage()) {
1145 std::string temp;
1146 const char* descriptor = cls->GetDescriptor(&temp);
1147 return compiler_options.IsImageClass(descriptor);
1148 } else {
1149 return Runtime::Current()->GetHeap()->FindSpaceFromObject(cls, false)->IsImageSpace();
1150 }
1151}
1152
1153static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
1154 REQUIRES_SHARED(Locks::mutator_lock_) {
1155 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1156}
1157
1158static bool HasTrivialClinit(ObjPtr<mirror::Class> klass, PointerSize pointer_size)
1159 REQUIRES_SHARED(Locks::mutator_lock_) {
1160 // Check if the class has encoded fields that trigger bytecode execution.
1161 // (Encoded fields are just a different representation of <clinit>.)
1162 if (klass->NumStaticFields() != 0u) {
1163 DCHECK(klass->GetClassDef() != nullptr);
1164 EncodedStaticFieldValueIterator it(klass->GetDexFile(), *klass->GetClassDef());
1165 for (; it.HasNext(); it.Next()) {
1166 switch (it.GetValueType()) {
1167 case EncodedArrayValueIterator::ValueType::kBoolean:
1168 case EncodedArrayValueIterator::ValueType::kByte:
1169 case EncodedArrayValueIterator::ValueType::kShort:
1170 case EncodedArrayValueIterator::ValueType::kChar:
1171 case EncodedArrayValueIterator::ValueType::kInt:
1172 case EncodedArrayValueIterator::ValueType::kLong:
1173 case EncodedArrayValueIterator::ValueType::kFloat:
1174 case EncodedArrayValueIterator::ValueType::kDouble:
1175 case EncodedArrayValueIterator::ValueType::kNull:
1176 case EncodedArrayValueIterator::ValueType::kString:
1177 // Primitive, null or j.l.String initialization is permitted.
1178 break;
1179 case EncodedArrayValueIterator::ValueType::kType:
1180 // Type initialization can load classes and execute bytecode through a class loader
1181 // which can execute arbitrary bytecode. We do not optimize for known class loaders;
1182 // kType is rarely used (if ever).
1183 return false;
1184 default:
1185 // Other types in the encoded static field list are rejected by the DexFileVerifier.
1186 LOG(FATAL) << "Unexpected type " << it.GetValueType();
1187 UNREACHABLE();
1188 }
1189 }
1190 }
1191 // Check if the class has <clinit> that executes arbitrary code.
1192 // Initialization of static fields of the class itself with constants is allowed.
1193 ArtMethod* clinit = klass->FindClassInitializer(pointer_size);
1194 if (clinit != nullptr) {
1195 const DexFile& dex_file = *clinit->GetDexFile();
1196 CodeItemInstructionAccessor accessor(dex_file, clinit->GetCodeItem());
1197 for (DexInstructionPcPair it : accessor) {
1198 switch (it->Opcode()) {
1199 case Instruction::CONST_4:
1200 case Instruction::CONST_16:
1201 case Instruction::CONST:
1202 case Instruction::CONST_HIGH16:
1203 case Instruction::CONST_WIDE_16:
1204 case Instruction::CONST_WIDE_32:
1205 case Instruction::CONST_WIDE:
1206 case Instruction::CONST_WIDE_HIGH16:
1207 case Instruction::CONST_STRING:
1208 case Instruction::CONST_STRING_JUMBO:
1209 // Primitive, null or j.l.String initialization is permitted.
1210 break;
1211 case Instruction::RETURN_VOID:
1212 case Instruction::RETURN_VOID_NO_BARRIER:
1213 break;
1214 case Instruction::SPUT:
1215 case Instruction::SPUT_WIDE:
1216 case Instruction::SPUT_OBJECT:
1217 case Instruction::SPUT_BOOLEAN:
1218 case Instruction::SPUT_BYTE:
1219 case Instruction::SPUT_CHAR:
1220 case Instruction::SPUT_SHORT:
1221 // Only initialization of a static field of the same class is permitted.
1222 if (dex_file.GetFieldId(it->VRegB_21c()).class_idx_ != klass->GetDexTypeIndex()) {
1223 return false;
1224 }
1225 break;
1226 case Instruction::NEW_ARRAY:
1227 // Only primitive arrays are permitted.
1228 if (Primitive::GetType(dex_file.GetTypeDescriptor(dex_file.GetTypeId(
1229 dex::TypeIndex(it->VRegC_22c())))[1]) == Primitive::kPrimNot) {
1230 return false;
1231 }
1232 break;
1233 case Instruction::APUT:
1234 case Instruction::APUT_WIDE:
1235 case Instruction::APUT_BOOLEAN:
1236 case Instruction::APUT_BYTE:
1237 case Instruction::APUT_CHAR:
1238 case Instruction::APUT_SHORT:
1239 case Instruction::FILL_ARRAY_DATA:
1240 case Instruction::NOP:
1241 // Allow initialization of primitive arrays (only constants can be stored).
1242 // Note: We expect NOPs used for fill-array-data-payload but accept all NOPs
1243 // (even unreferenced switch payloads if they make it through the verifier).
1244 break;
1245 default:
1246 return false;
1247 }
1248 }
1249 }
1250 return true;
1251}
1252
1253static bool HasTrivialInitialization(ObjPtr<mirror::Class> cls,
1254 const CompilerOptions& compiler_options)
1255 REQUIRES_SHARED(Locks::mutator_lock_) {
1256 Runtime* runtime = Runtime::Current();
1257 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
1258
1259 // Check the superclass chain.
1260 for (ObjPtr<mirror::Class> klass = cls; klass != nullptr; klass = klass->GetSuperClass()) {
1261 if (klass->IsInitialized() && IsInBootImage(klass, compiler_options)) {
1262 break; // `klass` and its superclasses are already initialized in the boot image.
1263 }
1264 if (!HasTrivialClinit(klass, pointer_size)) {
1265 return false;
1266 }
1267 }
1268
1269 // Also check interfaces with default methods as they need to be initialized as well.
1270 ObjPtr<mirror::IfTable> iftable = cls->GetIfTable();
1271 DCHECK(iftable != nullptr);
1272 for (int32_t i = 0, count = iftable->Count(); i != count; ++i) {
1273 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
1274 if (!iface->HasDefaultMethods()) {
1275 continue; // Initializing `cls` does not initialize this interface.
1276 }
1277 if (iface->IsInitialized() && IsInBootImage(iface, compiler_options)) {
1278 continue; // This interface is already initialized in the boot image.
1279 }
1280 if (!HasTrivialClinit(iface, pointer_size)) {
1281 return false;
1282 }
1283 }
1284 return true;
1285}
1286
Vladimir Markofca0b492018-07-23 15:30:52 +01001287bool HInstructionBuilder::IsInitialized(ScopedObjectAccess& soa, Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001288 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001289 return false;
1290 }
1291
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001292 // Check if the class will be initialized at runtime.
1293 if (cls->IsInitialized()) {
1294 Runtime* runtime = Runtime::Current();
1295 if (!runtime->IsAotCompiler()) {
1296 DCHECK(runtime->UseJitCompilation());
1297 // For JIT, the class cannot revert to an uninitialized state.
1298 return true;
1299 }
1300 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1301 // loaded because we don't even know what class loader will be used to load them.
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001302 if (IsInBootImage(cls.Get(), compiler_driver_->GetCompilerOptions())) {
1303 return true;
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001304 }
1305 }
1306
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001307 // We can avoid the class initialization check for `cls` in static methods in the
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001308 // very same class. Instance methods of the same class can run on an escaped instance
1309 // of an erroneous class. Even a superclass may need to be checked as the subclass
1310 // can be completely initialized while the superclass is initializing and the subclass
1311 // remains initialized when the superclass initializer throws afterwards. b/62478025
1312 // Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001313 ObjPtr<mirror::Class> outermost_cls = ResolveOutermostCompilingClass(soa);
1314 bool is_static = (dex_compilation_unit_->GetAccessFlags() & kAccStatic) != 0u;
1315 if (is_static && outermost_cls == cls.Get()) {
1316 return true;
1317 }
1318 // Remember if the compiled class is a subclass of `cls`. By the time this is used
1319 // below the `outermost_cls` may be invalidated by calling ResolveCompilingClass().
1320 bool is_subclass = IsSubClass(outermost_cls, cls.Get());
1321 if (dex_compilation_unit_ != outer_compilation_unit_) {
1322 // Check also the innermost method. Though excessive copies of ClinitCheck can be
1323 // eliminated by GVN, that happens only after the decision whether to inline the
1324 // graph or not and that may depend on the presence of the ClinitCheck.
1325 // TODO: We should walk over the entire inlined method chain, but we don't pass that
1326 // information to the builder.
1327 ObjPtr<mirror::Class> innermost_cls = ResolveCompilingClass(soa);
1328 if (is_static && innermost_cls == cls.Get()) {
Vladimir Markofca0b492018-07-23 15:30:52 +01001329 return true;
1330 }
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001331 is_subclass = is_subclass || IsSubClass(innermost_cls, cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001332 }
1333
Vladimir Marko2ab1bdd2018-07-12 09:59:56 +01001334 // Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
1335 // compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
1336 // before calling any `cls` or subclass methods. Static methods require a clinit check and
1337 // instance methods require an instance which cannot be created before doing a clinit check.
1338 // When a subclass of `cls` starts initializing, it starts initializing its superclass
1339 // chain up to `cls` without running any bytecode, i.e. without any opportunity for circular
1340 // initialization weirdness.
1341 //
1342 // If the initialization of `cls` is trivial (`cls` and its superclasses and superinterfaces
1343 // with default methods initialize only their own static fields using constant values), it must
1344 // complete, either successfully or by throwing and marking `cls` erroneous, without allocating
1345 // any instances of `cls` or subclasses (or any other class) and without calling any methods.
1346 // If it completes by throwing, no instances of `cls` shall be created and no subclass method
1347 // bytecode shall execute (see above), therefore the instruction we're building shall be
1348 // unreachable. By reaching the instruction, we know that `cls` was initialized successfully.
1349 //
1350 // TODO: We should walk over the entire inlined methods chain, but we don't pass that
1351 // information to the builder. (We could also check if we're guaranteed a non-null instance
1352 // of `cls` at this location but that's outside the scope of the instruction builder.)
1353 if (is_subclass && HasTrivialInitialization(cls.Get(), compiler_driver_->GetCompilerOptions())) {
1354 return true;
1355 }
David Brazdildee58d62016-04-07 09:54:26 +00001356
1357 return false;
1358}
1359
1360HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
Vladimir Markofca0b492018-07-23 15:30:52 +01001361 ScopedObjectAccess& soa,
1362 uint32_t dex_pc,
1363 ArtMethod* resolved_method,
1364 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001365 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001366
1367 HClinitCheck* clinit_check = nullptr;
Vladimir Markofca0b492018-07-23 15:30:52 +01001368 if (IsInitialized(soa, klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001369 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001370 } else {
Vladimir Markofca0b492018-07-23 15:30:52 +01001371 HLoadClass* cls = BuildLoadClass(soa,
1372 klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001373 klass->GetDexFile(),
1374 klass,
1375 dex_pc,
1376 /* needs_access_check */ false);
1377 if (cls != nullptr) {
1378 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001379 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001380 AppendInstruction(clinit_check);
1381 }
David Brazdildee58d62016-04-07 09:54:26 +00001382 }
1383 return clinit_check;
1384}
1385
1386bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001387 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001388 const char* shorty,
David Brazdildee58d62016-04-07 09:54:26 +00001389 size_t start_index,
1390 size_t* argument_index) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001391 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001392 const size_t number_of_operands = operands.GetNumberOfOperands();
David Brazdildee58d62016-04-07 09:54:26 +00001393 for (size_t i = start_index;
1394 // Make sure we don't go over the expected arguments or over the number of
1395 // dex registers given. If the instruction was seen as dead by the verifier,
1396 // it hasn't been properly checked.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001397 (i < number_of_operands) && (*argument_index < invoke->GetNumberOfArguments());
David Brazdildee58d62016-04-07 09:54:26 +00001398 i++, (*argument_index)++) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001399 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001400 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001401 if (is_wide && ((i + 1 == number_of_operands) ||
1402 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
David Brazdildee58d62016-04-07 09:54:26 +00001403 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1404 // reject any class where this is violated. However, the verifier only does these checks
1405 // on non trivially dead instructions, so we just bailout the compilation.
1406 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001407 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001408 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001409 MaybeRecordStat(compilation_stats_,
1410 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001411 return false;
1412 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001413 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001414 invoke->SetArgumentAt(*argument_index, arg);
1415 if (is_wide) {
1416 i++;
1417 }
1418 }
1419
1420 if (*argument_index != invoke->GetNumberOfArguments()) {
1421 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001422 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001423 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001424 MaybeRecordStat(compilation_stats_,
1425 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001426 return false;
1427 }
1428
1429 if (invoke->IsInvokeStaticOrDirect() &&
1430 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1431 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1432 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1433 (*argument_index)++;
1434 }
1435
1436 return true;
1437}
1438
1439bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001440 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001441 const char* shorty,
1442 bool is_unresolved,
1443 HClinitCheck* clinit_check) {
David Brazdildee58d62016-04-07 09:54:26 +00001444 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1445
1446 size_t start_index = 0;
1447 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001448 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001449 uint32_t obj_reg = operands.GetOperand(0);
Aart Bik296fbb42016-06-07 13:49:12 -07001450 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001451 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001452 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001453 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001454 start_index = 1;
1455 argument_index = 1;
1456 }
1457
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001458 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001459 return false;
1460 }
1461
1462 if (clinit_check != nullptr) {
1463 // Add the class initialization check as last input of `invoke`.
1464 DCHECK(invoke->IsInvokeStaticOrDirect());
1465 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1466 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1467 invoke->SetArgumentAt(argument_index, clinit_check);
1468 argument_index++;
1469 }
1470
1471 AppendInstruction(invoke);
1472 latest_result_ = invoke;
1473
1474 return true;
1475}
1476
1477bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001478 const InstructionOperands& operands,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001479 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001480 DCHECK(invoke->IsInvokeStaticOrDirect());
1481 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1482
1483 size_t start_index = 1;
1484 size_t argument_index = 0;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001485 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001486 return false;
1487 }
1488
1489 AppendInstruction(invoke);
1490
1491 // This is a StringFactory call, not an actual String constructor. Its result
1492 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001493 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001494 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001495
1496 // Replacing the NewInstance might render it redundant. Keep a list of these
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001497 // to be visited once it is clear whether it has remaining uses.
David Brazdildee58d62016-04-07 09:54:26 +00001498 if (arg_this->IsNewInstance()) {
1499 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01001500 } else {
1501 DCHECK(arg_this->IsPhi());
1502 // We can get a phi as input of a String.<init> if there is a loop between the
1503 // allocation and the String.<init> call. As we don't know which other phis might alias
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01001504 // with `arg_this`, we keep a record of those invocations so we can later replace
1505 // the allocation with the invocation.
1506 // Add the actual 'this' input so the analysis knows what is the allocation instruction.
1507 // The input will be removed during the analysis.
1508 invoke->AddInput(arg_this);
1509 ssa_builder_->AddUninitializedStringPhi(invoke);
1510 }
1511 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1512 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1513 if ((*current_locals_)[vreg] == arg_this) {
1514 (*current_locals_)[vreg] = invoke;
1515 }
David Brazdildee58d62016-04-07 09:54:26 +00001516 }
David Brazdildee58d62016-04-07 09:54:26 +00001517 return true;
1518}
1519
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001520static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001521 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1522 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001523 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001524}
1525
1526bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1527 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001528 bool is_put,
1529 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001530 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1531 uint32_t obj_reg = instruction.VRegB_22c();
1532 uint16_t field_index;
1533 if (instruction.IsQuickened()) {
1534 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001535 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1536 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001537 return false;
1538 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001539 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001540 } else {
1541 field_index = instruction.VRegC_22c();
1542 }
1543
1544 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001545 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001546
Aart Bik14154132016-06-02 17:53:58 -07001547 // Generate an explicit null check on the reference, unless the field access
1548 // is unresolved. In that case, we rely on the runtime to perform various
1549 // checks first, followed by a null check.
1550 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001551 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001552 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001554 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001555 if (is_put) {
1556 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1557 HInstruction* field_set = nullptr;
1558 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001559 MaybeRecordStat(compilation_stats_,
1560 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001561 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1562 value,
1563 field_type,
1564 field_index,
1565 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001566 } else {
1567 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001568 field_set = new (allocator_) HInstanceFieldSet(object,
1569 value,
1570 resolved_field,
1571 field_type,
1572 resolved_field->GetOffset(),
1573 resolved_field->IsVolatile(),
1574 field_index,
1575 class_def_index,
1576 *dex_file_,
1577 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001578 }
1579 AppendInstruction(field_set);
1580 } else {
1581 HInstruction* field_get = nullptr;
1582 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001583 MaybeRecordStat(compilation_stats_,
1584 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001585 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1586 field_type,
1587 field_index,
1588 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001589 } else {
1590 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001591 field_get = new (allocator_) HInstanceFieldGet(object,
1592 resolved_field,
1593 field_type,
1594 resolved_field->GetOffset(),
1595 resolved_field->IsVolatile(),
1596 field_index,
1597 class_def_index,
1598 *dex_file_,
1599 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001600 }
1601 AppendInstruction(field_get);
1602 UpdateLocal(source_or_dest_reg, field_get);
1603 }
1604
1605 return true;
1606}
1607
Vladimir Markofca0b492018-07-23 15:30:52 +01001608static ObjPtr<mirror::Class> ResolveClassFrom(ScopedObjectAccess& soa,
1609 CompilerDriver* driver,
1610 const DexCompilationUnit& compilation_unit)
1611 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001612 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001613 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001614
1615 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1616}
1617
Vladimir Markofca0b492018-07-23 15:30:52 +01001618ObjPtr<mirror::Class> HInstructionBuilder::ResolveOutermostCompilingClass(
1619 ScopedObjectAccess& soa) const {
1620 return ResolveClassFrom(soa, compiler_driver_, *outer_compilation_unit_);
David Brazdildee58d62016-04-07 09:54:26 +00001621}
1622
Vladimir Markofca0b492018-07-23 15:30:52 +01001623ObjPtr<mirror::Class> HInstructionBuilder::ResolveCompilingClass(ScopedObjectAccess& soa) const {
1624 return ResolveClassFrom(soa, compiler_driver_, *dex_compilation_unit_);
David Brazdildee58d62016-04-07 09:54:26 +00001625}
1626
Andreas Gampea5b09a62016-11-17 15:21:22 -08001627bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001628 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001629 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001630 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001631 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001632 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1633 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
Vladimir Markofca0b492018-07-23 15:30:52 +01001634 Handle<mirror::Class> outer_class(hs.NewHandle(ResolveOutermostCompilingClass(soa)));
David Brazdildee58d62016-04-07 09:54:26 +00001635
1636 // GetOutermostCompilingClass returns null when the class is unresolved
1637 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1638 // we are compiling it.
1639 // When this happens we cannot establish a direct relation between the current
1640 // class and the outer class, so we return false.
1641 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001642 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001643}
1644
1645void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001646 uint32_t dex_pc,
1647 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001648 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001649 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1650 uint16_t field_index = instruction.VRegB_21c();
1651
1652 if (is_put) {
1653 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1654 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001655 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001656 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001657 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001658 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1659 }
1660}
1661
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001662ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1663 ScopedObjectAccess soa(Thread::Current());
1664 StackHandleScope<2> hs(soa.Self());
1665
1666 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001667 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Vladimir Markofca0b492018-07-23 15:30:52 +01001668 Handle<mirror::Class> compiling_class(hs.NewHandle(ResolveCompilingClass(soa)));
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001669
Vladimir Markoe11dd502017-12-08 14:09:45 +00001670 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001671 dex_compilation_unit_->GetDexCache(),
1672 class_loader,
1673 is_static);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001674 if (UNLIKELY(resolved_field == nullptr)) {
1675 // Clean up any exception left by type resolution.
1676 soa.Self()->ClearException();
1677 return nullptr;
1678 }
1679
1680 // Check static/instance. The class linker has a fast path for looking into the dex cache
1681 // and does not check static/instance if it hits it.
1682 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1683 return nullptr;
1684 }
1685
1686 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001687 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001688 if (!resolved_field->IsPublic()) {
1689 return nullptr;
1690 }
1691 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1692 resolved_field,
1693 dex_compilation_unit_->GetDexCache().Get(),
1694 field_idx)) {
1695 return nullptr;
1696 }
1697
1698 if (is_put &&
1699 resolved_field->IsFinal() &&
1700 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1701 // Final fields can only be updated within their own class.
1702 // TODO: Only allow it in constructors. b/34966607.
1703 return nullptr;
1704 }
1705
1706 return resolved_field;
1707}
1708
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001709void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001710 uint32_t dex_pc,
1711 bool is_put) {
1712 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1713 uint16_t field_index = instruction.VRegB_21c();
1714
1715 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001716 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001717
1718 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001719 MaybeRecordStat(compilation_stats_,
1720 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001722 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001723 return;
David Brazdildee58d62016-04-07 09:54:26 +00001724 }
1725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001727
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001728 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
Vladimir Markofca0b492018-07-23 15:30:52 +01001729 HLoadClass* constant = BuildLoadClass(soa,
1730 klass->GetDexTypeIndex(),
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001731 klass->GetDexFile(),
1732 klass,
1733 dex_pc,
1734 /* needs_access_check */ false);
1735
1736 if (constant == nullptr) {
1737 // The class cannot be referenced from this compiled code. Generate
1738 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001739 MaybeRecordStat(compilation_stats_,
1740 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001741 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001742 return;
David Brazdildee58d62016-04-07 09:54:26 +00001743 }
1744
David Brazdildee58d62016-04-07 09:54:26 +00001745 HInstruction* cls = constant;
Vladimir Markofca0b492018-07-23 15:30:52 +01001746 if (!IsInitialized(soa, klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001747 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001748 AppendInstruction(cls);
1749 }
1750
1751 uint16_t class_def_index = klass->GetDexClassDefIndex();
1752 if (is_put) {
1753 // We need to keep the class alive before loading the value.
1754 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1755 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001756 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1757 value,
1758 resolved_field,
1759 field_type,
1760 resolved_field->GetOffset(),
1761 resolved_field->IsVolatile(),
1762 field_index,
1763 class_def_index,
1764 *dex_file_,
1765 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001766 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001767 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1768 resolved_field,
1769 field_type,
1770 resolved_field->GetOffset(),
1771 resolved_field->IsVolatile(),
1772 field_index,
1773 class_def_index,
1774 *dex_file_,
1775 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001776 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1777 }
David Brazdildee58d62016-04-07 09:54:26 +00001778}
1779
1780void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001781 uint16_t first_vreg,
1782 int64_t second_vreg_or_constant,
1783 uint32_t dex_pc,
1784 DataType::Type type,
1785 bool second_is_constant,
1786 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001788
1789 HInstruction* first = LoadLocal(first_vreg, type);
1790 HInstruction* second = nullptr;
1791 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001792 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001793 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1794 } else {
1795 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1796 }
1797 } else {
1798 second = LoadLocal(second_vreg_or_constant, type);
1799 }
1800
1801 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1803 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001804 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001805 AppendInstruction(second);
1806 }
1807
1808 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001809 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001810 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001811 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001812 }
1813 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1814}
1815
1816void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1817 uint32_t dex_pc,
1818 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001820 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1821 uint8_t array_reg = instruction.VRegB_23x();
1822 uint8_t index_reg = instruction.VRegC_23x();
1823
David Brazdilc120bbe2016-04-22 16:57:00 +01001824 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001825 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001826 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001828 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001829 AppendInstruction(index);
1830 if (is_put) {
1831 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1832 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001833 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001834 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1835 AppendInstruction(aset);
1836 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001838 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1839 AppendInstruction(aget);
1840 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1841 }
1842 graph_->SetHasBoundsChecks(true);
1843}
1844
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001845HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1846 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001847 const InstructionOperands& operands) {
1848 const size_t number_of_operands = operands.GetNumberOfOperands();
1849 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001850 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001851 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001852 AppendInstruction(object);
1853
1854 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1855 DCHECK_EQ(descriptor[0], '[') << descriptor;
1856 char primitive = descriptor[1];
1857 DCHECK(primitive == 'I'
1858 || primitive == 'L'
1859 || primitive == '[') << descriptor;
1860 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001862
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001863 for (size_t i = 0; i < number_of_operands; ++i) {
1864 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001865 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001866 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001867 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1868 AppendInstruction(aset);
1869 }
1870 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001871
1872 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001873}
1874
1875template <typename T>
1876void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1877 const T* data,
1878 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001879 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001880 uint32_t dex_pc) {
1881 for (uint32_t i = 0; i < element_count; ++i) {
1882 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1883 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001884 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001885 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1886 AppendInstruction(aset);
1887 }
1888}
1889
1890void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001891 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001892
1893 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1894 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001895 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1896 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001897 const uint8_t* data = payload->data;
1898 uint32_t element_count = payload->element_count;
1899
Vladimir Markoc69fba22016-09-06 16:49:15 +01001900 if (element_count == 0u) {
1901 // For empty payload we emit only the null check above.
1902 return;
1903 }
1904
Vladimir Markoca6fff82017-10-03 14:49:14 +01001905 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001906 AppendInstruction(length);
1907
David Brazdildee58d62016-04-07 09:54:26 +00001908 // Implementation of this DEX instruction seems to be that the bounds check is
1909 // done before doing any stores.
1910 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001911 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001912
1913 switch (payload->element_width) {
1914 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001915 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001916 reinterpret_cast<const int8_t*>(data),
1917 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001919 dex_pc);
1920 break;
1921 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001922 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001923 reinterpret_cast<const int16_t*>(data),
1924 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001925 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001926 dex_pc);
1927 break;
1928 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001929 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001930 reinterpret_cast<const int32_t*>(data),
1931 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001933 dex_pc);
1934 break;
1935 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001936 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001937 reinterpret_cast<const int64_t*>(data),
1938 element_count,
1939 dex_pc);
1940 break;
1941 default:
1942 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1943 }
1944 graph_->SetHasBoundsChecks(true);
1945}
1946
1947void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1948 const int64_t* data,
1949 uint32_t element_count,
1950 uint32_t dex_pc) {
1951 for (uint32_t i = 0; i < element_count; ++i) {
1952 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1953 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001954 HArraySet* aset =
1955 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001956 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1957 AppendInstruction(aset);
1958 }
1959}
1960
Vladimir Marko28e012a2017-12-07 11:22:59 +00001961void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1962 HLoadString* load_string =
1963 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1964 HSharpening::ProcessLoadString(load_string,
1965 code_generator_,
Vladimir Marko28e012a2017-12-07 11:22:59 +00001966 *dex_compilation_unit_,
1967 handles_);
1968 AppendInstruction(load_string);
1969}
1970
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001971HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001972 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001973 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00001974 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Markofca0b492018-07-23 15:30:52 +01001975 bool needs_access_check = LoadClassNeedsAccessCheck(soa, klass);
1976 return BuildLoadClass(soa, type_index, dex_file, klass, dex_pc, needs_access_check);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001977}
1978
Vladimir Markofca0b492018-07-23 15:30:52 +01001979HLoadClass* HInstructionBuilder::BuildLoadClass(ScopedObjectAccess& soa,
1980 dex::TypeIndex type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001981 const DexFile& dex_file,
1982 Handle<mirror::Class> klass,
1983 uint32_t dex_pc,
1984 bool needs_access_check) {
1985 // Try to find a reference in the compiling dex file.
1986 const DexFile* actual_dex_file = &dex_file;
1987 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1988 dex::TypeIndex local_type_index =
1989 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1990 if (local_type_index.IsValid()) {
1991 type_index = local_type_index;
1992 actual_dex_file = dex_compilation_unit_->GetDexFile();
1993 }
1994 }
1995
1996 // Note: `klass` must be from `handles_`.
Vladimir Markofca0b492018-07-23 15:30:52 +01001997 bool is_referrers_class = false;
1998 if (klass != nullptr) {
1999 ObjPtr<mirror::Class> outermost_cls = ResolveOutermostCompilingClass(soa);
2000 is_referrers_class = (outermost_cls == klass.Get());
2001 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002002 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002003 graph_->GetCurrentMethod(),
2004 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002005 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002006 klass,
Vladimir Markofca0b492018-07-23 15:30:52 +01002007 is_referrers_class,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002008 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002009 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002010
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002011 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
2012 code_generator_,
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00002013 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002014
2015 if (load_kind == HLoadClass::LoadKind::kInvalid) {
2016 // We actually cannot reference this class, we're forced to bail.
2017 return nullptr;
2018 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00002019 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002020 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00002021 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002022 return load_class;
2023}
2024
Vladimir Marko175e7862018-03-27 09:03:13 +00002025Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
2026 dex::TypeIndex type_index) {
2027 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
2028 ObjPtr<mirror::Class> klass = compiler_driver_->ResolveClass(
2029 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_);
2030 // TODO: Avoid creating excessive handles if the method references the same class repeatedly.
2031 // (Use a map on the local_allocator_.)
2032 return handles_->NewHandle(klass);
2033}
2034
Vladimir Markofca0b492018-07-23 15:30:52 +01002035bool HInstructionBuilder::LoadClassNeedsAccessCheck(ScopedObjectAccess& soa,
2036 Handle<mirror::Class> klass) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002037 if (klass == nullptr) {
2038 return true;
2039 } else if (klass->IsPublic()) {
2040 return false;
2041 } else {
Vladimir Markofca0b492018-07-23 15:30:52 +01002042 ObjPtr<mirror::Class> compiling_class = ResolveCompilingClass(soa);
Vladimir Marko175e7862018-03-27 09:03:13 +00002043 return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
2044 }
2045}
2046
Orion Hodson06d10a72018-05-14 08:53:38 +01002047void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002048 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01002049 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
2050 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002051 AppendInstruction(load_method_handle);
2052}
2053
Orion Hodson06d10a72018-05-14 08:53:38 +01002054void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01002055 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2056 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01002057 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01002058 AppendInstruction(load_method_type);
2059}
2060
David Brazdildee58d62016-04-07 09:54:26 +00002061void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
2062 uint8_t destination,
2063 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08002064 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00002065 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00002067
Nicolas Geoffray5247c082017-01-13 14:17:29 +00002068 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00002069 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
2070 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
Vladimir Markofca0b492018-07-23 15:30:52 +01002071 bool needs_access_check = LoadClassNeedsAccessCheck(soa, klass);
Vladimir Marko175e7862018-03-27 09:03:13 +00002072 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002073 klass.Get(), code_generator_, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002074
2075 HInstruction* class_or_null = nullptr;
2076 HIntConstant* bitstring_path_to_root = nullptr;
2077 HIntConstant* bitstring_mask = nullptr;
2078 if (check_kind == TypeCheckKind::kBitstringCheck) {
2079 // TODO: Allow using the bitstring check also if we need an access check.
2080 DCHECK(!needs_access_check);
2081 class_or_null = graph_->GetNullConstant(dex_pc);
2082 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
2083 uint32_t path_to_root =
2084 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
2085 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
2086 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
2087 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
2088 } else {
Vladimir Markofca0b492018-07-23 15:30:52 +01002089 class_or_null = BuildLoadClass(soa, type_index, dex_file, klass, dex_pc, needs_access_check);
Vladimir Marko175e7862018-03-27 09:03:13 +00002090 }
2091 DCHECK(class_or_null != nullptr);
2092
David Brazdildee58d62016-04-07 09:54:26 +00002093 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00002094 AppendInstruction(new (allocator_) HInstanceOf(object,
2095 class_or_null,
2096 check_kind,
2097 klass,
2098 dex_pc,
2099 allocator_,
2100 bitstring_path_to_root,
2101 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00002102 UpdateLocal(destination, current_block_->GetLastInstruction());
2103 } else {
2104 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
2105 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
2106 // which may throw. If it succeeds BoundType sets the new type of `object`
2107 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00002108 AppendInstruction(
2109 new (allocator_) HCheckCast(object,
2110 class_or_null,
2111 check_kind,
2112 klass,
2113 dex_pc,
2114 allocator_,
2115 bitstring_path_to_root,
2116 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01002117 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002118 UpdateLocal(reference, current_block_->GetLastInstruction());
2119 }
2120}
2121
David Brazdildee58d62016-04-07 09:54:26 +00002122bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002123 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00002124}
2125
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002126uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
2127 DCHECK(CanDecodeQuickenedInfo());
2128 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002129}
2130
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002131bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
2132 uint32_t dex_pc,
2133 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00002134 switch (instruction.Opcode()) {
2135 case Instruction::CONST_4: {
2136 int32_t register_index = instruction.VRegA();
2137 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
2138 UpdateLocal(register_index, constant);
2139 break;
2140 }
2141
2142 case Instruction::CONST_16: {
2143 int32_t register_index = instruction.VRegA();
2144 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
2145 UpdateLocal(register_index, constant);
2146 break;
2147 }
2148
2149 case Instruction::CONST: {
2150 int32_t register_index = instruction.VRegA();
2151 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
2152 UpdateLocal(register_index, constant);
2153 break;
2154 }
2155
2156 case Instruction::CONST_HIGH16: {
2157 int32_t register_index = instruction.VRegA();
2158 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
2159 UpdateLocal(register_index, constant);
2160 break;
2161 }
2162
2163 case Instruction::CONST_WIDE_16: {
2164 int32_t register_index = instruction.VRegA();
2165 // Get 16 bits of constant value, sign extended to 64 bits.
2166 int64_t value = instruction.VRegB_21s();
2167 value <<= 48;
2168 value >>= 48;
2169 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2170 UpdateLocal(register_index, constant);
2171 break;
2172 }
2173
2174 case Instruction::CONST_WIDE_32: {
2175 int32_t register_index = instruction.VRegA();
2176 // Get 32 bits of constant value, sign extended to 64 bits.
2177 int64_t value = instruction.VRegB_31i();
2178 value <<= 32;
2179 value >>= 32;
2180 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2181 UpdateLocal(register_index, constant);
2182 break;
2183 }
2184
2185 case Instruction::CONST_WIDE: {
2186 int32_t register_index = instruction.VRegA();
2187 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2188 UpdateLocal(register_index, constant);
2189 break;
2190 }
2191
2192 case Instruction::CONST_WIDE_HIGH16: {
2193 int32_t register_index = instruction.VRegA();
2194 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2195 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2196 UpdateLocal(register_index, constant);
2197 break;
2198 }
2199
2200 // Note that the SSA building will refine the types.
2201 case Instruction::MOVE:
2202 case Instruction::MOVE_FROM16:
2203 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002205 UpdateLocal(instruction.VRegA(), value);
2206 break;
2207 }
2208
2209 // Note that the SSA building will refine the types.
2210 case Instruction::MOVE_WIDE:
2211 case Instruction::MOVE_WIDE_FROM16:
2212 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002214 UpdateLocal(instruction.VRegA(), value);
2215 break;
2216 }
2217
2218 case Instruction::MOVE_OBJECT:
2219 case Instruction::MOVE_OBJECT_16:
2220 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002221 // The verifier has no notion of a null type, so a move-object of constant 0
2222 // will lead to the same constant 0 in the destination register. To mimic
2223 // this behavior, we just pretend we haven't seen a type change (int to reference)
2224 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2225 // types correct.
2226 uint32_t reg_number = instruction.VRegB();
2227 HInstruction* value = (*current_locals_)[reg_number];
2228 if (value->IsIntConstant()) {
2229 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2230 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2232 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002233 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002235 }
David Brazdildee58d62016-04-07 09:54:26 +00002236 UpdateLocal(instruction.VRegA(), value);
2237 break;
2238 }
2239
2240 case Instruction::RETURN_VOID_NO_BARRIER:
2241 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002243 break;
2244 }
2245
2246#define IF_XX(comparison, cond) \
2247 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2248 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2249
2250 IF_XX(HEqual, EQ);
2251 IF_XX(HNotEqual, NE);
2252 IF_XX(HLessThan, LT);
2253 IF_XX(HLessThanOrEqual, LE);
2254 IF_XX(HGreaterThan, GT);
2255 IF_XX(HGreaterThanOrEqual, GE);
2256
2257 case Instruction::GOTO:
2258 case Instruction::GOTO_16:
2259 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002260 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002261 current_block_ = nullptr;
2262 break;
2263 }
2264
2265 case Instruction::RETURN: {
2266 BuildReturn(instruction, return_type_, dex_pc);
2267 break;
2268 }
2269
2270 case Instruction::RETURN_OBJECT: {
2271 BuildReturn(instruction, return_type_, dex_pc);
2272 break;
2273 }
2274
2275 case Instruction::RETURN_WIDE: {
2276 BuildReturn(instruction, return_type_, dex_pc);
2277 break;
2278 }
2279
2280 case Instruction::INVOKE_DIRECT:
2281 case Instruction::INVOKE_INTERFACE:
2282 case Instruction::INVOKE_STATIC:
2283 case Instruction::INVOKE_SUPER:
2284 case Instruction::INVOKE_VIRTUAL:
2285 case Instruction::INVOKE_VIRTUAL_QUICK: {
2286 uint16_t method_idx;
2287 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2288 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002289 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2290 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002291 return false;
2292 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002293 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002294 } else {
2295 method_idx = instruction.VRegB_35c();
2296 }
David Brazdildee58d62016-04-07 09:54:26 +00002297 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002298 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2299 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2300 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002301 return false;
2302 }
2303 break;
2304 }
2305
2306 case Instruction::INVOKE_DIRECT_RANGE:
2307 case Instruction::INVOKE_INTERFACE_RANGE:
2308 case Instruction::INVOKE_STATIC_RANGE:
2309 case Instruction::INVOKE_SUPER_RANGE:
2310 case Instruction::INVOKE_VIRTUAL_RANGE:
2311 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2312 uint16_t method_idx;
2313 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2314 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002315 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2316 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002317 return false;
2318 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002319 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002320 } else {
2321 method_idx = instruction.VRegB_3rc();
2322 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002323 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2324 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002325 return false;
2326 }
2327 break;
2328 }
2329
Orion Hodsonac141392017-01-13 11:53:47 +00002330 case Instruction::INVOKE_POLYMORPHIC: {
2331 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002332 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002333 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002334 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2335 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002336 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002337 }
2338
2339 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2340 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002341 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002342 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002343 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2344 }
2345
2346 case Instruction::INVOKE_CUSTOM: {
2347 uint16_t call_site_idx = instruction.VRegB_35c();
2348 uint32_t args[5];
2349 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2350 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2351 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2352 }
2353
2354 case Instruction::INVOKE_CUSTOM_RANGE: {
2355 uint16_t call_site_idx = instruction.VRegB_3rc();
2356 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2357 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002358 }
2359
David Brazdildee58d62016-04-07 09:54:26 +00002360 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002362 break;
2363 }
2364
2365 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002367 break;
2368 }
2369
2370 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002371 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002372 break;
2373 }
2374
2375 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002377 break;
2378 }
2379
2380 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002382 break;
2383 }
2384
2385 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002387 break;
2388 }
2389
2390 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002392 break;
2393 }
2394
2395 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002397 break;
2398 }
2399
2400 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002401 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002402 break;
2403 }
2404
2405 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002407 break;
2408 }
2409
2410 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002412 break;
2413 }
2414
2415 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002417 break;
2418 }
2419
2420 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002422 break;
2423 }
2424
2425 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002427 break;
2428 }
2429
2430 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002432 break;
2433 }
2434
2435 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002437 break;
2438 }
2439
2440 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002442 break;
2443 }
2444
2445 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002447 break;
2448 }
2449
2450 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002451 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002452 break;
2453 }
2454
2455 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002457 break;
2458 }
2459
2460 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002462 break;
2463 }
2464
2465 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002467 break;
2468 }
2469
2470 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002472 break;
2473 }
2474
2475 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002477 break;
2478 }
2479
2480 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002482 break;
2483 }
2484
2485 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002487 break;
2488 }
2489
2490 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002492 break;
2493 }
2494
2495 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002497 break;
2498 }
2499
2500 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002502 break;
2503 }
2504
2505 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002507 break;
2508 }
2509
2510 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002512 break;
2513 }
2514
2515 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002517 break;
2518 }
2519
2520 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002522 break;
2523 }
2524
2525 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002527 break;
2528 }
2529
2530 case Instruction::DIV_INT: {
2531 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002533 break;
2534 }
2535
2536 case Instruction::DIV_LONG: {
2537 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002539 break;
2540 }
2541
2542 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002543 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002544 break;
2545 }
2546
2547 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002549 break;
2550 }
2551
2552 case Instruction::REM_INT: {
2553 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002555 break;
2556 }
2557
2558 case Instruction::REM_LONG: {
2559 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002561 break;
2562 }
2563
2564 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002566 break;
2567 }
2568
2569 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002570 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002571 break;
2572 }
2573
2574 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002576 break;
2577 }
2578
2579 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002580 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002581 break;
2582 }
2583
2584 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002585 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002586 break;
2587 }
2588
2589 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002591 break;
2592 }
2593
2594 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002596 break;
2597 }
2598
2599 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002601 break;
2602 }
2603
2604 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002606 break;
2607 }
2608
2609 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002611 break;
2612 }
2613
2614 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002615 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002616 break;
2617 }
2618
2619 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002621 break;
2622 }
2623
2624 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002625 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002626 break;
2627 }
2628
2629 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002631 break;
2632 }
2633
2634 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002635 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002636 break;
2637 }
2638
2639 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002641 break;
2642 }
2643
2644 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002646 break;
2647 }
2648
2649 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002651 break;
2652 }
2653
2654 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002656 break;
2657 }
2658
2659 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002661 break;
2662 }
2663
2664 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002666 break;
2667 }
2668
2669 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002671 break;
2672 }
2673
2674 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002675 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002676 break;
2677 }
2678
2679 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002681 break;
2682 }
2683
2684 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002686 break;
2687 }
2688
2689 case Instruction::DIV_INT_2ADDR: {
2690 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002692 break;
2693 }
2694
2695 case Instruction::DIV_LONG_2ADDR: {
2696 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002698 break;
2699 }
2700
2701 case Instruction::REM_INT_2ADDR: {
2702 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002704 break;
2705 }
2706
2707 case Instruction::REM_LONG_2ADDR: {
2708 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002710 break;
2711 }
2712
2713 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002715 break;
2716 }
2717
2718 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002720 break;
2721 }
2722
2723 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002724 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002725 break;
2726 }
2727
2728 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002730 break;
2731 }
2732
2733 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002735 break;
2736 }
2737
2738 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002740 break;
2741 }
2742
2743 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002745 break;
2746 }
2747
2748 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002750 break;
2751 }
2752
2753 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002755 break;
2756 }
2757
2758 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002760 break;
2761 }
2762
2763 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002765 break;
2766 }
2767
2768 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002770 break;
2771 }
2772
2773 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002775 break;
2776 }
2777
2778 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002779 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002780 break;
2781 }
2782
2783 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002784 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002785 break;
2786 }
2787
2788 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002790 break;
2791 }
2792
2793 case Instruction::ADD_INT_LIT16: {
2794 Binop_22s<HAdd>(instruction, false, dex_pc);
2795 break;
2796 }
2797
2798 case Instruction::AND_INT_LIT16: {
2799 Binop_22s<HAnd>(instruction, false, dex_pc);
2800 break;
2801 }
2802
2803 case Instruction::OR_INT_LIT16: {
2804 Binop_22s<HOr>(instruction, false, dex_pc);
2805 break;
2806 }
2807
2808 case Instruction::XOR_INT_LIT16: {
2809 Binop_22s<HXor>(instruction, false, dex_pc);
2810 break;
2811 }
2812
2813 case Instruction::RSUB_INT: {
2814 Binop_22s<HSub>(instruction, true, dex_pc);
2815 break;
2816 }
2817
2818 case Instruction::MUL_INT_LIT16: {
2819 Binop_22s<HMul>(instruction, false, dex_pc);
2820 break;
2821 }
2822
2823 case Instruction::ADD_INT_LIT8: {
2824 Binop_22b<HAdd>(instruction, false, dex_pc);
2825 break;
2826 }
2827
2828 case Instruction::AND_INT_LIT8: {
2829 Binop_22b<HAnd>(instruction, false, dex_pc);
2830 break;
2831 }
2832
2833 case Instruction::OR_INT_LIT8: {
2834 Binop_22b<HOr>(instruction, false, dex_pc);
2835 break;
2836 }
2837
2838 case Instruction::XOR_INT_LIT8: {
2839 Binop_22b<HXor>(instruction, false, dex_pc);
2840 break;
2841 }
2842
2843 case Instruction::RSUB_INT_LIT8: {
2844 Binop_22b<HSub>(instruction, true, dex_pc);
2845 break;
2846 }
2847
2848 case Instruction::MUL_INT_LIT8: {
2849 Binop_22b<HMul>(instruction, false, dex_pc);
2850 break;
2851 }
2852
2853 case Instruction::DIV_INT_LIT16:
2854 case Instruction::DIV_INT_LIT8: {
2855 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002856 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002857 break;
2858 }
2859
2860 case Instruction::REM_INT_LIT16:
2861 case Instruction::REM_INT_LIT8: {
2862 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002863 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002864 break;
2865 }
2866
2867 case Instruction::SHL_INT_LIT8: {
2868 Binop_22b<HShl>(instruction, false, dex_pc);
2869 break;
2870 }
2871
2872 case Instruction::SHR_INT_LIT8: {
2873 Binop_22b<HShr>(instruction, false, dex_pc);
2874 break;
2875 }
2876
2877 case Instruction::USHR_INT_LIT8: {
2878 Binop_22b<HUShr>(instruction, false, dex_pc);
2879 break;
2880 }
2881
2882 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002883 HNewInstance* new_instance =
2884 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2885 DCHECK(new_instance != nullptr);
2886
David Brazdildee58d62016-04-07 09:54:26 +00002887 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002888 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002889 break;
2890 }
2891
2892 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002893 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002895 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002896
Vladimir Markoca6fff82017-10-03 14:49:14 +01002897 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002898 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002899 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002900 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002901 break;
2902 }
2903
2904 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002905 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002906 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002907 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2908 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2909 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002910 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002911 break;
2912 }
2913
2914 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002915 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002916 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2917 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002918 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002919 break;
2920 }
2921
2922 case Instruction::FILL_ARRAY_DATA: {
2923 BuildFillArrayData(instruction, dex_pc);
2924 break;
2925 }
2926
2927 case Instruction::MOVE_RESULT:
2928 case Instruction::MOVE_RESULT_WIDE:
2929 case Instruction::MOVE_RESULT_OBJECT: {
2930 DCHECK(latest_result_ != nullptr);
2931 UpdateLocal(instruction.VRegA(), latest_result_);
2932 latest_result_ = nullptr;
2933 break;
2934 }
2935
2936 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002938 break;
2939 }
2940
2941 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002943 break;
2944 }
2945
2946 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002948 break;
2949 }
2950
2951 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002953 break;
2954 }
2955
2956 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002958 break;
2959 }
2960
2961 case Instruction::NOP:
2962 break;
2963
2964 case Instruction::IGET:
2965 case Instruction::IGET_QUICK:
2966 case Instruction::IGET_WIDE:
2967 case Instruction::IGET_WIDE_QUICK:
2968 case Instruction::IGET_OBJECT:
2969 case Instruction::IGET_OBJECT_QUICK:
2970 case Instruction::IGET_BOOLEAN:
2971 case Instruction::IGET_BOOLEAN_QUICK:
2972 case Instruction::IGET_BYTE:
2973 case Instruction::IGET_BYTE_QUICK:
2974 case Instruction::IGET_CHAR:
2975 case Instruction::IGET_CHAR_QUICK:
2976 case Instruction::IGET_SHORT:
2977 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002978 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002979 return false;
2980 }
2981 break;
2982 }
2983
2984 case Instruction::IPUT:
2985 case Instruction::IPUT_QUICK:
2986 case Instruction::IPUT_WIDE:
2987 case Instruction::IPUT_WIDE_QUICK:
2988 case Instruction::IPUT_OBJECT:
2989 case Instruction::IPUT_OBJECT_QUICK:
2990 case Instruction::IPUT_BOOLEAN:
2991 case Instruction::IPUT_BOOLEAN_QUICK:
2992 case Instruction::IPUT_BYTE:
2993 case Instruction::IPUT_BYTE_QUICK:
2994 case Instruction::IPUT_CHAR:
2995 case Instruction::IPUT_CHAR_QUICK:
2996 case Instruction::IPUT_SHORT:
2997 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002998 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002999 return false;
3000 }
3001 break;
3002 }
3003
3004 case Instruction::SGET:
3005 case Instruction::SGET_WIDE:
3006 case Instruction::SGET_OBJECT:
3007 case Instruction::SGET_BOOLEAN:
3008 case Instruction::SGET_BYTE:
3009 case Instruction::SGET_CHAR:
3010 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00003011 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00003012 break;
3013 }
3014
3015 case Instruction::SPUT:
3016 case Instruction::SPUT_WIDE:
3017 case Instruction::SPUT_OBJECT:
3018 case Instruction::SPUT_BOOLEAN:
3019 case Instruction::SPUT_BYTE:
3020 case Instruction::SPUT_CHAR:
3021 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00003022 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00003023 break;
3024 }
3025
3026#define ARRAY_XX(kind, anticipated_type) \
3027 case Instruction::AGET##kind: { \
3028 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
3029 break; \
3030 } \
3031 case Instruction::APUT##kind: { \
3032 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
3033 break; \
3034 }
3035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 ARRAY_XX(, DataType::Type::kInt32);
3037 ARRAY_XX(_WIDE, DataType::Type::kInt64);
3038 ARRAY_XX(_OBJECT, DataType::Type::kReference);
3039 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
3040 ARRAY_XX(_BYTE, DataType::Type::kInt8);
3041 ARRAY_XX(_CHAR, DataType::Type::kUint16);
3042 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00003043
3044 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01003045 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003046 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003047 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
3048 break;
3049 }
3050
3051 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003052 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003053 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003054 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3055 break;
3056 }
3057
3058 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08003059 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00003060 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003061 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
3062 break;
3063 }
3064
3065 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003066 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003067 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00003068 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3069 break;
3070 }
3071
Orion Hodsondbaa5c72018-05-10 08:22:46 +01003072 case Instruction::CONST_METHOD_HANDLE: {
3073 uint16_t method_handle_idx = instruction.VRegB_21c();
3074 BuildLoadMethodHandle(method_handle_idx, dex_pc);
3075 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3076 break;
3077 }
3078
Orion Hodson18259d72018-04-12 11:18:23 +01003079 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01003080 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01003081 BuildLoadMethodType(proto_idx, dex_pc);
3082 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
3083 break;
3084 }
3085
David Brazdildee58d62016-04-07 09:54:26 +00003086 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003087 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003088 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01003089 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003090 break;
3091 }
3092
3093 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003095 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00003096 // We finished building this block. Set the current block to null to avoid
3097 // adding dead instructions to it.
3098 current_block_ = nullptr;
3099 break;
3100 }
3101
3102 case Instruction::INSTANCE_OF: {
3103 uint8_t destination = instruction.VRegA_22c();
3104 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003105 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00003106 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
3107 break;
3108 }
3109
3110 case Instruction::CHECK_CAST: {
3111 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08003112 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00003113 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
3114 break;
3115 }
3116
3117 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003118 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003119 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003120 HMonitorOperation::OperationKind::kEnter,
3121 dex_pc));
3122 break;
3123 }
3124
3125 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003126 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00003128 HMonitorOperation::OperationKind::kExit,
3129 dex_pc));
3130 break;
3131 }
3132
3133 case Instruction::SPARSE_SWITCH:
3134 case Instruction::PACKED_SWITCH: {
3135 BuildSwitch(instruction, dex_pc);
3136 break;
3137 }
3138
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003139 case Instruction::UNUSED_3E:
3140 case Instruction::UNUSED_3F:
3141 case Instruction::UNUSED_40:
3142 case Instruction::UNUSED_41:
3143 case Instruction::UNUSED_42:
3144 case Instruction::UNUSED_43:
3145 case Instruction::UNUSED_79:
3146 case Instruction::UNUSED_7A:
3147 case Instruction::UNUSED_F3:
3148 case Instruction::UNUSED_F4:
3149 case Instruction::UNUSED_F5:
3150 case Instruction::UNUSED_F6:
3151 case Instruction::UNUSED_F7:
3152 case Instruction::UNUSED_F8:
3153 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00003154 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07003155 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00003156 << " because of unhandled instruction "
3157 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07003158 MaybeRecordStat(compilation_stats_,
3159 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00003160 return false;
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003161 }
David Brazdildee58d62016-04-07 09:54:26 +00003162 }
3163 return true;
3164} // NOLINT(readability/fn_size)
3165
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003166ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
3167 dex::TypeIndex type_index,
3168 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003169 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003170 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
3171}
3172
3173ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
3174 // TODO: Cache the result in a Handle<mirror::Class>.
3175 const DexFile::MethodId& method_id =
3176 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
3177 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
3178}
3179
David Brazdildee58d62016-04-07 09:54:26 +00003180} // namespace art