blob: 649c6a91d11b4638b9bd8fdaf946f19b7795f136 [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 Sehre6564f42018-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 Robota9e5f952018-05-17 22:26:08 +0000451 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Orion Hodson20e7a6e2018-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() {
David Brazdildee58d62016-04-07 09:54:26 +0000469 // The callback gets called when the line number changes.
470 // In other words, it marks the start of new java statement.
471 struct Callback {
472 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
473 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
474 return false;
475 }
476 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100477 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800478 code_item_accessor_.InsnsSizeInCodeUnits(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100479 /* expandable */ false,
480 kArenaAllocGraphBuilder);
481 locations->ClearAllBits();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800482 dex_file_->DecodeDebugPositionInfo(code_item_accessor_.DebugInfoOffset(),
483 Callback::Position,
484 locations);
David Brazdildee58d62016-04-07 09:54:26 +0000485 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800486 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800487 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000488 case Instruction::MOVE_EXCEPTION: {
489 // Stop in native debugger after the exception has been moved.
490 // The compiler also expects the move at the start of basic block so
491 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800492 locations->ClearBit(inst.DexPc());
493 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
494 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800495 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700496 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000497 }
498 break;
499 }
500 default:
501 break;
502 }
503 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100504 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000505}
506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100507HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000508 HInstruction* value = (*current_locals_)[reg_number];
509 DCHECK(value != nullptr);
510
511 // If the operation requests a specific type, we make sure its input is of that type.
512 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100513 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700514 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100515 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700516 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000517 }
Aart Bik31883642016-06-06 15:02:44 -0700518 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000519 }
520
521 return value;
522}
523
524void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100525 DataType::Type stored_type = stored_value->GetType();
526 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000527
528 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
529 // registers. Consider the following cases:
530 // (1) Storing a wide value must overwrite previous values in both `reg_number`
531 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
532 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
533 // must invalidate it. We store `nullptr` in `reg_number-1`.
534 // Consequently, storing a wide value into the high vreg of another wide value
535 // will invalidate both `reg_number-1` and `reg_number+1`.
536
537 if (reg_number != 0) {
538 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100539 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000540 // The vreg we are storing into was previously the high vreg of a pair.
541 // We need to invalidate its low vreg.
542 DCHECK((*current_locals_)[reg_number] == nullptr);
543 (*current_locals_)[reg_number - 1] = nullptr;
544 }
545 }
546
547 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100548 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000549 // We are storing a pair. Invalidate the instruction in the high vreg.
550 (*current_locals_)[reg_number + 1] = nullptr;
551 }
552}
553
554void HInstructionBuilder::InitializeParameters() {
555 DCHECK(current_block_->IsEntryBlock());
556
Vladimir Marko69d310e2017-10-09 14:12:23 +0100557 // outer_compilation_unit_ is null only when unit testing.
558 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000559 return;
560 }
561
562 const char* shorty = dex_compilation_unit_->GetShorty();
563 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
564 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
565 uint16_t parameter_index = 0;
566
567 const DexFile::MethodId& referrer_method_id =
568 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
569 if (!dex_compilation_unit_->IsStatic()) {
570 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100571 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000572 referrer_method_id.class_idx_,
573 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100574 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700575 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000576 AppendInstruction(parameter);
577 UpdateLocal(locals_index++, parameter);
578 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700579 current_this_parameter_ = parameter;
580 } else {
581 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000582 }
583
584 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
585 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
586 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100587 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000588 *dex_file_,
589 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
590 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100591 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700592 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000593 ++shorty_pos;
594 AppendInstruction(parameter);
595 // Store the parameter value in the local that the dex code will use
596 // to reference that parameter.
597 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100598 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000599 i++;
600 locals_index++;
601 parameter_index++;
602 }
603 }
604}
605
606template<typename T>
607void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100608 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
609 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100610 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000611 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100612 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000613 current_block_ = nullptr;
614}
615
616template<typename T>
617void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100618 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100619 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000620 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100621 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000622 current_block_ = nullptr;
623}
624
625template<typename T>
626void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100627 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000628 uint32_t dex_pc) {
629 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100630 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000631 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
632}
633
634void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100635 DataType::Type input_type,
636 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000637 uint32_t dex_pc) {
638 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100639 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000640 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
641}
642
643template<typename T>
644void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100645 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000646 uint32_t dex_pc) {
647 HInstruction* first = LoadLocal(instruction.VRegB(), type);
648 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000650 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
651}
652
653template<typename T>
654void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100655 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000656 uint32_t dex_pc) {
657 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100658 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100659 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000660 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
661}
662
663void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000665 ComparisonBias bias,
666 uint32_t dex_pc) {
667 HInstruction* first = LoadLocal(instruction.VRegB(), type);
668 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100669 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000670 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
671}
672
673template<typename T>
674void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100675 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000676 uint32_t dex_pc) {
677 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100678 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100679 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000680 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
681}
682
683template<typename T>
684void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100685 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000686 uint32_t dex_pc) {
687 HInstruction* first = LoadLocal(instruction.VRegA(), type);
688 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100689 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000690 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
691}
692
693template<typename T>
694void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100695 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000696 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
697 if (reverse) {
698 std::swap(first, second);
699 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100700 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000701 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
702}
703
704template<typename T>
705void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100706 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000707 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
708 if (reverse) {
709 std::swap(first, second);
710 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100711 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000712 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
713}
714
Igor Murashkind01745e2017-04-05 16:40:31 -0700715// Does the method being compiled need any constructor barriers being inserted?
716// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700717static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700718 // Can be null in unit tests only.
719 if (UNLIKELY(cu == nullptr)) {
720 return false;
721 }
722
David Brazdildee58d62016-04-07 09:54:26 +0000723 Thread* self = Thread::Current();
724 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700725 && !cu->IsStatic()
726 // RequiresConstructorBarrier must only be queried for <init> methods;
727 // it's effectively "false" for every other method.
728 //
729 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700730 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000731}
732
733// Returns true if `block` has only one successor which starts at the next
734// dex_pc after `instruction` at `dex_pc`.
735static bool IsFallthroughInstruction(const Instruction& instruction,
736 uint32_t dex_pc,
737 HBasicBlock* block) {
738 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
739 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
740}
741
742void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100743 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000744 DexSwitchTable table(instruction, dex_pc);
745
746 if (table.GetNumEntries() == 0) {
747 // Empty Switch. Code falls through to the next block.
748 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100749 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000750 } else if (table.ShouldBuildDecisionTree()) {
751 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
752 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100753 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000754 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100755 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000756
757 if (!it.IsLast()) {
758 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
759 }
760 }
761 } else {
762 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100763 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000764 }
765
766 current_block_ = nullptr;
767}
768
769void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100770 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000771 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100772 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700773 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700774 // This may insert additional redundant constructor fences from the super constructors.
775 // TODO: remove redundant constructor fences (b/36656456).
776 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700777 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100778 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700779
780 HInstruction* fence_target = current_this_parameter_;
781 DCHECK(fence_target != nullptr);
782
Vladimir Markoca6fff82017-10-03 14:49:14 +0100783 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700784 MaybeRecordStat(
785 compilation_stats_,
786 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000787 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100788 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000789 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700790 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000791 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100792 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000793 }
794 current_block_ = nullptr;
795}
796
797static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
798 switch (opcode) {
799 case Instruction::INVOKE_STATIC:
800 case Instruction::INVOKE_STATIC_RANGE:
801 return kStatic;
802 case Instruction::INVOKE_DIRECT:
803 case Instruction::INVOKE_DIRECT_RANGE:
804 return kDirect;
805 case Instruction::INVOKE_VIRTUAL:
806 case Instruction::INVOKE_VIRTUAL_QUICK:
807 case Instruction::INVOKE_VIRTUAL_RANGE:
808 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
809 return kVirtual;
810 case Instruction::INVOKE_INTERFACE:
811 case Instruction::INVOKE_INTERFACE_RANGE:
812 return kInterface;
813 case Instruction::INVOKE_SUPER_RANGE:
814 case Instruction::INVOKE_SUPER:
815 return kSuper;
816 default:
817 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
818 UNREACHABLE();
819 }
820}
821
822ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
823 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000824
825 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000826 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100827
Vladimir Markoba118822017-06-12 15:41:56 +0100828 ArtMethod* resolved_method =
829 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100830 method_idx,
831 dex_compilation_unit_->GetDexCache(),
832 class_loader,
833 graph_->GetArtMethod(),
834 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000835
836 if (UNLIKELY(resolved_method == nullptr)) {
837 // Clean up any exception left by type resolution.
838 soa.Self()->ClearException();
839 return nullptr;
840 }
841
Vladimir Markoba118822017-06-12 15:41:56 +0100842 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
843 // resolved because, for example, we don't find a superclass in the classpath.
844 if (graph_->GetArtMethod() == nullptr) {
845 // The class linker cannot check access without a referrer, so we have to do it.
846 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000847 if (!resolved_method->IsPublic()) {
848 return nullptr;
849 }
David Brazdildee58d62016-04-07 09:54:26 +0000850 }
851
852 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
853 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
854 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
855 // which require runtime handling.
856 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100857 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800858 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000859 // We could not determine the method's class we need to wait until runtime.
860 DCHECK(Runtime::Current()->IsAotCompiler());
861 return nullptr;
862 }
Vladimir Markoba118822017-06-12 15:41:56 +0100863 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100864 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
865 dex_compilation_unit_->GetDexCache().Get(),
866 class_loader.Get());
867 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
868 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700869 // We cannot statically determine the target method. The runtime will throw a
870 // NoSuchMethodError on this one.
871 return nullptr;
872 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100873 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100874 if (referenced_class->IsInterface()) {
875 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100876 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000877 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100878 uint16_t vtable_index = resolved_method->GetMethodIndex();
879 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
880 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000881 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100882 if (actual_method != resolved_method &&
883 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
884 // The back-end code generator relies on this check in order to ensure that it will not
885 // attempt to read the dex_cache with a dex_method_index that is not from the correct
886 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
887 // builder, which means that the code-generator (and compiler driver during sharpening and
888 // inliner, maybe) might invoke an incorrect method.
889 // TODO: The actual method could still be referenced in the current dex file, so we
890 // could try locating it.
891 // TODO: Remove the dex_file restriction.
892 return nullptr;
893 }
894 if (!actual_method->IsInvokable()) {
895 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
896 // could resolve the callee to the wrong method.
897 return nullptr;
898 }
899 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000900 }
901
David Brazdildee58d62016-04-07 09:54:26 +0000902 return resolved_method;
903}
904
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100905static bool IsStringConstructor(ArtMethod* method) {
906 ScopedObjectAccess soa(Thread::Current());
907 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
908}
909
David Brazdildee58d62016-04-07 09:54:26 +0000910bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
911 uint32_t dex_pc,
912 uint32_t method_idx,
Treehugger Robota9e5f952018-05-17 22:26:08 +0000913 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000914 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
Orion Hodson20e7a6e2018-05-18 08:33:20 +0100915 const char* shorty = dex_file_->GetMethodShorty(method_idx);
916 DataType::Type return_type = DataType::FromShorty(shorty[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000917
918 // Remove the return type from the 'proto'.
Orion Hodson20e7a6e2018-05-18 08:33:20 +0100919 size_t number_of_arguments = strlen(shorty) - 1;
David Brazdildee58d62016-04-07 09:54:26 +0000920 if (invoke_type != kStatic) { // instance call
921 // One extra argument for 'this'.
922 number_of_arguments++;
923 }
924
David Brazdildee58d62016-04-07 09:54:26 +0000925 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
926
927 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700928 MaybeRecordStat(compilation_stats_,
929 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100930 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
931 number_of_arguments,
932 return_type,
933 dex_pc,
934 method_idx,
935 invoke_type);
Orion Hodson20e7a6e2018-05-18 08:33:20 +0100936 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000937 }
938
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100939 // Replace calls to String.<init> with StringFactory.
940 if (IsStringConstructor(resolved_method)) {
941 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
942 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
943 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
944 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000945 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100946 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000947 ScopedObjectAccess soa(Thread::Current());
948 MethodReference target_method(resolved_method->GetDexFile(),
949 resolved_method->GetDexMethodIndex());
950 // We pass null for the resolved_method to ensure optimizations
951 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
953 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100954 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100955 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100956 dex_pc,
957 method_idx,
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000958 nullptr /* resolved_method */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100959 dispatch_info,
960 invoke_type,
961 target_method,
962 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Orion Hodson20e7a6e2018-05-18 08:33:20 +0100963 return HandleStringInit(invoke, operands, shorty);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100964 }
965
David Brazdildee58d62016-04-07 09:54:26 +0000966 // Potential class initialization check, in the case of a static method call.
967 HClinitCheck* clinit_check = nullptr;
968 HInvoke* invoke = nullptr;
969 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
970 // By default, consider that the called method implicitly requires
971 // an initialization check of its declaring method.
972 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
973 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
974 ScopedObjectAccess soa(Thread::Current());
975 if (invoke_type == kStatic) {
976 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000977 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000978 } else if (invoke_type == kSuper) {
979 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100980 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000981 // we resolved to the method referenced by the instruction.
982 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000983 }
984 }
985
986 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100987 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000988 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000989 0u
David Brazdildee58d62016-04-07 09:54:26 +0000990 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100991 MethodReference target_method(resolved_method->GetDexFile(),
992 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100993 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
994 number_of_arguments,
995 return_type,
996 dex_pc,
997 method_idx,
998 resolved_method,
999 dispatch_info,
1000 invoke_type,
1001 target_method,
1002 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +00001003 } else if (invoke_type == kVirtual) {
1004 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +01001005 invoke = new (allocator_) HInvokeVirtual(allocator_,
1006 number_of_arguments,
1007 return_type,
1008 dex_pc,
1009 method_idx,
1010 resolved_method,
1011 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +00001012 } else {
1013 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001014 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 invoke = new (allocator_) HInvokeInterface(allocator_,
1016 number_of_arguments,
1017 return_type,
1018 dex_pc,
1019 method_idx,
1020 resolved_method,
1021 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001022 }
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001023 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false, clinit_check);
David Brazdildee58d62016-04-07 09:54:26 +00001024}
1025
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001026bool HInstructionBuilder::BuildInvokePolymorphic(uint32_t dex_pc,
Orion Hodsonac141392017-01-13 11:53:47 +00001027 uint32_t method_idx,
Orion Hodson16b2adf2018-05-14 08:53:38 +01001028 dex::ProtoIndex proto_idx,
Treehugger Robota9e5f952018-05-17 22:26:08 +00001029 const InstructionOperands& operands) {
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001030 const char* shorty = dex_file_->GetShorty(proto_idx);
1031 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(shorty), operands.GetNumberOfOperands());
1032 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1033 size_t number_of_arguments = strlen(shorty);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001034 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1035 number_of_arguments,
1036 return_type,
1037 dex_pc,
1038 method_idx);
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001039 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false);
1040}
1041
1042
1043bool HInstructionBuilder::BuildInvokeCustom(uint32_t dex_pc,
1044 uint32_t call_site_idx,
1045 const InstructionOperands& operands) {
1046 dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
1047 const char* shorty = dex_file_->GetShorty(proto_idx);
1048 DataType::Type return_type = DataType::FromShorty(shorty[0]);
1049 size_t number_of_arguments = strlen(shorty) - 1;
1050 HInvoke* invoke = new (allocator_) HInvokeCustom(allocator_,
1051 number_of_arguments,
1052 call_site_idx,
1053 return_type,
1054 dex_pc);
1055 return HandleInvoke(invoke, operands, shorty, /* is_unresolved */ false);
Orion Hodsonac141392017-01-13 11:53:47 +00001056}
1057
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001058HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001059 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001060
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001061 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001062
David Brazdildee58d62016-04-07 09:54:26 +00001063 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001064 Handle<mirror::Class> klass = load_class->GetClass();
1065
1066 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001067 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001068 AppendInstruction(cls);
1069 }
1070
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001071 // Only the access check entrypoint handles the finalizable class case. If we
1072 // need access checks, then we haven't resolved the method and the class may
1073 // again be finalizable.
1074 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1075 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1076 entrypoint = kQuickAllocObjectWithChecks;
1077 }
1078
1079 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001080 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001081
Vladimir Markoca6fff82017-10-03 14:49:14 +01001082 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001083 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001084 dex_pc,
1085 type_index,
1086 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001087 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001088 entrypoint);
1089 AppendInstruction(new_instance);
1090
1091 return new_instance;
1092}
1093
1094void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1095 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001096 (allocation->IsNewInstance() ||
1097 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001098
1099 if (allocation->IsNewInstance()) {
1100 // STRING SPECIAL HANDLING:
1101 // -------------------------------
1102 // Strings have a real HNewInstance node but they end up always having 0 uses.
1103 // All uses of a String HNewInstance are always transformed to replace their input
1104 // of the HNewInstance with an input of the invoke to StringFactory.
1105 //
1106 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1107 // optimizations (to pass checker tests that rely on those optimizations).
1108 HNewInstance* new_inst = allocation->AsNewInstance();
1109 HLoadClass* load_class = new_inst->GetLoadClass();
1110
1111 Thread* self = Thread::Current();
1112 ScopedObjectAccess soa(self);
1113 StackHandleScope<1> hs(self);
1114 Handle<mirror::Class> klass = load_class->GetClass();
1115 if (klass != nullptr && klass->IsStringClass()) {
1116 return;
1117 // Note: Do not use allocation->IsStringAlloc which requires
1118 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1119 // propagation (and instruction builder is too early).
1120 }
1121 // (In terms of correctness, the StringFactory needs to provide its own
1122 // default initialization barrier, see below.)
1123 }
1124
1125 // JLS 17.4.5 "Happens-before Order" describes:
1126 //
1127 // The default initialization of any object happens-before any other actions (other than
1128 // default-writes) of a program.
1129 //
1130 // In our implementation the default initialization of an object to type T means
1131 // setting all of its initial data (object[0..size)) to 0, and setting the
1132 // object's class header (i.e. object.getClass() == T.class).
1133 //
1134 // In practice this fence ensures that the writes to the object header
1135 // are visible to other threads if this object escapes the current thread.
1136 // (and in theory the 0-initializing, but that happens automatically
1137 // when new memory pages are mapped in by the OS).
1138 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001139 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001140 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001141 MaybeRecordStat(
1142 compilation_stats_,
1143 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001144}
1145
Vladimir Marko28e012a2017-12-07 11:22:59 +00001146static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001147 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001148 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1149}
1150
1151bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001152 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001153 return false;
1154 }
1155
1156 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1157 // check whether the class is in an image for the AOT compilation.
1158 if (cls->IsInitialized() &&
1159 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1160 return true;
1161 }
1162
1163 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1164 return true;
1165 }
1166
1167 // TODO: We should walk over the inlined methods, but we don't pass
1168 // that information to the builder.
1169 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1170 return true;
1171 }
1172
1173 return false;
1174}
1175
1176HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1177 uint32_t dex_pc,
1178 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001179 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001180 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001181
1182 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001183 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001184 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001185 } else {
1186 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1187 klass->GetDexFile(),
1188 klass,
1189 dex_pc,
1190 /* needs_access_check */ false);
1191 if (cls != nullptr) {
1192 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001193 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001194 AppendInstruction(clinit_check);
1195 }
David Brazdildee58d62016-04-07 09:54:26 +00001196 }
1197 return clinit_check;
1198}
1199
1200bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
Treehugger Robota9e5f952018-05-17 22:26:08 +00001201 const InstructionOperands& operands,
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001202 const char* shorty,
David Brazdildee58d62016-04-07 09:54:26 +00001203 size_t start_index,
1204 size_t* argument_index) {
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001205 uint32_t shorty_index = 1; // Skip the return type.
Treehugger Robota9e5f952018-05-17 22:26:08 +00001206 const size_t number_of_operands = operands.GetNumberOfOperands();
David Brazdildee58d62016-04-07 09:54:26 +00001207 for (size_t i = start_index;
1208 // Make sure we don't go over the expected arguments or over the number of
1209 // dex registers given. If the instruction was seen as dead by the verifier,
1210 // it hasn't been properly checked.
Treehugger Robota9e5f952018-05-17 22:26:08 +00001211 (i < number_of_operands) && (*argument_index < invoke->GetNumberOfArguments());
David Brazdildee58d62016-04-07 09:54:26 +00001212 i++, (*argument_index)++) {
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001213 DataType::Type type = DataType::FromShorty(shorty[shorty_index++]);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robota9e5f952018-05-17 22:26:08 +00001215 if (is_wide && ((i + 1 == number_of_operands) ||
1216 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
David Brazdildee58d62016-04-07 09:54:26 +00001217 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1218 // reject any class where this is violated. However, the verifier only does these checks
1219 // on non trivially dead instructions, so we just bailout the compilation.
1220 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001221 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001222 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001223 MaybeRecordStat(compilation_stats_,
1224 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001225 return false;
1226 }
Treehugger Robota9e5f952018-05-17 22:26:08 +00001227 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001228 invoke->SetArgumentAt(*argument_index, arg);
1229 if (is_wide) {
1230 i++;
1231 }
1232 }
1233
1234 if (*argument_index != invoke->GetNumberOfArguments()) {
1235 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001236 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001237 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001238 MaybeRecordStat(compilation_stats_,
1239 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001240 return false;
1241 }
1242
1243 if (invoke->IsInvokeStaticOrDirect() &&
1244 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1245 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1246 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1247 (*argument_index)++;
1248 }
1249
1250 return true;
1251}
1252
1253bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robota9e5f952018-05-17 22:26:08 +00001254 const InstructionOperands& operands,
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001255 const char* shorty,
1256 bool is_unresolved,
1257 HClinitCheck* clinit_check) {
David Brazdildee58d62016-04-07 09:54:26 +00001258 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1259
1260 size_t start_index = 0;
1261 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001262 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Treehugger Robota9e5f952018-05-17 22:26:08 +00001263 uint32_t obj_reg = operands.GetOperand(0);
Aart Bik296fbb42016-06-07 13:49:12 -07001264 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001265 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001266 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001267 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001268 start_index = 1;
1269 argument_index = 1;
1270 }
1271
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001272 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001273 return false;
1274 }
1275
1276 if (clinit_check != nullptr) {
1277 // Add the class initialization check as last input of `invoke`.
1278 DCHECK(invoke->IsInvokeStaticOrDirect());
1279 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1280 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1281 invoke->SetArgumentAt(argument_index, clinit_check);
1282 argument_index++;
1283 }
1284
1285 AppendInstruction(invoke);
1286 latest_result_ = invoke;
1287
1288 return true;
1289}
1290
1291bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robota9e5f952018-05-17 22:26:08 +00001292 const InstructionOperands& operands,
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001293 const char* shorty) {
David Brazdildee58d62016-04-07 09:54:26 +00001294 DCHECK(invoke->IsInvokeStaticOrDirect());
1295 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1296
1297 size_t start_index = 1;
1298 size_t argument_index = 0;
Orion Hodson20e7a6e2018-05-18 08:33:20 +01001299 if (!SetupInvokeArguments(invoke, operands, shorty, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001300 return false;
1301 }
1302
1303 AppendInstruction(invoke);
1304
1305 // This is a StringFactory call, not an actual String constructor. Its result
1306 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robota9e5f952018-05-17 22:26:08 +00001307 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001308 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001309
1310 // Replacing the NewInstance might render it redundant. Keep a list of these
1311 // to be visited once it is clear whether it is has remaining uses.
1312 if (arg_this->IsNewInstance()) {
1313 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1314 } else {
1315 DCHECK(arg_this->IsPhi());
1316 // NewInstance is not the direct input of the StringFactory call. It might
1317 // be redundant but optimizing this case is not worth the effort.
1318 }
1319
1320 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1321 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1322 if ((*current_locals_)[vreg] == arg_this) {
1323 (*current_locals_)[vreg] = invoke;
1324 }
1325 }
1326
1327 return true;
1328}
1329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001330static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001331 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1332 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001334}
1335
1336bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1337 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001338 bool is_put,
1339 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001340 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1341 uint32_t obj_reg = instruction.VRegB_22c();
1342 uint16_t field_index;
1343 if (instruction.IsQuickened()) {
1344 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001345 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1346 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001347 return false;
1348 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001349 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001350 } else {
1351 field_index = instruction.VRegC_22c();
1352 }
1353
1354 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001355 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001356
Aart Bik14154132016-06-02 17:53:58 -07001357 // Generate an explicit null check on the reference, unless the field access
1358 // is unresolved. In that case, we rely on the runtime to perform various
1359 // checks first, followed by a null check.
1360 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001361 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001362 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001364 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001365 if (is_put) {
1366 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1367 HInstruction* field_set = nullptr;
1368 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001369 MaybeRecordStat(compilation_stats_,
1370 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001371 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1372 value,
1373 field_type,
1374 field_index,
1375 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001376 } else {
1377 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001378 field_set = new (allocator_) HInstanceFieldSet(object,
1379 value,
1380 resolved_field,
1381 field_type,
1382 resolved_field->GetOffset(),
1383 resolved_field->IsVolatile(),
1384 field_index,
1385 class_def_index,
1386 *dex_file_,
1387 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001388 }
1389 AppendInstruction(field_set);
1390 } else {
1391 HInstruction* field_get = nullptr;
1392 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001393 MaybeRecordStat(compilation_stats_,
1394 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001395 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1396 field_type,
1397 field_index,
1398 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001399 } else {
1400 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001401 field_get = new (allocator_) HInstanceFieldGet(object,
1402 resolved_field,
1403 field_type,
1404 resolved_field->GetOffset(),
1405 resolved_field->IsVolatile(),
1406 field_index,
1407 class_def_index,
1408 *dex_file_,
1409 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001410 }
1411 AppendInstruction(field_get);
1412 UpdateLocal(source_or_dest_reg, field_get);
1413 }
1414
1415 return true;
1416}
1417
Vladimir Marko28e012a2017-12-07 11:22:59 +00001418static ObjPtr<mirror::Class> GetClassFrom(CompilerDriver* driver,
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001419 const DexCompilationUnit& compilation_unit) {
David Brazdildee58d62016-04-07 09:54:26 +00001420 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001421 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001422 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001423
1424 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1425}
1426
Vladimir Marko28e012a2017-12-07 11:22:59 +00001427ObjPtr<mirror::Class> HInstructionBuilder::GetOutermostCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001428 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1429}
1430
Vladimir Marko28e012a2017-12-07 11:22:59 +00001431ObjPtr<mirror::Class> HInstructionBuilder::GetCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001432 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1433}
1434
Andreas Gampea5b09a62016-11-17 15:21:22 -08001435bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001436 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001437 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001438 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001439 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001440 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1441 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1442 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1443
1444 // GetOutermostCompilingClass returns null when the class is unresolved
1445 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1446 // we are compiling it.
1447 // When this happens we cannot establish a direct relation between the current
1448 // class and the outer class, so we return false.
1449 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001450 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001451}
1452
1453void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001454 uint32_t dex_pc,
1455 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001456 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001457 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1458 uint16_t field_index = instruction.VRegB_21c();
1459
1460 if (is_put) {
1461 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1462 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001463 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001464 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001465 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001466 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1467 }
1468}
1469
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001470ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1471 ScopedObjectAccess soa(Thread::Current());
1472 StackHandleScope<2> hs(soa.Self());
1473
1474 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001475 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001476 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1477
Vladimir Markoe11dd502017-12-08 14:09:45 +00001478 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001479 dex_compilation_unit_->GetDexCache(),
1480 class_loader,
1481 is_static);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001482 if (UNLIKELY(resolved_field == nullptr)) {
1483 // Clean up any exception left by type resolution.
1484 soa.Self()->ClearException();
1485 return nullptr;
1486 }
1487
1488 // Check static/instance. The class linker has a fast path for looking into the dex cache
1489 // and does not check static/instance if it hits it.
1490 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1491 return nullptr;
1492 }
1493
1494 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001495 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001496 if (!resolved_field->IsPublic()) {
1497 return nullptr;
1498 }
1499 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1500 resolved_field,
1501 dex_compilation_unit_->GetDexCache().Get(),
1502 field_idx)) {
1503 return nullptr;
1504 }
1505
1506 if (is_put &&
1507 resolved_field->IsFinal() &&
1508 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1509 // Final fields can only be updated within their own class.
1510 // TODO: Only allow it in constructors. b/34966607.
1511 return nullptr;
1512 }
1513
1514 return resolved_field;
1515}
1516
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001517void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001518 uint32_t dex_pc,
1519 bool is_put) {
1520 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1521 uint16_t field_index = instruction.VRegB_21c();
1522
1523 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001524 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001525
1526 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001527 MaybeRecordStat(compilation_stats_,
1528 MethodCompilationStat::kUnresolvedField);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001529 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001530 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001531 return;
David Brazdildee58d62016-04-07 09:54:26 +00001532 }
1533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001534 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001535
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001536 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1537 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1538 klass->GetDexFile(),
1539 klass,
1540 dex_pc,
1541 /* needs_access_check */ false);
1542
1543 if (constant == nullptr) {
1544 // The class cannot be referenced from this compiled code. Generate
1545 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001546 MaybeRecordStat(compilation_stats_,
1547 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001548 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001549 return;
David Brazdildee58d62016-04-07 09:54:26 +00001550 }
1551
David Brazdildee58d62016-04-07 09:54:26 +00001552 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001553 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001554 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001555 AppendInstruction(cls);
1556 }
1557
1558 uint16_t class_def_index = klass->GetDexClassDefIndex();
1559 if (is_put) {
1560 // We need to keep the class alive before loading the value.
1561 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1562 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001563 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1564 value,
1565 resolved_field,
1566 field_type,
1567 resolved_field->GetOffset(),
1568 resolved_field->IsVolatile(),
1569 field_index,
1570 class_def_index,
1571 *dex_file_,
1572 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001573 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001574 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1575 resolved_field,
1576 field_type,
1577 resolved_field->GetOffset(),
1578 resolved_field->IsVolatile(),
1579 field_index,
1580 class_def_index,
1581 *dex_file_,
1582 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001583 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1584 }
David Brazdildee58d62016-04-07 09:54:26 +00001585}
1586
1587void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001588 uint16_t first_vreg,
1589 int64_t second_vreg_or_constant,
1590 uint32_t dex_pc,
1591 DataType::Type type,
1592 bool second_is_constant,
1593 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001594 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001595
1596 HInstruction* first = LoadLocal(first_vreg, type);
1597 HInstruction* second = nullptr;
1598 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001599 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001600 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1601 } else {
1602 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1603 }
1604 } else {
1605 second = LoadLocal(second_vreg_or_constant, type);
1606 }
1607
1608 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1610 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001611 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001612 AppendInstruction(second);
1613 }
1614
1615 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001616 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001617 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001618 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001619 }
1620 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1621}
1622
1623void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1624 uint32_t dex_pc,
1625 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001627 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1628 uint8_t array_reg = instruction.VRegB_23x();
1629 uint8_t index_reg = instruction.VRegC_23x();
1630
David Brazdilc120bbe2016-04-22 16:57:00 +01001631 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001632 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001633 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001634 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001635 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001636 AppendInstruction(index);
1637 if (is_put) {
1638 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1639 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001640 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001641 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1642 AppendInstruction(aset);
1643 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001644 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001645 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1646 AppendInstruction(aget);
1647 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1648 }
1649 graph_->SetHasBoundsChecks(true);
1650}
1651
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001652HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1653 dex::TypeIndex type_index,
Treehugger Robota9e5f952018-05-17 22:26:08 +00001654 const InstructionOperands& operands) {
1655 const size_t number_of_operands = operands.GetNumberOfOperands();
1656 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001657 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001658 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001659 AppendInstruction(object);
1660
1661 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1662 DCHECK_EQ(descriptor[0], '[') << descriptor;
1663 char primitive = descriptor[1];
1664 DCHECK(primitive == 'I'
1665 || primitive == 'L'
1666 || primitive == '[') << descriptor;
1667 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001668 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001669
Treehugger Robota9e5f952018-05-17 22:26:08 +00001670 for (size_t i = 0; i < number_of_operands; ++i) {
1671 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001672 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001673 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001674 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1675 AppendInstruction(aset);
1676 }
1677 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001678
1679 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001680}
1681
1682template <typename T>
1683void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1684 const T* data,
1685 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001687 uint32_t dex_pc) {
1688 for (uint32_t i = 0; i < element_count; ++i) {
1689 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1690 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001691 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001692 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1693 AppendInstruction(aset);
1694 }
1695}
1696
1697void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001698 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001699
1700 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1701 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001702 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1703 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001704 const uint8_t* data = payload->data;
1705 uint32_t element_count = payload->element_count;
1706
Vladimir Markoc69fba22016-09-06 16:49:15 +01001707 if (element_count == 0u) {
1708 // For empty payload we emit only the null check above.
1709 return;
1710 }
1711
Vladimir Markoca6fff82017-10-03 14:49:14 +01001712 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001713 AppendInstruction(length);
1714
David Brazdildee58d62016-04-07 09:54:26 +00001715 // Implementation of this DEX instruction seems to be that the bounds check is
1716 // done before doing any stores.
1717 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001718 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001719
1720 switch (payload->element_width) {
1721 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001722 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001723 reinterpret_cast<const int8_t*>(data),
1724 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001725 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001726 dex_pc);
1727 break;
1728 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001729 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001730 reinterpret_cast<const int16_t*>(data),
1731 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001732 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001733 dex_pc);
1734 break;
1735 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001736 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001737 reinterpret_cast<const int32_t*>(data),
1738 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001740 dex_pc);
1741 break;
1742 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001743 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001744 reinterpret_cast<const int64_t*>(data),
1745 element_count,
1746 dex_pc);
1747 break;
1748 default:
1749 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1750 }
1751 graph_->SetHasBoundsChecks(true);
1752}
1753
1754void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1755 const int64_t* data,
1756 uint32_t element_count,
1757 uint32_t dex_pc) {
1758 for (uint32_t i = 0; i < element_count; ++i) {
1759 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1760 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001761 HArraySet* aset =
1762 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001763 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1764 AppendInstruction(aset);
1765 }
1766}
1767
Vladimir Marko28e012a2017-12-07 11:22:59 +00001768void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1769 HLoadString* load_string =
1770 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1771 HSharpening::ProcessLoadString(load_string,
1772 code_generator_,
1773 compiler_driver_,
1774 *dex_compilation_unit_,
1775 handles_);
1776 AppendInstruction(load_string);
1777}
1778
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001779HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001780 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001781 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Markoaa6fb672018-03-27 09:03:13 +00001782 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
1783 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001784 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1785}
1786
1787HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1788 const DexFile& dex_file,
1789 Handle<mirror::Class> klass,
1790 uint32_t dex_pc,
1791 bool needs_access_check) {
1792 // Try to find a reference in the compiling dex file.
1793 const DexFile* actual_dex_file = &dex_file;
1794 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1795 dex::TypeIndex local_type_index =
1796 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1797 if (local_type_index.IsValid()) {
1798 type_index = local_type_index;
1799 actual_dex_file = dex_compilation_unit_->GetDexFile();
1800 }
1801 }
1802
1803 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001804 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001805 graph_->GetCurrentMethod(),
1806 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001807 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001808 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001809 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001810 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001811 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001812
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001813 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1814 code_generator_,
1815 compiler_driver_,
1816 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001817
1818 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1819 // We actually cannot reference this class, we're forced to bail.
1820 return nullptr;
1821 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00001822 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001823 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001824 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001825 return load_class;
1826}
1827
Vladimir Markoaa6fb672018-03-27 09:03:13 +00001828Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
1829 dex::TypeIndex type_index) {
1830 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
1831 ObjPtr<mirror::Class> klass = compiler_driver_->ResolveClass(
1832 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_);
1833 // TODO: Avoid creating excessive handles if the method references the same class repeatedly.
1834 // (Use a map on the local_allocator_.)
1835 return handles_->NewHandle(klass);
1836}
1837
1838bool HInstructionBuilder::LoadClassNeedsAccessCheck(Handle<mirror::Class> klass) {
1839 if (klass == nullptr) {
1840 return true;
1841 } else if (klass->IsPublic()) {
1842 return false;
1843 } else {
1844 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
1845 return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
1846 }
1847}
1848
Orion Hodson16b2adf2018-05-14 08:53:38 +01001849void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodson75046d12018-05-10 08:22:46 +01001850 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson16b2adf2018-05-14 08:53:38 +01001851 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
1852 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodson75046d12018-05-10 08:22:46 +01001853 AppendInstruction(load_method_handle);
1854}
1855
Orion Hodson16b2adf2018-05-14 08:53:38 +01001856void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodsonb426afb2018-04-12 11:18:23 +01001857 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1858 HLoadMethodType* load_method_type =
Orion Hodson16b2adf2018-05-14 08:53:38 +01001859 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodsonb426afb2018-04-12 11:18:23 +01001860 AppendInstruction(load_method_type);
1861}
1862
David Brazdildee58d62016-04-07 09:54:26 +00001863void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1864 uint8_t destination,
1865 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001866 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001867 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001868 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001869
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001870 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoaa6fb672018-03-27 09:03:13 +00001871 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1872 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
1873 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
1874 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
1875 klass.Get(), code_generator_, compiler_driver_, needs_access_check);
1876
1877 HInstruction* class_or_null = nullptr;
1878 HIntConstant* bitstring_path_to_root = nullptr;
1879 HIntConstant* bitstring_mask = nullptr;
1880 if (check_kind == TypeCheckKind::kBitstringCheck) {
1881 // TODO: Allow using the bitstring check also if we need an access check.
1882 DCHECK(!needs_access_check);
1883 class_or_null = graph_->GetNullConstant(dex_pc);
1884 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
1885 uint32_t path_to_root =
1886 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
1887 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
1888 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
1889 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
1890 } else {
1891 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1892 }
1893 DCHECK(class_or_null != nullptr);
1894
David Brazdildee58d62016-04-07 09:54:26 +00001895 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoaa6fb672018-03-27 09:03:13 +00001896 AppendInstruction(new (allocator_) HInstanceOf(object,
1897 class_or_null,
1898 check_kind,
1899 klass,
1900 dex_pc,
1901 allocator_,
1902 bitstring_path_to_root,
1903 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00001904 UpdateLocal(destination, current_block_->GetLastInstruction());
1905 } else {
1906 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1907 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1908 // which may throw. If it succeeds BoundType sets the new type of `object`
1909 // for all subsequent uses.
Vladimir Markoaa6fb672018-03-27 09:03:13 +00001910 AppendInstruction(
1911 new (allocator_) HCheckCast(object,
1912 class_or_null,
1913 check_kind,
1914 klass,
1915 dex_pc,
1916 allocator_,
1917 bitstring_path_to_root,
1918 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001919 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001920 UpdateLocal(reference, current_block_->GetLastInstruction());
1921 }
1922}
1923
Vladimir Marko0b66d612017-03-13 14:50:04 +00001924bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001925 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1926 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001927}
1928
1929bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001930 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001931}
1932
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001933uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1934 DCHECK(CanDecodeQuickenedInfo());
1935 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001936}
1937
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001938bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1939 uint32_t dex_pc,
1940 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001941 switch (instruction.Opcode()) {
1942 case Instruction::CONST_4: {
1943 int32_t register_index = instruction.VRegA();
1944 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1945 UpdateLocal(register_index, constant);
1946 break;
1947 }
1948
1949 case Instruction::CONST_16: {
1950 int32_t register_index = instruction.VRegA();
1951 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1952 UpdateLocal(register_index, constant);
1953 break;
1954 }
1955
1956 case Instruction::CONST: {
1957 int32_t register_index = instruction.VRegA();
1958 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1959 UpdateLocal(register_index, constant);
1960 break;
1961 }
1962
1963 case Instruction::CONST_HIGH16: {
1964 int32_t register_index = instruction.VRegA();
1965 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1966 UpdateLocal(register_index, constant);
1967 break;
1968 }
1969
1970 case Instruction::CONST_WIDE_16: {
1971 int32_t register_index = instruction.VRegA();
1972 // Get 16 bits of constant value, sign extended to 64 bits.
1973 int64_t value = instruction.VRegB_21s();
1974 value <<= 48;
1975 value >>= 48;
1976 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1977 UpdateLocal(register_index, constant);
1978 break;
1979 }
1980
1981 case Instruction::CONST_WIDE_32: {
1982 int32_t register_index = instruction.VRegA();
1983 // Get 32 bits of constant value, sign extended to 64 bits.
1984 int64_t value = instruction.VRegB_31i();
1985 value <<= 32;
1986 value >>= 32;
1987 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1988 UpdateLocal(register_index, constant);
1989 break;
1990 }
1991
1992 case Instruction::CONST_WIDE: {
1993 int32_t register_index = instruction.VRegA();
1994 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1995 UpdateLocal(register_index, constant);
1996 break;
1997 }
1998
1999 case Instruction::CONST_WIDE_HIGH16: {
2000 int32_t register_index = instruction.VRegA();
2001 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2002 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2003 UpdateLocal(register_index, constant);
2004 break;
2005 }
2006
2007 // Note that the SSA building will refine the types.
2008 case Instruction::MOVE:
2009 case Instruction::MOVE_FROM16:
2010 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002012 UpdateLocal(instruction.VRegA(), value);
2013 break;
2014 }
2015
2016 // Note that the SSA building will refine the types.
2017 case Instruction::MOVE_WIDE:
2018 case Instruction::MOVE_WIDE_FROM16:
2019 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002021 UpdateLocal(instruction.VRegA(), value);
2022 break;
2023 }
2024
2025 case Instruction::MOVE_OBJECT:
2026 case Instruction::MOVE_OBJECT_16:
2027 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002028 // The verifier has no notion of a null type, so a move-object of constant 0
2029 // will lead to the same constant 0 in the destination register. To mimic
2030 // this behavior, we just pretend we haven't seen a type change (int to reference)
2031 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2032 // types correct.
2033 uint32_t reg_number = instruction.VRegB();
2034 HInstruction* value = (*current_locals_)[reg_number];
2035 if (value->IsIntConstant()) {
2036 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2037 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002038 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2039 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002040 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002042 }
David Brazdildee58d62016-04-07 09:54:26 +00002043 UpdateLocal(instruction.VRegA(), value);
2044 break;
2045 }
2046
2047 case Instruction::RETURN_VOID_NO_BARRIER:
2048 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002050 break;
2051 }
2052
2053#define IF_XX(comparison, cond) \
2054 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2055 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2056
2057 IF_XX(HEqual, EQ);
2058 IF_XX(HNotEqual, NE);
2059 IF_XX(HLessThan, LT);
2060 IF_XX(HLessThanOrEqual, LE);
2061 IF_XX(HGreaterThan, GT);
2062 IF_XX(HGreaterThanOrEqual, GE);
2063
2064 case Instruction::GOTO:
2065 case Instruction::GOTO_16:
2066 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002067 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002068 current_block_ = nullptr;
2069 break;
2070 }
2071
2072 case Instruction::RETURN: {
2073 BuildReturn(instruction, return_type_, dex_pc);
2074 break;
2075 }
2076
2077 case Instruction::RETURN_OBJECT: {
2078 BuildReturn(instruction, return_type_, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::RETURN_WIDE: {
2083 BuildReturn(instruction, return_type_, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::INVOKE_DIRECT:
2088 case Instruction::INVOKE_INTERFACE:
2089 case Instruction::INVOKE_STATIC:
2090 case Instruction::INVOKE_SUPER:
2091 case Instruction::INVOKE_VIRTUAL:
2092 case Instruction::INVOKE_VIRTUAL_QUICK: {
2093 uint16_t method_idx;
2094 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2095 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002096 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2097 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002098 return false;
2099 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002100 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002101 } else {
2102 method_idx = instruction.VRegB_35c();
2103 }
David Brazdildee58d62016-04-07 09:54:26 +00002104 uint32_t args[5];
Treehugger Robota9e5f952018-05-17 22:26:08 +00002105 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2106 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2107 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002108 return false;
2109 }
2110 break;
2111 }
2112
2113 case Instruction::INVOKE_DIRECT_RANGE:
2114 case Instruction::INVOKE_INTERFACE_RANGE:
2115 case Instruction::INVOKE_STATIC_RANGE:
2116 case Instruction::INVOKE_SUPER_RANGE:
2117 case Instruction::INVOKE_VIRTUAL_RANGE:
2118 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2119 uint16_t method_idx;
2120 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2121 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002122 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2123 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002124 return false;
2125 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002126 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002127 } else {
2128 method_idx = instruction.VRegB_3rc();
2129 }
Treehugger Robota9e5f952018-05-17 22:26:08 +00002130 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2131 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002132 return false;
2133 }
2134 break;
2135 }
2136
Orion Hodsonac141392017-01-13 11:53:47 +00002137 case Instruction::INVOKE_POLYMORPHIC: {
2138 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson16b2adf2018-05-14 08:53:38 +01002139 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002140 uint32_t args[5];
Treehugger Robota9e5f952018-05-17 22:26:08 +00002141 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2142 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
Orion Hodson20e7a6e2018-05-18 08:33:20 +01002143 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002144 }
2145
2146 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2147 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson16b2adf2018-05-14 08:53:38 +01002148 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robota9e5f952018-05-17 22:26:08 +00002149 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
Orion Hodson20e7a6e2018-05-18 08:33:20 +01002150 return BuildInvokePolymorphic(dex_pc, method_idx, proto_idx, operands);
2151 }
2152
2153 case Instruction::INVOKE_CUSTOM: {
2154 uint16_t call_site_idx = instruction.VRegB_35c();
2155 uint32_t args[5];
2156 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2157 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2158 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
2159 }
2160
2161 case Instruction::INVOKE_CUSTOM_RANGE: {
2162 uint16_t call_site_idx = instruction.VRegB_3rc();
2163 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2164 return BuildInvokeCustom(dex_pc, call_site_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002165 }
2166
David Brazdildee58d62016-04-07 09:54:26 +00002167 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002168 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002169 break;
2170 }
2171
2172 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002174 break;
2175 }
2176
2177 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002179 break;
2180 }
2181
2182 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002184 break;
2185 }
2186
2187 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002189 break;
2190 }
2191
2192 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002194 break;
2195 }
2196
2197 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002199 break;
2200 }
2201
2202 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002204 break;
2205 }
2206
2207 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002209 break;
2210 }
2211
2212 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002214 break;
2215 }
2216
2217 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002219 break;
2220 }
2221
2222 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002224 break;
2225 }
2226
2227 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002229 break;
2230 }
2231
2232 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002234 break;
2235 }
2236
2237 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002239 break;
2240 }
2241
2242 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002244 break;
2245 }
2246
2247 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002248 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002249 break;
2250 }
2251
2252 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002254 break;
2255 }
2256
2257 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002259 break;
2260 }
2261
2262 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002264 break;
2265 }
2266
2267 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002269 break;
2270 }
2271
2272 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002274 break;
2275 }
2276
2277 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002279 break;
2280 }
2281
2282 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002284 break;
2285 }
2286
2287 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002289 break;
2290 }
2291
2292 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002294 break;
2295 }
2296
2297 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002299 break;
2300 }
2301
2302 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002304 break;
2305 }
2306
2307 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002309 break;
2310 }
2311
2312 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002314 break;
2315 }
2316
2317 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002319 break;
2320 }
2321
2322 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002323 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002324 break;
2325 }
2326
2327 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002329 break;
2330 }
2331
2332 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002334 break;
2335 }
2336
2337 case Instruction::DIV_INT: {
2338 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002339 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002340 break;
2341 }
2342
2343 case Instruction::DIV_LONG: {
2344 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002346 break;
2347 }
2348
2349 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002350 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002351 break;
2352 }
2353
2354 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002356 break;
2357 }
2358
2359 case Instruction::REM_INT: {
2360 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002362 break;
2363 }
2364
2365 case Instruction::REM_LONG: {
2366 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002368 break;
2369 }
2370
2371 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002373 break;
2374 }
2375
2376 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002378 break;
2379 }
2380
2381 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002382 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002383 break;
2384 }
2385
2386 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002388 break;
2389 }
2390
2391 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002392 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002393 break;
2394 }
2395
2396 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002398 break;
2399 }
2400
2401 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002403 break;
2404 }
2405
2406 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002408 break;
2409 }
2410
2411 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002413 break;
2414 }
2415
2416 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002418 break;
2419 }
2420
2421 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002423 break;
2424 }
2425
2426 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002428 break;
2429 }
2430
2431 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002433 break;
2434 }
2435
2436 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002438 break;
2439 }
2440
2441 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002443 break;
2444 }
2445
2446 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002447 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002448 break;
2449 }
2450
2451 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002453 break;
2454 }
2455
2456 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002458 break;
2459 }
2460
2461 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002463 break;
2464 }
2465
2466 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002468 break;
2469 }
2470
2471 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002472 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002473 break;
2474 }
2475
2476 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002478 break;
2479 }
2480
2481 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002483 break;
2484 }
2485
2486 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002488 break;
2489 }
2490
2491 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002492 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002493 break;
2494 }
2495
2496 case Instruction::DIV_INT_2ADDR: {
2497 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002499 break;
2500 }
2501
2502 case Instruction::DIV_LONG_2ADDR: {
2503 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002505 break;
2506 }
2507
2508 case Instruction::REM_INT_2ADDR: {
2509 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002511 break;
2512 }
2513
2514 case Instruction::REM_LONG_2ADDR: {
2515 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002517 break;
2518 }
2519
2520 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002522 break;
2523 }
2524
2525 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002527 break;
2528 }
2529
2530 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002532 break;
2533 }
2534
2535 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002537 break;
2538 }
2539
2540 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002542 break;
2543 }
2544
2545 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002547 break;
2548 }
2549
2550 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002552 break;
2553 }
2554
2555 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002557 break;
2558 }
2559
2560 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002562 break;
2563 }
2564
2565 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002567 break;
2568 }
2569
2570 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002572 break;
2573 }
2574
2575 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002577 break;
2578 }
2579
2580 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002581 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002582 break;
2583 }
2584
2585 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002586 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002587 break;
2588 }
2589
2590 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002592 break;
2593 }
2594
2595 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002596 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002597 break;
2598 }
2599
2600 case Instruction::ADD_INT_LIT16: {
2601 Binop_22s<HAdd>(instruction, false, dex_pc);
2602 break;
2603 }
2604
2605 case Instruction::AND_INT_LIT16: {
2606 Binop_22s<HAnd>(instruction, false, dex_pc);
2607 break;
2608 }
2609
2610 case Instruction::OR_INT_LIT16: {
2611 Binop_22s<HOr>(instruction, false, dex_pc);
2612 break;
2613 }
2614
2615 case Instruction::XOR_INT_LIT16: {
2616 Binop_22s<HXor>(instruction, false, dex_pc);
2617 break;
2618 }
2619
2620 case Instruction::RSUB_INT: {
2621 Binop_22s<HSub>(instruction, true, dex_pc);
2622 break;
2623 }
2624
2625 case Instruction::MUL_INT_LIT16: {
2626 Binop_22s<HMul>(instruction, false, dex_pc);
2627 break;
2628 }
2629
2630 case Instruction::ADD_INT_LIT8: {
2631 Binop_22b<HAdd>(instruction, false, dex_pc);
2632 break;
2633 }
2634
2635 case Instruction::AND_INT_LIT8: {
2636 Binop_22b<HAnd>(instruction, false, dex_pc);
2637 break;
2638 }
2639
2640 case Instruction::OR_INT_LIT8: {
2641 Binop_22b<HOr>(instruction, false, dex_pc);
2642 break;
2643 }
2644
2645 case Instruction::XOR_INT_LIT8: {
2646 Binop_22b<HXor>(instruction, false, dex_pc);
2647 break;
2648 }
2649
2650 case Instruction::RSUB_INT_LIT8: {
2651 Binop_22b<HSub>(instruction, true, dex_pc);
2652 break;
2653 }
2654
2655 case Instruction::MUL_INT_LIT8: {
2656 Binop_22b<HMul>(instruction, false, dex_pc);
2657 break;
2658 }
2659
2660 case Instruction::DIV_INT_LIT16:
2661 case Instruction::DIV_INT_LIT8: {
2662 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002664 break;
2665 }
2666
2667 case Instruction::REM_INT_LIT16:
2668 case Instruction::REM_INT_LIT8: {
2669 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002671 break;
2672 }
2673
2674 case Instruction::SHL_INT_LIT8: {
2675 Binop_22b<HShl>(instruction, false, dex_pc);
2676 break;
2677 }
2678
2679 case Instruction::SHR_INT_LIT8: {
2680 Binop_22b<HShr>(instruction, false, dex_pc);
2681 break;
2682 }
2683
2684 case Instruction::USHR_INT_LIT8: {
2685 Binop_22b<HUShr>(instruction, false, dex_pc);
2686 break;
2687 }
2688
2689 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002690 HNewInstance* new_instance =
2691 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2692 DCHECK(new_instance != nullptr);
2693
David Brazdildee58d62016-04-07 09:54:26 +00002694 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002695 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002696 break;
2697 }
2698
2699 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002700 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002702 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002703
Vladimir Markoca6fff82017-10-03 14:49:14 +01002704 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002705 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002706 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002707 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002708 break;
2709 }
2710
2711 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002712 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002713 uint32_t args[5];
Treehugger Robota9e5f952018-05-17 22:26:08 +00002714 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2715 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2716 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002717 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002718 break;
2719 }
2720
2721 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002722 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robota9e5f952018-05-17 22:26:08 +00002723 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2724 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002725 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002726 break;
2727 }
2728
2729 case Instruction::FILL_ARRAY_DATA: {
2730 BuildFillArrayData(instruction, dex_pc);
2731 break;
2732 }
2733
2734 case Instruction::MOVE_RESULT:
2735 case Instruction::MOVE_RESULT_WIDE:
2736 case Instruction::MOVE_RESULT_OBJECT: {
2737 DCHECK(latest_result_ != nullptr);
2738 UpdateLocal(instruction.VRegA(), latest_result_);
2739 latest_result_ = nullptr;
2740 break;
2741 }
2742
2743 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002745 break;
2746 }
2747
2748 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002750 break;
2751 }
2752
2753 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002755 break;
2756 }
2757
2758 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002760 break;
2761 }
2762
2763 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002765 break;
2766 }
2767
2768 case Instruction::NOP:
2769 break;
2770
2771 case Instruction::IGET:
2772 case Instruction::IGET_QUICK:
2773 case Instruction::IGET_WIDE:
2774 case Instruction::IGET_WIDE_QUICK:
2775 case Instruction::IGET_OBJECT:
2776 case Instruction::IGET_OBJECT_QUICK:
2777 case Instruction::IGET_BOOLEAN:
2778 case Instruction::IGET_BOOLEAN_QUICK:
2779 case Instruction::IGET_BYTE:
2780 case Instruction::IGET_BYTE_QUICK:
2781 case Instruction::IGET_CHAR:
2782 case Instruction::IGET_CHAR_QUICK:
2783 case Instruction::IGET_SHORT:
2784 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002785 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002786 return false;
2787 }
2788 break;
2789 }
2790
2791 case Instruction::IPUT:
2792 case Instruction::IPUT_QUICK:
2793 case Instruction::IPUT_WIDE:
2794 case Instruction::IPUT_WIDE_QUICK:
2795 case Instruction::IPUT_OBJECT:
2796 case Instruction::IPUT_OBJECT_QUICK:
2797 case Instruction::IPUT_BOOLEAN:
2798 case Instruction::IPUT_BOOLEAN_QUICK:
2799 case Instruction::IPUT_BYTE:
2800 case Instruction::IPUT_BYTE_QUICK:
2801 case Instruction::IPUT_CHAR:
2802 case Instruction::IPUT_CHAR_QUICK:
2803 case Instruction::IPUT_SHORT:
2804 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002805 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002806 return false;
2807 }
2808 break;
2809 }
2810
2811 case Instruction::SGET:
2812 case Instruction::SGET_WIDE:
2813 case Instruction::SGET_OBJECT:
2814 case Instruction::SGET_BOOLEAN:
2815 case Instruction::SGET_BYTE:
2816 case Instruction::SGET_CHAR:
2817 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002818 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00002819 break;
2820 }
2821
2822 case Instruction::SPUT:
2823 case Instruction::SPUT_WIDE:
2824 case Instruction::SPUT_OBJECT:
2825 case Instruction::SPUT_BOOLEAN:
2826 case Instruction::SPUT_BYTE:
2827 case Instruction::SPUT_CHAR:
2828 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002829 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002830 break;
2831 }
2832
2833#define ARRAY_XX(kind, anticipated_type) \
2834 case Instruction::AGET##kind: { \
2835 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2836 break; \
2837 } \
2838 case Instruction::APUT##kind: { \
2839 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2840 break; \
2841 }
2842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 ARRAY_XX(, DataType::Type::kInt32);
2844 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2845 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2846 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2847 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2848 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2849 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002850
2851 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002852 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002853 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002854 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2855 break;
2856 }
2857
2858 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002859 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002860 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002861 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2862 break;
2863 }
2864
2865 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002866 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002867 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002868 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2869 break;
2870 }
2871
2872 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002873 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002874 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002875 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2876 break;
2877 }
2878
Orion Hodson75046d12018-05-10 08:22:46 +01002879 case Instruction::CONST_METHOD_HANDLE: {
2880 uint16_t method_handle_idx = instruction.VRegB_21c();
2881 BuildLoadMethodHandle(method_handle_idx, dex_pc);
2882 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2883 break;
2884 }
2885
Orion Hodsonb426afb2018-04-12 11:18:23 +01002886 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson16b2adf2018-05-14 08:53:38 +01002887 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodsonb426afb2018-04-12 11:18:23 +01002888 BuildLoadMethodType(proto_idx, dex_pc);
2889 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2890 break;
2891 }
2892
David Brazdildee58d62016-04-07 09:54:26 +00002893 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002894 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002895 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002896 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002897 break;
2898 }
2899
2900 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002902 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002903 // We finished building this block. Set the current block to null to avoid
2904 // adding dead instructions to it.
2905 current_block_ = nullptr;
2906 break;
2907 }
2908
2909 case Instruction::INSTANCE_OF: {
2910 uint8_t destination = instruction.VRegA_22c();
2911 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002912 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002913 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2914 break;
2915 }
2916
2917 case Instruction::CHECK_CAST: {
2918 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002919 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002920 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2921 break;
2922 }
2923
2924 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002925 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002927 HMonitorOperation::OperationKind::kEnter,
2928 dex_pc));
2929 break;
2930 }
2931
2932 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002933 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002935 HMonitorOperation::OperationKind::kExit,
2936 dex_pc));
2937 break;
2938 }
2939
2940 case Instruction::SPARSE_SWITCH:
2941 case Instruction::PACKED_SWITCH: {
2942 BuildSwitch(instruction, dex_pc);
2943 break;
2944 }
2945
Orion Hodson20e7a6e2018-05-18 08:33:20 +01002946 case Instruction::UNUSED_3E:
2947 case Instruction::UNUSED_3F:
2948 case Instruction::UNUSED_40:
2949 case Instruction::UNUSED_41:
2950 case Instruction::UNUSED_42:
2951 case Instruction::UNUSED_43:
2952 case Instruction::UNUSED_79:
2953 case Instruction::UNUSED_7A:
2954 case Instruction::UNUSED_F3:
2955 case Instruction::UNUSED_F4:
2956 case Instruction::UNUSED_F5:
2957 case Instruction::UNUSED_F6:
2958 case Instruction::UNUSED_F7:
2959 case Instruction::UNUSED_F8:
2960 case Instruction::UNUSED_F9: {
David Brazdildee58d62016-04-07 09:54:26 +00002961 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002962 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002963 << " because of unhandled instruction "
2964 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002965 MaybeRecordStat(compilation_stats_,
2966 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002967 return false;
Orion Hodson20e7a6e2018-05-18 08:33:20 +01002968 }
David Brazdildee58d62016-04-07 09:54:26 +00002969 }
2970 return true;
2971} // NOLINT(readability/fn_size)
2972
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002973ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2974 dex::TypeIndex type_index,
2975 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002976 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002977 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2978}
2979
2980ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2981 // TODO: Cache the result in a Handle<mirror::Class>.
2982 const DexFile::MethodId& method_id =
2983 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2984 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2985}
2986
David Brazdildee58d62016-04-07 09:54:26 +00002987} // namespace art