blob: e36d91fb053f919a7e2560ce38b96cc40aeeb3bd [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 "bytecode_utils.h"
24#include "class_linker.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010025#include "data_type-inl.h"
Andreas Gampe26de38b2016-07-27 17:53:11 -070026#include "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
David Brazdildee58d62016-04-07 09:54:26 +000042HBasicBlock* HInstructionBuilder::FindBlockStartingAt(uint32_t dex_pc) const {
43 return block_builder_->GetBlockAt(dex_pc);
44}
45
Vladimir Marko69d310e2017-10-09 14:12:23 +010046inline ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsFor(HBasicBlock* block) {
47 ScopedArenaVector<HInstruction*>* locals = &locals_for_[block->GetBlockId()];
David Brazdildee58d62016-04-07 09:54:26 +000048 const size_t vregs = graph_->GetNumberOfVRegs();
Mingyao Yang01b47b02017-02-03 12:09:57 -080049 if (locals->size() == vregs) {
50 return locals;
51 }
52 return GetLocalsForWithAllocation(block, locals, vregs);
53}
David Brazdildee58d62016-04-07 09:54:26 +000054
Vladimir Marko69d310e2017-10-09 14:12:23 +010055ScopedArenaVector<HInstruction*>* HInstructionBuilder::GetLocalsForWithAllocation(
Mingyao Yang01b47b02017-02-03 12:09:57 -080056 HBasicBlock* block,
Vladimir Marko69d310e2017-10-09 14:12:23 +010057 ScopedArenaVector<HInstruction*>* locals,
Mingyao Yang01b47b02017-02-03 12:09:57 -080058 const size_t vregs) {
59 DCHECK_NE(locals->size(), vregs);
60 locals->resize(vregs, nullptr);
61 if (block->IsCatchBlock()) {
62 // We record incoming inputs of catch phis at throwing instructions and
63 // must therefore eagerly create the phis. Phis for undefined vregs will
64 // be deleted when the first throwing instruction with the vreg undefined
65 // is encountered. Unused phis will be removed by dead phi analysis.
66 for (size_t i = 0; i < vregs; ++i) {
67 // No point in creating the catch phi if it is already undefined at
68 // the first throwing instruction.
69 HInstruction* current_local_value = (*current_locals_)[i];
70 if (current_local_value != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010071 HPhi* phi = new (allocator_) HPhi(
72 allocator_,
Mingyao Yang01b47b02017-02-03 12:09:57 -080073 i,
74 0,
75 current_local_value->GetType());
76 block->AddPhi(phi);
77 (*locals)[i] = phi;
David Brazdildee58d62016-04-07 09:54:26 +000078 }
79 }
80 }
81 return locals;
82}
83
Mingyao Yang01b47b02017-02-03 12:09:57 -080084inline HInstruction* HInstructionBuilder::ValueOfLocalAt(HBasicBlock* block, size_t local) {
Vladimir Marko69d310e2017-10-09 14:12:23 +010085 ScopedArenaVector<HInstruction*>* locals = GetLocalsFor(block);
David Brazdildee58d62016-04-07 09:54:26 +000086 return (*locals)[local];
87}
88
89void HInstructionBuilder::InitializeBlockLocals() {
90 current_locals_ = GetLocalsFor(current_block_);
91
92 if (current_block_->IsCatchBlock()) {
93 // Catch phis were already created and inputs collected from throwing sites.
94 if (kIsDebugBuild) {
95 // Make sure there was at least one throwing instruction which initialized
96 // locals (guaranteed by HGraphBuilder) and that all try blocks have been
97 // visited already (from HTryBoundary scoping and reverse post order).
98 bool catch_block_visited = false;
Vladimir Marko2c45bc92016-10-25 16:54:12 +010099 for (HBasicBlock* current : graph_->GetReversePostOrder()) {
David Brazdildee58d62016-04-07 09:54:26 +0000100 if (current == current_block_) {
101 catch_block_visited = true;
102 } else if (current->IsTryBlock()) {
103 const HTryBoundary& try_entry = current->GetTryCatchInformation()->GetTryEntry();
104 if (try_entry.HasExceptionHandler(*current_block_)) {
105 DCHECK(!catch_block_visited) << "Catch block visited before its try block.";
106 }
107 }
108 }
109 DCHECK_EQ(current_locals_->size(), graph_->GetNumberOfVRegs())
110 << "No instructions throwing into a live catch block.";
111 }
112 } else if (current_block_->IsLoopHeader()) {
113 // If the block is a loop header, we know we only have visited the pre header
114 // because we are visiting in reverse post order. We create phis for all initialized
115 // locals from the pre header. Their inputs will be populated at the end of
116 // the analysis.
117 for (size_t local = 0; local < current_locals_->size(); ++local) {
118 HInstruction* incoming =
119 ValueOfLocalAt(current_block_->GetLoopInformation()->GetPreHeader(), local);
120 if (incoming != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100121 HPhi* phi = new (allocator_) HPhi(
122 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000123 local,
124 0,
125 incoming->GetType());
126 current_block_->AddPhi(phi);
127 (*current_locals_)[local] = phi;
128 }
129 }
130
131 // Save the loop header so that the last phase of the analysis knows which
132 // blocks need to be updated.
133 loop_headers_.push_back(current_block_);
134 } else if (current_block_->GetPredecessors().size() > 0) {
135 // All predecessors have already been visited because we are visiting in reverse post order.
136 // We merge the values of all locals, creating phis if those values differ.
137 for (size_t local = 0; local < current_locals_->size(); ++local) {
138 bool one_predecessor_has_no_value = false;
139 bool is_different = false;
140 HInstruction* value = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
141
142 for (HBasicBlock* predecessor : current_block_->GetPredecessors()) {
143 HInstruction* current = ValueOfLocalAt(predecessor, local);
144 if (current == nullptr) {
145 one_predecessor_has_no_value = true;
146 break;
147 } else if (current != value) {
148 is_different = true;
149 }
150 }
151
152 if (one_predecessor_has_no_value) {
153 // If one predecessor has no value for this local, we trust the verifier has
154 // successfully checked that there is a store dominating any read after this block.
155 continue;
156 }
157
158 if (is_different) {
159 HInstruction* first_input = ValueOfLocalAt(current_block_->GetPredecessors()[0], local);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100160 HPhi* phi = new (allocator_) HPhi(
161 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000162 local,
163 current_block_->GetPredecessors().size(),
164 first_input->GetType());
165 for (size_t i = 0; i < current_block_->GetPredecessors().size(); i++) {
166 HInstruction* pred_value = ValueOfLocalAt(current_block_->GetPredecessors()[i], local);
167 phi->SetRawInputAt(i, pred_value);
168 }
169 current_block_->AddPhi(phi);
170 value = phi;
171 }
172 (*current_locals_)[local] = value;
173 }
174 }
175}
176
177void HInstructionBuilder::PropagateLocalsToCatchBlocks() {
178 const HTryBoundary& try_entry = current_block_->GetTryCatchInformation()->GetTryEntry();
179 for (HBasicBlock* catch_block : try_entry.GetExceptionHandlers()) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100180 ScopedArenaVector<HInstruction*>* handler_locals = GetLocalsFor(catch_block);
David Brazdildee58d62016-04-07 09:54:26 +0000181 DCHECK_EQ(handler_locals->size(), current_locals_->size());
182 for (size_t vreg = 0, e = current_locals_->size(); vreg < e; ++vreg) {
183 HInstruction* handler_value = (*handler_locals)[vreg];
184 if (handler_value == nullptr) {
185 // Vreg was undefined at a previously encountered throwing instruction
186 // and the catch phi was deleted. Do not record the local value.
187 continue;
188 }
189 DCHECK(handler_value->IsPhi());
190
191 HInstruction* local_value = (*current_locals_)[vreg];
192 if (local_value == nullptr) {
193 // This is the first instruction throwing into `catch_block` where
194 // `vreg` is undefined. Delete the catch phi.
195 catch_block->RemovePhi(handler_value->AsPhi());
196 (*handler_locals)[vreg] = nullptr;
197 } else {
198 // Vreg has been defined at all instructions throwing into `catch_block`
199 // encountered so far. Record the local value in the catch phi.
200 handler_value->AsPhi()->AddInput(local_value);
201 }
202 }
203 }
204}
205
206void HInstructionBuilder::AppendInstruction(HInstruction* instruction) {
207 current_block_->AddInstruction(instruction);
208 InitializeInstruction(instruction);
209}
210
211void HInstructionBuilder::InsertInstructionAtTop(HInstruction* instruction) {
212 if (current_block_->GetInstructions().IsEmpty()) {
213 current_block_->AddInstruction(instruction);
214 } else {
215 current_block_->InsertInstructionBefore(instruction, current_block_->GetFirstInstruction());
216 }
217 InitializeInstruction(instruction);
218}
219
220void HInstructionBuilder::InitializeInstruction(HInstruction* instruction) {
221 if (instruction->NeedsEnvironment()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100222 HEnvironment* environment = new (allocator_) HEnvironment(
223 allocator_,
David Brazdildee58d62016-04-07 09:54:26 +0000224 current_locals_->size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000225 graph_->GetArtMethod(),
David Brazdildee58d62016-04-07 09:54:26 +0000226 instruction->GetDexPc(),
David Brazdildee58d62016-04-07 09:54:26 +0000227 instruction);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100228 environment->CopyFrom(ArrayRef<HInstruction* const>(*current_locals_));
David Brazdildee58d62016-04-07 09:54:26 +0000229 instruction->SetRawEnvironment(environment);
230 }
231}
232
David Brazdilc120bbe2016-04-22 16:57:00 +0100233HInstruction* HInstructionBuilder::LoadNullCheckedLocal(uint32_t register_index, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100234 HInstruction* ref = LoadLocal(register_index, DataType::Type::kReference);
David Brazdilc120bbe2016-04-22 16:57:00 +0100235 if (!ref->CanBeNull()) {
236 return ref;
237 }
238
Vladimir Markoca6fff82017-10-03 14:49:14 +0100239 HNullCheck* null_check = new (allocator_) HNullCheck(ref, dex_pc);
David Brazdilc120bbe2016-04-22 16:57:00 +0100240 AppendInstruction(null_check);
241 return null_check;
242}
243
David Brazdildee58d62016-04-07 09:54:26 +0000244void HInstructionBuilder::SetLoopHeaderPhiInputs() {
245 for (size_t i = loop_headers_.size(); i > 0; --i) {
246 HBasicBlock* block = loop_headers_[i - 1];
247 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
248 HPhi* phi = it.Current()->AsPhi();
249 size_t vreg = phi->GetRegNumber();
250 for (HBasicBlock* predecessor : block->GetPredecessors()) {
251 HInstruction* value = ValueOfLocalAt(predecessor, vreg);
252 if (value == nullptr) {
253 // Vreg is undefined at this predecessor. Mark it dead and leave with
254 // fewer inputs than predecessors. SsaChecker will fail if not removed.
255 phi->SetDead();
256 break;
257 } else {
258 phi->AddInput(value);
259 }
260 }
261 }
262 }
263}
264
265static bool IsBlockPopulated(HBasicBlock* block) {
266 if (block->IsLoopHeader()) {
267 // Suspend checks were inserted into loop headers during building of dominator tree.
268 DCHECK(block->GetFirstInstruction()->IsSuspendCheck());
269 return block->GetFirstInstruction() != block->GetLastInstruction();
270 } else {
271 return !block->GetInstructions().IsEmpty();
272 }
273}
274
275bool HInstructionBuilder::Build() {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000276 DCHECK(code_item_ != nullptr);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100277 locals_for_.resize(
278 graph_->GetBlocks().size(),
279 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
David Brazdildee58d62016-04-07 09:54:26 +0000280
281 // Find locations where we want to generate extra stackmaps for native debugging.
282 // This allows us to generate the info only at interesting points (for example,
283 // at start of java statement) rather than before every dex instruction.
284 const bool native_debuggable = compiler_driver_ != nullptr &&
285 compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
286 ArenaBitVector* native_debug_info_locations = nullptr;
287 if (native_debuggable) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100288 native_debug_info_locations = FindNativeDebugInfoLocations();
David Brazdildee58d62016-04-07 09:54:26 +0000289 }
290
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100291 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
292 current_block_ = block;
David Brazdildee58d62016-04-07 09:54:26 +0000293 uint32_t block_dex_pc = current_block_->GetDexPc();
294
295 InitializeBlockLocals();
296
297 if (current_block_->IsEntryBlock()) {
298 InitializeParameters();
Vladimir Markoca6fff82017-10-03 14:49:14 +0100299 AppendInstruction(new (allocator_) HSuspendCheck(0u));
300 AppendInstruction(new (allocator_) HGoto(0u));
David Brazdildee58d62016-04-07 09:54:26 +0000301 continue;
302 } else if (current_block_->IsExitBlock()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100303 AppendInstruction(new (allocator_) HExit());
David Brazdildee58d62016-04-07 09:54:26 +0000304 continue;
305 } else if (current_block_->IsLoopHeader()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100306 HSuspendCheck* suspend_check = new (allocator_) HSuspendCheck(current_block_->GetDexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000307 current_block_->GetLoopInformation()->SetSuspendCheck(suspend_check);
308 // This is slightly odd because the loop header might not be empty (TryBoundary).
309 // But we're still creating the environment with locals from the top of the block.
310 InsertInstructionAtTop(suspend_check);
311 }
312
313 if (block_dex_pc == kNoDexPc || current_block_ != block_builder_->GetBlockAt(block_dex_pc)) {
314 // Synthetic block that does not need to be populated.
315 DCHECK(IsBlockPopulated(current_block_));
316 continue;
317 }
318
319 DCHECK(!IsBlockPopulated(current_block_));
320
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700321 uint32_t quicken_index = 0;
322 if (CanDecodeQuickenedInfo()) {
323 quicken_index = block_builder_->GetQuickenIndex(block_dex_pc);
324 }
325
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000326 for (const DexInstructionPcPair& pair : code_item_->Instructions(block_dex_pc)) {
David Brazdildee58d62016-04-07 09:54:26 +0000327 if (current_block_ == nullptr) {
328 // The previous instruction ended this block.
329 break;
330 }
331
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800332 const uint32_t dex_pc = pair.DexPc();
David Brazdildee58d62016-04-07 09:54:26 +0000333 if (dex_pc != block_dex_pc && FindBlockStartingAt(dex_pc) != nullptr) {
334 // This dex_pc starts a new basic block.
335 break;
336 }
337
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800338 if (current_block_->IsTryBlock() && IsThrowingDexInstruction(pair.Inst())) {
David Brazdildee58d62016-04-07 09:54:26 +0000339 PropagateLocalsToCatchBlocks();
340 }
341
342 if (native_debuggable && native_debug_info_locations->IsBitSet(dex_pc)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 AppendInstruction(new (allocator_) HNativeDebugInfo(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000344 }
345
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800346 if (!ProcessDexInstruction(pair.Inst(), dex_pc, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +0000347 return false;
348 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700349
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800350 if (QuickenInfoTable::NeedsIndexForInstruction(&pair.Inst())) {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -0700351 ++quicken_index;
352 }
David Brazdildee58d62016-04-07 09:54:26 +0000353 }
354
355 if (current_block_ != nullptr) {
356 // Branching instructions clear current_block, so we know the last
357 // instruction of the current block is not a branching instruction.
358 // We add an unconditional Goto to the next block.
359 DCHECK_EQ(current_block_->GetSuccessors().size(), 1u);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 AppendInstruction(new (allocator_) HGoto());
David Brazdildee58d62016-04-07 09:54:26 +0000361 }
362 }
363
364 SetLoopHeaderPhiInputs();
365
366 return true;
367}
368
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000369void HInstructionBuilder::BuildIntrinsic(ArtMethod* method) {
370 DCHECK(code_item_ == nullptr);
371 DCHECK(method->IsIntrinsic());
372
373 locals_for_.resize(
374 graph_->GetBlocks().size(),
375 ScopedArenaVector<HInstruction*>(local_allocator_->Adapter(kArenaAllocGraphBuilder)));
376
377 // Fill the entry block. Do not add suspend check, we do not want a suspend
378 // check in intrinsics; intrinsic methods are supposed to be fast.
379 current_block_ = graph_->GetEntryBlock();
380 InitializeBlockLocals();
381 InitializeParameters();
382 AppendInstruction(new (allocator_) HGoto(0u));
383
384 // Fill the body.
385 current_block_ = current_block_->GetSingleSuccessor();
386 InitializeBlockLocals();
387 DCHECK(!IsBlockPopulated(current_block_));
388
389 // Add the invoke and return instruction. Use HInvokeStaticOrDirect even
390 // for methods that would normally use an HInvokeVirtual (sharpen the call).
391 size_t in_vregs = graph_->GetNumberOfInVRegs();
392 size_t number_of_arguments =
393 in_vregs - std::count(current_locals_->end() - in_vregs, current_locals_->end(), nullptr);
394 uint32_t method_idx = dex_compilation_unit_->GetDexMethodIndex();
395 MethodReference target_method(dex_file_, method_idx);
396 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
397 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
398 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
399 /* method_load_data */ 0u
400 };
401 InvokeType invoke_type = dex_compilation_unit_->IsStatic() ? kStatic : kDirect;
402 HInvokeStaticOrDirect* invoke = new (allocator_) HInvokeStaticOrDirect(
403 allocator_,
404 number_of_arguments,
405 return_type_,
406 kNoDexPc,
407 method_idx,
408 method,
409 dispatch_info,
410 invoke_type,
411 target_method,
412 HInvokeStaticOrDirect::ClinitCheckRequirement::kNone);
413 HandleInvoke(invoke,
414 in_vregs,
415 /* args */ nullptr,
416 graph_->GetNumberOfVRegs() - in_vregs,
417 /* is_range */ true,
418 dex_file_->GetMethodShorty(method_idx),
419 /* clinit_check */ nullptr,
420 /* is_unresolved */ false);
421
422 // Add the return instruction.
423 if (return_type_ == DataType::Type::kVoid) {
424 AppendInstruction(new (allocator_) HReturnVoid());
425 } else {
426 AppendInstruction(new (allocator_) HReturn(invoke));
427 }
428
429 // Fill the exit block.
430 DCHECK_EQ(current_block_->GetSingleSuccessor(), graph_->GetExitBlock());
431 current_block_ = graph_->GetExitBlock();
432 InitializeBlockLocals();
433 AppendInstruction(new (allocator_) HExit());
434}
435
Vladimir Marko69d310e2017-10-09 14:12:23 +0100436ArenaBitVector* HInstructionBuilder::FindNativeDebugInfoLocations() {
David Brazdildee58d62016-04-07 09:54:26 +0000437 // The callback gets called when the line number changes.
438 // In other words, it marks the start of new java statement.
439 struct Callback {
440 static bool Position(void* ctx, const DexFile::PositionInfo& entry) {
441 static_cast<ArenaBitVector*>(ctx)->SetBit(entry.address_);
442 return false;
443 }
444 };
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800445 CodeItemDebugInfoAccessor accessor(dex_file_, code_item_);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100446 ArenaBitVector* locations = ArenaBitVector::Create(local_allocator_,
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800447 accessor.InsnsSizeInCodeUnits(),
Vladimir Marko69d310e2017-10-09 14:12:23 +0100448 /* expandable */ false,
449 kArenaAllocGraphBuilder);
450 locations->ClearAllBits();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800451 dex_file_->DecodeDebugPositionInfo(accessor.DebugInfoOffset(), Callback::Position, locations);
David Brazdildee58d62016-04-07 09:54:26 +0000452 // Instruction-specific tweaks.
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800453 for (const DexInstructionPcPair& inst : accessor) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800454 switch (inst->Opcode()) {
David Brazdildee58d62016-04-07 09:54:26 +0000455 case Instruction::MOVE_EXCEPTION: {
456 // Stop in native debugger after the exception has been moved.
457 // The compiler also expects the move at the start of basic block so
458 // we do not want to interfere by inserting native-debug-info before it.
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800459 locations->ClearBit(inst.DexPc());
460 DexInstructionIterator next = std::next(DexInstructionIterator(inst));
461 DCHECK(next.DexPc() != inst.DexPc());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800462 if (next != accessor.end()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700463 locations->SetBit(next.DexPc());
David Brazdildee58d62016-04-07 09:54:26 +0000464 }
465 break;
466 }
467 default:
468 break;
469 }
470 }
Vladimir Marko69d310e2017-10-09 14:12:23 +0100471 return locations;
David Brazdildee58d62016-04-07 09:54:26 +0000472}
473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100474HInstruction* HInstructionBuilder::LoadLocal(uint32_t reg_number, DataType::Type type) const {
David Brazdildee58d62016-04-07 09:54:26 +0000475 HInstruction* value = (*current_locals_)[reg_number];
476 DCHECK(value != nullptr);
477
478 // If the operation requests a specific type, we make sure its input is of that type.
479 if (type != value->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100480 if (DataType::IsFloatingPointType(type)) {
Aart Bik31883642016-06-06 15:02:44 -0700481 value = ssa_builder_->GetFloatOrDoubleEquivalent(value, type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 } else if (type == DataType::Type::kReference) {
Aart Bik31883642016-06-06 15:02:44 -0700483 value = ssa_builder_->GetReferenceTypeEquivalent(value);
David Brazdildee58d62016-04-07 09:54:26 +0000484 }
Aart Bik31883642016-06-06 15:02:44 -0700485 DCHECK(value != nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000486 }
487
488 return value;
489}
490
491void HInstructionBuilder::UpdateLocal(uint32_t reg_number, HInstruction* stored_value) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 DataType::Type stored_type = stored_value->GetType();
493 DCHECK_NE(stored_type, DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +0000494
495 // Storing into vreg `reg_number` may implicitly invalidate the surrounding
496 // registers. Consider the following cases:
497 // (1) Storing a wide value must overwrite previous values in both `reg_number`
498 // and `reg_number+1`. We store `nullptr` in `reg_number+1`.
499 // (2) If vreg `reg_number-1` holds a wide value, writing into `reg_number`
500 // must invalidate it. We store `nullptr` in `reg_number-1`.
501 // Consequently, storing a wide value into the high vreg of another wide value
502 // will invalidate both `reg_number-1` and `reg_number+1`.
503
504 if (reg_number != 0) {
505 HInstruction* local_low = (*current_locals_)[reg_number - 1];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100506 if (local_low != nullptr && DataType::Is64BitType(local_low->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000507 // The vreg we are storing into was previously the high vreg of a pair.
508 // We need to invalidate its low vreg.
509 DCHECK((*current_locals_)[reg_number] == nullptr);
510 (*current_locals_)[reg_number - 1] = nullptr;
511 }
512 }
513
514 (*current_locals_)[reg_number] = stored_value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100515 if (DataType::Is64BitType(stored_type)) {
David Brazdildee58d62016-04-07 09:54:26 +0000516 // We are storing a pair. Invalidate the instruction in the high vreg.
517 (*current_locals_)[reg_number + 1] = nullptr;
518 }
519}
520
521void HInstructionBuilder::InitializeParameters() {
522 DCHECK(current_block_->IsEntryBlock());
523
Vladimir Marko69d310e2017-10-09 14:12:23 +0100524 // outer_compilation_unit_ is null only when unit testing.
525 if (outer_compilation_unit_ == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000526 return;
527 }
528
529 const char* shorty = dex_compilation_unit_->GetShorty();
530 uint16_t number_of_parameters = graph_->GetNumberOfInVRegs();
531 uint16_t locals_index = graph_->GetNumberOfLocalVRegs();
532 uint16_t parameter_index = 0;
533
534 const DexFile::MethodId& referrer_method_id =
535 dex_file_->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
536 if (!dex_compilation_unit_->IsStatic()) {
537 // Add the implicit 'this' argument, not expressed in the signature.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100538 HParameterValue* parameter = new (allocator_) HParameterValue(*dex_file_,
David Brazdildee58d62016-04-07 09:54:26 +0000539 referrer_method_id.class_idx_,
540 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100541 DataType::Type::kReference,
Igor Murashkind01745e2017-04-05 16:40:31 -0700542 /* is_this */ true);
David Brazdildee58d62016-04-07 09:54:26 +0000543 AppendInstruction(parameter);
544 UpdateLocal(locals_index++, parameter);
545 number_of_parameters--;
Igor Murashkind01745e2017-04-05 16:40:31 -0700546 current_this_parameter_ = parameter;
547 } else {
548 DCHECK(current_this_parameter_ == nullptr);
David Brazdildee58d62016-04-07 09:54:26 +0000549 }
550
551 const DexFile::ProtoId& proto = dex_file_->GetMethodPrototype(referrer_method_id);
552 const DexFile::TypeList* arg_types = dex_file_->GetProtoParameters(proto);
553 for (int i = 0, shorty_pos = 1; i < number_of_parameters; i++) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100554 HParameterValue* parameter = new (allocator_) HParameterValue(
David Brazdildee58d62016-04-07 09:54:26 +0000555 *dex_file_,
556 arg_types->GetTypeItem(shorty_pos - 1).type_idx_,
557 parameter_index++,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100558 DataType::FromShorty(shorty[shorty_pos]),
Igor Murashkind01745e2017-04-05 16:40:31 -0700559 /* is_this */ false);
David Brazdildee58d62016-04-07 09:54:26 +0000560 ++shorty_pos;
561 AppendInstruction(parameter);
562 // Store the parameter value in the local that the dex code will use
563 // to reference that parameter.
564 UpdateLocal(locals_index++, parameter);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100565 if (DataType::Is64BitType(parameter->GetType())) {
David Brazdildee58d62016-04-07 09:54:26 +0000566 i++;
567 locals_index++;
568 parameter_index++;
569 }
570 }
571}
572
573template<typename T>
574void HInstructionBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100575 HInstruction* first = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
576 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100577 T* comparison = new (allocator_) T(first, second, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000578 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100579 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000580 current_block_ = nullptr;
581}
582
583template<typename T>
584void HInstructionBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100585 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100586 T* comparison = new (allocator_) T(value, graph_->GetIntConstant(0, dex_pc), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000587 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100588 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000589 current_block_ = nullptr;
590}
591
592template<typename T>
593void HInstructionBuilder::Unop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100594 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000595 uint32_t dex_pc) {
596 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100597 AppendInstruction(new (allocator_) T(type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000598 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
599}
600
601void HInstructionBuilder::Conversion_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100602 DataType::Type input_type,
603 DataType::Type result_type,
David Brazdildee58d62016-04-07 09:54:26 +0000604 uint32_t dex_pc) {
605 HInstruction* first = LoadLocal(instruction.VRegB(), input_type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100606 AppendInstruction(new (allocator_) HTypeConversion(result_type, first, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000607 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
608}
609
610template<typename T>
611void HInstructionBuilder::Binop_23x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100612 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000613 uint32_t dex_pc) {
614 HInstruction* first = LoadLocal(instruction.VRegB(), type);
615 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000617 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
618}
619
620template<typename T>
621void HInstructionBuilder::Binop_23x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100622 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000623 uint32_t dex_pc) {
624 HInstruction* first = LoadLocal(instruction.VRegB(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100625 HInstruction* second = LoadLocal(instruction.VRegC(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100626 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000627 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
628}
629
630void HInstructionBuilder::Binop_23x_cmp(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100631 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000632 ComparisonBias bias,
633 uint32_t dex_pc) {
634 HInstruction* first = LoadLocal(instruction.VRegB(), type);
635 HInstruction* second = LoadLocal(instruction.VRegC(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 AppendInstruction(new (allocator_) HCompare(type, first, second, bias, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000637 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
638}
639
640template<typename T>
641void HInstructionBuilder::Binop_12x_shift(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100642 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000643 uint32_t dex_pc) {
644 HInstruction* first = LoadLocal(instruction.VRegA(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100645 HInstruction* second = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100646 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000647 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
648}
649
650template<typename T>
651void HInstructionBuilder::Binop_12x(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100652 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000653 uint32_t dex_pc) {
654 HInstruction* first = LoadLocal(instruction.VRegA(), type);
655 HInstruction* second = LoadLocal(instruction.VRegB(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100656 AppendInstruction(new (allocator_) T(type, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000657 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
658}
659
660template<typename T>
661void HInstructionBuilder::Binop_22s(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100662 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000663 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s(), dex_pc);
664 if (reverse) {
665 std::swap(first, second);
666 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100667 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000668 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
669}
670
671template<typename T>
672void HInstructionBuilder::Binop_22b(const Instruction& instruction, bool reverse, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100673 HInstruction* first = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000674 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b(), dex_pc);
675 if (reverse) {
676 std::swap(first, second);
677 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100678 AppendInstruction(new (allocator_) T(DataType::Type::kInt32, first, second, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000679 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
680}
681
Igor Murashkind01745e2017-04-05 16:40:31 -0700682// Does the method being compiled need any constructor barriers being inserted?
683// (Always 'false' for methods that aren't <init>.)
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700684static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
Igor Murashkin032cacd2017-04-06 14:40:08 -0700685 // Can be null in unit tests only.
686 if (UNLIKELY(cu == nullptr)) {
687 return false;
688 }
689
David Brazdildee58d62016-04-07 09:54:26 +0000690 Thread* self = Thread::Current();
691 return cu->IsConstructor()
Igor Murashkind01745e2017-04-05 16:40:31 -0700692 && !cu->IsStatic()
693 // RequiresConstructorBarrier must only be queried for <init> methods;
694 // it's effectively "false" for every other method.
695 //
696 // See CompilerDriver::RequiresConstructBarrier for more explanation.
Mathieu Chartierc4ae9162016-04-07 13:19:19 -0700697 && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000698}
699
700// Returns true if `block` has only one successor which starts at the next
701// dex_pc after `instruction` at `dex_pc`.
702static bool IsFallthroughInstruction(const Instruction& instruction,
703 uint32_t dex_pc,
704 HBasicBlock* block) {
705 uint32_t next_dex_pc = dex_pc + instruction.SizeInCodeUnits();
706 return block->GetSingleSuccessor()->GetDexPc() == next_dex_pc;
707}
708
709void HInstructionBuilder::BuildSwitch(const Instruction& instruction, uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100710 HInstruction* value = LoadLocal(instruction.VRegA(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +0000711 DexSwitchTable table(instruction, dex_pc);
712
713 if (table.GetNumEntries() == 0) {
714 // Empty Switch. Code falls through to the next block.
715 DCHECK(IsFallthroughInstruction(instruction, dex_pc, current_block_));
Vladimir Markoca6fff82017-10-03 14:49:14 +0100716 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000717 } else if (table.ShouldBuildDecisionTree()) {
718 for (DexSwitchTableIterator it(table); !it.Done(); it.Advance()) {
719 HInstruction* case_value = graph_->GetIntConstant(it.CurrentKey(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100720 HEqual* comparison = new (allocator_) HEqual(value, case_value, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +0000721 AppendInstruction(comparison);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100722 AppendInstruction(new (allocator_) HIf(comparison, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000723
724 if (!it.IsLast()) {
725 current_block_ = FindBlockStartingAt(it.GetDexPcForCurrentIndex());
726 }
727 }
728 } else {
729 AppendInstruction(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100730 new (allocator_) HPackedSwitch(table.GetEntryAt(0), table.GetNumEntries(), value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000731 }
732
733 current_block_ = nullptr;
734}
735
736void HInstructionBuilder::BuildReturn(const Instruction& instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100737 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +0000738 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 if (type == DataType::Type::kVoid) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700740 // Only <init> (which is a return-void) could possibly have a constructor fence.
Igor Murashkin032cacd2017-04-06 14:40:08 -0700741 // This may insert additional redundant constructor fences from the super constructors.
742 // TODO: remove redundant constructor fences (b/36656456).
743 if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
Igor Murashkind01745e2017-04-05 16:40:31 -0700744 // Compiling instance constructor.
Vladimir Markoba118822017-06-12 15:41:56 +0100745 DCHECK_STREQ("<init>", graph_->GetMethodName());
Igor Murashkind01745e2017-04-05 16:40:31 -0700746
747 HInstruction* fence_target = current_this_parameter_;
748 DCHECK(fence_target != nullptr);
749
Vladimir Markoca6fff82017-10-03 14:49:14 +0100750 AppendInstruction(new (allocator_) HConstructorFence(fence_target, dex_pc, allocator_));
Igor Murashkin6ef45672017-08-08 13:59:55 -0700751 MaybeRecordStat(
752 compilation_stats_,
753 MethodCompilationStat::kConstructorFenceGeneratedFinal);
David Brazdildee58d62016-04-07 09:54:26 +0000754 }
Vladimir Markoca6fff82017-10-03 14:49:14 +0100755 AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000756 } else {
Igor Murashkind01745e2017-04-05 16:40:31 -0700757 DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
David Brazdildee58d62016-04-07 09:54:26 +0000758 HInstruction* value = LoadLocal(instruction.VRegA(), type);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 AppendInstruction(new (allocator_) HReturn(value, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +0000760 }
761 current_block_ = nullptr;
762}
763
764static InvokeType GetInvokeTypeFromOpCode(Instruction::Code opcode) {
765 switch (opcode) {
766 case Instruction::INVOKE_STATIC:
767 case Instruction::INVOKE_STATIC_RANGE:
768 return kStatic;
769 case Instruction::INVOKE_DIRECT:
770 case Instruction::INVOKE_DIRECT_RANGE:
771 return kDirect;
772 case Instruction::INVOKE_VIRTUAL:
773 case Instruction::INVOKE_VIRTUAL_QUICK:
774 case Instruction::INVOKE_VIRTUAL_RANGE:
775 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
776 return kVirtual;
777 case Instruction::INVOKE_INTERFACE:
778 case Instruction::INVOKE_INTERFACE_RANGE:
779 return kInterface;
780 case Instruction::INVOKE_SUPER_RANGE:
781 case Instruction::INVOKE_SUPER:
782 return kSuper;
783 default:
784 LOG(FATAL) << "Unexpected invoke opcode: " << opcode;
785 UNREACHABLE();
786 }
787}
788
789ArtMethod* HInstructionBuilder::ResolveMethod(uint16_t method_idx, InvokeType invoke_type) {
790 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +0000791
792 ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000793 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100794
Vladimir Markoba118822017-06-12 15:41:56 +0100795 ArtMethod* resolved_method =
796 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Vladimir Markoba118822017-06-12 15:41:56 +0100797 method_idx,
798 dex_compilation_unit_->GetDexCache(),
799 class_loader,
800 graph_->GetArtMethod(),
801 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000802
803 if (UNLIKELY(resolved_method == nullptr)) {
804 // Clean up any exception left by type resolution.
805 soa.Self()->ClearException();
806 return nullptr;
807 }
808
Vladimir Markoba118822017-06-12 15:41:56 +0100809 // The referrer may be unresolved for AOT if we're compiling a class that cannot be
810 // resolved because, for example, we don't find a superclass in the classpath.
811 if (graph_->GetArtMethod() == nullptr) {
812 // The class linker cannot check access without a referrer, so we have to do it.
813 // Fall back to HInvokeUnresolved if the method isn't public.
David Brazdildee58d62016-04-07 09:54:26 +0000814 if (!resolved_method->IsPublic()) {
815 return nullptr;
816 }
David Brazdildee58d62016-04-07 09:54:26 +0000817 }
818
819 // We have to special case the invoke-super case, as ClassLinker::ResolveMethod does not.
820 // We need to look at the referrer's super class vtable. We need to do this to know if we need to
821 // make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
822 // which require runtime handling.
823 if (invoke_type == kSuper) {
Vladimir Markoba118822017-06-12 15:41:56 +0100824 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Andreas Gampefa4333d2017-02-14 11:10:34 -0800825 if (compiling_class == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +0000826 // We could not determine the method's class we need to wait until runtime.
827 DCHECK(Runtime::Current()->IsAotCompiler());
828 return nullptr;
829 }
Vladimir Markoba118822017-06-12 15:41:56 +0100830 ObjPtr<mirror::Class> referenced_class = class_linker->LookupResolvedType(
Vladimir Markoba118822017-06-12 15:41:56 +0100831 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx).class_idx_,
832 dex_compilation_unit_->GetDexCache().Get(),
833 class_loader.Get());
834 DCHECK(referenced_class != nullptr); // We have already resolved a method from this class.
835 if (!referenced_class->IsAssignableFrom(compiling_class)) {
Aart Bikf663e342016-04-04 17:28:59 -0700836 // We cannot statically determine the target method. The runtime will throw a
837 // NoSuchMethodError on this one.
838 return nullptr;
839 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100840 ArtMethod* actual_method;
Vladimir Markoba118822017-06-12 15:41:56 +0100841 if (referenced_class->IsInterface()) {
842 actual_method = referenced_class->FindVirtualMethodForInterfaceSuper(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100843 resolved_method, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000844 } else {
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100845 uint16_t vtable_index = resolved_method->GetMethodIndex();
846 actual_method = compiling_class->GetSuperClass()->GetVTableEntry(
847 vtable_index, class_linker->GetImagePointerSize());
David Brazdildee58d62016-04-07 09:54:26 +0000848 }
Nicolas Geoffray393fdb82016-04-25 14:58:06 +0100849 if (actual_method != resolved_method &&
850 !IsSameDexFile(*actual_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
851 // The back-end code generator relies on this check in order to ensure that it will not
852 // attempt to read the dex_cache with a dex_method_index that is not from the correct
853 // dex_file. If we didn't do this check then the dex_method_index will not be updated in the
854 // builder, which means that the code-generator (and compiler driver during sharpening and
855 // inliner, maybe) might invoke an incorrect method.
856 // TODO: The actual method could still be referenced in the current dex file, so we
857 // could try locating it.
858 // TODO: Remove the dex_file restriction.
859 return nullptr;
860 }
861 if (!actual_method->IsInvokable()) {
862 // Fail if the actual method cannot be invoked. Otherwise, the runtime resolution stub
863 // could resolve the callee to the wrong method.
864 return nullptr;
865 }
866 resolved_method = actual_method;
David Brazdildee58d62016-04-07 09:54:26 +0000867 }
868
David Brazdildee58d62016-04-07 09:54:26 +0000869 return resolved_method;
870}
871
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100872static bool IsStringConstructor(ArtMethod* method) {
873 ScopedObjectAccess soa(Thread::Current());
874 return method->GetDeclaringClass()->IsStringClass() && method->IsConstructor();
875}
876
David Brazdildee58d62016-04-07 09:54:26 +0000877bool HInstructionBuilder::BuildInvoke(const Instruction& instruction,
878 uint32_t dex_pc,
879 uint32_t method_idx,
880 uint32_t number_of_vreg_arguments,
881 bool is_range,
882 uint32_t* args,
883 uint32_t register_index) {
884 InvokeType invoke_type = GetInvokeTypeFromOpCode(instruction.Opcode());
885 const char* descriptor = dex_file_->GetMethodShorty(method_idx);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
David Brazdildee58d62016-04-07 09:54:26 +0000887
888 // Remove the return type from the 'proto'.
889 size_t number_of_arguments = strlen(descriptor) - 1;
890 if (invoke_type != kStatic) { // instance call
891 // One extra argument for 'this'.
892 number_of_arguments++;
893 }
894
David Brazdildee58d62016-04-07 09:54:26 +0000895 ArtMethod* resolved_method = ResolveMethod(method_idx, invoke_type);
896
897 if (UNLIKELY(resolved_method == nullptr)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700898 MaybeRecordStat(compilation_stats_,
899 MethodCompilationStat::kUnresolvedMethod);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100900 HInvoke* invoke = new (allocator_) HInvokeUnresolved(allocator_,
901 number_of_arguments,
902 return_type,
903 dex_pc,
904 method_idx,
905 invoke_type);
David Brazdildee58d62016-04-07 09:54:26 +0000906 return HandleInvoke(invoke,
907 number_of_vreg_arguments,
908 args,
909 register_index,
910 is_range,
911 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -0700912 nullptr, /* clinit_check */
913 true /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +0000914 }
915
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100916 // Replace calls to String.<init> with StringFactory.
917 if (IsStringConstructor(resolved_method)) {
918 uint32_t string_init_entry_point = WellKnownClasses::StringInitToEntryPoint(resolved_method);
919 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
920 HInvokeStaticOrDirect::MethodLoadKind::kStringInit,
921 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000922 dchecked_integral_cast<uint64_t>(string_init_entry_point)
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100923 };
924 MethodReference target_method(dex_file_, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100925 HInvoke* invoke = new (allocator_) HInvokeStaticOrDirect(
926 allocator_,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100927 number_of_arguments - 1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100928 DataType::Type::kReference /*return_type */,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100929 dex_pc,
930 method_idx,
931 nullptr,
932 dispatch_info,
933 invoke_type,
934 target_method,
935 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit);
936 return HandleStringInit(invoke,
937 number_of_vreg_arguments,
938 args,
939 register_index,
940 is_range,
941 descriptor);
942 }
943
David Brazdildee58d62016-04-07 09:54:26 +0000944 // Potential class initialization check, in the case of a static method call.
945 HClinitCheck* clinit_check = nullptr;
946 HInvoke* invoke = nullptr;
947 if (invoke_type == kDirect || invoke_type == kStatic || invoke_type == kSuper) {
948 // By default, consider that the called method implicitly requires
949 // an initialization check of its declaring method.
950 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement
951 = HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit;
952 ScopedObjectAccess soa(Thread::Current());
953 if (invoke_type == kStatic) {
954 clinit_check = ProcessClinitCheckForInvoke(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000955 dex_pc, resolved_method, &clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000956 } else if (invoke_type == kSuper) {
957 if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100958 // Update the method index to the one resolved. Note that this may be a no-op if
David Brazdildee58d62016-04-07 09:54:26 +0000959 // we resolved to the method referenced by the instruction.
960 method_idx = resolved_method->GetDexMethodIndex();
David Brazdildee58d62016-04-07 09:54:26 +0000961 }
962 }
963
964 HInvokeStaticOrDirect::DispatchInfo dispatch_info = {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100965 HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall,
David Brazdildee58d62016-04-07 09:54:26 +0000966 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000967 0u
David Brazdildee58d62016-04-07 09:54:26 +0000968 };
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100969 MethodReference target_method(resolved_method->GetDexFile(),
970 resolved_method->GetDexMethodIndex());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100971 invoke = new (allocator_) HInvokeStaticOrDirect(allocator_,
972 number_of_arguments,
973 return_type,
974 dex_pc,
975 method_idx,
976 resolved_method,
977 dispatch_info,
978 invoke_type,
979 target_method,
980 clinit_check_requirement);
David Brazdildee58d62016-04-07 09:54:26 +0000981 } else if (invoke_type == kVirtual) {
982 ScopedObjectAccess soa(Thread::Current()); // Needed for the method index
Vladimir Markoca6fff82017-10-03 14:49:14 +0100983 invoke = new (allocator_) HInvokeVirtual(allocator_,
984 number_of_arguments,
985 return_type,
986 dex_pc,
987 method_idx,
988 resolved_method,
989 resolved_method->GetMethodIndex());
David Brazdildee58d62016-04-07 09:54:26 +0000990 } else {
991 DCHECK_EQ(invoke_type, kInterface);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100992 ScopedObjectAccess soa(Thread::Current()); // Needed for the IMT index.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100993 invoke = new (allocator_) HInvokeInterface(allocator_,
994 number_of_arguments,
995 return_type,
996 dex_pc,
997 method_idx,
998 resolved_method,
999 ImTable::GetImtIndex(resolved_method));
David Brazdildee58d62016-04-07 09:54:26 +00001000 }
1001
1002 return HandleInvoke(invoke,
1003 number_of_vreg_arguments,
1004 args,
1005 register_index,
1006 is_range,
1007 descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001008 clinit_check,
1009 false /* is_unresolved */);
David Brazdildee58d62016-04-07 09:54:26 +00001010}
1011
Orion Hodsonac141392017-01-13 11:53:47 +00001012bool HInstructionBuilder::BuildInvokePolymorphic(const Instruction& instruction ATTRIBUTE_UNUSED,
1013 uint32_t dex_pc,
1014 uint32_t method_idx,
1015 uint32_t proto_idx,
1016 uint32_t number_of_vreg_arguments,
1017 bool is_range,
1018 uint32_t* args,
1019 uint32_t register_index) {
1020 const char* descriptor = dex_file_->GetShorty(proto_idx);
1021 DCHECK_EQ(1 + ArtMethod::NumArgRegisters(descriptor), number_of_vreg_arguments);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001022 DataType::Type return_type = DataType::FromShorty(descriptor[0]);
Orion Hodsonac141392017-01-13 11:53:47 +00001023 size_t number_of_arguments = strlen(descriptor);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001024 HInvoke* invoke = new (allocator_) HInvokePolymorphic(allocator_,
1025 number_of_arguments,
1026 return_type,
1027 dex_pc,
1028 method_idx);
Orion Hodsonac141392017-01-13 11:53:47 +00001029 return HandleInvoke(invoke,
1030 number_of_vreg_arguments,
1031 args,
1032 register_index,
1033 is_range,
1034 descriptor,
1035 nullptr /* clinit_check */,
1036 false /* is_unresolved */);
1037}
1038
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001039HNewInstance* HInstructionBuilder::BuildNewInstance(dex::TypeIndex type_index, uint32_t dex_pc) {
Vladimir Marko3cd50df2016-04-13 19:29:26 +01001040 ScopedObjectAccess soa(Thread::Current());
David Brazdildee58d62016-04-07 09:54:26 +00001041
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001042 HLoadClass* load_class = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001043
David Brazdildee58d62016-04-07 09:54:26 +00001044 HInstruction* cls = load_class;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001045 Handle<mirror::Class> klass = load_class->GetClass();
1046
1047 if (!IsInitialized(klass)) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001048 cls = new (allocator_) HClinitCheck(load_class, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001049 AppendInstruction(cls);
1050 }
1051
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001052 // Only the access check entrypoint handles the finalizable class case. If we
1053 // need access checks, then we haven't resolved the method and the class may
1054 // again be finalizable.
1055 QuickEntrypointEnum entrypoint = kQuickAllocObjectInitialized;
1056 if (load_class->NeedsAccessCheck() || klass->IsFinalizable() || !klass->IsInstantiable()) {
1057 entrypoint = kQuickAllocObjectWithChecks;
1058 }
1059
1060 // Consider classes we haven't resolved as potentially finalizable.
Andreas Gampefa4333d2017-02-14 11:10:34 -08001061 bool finalizable = (klass == nullptr) || klass->IsFinalizable();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001062
Vladimir Markoca6fff82017-10-03 14:49:14 +01001063 HNewInstance* new_instance = new (allocator_) HNewInstance(
David Brazdildee58d62016-04-07 09:54:26 +00001064 cls,
David Brazdildee58d62016-04-07 09:54:26 +00001065 dex_pc,
1066 type_index,
1067 *dex_compilation_unit_->GetDexFile(),
David Brazdildee58d62016-04-07 09:54:26 +00001068 finalizable,
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001069 entrypoint);
1070 AppendInstruction(new_instance);
1071
1072 return new_instance;
1073}
1074
1075void HInstructionBuilder::BuildConstructorFenceForAllocation(HInstruction* allocation) {
1076 DCHECK(allocation != nullptr &&
George Burgess IVf2072992017-05-23 15:36:41 -07001077 (allocation->IsNewInstance() ||
1078 allocation->IsNewArray())); // corresponding to "new" keyword in JLS.
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001079
1080 if (allocation->IsNewInstance()) {
1081 // STRING SPECIAL HANDLING:
1082 // -------------------------------
1083 // Strings have a real HNewInstance node but they end up always having 0 uses.
1084 // All uses of a String HNewInstance are always transformed to replace their input
1085 // of the HNewInstance with an input of the invoke to StringFactory.
1086 //
1087 // Do not emit an HConstructorFence here since it can inhibit some String new-instance
1088 // optimizations (to pass checker tests that rely on those optimizations).
1089 HNewInstance* new_inst = allocation->AsNewInstance();
1090 HLoadClass* load_class = new_inst->GetLoadClass();
1091
1092 Thread* self = Thread::Current();
1093 ScopedObjectAccess soa(self);
1094 StackHandleScope<1> hs(self);
1095 Handle<mirror::Class> klass = load_class->GetClass();
1096 if (klass != nullptr && klass->IsStringClass()) {
1097 return;
1098 // Note: Do not use allocation->IsStringAlloc which requires
1099 // a valid ReferenceTypeInfo, but that doesn't get made until after reference type
1100 // propagation (and instruction builder is too early).
1101 }
1102 // (In terms of correctness, the StringFactory needs to provide its own
1103 // default initialization barrier, see below.)
1104 }
1105
1106 // JLS 17.4.5 "Happens-before Order" describes:
1107 //
1108 // The default initialization of any object happens-before any other actions (other than
1109 // default-writes) of a program.
1110 //
1111 // In our implementation the default initialization of an object to type T means
1112 // setting all of its initial data (object[0..size)) to 0, and setting the
1113 // object's class header (i.e. object.getClass() == T.class).
1114 //
1115 // In practice this fence ensures that the writes to the object header
1116 // are visible to other threads if this object escapes the current thread.
1117 // (and in theory the 0-initializing, but that happens automatically
1118 // when new memory pages are mapped in by the OS).
1119 HConstructorFence* ctor_fence =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001120 new (allocator_) HConstructorFence(allocation, allocation->GetDexPc(), allocator_);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001121 AppendInstruction(ctor_fence);
Igor Murashkin6ef45672017-08-08 13:59:55 -07001122 MaybeRecordStat(
1123 compilation_stats_,
1124 MethodCompilationStat::kConstructorFenceGeneratedNew);
David Brazdildee58d62016-04-07 09:54:26 +00001125}
1126
Vladimir Marko28e012a2017-12-07 11:22:59 +00001127static bool IsSubClass(ObjPtr<mirror::Class> to_test, ObjPtr<mirror::Class> super_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001128 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdildee58d62016-04-07 09:54:26 +00001129 return to_test != nullptr && !to_test->IsInterface() && to_test->IsSubClass(super_class);
1130}
1131
1132bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001133 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001134 return false;
1135 }
1136
1137 // `CanAssumeClassIsLoaded` will return true if we're JITting, or will
1138 // check whether the class is in an image for the AOT compilation.
1139 if (cls->IsInitialized() &&
1140 compiler_driver_->CanAssumeClassIsLoaded(cls.Get())) {
1141 return true;
1142 }
1143
1144 if (IsSubClass(GetOutermostCompilingClass(), cls.Get())) {
1145 return true;
1146 }
1147
1148 // TODO: We should walk over the inlined methods, but we don't pass
1149 // that information to the builder.
1150 if (IsSubClass(GetCompilingClass(), cls.Get())) {
1151 return true;
1152 }
1153
1154 return false;
1155}
1156
1157HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
1158 uint32_t dex_pc,
1159 ArtMethod* resolved_method,
David Brazdildee58d62016-04-07 09:54:26 +00001160 HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001161 Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
David Brazdildee58d62016-04-07 09:54:26 +00001162
1163 HClinitCheck* clinit_check = nullptr;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001164 if (IsInitialized(klass)) {
David Brazdildee58d62016-04-07 09:54:26 +00001165 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001166 } else {
1167 HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
1168 klass->GetDexFile(),
1169 klass,
1170 dex_pc,
1171 /* needs_access_check */ false);
1172 if (cls != nullptr) {
1173 *clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001174 clinit_check = new (allocator_) HClinitCheck(cls, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001175 AppendInstruction(clinit_check);
1176 }
David Brazdildee58d62016-04-07 09:54:26 +00001177 }
1178 return clinit_check;
1179}
1180
1181bool HInstructionBuilder::SetupInvokeArguments(HInvoke* invoke,
1182 uint32_t number_of_vreg_arguments,
1183 uint32_t* args,
1184 uint32_t register_index,
1185 bool is_range,
1186 const char* descriptor,
1187 size_t start_index,
1188 size_t* argument_index) {
1189 uint32_t descriptor_index = 1; // Skip the return type.
1190
1191 for (size_t i = start_index;
1192 // Make sure we don't go over the expected arguments or over the number of
1193 // dex registers given. If the instruction was seen as dead by the verifier,
1194 // it hasn't been properly checked.
1195 (i < number_of_vreg_arguments) && (*argument_index < invoke->GetNumberOfArguments());
1196 i++, (*argument_index)++) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 DataType::Type type = DataType::FromShorty(descriptor[descriptor_index++]);
1198 bool is_wide = (type == DataType::Type::kInt64) || (type == DataType::Type::kFloat64);
David Brazdildee58d62016-04-07 09:54:26 +00001199 if (!is_range
1200 && is_wide
1201 && ((i + 1 == number_of_vreg_arguments) || (args[i] + 1 != args[i + 1]))) {
1202 // Longs and doubles should be in pairs, that is, sequential registers. The verifier should
1203 // reject any class where this is violated. However, the verifier only does these checks
1204 // on non trivially dead instructions, so we just bailout the compilation.
1205 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001206 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001207 << " because of non-sequential dex register pair in wide argument";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001208 MaybeRecordStat(compilation_stats_,
1209 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001210 return false;
1211 }
1212 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
1213 invoke->SetArgumentAt(*argument_index, arg);
1214 if (is_wide) {
1215 i++;
1216 }
1217 }
1218
1219 if (*argument_index != invoke->GetNumberOfArguments()) {
1220 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07001221 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00001222 << " because of wrong number of arguments in invoke instruction";
Igor Murashkin1e065a52017-08-09 13:20:34 -07001223 MaybeRecordStat(compilation_stats_,
1224 MethodCompilationStat::kNotCompiledMalformedOpcode);
David Brazdildee58d62016-04-07 09:54:26 +00001225 return false;
1226 }
1227
1228 if (invoke->IsInvokeStaticOrDirect() &&
1229 HInvokeStaticOrDirect::NeedsCurrentMethodInput(
1230 invoke->AsInvokeStaticOrDirect()->GetMethodLoadKind())) {
1231 invoke->SetArgumentAt(*argument_index, graph_->GetCurrentMethod());
1232 (*argument_index)++;
1233 }
1234
1235 return true;
1236}
1237
1238bool HInstructionBuilder::HandleInvoke(HInvoke* invoke,
1239 uint32_t number_of_vreg_arguments,
1240 uint32_t* args,
1241 uint32_t register_index,
1242 bool is_range,
1243 const char* descriptor,
Aart Bik296fbb42016-06-07 13:49:12 -07001244 HClinitCheck* clinit_check,
1245 bool is_unresolved) {
David Brazdildee58d62016-04-07 09:54:26 +00001246 DCHECK(!invoke->IsInvokeStaticOrDirect() || !invoke->AsInvokeStaticOrDirect()->IsStringInit());
1247
1248 size_t start_index = 0;
1249 size_t argument_index = 0;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01001250 if (invoke->GetInvokeType() != InvokeType::kStatic) { // Instance call.
Aart Bik296fbb42016-06-07 13:49:12 -07001251 uint32_t obj_reg = is_range ? register_index : args[0];
1252 HInstruction* arg = is_unresolved
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001253 ? LoadLocal(obj_reg, DataType::Type::kReference)
Aart Bik296fbb42016-06-07 13:49:12 -07001254 : LoadNullCheckedLocal(obj_reg, invoke->GetDexPc());
David Brazdilc120bbe2016-04-22 16:57:00 +01001255 invoke->SetArgumentAt(0, arg);
David Brazdildee58d62016-04-07 09:54:26 +00001256 start_index = 1;
1257 argument_index = 1;
1258 }
1259
1260 if (!SetupInvokeArguments(invoke,
1261 number_of_vreg_arguments,
1262 args,
1263 register_index,
1264 is_range,
1265 descriptor,
1266 start_index,
1267 &argument_index)) {
1268 return false;
1269 }
1270
1271 if (clinit_check != nullptr) {
1272 // Add the class initialization check as last input of `invoke`.
1273 DCHECK(invoke->IsInvokeStaticOrDirect());
1274 DCHECK(invoke->AsInvokeStaticOrDirect()->GetClinitCheckRequirement()
1275 == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit);
1276 invoke->SetArgumentAt(argument_index, clinit_check);
1277 argument_index++;
1278 }
1279
1280 AppendInstruction(invoke);
1281 latest_result_ = invoke;
1282
1283 return true;
1284}
1285
1286bool HInstructionBuilder::HandleStringInit(HInvoke* invoke,
1287 uint32_t number_of_vreg_arguments,
1288 uint32_t* args,
1289 uint32_t register_index,
1290 bool is_range,
1291 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;
1297 if (!SetupInvokeArguments(invoke,
1298 number_of_vreg_arguments,
1299 args,
1300 register_index,
1301 is_range,
1302 descriptor,
1303 start_index,
1304 &argument_index)) {
1305 return false;
1306 }
1307
1308 AppendInstruction(invoke);
1309
1310 // This is a StringFactory call, not an actual String constructor. Its result
1311 // replaces the empty String pre-allocated by NewInstance.
1312 uint32_t orig_this_reg = is_range ? register_index : args[0];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001313 HInstruction* arg_this = LoadLocal(orig_this_reg, DataType::Type::kReference);
David Brazdildee58d62016-04-07 09:54:26 +00001314
1315 // Replacing the NewInstance might render it redundant. Keep a list of these
1316 // to be visited once it is clear whether it is has remaining uses.
1317 if (arg_this->IsNewInstance()) {
1318 ssa_builder_->AddUninitializedString(arg_this->AsNewInstance());
1319 } else {
1320 DCHECK(arg_this->IsPhi());
1321 // NewInstance is not the direct input of the StringFactory call. It might
1322 // be redundant but optimizing this case is not worth the effort.
1323 }
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,
1659 uint32_t number_of_vreg_arguments,
1660 bool is_range,
1661 uint32_t* args,
1662 uint32_t register_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001663 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments, dex_pc);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001664 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001665 HNewArray* const object = new (allocator_) HNewArray(cls, length, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001666 AppendInstruction(object);
1667
1668 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
1669 DCHECK_EQ(descriptor[0], '[') << descriptor;
1670 char primitive = descriptor[1];
1671 DCHECK(primitive == 'I'
1672 || primitive == 'L'
1673 || primitive == '[') << descriptor;
1674 bool is_reference_array = (primitive == 'L') || (primitive == '[');
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001675 DataType::Type type = is_reference_array ? DataType::Type::kReference : DataType::Type::kInt32;
David Brazdildee58d62016-04-07 09:54:26 +00001676
1677 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
1678 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
1679 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001680 HArraySet* aset = new (allocator_) HArraySet(object, index, value, type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001681 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1682 AppendInstruction(aset);
1683 }
1684 latest_result_ = object;
Igor Murashkin79d8fa72017-04-18 09:37:23 -07001685
1686 return object;
David Brazdildee58d62016-04-07 09:54:26 +00001687}
1688
1689template <typename T>
1690void HInstructionBuilder::BuildFillArrayData(HInstruction* object,
1691 const T* data,
1692 uint32_t element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 DataType::Type anticipated_type,
David Brazdildee58d62016-04-07 09:54:26 +00001694 uint32_t dex_pc) {
1695 for (uint32_t i = 0; i < element_count; ++i) {
1696 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1697 HInstruction* value = graph_->GetIntConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001698 HArraySet* aset = new (allocator_) HArraySet(object, index, value, anticipated_type, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001699 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1700 AppendInstruction(aset);
1701 }
1702}
1703
1704void HInstructionBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) {
David Brazdilc120bbe2016-04-22 16:57:00 +01001705 HInstruction* array = LoadNullCheckedLocal(instruction.VRegA_31t(), dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001706
1707 int32_t payload_offset = instruction.VRegB_31t() + dex_pc;
1708 const Instruction::ArrayDataPayload* payload =
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001709 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_item_->insns_ + payload_offset);
David Brazdildee58d62016-04-07 09:54:26 +00001710 const uint8_t* data = payload->data;
1711 uint32_t element_count = payload->element_count;
1712
Vladimir Markoc69fba22016-09-06 16:49:15 +01001713 if (element_count == 0u) {
1714 // For empty payload we emit only the null check above.
1715 return;
1716 }
1717
Vladimir Markoca6fff82017-10-03 14:49:14 +01001718 HInstruction* length = new (allocator_) HArrayLength(array, dex_pc);
Vladimir Markoc69fba22016-09-06 16:49:15 +01001719 AppendInstruction(length);
1720
David Brazdildee58d62016-04-07 09:54:26 +00001721 // Implementation of this DEX instruction seems to be that the bounds check is
1722 // done before doing any stores.
1723 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1, dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001724 AppendInstruction(new (allocator_) HBoundsCheck(last_index, length, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001725
1726 switch (payload->element_width) {
1727 case 1:
David Brazdilc120bbe2016-04-22 16:57:00 +01001728 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001729 reinterpret_cast<const int8_t*>(data),
1730 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001731 DataType::Type::kInt8,
David Brazdildee58d62016-04-07 09:54:26 +00001732 dex_pc);
1733 break;
1734 case 2:
David Brazdilc120bbe2016-04-22 16:57:00 +01001735 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001736 reinterpret_cast<const int16_t*>(data),
1737 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001738 DataType::Type::kInt16,
David Brazdildee58d62016-04-07 09:54:26 +00001739 dex_pc);
1740 break;
1741 case 4:
David Brazdilc120bbe2016-04-22 16:57:00 +01001742 BuildFillArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001743 reinterpret_cast<const int32_t*>(data),
1744 element_count,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 DataType::Type::kInt32,
David Brazdildee58d62016-04-07 09:54:26 +00001746 dex_pc);
1747 break;
1748 case 8:
David Brazdilc120bbe2016-04-22 16:57:00 +01001749 BuildFillWideArrayData(array,
David Brazdildee58d62016-04-07 09:54:26 +00001750 reinterpret_cast<const int64_t*>(data),
1751 element_count,
1752 dex_pc);
1753 break;
1754 default:
1755 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1756 }
1757 graph_->SetHasBoundsChecks(true);
1758}
1759
1760void HInstructionBuilder::BuildFillWideArrayData(HInstruction* object,
1761 const int64_t* data,
1762 uint32_t element_count,
1763 uint32_t dex_pc) {
1764 for (uint32_t i = 0; i < element_count; ++i) {
1765 HInstruction* index = graph_->GetIntConstant(i, dex_pc);
1766 HInstruction* value = graph_->GetLongConstant(data[i], dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001767 HArraySet* aset =
1768 new (allocator_) HArraySet(object, index, value, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001769 ssa_builder_->MaybeAddAmbiguousArraySet(aset);
1770 AppendInstruction(aset);
1771 }
1772}
1773
1774static TypeCheckKind ComputeTypeCheckKind(Handle<mirror::Class> cls)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001775 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001776 if (cls == nullptr) {
David Brazdildee58d62016-04-07 09:54:26 +00001777 return TypeCheckKind::kUnresolvedCheck;
1778 } else if (cls->IsInterface()) {
1779 return TypeCheckKind::kInterfaceCheck;
1780 } else if (cls->IsArrayClass()) {
1781 if (cls->GetComponentType()->IsObjectClass()) {
1782 return TypeCheckKind::kArrayObjectCheck;
1783 } else if (cls->CannotBeAssignedFromOtherTypes()) {
1784 return TypeCheckKind::kExactCheck;
1785 } else {
1786 return TypeCheckKind::kArrayCheck;
1787 }
1788 } else if (cls->IsFinal()) {
1789 return TypeCheckKind::kExactCheck;
1790 } else if (cls->IsAbstract()) {
1791 return TypeCheckKind::kAbstractClassCheck;
1792 } else {
1793 return TypeCheckKind::kClassHierarchyCheck;
1794 }
1795}
1796
Vladimir Marko28e012a2017-12-07 11:22:59 +00001797void HInstructionBuilder::BuildLoadString(dex::StringIndex string_index, uint32_t dex_pc) {
1798 HLoadString* load_string =
1799 new (allocator_) HLoadString(graph_->GetCurrentMethod(), string_index, *dex_file_, dex_pc);
1800 HSharpening::ProcessLoadString(load_string,
1801 code_generator_,
1802 compiler_driver_,
1803 *dex_compilation_unit_,
1804 handles_);
1805 AppendInstruction(load_string);
1806}
1807
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001808HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001809 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001810 const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001811 Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001812 Handle<mirror::Class> klass = handles_->NewHandle(compiler_driver_->ResolveClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001813 soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00001814
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001815 bool needs_access_check = true;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001816 if (klass != nullptr) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001817 if (klass->IsPublic()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001818 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001819 } else {
Vladimir Marko28e012a2017-12-07 11:22:59 +00001820 ObjPtr<mirror::Class> compiling_class = GetCompilingClass();
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001821 if (compiling_class != nullptr && compiling_class->CanAccess(klass.Get())) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001822 needs_access_check = false;
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001823 }
1824 }
1825 }
1826
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001827 return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
1828}
1829
1830HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
1831 const DexFile& dex_file,
1832 Handle<mirror::Class> klass,
1833 uint32_t dex_pc,
1834 bool needs_access_check) {
1835 // Try to find a reference in the compiling dex file.
1836 const DexFile* actual_dex_file = &dex_file;
1837 if (!IsSameDexFile(dex_file, *dex_compilation_unit_->GetDexFile())) {
1838 dex::TypeIndex local_type_index =
1839 klass->FindTypeIndexInOtherDexFile(*dex_compilation_unit_->GetDexFile());
1840 if (local_type_index.IsValid()) {
1841 type_index = local_type_index;
1842 actual_dex_file = dex_compilation_unit_->GetDexFile();
1843 }
1844 }
1845
1846 // Note: `klass` must be from `handles_`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001847 HLoadClass* load_class = new (allocator_) HLoadClass(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001848 graph_->GetCurrentMethod(),
1849 type_index,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001850 *actual_dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001851 klass,
Andreas Gampefa4333d2017-02-14 11:10:34 -08001852 klass != nullptr && (klass.Get() == GetOutermostCompilingClass()),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001853 dex_pc,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001854 needs_access_check);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001855
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00001856 HLoadClass::LoadKind load_kind = HSharpening::ComputeLoadClassKind(load_class,
1857 code_generator_,
1858 compiler_driver_,
1859 *dex_compilation_unit_);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001860
1861 if (load_kind == HLoadClass::LoadKind::kInvalid) {
1862 // We actually cannot reference this class, we're forced to bail.
1863 return nullptr;
1864 }
Vladimir Marko28e012a2017-12-07 11:22:59 +00001865 // Load kind must be set before inserting the instruction into the graph.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001866 load_class->SetLoadKind(load_kind);
Vladimir Marko28e012a2017-12-07 11:22:59 +00001867 AppendInstruction(load_class);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001868 return load_class;
1869}
1870
David Brazdildee58d62016-04-07 09:54:26 +00001871void HInstructionBuilder::BuildTypeCheck(const Instruction& instruction,
1872 uint8_t destination,
1873 uint8_t reference,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001874 dex::TypeIndex type_index,
David Brazdildee58d62016-04-07 09:54:26 +00001875 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 HInstruction* object = LoadLocal(reference, DataType::Type::kReference);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001877 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00001878
Nicolas Geoffray5247c082017-01-13 14:17:29 +00001879 ScopedObjectAccess soa(Thread::Current());
1880 TypeCheckKind check_kind = ComputeTypeCheckKind(cls->GetClass());
David Brazdildee58d62016-04-07 09:54:26 +00001881 if (instruction.Opcode() == Instruction::INSTANCE_OF) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001882 AppendInstruction(new (allocator_) HInstanceOf(object, cls, check_kind, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001883 UpdateLocal(destination, current_block_->GetLastInstruction());
1884 } else {
1885 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST);
1886 // We emit a CheckCast followed by a BoundType. CheckCast is a statement
1887 // which may throw. If it succeeds BoundType sets the new type of `object`
1888 // for all subsequent uses.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001889 AppendInstruction(new (allocator_) HCheckCast(object, cls, check_kind, dex_pc));
1890 AppendInstruction(new (allocator_) HBoundType(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00001891 UpdateLocal(reference, current_block_->GetLastInstruction());
1892 }
1893}
1894
Vladimir Marko0b66d612017-03-13 14:50:04 +00001895bool HInstructionBuilder::NeedsAccessCheck(dex::TypeIndex type_index, bool* finalizable) const {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001896 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
1897 LookupReferrerClass(), LookupResolvedType(type_index, *dex_compilation_unit_), finalizable);
David Brazdildee58d62016-04-07 09:54:26 +00001898}
1899
1900bool HInstructionBuilder::CanDecodeQuickenedInfo() const {
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001901 return !quicken_info_.IsNull();
David Brazdildee58d62016-04-07 09:54:26 +00001902}
1903
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001904uint16_t HInstructionBuilder::LookupQuickenedInfo(uint32_t quicken_index) {
1905 DCHECK(CanDecodeQuickenedInfo());
1906 return quicken_info_.GetData(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00001907}
1908
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07001909bool HInstructionBuilder::ProcessDexInstruction(const Instruction& instruction,
1910 uint32_t dex_pc,
1911 size_t quicken_index) {
David Brazdildee58d62016-04-07 09:54:26 +00001912 switch (instruction.Opcode()) {
1913 case Instruction::CONST_4: {
1914 int32_t register_index = instruction.VRegA();
1915 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n(), dex_pc);
1916 UpdateLocal(register_index, constant);
1917 break;
1918 }
1919
1920 case Instruction::CONST_16: {
1921 int32_t register_index = instruction.VRegA();
1922 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s(), dex_pc);
1923 UpdateLocal(register_index, constant);
1924 break;
1925 }
1926
1927 case Instruction::CONST: {
1928 int32_t register_index = instruction.VRegA();
1929 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i(), dex_pc);
1930 UpdateLocal(register_index, constant);
1931 break;
1932 }
1933
1934 case Instruction::CONST_HIGH16: {
1935 int32_t register_index = instruction.VRegA();
1936 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16, dex_pc);
1937 UpdateLocal(register_index, constant);
1938 break;
1939 }
1940
1941 case Instruction::CONST_WIDE_16: {
1942 int32_t register_index = instruction.VRegA();
1943 // Get 16 bits of constant value, sign extended to 64 bits.
1944 int64_t value = instruction.VRegB_21s();
1945 value <<= 48;
1946 value >>= 48;
1947 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1948 UpdateLocal(register_index, constant);
1949 break;
1950 }
1951
1952 case Instruction::CONST_WIDE_32: {
1953 int32_t register_index = instruction.VRegA();
1954 // Get 32 bits of constant value, sign extended to 64 bits.
1955 int64_t value = instruction.VRegB_31i();
1956 value <<= 32;
1957 value >>= 32;
1958 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1959 UpdateLocal(register_index, constant);
1960 break;
1961 }
1962
1963 case Instruction::CONST_WIDE: {
1964 int32_t register_index = instruction.VRegA();
1965 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l(), dex_pc);
1966 UpdateLocal(register_index, constant);
1967 break;
1968 }
1969
1970 case Instruction::CONST_WIDE_HIGH16: {
1971 int32_t register_index = instruction.VRegA();
1972 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
1973 HLongConstant* constant = graph_->GetLongConstant(value, dex_pc);
1974 UpdateLocal(register_index, constant);
1975 break;
1976 }
1977
1978 // Note that the SSA building will refine the types.
1979 case Instruction::MOVE:
1980 case Instruction::MOVE_FROM16:
1981 case Instruction::MOVE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt32);
David Brazdildee58d62016-04-07 09:54:26 +00001983 UpdateLocal(instruction.VRegA(), value);
1984 break;
1985 }
1986
1987 // Note that the SSA building will refine the types.
1988 case Instruction::MOVE_WIDE:
1989 case Instruction::MOVE_WIDE_FROM16:
1990 case Instruction::MOVE_WIDE_16: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 HInstruction* value = LoadLocal(instruction.VRegB(), DataType::Type::kInt64);
David Brazdildee58d62016-04-07 09:54:26 +00001992 UpdateLocal(instruction.VRegA(), value);
1993 break;
1994 }
1995
1996 case Instruction::MOVE_OBJECT:
1997 case Instruction::MOVE_OBJECT_16:
1998 case Instruction::MOVE_OBJECT_FROM16: {
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01001999 // The verifier has no notion of a null type, so a move-object of constant 0
2000 // will lead to the same constant 0 in the destination register. To mimic
2001 // this behavior, we just pretend we haven't seen a type change (int to reference)
2002 // for the 0 constant and phis. We rely on our type propagation to eventually get the
2003 // types correct.
2004 uint32_t reg_number = instruction.VRegB();
2005 HInstruction* value = (*current_locals_)[reg_number];
2006 if (value->IsIntConstant()) {
2007 DCHECK_EQ(value->AsIntConstant()->GetValue(), 0);
2008 } else if (value->IsPhi()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 DCHECK(value->GetType() == DataType::Type::kInt32 ||
2010 value->GetType() == DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002011 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002012 value = LoadLocal(reg_number, DataType::Type::kReference);
Nicolas Geoffray50a9ed02016-09-23 15:40:41 +01002013 }
David Brazdildee58d62016-04-07 09:54:26 +00002014 UpdateLocal(instruction.VRegA(), value);
2015 break;
2016 }
2017
2018 case Instruction::RETURN_VOID_NO_BARRIER:
2019 case Instruction::RETURN_VOID: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 BuildReturn(instruction, DataType::Type::kVoid, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002021 break;
2022 }
2023
2024#define IF_XX(comparison, cond) \
2025 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \
2026 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break
2027
2028 IF_XX(HEqual, EQ);
2029 IF_XX(HNotEqual, NE);
2030 IF_XX(HLessThan, LT);
2031 IF_XX(HLessThanOrEqual, LE);
2032 IF_XX(HGreaterThan, GT);
2033 IF_XX(HGreaterThanOrEqual, GE);
2034
2035 case Instruction::GOTO:
2036 case Instruction::GOTO_16:
2037 case Instruction::GOTO_32: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002038 AppendInstruction(new (allocator_) HGoto(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002039 current_block_ = nullptr;
2040 break;
2041 }
2042
2043 case Instruction::RETURN: {
2044 BuildReturn(instruction, return_type_, dex_pc);
2045 break;
2046 }
2047
2048 case Instruction::RETURN_OBJECT: {
2049 BuildReturn(instruction, return_type_, dex_pc);
2050 break;
2051 }
2052
2053 case Instruction::RETURN_WIDE: {
2054 BuildReturn(instruction, return_type_, dex_pc);
2055 break;
2056 }
2057
2058 case Instruction::INVOKE_DIRECT:
2059 case Instruction::INVOKE_INTERFACE:
2060 case Instruction::INVOKE_STATIC:
2061 case Instruction::INVOKE_SUPER:
2062 case Instruction::INVOKE_VIRTUAL:
2063 case Instruction::INVOKE_VIRTUAL_QUICK: {
2064 uint16_t method_idx;
2065 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_QUICK) {
2066 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002067 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2068 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002069 return false;
2070 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002071 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002072 } else {
2073 method_idx = instruction.VRegB_35c();
2074 }
2075 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
2076 uint32_t args[5];
2077 instruction.GetVarArgs(args);
2078 if (!BuildInvoke(instruction, dex_pc, method_idx,
2079 number_of_vreg_arguments, false, args, -1)) {
2080 return false;
2081 }
2082 break;
2083 }
2084
2085 case Instruction::INVOKE_DIRECT_RANGE:
2086 case Instruction::INVOKE_INTERFACE_RANGE:
2087 case Instruction::INVOKE_STATIC_RANGE:
2088 case Instruction::INVOKE_SUPER_RANGE:
2089 case Instruction::INVOKE_VIRTUAL_RANGE:
2090 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2091 uint16_t method_idx;
2092 if (instruction.Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK) {
2093 if (!CanDecodeQuickenedInfo()) {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002094 VLOG(compiler) << "Not compiled: Could not decode quickened instruction "
2095 << instruction.Opcode();
David Brazdildee58d62016-04-07 09:54:26 +00002096 return false;
2097 }
Mathieu Chartierde4b08f2017-07-10 14:13:41 -07002098 method_idx = LookupQuickenedInfo(quicken_index);
David Brazdildee58d62016-04-07 09:54:26 +00002099 } else {
2100 method_idx = instruction.VRegB_3rc();
2101 }
2102 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
2103 uint32_t register_index = instruction.VRegC();
2104 if (!BuildInvoke(instruction, dex_pc, method_idx,
2105 number_of_vreg_arguments, true, nullptr, register_index)) {
2106 return false;
2107 }
2108 break;
2109 }
2110
Orion Hodsonac141392017-01-13 11:53:47 +00002111 case Instruction::INVOKE_POLYMORPHIC: {
2112 uint16_t method_idx = instruction.VRegB_45cc();
2113 uint16_t proto_idx = instruction.VRegH_45cc();
2114 uint32_t number_of_vreg_arguments = instruction.VRegA_45cc();
2115 uint32_t args[5];
2116 instruction.GetVarArgs(args);
2117 return BuildInvokePolymorphic(instruction,
2118 dex_pc,
2119 method_idx,
2120 proto_idx,
2121 number_of_vreg_arguments,
2122 false,
2123 args,
2124 -1);
2125 }
2126
2127 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
2128 uint16_t method_idx = instruction.VRegB_4rcc();
2129 uint16_t proto_idx = instruction.VRegH_4rcc();
2130 uint32_t number_of_vreg_arguments = instruction.VRegA_4rcc();
2131 uint32_t register_index = instruction.VRegC_4rcc();
2132 return BuildInvokePolymorphic(instruction,
2133 dex_pc,
2134 method_idx,
2135 proto_idx,
2136 number_of_vreg_arguments,
2137 true,
2138 nullptr,
2139 register_index);
2140 }
2141
David Brazdildee58d62016-04-07 09:54:26 +00002142 case Instruction::NEG_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 Unop_12x<HNeg>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002144 break;
2145 }
2146
2147 case Instruction::NEG_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 Unop_12x<HNeg>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002149 break;
2150 }
2151
2152 case Instruction::NEG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 Unop_12x<HNeg>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002154 break;
2155 }
2156
2157 case Instruction::NEG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002158 Unop_12x<HNeg>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002159 break;
2160 }
2161
2162 case Instruction::NOT_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 Unop_12x<HNot>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002164 break;
2165 }
2166
2167 case Instruction::NOT_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002168 Unop_12x<HNot>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002169 break;
2170 }
2171
2172 case Instruction::INT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002174 break;
2175 }
2176
2177 case Instruction::INT_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002179 break;
2180 }
2181
2182 case Instruction::INT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002184 break;
2185 }
2186
2187 case Instruction::LONG_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002189 break;
2190 }
2191
2192 case Instruction::LONG_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002194 break;
2195 }
2196
2197 case Instruction::LONG_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 Conversion_12x(instruction, DataType::Type::kInt64, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002199 break;
2200 }
2201
2202 case Instruction::FLOAT_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002204 break;
2205 }
2206
2207 case Instruction::FLOAT_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002209 break;
2210 }
2211
2212 case Instruction::FLOAT_TO_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 Conversion_12x(instruction, DataType::Type::kFloat32, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002214 break;
2215 }
2216
2217 case Instruction::DOUBLE_TO_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002219 break;
2220 }
2221
2222 case Instruction::DOUBLE_TO_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002224 break;
2225 }
2226
2227 case Instruction::DOUBLE_TO_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 Conversion_12x(instruction, DataType::Type::kFloat64, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002229 break;
2230 }
2231
2232 case Instruction::INT_TO_BYTE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt8, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002234 break;
2235 }
2236
2237 case Instruction::INT_TO_SHORT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kInt16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002239 break;
2240 }
2241
2242 case Instruction::INT_TO_CHAR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 Conversion_12x(instruction, DataType::Type::kInt32, DataType::Type::kUint16, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002244 break;
2245 }
2246
2247 case Instruction::ADD_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002248 Binop_23x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002249 break;
2250 }
2251
2252 case Instruction::ADD_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 Binop_23x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002254 break;
2255 }
2256
2257 case Instruction::ADD_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 Binop_23x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002259 break;
2260 }
2261
2262 case Instruction::ADD_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 Binop_23x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002264 break;
2265 }
2266
2267 case Instruction::SUB_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 Binop_23x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002269 break;
2270 }
2271
2272 case Instruction::SUB_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 Binop_23x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002274 break;
2275 }
2276
2277 case Instruction::SUB_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 Binop_23x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002279 break;
2280 }
2281
2282 case Instruction::SUB_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 Binop_23x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002284 break;
2285 }
2286
2287 case Instruction::ADD_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 Binop_12x<HAdd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002289 break;
2290 }
2291
2292 case Instruction::MUL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 Binop_23x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002294 break;
2295 }
2296
2297 case Instruction::MUL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 Binop_23x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002299 break;
2300 }
2301
2302 case Instruction::MUL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 Binop_23x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002304 break;
2305 }
2306
2307 case Instruction::MUL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 Binop_23x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002309 break;
2310 }
2311
2312 case Instruction::DIV_INT: {
2313 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002315 break;
2316 }
2317
2318 case Instruction::DIV_LONG: {
2319 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002321 break;
2322 }
2323
2324 case Instruction::DIV_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325 Binop_23x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002326 break;
2327 }
2328
2329 case Instruction::DIV_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002330 Binop_23x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002331 break;
2332 }
2333
2334 case Instruction::REM_INT: {
2335 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002337 break;
2338 }
2339
2340 case Instruction::REM_LONG: {
2341 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002343 break;
2344 }
2345
2346 case Instruction::REM_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 Binop_23x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002348 break;
2349 }
2350
2351 case Instruction::REM_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 Binop_23x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002353 break;
2354 }
2355
2356 case Instruction::AND_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 Binop_23x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002358 break;
2359 }
2360
2361 case Instruction::AND_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 Binop_23x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002363 break;
2364 }
2365
2366 case Instruction::SHL_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002368 break;
2369 }
2370
2371 case Instruction::SHL_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 Binop_23x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002373 break;
2374 }
2375
2376 case Instruction::SHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002378 break;
2379 }
2380
2381 case Instruction::SHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002382 Binop_23x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002383 break;
2384 }
2385
2386 case Instruction::USHR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002388 break;
2389 }
2390
2391 case Instruction::USHR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002392 Binop_23x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002393 break;
2394 }
2395
2396 case Instruction::OR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 Binop_23x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002398 break;
2399 }
2400
2401 case Instruction::OR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 Binop_23x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002403 break;
2404 }
2405
2406 case Instruction::XOR_INT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 Binop_23x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002408 break;
2409 }
2410
2411 case Instruction::XOR_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 Binop_23x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002413 break;
2414 }
2415
2416 case Instruction::ADD_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 Binop_12x<HAdd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002418 break;
2419 }
2420
2421 case Instruction::ADD_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 Binop_12x<HAdd>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002423 break;
2424 }
2425
2426 case Instruction::ADD_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 Binop_12x<HAdd>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002428 break;
2429 }
2430
2431 case Instruction::SUB_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 Binop_12x<HSub>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002433 break;
2434 }
2435
2436 case Instruction::SUB_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 Binop_12x<HSub>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002438 break;
2439 }
2440
2441 case Instruction::SUB_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 Binop_12x<HSub>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002443 break;
2444 }
2445
2446 case Instruction::SUB_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002447 Binop_12x<HSub>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002448 break;
2449 }
2450
2451 case Instruction::MUL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 Binop_12x<HMul>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002453 break;
2454 }
2455
2456 case Instruction::MUL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 Binop_12x<HMul>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002458 break;
2459 }
2460
2461 case Instruction::MUL_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 Binop_12x<HMul>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002463 break;
2464 }
2465
2466 case Instruction::MUL_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 Binop_12x<HMul>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002468 break;
2469 }
2470
2471 case Instruction::DIV_INT_2ADDR: {
2472 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 dex_pc, DataType::Type::kInt32, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002474 break;
2475 }
2476
2477 case Instruction::DIV_LONG_2ADDR: {
2478 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 dex_pc, DataType::Type::kInt64, false, true);
David Brazdildee58d62016-04-07 09:54:26 +00002480 break;
2481 }
2482
2483 case Instruction::REM_INT_2ADDR: {
2484 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485 dex_pc, DataType::Type::kInt32, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002486 break;
2487 }
2488
2489 case Instruction::REM_LONG_2ADDR: {
2490 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 dex_pc, DataType::Type::kInt64, false, false);
David Brazdildee58d62016-04-07 09:54:26 +00002492 break;
2493 }
2494
2495 case Instruction::REM_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 Binop_12x<HRem>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002497 break;
2498 }
2499
2500 case Instruction::REM_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 Binop_12x<HRem>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002502 break;
2503 }
2504
2505 case Instruction::SHL_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002507 break;
2508 }
2509
2510 case Instruction::SHL_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 Binop_12x_shift<HShl>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002512 break;
2513 }
2514
2515 case Instruction::SHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002517 break;
2518 }
2519
2520 case Instruction::SHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 Binop_12x_shift<HShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002522 break;
2523 }
2524
2525 case Instruction::USHR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002527 break;
2528 }
2529
2530 case Instruction::USHR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 Binop_12x_shift<HUShr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002532 break;
2533 }
2534
2535 case Instruction::DIV_FLOAT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 Binop_12x<HDiv>(instruction, DataType::Type::kFloat32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002537 break;
2538 }
2539
2540 case Instruction::DIV_DOUBLE_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 Binop_12x<HDiv>(instruction, DataType::Type::kFloat64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002542 break;
2543 }
2544
2545 case Instruction::AND_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 Binop_12x<HAnd>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002547 break;
2548 }
2549
2550 case Instruction::AND_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 Binop_12x<HAnd>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002552 break;
2553 }
2554
2555 case Instruction::OR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 Binop_12x<HOr>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002557 break;
2558 }
2559
2560 case Instruction::OR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 Binop_12x<HOr>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002562 break;
2563 }
2564
2565 case Instruction::XOR_INT_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 Binop_12x<HXor>(instruction, DataType::Type::kInt32, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002567 break;
2568 }
2569
2570 case Instruction::XOR_LONG_2ADDR: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 Binop_12x<HXor>(instruction, DataType::Type::kInt64, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002572 break;
2573 }
2574
2575 case Instruction::ADD_INT_LIT16: {
2576 Binop_22s<HAdd>(instruction, false, dex_pc);
2577 break;
2578 }
2579
2580 case Instruction::AND_INT_LIT16: {
2581 Binop_22s<HAnd>(instruction, false, dex_pc);
2582 break;
2583 }
2584
2585 case Instruction::OR_INT_LIT16: {
2586 Binop_22s<HOr>(instruction, false, dex_pc);
2587 break;
2588 }
2589
2590 case Instruction::XOR_INT_LIT16: {
2591 Binop_22s<HXor>(instruction, false, dex_pc);
2592 break;
2593 }
2594
2595 case Instruction::RSUB_INT: {
2596 Binop_22s<HSub>(instruction, true, dex_pc);
2597 break;
2598 }
2599
2600 case Instruction::MUL_INT_LIT16: {
2601 Binop_22s<HMul>(instruction, false, dex_pc);
2602 break;
2603 }
2604
2605 case Instruction::ADD_INT_LIT8: {
2606 Binop_22b<HAdd>(instruction, false, dex_pc);
2607 break;
2608 }
2609
2610 case Instruction::AND_INT_LIT8: {
2611 Binop_22b<HAnd>(instruction, false, dex_pc);
2612 break;
2613 }
2614
2615 case Instruction::OR_INT_LIT8: {
2616 Binop_22b<HOr>(instruction, false, dex_pc);
2617 break;
2618 }
2619
2620 case Instruction::XOR_INT_LIT8: {
2621 Binop_22b<HXor>(instruction, false, dex_pc);
2622 break;
2623 }
2624
2625 case Instruction::RSUB_INT_LIT8: {
2626 Binop_22b<HSub>(instruction, true, dex_pc);
2627 break;
2628 }
2629
2630 case Instruction::MUL_INT_LIT8: {
2631 Binop_22b<HMul>(instruction, false, dex_pc);
2632 break;
2633 }
2634
2635 case Instruction::DIV_INT_LIT16:
2636 case Instruction::DIV_INT_LIT8: {
2637 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002638 dex_pc, DataType::Type::kInt32, true, true);
David Brazdildee58d62016-04-07 09:54:26 +00002639 break;
2640 }
2641
2642 case Instruction::REM_INT_LIT16:
2643 case Instruction::REM_INT_LIT8: {
2644 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 dex_pc, DataType::Type::kInt32, true, false);
David Brazdildee58d62016-04-07 09:54:26 +00002646 break;
2647 }
2648
2649 case Instruction::SHL_INT_LIT8: {
2650 Binop_22b<HShl>(instruction, false, dex_pc);
2651 break;
2652 }
2653
2654 case Instruction::SHR_INT_LIT8: {
2655 Binop_22b<HShr>(instruction, false, dex_pc);
2656 break;
2657 }
2658
2659 case Instruction::USHR_INT_LIT8: {
2660 Binop_22b<HUShr>(instruction, false, dex_pc);
2661 break;
2662 }
2663
2664 case Instruction::NEW_INSTANCE: {
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002665 HNewInstance* new_instance =
2666 BuildNewInstance(dex::TypeIndex(instruction.VRegB_21c()), dex_pc);
2667 DCHECK(new_instance != nullptr);
2668
David Brazdildee58d62016-04-07 09:54:26 +00002669 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002670 BuildConstructorFenceForAllocation(new_instance);
David Brazdildee58d62016-04-07 09:54:26 +00002671 break;
2672 }
2673
2674 case Instruction::NEW_ARRAY: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002675 dex::TypeIndex type_index(instruction.VRegC_22c());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 HInstruction* length = LoadLocal(instruction.VRegB_22c(), DataType::Type::kInt32);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002677 HLoadClass* cls = BuildLoadClass(type_index, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002678
Vladimir Markoca6fff82017-10-03 14:49:14 +01002679 HNewArray* new_array = new (allocator_) HNewArray(cls, length, dex_pc);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002680 AppendInstruction(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002681 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002682 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002683 break;
2684 }
2685
2686 case Instruction::FILLED_NEW_ARRAY: {
2687 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002688 dex::TypeIndex type_index(instruction.VRegB_35c());
David Brazdildee58d62016-04-07 09:54:26 +00002689 uint32_t args[5];
2690 instruction.GetVarArgs(args);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002691 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2692 type_index,
2693 number_of_vreg_arguments,
2694 /* is_range */ false,
2695 args,
2696 /* register_index */ 0);
2697 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002698 break;
2699 }
2700
2701 case Instruction::FILLED_NEW_ARRAY_RANGE: {
2702 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002703 dex::TypeIndex type_index(instruction.VRegB_3rc());
David Brazdildee58d62016-04-07 09:54:26 +00002704 uint32_t register_index = instruction.VRegC_3rc();
Igor Murashkin79d8fa72017-04-18 09:37:23 -07002705 HNewArray* new_array = BuildFilledNewArray(dex_pc,
2706 type_index,
2707 number_of_vreg_arguments,
2708 /* is_range */ true,
2709 /* args*/ nullptr,
2710 register_index);
2711 BuildConstructorFenceForAllocation(new_array);
David Brazdildee58d62016-04-07 09:54:26 +00002712 break;
2713 }
2714
2715 case Instruction::FILL_ARRAY_DATA: {
2716 BuildFillArrayData(instruction, dex_pc);
2717 break;
2718 }
2719
2720 case Instruction::MOVE_RESULT:
2721 case Instruction::MOVE_RESULT_WIDE:
2722 case Instruction::MOVE_RESULT_OBJECT: {
2723 DCHECK(latest_result_ != nullptr);
2724 UpdateLocal(instruction.VRegA(), latest_result_);
2725 latest_result_ = nullptr;
2726 break;
2727 }
2728
2729 case Instruction::CMP_LONG: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 Binop_23x_cmp(instruction, DataType::Type::kInt64, ComparisonBias::kNoBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002731 break;
2732 }
2733
2734 case Instruction::CMPG_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002736 break;
2737 }
2738
2739 case Instruction::CMPG_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kGtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002741 break;
2742 }
2743
2744 case Instruction::CMPL_FLOAT: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 Binop_23x_cmp(instruction, DataType::Type::kFloat32, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002746 break;
2747 }
2748
2749 case Instruction::CMPL_DOUBLE: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002750 Binop_23x_cmp(instruction, DataType::Type::kFloat64, ComparisonBias::kLtBias, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002751 break;
2752 }
2753
2754 case Instruction::NOP:
2755 break;
2756
2757 case Instruction::IGET:
2758 case Instruction::IGET_QUICK:
2759 case Instruction::IGET_WIDE:
2760 case Instruction::IGET_WIDE_QUICK:
2761 case Instruction::IGET_OBJECT:
2762 case Instruction::IGET_OBJECT_QUICK:
2763 case Instruction::IGET_BOOLEAN:
2764 case Instruction::IGET_BOOLEAN_QUICK:
2765 case Instruction::IGET_BYTE:
2766 case Instruction::IGET_BYTE_QUICK:
2767 case Instruction::IGET_CHAR:
2768 case Instruction::IGET_CHAR_QUICK:
2769 case Instruction::IGET_SHORT:
2770 case Instruction::IGET_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002771 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ false, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002772 return false;
2773 }
2774 break;
2775 }
2776
2777 case Instruction::IPUT:
2778 case Instruction::IPUT_QUICK:
2779 case Instruction::IPUT_WIDE:
2780 case Instruction::IPUT_WIDE_QUICK:
2781 case Instruction::IPUT_OBJECT:
2782 case Instruction::IPUT_OBJECT_QUICK:
2783 case Instruction::IPUT_BOOLEAN:
2784 case Instruction::IPUT_BOOLEAN_QUICK:
2785 case Instruction::IPUT_BYTE:
2786 case Instruction::IPUT_BYTE_QUICK:
2787 case Instruction::IPUT_CHAR:
2788 case Instruction::IPUT_CHAR_QUICK:
2789 case Instruction::IPUT_SHORT:
2790 case Instruction::IPUT_SHORT_QUICK: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002791 if (!BuildInstanceFieldAccess(instruction, dex_pc, /* is_put */ true, quicken_index)) {
David Brazdildee58d62016-04-07 09:54:26 +00002792 return false;
2793 }
2794 break;
2795 }
2796
2797 case Instruction::SGET:
2798 case Instruction::SGET_WIDE:
2799 case Instruction::SGET_OBJECT:
2800 case Instruction::SGET_BOOLEAN:
2801 case Instruction::SGET_BYTE:
2802 case Instruction::SGET_CHAR:
2803 case Instruction::SGET_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002804 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ false);
David Brazdildee58d62016-04-07 09:54:26 +00002805 break;
2806 }
2807
2808 case Instruction::SPUT:
2809 case Instruction::SPUT_WIDE:
2810 case Instruction::SPUT_OBJECT:
2811 case Instruction::SPUT_BOOLEAN:
2812 case Instruction::SPUT_BYTE:
2813 case Instruction::SPUT_CHAR:
2814 case Instruction::SPUT_SHORT: {
Nicolas Geoffraydbb9aef2017-11-23 10:44:11 +00002815 BuildStaticFieldAccess(instruction, dex_pc, /* is_put */ true);
David Brazdildee58d62016-04-07 09:54:26 +00002816 break;
2817 }
2818
2819#define ARRAY_XX(kind, anticipated_type) \
2820 case Instruction::AGET##kind: { \
2821 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \
2822 break; \
2823 } \
2824 case Instruction::APUT##kind: { \
2825 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \
2826 break; \
2827 }
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 ARRAY_XX(, DataType::Type::kInt32);
2830 ARRAY_XX(_WIDE, DataType::Type::kInt64);
2831 ARRAY_XX(_OBJECT, DataType::Type::kReference);
2832 ARRAY_XX(_BOOLEAN, DataType::Type::kBool);
2833 ARRAY_XX(_BYTE, DataType::Type::kInt8);
2834 ARRAY_XX(_CHAR, DataType::Type::kUint16);
2835 ARRAY_XX(_SHORT, DataType::Type::kInt16);
David Brazdildee58d62016-04-07 09:54:26 +00002836
2837 case Instruction::ARRAY_LENGTH: {
David Brazdilc120bbe2016-04-22 16:57:00 +01002838 HInstruction* object = LoadNullCheckedLocal(instruction.VRegB_12x(), dex_pc);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002839 AppendInstruction(new (allocator_) HArrayLength(object, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002840 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
2841 break;
2842 }
2843
2844 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002845 dex::StringIndex string_index(instruction.VRegB_21c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002846 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002847 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2848 break;
2849 }
2850
2851 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08002852 dex::StringIndex string_index(instruction.VRegB_31c());
Vladimir Marko28e012a2017-12-07 11:22:59 +00002853 BuildLoadString(string_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002854 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
2855 break;
2856 }
2857
2858 case Instruction::CONST_CLASS: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002859 dex::TypeIndex type_index(instruction.VRegB_21c());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00002860 BuildLoadClass(type_index, dex_pc);
David Brazdildee58d62016-04-07 09:54:26 +00002861 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
2862 break;
2863 }
2864
2865 case Instruction::MOVE_EXCEPTION: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002866 AppendInstruction(new (allocator_) HLoadException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002867 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +01002868 AppendInstruction(new (allocator_) HClearException(dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002869 break;
2870 }
2871
2872 case Instruction::THROW: {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002874 AppendInstruction(new (allocator_) HThrow(exception, dex_pc));
David Brazdildee58d62016-04-07 09:54:26 +00002875 // We finished building this block. Set the current block to null to avoid
2876 // adding dead instructions to it.
2877 current_block_ = nullptr;
2878 break;
2879 }
2880
2881 case Instruction::INSTANCE_OF: {
2882 uint8_t destination = instruction.VRegA_22c();
2883 uint8_t reference = instruction.VRegB_22c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002884 dex::TypeIndex type_index(instruction.VRegC_22c());
David Brazdildee58d62016-04-07 09:54:26 +00002885 BuildTypeCheck(instruction, destination, reference, type_index, dex_pc);
2886 break;
2887 }
2888
2889 case Instruction::CHECK_CAST: {
2890 uint8_t reference = instruction.VRegA_21c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08002891 dex::TypeIndex type_index(instruction.VRegB_21c());
David Brazdildee58d62016-04-07 09:54:26 +00002892 BuildTypeCheck(instruction, -1, reference, type_index, dex_pc);
2893 break;
2894 }
2895
2896 case Instruction::MONITOR_ENTER: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002897 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002899 HMonitorOperation::OperationKind::kEnter,
2900 dex_pc));
2901 break;
2902 }
2903
2904 case Instruction::MONITOR_EXIT: {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002905 AppendInstruction(new (allocator_) HMonitorOperation(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 LoadLocal(instruction.VRegA_11x(), DataType::Type::kReference),
David Brazdildee58d62016-04-07 09:54:26 +00002907 HMonitorOperation::OperationKind::kExit,
2908 dex_pc));
2909 break;
2910 }
2911
2912 case Instruction::SPARSE_SWITCH:
2913 case Instruction::PACKED_SWITCH: {
2914 BuildSwitch(instruction, dex_pc);
2915 break;
2916 }
2917
2918 default:
2919 VLOG(compiler) << "Did not compile "
David Sehr709b0702016-10-13 09:12:37 -07002920 << dex_file_->PrettyMethod(dex_compilation_unit_->GetDexMethodIndex())
David Brazdildee58d62016-04-07 09:54:26 +00002921 << " because of unhandled instruction "
2922 << instruction.Name();
Igor Murashkin1e065a52017-08-09 13:20:34 -07002923 MaybeRecordStat(compilation_stats_,
2924 MethodCompilationStat::kNotCompiledUnhandledInstruction);
David Brazdildee58d62016-04-07 09:54:26 +00002925 return false;
2926 }
2927 return true;
2928} // NOLINT(readability/fn_size)
2929
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002930ObjPtr<mirror::Class> HInstructionBuilder::LookupResolvedType(
2931 dex::TypeIndex type_index,
2932 const DexCompilationUnit& compilation_unit) const {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002933 return compilation_unit.GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002934 type_index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get());
2935}
2936
2937ObjPtr<mirror::Class> HInstructionBuilder::LookupReferrerClass() const {
2938 // TODO: Cache the result in a Handle<mirror::Class>.
2939 const DexFile::MethodId& method_id =
2940 dex_compilation_unit_->GetDexFile()->GetMethodId(dex_compilation_unit_->GetDexMethodIndex());
2941 return LookupResolvedType(method_id.class_idx_, *dex_compilation_unit_);
2942}
2943
David Brazdildee58d62016-04-07 09:54:26 +00002944} // namespace art