blob: 887f4f6e41ffe16fdaaaa86ebdfc2b781f634426 [file] [log] [blame]
David Brazdildee58d62016-04-07 09:54:26 +00001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "instruction_builder.h"
18
Matthew Gharrity465ecc82016-07-19 21:32:52 +000019#include "art_method-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010020#include "base/arena_bit_vector.h"
21#include "base/bit_vector-inl.h"
22#include "block_builder.h"
David Brazdildee58d62016-04-07 09:54:26 +000023#include "class_linker.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010024#include "data_type-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070025#include "dex/bytecode_utils.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_instruction-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010027#include "driver/compiler_driver-inl.h"
28#include "driver/dex_compilation_unit.h"
David Brazdildee58d62016-04-07 09:54:26 +000029#include "driver/compiler_options.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070030#include "imtable-inl.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010031#include "mirror/dex_cache.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000032#include "oat_file.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010033#include "optimizing_compiler_stats.h"
Mathieu Chartierde4b08f2017-07-10 14:13:41 -070034#include "quicken_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "sharpening.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010037#include "ssa_builder.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070038#include "well_known_classes.h"
David Brazdildee58d62016-04-07 09:54:26 +000039
40namespace art {
41
Mathieu Chartier808c7a52017-12-15 11:19:33 -080042HInstructionBuilder::HInstructionBuilder(HGraph* graph,
43 HBasicBlockBuilder* block_builder,
44 SsaBuilder* ssa_builder,
45 const DexFile* dex_file,
46 const CodeItemDebugInfoAccessor& accessor,
47 DataType::Type return_type,
48 const DexCompilationUnit* dex_compilation_unit,
49 const DexCompilationUnit* outer_compilation_unit,
50 CompilerDriver* compiler_driver,
51 CodeGenerator* code_generator,
Mathieu Chartier210531f2018-01-12 10:15:51 -080052 ArrayRef<const uint8_t> interpreter_metadata,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080053 OptimizingCompilerStats* compiler_stats,
54 VariableSizedHandleScope* handles,
55 ScopedArenaAllocator* local_allocator)
56 : allocator_(graph->GetAllocator()),
57 graph_(graph),
58 handles_(handles),
59 dex_file_(dex_file),
60 code_item_accessor_(accessor),
61 return_type_(return_type),
62 block_builder_(block_builder),
63 ssa_builder_(ssa_builder),
64 compiler_driver_(compiler_driver),
65 code_generator_(code_generator),
66 dex_compilation_unit_(dex_compilation_unit),
67 outer_compilation_unit_(outer_compilation_unit),
68 quicken_info_(interpreter_metadata),
69 compilation_stats_(compiler_stats),
70 local_allocator_(local_allocator),
71 locals_for_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
72 current_block_(nullptr),
73 current_locals_(nullptr),
74 latest_result_(nullptr),
75 current_this_parameter_(nullptr),
76 loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
77 loop_headers_.reserve(kDefaultNumberOfLoops);
78}
79
David Brazdildee58d62016-04-07 09:54:26 +000080HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
81 return block_builder_->GetBlockAt(dex_pc);
82}
83
Vladimir Marko69d310e2017-10-09 14:12:23 +010084inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
85 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000086 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080087 if (locals->size() == vregs) {
88 return locals;
89 }
90 return GetLocalsForWithAllocation(block, locals, vregs);
91}
David Brazdildee58d62016-04-07 09:54:26 +000092
Vladimir Marko69d310e2017-10-09 14:12:23 +010093ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080094 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010095 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080096 const size_t vregs) {
97 DCHECK_NE(locals->size(), vregs);
98 locals->resize(vregs, nullptr);
99 if (block->IsCatchBlock()) {
100 // We record incoming inputs of catch phis at throwing instructions and
101 // must therefore eagerly create the phis. Phis for undefined vregs will
102 // be deleted when the first throwing instruction with the vreg undefined
103 // is encountered. Unused phis will be removed by dead phi analysis.
104 for (size_t i = 0; i < vregs; ++i) {
105 // No point in creating the catch phi if it is already undefined at
106 // the first throwing instruction.
107 HInstruction* current_local_value = (*current_locals_)[i];
108 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109 HPhi* phi = new (allocator_) HPhi(
110 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -0800111 i,
112 0,
113 current_local_value->GetType());
114 block->AddPhi(phi);
115 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +0000116 }
117 }
118 }
119 return locals;
120}
121
Mingyao Yang01b47b02017-02-03 12:09:57 -0800122inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100123 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +0000124 return (*locals)[local];
125}
126
127void HInstructionBuilder::InitializeBlockLocals() {
128 current_locals_ = GetLocalsFor(current_block_);
129
130 if (current_block_->IsCatchBlock()) {
131 // Catch phis were already created and inputs collected from throwing sites.
132 if (kIsDebugBuild) {
133 // Make sure there was at least one throwing instruction which initialized
134 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
135 // visited already (from HTryBoundary scoping and reverse post order).
136 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100137 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000138 if (current == current_block_) {
139 catch_block_visited = true;
140 } else if (current->IsTryBlock()) {
141 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
142 if (try_entry.HasExceptionHandler(*current_block_)) {
143 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
144 }
145 }
146 }
147 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
148 << "No instructions throwing into a live catch block.";
149 }
150 } else if (current_block_->IsLoopHeader()) {
151 // If the block is a loop header, we know we only have visited the pre header
152 // because we are visiting in reverse post order. We create phis for all initialized
153 // locals from the pre header. Their inputs will be populated at the end of
154 // the analysis.
155 for (size_t local = 0; local < current_locals_->size(); ++local) {
156 HInstruction* incoming =
157 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
158 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HPhi* phi = new (allocator_) HPhi(
160 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000161 local,
162 0,
163 incoming->GetType());
164 current_block_->AddPhi(phi);
165 (*current_locals_)[local] = phi;
166 }
167 }
168
169 // Save the loop header so that the last phase of the analysis knows which
170 // blocks need to be updated.
171 loop_headers_.push_back(current_block_);
172 } else if (current_block_->GetPredecessors().size() > 0) {
173 // All predecessors have already been visited because we are visiting in reverse post order.
174 // We merge the values of all locals, creating phis if those values differ.
175 for (size_t local = 0; local < current_locals_->size(); ++local) {
176 bool one_predecessor_has_no_value = false;
177 bool is_different = false;
178 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
179
180 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
181 HInstruction* current = ValueOfLocalAt(predecessor, local);
182 if (current == nullptr) {
183 one_predecessor_has_no_value = true;
184 break;
185 } else if (current != value) {
186 is_different = true;
187 }
188 }
189
190 if (one_predecessor_has_no_value) {
191 // If one predecessor has no value for this local, we trust the verifier has
192 // successfully checked that there is a store dominating any read after this block.
193 continue;
194 }
195
196 if (is_different) {
197 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100198 HPhi* phi = new (allocator_) HPhi(
199 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000200 local,
201 current_block_->GetPredecessors().size(),
202 first_input->GetType());
203 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
204 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
205 phi->SetRawInputAt(i, pred_value);
206 }
207 current_block_->AddPhi(phi);
208 value = phi;
209 }
210 (*current_locals_)[local] = value;
211 }
212 }
213}
214
215void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
216 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
217 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100218 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000219 DCHECK_EQ(handler_locals->size(), current_locals_->size());
220 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
221 HInstruction* handler_value = (*handler_locals)[vreg];
222 if (handler_value == nullptr) {
223 // Vreg was undefined at a previously encountered throwing instruction
224 // and the catch phi was deleted. Do not record the local value.
225 continue;
226 }
227 DCHECK(handler_value->IsPhi());
228
229 HInstruction* local_value = (*current_locals_)[vreg];
230 if (local_value == nullptr) {
231 // This is the first instruction throwing into `catch_block` where
232 // `vreg` is undefined. Delete the catch phi.
233 catch_block->RemovePhi(handler_value->AsPhi());
234 (*handler_locals)[vreg] = nullptr;
235 } else {
236 // Vreg has been defined at all instructions throwing into `catch_block`
237 // encountered so far. Record the local value in the catch phi.
238 handler_value->AsPhi()->AddInput(local_value);
239 }
240 }
241 }
242}
243
244void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
245 current_block_->AddInstruction(instruction);
246 InitializeInstruction(instruction);
247}
248
249void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
250 if (current_block_->GetInstructions().IsEmpty()) {
251 current_block_->AddInstruction(instruction);
252 } else {
253 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
254 }
255 InitializeInstruction(instruction);
256}
257
258void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
259 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100260 HEnvironment* environment = new (allocator_) HEnvironment(
261 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000262 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000263 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000264 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000265 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100266 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000267 instruction->SetRawEnvironment(environment);
268 }
269}
270
David Brazdilc120bbe2016-04-22 16:57:00 +0100271HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100272 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100273 if (!ref->CanBeNull()) {
274 return ref;
275 }
276
Vladimir Markoca6fff82017-10-03 14:49:14 +0100277 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100278 AppendInstruction(null_check);
279 return null_check;
280}
281
David Brazdildee58d62016-04-07 09:54:26 +0000282void HInstructionBuilder::SetLoopHeaderPhiInputs() {
283 for (size_t i = loop_headers_.size(); i > 0; --i) {
284 HBasicBlock* block = loop_headers_[i - 1];
285 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
286 HPhi* phi = it.Current()->AsPhi();
287 size_t vreg = phi->GetRegNumber();
288 for (HBasicBlock* predecessor : block->GetPredecessors()) {
289 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
290 if (value == nullptr) {
291 // Vreg is undefined at this predecessor. Mark it dead and leave with
292 // fewer inputs than predecessors. SsaChecker will fail if not removed.
293 phi->SetDead();
294 break;
295 } else {
296 phi->AddInput(value);
297 }
298 }
299 }
300 }
301}
302
303static bool IsBlockPopulated(HBasicBlock* block) {
304 if (block->IsLoopHeader()) {
305 // Suspend checks were inserted into loop headers during building of dominator tree.
306 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
307 return block->GetFirstInstruction() != block->GetLastInstruction();
308 } else {
309 return !block->GetInstructions().IsEmpty();
310 }
311}
312
313bool HInstructionBuilder::Build() {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800314 DCHECK(code_item_accessor_.HasCodeItem());
Vladimir Marko69d310e2017-10-09 14:12:23 +0100315 locals_for_.resize(
316 graph_->GetBlocks().size(),
317 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000318
319 // Find locations where we want to generate extra stackmaps for native debugging.
320 // This allows us to generate the info only at interesting points (for example,
321 // at start of java statement) rather than before every dex instruction.
322 const bool native_debuggable = compiler_driver_ != nullptr &&
323 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
324 ArenaBitVector* native_debug_info_locations = nullptr;
325 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100326 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000327 }
328
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100329 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
330 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000331 uint32_t block_dex_pc = current_block_->GetDexPc();
332
333 InitializeBlockLocals();
334
335 if (current_block_->IsEntryBlock()) {
336 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100337 AppendInstruction(new (allocator_) HSuspendCheck(0u));
338 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000339 continue;
340 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100341 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000342 continue;
343 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000345 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
346 // This is slightly odd because the loop header might not be empty (TryBoundary).
347 // But we're still creating the environment with locals from the top of the block.
348 InsertInstructionAtTop(suspend_check);
349 }
350
351 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
352 // Synthetic block that does not need to be populated.
353 DCHECK(IsBlockPopulated(current_block_));
354 continue;
355 }
356
357 DCHECK(!IsBlockPopulated(current_block_));
358
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700359 uint32_t quicken_index = 0;
360 if (CanDecodeQuickenedInfo()) {
361 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
362 }
363
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800364 for (const DexInstructionPcPair& pair : code_item_accessor_.InstructionsFrom(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000365 if (current_block_ == nullptr) {
366 // The previous instruction ended this block.
367 break;
368 }
369
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800370 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000371 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
372 // This dex_pc starts a new basic block.
373 break;
374 }
375
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800376 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000377 PropagateLocalsToCatchBlocks();
378 }
379
380 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100381 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000382 }
383
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800384 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000385 return false;
386 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700387
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800388 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700389 ++quicken_index;
390 }
David Brazdildee58d62016-04-07 09:54:26 +0000391 }
392
393 if (current_block_ != nullptr) {
394 // Branching instructions clear current_block, so we know the last
395 // instruction of the current block is not a branching instruction.
396 // We add an unconditional Goto to the next block.
397 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100398 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000399 }
400 }
401
402 SetLoopHeaderPhiInputs();
403
404 return true;
405}
406
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000407void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800408 DCHECK(!code_item_accessor_.HasCodeItem());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000409 DCHECK(method->IsIntrinsic());
410
411 locals_for_.resize(
412 graph_->GetBlocks().size(),
413 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
414
415 // Fill the entry block. Do not add suspend check, we do not want a suspend
416 // check in intrinsics; intrinsic methods are supposed to be fast.
417 current_block_ = graph_->GetEntryBlock();
418 InitializeBlockLocals();
419 InitializeParameters();
420 AppendInstruction(new (allocator_) HGoto(0u));
421
422 // Fill the body.
423 current_block_ = current_block_->GetSingleSuccessor();
424 InitializeBlockLocals();
425 DCHECK(!IsBlockPopulated(current_block_));
426
427 // Add the invoke and return instruction. Use HInvokeStaticOrDirect even
428 // for methods that would normally use an HInvokeVirtual (sharpen the call).
429 size_t in_vregs = graph_->GetNumberOfInVRegs();
430 size_t number_of_arguments =
431 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
432 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
433 MethodReference target_method(dex_file_, method_idx);
434 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
435 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
436 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
437 /* method_load_data */ 0u
438 };
439 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
440 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
441 allocator_,
442 number_of_arguments,
443 return_type_,
444 kNoDexPc,
445 method_idx,
446 method,
447 dispatch_info,
448 invoke_type,
449 target_method,
450 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000451 RangeInstructionOperands operands(graph_->GetNumberOfVRegs() - in_vregs, in_vregs);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000452 HandleInvoke(invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000453 operands,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000454 dex_file_->GetMethodShorty(method_idx),
455 /* clinit_check */ nullptr,
456 /* is_unresolved */ false);
457
458 // Add the return instruction.
459 if (return_type_ == DataType::Type::kVoid) {
460 AppendInstruction(new (allocator_) HReturnVoid());
461 } else {
462 AppendInstruction(new (allocator_) HReturn(invoke));
463 }
464
465 // Fill the exit block.
466 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
467 current_block_ = graph_->GetExitBlock();
468 InitializeBlockLocals();
469 AppendInstruction(new (allocator_) HExit());
470}
471
Vladimir Marko69d310e2017-10-09 14:12:23 +0100472ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
David Brazdildee58d62016-04-07 09:54:26 +0000473 // The callback gets called when the line number changes.
474 // In other words, it marks the start of new java statement.
475 struct Callback {
476 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
477 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
478 return false;
479 }
480 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100481 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800482 code_item_accessor_.InsnsSizeInCodeUnits(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100483 /* expandable */ false,
484 kArenaAllocGraphBuilder);
485 locations->ClearAllBits();
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800486 dex_file_->DecodeDebugPositionInfo(code_item_accessor_.DebugInfoOffset(),
487 Callback::Position,
488 locations);
David Brazdildee58d62016-04-07 09:54:26 +0000489 // Instruction-specific tweaks.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800490 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800491 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000492 case Instruction::MOVE_EXCEPTION: {
493 // Stop in native debugger after the exception has been moved.
494 // The compiler also expects the move at the start of basic block so
495 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800496 locations->ClearBit(inst.DexPc());
497 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
498 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800499 if (next != code_item_accessor_.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700500 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000501 }
502 break;
503 }
504 default:
505 break;
506 }
507 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100508 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000509}
510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100511HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000512 HInstruction* value = (*current_locals_)[reg_number];
513 DCHECK(value != nullptr);
514
515 // If the operation requests a specific type, we make sure its input is of that type.
516 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100517 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700518 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100519 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700520 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000521 }
Aart Bik31883642016-06-06 15:02:44 -0700522 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000523 }
524
525 return value;
526}
527
528void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 DataType::Type stored_type = stored_value->GetType();
530 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000531
532 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
533 // registers. Consider the following cases:
534 // (1) Storing a wide value must overwrite previous values in both `reg_number`
535 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
536 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
537 // must invalidate it. We store `nullptr` in `reg_number-1`.
538 // Consequently, storing a wide value into the high vreg of another wide value
539 // will invalidate both `reg_number-1` and `reg_number+1`.
540
541 if (reg_number != 0) {
542 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100543 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000544 // The vreg we are storing into was previously the high vreg of a pair.
545 // We need to invalidate its low vreg.
546 DCHECK((*current_locals_)[reg_number] == nullptr);
547 (*current_locals_)[reg_number - 1] = nullptr;
548 }
549 }
550
551 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100552 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000553 // We are storing a pair. Invalidate the instruction in the high vreg.
554 (*current_locals_)[reg_number + 1] = nullptr;
555 }
556}
557
558void HInstructionBuilder::InitializeParameters() {
559 DCHECK(current_block_->IsEntryBlock());
560
Vladimir Marko69d310e2017-10-09 14:12:23 +0100561 // outer_compilation_unit_ is null only when unit testing.
562 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000563 return;
564 }
565
566 const char* shorty = dex_compilation_unit_->GetShorty();
567 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
568 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
569 uint16_t parameter_index = 0;
570
571 const DexFile::MethodId& referrer_method_id =
572 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
573 if (!dex_compilation_unit_->IsStatic()) {
574 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100575 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000576 referrer_method_id.class_idx_,
577 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100578 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700579 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000580 AppendInstruction(parameter);
581 UpdateLocal(locals_index++, parameter);
582 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700583 current_this_parameter_ = parameter;
584 } else {
585 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000586 }
587
588 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
589 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
590 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100591 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000592 *dex_file_,
593 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
594 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100595 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700596 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000597 ++shorty_pos;
598 AppendInstruction(parameter);
599 // Store the parameter value in the local that the dex code will use
600 // to reference that parameter.
601 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100602 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000603 i++;
604 locals_index++;
605 parameter_index++;
606 }
607 }
608}
609
610template<typename T>
611void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100612 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
613 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100614 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000615 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000617 current_block_ = nullptr;
618}
619
620template<typename T>
621void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100622 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100623 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000624 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100625 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000626 current_block_ = nullptr;
627}
628
629template<typename T>
630void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100631 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000632 uint32_t dex_pc) {
633 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100634 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000635 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
636}
637
638void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100639 DataType::Type input_type,
640 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000641 uint32_t dex_pc) {
642 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100643 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000644 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
645}
646
647template<typename T>
648void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100649 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000650 uint32_t dex_pc) {
651 HInstruction* first = LoadLocal(instruction.VRegB(), type);
652 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000654 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
655}
656
657template<typename T>
658void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100659 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000660 uint32_t dex_pc) {
661 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100662 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100663 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000664 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
665}
666
667void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100668 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000669 ComparisonBias bias,
670 uint32_t dex_pc) {
671 HInstruction* first = LoadLocal(instruction.VRegB(), type);
672 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100673 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000674 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
675}
676
677template<typename T>
678void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100679 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000680 uint32_t dex_pc) {
681 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100682 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100683 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000684 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
685}
686
687template<typename T>
688void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100689 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000690 uint32_t dex_pc) {
691 HInstruction* first = LoadLocal(instruction.VRegA(), type);
692 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100693 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000694 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
695}
696
697template<typename T>
698void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100699 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000700 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
701 if (reverse) {
702 std::swap(first, second);
703 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100704 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000705 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
706}
707
708template<typename T>
709void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100710 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000711 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
712 if (reverse) {
713 std::swap(first, second);
714 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100715 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000716 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
717}
718
Igor Murashkind01745e2017-04-05 16:40:31 -0700719// Does the method being compiled need any constructor barriers being inserted?
720// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700721static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700722 // Can be null in unit tests only.
723 if (UNLIKELY(cu == nullptr)) {
724 return false;
725 }
726
David Brazdildee58d62016-04-07 09:54:26 +0000727 Thread* self = Thread::Current();
728 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700729 && !cu->IsStatic()
730 // RequiresConstructorBarrier must only be queried for <init> methods;
731 // it's effectively "false" for every other method.
732 //
733 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700734 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000735}
736
737// Returns true if `block` has only one successor which starts at the next
738// dex_pc after `instruction` at `dex_pc`.
739static bool IsFallthroughInstruction(const Instruction& instruction,
740 uint32_t dex_pc,
741 HBasicBlock* block) {
742 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
743 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
744}
745
746void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100747 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000748 DexSwitchTable table(instruction, dex_pc);
749
750 if (table.GetNumEntries() == 0) {
751 // Empty Switch. Code falls through to the next block.
752 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100753 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000754 } else if (table.ShouldBuildDecisionTree()) {
755 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
756 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100757 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000758 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000760
761 if (!it.IsLast()) {
762 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
763 }
764 }
765 } else {
766 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100767 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000768 }
769
770 current_block_ = nullptr;
771}
772
773void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100774 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000775 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100776 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700777 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700778 // This may insert additional redundant constructor fences from the super constructors.
779 // TODO: remove redundant constructor fences (b/36656456).
780 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700781 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100782 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700783
784 HInstruction* fence_target = current_this_parameter_;
785 DCHECK(fence_target != nullptr);
786
Vladimir Markoca6fff82017-10-03 14:49:14 +0100787 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700788 MaybeRecordStat(
789 compilation_stats_,
790 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000791 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100792 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000793 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700794 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000795 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100796 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000797 }
798 current_block_ = nullptr;
799}
800
801static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
802 switch (opcode) {
803 case Instruction::INVOKE_STATIC:
804 case Instruction::INVOKE_STATIC_RANGE:
805 return kStatic;
806 case Instruction::INVOKE_DIRECT:
807 case Instruction::INVOKE_DIRECT_RANGE:
808 return kDirect;
809 case Instruction::INVOKE_VIRTUAL:
810 case Instruction::INVOKE_VIRTUAL_QUICK:
811 case Instruction::INVOKE_VIRTUAL_RANGE:
812 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
813 return kVirtual;
814 case Instruction::INVOKE_INTERFACE:
815 case Instruction::INVOKE_INTERFACE_RANGE:
816 return kInterface;
817 case Instruction::INVOKE_SUPER_RANGE:
818 case Instruction::INVOKE_SUPER:
819 return kSuper;
820 default:
821 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
822 UNREACHABLE();
823 }
824}
825
826ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
827 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000828
829 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000830 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100831
Vladimir Markoba118822017-06-12 15:41:56 +0100832 ArtMethod* resolved_method =
833 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100834 method_idx,
835 dex_compilation_unit_->GetDexCache(),
836 class_loader,
837 graph_->GetArtMethod(),
838 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000839
840 if (UNLIKELY(resolved_method == nullptr)) {
841 // Clean up any exception left by type resolution.
842 soa.Self()->ClearException();
843 return nullptr;
844 }
845
Vladimir Markoba118822017-06-12 15:41:56 +0100846 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
847 // resolved because, for example, we don't find a superclass in the classpath.
848 if (graph_->GetArtMethod() == nullptr) {
849 // The class linker cannot check access without a referrer, so we have to do it.
850 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000851 if (!resolved_method->IsPublic()) {
852 return nullptr;
853 }
David Brazdildee58d62016-04-07 09:54:26 +0000854 }
855
856 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
857 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
858 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
859 // which require runtime handling.
860 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100861 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800862 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000863 // We could not determine the method's class we need to wait until runtime.
864 DCHECK(Runtime::Current()->IsAotCompiler());
865 return nullptr;
866 }
Vladimir Markoba118822017-06-12 15:41:56 +0100867 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100868 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
869 dex_compilation_unit_->GetDexCache().Get(),
870 class_loader.Get());
871 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
872 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700873 // We cannot statically determine the target method. The runtime will throw a
874 // NoSuchMethodError on this one.
875 return nullptr;
876 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100877 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100878 if (referenced_class->IsInterface()) {
879 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100880 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000881 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100882 uint16_t vtable_index = resolved_method->GetMethodIndex();
883 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
884 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000885 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100886 if (actual_method != resolved_method &&
887 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
888 // The back-end code generator relies on this check in order to ensure that it will not
889 // attempt to read the dex_cache with a dex_method_index that is not from the correct
890 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
891 // builder, which means that the code-generator (and compiler driver during sharpening and
892 // inliner, maybe) might invoke an incorrect method.
893 // TODO: The actual method could still be referenced in the current dex file, so we
894 // could try locating it.
895 // TODO: Remove the dex_file restriction.
896 return nullptr;
897 }
898 if (!actual_method->IsInvokable()) {
899 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
900 // could resolve the callee to the wrong method.
901 return nullptr;
902 }
903 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000904 }
905
David Brazdildee58d62016-04-07 09:54:26 +0000906 return resolved_method;
907}
908
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100909static bool IsStringConstructor(ArtMethod* method) {
910 ScopedObjectAccess soa(Thread::Current());
911 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
912}
913
David Brazdildee58d62016-04-07 09:54:26 +0000914bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
915 uint32_t dex_pc,
916 uint32_t method_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000917 const InstructionOperands& operands) {
David Brazdildee58d62016-04-07 09:54:26 +0000918 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
919 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100920 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000921
922 // Remove the return type from the 'proto'.
923 size_t number_of_arguments = strlen(descriptor) - 1;
924 if (invoke_type != kStatic) { // instance call
925 // One extra argument for 'this'.
926 number_of_arguments++;
927 }
928
David Brazdildee58d62016-04-07 09:54:26 +0000929 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
930
931 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700932 MaybeRecordStat(compilation_stats_,
933 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100934 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
935 number_of_arguments,
936 return_type,
937 dex_pc,
938 method_idx,
939 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000940 return HandleInvoke(invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000941 operands,
David Brazdildee58d62016-04-07 09:54:26 +0000942 descriptor,
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000943 nullptr /* clinit_check */,
Aart Bik296fbb42016-06-07 13:49:12 -0700944 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000945 }
946
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100947 // Replace calls to String.<init> with StringFactory.
948 if (IsStringConstructor(resolved_method)) {
949 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
950 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
951 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
952 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000953 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100954 };
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000955 ScopedObjectAccess soa(Thread::Current());
956 MethodReference target_method(resolved_method->GetDexFile(),
957 resolved_method->GetDexMethodIndex());
958 // We pass null for the resolved_method to ensure optimizations
959 // don't rely on it.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
961 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100962 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100963 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100964 dex_pc,
965 method_idx,
Nicolas Geoffrayf665f842018-02-15 12:29:06 +0000966 nullptr /* resolved_method */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100967 dispatch_info,
968 invoke_type,
969 target_method,
970 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
Treehugger Robot2c5827a2018-05-17 22:26:08 +0000971 return HandleStringInit(invoke, operands, descriptor);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100972 }
973
David Brazdildee58d62016-04-07 09:54:26 +0000974 // Potential class initialization check, in the case of a static method call.
975 HClinitCheck* clinit_check = nullptr;
976 HInvoke* invoke = nullptr;
977 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
978 // By default, consider that the called method implicitly requires
979 // an initialization check of its declaring method.
980 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
981 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
982 ScopedObjectAccess soa(Thread::Current());
983 if (invoke_type == kStatic) {
984 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000985 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000986 } else if (invoke_type == kSuper) {
987 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100988 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000989 // we resolved to the method referenced by the instruction.
990 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000991 }
992 }
993
994 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100995 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000996 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000997 0u
David Brazdildee58d62016-04-07 09:54:26 +0000998 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100999 MethodReference target_method(resolved_method->GetDexFile(),
1000 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001001 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
1002 number_of_arguments,
1003 return_type,
1004 dex_pc,
1005 method_idx,
1006 resolved_method,
1007 dispatch_info,
1008 invoke_type,
1009 target_method,
1010 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +00001011 } else if (invoke_type == kVirtual) {
1012 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +01001013 invoke = new (allocator_) HInvokeVirtual(allocator_,
1014 number_of_arguments,
1015 return_type,
1016 dex_pc,
1017 method_idx,
1018 resolved_method,
1019 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +00001020 } else {
1021 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001022 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 invoke = new (allocator_) HInvokeInterface(allocator_,
1024 number_of_arguments,
1025 return_type,
1026 dex_pc,
1027 method_idx,
1028 resolved_method,
1029 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001030 }
1031
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001032 return HandleInvoke(invoke, operands, descriptor, clinit_check, false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +00001033}
1034
Orion Hodsonac141392017-01-13 11:53:47 +00001035bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
1036 uint32_t dex_pc,
1037 uint32_t method_idx,
Orion Hodson06d10a72018-05-14 08:53:38 +01001038 dex::ProtoIndex proto_idx,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001039 const InstructionOperands& operands) {
Orion Hodsonac141392017-01-13 11:53:47 +00001040 const char* descriptor = dex_file_->GetShorty(proto_idx);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001041 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), operands.GetNumberOfOperands());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001042 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +00001043 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1045 number_of_arguments,
1046 return_type,
1047 dex_pc,
1048 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00001049 return HandleInvoke(invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001050 operands,
Orion Hodsonac141392017-01-13 11:53:47 +00001051 descriptor,
1052 nullptr /* clinit_check */,
1053 false /* is_unresolved */);
1054}
1055
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001056HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001057 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001058
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001059 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001060
David Brazdildee58d62016-04-07 09:54:26 +00001061 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001062 Handle<mirror::Class> klass = load_class->GetClass();
1063
1064 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001065 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001066 AppendInstruction(cls);
1067 }
1068
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001069 // Only the access check entrypoint handles the finalizable class case. If we
1070 // need access checks, then we haven't resolved the method and the class may
1071 // again be finalizable.
1072 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1073 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1074 entrypoint = kQuickAllocObjectWithChecks;
1075 }
1076
1077 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001078 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001079
Vladimir Markoca6fff82017-10-03 14:49:14 +01001080 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001081 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001082 dex_pc,
1083 type_index,
1084 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001085 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001086 entrypoint);
1087 AppendInstruction(new_instance);
1088
1089 return new_instance;
1090}
1091
1092void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1093 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001094 (allocation->IsNewInstance() ||
1095 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001096
1097 if (allocation->IsNewInstance()) {
1098 // STRING SPECIAL HANDLING:
1099 // -------------------------------
1100 // Strings have a real HNewInstance node but they end up always having 0 uses.
1101 // All uses of a String HNewInstance are always transformed to replace their input
1102 // of the HNewInstance with an input of the invoke to StringFactory.
1103 //
1104 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1105 // optimizations (to pass checker tests that rely on those optimizations).
1106 HNewInstance* new_inst = allocation->AsNewInstance();
1107 HLoadClass* load_class = new_inst->GetLoadClass();
1108
1109 Thread* self = Thread::Current();
1110 ScopedObjectAccess soa(self);
1111 StackHandleScope<1> hs(self);
1112 Handle<mirror::Class> klass = load_class->GetClass();
1113 if (klass != nullptr && klass->IsStringClass()) {
1114 return;
1115 // Note: Do not use allocation->IsStringAlloc which requires
1116 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1117 // propagation (and instruction builder is too early).
1118 }
1119 // (In terms of correctness, the StringFactory needs to provide its own
1120 // default initialization barrier, see below.)
1121 }
1122
1123 // JLS 17.4.5 "Happens-before Order" describes:
1124 //
1125 // The default initialization of any object happens-before any other actions (other than
1126 // default-writes) of a program.
1127 //
1128 // In our implementation the default initialization of an object to type T means
1129 // setting all of its initial data (object[0..size)) to 0, and setting the
1130 // object's class header (i.e. object.getClass() == T.class).
1131 //
1132 // In practice this fence ensures that the writes to the object header
1133 // are visible to other threads if this object escapes the current thread.
1134 // (and in theory the 0-initializing, but that happens automatically
1135 // when new memory pages are mapped in by the OS).
1136 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001137 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001138 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001139 MaybeRecordStat(
1140 compilation_stats_,
1141 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001142}
1143
Vladimir Marko28e012a2017-12-07 11:22:59 +00001144static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001145 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001146 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1147}
1148
1149bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001150 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001151 return false;
1152 }
1153
1154 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1155 // check whether the class is in an image for the AOT compilation.
1156 if (cls->IsInitialized() &&
1157 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1158 return true;
1159 }
1160
1161 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1162 return true;
1163 }
1164
1165 // TODO: We should walk over the inlined methods, but we don't pass
1166 // that information to the builder.
1167 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1168 return true;
1169 }
1170
1171 return false;
1172}
1173
1174HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1175 uint32_t dex_pc,
1176 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001177 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001178 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001179
1180 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001181 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001182 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001183 } else {
1184 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1185 klass->GetDexFile(),
1186 klass,
1187 dex_pc,
1188 /* needs_access_check */ false);
1189 if (cls != nullptr) {
1190 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001191 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001192 AppendInstruction(clinit_check);
1193 }
David Brazdildee58d62016-04-07 09:54:26 +00001194 }
1195 return clinit_check;
1196}
1197
1198bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001199 const InstructionOperands& operands,
David Brazdildee58d62016-04-07 09:54:26 +00001200 const char* descriptor,
1201 size_t start_index,
1202 size_t* argument_index) {
1203 uint32_t descriptor_index = 1; // Skip the return type.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001204 const size_t number_of_operands = operands.GetNumberOfOperands();
David Brazdildee58d62016-04-07 09:54:26 +00001205 for (size_t i = start_index;
1206 // Make sure we don't go over the expected arguments or over the number of
1207 // dex registers given. If the instruction was seen as dead by the verifier,
1208 // it hasn't been properly checked.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001209 (i < number_of_operands) && (*argument_index < invoke->GetNumberOfArguments());
David Brazdildee58d62016-04-07 09:54:26 +00001210 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001211 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1212 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001213 if (is_wide && ((i + 1 == number_of_operands) ||
1214 (operands.GetOperand(i) + 1 != operands.GetOperand(i + 1)))) {
David Brazdildee58d62016-04-07 09:54:26 +00001215 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1216 // reject any class where this is violated. However, the verifier only does these checks
1217 // on non trivially dead instructions, so we just bailout the compilation.
1218 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001219 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001220 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001221 MaybeRecordStat(compilation_stats_,
1222 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001223 return false;
1224 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001225 HInstruction* arg = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001226 invoke->SetArgumentAt(*argument_index, arg);
1227 if (is_wide) {
1228 i++;
1229 }
1230 }
1231
1232 if (*argument_index != invoke->GetNumberOfArguments()) {
1233 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001234 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001235 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001236 MaybeRecordStat(compilation_stats_,
1237 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001238 return false;
1239 }
1240
1241 if (invoke->IsInvokeStaticOrDirect() &&
1242 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1243 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1244 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1245 (*argument_index)++;
1246 }
1247
1248 return true;
1249}
1250
1251bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001252 const InstructionOperands& operands,
David Brazdildee58d62016-04-07 09:54:26 +00001253 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001254 HClinitCheck* clinit_check,
1255 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001256 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1257
1258 size_t start_index = 0;
1259 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001260 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001261 uint32_t obj_reg = operands.GetOperand(0);
Aart Bik296fbb42016-06-07 13:49:12 -07001262 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001263 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001264 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001265 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001266 start_index = 1;
1267 argument_index = 1;
1268 }
1269
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001270 if (!SetupInvokeArguments(invoke, operands, descriptor, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001271 return false;
1272 }
1273
1274 if (clinit_check != nullptr) {
1275 // Add the class initialization check as last input of `invoke`.
1276 DCHECK(invoke->IsInvokeStaticOrDirect());
1277 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1278 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1279 invoke->SetArgumentAt(argument_index, clinit_check);
1280 argument_index++;
1281 }
1282
1283 AppendInstruction(invoke);
1284 latest_result_ = invoke;
1285
1286 return true;
1287}
1288
1289bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001290 const InstructionOperands& operands,
David Brazdildee58d62016-04-07 09:54:26 +00001291 const char* descriptor) {
1292 DCHECK(invoke->IsInvokeStaticOrDirect());
1293 DCHECK(invoke->AsInvokeStaticOrDirect()->IsStringInit());
1294
1295 size_t start_index = 1;
1296 size_t argument_index = 0;
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001297 if (!SetupInvokeArguments(invoke, operands, descriptor, start_index, &argument_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00001298 return false;
1299 }
1300
1301 AppendInstruction(invoke);
1302
1303 // This is a StringFactory call, not an actual String constructor. Its result
1304 // replaces the empty String pre-allocated by NewInstance.
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001305 uint32_t orig_this_reg = operands.GetOperand(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001306 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001307
1308 // Replacing the NewInstance might render it redundant. Keep a list of these
1309 // to be visited once it is clear whether it is has remaining uses.
1310 if (arg_this->IsNewInstance()) {
1311 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1312 } else {
Nicolas Geoffrayd147e2f2018-05-16 11:37:41 +01001313 // The only reason a HPhi can flow in a String.<init> is when there is an
1314 // irreducible loop, which will create HPhi for all dex registers at loop entry.
David Brazdildee58d62016-04-07 09:54:26 +00001315 DCHECK(arg_this->IsPhi());
Nicolas Geoffray96f0ec12018-06-08 15:30:20 +01001316 // TODO(b/109666561): Re-enable.
1317 // DCHECK(graph_->HasIrreducibleLoops());
Nicolas Geoffrayd147e2f2018-05-16 11:37:41 +01001318 // Don't bother compiling a method in that situation. While we could look at all
1319 // phis related to the HNewInstance, it's not worth the trouble.
1320 MaybeRecordStat(compilation_stats_,
1321 MethodCompilationStat::kNotCompiledIrreducibleAndStringInit);
1322 return false;
David Brazdildee58d62016-04-07 09:54:26 +00001323 }
1324
1325 // Walk over all vregs and replace any occurrence of `arg_this` with `invoke`.
1326 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
1327 if ((*current_locals_)[vreg] == arg_this) {
1328 (*current_locals_)[vreg] = invoke;
1329 }
1330 }
1331
1332 return true;
1333}
1334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001335static DataType::Type GetFieldAccessType(const DexFile& dex_file, uint16_t field_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001336 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
1337 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001338 return DataType::FromShorty(type[0]);
David Brazdildee58d62016-04-07 09:54:26 +00001339}
1340
1341bool HInstructionBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
1342 uint32_t dex_pc,
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001343 bool is_put,
1344 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001345 uint32_t source_or_dest_reg = instruction.VRegA_22c();
1346 uint32_t obj_reg = instruction.VRegB_22c();
1347 uint16_t field_index;
1348 if (instruction.IsQuickened()) {
1349 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001350 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
1351 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00001352 return false;
1353 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001354 field_index = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001355 } else {
1356 field_index = instruction.VRegC_22c();
1357 }
1358
1359 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001360 ArtField* resolved_field = ResolveField(field_index, /* is_static */ false, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001361
Aart Bik14154132016-06-02 17:53:58 -07001362 // Generate an explicit null check on the reference, unless the field access
1363 // is unresolved. In that case, we rely on the runtime to perform various
1364 // checks first, followed by a null check.
1365 HInstruction* object = (resolved_field == nullptr)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001366 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik14154132016-06-02 17:53:58 -07001367 : LoadNullCheckedLocal(obj_reg, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001369 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001370 if (is_put) {
1371 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1372 HInstruction* field_set = nullptr;
1373 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001374 MaybeRecordStat(compilation_stats_,
1375 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001376 field_set = new (allocator_) HUnresolvedInstanceFieldSet(object,
1377 value,
1378 field_type,
1379 field_index,
1380 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001381 } else {
1382 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001383 field_set = new (allocator_) HInstanceFieldSet(object,
1384 value,
1385 resolved_field,
1386 field_type,
1387 resolved_field->GetOffset(),
1388 resolved_field->IsVolatile(),
1389 field_index,
1390 class_def_index,
1391 *dex_file_,
1392 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001393 }
1394 AppendInstruction(field_set);
1395 } else {
1396 HInstruction* field_get = nullptr;
1397 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001398 MaybeRecordStat(compilation_stats_,
1399 MethodCompilationStat::kUnresolvedField);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001400 field_get = new (allocator_) HUnresolvedInstanceFieldGet(object,
1401 field_type,
1402 field_index,
1403 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001404 } else {
1405 uint16_t class_def_index = resolved_field->GetDeclaringClass()->GetDexClassDefIndex();
Vladimir Markoca6fff82017-10-03 14:49:14 +01001406 field_get = new (allocator_) HInstanceFieldGet(object,
1407 resolved_field,
1408 field_type,
1409 resolved_field->GetOffset(),
1410 resolved_field->IsVolatile(),
1411 field_index,
1412 class_def_index,
1413 *dex_file_,
1414 dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001415 }
1416 AppendInstruction(field_get);
1417 UpdateLocal(source_or_dest_reg, field_get);
1418 }
1419
1420 return true;
1421}
1422
Vladimir Marko28e012a2017-12-07 11:22:59 +00001423static ObjPtr<mirror::Class> GetClassFrom(CompilerDriver* driver,
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001424 const DexCompilationUnit& compilation_unit) {
David Brazdildee58d62016-04-07 09:54:26 +00001425 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001426 Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001427 Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
David Brazdildee58d62016-04-07 09:54:26 +00001428
1429 return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
1430}
1431
Vladimir Marko28e012a2017-12-07 11:22:59 +00001432ObjPtr<mirror::Class> HInstructionBuilder::GetOutermostCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001433 return GetClassFrom(compiler_driver_, *outer_compilation_unit_);
1434}
1435
Vladimir Marko28e012a2017-12-07 11:22:59 +00001436ObjPtr<mirror::Class> HInstructionBuilder::GetCompilingClass() const {
David Brazdildee58d62016-04-07 09:54:26 +00001437 return GetClassFrom(compiler_driver_, *dex_compilation_unit_);
1438}
1439
Andreas Gampea5b09a62016-11-17 15:21:22 -08001440bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
David Brazdildee58d62016-04-07 09:54:26 +00001441 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001442 StackHandleScope<2> hs(soa.Self());
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001443 Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001444 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
David Brazdildee58d62016-04-07 09:54:26 +00001445 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
1446 soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
1447 Handle<mirror::Class> outer_class(hs.NewHandle(GetOutermostCompilingClass()));
1448
1449 // GetOutermostCompilingClass returns null when the class is unresolved
1450 // (e.g. if it derives from an unresolved class). This is bogus knowing that
1451 // we are compiling it.
1452 // When this happens we cannot establish a direct relation between the current
1453 // class and the outer class, so we return false.
1454 // (Note that this is only used for optimizing invokes and field accesses)
Andreas Gampefa4333d2017-02-14 11:10:34 -08001455 return (cls != nullptr) && (outer_class.Get() == cls.Get());
David Brazdildee58d62016-04-07 09:54:26 +00001456}
1457
1458void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00001459 uint32_t dex_pc,
1460 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001461 DataType::Type field_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001462 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1463 uint16_t field_index = instruction.VRegB_21c();
1464
1465 if (is_put) {
1466 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1467 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001468 new (allocator_) HUnresolvedStaticFieldSet(value, field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001469 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001470 AppendInstruction(new (allocator_) HUnresolvedStaticFieldGet(field_type, field_index, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001471 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1472 }
1473}
1474
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001475ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
1476 ScopedObjectAccess soa(Thread::Current());
1477 StackHandleScope<2> hs(soa.Self());
1478
1479 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001480 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001481 Handle<mirror::Class> compiling_class(hs.NewHandle(GetCompilingClass()));
1482
Vladimir Markoe11dd502017-12-08 14:09:45 +00001483 ArtField* resolved_field = class_linker->ResolveField(field_idx,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001484 dex_compilation_unit_->GetDexCache(),
1485 class_loader,
1486 is_static);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001487 if (UNLIKELY(resolved_field == nullptr)) {
1488 // Clean up any exception left by type resolution.
1489 soa.Self()->ClearException();
1490 return nullptr;
1491 }
1492
1493 // Check static/instance. The class linker has a fast path for looking into the dex cache
1494 // and does not check static/instance if it hits it.
1495 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
1496 return nullptr;
1497 }
1498
1499 // Check access.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001500 if (compiling_class == nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001501 if (!resolved_field->IsPublic()) {
1502 return nullptr;
1503 }
1504 } else if (!compiling_class->CanAccessResolvedField(resolved_field->GetDeclaringClass(),
1505 resolved_field,
1506 dex_compilation_unit_->GetDexCache().Get(),
1507 field_idx)) {
1508 return nullptr;
1509 }
1510
1511 if (is_put &&
1512 resolved_field->IsFinal() &&
1513 (compiling_class.Get() != resolved_field->GetDeclaringClass())) {
1514 // Final fields can only be updated within their own class.
1515 // TODO: Only allow it in constructors. b/34966607.
1516 return nullptr;
1517 }
1518
1519 return resolved_field;
1520}
1521
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001522void HInstructionBuilder::BuildStaticFieldAccess(const Instruction& instruction,
David Brazdildee58d62016-04-07 09:54:26 +00001523 uint32_t dex_pc,
1524 bool is_put) {
1525 uint32_t source_or_dest_reg = instruction.VRegA_21c();
1526 uint16_t field_index = instruction.VRegB_21c();
1527
1528 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001529 ArtField* resolved_field = ResolveField(field_index, /* is_static */ true, is_put);
David Brazdildee58d62016-04-07 09:54:26 +00001530
1531 if (resolved_field == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001532 MaybeRecordStat(compilation_stats_,
1533 MethodCompilationStat::kUnresolvedField);
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 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001536 return;
David Brazdildee58d62016-04-07 09:54:26 +00001537 }
1538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
David Brazdildee58d62016-04-07 09:54:26 +00001540
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001541 Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
1542 HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
1543 klass->GetDexFile(),
1544 klass,
1545 dex_pc,
1546 /* needs_access_check */ false);
1547
1548 if (constant == nullptr) {
1549 // The class cannot be referenced from this compiled code. Generate
1550 // an unresolved access.
Igor Murashkin1e065a52017-08-09 13:20:34 -07001551 MaybeRecordStat(compilation_stats_,
1552 MethodCompilationStat::kUnresolvedFieldNotAFastAccess);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001553 BuildUnresolvedStaticFieldAccess(instruction, dex_pc, is_put, field_type);
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00001554 return;
David Brazdildee58d62016-04-07 09:54:26 +00001555 }
1556
David Brazdildee58d62016-04-07 09:54:26 +00001557 HInstruction* cls = constant;
David Brazdildee58d62016-04-07 09:54:26 +00001558 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001559 cls = new (allocator_) HClinitCheck(constant, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001560 AppendInstruction(cls);
1561 }
1562
1563 uint16_t class_def_index = klass->GetDexClassDefIndex();
1564 if (is_put) {
1565 // We need to keep the class alive before loading the value.
1566 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
1567 DCHECK_EQ(HPhi::ToPhiType(value->GetType()), HPhi::ToPhiType(field_type));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001568 AppendInstruction(new (allocator_) HStaticFieldSet(cls,
1569 value,
1570 resolved_field,
1571 field_type,
1572 resolved_field->GetOffset(),
1573 resolved_field->IsVolatile(),
1574 field_index,
1575 class_def_index,
1576 *dex_file_,
1577 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001578 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001579 AppendInstruction(new (allocator_) HStaticFieldGet(cls,
1580 resolved_field,
1581 field_type,
1582 resolved_field->GetOffset(),
1583 resolved_field->IsVolatile(),
1584 field_index,
1585 class_def_index,
1586 *dex_file_,
1587 dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001588 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1589 }
David Brazdildee58d62016-04-07 09:54:26 +00001590}
1591
1592void HInstructionBuilder::BuildCheckedDivRem(uint16_t out_vreg,
Vladimir Markoca6fff82017-10-03 14:49:14 +01001593 uint16_t first_vreg,
1594 int64_t second_vreg_or_constant,
1595 uint32_t dex_pc,
1596 DataType::Type type,
1597 bool second_is_constant,
1598 bool isDiv) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001599 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001600
1601 HInstruction* first = LoadLocal(first_vreg, type);
1602 HInstruction* second = nullptr;
1603 if (second_is_constant) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001604 if (type == DataType::Type::kInt32) {
David Brazdildee58d62016-04-07 09:54:26 +00001605 second = graph_->GetIntConstant(second_vreg_or_constant, dex_pc);
1606 } else {
1607 second = graph_->GetLongConstant(second_vreg_or_constant, dex_pc);
1608 }
1609 } else {
1610 second = LoadLocal(second_vreg_or_constant, type);
1611 }
1612
1613 if (!second_is_constant
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001614 || (type == DataType::Type::kInt32 && second->AsIntConstant()->GetValue() == 0)
1615 || (type == DataType::Type::kInt64 && second->AsLongConstant()->GetValue() == 0)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001616 second = new (allocator_) HDivZeroCheck(second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001617 AppendInstruction(second);
1618 }
1619
1620 if (isDiv) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001621 AppendInstruction(new (allocator_) HDiv(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001622 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001623 AppendInstruction(new (allocator_) HRem(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001624 }
1625 UpdateLocal(out_vreg, current_block_->GetLastInstruction());
1626}
1627
1628void HInstructionBuilder::BuildArrayAccess(const Instruction& instruction,
1629 uint32_t dex_pc,
1630 bool is_put,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 DataType::Type anticipated_type) {
David Brazdildee58d62016-04-07 09:54:26 +00001632 uint8_t source_or_dest_reg = instruction.VRegA_23x();
1633 uint8_t array_reg = instruction.VRegB_23x();
1634 uint8_t index_reg = instruction.VRegC_23x();
1635
David Brazdilc120bbe2016-04-22 16:57:00 +01001636 HInstruction* object = LoadNullCheckedLocal(array_reg, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001637 HInstruction* length = new (allocator_) HArrayLength(object, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001638 AppendInstruction(length);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001639 HInstruction* index = LoadLocal(index_reg, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001640 index = new (allocator_) HBoundsCheck(index, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001641 AppendInstruction(index);
1642 if (is_put) {
1643 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
1644 // TODO: Insert a type check node if the type is Object.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001645 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001646 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1647 AppendInstruction(aset);
1648 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001649 HArrayGet* aget = new (allocator_) HArrayGet(object, index, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001650 ssa_builder_->MaybeAddAmbiguousArrayGet(aget);
1651 AppendInstruction(aget);
1652 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
1653 }
1654 graph_->SetHasBoundsChecks(true);
1655}
1656
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001657HNewArray* HInstructionBuilder::BuildFilledNewArray(uint32_t dex_pc,
1658 dex::TypeIndex type_index,
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001659 const InstructionOperands& operands) {
1660 const size_t number_of_operands = operands.GetNumberOfOperands();
1661 HInstruction* length = graph_->GetIntConstant(number_of_operands, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001662 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001663 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001664 AppendInstruction(object);
1665
1666 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1667 DCHECK_EQ(descriptor[0], '[') << descriptor;
1668 char primitive = descriptor[1];
1669 DCHECK(primitive == 'I'
1670 || primitive == 'L'
1671 || primitive == '[') << descriptor;
1672 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001674
Treehugger Robot2c5827a2018-05-17 22:26:08 +00001675 for (size_t i = 0; i < number_of_operands; ++i) {
1676 HInstruction* value = LoadLocal(operands.GetOperand(i), type);
David Brazdildee58d62016-04-07 09:54:26 +00001677 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001678 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001679 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1680 AppendInstruction(aset);
1681 }
1682 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001683
1684 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001685}
1686
1687template <typename T>
1688void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1689 const T* data,
1690 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001692 uint32_t dex_pc) {
1693 for (uint32_t i = 0; i < element_count; ++i) {
1694 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1695 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001696 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001697 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1698 AppendInstruction(aset);
1699 }
1700}
1701
1702void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001703 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001704
1705 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1706 const Instruction::ArrayDataPayload* payload =
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001707 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1708 code_item_accessor_.Insns() + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001709 const uint8_t* data = payload->data;
1710 uint32_t element_count = payload->element_count;
1711
Vladimir Markoc69fba22016-09-06 16:49:15 +01001712 if (element_count == 0u) {
1713 // For empty payload we emit only the null check above.
1714 return;
1715 }
1716
Vladimir Markoca6fff82017-10-03 14:49:14 +01001717 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001718 AppendInstruction(length);
1719
David Brazdildee58d62016-04-07 09:54:26 +00001720 // Implementation of this DEX instruction seems to be that the bounds check is
1721 // done before doing any stores.
1722 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001723 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001724
1725 switch (payload->element_width) {
1726 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001727 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001728 reinterpret_cast<const int8_t*>(data),
1729 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001730 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001731 dex_pc);
1732 break;
1733 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001734 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001735 reinterpret_cast<const int16_t*>(data),
1736 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001737 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001738 dex_pc);
1739 break;
1740 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001741 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001742 reinterpret_cast<const int32_t*>(data),
1743 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001745 dex_pc);
1746 break;
1747 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001748 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001749 reinterpret_cast<const int64_t*>(data),
1750 element_count,
1751 dex_pc);
1752 break;
1753 default:
1754 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1755 }
1756 graph_->SetHasBoundsChecks(true);
1757}
1758
1759void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1760 const int64_t* data,
1761 uint32_t element_count,
1762 uint32_t dex_pc) {
1763 for (uint32_t i = 0; i < element_count; ++i) {
1764 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1765 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001766 HArraySet* aset =
1767 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001768 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1769 AppendInstruction(aset);
1770 }
1771}
1772
Vladimir Marko28e012a2017-12-07 11:22:59 +00001773void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1774 HLoadString* load_string =
1775 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1776 HSharpening::ProcessLoadString(load_string,
1777 code_generator_,
1778 compiler_driver_,
1779 *dex_compilation_unit_,
1780 handles_);
1781 AppendInstruction(load_string);
1782}
1783
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001784HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001785 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001786 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko175e7862018-03-27 09:03:13 +00001787 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
1788 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001789 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1790}
1791
1792HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1793 const DexFile& dex_file,
1794 Handle<mirror::Class> klass,
1795 uint32_t dex_pc,
1796 bool needs_access_check) {
1797 // Try to find a reference in the compiling dex file.
1798 const DexFile* actual_dex_file = &dex_file;
1799 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1800 dex::TypeIndex local_type_index =
1801 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1802 if (local_type_index.IsValid()) {
1803 type_index = local_type_index;
1804 actual_dex_file = dex_compilation_unit_->GetDexFile();
1805 }
1806 }
1807
1808 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001809 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001810 graph_->GetCurrentMethod(),
1811 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001812 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001813 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001814 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001815 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001816 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001817
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001818 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1819 code_generator_,
1820 compiler_driver_,
1821 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001822
1823 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1824 // We actually cannot reference this class, we're forced to bail.
1825 return nullptr;
1826 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00001827 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001828 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001829 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001830 return load_class;
1831}
1832
Vladimir Marko175e7862018-03-27 09:03:13 +00001833Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
1834 dex::TypeIndex type_index) {
1835 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
1836 ObjPtr<mirror::Class> klass = compiler_driver_->ResolveClass(
1837 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_);
1838 // TODO: Avoid creating excessive handles if the method references the same class repeatedly.
1839 // (Use a map on the local_allocator_.)
1840 return handles_->NewHandle(klass);
1841}
1842
1843bool HInstructionBuilder::LoadClassNeedsAccessCheck(Handle<mirror::Class> klass) {
1844 if (klass == nullptr) {
1845 return true;
1846 } else if (klass->IsPublic()) {
1847 return false;
1848 } else {
1849 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
1850 return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
1851 }
1852}
1853
Orion Hodson06d10a72018-05-14 08:53:38 +01001854void HInstructionBuilder::BuildLoadMethodHandle(uint16_t method_handle_index, uint32_t dex_pc) {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001855 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Orion Hodson06d10a72018-05-14 08:53:38 +01001856 HLoadMethodHandle* load_method_handle = new (allocator_) HLoadMethodHandle(
1857 graph_->GetCurrentMethod(), method_handle_index, dex_file, dex_pc);
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001858 AppendInstruction(load_method_handle);
1859}
1860
Orion Hodson06d10a72018-05-14 08:53:38 +01001861void HInstructionBuilder::BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc) {
Orion Hodson18259d72018-04-12 11:18:23 +01001862 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1863 HLoadMethodType* load_method_type =
Orion Hodson06d10a72018-05-14 08:53:38 +01001864 new (allocator_) HLoadMethodType(graph_->GetCurrentMethod(), proto_index, dex_file, dex_pc);
Orion Hodson18259d72018-04-12 11:18:23 +01001865 AppendInstruction(load_method_type);
1866}
1867
David Brazdildee58d62016-04-07 09:54:26 +00001868void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1869 uint8_t destination,
1870 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001871 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001872 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001874
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001875 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko175e7862018-03-27 09:03:13 +00001876 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
1877 Handle<mirror::Class> klass = ResolveClass(soa, type_index);
1878 bool needs_access_check = LoadClassNeedsAccessCheck(klass);
1879 TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
1880 klass.Get(), code_generator_, compiler_driver_, needs_access_check);
1881
1882 HInstruction* class_or_null = nullptr;
1883 HIntConstant* bitstring_path_to_root = nullptr;
1884 HIntConstant* bitstring_mask = nullptr;
1885 if (check_kind == TypeCheckKind::kBitstringCheck) {
1886 // TODO: Allow using the bitstring check also if we need an access check.
1887 DCHECK(!needs_access_check);
1888 class_or_null = graph_->GetNullConstant(dex_pc);
1889 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
1890 uint32_t path_to_root =
1891 SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootForTarget(klass.Get());
1892 uint32_t mask = SubtypeCheck<ObjPtr<mirror::Class>>::GetEncodedPathToRootMask(klass.Get());
1893 bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
1894 bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
1895 } else {
1896 class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1897 }
1898 DCHECK(class_or_null != nullptr);
1899
David Brazdildee58d62016-04-07 09:54:26 +00001900 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Marko175e7862018-03-27 09:03:13 +00001901 AppendInstruction(new (allocator_) HInstanceOf(object,
1902 class_or_null,
1903 check_kind,
1904 klass,
1905 dex_pc,
1906 allocator_,
1907 bitstring_path_to_root,
1908 bitstring_mask));
David Brazdildee58d62016-04-07 09:54:26 +00001909 UpdateLocal(destination, current_block_->GetLastInstruction());
1910 } else {
1911 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1912 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1913 // which may throw. If it succeeds BoundType sets the new type of `object`
1914 // for all subsequent uses.
Vladimir Marko175e7862018-03-27 09:03:13 +00001915 AppendInstruction(
1916 new (allocator_) HCheckCast(object,
1917 class_or_null,
1918 check_kind,
1919 klass,
1920 dex_pc,
1921 allocator_,
1922 bitstring_path_to_root,
1923 bitstring_mask));
Vladimir Markoca6fff82017-10-03 14:49:14 +01001924 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001925 UpdateLocal(reference, current_block_->GetLastInstruction());
1926 }
1927}
1928
Vladimir Marko0b66d612017-03-13 14:50:04 +00001929bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001930 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1931 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001932}
1933
1934bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001935 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001936}
1937
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001938uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1939 DCHECK(CanDecodeQuickenedInfo());
1940 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001941}
1942
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001943bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1944 uint32_t dex_pc,
1945 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001946 switch (instruction.Opcode()) {
1947 case Instruction::CONST_4: {
1948 int32_t register_index = instruction.VRegA();
1949 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1950 UpdateLocal(register_index, constant);
1951 break;
1952 }
1953
1954 case Instruction::CONST_16: {
1955 int32_t register_index = instruction.VRegA();
1956 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1957 UpdateLocal(register_index, constant);
1958 break;
1959 }
1960
1961 case Instruction::CONST: {
1962 int32_t register_index = instruction.VRegA();
1963 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1964 UpdateLocal(register_index, constant);
1965 break;
1966 }
1967
1968 case Instruction::CONST_HIGH16: {
1969 int32_t register_index = instruction.VRegA();
1970 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1971 UpdateLocal(register_index, constant);
1972 break;
1973 }
1974
1975 case Instruction::CONST_WIDE_16: {
1976 int32_t register_index = instruction.VRegA();
1977 // Get 16 bits of constant value, sign extended to 64 bits.
1978 int64_t value = instruction.VRegB_21s();
1979 value <<= 48;
1980 value >>= 48;
1981 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1982 UpdateLocal(register_index, constant);
1983 break;
1984 }
1985
1986 case Instruction::CONST_WIDE_32: {
1987 int32_t register_index = instruction.VRegA();
1988 // Get 32 bits of constant value, sign extended to 64 bits.
1989 int64_t value = instruction.VRegB_31i();
1990 value <<= 32;
1991 value >>= 32;
1992 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1993 UpdateLocal(register_index, constant);
1994 break;
1995 }
1996
1997 case Instruction::CONST_WIDE: {
1998 int32_t register_index = instruction.VRegA();
1999 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
2000 UpdateLocal(register_index, constant);
2001 break;
2002 }
2003
2004 case Instruction::CONST_WIDE_HIGH16: {
2005 int32_t register_index = instruction.VRegA();
2006 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
2007 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
2008 UpdateLocal(register_index, constant);
2009 break;
2010 }
2011
2012 // Note that the SSA building will refine the types.
2013 case Instruction::MOVE:
2014 case Instruction::MOVE_FROM16:
2015 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00002017 UpdateLocal(instruction.VRegA(), value);
2018 break;
2019 }
2020
2021 // Note that the SSA building will refine the types.
2022 case Instruction::MOVE_WIDE:
2023 case Instruction::MOVE_WIDE_FROM16:
2024 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00002026 UpdateLocal(instruction.VRegA(), value);
2027 break;
2028 }
2029
2030 case Instruction::MOVE_OBJECT:
2031 case Instruction::MOVE_OBJECT_16:
2032 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002033 // The verifier has no notion of a null type, so a move-object of constant 0
2034 // will lead to the same constant 0 in the destination register. To mimic
2035 // this behavior, we just pretend we haven't seen a type change (int to reference)
2036 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2037 // types correct.
2038 uint32_t reg_number = instruction.VRegB();
2039 HInstruction* value = (*current_locals_)[reg_number];
2040 if (value->IsIntConstant()) {
2041 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2042 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2044 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002045 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002047 }
David Brazdildee58d62016-04-07 09:54:26 +00002048 UpdateLocal(instruction.VRegA(), value);
2049 break;
2050 }
2051
2052 case Instruction::RETURN_VOID_NO_BARRIER:
2053 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002054 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002055 break;
2056 }
2057
2058#define IF_XX(comparison, cond) \
2059 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2060 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2061
2062 IF_XX(HEqual, EQ);
2063 IF_XX(HNotEqual, NE);
2064 IF_XX(HLessThan, LT);
2065 IF_XX(HLessThanOrEqual, LE);
2066 IF_XX(HGreaterThan, GT);
2067 IF_XX(HGreaterThanOrEqual, GE);
2068
2069 case Instruction::GOTO:
2070 case Instruction::GOTO_16:
2071 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002072 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002073 current_block_ = nullptr;
2074 break;
2075 }
2076
2077 case Instruction::RETURN: {
2078 BuildReturn(instruction, return_type_, dex_pc);
2079 break;
2080 }
2081
2082 case Instruction::RETURN_OBJECT: {
2083 BuildReturn(instruction, return_type_, dex_pc);
2084 break;
2085 }
2086
2087 case Instruction::RETURN_WIDE: {
2088 BuildReturn(instruction, return_type_, dex_pc);
2089 break;
2090 }
2091
2092 case Instruction::INVOKE_DIRECT:
2093 case Instruction::INVOKE_INTERFACE:
2094 case Instruction::INVOKE_STATIC:
2095 case Instruction::INVOKE_SUPER:
2096 case Instruction::INVOKE_VIRTUAL:
2097 case Instruction::INVOKE_VIRTUAL_QUICK: {
2098 uint16_t method_idx;
2099 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2100 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002101 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2102 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002103 return false;
2104 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002105 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002106 } else {
2107 method_idx = instruction.VRegB_35c();
2108 }
David Brazdildee58d62016-04-07 09:54:26 +00002109 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002110 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2111 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2112 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002113 return false;
2114 }
2115 break;
2116 }
2117
2118 case Instruction::INVOKE_DIRECT_RANGE:
2119 case Instruction::INVOKE_INTERFACE_RANGE:
2120 case Instruction::INVOKE_STATIC_RANGE:
2121 case Instruction::INVOKE_SUPER_RANGE:
2122 case Instruction::INVOKE_VIRTUAL_RANGE:
2123 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2124 uint16_t method_idx;
2125 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2126 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002127 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2128 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002129 return false;
2130 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002131 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002132 } else {
2133 method_idx = instruction.VRegB_3rc();
2134 }
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002135 RangeInstructionOperands operands(instruction.VRegC(), instruction.VRegA_3rc());
2136 if (!BuildInvoke(instruction, dex_pc, method_idx, operands)) {
David Brazdildee58d62016-04-07 09:54:26 +00002137 return false;
2138 }
2139 break;
2140 }
2141
Orion Hodsonac141392017-01-13 11:53:47 +00002142 case Instruction::INVOKE_POLYMORPHIC: {
2143 uint16_t method_idx = instruction.VRegB_45cc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002144 dex::ProtoIndex proto_idx(instruction.VRegH_45cc());
Orion Hodsonac141392017-01-13 11:53:47 +00002145 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002146 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2147 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2148 return BuildInvokePolymorphic(instruction, dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002149 }
2150
2151 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2152 uint16_t method_idx = instruction.VRegB_4rcc();
Orion Hodson06d10a72018-05-14 08:53:38 +01002153 dex::ProtoIndex proto_idx(instruction.VRegH_4rcc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002154 RangeInstructionOperands operands(instruction.VRegC_4rcc(), instruction.VRegA_4rcc());
2155 return BuildInvokePolymorphic(instruction, dex_pc, method_idx, proto_idx, operands);
Orion Hodsonac141392017-01-13 11:53:47 +00002156 }
2157
David Brazdildee58d62016-04-07 09:54:26 +00002158 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002160 break;
2161 }
2162
2163 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002165 break;
2166 }
2167
2168 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002170 break;
2171 }
2172
2173 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002175 break;
2176 }
2177
2178 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002180 break;
2181 }
2182
2183 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002185 break;
2186 }
2187
2188 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002190 break;
2191 }
2192
2193 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002194 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002195 break;
2196 }
2197
2198 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002199 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002200 break;
2201 }
2202
2203 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002205 break;
2206 }
2207
2208 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002210 break;
2211 }
2212
2213 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002215 break;
2216 }
2217
2218 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002220 break;
2221 }
2222
2223 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002225 break;
2226 }
2227
2228 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002229 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002230 break;
2231 }
2232
2233 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002235 break;
2236 }
2237
2238 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002240 break;
2241 }
2242
2243 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002245 break;
2246 }
2247
2248 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002250 break;
2251 }
2252
2253 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002254 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002255 break;
2256 }
2257
2258 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002260 break;
2261 }
2262
2263 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002265 break;
2266 }
2267
2268 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002270 break;
2271 }
2272
2273 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002275 break;
2276 }
2277
2278 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002280 break;
2281 }
2282
2283 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002284 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002285 break;
2286 }
2287
2288 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002290 break;
2291 }
2292
2293 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002294 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002295 break;
2296 }
2297
2298 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002300 break;
2301 }
2302
2303 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002304 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002305 break;
2306 }
2307
2308 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002310 break;
2311 }
2312
2313 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002315 break;
2316 }
2317
2318 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002320 break;
2321 }
2322
2323 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002325 break;
2326 }
2327
2328 case Instruction::DIV_INT: {
2329 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002330 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002331 break;
2332 }
2333
2334 case Instruction::DIV_LONG: {
2335 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002337 break;
2338 }
2339
2340 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002342 break;
2343 }
2344
2345 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002347 break;
2348 }
2349
2350 case Instruction::REM_INT: {
2351 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002353 break;
2354 }
2355
2356 case Instruction::REM_LONG: {
2357 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002358 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002359 break;
2360 }
2361
2362 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002364 break;
2365 }
2366
2367 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002369 break;
2370 }
2371
2372 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002374 break;
2375 }
2376
2377 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002378 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002379 break;
2380 }
2381
2382 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002383 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002384 break;
2385 }
2386
2387 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002389 break;
2390 }
2391
2392 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002394 break;
2395 }
2396
2397 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002399 break;
2400 }
2401
2402 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002404 break;
2405 }
2406
2407 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002409 break;
2410 }
2411
2412 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002414 break;
2415 }
2416
2417 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002419 break;
2420 }
2421
2422 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002424 break;
2425 }
2426
2427 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002428 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002429 break;
2430 }
2431
2432 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002433 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002434 break;
2435 }
2436
2437 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002439 break;
2440 }
2441
2442 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002444 break;
2445 }
2446
2447 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002448 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002449 break;
2450 }
2451
2452 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002453 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002454 break;
2455 }
2456
2457 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002458 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002459 break;
2460 }
2461
2462 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002463 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002464 break;
2465 }
2466
2467 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002469 break;
2470 }
2471
2472 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002474 break;
2475 }
2476
2477 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002478 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002479 break;
2480 }
2481
2482 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002484 break;
2485 }
2486
2487 case Instruction::DIV_INT_2ADDR: {
2488 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002489 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002490 break;
2491 }
2492
2493 case Instruction::DIV_LONG_2ADDR: {
2494 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002496 break;
2497 }
2498
2499 case Instruction::REM_INT_2ADDR: {
2500 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002502 break;
2503 }
2504
2505 case Instruction::REM_LONG_2ADDR: {
2506 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002508 break;
2509 }
2510
2511 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002512 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002513 break;
2514 }
2515
2516 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002517 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002518 break;
2519 }
2520
2521 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002523 break;
2524 }
2525
2526 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002528 break;
2529 }
2530
2531 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002532 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002533 break;
2534 }
2535
2536 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002538 break;
2539 }
2540
2541 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002543 break;
2544 }
2545
2546 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002548 break;
2549 }
2550
2551 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002553 break;
2554 }
2555
2556 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002558 break;
2559 }
2560
2561 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002563 break;
2564 }
2565
2566 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002568 break;
2569 }
2570
2571 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002573 break;
2574 }
2575
2576 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002578 break;
2579 }
2580
2581 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002583 break;
2584 }
2585
2586 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002588 break;
2589 }
2590
2591 case Instruction::ADD_INT_LIT16: {
2592 Binop_22s<HAdd>(instruction, false, dex_pc);
2593 break;
2594 }
2595
2596 case Instruction::AND_INT_LIT16: {
2597 Binop_22s<HAnd>(instruction, false, dex_pc);
2598 break;
2599 }
2600
2601 case Instruction::OR_INT_LIT16: {
2602 Binop_22s<HOr>(instruction, false, dex_pc);
2603 break;
2604 }
2605
2606 case Instruction::XOR_INT_LIT16: {
2607 Binop_22s<HXor>(instruction, false, dex_pc);
2608 break;
2609 }
2610
2611 case Instruction::RSUB_INT: {
2612 Binop_22s<HSub>(instruction, true, dex_pc);
2613 break;
2614 }
2615
2616 case Instruction::MUL_INT_LIT16: {
2617 Binop_22s<HMul>(instruction, false, dex_pc);
2618 break;
2619 }
2620
2621 case Instruction::ADD_INT_LIT8: {
2622 Binop_22b<HAdd>(instruction, false, dex_pc);
2623 break;
2624 }
2625
2626 case Instruction::AND_INT_LIT8: {
2627 Binop_22b<HAnd>(instruction, false, dex_pc);
2628 break;
2629 }
2630
2631 case Instruction::OR_INT_LIT8: {
2632 Binop_22b<HOr>(instruction, false, dex_pc);
2633 break;
2634 }
2635
2636 case Instruction::XOR_INT_LIT8: {
2637 Binop_22b<HXor>(instruction, false, dex_pc);
2638 break;
2639 }
2640
2641 case Instruction::RSUB_INT_LIT8: {
2642 Binop_22b<HSub>(instruction, true, dex_pc);
2643 break;
2644 }
2645
2646 case Instruction::MUL_INT_LIT8: {
2647 Binop_22b<HMul>(instruction, false, dex_pc);
2648 break;
2649 }
2650
2651 case Instruction::DIV_INT_LIT16:
2652 case Instruction::DIV_INT_LIT8: {
2653 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002655 break;
2656 }
2657
2658 case Instruction::REM_INT_LIT16:
2659 case Instruction::REM_INT_LIT8: {
2660 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002662 break;
2663 }
2664
2665 case Instruction::SHL_INT_LIT8: {
2666 Binop_22b<HShl>(instruction, false, dex_pc);
2667 break;
2668 }
2669
2670 case Instruction::SHR_INT_LIT8: {
2671 Binop_22b<HShr>(instruction, false, dex_pc);
2672 break;
2673 }
2674
2675 case Instruction::USHR_INT_LIT8: {
2676 Binop_22b<HUShr>(instruction, false, dex_pc);
2677 break;
2678 }
2679
2680 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002681 HNewInstance* new_instance =
2682 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2683 DCHECK(new_instance != nullptr);
2684
David Brazdildee58d62016-04-07 09:54:26 +00002685 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002686 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002687 break;
2688 }
2689
2690 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002691 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002693 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002694
Vladimir Markoca6fff82017-10-03 14:49:14 +01002695 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002696 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002697 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002698 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002699 break;
2700 }
2701
2702 case Instruction::FILLED_NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002703 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002704 uint32_t args[5];
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002705 uint32_t number_of_vreg_arguments = instruction.GetVarArgs(args);
2706 VarArgsInstructionOperands operands(args, number_of_vreg_arguments);
2707 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002708 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002709 break;
2710 }
2711
2712 case Instruction::FILLED_NEW_ARRAY_RANGE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002713 dex::TypeIndex type_index(instruction.VRegB_3rc());
Treehugger Robot2c5827a2018-05-17 22:26:08 +00002714 RangeInstructionOperands operands(instruction.VRegC_3rc(), instruction.VRegA_3rc());
2715 HNewArray* new_array = BuildFilledNewArray(dex_pc, type_index, operands);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002716 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002717 break;
2718 }
2719
2720 case Instruction::FILL_ARRAY_DATA: {
2721 BuildFillArrayData(instruction, dex_pc);
2722 break;
2723 }
2724
2725 case Instruction::MOVE_RESULT:
2726 case Instruction::MOVE_RESULT_WIDE:
2727 case Instruction::MOVE_RESULT_OBJECT: {
2728 DCHECK(latest_result_ != nullptr);
2729 UpdateLocal(instruction.VRegA(), latest_result_);
2730 latest_result_ = nullptr;
2731 break;
2732 }
2733
2734 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002736 break;
2737 }
2738
2739 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002741 break;
2742 }
2743
2744 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002746 break;
2747 }
2748
2749 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002750 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002751 break;
2752 }
2753
2754 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002756 break;
2757 }
2758
2759 case Instruction::NOP:
2760 break;
2761
2762 case Instruction::IGET:
2763 case Instruction::IGET_QUICK:
2764 case Instruction::IGET_WIDE:
2765 case Instruction::IGET_WIDE_QUICK:
2766 case Instruction::IGET_OBJECT:
2767 case Instruction::IGET_OBJECT_QUICK:
2768 case Instruction::IGET_BOOLEAN:
2769 case Instruction::IGET_BOOLEAN_QUICK:
2770 case Instruction::IGET_BYTE:
2771 case Instruction::IGET_BYTE_QUICK:
2772 case Instruction::IGET_CHAR:
2773 case Instruction::IGET_CHAR_QUICK:
2774 case Instruction::IGET_SHORT:
2775 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002776 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002777 return false;
2778 }
2779 break;
2780 }
2781
2782 case Instruction::IPUT:
2783 case Instruction::IPUT_QUICK:
2784 case Instruction::IPUT_WIDE:
2785 case Instruction::IPUT_WIDE_QUICK:
2786 case Instruction::IPUT_OBJECT:
2787 case Instruction::IPUT_OBJECT_QUICK:
2788 case Instruction::IPUT_BOOLEAN:
2789 case Instruction::IPUT_BOOLEAN_QUICK:
2790 case Instruction::IPUT_BYTE:
2791 case Instruction::IPUT_BYTE_QUICK:
2792 case Instruction::IPUT_CHAR:
2793 case Instruction::IPUT_CHAR_QUICK:
2794 case Instruction::IPUT_SHORT:
2795 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002796 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002797 return false;
2798 }
2799 break;
2800 }
2801
2802 case Instruction::SGET:
2803 case Instruction::SGET_WIDE:
2804 case Instruction::SGET_OBJECT:
2805 case Instruction::SGET_BOOLEAN:
2806 case Instruction::SGET_BYTE:
2807 case Instruction::SGET_CHAR:
2808 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002809 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00002810 break;
2811 }
2812
2813 case Instruction::SPUT:
2814 case Instruction::SPUT_WIDE:
2815 case Instruction::SPUT_OBJECT:
2816 case Instruction::SPUT_BOOLEAN:
2817 case Instruction::SPUT_BYTE:
2818 case Instruction::SPUT_CHAR:
2819 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002820 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002821 break;
2822 }
2823
2824#define ARRAY_XX(kind, anticipated_type) \
2825 case Instruction::AGET##kind: { \
2826 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2827 break; \
2828 } \
2829 case Instruction::APUT##kind: { \
2830 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2831 break; \
2832 }
2833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 ARRAY_XX(, DataType::Type::kInt32);
2835 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2836 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2837 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2838 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2839 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2840 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002841
2842 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002843 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002844 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002845 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2846 break;
2847 }
2848
2849 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002850 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002851 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002852 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2853 break;
2854 }
2855
2856 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002857 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002858 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002859 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2860 break;
2861 }
2862
2863 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002864 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002865 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002866 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2867 break;
2868 }
2869
Orion Hodsondbaa5c72018-05-10 08:22:46 +01002870 case Instruction::CONST_METHOD_HANDLE: {
2871 uint16_t method_handle_idx = instruction.VRegB_21c();
2872 BuildLoadMethodHandle(method_handle_idx, dex_pc);
2873 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2874 break;
2875 }
2876
Orion Hodson18259d72018-04-12 11:18:23 +01002877 case Instruction::CONST_METHOD_TYPE: {
Orion Hodson06d10a72018-05-14 08:53:38 +01002878 dex::ProtoIndex proto_idx(instruction.VRegB_21c());
Orion Hodson18259d72018-04-12 11:18:23 +01002879 BuildLoadMethodType(proto_idx, dex_pc);
2880 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2881 break;
2882 }
2883
David Brazdildee58d62016-04-07 09:54:26 +00002884 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002885 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002886 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002887 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002888 break;
2889 }
2890
2891 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002893 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002894 // We finished building this block. Set the current block to null to avoid
2895 // adding dead instructions to it.
2896 current_block_ = nullptr;
2897 break;
2898 }
2899
2900 case Instruction::INSTANCE_OF: {
2901 uint8_t destination = instruction.VRegA_22c();
2902 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002903 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002904 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2905 break;
2906 }
2907
2908 case Instruction::CHECK_CAST: {
2909 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002910 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002911 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2912 break;
2913 }
2914
2915 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002916 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002918 HMonitorOperation::OperationKind::kEnter,
2919 dex_pc));
2920 break;
2921 }
2922
2923 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002924 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002926 HMonitorOperation::OperationKind::kExit,
2927 dex_pc));
2928 break;
2929 }
2930
2931 case Instruction::SPARSE_SWITCH:
2932 case Instruction::PACKED_SWITCH: {
2933 BuildSwitch(instruction, dex_pc);
2934 break;
2935 }
2936
2937 default:
2938 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002939 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002940 << " because of unhandled instruction "
2941 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002942 MaybeRecordStat(compilation_stats_,
2943 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002944 return false;
2945 }
2946 return true;
2947} // NOLINT(readability/fn_size)
2948
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002949ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2950 dex::TypeIndex type_index,
2951 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002952 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002953 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2954}
2955
2956ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2957 // TODO: Cache the result in a Handle<mirror::Class>.
2958 const DexFile::MethodId& method_id =
2959 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2960 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2961}
2962
David Brazdildee58d62016-04-07 09:54:26 +00002963} // namespace art